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 (token <= 6) {
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 {
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 }
00940 }
00941
00942 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
00943 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
00944
00945
00946
00947 if (blocks_ended)
00948 for (i = coeff_index+1; i < 64; i++)
00949 s->num_coded_frags[plane][i] -= blocks_ended;
00950
00951
00952 if (plane < 2)
00953 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
00954 else if (coeff_index < 63)
00955 s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
00956
00957 return eob_run;
00958 }
00959
00960 static void reverse_dc_prediction(Vp3DecodeContext *s,
00961 int first_fragment,
00962 int fragment_width,
00963 int fragment_height);
00964
00965
00966
00967
00968 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
00969 {
00970 int i;
00971 int dc_y_table;
00972 int dc_c_table;
00973 int ac_y_table;
00974 int ac_c_table;
00975 int residual_eob_run = 0;
00976 VLC *y_tables[64];
00977 VLC *c_tables[64];
00978
00979 s->dct_tokens[0][0] = s->dct_tokens_base;
00980
00981
00982 dc_y_table = get_bits(gb, 4);
00983 dc_c_table = get_bits(gb, 4);
00984
00985
00986 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
00987 0, residual_eob_run);
00988
00989
00990 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
00991
00992
00993 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
00994 1, residual_eob_run);
00995 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
00996 2, residual_eob_run);
00997
00998
00999 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
01000 {
01001 reverse_dc_prediction(s, s->fragment_start[1],
01002 s->fragment_width[1], s->fragment_height[1]);
01003 reverse_dc_prediction(s, s->fragment_start[2],
01004 s->fragment_width[1], s->fragment_height[1]);
01005 }
01006
01007
01008 ac_y_table = get_bits(gb, 4);
01009 ac_c_table = get_bits(gb, 4);
01010
01011
01012 for (i = 1; i <= 5; i++) {
01013 y_tables[i] = &s->ac_vlc_1[ac_y_table];
01014 c_tables[i] = &s->ac_vlc_1[ac_c_table];
01015 }
01016 for (i = 6; i <= 14; i++) {
01017 y_tables[i] = &s->ac_vlc_2[ac_y_table];
01018 c_tables[i] = &s->ac_vlc_2[ac_c_table];
01019 }
01020 for (i = 15; i <= 27; i++) {
01021 y_tables[i] = &s->ac_vlc_3[ac_y_table];
01022 c_tables[i] = &s->ac_vlc_3[ac_c_table];
01023 }
01024 for (i = 28; i <= 63; i++) {
01025 y_tables[i] = &s->ac_vlc_4[ac_y_table];
01026 c_tables[i] = &s->ac_vlc_4[ac_c_table];
01027 }
01028
01029
01030 for (i = 1; i <= 63; i++) {
01031 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
01032 0, residual_eob_run);
01033
01034 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
01035 1, residual_eob_run);
01036 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
01037 2, residual_eob_run);
01038 }
01039
01040 return 0;
01041 }
01042
01043
01044
01045
01046
01047
01048 #define COMPATIBLE_FRAME(x) \
01049 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
01050 #define DC_COEFF(u) s->all_fragments[u].dc
01051
01052 static void reverse_dc_prediction(Vp3DecodeContext *s,
01053 int first_fragment,
01054 int fragment_width,
01055 int fragment_height)
01056 {
01057
01058 #define PUL 8
01059 #define PU 4
01060 #define PUR 2
01061 #define PL 1
01062
01063 int x, y;
01064 int i = first_fragment;
01065
01066 int predicted_dc;
01067
01068
01069 int vl, vul, vu, vur;
01070
01071
01072 int l, ul, u, ur;
01073
01074
01075
01076
01077
01078
01079
01080
01081 static const int predictor_transform[16][4] = {
01082 { 0, 0, 0, 0},
01083 { 0, 0, 0,128},
01084 { 0, 0,128, 0},
01085 { 0, 0, 53, 75},
01086 { 0,128, 0, 0},
01087 { 0, 64, 0, 64},
01088 { 0,128, 0, 0},
01089 { 0, 0, 53, 75},
01090 {128, 0, 0, 0},
01091 { 0, 0, 0,128},
01092 { 64, 0, 64, 0},
01093 { 0, 0, 53, 75},
01094 { 0,128, 0, 0},
01095 {-104,116, 0,116},
01096 { 24, 80, 24, 0},
01097 {-104,116, 0,116}
01098 };
01099
01100
01101
01102
01103
01104
01105
01106 static const unsigned char compatible_frame[9] = {
01107 1,
01108 0,
01109 1,
01110 1,
01111 1,
01112 2,
01113 2,
01114 1,
01115 3
01116 };
01117 int current_frame_type;
01118
01119
01120 short last_dc[3];
01121
01122 int transform = 0;
01123
01124 vul = vu = vur = vl = 0;
01125 last_dc[0] = last_dc[1] = last_dc[2] = 0;
01126
01127
01128 for (y = 0; y < fragment_height; y++) {
01129
01130
01131 for (x = 0; x < fragment_width; x++, i++) {
01132
01133
01134 if (s->all_fragments[i].coding_method != MODE_COPY) {
01135
01136 current_frame_type =
01137 compatible_frame[s->all_fragments[i].coding_method];
01138
01139 transform= 0;
01140 if(x){
01141 l= i-1;
01142 vl = DC_COEFF(l);
01143 if(COMPATIBLE_FRAME(l))
01144 transform |= PL;
01145 }
01146 if(y){
01147 u= i-fragment_width;
01148 vu = DC_COEFF(u);
01149 if(COMPATIBLE_FRAME(u))
01150 transform |= PU;
01151 if(x){
01152 ul= i-fragment_width-1;
01153 vul = DC_COEFF(ul);
01154 if(COMPATIBLE_FRAME(ul))
01155 transform |= PUL;
01156 }
01157 if(x + 1 < fragment_width){
01158 ur= i-fragment_width+1;
01159 vur = DC_COEFF(ur);
01160 if(COMPATIBLE_FRAME(ur))
01161 transform |= PUR;
01162 }
01163 }
01164
01165 if (transform == 0) {
01166
01167
01168
01169 predicted_dc = last_dc[current_frame_type];
01170 } else {
01171
01172
01173 predicted_dc =
01174 (predictor_transform[transform][0] * vul) +
01175 (predictor_transform[transform][1] * vu) +
01176 (predictor_transform[transform][2] * vur) +
01177 (predictor_transform[transform][3] * vl);
01178
01179 predicted_dc /= 128;
01180
01181
01182
01183 if ((transform == 15) || (transform == 13)) {
01184 if (FFABS(predicted_dc - vu) > 128)
01185 predicted_dc = vu;
01186 else if (FFABS(predicted_dc - vl) > 128)
01187 predicted_dc = vl;
01188 else if (FFABS(predicted_dc - vul) > 128)
01189 predicted_dc = vul;
01190 }
01191 }
01192
01193
01194 DC_COEFF(i) += predicted_dc;
01195
01196 last_dc[current_frame_type] = DC_COEFF(i);
01197 }
01198 }
01199 }
01200 }
01201
01202 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
01203 {
01204 int x, y;
01205 int *bounding_values= s->bounding_values_array+127;
01206
01207 int width = s->fragment_width[!!plane];
01208 int height = s->fragment_height[!!plane];
01209 int fragment = s->fragment_start [plane] + ystart * width;
01210 int stride = s->current_frame.linesize[plane];
01211 uint8_t *plane_data = s->current_frame.data [plane];
01212 if (!s->flipped_image) stride = -stride;
01213 plane_data += s->data_offset[plane] + 8*ystart*stride;
01214
01215 for (y = ystart; y < yend; y++) {
01216
01217 for (x = 0; x < width; x++) {
01218
01219
01220
01221
01222 if( s->all_fragments[fragment].coding_method != MODE_COPY )
01223 {
01224
01225 if (x > 0) {
01226 s->dsp.vp3_h_loop_filter(
01227 plane_data + 8*x,
01228 stride, bounding_values);
01229 }
01230
01231
01232 if (y > 0) {
01233 s->dsp.vp3_v_loop_filter(
01234 plane_data + 8*x,
01235 stride, bounding_values);
01236 }
01237
01238
01239
01240
01241 if ((x < width - 1) &&
01242 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
01243 s->dsp.vp3_h_loop_filter(
01244 plane_data + 8*x + 8,
01245 stride, bounding_values);
01246 }
01247
01248
01249
01250
01251 if ((y < height - 1) &&
01252 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
01253 s->dsp.vp3_v_loop_filter(
01254 plane_data + 8*x + 8*stride,
01255 stride, bounding_values);
01256 }
01257 }
01258
01259 fragment++;
01260 }
01261 plane_data += 8*stride;
01262 }
01263 }
01264
01269 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
01270 int plane, int inter, DCTELEM block[64])
01271 {
01272 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
01273 uint8_t *perm = s->scantable.permutated;
01274 int i = 0;
01275
01276 do {
01277 int token = *s->dct_tokens[plane][i];
01278 switch (token & 3) {
01279 case 0:
01280 if (--token < 4)
01281 s->dct_tokens[plane][i]++;
01282 else
01283 *s->dct_tokens[plane][i] = token & ~3;
01284 goto end;
01285 case 1:
01286 s->dct_tokens[plane][i]++;
01287 i += (token >> 2) & 0x7f;
01288 if(i>63){
01289 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
01290 return -1;
01291 }
01292 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
01293 i++;
01294 break;
01295 case 2:
01296 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
01297 s->dct_tokens[plane][i++]++;
01298 break;
01299 default:
01300 return i;
01301 }
01302 } while (i < 64);
01303 end:
01304
01305 block[0] = frag->dc * s->qmat[0][inter][plane][0];
01306 return i;
01307 }
01308
01312 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
01313 {
01314 int h, cy;
01315 int offset[4];
01316
01317 if(s->avctx->draw_horiz_band==NULL)
01318 return;
01319
01320 h= y - s->last_slice_end;
01321 y -= h;
01322
01323 if (!s->flipped_image) {
01324 if (y == 0)
01325 h -= s->height - s->avctx->height;
01326 y = s->height - y - h;
01327 }
01328
01329 cy = y >> 1;
01330 offset[0] = s->current_frame.linesize[0]*y;
01331 offset[1] = s->current_frame.linesize[1]*cy;
01332 offset[2] = s->current_frame.linesize[2]*cy;
01333 offset[3] = 0;
01334
01335 emms_c();
01336 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
01337 s->last_slice_end= y + h;
01338 }
01339
01340
01341
01342
01343
01344 static void render_slice(Vp3DecodeContext *s, int slice)
01345 {
01346 int x, y, i, j;
01347 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
01348 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
01349 int motion_halfpel_index;
01350 uint8_t *motion_source;
01351 int plane, first_pixel;
01352
01353 if (slice >= s->c_superblock_height)
01354 return;
01355
01356 for (plane = 0; plane < 3; plane++) {
01357 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
01358 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
01359 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
01360 int stride = s->current_frame.linesize[plane];
01361 int plane_width = s->width >> (plane && s->chroma_x_shift);
01362 int plane_height = s->height >> (plane && s->chroma_y_shift);
01363 int8_t (*motion_val)[2] = s->motion_val[!!plane];
01364
01365 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
01366 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
01367 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
01368
01369 int fragment_width = s->fragment_width[!!plane];
01370 int fragment_height = s->fragment_height[!!plane];
01371 int fragment_start = s->fragment_start[plane];
01372
01373 if (!s->flipped_image) stride = -stride;
01374 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
01375 continue;
01376
01377
01378 if(FFABS(stride) > 2048)
01379 return;
01380
01381
01382 for (; sb_y < slice_height; sb_y++) {
01383
01384
01385 for (sb_x = 0; sb_x < slice_width; sb_x++) {
01386
01387
01388 for (j = 0; j < 16; j++) {
01389 x = 4*sb_x + hilbert_offset[j][0];
01390 y = 4*sb_y + hilbert_offset[j][1];
01391
01392 i = fragment_start + y*fragment_width + x;
01393
01394
01395 if (x >= fragment_width || y >= fragment_height)
01396 continue;
01397
01398 first_pixel = 8*y*stride + 8*x;
01399
01400
01401 if (s->all_fragments[i].coding_method != MODE_COPY) {
01402 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
01403 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
01404 motion_source= golden_plane;
01405 else
01406 motion_source= last_plane;
01407
01408 motion_source += first_pixel;
01409 motion_halfpel_index = 0;
01410
01411
01412
01413 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
01414 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
01415 int src_x, src_y;
01416 motion_x = motion_val[y*fragment_width + x][0];
01417 motion_y = motion_val[y*fragment_width + x][1];
01418
01419 src_x= (motion_x>>1) + 8*x;
01420 src_y= (motion_y>>1) + 8*y;
01421
01422 motion_halfpel_index = motion_x & 0x01;
01423 motion_source += (motion_x >> 1);
01424
01425 motion_halfpel_index |= (motion_y & 0x01) << 1;
01426 motion_source += ((motion_y >> 1) * stride);
01427
01428 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
01429 uint8_t *temp= s->edge_emu_buffer;
01430 if(stride<0) temp -= 9*stride;
01431 else temp += 9*stride;
01432
01433 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
01434 motion_source= temp;
01435 }
01436 }
01437
01438
01439
01440
01441 if (s->all_fragments[i].coding_method != MODE_INTRA) {
01442
01443
01444
01445
01446 if(motion_halfpel_index != 3){
01447 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
01448 output_plane + first_pixel,
01449 motion_source, stride, 8);
01450 }else{
01451 int d= (motion_x ^ motion_y)>>31;
01452 s->dsp.put_no_rnd_pixels_l2[1](
01453 output_plane + first_pixel,
01454 motion_source - d,
01455 motion_source + stride + 1 + d,
01456 stride, 8);
01457 }
01458 }
01459
01460 s->dsp.clear_block(block);
01461
01462
01463
01464 if (s->all_fragments[i].coding_method == MODE_INTRA) {
01465 vp3_dequant(s, s->all_fragments + i, plane, 0, block);
01466 if(s->avctx->idct_algo!=FF_IDCT_VP3)
01467 block[0] += 128<<3;
01468 s->dsp.idct_put(
01469 output_plane + first_pixel,
01470 stride,
01471 block);
01472 } else {
01473 if (vp3_dequant(s, s->all_fragments + i, plane, 1, block)) {
01474 s->dsp.idct_add(
01475 output_plane + first_pixel,
01476 stride,
01477 block);
01478 } else {
01479 s->dsp.vp3_idct_dc_add(output_plane + first_pixel, stride, block);
01480 }
01481 }
01482 } else {
01483
01484
01485 s->dsp.put_pixels_tab[1][0](
01486 output_plane + first_pixel,
01487 last_plane + first_pixel,
01488 stride, 8);
01489
01490 }
01491 }
01492 }
01493
01494
01495 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1));
01496 }
01497 }
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507 vp3_draw_horiz_band(s, FFMIN(64*slice + 64-16, s->height-16));
01508 }
01509
01510
01511
01512
01513 static av_cold int vp3_decode_init(AVCodecContext *avctx)
01514 {
01515 Vp3DecodeContext *s = avctx->priv_data;
01516 int i, inter, plane;
01517 int c_width;
01518 int c_height;
01519 int y_fragment_count, c_fragment_count;
01520
01521 if (avctx->codec_tag == MKTAG('V','P','3','0'))
01522 s->version = 0;
01523 else
01524 s->version = 1;
01525
01526 s->avctx = avctx;
01527 s->width = FFALIGN(avctx->width, 16);
01528 s->height = FFALIGN(avctx->height, 16);
01529 if (avctx->pix_fmt == PIX_FMT_NONE)
01530 avctx->pix_fmt = PIX_FMT_YUV420P;
01531 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01532 if(avctx->idct_algo==FF_IDCT_AUTO)
01533 avctx->idct_algo=FF_IDCT_VP3;
01534 dsputil_init(&s->dsp, avctx);
01535
01536 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
01537
01538
01539
01540 for (i = 0; i < 3; i++)
01541 s->qps[i] = -1;
01542
01543 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
01544
01545 s->y_superblock_width = (s->width + 31) / 32;
01546 s->y_superblock_height = (s->height + 31) / 32;
01547 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
01548
01549
01550 c_width = s->width >> s->chroma_x_shift;
01551 c_height = s->height >> s->chroma_y_shift;
01552 s->c_superblock_width = (c_width + 31) / 32;
01553 s->c_superblock_height = (c_height + 31) / 32;
01554 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
01555
01556 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
01557 s->u_superblock_start = s->y_superblock_count;
01558 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
01559 s->superblock_coding = av_malloc(s->superblock_count);
01560
01561 s->macroblock_width = (s->width + 15) / 16;
01562 s->macroblock_height = (s->height + 15) / 16;
01563 s->macroblock_count = s->macroblock_width * s->macroblock_height;
01564
01565 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
01566 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
01567 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
01568 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
01569
01570
01571 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
01572 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
01573 s->fragment_count = y_fragment_count + 2*c_fragment_count;
01574 s->fragment_start[1] = y_fragment_count;
01575 s->fragment_start[2] = y_fragment_count + c_fragment_count;
01576
01577 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
01578 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
01579 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
01580 s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
01581 s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
01582
01583 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
01584 !s->coded_fragment_list[0] || !s->motion_val[0] || !s->motion_val[1]) {
01585 vp3_decode_end(avctx);
01586 return -1;
01587 }
01588
01589 if (!s->theora_tables)
01590 {
01591 for (i = 0; i < 64; i++) {
01592 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
01593 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
01594 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
01595 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
01596 s->base_matrix[2][i] = vp31_inter_dequant[i];
01597 s->filter_limit_values[i] = vp31_filter_limit_values[i];
01598 }
01599
01600 for(inter=0; inter<2; inter++){
01601 for(plane=0; plane<3; plane++){
01602 s->qr_count[inter][plane]= 1;
01603 s->qr_size [inter][plane][0]= 63;
01604 s->qr_base [inter][plane][0]=
01605 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
01606 }
01607 }
01608
01609
01610 for (i = 0; i < 16; i++) {
01611
01612
01613 init_vlc(&s->dc_vlc[i], 11, 32,
01614 &dc_bias[i][0][1], 4, 2,
01615 &dc_bias[i][0][0], 4, 2, 0);
01616
01617
01618 init_vlc(&s->ac_vlc_1[i], 11, 32,
01619 &ac_bias_0[i][0][1], 4, 2,
01620 &ac_bias_0[i][0][0], 4, 2, 0);
01621
01622
01623 init_vlc(&s->ac_vlc_2[i], 11, 32,
01624 &ac_bias_1[i][0][1], 4, 2,
01625 &ac_bias_1[i][0][0], 4, 2, 0);
01626
01627
01628 init_vlc(&s->ac_vlc_3[i], 11, 32,
01629 &ac_bias_2[i][0][1], 4, 2,
01630 &ac_bias_2[i][0][0], 4, 2, 0);
01631
01632
01633 init_vlc(&s->ac_vlc_4[i], 11, 32,
01634 &ac_bias_3[i][0][1], 4, 2,
01635 &ac_bias_3[i][0][0], 4, 2, 0);
01636 }
01637 } else {
01638
01639 for (i = 0; i < 16; i++) {
01640
01641 if (init_vlc(&s->dc_vlc[i], 11, 32,
01642 &s->huffman_table[i][0][1], 8, 4,
01643 &s->huffman_table[i][0][0], 8, 4, 0) < 0)
01644 goto vlc_fail;
01645
01646
01647 if (init_vlc(&s->ac_vlc_1[i], 11, 32,
01648 &s->huffman_table[i+16][0][1], 8, 4,
01649 &s->huffman_table[i+16][0][0], 8, 4, 0) < 0)
01650 goto vlc_fail;
01651
01652
01653 if (init_vlc(&s->ac_vlc_2[i], 11, 32,
01654 &s->huffman_table[i+16*2][0][1], 8, 4,
01655 &s->huffman_table[i+16*2][0][0], 8, 4, 0) < 0)
01656 goto vlc_fail;
01657
01658
01659 if (init_vlc(&s->ac_vlc_3[i], 11, 32,
01660 &s->huffman_table[i+16*3][0][1], 8, 4,
01661 &s->huffman_table[i+16*3][0][0], 8, 4, 0) < 0)
01662 goto vlc_fail;
01663
01664
01665 if (init_vlc(&s->ac_vlc_4[i], 11, 32,
01666 &s->huffman_table[i+16*4][0][1], 8, 4,
01667 &s->huffman_table[i+16*4][0][0], 8, 4, 0) < 0)
01668 goto vlc_fail;
01669 }
01670 }
01671
01672 init_vlc(&s->superblock_run_length_vlc, 6, 34,
01673 &superblock_run_length_vlc_table[0][1], 4, 2,
01674 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
01675
01676 init_vlc(&s->fragment_run_length_vlc, 5, 30,
01677 &fragment_run_length_vlc_table[0][1], 4, 2,
01678 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
01679
01680 init_vlc(&s->mode_code_vlc, 3, 8,
01681 &mode_code_vlc_table[0][1], 2, 1,
01682 &mode_code_vlc_table[0][0], 2, 1, 0);
01683
01684 init_vlc(&s->motion_vector_vlc, 6, 63,
01685 &motion_vector_vlc_table[0][1], 2, 1,
01686 &motion_vector_vlc_table[0][0], 2, 1, 0);
01687
01688
01689 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
01690 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
01691 if (!s->superblock_fragments || !s->macroblock_coding) {
01692 vp3_decode_end(avctx);
01693 return -1;
01694 }
01695 init_block_mapping(s);
01696
01697 for (i = 0; i < 3; i++) {
01698 s->current_frame.data[i] = NULL;
01699 s->last_frame.data[i] = NULL;
01700 s->golden_frame.data[i] = NULL;
01701 }
01702
01703 return 0;
01704
01705 vlc_fail:
01706 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
01707 return -1;
01708 }
01709
01710
01711
01712
01713 static int vp3_decode_frame(AVCodecContext *avctx,
01714 void *data, int *data_size,
01715 AVPacket *avpkt)
01716 {
01717 const uint8_t *buf = avpkt->data;
01718 int buf_size = avpkt->size;
01719 Vp3DecodeContext *s = avctx->priv_data;
01720 GetBitContext gb;
01721 static int counter = 0;
01722 int i;
01723
01724 init_get_bits(&gb, buf, buf_size * 8);
01725
01726 if (s->theora && get_bits1(&gb))
01727 {
01728 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
01729 return -1;
01730 }
01731
01732 s->keyframe = !get_bits1(&gb);
01733 if (!s->theora)
01734 skip_bits(&gb, 1);
01735 for (i = 0; i < 3; i++)
01736 s->last_qps[i] = s->qps[i];
01737
01738 s->nqps=0;
01739 do{
01740 s->qps[s->nqps++]= get_bits(&gb, 6);
01741 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
01742 for (i = s->nqps; i < 3; i++)
01743 s->qps[i] = -1;
01744
01745 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01746 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
01747 s->keyframe?"key":"", counter, s->qps[0]);
01748 counter++;
01749
01750 if (s->qps[0] != s->last_qps[0])
01751 init_loop_filter(s);
01752
01753 for (i = 0; i < s->nqps; i++)
01754
01755
01756 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
01757 init_dequantizer(s, i);
01758
01759 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
01760 return buf_size;
01761
01762 s->current_frame.reference = 3;
01763 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
01764 if (avctx->get_buffer(avctx, &s->current_frame) < 0) {
01765 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01766 goto error;
01767 }
01768
01769 if (s->keyframe) {
01770 if (!s->theora)
01771 {
01772 skip_bits(&gb, 4);
01773 skip_bits(&gb, 4);
01774 if (s->version)
01775 {
01776 s->version = get_bits(&gb, 5);
01777 if (counter == 1)
01778 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
01779 }
01780 }
01781 if (s->version || s->theora)
01782 {
01783 if (get_bits1(&gb))
01784 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
01785 skip_bits(&gb, 2);
01786 }
01787 } else {
01788 if (!s->golden_frame.data[0]) {
01789 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
01790
01791 s->golden_frame.reference = 3;
01792 s->golden_frame.pict_type = FF_I_TYPE;
01793 if (avctx->get_buffer(avctx, &s->golden_frame) < 0) {
01794 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01795 goto error;
01796 }
01797 s->last_frame = s->golden_frame;
01798 s->last_frame.type = FF_BUFFER_TYPE_COPY;
01799 }
01800 }
01801
01802 s->current_frame.qscale_table= s->qscale_table;
01803 s->current_frame.qstride= 0;
01804
01805 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
01806
01807 if (unpack_superblocks(s, &gb)){
01808 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
01809 goto error;
01810 }
01811 if (unpack_modes(s, &gb)){
01812 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
01813 goto error;
01814 }
01815 if (unpack_vectors(s, &gb)){
01816 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
01817 goto error;
01818 }
01819 if (unpack_block_qpis(s, &gb)){
01820 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
01821 goto error;
01822 }
01823 if (unpack_dct_coeffs(s, &gb)){
01824 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
01825 goto error;
01826 }
01827
01828 for (i = 0; i < 3; i++) {
01829 int height = s->height >> (i && s->chroma_y_shift);
01830 if (s->flipped_image)
01831 s->data_offset[i] = 0;
01832 else
01833 s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
01834 }
01835
01836 s->last_slice_end = 0;
01837 for (i = 0; i < s->c_superblock_height; i++)
01838 render_slice(s, i);
01839
01840
01841 for (i = 0; i < 3; i++) {
01842 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
01843 apply_loop_filter(s, i, row, row+1);
01844 }
01845 vp3_draw_horiz_band(s, s->height);
01846
01847 *data_size=sizeof(AVFrame);
01848 *(AVFrame*)data= s->current_frame;
01849
01850
01851
01852 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
01853 avctx->release_buffer(avctx, &s->last_frame);
01854
01855
01856 s->last_frame= s->current_frame;
01857
01858 if (s->keyframe) {
01859 if (s->golden_frame.data[0])
01860 avctx->release_buffer(avctx, &s->golden_frame);
01861 s->golden_frame = s->current_frame;
01862 s->last_frame.type = FF_BUFFER_TYPE_COPY;
01863 }
01864
01865 s->current_frame.data[0]= NULL;
01866
01867 return buf_size;
01868
01869 error:
01870 if (s->current_frame.data[0])
01871 avctx->release_buffer(avctx, &s->current_frame);
01872 return -1;
01873 }
01874
01875
01876
01877
01878 static av_cold int vp3_decode_end(AVCodecContext *avctx)
01879 {
01880 Vp3DecodeContext *s = avctx->priv_data;
01881 int i;
01882
01883 av_free(s->superblock_coding);
01884 av_free(s->all_fragments);
01885 av_free(s->coded_fragment_list[0]);
01886 av_free(s->dct_tokens_base);
01887 av_free(s->superblock_fragments);
01888 av_free(s->macroblock_coding);
01889 av_free(s->motion_val[0]);
01890 av_free(s->motion_val[1]);
01891
01892 for (i = 0; i < 16; i++) {
01893 free_vlc(&s->dc_vlc[i]);
01894 free_vlc(&s->ac_vlc_1[i]);
01895 free_vlc(&s->ac_vlc_2[i]);
01896 free_vlc(&s->ac_vlc_3[i]);
01897 free_vlc(&s->ac_vlc_4[i]);
01898 }
01899
01900 free_vlc(&s->superblock_run_length_vlc);
01901 free_vlc(&s->fragment_run_length_vlc);
01902 free_vlc(&s->mode_code_vlc);
01903 free_vlc(&s->motion_vector_vlc);
01904
01905
01906 if (s->golden_frame.data[0])
01907 avctx->release_buffer(avctx, &s->golden_frame);
01908 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
01909 avctx->release_buffer(avctx, &s->last_frame);
01910
01911
01912
01913 return 0;
01914 }
01915
01916 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
01917 {
01918 Vp3DecodeContext *s = avctx->priv_data;
01919
01920 if (get_bits1(gb)) {
01921 int token;
01922 if (s->entries >= 32) {
01923 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
01924 return -1;
01925 }
01926 token = get_bits(gb, 5);
01927
01928 s->huffman_table[s->hti][token][0] = s->hbits;
01929 s->huffman_table[s->hti][token][1] = s->huff_code_size;
01930 s->entries++;
01931 }
01932 else {
01933 if (s->huff_code_size >= 32) {
01934 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
01935 return -1;
01936 }
01937 s->huff_code_size++;
01938 s->hbits <<= 1;
01939 if (read_huffman_tree(avctx, gb))
01940 return -1;
01941 s->hbits |= 1;
01942 if (read_huffman_tree(avctx, gb))
01943 return -1;
01944 s->hbits >>= 1;
01945 s->huff_code_size--;
01946 }
01947 return 0;
01948 }
01949
01950 #if CONFIG_THEORA_DECODER
01951 static const enum PixelFormat theora_pix_fmts[4] = {
01952 PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P
01953 };
01954
01955 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
01956 {
01957 Vp3DecodeContext *s = avctx->priv_data;
01958 int visible_width, visible_height, colorspace;
01959 int offset_x = 0, offset_y = 0;
01960 AVRational fps;
01961
01962 s->theora = get_bits_long(gb, 24);
01963 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
01964
01965
01966
01967 if (s->theora < 0x030200)
01968 {
01969 s->flipped_image = 1;
01970 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
01971 }
01972
01973 visible_width = s->width = get_bits(gb, 16) << 4;
01974 visible_height = s->height = get_bits(gb, 16) << 4;
01975
01976 if(avcodec_check_dimensions(avctx, s->width, s->height)){
01977 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
01978 s->width= s->height= 0;
01979 return -1;
01980 }
01981
01982 if (s->theora >= 0x030200) {
01983 visible_width = get_bits_long(gb, 24);
01984 visible_height = get_bits_long(gb, 24);
01985
01986 offset_x = get_bits(gb, 8);
01987 offset_y = get_bits(gb, 8);
01988 }
01989
01990 fps.num = get_bits_long(gb, 32);
01991 fps.den = get_bits_long(gb, 32);
01992 if (fps.num && fps.den) {
01993 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
01994 fps.den, fps.num, 1<<30);
01995 }
01996
01997 avctx->sample_aspect_ratio.num = get_bits_long(gb, 24);
01998 avctx->sample_aspect_ratio.den = get_bits_long(gb, 24);
01999
02000 if (s->theora < 0x030200)
02001 skip_bits(gb, 5);
02002 colorspace = get_bits(gb, 8);
02003 skip_bits(gb, 24);
02004
02005 skip_bits(gb, 6);
02006
02007 if (s->theora >= 0x030200)
02008 {
02009 skip_bits(gb, 5);
02010 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
02011 skip_bits(gb, 3);
02012 }
02013
02014
02015
02016 if ( visible_width <= s->width && visible_width > s->width-16
02017 && visible_height <= s->height && visible_height > s->height-16
02018 && !offset_x && (offset_y == s->height - visible_height))
02019 avcodec_set_dimensions(avctx, visible_width, visible_height);
02020 else
02021 avcodec_set_dimensions(avctx, s->width, s->height);
02022
02023 if (colorspace == 1) {
02024 avctx->color_primaries = AVCOL_PRI_BT470M;
02025 } else if (colorspace == 2) {
02026 avctx->color_primaries = AVCOL_PRI_BT470BG;
02027 }
02028 if (colorspace == 1 || colorspace == 2) {
02029 avctx->colorspace = AVCOL_SPC_BT470BG;
02030 avctx->color_trc = AVCOL_TRC_BT709;
02031 }
02032
02033 return 0;
02034 }
02035
02036 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
02037 {
02038 Vp3DecodeContext *s = avctx->priv_data;
02039 int i, n, matrices, inter, plane;
02040
02041 if (s->theora >= 0x030200) {
02042 n = get_bits(gb, 3);
02043
02044 for (i = 0; i < 64; i++) {
02045 s->filter_limit_values[i] = get_bits(gb, n);
02046 if (s->filter_limit_values[i] > 127) {
02047 av_log(avctx, AV_LOG_ERROR, "filter limit value too large (%i > 127), clamping\n", s->filter_limit_values[i]);
02048 s->filter_limit_values[i] = 127;
02049 }
02050 }
02051 }
02052
02053 if (s->theora >= 0x030200)
02054 n = get_bits(gb, 4) + 1;
02055 else
02056 n = 16;
02057
02058 for (i = 0; i < 64; i++)
02059 s->coded_ac_scale_factor[i] = get_bits(gb, n);
02060
02061 if (s->theora >= 0x030200)
02062 n = get_bits(gb, 4) + 1;
02063 else
02064 n = 16;
02065
02066 for (i = 0; i < 64; i++)
02067 s->coded_dc_scale_factor[i] = get_bits(gb, n);
02068
02069 if (s->theora >= 0x030200)
02070 matrices = get_bits(gb, 9) + 1;
02071 else
02072 matrices = 3;
02073
02074 if(matrices > 384){
02075 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
02076 return -1;
02077 }
02078
02079 for(n=0; n<matrices; n++){
02080 for (i = 0; i < 64; i++)
02081 s->base_matrix[n][i]= get_bits(gb, 8);
02082 }
02083
02084 for (inter = 0; inter <= 1; inter++) {
02085 for (plane = 0; plane <= 2; plane++) {
02086 int newqr= 1;
02087 if (inter || plane > 0)
02088 newqr = get_bits1(gb);
02089 if (!newqr) {
02090 int qtj, plj;
02091 if(inter && get_bits1(gb)){
02092 qtj = 0;
02093 plj = plane;
02094 }else{
02095 qtj= (3*inter + plane - 1) / 3;
02096 plj= (plane + 2) % 3;
02097 }
02098 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
02099 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
02100 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
02101 } else {
02102 int qri= 0;
02103 int qi = 0;
02104
02105 for(;;){
02106 i= get_bits(gb, av_log2(matrices-1)+1);
02107 if(i>= matrices){
02108 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
02109 return -1;
02110 }
02111 s->qr_base[inter][plane][qri]= i;
02112 if(qi >= 63)
02113 break;
02114 i = get_bits(gb, av_log2(63-qi)+1) + 1;
02115 s->qr_size[inter][plane][qri++]= i;
02116 qi += i;
02117 }
02118
02119 if (qi > 63) {
02120 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
02121 return -1;
02122 }
02123 s->qr_count[inter][plane]= qri;
02124 }
02125 }
02126 }
02127
02128
02129 for (s->hti = 0; s->hti < 80; s->hti++) {
02130 s->entries = 0;
02131 s->huff_code_size = 1;
02132 if (!get_bits1(gb)) {
02133 s->hbits = 0;
02134 if(read_huffman_tree(avctx, gb))
02135 return -1;
02136 s->hbits = 1;
02137 if(read_huffman_tree(avctx, gb))
02138 return -1;
02139 }
02140 }
02141
02142 s->theora_tables = 1;
02143
02144 return 0;
02145 }
02146
02147 static av_cold int theora_decode_init(AVCodecContext *avctx)
02148 {
02149 Vp3DecodeContext *s = avctx->priv_data;
02150 GetBitContext gb;
02151 int ptype;
02152 uint8_t *header_start[3];
02153 int header_len[3];
02154 int i;
02155
02156 s->theora = 1;
02157
02158 if (!avctx->extradata_size)
02159 {
02160 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
02161 return -1;
02162 }
02163
02164 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
02165 42, header_start, header_len) < 0) {
02166 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
02167 return -1;
02168 }
02169
02170 for(i=0;i<3;i++) {
02171 init_get_bits(&gb, header_start[i], header_len[i] * 8);
02172
02173 ptype = get_bits(&gb, 8);
02174
02175 if (!(ptype & 0x80))
02176 {
02177 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
02178
02179 }
02180
02181
02182 skip_bits_long(&gb, 6*8);
02183
02184 switch(ptype)
02185 {
02186 case 0x80:
02187 theora_decode_header(avctx, &gb);
02188 break;
02189 case 0x81:
02190
02191
02192 break;
02193 case 0x82:
02194 if (theora_decode_tables(avctx, &gb))
02195 return -1;
02196 break;
02197 default:
02198 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
02199 break;
02200 }
02201 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
02202 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
02203 if (s->theora < 0x030200)
02204 break;
02205 }
02206
02207 return vp3_decode_init(avctx);
02208 }
02209
02210 AVCodec theora_decoder = {
02211 "theora",
02212 AVMEDIA_TYPE_VIDEO,
02213 CODEC_ID_THEORA,
02214 sizeof(Vp3DecodeContext),
02215 theora_decode_init,
02216 NULL,
02217 vp3_decode_end,
02218 vp3_decode_frame,
02219 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
02220 NULL,
02221 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
02222 };
02223 #endif
02224
02225 AVCodec vp3_decoder = {
02226 "vp3",
02227 AVMEDIA_TYPE_VIDEO,
02228 CODEC_ID_VP3,
02229 sizeof(Vp3DecodeContext),
02230 vp3_decode_init,
02231 NULL,
02232 vp3_decode_end,
02233 vp3_decode_frame,
02234 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
02235 NULL,
02236 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
02237 };