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