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