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