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 "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];
42  uint8_t *bitmap_table[3];
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  return AVERROR(ENOMEM);
62  }
63  }
64 
65  return 0;
66 }
67 
69 {
70  CinVideoContext *cin = avctx->priv_data;
71 
72  cin->avctx = avctx;
73  avctx->pix_fmt = AV_PIX_FMT_PAL8;
74 
75  cin->frame = av_frame_alloc();
76  if (!cin->frame)
77  return AVERROR(ENOMEM);
78 
79  cin->bitmap_size = avctx->width * avctx->height;
80  if (allocate_buffers(cin))
81  return AVERROR(ENOMEM);
82 
83  return 0;
84 }
85 
86 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
87  int size)
88 {
89  while (size--)
90  *dst++ += *src++;
91 }
92 
93 static int cin_decode_huffman(const unsigned char *src, int src_size,
94  unsigned char *dst, int dst_size)
95 {
96  int b, huff_code = 0;
97  unsigned char huff_code_table[15];
98  unsigned char *dst_cur = dst;
99  unsigned char *dst_end = dst + dst_size;
100  const unsigned char *src_end = src + src_size;
101 
102  memcpy(huff_code_table, src, 15);
103  src += 15;
104 
105  while (src < src_end) {
106  huff_code = *src++;
107  if ((huff_code >> 4) == 15) {
108  b = huff_code << 4;
109  huff_code = *src++;
110  *dst_cur++ = b | (huff_code >> 4);
111  } else
112  *dst_cur++ = huff_code_table[huff_code >> 4];
113  if (dst_cur >= dst_end)
114  break;
115 
116  huff_code &= 15;
117  if (huff_code == 15) {
118  *dst_cur++ = *src++;
119  } else
120  *dst_cur++ = huff_code_table[huff_code];
121  if (dst_cur >= dst_end)
122  break;
123  }
124 
125  return dst_cur - dst;
126 }
127 
128 static int cin_decode_lzss(const unsigned char *src, int src_size,
129  unsigned char *dst, int dst_size)
130 {
131  uint16_t cmd;
132  int i, sz, offset, code;
133  unsigned char *dst_end = dst + dst_size, *dst_start = dst;
134  const unsigned char *src_end = src + src_size;
135 
136  while (src < src_end && dst < dst_end) {
137  code = *src++;
138  for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
139  if (code & (1 << i)) {
140  *dst++ = *src++;
141  } else {
142  cmd = AV_RL16(src);
143  src += 2;
144  offset = cmd >> 4;
145  if ((int)(dst - dst_start) < offset + 1)
146  return AVERROR_INVALIDDATA;
147  sz = (cmd & 0xF) + 2;
148  /* don't use memcpy/memmove here as the decoding routine
149  * (ab)uses buffer overlappings to repeat bytes in the
150  * destination */
151  sz = FFMIN(sz, dst_end - dst);
152  while (sz--) {
153  *dst = *(dst - offset - 1);
154  ++dst;
155  }
156  }
157  }
158  }
159 
160  if (dst_end - dst > dst_size - dst_size/10)
161  return AVERROR_INVALIDDATA;
162 
163  return 0;
164 }
165 
166 static int cin_decode_rle(const unsigned char *src, int src_size,
167  unsigned char *dst, int dst_size)
168 {
169  int len, code;
170  unsigned char *dst_end = dst + dst_size;
171  const unsigned char *src_end = src + src_size;
172 
173  while (src + 1 < src_end && dst < dst_end) {
174  code = *src++;
175  if (code & 0x80) {
176  len = code - 0x7F;
177  memset(dst, *src++, FFMIN(len, dst_end - dst));
178  } else {
179  len = code + 1;
180  if (len > src_end-src) {
181  av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
182  return AVERROR_INVALIDDATA;
183  }
184  memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
185  src += len;
186  }
187  dst += len;
188  }
189 
190  if (dst_end - dst > dst_size - dst_size/10)
191  return AVERROR_INVALIDDATA;
192 
193  return 0;
194 }
195 
197  void *data, int *got_frame,
198  AVPacket *avpkt)
199 {
200  const uint8_t *buf = avpkt->data;
201  int buf_size = avpkt->size;
202  CinVideoContext *cin = avctx->priv_data;
203  int i, y, palette_type, palette_colors_count,
204  bitmap_frame_type, bitmap_frame_size, res = 0;
205 
206  palette_type = buf[0];
207  palette_colors_count = AV_RL16(buf + 1);
208  bitmap_frame_type = buf[3];
209  buf += 4;
210 
211  bitmap_frame_size = buf_size - 4;
212 
213  /* handle palette */
214  if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
215  return AVERROR_INVALIDDATA;
216  if (palette_type == 0) {
217  if (palette_colors_count > 256)
218  return AVERROR_INVALIDDATA;
219  for (i = 0; i < palette_colors_count; ++i) {
220  cin->palette[i] = 0xFFU << 24 | bytestream_get_le24(&buf);
221  bitmap_frame_size -= 3;
222  }
223  } else {
224  for (i = 0; i < palette_colors_count; ++i) {
225  cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
226  buf += 4;
227  bitmap_frame_size -= 4;
228  }
229  }
230 
231  /* note: the decoding routines below assumes that
232  * surface.width = surface.pitch */
233  switch (bitmap_frame_type) {
234  case 9:
235  res = cin_decode_rle(buf, bitmap_frame_size,
236  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
237  if (res < 0)
238  return res;
239  break;
240  case 34:
241  res = cin_decode_rle(buf, bitmap_frame_size,
242  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
243  if (res < 0)
244  return res;
246  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
247  break;
248  case 35:
249  bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
250  cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
251  res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
252  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
253  if (res < 0)
254  return res;
255  break;
256  case 36:
257  bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
259  cin->bitmap_size);
260  res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
261  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
262  if (res < 0)
263  return res;
265  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
266  break;
267  case 37:
268  res = cin_decode_huffman(buf, bitmap_frame_size,
269  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
270 
271  if (cin->bitmap_size - avctx->discard_damaged_percentage*cin->bitmap_size/100 > res)
272  return AVERROR_INVALIDDATA;
273  break;
274  case 38:
275  res = cin_decode_lzss(buf, bitmap_frame_size,
277  cin->bitmap_size);
278  if (res < 0)
279  return res;
280  break;
281  case 39:
282  res = cin_decode_lzss(buf, bitmap_frame_size,
284  cin->bitmap_size);
285  if (res < 0)
286  return res;
288  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
289  break;
290  }
291 
292  if ((res = ff_reget_buffer(avctx, cin->frame, 0)) < 0)
293  return res;
294 
295  memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
296  cin->frame->palette_has_changed = 1;
297  for (y = 0; y < cin->avctx->height; ++y)
298  memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
299  cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
300  cin->avctx->width);
301 
302  FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
303  cin->bitmap_table[CIN_PRE_BMP]);
304 
305  if ((res = av_frame_ref(data, cin->frame)) < 0)
306  return res;
307 
308  *got_frame = 1;
309 
310  return buf_size;
311 }
312 
314 {
315  CinVideoContext *cin = avctx->priv_data;
316 
317  av_frame_free(&cin->frame);
318 
319  destroy_buffers(cin);
320 
321  return 0;
322 }
323 
325  .name = "dsicinvideo",
326  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
327  .type = AVMEDIA_TYPE_VIDEO,
329  .priv_data_size = sizeof(CinVideoContext),
331  .close = cinvideo_decode_end,
334  .capabilities = AV_CODEC_CAP_DR1,
335 };
AVCodec
AVCodec.
Definition: codec.h:202
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
b
#define b
Definition: input.c:40
data
const char data[16]
Definition: mxf.c:143
CinVideoContext
Definition: dsicinvideo.c:37
destroy_buffers
static av_cold void destroy_buffers(CinVideoContext *cin)
Definition: dsicinvideo.c:45
AV_CODEC_ID_DSICINVIDEO
@ AV_CODEC_ID_DSICINVIDEO
Definition: codec_id.h:144
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
init
static int init
Definition: av_tx.c:47
cinvideo_decode_frame
static int cinvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dsicinvideo.c:196
U
#define U(x)
Definition: vp56_arith.h:37
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:97
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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:86
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
CinVideoBitmapIndex
CinVideoBitmapIndex
Definition: dsicinvideo.c:31
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:42
NULL
#define NULL
Definition: coverity.c:32
src
#define src
Definition: vp8dsp.c:255
CIN_PRE_BMP
@ CIN_PRE_BMP
Definition: dsicinvideo.c:33
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:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
cinvideo_decode_end
static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
Definition: dsicinvideo.c:313
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:325
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:166
CIN_CUR_BMP
@ CIN_CUR_BMP
Definition: dsicinvideo.c:32
CinVideoContext::bitmap_size
unsigned int bitmap_size
Definition: dsicinvideo.c:40
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
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:68
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: internal.h:50
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
allocate_buffers
static av_cold int allocate_buffers(CinVideoContext *cin)
Definition: dsicinvideo.c:53
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:263
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
CinVideoContext::frame
AVFrame * frame
Definition: dsicinvideo.c:39
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
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:1759
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVCodecContext
main external API structure.
Definition: avcodec.h:383
cin_decode_lzss
static int cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:128
AVFrame::palette_has_changed
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:479
AVCodecContext::discard_damaged_percentage
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
Definition: avcodec.h:1966
ff_dsicinvideo_decoder
const AVCodec ff_dsicinvideo_decoder
Definition: dsicinvideo.c:324
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
CinVideoContext::avctx
AVCodecContext * avctx
Definition: dsicinvideo.c:38
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
CIN_INT_BMP
@ CIN_INT_BMP
Definition: dsicinvideo.c:34
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
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:362
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
CinVideoContext::palette
uint32_t palette[256]
Definition: dsicinvideo.c:41
cin_decode_huffman
static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:93