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
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
36 #include "libavutil/intreadwrite.h"
41 uint8_t y0, y1, y2, y3;
51 cvid_codebook v4_codebook[256];
52 cvid_codebook v1_codebook[256];
55 typedef struct CinepakContext {
57 AVCodecContext *avctx;
60 const unsigned char *data;
66 cvid_strip strips[MAX_STRIPS];
68 int sega_film_skip_bytes;
73 static void cinepak_decode_codebook (cvid_codebook *codebook,
74 int chunk_id, int size, const uint8_t *data)
76 const uint8_t *eod = (data + size);
80 /* check if this chunk contains 4- or 6-element vectors */
81 n = (chunk_id & 0x04) ? 4 : 6;
85 for (i=0; i < 256; i++) {
86 if ((chunk_id & 0x01) && !(mask >>= 1)) {
90 flag = AV_RB32 (data);
95 if (!(chunk_id & 0x01) || (flag & mask)) {
100 codebook[i].y0 = *data++;
101 codebook[i].y1 = *data++;
102 codebook[i].y2 = *data++;
103 codebook[i].y3 = *data++;
104 codebook[i].u = 128 + *data++;
105 codebook[i].v = 128 + *data++;
107 /* this codebook type indicates either greyscale or
108 * palettized video; if palettized, U & V components will
109 * not be used so it is safe to set them to 128 for the
110 * benefit of greyscale rendering in YUV420P */
111 codebook[i].y0 = *data++;
112 codebook[i].y1 = *data++;
113 codebook[i].y2 = *data++;
114 codebook[i].y3 = *data++;
122 static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
123 int chunk_id, int size, const uint8_t *data)
125 const uint8_t *eod = (data + size);
127 cvid_codebook *codebook;
136 for (y=strip->y1; y < strip->y2; y+=4) {
138 iy[0] = strip->x1 + (y * s->frame.linesize[0]);
139 iy[1] = iy[0] + s->frame.linesize[0];
140 iy[2] = iy[1] + s->frame.linesize[0];
141 iy[3] = iy[2] + s->frame.linesize[0];
142 iu[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[1]);
143 iu[1] = iu[0] + s->frame.linesize[1];
144 iv[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[2]);
145 iv[1] = iv[0] + s->frame.linesize[2];
147 for (x=strip->x1; x < strip->x2; x+=4) {
148 if ((chunk_id & 0x01) && !(mask >>= 1)) {
149 if ((data + 4) > eod)
150 return AVERROR_INVALIDDATA;
152 flag = AV_RB32 (data);
157 if (!(chunk_id & 0x01) || (flag & mask)) {
158 if (!(chunk_id & 0x02) && !(mask >>= 1)) {
159 if ((data + 4) > eod)
160 return AVERROR_INVALIDDATA;
162 flag = AV_RB32 (data);
167 if ((chunk_id & 0x02) || (~flag & mask)) {
169 return AVERROR_INVALIDDATA;
171 codebook = &strip->v1_codebook[*data++];
172 s->frame.data[0][iy[0] + 0] = codebook->y0;
173 s->frame.data[0][iy[0] + 1] = codebook->y0;
174 s->frame.data[0][iy[1] + 0] = codebook->y0;
175 s->frame.data[0][iy[1] + 1] = codebook->y0;
176 if (!s->palette_video) {
177 s->frame.data[1][iu[0]] = codebook->u;
178 s->frame.data[2][iv[0]] = codebook->v;
181 s->frame.data[0][iy[0] + 2] = codebook->y1;
182 s->frame.data[0][iy[0] + 3] = codebook->y1;
183 s->frame.data[0][iy[1] + 2] = codebook->y1;
184 s->frame.data[0][iy[1] + 3] = codebook->y1;
185 if (!s->palette_video) {
186 s->frame.data[1][iu[0] + 1] = codebook->u;
187 s->frame.data[2][iv[0] + 1] = codebook->v;
190 s->frame.data[0][iy[2] + 0] = codebook->y2;
191 s->frame.data[0][iy[2] + 1] = codebook->y2;
192 s->frame.data[0][iy[3] + 0] = codebook->y2;
193 s->frame.data[0][iy[3] + 1] = codebook->y2;
194 if (!s->palette_video) {
195 s->frame.data[1][iu[1]] = codebook->u;
196 s->frame.data[2][iv[1]] = codebook->v;
199 s->frame.data[0][iy[2] + 2] = codebook->y3;
200 s->frame.data[0][iy[2] + 3] = codebook->y3;
201 s->frame.data[0][iy[3] + 2] = codebook->y3;
202 s->frame.data[0][iy[3] + 3] = codebook->y3;
203 if (!s->palette_video) {
204 s->frame.data[1][iu[1] + 1] = codebook->u;
205 s->frame.data[2][iv[1] + 1] = codebook->v;
208 } else if (flag & mask) {
209 if ((data + 4) > eod)
210 return AVERROR_INVALIDDATA;
212 codebook = &strip->v4_codebook[*data++];
213 s->frame.data[0][iy[0] + 0] = codebook->y0;
214 s->frame.data[0][iy[0] + 1] = codebook->y1;
215 s->frame.data[0][iy[1] + 0] = codebook->y2;
216 s->frame.data[0][iy[1] + 1] = codebook->y3;
217 if (!s->palette_video) {
218 s->frame.data[1][iu[0]] = codebook->u;
219 s->frame.data[2][iv[0]] = codebook->v;
222 codebook = &strip->v4_codebook[*data++];
223 s->frame.data[0][iy[0] + 2] = codebook->y0;
224 s->frame.data[0][iy[0] + 3] = codebook->y1;
225 s->frame.data[0][iy[1] + 2] = codebook->y2;
226 s->frame.data[0][iy[1] + 3] = codebook->y3;
227 if (!s->palette_video) {
228 s->frame.data[1][iu[0] + 1] = codebook->u;
229 s->frame.data[2][iv[0] + 1] = codebook->v;
232 codebook = &strip->v4_codebook[*data++];
233 s->frame.data[0][iy[2] + 0] = codebook->y0;
234 s->frame.data[0][iy[2] + 1] = codebook->y1;
235 s->frame.data[0][iy[3] + 0] = codebook->y2;
236 s->frame.data[0][iy[3] + 1] = codebook->y3;
237 if (!s->palette_video) {
238 s->frame.data[1][iu[1]] = codebook->u;
239 s->frame.data[2][iv[1]] = codebook->v;
242 codebook = &strip->v4_codebook[*data++];
243 s->frame.data[0][iy[2] + 2] = codebook->y0;
244 s->frame.data[0][iy[2] + 3] = codebook->y1;
245 s->frame.data[0][iy[3] + 2] = codebook->y2;
246 s->frame.data[0][iy[3] + 3] = codebook->y3;
247 if (!s->palette_video) {
248 s->frame.data[1][iu[1] + 1] = codebook->u;
249 s->frame.data[2][iv[1] + 1] = codebook->v;
255 iy[0] += 4; iy[1] += 4;
256 iy[2] += 4; iy[3] += 4;
257 iu[0] += 2; iu[1] += 2;
258 iv[0] += 2; iv[1] += 2;
265 static int cinepak_decode_strip (CinepakContext *s,
266 cvid_strip *strip, const uint8_t *data, int size)
268 const uint8_t *eod = (data + size);
269 int chunk_id, chunk_size;
271 /* coordinate sanity checks */
272 if (strip->x2 > s->width ||
273 strip->y2 > s->height ||
274 strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
275 return AVERROR_INVALIDDATA;
277 while ((data + 4) <= eod) {
279 chunk_size = AV_RB24 (&data[1]) - 4;
281 return AVERROR_INVALIDDATA;
284 chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
292 cinepak_decode_codebook (strip->v4_codebook, chunk_id,
300 cinepak_decode_codebook (strip->v1_codebook, chunk_id,
307 return cinepak_decode_vectors (s, strip, chunk_id,
314 return AVERROR_INVALIDDATA;
317 static int cinepak_decode (CinepakContext *s)
319 const uint8_t *eod = (s->data + s->size);
320 int i, result, strip_size, frame_flags, num_strips;
322 int encoded_buf_size;
325 return AVERROR_INVALIDDATA;
327 frame_flags = s->data[0];
328 num_strips = AV_RB16 (&s->data[8]);
329 encoded_buf_size = AV_RB24(&s->data[1]);
331 /* if this is the first frame, check for deviant Sega FILM data */
332 if (s->sega_film_skip_bytes == -1) {
333 if (!encoded_buf_size) {
334 av_log_ask_for_sample(s->avctx, "encoded_buf_size is 0");
335 return AVERROR_INVALIDDATA;
337 if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
338 /* If the encoded frame size differs from the frame size as indicated
339 * by the container file, this data likely comes from a Sega FILM/CPK file.
340 * If the frame header is followed by the bytes FE 00 00 06 00 00 then
341 * this is probably one of the two known files that have 6 extra bytes
342 * after the frame header. Else, assume 2 extra bytes. The container
343 * size also cannot be a multiple of the encoded size. */
345 (s->data[10] == 0xFE) &&
346 (s->data[11] == 0x00) &&
347 (s->data[12] == 0x00) &&
348 (s->data[13] == 0x06) &&
349 (s->data[14] == 0x00) &&
350 (s->data[15] == 0x00))
351 s->sega_film_skip_bytes = 6;
353 s->sega_film_skip_bytes = 2;
355 s->sega_film_skip_bytes = 0;
358 s->data += 10 + s->sega_film_skip_bytes;
360 num_strips = FFMIN(num_strips, MAX_STRIPS);
362 s->frame.key_frame = 0;
364 for (i=0; i < num_strips; i++) {
365 if ((s->data + 12) > eod)
366 return AVERROR_INVALIDDATA;
368 s->strips[i].id = s->data[0];
369 s->strips[i].y1 = y0;
371 s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
372 s->strips[i].x2 = s->avctx->width;
374 if (s->strips[i].id == 0x10)
375 s->frame.key_frame = 1;
377 strip_size = AV_RB24 (&s->data[1]) - 12;
379 return AVERROR_INVALIDDATA;
381 strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
383 if ((i > 0) && !(frame_flags & 0x01)) {
384 memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
385 sizeof(s->strips[i].v4_codebook));
386 memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
387 sizeof(s->strips[i].v1_codebook));
390 result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
395 s->data += strip_size;
396 y0 = s->strips[i].y2;
401 static av_cold int cinepak_decode_init(AVCodecContext *avctx)
403 CinepakContext *s = avctx->priv_data;
406 s->width = (avctx->width + 3) & ~3;
407 s->height = (avctx->height + 3) & ~3;
408 s->sega_film_skip_bytes = -1; /* uninitialized state */
410 // check for paletted data
411 if (avctx->bits_per_coded_sample != 8) {
412 s->palette_video = 0;
413 avctx->pix_fmt = PIX_FMT_YUV420P;
415 s->palette_video = 1;
416 avctx->pix_fmt = PIX_FMT_PAL8;
419 avcodec_get_frame_defaults(&s->frame);
420 s->frame.data[0] = NULL;
425 static int cinepak_decode_frame(AVCodecContext *avctx,
426 void *data, int *data_size,
429 const uint8_t *buf = avpkt->data;
430 int ret = 0, buf_size = avpkt->size;
431 CinepakContext *s = avctx->priv_data;
436 s->frame.reference = 3;
437 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
438 FF_BUFFER_HINTS_REUSABLE;
439 if ((ret = avctx->reget_buffer(avctx, &s->frame))) {
440 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
444 if (s->palette_video) {
445 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
447 s->frame.palette_has_changed = 1;
448 memcpy(s->pal, pal, AVPALETTE_SIZE);
454 if (s->palette_video)
455 memcpy (s->frame.data[1], s->pal, AVPALETTE_SIZE);
457 *data_size = sizeof(AVFrame);
458 *(AVFrame*)data = s->frame;
460 /* report that the buffer was completely consumed */
464 static av_cold int cinepak_decode_end(AVCodecContext *avctx)
466 CinepakContext *s = avctx->priv_data;
468 if (s->frame.data[0])
469 avctx->release_buffer(avctx, &s->frame);
474 AVCodec ff_cinepak_decoder = {
476 .type = AVMEDIA_TYPE_VIDEO,
477 .id = CODEC_ID_CINEPAK,
478 .priv_data_size = sizeof(CinepakContext),
479 .init = cinepak_decode_init,
480 .close = cinepak_decode_end,
481 .decode = cinepak_decode_frame,
482 .capabilities = CODEC_CAP_DR1,
483 .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),