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 "vp3dsp.h"
00044 #include "xiph.h"
00045 #include "thread.h"
00046
00047 #define FRAGMENT_PIXELS 8
00048
00049
00050 typedef struct Vp3Fragment {
00051 int16_t dc;
00052 uint8_t coding_method;
00053 uint8_t qpi;
00054 } Vp3Fragment;
00055
00056 #define SB_NOT_CODED 0
00057 #define SB_PARTIALLY_CODED 1
00058 #define SB_FULLY_CODED 2
00059
00060
00061
00062
00063 #define MAXIMUM_LONG_BIT_RUN 4129
00064
00065 #define MODE_INTER_NO_MV 0
00066 #define MODE_INTRA 1
00067 #define MODE_INTER_PLUS_MV 2
00068 #define MODE_INTER_LAST_MV 3
00069 #define MODE_INTER_PRIOR_LAST 4
00070 #define MODE_USING_GOLDEN 5
00071 #define MODE_GOLDEN_MV 6
00072 #define MODE_INTER_FOURMV 7
00073 #define CODING_MODE_COUNT 8
00074
00075
00076 #define MODE_COPY 8
00077
00078
00079 static const int ModeAlphabet[6][CODING_MODE_COUNT] =
00080 {
00081
00082 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00083 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
00084 MODE_INTRA, MODE_USING_GOLDEN,
00085 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00086
00087
00088 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00089 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
00090 MODE_INTRA, MODE_USING_GOLDEN,
00091 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00092
00093
00094 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00095 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
00096 MODE_INTRA, MODE_USING_GOLDEN,
00097 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00098
00099
00100 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
00101 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
00102 MODE_INTRA, MODE_USING_GOLDEN,
00103 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00104
00105
00106 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
00107 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
00108 MODE_INTRA, MODE_USING_GOLDEN,
00109 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00110
00111
00112 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
00113 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
00114 MODE_INTER_PLUS_MV, MODE_INTRA,
00115 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
00116
00117 };
00118
00119 static const uint8_t hilbert_offset[16][2] = {
00120 {0,0}, {1,0}, {1,1}, {0,1},
00121 {0,2}, {0,3}, {1,3}, {1,2},
00122 {2,2}, {2,3}, {3,3}, {3,2},
00123 {3,1}, {2,1}, {2,0}, {3,0}
00124 };
00125
00126 #define MIN_DEQUANT_VAL 2
00127
00128 typedef struct Vp3DecodeContext {
00129 AVCodecContext *avctx;
00130 int theora, theora_tables;
00131 int version;
00132 int width, height;
00133 int chroma_x_shift, chroma_y_shift;
00134 AVFrame golden_frame;
00135 AVFrame last_frame;
00136 AVFrame current_frame;
00137 int keyframe;
00138 DSPContext dsp;
00139 VP3DSPContext vp3dsp;
00140 int flipped_image;
00141 int last_slice_end;
00142 int skip_loop_filter;
00143
00144 int qps[3];
00145 int nqps;
00146 int last_qps[3];
00147
00148 int superblock_count;
00149 int y_superblock_width;
00150 int y_superblock_height;
00151 int y_superblock_count;
00152 int c_superblock_width;
00153 int c_superblock_height;
00154 int c_superblock_count;
00155 int u_superblock_start;
00156 int v_superblock_start;
00157 unsigned char *superblock_coding;
00158
00159 int macroblock_count;
00160 int macroblock_width;
00161 int macroblock_height;
00162
00163 int fragment_count;
00164 int fragment_width[2];
00165 int fragment_height[2];
00166
00167 Vp3Fragment *all_fragments;
00168 int fragment_start[3];
00169 int data_offset[3];
00170
00171 int8_t (*motion_val[2])[2];
00172
00173 ScanTable scantable;
00174
00175
00176 uint16_t coded_dc_scale_factor[64];
00177 uint32_t coded_ac_scale_factor[64];
00178 uint8_t base_matrix[384][64];
00179 uint8_t qr_count[2][3];
00180 uint8_t qr_size [2][3][64];
00181 uint16_t qr_base[2][3][64];
00182
00200 int16_t *dct_tokens[3][64];
00201 int16_t *dct_tokens_base;
00202 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
00203 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
00204 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
00205
00209 int num_coded_frags[3][64];
00210 int total_num_coded_frags;
00211
00212
00213
00214 int *coded_fragment_list[3];
00215
00216 VLC dc_vlc[16];
00217 VLC ac_vlc_1[16];
00218 VLC ac_vlc_2[16];
00219 VLC ac_vlc_3[16];
00220 VLC ac_vlc_4[16];
00221
00222 VLC superblock_run_length_vlc;
00223 VLC fragment_run_length_vlc;
00224 VLC mode_code_vlc;
00225 VLC motion_vector_vlc;
00226
00227
00228
00229 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];
00230
00231
00232
00233
00234
00235 int *superblock_fragments;
00236
00237
00238
00239 unsigned char *macroblock_coding;
00240
00241 uint8_t *edge_emu_buffer;
00242
00243
00244 int hti;
00245 unsigned int hbits;
00246 int entries;
00247 int huff_code_size;
00248 uint32_t huffman_table[80][32][2];
00249
00250 uint8_t filter_limit_values[64];
00251 DECLARE_ALIGNED(8, int, bounding_values_array)[256+2];
00252 } Vp3DecodeContext;
00253
00254
00255
00256
00257
00258 static void vp3_decode_flush(AVCodecContext *avctx)
00259 {
00260 Vp3DecodeContext *s = avctx->priv_data;
00261
00262 if (s->golden_frame.data[0]) {
00263 if (s->golden_frame.data[0] == s->last_frame.data[0])
00264 memset(&s->last_frame, 0, sizeof(AVFrame));
00265 if (s->current_frame.data[0] == s->golden_frame.data[0])
00266 memset(&s->current_frame, 0, sizeof(AVFrame));
00267 ff_thread_release_buffer(avctx, &s->golden_frame);
00268 }
00269 if (s->last_frame.data[0]) {
00270 if (s->current_frame.data[0] == s->last_frame.data[0])
00271 memset(&s->current_frame, 0, sizeof(AVFrame));
00272 ff_thread_release_buffer(avctx, &s->last_frame);
00273 }
00274 if (s->current_frame.data[0])
00275 ff_thread_release_buffer(avctx, &s->current_frame);
00276 }
00277
00278 static av_cold int vp3_decode_end(AVCodecContext *avctx)
00279 {
00280 Vp3DecodeContext *s = avctx->priv_data;
00281 int i;
00282
00283 av_free(s->superblock_coding);
00284 av_free(s->all_fragments);
00285 av_free(s->coded_fragment_list[0]);
00286 av_free(s->dct_tokens_base);
00287 av_free(s->superblock_fragments);
00288 av_free(s->macroblock_coding);
00289 av_free(s->motion_val[0]);
00290 av_free(s->motion_val[1]);
00291 av_free(s->edge_emu_buffer);
00292
00293 if (avctx->internal->is_copy)
00294 return 0;
00295
00296 for (i = 0; i < 16; i++) {
00297 ff_free_vlc(&s->dc_vlc[i]);
00298 ff_free_vlc(&s->ac_vlc_1[i]);
00299 ff_free_vlc(&s->ac_vlc_2[i]);
00300 ff_free_vlc(&s->ac_vlc_3[i]);
00301 ff_free_vlc(&s->ac_vlc_4[i]);
00302 }
00303
00304 ff_free_vlc(&s->superblock_run_length_vlc);
00305 ff_free_vlc(&s->fragment_run_length_vlc);
00306 ff_free_vlc(&s->mode_code_vlc);
00307 ff_free_vlc(&s->motion_vector_vlc);
00308
00309
00310 vp3_decode_flush(avctx);
00311
00312 return 0;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322 static int init_block_mapping(Vp3DecodeContext *s)
00323 {
00324 int sb_x, sb_y, plane;
00325 int x, y, i, j = 0;
00326
00327 for (plane = 0; plane < 3; plane++) {
00328 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
00329 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
00330 int frag_width = s->fragment_width[!!plane];
00331 int frag_height = s->fragment_height[!!plane];
00332
00333 for (sb_y = 0; sb_y < sb_height; sb_y++)
00334 for (sb_x = 0; sb_x < sb_width; sb_x++)
00335 for (i = 0; i < 16; i++) {
00336 x = 4*sb_x + hilbert_offset[i][0];
00337 y = 4*sb_y + hilbert_offset[i][1];
00338
00339 if (x < frag_width && y < frag_height)
00340 s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x;
00341 else
00342 s->superblock_fragments[j++] = -1;
00343 }
00344 }
00345
00346 return 0;
00347 }
00348
00349
00350
00351
00352
00353 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
00354 {
00355 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
00356 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
00357 int i, plane, inter, qri, bmi, bmj, qistart;
00358
00359 for(inter=0; inter<2; inter++){
00360 for(plane=0; plane<3; plane++){
00361 int sum=0;
00362 for(qri=0; qri<s->qr_count[inter][plane]; qri++){
00363 sum+= s->qr_size[inter][plane][qri];
00364 if(s->qps[qpi] <= sum)
00365 break;
00366 }
00367 qistart= sum - s->qr_size[inter][plane][qri];
00368 bmi= s->qr_base[inter][plane][qri ];
00369 bmj= s->qr_base[inter][plane][qri+1];
00370 for(i=0; i<64; i++){
00371 int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
00372 - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
00373 + s->qr_size[inter][plane][qri])
00374 / (2*s->qr_size[inter][plane][qri]);
00375
00376 int qmin= 8<<(inter + !i);
00377 int qscale= i ? ac_scale_factor : dc_scale_factor;
00378
00379 s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
00380 }
00381
00382 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
00383 }
00384 }
00385 }
00386
00387
00388
00389
00390
00391
00392
00393 static void init_loop_filter(Vp3DecodeContext *s)
00394 {
00395 int *bounding_values= s->bounding_values_array+127;
00396 int filter_limit;
00397 int x;
00398 int value;
00399
00400 filter_limit = s->filter_limit_values[s->qps[0]];
00401 av_assert0(filter_limit < 128U);
00402
00403
00404 memset(s->bounding_values_array, 0, 256 * sizeof(int));
00405 for (x = 0; x < filter_limit; x++) {
00406 bounding_values[-x] = -x;
00407 bounding_values[x] = x;
00408 }
00409 for (x = value = filter_limit; x < 128 && value; x++, value--) {
00410 bounding_values[ x] = value;
00411 bounding_values[-x] = -value;
00412 }
00413 if (value)
00414 bounding_values[128] = value;
00415 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
00416 }
00417
00418
00419
00420
00421
00422 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
00423 {
00424 int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start };
00425 int bit = 0;
00426 int current_superblock = 0;
00427 int current_run = 0;
00428 int num_partial_superblocks = 0;
00429
00430 int i, j;
00431 int current_fragment;
00432 int plane;
00433
00434 if (s->keyframe) {
00435 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
00436
00437 } else {
00438
00439
00440 bit = get_bits1(gb) ^ 1;
00441 current_run = 0;
00442
00443 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
00444 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
00445 bit = get_bits1(gb);
00446 else
00447 bit ^= 1;
00448
00449 current_run = get_vlc2(gb,
00450 s->superblock_run_length_vlc.table, 6, 2) + 1;
00451 if (current_run == 34)
00452 current_run += get_bits(gb, 12);
00453
00454 if (current_superblock + current_run > s->superblock_count) {
00455 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
00456 return -1;
00457 }
00458
00459 memset(s->superblock_coding + current_superblock, bit, current_run);
00460
00461 current_superblock += current_run;
00462 if (bit)
00463 num_partial_superblocks += current_run;
00464 }
00465
00466
00467
00468 if (num_partial_superblocks < s->superblock_count) {
00469 int superblocks_decoded = 0;
00470
00471 current_superblock = 0;
00472 bit = get_bits1(gb) ^ 1;
00473 current_run = 0;
00474
00475 while (superblocks_decoded < s->superblock_count - num_partial_superblocks
00476 && get_bits_left(gb) > 0) {
00477
00478 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
00479 bit = get_bits1(gb);
00480 else
00481 bit ^= 1;
00482
00483 current_run = get_vlc2(gb,
00484 s->superblock_run_length_vlc.table, 6, 2) + 1;
00485 if (current_run == 34)
00486 current_run += get_bits(gb, 12);
00487
00488 for (j = 0; j < current_run; current_superblock++) {
00489 if (current_superblock >= s->superblock_count) {
00490 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n");
00491 return -1;
00492 }
00493
00494
00495 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
00496 s->superblock_coding[current_superblock] = 2*bit;
00497 j++;
00498 }
00499 }
00500 superblocks_decoded += current_run;
00501 }
00502 }
00503
00504
00505
00506 if (num_partial_superblocks) {
00507
00508 current_run = 0;
00509 bit = get_bits1(gb);
00510
00511
00512 bit ^= 1;
00513 }
00514 }
00515
00516
00517
00518 s->total_num_coded_frags = 0;
00519 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
00520
00521 for (plane = 0; plane < 3; plane++) {
00522 int sb_start = superblock_starts[plane];
00523 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count);
00524 int num_coded_frags = 0;
00525
00526 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
00527
00528
00529 for (j = 0; j < 16; j++) {
00530
00531
00532 current_fragment = s->superblock_fragments[i * 16 + j];
00533 if (current_fragment != -1) {
00534 int coded = s->superblock_coding[i];
00535
00536 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
00537
00538
00539
00540 if (current_run-- == 0) {
00541 bit ^= 1;
00542 current_run = get_vlc2(gb,
00543 s->fragment_run_length_vlc.table, 5, 2);
00544 }
00545 coded = bit;
00546 }
00547
00548 if (coded) {
00549
00550
00551 s->all_fragments[current_fragment].coding_method =
00552 MODE_INTER_NO_MV;
00553 s->coded_fragment_list[plane][num_coded_frags++] =
00554 current_fragment;
00555 } else {
00556
00557 s->all_fragments[current_fragment].coding_method =
00558 MODE_COPY;
00559 }
00560 }
00561 }
00562 }
00563 s->total_num_coded_frags += num_coded_frags;
00564 for (i = 0; i < 64; i++)
00565 s->num_coded_frags[plane][i] = num_coded_frags;
00566 if (plane < 2)
00567 s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags;
00568 }
00569 return 0;
00570 }
00571
00572
00573
00574
00575
00576 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
00577 {
00578 int i, j, k, sb_x, sb_y;
00579 int scheme;
00580 int current_macroblock;
00581 int current_fragment;
00582 int coding_mode;
00583 int custom_mode_alphabet[CODING_MODE_COUNT];
00584 const int *alphabet;
00585 Vp3Fragment *frag;
00586
00587 if (s->keyframe) {
00588 for (i = 0; i < s->fragment_count; i++)
00589 s->all_fragments[i].coding_method = MODE_INTRA;
00590
00591 } else {
00592
00593
00594 scheme = get_bits(gb, 3);
00595
00596
00597 if (scheme == 0) {
00598 for (i = 0; i < 8; i++)
00599 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
00600 for (i = 0; i < 8; i++)
00601 custom_mode_alphabet[get_bits(gb, 3)] = i;
00602 alphabet = custom_mode_alphabet;
00603 } else
00604 alphabet = ModeAlphabet[scheme-1];
00605
00606
00607
00608 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
00609 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
00610 if (get_bits_left(gb) <= 0)
00611 return -1;
00612
00613 for (j = 0; j < 4; j++) {
00614 int mb_x = 2*sb_x + (j>>1);
00615 int mb_y = 2*sb_y + (((j>>1)+j)&1);
00616 current_macroblock = mb_y * s->macroblock_width + mb_x;
00617
00618 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
00619 continue;
00620
00621 #define BLOCK_X (2*mb_x + (k&1))
00622 #define BLOCK_Y (2*mb_y + (k>>1))
00623
00624
00625 for (k = 0; k < 4; k++) {
00626 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00627 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
00628 break;
00629 }
00630 if (k == 4) {
00631 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
00632 continue;
00633 }
00634
00635
00636 if (scheme == 7)
00637 coding_mode = get_bits(gb, 3);
00638 else
00639 coding_mode = alphabet
00640 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
00641
00642 s->macroblock_coding[current_macroblock] = coding_mode;
00643 for (k = 0; k < 4; k++) {
00644 frag = s->all_fragments + BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00645 if (frag->coding_method != MODE_COPY)
00646 frag->coding_method = coding_mode;
00647 }
00648
00649 #define SET_CHROMA_MODES \
00650 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
00651 frag[s->fragment_start[1]].coding_method = coding_mode;\
00652 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
00653 frag[s->fragment_start[2]].coding_method = coding_mode;
00654
00655 if (s->chroma_y_shift) {
00656 frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;
00657 SET_CHROMA_MODES
00658 } else if (s->chroma_x_shift) {
00659 frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;
00660 for (k = 0; k < 2; k++) {
00661 SET_CHROMA_MODES
00662 frag += s->fragment_width[1];
00663 }
00664 } else {
00665 for (k = 0; k < 4; k++) {
00666 frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;
00667 SET_CHROMA_MODES
00668 }
00669 }
00670 }
00671 }
00672 }
00673 }
00674
00675 return 0;
00676 }
00677
00678
00679
00680
00681
00682 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
00683 {
00684 int j, k, sb_x, sb_y;
00685 int coding_mode;
00686 int motion_x[4];
00687 int motion_y[4];
00688 int last_motion_x = 0;
00689 int last_motion_y = 0;
00690 int prior_last_motion_x = 0;
00691 int prior_last_motion_y = 0;
00692 int current_macroblock;
00693 int current_fragment;
00694 int frag;
00695
00696 if (s->keyframe)
00697 return 0;
00698
00699
00700 coding_mode = get_bits1(gb);
00701
00702
00703
00704 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
00705 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
00706 if (get_bits_left(gb) <= 0)
00707 return -1;
00708
00709 for (j = 0; j < 4; j++) {
00710 int mb_x = 2*sb_x + (j>>1);
00711 int mb_y = 2*sb_y + (((j>>1)+j)&1);
00712 current_macroblock = mb_y * s->macroblock_width + mb_x;
00713
00714 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
00715 (s->macroblock_coding[current_macroblock] == MODE_COPY))
00716 continue;
00717
00718 switch (s->macroblock_coding[current_macroblock]) {
00719
00720 case MODE_INTER_PLUS_MV:
00721 case MODE_GOLDEN_MV:
00722
00723 if (coding_mode == 0) {
00724 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00725 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00726 } else {
00727 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
00728 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
00729 }
00730
00731
00732 if (s->macroblock_coding[current_macroblock] ==
00733 MODE_INTER_PLUS_MV) {
00734 prior_last_motion_x = last_motion_x;
00735 prior_last_motion_y = last_motion_y;
00736 last_motion_x = motion_x[0];
00737 last_motion_y = motion_y[0];
00738 }
00739 break;
00740
00741 case MODE_INTER_FOURMV:
00742
00743 prior_last_motion_x = last_motion_x;
00744 prior_last_motion_y = last_motion_y;
00745
00746
00747
00748 for (k = 0; k < 4; k++) {
00749 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00750 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
00751 if (coding_mode == 0) {
00752 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00753 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
00754 } else {
00755 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
00756 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
00757 }
00758 last_motion_x = motion_x[k];
00759 last_motion_y = motion_y[k];
00760 } else {
00761 motion_x[k] = 0;
00762 motion_y[k] = 0;
00763 }
00764 }
00765 break;
00766
00767 case MODE_INTER_LAST_MV:
00768
00769 motion_x[0] = last_motion_x;
00770 motion_y[0] = last_motion_y;
00771
00772
00773
00774 break;
00775
00776 case MODE_INTER_PRIOR_LAST:
00777
00778
00779 motion_x[0] = prior_last_motion_x;
00780 motion_y[0] = prior_last_motion_y;
00781
00782
00783 prior_last_motion_x = last_motion_x;
00784 prior_last_motion_y = last_motion_y;
00785 last_motion_x = motion_x[0];
00786 last_motion_y = motion_y[0];
00787 break;
00788
00789 default:
00790
00791 motion_x[0] = 0;
00792 motion_y[0] = 0;
00793
00794
00795 break;
00796 }
00797
00798
00799 for (k = 0; k < 4; k++) {
00800 current_fragment =
00801 BLOCK_Y*s->fragment_width[0] + BLOCK_X;
00802 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00803 s->motion_val[0][current_fragment][0] = motion_x[k];
00804 s->motion_val[0][current_fragment][1] = motion_y[k];
00805 } else {
00806 s->motion_val[0][current_fragment][0] = motion_x[0];
00807 s->motion_val[0][current_fragment][1] = motion_y[0];
00808 }
00809 }
00810
00811 if (s->chroma_y_shift) {
00812 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00813 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
00814 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
00815 }
00816 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
00817 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
00818 frag = mb_y*s->fragment_width[1] + mb_x;
00819 s->motion_val[1][frag][0] = motion_x[0];
00820 s->motion_val[1][frag][1] = motion_y[0];
00821 } else if (s->chroma_x_shift) {
00822 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00823 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
00824 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
00825 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
00826 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
00827 } else {
00828 motion_x[1] = motion_x[0];
00829 motion_y[1] = motion_y[0];
00830 }
00831 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
00832 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
00833
00834 frag = 2*mb_y*s->fragment_width[1] + mb_x;
00835 for (k = 0; k < 2; k++) {
00836 s->motion_val[1][frag][0] = motion_x[k];
00837 s->motion_val[1][frag][1] = motion_y[k];
00838 frag += s->fragment_width[1];
00839 }
00840 } else {
00841 for (k = 0; k < 4; k++) {
00842 frag = BLOCK_Y*s->fragment_width[1] + BLOCK_X;
00843 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
00844 s->motion_val[1][frag][0] = motion_x[k];
00845 s->motion_val[1][frag][1] = motion_y[k];
00846 } else {
00847 s->motion_val[1][frag][0] = motion_x[0];
00848 s->motion_val[1][frag][1] = motion_y[0];
00849 }
00850 }
00851 }
00852 }
00853 }
00854 }
00855
00856 return 0;
00857 }
00858
00859 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
00860 {
00861 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
00862 int num_blocks = s->total_num_coded_frags;
00863
00864 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
00865 i = blocks_decoded = num_blocks_at_qpi = 0;
00866
00867 bit = get_bits1(gb) ^ 1;
00868 run_length = 0;
00869
00870 do {
00871 if (run_length == MAXIMUM_LONG_BIT_RUN)
00872 bit = get_bits1(gb);
00873 else
00874 bit ^= 1;
00875
00876 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
00877 if (run_length == 34)
00878 run_length += get_bits(gb, 12);
00879 blocks_decoded += run_length;
00880
00881 if (!bit)
00882 num_blocks_at_qpi += run_length;
00883
00884 for (j = 0; j < run_length; i++) {
00885 if (i >= s->total_num_coded_frags)
00886 return -1;
00887
00888 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
00889 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
00890 j++;
00891 }
00892 }
00893 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
00894
00895 num_blocks -= num_blocks_at_qpi;
00896 }
00897
00898 return 0;
00899 }
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
00914 VLC *table, int coeff_index,
00915 int plane,
00916 int eob_run)
00917 {
00918 int i, j = 0;
00919 int token;
00920 int zero_run = 0;
00921 DCTELEM coeff = 0;
00922 int bits_to_get;
00923 int blocks_ended;
00924 int coeff_i = 0;
00925 int num_coeffs = s->num_coded_frags[plane][coeff_index];
00926 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
00927
00928
00929 int *coded_fragment_list = s->coded_fragment_list[plane];
00930 Vp3Fragment *all_fragments = s->all_fragments;
00931 VLC_TYPE (*vlc_table)[2] = table->table;
00932
00933 if (num_coeffs < 0)
00934 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index);
00935
00936 if (eob_run > num_coeffs) {
00937 coeff_i = blocks_ended = num_coeffs;
00938 eob_run -= num_coeffs;
00939 } else {
00940 coeff_i = blocks_ended = eob_run;
00941 eob_run = 0;
00942 }
00943
00944
00945 if (blocks_ended)
00946 dct_tokens[j++] = blocks_ended << 2;
00947
00948 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
00949
00950 token = get_vlc2(gb, vlc_table, 11, 3);
00951
00952 if ((unsigned) token <= 6U) {
00953 eob_run = eob_run_base[token];
00954 if (eob_run_get_bits[token])
00955 eob_run += get_bits(gb, eob_run_get_bits[token]);
00956
00957
00958
00959 if (eob_run > num_coeffs - coeff_i) {
00960 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
00961 blocks_ended += num_coeffs - coeff_i;
00962 eob_run -= num_coeffs - coeff_i;
00963 coeff_i = num_coeffs;
00964 } else {
00965 dct_tokens[j++] = TOKEN_EOB(eob_run);
00966 blocks_ended += eob_run;
00967 coeff_i += eob_run;
00968 eob_run = 0;
00969 }
00970 } else if (token >= 0) {
00971 bits_to_get = coeff_get_bits[token];
00972 if (bits_to_get)
00973 bits_to_get = get_bits(gb, bits_to_get);
00974 coeff = coeff_tables[token][bits_to_get];
00975
00976 zero_run = zero_run_base[token];
00977 if (zero_run_get_bits[token])
00978 zero_run += get_bits(gb, zero_run_get_bits[token]);
00979
00980 if (zero_run) {
00981 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
00982 } else {
00983
00984
00985
00986
00987 if (!coeff_index)
00988 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
00989
00990 dct_tokens[j++] = TOKEN_COEFF(coeff);
00991 }
00992
00993 if (coeff_index + zero_run > 64) {
00994 av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with"
00995 " %d coeffs left\n", zero_run, 64-coeff_index);
00996 zero_run = 64 - coeff_index;
00997 }
00998
00999
01000
01001 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
01002 s->num_coded_frags[plane][i]--;
01003 coeff_i++;
01004 } else {
01005 av_log(s->avctx, AV_LOG_ERROR,
01006 "Invalid token %d\n", token);
01007 return -1;
01008 }
01009 }
01010
01011 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
01012 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
01013
01014
01015
01016 if (blocks_ended)
01017 for (i = coeff_index+1; i < 64; i++)
01018 s->num_coded_frags[plane][i] -= blocks_ended;
01019
01020
01021 if (plane < 2)
01022 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
01023 else if (coeff_index < 63)
01024 s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
01025
01026 return eob_run;
01027 }
01028
01029 static void reverse_dc_prediction(Vp3DecodeContext *s,
01030 int first_fragment,
01031 int fragment_width,
01032 int fragment_height);
01033
01034
01035
01036
01037 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
01038 {
01039 int i;
01040 int dc_y_table;
01041 int dc_c_table;
01042 int ac_y_table;
01043 int ac_c_table;
01044 int residual_eob_run = 0;
01045 VLC *y_tables[64];
01046 VLC *c_tables[64];
01047
01048 s->dct_tokens[0][0] = s->dct_tokens_base;
01049
01050
01051 dc_y_table = get_bits(gb, 4);
01052 dc_c_table = get_bits(gb, 4);
01053
01054
01055 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
01056 0, residual_eob_run);
01057 if (residual_eob_run < 0)
01058 return residual_eob_run;
01059
01060
01061 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
01062
01063
01064 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
01065 1, residual_eob_run);
01066 if (residual_eob_run < 0)
01067 return residual_eob_run;
01068 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
01069 2, residual_eob_run);
01070 if (residual_eob_run < 0)
01071 return residual_eob_run;
01072
01073
01074 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
01075 {
01076 reverse_dc_prediction(s, s->fragment_start[1],
01077 s->fragment_width[1], s->fragment_height[1]);
01078 reverse_dc_prediction(s, s->fragment_start[2],
01079 s->fragment_width[1], s->fragment_height[1]);
01080 }
01081
01082
01083 ac_y_table = get_bits(gb, 4);
01084 ac_c_table = get_bits(gb, 4);
01085
01086
01087 for (i = 1; i <= 5; i++) {
01088 y_tables[i] = &s->ac_vlc_1[ac_y_table];
01089 c_tables[i] = &s->ac_vlc_1[ac_c_table];
01090 }
01091 for (i = 6; i <= 14; i++) {
01092 y_tables[i] = &s->ac_vlc_2[ac_y_table];
01093 c_tables[i] = &s->ac_vlc_2[ac_c_table];
01094 }
01095 for (i = 15; i <= 27; i++) {
01096 y_tables[i] = &s->ac_vlc_3[ac_y_table];
01097 c_tables[i] = &s->ac_vlc_3[ac_c_table];
01098 }
01099 for (i = 28; i <= 63; i++) {
01100 y_tables[i] = &s->ac_vlc_4[ac_y_table];
01101 c_tables[i] = &s->ac_vlc_4[ac_c_table];
01102 }
01103
01104
01105 for (i = 1; i <= 63; i++) {
01106 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
01107 0, residual_eob_run);
01108 if (residual_eob_run < 0)
01109 return residual_eob_run;
01110
01111 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
01112 1, residual_eob_run);
01113 if (residual_eob_run < 0)
01114 return residual_eob_run;
01115 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
01116 2, residual_eob_run);
01117 if (residual_eob_run < 0)
01118 return residual_eob_run;
01119 }
01120
01121 return 0;
01122 }
01123
01124
01125
01126
01127
01128
01129 #define COMPATIBLE_FRAME(x) \
01130 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
01131 #define DC_COEFF(u) s->all_fragments[u].dc
01132
01133 static void reverse_dc_prediction(Vp3DecodeContext *s,
01134 int first_fragment,
01135 int fragment_width,
01136 int fragment_height)
01137 {
01138
01139 #define PUL 8
01140 #define PU 4
01141 #define PUR 2
01142 #define PL 1
01143
01144 int x, y;
01145 int i = first_fragment;
01146
01147 int predicted_dc;
01148
01149
01150 int vl, vul, vu, vur;
01151
01152
01153 int l, ul, u, ur;
01154
01155
01156
01157
01158
01159
01160
01161
01162 static const int predictor_transform[16][4] = {
01163 { 0, 0, 0, 0},
01164 { 0, 0, 0,128},
01165 { 0, 0,128, 0},
01166 { 0, 0, 53, 75},
01167 { 0,128, 0, 0},
01168 { 0, 64, 0, 64},
01169 { 0,128, 0, 0},
01170 { 0, 0, 53, 75},
01171 {128, 0, 0, 0},
01172 { 0, 0, 0,128},
01173 { 64, 0, 64, 0},
01174 { 0, 0, 53, 75},
01175 { 0,128, 0, 0},
01176 {-104,116, 0,116},
01177 { 24, 80, 24, 0},
01178 {-104,116, 0,116}
01179 };
01180
01181
01182
01183
01184
01185
01186
01187 static const unsigned char compatible_frame[9] = {
01188 1,
01189 0,
01190 1,
01191 1,
01192 1,
01193 2,
01194 2,
01195 1,
01196 3
01197 };
01198 int current_frame_type;
01199
01200
01201 short last_dc[3];
01202
01203 int transform = 0;
01204
01205 vul = vu = vur = vl = 0;
01206 last_dc[0] = last_dc[1] = last_dc[2] = 0;
01207
01208
01209 for (y = 0; y < fragment_height; y++) {
01210
01211
01212 for (x = 0; x < fragment_width; x++, i++) {
01213
01214
01215 if (s->all_fragments[i].coding_method != MODE_COPY) {
01216
01217 current_frame_type =
01218 compatible_frame[s->all_fragments[i].coding_method];
01219
01220 transform= 0;
01221 if(x){
01222 l= i-1;
01223 vl = DC_COEFF(l);
01224 if(COMPATIBLE_FRAME(l))
01225 transform |= PL;
01226 }
01227 if(y){
01228 u= i-fragment_width;
01229 vu = DC_COEFF(u);
01230 if(COMPATIBLE_FRAME(u))
01231 transform |= PU;
01232 if(x){
01233 ul= i-fragment_width-1;
01234 vul = DC_COEFF(ul);
01235 if(COMPATIBLE_FRAME(ul))
01236 transform |= PUL;
01237 }
01238 if(x + 1 < fragment_width){
01239 ur= i-fragment_width+1;
01240 vur = DC_COEFF(ur);
01241 if(COMPATIBLE_FRAME(ur))
01242 transform |= PUR;
01243 }
01244 }
01245
01246 if (transform == 0) {
01247
01248
01249
01250 predicted_dc = last_dc[current_frame_type];
01251 } else {
01252
01253
01254 predicted_dc =
01255 (predictor_transform[transform][0] * vul) +
01256 (predictor_transform[transform][1] * vu) +
01257 (predictor_transform[transform][2] * vur) +
01258 (predictor_transform[transform][3] * vl);
01259
01260 predicted_dc /= 128;
01261
01262
01263
01264 if ((transform == 15) || (transform == 13)) {
01265 if (FFABS(predicted_dc - vu) > 128)
01266 predicted_dc = vu;
01267 else if (FFABS(predicted_dc - vl) > 128)
01268 predicted_dc = vl;
01269 else if (FFABS(predicted_dc - vul) > 128)
01270 predicted_dc = vul;
01271 }
01272 }
01273
01274
01275 DC_COEFF(i) += predicted_dc;
01276
01277 last_dc[current_frame_type] = DC_COEFF(i);
01278 }
01279 }
01280 }
01281 }
01282
01283 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
01284 {
01285 int x, y;
01286 int *bounding_values= s->bounding_values_array+127;
01287
01288 int width = s->fragment_width[!!plane];
01289 int height = s->fragment_height[!!plane];
01290 int fragment = s->fragment_start [plane] + ystart * width;
01291 int stride = s->current_frame.linesize[plane];
01292 uint8_t *plane_data = s->current_frame.data [plane];
01293 if (!s->flipped_image) stride = -stride;
01294 plane_data += s->data_offset[plane] + 8*ystart*stride;
01295
01296 for (y = ystart; y < yend; y++) {
01297
01298 for (x = 0; x < width; x++) {
01299
01300
01301
01302
01303 if( s->all_fragments[fragment].coding_method != MODE_COPY )
01304 {
01305
01306 if (x > 0) {
01307 s->vp3dsp.h_loop_filter(
01308 plane_data + 8*x,
01309 stride, bounding_values);
01310 }
01311
01312
01313 if (y > 0) {
01314 s->vp3dsp.v_loop_filter(
01315 plane_data + 8*x,
01316 stride, bounding_values);
01317 }
01318
01319
01320
01321
01322 if ((x < width - 1) &&
01323 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
01324 s->vp3dsp.h_loop_filter(
01325 plane_data + 8*x + 8,
01326 stride, bounding_values);
01327 }
01328
01329
01330
01331
01332 if ((y < height - 1) &&
01333 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
01334 s->vp3dsp.v_loop_filter(
01335 plane_data + 8*x + 8*stride,
01336 stride, bounding_values);
01337 }
01338 }
01339
01340 fragment++;
01341 }
01342 plane_data += 8*stride;
01343 }
01344 }
01345
01350 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
01351 int plane, int inter, DCTELEM block[64])
01352 {
01353 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
01354 uint8_t *perm = s->scantable.permutated;
01355 int i = 0;
01356
01357 do {
01358 int token = *s->dct_tokens[plane][i];
01359 switch (token & 3) {
01360 case 0:
01361 if (--token < 4)
01362 s->dct_tokens[plane][i]++;
01363 else
01364 *s->dct_tokens[plane][i] = token & ~3;
01365 goto end;
01366 case 1:
01367 s->dct_tokens[plane][i]++;
01368 i += (token >> 2) & 0x7f;
01369 if (i > 63) {
01370 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
01371 return i;
01372 }
01373 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
01374 i++;
01375 break;
01376 case 2:
01377 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
01378 s->dct_tokens[plane][i++]++;
01379 break;
01380 default:
01381 return i;
01382 }
01383 } while (i < 64);
01384
01385 i--;
01386 end:
01387
01388 block[0] = frag->dc * s->qmat[0][inter][plane][0];
01389 return i;
01390 }
01391
01395 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
01396 {
01397 int h, cy, i;
01398 int offset[AV_NUM_DATA_POINTERS];
01399
01400 if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
01401 int y_flipped = s->flipped_image ? s->avctx->height-y : y;
01402
01403
01404
01405
01406 ff_thread_report_progress(&s->current_frame, y_flipped==s->avctx->height ? INT_MAX : y_flipped-1, 0);
01407 }
01408
01409 if(s->avctx->draw_horiz_band==NULL)
01410 return;
01411
01412 h= y - s->last_slice_end;
01413 s->last_slice_end= y;
01414 y -= h;
01415
01416 if (!s->flipped_image) {
01417 y = s->avctx->height - y - h;
01418 }
01419
01420 cy = y >> s->chroma_y_shift;
01421 offset[0] = s->current_frame.linesize[0]*y;
01422 offset[1] = s->current_frame.linesize[1]*cy;
01423 offset[2] = s->current_frame.linesize[2]*cy;
01424 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
01425 offset[i] = 0;
01426
01427 emms_c();
01428 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
01429 }
01430
01435 static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
01436 {
01437 AVFrame *ref_frame;
01438 int ref_row;
01439 int border = motion_y&1;
01440
01441 if (fragment->coding_method == MODE_USING_GOLDEN ||
01442 fragment->coding_method == MODE_GOLDEN_MV)
01443 ref_frame = &s->golden_frame;
01444 else
01445 ref_frame = &s->last_frame;
01446
01447 ref_row = y + (motion_y>>1);
01448 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
01449
01450 ff_thread_await_progress(ref_frame, ref_row, 0);
01451 }
01452
01453
01454
01455
01456
01457 static void render_slice(Vp3DecodeContext *s, int slice)
01458 {
01459 int x, y, i, j, fragment;
01460 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
01461 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
01462 int motion_halfpel_index;
01463 uint8_t *motion_source;
01464 int plane, first_pixel;
01465
01466 if (slice >= s->c_superblock_height)
01467 return;
01468
01469 for (plane = 0; plane < 3; plane++) {
01470 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
01471 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
01472 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
01473 int stride = s->current_frame.linesize[plane];
01474 int plane_width = s->width >> (plane && s->chroma_x_shift);
01475 int plane_height = s->height >> (plane && s->chroma_y_shift);
01476 int8_t (*motion_val)[2] = s->motion_val[!!plane];
01477
01478 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
01479 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
01480 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
01481
01482 int fragment_width = s->fragment_width[!!plane];
01483 int fragment_height = s->fragment_height[!!plane];
01484 int fragment_start = s->fragment_start[plane];
01485 int do_await = !plane && HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
01486
01487 if (!s->flipped_image) stride = -stride;
01488 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
01489 continue;
01490
01491
01492 for (; sb_y < slice_height; sb_y++) {
01493
01494
01495 for (sb_x = 0; sb_x < slice_width; sb_x++) {
01496
01497
01498 for (j = 0; j < 16; j++) {
01499 x = 4*sb_x + hilbert_offset[j][0];
01500 y = 4*sb_y + hilbert_offset[j][1];
01501 fragment = y*fragment_width + x;
01502
01503 i = fragment_start + fragment;
01504
01505
01506 if (x >= fragment_width || y >= fragment_height)
01507 continue;
01508
01509 first_pixel = 8*y*stride + 8*x;
01510
01511 if (do_await && s->all_fragments[i].coding_method != MODE_INTRA)
01512 await_reference_row(s, &s->all_fragments[i], motion_val[fragment][1], (16*y) >> s->chroma_y_shift);
01513
01514
01515 if (s->all_fragments[i].coding_method != MODE_COPY) {
01516 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
01517 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
01518 motion_source= golden_plane;
01519 else
01520 motion_source= last_plane;
01521
01522 motion_source += first_pixel;
01523 motion_halfpel_index = 0;
01524
01525
01526
01527 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
01528 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
01529 int src_x, src_y;
01530 motion_x = motion_val[fragment][0];
01531 motion_y = motion_val[fragment][1];
01532
01533 src_x= (motion_x>>1) + 8*x;
01534 src_y= (motion_y>>1) + 8*y;
01535
01536 motion_halfpel_index = motion_x & 0x01;
01537 motion_source += (motion_x >> 1);
01538
01539 motion_halfpel_index |= (motion_y & 0x01) << 1;
01540 motion_source += ((motion_y >> 1) * stride);
01541
01542 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
01543 uint8_t *temp= s->edge_emu_buffer;
01544 if(stride<0) temp -= 8*stride;
01545
01546 s->dsp.emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
01547 motion_source= temp;
01548 }
01549 }
01550
01551
01552
01553
01554 if (s->all_fragments[i].coding_method != MODE_INTRA) {
01555
01556
01557
01558
01559 if(motion_halfpel_index != 3){
01560 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
01561 output_plane + first_pixel,
01562 motion_source, stride, 8);
01563 }else{
01564 int d= (motion_x ^ motion_y)>>31;
01565 s->dsp.put_no_rnd_pixels_l2[1](
01566 output_plane + first_pixel,
01567 motion_source - d,
01568 motion_source + stride + 1 + d,
01569 stride, 8);
01570 }
01571 }
01572
01573 s->dsp.clear_block(block);
01574
01575
01576
01577 if (s->all_fragments[i].coding_method == MODE_INTRA) {
01578 vp3_dequant(s, s->all_fragments + i, plane, 0, block);
01579 s->vp3dsp.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->vp3dsp.idct_add(
01586 output_plane + first_pixel,
01587 stride,
01588 block);
01589 } else {
01590 s->vp3dsp.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->codec_id != AV_CODEC_ID_THEORA)
01671 avctx->pix_fmt = PIX_FMT_YUV420P;
01672 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01673 ff_dsputil_init(&s->dsp, avctx);
01674 ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
01675
01676 ff_init_scantable_permutation(s->dsp.idct_permutation, s->vp3dsp.idct_perm);
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 avctx->pix_fmt = PIX_FMT_YUV420P;
02319
02320 s->theora = 1;
02321
02322 if (!avctx->extradata_size)
02323 {
02324 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
02325 return -1;
02326 }
02327
02328 if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
02329 42, header_start, header_len) < 0) {
02330 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
02331 return -1;
02332 }
02333
02334 for(i=0;i<3;i++) {
02335 init_get_bits(&gb, header_start[i], header_len[i] * 8);
02336
02337 ptype = get_bits(&gb, 8);
02338
02339 if (!(ptype & 0x80))
02340 {
02341 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
02342
02343 }
02344
02345
02346 skip_bits_long(&gb, 6*8);
02347
02348 switch(ptype)
02349 {
02350 case 0x80:
02351 theora_decode_header(avctx, &gb);
02352 break;
02353 case 0x81:
02354
02355
02356 break;
02357 case 0x82:
02358 if (theora_decode_tables(avctx, &gb))
02359 return -1;
02360 break;
02361 default:
02362 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
02363 break;
02364 }
02365 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
02366 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
02367 if (s->theora < 0x030200)
02368 break;
02369 }
02370
02371 return vp3_decode_init(avctx);
02372 }
02373
02374 AVCodec ff_theora_decoder = {
02375 .name = "theora",
02376 .type = AVMEDIA_TYPE_VIDEO,
02377 .id = AV_CODEC_ID_THEORA,
02378 .priv_data_size = sizeof(Vp3DecodeContext),
02379 .init = theora_decode_init,
02380 .close = vp3_decode_end,
02381 .decode = vp3_decode_frame,
02382 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
02383 CODEC_CAP_FRAME_THREADS,
02384 .flush = vp3_decode_flush,
02385 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
02386 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
02387 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
02388 };
02389 #endif
02390
02391 AVCodec ff_vp3_decoder = {
02392 .name = "vp3",
02393 .type = AVMEDIA_TYPE_VIDEO,
02394 .id = AV_CODEC_ID_VP3,
02395 .priv_data_size = sizeof(Vp3DecodeContext),
02396 .init = vp3_decode_init,
02397 .close = vp3_decode_end,
02398 .decode = vp3_decode_frame,
02399 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
02400 CODEC_CAP_FRAME_THREADS,
02401 .flush = vp3_decode_flush,
02402 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
02403 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
02404 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
02405 };