Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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  CinVideoContext *cin = avctx->priv_data;
48  unsigned int i;
49 
50  cin->avctx = avctx;
51  avctx->pix_fmt = AV_PIX_FMT_PAL8;
52 
53  cin->frame = av_frame_alloc();
54  if (!cin->frame)
55  return AVERROR(ENOMEM);
56 
57  cin->bitmap_size = avctx->width * avctx->height;
58  for (i = 0; i < 3; ++i) {
59  cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
60  if (!cin->bitmap_table[i])
61  av_log(avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
62  }
63 
64  return 0;
65 }
66 
67 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
68  int size)
69 {
70  while (size--)
71  *dst++ += *src++;
72 }
73 
74 static int cin_decode_huffman(const unsigned char *src, int src_size,
75  unsigned char *dst, int dst_size)
76 {
77  int b, huff_code = 0;
78  unsigned char huff_code_table[15];
79  unsigned char *dst_cur = dst;
80  unsigned char *dst_end = dst + dst_size;
81  const unsigned char *src_end = src + src_size;
82 
83  memcpy(huff_code_table, src, 15);
84  src += 15;
85 
86  while (src < src_end) {
87  huff_code = *src++;
88  if ((huff_code >> 4) == 15) {
89  b = huff_code << 4;
90  huff_code = *src++;
91  *dst_cur++ = b | (huff_code >> 4);
92  } else
93  *dst_cur++ = huff_code_table[huff_code >> 4];
94  if (dst_cur >= dst_end)
95  break;
96 
97  huff_code &= 15;
98  if (huff_code == 15) {
99  *dst_cur++ = *src++;
100  } else
101  *dst_cur++ = huff_code_table[huff_code];
102  if (dst_cur >= dst_end)
103  break;
104  }
105 
106  return dst_cur - dst;
107 }
108 
109 static int cin_decode_lzss(const unsigned char *src, int src_size,
110  unsigned char *dst, int dst_size)
111 {
112  uint16_t cmd;
113  int i, sz, offset, code;
114  unsigned char *dst_end = dst + dst_size, *dst_start = dst;
115  const unsigned char *src_end = src + src_size;
116 
117  while (src < src_end && dst < dst_end) {
118  code = *src++;
119  for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
120  if (code & (1 << i)) {
121  *dst++ = *src++;
122  } else {
123  cmd = AV_RL16(src);
124  src += 2;
125  offset = cmd >> 4;
126  if ((int)(dst - dst_start) < offset + 1)
127  return AVERROR_INVALIDDATA;
128  sz = (cmd & 0xF) + 2;
129  /* don't use memcpy/memmove here as the decoding routine
130  * (ab)uses buffer overlappings to repeat bytes in the
131  * destination */
132  sz = FFMIN(sz, dst_end - dst);
133  while (sz--) {
134  *dst = *(dst - offset - 1);
135  ++dst;
136  }
137  }
138  }
139  }
140 
141  return 0;
142 }
143 
144 static void cin_decode_rle(const unsigned char *src, int src_size,
145  unsigned char *dst, int dst_size)
146 {
147  int len, code;
148  unsigned char *dst_end = dst + dst_size;
149  const unsigned char *src_end = src + src_size;
150 
151  while (src < src_end && dst < dst_end) {
152  code = *src++;
153  if (code & 0x80) {
154  if (src >= src_end)
155  break;
156  len = code - 0x7F;
157  memset(dst, *src++, FFMIN(len, dst_end - dst));
158  } else {
159  len = code + 1;
160  memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
161  src += len;
162  }
163  dst += len;
164  }
165 }
166 
168  void *data, int *got_frame,
169  AVPacket *avpkt)
170 {
171  const uint8_t *buf = avpkt->data;
172  int buf_size = avpkt->size;
173  CinVideoContext *cin = avctx->priv_data;
174  int i, y, palette_type, palette_colors_count,
175  bitmap_frame_type, bitmap_frame_size, res = 0;
176 
177  palette_type = buf[0];
178  palette_colors_count = AV_RL16(buf + 1);
179  bitmap_frame_type = buf[3];
180  buf += 4;
181 
182  bitmap_frame_size = buf_size - 4;
183 
184  /* handle palette */
185  if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
186  return AVERROR_INVALIDDATA;
187  if (palette_type == 0) {
188  if (palette_colors_count > 256)
189  return AVERROR_INVALIDDATA;
190  for (i = 0; i < palette_colors_count; ++i) {
191  cin->palette[i] = bytestream_get_le24(&buf);
192  bitmap_frame_size -= 3;
193  }
194  } else {
195  for (i = 0; i < palette_colors_count; ++i) {
196  cin->palette[buf[0]] = AV_RL24(buf + 1);
197  buf += 4;
198  bitmap_frame_size -= 4;
199  }
200  }
201 
202  bitmap_frame_size = FFMIN(cin->bitmap_size, bitmap_frame_size);
203 
204  /* note: the decoding routines below assumes that
205  * surface.width = surface.pitch */
206  switch (bitmap_frame_type) {
207  case 9:
208  cin_decode_rle(buf, bitmap_frame_size,
209  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
210  break;
211  case 34:
212  cin_decode_rle(buf, bitmap_frame_size,
213  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
215  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
216  break;
217  case 35:
218  cin_decode_huffman(buf, bitmap_frame_size,
219  cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
220  cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
221  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
222  break;
223  case 36:
224  bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
226  cin->bitmap_size);
227  cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
228  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
230  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
231  break;
232  case 37:
233  cin_decode_huffman(buf, bitmap_frame_size,
234  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
235  break;
236  case 38:
237  res = cin_decode_lzss(buf, bitmap_frame_size,
239  cin->bitmap_size);
240  if (res < 0)
241  return res;
242  break;
243  case 39:
244  res = cin_decode_lzss(buf, bitmap_frame_size,
246  cin->bitmap_size);
247  if (res < 0)
248  return res;
250  cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
251  break;
252  }
253 
254  if ((res = ff_reget_buffer(avctx, cin->frame)) < 0) {
255  av_log(cin->avctx, AV_LOG_ERROR,
256  "delphinecinvideo: reget_buffer() failed to allocate a frame\n");
257  return res;
258  }
259 
260  memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
261  cin->frame->palette_has_changed = 1;
262  for (y = 0; y < cin->avctx->height; ++y)
263  memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
264  cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
265  cin->avctx->width);
266 
268  cin->bitmap_table[CIN_PRE_BMP]);
269 
270  if ((res = av_frame_ref(data, cin->frame)) < 0)
271  return res;
272 
273  *got_frame = 1;
274 
275  return buf_size;
276 }
277 
279 {
280  CinVideoContext *cin = avctx->priv_data;
281  int i;
282 
283  av_frame_free(&cin->frame);
284 
285  for (i = 0; i < 3; ++i)
286  av_free(cin->bitmap_table[i]);
287 
288  return 0;
289 }
290 
292  .name = "dsicinvideo",
293  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
294  .type = AVMEDIA_TYPE_VIDEO,
296  .priv_data_size = sizeof(CinVideoContext),
300  .capabilities = CODEC_CAP_DR1,
301 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVFrame * frame
Definition: dsicinvideo.c:39
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
Definition: dsicinvideo.c:67
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
#define AV_RL16
Definition: intreadwrite.h:42
AVCodecContext * avctx
Definition: dsicinvideo.c:38
CinVideoBitmapIndex
Definition: dsicinvideo.c:31
AVCodec.
Definition: avcodec.h:2796
AVCodec ff_dsicinvideo_decoder
Definition: dsicinvideo.c:291
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define b
Definition: input.c:52
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:188
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
#define FFMIN3(a, b, c)
Definition: common.h:58
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:74
#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:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
uint8_t * bitmap_table[3]
Definition: dsicinvideo.c:42
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
Definition: dsicinvideo.c:45
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:808
static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
Definition: dsicinvideo.c:278
#define FFMIN(a, b)
Definition: common.h:57
int width
picture width / height.
Definition: avcodec.h:1224
static int cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:109
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:330
#define AV_RL24
Definition: intreadwrite.h:78
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
common internal api header.
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int len
unsigned int bitmap_size
Definition: dsicinvideo.c:40
#define FFSWAP(type, a, b)
Definition: common.h:60
static void cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
Definition: dsicinvideo.c:144
uint32_t palette[256]
Definition: dsicinvideo.c:41
This structure stores compressed data.
Definition: avcodec.h:950
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
static int cinvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dsicinvideo.c:167