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