annotate ffmpeg/libavcodec/vqavideo.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Westwood Studios VQA Video Decoder
yading@10 3 * Copyright (C) 2003 the ffmpeg project
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * VQA Video Decoder
yading@10 25 * @author Mike Melanson (melanson@pcisys.net)
yading@10 26 * @see http://wiki.multimedia.cx/index.php?title=VQA
yading@10 27 *
yading@10 28 * The VQA video decoder outputs PAL8 or RGB555 colorspace data, depending
yading@10 29 * on the type of data in the file.
yading@10 30 *
yading@10 31 * This decoder needs the 42-byte VQHD header from the beginning
yading@10 32 * of the VQA file passed through the extradata field. The VQHD header
yading@10 33 * is laid out as:
yading@10 34 *
yading@10 35 * bytes 0-3 chunk fourcc: 'VQHD'
yading@10 36 * bytes 4-7 chunk size in big-endian format, should be 0x0000002A
yading@10 37 * bytes 8-49 VQHD chunk data
yading@10 38 *
yading@10 39 * Bytes 8-49 are what this decoder expects to see.
yading@10 40 *
yading@10 41 * Briefly, VQA is a vector quantized animation format that operates in a
yading@10 42 * VGA palettized colorspace. It operates on pixel vectors (blocks)
yading@10 43 * of either 4x2 or 4x4 in size. Compressed VQA chunks can contain vector
yading@10 44 * codebooks, palette information, and code maps for rendering vectors onto
yading@10 45 * frames. Any of these components can also be compressed with a run-length
yading@10 46 * encoding (RLE) algorithm commonly referred to as "format80".
yading@10 47 *
yading@10 48 * VQA takes a novel approach to rate control. Each group of n frames
yading@10 49 * (usually, n = 8) relies on a different vector codebook. Rather than
yading@10 50 * transporting an entire codebook every 8th frame, the new codebook is
yading@10 51 * broken up into 8 pieces and sent along with the compressed video chunks
yading@10 52 * for each of the 8 frames preceding the 8 frames which require the
yading@10 53 * codebook. A full codebook is also sent on the very first frame of a
yading@10 54 * file. This is an interesting technique, although it makes random file
yading@10 55 * seeking difficult despite the fact that the frames are all intracoded.
yading@10 56 *
yading@10 57 * V1,2 VQA uses 12-bit codebook indexes. If the 12-bit indexes were
yading@10 58 * packed into bytes and then RLE compressed, bytewise, the results would
yading@10 59 * be poor. That is why the coding method divides each index into 2 parts,
yading@10 60 * the top 4 bits and the bottom 8 bits, then RL encodes the 4-bit pieces
yading@10 61 * together and the 8-bit pieces together. If most of the vectors are
yading@10 62 * clustered into one group of 256 vectors, most of the 4-bit index pieces
yading@10 63 * should be the same.
yading@10 64 */
yading@10 65
yading@10 66 #include <stdio.h>
yading@10 67 #include <stdlib.h>
yading@10 68 #include <string.h>
yading@10 69
yading@10 70 #include "libavutil/intreadwrite.h"
yading@10 71 #include "libavutil/imgutils.h"
yading@10 72 #include "avcodec.h"
yading@10 73 #include "bytestream.h"
yading@10 74 #include "internal.h"
yading@10 75
yading@10 76 #define PALETTE_COUNT 256
yading@10 77 #define VQA_HEADER_SIZE 0x2A
yading@10 78
yading@10 79 /* allocate the maximum vector space, regardless of the file version:
yading@10 80 * (0xFF00 codebook vectors + 0x100 solid pixel vectors) * (4x4 pixels/block) */
yading@10 81 #define MAX_CODEBOOK_VECTORS 0xFF00
yading@10 82 #define SOLID_PIXEL_VECTORS 0x100
yading@10 83 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
yading@10 84 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4)
yading@10 85
yading@10 86 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
yading@10 87 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
yading@10 88 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
yading@10 89 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
yading@10 90 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
yading@10 91 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
yading@10 92 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
yading@10 93
yading@10 94 typedef struct VqaContext {
yading@10 95
yading@10 96 AVCodecContext *avctx;
yading@10 97 GetByteContext gb;
yading@10 98
yading@10 99 uint32_t palette[PALETTE_COUNT];
yading@10 100
yading@10 101 int width; /* width of a frame */
yading@10 102 int height; /* height of a frame */
yading@10 103 int vector_width; /* width of individual vector */
yading@10 104 int vector_height; /* height of individual vector */
yading@10 105 int vqa_version; /* this should be either 1, 2 or 3 */
yading@10 106
yading@10 107 unsigned char *codebook; /* the current codebook */
yading@10 108 int codebook_size;
yading@10 109 unsigned char *next_codebook_buffer; /* accumulator for next codebook */
yading@10 110 int next_codebook_buffer_index;
yading@10 111
yading@10 112 unsigned char *decode_buffer;
yading@10 113 int decode_buffer_size;
yading@10 114
yading@10 115 /* number of frames to go before replacing codebook */
yading@10 116 int partial_countdown;
yading@10 117 int partial_count;
yading@10 118
yading@10 119 } VqaContext;
yading@10 120
yading@10 121 static av_cold int vqa_decode_init(AVCodecContext *avctx)
yading@10 122 {
yading@10 123 VqaContext *s = avctx->priv_data;
yading@10 124 int i, j, codebook_index, ret;
yading@10 125
yading@10 126 s->avctx = avctx;
yading@10 127 avctx->pix_fmt = AV_PIX_FMT_PAL8;
yading@10 128
yading@10 129 /* make sure the extradata made it */
yading@10 130 if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
yading@10 131 av_log(s->avctx, AV_LOG_ERROR, "expected extradata size of %d\n", VQA_HEADER_SIZE);
yading@10 132 return AVERROR(EINVAL);
yading@10 133 }
yading@10 134
yading@10 135 /* load up the VQA parameters from the header */
yading@10 136 s->vqa_version = s->avctx->extradata[0];
yading@10 137 if (s->vqa_version < 1 || s->vqa_version > 3) {
yading@10 138 av_log(s->avctx, AV_LOG_ERROR, "unsupported version %d\n", s->vqa_version);
yading@10 139 return AVERROR_PATCHWELCOME;
yading@10 140 }
yading@10 141 s->width = AV_RL16(&s->avctx->extradata[6]);
yading@10 142 s->height = AV_RL16(&s->avctx->extradata[8]);
yading@10 143 if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0) {
yading@10 144 s->width= s->height= 0;
yading@10 145 return ret;
yading@10 146 }
yading@10 147 s->vector_width = s->avctx->extradata[10];
yading@10 148 s->vector_height = s->avctx->extradata[11];
yading@10 149 s->partial_count = s->partial_countdown = s->avctx->extradata[13];
yading@10 150
yading@10 151 /* the vector dimensions have to meet very stringent requirements */
yading@10 152 if ((s->vector_width != 4) ||
yading@10 153 ((s->vector_height != 2) && (s->vector_height != 4))) {
yading@10 154 /* return without further initialization */
yading@10 155 return AVERROR_INVALIDDATA;
yading@10 156 }
yading@10 157
yading@10 158 if (s->width % s->vector_width || s->height % s->vector_height) {
yading@10 159 av_log(avctx, AV_LOG_ERROR, "Image size not multiple of block size\n");
yading@10 160 return AVERROR_INVALIDDATA;
yading@10 161 }
yading@10 162
yading@10 163 /* allocate codebooks */
yading@10 164 s->codebook_size = MAX_CODEBOOK_SIZE;
yading@10 165 s->codebook = av_malloc(s->codebook_size);
yading@10 166 if (!s->codebook)
yading@10 167 goto fail;
yading@10 168 s->next_codebook_buffer = av_malloc(s->codebook_size);
yading@10 169 if (!s->next_codebook_buffer)
yading@10 170 goto fail;
yading@10 171
yading@10 172 /* allocate decode buffer */
yading@10 173 s->decode_buffer_size = (s->width / s->vector_width) *
yading@10 174 (s->height / s->vector_height) * 2;
yading@10 175 s->decode_buffer = av_malloc(s->decode_buffer_size);
yading@10 176 if (!s->decode_buffer)
yading@10 177 goto fail;
yading@10 178
yading@10 179 /* initialize the solid-color vectors */
yading@10 180 if (s->vector_height == 4) {
yading@10 181 codebook_index = 0xFF00 * 16;
yading@10 182 for (i = 0; i < 256; i++)
yading@10 183 for (j = 0; j < 16; j++)
yading@10 184 s->codebook[codebook_index++] = i;
yading@10 185 } else {
yading@10 186 codebook_index = 0xF00 * 8;
yading@10 187 for (i = 0; i < 256; i++)
yading@10 188 for (j = 0; j < 8; j++)
yading@10 189 s->codebook[codebook_index++] = i;
yading@10 190 }
yading@10 191 s->next_codebook_buffer_index = 0;
yading@10 192
yading@10 193 return 0;
yading@10 194 fail:
yading@10 195 av_freep(&s->codebook);
yading@10 196 av_freep(&s->next_codebook_buffer);
yading@10 197 av_freep(&s->decode_buffer);
yading@10 198 return AVERROR(ENOMEM);
yading@10 199 }
yading@10 200
yading@10 201 #define CHECK_COUNT() \
yading@10 202 if (dest_index + count > dest_size) { \
yading@10 203 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
yading@10 204 av_log(s->avctx, AV_LOG_ERROR, "current dest_index = %d, count = %d, dest_size = %d\n", \
yading@10 205 dest_index, count, dest_size); \
yading@10 206 return AVERROR_INVALIDDATA; \
yading@10 207 }
yading@10 208
yading@10 209 #define CHECK_COPY(idx) \
yading@10 210 if (idx < 0 || idx + count > dest_size) { \
yading@10 211 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: next op would overflow dest_index\n"); \
yading@10 212 av_log(s->avctx, AV_LOG_ERROR, "current src_pos = %d, count = %d, dest_size = %d\n", \
yading@10 213 src_pos, count, dest_size); \
yading@10 214 return AVERROR_INVALIDDATA; \
yading@10 215 }
yading@10 216
yading@10 217
yading@10 218 static int decode_format80(VqaContext *s, int src_size,
yading@10 219 unsigned char *dest, int dest_size, int check_size) {
yading@10 220
yading@10 221 int dest_index = 0;
yading@10 222 int count, opcode, start;
yading@10 223 int src_pos;
yading@10 224 unsigned char color;
yading@10 225 int i;
yading@10 226
yading@10 227 start = bytestream2_tell(&s->gb);
yading@10 228 while (bytestream2_tell(&s->gb) - start < src_size) {
yading@10 229 opcode = bytestream2_get_byte(&s->gb);
yading@10 230 av_dlog(s->avctx, "opcode %02X: ", opcode);
yading@10 231
yading@10 232 /* 0x80 means that frame is finished */
yading@10 233 if (opcode == 0x80)
yading@10 234 return 0;
yading@10 235
yading@10 236 if (dest_index >= dest_size) {
yading@10 237 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
yading@10 238 dest_index, dest_size);
yading@10 239 return AVERROR_INVALIDDATA;
yading@10 240 }
yading@10 241
yading@10 242 if (opcode == 0xFF) {
yading@10 243
yading@10 244 count = bytestream2_get_le16(&s->gb);
yading@10 245 src_pos = bytestream2_get_le16(&s->gb);
yading@10 246 av_dlog(s->avctx, "(1) copy %X bytes from absolute pos %X\n", count, src_pos);
yading@10 247 CHECK_COUNT();
yading@10 248 CHECK_COPY(src_pos);
yading@10 249 for (i = 0; i < count; i++)
yading@10 250 dest[dest_index + i] = dest[src_pos + i];
yading@10 251 dest_index += count;
yading@10 252
yading@10 253 } else if (opcode == 0xFE) {
yading@10 254
yading@10 255 count = bytestream2_get_le16(&s->gb);
yading@10 256 color = bytestream2_get_byte(&s->gb);
yading@10 257 av_dlog(s->avctx, "(2) set %X bytes to %02X\n", count, color);
yading@10 258 CHECK_COUNT();
yading@10 259 memset(&dest[dest_index], color, count);
yading@10 260 dest_index += count;
yading@10 261
yading@10 262 } else if ((opcode & 0xC0) == 0xC0) {
yading@10 263
yading@10 264 count = (opcode & 0x3F) + 3;
yading@10 265 src_pos = bytestream2_get_le16(&s->gb);
yading@10 266 av_dlog(s->avctx, "(3) copy %X bytes from absolute pos %X\n", count, src_pos);
yading@10 267 CHECK_COUNT();
yading@10 268 CHECK_COPY(src_pos);
yading@10 269 for (i = 0; i < count; i++)
yading@10 270 dest[dest_index + i] = dest[src_pos + i];
yading@10 271 dest_index += count;
yading@10 272
yading@10 273 } else if (opcode > 0x80) {
yading@10 274
yading@10 275 count = opcode & 0x3F;
yading@10 276 av_dlog(s->avctx, "(4) copy %X bytes from source to dest\n", count);
yading@10 277 CHECK_COUNT();
yading@10 278 bytestream2_get_buffer(&s->gb, &dest[dest_index], count);
yading@10 279 dest_index += count;
yading@10 280
yading@10 281 } else {
yading@10 282
yading@10 283 count = ((opcode & 0x70) >> 4) + 3;
yading@10 284 src_pos = bytestream2_get_byte(&s->gb) | ((opcode & 0x0F) << 8);
yading@10 285 av_dlog(s->avctx, "(5) copy %X bytes from relpos %X\n", count, src_pos);
yading@10 286 CHECK_COUNT();
yading@10 287 CHECK_COPY(dest_index - src_pos);
yading@10 288 for (i = 0; i < count; i++)
yading@10 289 dest[dest_index + i] = dest[dest_index - src_pos + i];
yading@10 290 dest_index += count;
yading@10 291 }
yading@10 292 }
yading@10 293
yading@10 294 /* validate that the entire destination buffer was filled; this is
yading@10 295 * important for decoding frame maps since each vector needs to have a
yading@10 296 * codebook entry; it is not important for compressed codebooks because
yading@10 297 * not every entry needs to be filled */
yading@10 298 if (check_size)
yading@10 299 if (dest_index < dest_size)
yading@10 300 av_log(s->avctx, AV_LOG_ERROR, "decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
yading@10 301 dest_index, dest_size);
yading@10 302
yading@10 303 return 0; // let's display what we decoded anyway
yading@10 304 }
yading@10 305
yading@10 306 static int vqa_decode_chunk(VqaContext *s, AVFrame *frame)
yading@10 307 {
yading@10 308 unsigned int chunk_type;
yading@10 309 unsigned int chunk_size;
yading@10 310 int byte_skip;
yading@10 311 unsigned int index = 0;
yading@10 312 int i;
yading@10 313 unsigned char r, g, b;
yading@10 314 int index_shift;
yading@10 315 int res;
yading@10 316
yading@10 317 int cbf0_chunk = -1;
yading@10 318 int cbfz_chunk = -1;
yading@10 319 int cbp0_chunk = -1;
yading@10 320 int cbpz_chunk = -1;
yading@10 321 int cpl0_chunk = -1;
yading@10 322 int cplz_chunk = -1;
yading@10 323 int vptz_chunk = -1;
yading@10 324
yading@10 325 int x, y;
yading@10 326 int lines = 0;
yading@10 327 int pixel_ptr;
yading@10 328 int vector_index = 0;
yading@10 329 int lobyte = 0;
yading@10 330 int hibyte = 0;
yading@10 331 int lobytes = 0;
yading@10 332 int hibytes = s->decode_buffer_size / 2;
yading@10 333
yading@10 334 /* first, traverse through the frame and find the subchunks */
yading@10 335 while (bytestream2_get_bytes_left(&s->gb) >= 8) {
yading@10 336
yading@10 337 chunk_type = bytestream2_get_be32u(&s->gb);
yading@10 338 index = bytestream2_tell(&s->gb);
yading@10 339 chunk_size = bytestream2_get_be32u(&s->gb);
yading@10 340
yading@10 341 switch (chunk_type) {
yading@10 342
yading@10 343 case CBF0_TAG:
yading@10 344 cbf0_chunk = index;
yading@10 345 break;
yading@10 346
yading@10 347 case CBFZ_TAG:
yading@10 348 cbfz_chunk = index;
yading@10 349 break;
yading@10 350
yading@10 351 case CBP0_TAG:
yading@10 352 cbp0_chunk = index;
yading@10 353 break;
yading@10 354
yading@10 355 case CBPZ_TAG:
yading@10 356 cbpz_chunk = index;
yading@10 357 break;
yading@10 358
yading@10 359 case CPL0_TAG:
yading@10 360 cpl0_chunk = index;
yading@10 361 break;
yading@10 362
yading@10 363 case CPLZ_TAG:
yading@10 364 cplz_chunk = index;
yading@10 365 break;
yading@10 366
yading@10 367 case VPTZ_TAG:
yading@10 368 vptz_chunk = index;
yading@10 369 break;
yading@10 370
yading@10 371 default:
yading@10 372 av_log(s->avctx, AV_LOG_ERROR, "Found unknown chunk type: %c%c%c%c (%08X)\n",
yading@10 373 (chunk_type >> 24) & 0xFF,
yading@10 374 (chunk_type >> 16) & 0xFF,
yading@10 375 (chunk_type >> 8) & 0xFF,
yading@10 376 (chunk_type >> 0) & 0xFF,
yading@10 377 chunk_type);
yading@10 378 break;
yading@10 379 }
yading@10 380
yading@10 381 byte_skip = chunk_size & 0x01;
yading@10 382 bytestream2_skip(&s->gb, chunk_size + byte_skip);
yading@10 383 }
yading@10 384
yading@10 385 /* next, deal with the palette */
yading@10 386 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
yading@10 387
yading@10 388 /* a chunk should not have both chunk types */
yading@10 389 av_log(s->avctx, AV_LOG_ERROR, "problem: found both CPL0 and CPLZ chunks\n");
yading@10 390 return AVERROR_INVALIDDATA;
yading@10 391 }
yading@10 392
yading@10 393 /* decompress the palette chunk */
yading@10 394 if (cplz_chunk != -1) {
yading@10 395
yading@10 396 /* yet to be handled */
yading@10 397
yading@10 398 }
yading@10 399
yading@10 400 /* convert the RGB palette into the machine's endian format */
yading@10 401 if (cpl0_chunk != -1) {
yading@10 402
yading@10 403 bytestream2_seek(&s->gb, cpl0_chunk, SEEK_SET);
yading@10 404 chunk_size = bytestream2_get_be32(&s->gb);
yading@10 405 /* sanity check the palette size */
yading@10 406 if (chunk_size / 3 > 256 || chunk_size > bytestream2_get_bytes_left(&s->gb)) {
yading@10 407 av_log(s->avctx, AV_LOG_ERROR, "problem: found a palette chunk with %d colors\n",
yading@10 408 chunk_size / 3);
yading@10 409 return AVERROR_INVALIDDATA;
yading@10 410 }
yading@10 411 for (i = 0; i < chunk_size / 3; i++) {
yading@10 412 /* scale by 4 to transform 6-bit palette -> 8-bit */
yading@10 413 r = bytestream2_get_byteu(&s->gb) * 4;
yading@10 414 g = bytestream2_get_byteu(&s->gb) * 4;
yading@10 415 b = bytestream2_get_byteu(&s->gb) * 4;
yading@10 416 s->palette[i] = 0xFFU << 24 | r << 16 | g << 8 | b;
yading@10 417 s->palette[i] |= s->palette[i] >> 6 & 0x30303;
yading@10 418 }
yading@10 419 }
yading@10 420
yading@10 421 /* next, look for a full codebook */
yading@10 422 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
yading@10 423
yading@10 424 /* a chunk should not have both chunk types */
yading@10 425 av_log(s->avctx, AV_LOG_ERROR, "problem: found both CBF0 and CBFZ chunks\n");
yading@10 426 return AVERROR_INVALIDDATA;
yading@10 427 }
yading@10 428
yading@10 429 /* decompress the full codebook chunk */
yading@10 430 if (cbfz_chunk != -1) {
yading@10 431
yading@10 432 bytestream2_seek(&s->gb, cbfz_chunk, SEEK_SET);
yading@10 433 chunk_size = bytestream2_get_be32(&s->gb);
yading@10 434 if ((res = decode_format80(s, chunk_size, s->codebook,
yading@10 435 s->codebook_size, 0)) < 0)
yading@10 436 return res;
yading@10 437 }
yading@10 438
yading@10 439 /* copy a full codebook */
yading@10 440 if (cbf0_chunk != -1) {
yading@10 441
yading@10 442 bytestream2_seek(&s->gb, cbf0_chunk, SEEK_SET);
yading@10 443 chunk_size = bytestream2_get_be32(&s->gb);
yading@10 444 /* sanity check the full codebook size */
yading@10 445 if (chunk_size > MAX_CODEBOOK_SIZE) {
yading@10 446 av_log(s->avctx, AV_LOG_ERROR, "problem: CBF0 chunk too large (0x%X bytes)\n",
yading@10 447 chunk_size);
yading@10 448 return AVERROR_INVALIDDATA;
yading@10 449 }
yading@10 450
yading@10 451 bytestream2_get_buffer(&s->gb, s->codebook, chunk_size);
yading@10 452 }
yading@10 453
yading@10 454 /* decode the frame */
yading@10 455 if (vptz_chunk == -1) {
yading@10 456
yading@10 457 /* something is wrong if there is no VPTZ chunk */
yading@10 458 av_log(s->avctx, AV_LOG_ERROR, "problem: no VPTZ chunk found\n");
yading@10 459 return AVERROR_INVALIDDATA;
yading@10 460 }
yading@10 461
yading@10 462 bytestream2_seek(&s->gb, vptz_chunk, SEEK_SET);
yading@10 463 chunk_size = bytestream2_get_be32(&s->gb);
yading@10 464 if ((res = decode_format80(s, chunk_size,
yading@10 465 s->decode_buffer, s->decode_buffer_size, 1)) < 0)
yading@10 466 return res;
yading@10 467
yading@10 468 /* render the final PAL8 frame */
yading@10 469 if (s->vector_height == 4)
yading@10 470 index_shift = 4;
yading@10 471 else
yading@10 472 index_shift = 3;
yading@10 473 for (y = 0; y < s->height; y += s->vector_height) {
yading@10 474 for (x = 0; x < s->width; x += 4, lobytes++, hibytes++) {
yading@10 475 pixel_ptr = y * frame->linesize[0] + x;
yading@10 476
yading@10 477 /* get the vector index, the method for which varies according to
yading@10 478 * VQA file version */
yading@10 479 switch (s->vqa_version) {
yading@10 480
yading@10 481 case 1:
yading@10 482 lobyte = s->decode_buffer[lobytes * 2];
yading@10 483 hibyte = s->decode_buffer[(lobytes * 2) + 1];
yading@10 484 vector_index = ((hibyte << 8) | lobyte) >> 3;
yading@10 485 vector_index <<= index_shift;
yading@10 486 lines = s->vector_height;
yading@10 487 /* uniform color fill - a quick hack */
yading@10 488 if (hibyte == 0xFF) {
yading@10 489 while (lines--) {
yading@10 490 frame->data[0][pixel_ptr + 0] = 255 - lobyte;
yading@10 491 frame->data[0][pixel_ptr + 1] = 255 - lobyte;
yading@10 492 frame->data[0][pixel_ptr + 2] = 255 - lobyte;
yading@10 493 frame->data[0][pixel_ptr + 3] = 255 - lobyte;
yading@10 494 pixel_ptr += frame->linesize[0];
yading@10 495 }
yading@10 496 lines=0;
yading@10 497 }
yading@10 498 break;
yading@10 499
yading@10 500 case 2:
yading@10 501 lobyte = s->decode_buffer[lobytes];
yading@10 502 hibyte = s->decode_buffer[hibytes];
yading@10 503 vector_index = (hibyte << 8) | lobyte;
yading@10 504 vector_index <<= index_shift;
yading@10 505 lines = s->vector_height;
yading@10 506 break;
yading@10 507
yading@10 508 case 3:
yading@10 509 /* not implemented yet */
yading@10 510 lines = 0;
yading@10 511 break;
yading@10 512 }
yading@10 513
yading@10 514 while (lines--) {
yading@10 515 frame->data[0][pixel_ptr + 0] = s->codebook[vector_index++];
yading@10 516 frame->data[0][pixel_ptr + 1] = s->codebook[vector_index++];
yading@10 517 frame->data[0][pixel_ptr + 2] = s->codebook[vector_index++];
yading@10 518 frame->data[0][pixel_ptr + 3] = s->codebook[vector_index++];
yading@10 519 pixel_ptr += frame->linesize[0];
yading@10 520 }
yading@10 521 }
yading@10 522 }
yading@10 523
yading@10 524 /* handle partial codebook */
yading@10 525 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
yading@10 526 /* a chunk should not have both chunk types */
yading@10 527 av_log(s->avctx, AV_LOG_ERROR, "problem: found both CBP0 and CBPZ chunks\n");
yading@10 528 return AVERROR_INVALIDDATA;
yading@10 529 }
yading@10 530
yading@10 531 if (cbp0_chunk != -1) {
yading@10 532
yading@10 533 bytestream2_seek(&s->gb, cbp0_chunk, SEEK_SET);
yading@10 534 chunk_size = bytestream2_get_be32(&s->gb);
yading@10 535
yading@10 536 if (chunk_size > MAX_CODEBOOK_SIZE - s->next_codebook_buffer_index) {
yading@10 537 av_log(s->avctx, AV_LOG_ERROR, "cbp0 chunk too large (%u bytes)\n",
yading@10 538 chunk_size);
yading@10 539 return AVERROR_INVALIDDATA;
yading@10 540 }
yading@10 541
yading@10 542 /* accumulate partial codebook */
yading@10 543 bytestream2_get_buffer(&s->gb, &s->next_codebook_buffer[s->next_codebook_buffer_index],
yading@10 544 chunk_size);
yading@10 545 s->next_codebook_buffer_index += chunk_size;
yading@10 546
yading@10 547 s->partial_countdown--;
yading@10 548 if (s->partial_countdown <= 0) {
yading@10 549
yading@10 550 /* time to replace codebook */
yading@10 551 memcpy(s->codebook, s->next_codebook_buffer,
yading@10 552 s->next_codebook_buffer_index);
yading@10 553
yading@10 554 /* reset accounting */
yading@10 555 s->next_codebook_buffer_index = 0;
yading@10 556 s->partial_countdown = s->partial_count;
yading@10 557 }
yading@10 558 }
yading@10 559
yading@10 560 if (cbpz_chunk != -1) {
yading@10 561
yading@10 562 bytestream2_seek(&s->gb, cbpz_chunk, SEEK_SET);
yading@10 563 chunk_size = bytestream2_get_be32(&s->gb);
yading@10 564
yading@10 565 if (chunk_size > MAX_CODEBOOK_SIZE - s->next_codebook_buffer_index) {
yading@10 566 av_log(s->avctx, AV_LOG_ERROR, "cbpz chunk too large (%u bytes)\n",
yading@10 567 chunk_size);
yading@10 568 return AVERROR_INVALIDDATA;
yading@10 569 }
yading@10 570
yading@10 571 /* accumulate partial codebook */
yading@10 572 bytestream2_get_buffer(&s->gb, &s->next_codebook_buffer[s->next_codebook_buffer_index],
yading@10 573 chunk_size);
yading@10 574 s->next_codebook_buffer_index += chunk_size;
yading@10 575
yading@10 576 s->partial_countdown--;
yading@10 577 if (s->partial_countdown <= 0) {
yading@10 578 bytestream2_init(&s->gb, s->next_codebook_buffer, s->next_codebook_buffer_index);
yading@10 579 /* decompress codebook */
yading@10 580 if ((res = decode_format80(s, s->next_codebook_buffer_index,
yading@10 581 s->codebook, s->codebook_size, 0)) < 0)
yading@10 582 return res;
yading@10 583
yading@10 584 /* reset accounting */
yading@10 585 s->next_codebook_buffer_index = 0;
yading@10 586 s->partial_countdown = s->partial_count;
yading@10 587 }
yading@10 588 }
yading@10 589
yading@10 590 return 0;
yading@10 591 }
yading@10 592
yading@10 593 static int vqa_decode_frame(AVCodecContext *avctx,
yading@10 594 void *data, int *got_frame,
yading@10 595 AVPacket *avpkt)
yading@10 596 {
yading@10 597 VqaContext *s = avctx->priv_data;
yading@10 598 AVFrame *frame = data;
yading@10 599 int res;
yading@10 600
yading@10 601 if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
yading@10 602 return res;
yading@10 603
yading@10 604 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
yading@10 605 if ((res = vqa_decode_chunk(s, frame)) < 0)
yading@10 606 return res;
yading@10 607
yading@10 608 /* make the palette available on the way out */
yading@10 609 memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
yading@10 610 frame->palette_has_changed = 1;
yading@10 611
yading@10 612 *got_frame = 1;
yading@10 613
yading@10 614 /* report that the buffer was completely consumed */
yading@10 615 return avpkt->size;
yading@10 616 }
yading@10 617
yading@10 618 static av_cold int vqa_decode_end(AVCodecContext *avctx)
yading@10 619 {
yading@10 620 VqaContext *s = avctx->priv_data;
yading@10 621
yading@10 622 av_freep(&s->codebook);
yading@10 623 av_freep(&s->next_codebook_buffer);
yading@10 624 av_freep(&s->decode_buffer);
yading@10 625
yading@10 626 return 0;
yading@10 627 }
yading@10 628
yading@10 629 AVCodec ff_vqa_decoder = {
yading@10 630 .name = "vqavideo",
yading@10 631 .type = AVMEDIA_TYPE_VIDEO,
yading@10 632 .id = AV_CODEC_ID_WS_VQA,
yading@10 633 .priv_data_size = sizeof(VqaContext),
yading@10 634 .init = vqa_decode_init,
yading@10 635 .close = vqa_decode_end,
yading@10 636 .decode = vqa_decode_frame,
yading@10 637 .capabilities = CODEC_CAP_DR1,
yading@10 638 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA (Vector Quantized Animation) video"),
yading@10 639 };