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 if (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) < 0)
01750 goto vlc_fail;
01751
01752
01753 if (init_vlc(&s->ac_vlc_1[i], 5, 32,
01754 &s->huffman_table[i+16][0][1], 4, 2,
01755 &s->huffman_table[i+16][0][0], 4, 2, 0) < 0)
01756 goto vlc_fail;
01757
01758
01759 if (init_vlc(&s->ac_vlc_2[i], 5, 32,
01760 &s->huffman_table[i+16*2][0][1], 4, 2,
01761 &s->huffman_table[i+16*2][0][0], 4, 2, 0) < 0)
01762 goto vlc_fail;
01763
01764
01765 if (init_vlc(&s->ac_vlc_3[i], 5, 32,
01766 &s->huffman_table[i+16*3][0][1], 4, 2,
01767 &s->huffman_table[i+16*3][0][0], 4, 2, 0) < 0)
01768 goto vlc_fail;
01769
01770
01771 if (init_vlc(&s->ac_vlc_4[i], 5, 32,
01772 &s->huffman_table[i+16*4][0][1], 4, 2,
01773 &s->huffman_table[i+16*4][0][0], 4, 2, 0) < 0)
01774 goto vlc_fail;
01775 }
01776 }
01777
01778 init_vlc(&s->superblock_run_length_vlc, 6, 34,
01779 &superblock_run_length_vlc_table[0][1], 4, 2,
01780 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
01781
01782 init_vlc(&s->fragment_run_length_vlc, 5, 30,
01783 &fragment_run_length_vlc_table[0][1], 4, 2,
01784 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
01785
01786 init_vlc(&s->mode_code_vlc, 3, 8,
01787 &mode_code_vlc_table[0][1], 2, 1,
01788 &mode_code_vlc_table[0][0], 2, 1, 0);
01789
01790 init_vlc(&s->motion_vector_vlc, 6, 63,
01791 &motion_vector_vlc_table[0][1], 2, 1,
01792 &motion_vector_vlc_table[0][0], 2, 1, 0);
01793
01794
01795 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
01796 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
01797 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
01798 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
01799 if (!s->superblock_fragments || !s->superblock_macroblocks ||
01800 !s->macroblock_fragments || !s->macroblock_coding) {
01801 vp3_decode_end(avctx);
01802 return -1;
01803 }
01804 init_block_mapping(s);
01805
01806 for (i = 0; i < 3; i++) {
01807 s->current_frame.data[i] = NULL;
01808 s->last_frame.data[i] = NULL;
01809 s->golden_frame.data[i] = NULL;
01810 }
01811
01812 return 0;
01813
01814 vlc_fail:
01815 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
01816 return -1;
01817 }
01818
01819
01820
01821
01822 static int vp3_decode_frame(AVCodecContext *avctx,
01823 void *data, int *data_size,
01824 const uint8_t *buf, int buf_size)
01825 {
01826 Vp3DecodeContext *s = avctx->priv_data;
01827 GetBitContext gb;
01828 static int counter = 0;
01829 int i;
01830
01831 init_get_bits(&gb, buf, buf_size * 8);
01832
01833 if (s->theora && get_bits1(&gb))
01834 {
01835 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
01836 return -1;
01837 }
01838
01839 s->keyframe = !get_bits1(&gb);
01840 if (!s->theora)
01841 skip_bits(&gb, 1);
01842 s->last_quality_index = s->quality_index;
01843
01844 s->nqis=0;
01845 do{
01846 s->qis[s->nqis++]= get_bits(&gb, 6);
01847 } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
01848
01849 s->quality_index= s->qis[0];
01850
01851 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01852 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
01853 s->keyframe?"key":"", counter, s->quality_index);
01854 counter++;
01855
01856 if (s->quality_index != s->last_quality_index) {
01857 init_dequantizer(s);
01858 init_loop_filter(s);
01859 }
01860
01861 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
01862 return buf_size;
01863
01864 if (s->keyframe) {
01865 if (!s->theora)
01866 {
01867 skip_bits(&gb, 4);
01868 skip_bits(&gb, 4);
01869 if (s->version)
01870 {
01871 s->version = get_bits(&gb, 5);
01872 if (counter == 1)
01873 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
01874 }
01875 }
01876 if (s->version || s->theora)
01877 {
01878 if (get_bits1(&gb))
01879 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
01880 skip_bits(&gb, 2);
01881 }
01882
01883 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
01884 if (s->golden_frame.data[0])
01885 avctx->release_buffer(avctx, &s->golden_frame);
01886 s->last_frame= s->golden_frame;
01887 } else {
01888 if (s->golden_frame.data[0])
01889 avctx->release_buffer(avctx, &s->golden_frame);
01890 if (s->last_frame.data[0])
01891 avctx->release_buffer(avctx, &s->last_frame);
01892 }
01893
01894 s->golden_frame.reference = 3;
01895 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
01896 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
01897 return -1;
01898 }
01899
01900
01901 s->current_frame= s->golden_frame;
01902
01903
01904 if (!s->pixel_addresses_initialized)
01905 {
01906 vp3_calculate_pixel_addresses(s);
01907 s->pixel_addresses_initialized = 1;
01908 }
01909 } else {
01910
01911 s->current_frame.reference = 3;
01912 if (!s->pixel_addresses_initialized) {
01913 av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
01914 return -1;
01915 }
01916 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
01917 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
01918 return -1;
01919 }
01920 }
01921
01922 s->current_frame.qscale_table= s->qscale_table;
01923 s->current_frame.qstride= 0;
01924
01925 init_frame(s, &gb);
01926
01927 if (unpack_superblocks(s, &gb)){
01928 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
01929 return -1;
01930 }
01931 if (unpack_modes(s, &gb)){
01932 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
01933 return -1;
01934 }
01935 if (unpack_vectors(s, &gb)){
01936 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
01937 return -1;
01938 }
01939 if (unpack_dct_coeffs(s, &gb)){
01940 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
01941 return -1;
01942 }
01943
01944 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
01945 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
01946 reverse_dc_prediction(s, s->fragment_start[1],
01947 s->fragment_width / 2, s->fragment_height / 2);
01948 reverse_dc_prediction(s, s->fragment_start[2],
01949 s->fragment_width / 2, s->fragment_height / 2);
01950 }
01951
01952 for (i = 0; i < s->macroblock_height; i++)
01953 render_slice(s, i);
01954
01955 apply_loop_filter(s);
01956
01957 *data_size=sizeof(AVFrame);
01958 *(AVFrame*)data= s->current_frame;
01959
01960
01961
01962 if ((s->last_frame.data[0]) &&
01963 (s->last_frame.data[0] != s->golden_frame.data[0]))
01964 avctx->release_buffer(avctx, &s->last_frame);
01965
01966
01967 s->last_frame= s->current_frame;
01968 s->current_frame.data[0]= NULL;
01969
01970 return buf_size;
01971 }
01972
01973
01974
01975
01976 static av_cold int vp3_decode_end(AVCodecContext *avctx)
01977 {
01978 Vp3DecodeContext *s = avctx->priv_data;
01979 int i;
01980
01981 av_free(s->superblock_coding);
01982 av_free(s->all_fragments);
01983 av_free(s->coeff_counts);
01984 av_free(s->coeffs);
01985 av_free(s->coded_fragment_list);
01986 av_free(s->superblock_fragments);
01987 av_free(s->superblock_macroblocks);
01988 av_free(s->macroblock_fragments);
01989 av_free(s->macroblock_coding);
01990
01991 for (i = 0; i < 16; i++) {
01992 free_vlc(&s->dc_vlc[i]);
01993 free_vlc(&s->ac_vlc_1[i]);
01994 free_vlc(&s->ac_vlc_2[i]);
01995 free_vlc(&s->ac_vlc_3[i]);
01996 free_vlc(&s->ac_vlc_4[i]);
01997 }
01998
01999 free_vlc(&s->superblock_run_length_vlc);
02000 free_vlc(&s->fragment_run_length_vlc);
02001 free_vlc(&s->mode_code_vlc);
02002 free_vlc(&s->motion_vector_vlc);
02003
02004
02005 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
02006 avctx->release_buffer(avctx, &s->golden_frame);
02007 if (s->last_frame.data[0])
02008 avctx->release_buffer(avctx, &s->last_frame);
02009
02010
02011
02012 return 0;
02013 }
02014
02015 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
02016 {
02017 Vp3DecodeContext *s = avctx->priv_data;
02018
02019 if (get_bits1(gb)) {
02020 int token;
02021 if (s->entries >= 32) {
02022 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
02023 return -1;
02024 }
02025 token = get_bits(gb, 5);
02026
02027 s->huffman_table[s->hti][token][0] = s->hbits;
02028 s->huffman_table[s->hti][token][1] = s->huff_code_size;
02029 s->entries++;
02030 }
02031 else {
02032 if (s->huff_code_size >= 32) {
02033 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
02034 return -1;
02035 }
02036 s->huff_code_size++;
02037 s->hbits <<= 1;
02038 if (read_huffman_tree(avctx, gb))
02039 return -1;
02040 s->hbits |= 1;
02041 if (read_huffman_tree(avctx, gb))
02042 return -1;
02043 s->hbits >>= 1;
02044 s->huff_code_size--;
02045 }
02046 return 0;
02047 }
02048
02049 #if CONFIG_THEORA_DECODER
02050 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
02051 {
02052 Vp3DecodeContext *s = avctx->priv_data;
02053 int visible_width, visible_height;
02054
02055 s->theora = get_bits_long(gb, 24);
02056 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
02057
02058
02059
02060 if (s->theora < 0x030200)
02061 {
02062 s->flipped_image = 1;
02063 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
02064 }
02065
02066 visible_width = s->width = get_bits(gb, 16) << 4;
02067 visible_height = s->height = get_bits(gb, 16) << 4;
02068
02069 if(avcodec_check_dimensions(avctx, s->width, s->height)){
02070 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
02071 s->width= s->height= 0;
02072 return -1;
02073 }
02074
02075 if (s->theora >= 0x030400)
02076 {
02077 skip_bits(gb, 32);
02078
02079 skip_bits(gb, 32);
02080 skip_bits(gb, 4);
02081 skip_bits(gb, 32);
02082 }
02083
02084 if (s->theora >= 0x030200) {
02085 visible_width = get_bits_long(gb, 24);
02086 visible_height = get_bits_long(gb, 24);
02087
02088 skip_bits(gb, 8);
02089 skip_bits(gb, 8);
02090 }
02091
02092 skip_bits(gb, 32);
02093 skip_bits(gb, 32);
02094 skip_bits(gb, 24);
02095 skip_bits(gb, 24);
02096
02097 if (s->theora < 0x030200)
02098 skip_bits(gb, 5);
02099 skip_bits(gb, 8);
02100 if (s->theora >= 0x030400)
02101 skip_bits(gb, 2);
02102 skip_bits(gb, 24);
02103
02104 skip_bits(gb, 6);
02105
02106 if (s->theora >= 0x030200)
02107 {
02108 skip_bits(gb, 5);
02109
02110 if (s->theora < 0x030400)
02111 skip_bits(gb, 5);
02112 }
02113
02114
02115
02116 if ( visible_width <= s->width && visible_width > s->width-16
02117 && visible_height <= s->height && visible_height > s->height-16)
02118 avcodec_set_dimensions(avctx, visible_width, visible_height);
02119 else
02120 avcodec_set_dimensions(avctx, s->width, s->height);
02121
02122 return 0;
02123 }
02124
02125 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
02126 {
02127 Vp3DecodeContext *s = avctx->priv_data;
02128 int i, n, matrices, inter, plane;
02129
02130 if (s->theora >= 0x030200) {
02131 n = get_bits(gb, 3);
02132
02133 for (i = 0; i < 64; i++)
02134 s->filter_limit_values[i] = get_bits(gb, n);
02135 }
02136
02137 if (s->theora >= 0x030200)
02138 n = get_bits(gb, 4) + 1;
02139 else
02140 n = 16;
02141
02142 for (i = 0; i < 64; i++)
02143 s->coded_ac_scale_factor[i] = get_bits(gb, n);
02144
02145 if (s->theora >= 0x030200)
02146 n = get_bits(gb, 4) + 1;
02147 else
02148 n = 16;
02149
02150 for (i = 0; i < 64; i++)
02151 s->coded_dc_scale_factor[i] = get_bits(gb, n);
02152
02153 if (s->theora >= 0x030200)
02154 matrices = get_bits(gb, 9) + 1;
02155 else
02156 matrices = 3;
02157
02158 if(matrices > 384){
02159 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
02160 return -1;
02161 }
02162
02163 for(n=0; n<matrices; n++){
02164 for (i = 0; i < 64; i++)
02165 s->base_matrix[n][i]= get_bits(gb, 8);
02166 }
02167
02168 for (inter = 0; inter <= 1; inter++) {
02169 for (plane = 0; plane <= 2; plane++) {
02170 int newqr= 1;
02171 if (inter || plane > 0)
02172 newqr = get_bits1(gb);
02173 if (!newqr) {
02174 int qtj, plj;
02175 if(inter && get_bits1(gb)){
02176 qtj = 0;
02177 plj = plane;
02178 }else{
02179 qtj= (3*inter + plane - 1) / 3;
02180 plj= (plane + 2) % 3;
02181 }
02182 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
02183 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
02184 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
02185 } else {
02186 int qri= 0;
02187 int qi = 0;
02188
02189 for(;;){
02190 i= get_bits(gb, av_log2(matrices-1)+1);
02191 if(i>= matrices){
02192 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
02193 return -1;
02194 }
02195 s->qr_base[inter][plane][qri]= i;
02196 if(qi >= 63)
02197 break;
02198 i = get_bits(gb, av_log2(63-qi)+1) + 1;
02199 s->qr_size[inter][plane][qri++]= i;
02200 qi += i;
02201 }
02202
02203 if (qi > 63) {
02204 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
02205 return -1;
02206 }
02207 s->qr_count[inter][plane]= qri;
02208 }
02209 }
02210 }
02211
02212
02213 for (s->hti = 0; s->hti < 80; s->hti++) {
02214 s->entries = 0;
02215 s->huff_code_size = 1;
02216 if (!get_bits1(gb)) {
02217 s->hbits = 0;
02218 if(read_huffman_tree(avctx, gb))
02219 return -1;
02220 s->hbits = 1;
02221 if(read_huffman_tree(avctx, gb))
02222 return -1;
02223 }
02224 }
02225
02226 s->theora_tables = 1;
02227
02228 return 0;
02229 }
02230
02231 static av_cold int theora_decode_init(AVCodecContext *avctx)
02232 {
02233 Vp3DecodeContext *s = avctx->priv_data;
02234 GetBitContext gb;
02235 int ptype;
02236 uint8_t *header_start[3];
02237 int header_len[3];
02238 int i;
02239
02240 s->theora = 1;
02241
02242 if (!avctx->extradata_size)
02243 {
02244 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
02245 return -1;
02246 }
02247
02248 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
02249 42, header_start, header_len) < 0) {
02250 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
02251 return -1;
02252 }
02253
02254 for(i=0;i<3;i++) {
02255 init_get_bits(&gb, header_start[i], header_len[i] * 8);
02256
02257 ptype = get_bits(&gb, 8);
02258
02259 if (!(ptype & 0x80))
02260 {
02261 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
02262
02263 }
02264
02265
02266 skip_bits(&gb, 6*8);
02267
02268 switch(ptype)
02269 {
02270 case 0x80:
02271 theora_decode_header(avctx, &gb);
02272 break;
02273 case 0x81:
02274
02275
02276 break;
02277 case 0x82:
02278 if (theora_decode_tables(avctx, &gb))
02279 return -1;
02280 break;
02281 default:
02282 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
02283 break;
02284 }
02285 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
02286 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
02287 if (s->theora < 0x030200)
02288 break;
02289 }
02290
02291 vp3_decode_init(avctx);
02292 return 0;
02293 }
02294
02295 AVCodec theora_decoder = {
02296 "theora",
02297 CODEC_TYPE_VIDEO,
02298 CODEC_ID_THEORA,
02299 sizeof(Vp3DecodeContext),
02300 theora_decode_init,
02301 NULL,
02302 vp3_decode_end,
02303 vp3_decode_frame,
02304 0,
02305 NULL,
02306 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
02307 };
02308 #endif
02309
02310 AVCodec vp3_decoder = {
02311 "vp3",
02312 CODEC_TYPE_VIDEO,
02313 CODEC_ID_VP3,
02314 sizeof(Vp3DecodeContext),
02315 vp3_decode_init,
02316 NULL,
02317 vp3_decode_end,
02318 vp3_decode_frame,
02319 0,
02320 NULL,
02321 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
02322 };