]> git.sesse.net Git - ffmpeg/blob - libavcodec/cinepak.c
Merge commit '399663be9d4a839b894c48a21b62926eb8497d72'
[ffmpeg] / libavcodec / cinepak.c
1 /*
2  * Cinepak Video Decoder
3  * Copyright (C) 2003 the ffmpeg project
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Cinepak video decoder
25  * @author Ewald Snel <ewald@rambo.its.tudelft.nl>
26  *
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
31  *
32  * Cinepak colorspace support (c) 2013 Rl, Aetey Global Technologies AB
33  * @author Cinepak colorspace, Rl, Aetey Global Technologies AB
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "libavutil/common.h"
41 #include "libavutil/intreadwrite.h"
42 #include "avcodec.h"
43
44
45 typedef uint8_t cvid_codebook[12];
46
47 #define MAX_STRIPS      32
48
49 typedef struct {
50     uint16_t          id;
51     uint16_t          x1, y1;
52     uint16_t          x2, y2;
53     cvid_codebook     v4_codebook[256];
54     cvid_codebook     v1_codebook[256];
55 } cvid_strip;
56
57 typedef struct CinepakContext {
58
59     AVCodecContext *avctx;
60     AVFrame frame;
61
62     const unsigned char *data;
63     int size;
64
65     int width, height;
66
67     int palette_video;
68     cvid_strip strips[MAX_STRIPS];
69
70     int sega_film_skip_bytes;
71
72     uint32_t pal[256];
73 } CinepakContext;
74
75 static void cinepak_decode_codebook (cvid_codebook *codebook,
76                                      int chunk_id, int size, const uint8_t *data)
77 {
78     const uint8_t *eod = (data + size);
79     uint32_t flag, mask;
80     int      i, n;
81     uint8_t *p;
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     p = codebook[0];
89     for (i=0; i < 256; i++) {
90         if ((chunk_id & 0x01) && !(mask >>= 1)) {
91             if ((data + 4) > eod)
92                 break;
93
94             flag  = AV_RB32 (data);
95             data += 4;
96             mask  = 0x80000000;
97         }
98
99         if (!(chunk_id & 0x01) || (flag & mask)) {
100             int k, kk;
101
102             if ((data + n) > eod)
103                 break;
104
105             for (k = 0; k < 4; ++k) {
106                 int r = *data++;
107                 for (kk = 0; kk < 3; ++kk)
108                     *p++ = r;
109             }
110             if (n == 6) {
111                 int r, g, b, u, v;
112                 u = *(int8_t *)data++;
113                 v = *(int8_t *)data++;
114                 p -= 12;
115                 for(k=0; k<4; ++k) {
116                     r = *p++ + v*2;
117                     g = *p++ - (u/2) - v;
118                     b = *p   + u*2;
119                     p -= 2;
120                     *p++ = av_clip_uint8(r);
121                     *p++ = av_clip_uint8(g);
122                     *p++ = av_clip_uint8(b);
123                 }
124             }
125         } else {
126             p += 12;
127         }
128     }
129 }
130
131 static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
132                                    int chunk_id, int size, const uint8_t *data)
133 {
134     const uint8_t   *eod = (data + size);
135     uint32_t         flag, mask;
136     uint8_t         *cb0, *cb1, *cb2, *cb3;
137     unsigned int     x, y;
138     char            *ip0, *ip1, *ip2, *ip3;
139
140     flag = 0;
141     mask = 0;
142
143     for (y=strip->y1; y < strip->y2; y+=4) {
144
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];
154                 }
155             }
156         }
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) */
161
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;
166
167                 flag  = AV_RB32 (data);
168                 data += 4;
169                 mask  = 0x80000000;
170             }
171
172             if (!(chunk_id & 0x01) || (flag & mask)) {
173                 if (!(chunk_id & 0x02) && !(mask >>= 1)) {
174                     if ((data + 4) > eod)
175                         return AVERROR_INVALIDDATA;
176
177                     flag  = AV_RB32 (data);
178                     data += 4;
179                     mask  = 0x80000000;
180                 }
181
182                 if ((chunk_id & 0x02) || (~flag & mask)) {
183                     uint8_t *p;
184                     if (data >= eod)
185                         return AVERROR_INVALIDDATA;
186
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];
193                     } else {
194                         p += 6;
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);
206                     }
207
208                 } else if (flag & mask) {
209                     if ((data + 4) > eod)
210                         return AVERROR_INVALIDDATA;
211
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) {
217                         uint8_t *p;
218                         p = ip3;
219                         *p++ = cb2[6];
220                         *p++ = cb2[9];
221                         *p++ = cb3[6];
222                         *p   = cb3[9];
223                         p = ip2;
224                         *p++ = cb2[0];
225                         *p++ = cb2[3];
226                         *p++ = cb3[0];
227                         *p   = cb3[3];
228                         p = ip1;
229                         *p++ = cb0[6];
230                         *p++ = cb0[9];
231                         *p++ = cb1[6];
232                         *p   = cb1[9];
233                         p = ip0;
234                         *p++ = cb0[0];
235                         *p++ = cb0[3];
236                         *p++ = cb1[0];
237                         *p   = cb1[3];
238                     } else {
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);
247                     }
248
249                 }
250             }
251
252             if (s->palette_video) {
253                 ip0 += 4;  ip1 += 4;
254                 ip2 += 4;  ip3 += 4;
255             } else {
256                 ip0 += 12;  ip1 += 12;
257                 ip2 += 12;  ip3 += 12;
258             }
259         }
260     }
261
262     return 0;
263 }
264
265 static int cinepak_decode_strip (CinepakContext *s,
266                                  cvid_strip *strip, const uint8_t *data, int size)
267 {
268     const uint8_t *eod = (data + size);
269     int      chunk_id, chunk_size;
270
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;
276
277     while ((data + 4) <= eod) {
278         chunk_id   = data[0];
279         chunk_size = AV_RB24 (&data[1]) - 4;
280         if(chunk_size < 0)
281             return AVERROR_INVALIDDATA;
282
283         data      += 4;
284         chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
285
286         switch (chunk_id) {
287
288         case 0x20:
289         case 0x21:
290         case 0x24:
291         case 0x25:
292             cinepak_decode_codebook (strip->v4_codebook, chunk_id,
293                 chunk_size, data);
294             break;
295
296         case 0x22:
297         case 0x23:
298         case 0x26:
299         case 0x27:
300             cinepak_decode_codebook (strip->v1_codebook, chunk_id,
301                 chunk_size, data);
302             break;
303
304         case 0x30:
305         case 0x31:
306         case 0x32:
307             return cinepak_decode_vectors (s, strip, chunk_id,
308                 chunk_size, data);
309         }
310
311         data += chunk_size;
312     }
313
314     return AVERROR_INVALIDDATA;
315 }
316
317 static int cinepak_decode (CinepakContext *s)
318 {
319     const uint8_t  *eod = (s->data + s->size);
320     int           i, result, strip_size, frame_flags, num_strips;
321     int           y0 = 0;
322     int           encoded_buf_size;
323
324     if (s->size < 10)
325         return AVERROR_INVALIDDATA;
326
327     frame_flags = s->data[0];
328     num_strips  = AV_RB16 (&s->data[8]);
329     encoded_buf_size = AV_RB24(&s->data[1]);
330
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;
336         }
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. */
344             if (s->size >= 16 &&
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;
352             else
353                 s->sega_film_skip_bytes = 2;
354         } else
355             s->sega_film_skip_bytes = 0;
356     }
357
358     s->data += 10 + s->sega_film_skip_bytes;
359
360     num_strips = FFMIN(num_strips, MAX_STRIPS);
361
362     s->frame.key_frame = 0;
363
364     for (i=0; i < num_strips; i++) {
365         if ((s->data + 12) > eod)
366             return AVERROR_INVALIDDATA;
367
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]);
372         else
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]);
376
377         if (s->strips[i].id == 0x10)
378             s->frame.key_frame = 1;
379
380         strip_size = AV_RB24 (&s->data[1]) - 12;
381         if (strip_size < 0)
382             return AVERROR_INVALIDDATA;
383         s->data   += 12;
384         strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
385
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));
391         }
392
393         result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
394
395         if (result != 0)
396             return result;
397
398         s->data += strip_size;
399         y0    = s->strips[i].y2;
400     }
401     return 0;
402 }
403
404 static av_cold int cinepak_decode_init(AVCodecContext *avctx)
405 {
406     CinepakContext *s = avctx->priv_data;
407
408     s->avctx = avctx;
409     s->width = (avctx->width + 3) & ~3;
410     s->height = (avctx->height + 3) & ~3;
411
412     s->sega_film_skip_bytes = -1;  /* uninitialized state */
413
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;
418     } else {
419         s->palette_video = 1;
420         avctx->pix_fmt = AV_PIX_FMT_PAL8;
421     }
422
423     avcodec_get_frame_defaults(&s->frame);
424     s->frame.data[0] = NULL;
425
426     return 0;
427 }
428
429 static int cinepak_decode_frame(AVCodecContext *avctx,
430                                 void *data, int *got_frame,
431                                 AVPacket *avpkt)
432 {
433     const uint8_t *buf = avpkt->data;
434     int ret = 0, buf_size = avpkt->size;
435     CinepakContext *s = avctx->priv_data;
436
437     s->data = buf;
438     s->size = buf_size;
439
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");
445         return ret;
446     }
447
448     if (s->palette_video) {
449         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
450         if (pal) {
451             s->frame.palette_has_changed = 1;
452             memcpy(s->pal, pal, AVPALETTE_SIZE);
453         }
454     }
455
456     if ((ret = cinepak_decode(s)) < 0) {
457         av_log(avctx, AV_LOG_ERROR, "cinepak_decode failed\n");
458     }
459
460     if (s->palette_video)
461         memcpy (s->frame.data[1], s->pal, AVPALETTE_SIZE);
462
463     *got_frame = 1;
464     *(AVFrame*)data = s->frame;
465
466     /* report that the buffer was completely consumed */
467     return buf_size;
468 }
469
470 static av_cold int cinepak_decode_end(AVCodecContext *avctx)
471 {
472     CinepakContext *s = avctx->priv_data;
473
474     if (s->frame.data[0])
475         avctx->release_buffer(avctx, &s->frame);
476
477     return 0;
478 }
479
480 AVCodec ff_cinepak_decoder = {
481     .name           = "cinepak",
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"),
490 };