FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dsicinvideo.c
Go to the documentation of this file.
1 /*
2  * Delphine Software International CIN video decoder
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 video decoder
25  */
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 
31 typedef enum CinVideoBitmapIndex {
32  CIN_CUR_BMP = 0, /* current */
33  CIN_PRE_BMP = 1, /* previous */
34  CIN_INT_BMP = 2 /* intermediate */
36 
37 typedef struct CinVideoContext {
40  unsigned int bitmap_size;
41  uint32_t palette[256];
44 
46 {
47  int i;
48 
49  for (i = 0; i < 3; ++i)
50  av_freep(&cin->bitmap_table[i]);
51 }
52 
54 {
55  int i;
56 
57  for (i = 0; i < 3; ++i) {
58  cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
59  if (!cin->bitmap_table[i]) {
60  av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
61  destroy_buffers(cin);
62  return AVERROR(ENOMEM);
63  }
64  }
65 
66  return 0;
67 }
68 
70 {
71  CinVideoContext *cin = avctx->priv_data;
72 
73  cin->avctx = avctx;
74  avctx->pix_fmt = AV_PIX_FMT_PAL8;
75 
76  cin->frame = av_frame_alloc();
77  if (!cin->frame)
78  return AVERROR(ENOMEM);
79 
80  cin->bitmap_size = avctx->width * avctx->height;
81  if (allocate_buffers(cin))
82  return AVERROR(ENOMEM);
83 
84  return 0;
85 }
86 
87 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
88  int size)
89 {
90  while (size--)
91  *dst++ += *src++;
92 }
93 
94 static int cin_decode_huffman(const unsigned char *src, int src_size,
95  unsigned char *dst, int dst_size)
96 {
97  int b, huff_code = 0;
98  unsigned char huff_code_table[15];
99  unsigned char *dst_cur = dst;
100  unsigned char *dst_end = dst + dst_size;
101  const unsigned char *src_end = src + src_size;
102 
103  memcpy(huff_code_table, src, 15);
104  src += 15;
105 
106  while (src < src_end) {
107  huff_code = *src++;
108  if ((huff_code >> 4) == 15) {
109  b = huff_code << 4;
110  huff_code = *src++;
111  *dst_cur++ = b | (huff_code >> 4);
112  } else
113  *dst_cur++ = huff_code_table[huff_code >> 4];
114  if (dst_cur >= dst_end)
115  break;
116 
117  huff_code &= 15;
118  if (huff_code == 15) {
119  *dst_cur++ = *src++;
120  } else
121  *dst_cur++ = huff_code_table[huff_code];
122  if (dst_cur >= dst_end)
123  break;
124  }
125 
126  return dst_cur - dst;
127 }
128 
129 static int cin_decode_lzss(const unsigned char *src, int src_size,
130  unsigned char *dst, int dst_size)
131 {
132  uint16_t cmd;
133  int i, sz, offset, code;
134  unsigned char *dst_end = dst + dst_size, *dst_start = dst;
135  const unsigned char *src_end = src + src_size;
136 
137  while (src < src_end && dst < dst_end) {
138  code = *src++;
139  for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
140  if (code & (1 << i)) {
141  *dst++ = *src++;
142  } else {
143  cmd = AV_RL16(src);
144  src += 2;
145  offset = cmd >> 4;
146  if ((int)(dst - dst_start) < offset + 1)
147  return AVERROR_INVALIDDATA;
148  sz = (cmd & 0xF) + 2;
149  /* don't use memcpy/memmove here as the decoding routine
150  * (ab)uses buffer overlappings to repeat bytes in the
151  * destination */
152  sz = FFMIN(sz, dst_end - dst);
153  while (sz--) {
154  *dst = *(dst - offset - 1);
155  ++dst;
156  }
157  }
158  }
159  }
160 
161  if (dst_end - dst > dst_size - dst_size/10)
162  return AVERROR_INVALIDDATA;
163 
164  return 0;
165 }
166 
167 static int cin_decode_rle(const unsigned char *src, int src_size,
168  unsigned char *dst, int dst_size)
169 {
170  int len, code;
171  unsigned char *dst_end = dst + dst_size;
172  const unsigned char *src_end = src + src_size;
173 
174  while (src + 1 < src_end && dst < dst_end) {
175  code = *src++;
176  if (code & 0x80) {
177  len = code - 0x7F;
178  memset(dst, *src++, FFMIN(len, dst_end - dst));
179  } else {
180  len = code + 1;
181  if (len > src_end-src) {
182  av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
183  return AVERROR_INVALIDDATA;
184  }
185  memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
186  src += len;
187  }
188  dst += len;
189  }
190 
191  if (dst_end - dst > dst_size - dst_size/10)
192  return AVERROR_INVALIDDATA;
193 
194  return 0;
195 }
196 
198  void *data, int *got_frame,
199  AVPacket *avpkt)
200 {
201  const uint8_t *buf = avpkt->data;
202  int buf_size = avpkt->size;
203  CinVideoContext *cin = avctx->priv_data;
204  int i, y, palette_type, palette_colors_count,
205  bitmap_frame_type, bitmap_frame_size, res = 0;
206 
207  palette_type = buf[0];
208  palette_colors_count = AV_RL16(buf + 1);
209  bitmap_frame_type = buf[3];
210  buf += 4;
211 
212  bitmap_frame_size = buf_size - 4;
213 
214  /* handle palette */
215  if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
216  return AVERROR_INVALIDDATA;
217  if (palette_type == 0) {
218  if (palette_colors_count > 256)
219  return AVERROR_INVALIDDATA;
220  for (i = 0; i < palette_colors_count; ++i) {
221  cin->palette[i] = 0xFFU << 24 | bytestream_get_le24(&buf);
222  bitmap_frame_size -= 3;
223  }
224  } else {
225  for (i = 0; i < palette_colors_count; ++i) {
226  cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
227  buf += 4;
228  bitmap_frame_size -= 4;
229  }
230  }
231 
232  /* note: the decoding routines below assumes that
233  * surface.width = surface.pitch */
234  switch (bitmap_frame_type) {
235  case 9:
236  res = cin_decode_rle(buf, bitmap_frame_size,
237  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
238  if (res < 0)
239  return res;
240  break;
241  case 34:
242  res = cin_decode_rle(buf, bitmap_frame_size,
243  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
244  if (res < 0)
245  return res;
247  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
248  break;
249  case 35:
250  bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
251  cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
252  res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
253  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
254  if (res < 0)
255  return res;
256  break;
257  case 36:
258  bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
260  cin->bitmap_size);
261  res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
262  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
263  if (res < 0)
264  return res;
266  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
267  break;
268  case 37:
269  cin_decode_huffman(buf, bitmap_frame_size,
270  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
271  break;
272  case 38:
273  res = cin_decode_lzss(buf, bitmap_frame_size,
275  cin->bitmap_size);
276  if (res < 0)
277  return res;
278  break;
279  case 39:
280  res = cin_decode_lzss(buf, bitmap_frame_size,
282  cin->bitmap_size);
283  if (res < 0)
284  return res;
286  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
287  break;
288  }
289 
290  if ((res = ff_reget_buffer(avctx, cin->frame)) < 0)
291  return res;
292 
293  memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
294  cin->frame->palette_has_changed = 1;
295  for (y = 0; y < cin->avctx->height; ++y)
296  memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
297  cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
298  cin->avctx->width);
299 
301  cin->bitmap_table[CIN_PRE_BMP]);
302 
303  if ((res = av_frame_ref(data, cin->frame)) < 0)
304  return res;
305 
306  *got_frame = 1;
307 
308  return buf_size;
309 }
310 
312 {
313  CinVideoContext *cin = avctx->priv_data;
314 
315  av_frame_free(&cin->frame);
316 
317  destroy_buffers(cin);
318 
319  return 0;
320 }
321 
323  .name = "dsicinvideo",
324  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
325  .type = AVMEDIA_TYPE_VIDEO,
327  .priv_data_size = sizeof(CinVideoContext),
329  .close = cinvideo_decode_end,
331  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
332  .capabilities = AV_CODEC_CAP_DR1,
333 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVFrame * frame
Definition: dsicinvideo.c:39
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
Definition: dsicinvideo.c:87
int size
Definition: avcodec.h:1446
const char * b
Definition: vf_curves.c:116
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
AVCodecContext * avctx
Definition: dsicinvideo.c:38
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
CinVideoBitmapIndex
Definition: dsicinvideo.c:31
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3424
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
AVCodec ff_dsicinvideo_decoder
Definition: dsicinvideo.c:322
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: decode.c:1965
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
uint8_t * data
Definition: avcodec.h:1445
#define FFMIN3(a, b, c)
Definition: common.h:97
static av_cold void destroy_buffers(CinVideoContext *cin)
Definition: dsicinvideo.c:45
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:94
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
uint8_t * bitmap_table[3]
Definition: dsicinvideo.c:42
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:87
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
Definition: dsicinvideo.c:69
static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
Definition: dsicinvideo.c:311
#define FFMIN(a, b)
Definition: common.h:96
int width
picture width / height.
Definition: avcodec.h:1706
static av_cold int allocate_buffers(CinVideoContext *cin)
Definition: dsicinvideo.c:53
static int cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:129
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
main external API structure.
Definition: avcodec.h:1533
void * buf
Definition: avisynth_c.h:690
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:383
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
common internal api header.
static int cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:167
void * priv_data
Definition: avcodec.h:1560
int len
unsigned int bitmap_size
Definition: dsicinvideo.c:40
#define av_freep(p)
#define FFSWAP(type, a, b)
Definition: common.h:99
uint32_t palette[256]
Definition: dsicinvideo.c:41
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
static int cinvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dsicinvideo.c:197