00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036
00037 #include "libavutil/common.h"
00038 #include "libavutil/intreadwrite.h"
00039 #include "avcodec.h"
00040
00041
00042 typedef struct {
00043 uint8_t y0, y1, y2, y3;
00044 uint8_t u, v;
00045 } cvid_codebook;
00046
00047 #define MAX_STRIPS 32
00048
00049 typedef struct {
00050 uint16_t id;
00051 uint16_t x1, y1;
00052 uint16_t x2, y2;
00053 cvid_codebook v4_codebook[256];
00054 cvid_codebook v1_codebook[256];
00055 } cvid_strip;
00056
00057 typedef struct CinepakContext {
00058
00059 AVCodecContext *avctx;
00060 AVFrame frame;
00061
00062 const unsigned char *data;
00063 int size;
00064
00065 int width, height;
00066
00067 int palette_video;
00068 cvid_strip strips[MAX_STRIPS];
00069
00070 int sega_film_skip_bytes;
00071
00072 uint32_t pal[256];
00073 } CinepakContext;
00074
00075 static void cinepak_decode_codebook (cvid_codebook *codebook,
00076 int chunk_id, int size, const uint8_t *data)
00077 {
00078 const uint8_t *eod = (data + size);
00079 uint32_t flag, mask;
00080 int i, n;
00081
00082
00083 n = (chunk_id & 0x04) ? 4 : 6;
00084 flag = 0;
00085 mask = 0;
00086
00087 for (i=0; i < 256; i++) {
00088 if ((chunk_id & 0x01) && !(mask >>= 1)) {
00089 if ((data + 4) > eod)
00090 break;
00091
00092 flag = AV_RB32 (data);
00093 data += 4;
00094 mask = 0x80000000;
00095 }
00096
00097 if (!(chunk_id & 0x01) || (flag & mask)) {
00098 if ((data + n) > eod)
00099 break;
00100
00101 if (n == 6) {
00102 codebook[i].y0 = *data++;
00103 codebook[i].y1 = *data++;
00104 codebook[i].y2 = *data++;
00105 codebook[i].y3 = *data++;
00106 codebook[i].u = 128 + *data++;
00107 codebook[i].v = 128 + *data++;
00108 } else {
00109
00110
00111
00112
00113 codebook[i].y0 = *data++;
00114 codebook[i].y1 = *data++;
00115 codebook[i].y2 = *data++;
00116 codebook[i].y3 = *data++;
00117 codebook[i].u = 128;
00118 codebook[i].v = 128;
00119 }
00120 }
00121 }
00122 }
00123
00124 static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
00125 int chunk_id, int size, const uint8_t *data)
00126 {
00127 const uint8_t *eod = (data + size);
00128 uint32_t flag, mask;
00129 cvid_codebook *codebook;
00130 unsigned int x, y;
00131 uint32_t iy[4];
00132 uint32_t iu[2];
00133 uint32_t iv[2];
00134
00135 flag = 0;
00136 mask = 0;
00137
00138 for (y=strip->y1; y < strip->y2; y+=4) {
00139
00140 iy[0] = strip->x1 + (y * s->frame.linesize[0]);
00141 iy[1] = iy[0] + s->frame.linesize[0];
00142 iy[2] = iy[1] + s->frame.linesize[0];
00143 iy[3] = iy[2] + s->frame.linesize[0];
00144 iu[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[1]);
00145 iu[1] = iu[0] + s->frame.linesize[1];
00146 iv[0] = (strip->x1/2) + ((y/2) * s->frame.linesize[2]);
00147 iv[1] = iv[0] + s->frame.linesize[2];
00148
00149 for (x=strip->x1; x < strip->x2; x+=4) {
00150 if ((chunk_id & 0x01) && !(mask >>= 1)) {
00151 if ((data + 4) > eod)
00152 return AVERROR_INVALIDDATA;
00153
00154 flag = AV_RB32 (data);
00155 data += 4;
00156 mask = 0x80000000;
00157 }
00158
00159 if (!(chunk_id & 0x01) || (flag & mask)) {
00160 if (!(chunk_id & 0x02) && !(mask >>= 1)) {
00161 if ((data + 4) > eod)
00162 return AVERROR_INVALIDDATA;
00163
00164 flag = AV_RB32 (data);
00165 data += 4;
00166 mask = 0x80000000;
00167 }
00168
00169 if ((chunk_id & 0x02) || (~flag & mask)) {
00170 if (data >= eod)
00171 return AVERROR_INVALIDDATA;
00172
00173 codebook = &strip->v1_codebook[*data++];
00174 s->frame.data[0][iy[0] + 0] = codebook->y0;
00175 s->frame.data[0][iy[0] + 1] = codebook->y0;
00176 s->frame.data[0][iy[1] + 0] = codebook->y0;
00177 s->frame.data[0][iy[1] + 1] = codebook->y0;
00178 if (!s->palette_video) {
00179 s->frame.data[1][iu[0]] = codebook->u;
00180 s->frame.data[2][iv[0]] = codebook->v;
00181 }
00182
00183 s->frame.data[0][iy[0] + 2] = codebook->y1;
00184 s->frame.data[0][iy[0] + 3] = codebook->y1;
00185 s->frame.data[0][iy[1] + 2] = codebook->y1;
00186 s->frame.data[0][iy[1] + 3] = codebook->y1;
00187 if (!s->palette_video) {
00188 s->frame.data[1][iu[0] + 1] = codebook->u;
00189 s->frame.data[2][iv[0] + 1] = codebook->v;
00190 }
00191
00192 s->frame.data[0][iy[2] + 0] = codebook->y2;
00193 s->frame.data[0][iy[2] + 1] = codebook->y2;
00194 s->frame.data[0][iy[3] + 0] = codebook->y2;
00195 s->frame.data[0][iy[3] + 1] = codebook->y2;
00196 if (!s->palette_video) {
00197 s->frame.data[1][iu[1]] = codebook->u;
00198 s->frame.data[2][iv[1]] = codebook->v;
00199 }
00200
00201 s->frame.data[0][iy[2] + 2] = codebook->y3;
00202 s->frame.data[0][iy[2] + 3] = codebook->y3;
00203 s->frame.data[0][iy[3] + 2] = codebook->y3;
00204 s->frame.data[0][iy[3] + 3] = codebook->y3;
00205 if (!s->palette_video) {
00206 s->frame.data[1][iu[1] + 1] = codebook->u;
00207 s->frame.data[2][iv[1] + 1] = codebook->v;
00208 }
00209
00210 } else if (flag & mask) {
00211 if ((data + 4) > eod)
00212 return AVERROR_INVALIDDATA;
00213
00214 codebook = &strip->v4_codebook[*data++];
00215 s->frame.data[0][iy[0] + 0] = codebook->y0;
00216 s->frame.data[0][iy[0] + 1] = codebook->y1;
00217 s->frame.data[0][iy[1] + 0] = codebook->y2;
00218 s->frame.data[0][iy[1] + 1] = codebook->y3;
00219 if (!s->palette_video) {
00220 s->frame.data[1][iu[0]] = codebook->u;
00221 s->frame.data[2][iv[0]] = codebook->v;
00222 }
00223
00224 codebook = &strip->v4_codebook[*data++];
00225 s->frame.data[0][iy[0] + 2] = codebook->y0;
00226 s->frame.data[0][iy[0] + 3] = codebook->y1;
00227 s->frame.data[0][iy[1] + 2] = codebook->y2;
00228 s->frame.data[0][iy[1] + 3] = codebook->y3;
00229 if (!s->palette_video) {
00230 s->frame.data[1][iu[0] + 1] = codebook->u;
00231 s->frame.data[2][iv[0] + 1] = codebook->v;
00232 }
00233
00234 codebook = &strip->v4_codebook[*data++];
00235 s->frame.data[0][iy[2] + 0] = codebook->y0;
00236 s->frame.data[0][iy[2] + 1] = codebook->y1;
00237 s->frame.data[0][iy[3] + 0] = codebook->y2;
00238 s->frame.data[0][iy[3] + 1] = codebook->y3;
00239 if (!s->palette_video) {
00240 s->frame.data[1][iu[1]] = codebook->u;
00241 s->frame.data[2][iv[1]] = codebook->v;
00242 }
00243
00244 codebook = &strip->v4_codebook[*data++];
00245 s->frame.data[0][iy[2] + 2] = codebook->y0;
00246 s->frame.data[0][iy[2] + 3] = codebook->y1;
00247 s->frame.data[0][iy[3] + 2] = codebook->y2;
00248 s->frame.data[0][iy[3] + 3] = codebook->y3;
00249 if (!s->palette_video) {
00250 s->frame.data[1][iu[1] + 1] = codebook->u;
00251 s->frame.data[2][iv[1] + 1] = codebook->v;
00252 }
00253
00254 }
00255 }
00256
00257 iy[0] += 4; iy[1] += 4;
00258 iy[2] += 4; iy[3] += 4;
00259 iu[0] += 2; iu[1] += 2;
00260 iv[0] += 2; iv[1] += 2;
00261 }
00262 }
00263
00264 return 0;
00265 }
00266
00267 static int cinepak_decode_strip (CinepakContext *s,
00268 cvid_strip *strip, const uint8_t *data, int size)
00269 {
00270 const uint8_t *eod = (data + size);
00271 int chunk_id, chunk_size;
00272
00273
00274 if (strip->x2 > s->width ||
00275 strip->y2 > s->height ||
00276 strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
00277 return AVERROR_INVALIDDATA;
00278
00279 while ((data + 4) <= eod) {
00280 chunk_id = data[0];
00281 chunk_size = AV_RB24 (&data[1]) - 4;
00282 if(chunk_size < 0)
00283 return AVERROR_INVALIDDATA;
00284
00285 data += 4;
00286 chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
00287
00288 switch (chunk_id) {
00289
00290 case 0x20:
00291 case 0x21:
00292 case 0x24:
00293 case 0x25:
00294 cinepak_decode_codebook (strip->v4_codebook, chunk_id,
00295 chunk_size, data);
00296 break;
00297
00298 case 0x22:
00299 case 0x23:
00300 case 0x26:
00301 case 0x27:
00302 cinepak_decode_codebook (strip->v1_codebook, chunk_id,
00303 chunk_size, data);
00304 break;
00305
00306 case 0x30:
00307 case 0x31:
00308 case 0x32:
00309 return cinepak_decode_vectors (s, strip, chunk_id,
00310 chunk_size, data);
00311 }
00312
00313 data += chunk_size;
00314 }
00315
00316 return AVERROR_INVALIDDATA;
00317 }
00318
00319 static int cinepak_decode (CinepakContext *s)
00320 {
00321 const uint8_t *eod = (s->data + s->size);
00322 int i, result, strip_size, frame_flags, num_strips;
00323 int y0 = 0;
00324 int encoded_buf_size;
00325
00326 if (s->size < 10)
00327 return AVERROR_INVALIDDATA;
00328
00329 frame_flags = s->data[0];
00330 num_strips = AV_RB16 (&s->data[8]);
00331 encoded_buf_size = AV_RB24(&s->data[1]);
00332
00333
00334 if (s->sega_film_skip_bytes == -1) {
00335 if (!encoded_buf_size) {
00336 av_log_ask_for_sample(s->avctx, "encoded_buf_size is 0");
00337 return AVERROR_INVALIDDATA;
00338 }
00339 if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
00340
00341
00342
00343
00344
00345
00346 if (s->size >= 16 &&
00347 (s->data[10] == 0xFE) &&
00348 (s->data[11] == 0x00) &&
00349 (s->data[12] == 0x00) &&
00350 (s->data[13] == 0x06) &&
00351 (s->data[14] == 0x00) &&
00352 (s->data[15] == 0x00))
00353 s->sega_film_skip_bytes = 6;
00354 else
00355 s->sega_film_skip_bytes = 2;
00356 } else
00357 s->sega_film_skip_bytes = 0;
00358 }
00359
00360 s->data += 10 + s->sega_film_skip_bytes;
00361
00362 num_strips = FFMIN(num_strips, MAX_STRIPS);
00363
00364 s->frame.key_frame = 0;
00365
00366 for (i=0; i < num_strips; i++) {
00367 if ((s->data + 12) > eod)
00368 return AVERROR_INVALIDDATA;
00369
00370 s->strips[i].id = s->data[0];
00371 s->strips[i].y1 = y0;
00372 s->strips[i].x1 = 0;
00373 s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
00374 s->strips[i].x2 = s->avctx->width;
00375
00376 if (s->strips[i].id == 0x10)
00377 s->frame.key_frame = 1;
00378
00379 strip_size = AV_RB24 (&s->data[1]) - 12;
00380 if (strip_size < 0)
00381 return AVERROR_INVALIDDATA;
00382 s->data += 12;
00383 strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
00384
00385 if ((i > 0) && !(frame_flags & 0x01)) {
00386 memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
00387 sizeof(s->strips[i].v4_codebook));
00388 memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
00389 sizeof(s->strips[i].v1_codebook));
00390 }
00391
00392 result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
00393
00394 if (result != 0)
00395 return result;
00396
00397 s->data += strip_size;
00398 y0 = s->strips[i].y2;
00399 }
00400 return 0;
00401 }
00402
00403 static av_cold int cinepak_decode_init(AVCodecContext *avctx)
00404 {
00405 CinepakContext *s = avctx->priv_data;
00406
00407 s->avctx = avctx;
00408 s->width = (avctx->width + 3) & ~3;
00409 s->height = (avctx->height + 3) & ~3;
00410 s->sega_film_skip_bytes = -1;
00411
00412
00413 if (avctx->bits_per_coded_sample != 8) {
00414 s->palette_video = 0;
00415 avctx->pix_fmt = PIX_FMT_YUV420P;
00416 } else {
00417 s->palette_video = 1;
00418 avctx->pix_fmt = PIX_FMT_PAL8;
00419 }
00420
00421 avcodec_get_frame_defaults(&s->frame);
00422 s->frame.data[0] = NULL;
00423
00424 return 0;
00425 }
00426
00427 static int cinepak_decode_frame(AVCodecContext *avctx,
00428 void *data, int *data_size,
00429 AVPacket *avpkt)
00430 {
00431 const uint8_t *buf = avpkt->data;
00432 int ret = 0, buf_size = avpkt->size;
00433 CinepakContext *s = avctx->priv_data;
00434
00435 s->data = buf;
00436 s->size = buf_size;
00437
00438 s->frame.reference = 3;
00439 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00440 FF_BUFFER_HINTS_REUSABLE;
00441 if ((ret = avctx->reget_buffer(avctx, &s->frame))) {
00442 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00443 return ret;
00444 }
00445
00446 if (s->palette_video) {
00447 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00448 if (pal) {
00449 s->frame.palette_has_changed = 1;
00450 memcpy(s->pal, pal, AVPALETTE_SIZE);
00451 }
00452 }
00453
00454 cinepak_decode(s);
00455
00456 if (s->palette_video)
00457 memcpy (s->frame.data[1], s->pal, AVPALETTE_SIZE);
00458
00459 *data_size = sizeof(AVFrame);
00460 *(AVFrame*)data = s->frame;
00461
00462
00463 return buf_size;
00464 }
00465
00466 static av_cold int cinepak_decode_end(AVCodecContext *avctx)
00467 {
00468 CinepakContext *s = avctx->priv_data;
00469
00470 if (s->frame.data[0])
00471 avctx->release_buffer(avctx, &s->frame);
00472
00473 return 0;
00474 }
00475
00476 AVCodec ff_cinepak_decoder = {
00477 .name = "cinepak",
00478 .type = AVMEDIA_TYPE_VIDEO,
00479 .id = AV_CODEC_ID_CINEPAK,
00480 .priv_data_size = sizeof(CinepakContext),
00481 .init = cinepak_decode_init,
00482 .close = cinepak_decode_end,
00483 .decode = cinepak_decode_frame,
00484 .capabilities = CODEC_CAP_DR1,
00485 .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
00486 };