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