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 * @author Ewald Snel <ewald@rambo.its.tudelft.nl>
27 * @see For more information on the Cinepak algorithm, visit:
28 * http://www.csse.monash.edu.au/~timf/
29 * @see For more information on the quirky data inside Sega FILM/CPK files, visit:
30 * http://wiki.multimedia.cx/index.php?title=Sega_FILM
32 * Cinepak colorspace support (c) 2013 Rl, Aetey Global Technologies AB
33 * @author Cinepak colorspace, Rl, Aetey Global Technologies AB
40 #include "libavutil/common.h"
41 #include "libavutil/intreadwrite.h"
45 typedef uint8_t cvid_codebook[12];
53 cvid_codebook v4_codebook[256];
54 cvid_codebook v1_codebook[256];
57 typedef struct CinepakContext {
59 AVCodecContext *avctx;
62 const unsigned char *data;
68 cvid_strip strips[MAX_STRIPS];
70 int sega_film_skip_bytes;
75 static void cinepak_decode_codebook (cvid_codebook *codebook,
76 int chunk_id, int size, const uint8_t *data)
78 const uint8_t *eod = (data + size);
83 /* check if this chunk contains 4- or 6-element vectors */
84 n = (chunk_id & 0x04) ? 4 : 6;
89 for (i=0; i < 256; i++) {
90 if ((chunk_id & 0x01) && !(mask >>= 1)) {
94 flag = AV_RB32 (data);
99 if (!(chunk_id & 0x01) || (flag & mask)) {
102 if ((data + n) > eod)
105 for (k = 0; k < 4; ++k) {
107 for (kk = 0; kk < 3; ++kk)
112 u = *(int8_t *)data++;
113 v = *(int8_t *)data++;
117 g = *p++ - (u/2) - v;
120 *p++ = av_clip_uint8(r);
121 *p++ = av_clip_uint8(g);
122 *p++ = av_clip_uint8(b);
131 static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
132 int chunk_id, int size, const uint8_t *data)
134 const uint8_t *eod = (data + size);
136 uint8_t *cb0, *cb1, *cb2, *cb3;
138 char *ip0, *ip1, *ip2, *ip3;
143 for (y=strip->y1; y < strip->y2; y+=4) {
145 /* take care of y dimension not being multiple of 4, such streams exist */
146 ip0 = ip1 = ip2 = ip3 = s->frame.data[0] +
147 (s->palette_video?strip->x1:strip->x1*3) + (y * s->frame.linesize[0]);
148 if(s->avctx->height - y > 1) {
149 ip1 = ip0 + s->frame.linesize[0];
150 if(s->avctx->height - y > 2) {
151 ip2 = ip1 + s->frame.linesize[0];
152 if(s->avctx->height - y > 3) {
153 ip3 = ip2 + s->frame.linesize[0];
157 /* to get the correct picture for not-multiple-of-4 cases let us fill
158 * each block from the bottom up, thus possibly overwriting the top line
159 * more than once but ending with the correct data in place
160 * (instead of in-loop checking) */
162 for (x=strip->x1; x < strip->x2; x+=4) {
163 if ((chunk_id & 0x01) && !(mask >>= 1)) {
164 if ((data + 4) > eod)
165 return AVERROR_INVALIDDATA;
167 flag = AV_RB32 (data);
172 if (!(chunk_id & 0x01) || (flag & mask)) {
173 if (!(chunk_id & 0x02) && !(mask >>= 1)) {
174 if ((data + 4) > eod)
175 return AVERROR_INVALIDDATA;
177 flag = AV_RB32 (data);
182 if ((chunk_id & 0x02) || (~flag & mask)) {
185 return AVERROR_INVALIDDATA;
187 p = strip->v1_codebook[*data++];
188 if (s->palette_video) {
189 ip3[0] = ip3[1] = ip2[0] = ip2[1] = p[6];
190 ip3[2] = ip3[3] = ip2[2] = ip2[3] = p[9];
191 ip1[0] = ip1[1] = ip0[0] = ip0[1] = p[0];
192 ip1[2] = ip1[3] = ip0[2] = ip0[3] = p[3];
195 memcpy(ip3 + 0, p, 3); memcpy(ip3 + 3, p, 3);
196 memcpy(ip2 + 0, p, 3); memcpy(ip2 + 3, p, 3);
197 p += 3; /* ... + 9 */
198 memcpy(ip3 + 6, p, 3); memcpy(ip3 + 9, p, 3);
199 memcpy(ip2 + 6, p, 3); memcpy(ip2 + 9, p, 3);
200 p -= 9; /* ... + 0 */
201 memcpy(ip1 + 0, p, 3); memcpy(ip1 + 3, p, 3);
202 memcpy(ip0 + 0, p, 3); memcpy(ip0 + 3, p, 3);
203 p += 3; /* ... + 3 */
204 memcpy(ip1 + 6, p, 3); memcpy(ip1 + 9, p, 3);
205 memcpy(ip0 + 6, p, 3); memcpy(ip0 + 9, p, 3);
208 } else if (flag & mask) {
209 if ((data + 4) > eod)
210 return AVERROR_INVALIDDATA;
212 cb0 = strip->v4_codebook[*data++];
213 cb1 = strip->v4_codebook[*data++];
214 cb2 = strip->v4_codebook[*data++];
215 cb3 = strip->v4_codebook[*data++];
216 if (s->palette_video) {
239 memcpy(ip3 + 0, cb2 + 6, 6);
240 memcpy(ip3 + 6, cb3 + 6, 6);
241 memcpy(ip2 + 0, cb2 + 0, 6);
242 memcpy(ip2 + 6, cb3 + 0, 6);
243 memcpy(ip1 + 0, cb0 + 6, 6);
244 memcpy(ip1 + 6, cb1 + 6, 6);
245 memcpy(ip0 + 0, cb0 + 0, 6);
246 memcpy(ip0 + 6, cb1 + 0, 6);
252 if (s->palette_video) {
256 ip0 += 12; ip1 += 12;
257 ip2 += 12; ip3 += 12;
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_PATCHWELCOME;
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 /* zero y1 means "relative to the previous stripe" */
370 if (!(s->strips[i].y1 = AV_RB16 (&s->data[4])))
371 s->strips[i].y2 = (s->strips[i].y1 = y0) + AV_RB16 (&s->data[8]);
373 s->strips[i].y2 = AV_RB16 (&s->data[8]);
374 s->strips[i].x1 = AV_RB16 (&s->data[6]);
375 s->strips[i].x2 = AV_RB16 (&s->data[10]);
377 if (s->strips[i].id == 0x10)
378 s->frame.key_frame = 1;
380 strip_size = AV_RB24 (&s->data[1]) - 12;
382 return AVERROR_INVALIDDATA;
384 strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
386 if ((i > 0) && !(frame_flags & 0x01)) {
387 memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
388 sizeof(s->strips[i].v4_codebook));
389 memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
390 sizeof(s->strips[i].v1_codebook));
393 result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
398 s->data += strip_size;
399 y0 = s->strips[i].y2;
404 static av_cold int cinepak_decode_init(AVCodecContext *avctx)
406 CinepakContext *s = avctx->priv_data;
409 s->width = (avctx->width + 3) & ~3;
410 s->height = (avctx->height + 3) & ~3;
412 s->sega_film_skip_bytes = -1; /* uninitialized state */
414 // check for paletted data
415 if (avctx->bits_per_coded_sample != 8) {
416 s->palette_video = 0;
417 avctx->pix_fmt = AV_PIX_FMT_RGB24;
419 s->palette_video = 1;
420 avctx->pix_fmt = AV_PIX_FMT_PAL8;
423 avcodec_get_frame_defaults(&s->frame);
424 s->frame.data[0] = NULL;
429 static int cinepak_decode_frame(AVCodecContext *avctx,
430 void *data, int *got_frame,
433 const uint8_t *buf = avpkt->data;
434 int ret = 0, buf_size = avpkt->size;
435 CinepakContext *s = avctx->priv_data;
440 s->frame.reference = 3;
441 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
442 FF_BUFFER_HINTS_REUSABLE;
443 if ((ret = avctx->reget_buffer(avctx, &s->frame))) {
444 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
448 if (s->palette_video) {
449 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
451 s->frame.palette_has_changed = 1;
452 memcpy(s->pal, pal, AVPALETTE_SIZE);
456 if ((ret = cinepak_decode(s)) < 0) {
457 av_log(avctx, AV_LOG_ERROR, "cinepak_decode failed\n");
460 if (s->palette_video)
461 memcpy (s->frame.data[1], s->pal, AVPALETTE_SIZE);
464 *(AVFrame*)data = s->frame;
466 /* report that the buffer was completely consumed */
470 static av_cold int cinepak_decode_end(AVCodecContext *avctx)
472 CinepakContext *s = avctx->priv_data;
474 if (s->frame.data[0])
475 avctx->release_buffer(avctx, &s->frame);
480 AVCodec ff_cinepak_decoder = {
482 .type = AVMEDIA_TYPE_VIDEO,
483 .id = AV_CODEC_ID_CINEPAK,
484 .priv_data_size = sizeof(CinepakContext),
485 .init = cinepak_decode_init,
486 .close = cinepak_decode_end,
487 .decode = cinepak_decode_frame,
488 .capabilities = CODEC_CAP_DR1,
489 .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),