2 * Cinepak Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
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.
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.
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
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/
38 #define PALETTE_COUNT 256
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) | \
47 uint8_t y0, y1, y2, y3;
57 cvid_codebook_t v4_codebook[256];
58 cvid_codebook_t v1_codebook[256];
61 typedef struct CinepakContext {
63 AVCodecContext *avctx;
73 unsigned char palette[PALETTE_COUNT * 4];
75 cvid_strip_t strips[MAX_STRIPS];
79 static void cinepak_decode_codebook (cvid_codebook_t *codebook,
80 int chunk_id, int size, uint8_t *data)
82 uint8_t *eod = (data + size);
86 /* check if this chunk contains 4- or 6-element vectors */
87 n = (chunk_id & 0x0400) ? 4 : 6;
91 for (i=0; i < 256; i++) {
92 if ((chunk_id & 0x0100) && !(mask >>= 1)) {
101 if (!(chunk_id & 0x0100) || (flag & mask)) {
102 if ((data + n) > eod)
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++;
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++;
128 static int cinepak_decode_vectors (CinepakContext *s, cvid_strip_t *strip,
129 int chunk_id, int size, uint8_t *data)
131 uint8_t *eod = (data + size);
133 cvid_codebook_t *codebook;
134 unsigned int i, j, x, y;
142 for (y=strip->y1; y < strip->y2; y+=4) {
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];
153 for (x=strip->x1; x < strip->x2; x+=4) {
154 if ((chunk_id & 0x0100) && !(mask >>= 1)) {
155 if ((data + 4) > eod)
163 if (!(chunk_id & 0x0100) || (flag & mask)) {
164 if (!(chunk_id & 0x0200) && !(mask >>= 1)) {
165 if ((data + 4) > eod)
173 if ((chunk_id & 0x0200) || (~flag & mask)) {
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;
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;
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;
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;
214 } else if (flag & mask) {
215 if ((data + 4) > eod)
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;
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;
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;
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;
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];
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];
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;
287 static int cinepak_decode_strip (CinepakContext *s,
288 cvid_strip_t *strip, uint8_t *data, int size)
290 uint8_t *eod = (data + size);
291 int chunk_id, chunk_size;
293 /* coordinate sanity checks */
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)
299 while ((data + 4) <= eod) {
300 chunk_id = BE_16 (&data[0]);
301 chunk_size = BE_16 (&data[2]) - 4;
303 chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
311 cinepak_decode_codebook (strip->v4_codebook, chunk_id,
319 cinepak_decode_codebook (strip->v1_codebook, chunk_id,
326 return cinepak_decode_vectors (s, strip, chunk_id,
336 static int cinepak_decode (CinepakContext *s)
338 uint8_t *eod = (s->data + s->size);
339 int i, result, strip_size, frame_flags, num_strips;
345 frame_flags = s->data[0];
346 num_strips = BE_16 (&s->data[8]);
349 if (num_strips > MAX_STRIPS)
350 num_strips = MAX_STRIPS;
352 for (i=0; i < num_strips; i++) {
353 if ((s->data + 12) > eod)
356 s->strips[i].id = BE_16 (s->data);
357 s->strips[i].y1 = y0;
359 s->strips[i].y2 = y0 + BE_16 (&s->data[8]);
360 s->strips[i].x2 = s->avctx->width;
362 strip_size = BE_16 (&s->data[2]) - 12;
364 strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
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));
373 result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
378 s->data += strip_size;
379 y0 = s->strips[i].y2;
384 static int cinepak_decode_init(AVCodecContext *avctx)
386 CinepakContext *s = (CinepakContext *)avctx->priv_data;
389 unsigned char r, g, b;
390 unsigned char *raw_palette;
391 unsigned int *palette32;
395 s->width = (avctx->width + 3) & ~3;
396 s->height = (avctx->height + 3) & ~3;
398 // check for paletted data
399 s->palette_video = 0;
402 avctx->pix_fmt = PIX_FMT_YUV420P;
403 avctx->has_b_frames = 0;
404 dsputil_init(&s->dsp, avctx);
406 s->frame.data[0] = s->prev_frame.data[0] = NULL;
411 static int cinepak_decode_frame(AVCodecContext *avctx,
412 void *data, int *data_size,
413 uint8_t *buf, int buf_size)
415 CinepakContext *s = (CinepakContext *)avctx->priv_data;
420 if (avctx->get_buffer(avctx, &s->frame)) {
421 av_log(avctx, AV_LOG_ERROR, " Cinepak: get_buffer() failed\n");
427 if (s->prev_frame.data[0])
428 avctx->release_buffer(avctx, &s->prev_frame);
431 s->prev_frame = s->frame;
433 *data_size = sizeof(AVFrame);
434 *(AVFrame*)data = s->frame;
436 /* report that the buffer was completely consumed */
440 static int cinepak_decode_end(AVCodecContext *avctx)
442 CinepakContext *s = (CinepakContext *)avctx->priv_data;
444 if (s->prev_frame.data[0])
445 avctx->release_buffer(avctx, &s->prev_frame);
450 AVCodec cinepak_decoder = {
454 sizeof(CinepakContext),
458 cinepak_decode_frame,