FFmpeg  2.1.1
dsicinav.c
Go to the documentation of this file.
1 /*
2  * Delphine Software International CIN Audio/Video Decoders
3  * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Delphine Software International CIN audio/video decoders
25  */
26 
27 #include "libavutil/channel_layout.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31 #include "mathops.h"
32 
33 
34 typedef enum CinVideoBitmapIndex {
35  CIN_CUR_BMP = 0, /* current */
36  CIN_PRE_BMP = 1, /* previous */
37  CIN_INT_BMP = 2 /* intermediate */
39 
40 typedef struct CinVideoContext {
43  unsigned int bitmap_size;
44  uint32_t palette[256];
47 
48 typedef struct CinAudioContext {
50  int delta;
52 
53 
54 /* table defining a geometric sequence with multiplier = 32767 ^ (1 / 128) */
55 static const int16_t cinaudio_delta16_table[256] = {
56  0, 0, 0, 0, 0, 0, 0, 0,
57  0, 0, 0, 0, 0, 0, 0, 0,
58  0, 0, 0, -30210, -27853, -25680, -23677, -21829,
59  -20126, -18556, -17108, -15774, -14543, -13408, -12362, -11398,
60  -10508, -9689, -8933, -8236, -7593, -7001, -6455, -5951,
61  -5487, -5059, -4664, -4300, -3964, -3655, -3370, -3107,
62  -2865, -2641, -2435, -2245, -2070, -1908, -1759, -1622,
63  -1495, -1379, -1271, -1172, -1080, -996, -918, -847,
64  -781, -720, -663, -612, -564, -520, -479, -442,
65  -407, -376, -346, -319, -294, -271, -250, -230,
66  -212, -196, -181, -166, -153, -141, -130, -120,
67  -111, -102, -94, -87, -80, -74, -68, -62,
68  -58, -53, -49, -45, -41, -38, -35, -32,
69  -30, -27, -25, -23, -21, -20, -18, -17,
70  -15, -14, -13, -12, -11, -10, -9, -8,
71  -7, -6, -5, -4, -3, -2, -1, 0,
72  0, 1, 2, 3, 4, 5, 6, 7,
73  8, 9, 10, 11, 12, 13, 14, 15,
74  17, 18, 20, 21, 23, 25, 27, 30,
75  32, 35, 38, 41, 45, 49, 53, 58,
76  62, 68, 74, 80, 87, 94, 102, 111,
77  120, 130, 141, 153, 166, 181, 196, 212,
78  230, 250, 271, 294, 319, 346, 376, 407,
79  442, 479, 520, 564, 612, 663, 720, 781,
80  847, 918, 996, 1080, 1172, 1271, 1379, 1495,
81  1622, 1759, 1908, 2070, 2245, 2435, 2641, 2865,
82  3107, 3370, 3655, 3964, 4300, 4664, 5059, 5487,
83  5951, 6455, 7001, 7593, 8236, 8933, 9689, 10508,
84  11398, 12362, 13408, 14543, 15774, 17108, 18556, 20126,
85  21829, 23677, 25680, 27853, 30210, 0, 0, 0,
86  0, 0, 0, 0, 0, 0, 0, 0,
87  0, 0, 0, 0, 0, 0, 0, 0
88 };
89 
91 {
92  int i;
93 
94  for (i = 0; i < 3; ++i)
95  av_freep(&cin->bitmap_table[i]);
96 }
97 
99 {
100  int i;
101 
102  for (i = 0; i < 3; ++i) {
103  cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
104  if (!cin->bitmap_table[i]) {
105  av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
106  destroy_buffers(cin);
107  return AVERROR(ENOMEM);
108  }
109  }
110 
111  return 0;
112 }
113 
115 {
116  CinVideoContext *cin = avctx->priv_data;
117 
118  cin->avctx = avctx;
119  avctx->pix_fmt = AV_PIX_FMT_PAL8;
120 
122 
123  cin->bitmap_size = avctx->width * avctx->height;
124  if (allocate_buffers(cin))
125  return AVERROR(ENOMEM);
126 
127  return 0;
128 }
129 
130 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
131  int size)
132 {
133  while (size--)
134  *dst++ += *src++;
135 }
136 
137 static int cin_decode_huffman(const unsigned char *src, int src_size,
138  unsigned char *dst, int dst_size)
139 {
140  int b, huff_code = 0;
141  unsigned char huff_code_table[15];
142  unsigned char *dst_cur = dst;
143  unsigned char *dst_end = dst + dst_size;
144  const unsigned char *src_end = src + src_size;
145 
146  memcpy(huff_code_table, src, 15);
147  src += 15;
148 
149  while (src < src_end) {
150  huff_code = *src++;
151  if ((huff_code >> 4) == 15) {
152  b = huff_code << 4;
153  huff_code = *src++;
154  *dst_cur++ = b | (huff_code >> 4);
155  } else
156  *dst_cur++ = huff_code_table[huff_code >> 4];
157  if (dst_cur >= dst_end)
158  break;
159 
160  huff_code &= 15;
161  if (huff_code == 15) {
162  *dst_cur++ = *src++;
163  } else
164  *dst_cur++ = huff_code_table[huff_code];
165  if (dst_cur >= dst_end)
166  break;
167  }
168 
169  return dst_cur - dst;
170 }
171 
172 static int cin_decode_lzss(const unsigned char *src, int src_size,
173  unsigned char *dst, int dst_size)
174 {
175  uint16_t cmd;
176  int i, sz, offset, code;
177  unsigned char *dst_end = dst + dst_size, *dst_start = dst;
178  const unsigned char *src_end = src + src_size;
179 
180  while (src < src_end && dst < dst_end) {
181  code = *src++;
182  for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
183  if (code & (1 << i)) {
184  *dst++ = *src++;
185  } else {
186  cmd = AV_RL16(src);
187  src += 2;
188  offset = cmd >> 4;
189  if ((int)(dst - dst_start) < offset + 1)
190  return AVERROR_INVALIDDATA;
191  sz = (cmd & 0xF) + 2;
192  /* don't use memcpy/memmove here as the decoding routine
193  * (ab)uses buffer overlappings to repeat bytes in the
194  * destination */
195  sz = FFMIN(sz, dst_end - dst);
196  while (sz--) {
197  *dst = *(dst - offset - 1);
198  ++dst;
199  }
200  }
201  }
202  }
203 
204  return 0;
205 }
206 
207 static int cin_decode_rle(const unsigned char *src, int src_size,
208  unsigned char *dst, int dst_size)
209 {
210  int len, code;
211  unsigned char *dst_end = dst + dst_size;
212  const unsigned char *src_end = src + src_size;
213 
214  while (src + 1 < src_end && dst < dst_end) {
215  code = *src++;
216  if (code & 0x80) {
217  len = code - 0x7F;
218  memset(dst, *src++, FFMIN(len, dst_end - dst));
219  } else {
220  len = code + 1;
221  if (len > src_end-src) {
222  av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
223  return AVERROR_INVALIDDATA;
224  }
225  memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
226  src += len;
227  }
228  dst += len;
229  }
230  return 0;
231 }
232 
234  void *data, int *got_frame,
235  AVPacket *avpkt)
236 {
237  const uint8_t *buf = avpkt->data;
238  int buf_size = avpkt->size;
239  CinVideoContext *cin = avctx->priv_data;
240  int i, y, palette_type, palette_colors_count,
241  bitmap_frame_type, bitmap_frame_size, res = 0;
242 
243  palette_type = buf[0];
244  palette_colors_count = AV_RL16(buf + 1);
245  bitmap_frame_type = buf[3];
246  buf += 4;
247 
248  bitmap_frame_size = buf_size - 4;
249 
250  /* handle palette */
251  if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
252  return AVERROR_INVALIDDATA;
253  if (palette_type == 0) {
254  if (palette_colors_count > 256)
255  return AVERROR_INVALIDDATA;
256  for (i = 0; i < palette_colors_count; ++i) {
257  cin->palette[i] = 0xFFU << 24 | bytestream_get_le24(&buf);
258  bitmap_frame_size -= 3;
259  }
260  } else {
261  for (i = 0; i < palette_colors_count; ++i) {
262  cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
263  buf += 4;
264  bitmap_frame_size -= 4;
265  }
266  }
267 
268  /* note: the decoding routines below assumes that
269  * surface.width = surface.pitch */
270  switch (bitmap_frame_type) {
271  case 9:
272  cin_decode_rle(buf, bitmap_frame_size,
273  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
274  break;
275  case 34:
276  cin_decode_rle(buf, bitmap_frame_size,
277  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
279  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
280  break;
281  case 35:
282  bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
283  cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
284  cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
285  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
286  break;
287  case 36:
288  bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
290  cin->bitmap_size);
291  cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
292  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
294  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
295  break;
296  case 37:
297  cin_decode_huffman(buf, bitmap_frame_size,
298  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
299  break;
300  case 38:
301  res = cin_decode_lzss(buf, bitmap_frame_size,
303  cin->bitmap_size);
304  if (res < 0)
305  return res;
306  break;
307  case 39:
308  res = cin_decode_lzss(buf, bitmap_frame_size,
310  cin->bitmap_size);
311  if (res < 0)
312  return res;
314  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
315  break;
316  }
317 
318  if ((res = ff_reget_buffer(avctx, &cin->frame)) < 0)
319  return res;
320 
321  memcpy(cin->frame.data[1], cin->palette, sizeof(cin->palette));
322  cin->frame.palette_has_changed = 1;
323  for (y = 0; y < cin->avctx->height; ++y)
324  memcpy(cin->frame.data[0] + (cin->avctx->height - 1 - y) * cin->frame.linesize[0],
325  cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
326  cin->avctx->width);
327 
329  cin->bitmap_table[CIN_PRE_BMP]);
330 
331  if ((res = av_frame_ref(data, &cin->frame)) < 0)
332  return res;
333 
334  *got_frame = 1;
335 
336  return buf_size;
337 }
338 
340 {
341  CinVideoContext *cin = avctx->priv_data;
342 
343  av_frame_unref(&cin->frame);
344 
345  destroy_buffers(cin);
346 
347  return 0;
348 }
349 
351 {
352  CinAudioContext *cin = avctx->priv_data;
353 
354  cin->initial_decode_frame = 1;
355  cin->delta = 0;
356  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
357  avctx->channels = 1;
359 
360  return 0;
361 }
362 
363 static int cinaudio_decode_frame(AVCodecContext *avctx, void *data,
364  int *got_frame_ptr, AVPacket *avpkt)
365 {
366  AVFrame *frame = data;
367  const uint8_t *buf = avpkt->data;
368  CinAudioContext *cin = avctx->priv_data;
369  const uint8_t *buf_end = buf + avpkt->size;
370  int16_t *samples;
371  int delta, ret;
372 
373  /* get output buffer */
374  frame->nb_samples = avpkt->size - cin->initial_decode_frame;
375  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
376  return ret;
377  samples = (int16_t *)frame->data[0];
378 
379  delta = cin->delta;
380  if (cin->initial_decode_frame) {
381  cin->initial_decode_frame = 0;
382  delta = sign_extend(AV_RL16(buf), 16);
383  buf += 2;
384  *samples++ = delta;
385  }
386  while (buf < buf_end) {
387  delta += cinaudio_delta16_table[*buf++];
388  delta = av_clip_int16(delta);
389  *samples++ = delta;
390  }
391  cin->delta = delta;
392 
393  *got_frame_ptr = 1;
394 
395  return avpkt->size;
396 }
397 
399  .name = "dsicinvideo",
400  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
401  .type = AVMEDIA_TYPE_VIDEO,
403  .priv_data_size = sizeof(CinVideoContext),
407  .capabilities = CODEC_CAP_DR1,
408 };
409 
411  .name = "dsicinaudio",
412  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN audio"),
413  .type = AVMEDIA_TYPE_AUDIO,
415  .priv_data_size = sizeof(CinAudioContext),
418  .capabilities = CODEC_CAP_DR1,
419 };
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
#define AV_RL24(x)
Definition: intreadwrite.h:450
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
Definition: dsicinav.c:114
#define AV_RL16(x)
Definition: intreadwrite.h:245
int size
Definition: avcodec.h:1064
const char * b
Definition: vf_curves.c:105
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
AVCodecContext * avctx
Definition: dsicinav.c:41
static int cinaudio_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: dsicinav.c:363
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
CinVideoBitmapIndex
Definition: dsicinav.c:34
static uint8_t * res
Definition: ffhash.c:43
static int cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinav.c:172
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1881
uint8_t
float delta
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by a given frame.
Definition: frame.c:247
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
#define FFSWAP(type, a, b)
Definition: avcodec.h:928
signed 16 bits
Definition: samplefmt.h:52
static AVFrame * frame
Definition: demuxing.c:51
#define U(x)
Definition: vp56_arith.h:37
AVFrame frame
Definition: dsicinav.c:42
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
uint8_t * bitmap_table[3]
Definition: dsicinav.c:45
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1934
static const int16_t cinaudio_delta16_table[256]
Definition: dsicinav.c:55
static av_cold void destroy_buffers(CinVideoContext *cin)
Definition: dsicinav.c:90
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:986
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:351
float y
AVCodec ff_dsicinaudio_decoder
Definition: dsicinav.c:410
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
#define FFMIN3(a, b, c)
Definition: avcodec.h:926
static int cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinav.c:207
#define FFMIN(a, b)
Definition: avcodec.h:925
static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
Definition: dsicinav.c:339
AVS_Value src
Definition: avisynth_c.h:523
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
void * buf
Definition: avisynth_c.h:594
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:1046
uint8_t * data
Definition: avcodec.h:1063
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:303
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:123
void * priv_data
Definition: avcodec.h:1182
static av_cold int allocate_buffers(CinVideoContext *cin)
Definition: dsicinav.c:98
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
Definition: dsicinav.c:130
common internal api header.
static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinav.c:137
#define AVERROR_INVALIDDATA
int len
int channels
number of audio channels
Definition: avcodec.h:1874
static int cinvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dsicinav.c:233
#define AVERROR(e)
static av_cold int cinaudio_decode_init(AVCodecContext *avctx)
Definition: dsicinav.c:350
unsigned int bitmap_size
Definition: dsicinav.c:43
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
AVCodec ff_dsicinvideo_decoder
Definition: dsicinav.c:398
8 bit with PIX_FMT_RGB32 palette
Definition: avcodec.h:4545
#define AV_CH_LAYOUT_MONO
uint32_t palette[256]
Definition: dsicinav.c:44
int initial_decode_frame
Definition: dsicinav.c:49
This structure stores compressed data.
Definition: avcodec.h:1040
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:150
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241