00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036
00037 #include "avcodec.h"
00038 #include "dsputil.h"
00039 #include "mpegvideo.h"
00040
00041 #include "vp3data.h"
00042 #include "xiph.h"
00043
00044 #define FRAGMENT_PIXELS 8
00045
00046 static int vp3_decode_end(AVCodecContext *avctx);
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #define KEYFRAMES_ONLY 0
00068
00069 #define DEBUG_VP3 0
00070 #define DEBUG_INIT 0
00071 #define DEBUG_DEQUANTIZERS 0
00072 #define DEBUG_BLOCK_CODING 0
00073 #define DEBUG_MODES 0
00074 #define DEBUG_VECTORS 0
00075 #define DEBUG_TOKEN 0
00076 #define DEBUG_VLC 0
00077 #define DEBUG_DC_PRED 0
00078 #define DEBUG_IDCT 0
00079
00080 #if DEBUG_VP3
00081 #define debug_vp3(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00082 #else
00083 static inline void debug_vp3(const char *format, ...) { }
00084 #endif
00085
00086 #if DEBUG_INIT
00087 #define debug_init(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00088 #else
00089 static inline void debug_init(const char *format, ...) { }
00090 #endif
00091
00092 #if DEBUG_DEQUANTIZERS
00093 #define debug_dequantizers(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00094 #else
00095 static inline void debug_dequantizers(const char *format, ...) { }
00096 #endif
00097
00098 #if DEBUG_BLOCK_CODING
00099 #define debug_block_coding(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00100 #else
00101 static inline void debug_block_coding(const char *format, ...) { }
00102 #endif
00103
00104 #if DEBUG_MODES
00105 #define debug_modes(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00106 #else
00107 static inline void debug_modes(const char *format, ...) { }
00108 #endif
00109
00110 #if DEBUG_VECTORS
00111 #define debug_vectors(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00112 #else
00113 static inline void debug_vectors(const char *format, ...) { }
00114 #endif
00115
00116 #if DEBUG_TOKEN
00117 #define debug_token(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00118 #else
00119 static inline void debug_token(const char *format, ...) { }
00120 #endif
00121
00122 #if DEBUG_VLC
00123 #define debug_vlc(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00124 #else
00125 static inline void debug_vlc(const char *format, ...) { }
00126 #endif
00127
00128 #if DEBUG_DC_PRED
00129 #define debug_dc_pred(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00130 #else
00131 static inline void debug_dc_pred(const char *format, ...) { }
00132 #endif
00133
00134 #if DEBUG_IDCT
00135 #define debug_idct(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
00136 #else
00137 static inline void debug_idct(const char *format, ...) { }
00138 #endif
00139
00140 typedef struct Coeff {
00141 struct Coeff *next;
00142 DCTELEM coeff;
00143 uint8_t index;
00144 } Coeff;
00145
00146
00147 typedef struct Vp3Fragment {
00148 Coeff *next_coeff;
00149
00150
00151 int first_pixel;
00152
00153 uint16_t macroblock;
00154 uint8_t coding_method;
00155 uint8_t coeff_count;
00156 int8_t motion_x;
00157 int8_t motion_y;
00158 } Vp3Fragment;
00159
00160 #define SB_NOT_CODED 0
00161 #define SB_PARTIALLY_CODED 1
00162 #define SB_FULLY_CODED 2
00163
00164 #define MODE_INTER_NO_MV 0
00165 #define MODE_INTRA 1
00166 #define MODE_INTER_PLUS_MV 2
00167 #define MODE_INTER_LAST_MV 3
00168 #define MODE_INTER_PRIOR_LAST 4
00169 #define MODE_USING_GOLDEN 5
00170 #define MODE_GOLDEN_MV 6
00171 #define MODE_INTER_FOURMV 7
00172 #define CODING_MODE_COUNT 8
00173
00174
00175 #define MODE_COPY 8
00176
00177
00178 static int ModeAlphabet[7][CODING_MODE_COUNT] =
00179 {
00180
00181 { 0, 0, 0, 0, 0, 0, 0, 0 },
00182
00183
00184 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00185 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
00186 MODE_INTRA, MODE_USING_GOLDEN,
00187 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00188
00189
00190 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00191 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
00192 MODE_INTRA, MODE_USING_GOLDEN,
00193 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00194
00195
00196 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00197 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
00198 MODE_INTRA, MODE_USING_GOLDEN,
00199 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00200
00201
00202 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00203 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
00204 MODE_INTRA, MODE_USING_GOLDEN,
00205 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00206
00207
00208 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
00209 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
00210 MODE_INTRA, MODE_USING_GOLDEN,
00211 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00212
00213
00214 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
00215 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00216 MODE_INTER_PLUS_MV, MODE_INTRA,
00217 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00218
00219 };
00220
00221 #define MIN_DEQUANT_VAL 2
00222
00223 typedef struct Vp3DecodeContext {
00224 AVCodecContext *avctx;
00225 int theora, theora_tables;
00226 int version;
00227 int width, height;
00228 AVFrame golden_frame;
00229 AVFrame last_frame;
00230 AVFrame current_frame;
00231 int keyframe;
00232 DSPContext dsp;
00233 int flipped_image;
00234
00235 int qis[3];
00236 int nqis;
00237 int quality_index;
00238 int last_quality_index;
00239
00240 int superblock_count;
00241 int superblock_width;
00242 int superblock_height;
00243 int y_superblock_width;
00244 int y_superblock_height;
00245 int c_superblock_width;
00246 int c_superblock_height;
00247 int u_superblock_start;
00248 int v_superblock_start;
00249 unsigned char *superblock_coding;
00250
00251 int macroblock_count;
00252 int macroblock_width;
00253 int macroblock_height;
00254
00255 int fragment_count;
00256 int fragment_width;
00257 int fragment_height;
00258
00259 Vp3Fragment *all_fragments;
00260 Coeff *coeffs;
00261 Coeff *next_coeff;
00262 int fragment_start[3];
00263
00264 ScanTable scantable;
00265
00266
00267 uint16_t coded_dc_scale_factor[64];
00268 uint32_t coded_ac_scale_factor[64];
00269 uint8_t base_matrix[384][64];
00270 uint8_t qr_count[2][3];
00271 uint8_t qr_size [2][3][64];
00272 uint16_t qr_base[2][3][64];
00273
00274
00275
00276 int *coded_fragment_list;
00277 int coded_fragment_list_index;
00278 int pixel_addresses_inited;
00279
00280 VLC dc_vlc[16];
00281 VLC ac_vlc_1[16];
00282 VLC ac_vlc_2[16];
00283 VLC ac_vlc_3[16];
00284 VLC ac_vlc_4[16];
00285
00286 VLC superblock_run_length_vlc;
00287 VLC fragment_run_length_vlc;
00288 VLC mode_code_vlc;
00289 VLC motion_vector_vlc;
00290
00291
00292
00293 DECLARE_ALIGNED_16(int16_t, qmat[2][4][64]);
00294
00295
00296
00297
00298
00299 int *superblock_fragments;
00300
00301
00302
00303
00304
00305 int *superblock_macroblocks;
00306
00307
00308
00309
00310 int *macroblock_fragments;
00311
00312
00313 unsigned char *macroblock_coding;
00314
00315 int first_coded_y_fragment;
00316 int first_coded_c_fragment;
00317 int last_coded_y_fragment;
00318 int last_coded_c_fragment;
00319
00320 uint8_t edge_emu_buffer[9*2048];
00321 int8_t qscale_table[2048];
00322
00323
00324 int hti;
00325 unsigned int hbits;
00326 int entries;
00327 int huff_code_size;
00328 uint16_t huffman_table[80][32][2];
00329
00330 uint32_t filter_limit_values[64];
00331 int bounding_values_array[256];
00332 } Vp3DecodeContext;
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_mb[4];
00349
00350 int current_fragment = 0;
00351 int current_width = 0;
00352 int current_height = 0;
00353 int right_edge = 0;
00354 int bottom_edge = 0;
00355 int superblock_row_inc = 0;
00356 int *hilbert = NULL;
00357 int mapping_index = 0;
00358
00359 int current_macroblock;
00360 int c_fragment;
00361
00362 signed char travel_width[16] = {
00363 1, 1, 0, -1,
00364 0, 0, 1, 0,
00365 1, 0, 1, 0,
00366 0, -1, 0, 1
00367 };
00368
00369 signed char travel_height[16] = {
00370 0, 0, 1, 0,
00371 1, 1, 0, -1,
00372 0, 1, 0, -1,
00373 -1, 0, -1, 0
00374 };
00375
00376 signed char travel_width_mb[4] = {
00377 1, 0, 1, 0
00378 };
00379
00380 signed char travel_height_mb[4] = {
00381 0, 1, 0, -1
00382 };
00383
00384 debug_vp3(" vp3: initialize block mapping tables\n");
00385
00386 hilbert_walk_mb[0] = 1;
00387 hilbert_walk_mb[1] = s->macroblock_width;
00388 hilbert_walk_mb[2] = 1;
00389 hilbert_walk_mb[3] = -s->macroblock_width;
00390
00391
00392 for (i = 0; i < s->superblock_count; i++) {
00393 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
00394 i, s->u_superblock_start, s->v_superblock_start);
00395
00396
00397 if (i == 0) {
00398
00399
00400 right_edge = s->fragment_width;
00401 bottom_edge = s->fragment_height;
00402 current_width = -1;
00403 current_height = 0;
00404 superblock_row_inc = 3 * s->fragment_width -
00405 (s->y_superblock_width * 4 - s->fragment_width);
00406
00407
00408 current_fragment = -1;
00409
00410 } else if (i == s->u_superblock_start) {
00411
00412
00413 right_edge = s->fragment_width / 2;
00414 bottom_edge = s->fragment_height / 2;
00415 current_width = -1;
00416 current_height = 0;
00417 superblock_row_inc = 3 * (s->fragment_width / 2) -
00418 (s->c_superblock_width * 4 - s->fragment_width / 2);
00419
00420
00421 current_fragment = s->fragment_start[1] - 1;
00422
00423 } else if (i == s->v_superblock_start) {
00424
00425
00426 right_edge = s->fragment_width / 2;
00427 bottom_edge = s->fragment_height / 2;
00428 current_width = -1;
00429 current_height = 0;
00430 superblock_row_inc = 3 * (s->fragment_width / 2) -
00431 (s->c_superblock_width * 4 - s->fragment_width / 2);
00432
00433
00434 current_fragment = s->fragment_start[2] - 1;
00435
00436 }
00437
00438 if (current_width >= right_edge - 1) {
00439
00440 current_width = -1;
00441 current_height += 4;
00442
00443
00444 current_fragment += superblock_row_inc;
00445 }
00446
00447
00448 for (j = 0; j < 16; j++) {
00449 current_fragment += travel_width[j] + right_edge * travel_height[j];
00450 current_width += travel_width[j];
00451 current_height += travel_height[j];
00452
00453
00454 if ((current_width < right_edge) &&
00455 (current_height < bottom_edge)) {
00456 s->superblock_fragments[mapping_index] = current_fragment;
00457 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
00458 s->superblock_fragments[mapping_index], i, j,
00459 current_width, right_edge, current_height, bottom_edge);
00460 } else {
00461 s->superblock_fragments[mapping_index] = -1;
00462 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
00463 i, j,
00464 current_width, right_edge, current_height, bottom_edge);
00465 }
00466
00467 mapping_index++;
00468 }
00469 }
00470
00471
00472
00473 right_edge = s->macroblock_width;
00474 bottom_edge = s->macroblock_height;
00475 current_width = -1;
00476 current_height = 0;
00477 superblock_row_inc = s->macroblock_width -
00478 (s->y_superblock_width * 2 - s->macroblock_width);;
00479 hilbert = hilbert_walk_mb;
00480 mapping_index = 0;
00481 current_macroblock = -1;
00482 for (i = 0; i < s->u_superblock_start; i++) {
00483
00484 if (current_width >= right_edge - 1) {
00485
00486 current_width = -1;
00487 current_height += 2;
00488
00489
00490 current_macroblock += superblock_row_inc;
00491 }
00492
00493
00494 for (j = 0; j < 4; j++) {
00495 current_macroblock += hilbert_walk_mb[j];
00496 current_width += travel_width_mb[j];
00497 current_height += travel_height_mb[j];
00498
00499
00500 if ((current_width < right_edge) &&
00501 (current_height < bottom_edge)) {
00502 s->superblock_macroblocks[mapping_index] = current_macroblock;
00503 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
00504 s->superblock_macroblocks[mapping_index], i, j,
00505 current_width, right_edge, current_height, bottom_edge);
00506 } else {
00507 s->superblock_macroblocks[mapping_index] = -1;
00508 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
00509 i, j,
00510 current_width, right_edge, current_height, bottom_edge);
00511 }
00512
00513 mapping_index++;
00514 }
00515 }
00516
00517
00518 current_fragment = 0;
00519 current_macroblock = 0;
00520 mapping_index = 0;
00521 for (i = 0; i < s->fragment_height; i += 2) {
00522
00523 for (j = 0; j < s->fragment_width; j += 2) {
00524
00525 debug_init(" macroblock %d contains fragments: ", current_macroblock);
00526 s->all_fragments[current_fragment].macroblock = current_macroblock;
00527 s->macroblock_fragments[mapping_index++] = current_fragment;
00528 debug_init("%d ", current_fragment);
00529
00530 if (j + 1 < s->fragment_width) {
00531 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
00532 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
00533 debug_init("%d ", current_fragment + 1);
00534 } else
00535 s->macroblock_fragments[mapping_index++] = -1;
00536
00537 if (i + 1 < s->fragment_height) {
00538 s->all_fragments[current_fragment + s->fragment_width].macroblock =
00539 current_macroblock;
00540 s->macroblock_fragments[mapping_index++] =
00541 current_fragment + s->fragment_width;
00542 debug_init("%d ", current_fragment + s->fragment_width);
00543 } else
00544 s->macroblock_fragments[mapping_index++] = -1;
00545
00546 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
00547 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
00548 current_macroblock;
00549 s->macroblock_fragments[mapping_index++] =
00550 current_fragment + s->fragment_width + 1;
00551 debug_init("%d ", current_fragment + s->fragment_width + 1);
00552 } else
00553 s->macroblock_fragments[mapping_index++] = -1;
00554
00555
00556 c_fragment = s->fragment_start[1] +
00557 (i * s->fragment_width / 4) + (j / 2);
00558 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
00559 s->macroblock_fragments[mapping_index++] = c_fragment;
00560 debug_init("%d ", c_fragment);
00561
00562 c_fragment = s->fragment_start[2] +
00563 (i * s->fragment_width / 4) + (j / 2);
00564 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
00565 s->macroblock_fragments[mapping_index++] = c_fragment;
00566 debug_init("%d ", c_fragment);
00567
00568 debug_init("\n");
00569
00570 if (j + 2 <= s->fragment_width)
00571 current_fragment += 2;
00572 else
00573 current_fragment++;
00574 current_macroblock++;
00575 }
00576
00577 current_fragment += s->fragment_width;
00578 }
00579
00580 return 0;
00581 }
00582
00583
00584
00585
00586 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
00587 {
00588 int i;
00589
00590
00591 s->coded_fragment_list_index = 0;
00592 for (i = 0; i < s->fragment_count; i++) {
00593 s->all_fragments[i].coeff_count = 0;
00594 s->all_fragments[i].motion_x = 127;
00595 s->all_fragments[i].motion_y = 127;
00596 s->all_fragments[i].next_coeff= NULL;
00597 s->coeffs[i].index=
00598 s->coeffs[i].coeff=0;
00599 s->coeffs[i].next= NULL;
00600 }
00601 }
00602
00603
00604
00605
00606
00607 static void init_dequantizer(Vp3DecodeContext *s)
00608 {
00609 int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index];
00610 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
00611 int i, plane, inter, qri, bmi, bmj, qistart;
00612
00613 debug_vp3(" vp3: initializing dequantization tables\n");
00614
00615 for(inter=0; inter<2; inter++){
00616 for(plane=0; plane<3; plane++){
00617 int sum=0;
00618 for(qri=0; qri<s->qr_count[inter][plane]; qri++){
00619 sum+= s->qr_size[inter][plane][qri];
00620 if(s->quality_index <= sum)
00621 break;
00622 }
00623 qistart= sum - s->qr_size[inter][plane][qri];
00624 bmi= s->qr_base[inter][plane][qri ];
00625 bmj= s->qr_base[inter][plane][qri+1];
00626 for(i=0; i<64; i++){
00627 int coeff= ( 2*(sum -s->quality_index)*s->base_matrix[bmi][i]
00628 - 2*(qistart-s->quality_index)*s->base_matrix[bmj][i]
00629 + s->qr_size[inter][plane][qri])
00630 / (2*s->qr_size[inter][plane][qri]);
00631
00632 int qmin= 8<<(inter + !i);
00633 int qscale= i ? ac_scale_factor : dc_scale_factor;
00634
00635 s->qmat[inter][plane][i]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
00636 }
00637 }
00638 }
00639
00640 memset(s->qscale_table, (FFMAX(s->qmat[0][0][1], s->qmat[0][1][1])+8)/16, 512);
00641 }
00642
00643
00644
00645
00646
00647 static void init_loop_filter(Vp3DecodeContext *s)
00648 {
00649 int *bounding_values= s->bounding_values_array+127;
00650 int filter_limit;
00651 int x;
00652
00653 filter_limit = s->filter_limit_values[s->quality_index];
00654
00655
00656 memset(s->bounding_values_array, 0, 256 * sizeof(int));
00657 for (x = 0; x < filter_limit; x++) {
00658 bounding_values[-x - filter_limit] = -filter_limit + x;
00659 bounding_values[-x] = -x;
00660 bounding_values[x] = x;
00661 bounding_values[x + filter_limit] = filter_limit - x;
00662 }
00663 }
00664
00665
00666
00667
00668
00669 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
00670 {
00671 int bit = 0;
00672 int current_superblock = 0;
00673 int current_run = 0;
00674 int decode_fully_flags = 0;
00675 int decode_partial_blocks = 0;
00676 int first_c_fragment_seen;
00677
00678 int i, j;
00679 int current_fragment;
00680
00681 debug_vp3(" vp3: unpacking superblock coding\n");
00682
00683 if (s->keyframe) {
00684
00685 debug_vp3(" keyframe-- all superblocks are fully coded\n");
00686 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
00687
00688 } else {
00689
00690
00691 bit = get_bits1(gb);
00692
00693
00694 bit ^= 1;
00695 while (current_superblock < s->superblock_count) {
00696 if (current_run-- == 0) {
00697 bit ^= 1;
00698 current_run = get_vlc2(gb,
00699 s->superblock_run_length_vlc.table, 6, 2);
00700 if (current_run == 33)
00701 current_run += get_bits(gb, 12);
00702 debug_block_coding(" setting superblocks %d..%d to %s\n",
00703 current_superblock,
00704 current_superblock + current_run - 1,
00705 (bit) ? "partially coded" : "not coded");
00706
00707
00708
00709 if (bit == 0) {
00710 decode_fully_flags = 1;
00711 } else {
00712
00713
00714
00715 decode_partial_blocks = 1;
00716 }
00717 }
00718 s->superblock_coding[current_superblock++] = bit;
00719 }
00720
00721
00722
00723 if (decode_fully_flags) {
00724
00725 current_superblock = 0;
00726 current_run = 0;
00727 bit = get_bits1(gb);
00728
00729
00730 bit ^= 1;
00731 while (current_superblock < s->superblock_count) {
00732
00733
00734 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
00735
00736 if (current_run-- == 0) {
00737 bit ^= 1;
00738 current_run = get_vlc2(gb,
00739 s->superblock_run_length_vlc.table, 6, 2);
00740 if (current_run == 33)
00741 current_run += get_bits(gb, 12);
00742 }
00743
00744 debug_block_coding(" setting superblock %d to %s\n",
00745 current_superblock,
00746 (bit) ? "fully coded" : "not coded");
00747 s->superblock_coding[current_superblock] = 2*bit;
00748 }
00749 current_superblock++;
00750 }
00751 }
00752
00753
00754
00755 if (decode_partial_blocks) {
00756
00757 current_run = 0;
00758 bit = get_bits1(gb);
00759
00760
00761 bit ^= 1;
00762 }
00763 }
00764
00765
00766
00767 s->coded_fragment_list_index = 0;
00768 s->next_coeff= s->coeffs + s->fragment_count;
00769 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
00770 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
00771 first_c_fragment_seen = 0;
00772 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
00773 for (i = 0; i < s->superblock_count; i++) {
00774
00775
00776 for (j = 0; j < 16; j++) {
00777
00778
00779 current_fragment = s->superblock_fragments[i * 16 + j];
00780 if (current_fragment >= s->fragment_count) {
00781 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
00782 current_fragment, s->fragment_count);
00783 return 1;
00784 }
00785 if (current_fragment != -1) {
00786 if (s->superblock_coding[i] == SB_NOT_CODED) {
00787
00788
00789 s->all_fragments[current_fragment].coding_method =
00790 MODE_COPY;
00791
00792 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
00793
00794
00795
00796 if (current_run-- == 0) {
00797 bit ^= 1;
00798 current_run = get_vlc2(gb,
00799 s->fragment_run_length_vlc.table, 5, 2);
00800 }
00801
00802 if (bit) {
00803
00804
00805 s->all_fragments[current_fragment].coding_method =
00806 MODE_INTER_NO_MV;
00807 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
00808 s->coded_fragment_list[s->coded_fragment_list_index] =
00809 current_fragment;
00810 if ((current_fragment >= s->fragment_start[1]) &&
00811 (s->last_coded_y_fragment == -1) &&
00812 (!first_c_fragment_seen)) {
00813 s->first_coded_c_fragment = s->coded_fragment_list_index;
00814 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
00815 first_c_fragment_seen = 1;
00816 }
00817 s->coded_fragment_list_index++;
00818 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
00819 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
00820 i, current_fragment);
00821 } else {
00822
00823 s->all_fragments[current_fragment].coding_method =
00824 MODE_COPY;
00825 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
00826 i, current_fragment);
00827 }
00828
00829 } else {
00830
00831
00832
00833 s->all_fragments[current_fragment].coding_method =
00834 MODE_INTER_NO_MV;
00835 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
00836 s->coded_fragment_list[s->coded_fragment_list_index] =
00837 current_fragment;
00838 if ((current_fragment >= s->fragment_start[1]) &&
00839 (s->last_coded_y_fragment == -1) &&
00840 (!first_c_fragment_seen)) {
00841 s->first_coded_c_fragment = s->coded_fragment_list_index;
00842 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
00843 first_c_fragment_seen = 1;
00844 }
00845 s->coded_fragment_list_index++;
00846 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
00847 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
00848 i, current_fragment);
00849 }
00850 }
00851 }
00852 }
00853
00854 if (!first_c_fragment_seen)
00855
00856 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
00857 else
00858
00859 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
00860
00861 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
00862 s->coded_fragment_list_index,
00863 s->first_coded_y_fragment,
00864 s->last_coded_y_fragment,
00865 s->first_coded_c_fragment,
00866 s->last_coded_c_fragment);
00867
00868 return 0;
00869 }
00870
00871
00872
00873
00874
00875 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
00876 {
00877 int i, j, k;
00878 int scheme;
00879 int current_macroblock;
00880 int current_fragment;
00881 int coding_mode;
00882
00883 debug_vp3(" vp3: unpacking encoding modes\n");
00884
00885 if (s->keyframe) {
00886 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
00887
00888 for (i = 0; i < s->fragment_count; i++)
00889 s->all_fragments[i].coding_method = MODE_INTRA;
00890
00891 } else {
00892
00893
00894 scheme = get_bits(gb, 3);
00895 debug_modes(" using mode alphabet %d\n", scheme);
00896
00897
00898 if (scheme == 0) {
00899 debug_modes(" custom mode alphabet ahead:\n");
00900 for (i = 0; i < 8; i++)
00901 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
00902 }
00903
00904 for (i = 0; i < 8; i++)
00905 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
00906 ModeAlphabet[scheme][i]);
00907
00908
00909
00910 for (i = 0; i < s->u_superblock_start; i++) {
00911
00912 for (j = 0; j < 4; j++) {
00913 current_macroblock = s->superblock_macroblocks[i * 4 + j];
00914 if ((current_macroblock == -1) ||
00915 (s->macroblock_coding[current_macroblock] == MODE_COPY))
00916 continue;
00917 if (current_macroblock >= s->macroblock_count) {
00918 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
00919 current_macroblock, s->macroblock_count);
00920 return 1;
00921 }
00922
00923
00924 if (scheme == 7)
00925 coding_mode = get_bits(gb, 3);
00926 else
00927 coding_mode = ModeAlphabet[scheme]
00928 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
00929
00930 s->macroblock_coding[current_macroblock] = coding_mode;
00931 for (k = 0; k < 6; k++) {
00932 current_fragment =
00933 s->macroblock_fragments[current_macroblock * 6 + k];
00934 if (current_fragment == -1)
00935 continue;
00936 if (current_fragment >= s->fragment_count) {
00937 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
00938 current_fragment, s->fragment_count);
00939 return 1;
00940 }
00941 if (s->all_fragments[current_fragment].coding_method !=
00942 MODE_COPY)
00943 s->all_fragments[current_fragment].coding_method =
00944 coding_mode;
00945 }
00946
00947 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
00948 s->macroblock_fragments[current_macroblock * 6], coding_mode);
00949 }
00950 }
00951 }
00952
00953 return 0;
00954 }
00955
00956
00957
00958
00959
00960 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
00961 {
00962 int i, j, k;
00963 int coding_mode;
00964 int motion_x[6];
00965 int motion_y[6];
00966 int last_motion_x = 0;
00967 int last_motion_y = 0;
00968 int prior_last_motion_x = 0;
00969 int prior_last_motion_y = 0;
00970 int current_macroblock;
00971 int current_fragment;
00972
00973 debug_vp3(" vp3: unpacking motion vectors\n");
00974 if (s->keyframe) {
00975
00976 debug_vp3(" keyframe-- there are no motion vectors\n");
00977
00978 } else {
00979
00980 memset(motion_x, 0, 6 * sizeof(int));
00981 memset(motion_y, 0, 6 * sizeof(int));
00982
00983
00984 coding_mode = get_bits1(gb);
00985 debug_vectors(" using %s scheme for unpacking motion vectors\n",
00986 (coding_mode == 0) ? "VLC" : "fixed-length");
00987
00988
00989
00990 for (i = 0; i < s->u_superblock_start; i++) {
00991
00992 for (j = 0; j < 4; j++) {
00993 current_macroblock = s->superblock_macroblocks[i * 4 + j];
00994 if ((current_macroblock == -1) ||
00995 (s->macroblock_coding[current_macroblock] == MODE_COPY))
00996 continue;
00997 if (current_macroblock >= s->macroblock_count) {
00998 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
00999 current_macroblock, s->macroblock_count);
01000 return 1;
01001 }
01002
01003 current_fragment = s->macroblock_fragments[current_macroblock * 6];
01004 if (current_fragment >= s->fragment_count) {
01005 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
01006 current_fragment, s->fragment_count);
01007 return 1;
01008 }
01009 switch (s->macroblock_coding[current_macroblock]) {
01010
01011 case MODE_INTER_PLUS_MV:
01012 case MODE_GOLDEN_MV:
01013
01014 if (coding_mode == 0) {
01015 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01016 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01017 } else {
01018 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
01019 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
01020 }
01021
01022 for (k = 1; k < 6; k++) {
01023 motion_x[k] = motion_x[0];
01024 motion_y[k] = motion_y[0];
01025 }
01026
01027
01028 if (s->macroblock_coding[current_macroblock] ==
01029 MODE_INTER_PLUS_MV) {
01030 prior_last_motion_x = last_motion_x;
01031 prior_last_motion_y = last_motion_y;
01032 last_motion_x = motion_x[0];
01033 last_motion_y = motion_y[0];
01034 }
01035 break;
01036
01037 case MODE_INTER_FOURMV:
01038
01039
01040 motion_x[4] = motion_y[4] = 0;
01041 for (k = 0; k < 4; k++) {
01042 if (coding_mode == 0) {
01043 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01044 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
01045 } else {
01046 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
01047 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
01048 }
01049 motion_x[4] += motion_x[k];
01050 motion_y[4] += motion_y[k];
01051 }
01052
01053 motion_x[5]=
01054 motion_x[4]= RSHIFT(motion_x[4], 2);
01055 motion_y[5]=
01056 motion_y[4]= RSHIFT(motion_y[4], 2);
01057
01058
01059
01060 prior_last_motion_x = last_motion_x;
01061 prior_last_motion_y = last_motion_y;
01062 last_motion_x = motion_x[3];
01063 last_motion_y = motion_y[3];
01064 break;
01065
01066 case MODE_INTER_LAST_MV:
01067
01068 motion_x[0] = last_motion_x;
01069 motion_y[0] = last_motion_y;
01070 for (k = 1; k < 6; k++) {
01071 motion_x[k] = motion_x[0];
01072 motion_y[k] = motion_y[0];
01073 }
01074
01075
01076
01077 break;
01078
01079 case MODE_INTER_PRIOR_LAST:
01080
01081
01082 motion_x[0] = prior_last_motion_x;
01083 motion_y[0] = prior_last_motion_y;
01084 for (k = 1; k < 6; k++) {
01085 motion_x[k] = motion_x[0];
01086 motion_y[k] = motion_y[0];
01087 }
01088
01089
01090 prior_last_motion_x = last_motion_x;
01091 prior_last_motion_y = last_motion_y;
01092 last_motion_x = motion_x[0];
01093 last_motion_y = motion_y[0];
01094 break;
01095
01096 default:
01097
01098 memset(motion_x, 0, 6 * sizeof(int));
01099 memset(motion_y, 0, 6 * sizeof(int));
01100
01101
01102 break;
01103 }
01104
01105
01106 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
01107 current_fragment,
01108 s->macroblock_coding[current_macroblock]);
01109 for (k = 0; k < 6; k++) {
01110 current_fragment =
01111 s->macroblock_fragments[current_macroblock * 6 + k];
01112 if (current_fragment == -1)
01113 continue;
01114 if (current_fragment >= s->fragment_count) {
01115 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
01116 current_fragment, s->fragment_count);
01117 return 1;
01118 }
01119 s->all_fragments[current_fragment].motion_x = motion_x[k];
01120 s->all_fragments[current_fragment].motion_y = motion_y[k];
01121 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
01122 k, current_fragment, motion_x[k], motion_y[k]);
01123 }
01124 }
01125 }
01126 }
01127
01128 return 0;
01129 }
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
01144 VLC *table, int coeff_index,
01145 int first_fragment, int last_fragment,
01146 int eob_run)
01147 {
01148 int i;
01149 int token;
01150 int zero_run = 0;
01151 DCTELEM coeff = 0;
01152 Vp3Fragment *fragment;
01153 uint8_t *perm= s->scantable.permutated;
01154 int bits_to_get;
01155
01156 if ((first_fragment >= s->fragment_count) ||
01157 (last_fragment >= s->fragment_count)) {
01158
01159 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
01160 first_fragment, last_fragment);
01161 return 0;
01162 }
01163
01164 for (i = first_fragment; i <= last_fragment; i++) {
01165
01166 fragment = &s->all_fragments[s->coded_fragment_list[i]];
01167 if (fragment->coeff_count > coeff_index)
01168 continue;
01169
01170 if (!eob_run) {
01171
01172 token = get_vlc2(gb, table->table, 5, 3);
01173 debug_vlc(" token = %2d, ", token);
01174
01175 if (token <= 6) {
01176 eob_run = eob_run_base[token];
01177 if (eob_run_get_bits[token])
01178 eob_run += get_bits(gb, eob_run_get_bits[token]);
01179 coeff = zero_run = 0;
01180 } else {
01181 bits_to_get = coeff_get_bits[token];
01182 if (!bits_to_get)
01183 coeff = coeff_tables[token][0];
01184 else
01185 coeff = coeff_tables[token][get_bits(gb, bits_to_get)];
01186
01187 zero_run = zero_run_base[token];
01188 if (zero_run_get_bits[token])
01189 zero_run += get_bits(gb, zero_run_get_bits[token]);
01190 }
01191 }
01192
01193 if (!eob_run) {
01194 fragment->coeff_count += zero_run;
01195 if (fragment->coeff_count < 64){
01196 fragment->next_coeff->coeff= coeff;
01197 fragment->next_coeff->index= perm[fragment->coeff_count++];
01198 fragment->next_coeff->next= s->next_coeff;
01199 s->next_coeff->next=NULL;
01200 fragment->next_coeff= s->next_coeff++;
01201 }
01202 debug_vlc(" fragment %d coeff = %d\n",
01203 s->coded_fragment_list[i], fragment->next_coeff[coeff_index]);
01204 } else {
01205 fragment->coeff_count |= 128;
01206 debug_vlc(" fragment %d eob with %d coefficients\n",
01207 s->coded_fragment_list[i], fragment->coeff_count&127);
01208 eob_run--;
01209 }
01210 }
01211
01212 return eob_run;
01213 }
01214
01215
01216
01217
01218
01219 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
01220 {
01221 int i;
01222 int dc_y_table;
01223 int dc_c_table;
01224 int ac_y_table;
01225 int ac_c_table;
01226 int residual_eob_run = 0;
01227
01228
01229 dc_y_table = get_bits(gb, 4);
01230 dc_c_table = get_bits(gb, 4);
01231
01232
01233 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
01234 dc_y_table);
01235 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
01236 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01237
01238
01239 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
01240 dc_c_table);
01241 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
01242 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01243
01244
01245 ac_y_table = get_bits(gb, 4);
01246 ac_c_table = get_bits(gb, 4);
01247
01248
01249 for (i = 1; i <= 5; i++) {
01250
01251 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01252 i, ac_y_table);
01253 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
01254 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01255
01256 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01257 i, ac_c_table);
01258 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
01259 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01260 }
01261
01262
01263 for (i = 6; i <= 14; i++) {
01264
01265 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01266 i, ac_y_table);
01267 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
01268 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01269
01270 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01271 i, ac_c_table);
01272 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
01273 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01274 }
01275
01276
01277 for (i = 15; i <= 27; i++) {
01278
01279 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01280 i, ac_y_table);
01281 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
01282 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01283
01284 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01285 i, ac_c_table);
01286 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
01287 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01288 }
01289
01290
01291 for (i = 28; i <= 63; i++) {
01292
01293 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
01294 i, ac_y_table);
01295 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
01296 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01297
01298 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
01299 i, ac_c_table);
01300 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
01301 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01302 }
01303
01304 return 0;
01305 }
01306
01307
01308
01309
01310
01311
01312 #define COMPATIBLE_FRAME(x) \
01313 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
01314 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
01315 #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
01316
01317 static void reverse_dc_prediction(Vp3DecodeContext *s,
01318 int first_fragment,
01319 int fragment_width,
01320 int fragment_height)
01321 {
01322
01323 #define PUL 8
01324 #define PU 4
01325 #define PUR 2
01326 #define PL 1
01327
01328 int x, y;
01329 int i = first_fragment;
01330
01331 int predicted_dc;
01332
01333
01334 int vl, vul, vu, vur;
01335
01336
01337 int l, ul, u, ur;
01338
01339
01340
01341
01342
01343
01344
01345
01346 int predictor_transform[16][4] = {
01347 { 0, 0, 0, 0},
01348 { 0, 0, 0,128},
01349 { 0, 0,128, 0},
01350 { 0, 0, 53, 75},
01351 { 0,128, 0, 0},
01352 { 0, 64, 0, 64},
01353 { 0,128, 0, 0},
01354 { 0, 0, 53, 75},
01355 {128, 0, 0, 0},
01356 { 0, 0, 0,128},
01357 { 64, 0, 64, 0},
01358 { 0, 0, 53, 75},
01359 { 0,128, 0, 0},
01360 {-104,116, 0,116},
01361 { 24, 80, 24, 0},
01362 {-104,116, 0,116}
01363 };
01364
01365
01366
01367
01368
01369
01370
01371 unsigned char compatible_frame[8] = {
01372 1,
01373 0,
01374 1,
01375 1,
01376 1,
01377 2,
01378 2,
01379 1
01380 };
01381 int current_frame_type;
01382
01383
01384 short last_dc[3];
01385
01386 int transform = 0;
01387
01388 debug_vp3(" vp3: reversing DC prediction\n");
01389
01390 vul = vu = vur = vl = 0;
01391 last_dc[0] = last_dc[1] = last_dc[2] = 0;
01392
01393
01394 for (y = 0; y < fragment_height; y++) {
01395
01396
01397 for (x = 0; x < fragment_width; x++, i++) {
01398
01399
01400 if (s->all_fragments[i].coding_method != MODE_COPY) {
01401
01402 current_frame_type =
01403 compatible_frame[s->all_fragments[i].coding_method];
01404 debug_dc_pred(" frag %d: orig DC = %d, ",
01405 i, DC_COEFF(i));
01406
01407 transform= 0;
01408 if(x){
01409 l= i-1;
01410 vl = DC_COEFF(l);
01411 if(FRAME_CODED(l) && COMPATIBLE_FRAME(l))
01412 transform |= PL;
01413 }
01414 if(y){
01415 u= i-fragment_width;
01416 vu = DC_COEFF(u);
01417 if(FRAME_CODED(u) && COMPATIBLE_FRAME(u))
01418 transform |= PU;
01419 if(x){
01420 ul= i-fragment_width-1;
01421 vul = DC_COEFF(ul);
01422 if(FRAME_CODED(ul) && COMPATIBLE_FRAME(ul))
01423 transform |= PUL;
01424 }
01425 if(x + 1 < fragment_width){
01426 ur= i-fragment_width+1;
01427 vur = DC_COEFF(ur);
01428 if(FRAME_CODED(ur) && COMPATIBLE_FRAME(ur))
01429 transform |= PUR;
01430 }
01431 }
01432
01433 debug_dc_pred("transform = %d, ", transform);
01434
01435 if (transform == 0) {
01436
01437
01438
01439 predicted_dc = last_dc[current_frame_type];
01440 debug_dc_pred("from last DC (%d) = %d\n",
01441 current_frame_type, DC_COEFF(i));
01442
01443 } else {
01444
01445
01446 predicted_dc =
01447 (predictor_transform[transform][0] * vul) +
01448 (predictor_transform[transform][1] * vu) +
01449 (predictor_transform[transform][2] * vur) +
01450 (predictor_transform[transform][3] * vl);
01451
01452 predicted_dc /= 128;
01453
01454
01455
01456 if ((transform == 13) || (transform == 15)) {
01457 if (FFABS(predicted_dc - vu) > 128)
01458 predicted_dc = vu;
01459 else if (FFABS(predicted_dc - vl) > 128)
01460 predicted_dc = vl;
01461 else if (FFABS(predicted_dc - vul) > 128)
01462 predicted_dc = vul;
01463 }
01464
01465 debug_dc_pred("from pred DC = %d\n",
01466 DC_COEFF(i));
01467 }
01468
01469
01470 if(s->coeffs[i].index){
01471 *s->next_coeff= s->coeffs[i];
01472 s->coeffs[i].index=0;
01473 s->coeffs[i].coeff=0;
01474 s->coeffs[i].next= s->next_coeff++;
01475 }
01476 s->coeffs[i].coeff += predicted_dc;
01477
01478 last_dc[current_frame_type] = DC_COEFF(i);
01479 if(DC_COEFF(i) && !(s->all_fragments[i].coeff_count&127)){
01480 s->all_fragments[i].coeff_count= 129;
01481
01482 s->coeffs[i].next= s->next_coeff;
01483 (s->next_coeff++)->next=NULL;
01484 }
01485 }
01486 }
01487 }
01488 }
01489
01490
01491 static void horizontal_filter(unsigned char *first_pixel, int stride,
01492 int *bounding_values);
01493 static void vertical_filter(unsigned char *first_pixel, int stride,
01494 int *bounding_values);
01495
01496
01497
01498
01499
01500 static void render_slice(Vp3DecodeContext *s, int slice)
01501 {
01502 int x;
01503 int m, n;
01504 int16_t *dequantizer;
01505 DECLARE_ALIGNED_16(DCTELEM, block[64]);
01506 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
01507 int motion_halfpel_index;
01508 uint8_t *motion_source;
01509 int plane;
01510 int current_macroblock_entry = slice * s->macroblock_width * 6;
01511
01512 if (slice >= s->macroblock_height)
01513 return;
01514
01515 for (plane = 0; plane < 3; plane++) {
01516 uint8_t *output_plane = s->current_frame.data [plane];
01517 uint8_t * last_plane = s-> last_frame.data [plane];
01518 uint8_t *golden_plane = s-> golden_frame.data [plane];
01519 int stride = s->current_frame.linesize[plane];
01520 int plane_width = s->width >> !!plane;
01521 int plane_height = s->height >> !!plane;
01522 int y = slice * FRAGMENT_PIXELS << !plane ;
01523 int slice_height = y + (FRAGMENT_PIXELS << !plane);
01524 int i = s->macroblock_fragments[current_macroblock_entry + plane + 3*!!plane];
01525
01526 if (!s->flipped_image) stride = -stride;
01527
01528
01529 if(FFABS(stride) > 2048)
01530 return;
01531
01532
01533 for (; y < slice_height; y += 8) {
01534
01535
01536 for (x = 0; x < plane_width; x += 8, i++) {
01537
01538 if ((i < 0) || (i >= s->fragment_count)) {
01539 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
01540 return;
01541 }
01542
01543
01544 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
01545 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
01546
01547 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
01548 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
01549 motion_source= golden_plane;
01550 else
01551 motion_source= last_plane;
01552
01553 motion_source += s->all_fragments[i].first_pixel;
01554 motion_halfpel_index = 0;
01555
01556
01557
01558 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
01559 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
01560 int src_x, src_y;
01561 motion_x = s->all_fragments[i].motion_x;
01562 motion_y = s->all_fragments[i].motion_y;
01563 if(plane){
01564 motion_x= (motion_x>>1) | (motion_x&1);
01565 motion_y= (motion_y>>1) | (motion_y&1);
01566 }
01567
01568 src_x= (motion_x>>1) + x;
01569 src_y= (motion_y>>1) + y;
01570 if ((motion_x == 127) || (motion_y == 127))
01571 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
01572
01573 motion_halfpel_index = motion_x & 0x01;
01574 motion_source += (motion_x >> 1);
01575
01576 motion_halfpel_index |= (motion_y & 0x01) << 1;
01577 motion_source += ((motion_y >> 1) * stride);
01578
01579 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
01580 uint8_t *temp= s->edge_emu_buffer;
01581 if(stride<0) temp -= 9*stride;
01582 else temp += 9*stride;
01583
01584 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
01585 motion_source= temp;
01586 }
01587 }
01588
01589
01590
01591
01592 if (s->all_fragments[i].coding_method != MODE_INTRA) {
01593
01594
01595
01596
01597 if(motion_halfpel_index != 3){
01598 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
01599 output_plane + s->all_fragments[i].first_pixel,
01600 motion_source, stride, 8);
01601 }else{
01602 int d= (motion_x ^ motion_y)>>31;
01603 s->dsp.put_no_rnd_pixels_l2[1](
01604 output_plane + s->all_fragments[i].first_pixel,
01605 motion_source - d,
01606 motion_source + stride + 1 + d,
01607 stride, 8);
01608 }
01609 dequantizer = s->qmat[1][plane];
01610 }else{
01611 dequantizer = s->qmat[0][plane];
01612 }
01613
01614
01615 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
01616 i, s->all_fragments[i].coding_method,
01617 DC_COEFF(i), dequantizer[0]);
01618
01619 if(s->avctx->idct_algo==FF_IDCT_VP3){
01620 Coeff *coeff= s->coeffs + i;
01621 memset(block, 0, sizeof(block));
01622 while(coeff->next){
01623 block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
01624 coeff= coeff->next;
01625 }
01626 }else{
01627 Coeff *coeff= s->coeffs + i;
01628 memset(block, 0, sizeof(block));
01629 while(coeff->next){
01630 block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
01631 coeff= coeff->next;
01632 }
01633 }
01634
01635
01636
01637 if (s->all_fragments[i].coding_method == MODE_INTRA) {
01638 if(s->avctx->idct_algo!=FF_IDCT_VP3)
01639 block[0] += 128<<3;
01640 s->dsp.idct_put(
01641 output_plane + s->all_fragments[i].first_pixel,
01642 stride,
01643 block);
01644 } else {
01645 s->dsp.idct_add(
01646 output_plane + s->all_fragments[i].first_pixel,
01647 stride,
01648 block);
01649 }
01650
01651 debug_idct("block after idct_%s():\n",
01652 (s->all_fragments[i].coding_method == MODE_INTRA)?
01653 "put" : "add");
01654 for (m = 0; m < 8; m++) {
01655 for (n = 0; n < 8; n++) {
01656 debug_idct(" %3d", *(output_plane +
01657 s->all_fragments[i].first_pixel + (m * stride + n)));
01658 }
01659 debug_idct("\n");
01660 }
01661 debug_idct("\n");
01662
01663 } else {
01664
01665
01666 s->dsp.put_pixels_tab[1][0](
01667 output_plane + s->all_fragments[i].first_pixel,
01668 last_plane + s->all_fragments[i].first_pixel,
01669 stride, 8);
01670
01671 }
01672 #if 0
01673
01674
01675
01676
01677
01678
01679
01680 if ((x > 0) &&
01681 ((s->all_fragments[i].coding_method != MODE_COPY) ||
01682 ((s->all_fragments[i].coding_method == MODE_COPY) &&
01683 (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
01684 horizontal_filter(
01685 output_plane + s->all_fragments[i].first_pixel + 7*stride,
01686 -stride, s->bounding_values_array + 127);
01687 }
01688
01689
01690
01691
01692
01693
01694
01695
01696 if ((y > 0) &&
01697 ((s->all_fragments[i].coding_method != MODE_COPY) ||
01698 ((s->all_fragments[i].coding_method == MODE_COPY) &&
01699 (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
01700 vertical_filter(
01701 output_plane + s->all_fragments[i].first_pixel - stride,
01702 -stride, s->bounding_values_array + 127);
01703 }
01704 #endif
01705 }
01706 }
01707 }
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717 emms_c();
01718 }
01719
01720 static void horizontal_filter(unsigned char *first_pixel, int stride,
01721 int *bounding_values)
01722 {
01723 unsigned char *end;
01724 int filter_value;
01725
01726 for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
01727 filter_value =
01728 (first_pixel[-2] - first_pixel[ 1])
01729 +3*(first_pixel[ 0] - first_pixel[-1]);
01730 filter_value = bounding_values[(filter_value + 4) >> 3];
01731 first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
01732 first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
01733 }
01734 }
01735
01736 static void vertical_filter(unsigned char *first_pixel, int stride,
01737 int *bounding_values)
01738 {
01739 unsigned char *end;
01740 int filter_value;
01741 const int nstride= -stride;
01742
01743 for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
01744 filter_value =
01745 (first_pixel[2 * nstride] - first_pixel[ stride])
01746 +3*(first_pixel[0 ] - first_pixel[nstride]);
01747 filter_value = bounding_values[(filter_value + 4) >> 3];
01748 first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
01749 first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
01750 }
01751 }
01752
01753 static void apply_loop_filter(Vp3DecodeContext *s)
01754 {
01755 int plane;
01756 int x, y;
01757 int *bounding_values= s->bounding_values_array+127;
01758
01759 #if 0
01760 int bounding_values_array[256];
01761 int filter_limit;
01762
01763
01764 for (x = 63; x >= 0; x--) {
01765 if (vp31_ac_scale_factor[x] >= s->quality_index)
01766 break;
01767 }
01768 filter_limit = vp31_filter_limit_values[s->quality_index];
01769
01770
01771 memset(bounding_values_array, 0, 256 * sizeof(int));
01772 for (x = 0; x < filter_limit; x++) {
01773 bounding_values[-x - filter_limit] = -filter_limit + x;
01774 bounding_values[-x] = -x;
01775 bounding_values[x] = x;
01776 bounding_values[x + filter_limit] = filter_limit - x;
01777 }
01778 #endif
01779
01780 for (plane = 0; plane < 3; plane++) {
01781 int width = s->fragment_width >> !!plane;
01782 int height = s->fragment_height >> !!plane;
01783 int fragment = s->fragment_start [plane];
01784 int stride = s->current_frame.linesize[plane];
01785 uint8_t *plane_data = s->current_frame.data [plane];
01786 if (!s->flipped_image) stride = -stride;
01787
01788 for (y = 0; y < height; y++) {
01789
01790 for (x = 0; x < width; x++) {
01791 START_TIMER
01792
01793 if ((x > 0) &&
01794 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
01795 horizontal_filter(
01796 plane_data + s->all_fragments[fragment].first_pixel,
01797 stride, bounding_values);
01798 }
01799
01800
01801 if ((y > 0) &&
01802 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
01803 vertical_filter(
01804 plane_data + s->all_fragments[fragment].first_pixel,
01805 stride, bounding_values);
01806 }
01807
01808
01809
01810
01811 if ((x < width - 1) &&
01812 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
01813 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
01814 horizontal_filter(
01815 plane_data + s->all_fragments[fragment + 1].first_pixel,
01816 stride, bounding_values);
01817 }
01818
01819
01820
01821
01822 if ((y < height - 1) &&
01823 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
01824 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
01825 vertical_filter(
01826 plane_data + s->all_fragments[fragment + width].first_pixel,
01827 stride, bounding_values);
01828 }
01829
01830 fragment++;
01831 STOP_TIMER("loop filter")
01832 }
01833 }
01834 }
01835 }
01836
01837
01838
01839
01840
01841
01842 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
01843 {
01844
01845 int i, x, y;
01846
01847
01848
01849 i = 0;
01850 for (y = s->fragment_height; y > 0; y--) {
01851 for (x = 0; x < s->fragment_width; x++) {
01852 s->all_fragments[i++].first_pixel =
01853 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
01854 s->golden_frame.linesize[0] +
01855 x * FRAGMENT_PIXELS;
01856 debug_init(" fragment %d, first pixel @ %d\n",
01857 i-1, s->all_fragments[i-1].first_pixel);
01858 }
01859 }
01860
01861
01862 i = s->fragment_start[1];
01863 for (y = s->fragment_height / 2; y > 0; y--) {
01864 for (x = 0; x < s->fragment_width / 2; x++) {
01865 s->all_fragments[i++].first_pixel =
01866 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
01867 s->golden_frame.linesize[1] +
01868 x * FRAGMENT_PIXELS;
01869 debug_init(" fragment %d, first pixel @ %d\n",
01870 i-1, s->all_fragments[i-1].first_pixel);
01871 }
01872 }
01873
01874
01875 i = s->fragment_start[2];
01876 for (y = s->fragment_height / 2; y > 0; y--) {
01877 for (x = 0; x < s->fragment_width / 2; x++) {
01878 s->all_fragments[i++].first_pixel =
01879 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
01880 s->golden_frame.linesize[2] +
01881 x * FRAGMENT_PIXELS;
01882 debug_init(" fragment %d, first pixel @ %d\n",
01883 i-1, s->all_fragments[i-1].first_pixel);
01884 }
01885 }
01886 }
01887
01888
01889 static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
01890 {
01891
01892 int i, x, y;
01893
01894
01895
01896 i = 0;
01897 for (y = 1; y <= s->fragment_height; y++) {
01898 for (x = 0; x < s->fragment_width; x++) {
01899 s->all_fragments[i++].first_pixel =
01900 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
01901 s->golden_frame.linesize[0] +
01902 x * FRAGMENT_PIXELS;
01903 debug_init(" fragment %d, first pixel @ %d\n",
01904 i-1, s->all_fragments[i-1].first_pixel);
01905 }
01906 }
01907
01908
01909 i = s->fragment_start[1];
01910 for (y = 1; y <= s->fragment_height / 2; y++) {
01911 for (x = 0; x < s->fragment_width / 2; x++) {
01912 s->all_fragments[i++].first_pixel =
01913 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
01914 s->golden_frame.linesize[1] +
01915 x * FRAGMENT_PIXELS;
01916 debug_init(" fragment %d, first pixel @ %d\n",
01917 i-1, s->all_fragments[i-1].first_pixel);
01918 }
01919 }
01920
01921
01922 i = s->fragment_start[2];
01923 for (y = 1; y <= s->fragment_height / 2; y++) {
01924 for (x = 0; x < s->fragment_width / 2; x++) {
01925 s->all_fragments[i++].first_pixel =
01926 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
01927 s->golden_frame.linesize[2] +
01928 x * FRAGMENT_PIXELS;
01929 debug_init(" fragment %d, first pixel @ %d\n",
01930 i-1, s->all_fragments[i-1].first_pixel);
01931 }
01932 }
01933 }
01934
01935
01936
01937
01938 static int vp3_decode_init(AVCodecContext *avctx)
01939 {
01940 Vp3DecodeContext *s = avctx->priv_data;
01941 int i, inter, plane;
01942 int c_width;
01943 int c_height;
01944 int y_superblock_count;
01945 int c_superblock_count;
01946
01947 if (avctx->codec_tag == MKTAG('V','P','3','0'))
01948 s->version = 0;
01949 else
01950 s->version = 1;
01951
01952 s->avctx = avctx;
01953 s->width = (avctx->width + 15) & 0xFFFFFFF0;
01954 s->height = (avctx->height + 15) & 0xFFFFFFF0;
01955 avctx->pix_fmt = PIX_FMT_YUV420P;
01956 if(avctx->idct_algo==FF_IDCT_AUTO)
01957 avctx->idct_algo=FF_IDCT_VP3;
01958 dsputil_init(&s->dsp, avctx);
01959
01960 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
01961
01962
01963
01964 s->quality_index = -1;
01965
01966 s->y_superblock_width = (s->width + 31) / 32;
01967 s->y_superblock_height = (s->height + 31) / 32;
01968 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
01969
01970
01971 c_width = s->width / 2;
01972 c_height = s->height / 2;
01973 s->c_superblock_width = (c_width + 31) / 32;
01974 s->c_superblock_height = (c_height + 31) / 32;
01975 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
01976
01977 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
01978 s->u_superblock_start = y_superblock_count;
01979 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
01980 s->superblock_coding = av_malloc(s->superblock_count);
01981
01982 s->macroblock_width = (s->width + 15) / 16;
01983 s->macroblock_height = (s->height + 15) / 16;
01984 s->macroblock_count = s->macroblock_width * s->macroblock_height;
01985
01986 s->fragment_width = s->width / FRAGMENT_PIXELS;
01987 s->fragment_height = s->height / FRAGMENT_PIXELS;
01988
01989
01990 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
01991 s->fragment_start[1] = s->fragment_width * s->fragment_height;
01992 s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
01993
01994 debug_init(" Y plane: %d x %d\n", s->width, s->height);
01995 debug_init(" C plane: %d x %d\n", c_width, c_height);
01996 debug_init(" Y superblocks: %d x %d, %d total\n",
01997 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
01998 debug_init(" C superblocks: %d x %d, %d total\n",
01999 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
02000 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
02001 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
02002 debug_init(" macroblocks: %d x %d, %d total\n",
02003 s->macroblock_width, s->macroblock_height, s->macroblock_count);
02004 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
02005 s->fragment_count,
02006 s->fragment_width,
02007 s->fragment_height,
02008 s->fragment_start[1],
02009 s->fragment_start[2]);
02010
02011 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
02012 s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
02013 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
02014 s->pixel_addresses_inited = 0;
02015 if (!s->superblock_coding || !s->all_fragments ||
02016 !s->coeffs || !s->coded_fragment_list) {
02017 vp3_decode_end(avctx);
02018 return -1;
02019 }
02020
02021 if (!s->theora_tables)
02022 {
02023 for (i = 0; i < 64; i++) {
02024 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
02025 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
02026 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
02027 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
02028 s->base_matrix[2][i] = vp31_inter_dequant[i];
02029 s->filter_limit_values[i] = vp31_filter_limit_values[i];
02030 }
02031
02032 for(inter=0; inter<2; inter++){
02033 for(plane=0; plane<3; plane++){
02034 s->qr_count[inter][plane]= 1;
02035 s->qr_size [inter][plane][0]= 63;
02036 s->qr_base [inter][plane][0]=
02037 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
02038 }
02039 }
02040
02041
02042 for (i = 0; i < 16; i++) {
02043
02044
02045 init_vlc(&s->dc_vlc[i], 5, 32,
02046 &dc_bias[i][0][1], 4, 2,
02047 &dc_bias[i][0][0], 4, 2, 0);
02048
02049
02050 init_vlc(&s->ac_vlc_1[i], 5, 32,
02051 &ac_bias_0[i][0][1], 4, 2,
02052 &ac_bias_0[i][0][0], 4, 2, 0);
02053
02054
02055 init_vlc(&s->ac_vlc_2[i], 5, 32,
02056 &ac_bias_1[i][0][1], 4, 2,
02057 &ac_bias_1[i][0][0], 4, 2, 0);
02058
02059
02060 init_vlc(&s->ac_vlc_3[i], 5, 32,
02061 &ac_bias_2[i][0][1], 4, 2,
02062 &ac_bias_2[i][0][0], 4, 2, 0);
02063
02064
02065 init_vlc(&s->ac_vlc_4[i], 5, 32,
02066 &ac_bias_3[i][0][1], 4, 2,
02067 &ac_bias_3[i][0][0], 4, 2, 0);
02068 }
02069 } else {
02070 for (i = 0; i < 16; i++) {
02071
02072
02073 init_vlc(&s->dc_vlc[i], 5, 32,
02074 &s->huffman_table[i][0][1], 4, 2,
02075 &s->huffman_table[i][0][0], 4, 2, 0);
02076
02077
02078 init_vlc(&s->ac_vlc_1[i], 5, 32,
02079 &s->huffman_table[i+16][0][1], 4, 2,
02080 &s->huffman_table[i+16][0][0], 4, 2, 0);
02081
02082
02083 init_vlc(&s->ac_vlc_2[i], 5, 32,
02084 &s->huffman_table[i+16*2][0][1], 4, 2,
02085 &s->huffman_table[i+16*2][0][0], 4, 2, 0);
02086
02087
02088 init_vlc(&s->ac_vlc_3[i], 5, 32,
02089 &s->huffman_table[i+16*3][0][1], 4, 2,
02090 &s->huffman_table[i+16*3][0][0], 4, 2, 0);
02091
02092
02093 init_vlc(&s->ac_vlc_4[i], 5, 32,
02094 &s->huffman_table[i+16*4][0][1], 4, 2,
02095 &s->huffman_table[i+16*4][0][0], 4, 2, 0);
02096 }
02097 }
02098
02099 init_vlc(&s->superblock_run_length_vlc, 6, 34,
02100 &superblock_run_length_vlc_table[0][1], 4, 2,
02101 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
02102
02103 init_vlc(&s->fragment_run_length_vlc, 5, 30,
02104 &fragment_run_length_vlc_table[0][1], 4, 2,
02105 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
02106
02107 init_vlc(&s->mode_code_vlc, 3, 8,
02108 &mode_code_vlc_table[0][1], 2, 1,
02109 &mode_code_vlc_table[0][0], 2, 1, 0);
02110
02111 init_vlc(&s->motion_vector_vlc, 6, 63,
02112 &motion_vector_vlc_table[0][1], 2, 1,
02113 &motion_vector_vlc_table[0][0], 2, 1, 0);
02114
02115
02116 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
02117 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
02118 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
02119 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
02120 if (!s->superblock_fragments || !s->superblock_macroblocks ||
02121 !s->macroblock_fragments || !s->macroblock_coding) {
02122 vp3_decode_end(avctx);
02123 return -1;
02124 }
02125 init_block_mapping(s);
02126
02127 for (i = 0; i < 3; i++) {
02128 s->current_frame.data[i] = NULL;
02129 s->last_frame.data[i] = NULL;
02130 s->golden_frame.data[i] = NULL;
02131 }
02132
02133 return 0;
02134 }
02135
02136
02137
02138
02139 static int vp3_decode_frame(AVCodecContext *avctx,
02140 void *data, int *data_size,
02141 const uint8_t *buf, int buf_size)
02142 {
02143 Vp3DecodeContext *s = avctx->priv_data;
02144 GetBitContext gb;
02145 static int counter = 0;
02146 int i;
02147
02148 init_get_bits(&gb, buf, buf_size * 8);
02149
02150 if (s->theora && get_bits1(&gb))
02151 {
02152 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
02153 return -1;
02154 }
02155
02156 s->keyframe = !get_bits1(&gb);
02157 if (!s->theora)
02158 skip_bits(&gb, 1);
02159 s->last_quality_index = s->quality_index;
02160
02161 s->nqis=0;
02162 do{
02163 s->qis[s->nqis++]= get_bits(&gb, 6);
02164 } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
02165
02166 s->quality_index= s->qis[0];
02167
02168 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
02169 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
02170 s->keyframe?"key":"", counter, s->quality_index);
02171 counter++;
02172
02173 if (s->quality_index != s->last_quality_index) {
02174 init_dequantizer(s);
02175 init_loop_filter(s);
02176 }
02177
02178 if (s->keyframe) {
02179 if (!s->theora)
02180 {
02181 skip_bits(&gb, 4);
02182 skip_bits(&gb, 4);
02183 if (s->version)
02184 {
02185 s->version = get_bits(&gb, 5);
02186 if (counter == 1)
02187 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
02188 }
02189 }
02190 if (s->version || s->theora)
02191 {
02192 if (get_bits1(&gb))
02193 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
02194 skip_bits(&gb, 2);
02195 }
02196
02197 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
02198 if (s->golden_frame.data[0])
02199 avctx->release_buffer(avctx, &s->golden_frame);
02200 s->last_frame= s->golden_frame;
02201 } else {
02202 if (s->golden_frame.data[0])
02203 avctx->release_buffer(avctx, &s->golden_frame);
02204 if (s->last_frame.data[0])
02205 avctx->release_buffer(avctx, &s->last_frame);
02206 }
02207
02208 s->golden_frame.reference = 3;
02209 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
02210 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
02211 return -1;
02212 }
02213
02214
02215 s->current_frame= s->golden_frame;
02216
02217
02218 if (!s->pixel_addresses_inited)
02219 {
02220 if (!s->flipped_image)
02221 vp3_calculate_pixel_addresses(s);
02222 else
02223 theora_calculate_pixel_addresses(s);
02224 s->pixel_addresses_inited = 1;
02225 }
02226 } else {
02227
02228 s->current_frame.reference = 3;
02229 if (!s->pixel_addresses_inited) {
02230 av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
02231 return -1;
02232 }
02233 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
02234 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
02235 return -1;
02236 }
02237 }
02238
02239 s->current_frame.qscale_table= s->qscale_table;
02240 s->current_frame.qstride= 0;
02241
02242 {START_TIMER
02243 init_frame(s, &gb);
02244 STOP_TIMER("init_frame")}
02245
02246 #if KEYFRAMES_ONLY
02247 if (!s->keyframe) {
02248
02249 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
02250 s->current_frame.linesize[0] * s->height);
02251 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
02252 s->current_frame.linesize[1] * s->height / 2);
02253 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
02254 s->current_frame.linesize[2] * s->height / 2);
02255
02256 } else {
02257 #endif
02258
02259 {START_TIMER
02260 if (unpack_superblocks(s, &gb)){
02261 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
02262 return -1;
02263 }
02264 STOP_TIMER("unpack_superblocks")}
02265 {START_TIMER
02266 if (unpack_modes(s, &gb)){
02267 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
02268 return -1;
02269 }
02270 STOP_TIMER("unpack_modes")}
02271 {START_TIMER
02272 if (unpack_vectors(s, &gb)){
02273 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
02274 return -1;
02275 }
02276 STOP_TIMER("unpack_vectors")}
02277 {START_TIMER
02278 if (unpack_dct_coeffs(s, &gb)){
02279 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
02280 return -1;
02281 }
02282 STOP_TIMER("unpack_dct_coeffs")}
02283 {START_TIMER
02284
02285 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
02286 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
02287 reverse_dc_prediction(s, s->fragment_start[1],
02288 s->fragment_width / 2, s->fragment_height / 2);
02289 reverse_dc_prediction(s, s->fragment_start[2],
02290 s->fragment_width / 2, s->fragment_height / 2);
02291 }
02292 STOP_TIMER("reverse_dc_prediction")}
02293 {START_TIMER
02294
02295 for (i = 0; i < s->macroblock_height; i++)
02296 render_slice(s, i);
02297 STOP_TIMER("render_fragments")}
02298
02299 {START_TIMER
02300 apply_loop_filter(s);
02301 STOP_TIMER("apply_loop_filter")}
02302 #if KEYFRAMES_ONLY
02303 }
02304 #endif
02305
02306 *data_size=sizeof(AVFrame);
02307 *(AVFrame*)data= s->current_frame;
02308
02309
02310
02311 if ((s->last_frame.data[0]) &&
02312 (s->last_frame.data[0] != s->golden_frame.data[0]))
02313 avctx->release_buffer(avctx, &s->last_frame);
02314
02315
02316 s->last_frame= s->current_frame;
02317 s->current_frame.data[0]= NULL;
02318
02319 return buf_size;
02320 }
02321
02322
02323
02324
02325 static int vp3_decode_end(AVCodecContext *avctx)
02326 {
02327 Vp3DecodeContext *s = avctx->priv_data;
02328
02329 av_free(s->all_fragments);
02330 av_free(s->coeffs);
02331 av_free(s->coded_fragment_list);
02332 av_free(s->superblock_fragments);
02333 av_free(s->superblock_macroblocks);
02334 av_free(s->macroblock_fragments);
02335 av_free(s->macroblock_coding);
02336
02337
02338 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
02339 avctx->release_buffer(avctx, &s->golden_frame);
02340 if (s->last_frame.data[0])
02341 avctx->release_buffer(avctx, &s->last_frame);
02342
02343
02344
02345 return 0;
02346 }
02347
02348 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
02349 {
02350 Vp3DecodeContext *s = avctx->priv_data;
02351
02352 if (get_bits1(gb)) {
02353 int token;
02354 if (s->entries >= 32) {
02355 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
02356 return -1;
02357 }
02358 token = get_bits(gb, 5);
02359
02360 s->huffman_table[s->hti][token][0] = s->hbits;
02361 s->huffman_table[s->hti][token][1] = s->huff_code_size;
02362 s->entries++;
02363 }
02364 else {
02365 if (s->huff_code_size >= 32) {
02366 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
02367 return -1;
02368 }
02369 s->huff_code_size++;
02370 s->hbits <<= 1;
02371 if (read_huffman_tree(avctx, gb))
02372 return -1;
02373 s->hbits |= 1;
02374 if (read_huffman_tree(avctx, gb))
02375 return -1;
02376 s->hbits >>= 1;
02377 s->huff_code_size--;
02378 }
02379 return 0;
02380 }
02381
02382 #ifdef CONFIG_THEORA_DECODER
02383 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
02384 {
02385 Vp3DecodeContext *s = avctx->priv_data;
02386 int visible_width, visible_height;
02387
02388 s->theora = get_bits_long(gb, 24);
02389 av_log(avctx, AV_LOG_VERBOSE, "Theora bitstream version %X\n", s->theora);
02390
02391
02392
02393 if (s->theora < 0x030200)
02394 {
02395 s->flipped_image = 1;
02396 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
02397 }
02398
02399 s->width = get_bits(gb, 16) << 4;
02400 s->height = get_bits(gb, 16) << 4;
02401
02402 if(avcodec_check_dimensions(avctx, s->width, s->height)){
02403 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
02404 s->width= s->height= 0;
02405 return -1;
02406 }
02407
02408 if (s->theora >= 0x030400)
02409 {
02410 skip_bits(gb, 32);
02411
02412 skip_bits(gb, 32);
02413 skip_bits(gb, 4);
02414 skip_bits(gb, 32);
02415 }
02416
02417 visible_width = get_bits_long(gb, 24);
02418 visible_height = get_bits_long(gb, 24);
02419
02420 if (s->theora >= 0x030200) {
02421 skip_bits(gb, 8);
02422 skip_bits(gb, 8);
02423 }
02424
02425 skip_bits(gb, 32);
02426 skip_bits(gb, 32);
02427 skip_bits(gb, 24);
02428 skip_bits(gb, 24);
02429
02430 if (s->theora < 0x030200)
02431 skip_bits(gb, 5);
02432 skip_bits(gb, 8);
02433 if (s->theora >= 0x030400)
02434 skip_bits(gb, 2);
02435 skip_bits(gb, 24);
02436
02437 skip_bits(gb, 6);
02438
02439 if (s->theora >= 0x030200)
02440 {
02441 skip_bits(gb, 5);
02442
02443 if (s->theora < 0x030400)
02444 skip_bits(gb, 5);
02445 }
02446
02447
02448
02449 if ( visible_width <= s->width && visible_width > s->width-16
02450 && visible_height <= s->height && visible_height > s->height-16)
02451 avcodec_set_dimensions(avctx, visible_width, visible_height);
02452 else
02453 avcodec_set_dimensions(avctx, s->width, s->height);
02454
02455 return 0;
02456 }
02457
02458 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
02459 {
02460 Vp3DecodeContext *s = avctx->priv_data;
02461 int i, n, matrices, inter, plane;
02462
02463 if (s->theora >= 0x030200) {
02464 n = get_bits(gb, 3);
02465
02466 for (i = 0; i < 64; i++)
02467 s->filter_limit_values[i] = get_bits(gb, n);
02468 }
02469
02470 if (s->theora >= 0x030200)
02471 n = get_bits(gb, 4) + 1;
02472 else
02473 n = 16;
02474
02475 for (i = 0; i < 64; i++)
02476 s->coded_ac_scale_factor[i] = get_bits(gb, n);
02477
02478 if (s->theora >= 0x030200)
02479 n = get_bits(gb, 4) + 1;
02480 else
02481 n = 16;
02482
02483 for (i = 0; i < 64; i++)
02484 s->coded_dc_scale_factor[i] = get_bits(gb, n);
02485
02486 if (s->theora >= 0x030200)
02487 matrices = get_bits(gb, 9) + 1;
02488 else
02489 matrices = 3;
02490
02491 if(matrices > 384){
02492 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
02493 return -1;
02494 }
02495
02496 for(n=0; n<matrices; n++){
02497 for (i = 0; i < 64; i++)
02498 s->base_matrix[n][i]= get_bits(gb, 8);
02499 }
02500
02501 for (inter = 0; inter <= 1; inter++) {
02502 for (plane = 0; plane <= 2; plane++) {
02503 int newqr= 1;
02504 if (inter || plane > 0)
02505 newqr = get_bits1(gb);
02506 if (!newqr) {
02507 int qtj, plj;
02508 if(inter && get_bits1(gb)){
02509 qtj = 0;
02510 plj = plane;
02511 }else{
02512 qtj= (3*inter + plane - 1) / 3;
02513 plj= (plane + 2) % 3;
02514 }
02515 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
02516 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
02517 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
02518 } else {
02519 int qri= 0;
02520 int qi = 0;
02521
02522 for(;;){
02523 i= get_bits(gb, av_log2(matrices-1)+1);
02524 if(i>= matrices){
02525 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
02526 return -1;
02527 }
02528 s->qr_base[inter][plane][qri]= i;
02529 if(qi >= 63)
02530 break;
02531 i = get_bits(gb, av_log2(63-qi)+1) + 1;
02532 s->qr_size[inter][plane][qri++]= i;
02533 qi += i;
02534 }
02535
02536 if (qi > 63) {
02537 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
02538 return -1;
02539 }
02540 s->qr_count[inter][plane]= qri;
02541 }
02542 }
02543 }
02544
02545
02546 for (s->hti = 0; s->hti < 80; s->hti++) {
02547 s->entries = 0;
02548 s->huff_code_size = 1;
02549 if (!get_bits1(gb)) {
02550 s->hbits = 0;
02551 if(read_huffman_tree(avctx, gb))
02552 return -1;
02553 s->hbits = 1;
02554 if(read_huffman_tree(avctx, gb))
02555 return -1;
02556 }
02557 }
02558
02559 s->theora_tables = 1;
02560
02561 return 0;
02562 }
02563
02564 static int theora_decode_init(AVCodecContext *avctx)
02565 {
02566 Vp3DecodeContext *s = avctx->priv_data;
02567 GetBitContext gb;
02568 int ptype;
02569 uint8_t *header_start[3];
02570 int header_len[3];
02571 int i;
02572
02573 s->theora = 1;
02574
02575 if (!avctx->extradata_size)
02576 {
02577 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
02578 return -1;
02579 }
02580
02581 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
02582 42, header_start, header_len) < 0) {
02583 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
02584 return -1;
02585 }
02586
02587 for(i=0;i<3;i++) {
02588 init_get_bits(&gb, header_start[i], header_len[i] * 8);
02589
02590 ptype = get_bits(&gb, 8);
02591 debug_vp3("Theora headerpacket type: %x\n", ptype);
02592
02593 if (!(ptype & 0x80))
02594 {
02595 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
02596
02597 }
02598
02599
02600 skip_bits(&gb, 6*8);
02601
02602 switch(ptype)
02603 {
02604 case 0x80:
02605 theora_decode_header(avctx, &gb);
02606 break;
02607 case 0x81:
02608
02609
02610 break;
02611 case 0x82:
02612 if (theora_decode_tables(avctx, &gb))
02613 return -1;
02614 break;
02615 default:
02616 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
02617 break;
02618 }
02619 if(8*header_len[i] != get_bits_count(&gb))
02620 av_log(avctx, AV_LOG_ERROR, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
02621 if (s->theora < 0x030200)
02622 break;
02623 }
02624
02625 vp3_decode_init(avctx);
02626 return 0;
02627 }
02628
02629 AVCodec theora_decoder = {
02630 "theora",
02631 CODEC_TYPE_VIDEO,
02632 CODEC_ID_THEORA,
02633 sizeof(Vp3DecodeContext),
02634 theora_decode_init,
02635 NULL,
02636 vp3_decode_end,
02637 vp3_decode_frame,
02638 0,
02639 NULL
02640 };
02641 #endif
02642
02643 AVCodec vp3_decoder = {
02644 "vp3",
02645 CODEC_TYPE_VIDEO,
02646 CODEC_ID_VP3,
02647 sizeof(Vp3DecodeContext),
02648 vp3_decode_init,
02649 NULL,
02650 vp3_decode_end,
02651 vp3_decode_frame,
02652 0,
02653 NULL
02654 };