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
00036 #include "libavcore/imgutils.h"
00037 #include "avcodec.h"
00038 #include "dsputil.h"
00039 #include "get_bits.h"
00040
00041 #include "vp3data.h"
00042 #include "xiph.h"
00043
00044 #define FRAGMENT_PIXELS 8
00045
00046 static av_cold int vp3_decode_end(AVCodecContext *avctx);
00047
00048
00049 typedef struct Vp3Fragment {
00050 int16_t dc;
00051 uint8_t coding_method;
00052 uint8_t qpi;
00053 } Vp3Fragment;
00054
00055 #define SB_NOT_CODED 0
00056 #define SB_PARTIALLY_CODED 1
00057 #define SB_FULLY_CODED 2
00058
00059
00060
00061
00062 #define MAXIMUM_LONG_BIT_RUN 4129
00063
00064 #define MODE_INTER_NO_MV 0
00065 #define MODE_INTRA 1
00066 #define MODE_INTER_PLUS_MV 2
00067 #define MODE_INTER_LAST_MV 3
00068 #define MODE_INTER_PRIOR_LAST 4
00069 #define MODE_USING_GOLDEN 5
00070 #define MODE_GOLDEN_MV 6
00071 #define MODE_INTER_FOURMV 7
00072 #define CODING_MODE_COUNT 8
00073
00074
00075 #define MODE_COPY 8
00076
00077
00078 static const int ModeAlphabet[6][CODING_MODE_COUNT] =
00079 {
00080
00081 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00082 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
00083 MODE_INTRA, MODE_USING_GOLDEN,
00084 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00085
00086
00087 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00088 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
00089 MODE_INTRA, MODE_USING_GOLDEN,
00090 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00091
00092
00093 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00094 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
00095 MODE_INTRA, MODE_USING_GOLDEN,
00096 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00097
00098
00099 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00100 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
00101 MODE_INTRA, MODE_USING_GOLDEN,
00102 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00103
00104
00105 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
00106 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
00107 MODE_INTRA, MODE_USING_GOLDEN,
00108 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00109
00110
00111 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
00112 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00113 MODE_INTER_PLUS_MV, MODE_INTRA,
00114 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00115
00116 };
00117
00118 static const uint8_t hilbert_offset[16][2] = {
00119 {0,0}, {1,0}, {1,1}, {0,1},
00120 {0,2}, {0,3}, {1,3}, {1,2},
00121 {2,2}, {2,3}, {3,3}, {3,2},
00122 {3,1}, {2,1}, {2,0}, {3,0}
00123 };
00124
00125 #define MIN_DEQUANT_VAL 2
00126
00127 typedef struct Vp3DecodeContext {
00128 AVCodecContext *avctx;
00129 int theora, theora_tables;
00130 int version;
00131 int width, height;
00132 int chroma_x_shift, chroma_y_shift;
00133 AVFrame golden_frame;
00134 AVFrame last_frame;
00135 AVFrame current_frame;
00136 int keyframe;
00137 DSPContext dsp;
00138 int flipped_image;
00139 int last_slice_end;
00140 int skip_loop_filter;
00141
00142 int qps[3];
00143 int nqps;
00144 int last_qps[3];
00145
00146 int superblock_count;
00147 int y_superblock_width;
00148 int y_superblock_height;
00149 int y_superblock_count;
00150 int c_superblock_width;
00151 int c_superblock_height;
00152 int c_superblock_count;
00153 int u_superblock_start;
00154 int v_superblock_start;
00155 unsigned char *superblock_coding;
00156
00157 int macroblock_count;
00158 int macroblock_width;
00159 int macroblock_height;
00160
00161 int fragment_count;
00162 int fragment_width[2];
00163 int fragment_height[2];
00164
00165 Vp3Fragment *all_fragments;
00166 int fragment_start[3];
00167 int data_offset[3];
00168
00169 int8_t (*motion_val[2])[2];
00170
00171 ScanTable scantable;
00172
00173
00174 uint16_t coded_dc_scale_factor[64];
00175 uint32_t coded_ac_scale_factor[64];
00176 uint8_t base_matrix[384][64];
00177 uint8_t qr_count[2][3];
00178 uint8_t qr_size [2][3][64];
00179 uint16_t qr_base[2][3][64];
00180
00198 int16_t *dct_tokens[3][64];
00199 int16_t *dct_tokens_base;
00200 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
00201 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
00202 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
00203
00207 int num_coded_frags[3][64];
00208 int total_num_coded_frags;
00209
00210
00211
00212 int *coded_fragment_list[3];
00213
00214 VLC dc_vlc[16];
00215 VLC ac_vlc_1[16];
00216 VLC ac_vlc_2[16];
00217 VLC ac_vlc_3[16];
00218 VLC ac_vlc_4[16];
00219
00220 VLC superblock_run_length_vlc;
00221 VLC fragment_run_length_vlc;
00222 VLC mode_code_vlc;
00223 VLC motion_vector_vlc;
00224
00225
00226
00227 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];
00228
00229
00230
00231
00232
00233 int *superblock_fragments;
00234
00235
00236
00237 unsigned char *macroblock_coding;
00238
00239 uint8_t edge_emu_buffer[9*2048];
00240 int8_t qscale_table[2048];
00241
00242
00243 int hti;
00244 unsigned int hbits;
00245 int entries;
00246 int huff_code_size;
00247 uint32_t huffman_table[80][32][2];
00248
00249 uint8_t filter_limit_values[64];
00250 DECLARE_ALIGNED(8, int, bounding_values_array)[256+2];
00251 } Vp3DecodeContext;
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 static int init_block_mapping(Vp3DecodeContext *s)
00265 {
00266 int sb_x, sb_y, plane;
00267 int x, y, i, j = 0;
00268
00269 for (plane = 0; plane < 3; plane++) {
00270 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
00271 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
00272 int frag_width = s->fragment_width[!!plane];
00273 int frag_height = s->fragment_height[!!plane];
00274
00275 for (sb_y = 0; sb_y < sb_height; sb_y++)
00276 for (sb_x = 0; sb_x < sb_width; sb_x++)
00277 for (i = 0; i < 16; i++) {
00278 x = 4*sb_x + hilbert_offset[i][0];
00279 y = 4*sb_y + hilbert_offset[i][1];
00280
00281 if (x < frag_width && y < frag_height)
00282 s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x;
00283 else
00284 s->superblock_fragments[j++] = -1;
00285 }
00286 }
00287
00288 return 0;
00289 }
00290
00291
00292
00293
00294
00295 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
00296 {
00297 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
00298 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
00299 int i, plane, inter, qri, bmi, bmj, qistart;
00300
00301 for(inter=0; inter<2; inter++){
00302 for(plane=0; plane<3; plane++){
00303 int sum=0;
00304 for(qri=0; qri<s->qr_count[inter][plane]; qri++){
00305 sum+= s->qr_size[inter][plane][qri];
00306 if(s->qps[qpi] <= sum)
00307 break;
00308 }
00309 qistart= sum - s->qr_size[inter][plane][qri];
00310 bmi= s->qr_base[inter][plane][qri ];
00311 bmj= s->qr_base[inter][plane][qri+1];
00312 for(i=0; i<64; i++){
00313 int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
00314 - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
00315 + s->qr_size[inter][plane][qri])
00316 / (2*s->qr_size[inter][plane][qri]);
00317
00318 int qmin= 8<<(inter + !i);
00319 int qscale= i ? ac_scale_factor : dc_scale_factor;
00320
00321 s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
00322 }
00323
00324 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
00325 }
00326 }
00327
00328 memset(s->qscale_table, (FFMAX(s->qmat[0][0][0][1], s->qmat[0][0][1][1])+8)/16, 512);
00329 }
00330
00331
00332
00333
00334
00335
00336
00337 static void init_loop_filter(Vp3DecodeContext *s)
00338 {
00339 int *bounding_values= s->bounding_values_array+127;
00340 int filter_limit;
00341 int x;
00342 int value;
00343
00344 filter_limit = s->filter_limit_values[s->qps[0]];
00345
00346
00347 memset(s->bounding_values_array, 0, 256 * sizeof(int));
00348 for (x = 0; x < filter_limit; x++) {
00349 bounding_values[-x] = -x;
00350 bounding_values[x] = x;
00351 }
00352 for (x = value = filter_limit; x < 128 && value; x++, value--) {
00353 bounding_values[ x] = value;
00354 bounding_values[-x] = -value;
00355 }
00356 if (value)
00357 bounding_values[128] = value;
00358 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
00359 }
00360
00361
00362
00363
00364
00365 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
00366 {
00367 int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start };
00368 int bit = 0;
00369 int current_superblock = 0;
00370 int current_run = 0;
00371 int num_partial_superblocks = 0;
00372
00373 int i, j;
00374 int current_fragment;
00375 int plane;
00376
00377 if (s->keyframe) {
00378 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
00379
00380 } else {
00381
00382
00383 bit = get_bits1(gb) ^ 1;
00384 current_run = 0;
00385
00386 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
00387 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
00388 bit = get_bits1(gb);
00389 else
00390 bit ^= 1;
00391
00392 current_run = get_vlc2(gb,
00393 s->superblock_run_length_vlc.table, 6, 2) + 1;
00394 if (current_run == 34)
00395 current_run += get_bits(gb, 12);
00396
00397 if (current_superblock + current_run > s->superblock_count) {
00398 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
00399 return -1;
00400 }
00401
00402 memset(s->superblock_coding + current_superblock, bit, current_run);
00403
00404 current_superblock += current_run;
00405 if (bit)
00406 num_partial_superblocks += current_run;
00407 }
00408
00409
00410
00411 if (num_partial_superblocks < s->superblock_count) {
00412 int superblocks_decoded = 0;
00413
00414 current_superblock = 0;
00415 bit = get_bits1(gb) ^ 1;
00416 current_run = 0;
00417
00418 while (superblocks_decoded < s->superblock_count - num_partial_superblocks
00419 && get_bits_left(gb) > 0) {
00420
00421 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
00422 bit = get_bits1(gb);
00423 else
00424 bit ^= 1;
00425
00426 current_run = get_vlc2(gb,
00427 s->superblock_run_length_vlc.table, 6, 2) + 1;
00428 if (current_run == 34)
00429 current_run += get_bits(gb, 12);
00430
00431 for (j = 0; j < current_run; current_superblock++) {
00432 if (current_superblock >= s->superblock_count) {
00433 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n");
00434 return -1;
00435 }
00436
00437
00438 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
00439 s->superblock_coding[current_superblock] = 2*bit;
00440 j++;
00441 }
00442 }
00443 superblocks_decoded += current_run;
00444 }
00445 }
00446
00447
00448
00449 if (num_partial_superblocks) {
00450
00451 current_run = 0;
00452 bit = get_bits1(gb);
00453
00454
00455 bit ^= 1;
00456 }
00457 }
00458
00459
00460
00461 s->total_num_coded_frags = 0;
00462 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
00463
00464 for (plane = 0; plane < 3; plane++) {
00465 int sb_start = superblock_starts[plane];
00466 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count);
00467 int num_coded_frags = 0;
00468
00469 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
00470
00471
00472 for (j = 0; j < 16; j++) {
00473
00474
00475 current_fragment = s->superblock_fragments[i * 16 + j];
00476 if (current_fragment != -1) {
00477 int coded = s->superblock_coding[i];
00478
00479 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
00480
00481
00482
00483 if (current_run-- == 0) {
00484 bit ^= 1;
00485 current_run = get_vlc2(gb,
00486 s->fragment_run_length_vlc.table, 5, 2);
00487 }
00488 coded = bit;
00489 }
00490
00491 if (coded) {
00492
00493
00494 s->all_fragments[current_fragment].coding_method =
00495 MODE_INTER_NO_MV;
00496 s->coded_fragment_list[plane][num_coded_frags++] =
00497 current_fragment;
00498 } else {
00499
00500 s->all_fragments[current_fragment].coding_method =
00501 MODE_COPY;
00502 }
00503 }
00504 }
00505 }
00506 s->total_num_coded_frags += num_coded_frags;
00507 for (i = 0; i < 64; i++)
00508 s->num_coded_frags[plane][i] = num_coded_frags;
00509 if (plane < 2)
00510 s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags;
00511 }
00512 return 0;
00513 }
00514
00515
00516
00517
00518
00519 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
00520 {
00521 int i, j, k, sb_x, sb_y;
00522 int scheme;
00523 int current_macroblock;
00524 int current_fragment;
00525 int coding_mode;
00526 int custom_mode_alphabet[CODING_MODE_COUNT];
00527 const int *alphabet;
00528 Vp3Fragment *frag;
00529
00530 if (s->keyframe) {
00531 for (i = 0; i < s->fragment_count; i++)
00532 s->all_fragments[i].coding_method = MODE_INTRA;
00533
00534 } else {
00535
00536
00537 scheme = get_bits(gb, 3);
00538
00539
00540 if (scheme == 0) {
00541 for (i = 0; i < 8; i++)
00542 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
00543 for (i = 0; i < 8; i++)
00544 custom_mode_alphabet[get_bits(gb, 3)] = i;
00545 alphabet = custom_mode_alphabet;
00546 } else
00547 alphabet = ModeAlphabet[scheme-1];
00548
00549
00550
00551 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
00552 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
00553 if (get_bits_left(gb) <= 0)
00554 return -1;
00555
00556 for (j = 0; j < 4; j++) {
00557 int mb_x = 2*sb_x + (j>>1);
00558 int mb_y = 2*sb_y + (((j>>1)+j)&1);
00559 current_macroblock = mb_y * s->macroblock_width + mb_x;
00560
00561 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
00562 continue;
00563
00564 #define BLOCK_X (2*mb_x + (k&1))
00565 #define BLOCK_Y (2*mb_y + (k>>1))
00566
00567
00568 for (k = 0; k < 4; k++) {
00569 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00570 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
00571 break;
00572 }
00573 if (k == 4) {
00574 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
00575 continue;
00576 }
00577
00578
00579 if (scheme == 7)
00580 coding_mode = get_bits(gb, 3);
00581 else
00582 coding_mode = alphabet
00583 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
00584
00585 s->macroblock_coding[current_macroblock] = coding_mode;
00586 for (k = 0; k < 4; k++) {
00587 frag = s->all_fragments + BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00588 if (frag->coding_method != MODE_COPY)
00589 frag->coding_method = coding_mode;
00590 }
00591
00592 #define SET_CHROMA_MODES \
00593 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
00594 frag[s->fragment_start[1]].coding_method = coding_mode;\
00595 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
00596 frag[s->fragment_start[2]].coding_method = coding_mode;
00597
00598 if (s->chroma_y_shift) {
00599 frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;
00600 SET_CHROMA_MODES
00601 } else if (s->chroma_x_shift) {
00602 frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;
00603 for (k = 0; k < 2; k++) {
00604 SET_CHROMA_MODES
00605 frag += s->fragment_width[1];
00606 }
00607 } else {
00608 for (k = 0; k < 4; k++) {
00609 frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;
00610 SET_CHROMA_MODES
00611 }
00612 }
00613 }
00614 }
00615 }
00616 }
00617
00618 return 0;
00619 }
00620
00621
00622
00623
00624
00625 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
00626 {
00627 int j, k, sb_x, sb_y;
00628 int coding_mode;
00629 int motion_x[4];
00630 int motion_y[4];
00631 int last_motion_x = 0;
00632 int last_motion_y = 0;
00633 int prior_last_motion_x = 0;
00634 int prior_last_motion_y = 0;
00635 int current_macroblock;
00636 int current_fragment;
00637 int frag;
00638
00639 if (s->keyframe)
00640 return 0;
00641
00642
00643 coding_mode = get_bits1(gb);
00644
00645
00646
00647 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
00648 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
00649 if (get_bits_left(gb) <= 0)
00650 return -1;
00651
00652 for (j = 0; j < 4; j++) {
00653 int mb_x = 2*sb_x + (j>>1);
00654 int mb_y = 2*sb_y + (((j>>1)+j)&1);
00655 current_macroblock = mb_y * s->macroblock_width + mb_x;
00656
00657 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
00658 (s->macroblock_coding[current_macroblock] == MODE_COPY))
00659 continue;
00660
00661 switch (s->macroblock_coding[current_macroblock]) {
00662
00663 case MODE_INTER_PLUS_MV:
00664 case MODE_GOLDEN_MV:
00665
00666 if (coding_mode == 0) {
00667 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00668 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00669 } else {
00670 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
00671 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
00672 }
00673
00674
00675 if (s->macroblock_coding[current_macroblock] ==
00676 MODE_INTER_PLUS_MV) {
00677 prior_last_motion_x = last_motion_x;
00678 prior_last_motion_y = last_motion_y;
00679 last_motion_x = motion_x[0];
00680 last_motion_y = motion_y[0];
00681 }
00682 break;
00683
00684 case MODE_INTER_FOURMV:
00685
00686 prior_last_motion_x = last_motion_x;
00687 prior_last_motion_y = last_motion_y;
00688
00689
00690
00691 for (k = 0; k < 4; k++) {
00692 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00693 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
00694 if (coding_mode == 0) {
00695 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00696 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00697 } else {
00698 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
00699 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
00700 }
00701 last_motion_x = motion_x[k];
00702 last_motion_y = motion_y[k];
00703 } else {
00704 motion_x[k] = 0;
00705 motion_y[k] = 0;
00706 }
00707 }
00708 break;
00709
00710 case MODE_INTER_LAST_MV:
00711
00712 motion_x[0] = last_motion_x;
00713 motion_y[0] = last_motion_y;
00714
00715
00716
00717 break;
00718
00719 case MODE_INTER_PRIOR_LAST:
00720
00721
00722 motion_x[0] = prior_last_motion_x;
00723 motion_y[0] = prior_last_motion_y;
00724
00725
00726 prior_last_motion_x = last_motion_x;
00727 prior_last_motion_y = last_motion_y;
00728 last_motion_x = motion_x[0];
00729 last_motion_y = motion_y[0];
00730 break;
00731
00732 default:
00733
00734 motion_x[0] = 0;
00735 motion_y[0] = 0;
00736
00737
00738 break;
00739 }
00740
00741
00742 for (k = 0; k < 4; k++) {
00743 current_fragment =
00744 BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00745 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00746 s->motion_val[0][current_fragment][0] = motion_x[k];
00747 s->motion_val[0][current_fragment][1] = motion_y[k];
00748 } else {
00749 s->motion_val[0][current_fragment][0] = motion_x[0];
00750 s->motion_val[0][current_fragment][1] = motion_y[0];
00751 }
00752 }
00753
00754 if (s->chroma_y_shift) {
00755 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00756 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
00757 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
00758 }
00759 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
00760 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
00761 frag = mb_y*s->fragment_width[1] + mb_x;
00762 s->motion_val[1][frag][0] = motion_x[0];
00763 s->motion_val[1][frag][1] = motion_y[0];
00764 } else if (s->chroma_x_shift) {
00765 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00766 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
00767 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
00768 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
00769 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
00770 } else {
00771 motion_x[1] = motion_x[0];
00772 motion_y[1] = motion_y[0];
00773 }
00774 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
00775 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
00776
00777 frag = 2*mb_y*s->fragment_width[1] + mb_x;
00778 for (k = 0; k < 2; k++) {
00779 s->motion_val[1][frag][0] = motion_x[k];
00780 s->motion_val[1][frag][1] = motion_y[k];
00781 frag += s->fragment_width[1];
00782 }
00783 } else {
00784 for (k = 0; k < 4; k++) {
00785 frag = BLOCK_Y*s->fragment_width[1] + BLOCK_X;
00786 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00787 s->motion_val[1][frag][0] = motion_x[k];
00788 s->motion_val[1][frag][1] = motion_y[k];
00789 } else {
00790 s->motion_val[1][frag][0] = motion_x[0];
00791 s->motion_val[1][frag][1] = motion_y[0];
00792 }
00793 }
00794 }
00795 }
00796 }
00797 }
00798
00799 return 0;
00800 }
00801
00802 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
00803 {
00804 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
00805 int num_blocks = s->total_num_coded_frags;
00806
00807 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
00808 i = blocks_decoded = num_blocks_at_qpi = 0;
00809
00810 bit = get_bits1(gb) ^ 1;
00811 run_length = 0;
00812
00813 do {
00814 if (run_length == MAXIMUM_LONG_BIT_RUN)
00815 bit = get_bits1(gb);
00816 else
00817 bit ^= 1;
00818
00819 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
00820 if (run_length == 34)
00821 run_length += get_bits(gb, 12);
00822 blocks_decoded += run_length;
00823
00824 if (!bit)
00825 num_blocks_at_qpi += run_length;
00826
00827 for (j = 0; j < run_length; i++) {
00828 if (i >= s->total_num_coded_frags)
00829 return -1;
00830
00831 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
00832 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
00833 j++;
00834 }
00835 }
00836 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
00837
00838 num_blocks -= num_blocks_at_qpi;
00839 }
00840
00841 return 0;
00842 }
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
00857 VLC *table, int coeff_index,
00858 int plane,
00859 int eob_run)
00860 {
00861 int i, j = 0;
00862 int token;
00863 int zero_run = 0;
00864 DCTELEM coeff = 0;
00865 int bits_to_get;
00866 int blocks_ended;
00867 int coeff_i = 0;
00868 int num_coeffs = s->num_coded_frags[plane][coeff_index];
00869 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
00870
00871
00872 int *coded_fragment_list = s->coded_fragment_list[plane];
00873 Vp3Fragment *all_fragments = s->all_fragments;
00874 VLC_TYPE (*vlc_table)[2] = table->table;
00875
00876 if (num_coeffs < 0)
00877 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index);
00878
00879 if (eob_run > num_coeffs) {
00880 coeff_i = blocks_ended = num_coeffs;
00881 eob_run -= num_coeffs;
00882 } else {
00883 coeff_i = blocks_ended = eob_run;
00884 eob_run = 0;
00885 }
00886
00887
00888 if (blocks_ended)
00889 dct_tokens[j++] = blocks_ended << 2;
00890
00891 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
00892
00893 token = get_vlc2(gb, vlc_table, 11, 3);
00894
00895 if (token <= 6) {
00896 eob_run = eob_run_base[token];
00897 if (eob_run_get_bits[token])
00898 eob_run += get_bits(gb, eob_run_get_bits[token]);
00899
00900
00901
00902 if (eob_run > num_coeffs - coeff_i) {
00903 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
00904 blocks_ended += num_coeffs - coeff_i;
00905 eob_run -= num_coeffs - coeff_i;
00906 coeff_i = num_coeffs;
00907 } else {
00908 dct_tokens[j++] = TOKEN_EOB(eob_run);
00909 blocks_ended += eob_run;
00910 coeff_i += eob_run;
00911 eob_run = 0;
00912 }
00913 } else {
00914 bits_to_get = coeff_get_bits[token];
00915 if (bits_to_get)
00916 bits_to_get = get_bits(gb, bits_to_get);
00917 coeff = coeff_tables[token][bits_to_get];
00918
00919 zero_run = zero_run_base[token];
00920 if (zero_run_get_bits[token])
00921 zero_run += get_bits(gb, zero_run_get_bits[token]);
00922
00923 if (zero_run) {
00924 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
00925 } else {
00926
00927
00928
00929
00930 if (!coeff_index)
00931 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
00932
00933 dct_tokens[j++] = TOKEN_COEFF(coeff);
00934 }
00935
00936 if (coeff_index + zero_run > 64) {
00937 av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with"
00938 " %d coeffs left\n", zero_run, 64-coeff_index);
00939 zero_run = 64 - coeff_index;
00940 }
00941
00942
00943
00944 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
00945 s->num_coded_frags[plane][i]--;
00946 coeff_i++;
00947 }
00948 }
00949
00950 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
00951 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
00952
00953
00954
00955 if (blocks_ended)
00956 for (i = coeff_index+1; i < 64; i++)
00957 s->num_coded_frags[plane][i] -= blocks_ended;
00958
00959
00960 if (plane < 2)
00961 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
00962 else if (coeff_index < 63)
00963 s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
00964
00965 return eob_run;
00966 }
00967
00968 static void reverse_dc_prediction(Vp3DecodeContext *s,
00969 int first_fragment,
00970 int fragment_width,
00971 int fragment_height);
00972
00973
00974
00975
00976 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
00977 {
00978 int i;
00979 int dc_y_table;
00980 int dc_c_table;
00981 int ac_y_table;
00982 int ac_c_table;
00983 int residual_eob_run = 0;
00984 VLC *y_tables[64];
00985 VLC *c_tables[64];
00986
00987 s->dct_tokens[0][0] = s->dct_tokens_base;
00988
00989
00990 dc_y_table = get_bits(gb, 4);
00991 dc_c_table = get_bits(gb, 4);
00992
00993
00994 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
00995 0, residual_eob_run);
00996
00997
00998 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
00999
01000
01001 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
01002 1, residual_eob_run);
01003 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
01004 2, residual_eob_run);
01005
01006
01007 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
01008 {
01009 reverse_dc_prediction(s, s->fragment_start[1],
01010 s->fragment_width[1], s->fragment_height[1]);
01011 reverse_dc_prediction(s, s->fragment_start[2],
01012 s->fragment_width[1], s->fragment_height[1]);
01013 }
01014
01015
01016 ac_y_table = get_bits(gb, 4);
01017 ac_c_table = get_bits(gb, 4);
01018
01019
01020 for (i = 1; i <= 5; i++) {
01021 y_tables[i] = &s->ac_vlc_1[ac_y_table];
01022 c_tables[i] = &s->ac_vlc_1[ac_c_table];
01023 }
01024 for (i = 6; i <= 14; i++) {
01025 y_tables[i] = &s->ac_vlc_2[ac_y_table];
01026 c_tables[i] = &s->ac_vlc_2[ac_c_table];
01027 }
01028 for (i = 15; i <= 27; i++) {
01029 y_tables[i] = &s->ac_vlc_3[ac_y_table];
01030 c_tables[i] = &s->ac_vlc_3[ac_c_table];
01031 }
01032 for (i = 28; i <= 63; i++) {
01033 y_tables[i] = &s->ac_vlc_4[ac_y_table];
01034 c_tables[i] = &s->ac_vlc_4[ac_c_table];
01035 }
01036
01037
01038 for (i = 1; i <= 63; i++) {
01039 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
01040 0, residual_eob_run);
01041
01042 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
01043 1, residual_eob_run);
01044 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
01045 2, residual_eob_run);
01046 }
01047
01048 return 0;
01049 }
01050
01051
01052
01053
01054
01055
01056 #define COMPATIBLE_FRAME(x) \
01057 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
01058 #define DC_COEFF(u) s->all_fragments[u].dc
01059
01060 static void reverse_dc_prediction(Vp3DecodeContext *s,
01061 int first_fragment,
01062 int fragment_width,
01063 int fragment_height)
01064 {
01065
01066 #define PUL 8
01067 #define PU 4
01068 #define PUR 2
01069 #define PL 1
01070
01071 int x, y;
01072 int i = first_fragment;
01073
01074 int predicted_dc;
01075
01076
01077 int vl, vul, vu, vur;
01078
01079
01080 int l, ul, u, ur;
01081
01082
01083
01084
01085
01086
01087
01088
01089 static const int predictor_transform[16][4] = {
01090 { 0, 0, 0, 0},
01091 { 0, 0, 0,128},
01092 { 0, 0,128, 0},
01093 { 0, 0, 53, 75},
01094 { 0,128, 0, 0},
01095 { 0, 64, 0, 64},
01096 { 0,128, 0, 0},
01097 { 0, 0, 53, 75},
01098 {128, 0, 0, 0},
01099 { 0, 0, 0,128},
01100 { 64, 0, 64, 0},
01101 { 0, 0, 53, 75},
01102 { 0,128, 0, 0},
01103 {-104,116, 0,116},
01104 { 24, 80, 24, 0},
01105 {-104,116, 0,116}
01106 };
01107
01108
01109
01110
01111
01112
01113
01114 static const unsigned char compatible_frame[9] = {
01115 1,
01116 0,
01117 1,
01118 1,
01119 1,
01120 2,
01121 2,
01122 1,
01123 3
01124 };
01125 int current_frame_type;
01126
01127
01128 short last_dc[3];
01129
01130 int transform = 0;
01131
01132 vul = vu = vur = vl = 0;
01133 last_dc[0] = last_dc[1] = last_dc[2] = 0;
01134
01135
01136 for (y = 0; y < fragment_height; y++) {
01137
01138
01139 for (x = 0; x < fragment_width; x++, i++) {
01140
01141
01142 if (s->all_fragments[i].coding_method != MODE_COPY) {
01143
01144 current_frame_type =
01145 compatible_frame[s->all_fragments[i].coding_method];
01146
01147 transform= 0;
01148 if(x){
01149 l= i-1;
01150 vl = DC_COEFF(l);
01151 if(COMPATIBLE_FRAME(l))
01152 transform |= PL;
01153 }
01154 if(y){
01155 u= i-fragment_width;
01156 vu = DC_COEFF(u);
01157 if(COMPATIBLE_FRAME(u))
01158 transform |= PU;
01159 if(x){
01160 ul= i-fragment_width-1;
01161 vul = DC_COEFF(ul);
01162 if(COMPATIBLE_FRAME(ul))
01163 transform |= PUL;
01164 }
01165 if(x + 1 < fragment_width){
01166 ur= i-fragment_width+1;
01167 vur = DC_COEFF(ur);
01168 if(COMPATIBLE_FRAME(ur))
01169 transform |= PUR;
01170 }
01171 }
01172
01173 if (transform == 0) {
01174
01175
01176
01177 predicted_dc = last_dc[current_frame_type];
01178 } else {
01179
01180
01181 predicted_dc =
01182 (predictor_transform[transform][0] * vul) +
01183 (predictor_transform[transform][1] * vu) +
01184 (predictor_transform[transform][2] * vur) +
01185 (predictor_transform[transform][3] * vl);
01186
01187 predicted_dc /= 128;
01188
01189
01190
01191 if ((transform == 15) || (transform == 13)) {
01192 if (FFABS(predicted_dc - vu) > 128)
01193 predicted_dc = vu;
01194 else if (FFABS(predicted_dc - vl) > 128)
01195 predicted_dc = vl;
01196 else if (FFABS(predicted_dc - vul) > 128)
01197 predicted_dc = vul;
01198 }
01199 }
01200
01201
01202 DC_COEFF(i) += predicted_dc;
01203
01204 last_dc[current_frame_type] = DC_COEFF(i);
01205 }
01206 }
01207 }
01208 }
01209
01210 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
01211 {
01212 int x, y;
01213 int *bounding_values= s->bounding_values_array+127;
01214
01215 int width = s->fragment_width[!!plane];
01216 int height = s->fragment_height[!!plane];
01217 int fragment = s->fragment_start [plane] + ystart * width;
01218 int stride = s->current_frame.linesize[plane];
01219 uint8_t *plane_data = s->current_frame.data [plane];
01220 if (!s->flipped_image) stride = -stride;
01221 plane_data += s->data_offset[plane] + 8*ystart*stride;
01222
01223 for (y = ystart; y < yend; y++) {
01224
01225 for (x = 0; x < width; x++) {
01226
01227
01228
01229
01230 if( s->all_fragments[fragment].coding_method != MODE_COPY )
01231 {
01232
01233 if (x > 0) {
01234 s->dsp.vp3_h_loop_filter(
01235 plane_data + 8*x,
01236 stride, bounding_values);
01237 }
01238
01239
01240 if (y > 0) {
01241 s->dsp.vp3_v_loop_filter(
01242 plane_data + 8*x,
01243 stride, bounding_values);
01244 }
01245
01246
01247
01248
01249 if ((x < width - 1) &&
01250 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
01251 s->dsp.vp3_h_loop_filter(
01252 plane_data + 8*x + 8,
01253 stride, bounding_values);
01254 }
01255
01256
01257
01258
01259 if ((y < height - 1) &&
01260 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
01261 s->dsp.vp3_v_loop_filter(
01262 plane_data + 8*x + 8*stride,
01263 stride, bounding_values);
01264 }
01265 }
01266
01267 fragment++;
01268 }
01269 plane_data += 8*stride;
01270 }
01271 }
01272
01277 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
01278 int plane, int inter, DCTELEM block[64])
01279 {
01280 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
01281 uint8_t *perm = s->scantable.permutated;
01282 int i = 0;
01283
01284 do {
01285 int token = *s->dct_tokens[plane][i];
01286 switch (token & 3) {
01287 case 0:
01288 if (--token < 4)
01289 s->dct_tokens[plane][i]++;
01290 else
01291 *s->dct_tokens[plane][i] = token & ~3;
01292 goto end;
01293 case 1:
01294 s->dct_tokens[plane][i]++;
01295 i += (token >> 2) & 0x7f;
01296 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
01297 i++;
01298 break;
01299 case 2:
01300 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
01301 s->dct_tokens[plane][i++]++;
01302 break;
01303 default:
01304 return i;
01305 }
01306 } while (i < 64);
01307 end:
01308
01309 block[0] = frag->dc * s->qmat[0][inter][plane][0];
01310 return i;
01311 }
01312
01316 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
01317 {
01318 int h, cy;
01319 int offset[4];
01320
01321 if(s->avctx->draw_horiz_band==NULL)
01322 return;
01323
01324 h= y - s->last_slice_end;
01325 s->last_slice_end= y;
01326 y -= h;
01327
01328 if (!s->flipped_image) {
01329 y = s->avctx->height - y - h;
01330 }
01331
01332 cy = y >> s->chroma_y_shift;
01333 offset[0] = s->current_frame.linesize[0]*y;
01334 offset[1] = s->current_frame.linesize[1]*cy;
01335 offset[2] = s->current_frame.linesize[2]*cy;
01336 offset[3] = 0;
01337
01338 emms_c();
01339 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
01340 }
01341
01342
01343
01344
01345
01346 static void render_slice(Vp3DecodeContext *s, int slice)
01347 {
01348 int x, y, i, j;
01349 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
01350 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
01351 int motion_halfpel_index;
01352 uint8_t *motion_source;
01353 int plane, first_pixel;
01354
01355 if (slice >= s->c_superblock_height)
01356 return;
01357
01358 for (plane = 0; plane < 3; plane++) {
01359 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
01360 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
01361 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
01362 int stride = s->current_frame.linesize[plane];
01363 int plane_width = s->width >> (plane && s->chroma_x_shift);
01364 int plane_height = s->height >> (plane && s->chroma_y_shift);
01365 int8_t (*motion_val)[2] = s->motion_val[!!plane];
01366
01367 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
01368 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
01369 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
01370
01371 int fragment_width = s->fragment_width[!!plane];
01372 int fragment_height = s->fragment_height[!!plane];
01373 int fragment_start = s->fragment_start[plane];
01374
01375 if (!s->flipped_image) stride = -stride;
01376 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
01377 continue;
01378
01379
01380 if(FFABS(stride) > 2048)
01381 return;
01382
01383
01384 for (; sb_y < slice_height; sb_y++) {
01385
01386
01387 for (sb_x = 0; sb_x < slice_width; sb_x++) {
01388
01389
01390 for (j = 0; j < 16; j++) {
01391 x = 4*sb_x + hilbert_offset[j][0];
01392 y = 4*sb_y + hilbert_offset[j][1];
01393
01394 i = fragment_start + y*fragment_width + x;
01395
01396
01397 if (x >= fragment_width || y >= fragment_height)
01398 continue;
01399
01400 first_pixel = 8*y*stride + 8*x;
01401
01402
01403 if (s->all_fragments[i].coding_method != MODE_COPY) {
01404 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
01405 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
01406 motion_source= golden_plane;
01407 else
01408 motion_source= last_plane;
01409
01410 motion_source += first_pixel;
01411 motion_halfpel_index = 0;
01412
01413
01414
01415 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
01416 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
01417 int src_x, src_y;
01418 motion_x = motion_val[y*fragment_width + x][0];
01419 motion_y = motion_val[y*fragment_width + x][1];
01420
01421 src_x= (motion_x>>1) + 8*x;
01422 src_y= (motion_y>>1) + 8*y;
01423
01424 motion_halfpel_index = motion_x & 0x01;
01425 motion_source += (motion_x >> 1);
01426
01427 motion_halfpel_index |= (motion_y & 0x01) << 1;
01428 motion_source += ((motion_y >> 1) * stride);
01429
01430 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
01431 uint8_t *temp= s->edge_emu_buffer;
01432 if(stride<0) temp -= 9*stride;
01433 else temp += 9*stride;
01434
01435 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
01436 motion_source= temp;
01437 }
01438 }
01439
01440
01441
01442
01443 if (s->all_fragments[i].coding_method != MODE_INTRA) {
01444
01445
01446
01447
01448 if(motion_halfpel_index != 3){
01449 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
01450 output_plane + first_pixel,
01451 motion_source, stride, 8);
01452 }else{
01453 int d= (motion_x ^ motion_y)>>31;
01454 s->dsp.put_no_rnd_pixels_l2[1](
01455 output_plane + first_pixel,
01456 motion_source - d,
01457 motion_source + stride + 1 + d,
01458 stride, 8);
01459 }
01460 }
01461
01462 s->dsp.clear_block(block);
01463
01464
01465
01466 if (s->all_fragments[i].coding_method == MODE_INTRA) {
01467 vp3_dequant(s, s->all_fragments + i, plane, 0, block);
01468 if(s->avctx->idct_algo!=FF_IDCT_VP3)
01469 block[0] += 128<<3;
01470 s->dsp.idct_put(
01471 output_plane + first_pixel,
01472 stride,
01473 block);
01474 } else {
01475 if (vp3_dequant(s, s->all_fragments + i, plane, 1, block)) {
01476 s->dsp.idct_add(
01477 output_plane + first_pixel,
01478 stride,
01479 block);
01480 } else {
01481 s->dsp.vp3_idct_dc_add(output_plane + first_pixel, stride, block);
01482 }
01483 }
01484 } else {
01485
01486
01487 s->dsp.put_pixels_tab[1][0](
01488 output_plane + first_pixel,
01489 last_plane + first_pixel,
01490 stride, 8);
01491
01492 }
01493 }
01494 }
01495
01496
01497 if (!s->skip_loop_filter)
01498 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1));
01499 }
01500 }
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510 vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) -16, s->height-16));
01511 }
01512
01513
01514
01515
01516 static av_cold int vp3_decode_init(AVCodecContext *avctx)
01517 {
01518 Vp3DecodeContext *s = avctx->priv_data;
01519 int i, inter, plane;
01520 int c_width;
01521 int c_height;
01522 int y_fragment_count, c_fragment_count;
01523
01524 if (avctx->codec_tag == MKTAG('V','P','3','0'))
01525 s->version = 0;
01526 else
01527 s->version = 1;
01528
01529 s->avctx = avctx;
01530 s->width = FFALIGN(avctx->width, 16);
01531 s->height = FFALIGN(avctx->height, 16);
01532 if (avctx->pix_fmt == PIX_FMT_NONE)
01533 avctx->pix_fmt = PIX_FMT_YUV420P;
01534 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01535 if(avctx->idct_algo==FF_IDCT_AUTO)
01536 avctx->idct_algo=FF_IDCT_VP3;
01537 dsputil_init(&s->dsp, avctx);
01538
01539 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
01540
01541
01542
01543 for (i = 0; i < 3; i++)
01544 s->qps[i] = -1;
01545
01546 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
01547
01548 s->y_superblock_width = (s->width + 31) / 32;
01549 s->y_superblock_height = (s->height + 31) / 32;
01550 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
01551
01552
01553 c_width = s->width >> s->chroma_x_shift;
01554 c_height = s->height >> s->chroma_y_shift;
01555 s->c_superblock_width = (c_width + 31) / 32;
01556 s->c_superblock_height = (c_height + 31) / 32;
01557 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
01558
01559 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
01560 s->u_superblock_start = s->y_superblock_count;
01561 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
01562 s->superblock_coding = av_malloc(s->superblock_count);
01563
01564 s->macroblock_width = (s->width + 15) / 16;
01565 s->macroblock_height = (s->height + 15) / 16;
01566 s->macroblock_count = s->macroblock_width * s->macroblock_height;
01567
01568 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
01569 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
01570 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
01571 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
01572
01573
01574 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
01575 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
01576 s->fragment_count = y_fragment_count + 2*c_fragment_count;
01577 s->fragment_start[1] = y_fragment_count;
01578 s->fragment_start[2] = y_fragment_count + c_fragment_count;
01579
01580 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
01581 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
01582 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
01583 s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
01584 s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
01585
01586 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
01587 !s->coded_fragment_list[0] || !s->motion_val[0] || !s->motion_val[1]) {
01588 vp3_decode_end(avctx);
01589 return -1;
01590 }
01591
01592 if (!s->theora_tables)
01593 {
01594 for (i = 0; i < 64; i++) {
01595 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
01596 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
01597 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
01598 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
01599 s->base_matrix[2][i] = vp31_inter_dequant[i];
01600 s->filter_limit_values[i] = vp31_filter_limit_values[i];
01601 }
01602
01603 for(inter=0; inter<2; inter++){
01604 for(plane=0; plane<3; plane++){
01605 s->qr_count[inter][plane]= 1;
01606 s->qr_size [inter][plane][0]= 63;
01607 s->qr_base [inter][plane][0]=
01608 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
01609 }
01610 }
01611
01612
01613 for (i = 0; i < 16; i++) {
01614
01615
01616 init_vlc(&s->dc_vlc[i], 11, 32,
01617 &dc_bias[i][0][1], 4, 2,
01618 &dc_bias[i][0][0], 4, 2, 0);
01619
01620
01621 init_vlc(&s->ac_vlc_1[i], 11, 32,
01622 &ac_bias_0[i][0][1], 4, 2,
01623 &ac_bias_0[i][0][0], 4, 2, 0);
01624
01625
01626 init_vlc(&s->ac_vlc_2[i], 11, 32,
01627 &ac_bias_1[i][0][1], 4, 2,
01628 &ac_bias_1[i][0][0], 4, 2, 0);
01629
01630
01631 init_vlc(&s->ac_vlc_3[i], 11, 32,
01632 &ac_bias_2[i][0][1], 4, 2,
01633 &ac_bias_2[i][0][0], 4, 2, 0);
01634
01635
01636 init_vlc(&s->ac_vlc_4[i], 11, 32,
01637 &ac_bias_3[i][0][1], 4, 2,
01638 &ac_bias_3[i][0][0], 4, 2, 0);
01639 }
01640 } else {
01641
01642 for (i = 0; i < 16; i++) {
01643
01644 if (init_vlc(&s->dc_vlc[i], 11, 32,
01645 &s->huffman_table[i][0][1], 8, 4,
01646 &s->huffman_table[i][0][0], 8, 4, 0) < 0)
01647 goto vlc_fail;
01648
01649
01650 if (init_vlc(&s->ac_vlc_1[i], 11, 32,
01651 &s->huffman_table[i+16][0][1], 8, 4,
01652 &s->huffman_table[i+16][0][0], 8, 4, 0) < 0)
01653 goto vlc_fail;
01654
01655
01656 if (init_vlc(&s->ac_vlc_2[i], 11, 32,
01657 &s->huffman_table[i+16*2][0][1], 8, 4,
01658 &s->huffman_table[i+16*2][0][0], 8, 4, 0) < 0)
01659 goto vlc_fail;
01660
01661
01662 if (init_vlc(&s->ac_vlc_3[i], 11, 32,
01663 &s->huffman_table[i+16*3][0][1], 8, 4,
01664 &s->huffman_table[i+16*3][0][0], 8, 4, 0) < 0)
01665 goto vlc_fail;
01666
01667
01668 if (init_vlc(&s->ac_vlc_4[i], 11, 32,
01669 &s->huffman_table[i+16*4][0][1], 8, 4,
01670 &s->huffman_table[i+16*4][0][0], 8, 4, 0) < 0)
01671 goto vlc_fail;
01672 }
01673 }
01674
01675 init_vlc(&s->superblock_run_length_vlc, 6, 34,
01676 &superblock_run_length_vlc_table[0][1], 4, 2,
01677 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
01678
01679 init_vlc(&s->fragment_run_length_vlc, 5, 30,
01680 &fragment_run_length_vlc_table[0][1], 4, 2,
01681 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
01682
01683 init_vlc(&s->mode_code_vlc, 3, 8,
01684 &mode_code_vlc_table[0][1], 2, 1,
01685 &mode_code_vlc_table[0][0], 2, 1, 0);
01686
01687 init_vlc(&s->motion_vector_vlc, 6, 63,
01688 &motion_vector_vlc_table[0][1], 2, 1,
01689 &motion_vector_vlc_table[0][0], 2, 1, 0);
01690
01691
01692 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
01693 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
01694 if (!s->superblock_fragments || !s->macroblock_coding) {
01695 vp3_decode_end(avctx);
01696 return -1;
01697 }
01698 init_block_mapping(s);
01699
01700 for (i = 0; i < 3; i++) {
01701 s->current_frame.data[i] = NULL;
01702 s->last_frame.data[i] = NULL;
01703 s->golden_frame.data[i] = NULL;
01704 }
01705
01706 return 0;
01707
01708 vlc_fail:
01709 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
01710 return -1;
01711 }
01712
01713
01714
01715
01716 static int vp3_decode_frame(AVCodecContext *avctx,
01717 void *data, int *data_size,
01718 AVPacket *avpkt)
01719 {
01720 const uint8_t *buf = avpkt->data;
01721 int buf_size = avpkt->size;
01722 Vp3DecodeContext *s = avctx->priv_data;
01723 GetBitContext gb;
01724 int i;
01725
01726 init_get_bits(&gb, buf, buf_size * 8);
01727
01728 if (s->theora && get_bits1(&gb))
01729 {
01730 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
01731 return -1;
01732 }
01733
01734 s->keyframe = !get_bits1(&gb);
01735 if (!s->theora)
01736 skip_bits(&gb, 1);
01737 for (i = 0; i < 3; i++)
01738 s->last_qps[i] = s->qps[i];
01739
01740 s->nqps=0;
01741 do{
01742 s->qps[s->nqps++]= get_bits(&gb, 6);
01743 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
01744 for (i = s->nqps; i < 3; i++)
01745 s->qps[i] = -1;
01746
01747 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01748 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
01749 s->keyframe?"key":"", avctx->frame_number+1, s->qps[0]);
01750
01751 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
01752 avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL : AVDISCARD_NONKEY);
01753
01754 if (s->qps[0] != s->last_qps[0])
01755 init_loop_filter(s);
01756
01757 for (i = 0; i < s->nqps; i++)
01758
01759
01760 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
01761 init_dequantizer(s, i);
01762
01763 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
01764 return buf_size;
01765
01766 s->current_frame.reference = 3;
01767 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
01768 if (avctx->get_buffer(avctx, &s->current_frame) < 0) {
01769 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01770 goto error;
01771 }
01772
01773 if (s->keyframe) {
01774 if (!s->theora)
01775 {
01776 skip_bits(&gb, 4);
01777 skip_bits(&gb, 4);
01778 if (s->version)
01779 {
01780 s->version = get_bits(&gb, 5);
01781 if (avctx->frame_number == 0)
01782 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
01783 }
01784 }
01785 if (s->version || s->theora)
01786 {
01787 if (get_bits1(&gb))
01788 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
01789 skip_bits(&gb, 2);
01790 }
01791 } else {
01792 if (!s->golden_frame.data[0]) {
01793 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
01794
01795 s->golden_frame.reference = 3;
01796 s->golden_frame.pict_type = FF_I_TYPE;
01797 if (avctx->get_buffer(avctx, &s->golden_frame) < 0) {
01798 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01799 goto error;
01800 }
01801 s->last_frame = s->golden_frame;
01802 s->last_frame.type = FF_BUFFER_TYPE_COPY;
01803 }
01804 }
01805
01806 s->current_frame.qscale_table= s->qscale_table;
01807 s->current_frame.qstride= 0;
01808
01809 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
01810
01811 if (unpack_superblocks(s, &gb)){
01812 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
01813 goto error;
01814 }
01815 if (unpack_modes(s, &gb)){
01816 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
01817 goto error;
01818 }
01819 if (unpack_vectors(s, &gb)){
01820 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
01821 goto error;
01822 }
01823 if (unpack_block_qpis(s, &gb)){
01824 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
01825 goto error;
01826 }
01827 if (unpack_dct_coeffs(s, &gb)){
01828 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
01829 goto error;
01830 }
01831
01832 for (i = 0; i < 3; i++) {
01833 int height = s->height >> (i && s->chroma_y_shift);
01834 if (s->flipped_image)
01835 s->data_offset[i] = 0;
01836 else
01837 s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
01838 }
01839
01840 s->last_slice_end = 0;
01841 for (i = 0; i < s->c_superblock_height; i++)
01842 render_slice(s, i);
01843
01844
01845 for (i = 0; i < 3; i++) {
01846 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
01847 apply_loop_filter(s, i, row, row+1);
01848 }
01849 vp3_draw_horiz_band(s, s->avctx->height);
01850
01851 *data_size=sizeof(AVFrame);
01852 *(AVFrame*)data= s->current_frame;
01853
01854
01855
01856 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
01857 avctx->release_buffer(avctx, &s->last_frame);
01858
01859
01860 s->last_frame= s->current_frame;
01861
01862 if (s->keyframe) {
01863 if (s->golden_frame.data[0])
01864 avctx->release_buffer(avctx, &s->golden_frame);
01865 s->golden_frame = s->current_frame;
01866 s->last_frame.type = FF_BUFFER_TYPE_COPY;
01867 }
01868
01869 s->current_frame.data[0]= NULL;
01870
01871 return buf_size;
01872
01873 error:
01874 if (s->current_frame.data[0])
01875 avctx->release_buffer(avctx, &s->current_frame);
01876 return -1;
01877 }
01878
01879
01880
01881
01882 static av_cold int vp3_decode_end(AVCodecContext *avctx)
01883 {
01884 Vp3DecodeContext *s = avctx->priv_data;
01885 int i;
01886
01887 av_free(s->superblock_coding);
01888 av_free(s->all_fragments);
01889 av_free(s->coded_fragment_list[0]);
01890 av_free(s->dct_tokens_base);
01891 av_free(s->superblock_fragments);
01892 av_free(s->macroblock_coding);
01893 av_free(s->motion_val[0]);
01894 av_free(s->motion_val[1]);
01895
01896 for (i = 0; i < 16; i++) {
01897 free_vlc(&s->dc_vlc[i]);
01898 free_vlc(&s->ac_vlc_1[i]);
01899 free_vlc(&s->ac_vlc_2[i]);
01900 free_vlc(&s->ac_vlc_3[i]);
01901 free_vlc(&s->ac_vlc_4[i]);
01902 }
01903
01904 free_vlc(&s->superblock_run_length_vlc);
01905 free_vlc(&s->fragment_run_length_vlc);
01906 free_vlc(&s->mode_code_vlc);
01907 free_vlc(&s->motion_vector_vlc);
01908
01909
01910 if (s->golden_frame.data[0])
01911 avctx->release_buffer(avctx, &s->golden_frame);
01912 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
01913 avctx->release_buffer(avctx, &s->last_frame);
01914
01915
01916
01917 return 0;
01918 }
01919
01920 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
01921 {
01922 Vp3DecodeContext *s = avctx->priv_data;
01923
01924 if (get_bits1(gb)) {
01925 int token;
01926 if (s->entries >= 32) {
01927 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
01928 return -1;
01929 }
01930 token = get_bits(gb, 5);
01931
01932 s->huffman_table[s->hti][token][0] = s->hbits;
01933 s->huffman_table[s->hti][token][1] = s->huff_code_size;
01934 s->entries++;
01935 }
01936 else {
01937 if (s->huff_code_size >= 32) {
01938 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
01939 return -1;
01940 }
01941 s->huff_code_size++;
01942 s->hbits <<= 1;
01943 if (read_huffman_tree(avctx, gb))
01944 return -1;
01945 s->hbits |= 1;
01946 if (read_huffman_tree(avctx, gb))
01947 return -1;
01948 s->hbits >>= 1;
01949 s->huff_code_size--;
01950 }
01951 return 0;
01952 }
01953
01954 #if CONFIG_THEORA_DECODER
01955 static const enum PixelFormat theora_pix_fmts[4] = {
01956 PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P
01957 };
01958
01959 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
01960 {
01961 Vp3DecodeContext *s = avctx->priv_data;
01962 int visible_width, visible_height, colorspace;
01963 int offset_x = 0, offset_y = 0;
01964 AVRational fps, aspect;
01965
01966 s->theora = get_bits_long(gb, 24);
01967 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
01968
01969
01970
01971 if (s->theora < 0x030200)
01972 {
01973 s->flipped_image = 1;
01974 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
01975 }
01976
01977 visible_width = s->width = get_bits(gb, 16) << 4;
01978 visible_height = s->height = get_bits(gb, 16) << 4;
01979
01980 if(av_image_check_size(s->width, s->height, 0, avctx)){
01981 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
01982 s->width= s->height= 0;
01983 return -1;
01984 }
01985
01986 if (s->theora >= 0x030200) {
01987 visible_width = get_bits_long(gb, 24);
01988 visible_height = get_bits_long(gb, 24);
01989
01990 offset_x = get_bits(gb, 8);
01991 offset_y = get_bits(gb, 8);
01992 }
01993
01994 fps.num = get_bits_long(gb, 32);
01995 fps.den = get_bits_long(gb, 32);
01996 if (fps.num && fps.den) {
01997 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
01998 fps.den, fps.num, 1<<30);
01999 }
02000
02001 aspect.num = get_bits_long(gb, 24);
02002 aspect.den = get_bits_long(gb, 24);
02003 if (aspect.num && aspect.den) {
02004 av_reduce(&avctx->sample_aspect_ratio.num,
02005 &avctx->sample_aspect_ratio.den,
02006 aspect.num, aspect.den, 1<<30);
02007 }
02008
02009 if (s->theora < 0x030200)
02010 skip_bits(gb, 5);
02011 colorspace = get_bits(gb, 8);
02012 skip_bits(gb, 24);
02013
02014 skip_bits(gb, 6);
02015
02016 if (s->theora >= 0x030200)
02017 {
02018 skip_bits(gb, 5);
02019 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
02020 skip_bits(gb, 3);
02021 }
02022
02023
02024
02025 if ( visible_width <= s->width && visible_width > s->width-16
02026 && visible_height <= s->height && visible_height > s->height-16
02027 && !offset_x && (offset_y == s->height - visible_height))
02028 avcodec_set_dimensions(avctx, visible_width, visible_height);
02029 else
02030 avcodec_set_dimensions(avctx, s->width, s->height);
02031
02032 if (colorspace == 1) {
02033 avctx->color_primaries = AVCOL_PRI_BT470M;
02034 } else if (colorspace == 2) {
02035 avctx->color_primaries = AVCOL_PRI_BT470BG;
02036 }
02037 if (colorspace == 1 || colorspace == 2) {
02038 avctx->colorspace = AVCOL_SPC_BT470BG;
02039 avctx->color_trc = AVCOL_TRC_BT709;
02040 }
02041
02042 return 0;
02043 }
02044
02045 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
02046 {
02047 Vp3DecodeContext *s = avctx->priv_data;
02048 int i, n, matrices, inter, plane;
02049
02050 if (s->theora >= 0x030200) {
02051 n = get_bits(gb, 3);
02052
02053 if (n)
02054 for (i = 0; i < 64; i++)
02055 s->filter_limit_values[i] = get_bits(gb, n);
02056 }
02057
02058 if (s->theora >= 0x030200)
02059 n = get_bits(gb, 4) + 1;
02060 else
02061 n = 16;
02062
02063 for (i = 0; i < 64; i++)
02064 s->coded_ac_scale_factor[i] = get_bits(gb, n);
02065
02066 if (s->theora >= 0x030200)
02067 n = get_bits(gb, 4) + 1;
02068 else
02069 n = 16;
02070
02071 for (i = 0; i < 64; i++)
02072 s->coded_dc_scale_factor[i] = get_bits(gb, n);
02073
02074 if (s->theora >= 0x030200)
02075 matrices = get_bits(gb, 9) + 1;
02076 else
02077 matrices = 3;
02078
02079 if(matrices > 384){
02080 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
02081 return -1;
02082 }
02083
02084 for(n=0; n<matrices; n++){
02085 for (i = 0; i < 64; i++)
02086 s->base_matrix[n][i]= get_bits(gb, 8);
02087 }
02088
02089 for (inter = 0; inter <= 1; inter++) {
02090 for (plane = 0; plane <= 2; plane++) {
02091 int newqr= 1;
02092 if (inter || plane > 0)
02093 newqr = get_bits1(gb);
02094 if (!newqr) {
02095 int qtj, plj;
02096 if(inter && get_bits1(gb)){
02097 qtj = 0;
02098 plj = plane;
02099 }else{
02100 qtj= (3*inter + plane - 1) / 3;
02101 plj= (plane + 2) % 3;
02102 }
02103 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
02104 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
02105 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
02106 } else {
02107 int qri= 0;
02108 int qi = 0;
02109
02110 for(;;){
02111 i= get_bits(gb, av_log2(matrices-1)+1);
02112 if(i>= matrices){
02113 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
02114 return -1;
02115 }
02116 s->qr_base[inter][plane][qri]= i;
02117 if(qi >= 63)
02118 break;
02119 i = get_bits(gb, av_log2(63-qi)+1) + 1;
02120 s->qr_size[inter][plane][qri++]= i;
02121 qi += i;
02122 }
02123
02124 if (qi > 63) {
02125 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
02126 return -1;
02127 }
02128 s->qr_count[inter][plane]= qri;
02129 }
02130 }
02131 }
02132
02133
02134 for (s->hti = 0; s->hti < 80; s->hti++) {
02135 s->entries = 0;
02136 s->huff_code_size = 1;
02137 if (!get_bits1(gb)) {
02138 s->hbits = 0;
02139 if(read_huffman_tree(avctx, gb))
02140 return -1;
02141 s->hbits = 1;
02142 if(read_huffman_tree(avctx, gb))
02143 return -1;
02144 }
02145 }
02146
02147 s->theora_tables = 1;
02148
02149 return 0;
02150 }
02151
02152 static av_cold int theora_decode_init(AVCodecContext *avctx)
02153 {
02154 Vp3DecodeContext *s = avctx->priv_data;
02155 GetBitContext gb;
02156 int ptype;
02157 uint8_t *header_start[3];
02158 int header_len[3];
02159 int i;
02160
02161 s->theora = 1;
02162
02163 if (!avctx->extradata_size)
02164 {
02165 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
02166 return -1;
02167 }
02168
02169 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
02170 42, header_start, header_len) < 0) {
02171 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
02172 return -1;
02173 }
02174
02175 for(i=0;i<3;i++) {
02176 init_get_bits(&gb, header_start[i], header_len[i] * 8);
02177
02178 ptype = get_bits(&gb, 8);
02179
02180 if (!(ptype & 0x80))
02181 {
02182 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
02183
02184 }
02185
02186
02187 skip_bits_long(&gb, 6*8);
02188
02189 switch(ptype)
02190 {
02191 case 0x80:
02192 theora_decode_header(avctx, &gb);
02193 break;
02194 case 0x81:
02195
02196
02197 break;
02198 case 0x82:
02199 if (theora_decode_tables(avctx, &gb))
02200 return -1;
02201 break;
02202 default:
02203 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
02204 break;
02205 }
02206 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
02207 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
02208 if (s->theora < 0x030200)
02209 break;
02210 }
02211
02212 return vp3_decode_init(avctx);
02213 }
02214
02215 AVCodec theora_decoder = {
02216 "theora",
02217 AVMEDIA_TYPE_VIDEO,
02218 CODEC_ID_THEORA,
02219 sizeof(Vp3DecodeContext),
02220 theora_decode_init,
02221 NULL,
02222 vp3_decode_end,
02223 vp3_decode_frame,
02224 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
02225 NULL,
02226 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
02227 };
02228 #endif
02229
02230 AVCodec vp3_decoder = {
02231 "vp3",
02232 AVMEDIA_TYPE_VIDEO,
02233 CODEC_ID_VP3,
02234 sizeof(Vp3DecodeContext),
02235 vp3_decode_init,
02236 NULL,
02237 vp3_decode_end,
02238 vp3_decode_frame,
02239 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
02240 NULL,
02241 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
02242 };