00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035
00036 #include "common.h"
00037 #include "avcodec.h"
00038 #include "dsputil.h"
00039 #include "mpegvideo.h"
00040
00041 #include "vp3data.h"
00042
00043 #define FRAGMENT_PIXELS 8
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #define KEYFRAMES_ONLY 0
00065
00066 #define DEBUG_VP3 0
00067 #define DEBUG_INIT 0
00068 #define DEBUG_DEQUANTIZERS 0
00069 #define DEBUG_BLOCK_CODING 0
00070 #define DEBUG_MODES 0
00071 #define DEBUG_VECTORS 0
00072 #define DEBUG_TOKEN 0
00073 #define DEBUG_VLC 0
00074 #define DEBUG_DC_PRED 0
00075 #define DEBUG_IDCT 0
00076
00077 #if DEBUG_VP3
00078 #define debug_vp3 printf
00079 #else
00080 static inline void debug_vp3(const char *format, ...) { }
00081 #endif
00082
00083 #if DEBUG_INIT
00084 #define debug_init printf
00085 #else
00086 static inline void debug_init(const char *format, ...) { }
00087 #endif
00088
00089 #if DEBUG_DEQUANTIZERS
00090 #define debug_dequantizers printf
00091 #else
00092 static inline void debug_dequantizers(const char *format, ...) { }
00093 #endif
00094
00095 #if DEBUG_BLOCK_CODING
00096 #define debug_block_coding printf
00097 #else
00098 static inline void debug_block_coding(const char *format, ...) { }
00099 #endif
00100
00101 #if DEBUG_MODES
00102 #define debug_modes printf
00103 #else
00104 static inline void debug_modes(const char *format, ...) { }
00105 #endif
00106
00107 #if DEBUG_VECTORS
00108 #define debug_vectors printf
00109 #else
00110 static inline void debug_vectors(const char *format, ...) { }
00111 #endif
00112
00113 #if DEBUG_TOKEN
00114 #define debug_token printf
00115 #else
00116 static inline void debug_token(const char *format, ...) { }
00117 #endif
00118
00119 #if DEBUG_VLC
00120 #define debug_vlc printf
00121 #else
00122 static inline void debug_vlc(const char *format, ...) { }
00123 #endif
00124
00125 #if DEBUG_DC_PRED
00126 #define debug_dc_pred printf
00127 #else
00128 static inline void debug_dc_pred(const char *format, ...) { }
00129 #endif
00130
00131 #if DEBUG_IDCT
00132 #define debug_idct printf
00133 #else
00134 static inline void debug_idct(const char *format, ...) { }
00135 #endif
00136
00137 typedef struct Coeff {
00138 struct Coeff *next;
00139 DCTELEM coeff;
00140 uint8_t index;
00141 } Coeff;
00142
00143
00144 typedef struct Vp3Fragment {
00145 Coeff *next_coeff;
00146
00147
00148 int first_pixel;
00149
00150 uint16_t macroblock;
00151 uint8_t coding_method;
00152 uint8_t coeff_count;
00153 int8_t motion_x;
00154 int8_t motion_y;
00155 } Vp3Fragment;
00156
00157 #define SB_NOT_CODED 0
00158 #define SB_PARTIALLY_CODED 1
00159 #define SB_FULLY_CODED 2
00160
00161 #define MODE_INTER_NO_MV 0
00162 #define MODE_INTRA 1
00163 #define MODE_INTER_PLUS_MV 2
00164 #define MODE_INTER_LAST_MV 3
00165 #define MODE_INTER_PRIOR_LAST 4
00166 #define MODE_USING_GOLDEN 5
00167 #define MODE_GOLDEN_MV 6
00168 #define MODE_INTER_FOURMV 7
00169 #define CODING_MODE_COUNT 8
00170
00171
00172 #define MODE_COPY 8
00173
00174
00175 static int ModeAlphabet[7][CODING_MODE_COUNT] =
00176 {
00177
00178 { 0, 0, 0, 0, 0, 0, 0, 0 },
00179
00180
00181 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00182 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
00183 MODE_INTRA, MODE_USING_GOLDEN,
00184 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00185
00186
00187 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00188 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
00189 MODE_INTRA, MODE_USING_GOLDEN,
00190 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00191
00192
00193 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00194 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
00195 MODE_INTRA, MODE_USING_GOLDEN,
00196 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00197
00198
00199 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00200 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
00201 MODE_INTRA, MODE_USING_GOLDEN,
00202 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00203
00204
00205 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
00206 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
00207 MODE_INTRA, MODE_USING_GOLDEN,
00208 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00209
00210
00211 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
00212 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00213 MODE_INTER_PLUS_MV, MODE_INTRA,
00214 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00215
00216 };
00217
00218 #define MIN_DEQUANT_VAL 2
00219
00220 typedef struct Vp3DecodeContext {
00221 AVCodecContext *avctx;
00222 int theora, theora_tables;
00223 int version;
00224 int width, height;
00225 AVFrame golden_frame;
00226 AVFrame last_frame;
00227 AVFrame current_frame;
00228 int keyframe;
00229 DSPContext dsp;
00230 int flipped_image;
00231
00232 int quality_index;
00233 int last_quality_index;
00234
00235 int superblock_count;
00236 int superblock_width;
00237 int superblock_height;
00238 int y_superblock_width;
00239 int y_superblock_height;
00240 int c_superblock_width;
00241 int c_superblock_height;
00242 int u_superblock_start;
00243 int v_superblock_start;
00244 unsigned char *superblock_coding;
00245
00246 int macroblock_count;
00247 int macroblock_width;
00248 int macroblock_height;
00249
00250 int fragment_count;
00251 int fragment_width;
00252 int fragment_height;
00253
00254 Vp3Fragment *all_fragments;
00255 Coeff *coeffs;
00256 Coeff *next_coeff;
00257 int u_fragment_start;
00258 int v_fragment_start;
00259
00260 ScanTable scantable;
00261
00262
00263 uint16_t coded_dc_scale_factor[64];
00264 uint32_t coded_ac_scale_factor[64];
00265 uint16_t coded_intra_y_dequant[64];
00266 uint16_t coded_intra_c_dequant[64];
00267 uint16_t coded_inter_dequant[64];
00268
00269
00270
00271 int *coded_fragment_list;
00272 int coded_fragment_list_index;
00273 int pixel_addresses_inited;
00274
00275 VLC dc_vlc[16];
00276 VLC ac_vlc_1[16];
00277 VLC ac_vlc_2[16];
00278 VLC ac_vlc_3[16];
00279 VLC ac_vlc_4[16];
00280
00281 VLC superblock_run_length_vlc;
00282 VLC fragment_run_length_vlc;
00283 VLC mode_code_vlc;
00284 VLC motion_vector_vlc;
00285
00286
00287
00288 int16_t __align16 intra_y_dequant[64];
00289 int16_t __align16 intra_c_dequant[64];
00290 int16_t __align16 inter_dequant[64];
00291
00292
00293
00294
00295
00296 int *superblock_fragments;
00297
00298
00299
00300
00301
00302 int *superblock_macroblocks;
00303
00304
00305
00306
00307 int *macroblock_fragments;
00308
00309
00310 unsigned char *macroblock_coding;
00311
00312 int first_coded_y_fragment;
00313 int first_coded_c_fragment;
00314 int last_coded_y_fragment;
00315 int last_coded_c_fragment;
00316
00317 uint8_t edge_emu_buffer[9*2048];
00318 uint8_t qscale_table[2048];
00319
00320
00321 int hti;
00322 unsigned int hbits;
00323 int entries;
00324 int huff_code_size;
00325 uint16_t huffman_table[80][32][2];
00326
00327 uint32_t filter_limit_values[64];
00328 int bounding_values_array[256];
00329 } Vp3DecodeContext;
00330
00331 static int theora_decode_comments(AVCodecContext *avctx, GetBitContext gb);
00332 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb);
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 static int init_block_mapping(Vp3DecodeContext *s)
00346 {
00347 int i, j;
00348 signed int hilbert_walk_y[16];
00349 signed int hilbert_walk_c[16];
00350 signed int hilbert_walk_mb[4];
00351
00352 int current_fragment = 0;
00353 int current_width = 0;
00354 int current_height = 0;
00355 int right_edge = 0;
00356 int bottom_edge = 0;
00357 int superblock_row_inc = 0;
00358 int *hilbert = NULL;
00359 int mapping_index = 0;
00360
00361 int current_macroblock;
00362 int c_fragment;
00363
00364 signed char travel_width[16] = {
00365 1, 1, 0, -1,
00366 0, 0, 1, 0,
00367 1, 0, 1, 0,
00368 0, -1, 0, 1
00369 };
00370
00371 signed char travel_height[16] = {
00372 0, 0, 1, 0,
00373 1, 1, 0, -1,
00374 0, 1, 0, -1,
00375 -1, 0, -1, 0
00376 };
00377
00378 signed char travel_width_mb[4] = {
00379 1, 0, 1, 0
00380 };
00381
00382 signed char travel_height_mb[4] = {
00383 0, 1, 0, -1
00384 };
00385
00386 debug_vp3(" vp3: initialize block mapping tables\n");
00387
00388
00389 hilbert_walk_y[0] = 1;
00390 hilbert_walk_y[1] = 1;
00391 hilbert_walk_y[2] = s->fragment_width;
00392 hilbert_walk_y[3] = -1;
00393 hilbert_walk_y[4] = s->fragment_width;
00394 hilbert_walk_y[5] = s->fragment_width;
00395 hilbert_walk_y[6] = 1;
00396 hilbert_walk_y[7] = -s->fragment_width;
00397 hilbert_walk_y[8] = 1;
00398 hilbert_walk_y[9] = s->fragment_width;
00399 hilbert_walk_y[10] = 1;
00400 hilbert_walk_y[11] = -s->fragment_width;
00401 hilbert_walk_y[12] = -s->fragment_width;
00402 hilbert_walk_y[13] = -1;
00403 hilbert_walk_y[14] = -s->fragment_width;
00404 hilbert_walk_y[15] = 1;
00405
00406 hilbert_walk_c[0] = 1;
00407 hilbert_walk_c[1] = 1;
00408 hilbert_walk_c[2] = s->fragment_width / 2;
00409 hilbert_walk_c[3] = -1;
00410 hilbert_walk_c[4] = s->fragment_width / 2;
00411 hilbert_walk_c[5] = s->fragment_width / 2;
00412 hilbert_walk_c[6] = 1;
00413 hilbert_walk_c[7] = -s->fragment_width / 2;
00414 hilbert_walk_c[8] = 1;
00415 hilbert_walk_c[9] = s->fragment_width / 2;
00416 hilbert_walk_c[10] = 1;
00417 hilbert_walk_c[11] = -s->fragment_width / 2;
00418 hilbert_walk_c[12] = -s->fragment_width / 2;
00419 hilbert_walk_c[13] = -1;
00420 hilbert_walk_c[14] = -s->fragment_width / 2;
00421 hilbert_walk_c[15] = 1;
00422
00423 hilbert_walk_mb[0] = 1;
00424 hilbert_walk_mb[1] = s->macroblock_width;
00425 hilbert_walk_mb[2] = 1;
00426 hilbert_walk_mb[3] = -s->macroblock_width;
00427
00428
00429 for (i = 0; i < s->superblock_count; i++) {
00430 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
00431 i, s->u_superblock_start, s->v_superblock_start);
00432
00433
00434 if (i == 0) {
00435
00436
00437 right_edge = s->fragment_width;
00438 bottom_edge = s->fragment_height;
00439 current_width = -1;
00440 current_height = 0;
00441 superblock_row_inc = 3 * s->fragment_width -
00442 (s->y_superblock_width * 4 - s->fragment_width);
00443 hilbert = hilbert_walk_y;
00444
00445
00446 current_fragment = -1;
00447
00448 } else if (i == s->u_superblock_start) {
00449
00450
00451 right_edge = s->fragment_width / 2;
00452 bottom_edge = s->fragment_height / 2;
00453 current_width = -1;
00454 current_height = 0;
00455 superblock_row_inc = 3 * (s->fragment_width / 2) -
00456 (s->c_superblock_width * 4 - s->fragment_width / 2);
00457 hilbert = hilbert_walk_c;
00458
00459
00460 current_fragment = s->u_fragment_start - 1;
00461
00462 } else if (i == s->v_superblock_start) {
00463
00464
00465 right_edge = s->fragment_width / 2;
00466 bottom_edge = s->fragment_height / 2;
00467 current_width = -1;
00468 current_height = 0;
00469 superblock_row_inc = 3 * (s->fragment_width / 2) -
00470 (s->c_superblock_width * 4 - s->fragment_width / 2);
00471 hilbert = hilbert_walk_c;
00472
00473
00474 current_fragment = s->v_fragment_start - 1;
00475
00476 }
00477
00478 if (current_width >= right_edge - 1) {
00479
00480 current_width = -1;
00481 current_height += 4;
00482
00483
00484 current_fragment += superblock_row_inc;
00485 }
00486
00487
00488 for (j = 0; j < 16; j++) {
00489 current_fragment += hilbert[j];
00490 current_width += travel_width[j];
00491 current_height += travel_height[j];
00492
00493
00494 if ((current_width < right_edge) &&
00495 (current_height < bottom_edge)) {
00496 s->superblock_fragments[mapping_index] = current_fragment;
00497 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
00498 s->superblock_fragments[mapping_index], i, j,
00499 current_width, right_edge, current_height, bottom_edge);
00500 } else {
00501 s->superblock_fragments[mapping_index] = -1;
00502 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
00503 i, j,
00504 current_width, right_edge, current_height, bottom_edge);
00505 }
00506
00507 mapping_index++;
00508 }
00509 }
00510
00511
00512
00513 right_edge = s->macroblock_width;
00514 bottom_edge = s->macroblock_height;
00515 current_width = -1;
00516 current_height = 0;
00517 superblock_row_inc = s->macroblock_width -
00518 (s->y_superblock_width * 2 - s->macroblock_width);;
00519 hilbert = hilbert_walk_mb;
00520 mapping_index = 0;
00521 current_macroblock = -1;
00522 for (i = 0; i < s->u_superblock_start; i++) {
00523
00524 if (current_width >= right_edge - 1) {
00525
00526 current_width = -1;
00527 current_height += 2;
00528
00529
00530 current_macroblock += superblock_row_inc;
00531 }
00532
00533
00534 for (j = 0; j < 4; j++) {
00535 current_macroblock += hilbert_walk_mb[j];
00536 current_width += travel_width_mb[j];
00537 current_height += travel_height_mb[j];
00538
00539
00540 if ((current_width < right_edge) &&
00541 (current_height < bottom_edge)) {
00542 s->superblock_macroblocks[mapping_index] = current_macroblock;
00543 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
00544 s->superblock_macroblocks[mapping_index], i, j,
00545 current_width, right_edge, current_height, bottom_edge);
00546 } else {
00547 s->superblock_macroblocks[mapping_index] = -1;
00548 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
00549 i, j,
00550 current_width, right_edge, current_height, bottom_edge);
00551 }
00552
00553 mapping_index++;
00554 }
00555 }
00556
00557
00558 current_fragment = 0;
00559 current_macroblock = 0;
00560 mapping_index = 0;
00561 for (i = 0; i < s->fragment_height; i += 2) {
00562
00563 for (j = 0; j < s->fragment_width; j += 2) {
00564
00565 debug_init(" macroblock %d contains fragments: ", current_macroblock);
00566 s->all_fragments[current_fragment].macroblock = current_macroblock;
00567 s->macroblock_fragments[mapping_index++] = current_fragment;
00568 debug_init("%d ", current_fragment);
00569
00570 if (j + 1 < s->fragment_width) {
00571 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
00572 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
00573 debug_init("%d ", current_fragment + 1);
00574 } else
00575 s->macroblock_fragments[mapping_index++] = -1;
00576
00577 if (i + 1 < s->fragment_height) {
00578 s->all_fragments[current_fragment + s->fragment_width].macroblock =
00579 current_macroblock;
00580 s->macroblock_fragments[mapping_index++] =
00581 current_fragment + s->fragment_width;
00582 debug_init("%d ", current_fragment + s->fragment_width);
00583 } else
00584 s->macroblock_fragments[mapping_index++] = -1;
00585
00586 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
00587 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
00588 current_macroblock;
00589 s->macroblock_fragments[mapping_index++] =
00590 current_fragment + s->fragment_width + 1;
00591 debug_init("%d ", current_fragment + s->fragment_width + 1);
00592 } else
00593 s->macroblock_fragments[mapping_index++] = -1;
00594
00595
00596 c_fragment = s->u_fragment_start +
00597 (i * s->fragment_width / 4) + (j / 2);
00598 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
00599 s->macroblock_fragments[mapping_index++] = c_fragment;
00600 debug_init("%d ", c_fragment);
00601
00602 c_fragment = s->v_fragment_start +
00603 (i * s->fragment_width / 4) + (j / 2);
00604 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
00605 s->macroblock_fragments[mapping_index++] = c_fragment;
00606 debug_init("%d ", c_fragment);
00607
00608 debug_init("\n");
00609
00610 if (j + 2 <= s->fragment_width)
00611 current_fragment += 2;
00612 else
00613 current_fragment++;
00614 current_macroblock++;
00615 }
00616
00617 current_fragment += s->fragment_width;
00618 }
00619
00620 return 0;
00621 }
00622
00623
00624
00625
00626
00627
00628
00629 static void unpack_token(GetBitContext *gb, int token, int *zero_run,
00630 DCTELEM *coeff, int *eob_run)
00631 {
00632 int sign;
00633
00634 *zero_run = 0;
00635 *eob_run = 0;
00636 *coeff = 0;
00637
00638 debug_token(" vp3 token %d: ", token);
00639 switch (token) {
00640
00641 case 0:
00642 debug_token("DCT_EOB_TOKEN, EOB next block\n");
00643 *eob_run = 1;
00644 break;
00645
00646 case 1:
00647 debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
00648 *eob_run = 2;
00649 break;
00650
00651 case 2:
00652 debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
00653 *eob_run = 3;
00654 break;
00655
00656 case 3:
00657 debug_token("DCT_REPEAT_RUN_TOKEN, ");
00658 *eob_run = get_bits(gb, 2) + 4;
00659 debug_token("EOB the next %d blocks\n", *eob_run);
00660 break;
00661
00662 case 4:
00663 debug_token("DCT_REPEAT_RUN2_TOKEN, ");
00664 *eob_run = get_bits(gb, 3) + 8;
00665 debug_token("EOB the next %d blocks\n", *eob_run);
00666 break;
00667
00668 case 5:
00669 debug_token("DCT_REPEAT_RUN3_TOKEN, ");
00670 *eob_run = get_bits(gb, 4) + 16;
00671 debug_token("EOB the next %d blocks\n", *eob_run);
00672 break;
00673
00674 case 6:
00675 debug_token("DCT_REPEAT_RUN4_TOKEN, ");
00676 *eob_run = get_bits(gb, 12);
00677 debug_token("EOB the next %d blocks\n", *eob_run);
00678 break;
00679
00680 case 7:
00681 debug_token("DCT_SHORT_ZRL_TOKEN, ");
00682
00683
00684
00685 *zero_run = get_bits(gb, 3);
00686 *coeff = 0;
00687 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
00688 break;
00689
00690 case 8:
00691 debug_token("DCT_ZRL_TOKEN, ");
00692
00693
00694
00695 *zero_run = get_bits(gb, 6);
00696 *coeff = 0;
00697 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
00698 break;
00699
00700 case 9:
00701 debug_token("ONE_TOKEN, output 1\n");
00702 *coeff = 1;
00703 break;
00704
00705 case 10:
00706 debug_token("MINUS_ONE_TOKEN, output -1\n");
00707 *coeff = -1;
00708 break;
00709
00710 case 11:
00711 debug_token("TWO_TOKEN, output 2\n");
00712 *coeff = 2;
00713 break;
00714
00715 case 12:
00716 debug_token("MINUS_TWO_TOKEN, output -2\n");
00717 *coeff = -2;
00718 break;
00719
00720 case 13:
00721 case 14:
00722 case 15:
00723 case 16:
00724 debug_token("LOW_VAL_TOKENS, ");
00725 if (get_bits(gb, 1))
00726 *coeff = -(3 + (token - 13));
00727 else
00728 *coeff = 3 + (token - 13);
00729 debug_token("output %d\n", *coeff);
00730 break;
00731
00732 case 17:
00733 debug_token("DCT_VAL_CATEGORY3, ");
00734 sign = get_bits(gb, 1);
00735 *coeff = 7 + get_bits(gb, 1);
00736 if (sign)
00737 *coeff = -(*coeff);
00738 debug_token("output %d\n", *coeff);
00739 break;
00740
00741 case 18:
00742 debug_token("DCT_VAL_CATEGORY4, ");
00743 sign = get_bits(gb, 1);
00744 *coeff = 9 + get_bits(gb, 2);
00745 if (sign)
00746 *coeff = -(*coeff);
00747 debug_token("output %d\n", *coeff);
00748 break;
00749
00750 case 19:
00751 debug_token("DCT_VAL_CATEGORY5, ");
00752 sign = get_bits(gb, 1);
00753 *coeff = 13 + get_bits(gb, 3);
00754 if (sign)
00755 *coeff = -(*coeff);
00756 debug_token("output %d\n", *coeff);
00757 break;
00758
00759 case 20:
00760 debug_token("DCT_VAL_CATEGORY6, ");
00761 sign = get_bits(gb, 1);
00762 *coeff = 21 + get_bits(gb, 4);
00763 if (sign)
00764 *coeff = -(*coeff);
00765 debug_token("output %d\n", *coeff);
00766 break;
00767
00768 case 21:
00769 debug_token("DCT_VAL_CATEGORY7, ");
00770 sign = get_bits(gb, 1);
00771 *coeff = 37 + get_bits(gb, 5);
00772 if (sign)
00773 *coeff = -(*coeff);
00774 debug_token("output %d\n", *coeff);
00775 break;
00776
00777 case 22:
00778 debug_token("DCT_VAL_CATEGORY8, ");
00779 sign = get_bits(gb, 1);
00780 *coeff = 69 + get_bits(gb, 9);
00781 if (sign)
00782 *coeff = -(*coeff);
00783 debug_token("output %d\n", *coeff);
00784 break;
00785
00786 case 23:
00787 case 24:
00788 case 25:
00789 case 26:
00790 case 27:
00791 debug_token("DCT_RUN_CATEGORY1, ");
00792 *zero_run = token - 22;
00793 if (get_bits(gb, 1))
00794 *coeff = -1;
00795 else
00796 *coeff = 1;
00797 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
00798 break;
00799
00800 case 28:
00801 debug_token("DCT_RUN_CATEGORY1B, ");
00802 if (get_bits(gb, 1))
00803 *coeff = -1;
00804 else
00805 *coeff = 1;
00806 *zero_run = 6 + get_bits(gb, 2);
00807 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
00808 break;
00809
00810 case 29:
00811 debug_token("DCT_RUN_CATEGORY1C, ");
00812 if (get_bits(gb, 1))
00813 *coeff = -1;
00814 else
00815 *coeff = 1;
00816 *zero_run = 10 + get_bits(gb, 3);
00817 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
00818 break;
00819
00820 case 30:
00821 debug_token("DCT_RUN_CATEGORY2, ");
00822 sign = get_bits(gb, 1);
00823 *coeff = 2 + get_bits(gb, 1);
00824 if (sign)
00825 *coeff = -(*coeff);
00826 *zero_run = 1;
00827 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
00828 break;
00829
00830 case 31:
00831 debug_token("DCT_RUN_CATEGORY2, ");
00832 sign = get_bits(gb, 1);
00833 *coeff = 2 + get_bits(gb, 1);
00834 if (sign)
00835 *coeff = -(*coeff);
00836 *zero_run = 2 + get_bits(gb, 1);
00837 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
00838 break;
00839
00840 default:
00841 av_log(NULL, AV_LOG_ERROR, " vp3: help! Got a bad token: %d > 31\n", token);
00842 break;
00843
00844 }
00845 }
00846
00847
00848
00849
00850 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
00851 {
00852 int i;
00853
00854
00855 s->coded_fragment_list_index = 0;
00856 for (i = 0; i < s->fragment_count; i++) {
00857 s->all_fragments[i].coeff_count = 0;
00858 s->all_fragments[i].motion_x = 127;
00859 s->all_fragments[i].motion_y = 127;
00860 s->all_fragments[i].next_coeff= NULL;
00861 s->coeffs[i].index=
00862 s->coeffs[i].coeff=0;
00863 s->coeffs[i].next= NULL;
00864 }
00865 }
00866
00867
00868
00869
00870
00871 static void init_dequantizer(Vp3DecodeContext *s)
00872 {
00873
00874 int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index];
00875 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
00876 int i, j;
00877
00878 debug_vp3(" vp3: initializing dequantization tables\n");
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892 #define SCALER 4
00893
00894
00895 s->intra_y_dequant[0] = s->coded_intra_y_dequant[0] * dc_scale_factor / 100;
00896 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
00897 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
00898 s->intra_y_dequant[0] *= SCALER;
00899
00900 s->intra_c_dequant[0] = s->coded_intra_c_dequant[0] * dc_scale_factor / 100;
00901 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
00902 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
00903 s->intra_c_dequant[0] *= SCALER;
00904
00905 s->inter_dequant[0] = s->coded_inter_dequant[0] * dc_scale_factor / 100;
00906 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
00907 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
00908 s->inter_dequant[0] *= SCALER;
00909
00910
00911
00912 for (i = 1; i < 64; i++) {
00913 int k= s->scantable.scantable[i];
00914 j = s->scantable.permutated[i];
00915
00916 s->intra_y_dequant[j] = s->coded_intra_y_dequant[k] * ac_scale_factor / 100;
00917 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
00918 s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
00919 s->intra_y_dequant[j] *= SCALER;
00920
00921 s->intra_c_dequant[j] = s->coded_intra_c_dequant[k] * ac_scale_factor / 100;
00922 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
00923 s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
00924 s->intra_c_dequant[j] *= SCALER;
00925
00926 s->inter_dequant[j] = s->coded_inter_dequant[k] * ac_scale_factor / 100;
00927 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
00928 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
00929 s->inter_dequant[j] *= SCALER;
00930 }
00931
00932 memset(s->qscale_table, (FFMAX(s->intra_y_dequant[1], s->intra_c_dequant[1])+8)/16, 512);
00933
00934
00935 debug_dequantizers("intra Y dequantizers:\n");
00936 for (i = 0; i < 8; i++) {
00937 for (j = i * 8; j < i * 8 + 8; j++) {
00938 debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
00939 }
00940 debug_dequantizers("\n");
00941 }
00942 debug_dequantizers("\n");
00943
00944 debug_dequantizers("intra C dequantizers:\n");
00945 for (i = 0; i < 8; i++) {
00946 for (j = i * 8; j < i * 8 + 8; j++) {
00947 debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
00948 }
00949 debug_dequantizers("\n");
00950 }
00951 debug_dequantizers("\n");
00952
00953 debug_dequantizers("interframe dequantizers:\n");
00954 for (i = 0; i < 8; i++) {
00955 for (j = i * 8; j < i * 8 + 8; j++) {
00956 debug_dequantizers(" %4d,", s->inter_dequant[j]);
00957 }
00958 debug_dequantizers("\n");
00959 }
00960 debug_dequantizers("\n");
00961 }
00962
00963
00964
00965
00966
00967 static void init_loop_filter(Vp3DecodeContext *s)
00968 {
00969 int *bounding_values= s->bounding_values_array+127;
00970 int filter_limit;
00971 int x;
00972
00973 filter_limit = s->filter_limit_values[s->quality_index];
00974
00975
00976 memset(s->bounding_values_array, 0, 256 * sizeof(int));
00977 for (x = 0; x < filter_limit; x++) {
00978 bounding_values[-x - filter_limit] = -filter_limit + x;
00979 bounding_values[-x] = -x;
00980 bounding_values[x] = x;
00981 bounding_values[x + filter_limit] = filter_limit - x;
00982 }
00983 }
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998 static int get_superblock_run_length(GetBitContext *gb)
00999 {
01000
01001 if (get_bits(gb, 1) == 0)
01002 return 1;
01003
01004 else if (get_bits(gb, 1) == 0)
01005 return (2 + get_bits(gb, 1));
01006
01007 else if (get_bits(gb, 1) == 0)
01008 return (4 + get_bits(gb, 1));
01009
01010 else if (get_bits(gb, 1) == 0)
01011 return (6 + get_bits(gb, 2));
01012
01013 else if (get_bits(gb, 1) == 0)
01014 return (10 + get_bits(gb, 3));
01015
01016 else if (get_bits(gb, 1) == 0)
01017 return (18 + get_bits(gb, 4));
01018
01019 else
01020 return (34 + get_bits(gb, 12));
01021
01022 }
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036 static int get_fragment_run_length(GetBitContext *gb)
01037 {
01038
01039 if (get_bits(gb, 1) == 0)
01040 return (1 + get_bits(gb, 1));
01041
01042 else if (get_bits(gb, 1) == 0)
01043 return (3 + get_bits(gb, 1));
01044
01045 else if (get_bits(gb, 1) == 0)
01046 return (5 + get_bits(gb, 1));
01047
01048 else if (get_bits(gb, 1) == 0)
01049 return (7 + get_bits(gb, 2));
01050
01051 else if (get_bits(gb, 1) == 0)
01052 return (11 + get_bits(gb, 2));
01053
01054 else
01055 return (15 + get_bits(gb, 4));
01056
01057 }
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075 static int get_mode_code(GetBitContext *gb)
01076 {
01077
01078 if (get_bits(gb, 1) == 0)
01079 return 0;
01080
01081 else if (get_bits(gb, 1) == 0)
01082 return 1;
01083
01084 else if (get_bits(gb, 1) == 0)
01085 return 2;
01086
01087 else if (get_bits(gb, 1) == 0)
01088 return 3;
01089
01090 else if (get_bits(gb, 1) == 0)
01091 return 4;
01092
01093 else if (get_bits(gb, 1) == 0)
01094 return 5;
01095
01096 else if (get_bits(gb, 1) == 0)
01097 return 6;
01098
01099 else
01100 return 7;
01101
01102 }
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118 static int get_motion_vector_vlc(GetBitContext *gb)
01119 {
01120 int bits;
01121
01122 bits = get_bits(gb, 3);
01123
01124 switch(bits) {
01125
01126 case 0:
01127 bits = 0;
01128 break;
01129
01130 case 1:
01131 bits = 1;
01132 break;
01133
01134 case 2:
01135 bits = -1;
01136 break;
01137
01138 case 3:
01139 if (get_bits(gb, 1) == 0)
01140 bits = 2;
01141 else
01142 bits = -2;
01143 break;
01144
01145 case 4:
01146 if (get_bits(gb, 1) == 0)
01147 bits = 3;
01148 else
01149 bits = -3;
01150 break;
01151
01152 case 5:
01153 bits = 4 + get_bits(gb, 2);
01154 if (get_bits(gb, 1) == 1)
01155 bits = -bits;
01156 break;
01157
01158 case 6:
01159 bits = 8 + get_bits(gb, 3);
01160 if (get_bits(gb, 1) == 1)
01161 bits = -bits;
01162 break;
01163
01164 case 7:
01165 bits = 16 + get_bits(gb, 4);
01166 if (get_bits(gb, 1) == 1)
01167 bits = -bits;
01168 break;
01169
01170 }
01171
01172 return bits;
01173 }
01174
01175
01176
01177
01178
01179 static int get_motion_vector_fixed(GetBitContext *gb)
01180 {
01181
01182 int bits;
01183
01184 bits = get_bits(gb, 5);
01185
01186 if (get_bits(gb, 1) == 1)
01187 bits = -bits;
01188
01189 return bits;
01190 }
01191
01192
01193
01194
01195
01196 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
01197 {
01198 int bit = 0;
01199 int current_superblock = 0;
01200 int current_run = 0;
01201 int decode_fully_flags = 0;
01202 int decode_partial_blocks = 0;
01203 int first_c_fragment_seen;
01204
01205 int i, j;
01206 int current_fragment;
01207
01208 debug_vp3(" vp3: unpacking superblock coding\n");
01209
01210 if (s->keyframe) {
01211
01212 debug_vp3(" keyframe-- all superblocks are fully coded\n");
01213 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
01214
01215 } else {
01216
01217
01218 bit = get_bits(gb, 1);
01219
01220
01221 bit ^= 1;
01222 while (current_superblock < s->superblock_count) {
01223 if (current_run-- == 0) {
01224 bit ^= 1;
01225 #if 1
01226 current_run = get_vlc2(gb,
01227 s->superblock_run_length_vlc.table, 6, 2);
01228 if (current_run == 33)
01229 current_run += get_bits(gb, 12);
01230 #else
01231 current_run = get_superblock_run_length(gb);
01232 #endif
01233 debug_block_coding(" setting superblocks %d..%d to %s\n",
01234 current_superblock,
01235 current_superblock + current_run - 1,
01236 (bit) ? "partially coded" : "not coded");
01237
01238
01239
01240 if (bit == 0) {
01241 decode_fully_flags = 1;
01242 } else {
01243
01244
01245
01246 decode_partial_blocks = 1;
01247 }
01248 }
01249 s->superblock_coding[current_superblock++] = bit;
01250 }
01251
01252
01253
01254 if (decode_fully_flags) {
01255
01256 current_superblock = 0;
01257 current_run = 0;
01258 bit = get_bits(gb, 1);
01259
01260
01261 bit ^= 1;
01262 while (current_superblock < s->superblock_count) {
01263
01264
01265 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
01266
01267 if (current_run-- == 0) {
01268 bit ^= 1;
01269 #if 1
01270 current_run = get_vlc2(gb,
01271 s->superblock_run_length_vlc.table, 6, 2);
01272 if (current_run == 33)
01273 current_run += get_bits(gb, 12);
01274 #else
01275 current_run = get_superblock_run_length(gb);
01276 #endif
01277 }
01278
01279 debug_block_coding(" setting superblock %d to %s\n",
01280 current_superblock,
01281 (bit) ? "fully coded" : "not coded");
01282 s->superblock_coding[current_superblock] = 2*bit;
01283 }
01284 current_superblock++;
01285 }
01286 }
01287
01288
01289
01290 if (decode_partial_blocks) {
01291
01292 current_run = 0;
01293 bit = get_bits(gb, 1);
01294
01295
01296 bit ^= 1;
01297 }
01298 }
01299
01300
01301
01302 s->coded_fragment_list_index = 0;
01303 s->next_coeff= s->coeffs + s->fragment_count;
01304 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
01305 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
01306 first_c_fragment_seen = 0;
01307 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
01308 for (i = 0; i < s->superblock_count; i++) {
01309
01310
01311 for (j = 0; j < 16; j++) {
01312
01313
01314 current_fragment = s->superblock_fragments[i * 16 + j];
01315 if (current_fragment >= s->fragment_count) {
01316 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
01317 current_fragment, s->fragment_count);
01318 return 1;
01319 }
01320 if (current_fragment != -1) {
01321 if (s->superblock_coding[i] == SB_NOT_CODED) {
01322
01323
01324 s->all_fragments[current_fragment].coding_method =
01325 MODE_COPY;
01326
01327 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
01328
01329
01330
01331 if (current_run-- == 0) {
01332 bit ^= 1;
01333 #if 1
01334 current_run = get_vlc2(gb,
01335 s->fragment_run_length_vlc.table, 5, 2);
01336 #else
01337 current_run = get_fragment_run_length(gb);
01338 #endif
01339 }
01340
01341 if (bit) {
01342
01343
01344 s->all_fragments[current_fragment].coding_method =
01345 MODE_INTER_NO_MV;
01346 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
01347 s->coded_fragment_list[s->coded_fragment_list_index] =
01348 current_fragment;
01349 if ((current_fragment >= s->u_fragment_start) &&
01350 (s->last_coded_y_fragment == -1) &&
01351 (!first_c_fragment_seen)) {
01352 s->first_coded_c_fragment = s->coded_fragment_list_index;
01353 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
01354 first_c_fragment_seen = 1;
01355 }
01356 s->coded_fragment_list_index++;
01357 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
01358 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
01359 i, current_fragment);
01360 } else {
01361
01362 s->all_fragments[current_fragment].coding_method =
01363 MODE_COPY;
01364 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
01365 i, current_fragment);
01366 }
01367
01368 } else {
01369
01370
01371
01372 s->all_fragments[current_fragment].coding_method =
01373 MODE_INTER_NO_MV;
01374 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
01375 s->coded_fragment_list[s->coded_fragment_list_index] =
01376 current_fragment;
01377 if ((current_fragment >= s->u_fragment_start) &&
01378 (s->last_coded_y_fragment == -1) &&
01379 (!first_c_fragment_seen)) {
01380 s->first_coded_c_fragment = s->coded_fragment_list_index;
01381 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
01382 first_c_fragment_seen = 1;
01383 }
01384 s->coded_fragment_list_index++;
01385 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
01386 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
01387 i, current_fragment);
01388 }
01389 }
01390 }
01391 }
01392
01393 if (!first_c_fragment_seen)
01394
01395 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
01396 else
01397
01398 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
01399
01400 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
01401 s->coded_fragment_list_index,
01402 s->first_coded_y_fragment,
01403 s->last_coded_y_fragment,
01404 s->first_coded_c_fragment,
01405 s->last_coded_c_fragment);
01406
01407 return 0;
01408 }
01409
01410
01411
01412
01413
01414 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
01415 {
01416 int i, j, k;
01417 int scheme;
01418 int current_macroblock;
01419 int current_fragment;
01420 int coding_mode;
01421
01422 debug_vp3(" vp3: unpacking encoding modes\n");
01423
01424 if (s->keyframe) {
01425 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
01426
01427 for (i = 0; i < s->fragment_count; i++)
01428 s->all_fragments[i].coding_method = MODE_INTRA;
01429
01430 } else {
01431
01432
01433 scheme = get_bits(gb, 3);
01434 debug_modes(" using mode alphabet %d\n", scheme);
01435
01436
01437 if (scheme == 0) {
01438 debug_modes(" custom mode alphabet ahead:\n");
01439 for (i = 0; i < 8; i++)
01440 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
01441 }
01442
01443 for (i = 0; i < 8; i++)
01444 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
01445 ModeAlphabet[scheme][i]);
01446
01447
01448
01449 for (i = 0; i < s->u_superblock_start; i++) {
01450
01451 for (j = 0; j < 4; j++) {
01452 current_macroblock = s->superblock_macroblocks[i * 4 + j];
01453 if ((current_macroblock == -1) ||
01454 (s->macroblock_coding[current_macroblock] == MODE_COPY))
01455 continue;
01456 if (current_macroblock >= s->macroblock_count) {
01457 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
01458 current_macroblock, s->macroblock_count);
01459 return 1;
01460 }
01461
01462
01463 if (scheme == 7)
01464 coding_mode = get_bits(gb, 3);
01465 else
01466 {
01467 #if 1
01468 coding_mode = ModeAlphabet[scheme]
01469 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
01470 #else
01471 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
01472 #endif
01473 }
01474
01475 s->macroblock_coding[current_macroblock] = coding_mode;
01476 for (k = 0; k < 6; k++) {
01477 current_fragment =
01478 s->macroblock_fragments[current_macroblock * 6 + k];
01479 if (current_fragment == -1)
01480 continue;
01481 if (current_fragment >= s->fragment_count) {
01482 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
01483 current_fragment, s->fragment_count);
01484 return 1;
01485 }
01486 if (s->all_fragments[current_fragment].coding_method !=
01487 MODE_COPY)
01488 s->all_fragments[current_fragment].coding_method =
01489 coding_mode;
01490 }
01491
01492 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
01493 s->macroblock_fragments[current_macroblock * 6], coding_mode);
01494 }
01495 }
01496 }
01497
01498 return 0;
01499 }
01500
01501
01502
01503
01504
01505 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
01506 {
01507 int i, j, k;
01508 int coding_mode;
01509 int motion_x[6];
01510 int motion_y[6];
01511 int last_motion_x = 0;
01512 int last_motion_y = 0;
01513 int prior_last_motion_x = 0;
01514 int prior_last_motion_y = 0;
01515 int current_macroblock;
01516 int current_fragment;
01517
01518 debug_vp3(" vp3: unpacking motion vectors\n");
01519 if (s->keyframe) {
01520
01521 debug_vp3(" keyframe-- there are no motion vectors\n");
01522
01523 } else {
01524
01525 memset(motion_x, 0, 6 * sizeof(int));
01526 memset(motion_y, 0, 6 * sizeof(int));
01527
01528
01529 coding_mode = get_bits(gb, 1);
01530 debug_vectors(" using %s scheme for unpacking motion vectors\n",
01531 (coding_mode == 0) ? "VLC" : "fixed-length");
01532
01533
01534
01535 for (i = 0; i < s->u_superblock_start; i++) {
01536
01537 for (j = 0; j < 4; j++) {
01538 current_macroblock = s->superblock_macroblocks[i * 4 + j];
01539 if ((current_macroblock == -1) ||
01540 (s->macroblock_coding[current_macroblock] == MODE_COPY))
01541 continue;
01542 if (current_macroblock >= s->macroblock_count) {
01543 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
01544 current_macroblock, s->macroblock_count);
01545 return 1;
01546 }
01547
01548 current_fragment = s->macroblock_fragments[current_macroblock * 6];
01549 if (current_fragment >= s->fragment_count) {
01550 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
01551 current_fragment, s->fragment_count);
01552 return 1;
01553 }
01554 switch (s->macroblock_coding[current_macroblock]) {
01555
01556 case MODE_INTER_PLUS_MV:
01557 case MODE_GOLDEN_MV:
01558
01559 if (coding_mode == 0) {
01560 #if 1
01561 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01562 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01563 #else
01564 motion_x[0] = get_motion_vector_vlc(gb);
01565 motion_y[0] = get_motion_vector_vlc(gb);
01566 #endif
01567 } else {
01568 #if 1
01569 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
01570 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
01571 #else
01572 motion_x[0] = get_motion_vector_fixed(gb);
01573 motion_y[0] = get_motion_vector_fixed(gb);
01574 #endif
01575 }
01576
01577 for (k = 1; k < 6; k++) {
01578 motion_x[k] = motion_x[0];
01579 motion_y[k] = motion_y[0];
01580 }
01581
01582
01583 if (s->macroblock_coding[current_macroblock] ==
01584 MODE_INTER_PLUS_MV) {
01585 prior_last_motion_x = last_motion_x;
01586 prior_last_motion_y = last_motion_y;
01587 last_motion_x = motion_x[0];
01588 last_motion_y = motion_y[0];
01589 }
01590 break;
01591
01592 case MODE_INTER_FOURMV:
01593
01594
01595 motion_x[4] = motion_y[4] = 0;
01596 for (k = 0; k < 4; k++) {
01597 if (coding_mode == 0) {
01598 #if 1
01599 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01600 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01601 #else
01602 motion_x[k] = get_motion_vector_vlc(gb);
01603 motion_y[k] = get_motion_vector_vlc(gb);
01604 #endif
01605 } else {
01606 #if 1
01607 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
01608 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
01609 #else
01610 motion_x[k] = get_motion_vector_fixed(gb);
01611 motion_y[k] = get_motion_vector_fixed(gb);
01612 #endif
01613 }
01614 motion_x[4] += motion_x[k];
01615 motion_y[4] += motion_y[k];
01616 }
01617
01618 if (motion_x[4] >= 0)
01619 motion_x[4] = (motion_x[4] + 2) / 4;
01620 else
01621 motion_x[4] = (motion_x[4] - 2) / 4;
01622 motion_x[5] = motion_x[4];
01623
01624 if (motion_y[4] >= 0)
01625 motion_y[4] = (motion_y[4] + 2) / 4;
01626 else
01627 motion_y[4] = (motion_y[4] - 2) / 4;
01628 motion_y[5] = motion_y[4];
01629
01630
01631
01632 prior_last_motion_x = last_motion_x;
01633 prior_last_motion_y = last_motion_y;
01634 last_motion_x = motion_x[3];
01635 last_motion_y = motion_y[3];
01636 break;
01637
01638 case MODE_INTER_LAST_MV:
01639
01640 motion_x[0] = last_motion_x;
01641 motion_y[0] = last_motion_y;
01642 for (k = 1; k < 6; k++) {
01643 motion_x[k] = motion_x[0];
01644 motion_y[k] = motion_y[0];
01645 }
01646
01647
01648
01649 break;
01650
01651 case MODE_INTER_PRIOR_LAST:
01652
01653
01654 motion_x[0] = prior_last_motion_x;
01655 motion_y[0] = prior_last_motion_y;
01656 for (k = 1; k < 6; k++) {
01657 motion_x[k] = motion_x[0];
01658 motion_y[k] = motion_y[0];
01659 }
01660
01661
01662 prior_last_motion_x = last_motion_x;
01663 prior_last_motion_y = last_motion_y;
01664 last_motion_x = motion_x[0];
01665 last_motion_y = motion_y[0];
01666 break;
01667
01668 default:
01669
01670 memset(motion_x, 0, 6 * sizeof(int));
01671 memset(motion_y, 0, 6 * sizeof(int));
01672
01673
01674 break;
01675 }
01676
01677
01678 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
01679 current_fragment,
01680 s->macroblock_coding[current_macroblock]);
01681 for (k = 0; k < 6; k++) {
01682 current_fragment =
01683 s->macroblock_fragments[current_macroblock * 6 + k];
01684 if (current_fragment == -1)
01685 continue;
01686 if (current_fragment >= s->fragment_count) {
01687 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
01688 current_fragment, s->fragment_count);
01689 return 1;
01690 }
01691 s->all_fragments[current_fragment].motion_x = motion_x[k];
01692 s->all_fragments[current_fragment].motion_y = motion_y[k];
01693 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
01694 k, current_fragment, motion_x[k], motion_y[k]);
01695 }
01696 }
01697 }
01698 }
01699
01700 return 0;
01701 }
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
01716 VLC *table, int coeff_index,
01717 int first_fragment, int last_fragment,
01718 int eob_run)
01719 {
01720 int i;
01721 int token;
01722 int zero_run = 0;
01723 DCTELEM coeff = 0;
01724 Vp3Fragment *fragment;
01725 uint8_t *perm= s->scantable.permutated;
01726 int bits_to_get;
01727
01728 if ((first_fragment >= s->fragment_count) ||
01729 (last_fragment >= s->fragment_count)) {
01730
01731 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
01732 first_fragment, last_fragment);
01733 return 0;
01734 }
01735
01736 for (i = first_fragment; i <= last_fragment; i++) {
01737
01738 fragment = &s->all_fragments[s->coded_fragment_list[i]];
01739 if (fragment->coeff_count > coeff_index)
01740 continue;
01741
01742 if (!eob_run) {
01743
01744 token = get_vlc2(gb, table->table, 5, 3);
01745 debug_vlc(" token = %2d, ", token);
01746
01747 #if 1
01748 if (token <= 6) {
01749 eob_run = eob_run_base[token];
01750 if (eob_run_get_bits[token])
01751 eob_run += get_bits(gb, eob_run_get_bits[token]);
01752 coeff = zero_run = 0;
01753 } else {
01754 bits_to_get = coeff_get_bits[token];
01755 if (!bits_to_get)
01756 coeff = coeff_tables[token][0];
01757 else
01758 coeff = coeff_tables[token][get_bits(gb, bits_to_get)];
01759
01760 zero_run = zero_run_base[token];
01761 if (zero_run_get_bits[token])
01762 zero_run += get_bits(gb, zero_run_get_bits[token]);
01763 }
01764 #else
01765 unpack_token(gb, token, &zero_run, &coeff, &eob_run);
01766 #endif
01767 }
01768
01769 if (!eob_run) {
01770 fragment->coeff_count += zero_run;
01771 if (fragment->coeff_count < 64){
01772 fragment->next_coeff->coeff= coeff;
01773 fragment->next_coeff->index= perm[fragment->coeff_count++];
01774 fragment->next_coeff->next= s->next_coeff;
01775 s->next_coeff->next=NULL;
01776 fragment->next_coeff= s->next_coeff++;
01777 }
01778 debug_vlc(" fragment %d coeff = %d\n",
01779 s->coded_fragment_list[i], fragment->next_coeff[coeff_index]);
01780 } else {
01781 fragment->coeff_count |= 128;
01782 debug_vlc(" fragment %d eob with %d coefficients\n",
01783 s->coded_fragment_list[i], fragment->coeff_count&127);
01784 eob_run--;
01785 }
01786 }
01787
01788 return eob_run;
01789 }
01790
01791
01792
01793
01794
01795 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
01796 {
01797 int i;
01798 int dc_y_table;
01799 int dc_c_table;
01800 int ac_y_table;
01801 int ac_c_table;
01802 int residual_eob_run = 0;
01803
01804
01805 dc_y_table = get_bits(gb, 4);
01806 dc_c_table = get_bits(gb, 4);
01807
01808
01809 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
01810 dc_y_table);
01811 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
01812 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01813
01814
01815 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
01816 dc_c_table);
01817 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
01818 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01819
01820
01821 ac_y_table = get_bits(gb, 4);
01822 ac_c_table = get_bits(gb, 4);
01823
01824
01825 for (i = 1; i <= 5; i++) {
01826
01827 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01828 i, ac_y_table);
01829 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
01830 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01831
01832 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01833 i, ac_c_table);
01834 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
01835 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01836 }
01837
01838
01839 for (i = 6; i <= 14; i++) {
01840
01841 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01842 i, ac_y_table);
01843 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
01844 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01845
01846 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01847 i, ac_c_table);
01848 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
01849 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01850 }
01851
01852
01853 for (i = 15; i <= 27; i++) {
01854
01855 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01856 i, ac_y_table);
01857 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
01858 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01859
01860 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01861 i, ac_c_table);
01862 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
01863 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01864 }
01865
01866
01867 for (i = 28; i <= 63; i++) {
01868
01869 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01870 i, ac_y_table);
01871 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
01872 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01873
01874 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01875 i, ac_c_table);
01876 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
01877 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01878 }
01879
01880 return 0;
01881 }
01882
01883
01884
01885
01886
01887
01888 #define COMPATIBLE_FRAME(x) \
01889 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
01890 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
01891 #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
01892 static inline int iabs (int x) { return ((x < 0) ? -x : x); }
01893
01894 static void reverse_dc_prediction(Vp3DecodeContext *s,
01895 int first_fragment,
01896 int fragment_width,
01897 int fragment_height)
01898 {
01899
01900 #define PUL 8
01901 #define PU 4
01902 #define PUR 2
01903 #define PL 1
01904
01905 int x, y;
01906 int i = first_fragment;
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920 int predictor_group;
01921 short predicted_dc;
01922
01923
01924 int fl, ful, fu, fur;
01925
01926
01927 int vl, vul, vu, vur;
01928
01929
01930 int l, ul, u, ur;
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941 int predictor_transform[16][6] = {
01942 { 0, 0, 0, 0, 0, 0 },
01943 { 0, 0, 0, 1, 0, 0 },
01944 { 0, 0, 1, 0, 0, 0 },
01945 { 0, 0, 53, 75, 127, 7 },
01946 { 0, 1, 0, 0, 0, 0 },
01947 { 0, 1, 0, 1, 1, 1 },
01948 { 0, 1, 0, 0, 0, 0 },
01949 { 0, 0, 53, 75, 127, 7 },
01950 { 1, 0, 0, 0, 0, 0 },
01951 { 0, 0, 0, 1, 0, 0 },
01952 { 1, 0, 1, 0, 1, 1 },
01953 { 0, 0, 53, 75, 127, 7 },
01954 { 0, 1, 0, 0, 0, 0 },
01955 {-26, 29, 0, 29, 31, 5 },
01956 { 3, 10, 3, 0, 15, 4 },
01957 {-26, 29, 0, 29, 31, 5 }
01958 };
01959
01960
01961
01962
01963
01964
01965
01966 unsigned char compatible_frame[8] = {
01967 1,
01968 0,
01969 1,
01970 1,
01971 1,
01972 2,
01973 2,
01974 1
01975 };
01976 int current_frame_type;
01977
01978
01979 short last_dc[3];
01980
01981 int transform = 0;
01982
01983 debug_vp3(" vp3: reversing DC prediction\n");
01984
01985 vul = vu = vur = vl = 0;
01986 last_dc[0] = last_dc[1] = last_dc[2] = 0;
01987
01988
01989 for (y = 0; y < fragment_height; y++) {
01990
01991
01992 for (x = 0; x < fragment_width; x++, i++) {
01993
01994
01995 if (s->all_fragments[i].coding_method != MODE_COPY) {
01996
01997 current_frame_type =
01998 compatible_frame[s->all_fragments[i].coding_method];
01999 predictor_group = (x == 0) + ((y == 0) << 1) +
02000 ((x + 1 == fragment_width) << 2);
02001 debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
02002 i, predictor_group, DC_COEFF(i));
02003
02004 switch (predictor_group) {
02005
02006 case 0:
02007
02008
02009
02010
02011 ul = i - fragment_width - 1;
02012 u = i - fragment_width;
02013 ur = i - fragment_width + 1;
02014 l = i - 1;
02015
02016
02017 vul = DC_COEFF(ul);
02018 vu = DC_COEFF(u);
02019 vur = DC_COEFF(ur);
02020 vl = DC_COEFF(l);
02021
02022
02023 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
02024 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
02025 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
02026 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
02027
02028
02029 transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
02030
02031 break;
02032
02033 case 1:
02034
02035
02036
02037
02038 u = i - fragment_width;
02039 ur = i - fragment_width + 1;
02040
02041
02042 vu = DC_COEFF(u);
02043 vur = DC_COEFF(ur);
02044
02045
02046 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
02047 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
02048
02049
02050 transform = (fu*PU) | (fur*PUR);
02051
02052 break;
02053
02054 case 2:
02055 case 6:
02056
02057
02058
02059
02060 l = i - 1;
02061
02062
02063 vl = DC_COEFF(l);
02064
02065
02066 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
02067
02068
02069 transform = (fl*PL);
02070
02071 break;
02072
02073 case 3:
02074
02075
02076
02077 transform = 0;
02078
02079 break;
02080
02081 case 4:
02082
02083
02084
02085
02086
02087 ul = i - fragment_width - 1;
02088 u = i - fragment_width;
02089 l = i - 1;
02090
02091
02092 vul = DC_COEFF(ul);
02093 vu = DC_COEFF(u);
02094 vl = DC_COEFF(l);
02095
02096
02097 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
02098 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
02099 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
02100
02101
02102 transform = (fl*PL) | (fu*PU) | (ful*PUL);
02103
02104 break;
02105
02106 }
02107
02108 debug_dc_pred("transform = %d, ", transform);
02109
02110 if (transform == 0) {
02111
02112
02113
02114 predicted_dc = last_dc[current_frame_type];
02115 debug_dc_pred("from last DC (%d) = %d\n",
02116 current_frame_type, DC_COEFF(i));
02117
02118 } else {
02119
02120
02121 predicted_dc =
02122 (predictor_transform[transform][0] * vul) +
02123 (predictor_transform[transform][1] * vu) +
02124 (predictor_transform[transform][2] * vur) +
02125 (predictor_transform[transform][3] * vl);
02126
02127
02128
02129 if (predictor_transform[transform][5] != 0) {
02130 predicted_dc += ((predicted_dc >> 15) &
02131 predictor_transform[transform][4]);
02132 predicted_dc >>= predictor_transform[transform][5];
02133 }
02134
02135
02136
02137 if ((transform == 13) || (transform == 15)) {
02138 if (iabs(predicted_dc - vu) > 128)
02139 predicted_dc = vu;
02140 else if (iabs(predicted_dc - vl) > 128)
02141 predicted_dc = vl;
02142 else if (iabs(predicted_dc - vul) > 128)
02143 predicted_dc = vul;
02144 }
02145
02146 debug_dc_pred("from pred DC = %d\n",
02147 DC_COEFF(i));
02148 }
02149
02150
02151 if(s->coeffs[i].index){
02152 *s->next_coeff= s->coeffs[i];
02153 s->coeffs[i].index=0;
02154 s->coeffs[i].coeff=0;
02155 s->coeffs[i].next= s->next_coeff++;
02156 }
02157 s->coeffs[i].coeff += predicted_dc;
02158
02159 last_dc[current_frame_type] = DC_COEFF(i);
02160 if(DC_COEFF(i) && !(s->all_fragments[i].coeff_count&127)){
02161 s->all_fragments[i].coeff_count= 129;
02162
02163 s->coeffs[i].next= s->next_coeff;
02164 (s->next_coeff++)->next=NULL;
02165 }
02166 }
02167 }
02168 }
02169 }
02170
02171
02172 static void horizontal_filter(unsigned char *first_pixel, int stride,
02173 int *bounding_values);
02174 static void vertical_filter(unsigned char *first_pixel, int stride,
02175 int *bounding_values);
02176
02177
02178
02179
02180
02181 static void render_slice(Vp3DecodeContext *s, int slice)
02182 {
02183 int x, y;
02184 int m, n;
02185 int i;
02186 int16_t *dequantizer;
02187 DCTELEM __align16 block[64];
02188 unsigned char *output_plane;
02189 unsigned char *last_plane;
02190 unsigned char *golden_plane;
02191 int stride;
02192 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
02193 int upper_motion_limit, lower_motion_limit;
02194 int motion_halfpel_index;
02195 uint8_t *motion_source;
02196 int plane;
02197 int plane_width;
02198 int plane_height;
02199 int slice_height;
02200 int current_macroblock_entry = slice * s->macroblock_width * 6;
02201 int *bounding_values= s->bounding_values_array+127;
02202 int fragment_width;
02203
02204 if (slice >= s->macroblock_height)
02205 return;
02206
02207 for (plane = 0; plane < 3; plane++) {
02208
02209
02210 if (plane == 0) {
02211 output_plane = s->current_frame.data[0];
02212 last_plane = s->last_frame.data[0];
02213 golden_plane = s->golden_frame.data[0];
02214 stride = s->current_frame.linesize[0];
02215 if (!s->flipped_image) stride = -stride;
02216 upper_motion_limit = 7 * s->current_frame.linesize[0];
02217 lower_motion_limit = s->height * s->current_frame.linesize[0] + s->width - 8;
02218 y = slice * FRAGMENT_PIXELS * 2;
02219 plane_width = s->width;
02220 plane_height = s->height;
02221 slice_height = y + FRAGMENT_PIXELS * 2;
02222 i = s->macroblock_fragments[current_macroblock_entry + 0];
02223 } else if (plane == 1) {
02224 output_plane = s->current_frame.data[1];
02225 last_plane = s->last_frame.data[1];
02226 golden_plane = s->golden_frame.data[1];
02227 stride = s->current_frame.linesize[1];
02228 if (!s->flipped_image) stride = -stride;
02229 upper_motion_limit = 7 * s->current_frame.linesize[1];
02230 lower_motion_limit = (s->height / 2) * s->current_frame.linesize[1] + (s->width / 2) - 8;
02231 y = slice * FRAGMENT_PIXELS;
02232 plane_width = s->width / 2;
02233 plane_height = s->height / 2;
02234 slice_height = y + FRAGMENT_PIXELS;
02235 i = s->macroblock_fragments[current_macroblock_entry + 4];
02236 } else {
02237 output_plane = s->current_frame.data[2];
02238 last_plane = s->last_frame.data[2];
02239 golden_plane = s->golden_frame.data[2];
02240 stride = s->current_frame.linesize[2];
02241 if (!s->flipped_image) stride = -stride;
02242 upper_motion_limit = 7 * s->current_frame.linesize[2];
02243 lower_motion_limit = (s->height / 2) * s->current_frame.linesize[2] + (s->width / 2) - 8;
02244 y = slice * FRAGMENT_PIXELS;
02245 plane_width = s->width / 2;
02246 plane_height = s->height / 2;
02247 slice_height = y + FRAGMENT_PIXELS;
02248 i = s->macroblock_fragments[current_macroblock_entry + 5];
02249 }
02250 fragment_width = plane_width / FRAGMENT_PIXELS;
02251
02252 if(ABS(stride) > 2048)
02253 return;
02254
02255
02256 for (; y < slice_height; y += 8) {
02257
02258
02259 for (x = 0; x < plane_width; x += 8, i++) {
02260
02261 if ((i < 0) || (i >= s->fragment_count)) {
02262 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
02263 return;
02264 }
02265
02266
02267 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
02268 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
02269
02270 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
02271 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
02272 motion_source= golden_plane;
02273 else
02274 motion_source= last_plane;
02275
02276 motion_source += s->all_fragments[i].first_pixel;
02277 motion_halfpel_index = 0;
02278
02279
02280
02281 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
02282 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
02283 int src_x, src_y;
02284 motion_x = s->all_fragments[i].motion_x;
02285 motion_y = s->all_fragments[i].motion_y;
02286 if(plane){
02287 motion_x= (motion_x>>1) | (motion_x&1);
02288 motion_y= (motion_y>>1) | (motion_y&1);
02289 }
02290
02291 src_x= (motion_x>>1) + x;
02292 src_y= (motion_y>>1) + y;
02293 if ((motion_x == 127) || (motion_y == 127))
02294 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
02295
02296 motion_halfpel_index = motion_x & 0x01;
02297 motion_source += (motion_x >> 1);
02298
02299 motion_halfpel_index |= (motion_y & 0x01) << 1;
02300 motion_source += ((motion_y >> 1) * stride);
02301
02302 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
02303 uint8_t *temp= s->edge_emu_buffer;
02304 if(stride<0) temp -= 9*stride;
02305 else temp += 9*stride;
02306
02307 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
02308 motion_source= temp;
02309 }
02310 }
02311
02312
02313
02314
02315 if (s->all_fragments[i].coding_method != MODE_INTRA) {
02316
02317
02318
02319
02320 if(motion_halfpel_index != 3){
02321 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
02322 output_plane + s->all_fragments[i].first_pixel,
02323 motion_source, stride, 8);
02324 }else{
02325 int d= (motion_x ^ motion_y)>>31;
02326 s->dsp.put_no_rnd_pixels_l2[1](
02327 output_plane + s->all_fragments[i].first_pixel,
02328 motion_source - d,
02329 motion_source + stride + 1 + d,
02330 stride, 8);
02331 }
02332 dequantizer = s->inter_dequant;
02333 }else{
02334 if (plane == 0)
02335 dequantizer = s->intra_y_dequant;
02336 else
02337 dequantizer = s->intra_c_dequant;
02338 }
02339
02340
02341 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
02342 i, s->all_fragments[i].coding_method,
02343 DC_COEFF(i), dequantizer[0]);
02344
02345 if(s->avctx->idct_algo==FF_IDCT_VP3){
02346 Coeff *coeff= s->coeffs + i;
02347 memset(block, 0, sizeof(block));
02348 while(coeff->next){
02349 block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
02350 coeff= coeff->next;
02351 }
02352 }else{
02353 Coeff *coeff= s->coeffs + i;
02354 memset(block, 0, sizeof(block));
02355 while(coeff->next){
02356 block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
02357 coeff= coeff->next;
02358 }
02359 }
02360
02361
02362
02363 if (s->all_fragments[i].coding_method == MODE_INTRA) {
02364 if(s->avctx->idct_algo!=FF_IDCT_VP3)
02365 block[0] += 128<<3;
02366 s->dsp.idct_put(
02367 output_plane + s->all_fragments[i].first_pixel,
02368 stride,
02369 block);
02370 } else {
02371 s->dsp.idct_add(
02372 output_plane + s->all_fragments[i].first_pixel,
02373 stride,
02374 block);
02375 }
02376
02377 debug_idct("block after idct_%s():\n",
02378 (s->all_fragments[i].coding_method == MODE_INTRA)?
02379 "put" : "add");
02380 for (m = 0; m < 8; m++) {
02381 for (n = 0; n < 8; n++) {
02382 debug_idct(" %3d", *(output_plane +
02383 s->all_fragments[i].first_pixel + (m * stride + n)));
02384 }
02385 debug_idct("\n");
02386 }
02387 debug_idct("\n");
02388
02389 } else {
02390
02391
02392 s->dsp.put_pixels_tab[1][0](
02393 output_plane + s->all_fragments[i].first_pixel,
02394 last_plane + s->all_fragments[i].first_pixel,
02395 stride, 8);
02396
02397 }
02398 #if 0
02399
02400
02401
02402
02403
02404
02405
02406 if ((x > 0) &&
02407 ((s->all_fragments[i].coding_method != MODE_COPY) ||
02408 ((s->all_fragments[i].coding_method == MODE_COPY) &&
02409 (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
02410 horizontal_filter(
02411 output_plane + s->all_fragments[i].first_pixel + 7*stride,
02412 -stride, bounding_values);
02413 }
02414
02415
02416
02417
02418
02419
02420
02421
02422 if ((y > 0) &&
02423 ((s->all_fragments[i].coding_method != MODE_COPY) ||
02424 ((s->all_fragments[i].coding_method == MODE_COPY) &&
02425 (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
02426 vertical_filter(
02427 output_plane + s->all_fragments[i].first_pixel - stride,
02428 -stride, bounding_values);
02429 }
02430 #endif
02431 }
02432 }
02433 }
02434
02435
02436
02437
02438
02439
02440
02441
02442
02443 emms_c();
02444 }
02445
02446
02447
02448
02449
02450 static void render_fragments(Vp3DecodeContext *s,
02451 int first_fragment,
02452 int width,
02453 int height,
02454 int plane )
02455 {
02456 int x, y;
02457 int m, n;
02458 int i = first_fragment;
02459 int16_t *dequantizer;
02460 DCTELEM __align16 block[64];
02461 unsigned char *output_plane;
02462 unsigned char *last_plane;
02463 unsigned char *golden_plane;
02464 int stride;
02465 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
02466 int upper_motion_limit, lower_motion_limit;
02467 int motion_halfpel_index;
02468 uint8_t *motion_source;
02469
02470 debug_vp3(" vp3: rendering final fragments for %s\n",
02471 (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
02472
02473
02474 if (plane == 0) {
02475 output_plane = s->current_frame.data[0];
02476 last_plane = s->last_frame.data[0];
02477 golden_plane = s->golden_frame.data[0];
02478 stride = s->current_frame.linesize[0];
02479 if (!s->flipped_image) stride = -stride;
02480 upper_motion_limit = 7 * s->current_frame.linesize[0];
02481 lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
02482 } else if (plane == 1) {
02483 output_plane = s->current_frame.data[1];
02484 last_plane = s->last_frame.data[1];
02485 golden_plane = s->golden_frame.data[1];
02486 stride = s->current_frame.linesize[1];
02487 if (!s->flipped_image) stride = -stride;
02488 upper_motion_limit = 7 * s->current_frame.linesize[1];
02489 lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
02490 } else {
02491 output_plane = s->current_frame.data[2];
02492 last_plane = s->last_frame.data[2];
02493 golden_plane = s->golden_frame.data[2];
02494 stride = s->current_frame.linesize[2];
02495 if (!s->flipped_image) stride = -stride;
02496 upper_motion_limit = 7 * s->current_frame.linesize[2];
02497 lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
02498 }
02499
02500 if(ABS(stride) > 2048)
02501 return;
02502
02503
02504 for (y = 0; y < height; y += 8) {
02505
02506
02507 for (x = 0; x < width; x += 8, i++) {
02508
02509 if ((i < 0) || (i >= s->fragment_count)) {
02510 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_fragments(): bad fragment number (%d)\n", i);
02511 return;
02512 }
02513
02514
02515 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
02516 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
02517
02518 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
02519 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
02520 motion_source= golden_plane;
02521 else
02522 motion_source= last_plane;
02523
02524 motion_source += s->all_fragments[i].first_pixel;
02525 motion_halfpel_index = 0;
02526
02527
02528
02529 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
02530 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
02531 int src_x, src_y;
02532 motion_x = s->all_fragments[i].motion_x;
02533 motion_y = s->all_fragments[i].motion_y;
02534 if(plane){
02535 motion_x= (motion_x>>1) | (motion_x&1);
02536 motion_y= (motion_y>>1) | (motion_y&1);
02537 }
02538
02539 src_x= (motion_x>>1) + x;
02540 src_y= (motion_y>>1) + y;
02541 if ((motion_x == 127) || (motion_y == 127))
02542 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
02543
02544 motion_halfpel_index = motion_x & 0x01;
02545 motion_source += (motion_x >> 1);
02546
02547 motion_halfpel_index |= (motion_y & 0x01) << 1;
02548 motion_source += ((motion_y >> 1) * stride);
02549
02550 if(src_x<0 || src_y<0 || src_x + 9 >= width || src_y + 9 >= height){
02551 uint8_t *temp= s->edge_emu_buffer;
02552 if(stride<0) temp -= 9*stride;
02553 else temp += 9*stride;
02554
02555 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, width, height);
02556 motion_source= temp;
02557 }
02558 }
02559
02560
02561
02562
02563 if (s->all_fragments[i].coding_method != MODE_INTRA) {
02564
02565 if(motion_halfpel_index != 3){
02566 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
02567 output_plane + s->all_fragments[i].first_pixel,
02568 motion_source, stride, 8);
02569 }else{
02570 int d= (motion_x ^ motion_y)>>31;
02571 s->dsp.put_no_rnd_pixels_l2[1](
02572 output_plane + s->all_fragments[i].first_pixel,
02573 motion_source - d,
02574 motion_source + stride + 1 + d,
02575 stride, 8);
02576 }
02577 dequantizer = s->inter_dequant;
02578 }else{
02579 if (plane == 0)
02580 dequantizer = s->intra_y_dequant;
02581 else
02582 dequantizer = s->intra_c_dequant;
02583 }
02584
02585
02586 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
02587 i, s->all_fragments[i].coding_method,
02588 DC_COEFF(i), dequantizer[0]);
02589
02590 if(s->avctx->idct_algo==FF_IDCT_VP3){
02591 Coeff *coeff= s->coeffs + i;
02592 memset(block, 0, sizeof(block));
02593 while(coeff->next){
02594 block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
02595 coeff= coeff->next;
02596 }
02597 }else{
02598 Coeff *coeff= s->coeffs + i;
02599 memset(block, 0, sizeof(block));
02600 while(coeff->next){
02601 block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
02602 coeff= coeff->next;
02603 }
02604 }
02605
02606
02607
02608 if (s->all_fragments[i].coding_method == MODE_INTRA) {
02609 if(s->avctx->idct_algo!=FF_IDCT_VP3)
02610 block[0] += 128<<3;
02611 s->dsp.idct_put(
02612 output_plane + s->all_fragments[i].first_pixel,
02613 stride,
02614 block);
02615 } else {
02616 s->dsp.idct_add(
02617 output_plane + s->all_fragments[i].first_pixel,
02618 stride,
02619 block);
02620 }
02621
02622 debug_idct("block after idct_%s():\n",
02623 (s->all_fragments[i].coding_method == MODE_INTRA)?
02624 "put" : "add");
02625 for (m = 0; m < 8; m++) {
02626 for (n = 0; n < 8; n++) {
02627 debug_idct(" %3d", *(output_plane +
02628 s->all_fragments[i].first_pixel + (m * stride + n)));
02629 }
02630 debug_idct("\n");
02631 }
02632 debug_idct("\n");
02633
02634 } else {
02635
02636
02637 s->dsp.put_pixels_tab[1][0](
02638 output_plane + s->all_fragments[i].first_pixel,
02639 last_plane + s->all_fragments[i].first_pixel,
02640 stride, 8);
02641
02642 }
02643 }
02644 }
02645
02646 emms_c();
02647 }
02648
02649 static void horizontal_filter(unsigned char *first_pixel, int stride,
02650 int *bounding_values)
02651 {
02652 unsigned char *end;
02653 int filter_value;
02654
02655 for (end= first_pixel + 8*stride; first_pixel < end; first_pixel += stride) {
02656 filter_value =
02657 (first_pixel[-2] - first_pixel[ 1])
02658 +3*(first_pixel[ 0] - first_pixel[-1]);
02659 filter_value = bounding_values[(filter_value + 4) >> 3];
02660 first_pixel[-1] = clip_uint8(first_pixel[-1] + filter_value);
02661 first_pixel[ 0] = clip_uint8(first_pixel[ 0] - filter_value);
02662 }
02663 }
02664
02665 static void vertical_filter(unsigned char *first_pixel, int stride,
02666 int *bounding_values)
02667 {
02668 unsigned char *end;
02669 int filter_value;
02670 const int nstride= -stride;
02671
02672 for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
02673 filter_value =
02674 (first_pixel[2 * nstride] - first_pixel[ stride])
02675 +3*(first_pixel[0 ] - first_pixel[nstride]);
02676 filter_value = bounding_values[(filter_value + 4) >> 3];
02677 first_pixel[nstride] = clip_uint8(first_pixel[nstride] + filter_value);
02678 first_pixel[0] = clip_uint8(first_pixel[0] - filter_value);
02679 }
02680 }
02681
02682 static void apply_loop_filter(Vp3DecodeContext *s)
02683 {
02684 int x, y, plane;
02685 int width, height;
02686 int fragment;
02687 int stride;
02688 unsigned char *plane_data;
02689 int *bounding_values= s->bounding_values_array+127;
02690
02691 #if 0
02692 int bounding_values_array[256];
02693 int filter_limit;
02694
02695
02696 for (x = 63; x >= 0; x--) {
02697 if (vp31_ac_scale_factor[x] >= s->quality_index)
02698 break;
02699 }
02700 filter_limit = vp31_filter_limit_values[s->quality_index];
02701
02702
02703 memset(bounding_values_array, 0, 256 * sizeof(int));
02704 for (x = 0; x < filter_limit; x++) {
02705 bounding_values[-x - filter_limit] = -filter_limit + x;
02706 bounding_values[-x] = -x;
02707 bounding_values[x] = x;
02708 bounding_values[x + filter_limit] = filter_limit - x;
02709 }
02710 #endif
02711
02712 for (plane = 0; plane < 3; plane++) {
02713
02714 if (plane == 0) {
02715
02716 fragment = 0;
02717 width = s->fragment_width;
02718 height = s->fragment_height;
02719 stride = s->current_frame.linesize[0];
02720 plane_data = s->current_frame.data[0];
02721 } else if (plane == 1) {
02722
02723 fragment = s->u_fragment_start;
02724 width = s->fragment_width / 2;
02725 height = s->fragment_height / 2;
02726 stride = s->current_frame.linesize[1];
02727 plane_data = s->current_frame.data[1];
02728 } else {
02729
02730 fragment = s->v_fragment_start;
02731 width = s->fragment_width / 2;
02732 height = s->fragment_height / 2;
02733 stride = s->current_frame.linesize[2];
02734 plane_data = s->current_frame.data[2];
02735 }
02736
02737 for (y = 0; y < height; y++) {
02738
02739 for (x = 0; x < width; x++) {
02740 START_TIMER
02741
02742 if ((x > 0) &&
02743 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
02744 horizontal_filter(
02745 plane_data + s->all_fragments[fragment].first_pixel - 7*stride,
02746 stride, bounding_values);
02747 }
02748
02749
02750 if ((y > 0) &&
02751 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
02752 vertical_filter(
02753 plane_data + s->all_fragments[fragment].first_pixel + stride,
02754 stride, bounding_values);
02755 }
02756
02757
02758
02759
02760 if ((x < width - 1) &&
02761 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
02762 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
02763 horizontal_filter(
02764 plane_data + s->all_fragments[fragment + 1].first_pixel - 7*stride,
02765 stride, bounding_values);
02766 }
02767
02768
02769
02770
02771 if ((y < height - 1) &&
02772 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
02773 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
02774 vertical_filter(
02775 plane_data + s->all_fragments[fragment + width].first_pixel + stride,
02776 stride, bounding_values);
02777 }
02778
02779 fragment++;
02780 STOP_TIMER("loop filter")
02781 }
02782 }
02783 }
02784 }
02785
02786
02787
02788
02789
02790
02791 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
02792 {
02793
02794 int i, x, y;
02795
02796
02797
02798 i = 0;
02799 for (y = s->fragment_height; y > 0; y--) {
02800 for (x = 0; x < s->fragment_width; x++) {
02801 s->all_fragments[i++].first_pixel =
02802 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
02803 s->golden_frame.linesize[0] +
02804 x * FRAGMENT_PIXELS;
02805 debug_init(" fragment %d, first pixel @ %d\n",
02806 i-1, s->all_fragments[i-1].first_pixel);
02807 }
02808 }
02809
02810
02811 i = s->u_fragment_start;
02812 for (y = s->fragment_height / 2; y > 0; y--) {
02813 for (x = 0; x < s->fragment_width / 2; x++) {
02814 s->all_fragments[i++].first_pixel =
02815 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
02816 s->golden_frame.linesize[1] +
02817 x * FRAGMENT_PIXELS;
02818 debug_init(" fragment %d, first pixel @ %d\n",
02819 i-1, s->all_fragments[i-1].first_pixel);
02820 }
02821 }
02822
02823
02824 i = s->v_fragment_start;
02825 for (y = s->fragment_height / 2; y > 0; y--) {
02826 for (x = 0; x < s->fragment_width / 2; x++) {
02827 s->all_fragments[i++].first_pixel =
02828 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
02829 s->golden_frame.linesize[2] +
02830 x * FRAGMENT_PIXELS;
02831 debug_init(" fragment %d, first pixel @ %d\n",
02832 i-1, s->all_fragments[i-1].first_pixel);
02833 }
02834 }
02835 }
02836
02837
02838 static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
02839 {
02840
02841 int i, x, y;
02842
02843
02844
02845 i = 0;
02846 for (y = 1; y <= s->fragment_height; y++) {
02847 for (x = 0; x < s->fragment_width; x++) {
02848 s->all_fragments[i++].first_pixel =
02849 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
02850 s->golden_frame.linesize[0] +
02851 x * FRAGMENT_PIXELS;
02852 debug_init(" fragment %d, first pixel @ %d\n",
02853 i-1, s->all_fragments[i-1].first_pixel);
02854 }
02855 }
02856
02857
02858 i = s->u_fragment_start;
02859 for (y = 1; y <= s->fragment_height / 2; y++) {
02860 for (x = 0; x < s->fragment_width / 2; x++) {
02861 s->all_fragments[i++].first_pixel =
02862 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
02863 s->golden_frame.linesize[1] +
02864 x * FRAGMENT_PIXELS;
02865 debug_init(" fragment %d, first pixel @ %d\n",
02866 i-1, s->all_fragments[i-1].first_pixel);
02867 }
02868 }
02869
02870
02871 i = s->v_fragment_start;
02872 for (y = 1; y <= s->fragment_height / 2; y++) {
02873 for (x = 0; x < s->fragment_width / 2; x++) {
02874 s->all_fragments[i++].first_pixel =
02875 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
02876 s->golden_frame.linesize[2] +
02877 x * FRAGMENT_PIXELS;
02878 debug_init(" fragment %d, first pixel @ %d\n",
02879 i-1, s->all_fragments[i-1].first_pixel);
02880 }
02881 }
02882 }
02883
02884
02885
02886
02887 static int vp3_decode_init(AVCodecContext *avctx)
02888 {
02889 Vp3DecodeContext *s = avctx->priv_data;
02890 int i;
02891 int c_width;
02892 int c_height;
02893 int y_superblock_count;
02894 int c_superblock_count;
02895
02896 if (avctx->codec_tag == MKTAG('V','P','3','0'))
02897 s->version = 0;
02898 else
02899 s->version = 1;
02900
02901 s->avctx = avctx;
02902 s->width = (avctx->width + 15) & 0xFFFFFFF0;
02903 s->height = (avctx->height + 15) & 0xFFFFFFF0;
02904 avctx->pix_fmt = PIX_FMT_YUV420P;
02905 avctx->has_b_frames = 0;
02906 if(avctx->idct_algo==FF_IDCT_AUTO)
02907 avctx->idct_algo=FF_IDCT_VP3;
02908 dsputil_init(&s->dsp, avctx);
02909
02910 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
02911
02912
02913
02914 s->quality_index = -1;
02915
02916 s->y_superblock_width = (s->width + 31) / 32;
02917 s->y_superblock_height = (s->height + 31) / 32;
02918 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
02919
02920
02921 c_width = s->width / 2;
02922 c_height = s->height / 2;
02923 s->c_superblock_width = (c_width + 31) / 32;
02924 s->c_superblock_height = (c_height + 31) / 32;
02925 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
02926
02927 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
02928 s->u_superblock_start = y_superblock_count;
02929 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
02930 s->superblock_coding = av_malloc(s->superblock_count);
02931
02932 s->macroblock_width = (s->width + 15) / 16;
02933 s->macroblock_height = (s->height + 15) / 16;
02934 s->macroblock_count = s->macroblock_width * s->macroblock_height;
02935
02936 s->fragment_width = s->width / FRAGMENT_PIXELS;
02937 s->fragment_height = s->height / FRAGMENT_PIXELS;
02938
02939
02940 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
02941 s->u_fragment_start = s->fragment_width * s->fragment_height;
02942 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
02943
02944 debug_init(" Y plane: %d x %d\n", s->width, s->height);
02945 debug_init(" C plane: %d x %d\n", c_width, c_height);
02946 debug_init(" Y superblocks: %d x %d, %d total\n",
02947 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
02948 debug_init(" C superblocks: %d x %d, %d total\n",
02949 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
02950 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
02951 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
02952 debug_init(" macroblocks: %d x %d, %d total\n",
02953 s->macroblock_width, s->macroblock_height, s->macroblock_count);
02954 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
02955 s->fragment_count,
02956 s->fragment_width,
02957 s->fragment_height,
02958 s->u_fragment_start,
02959 s->v_fragment_start);
02960
02961 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
02962 s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
02963 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
02964 s->pixel_addresses_inited = 0;
02965
02966 if (!s->theora_tables)
02967 {
02968 for (i = 0; i < 64; i++)
02969 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
02970 for (i = 0; i < 64; i++)
02971 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
02972 for (i = 0; i < 64; i++)
02973 s->coded_intra_y_dequant[i] = vp31_intra_y_dequant[i];
02974 for (i = 0; i < 64; i++)
02975 s->coded_intra_c_dequant[i] = vp31_intra_c_dequant[i];
02976 for (i = 0; i < 64; i++)
02977 s->coded_inter_dequant[i] = vp31_inter_dequant[i];
02978 for (i = 0; i < 64; i++)
02979 s->filter_limit_values[i] = vp31_filter_limit_values[i];
02980
02981
02982 for (i = 0; i < 16; i++) {
02983
02984
02985 init_vlc(&s->dc_vlc[i], 5, 32,
02986 &dc_bias[i][0][1], 4, 2,
02987 &dc_bias[i][0][0], 4, 2, 0);
02988
02989
02990 init_vlc(&s->ac_vlc_1[i], 5, 32,
02991 &ac_bias_0[i][0][1], 4, 2,
02992 &ac_bias_0[i][0][0], 4, 2, 0);
02993
02994
02995 init_vlc(&s->ac_vlc_2[i], 5, 32,
02996 &ac_bias_1[i][0][1], 4, 2,
02997 &ac_bias_1[i][0][0], 4, 2, 0);
02998
02999
03000 init_vlc(&s->ac_vlc_3[i], 5, 32,
03001 &ac_bias_2[i][0][1], 4, 2,
03002 &ac_bias_2[i][0][0], 4, 2, 0);
03003
03004
03005 init_vlc(&s->ac_vlc_4[i], 5, 32,
03006 &ac_bias_3[i][0][1], 4, 2,
03007 &ac_bias_3[i][0][0], 4, 2, 0);
03008 }
03009 } else {
03010 for (i = 0; i < 16; i++) {
03011
03012
03013 init_vlc(&s->dc_vlc[i], 5, 32,
03014 &s->huffman_table[i][0][1], 4, 2,
03015 &s->huffman_table[i][0][0], 4, 2, 0);
03016
03017
03018 init_vlc(&s->ac_vlc_1[i], 5, 32,
03019 &s->huffman_table[i+16][0][1], 4, 2,
03020 &s->huffman_table[i+16][0][0], 4, 2, 0);
03021
03022
03023 init_vlc(&s->ac_vlc_2[i], 5, 32,
03024 &s->huffman_table[i+16*2][0][1], 4, 2,
03025 &s->huffman_table[i+16*2][0][0], 4, 2, 0);
03026
03027
03028 init_vlc(&s->ac_vlc_3[i], 5, 32,
03029 &s->huffman_table[i+16*3][0][1], 4, 2,
03030 &s->huffman_table[i+16*3][0][0], 4, 2, 0);
03031
03032
03033 init_vlc(&s->ac_vlc_4[i], 5, 32,
03034 &s->huffman_table[i+16*4][0][1], 4, 2,
03035 &s->huffman_table[i+16*4][0][0], 4, 2, 0);
03036 }
03037 }
03038
03039 init_vlc(&s->superblock_run_length_vlc, 6, 34,
03040 &superblock_run_length_vlc_table[0][1], 4, 2,
03041 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
03042
03043 init_vlc(&s->fragment_run_length_vlc, 5, 31,
03044 &fragment_run_length_vlc_table[0][1], 4, 2,
03045 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
03046
03047 init_vlc(&s->mode_code_vlc, 3, 8,
03048 &mode_code_vlc_table[0][1], 2, 1,
03049 &mode_code_vlc_table[0][0], 2, 1, 0);
03050
03051 init_vlc(&s->motion_vector_vlc, 6, 63,
03052 &motion_vector_vlc_table[0][1], 2, 1,
03053 &motion_vector_vlc_table[0][0], 2, 1, 0);
03054
03055
03056 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
03057 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
03058 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
03059 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
03060 init_block_mapping(s);
03061
03062 for (i = 0; i < 3; i++) {
03063 s->current_frame.data[i] = NULL;
03064 s->last_frame.data[i] = NULL;
03065 s->golden_frame.data[i] = NULL;
03066 }
03067
03068 return 0;
03069 }
03070
03071
03072
03073
03074 static int vp3_decode_frame(AVCodecContext *avctx,
03075 void *data, int *data_size,
03076 uint8_t *buf, int buf_size)
03077 {
03078 Vp3DecodeContext *s = avctx->priv_data;
03079 GetBitContext gb;
03080 static int counter = 0;
03081 int i;
03082
03083 init_get_bits(&gb, buf, buf_size * 8);
03084
03085 if (s->theora && get_bits1(&gb))
03086 {
03087 int ptype = get_bits(&gb, 7);
03088
03089 skip_bits(&gb, 6*8);
03090
03091 switch(ptype)
03092 {
03093 case 1:
03094 theora_decode_comments(avctx, gb);
03095 break;
03096 case 2:
03097 theora_decode_tables(avctx, gb);
03098 init_dequantizer(s);
03099 break;
03100 default:
03101 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype);
03102 }
03103 return buf_size;
03104 }
03105
03106 s->keyframe = !get_bits1(&gb);
03107 if (!s->theora)
03108 skip_bits(&gb, 1);
03109 s->last_quality_index = s->quality_index;
03110 s->quality_index = get_bits(&gb, 6);
03111 if (s->theora >= 0x030200)
03112 skip_bits1(&gb);
03113
03114 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
03115 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
03116 s->keyframe?"key":"", counter, s->quality_index);
03117 counter++;
03118
03119 if (s->quality_index != s->last_quality_index) {
03120 init_dequantizer(s);
03121 init_loop_filter(s);
03122 }
03123
03124 if (s->keyframe) {
03125 if (!s->theora)
03126 {
03127 skip_bits(&gb, 4);
03128 skip_bits(&gb, 4);
03129 if (s->version)
03130 {
03131 s->version = get_bits(&gb, 5);
03132 if (counter == 1)
03133 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
03134 }
03135 }
03136 if (s->version || s->theora)
03137 {
03138 if (get_bits1(&gb))
03139 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
03140 skip_bits(&gb, 2);
03141 }
03142
03143 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
03144 if (s->golden_frame.data[0])
03145 avctx->release_buffer(avctx, &s->golden_frame);
03146 s->last_frame= s->golden_frame;
03147 } else {
03148 if (s->golden_frame.data[0])
03149 avctx->release_buffer(avctx, &s->golden_frame);
03150 if (s->last_frame.data[0])
03151 avctx->release_buffer(avctx, &s->last_frame);
03152 }
03153
03154 s->golden_frame.reference = 3;
03155 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
03156 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
03157 return -1;
03158 }
03159
03160
03161 memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
03162
03163
03164 if (!s->pixel_addresses_inited)
03165 {
03166 if (!s->flipped_image)
03167 vp3_calculate_pixel_addresses(s);
03168 else
03169 theora_calculate_pixel_addresses(s);
03170 }
03171 } else {
03172
03173 s->current_frame.reference = 3;
03174 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
03175 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
03176 return -1;
03177 }
03178 }
03179
03180 s->current_frame.qscale_table= s->qscale_table;
03181 s->current_frame.qstride= 0;
03182
03183 {START_TIMER
03184 init_frame(s, &gb);
03185 STOP_TIMER("init_frame")}
03186
03187 #if KEYFRAMES_ONLY
03188 if (!s->keyframe) {
03189
03190 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
03191 s->current_frame.linesize[0] * s->height);
03192 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
03193 s->current_frame.linesize[1] * s->height / 2);
03194 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
03195 s->current_frame.linesize[2] * s->height / 2);
03196
03197 } else {
03198 #endif
03199
03200 {START_TIMER
03201 if (unpack_superblocks(s, &gb)){
03202 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
03203 return -1;
03204 }
03205 STOP_TIMER("unpack_superblocks")}
03206 {START_TIMER
03207 if (unpack_modes(s, &gb)){
03208 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
03209 return -1;
03210 }
03211 STOP_TIMER("unpack_modes")}
03212 {START_TIMER
03213 if (unpack_vectors(s, &gb)){
03214 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
03215 return -1;
03216 }
03217 STOP_TIMER("unpack_vectors")}
03218 {START_TIMER
03219 if (unpack_dct_coeffs(s, &gb)){
03220 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
03221 return -1;
03222 }
03223 STOP_TIMER("unpack_dct_coeffs")}
03224 {START_TIMER
03225
03226 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
03227 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
03228 reverse_dc_prediction(s, s->u_fragment_start,
03229 s->fragment_width / 2, s->fragment_height / 2);
03230 reverse_dc_prediction(s, s->v_fragment_start,
03231 s->fragment_width / 2, s->fragment_height / 2);
03232 }
03233 STOP_TIMER("reverse_dc_prediction")}
03234 {START_TIMER
03235
03236 #if 1
03237 for (i = 0; i < s->macroblock_height; i++)
03238 render_slice(s, i);
03239 #else
03240 render_fragments(s, 0, s->width, s->height, 0);
03241 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
03242 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
03243 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
03244 } else {
03245 memset(s->current_frame.data[1], 0x80, s->width * s->height / 4);
03246 memset(s->current_frame.data[2], 0x80, s->width * s->height / 4);
03247 }
03248 #endif
03249 STOP_TIMER("render_fragments")}
03250
03251 {START_TIMER
03252 apply_loop_filter(s);
03253 STOP_TIMER("apply_loop_filter")}
03254 #if KEYFRAMES_ONLY
03255 }
03256 #endif
03257
03258 *data_size=sizeof(AVFrame);
03259 *(AVFrame*)data= s->current_frame;
03260
03261
03262
03263 if ((s->last_frame.data[0]) &&
03264 (s->last_frame.data[0] != s->golden_frame.data[0]))
03265 avctx->release_buffer(avctx, &s->last_frame);
03266
03267
03268 memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
03269 s->current_frame.data[0]= NULL;
03270
03271 return buf_size;
03272 }
03273
03274
03275
03276
03277 static int vp3_decode_end(AVCodecContext *avctx)
03278 {
03279 Vp3DecodeContext *s = avctx->priv_data;
03280
03281 av_free(s->all_fragments);
03282 av_free(s->coeffs);
03283 av_free(s->coded_fragment_list);
03284 av_free(s->superblock_fragments);
03285 av_free(s->superblock_macroblocks);
03286 av_free(s->macroblock_fragments);
03287 av_free(s->macroblock_coding);
03288
03289
03290 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
03291 avctx->release_buffer(avctx, &s->golden_frame);
03292 if (s->last_frame.data[0])
03293 avctx->release_buffer(avctx, &s->last_frame);
03294
03295
03296
03297 return 0;
03298 }
03299
03300 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
03301 {
03302 Vp3DecodeContext *s = avctx->priv_data;
03303
03304 if (get_bits(gb, 1)) {
03305 int token;
03306 if (s->entries >= 32) {
03307 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
03308 return -1;
03309 }
03310 token = get_bits(gb, 5);
03311
03312 s->huffman_table[s->hti][token][0] = s->hbits;
03313 s->huffman_table[s->hti][token][1] = s->huff_code_size;
03314 s->entries++;
03315 }
03316 else {
03317 if (s->huff_code_size >= 32) {
03318 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
03319 return -1;
03320 }
03321 s->huff_code_size++;
03322 s->hbits <<= 1;
03323 read_huffman_tree(avctx, gb);
03324 s->hbits |= 1;
03325 read_huffman_tree(avctx, gb);
03326 s->hbits >>= 1;
03327 s->huff_code_size--;
03328 }
03329 return 0;
03330 }
03331
03332 static int theora_decode_header(AVCodecContext *avctx, GetBitContext gb)
03333 {
03334 Vp3DecodeContext *s = avctx->priv_data;
03335 int major, minor, micro;
03336
03337 major = get_bits(&gb, 8);
03338 minor = get_bits(&gb, 8);
03339 micro = get_bits(&gb, 8);
03340 av_log(avctx, AV_LOG_INFO, "Theora bitstream version %d.%d.%d\n",
03341 major, minor, micro);
03342
03343
03344 s->theora = (major << 16) | (minor << 8) | micro;
03345
03346
03347
03348 if (s->theora < 0x030200)
03349 {
03350 s->flipped_image = 1;
03351 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
03352 }
03353
03354 s->width = get_bits(&gb, 16) << 4;
03355 s->height = get_bits(&gb, 16) << 4;
03356
03357 if(avcodec_check_dimensions(avctx, s->width, s->height)){
03358 s->width= s->height= 0;
03359 return -1;
03360 }
03361
03362 skip_bits(&gb, 24);
03363 skip_bits(&gb, 24);
03364
03365 skip_bits(&gb, 8);
03366 skip_bits(&gb, 8);
03367
03368 skip_bits(&gb, 32);
03369 skip_bits(&gb, 32);
03370 skip_bits(&gb, 24);
03371 skip_bits(&gb, 24);
03372
03373 if (s->theora < 0x030200)
03374 skip_bits(&gb, 5);
03375 skip_bits(&gb, 8);
03376 skip_bits(&gb, 24);
03377
03378 skip_bits(&gb, 6);
03379
03380 if (s->theora >= 0x030200)
03381 {
03382 skip_bits(&gb, 5);
03383 skip_bits(&gb, 5);
03384 }
03385
03386
03387
03388 avctx->width = s->width;
03389 avctx->height = s->height;
03390
03391 return 0;
03392 }
03393
03394 static int theora_decode_comments(AVCodecContext *avctx, GetBitContext gb)
03395 {
03396 int nb_comments, i, tmp;
03397
03398 tmp = get_bits_long(&gb, 32);
03399 tmp = be2me_32(tmp);
03400 while(tmp--)
03401 skip_bits(&gb, 8);
03402
03403 nb_comments = get_bits_long(&gb, 32);
03404 nb_comments = be2me_32(nb_comments);
03405 for (i = 0; i < nb_comments; i++)
03406 {
03407 tmp = get_bits_long(&gb, 32);
03408 tmp = be2me_32(tmp);
03409 while(tmp--)
03410 skip_bits(&gb, 8);
03411 }
03412
03413 return 0;
03414 }
03415
03416 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb)
03417 {
03418 Vp3DecodeContext *s = avctx->priv_data;
03419 int i, n;
03420
03421 if (s->theora >= 0x030200) {
03422 n = get_bits(&gb, 3);
03423
03424 for (i = 0; i < 64; i++)
03425 s->filter_limit_values[i] = get_bits(&gb, n);
03426 }
03427
03428 if (s->theora >= 0x030200)
03429 n = get_bits(&gb, 4) + 1;
03430 else
03431 n = 16;
03432
03433 for (i = 0; i < 64; i++)
03434 s->coded_ac_scale_factor[i] = get_bits(&gb, n);
03435
03436 if (s->theora >= 0x030200)
03437 n = get_bits(&gb, 4) + 1;
03438 else
03439 n = 16;
03440
03441 for (i = 0; i < 64; i++)
03442 s->coded_dc_scale_factor[i] = get_bits(&gb, n);
03443
03444 if (s->theora >= 0x030200)
03445 n = get_bits(&gb, 9) + 1;
03446 else
03447 n = 3;
03448 if (n != 3) {
03449 av_log(NULL,AV_LOG_ERROR, "unsupported nbms : %d\n", n);
03450 return -1;
03451 }
03452
03453 for (i = 0; i < 64; i++)
03454 s->coded_intra_y_dequant[i] = get_bits(&gb, 8);
03455
03456
03457 for (i = 0; i < 64; i++)
03458 s->coded_intra_c_dequant[i] = get_bits(&gb, 8);
03459
03460
03461 for (i = 0; i < 64; i++)
03462 s->coded_inter_dequant[i] = get_bits(&gb, 8);
03463
03464
03465 for (i = 0; i <= 1; i++) {
03466 for (n = 0; n <= 2; n++) {
03467 int newqr;
03468 if (i > 0 || n > 0)
03469 newqr = get_bits(&gb, 1);
03470 else
03471 newqr = 1;
03472 if (!newqr) {
03473 if (i > 0)
03474 get_bits(&gb, 1);
03475 }
03476 else {
03477 int qi = 0;
03478 skip_bits(&gb, av_log2(2)+1);
03479 while (qi < 63) {
03480 qi += get_bits(&gb, av_log2(63-qi)+1) + 1;
03481 skip_bits(&gb, av_log2(2)+1);
03482 }
03483 if (qi > 63)
03484 av_log(NULL, AV_LOG_ERROR, "error...\n");
03485 }
03486 }
03487 }
03488
03489 for (s->hti = 0; s->hti < 80; s->hti++) {
03490 s->entries = 0;
03491 s->huff_code_size = 1;
03492 if (!get_bits(&gb, 1)) {
03493 s->hbits = 0;
03494 read_huffman_tree(avctx, &gb);
03495 s->hbits = 1;
03496 read_huffman_tree(avctx, &gb);
03497 }
03498 }
03499
03500 s->theora_tables = 1;
03501
03502 return 0;
03503 }
03504
03505 static int theora_decode_init(AVCodecContext *avctx)
03506 {
03507 Vp3DecodeContext *s = avctx->priv_data;
03508 GetBitContext gb;
03509 int ptype;
03510 uint8_t *p= avctx->extradata;
03511 int op_bytes, i;
03512
03513 s->theora = 1;
03514
03515 if (!avctx->extradata_size)
03516 return -1;
03517
03518 for(i=0;i<3;i++) {
03519 op_bytes = *(p++)<<8;
03520 op_bytes += *(p++);
03521
03522 init_get_bits(&gb, p, op_bytes);
03523 p += op_bytes;
03524
03525 ptype = get_bits(&gb, 8);
03526 debug_vp3("Theora headerpacket type: %x\n", ptype);
03527
03528 if (!(ptype & 0x80))
03529 return -1;
03530
03531 skip_bits(&gb, 6*8);
03532
03533 switch(ptype)
03534 {
03535 case 0x80:
03536 theora_decode_header(avctx, gb);
03537 break;
03538 case 0x81:
03539 theora_decode_comments(avctx, gb);
03540 break;
03541 case 0x82:
03542 theora_decode_tables(avctx, gb);
03543 break;
03544 }
03545 }
03546
03547 vp3_decode_init(avctx);
03548 return 0;
03549 }
03550
03551 AVCodec vp3_decoder = {
03552 "vp3",
03553 CODEC_TYPE_VIDEO,
03554 CODEC_ID_VP3,
03555 sizeof(Vp3DecodeContext),
03556 vp3_decode_init,
03557 NULL,
03558 vp3_decode_end,
03559 vp3_decode_frame,
03560 0,
03561 NULL
03562 };
03563
03564 #ifndef CONFIG_LIBTHEORA
03565 AVCodec theora_decoder = {
03566 "theora",
03567 CODEC_TYPE_VIDEO,
03568 CODEC_ID_THEORA,
03569 sizeof(Vp3DecodeContext),
03570 theora_decode_init,
03571 NULL,
03572 vp3_decode_end,
03573 vp3_decode_frame,
03574 0,
03575 NULL
03576 };
03577 #endif