FFmpeg
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 "libavutil/mem.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "codec_internal.h"
31 #include "decode.h"
32 
33 typedef enum CinVideoBitmapIndex {
34  CIN_CUR_BMP = 0, /* current */
35  CIN_PRE_BMP = 1, /* previous */
36  CIN_INT_BMP = 2 /* intermediate */
38 
39 typedef struct CinVideoContext {
42  unsigned int bitmap_size;
43  uint32_t palette[256];
44  uint8_t *bitmap_table[3];
46 
48 {
49  int i;
50 
51  for (i = 0; i < 3; ++i)
52  av_freep(&cin->bitmap_table[i]);
53 }
54 
56 {
57  int i;
58 
59  for (i = 0; i < 3; ++i) {
60  cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
61  if (!cin->bitmap_table[i]) {
62  av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
63  return AVERROR(ENOMEM);
64  }
65  }
66 
67  return 0;
68 }
69 
71 {
72  CinVideoContext *cin = avctx->priv_data;
73 
74  cin->avctx = avctx;
75  avctx->pix_fmt = AV_PIX_FMT_PAL8;
76 
77  cin->frame = av_frame_alloc();
78  if (!cin->frame)
79  return AVERROR(ENOMEM);
80 
81  cin->bitmap_size = avctx->width * avctx->height;
82  if (allocate_buffers(cin))
83  return AVERROR(ENOMEM);
84 
85  return 0;
86 }
87 
88 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
89  int size)
90 {
91  while (size--)
92  *dst++ += *src++;
93 }
94 
95 static int cin_decode_huffman(const unsigned char *src, int src_size,
96  unsigned char *dst, int dst_size)
97 {
98  int b, huff_code = 0;
99  unsigned char huff_code_table[15];
100  unsigned char *dst_cur = dst;
101  unsigned char *dst_end = dst + dst_size;
102  const unsigned char *src_end = src + src_size;
103 
104  memcpy(huff_code_table, src, 15);
105  src += 15;
106 
107  while (src < src_end) {
108  huff_code = *src++;
109  if ((huff_code >> 4) == 15) {
110  b = huff_code << 4;
111  huff_code = *src++;
112  *dst_cur++ = b | (huff_code >> 4);
113  } else
114  *dst_cur++ = huff_code_table[huff_code >> 4];
115  if (dst_cur >= dst_end)
116  break;
117 
118  huff_code &= 15;
119  if (huff_code == 15) {
120  *dst_cur++ = *src++;
121  } else
122  *dst_cur++ = huff_code_table[huff_code];
123  if (dst_cur >= dst_end)
124  break;
125  }
126 
127  return dst_cur - dst;
128 }
129 
130 static int cin_decode_lzss(const unsigned char *src, int src_size,
131  unsigned char *dst, int dst_size)
132 {
133  uint16_t cmd;
134  int i, sz, offset, code;
135  unsigned char *dst_end = dst + dst_size, *dst_start = dst;
136  const unsigned char *src_end = src + src_size;
137 
138  while (src < src_end && dst < dst_end) {
139  code = *src++;
140  for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
141  if (code & (1 << i)) {
142  *dst++ = *src++;
143  } else {
144  cmd = AV_RL16(src);
145  src += 2;
146  offset = cmd >> 4;
147  if ((int)(dst - dst_start) < offset + 1)
148  return AVERROR_INVALIDDATA;
149  sz = (cmd & 0xF) + 2;
150  /* don't use memcpy/memmove here as the decoding routine
151  * (ab)uses buffer overlappings to repeat bytes in the
152  * destination */
153  sz = FFMIN(sz, dst_end - dst);
154  while (sz--) {
155  *dst = *(dst - offset - 1);
156  ++dst;
157  }
158  }
159  }
160  }
161 
162  if (dst_end - dst > dst_size - dst_size/10)
163  return AVERROR_INVALIDDATA;
164 
165  return 0;
166 }
167 
168 static int cin_decode_rle(const unsigned char *src, int src_size,
169  unsigned char *dst, int dst_size)
170 {
171  int len, code;
172  unsigned char *dst_end = dst + dst_size;
173  const unsigned char *src_end = src + src_size;
174 
175  while (src + 1 < src_end && dst < dst_end) {
176  code = *src++;
177  if (code & 0x80) {
178  len = code - 0x7F;
179  memset(dst, *src++, FFMIN(len, dst_end - dst));
180  } else {
181  len = code + 1;
182  if (len > src_end-src) {
183  av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
184  return AVERROR_INVALIDDATA;
185  }
186  memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
187  src += len;
188  }
189  dst += len;
190  }
191 
192  if (dst_end - dst > dst_size - dst_size/10)
193  return AVERROR_INVALIDDATA;
194 
195  return 0;
196 }
197 
198 static int cinvideo_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
199  int *got_frame, 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  res = cin_decode_huffman(buf, bitmap_frame_size,
270  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
271 
272  if (cin->bitmap_size - avctx->discard_damaged_percentage*cin->bitmap_size/100 > res)
273  return AVERROR_INVALIDDATA;
274  break;
275  case 38:
276  res = cin_decode_lzss(buf, bitmap_frame_size,
278  cin->bitmap_size);
279  if (res < 0)
280  return res;
281  break;
282  case 39:
283  res = cin_decode_lzss(buf, bitmap_frame_size,
285  cin->bitmap_size);
286  if (res < 0)
287  return res;
289  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
290  break;
291  }
292 
293  if ((res = ff_reget_buffer(avctx, cin->frame, 0)) < 0)
294  return res;
295 
296  memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
297 #if FF_API_PALETTE_HAS_CHANGED
299  cin->frame->palette_has_changed = 1;
301 #endif
302  for (y = 0; y < cin->avctx->height; ++y)
303  memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
304  cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
305  cin->avctx->width);
306 
307  FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
308  cin->bitmap_table[CIN_PRE_BMP]);
309 
310  if ((res = av_frame_ref(rframe, cin->frame)) < 0)
311  return res;
312 
313  *got_frame = 1;
314 
315  return buf_size;
316 }
317 
319 {
320  CinVideoContext *cin = avctx->priv_data;
321 
322  av_frame_free(&cin->frame);
323 
324  destroy_buffers(cin);
325 
326  return 0;
327 }
328 
330  .p.name = "dsicinvideo",
331  CODEC_LONG_NAME("Delphine Software International CIN video"),
332  .p.type = AVMEDIA_TYPE_VIDEO,
333  .p.id = AV_CODEC_ID_DSICINVIDEO,
334  .priv_data_size = sizeof(CinVideoContext),
336  .close = cinvideo_decode_end,
338  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
339  .p.capabilities = AV_CODEC_CAP_DR1,
340 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
AVFrame::palette_has_changed
attribute_deprecated int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:582
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
b
#define b
Definition: input.c:41
FFCodec
Definition: codec_internal.h:127
CinVideoContext
Definition: dsicinvideo.c:39
destroy_buffers
static av_cold void destroy_buffers(CinVideoContext *cin)
Definition: dsicinvideo.c:47
AV_CODEC_ID_DSICINVIDEO
@ AV_CODEC_ID_DSICINVIDEO
Definition: codec_id.h:146
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
cin_apply_delta_data
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
Definition: dsicinvideo.c:88
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
CinVideoBitmapIndex
CinVideoBitmapIndex
Definition: dsicinvideo.c:33
decode.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
CinVideoContext::bitmap_table
uint8_t * bitmap_table[3]
Definition: dsicinvideo.c:44
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
NULL
#define NULL
Definition: coverity.c:32
CIN_PRE_BMP
@ CIN_PRE_BMP
Definition: dsicinvideo.c:35
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
cinvideo_decode_end
static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
Definition: dsicinvideo.c:318
av_frame_ref
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:400
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
ff_dsicinvideo_decoder
const FFCodec ff_dsicinvideo_decoder
Definition: dsicinvideo.c:329
size
int size
Definition: twinvq_data.h:10344
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
cin_decode_rle
static int cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:168
CIN_CUR_BMP
@ CIN_CUR_BMP
Definition: dsicinvideo.c:34
CinVideoContext::bitmap_size
unsigned int bitmap_size
Definition: dsicinvideo.c:42
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
cinvideo_decode_init
static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
Definition: dsicinvideo.c:70
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
allocate_buffers
static av_cold int allocate_buffers(CinVideoContext *cin)
Definition: dsicinvideo.c:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
CinVideoContext::frame
AVFrame * frame
Definition: dsicinvideo.c:41
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1815
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
cin_decode_lzss
static int cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:130
AVCodecContext::discard_damaged_percentage
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
Definition: avcodec.h:1980
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
CinVideoContext::avctx
AVCodecContext * avctx
Definition: dsicinvideo.c:40
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
CIN_INT_BMP
@ CIN_INT_BMP
Definition: dsicinvideo.c:36
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
bytestream.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
cinvideo_decode_frame
static int cinvideo_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: dsicinvideo.c:198
CinVideoContext::palette
uint32_t palette[256]
Definition: dsicinvideo.c:43
src
#define src
Definition: vp8dsp.c:248
cin_decode_huffman
static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:95