00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/intreadwrite.h"
00034 #include "avcodec.h"
00035 #include "dsputil.h"
00036 #include "bytestream.h"
00037 #include "get_bits.h"
00038
00039 #include "indeo3data.h"
00040
00041
00042 enum {
00043 RLE_ESC_F9 = 249,
00044 RLE_ESC_FA = 250,
00045 RLE_ESC_FB = 251,
00046 RLE_ESC_FC = 252,
00047 RLE_ESC_FD = 253,
00048 RLE_ESC_FE = 254,
00049 RLE_ESC_FF = 255
00050 };
00051
00052
00053
00054 #define BS_8BIT_PEL (1 << 1)
00055 #define BS_KEYFRAME (1 << 2)
00056 #define BS_MV_Y_HALF (1 << 4)
00057 #define BS_MV_X_HALF (1 << 5)
00058 #define BS_NONREF (1 << 8)
00059 #define BS_BUFFER 9
00060
00061
00062 typedef struct Plane {
00063 uint8_t *buffers[2];
00064 uint8_t *pixels[2];
00065 uint32_t width;
00066 uint32_t height;
00067 uint32_t pitch;
00068 } Plane;
00069
00070 #define CELL_STACK_MAX 20
00071
00072 typedef struct Cell {
00073 int16_t xpos;
00074 int16_t ypos;
00075 int16_t width;
00076 int16_t height;
00077 uint8_t tree;
00078 const int8_t *mv_ptr;
00079 } Cell;
00080
00081 typedef struct Indeo3DecodeContext {
00082 AVCodecContext *avctx;
00083 AVFrame frame;
00084 DSPContext dsp;
00085
00086 GetBitContext gb;
00087 int need_resync;
00088 int skip_bits;
00089 const uint8_t *next_cell_data;
00090 const uint8_t *last_byte;
00091 const int8_t *mc_vectors;
00092 unsigned num_vectors;
00093
00094 int16_t width, height;
00095 uint32_t frame_num;
00096 uint32_t data_size;
00097 uint16_t frame_flags;
00098 uint8_t cb_offset;
00099 uint8_t buf_sel;
00100 const uint8_t *y_data_ptr;
00101 const uint8_t *v_data_ptr;
00102 const uint8_t *u_data_ptr;
00103 int32_t y_data_size;
00104 int32_t v_data_size;
00105 int32_t u_data_size;
00106 const uint8_t *alt_quant;
00107 Plane planes[3];
00108 } Indeo3DecodeContext;
00109
00110
00111 static uint8_t requant_tab[8][128];
00112
00113
00114
00115
00116
00117
00118 static av_cold void build_requant_tab(void)
00119 {
00120 static int8_t offsets[8] = { 1, 1, 2, -3, -3, 3, 4, 4 };
00121 static int8_t deltas [8] = { 0, 1, 0, 4, 4, 1, 0, 1 };
00122
00123 int i, j, step;
00124
00125 for (i = 0; i < 8; i++) {
00126 step = i + 2;
00127 for (j = 0; j < 128; j++)
00128 requant_tab[i][j] = (j + offsets[i]) / step * step + deltas[i];
00129 }
00130
00131
00132
00133
00134 requant_tab[0][127] = 126;
00135 requant_tab[1][119] = 118;
00136 requant_tab[1][120] = 118;
00137 requant_tab[2][126] = 124;
00138 requant_tab[2][127] = 124;
00139 requant_tab[6][124] = 120;
00140 requant_tab[6][125] = 120;
00141 requant_tab[6][126] = 120;
00142 requant_tab[6][127] = 120;
00143
00144
00145 requant_tab[1][7] = 10;
00146 requant_tab[4][8] = 10;
00147 }
00148
00149
00150 static av_cold int allocate_frame_buffers(Indeo3DecodeContext *ctx,
00151 AVCodecContext *avctx)
00152 {
00153 int p, luma_width, luma_height, chroma_width, chroma_height;
00154 int luma_pitch, chroma_pitch, luma_size, chroma_size;
00155
00156 luma_width = ctx->width;
00157 luma_height = ctx->height;
00158
00159 if (luma_width < 16 || luma_width > 640 ||
00160 luma_height < 16 || luma_height > 480 ||
00161 luma_width & 3 || luma_height & 3) {
00162 av_log(avctx, AV_LOG_ERROR, "Invalid picture dimensions: %d x %d!\n",
00163 luma_width, luma_height);
00164 return AVERROR_INVALIDDATA;
00165 }
00166
00167 chroma_width = FFALIGN(luma_width >> 2, 4);
00168 chroma_height = FFALIGN(luma_height >> 2, 4);
00169
00170 luma_pitch = FFALIGN(luma_width, 16);
00171 chroma_pitch = FFALIGN(chroma_width, 16);
00172
00173
00174
00175 luma_size = luma_pitch * (luma_height + 1);
00176
00177
00178
00179 chroma_size = chroma_pitch * (chroma_height + 1);
00180
00181
00182 for (p = 0; p < 3; p++) {
00183 ctx->planes[p].pitch = !p ? luma_pitch : chroma_pitch;
00184 ctx->planes[p].width = !p ? luma_width : chroma_width;
00185 ctx->planes[p].height = !p ? luma_height : chroma_height;
00186
00187 ctx->planes[p].buffers[0] = av_malloc(!p ? luma_size : chroma_size);
00188 ctx->planes[p].buffers[1] = av_malloc(!p ? luma_size : chroma_size);
00189
00190
00191 memset(ctx->planes[p].buffers[0], 0x40, ctx->planes[p].pitch);
00192 memset(ctx->planes[p].buffers[1], 0x40, ctx->planes[p].pitch);
00193
00194
00195 ctx->planes[p].pixels[0] = ctx->planes[p].buffers[0] + ctx->planes[p].pitch;
00196 ctx->planes[p].pixels[1] = ctx->planes[p].buffers[1] + ctx->planes[p].pitch;
00197 }
00198
00199 return 0;
00200 }
00201
00202
00203 static av_cold void free_frame_buffers(Indeo3DecodeContext *ctx)
00204 {
00205 int p;
00206
00207 for (p = 0; p < 3; p++) {
00208 av_freep(&ctx->planes[p].buffers[0]);
00209 av_freep(&ctx->planes[p].buffers[1]);
00210 }
00211 }
00212
00213
00222 static void copy_cell(Indeo3DecodeContext *ctx, Plane *plane, Cell *cell)
00223 {
00224 int h, w, mv_x, mv_y, offset, offset_dst;
00225 uint8_t *src, *dst;
00226
00227
00228 offset_dst = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
00229 dst = plane->pixels[ctx->buf_sel] + offset_dst;
00230 if(cell->mv_ptr){
00231 mv_y = cell->mv_ptr[0];
00232 mv_x = cell->mv_ptr[1];
00233 }else
00234 mv_x= mv_y= 0;
00235 offset = offset_dst + mv_y * plane->pitch + mv_x;
00236 src = plane->pixels[ctx->buf_sel ^ 1] + offset;
00237
00238 h = cell->height << 2;
00239
00240 for (w = cell->width; w > 0;) {
00241
00242 if (!((cell->xpos << 2) & 15) && w >= 4) {
00243 for (; w >= 4; src += 16, dst += 16, w -= 4)
00244 ctx->dsp.put_no_rnd_pixels_tab[0][0](dst, src, plane->pitch, h);
00245 }
00246
00247
00248 if (!((cell->xpos << 2) & 7) && w >= 2) {
00249 ctx->dsp.put_no_rnd_pixels_tab[1][0](dst, src, plane->pitch, h);
00250 w -= 2;
00251 src += 8;
00252 dst += 8;
00253 }
00254
00255 if (w >= 1) {
00256 copy_block4(dst, src, plane->pitch, plane->pitch, h);
00257 w--;
00258 src += 4;
00259 dst += 4;
00260 }
00261 }
00262 }
00263
00264
00265
00266 #define AVG_32(dst, src, ref) \
00267 AV_WN32A(dst, ((AV_RN32A(src) + AV_RN32A(ref)) >> 1) & 0x7F7F7F7FUL)
00268
00269 #define AVG_64(dst, src, ref) \
00270 AV_WN64A(dst, ((AV_RN64A(src) + AV_RN64A(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
00271
00272
00273
00274
00275
00276
00277 static inline uint64_t replicate64(uint64_t a) {
00278 #if HAVE_BIGENDIAN
00279 a &= 0xFF00FF00FF00FF00ULL;
00280 a |= a >> 8;
00281 #else
00282 a &= 0x00FF00FF00FF00FFULL;
00283 a |= a << 8;
00284 #endif
00285 return a;
00286 }
00287
00288 static inline uint32_t replicate32(uint32_t a) {
00289 #if HAVE_BIGENDIAN
00290 a &= 0xFF00FF00UL;
00291 a |= a >> 8;
00292 #else
00293 a &= 0x00FF00FFUL;
00294 a |= a << 8;
00295 #endif
00296 return a;
00297 }
00298
00299
00300
00301 static inline void fill_64(uint8_t *dst, const uint64_t pix, int32_t n,
00302 int32_t row_offset)
00303 {
00304 for (; n > 0; dst += row_offset, n--)
00305 AV_WN64A(dst, pix);
00306 }
00307
00308
00309
00310 enum {
00311 IV3_NOERR = 0,
00312 IV3_BAD_RLE = 1,
00313 IV3_BAD_DATA = 2,
00314 IV3_BAD_COUNTER = 3,
00315 IV3_UNSUPPORTED = 4,
00316 IV3_OUT_OF_DATA = 5
00317 };
00318
00319
00320 #define BUFFER_PRECHECK \
00321 if (*data_ptr >= last_ptr) \
00322 return IV3_OUT_OF_DATA; \
00323
00324 #define RLE_BLOCK_COPY \
00325 if (cell->mv_ptr || !skip_flag) \
00326 copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
00327
00328 #define RLE_BLOCK_COPY_8 \
00329 pix64 = AV_RN64A(ref);\
00330 if (is_first_row) {\
00331 pix64 = replicate64(pix64);\
00332 fill_64(dst + row_offset, pix64, 7, row_offset);\
00333 AVG_64(dst, ref, dst + row_offset);\
00334 } else \
00335 fill_64(dst, pix64, 8, row_offset)
00336
00337 #define RLE_LINES_COPY \
00338 copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
00339
00340 #define RLE_LINES_COPY_M10 \
00341 pix64 = AV_RN64A(ref);\
00342 if (is_top_of_cell) {\
00343 pix64 = replicate64(pix64);\
00344 fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
00345 AVG_64(dst, ref, dst + row_offset);\
00346 } else \
00347 fill_64(dst, pix64, num_lines << 1, row_offset)
00348
00349 #define APPLY_DELTA_4 \
00350 AV_WN16A(dst + line_offset , AV_RN16A(ref ) + delta_tab->deltas[dyad1]);\
00351 AV_WN16A(dst + line_offset + 2, AV_RN16A(ref + 2) + delta_tab->deltas[dyad2]);\
00352 if (mode >= 3) {\
00353 if (is_top_of_cell && !cell->ypos) {\
00354 AV_COPY32(dst, dst + row_offset);\
00355 } else {\
00356 AVG_32(dst, ref, dst + row_offset);\
00357 }\
00358 }
00359
00360 #define APPLY_DELTA_8 \
00361 \
00362 if (is_top_of_cell) { \
00363 AV_WN32A(dst + row_offset , \
00364 replicate32(AV_RN32A(ref )) + delta_tab->deltas_m10[dyad1]);\
00365 AV_WN32A(dst + row_offset + 4, \
00366 replicate32(AV_RN32A(ref + 4)) + delta_tab->deltas_m10[dyad2]);\
00367 } else { \
00368 AV_WN32A(dst + row_offset , \
00369 AV_RN32A(ref ) + delta_tab->deltas_m10[dyad1]);\
00370 AV_WN32A(dst + row_offset + 4, \
00371 AV_RN32A(ref + 4) + delta_tab->deltas_m10[dyad2]);\
00372 } \
00373 \
00374 \
00375 \
00376 if (is_top_of_cell && !cell->ypos) {\
00377 AV_COPY64(dst, dst + row_offset);\
00378 } else \
00379 AVG_64(dst, ref, dst + row_offset);
00380
00381
00382 #define APPLY_DELTA_1011_INTER \
00383 if (mode == 10) { \
00384 AV_WN32A(dst , \
00385 AV_RN32A(dst ) + delta_tab->deltas_m10[dyad1]);\
00386 AV_WN32A(dst + 4 , \
00387 AV_RN32A(dst + 4 ) + delta_tab->deltas_m10[dyad2]);\
00388 AV_WN32A(dst + row_offset , \
00389 AV_RN32A(dst + row_offset ) + delta_tab->deltas_m10[dyad1]);\
00390 AV_WN32A(dst + row_offset + 4, \
00391 AV_RN32A(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]);\
00392 } else { \
00393 AV_WN16A(dst , \
00394 AV_RN16A(dst ) + delta_tab->deltas[dyad1]);\
00395 AV_WN16A(dst + 2 , \
00396 AV_RN16A(dst + 2 ) + delta_tab->deltas[dyad2]);\
00397 AV_WN16A(dst + row_offset , \
00398 AV_RN16A(dst + row_offset ) + delta_tab->deltas[dyad1]);\
00399 AV_WN16A(dst + row_offset + 2, \
00400 AV_RN16A(dst + row_offset + 2) + delta_tab->deltas[dyad2]);\
00401 }
00402
00403
00404 static int decode_cell_data(Cell *cell, uint8_t *block, uint8_t *ref_block,
00405 int pitch, int h_zoom, int v_zoom, int mode,
00406 const vqEntry *delta[2], int swap_quads[2],
00407 const uint8_t **data_ptr, const uint8_t *last_ptr)
00408 {
00409 int x, y, line, num_lines;
00410 int rle_blocks = 0;
00411 uint8_t code, *dst, *ref;
00412 const vqEntry *delta_tab;
00413 unsigned int dyad1, dyad2;
00414 uint64_t pix64;
00415 int skip_flag = 0, is_top_of_cell, is_first_row = 1;
00416 int row_offset, blk_row_offset, line_offset;
00417
00418 row_offset = pitch;
00419 blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->width << 2);
00420 line_offset = v_zoom ? row_offset : 0;
00421
00422 if (cell->height & v_zoom || cell->width & h_zoom)
00423 return IV3_BAD_DATA;
00424
00425 for (y = 0; y < cell->height; is_first_row = 0, y += 1 + v_zoom) {
00426 for (x = 0; x < cell->width; x += 1 + h_zoom) {
00427 ref = ref_block;
00428 dst = block;
00429
00430 if (rle_blocks > 0) {
00431 if (mode <= 4) {
00432 RLE_BLOCK_COPY;
00433 } else if (mode == 10 && !cell->mv_ptr) {
00434 RLE_BLOCK_COPY_8;
00435 }
00436 rle_blocks--;
00437 } else {
00438 for (line = 0; line < 4;) {
00439 num_lines = 1;
00440 is_top_of_cell = is_first_row && !line;
00441
00442
00443 if (mode <= 4)
00444 delta_tab = delta[line & 1];
00445 else
00446 delta_tab = delta[1];
00447 BUFFER_PRECHECK;
00448 code = bytestream_get_byte(data_ptr);
00449 if (code < 248) {
00450 if (code < delta_tab->num_dyads) {
00451 BUFFER_PRECHECK;
00452 dyad1 = bytestream_get_byte(data_ptr);
00453 dyad2 = code;
00454 if (dyad1 >= delta_tab->num_dyads || dyad1 >= 248)
00455 return IV3_BAD_DATA;
00456 } else {
00457
00458 code -= delta_tab->num_dyads;
00459 dyad1 = code / delta_tab->quad_exp;
00460 dyad2 = code % delta_tab->quad_exp;
00461 if (swap_quads[line & 1])
00462 FFSWAP(unsigned int, dyad1, dyad2);
00463 }
00464 if (mode <= 4) {
00465 APPLY_DELTA_4;
00466 } else if (mode == 10 && !cell->mv_ptr) {
00467 APPLY_DELTA_8;
00468 } else {
00469 APPLY_DELTA_1011_INTER;
00470 }
00471 } else {
00472
00473 switch (code) {
00474 case RLE_ESC_FC:
00475 skip_flag = 0;
00476 rle_blocks = 1;
00477 code = 253;
00478
00479 case RLE_ESC_FF:
00480 case RLE_ESC_FE:
00481 case RLE_ESC_FD:
00482 num_lines = 257 - code - line;
00483 if (num_lines <= 0)
00484 return IV3_BAD_RLE;
00485 if (mode <= 4) {
00486 RLE_LINES_COPY;
00487 } else if (mode == 10 && !cell->mv_ptr) {
00488 RLE_LINES_COPY_M10;
00489 }
00490 break;
00491 case RLE_ESC_FB:
00492 BUFFER_PRECHECK;
00493 code = bytestream_get_byte(data_ptr);
00494 rle_blocks = (code & 0x1F) - 1;
00495 if (code >= 64 || rle_blocks < 0)
00496 return IV3_BAD_COUNTER;
00497 skip_flag = code & 0x20;
00498 num_lines = 4 - line;
00499 if (mode >= 10 || (cell->mv_ptr || !skip_flag)) {
00500 if (mode <= 4) {
00501 RLE_LINES_COPY;
00502 } else if (mode == 10 && !cell->mv_ptr) {
00503 RLE_LINES_COPY_M10;
00504 }
00505 }
00506 break;
00507 case RLE_ESC_F9:
00508 skip_flag = 1;
00509 rle_blocks = 1;
00510
00511 case RLE_ESC_FA:
00512 if (line)
00513 return IV3_BAD_RLE;
00514 num_lines = 4;
00515 if (cell->mv_ptr) {
00516 if (mode <= 4) {
00517 RLE_LINES_COPY;
00518 } else if (mode == 10 && !cell->mv_ptr) {
00519 RLE_LINES_COPY_M10;
00520 }
00521 }
00522 break;
00523 default:
00524 return IV3_UNSUPPORTED;
00525 }
00526 }
00527
00528 line += num_lines;
00529 ref += row_offset * (num_lines << v_zoom);
00530 dst += row_offset * (num_lines << v_zoom);
00531 }
00532 }
00533
00534
00535 block += 4 << h_zoom;
00536 ref_block += 4 << h_zoom;
00537 }
00538
00539
00540 ref_block += blk_row_offset;
00541 block += blk_row_offset;
00542 }
00543 return IV3_NOERR;
00544 }
00545
00546
00560 static int decode_cell(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00561 Plane *plane, Cell *cell, const uint8_t *data_ptr,
00562 const uint8_t *last_ptr)
00563 {
00564 int x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;
00565 int zoom_fac;
00566 int offset, error = 0, swap_quads[2];
00567 uint8_t code, *block, *ref_block = 0;
00568 const vqEntry *delta[2];
00569 const uint8_t *data_start = data_ptr;
00570
00571
00572 code = *data_ptr++;
00573 mode = code >> 4;
00574 vq_index = code & 0xF;
00575
00576
00577 offset = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
00578 block = plane->pixels[ctx->buf_sel] + offset;
00579 if (!cell->mv_ptr) {
00580
00581 ref_block = block - plane->pitch;
00582 } else if (mode >= 10) {
00583
00584
00585 copy_cell(ctx, plane, cell);
00586 } else {
00587
00588 mv_y = cell->mv_ptr[0];
00589 mv_x = cell->mv_ptr[1];
00590 offset += mv_y * plane->pitch + mv_x;
00591 ref_block = plane->pixels[ctx->buf_sel ^ 1] + offset;
00592 }
00593
00594
00595
00596
00597 if (mode == 1 || mode == 4) {
00598 code = ctx->alt_quant[vq_index];
00599 prim_indx = (code >> 4) + ctx->cb_offset;
00600 second_indx = (code & 0xF) + ctx->cb_offset;
00601 } else {
00602 vq_index += ctx->cb_offset;
00603 prim_indx = second_indx = vq_index;
00604 }
00605
00606 if (prim_indx >= 24 || second_indx >= 24) {
00607 av_log(avctx, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
00608 prim_indx, second_indx);
00609 return AVERROR_INVALIDDATA;
00610 }
00611
00612 delta[0] = &vq_tab[second_indx];
00613 delta[1] = &vq_tab[prim_indx];
00614 swap_quads[0] = second_indx >= 16;
00615 swap_quads[1] = prim_indx >= 16;
00616
00617
00618
00619 if (vq_index >= 8 && ref_block) {
00620 for (x = 0; x < cell->width << 2; x++)
00621 ref_block[x] = requant_tab[vq_index & 7][ref_block[x]];
00622 }
00623
00624 error = IV3_NOERR;
00625
00626 switch (mode) {
00627 case 0:
00628 case 1:
00629 case 3:
00630 case 4:
00631 if (mode >= 3 && cell->mv_ptr) {
00632 av_log(avctx, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER cell!\n");
00633 return AVERROR_INVALIDDATA;
00634 }
00635
00636 zoom_fac = mode >= 3;
00637 error = decode_cell_data(cell, block, ref_block, plane->pitch, 0, zoom_fac,
00638 mode, delta, swap_quads, &data_ptr, last_ptr);
00639 break;
00640 case 10:
00641 case 11:
00642 if (mode == 10 && !cell->mv_ptr) {
00643 error = decode_cell_data(cell, block, ref_block, plane->pitch, 1, 1,
00644 mode, delta, swap_quads, &data_ptr, last_ptr);
00645 } else {
00646 if (mode == 11 && !cell->mv_ptr) {
00647 av_log(avctx, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA cell!\n");
00648 return AVERROR_INVALIDDATA;
00649 }
00650
00651 zoom_fac = mode == 10;
00652 error = decode_cell_data(cell, block, ref_block, plane->pitch,
00653 zoom_fac, 1, mode, delta, swap_quads,
00654 &data_ptr, last_ptr);
00655 }
00656 break;
00657 default:
00658 av_log(avctx, AV_LOG_ERROR, "Unsupported coding mode: %d\n", mode);
00659 return AVERROR_INVALIDDATA;
00660 }
00661
00662 switch (error) {
00663 case IV3_BAD_RLE:
00664 av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",
00665 mode, data_ptr[-1]);
00666 return AVERROR_INVALIDDATA;
00667 case IV3_BAD_DATA:
00668 av_log(avctx, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", mode);
00669 return AVERROR_INVALIDDATA;
00670 case IV3_BAD_COUNTER:
00671 av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", mode, code);
00672 return AVERROR_INVALIDDATA;
00673 case IV3_UNSUPPORTED:
00674 av_log(avctx, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
00675 return AVERROR_INVALIDDATA;
00676 case IV3_OUT_OF_DATA:
00677 av_log(avctx, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", mode);
00678 return AVERROR_INVALIDDATA;
00679 }
00680
00681 return data_ptr - data_start;
00682 }
00683
00684
00685
00686 enum {
00687 H_SPLIT = 0,
00688 V_SPLIT = 1,
00689 INTRA_NULL = 2,
00690 INTER_DATA = 3
00691 };
00692
00693
00694 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
00695
00696 #define UPDATE_BITPOS(n) \
00697 ctx->skip_bits += (n); \
00698 ctx->need_resync = 1
00699
00700 #define RESYNC_BITSTREAM \
00701 if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
00702 skip_bits_long(&ctx->gb, ctx->skip_bits); \
00703 ctx->skip_bits = 0; \
00704 ctx->need_resync = 0; \
00705 }
00706
00707 #define CHECK_CELL \
00708 if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \
00709 curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \
00710 av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \
00711 curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
00712 return AVERROR_INVALIDDATA; \
00713 }
00714
00715
00716 static int parse_bintree(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00717 Plane *plane, int code, Cell *ref_cell,
00718 const int depth, const int strip_width)
00719 {
00720 Cell curr_cell;
00721 int bytes_used;
00722
00723 if (depth <= 0) {
00724 av_log(avctx, AV_LOG_ERROR, "Stack overflow (corrupted binary tree)!\n");
00725 return AVERROR_INVALIDDATA;
00726 }
00727
00728 curr_cell = *ref_cell;
00729 if (code == H_SPLIT) {
00730 SPLIT_CELL(ref_cell->height, curr_cell.height);
00731 ref_cell->ypos += curr_cell.height;
00732 ref_cell->height -= curr_cell.height;
00733 if (ref_cell->height <= 0 || curr_cell.height <= 0)
00734 return AVERROR_INVALIDDATA;
00735 } else if (code == V_SPLIT) {
00736 if (curr_cell.width > strip_width) {
00737
00738 curr_cell.width = (curr_cell.width <= (strip_width << 1) ? 1 : 2) * strip_width;
00739 } else
00740 SPLIT_CELL(ref_cell->width, curr_cell.width);
00741 ref_cell->xpos += curr_cell.width;
00742 ref_cell->width -= curr_cell.width;
00743 if (ref_cell->width <= 0 || curr_cell.width <= 0)
00744 return AVERROR_INVALIDDATA;
00745 }
00746
00747 while (get_bits_left(&ctx->gb) >= 2) {
00748 RESYNC_BITSTREAM;
00749 switch (code = get_bits(&ctx->gb, 2)) {
00750 case H_SPLIT:
00751 case V_SPLIT:
00752 if (parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
00753 return AVERROR_INVALIDDATA;
00754 break;
00755 case INTRA_NULL:
00756 if (!curr_cell.tree) {
00757 curr_cell.mv_ptr = 0;
00758 curr_cell.tree = 1;
00759 } else {
00760 RESYNC_BITSTREAM;
00761 code = get_bits(&ctx->gb, 2);
00762 if (code >= 2) {
00763 av_log(avctx, AV_LOG_ERROR, "Invalid VQ_NULL code: %d\n", code);
00764 return AVERROR_INVALIDDATA;
00765 }
00766 if (code == 1)
00767 av_log(avctx, AV_LOG_ERROR, "SkipCell procedure not implemented yet!\n");
00768
00769 CHECK_CELL
00770 if (!curr_cell.mv_ptr)
00771 return AVERROR_INVALIDDATA;
00772 copy_cell(ctx, plane, &curr_cell);
00773 return 0;
00774 }
00775 break;
00776 case INTER_DATA:
00777 if (!curr_cell.tree) {
00778 unsigned mv_idx;
00779
00780 if (!ctx->need_resync)
00781 ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
00782 mv_idx = *(ctx->next_cell_data++);
00783 if (mv_idx >= ctx->num_vectors) {
00784 av_log(avctx, AV_LOG_ERROR, "motion vector index out of range\n");
00785 return AVERROR_INVALIDDATA;
00786 }
00787 curr_cell.mv_ptr = &ctx->mc_vectors[mv_idx << 1];
00788 curr_cell.tree = 1;
00789 UPDATE_BITPOS(8);
00790 } else {
00791 if (!ctx->need_resync)
00792 ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
00793
00794 CHECK_CELL
00795 bytes_used = decode_cell(ctx, avctx, plane, &curr_cell,
00796 ctx->next_cell_data, ctx->last_byte);
00797 if (bytes_used < 0)
00798 return AVERROR_INVALIDDATA;
00799
00800 UPDATE_BITPOS(bytes_used << 3);
00801 ctx->next_cell_data += bytes_used;
00802 return 0;
00803 }
00804 break;
00805 }
00806 }
00807
00808 return AVERROR_INVALIDDATA;
00809 }
00810
00811
00812 static int decode_plane(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00813 Plane *plane, const uint8_t *data, int32_t data_size,
00814 int32_t strip_width)
00815 {
00816 Cell curr_cell;
00817 unsigned num_vectors;
00818
00819
00820
00821 num_vectors = bytestream_get_le32(&data);
00822 if (num_vectors > 256) {
00823 av_log(ctx->avctx, AV_LOG_ERROR,
00824 "Read invalid number of motion vectors %d\n", num_vectors);
00825 return AVERROR_INVALIDDATA;
00826 }
00827 if (num_vectors * 2 >= data_size)
00828 return AVERROR_INVALIDDATA;
00829
00830 ctx->num_vectors = num_vectors;
00831 ctx->mc_vectors = num_vectors ? data : 0;
00832
00833
00834 init_get_bits(&ctx->gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
00835 ctx->skip_bits = 0;
00836 ctx->need_resync = 0;
00837
00838 ctx->last_byte = data + data_size - 1;
00839
00840
00841 curr_cell.xpos = curr_cell.ypos = 0;
00842 curr_cell.width = plane->width >> 2;
00843 curr_cell.height = plane->height >> 2;
00844 curr_cell.tree = 0;
00845 curr_cell.mv_ptr = 0;
00846
00847 return parse_bintree(ctx, avctx, plane, INTRA_NULL, &curr_cell, CELL_STACK_MAX, strip_width);
00848 }
00849
00850
00851 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H')
00852
00853 static int decode_frame_headers(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00854 const uint8_t *buf, int buf_size)
00855 {
00856 const uint8_t *buf_ptr = buf, *bs_hdr;
00857 uint32_t frame_num, word2, check_sum, data_size;
00858 uint32_t y_offset, u_offset, v_offset, starts[3], ends[3];
00859 uint16_t height, width;
00860 int i, j;
00861
00862
00863 frame_num = bytestream_get_le32(&buf_ptr);
00864 word2 = bytestream_get_le32(&buf_ptr);
00865 check_sum = bytestream_get_le32(&buf_ptr);
00866 data_size = bytestream_get_le32(&buf_ptr);
00867
00868 if ((frame_num ^ word2 ^ data_size ^ OS_HDR_ID) != check_sum) {
00869 av_log(avctx, AV_LOG_ERROR, "OS header checksum mismatch!\n");
00870 return AVERROR_INVALIDDATA;
00871 }
00872
00873
00874 bs_hdr = buf_ptr;
00875
00876 if (bytestream_get_le16(&buf_ptr) != 32) {
00877 av_log(avctx, AV_LOG_ERROR, "Unsupported codec version!\n");
00878 return AVERROR_INVALIDDATA;
00879 }
00880
00881 ctx->frame_num = frame_num;
00882 ctx->frame_flags = bytestream_get_le16(&buf_ptr);
00883 ctx->data_size = (bytestream_get_le32(&buf_ptr) + 7) >> 3;
00884 ctx->cb_offset = *buf_ptr++;
00885
00886 if (ctx->data_size == 16)
00887 return 4;
00888 if (ctx->data_size > buf_size)
00889 ctx->data_size = buf_size;
00890
00891 buf_ptr += 3;
00892
00893
00894 height = bytestream_get_le16(&buf_ptr);
00895 width = bytestream_get_le16(&buf_ptr);
00896 if (av_image_check_size(width, height, 0, avctx))
00897 return AVERROR_INVALIDDATA;
00898
00899 if (width != ctx->width || height != ctx->height) {
00900 int res;
00901
00902 av_dlog(avctx, "Frame dimensions changed!\n");
00903
00904 if (width < 16 || width > 640 ||
00905 height < 16 || height > 480 ||
00906 width & 3 || height & 3) {
00907 av_log(avctx, AV_LOG_ERROR,
00908 "Invalid picture dimensions: %d x %d!\n", width, height);
00909 return AVERROR_INVALIDDATA;
00910 }
00911
00912 ctx->width = width;
00913 ctx->height = height;
00914
00915 free_frame_buffers(ctx);
00916 if ((res = allocate_frame_buffers(ctx, avctx)) < 0)
00917 return res;
00918 avcodec_set_dimensions(avctx, width, height);
00919 }
00920
00921 y_offset = bytestream_get_le32(&buf_ptr);
00922 v_offset = bytestream_get_le32(&buf_ptr);
00923 u_offset = bytestream_get_le32(&buf_ptr);
00924
00925
00926
00927 starts[0] = y_offset;
00928 starts[1] = v_offset;
00929 starts[2] = u_offset;
00930
00931 for (j = 0; j < 3; j++) {
00932 ends[j] = ctx->data_size;
00933 for (i = 2; i >= 0; i--)
00934 if (starts[i] < ends[j] && starts[i] > starts[j])
00935 ends[j] = starts[i];
00936 }
00937
00938 ctx->y_data_size = ends[0] - starts[0];
00939 ctx->v_data_size = ends[1] - starts[1];
00940 ctx->u_data_size = ends[2] - starts[2];
00941 if (FFMAX3(y_offset, v_offset, u_offset) >= ctx->data_size - 16 ||
00942 FFMIN3(ctx->y_data_size, ctx->v_data_size, ctx->u_data_size) <= 0) {
00943 av_log(avctx, AV_LOG_ERROR, "One of the y/u/v offsets is invalid\n");
00944 return AVERROR_INVALIDDATA;
00945 }
00946
00947 ctx->y_data_ptr = bs_hdr + y_offset;
00948 ctx->v_data_ptr = bs_hdr + v_offset;
00949 ctx->u_data_ptr = bs_hdr + u_offset;
00950 ctx->alt_quant = buf_ptr + sizeof(uint32_t);
00951
00952 if (ctx->data_size == 16) {
00953 av_log(avctx, AV_LOG_DEBUG, "Sync frame encountered!\n");
00954 return 16;
00955 }
00956
00957 if (ctx->frame_flags & BS_8BIT_PEL) {
00958 av_log_ask_for_sample(avctx, "8-bit pixel format\n");
00959 return AVERROR_PATCHWELCOME;
00960 }
00961
00962 if (ctx->frame_flags & BS_MV_X_HALF || ctx->frame_flags & BS_MV_Y_HALF) {
00963 av_log_ask_for_sample(avctx, "halfpel motion vectors\n");
00964 return AVERROR_PATCHWELCOME;
00965 }
00966
00967 return 0;
00968 }
00969
00970
00980 static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, int dst_pitch)
00981 {
00982 int x,y;
00983 const uint8_t *src = plane->pixels[buf_sel];
00984 uint32_t pitch = plane->pitch;
00985
00986 for (y = 0; y < plane->height; y++) {
00987
00988 for (x = 0; x < plane->width >> 2; x++) {
00989 AV_WN32A(dst, (AV_RN32A(src) & 0x7F7F7F7F) << 1);
00990 src += 4;
00991 dst += 4;
00992 }
00993
00994 for (x <<= 2; x < plane->width; x++)
00995 *dst++ = *src++ << 1;
00996
00997 src += pitch - plane->width;
00998 dst += dst_pitch - plane->width;
00999 }
01000 }
01001
01002
01003 static av_cold int decode_init(AVCodecContext *avctx)
01004 {
01005 Indeo3DecodeContext *ctx = avctx->priv_data;
01006
01007 ctx->avctx = avctx;
01008 ctx->width = avctx->width;
01009 ctx->height = avctx->height;
01010 avctx->pix_fmt = PIX_FMT_YUV410P;
01011 avcodec_get_frame_defaults(&ctx->frame);
01012
01013 build_requant_tab();
01014
01015 dsputil_init(&ctx->dsp, avctx);
01016
01017 return allocate_frame_buffers(ctx, avctx);
01018 }
01019
01020
01021 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
01022 AVPacket *avpkt)
01023 {
01024 Indeo3DecodeContext *ctx = avctx->priv_data;
01025 const uint8_t *buf = avpkt->data;
01026 int buf_size = avpkt->size;
01027 int res;
01028
01029 res = decode_frame_headers(ctx, avctx, buf, buf_size);
01030 if (res < 0)
01031 return res;
01032
01033
01034 if (res) {
01035
01036 *data_size = 0;
01037 return buf_size;
01038 }
01039
01040
01041 if (ctx->frame_flags & BS_NONREF &&
01042 (avctx->skip_frame >= AVDISCARD_NONREF))
01043 return 0;
01044
01045
01046 if (!(ctx->frame_flags & BS_KEYFRAME) && avctx->skip_frame >= AVDISCARD_NONKEY)
01047 return 0;
01048
01049
01050 ctx->buf_sel = (ctx->frame_flags >> BS_BUFFER) & 1;
01051
01052
01053 if ((res = decode_plane(ctx, avctx, ctx->planes, ctx->y_data_ptr, ctx->y_data_size, 40)))
01054 return res;
01055
01056
01057 if ((res = decode_plane(ctx, avctx, &ctx->planes[1], ctx->u_data_ptr, ctx->u_data_size, 10)))
01058 return res;
01059
01060 if ((res = decode_plane(ctx, avctx, &ctx->planes[2], ctx->v_data_ptr, ctx->v_data_size, 10)))
01061 return res;
01062
01063 if (ctx->frame.data[0])
01064 avctx->release_buffer(avctx, &ctx->frame);
01065
01066 ctx->frame.reference = 0;
01067 if ((res = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
01068 av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01069 return res;
01070 }
01071
01072 output_plane(&ctx->planes[0], ctx->buf_sel, ctx->frame.data[0], ctx->frame.linesize[0]);
01073 output_plane(&ctx->planes[1], ctx->buf_sel, ctx->frame.data[1], ctx->frame.linesize[1]);
01074 output_plane(&ctx->planes[2], ctx->buf_sel, ctx->frame.data[2], ctx->frame.linesize[2]);
01075
01076 *data_size = sizeof(AVFrame);
01077 *(AVFrame*)data = ctx->frame;
01078
01079 return buf_size;
01080 }
01081
01082
01083 static av_cold int decode_close(AVCodecContext *avctx)
01084 {
01085 Indeo3DecodeContext *ctx = avctx->priv_data;
01086
01087 free_frame_buffers(avctx->priv_data);
01088
01089 if (ctx->frame.data[0])
01090 avctx->release_buffer(avctx, &ctx->frame);
01091
01092 return 0;
01093 }
01094
01095 AVCodec ff_indeo3_decoder = {
01096 .name = "indeo3",
01097 .type = AVMEDIA_TYPE_VIDEO,
01098 .id = CODEC_ID_INDEO3,
01099 .priv_data_size = sizeof(Indeo3DecodeContext),
01100 .init = decode_init,
01101 .close = decode_close,
01102 .decode = decode_frame,
01103 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
01104 };