check for request_channels at codec init
[ffmpeg-lucabe.git] / libavcodec / cinepak.c
blobb1399f997f5f141eb3470fad77ced1d6e46020a4
1 /*
2 * Cinepak Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file cinepak.c
24 * Cinepak video decoder
25 * by Ewald Snel <ewald@rambo.its.tudelft.nl>
26 * For more information on the Cinepak algorithm, visit:
27 * http://www.csse.monash.edu.au/~timf/
28 * For more information on the quirky data inside Sega FILM/CPK files, visit:
29 * http://wiki.multimedia.cx/index.php?title=Sega_FILM
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "avcodec.h"
38 #include "dsputil.h"
41 typedef struct {
42 uint8_t y0, y1, y2, y3;
43 uint8_t u, v;
44 } cvid_codebook_t;
46 #define MAX_STRIPS 32
48 typedef struct {
49 uint16_t id;
50 uint16_t x1, y1;
51 uint16_t x2, y2;
52 cvid_codebook_t v4_codebook[256];
53 cvid_codebook_t v1_codebook[256];
54 } cvid_strip_t;
56 typedef struct CinepakContext {
58 AVCodecContext *avctx;
59 DSPContext dsp;
60 AVFrame frame;
62 unsigned char *data;
63 int size;
65 int width, height;
67 int palette_video;
68 cvid_strip_t strips[MAX_STRIPS];
70 int sega_film_skip_bytes;
72 } CinepakContext;
74 static void cinepak_decode_codebook (cvid_codebook_t *codebook,
75 int chunk_id, int size, uint8_t *data)
77 uint8_t *eod = (data + size);
78 uint32_t flag, mask;
79 int i, n;
81 /* check if this chunk contains 4- or 6-element vectors */
82 n = (chunk_id & 0x0400) ? 4 : 6;
83 flag = 0;
84 mask = 0;
86 for (i=0; i < 256; i++) {
87 if ((chunk_id & 0x0100) && !(mask >>= 1)) {
88 if ((data + 4) > eod)
89 break;
91 flag = AV_RB32 (data);
92 data += 4;
93 mask = 0x80000000;
96 if (!(chunk_id & 0x0100) || (flag & mask)) {
97 if ((data + n) > eod)
98 break;
100 if (n == 6) {
101 codebook[i].y0 = *data++;
102 codebook[i].y1 = *data++;
103 codebook[i].y2 = *data++;
104 codebook[i].y3 = *data++;
105 codebook[i].u = 128 + *data++;
106 codebook[i].v = 128 + *data++;
107 } else {
108 /* this codebook type indicates either greyscale or
109 * palettized video; if palettized, U & V components will
110 * not be used so it is safe to set them to 128 for the
111 * benefit of greyscale rendering in YUV420P */
112 codebook[i].y0 = *data++;
113 codebook[i].y1 = *data++;
114 codebook[i].y2 = *data++;
115 codebook[i].y3 = *data++;
116 codebook[i].u = 128;
117 codebook[i].v = 128;
123 static int cinepak_decode_vectors (CinepakContext *s, cvid_strip_t *strip,
124 int chunk_id, int size, uint8_t *data)
126 uint8_t *eod = (data + size);
127 uint32_t flag, mask;
128 cvid_codebook_t *codebook;
129 unsigned int x, y;
130 uint32_t iy[4];
131 uint32_t iu[2];
132 uint32_t iv[2];
134 flag = 0;
135 mask = 0;
137 for (y=strip->y1; y < strip->y2; y+=4) {
139 iy[0] = strip->x1 + (y * s->frame.linesize[0]);
140 iy[1] = iy[0] + s->frame.linesize[0];
141 iy[2] = iy[1] + s->frame.linesize[0];
142 iy[3] = iy[2] + s->frame.linesize[0];
143 iu[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[1]);
144 iu[1] = iu[0] + s->frame.linesize[1];
145 iv[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[2]);
146 iv[1] = iv[0] + s->frame.linesize[2];
148 for (x=strip->x1; x < strip->x2; x+=4) {
149 if ((chunk_id & 0x0100) && !(mask >>= 1)) {
150 if ((data + 4) > eod)
151 return -1;
153 flag = AV_RB32 (data);
154 data += 4;
155 mask = 0x80000000;
158 if (!(chunk_id & 0x0100) || (flag & mask)) {
159 if (!(chunk_id & 0x0200) && !(mask >>= 1)) {
160 if ((data + 4) > eod)
161 return -1;
163 flag = AV_RB32 (data);
164 data += 4;
165 mask = 0x80000000;
168 if ((chunk_id & 0x0200) || (~flag & mask)) {
169 if (data >= eod)
170 return -1;
172 codebook = &strip->v1_codebook[*data++];
173 s->frame.data[0][iy[0] + 0] = codebook->y0;
174 s->frame.data[0][iy[0] + 1] = codebook->y0;
175 s->frame.data[0][iy[1] + 0] = codebook->y0;
176 s->frame.data[0][iy[1] + 1] = codebook->y0;
177 if (!s->palette_video) {
178 s->frame.data[1][iu[0]] = codebook->u;
179 s->frame.data[2][iv[0]] = codebook->v;
182 s->frame.data[0][iy[0] + 2] = codebook->y1;
183 s->frame.data[0][iy[0] + 3] = codebook->y1;
184 s->frame.data[0][iy[1] + 2] = codebook->y1;
185 s->frame.data[0][iy[1] + 3] = codebook->y1;
186 if (!s->palette_video) {
187 s->frame.data[1][iu[0] + 1] = codebook->u;
188 s->frame.data[2][iv[0] + 1] = codebook->v;
191 s->frame.data[0][iy[2] + 0] = codebook->y2;
192 s->frame.data[0][iy[2] + 1] = codebook->y2;
193 s->frame.data[0][iy[3] + 0] = codebook->y2;
194 s->frame.data[0][iy[3] + 1] = codebook->y2;
195 if (!s->palette_video) {
196 s->frame.data[1][iu[1]] = codebook->u;
197 s->frame.data[2][iv[1]] = codebook->v;
200 s->frame.data[0][iy[2] + 2] = codebook->y3;
201 s->frame.data[0][iy[2] + 3] = codebook->y3;
202 s->frame.data[0][iy[3] + 2] = codebook->y3;
203 s->frame.data[0][iy[3] + 3] = codebook->y3;
204 if (!s->palette_video) {
205 s->frame.data[1][iu[1] + 1] = codebook->u;
206 s->frame.data[2][iv[1] + 1] = codebook->v;
209 } else if (flag & mask) {
210 if ((data + 4) > eod)
211 return -1;
213 codebook = &strip->v4_codebook[*data++];
214 s->frame.data[0][iy[0] + 0] = codebook->y0;
215 s->frame.data[0][iy[0] + 1] = codebook->y1;
216 s->frame.data[0][iy[1] + 0] = codebook->y2;
217 s->frame.data[0][iy[1] + 1] = codebook->y3;
218 if (!s->palette_video) {
219 s->frame.data[1][iu[0]] = codebook->u;
220 s->frame.data[2][iv[0]] = codebook->v;
223 codebook = &strip->v4_codebook[*data++];
224 s->frame.data[0][iy[0] + 2] = codebook->y0;
225 s->frame.data[0][iy[0] + 3] = codebook->y1;
226 s->frame.data[0][iy[1] + 2] = codebook->y2;
227 s->frame.data[0][iy[1] + 3] = codebook->y3;
228 if (!s->palette_video) {
229 s->frame.data[1][iu[0] + 1] = codebook->u;
230 s->frame.data[2][iv[0] + 1] = codebook->v;
233 codebook = &strip->v4_codebook[*data++];
234 s->frame.data[0][iy[2] + 0] = codebook->y0;
235 s->frame.data[0][iy[2] + 1] = codebook->y1;
236 s->frame.data[0][iy[3] + 0] = codebook->y2;
237 s->frame.data[0][iy[3] + 1] = codebook->y3;
238 if (!s->palette_video) {
239 s->frame.data[1][iu[1]] = codebook->u;
240 s->frame.data[2][iv[1]] = codebook->v;
243 codebook = &strip->v4_codebook[*data++];
244 s->frame.data[0][iy[2] + 2] = codebook->y0;
245 s->frame.data[0][iy[2] + 3] = codebook->y1;
246 s->frame.data[0][iy[3] + 2] = codebook->y2;
247 s->frame.data[0][iy[3] + 3] = codebook->y3;
248 if (!s->palette_video) {
249 s->frame.data[1][iu[1] + 1] = codebook->u;
250 s->frame.data[2][iv[1] + 1] = codebook->v;
256 iy[0] += 4; iy[1] += 4;
257 iy[2] += 4; iy[3] += 4;
258 iu[0] += 2; iu[1] += 2;
259 iv[0] += 2; iv[1] += 2;
263 return 0;
266 static int cinepak_decode_strip (CinepakContext *s,
267 cvid_strip_t *strip, uint8_t *data, int size)
269 uint8_t *eod = (data + size);
270 int chunk_id, chunk_size;
272 /* coordinate sanity checks */
273 if (strip->x1 >= s->width || strip->x2 > s->width ||
274 strip->y1 >= s->height || strip->y2 > s->height ||
275 strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
276 return -1;
278 while ((data + 4) <= eod) {
279 chunk_id = AV_RB16 (&data[0]);
280 chunk_size = AV_RB16 (&data[2]) - 4;
281 if(chunk_size < 0)
282 return -1;
284 data += 4;
285 chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
287 switch (chunk_id) {
289 case 0x2000:
290 case 0x2100:
291 case 0x2400:
292 case 0x2500:
293 cinepak_decode_codebook (strip->v4_codebook, chunk_id,
294 chunk_size, data);
295 break;
297 case 0x2200:
298 case 0x2300:
299 case 0x2600:
300 case 0x2700:
301 cinepak_decode_codebook (strip->v1_codebook, chunk_id,
302 chunk_size, data);
303 break;
305 case 0x3000:
306 case 0x3100:
307 case 0x3200:
308 return cinepak_decode_vectors (s, strip, chunk_id,
309 chunk_size, data);
312 data += chunk_size;
315 return -1;
318 static int cinepak_decode (CinepakContext *s)
320 uint8_t *eod = (s->data + s->size);
321 int i, result, strip_size, frame_flags, num_strips;
322 int y0 = 0;
323 int encoded_buf_size;
325 if (s->size < 10)
326 return -1;
328 frame_flags = s->data[0];
329 num_strips = AV_RB16 (&s->data[8]);
330 encoded_buf_size = ((s->data[1] << 16) | AV_RB16 (&s->data[2]));
332 /* if this is the first frame, check for deviant Sega FILM data */
333 if (s->sega_film_skip_bytes == -1) {
334 if (encoded_buf_size != s->size) {
335 /* If the encoded frame size differs from the frame size as indicated
336 * by the container file, this data likely comes from a Sega FILM/CPK file.
337 * If the frame header is followed by the bytes FE 00 00 06 00 00 then
338 * this is probably one of the two known files that have 6 extra bytes
339 * after the frame header. Else, assume 2 extra bytes. */
340 if ((s->data[10] == 0xFE) &&
341 (s->data[11] == 0x00) &&
342 (s->data[12] == 0x00) &&
343 (s->data[13] == 0x06) &&
344 (s->data[14] == 0x00) &&
345 (s->data[15] == 0x00))
346 s->sega_film_skip_bytes = 6;
347 else
348 s->sega_film_skip_bytes = 2;
349 } else
350 s->sega_film_skip_bytes = 0;
353 s->data += 10 + s->sega_film_skip_bytes;
355 if (num_strips > MAX_STRIPS)
356 num_strips = MAX_STRIPS;
358 for (i=0; i < num_strips; i++) {
359 if ((s->data + 12) > eod)
360 return -1;
362 s->strips[i].id = AV_RB16 (s->data);
363 s->strips[i].y1 = y0;
364 s->strips[i].x1 = 0;
365 s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
366 s->strips[i].x2 = s->avctx->width;
368 strip_size = AV_RB16 (&s->data[2]) - 12;
369 s->data += 12;
370 strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
372 if ((i > 0) && !(frame_flags & 0x01)) {
373 memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
374 sizeof(s->strips[i].v4_codebook));
375 memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
376 sizeof(s->strips[i].v1_codebook));
379 result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
381 if (result != 0)
382 return result;
384 s->data += strip_size;
385 y0 = s->strips[i].y2;
387 return 0;
390 static int cinepak_decode_init(AVCodecContext *avctx)
392 CinepakContext *s = avctx->priv_data;
394 s->avctx = avctx;
395 s->width = (avctx->width + 3) & ~3;
396 s->height = (avctx->height + 3) & ~3;
397 s->sega_film_skip_bytes = -1; /* uninitialized state */
399 // check for paletted data
400 if ((avctx->palctrl == NULL) || (avctx->bits_per_sample == 40)) {
401 s->palette_video = 0;
402 avctx->pix_fmt = PIX_FMT_YUV420P;
403 } else {
404 s->palette_video = 1;
405 avctx->pix_fmt = PIX_FMT_PAL8;
408 dsputil_init(&s->dsp, avctx);
410 s->frame.data[0] = NULL;
412 return 0;
415 static int cinepak_decode_frame(AVCodecContext *avctx,
416 void *data, int *data_size,
417 uint8_t *buf, int buf_size)
419 CinepakContext *s = avctx->priv_data;
421 s->data = buf;
422 s->size = buf_size;
424 s->frame.reference = 1;
425 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
426 FF_BUFFER_HINTS_REUSABLE;
427 if (avctx->reget_buffer(avctx, &s->frame)) {
428 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
429 return -1;
432 cinepak_decode(s);
434 if (s->palette_video) {
435 memcpy (s->frame.data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
436 if (avctx->palctrl->palette_changed) {
437 s->frame.palette_has_changed = 1;
438 avctx->palctrl->palette_changed = 0;
439 } else
440 s->frame.palette_has_changed = 0;
443 *data_size = sizeof(AVFrame);
444 *(AVFrame*)data = s->frame;
446 /* report that the buffer was completely consumed */
447 return buf_size;
450 static int cinepak_decode_end(AVCodecContext *avctx)
452 CinepakContext *s = avctx->priv_data;
454 if (s->frame.data[0])
455 avctx->release_buffer(avctx, &s->frame);
457 return 0;
460 AVCodec cinepak_decoder = {
461 "cinepak",
462 CODEC_TYPE_VIDEO,
463 CODEC_ID_CINEPAK,
464 sizeof(CinepakContext),
465 cinepak_decode_init,
466 NULL,
467 cinepak_decode_end,
468 cinepak_decode_frame,
469 CODEC_CAP_DR1,