support Cinepak files with funky (not divisible by 4) resolutions
[libav.git] / libavcodec / cinepak.c
CommitLineData
2fdf638b
MM
1/*
2 * Cinepak Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21/**
22 * @file cinepak.c
23 * Cinepak video decoder
24 * by Ewald Snel <ewald@rambo.its.tudelft.nl>
25 * For more information on the Cinepak algorithm, visit:
26 * http://www.csse.monash.edu.au/~timf/
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34#include "common.h"
35#include "avcodec.h"
36#include "dsputil.h"
37
38#define PALETTE_COUNT 256
39
40#define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
41#define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
42 (((uint8_t*)(x))[1] << 16) | \
43 (((uint8_t*)(x))[2] << 8) | \
44 ((uint8_t*)(x))[3])
45
46typedef struct {
47 uint8_t y0, y1, y2, y3;
48 uint8_t u, v;
49} cvid_codebook_t;
50
51#define MAX_STRIPS 32
52
53typedef struct {
54 uint16_t id;
55 uint16_t x1, y1;
56 uint16_t x2, y2;
57 cvid_codebook_t v4_codebook[256];
58 cvid_codebook_t v1_codebook[256];
59} cvid_strip_t;
60
61typedef struct CinepakContext {
62
63 AVCodecContext *avctx;
64 DSPContext dsp;
65 AVFrame frame;
66 AVFrame prev_frame;
67
68 unsigned char *data;
69 int size;
70
94fd9201
MM
71 int width, height;
72
2fdf638b
MM
73 unsigned char palette[PALETTE_COUNT * 4];
74 int palette_video;
75 cvid_strip_t strips[MAX_STRIPS];
76
77} CinepakContext;
78
79static void cinepak_decode_codebook (cvid_codebook_t *codebook,
80 int chunk_id, int size, uint8_t *data)
81{
82 uint8_t *eod = (data + size);
83 uint32_t flag, mask;
84 int i, n;
85
86 /* check if this chunk contains 4- or 6-element vectors */
87 n = (chunk_id & 0x0400) ? 4 : 6;
88 flag = 0;
89 mask = 0;
90
91 for (i=0; i < 256; i++) {
92 if ((chunk_id & 0x0100) && !(mask >>= 1)) {
93 if ((data + 4) > eod)
94 break;
95
96 flag = BE_32 (data);
97 data += 4;
98 mask = 0x80000000;
99 }
100
101 if (!(chunk_id & 0x0100) || (flag & mask)) {
102 if ((data + n) > eod)
103 break;
104
105 if (n == 6) {
106 codebook[i].y0 = *data++;
107 codebook[i].y1 = *data++;
108 codebook[i].y2 = *data++;
109 codebook[i].y3 = *data++;
110 codebook[i].u = 128 + *data++;
111 codebook[i].v = 128 + *data++;
112 } else {
113 /* this codebook type indicates either greyscale or
114 * palettized video; if palettized, U & V components will
115 * not be used so it is safe to set them to 128 for the
116 * benefit of greyscale rendering in YUV420P */
117 codebook[i].y0 = *data++;
118 codebook[i].y1 = *data++;
119 codebook[i].y2 = *data++;
120 codebook[i].y3 = *data++;
121 codebook[i].u = 128;
122 codebook[i].v = 128;
123 }
124 }
125 }
126}
127
128static int cinepak_decode_vectors (CinepakContext *s, cvid_strip_t *strip,
129 int chunk_id, int size, uint8_t *data)
130{
131 uint8_t *eod = (data + size);
132 uint32_t flag, mask;
133 cvid_codebook_t *codebook;
134 unsigned int i, j, x, y;
135 uint32_t iy[4];
136 uint32_t iu[2];
137 uint32_t iv[2];
138
139 flag = 0;
140 mask = 0;
141
142 for (y=strip->y1; y < strip->y2; y+=4) {
143
144 iy[0] = strip->x1 + (y * s->frame.linesize[0]);
145 iy[1] = iy[0] + s->frame.linesize[0];
146 iy[2] = iy[1] + s->frame.linesize[0];
147 iy[3] = iy[2] + s->frame.linesize[0];
148 iu[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[1]);
149 iu[1] = iu[0] + s->frame.linesize[1];
150 iv[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[2]);
151 iv[1] = iv[0] + s->frame.linesize[2];
152
153 for (x=strip->x1; x < strip->x2; x+=4) {
154 if ((chunk_id & 0x0100) && !(mask >>= 1)) {
155 if ((data + 4) > eod)
156 return -1;
157
158 flag = BE_32 (data);
159 data += 4;
160 mask = 0x80000000;
161 }
162
163 if (!(chunk_id & 0x0100) || (flag & mask)) {
164 if (!(chunk_id & 0x0200) && !(mask >>= 1)) {
165 if ((data + 4) > eod)
166 return -1;
167
168 flag = BE_32 (data);
169 data += 4;
170 mask = 0x80000000;
171 }
172
173 if ((chunk_id & 0x0200) || (~flag & mask)) {
174 if (data >= eod)
175 return -1;
176
177 codebook = &strip->v1_codebook[*data++];
178 s->frame.data[0][iy[0] + 0] = codebook->y0;
179 s->frame.data[0][iy[0] + 1] = codebook->y0;
180 s->frame.data[0][iy[1] + 0] = codebook->y0;
181 s->frame.data[0][iy[1] + 1] = codebook->y0;
182 if (!s->palette_video) {
183 s->frame.data[1][iu[0]] = codebook->u;
184 s->frame.data[2][iv[0]] = codebook->v;
185 }
186
187 s->frame.data[0][iy[0] + 2] = codebook->y0;
188 s->frame.data[0][iy[0] + 3] = codebook->y0;
189 s->frame.data[0][iy[1] + 2] = codebook->y0;
190 s->frame.data[0][iy[1] + 3] = codebook->y0;
191 if (!s->palette_video) {
192 s->frame.data[1][iu[0] + 1] = codebook->u;
193 s->frame.data[2][iv[0] + 1] = codebook->v;
194 }
195
196 s->frame.data[0][iy[2] + 0] = codebook->y0;
197 s->frame.data[0][iy[2] + 1] = codebook->y0;
198 s->frame.data[0][iy[3] + 0] = codebook->y0;
199 s->frame.data[0][iy[3] + 1] = codebook->y0;
200 if (!s->palette_video) {
201 s->frame.data[1][iu[1]] = codebook->u;
202 s->frame.data[2][iv[1]] = codebook->v;
203 }
204
205 s->frame.data[0][iy[2] + 2] = codebook->y0;
206 s->frame.data[0][iy[2] + 3] = codebook->y0;
207 s->frame.data[0][iy[3] + 2] = codebook->y0;
208 s->frame.data[0][iy[3] + 3] = codebook->y0;
209 if (!s->palette_video) {
210 s->frame.data[1][iu[1] + 1] = codebook->u;
211 s->frame.data[2][iv[1] + 1] = codebook->v;
212 }
213
214 } else if (flag & mask) {
215 if ((data + 4) > eod)
216 return -1;
217
218 codebook = &strip->v4_codebook[*data++];
219 s->frame.data[0][iy[0] + 0] = codebook->y0;
220 s->frame.data[0][iy[0] + 1] = codebook->y1;
221 s->frame.data[0][iy[1] + 0] = codebook->y2;
222 s->frame.data[0][iy[1] + 1] = codebook->y3;
223 if (!s->palette_video) {
224 s->frame.data[1][iu[0]] = codebook->u;
225 s->frame.data[2][iv[0]] = codebook->v;
226 }
227
228 codebook = &strip->v4_codebook[*data++];
229 s->frame.data[0][iy[0] + 2] = codebook->y0;
230 s->frame.data[0][iy[0] + 3] = codebook->y1;
231 s->frame.data[0][iy[1] + 2] = codebook->y2;
232 s->frame.data[0][iy[1] + 3] = codebook->y3;
233 if (!s->palette_video) {
234 s->frame.data[1][iu[0] + 1] = codebook->u;
235 s->frame.data[2][iv[0] + 1] = codebook->v;
236 }
237
238 codebook = &strip->v4_codebook[*data++];
239 s->frame.data[0][iy[2] + 0] = codebook->y0;
240 s->frame.data[0][iy[2] + 1] = codebook->y1;
241 s->frame.data[0][iy[3] + 0] = codebook->y2;
242 s->frame.data[0][iy[3] + 1] = codebook->y3;
243 if (!s->palette_video) {
244 s->frame.data[1][iu[1]] = codebook->u;
245 s->frame.data[2][iv[1]] = codebook->v;
246 }
247
248 codebook = &strip->v4_codebook[*data++];
249 s->frame.data[0][iy[2] + 2] = codebook->y0;
250 s->frame.data[0][iy[2] + 3] = codebook->y1;
251 s->frame.data[0][iy[3] + 2] = codebook->y2;
252 s->frame.data[0][iy[3] + 3] = codebook->y3;
253 if (!s->palette_video) {
254 s->frame.data[1][iu[1] + 1] = codebook->u;
255 s->frame.data[2][iv[1] + 1] = codebook->v;
256 }
257
258 }
259 } else {
260 /* copy from the previous frame */
261 for (i = 0; i < 4; i++) {
262 for (j = 0; j < 4; j++) {
263 s->frame.data[0][iy[i] + j] =
264 s->prev_frame.data[0][iy[i] + j];
265 }
266 }
267 for (i = 0; i < 2; i++) {
268 for (j = 0; j < 2; j++) {
269 s->frame.data[1][iu[i] + j] =
270 s->prev_frame.data[1][iu[i] + j];
271 s->frame.data[2][iv[i] + j] =
272 s->prev_frame.data[2][iv[i] + j];
273 }
274 }
275 }
276
277 iy[0] += 4; iy[1] += 4;
278 iy[2] += 4; iy[3] += 4;
279 iu[0] += 2; iu[1] += 2;
280 iv[0] += 2; iv[1] += 2;
281 }
282 }
283
284 return 0;
285}
286
287static int cinepak_decode_strip (CinepakContext *s,
288 cvid_strip_t *strip, uint8_t *data, int size)
289{
290 uint8_t *eod = (data + size);
291 int chunk_id, chunk_size;
292
293 /* coordinate sanity checks */
94fd9201
MM
294 if (strip->x1 >= s->width || strip->x2 > s->width ||
295 strip->y1 >= s->height || strip->y2 > s->height ||
296 strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
2fdf638b
MM
297 return -1;
298
299 while ((data + 4) <= eod) {
300 chunk_id = BE_16 (&data[0]);
301 chunk_size = BE_16 (&data[2]) - 4;
302 data += 4;
303 chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
304
305 switch (chunk_id) {
306
307 case 0x2000:
308 case 0x2100:
309 case 0x2400:
310 case 0x2500:
311 cinepak_decode_codebook (strip->v4_codebook, chunk_id,
312 chunk_size, data);
313 break;
314
315 case 0x2200:
316 case 0x2300:
317 case 0x2600:
318 case 0x2700:
319 cinepak_decode_codebook (strip->v1_codebook, chunk_id,
320 chunk_size, data);
321 break;
322
323 case 0x3000:
324 case 0x3100:
325 case 0x3200:
326 return cinepak_decode_vectors (s, strip, chunk_id,
327 chunk_size, data);
328 }
329
330 data += chunk_size;
331 }
332
333 return -1;
334}
335
336static int cinepak_decode (CinepakContext *s)
337{
338 uint8_t *eod = (s->data + s->size);
339 int i, result, strip_size, frame_flags, num_strips;
340 int y0 = 0;
341
342 if (s->size < 10)
343 return -1;
344
345 frame_flags = s->data[0];
346 num_strips = BE_16 (&s->data[8]);
347 s->data += 10;
348
349 if (num_strips > MAX_STRIPS)
350 num_strips = MAX_STRIPS;
351
352 for (i=0; i < num_strips; i++) {
353 if ((s->data + 12) > eod)
354 return -1;
355
356 s->strips[i].id = BE_16 (s->data);
357 s->strips[i].y1 = y0;
358 s->strips[i].x1 = 0;
359 s->strips[i].y2 = y0 + BE_16 (&s->data[8]);
360 s->strips[i].x2 = s->avctx->width;
361
362 strip_size = BE_16 (&s->data[2]) - 12;
363 s->data += 12;
364 strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
365
366 if ((i > 0) && !(frame_flags & 0x01)) {
367 memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
368 sizeof(s->strips[i].v4_codebook));
369 memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
370 sizeof(s->strips[i].v1_codebook));
371 }
372
373 result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
374
375 if (result != 0)
376 return result;
377
378 s->data += strip_size;
379 y0 = s->strips[i].y2;
380 }
381 return 0;
382}
383
384static int cinepak_decode_init(AVCodecContext *avctx)
385{
386 CinepakContext *s = (CinepakContext *)avctx->priv_data;
387/*
388 int i;
389 unsigned char r, g, b;
390 unsigned char *raw_palette;
391 unsigned int *palette32;
392*/
393
394 s->avctx = avctx;
94fd9201
MM
395 s->width = (avctx->width + 3) & ~3;
396 s->height = (avctx->height + 3) & ~3;
2fdf638b
MM
397
398// check for paletted data
399s->palette_video = 0;
400
401
402 avctx->pix_fmt = PIX_FMT_YUV420P;
403 avctx->has_b_frames = 0;
404 dsputil_init(&s->dsp, avctx);
405
406 s->frame.data[0] = s->prev_frame.data[0] = NULL;
407
408 return 0;
409}
410
411static int cinepak_decode_frame(AVCodecContext *avctx,
412 void *data, int *data_size,
413 uint8_t *buf, int buf_size)
414{
415 CinepakContext *s = (CinepakContext *)avctx->priv_data;
416
417 s->data = buf;
418 s->size = buf_size;
419
420 if (avctx->get_buffer(avctx, &s->frame)) {
421 printf (" Cinepak: get_buffer() failed\n");
422 return -1;
423 }
424
425 cinepak_decode(s);
426
427 if (s->prev_frame.data[0])
428 avctx->release_buffer(avctx, &s->prev_frame);
429
430 /* shuffle frames */
431 s->prev_frame = s->frame;
432
433 *data_size = sizeof(AVFrame);
434 *(AVFrame*)data = s->frame;
435
436 /* report that the buffer was completely consumed */
437 return buf_size;
438}
439
440static int cinepak_decode_end(AVCodecContext *avctx)
441{
442 CinepakContext *s = (CinepakContext *)avctx->priv_data;
443
444 if (s->prev_frame.data[0])
445 avctx->release_buffer(avctx, &s->prev_frame);
446
447 return 0;
448}
449
450AVCodec cinepak_decoder = {
451 "cinepak",
452 CODEC_TYPE_VIDEO,
453 CODEC_ID_CINEPAK,
454 sizeof(CinepakContext),
455 cinepak_decode_init,
456 NULL,
457 cinepak_decode_end,
458 cinepak_decode_frame,
459 CODEC_CAP_DR1,
460};