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