00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00065 #include <stdio.h>
00066 #include <stdlib.h>
00067 #include <string.h>
00068 #include <unistd.h>
00069
00070 #include "common.h"
00071 #include "avcodec.h"
00072 #include "dsputil.h"
00073
00074 #define PALETTE_COUNT 256
00075 #define VQA_HEADER_SIZE 0x2A
00076 #define CHUNK_PREAMBLE_SIZE 8
00077
00078
00079
00080 #define MAX_CODEBOOK_VECTORS 0xFF00
00081 #define SOLID_PIXEL_VECTORS 0x100
00082 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
00083 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4)
00084
00085 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
00086 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
00087 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
00088 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
00089 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
00090 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
00091 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
00092
00093 #define VQA_DEBUG 0
00094
00095 #if VQA_DEBUG
00096 #define vqa_debug printf
00097 #else
00098 static inline void vqa_debug(const char *format, ...) { }
00099 #endif
00100
00101 typedef struct VqaContext {
00102
00103 AVCodecContext *avctx;
00104 DSPContext dsp;
00105 AVFrame frame;
00106
00107 unsigned char *buf;
00108 int size;
00109
00110 unsigned int palette[PALETTE_COUNT];
00111
00112 int width;
00113 int height;
00114 int vector_width;
00115 int vector_height;
00116 int vqa_version;
00117
00118 unsigned char *codebook;
00119 int codebook_size;
00120 unsigned char *next_codebook_buffer;
00121 int next_codebook_buffer_index;
00122
00123 unsigned char *decode_buffer;
00124 int decode_buffer_size;
00125
00126
00127 int partial_countdown;
00128 int partial_count;
00129
00130 } VqaContext;
00131
00132 static int vqa_decode_init(AVCodecContext *avctx)
00133 {
00134 VqaContext *s = (VqaContext *)avctx->priv_data;
00135 unsigned char *vqa_header;
00136 int i, j, codebook_index;;
00137
00138 s->avctx = avctx;
00139 avctx->pix_fmt = PIX_FMT_PAL8;
00140 avctx->has_b_frames = 0;
00141 dsputil_init(&s->dsp, avctx);
00142
00143
00144 if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
00145 av_log(s->avctx, AV_LOG_ERROR, " VQA video: expected extradata size of %d\n", VQA_HEADER_SIZE);
00146 return -1;
00147 }
00148
00149
00150 vqa_header = (unsigned char *)s->avctx->extradata;
00151 s->vqa_version = vqa_header[0];
00152 s->width = LE_16(&vqa_header[6]);
00153 s->height = LE_16(&vqa_header[8]);
00154 if(avcodec_check_dimensions(avctx, s->width, s->height)){
00155 s->width= s->height= 0;
00156 return -1;
00157 }
00158 s->vector_width = vqa_header[10];
00159 s->vector_height = vqa_header[11];
00160 s->partial_count = s->partial_countdown = vqa_header[13];
00161
00162
00163 if ((s->vector_width != 4) ||
00164 ((s->vector_height != 2) && (s->vector_height != 4))) {
00165
00166 return -1;
00167 }
00168
00169
00170 s->codebook_size = MAX_CODEBOOK_SIZE;
00171 s->codebook = av_malloc(s->codebook_size);
00172 s->next_codebook_buffer = av_malloc(s->codebook_size);
00173
00174
00175 if (s->vector_height == 4) {
00176 codebook_index = 0xFF00 * 16;
00177 for (i = 0; i < 256; i++)
00178 for (j = 0; j < 16; j++)
00179 s->codebook[codebook_index++] = i;
00180 } else {
00181 codebook_index = 0xF00 * 8;
00182 for (i = 0; i < 256; i++)
00183 for (j = 0; j < 8; j++)
00184 s->codebook[codebook_index++] = i;
00185 }
00186 s->next_codebook_buffer_index = 0;
00187
00188
00189 s->decode_buffer_size = (s->width / s->vector_width) *
00190 (s->height / s->vector_height) * 2;
00191 s->decode_buffer = av_malloc(s->decode_buffer_size);
00192
00193 s->frame.data[0] = NULL;
00194
00195 return 0;
00196 }
00197
00198 #define CHECK_COUNT() \
00199 if (dest_index + count > dest_size) { \
00200 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
00201 av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
00202 dest_index, count, dest_size); \
00203 return; \
00204 }
00205
00206 static void decode_format80(unsigned char *src, int src_size,
00207 unsigned char *dest, int dest_size, int check_size) {
00208
00209 int src_index = 0;
00210 int dest_index = 0;
00211 int count;
00212 int src_pos;
00213 unsigned char color;
00214 int i;
00215
00216 while (src_index < src_size) {
00217
00218 vqa_debug(" opcode %02X: ", src[src_index]);
00219
00220
00221 if (src[src_index] == 0x80)
00222 return;
00223
00224 if (dest_index >= dest_size) {
00225 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
00226 dest_index, dest_size);
00227 return;
00228 }
00229
00230 if (src[src_index] == 0xFF) {
00231
00232 src_index++;
00233 count = LE_16(&src[src_index]);
00234 src_index += 2;
00235 src_pos = LE_16(&src[src_index]);
00236 src_index += 2;
00237 vqa_debug("(1) copy %X bytes from absolute pos %X\n", count, src_pos);
00238 CHECK_COUNT();
00239 for (i = 0; i < count; i++)
00240 dest[dest_index + i] = dest[src_pos + i];
00241 dest_index += count;
00242
00243 } else if (src[src_index] == 0xFE) {
00244
00245 src_index++;
00246 count = LE_16(&src[src_index]);
00247 src_index += 2;
00248 color = src[src_index++];
00249 vqa_debug("(2) set %X bytes to %02X\n", count, color);
00250 CHECK_COUNT();
00251 memset(&dest[dest_index], color, count);
00252 dest_index += count;
00253
00254 } else if ((src[src_index] & 0xC0) == 0xC0) {
00255
00256 count = (src[src_index++] & 0x3F) + 3;
00257 src_pos = LE_16(&src[src_index]);
00258 src_index += 2;
00259 vqa_debug("(3) copy %X bytes from absolute pos %X\n", count, src_pos);
00260 CHECK_COUNT();
00261 for (i = 0; i < count; i++)
00262 dest[dest_index + i] = dest[src_pos + i];
00263 dest_index += count;
00264
00265 } else if (src[src_index] > 0x80) {
00266
00267 count = src[src_index++] & 0x3F;
00268 vqa_debug("(4) copy %X bytes from source to dest\n", count);
00269 CHECK_COUNT();
00270 memcpy(&dest[dest_index], &src[src_index], count);
00271 src_index += count;
00272 dest_index += count;
00273
00274 } else {
00275
00276 count = ((src[src_index] & 0x70) >> 4) + 3;
00277 src_pos = BE_16(&src[src_index]) & 0x0FFF;
00278 src_index += 2;
00279 vqa_debug("(5) copy %X bytes from relpos %X\n", count, src_pos);
00280 CHECK_COUNT();
00281 for (i = 0; i < count; i++)
00282 dest[dest_index + i] = dest[dest_index - src_pos + i];
00283 dest_index += count;
00284 }
00285 }
00286
00287
00288
00289
00290
00291 if (check_size)
00292 if (dest_index < dest_size)
00293 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
00294 dest_index, dest_size);
00295 }
00296
00297 static void vqa_decode_chunk(VqaContext *s)
00298 {
00299 unsigned int chunk_type;
00300 unsigned int chunk_size;
00301 int byte_skip;
00302 unsigned int index = 0;
00303 int i;
00304 unsigned char r, g, b;
00305 int index_shift;
00306
00307 int cbf0_chunk = -1;
00308 int cbfz_chunk = -1;
00309 int cbp0_chunk = -1;
00310 int cbpz_chunk = -1;
00311 int cpl0_chunk = -1;
00312 int cplz_chunk = -1;
00313 int vptz_chunk = -1;
00314
00315 int x, y;
00316 int lines = 0;
00317 int pixel_ptr;
00318 int vector_index = 0;
00319 int lobyte = 0;
00320 int hibyte = 0;
00321 int lobytes = 0;
00322 int hibytes = s->decode_buffer_size / 2;
00323
00324
00325 while (index < s->size) {
00326
00327 chunk_type = BE_32(&s->buf[index]);
00328 chunk_size = BE_32(&s->buf[index + 4]);
00329
00330 switch (chunk_type) {
00331
00332 case CBF0_TAG:
00333 cbf0_chunk = index;
00334 break;
00335
00336 case CBFZ_TAG:
00337 cbfz_chunk = index;
00338 break;
00339
00340 case CBP0_TAG:
00341 cbp0_chunk = index;
00342 break;
00343
00344 case CBPZ_TAG:
00345 cbpz_chunk = index;
00346 break;
00347
00348 case CPL0_TAG:
00349 cpl0_chunk = index;
00350 break;
00351
00352 case CPLZ_TAG:
00353 cplz_chunk = index;
00354 break;
00355
00356 case VPTZ_TAG:
00357 vptz_chunk = index;
00358 break;
00359
00360 default:
00361 av_log(s->avctx, AV_LOG_ERROR, " VQA video: Found unknown chunk type: %c%c%c%c (%08X)\n",
00362 (chunk_type >> 24) & 0xFF,
00363 (chunk_type >> 16) & 0xFF,
00364 (chunk_type >> 8) & 0xFF,
00365 (chunk_type >> 0) & 0xFF,
00366 chunk_type);
00367 break;
00368 }
00369
00370 byte_skip = chunk_size & 0x01;
00371 index += (CHUNK_PREAMBLE_SIZE + chunk_size + byte_skip);
00372 }
00373
00374
00375 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
00376
00377
00378 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CPL0 and CPLZ chunks\n");
00379 return;
00380 }
00381
00382
00383 if (cplz_chunk != -1) {
00384
00385
00386
00387 }
00388
00389
00390 if (cpl0_chunk != -1) {
00391
00392 chunk_size = BE_32(&s->buf[cpl0_chunk + 4]);
00393
00394 if (chunk_size / 3 > 256) {
00395 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found a palette chunk with %d colors\n",
00396 chunk_size / 3);
00397 return;
00398 }
00399 cpl0_chunk += CHUNK_PREAMBLE_SIZE;
00400 for (i = 0; i < chunk_size / 3; i++) {
00401
00402 r = s->buf[cpl0_chunk++] * 4;
00403 g = s->buf[cpl0_chunk++] * 4;
00404 b = s->buf[cpl0_chunk++] * 4;
00405 s->palette[i] = (r << 16) | (g << 8) | (b);
00406 }
00407 }
00408
00409
00410 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
00411
00412
00413 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBF0 and CBFZ chunks\n");
00414 return;
00415 }
00416
00417
00418 if (cbfz_chunk != -1) {
00419
00420 chunk_size = BE_32(&s->buf[cbfz_chunk + 4]);
00421 cbfz_chunk += CHUNK_PREAMBLE_SIZE;
00422 decode_format80(&s->buf[cbfz_chunk], chunk_size,
00423 s->codebook, s->codebook_size, 0);
00424 }
00425
00426
00427 if (cbf0_chunk != -1) {
00428
00429 chunk_size = BE_32(&s->buf[cbf0_chunk + 4]);
00430
00431 if (chunk_size > MAX_CODEBOOK_SIZE) {
00432 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
00433 chunk_size);
00434 return;
00435 }
00436 cbf0_chunk += CHUNK_PREAMBLE_SIZE;
00437
00438 memcpy(s->codebook, &s->buf[cbf0_chunk], chunk_size);
00439 }
00440
00441
00442 if (vptz_chunk == -1) {
00443
00444
00445 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: no VPTZ chunk found\n");
00446 return;
00447 }
00448
00449 chunk_size = BE_32(&s->buf[vptz_chunk + 4]);
00450 vptz_chunk += CHUNK_PREAMBLE_SIZE;
00451 decode_format80(&s->buf[vptz_chunk], chunk_size,
00452 s->decode_buffer, s->decode_buffer_size, 1);
00453
00454
00455 if (s->vector_height == 4)
00456 index_shift = 4;
00457 else
00458 index_shift = 3;
00459 for (y = 0; y < s->frame.linesize[0] * s->height;
00460 y += s->frame.linesize[0] * s->vector_height) {
00461
00462 for (x = y; x < y + s->width; x += 4, lobytes++, hibytes++) {
00463 pixel_ptr = x;
00464
00465
00466
00467 switch (s->vqa_version) {
00468
00469 case 1:
00470
00471
00472 lines = 0;
00473 break;
00474
00475 case 2:
00476 lobyte = s->decode_buffer[lobytes];
00477 hibyte = s->decode_buffer[hibytes];
00478 vector_index = (hibyte << 8) | lobyte;
00479 vector_index <<= index_shift;
00480 lines = s->vector_height;
00481 break;
00482
00483 case 3:
00484
00485 lines = 0;
00486 break;
00487 }
00488
00489 while (lines--) {
00490 s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
00491 s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
00492 s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
00493 s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
00494 pixel_ptr += s->frame.linesize[0];
00495 }
00496 }
00497 }
00498
00499
00500 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
00501
00502 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBP0 and CBPZ chunks\n");
00503 return;
00504 }
00505
00506 if (cbp0_chunk != -1) {
00507
00508 chunk_size = BE_32(&s->buf[cbp0_chunk + 4]);
00509 cbp0_chunk += CHUNK_PREAMBLE_SIZE;
00510
00511
00512 memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
00513 &s->buf[cbp0_chunk], chunk_size);
00514 s->next_codebook_buffer_index += chunk_size;
00515
00516 s->partial_countdown--;
00517 if (s->partial_countdown == 0) {
00518
00519
00520 memcpy(s->codebook, s->next_codebook_buffer,
00521 s->next_codebook_buffer_index);
00522
00523
00524 s->next_codebook_buffer_index = 0;
00525 s->partial_countdown = s->partial_count;
00526 }
00527 }
00528
00529 if (cbpz_chunk != -1) {
00530
00531 chunk_size = BE_32(&s->buf[cbpz_chunk + 4]);
00532 cbpz_chunk += CHUNK_PREAMBLE_SIZE;
00533
00534
00535 memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
00536 &s->buf[cbpz_chunk], chunk_size);
00537 s->next_codebook_buffer_index += chunk_size;
00538
00539 s->partial_countdown--;
00540 if (s->partial_countdown == 0) {
00541
00542
00543 decode_format80(s->next_codebook_buffer,
00544 s->next_codebook_buffer_index,
00545 s->codebook, s->codebook_size, 0);
00546
00547
00548 s->next_codebook_buffer_index = 0;
00549 s->partial_countdown = s->partial_count;
00550 }
00551 }
00552 }
00553
00554 static int vqa_decode_frame(AVCodecContext *avctx,
00555 void *data, int *data_size,
00556 uint8_t *buf, int buf_size)
00557 {
00558 VqaContext *s = (VqaContext *)avctx->priv_data;
00559
00560 s->buf = buf;
00561 s->size = buf_size;
00562
00563 if (s->frame.data[0])
00564 avctx->release_buffer(avctx, &s->frame);
00565
00566 if (avctx->get_buffer(avctx, &s->frame)) {
00567 av_log(s->avctx, AV_LOG_ERROR, " VQA Video: get_buffer() failed\n");
00568 return -1;
00569 }
00570
00571 vqa_decode_chunk(s);
00572
00573
00574 memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
00575 s->frame.palette_has_changed = 1;
00576
00577 *data_size = sizeof(AVFrame);
00578 *(AVFrame*)data = s->frame;
00579
00580
00581 return buf_size;
00582 }
00583
00584 static int vqa_decode_end(AVCodecContext *avctx)
00585 {
00586 VqaContext *s = (VqaContext *)avctx->priv_data;
00587
00588 av_free(s->codebook);
00589 av_free(s->next_codebook_buffer);
00590 av_free(s->decode_buffer);
00591
00592 if (s->frame.data[0])
00593 avctx->release_buffer(avctx, &s->frame);
00594
00595 return 0;
00596 }
00597
00598 AVCodec vqa_decoder = {
00599 "vqavideo",
00600 CODEC_TYPE_VIDEO,
00601 CODEC_ID_WS_VQA,
00602 sizeof(VqaContext),
00603 vqa_decode_init,
00604 NULL,
00605 vqa_decode_end,
00606 vqa_decode_frame,
00607 CODEC_CAP_DR1,
00608 };