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