Libav
cinepak.c
Go to the documentation of this file.
1 /*
2  * Cinepak Video Decoder
3  * Copyright (C) 2003 the ffmpeg project
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 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "libavutil/common.h"
38 #include "libavutil/intreadwrite.h"
39 #include "avcodec.h"
40 #include "internal.h"
41 
42 
43 typedef struct {
44  uint8_t y0, y1, y2, y3;
45  uint8_t u, v;
47 
48 #define MAX_STRIPS 32
49 
50 typedef struct {
51  uint16_t id;
52  uint16_t x1, y1;
53  uint16_t x2, y2;
54  cvid_codebook v4_codebook[256];
55  cvid_codebook v1_codebook[256];
56 } cvid_strip;
57 
58 typedef struct CinepakContext {
59 
62 
63  const unsigned char *data;
64  int size;
65 
66  int width, height;
67 
70 
72 
73  uint32_t pal[256];
75 
76 static void cinepak_decode_codebook (cvid_codebook *codebook,
77  int chunk_id, int size, const uint8_t *data)
78 {
79  const uint8_t *eod = (data + size);
80  uint32_t flag, mask;
81  int i, n;
82 
83  /* check if this chunk contains 4- or 6-element vectors */
84  n = (chunk_id & 0x04) ? 4 : 6;
85  flag = 0;
86  mask = 0;
87 
88  for (i=0; i < 256; i++) {
89  if ((chunk_id & 0x01) && !(mask >>= 1)) {
90  if ((data + 4) > eod)
91  break;
92 
93  flag = AV_RB32 (data);
94  data += 4;
95  mask = 0x80000000;
96  }
97 
98  if (!(chunk_id & 0x01) || (flag & mask)) {
99  if ((data + n) > eod)
100  break;
101 
102  if (n == 6) {
103  codebook[i].y0 = *data++;
104  codebook[i].y1 = *data++;
105  codebook[i].y2 = *data++;
106  codebook[i].y3 = *data++;
107  codebook[i].u = 128 + *data++;
108  codebook[i].v = 128 + *data++;
109  } else {
110  /* this codebook type indicates either greyscale or
111  * palettized video; if palettized, U & V components will
112  * not be used so it is safe to set them to 128 for the
113  * benefit of greyscale rendering in YUV420P */
114  codebook[i].y0 = *data++;
115  codebook[i].y1 = *data++;
116  codebook[i].y2 = *data++;
117  codebook[i].y3 = *data++;
118  codebook[i].u = 128;
119  codebook[i].v = 128;
120  }
121  }
122  }
123 }
124 
126  int chunk_id, int size, const uint8_t *data)
127 {
128  const uint8_t *eod = (data + size);
129  uint32_t flag, mask;
130  cvid_codebook *codebook;
131  unsigned int x, y;
132  uint32_t iy[4];
133  uint32_t iu[2];
134  uint32_t iv[2];
135 
136  flag = 0;
137  mask = 0;
138 
139  for (y=strip->y1; y < strip->y2; y+=4) {
140 
141  iy[0] = strip->x1 + (y * s->frame->linesize[0]);
142  iy[1] = iy[0] + s->frame->linesize[0];
143  iy[2] = iy[1] + s->frame->linesize[0];
144  iy[3] = iy[2] + s->frame->linesize[0];
145  iu[0] = (strip->x1/2) + ((y/2) * s->frame->linesize[1]);
146  iu[1] = iu[0] + s->frame->linesize[1];
147  iv[0] = (strip->x1/2) + ((y/2) * s->frame->linesize[2]);
148  iv[1] = iv[0] + s->frame->linesize[2];
149 
150  for (x=strip->x1; x < strip->x2; x+=4) {
151  if ((chunk_id & 0x01) && !(mask >>= 1)) {
152  if ((data + 4) > eod)
153  return AVERROR_INVALIDDATA;
154 
155  flag = AV_RB32 (data);
156  data += 4;
157  mask = 0x80000000;
158  }
159 
160  if (!(chunk_id & 0x01) || (flag & mask)) {
161  if (!(chunk_id & 0x02) && !(mask >>= 1)) {
162  if ((data + 4) > eod)
163  return AVERROR_INVALIDDATA;
164 
165  flag = AV_RB32 (data);
166  data += 4;
167  mask = 0x80000000;
168  }
169 
170  if ((chunk_id & 0x02) || (~flag & mask)) {
171  if (data >= eod)
172  return AVERROR_INVALIDDATA;
173 
174  codebook = &strip->v1_codebook[*data++];
175  s->frame->data[0][iy[0] + 0] = codebook->y0;
176  s->frame->data[0][iy[0] + 1] = codebook->y0;
177  s->frame->data[0][iy[1] + 0] = codebook->y0;
178  s->frame->data[0][iy[1] + 1] = codebook->y0;
179  if (!s->palette_video) {
180  s->frame->data[1][iu[0]] = codebook->u;
181  s->frame->data[2][iv[0]] = codebook->v;
182  }
183 
184  s->frame->data[0][iy[0] + 2] = codebook->y1;
185  s->frame->data[0][iy[0] + 3] = codebook->y1;
186  s->frame->data[0][iy[1] + 2] = codebook->y1;
187  s->frame->data[0][iy[1] + 3] = codebook->y1;
188  if (!s->palette_video) {
189  s->frame->data[1][iu[0] + 1] = codebook->u;
190  s->frame->data[2][iv[0] + 1] = codebook->v;
191  }
192 
193  s->frame->data[0][iy[2] + 0] = codebook->y2;
194  s->frame->data[0][iy[2] + 1] = codebook->y2;
195  s->frame->data[0][iy[3] + 0] = codebook->y2;
196  s->frame->data[0][iy[3] + 1] = codebook->y2;
197  if (!s->palette_video) {
198  s->frame->data[1][iu[1]] = codebook->u;
199  s->frame->data[2][iv[1]] = codebook->v;
200  }
201 
202  s->frame->data[0][iy[2] + 2] = codebook->y3;
203  s->frame->data[0][iy[2] + 3] = codebook->y3;
204  s->frame->data[0][iy[3] + 2] = codebook->y3;
205  s->frame->data[0][iy[3] + 3] = codebook->y3;
206  if (!s->palette_video) {
207  s->frame->data[1][iu[1] + 1] = codebook->u;
208  s->frame->data[2][iv[1] + 1] = codebook->v;
209  }
210 
211  } else if (flag & mask) {
212  if ((data + 4) > eod)
213  return AVERROR_INVALIDDATA;
214 
215  codebook = &strip->v4_codebook[*data++];
216  s->frame->data[0][iy[0] + 0] = codebook->y0;
217  s->frame->data[0][iy[0] + 1] = codebook->y1;
218  s->frame->data[0][iy[1] + 0] = codebook->y2;
219  s->frame->data[0][iy[1] + 1] = codebook->y3;
220  if (!s->palette_video) {
221  s->frame->data[1][iu[0]] = codebook->u;
222  s->frame->data[2][iv[0]] = codebook->v;
223  }
224 
225  codebook = &strip->v4_codebook[*data++];
226  s->frame->data[0][iy[0] + 2] = codebook->y0;
227  s->frame->data[0][iy[0] + 3] = codebook->y1;
228  s->frame->data[0][iy[1] + 2] = codebook->y2;
229  s->frame->data[0][iy[1] + 3] = codebook->y3;
230  if (!s->palette_video) {
231  s->frame->data[1][iu[0] + 1] = codebook->u;
232  s->frame->data[2][iv[0] + 1] = codebook->v;
233  }
234 
235  codebook = &strip->v4_codebook[*data++];
236  s->frame->data[0][iy[2] + 0] = codebook->y0;
237  s->frame->data[0][iy[2] + 1] = codebook->y1;
238  s->frame->data[0][iy[3] + 0] = codebook->y2;
239  s->frame->data[0][iy[3] + 1] = codebook->y3;
240  if (!s->palette_video) {
241  s->frame->data[1][iu[1]] = codebook->u;
242  s->frame->data[2][iv[1]] = codebook->v;
243  }
244 
245  codebook = &strip->v4_codebook[*data++];
246  s->frame->data[0][iy[2] + 2] = codebook->y0;
247  s->frame->data[0][iy[2] + 3] = codebook->y1;
248  s->frame->data[0][iy[3] + 2] = codebook->y2;
249  s->frame->data[0][iy[3] + 3] = codebook->y3;
250  if (!s->palette_video) {
251  s->frame->data[1][iu[1] + 1] = codebook->u;
252  s->frame->data[2][iv[1] + 1] = codebook->v;
253  }
254 
255  }
256  }
257 
258  iy[0] += 4; iy[1] += 4;
259  iy[2] += 4; iy[3] += 4;
260  iu[0] += 2; iu[1] += 2;
261  iv[0] += 2; iv[1] += 2;
262  }
263  }
264 
265  return 0;
266 }
267 
269  cvid_strip *strip, const uint8_t *data, int size)
270 {
271  const uint8_t *eod = (data + size);
272  int chunk_id, chunk_size;
273 
274  /* coordinate sanity checks */
275  if (strip->x2 > s->width ||
276  strip->y2 > s->height ||
277  strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
278  return AVERROR_INVALIDDATA;
279 
280  while ((data + 4) <= eod) {
281  chunk_id = data[0];
282  chunk_size = AV_RB24 (&data[1]) - 4;
283  if(chunk_size < 0)
284  return AVERROR_INVALIDDATA;
285 
286  data += 4;
287  chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
288 
289  switch (chunk_id) {
290 
291  case 0x20:
292  case 0x21:
293  case 0x24:
294  case 0x25:
295  cinepak_decode_codebook (strip->v4_codebook, chunk_id,
296  chunk_size, data);
297  break;
298 
299  case 0x22:
300  case 0x23:
301  case 0x26:
302  case 0x27:
303  cinepak_decode_codebook (strip->v1_codebook, chunk_id,
304  chunk_size, data);
305  break;
306 
307  case 0x30:
308  case 0x31:
309  case 0x32:
310  return cinepak_decode_vectors (s, strip, chunk_id,
311  chunk_size, data);
312  }
313 
314  data += chunk_size;
315  }
316 
317  return AVERROR_INVALIDDATA;
318 }
319 
321 {
322  const uint8_t *eod = (s->data + s->size);
323  int i, result, strip_size, frame_flags, num_strips;
324  int y0 = 0;
325  int encoded_buf_size;
326 
327  if (s->size < 10)
328  return AVERROR_INVALIDDATA;
329 
330  frame_flags = s->data[0];
331  num_strips = AV_RB16 (&s->data[8]);
332  encoded_buf_size = AV_RB24(&s->data[1]);
333 
334  /* if this is the first frame, check for deviant Sega FILM data */
335  if (s->sega_film_skip_bytes == -1) {
336  if (!encoded_buf_size) {
337  avpriv_request_sample(s->avctx, "encoded_buf_size 0");
338  return AVERROR_PATCHWELCOME;
339  }
340  if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
341  /* If the encoded frame size differs from the frame size as indicated
342  * by the container file, this data likely comes from a Sega FILM/CPK file.
343  * If the frame header is followed by the bytes FE 00 00 06 00 00 then
344  * this is probably one of the two known files that have 6 extra bytes
345  * after the frame header. Else, assume 2 extra bytes. The container
346  * size also cannot be a multiple of the encoded size. */
347  if (s->size >= 16 &&
348  (s->data[10] == 0xFE) &&
349  (s->data[11] == 0x00) &&
350  (s->data[12] == 0x00) &&
351  (s->data[13] == 0x06) &&
352  (s->data[14] == 0x00) &&
353  (s->data[15] == 0x00))
354  s->sega_film_skip_bytes = 6;
355  else
356  s->sega_film_skip_bytes = 2;
357  } else
358  s->sega_film_skip_bytes = 0;
359  }
360 
361  s->data += 10 + s->sega_film_skip_bytes;
362 
363  num_strips = FFMIN(num_strips, MAX_STRIPS);
364 
365  for (i=0; i < num_strips; i++) {
366  if ((s->data + 12) > eod)
367  return AVERROR_INVALIDDATA;
368 
369  s->strips[i].id = s->data[0];
370  s->strips[i].y1 = y0;
371  s->strips[i].x1 = 0;
372  s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
373  s->strips[i].x2 = s->avctx->width;
374 
375  strip_size = AV_RB24 (&s->data[1]) - 12;
376  if (strip_size < 0)
377  return AVERROR_INVALIDDATA;
378  s->data += 12;
379  strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
380 
381  if ((i > 0) && !(frame_flags & 0x01)) {
382  memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
383  sizeof(s->strips[i].v4_codebook));
384  memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
385  sizeof(s->strips[i].v1_codebook));
386  }
387 
388  result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
389 
390  if (result != 0)
391  return result;
392 
393  s->data += strip_size;
394  y0 = s->strips[i].y2;
395  }
396  return 0;
397 }
398 
400 {
401  CinepakContext *s = avctx->priv_data;
402 
403  s->avctx = avctx;
404  s->width = (avctx->width + 3) & ~3;
405  s->height = (avctx->height + 3) & ~3;
406  s->sega_film_skip_bytes = -1; /* uninitialized state */
407 
408  // check for paletted data
409  if (avctx->bits_per_coded_sample != 8) {
410  s->palette_video = 0;
411  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
412  } else {
413  s->palette_video = 1;
414  avctx->pix_fmt = AV_PIX_FMT_PAL8;
415  }
416 
417  s->frame = av_frame_alloc();
418  if (!s->frame)
419  return AVERROR(ENOMEM);
420 
421  return 0;
422 }
423 
425  void *data, int *got_frame,
426  AVPacket *avpkt)
427 {
428  const uint8_t *buf = avpkt->data;
429  int ret = 0, buf_size = avpkt->size;
430  CinepakContext *s = avctx->priv_data;
431 
432  s->data = buf;
433  s->size = buf_size;
434 
435  if ((ret = ff_reget_buffer(avctx, s->frame))) {
436  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
437  return ret;
438  }
439 
440  if (s->palette_video) {
442  if (pal) {
443  s->frame->palette_has_changed = 1;
444  memcpy(s->pal, pal, AVPALETTE_SIZE);
445  }
446  }
447 
448  cinepak_decode(s);
449 
450  if (s->palette_video)
451  memcpy (s->frame->data[1], s->pal, AVPALETTE_SIZE);
452 
453  if ((ret = av_frame_ref(data, s->frame)) < 0)
454  return ret;
455 
456  *got_frame = 1;
457 
458  /* report that the buffer was completely consumed */
459  return buf_size;
460 }
461 
463 {
464  CinepakContext *s = avctx->priv_data;
465 
466  av_frame_free(&s->frame);
467 
468  return 0;
469 }
470 
472  .name = "cinepak",
473  .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
474  .type = AVMEDIA_TYPE_VIDEO,
475  .id = AV_CODEC_ID_CINEPAK,
476  .priv_data_size = sizeof(CinepakContext),
480  .capabilities = CODEC_CAP_DR1,
481 };
#define AVPALETTE_SIZE
Definition: avcodec.h:3029
cvid_strip strips[MAX_STRIPS]
Definition: cinepak.c:69
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
uint16_t x1
Definition: cinepak.c:52
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
cvid_codebook v4_codebook[256]
Definition: cinepak.c:54
static int cinepak_decode(CinepakContext *s)
Definition: cinepak.c:320
int size
Definition: avcodec.h:968
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1248
AVCodec.
Definition: avcodec.h:2790
AVCodec ff_cinepak_decoder
Definition: cinepak.c:471
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int cinepak_decode_strip(CinepakContext *s, cvid_strip *strip, const uint8_t *data, int size)
Definition: cinepak.c:268
static int cinepak_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cinepak.c:424
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 AV_RB32
Definition: intreadwrite.h:130
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:967
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2501
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static const uint16_t mask[17]
Definition: lzw.c:38
#define AV_RB16
Definition: intreadwrite.h:53
#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
uint16_t y2
Definition: cinepak.c:53
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:2797
uint16_t x2
Definition: cinepak.c:53
AVFrame * frame
Definition: cinepak.c:61
const unsigned char * data
Definition: cinepak.c:63
int sega_film_skip_bytes
Definition: cinepak.c:71
uint8_t y2
Definition: cinepak.c:44
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:799
#define FFMIN(a, b)
Definition: common.h:57
int width
picture width / height.
Definition: avcodec.h:1218
#define MAX_STRIPS
Definition: cinepak.c:48
uint8_t v
Definition: cinepak.c:45
uint16_t y1
Definition: cinepak.c:52
static int cinepak_decode_vectors(CinepakContext *s, cvid_strip *strip, int chunk_id, int size, const uint8_t *data)
Definition: cinepak.c:125
uint8_t u
Definition: cinepak.c:45
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
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:1044
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
static av_cold int cinepak_decode_init(AVCodecContext *avctx)
Definition: cinepak.c:399
static void cinepak_decode_codebook(cvid_codebook *codebook, int chunk_id, int size, const uint8_t *data)
Definition: cinepak.c:76
AVCodecContext * avctx
Definition: cinepak.c:60
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:330
uint8_t y3
Definition: cinepak.c:44
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
uint16_t id
Definition: cinepak.c:51
int palette_video
Definition: cinepak.c:68
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
uint8_t y0
Definition: cinepak.c:44
common internal api header.
common internal and external API header
uint32_t pal[256]
Definition: cinepak.c:73
cvid_codebook v1_codebook[256]
Definition: cinepak.c:55
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1086
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:287
uint8_t y1
Definition: cinepak.c:44
This structure stores compressed data.
Definition: avcodec.h:944
static av_cold int cinepak_decode_end(AVCodecContext *avctx)
Definition: cinepak.c:462