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 ((unsigned) token <= 6U) {
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 if (token >= 0) {
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 } else {
01030 av_log(s->avctx, AV_LOG_ERROR,
01031 "Invalid token %d\n", token);
01032 return -1;
01033 }
01034 }
01035
01036 if (!eob_run) {
01037 s->coeff_counts[fragment_num] += zero_run;
01038 if (s->coeff_counts[fragment_num] < 64){
01039 fragment->next_coeff->coeff= coeff;
01040 fragment->next_coeff->index= perm[s->coeff_counts[fragment_num]++];
01041 fragment->next_coeff->next= s->next_coeff;
01042 s->next_coeff->next=NULL;
01043 fragment->next_coeff= s->next_coeff++;
01044 }
01045 } else {
01046 s->coeff_counts[fragment_num] |= 128;
01047 eob_run--;
01048 }
01049 }
01050
01051 return eob_run;
01052 }
01053
01054
01055
01056
01057
01058 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
01059 {
01060 int i;
01061 int dc_y_table;
01062 int dc_c_table;
01063 int ac_y_table;
01064 int ac_c_table;
01065 int residual_eob_run = 0;
01066
01067
01068 dc_y_table = get_bits(gb, 4);
01069 dc_c_table = get_bits(gb, 4);
01070
01071
01072 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
01073 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01074
01075
01076 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
01077 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01078 if (residual_eob_run < 0)
01079 return residual_eob_run;
01080
01081
01082 ac_y_table = get_bits(gb, 4);
01083 ac_c_table = get_bits(gb, 4);
01084
01085
01086 for (i = 1; i <= 5; i++) {
01087 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
01088 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01089 if (residual_eob_run < 0)
01090 return residual_eob_run;
01091
01092 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
01093 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01094 if (residual_eob_run < 0)
01095 return residual_eob_run;
01096 }
01097
01098
01099 for (i = 6; i <= 14; i++) {
01100 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
01101 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01102 if (residual_eob_run < 0)
01103 return residual_eob_run;
01104
01105 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
01106 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01107 if (residual_eob_run < 0)
01108 return residual_eob_run;
01109 }
01110
01111
01112 for (i = 15; i <= 27; i++) {
01113 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
01114 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01115 if (residual_eob_run < 0)
01116 return residual_eob_run;
01117
01118 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
01119 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01120 if (residual_eob_run < 0)
01121 return residual_eob_run;
01122 }
01123
01124
01125 for (i = 28; i <= 63; i++) {
01126 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
01127 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
01128 if (residual_eob_run < 0)
01129 return residual_eob_run;
01130
01131 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
01132 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
01133 if (residual_eob_run < 0)
01134 return residual_eob_run;
01135 }
01136
01137 return 0;
01138 }
01139
01140
01141
01142
01143
01144
01145 #define COMPATIBLE_FRAME(x) \
01146 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
01147 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
01148 #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
01149
01150 static void reverse_dc_prediction(Vp3DecodeContext *s,
01151 int first_fragment,
01152 int fragment_width,
01153 int fragment_height)
01154 {
01155
01156 #define PUL 8
01157 #define PU 4
01158 #define PUR 2
01159 #define PL 1
01160
01161 int x, y;
01162 int i = first_fragment;
01163
01164 int predicted_dc;
01165
01166
01167 int vl, vul, vu, vur;
01168
01169
01170 int l, ul, u, ur;
01171
01172
01173
01174
01175
01176
01177
01178
01179 int predictor_transform[16][4] = {
01180 { 0, 0, 0, 0},
01181 { 0, 0, 0,128},
01182 { 0, 0,128, 0},
01183 { 0, 0, 53, 75},
01184 { 0,128, 0, 0},
01185 { 0, 64, 0, 64},
01186 { 0,128, 0, 0},
01187 { 0, 0, 53, 75},
01188 {128, 0, 0, 0},
01189 { 0, 0, 0,128},
01190 { 64, 0, 64, 0},
01191 { 0, 0, 53, 75},
01192 { 0,128, 0, 0},
01193 {-104,116, 0,116},
01194 { 24, 80, 24, 0},
01195 {-104,116, 0,116}
01196 };
01197
01198
01199
01200
01201
01202
01203
01204 unsigned char compatible_frame[8] = {
01205 1,
01206 0,
01207 1,
01208 1,
01209 1,
01210 2,
01211 2,
01212 1
01213 };
01214 int current_frame_type;
01215
01216
01217 short last_dc[3];
01218
01219 int transform = 0;
01220
01221 vul = vu = vur = vl = 0;
01222 last_dc[0] = last_dc[1] = last_dc[2] = 0;
01223
01224
01225 for (y = 0; y < fragment_height; y++) {
01226
01227
01228 for (x = 0; x < fragment_width; x++, i++) {
01229
01230
01231 if (s->all_fragments[i].coding_method != MODE_COPY) {
01232
01233 current_frame_type =
01234 compatible_frame[s->all_fragments[i].coding_method];
01235
01236 transform= 0;
01237 if(x){
01238 l= i-1;
01239 vl = DC_COEFF(l);
01240 if(FRAME_CODED(l) && COMPATIBLE_FRAME(l))
01241 transform |= PL;
01242 }
01243 if(y){
01244 u= i-fragment_width;
01245 vu = DC_COEFF(u);
01246 if(FRAME_CODED(u) && COMPATIBLE_FRAME(u))
01247 transform |= PU;
01248 if(x){
01249 ul= i-fragment_width-1;
01250 vul = DC_COEFF(ul);
01251 if(FRAME_CODED(ul) && COMPATIBLE_FRAME(ul))
01252 transform |= PUL;
01253 }
01254 if(x + 1 < fragment_width){
01255 ur= i-fragment_width+1;
01256 vur = DC_COEFF(ur);
01257 if(FRAME_CODED(ur) && COMPATIBLE_FRAME(ur))
01258 transform |= PUR;
01259 }
01260 }
01261
01262 if (transform == 0) {
01263
01264
01265
01266 predicted_dc = last_dc[current_frame_type];
01267 } else {
01268
01269
01270 predicted_dc =
01271 (predictor_transform[transform][0] * vul) +
01272 (predictor_transform[transform][1] * vu) +
01273 (predictor_transform[transform][2] * vur) +
01274 (predictor_transform[transform][3] * vl);
01275
01276 predicted_dc /= 128;
01277
01278
01279
01280 if ((transform == 13) || (transform == 15)) {
01281 if (FFABS(predicted_dc - vu) > 128)
01282 predicted_dc = vu;
01283 else if (FFABS(predicted_dc - vl) > 128)
01284 predicted_dc = vl;
01285 else if (FFABS(predicted_dc - vul) > 128)
01286 predicted_dc = vul;
01287 }
01288 }
01289
01290
01291 if(s->coeffs[i].index){
01292 *s->next_coeff= s->coeffs[i];
01293 s->coeffs[i].index=0;
01294 s->coeffs[i].coeff=0;
01295 s->coeffs[i].next= s->next_coeff++;
01296 }
01297 s->coeffs[i].coeff += predicted_dc;
01298
01299 last_dc[current_frame_type] = DC_COEFF(i);
01300 if(DC_COEFF(i) && !(s->coeff_counts[i]&127)){
01301 s->coeff_counts[i]= 129;
01302
01303 s->coeffs[i].next= s->next_coeff;
01304 (s->next_coeff++)->next=NULL;
01305 }
01306 }
01307 }
01308 }
01309 }
01310
01311
01312
01313
01314
01315 static void render_slice(Vp3DecodeContext *s, int slice)
01316 {
01317 int x;
01318 int16_t *dequantizer;
01319 DECLARE_ALIGNED_16(DCTELEM, block[64]);
01320 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
01321 int motion_halfpel_index;
01322 uint8_t *motion_source;
01323 int plane;
01324 int current_macroblock_entry = slice * s->macroblock_width * 6;
01325
01326 if (slice >= s->macroblock_height)
01327 return;
01328
01329 for (plane = 0; plane < 3; plane++) {
01330 uint8_t *output_plane = s->current_frame.data [plane];
01331 uint8_t * last_plane = s-> last_frame.data [plane];
01332 uint8_t *golden_plane = s-> golden_frame.data [plane];
01333 int stride = s->current_frame.linesize[plane];
01334 int plane_width = s->width >> !!plane;
01335 int plane_height = s->height >> !!plane;
01336 int y = slice * FRAGMENT_PIXELS << !plane ;
01337 int slice_height = y + (FRAGMENT_PIXELS << !plane);
01338 int i = s->macroblock_fragments[current_macroblock_entry + plane + 3*!!plane];
01339
01340 if (!s->flipped_image) stride = -stride;
01341
01342
01343 if(FFABS(stride) > 2048)
01344 return;
01345
01346
01347 for (; y < slice_height; y += 8) {
01348
01349
01350 for (x = 0; x < plane_width; x += 8, i++) {
01351
01352 if ((i < 0) || (i >= s->fragment_count)) {
01353 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
01354 return;
01355 }
01356
01357
01358 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
01359 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
01360
01361 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
01362 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
01363 motion_source= golden_plane;
01364 else
01365 motion_source= last_plane;
01366
01367 motion_source += s->all_fragments[i].first_pixel;
01368 motion_halfpel_index = 0;
01369
01370
01371
01372 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
01373 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
01374 int src_x, src_y;
01375 motion_x = s->all_fragments[i].motion_x;
01376 motion_y = s->all_fragments[i].motion_y;
01377 if(plane){
01378 motion_x= (motion_x>>1) | (motion_x&1);
01379 motion_y= (motion_y>>1) | (motion_y&1);
01380 }
01381
01382 src_x= (motion_x>>1) + x;
01383 src_y= (motion_y>>1) + y;
01384 if ((motion_x == 127) || (motion_y == 127))
01385 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
01386
01387 motion_halfpel_index = motion_x & 0x01;
01388 motion_source += (motion_x >> 1);
01389
01390 motion_halfpel_index |= (motion_y & 0x01) << 1;
01391 motion_source += ((motion_y >> 1) * stride);
01392
01393 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
01394 uint8_t *temp= s->edge_emu_buffer;
01395 if(stride<0) temp -= 9*stride;
01396 else temp += 9*stride;
01397
01398 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
01399 motion_source= temp;
01400 }
01401 }
01402
01403
01404
01405
01406 if (s->all_fragments[i].coding_method != MODE_INTRA) {
01407
01408
01409
01410
01411 if(motion_halfpel_index != 3){
01412 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
01413 output_plane + s->all_fragments[i].first_pixel,
01414 motion_source, stride, 8);
01415 }else{
01416 int d= (motion_x ^ motion_y)>>31;
01417 s->dsp.put_no_rnd_pixels_l2[1](
01418 output_plane + s->all_fragments[i].first_pixel,
01419 motion_source - d,
01420 motion_source + stride + 1 + d,
01421 stride, 8);
01422 }
01423 dequantizer = s->qmat[1][plane];
01424 }else{
01425 dequantizer = s->qmat[0][plane];
01426 }
01427
01428
01429 if(s->avctx->idct_algo==FF_IDCT_VP3){
01430 Coeff *coeff= s->coeffs + i;
01431 s->dsp.clear_block(block);
01432 while(coeff->next){
01433 block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
01434 coeff= coeff->next;
01435 }
01436 }else{
01437 Coeff *coeff= s->coeffs + i;
01438 s->dsp.clear_block(block);
01439 while(coeff->next){
01440 block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
01441 coeff= coeff->next;
01442 }
01443 }
01444
01445
01446
01447 if (s->all_fragments[i].coding_method == MODE_INTRA) {
01448 if(s->avctx->idct_algo!=FF_IDCT_VP3)
01449 block[0] += 128<<3;
01450 s->dsp.idct_put(
01451 output_plane + s->all_fragments[i].first_pixel,
01452 stride,
01453 block);
01454 } else {
01455 s->dsp.idct_add(
01456 output_plane + s->all_fragments[i].first_pixel,
01457 stride,
01458 block);
01459 }
01460 } else {
01461
01462
01463 s->dsp.put_pixels_tab[1][0](
01464 output_plane + s->all_fragments[i].first_pixel,
01465 last_plane + s->all_fragments[i].first_pixel,
01466 stride, 8);
01467
01468 }
01469 #if 0
01470
01471
01472
01473
01474
01475
01476
01477 if ((x > 0) &&
01478 ((s->all_fragments[i].coding_method != MODE_COPY) ||
01479 ((s->all_fragments[i].coding_method == MODE_COPY) &&
01480 (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
01481 horizontal_filter(
01482 output_plane + s->all_fragments[i].first_pixel + 7*stride,
01483 -stride, s->bounding_values_array + 127);
01484 }
01485
01486
01487
01488
01489
01490
01491
01492
01493 if ((y > 0) &&
01494 ((s->all_fragments[i].coding_method != MODE_COPY) ||
01495 ((s->all_fragments[i].coding_method == MODE_COPY) &&
01496 (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
01497 vertical_filter(
01498 output_plane + s->all_fragments[i].first_pixel - stride,
01499 -stride, s->bounding_values_array + 127);
01500 }
01501 #endif
01502 }
01503 }
01504 }
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514 emms_c();
01515 }
01516
01517 static void apply_loop_filter(Vp3DecodeContext *s)
01518 {
01519 int plane;
01520 int x, y;
01521 int *bounding_values= s->bounding_values_array+127;
01522
01523 #if 0
01524 int bounding_values_array[256];
01525 int filter_limit;
01526
01527
01528 for (x = 63; x >= 0; x--) {
01529 if (vp31_ac_scale_factor[x] >= s->quality_index)
01530 break;
01531 }
01532 filter_limit = vp31_filter_limit_values[s->quality_index];
01533
01534
01535 memset(bounding_values_array, 0, 256 * sizeof(int));
01536 for (x = 0; x < filter_limit; x++) {
01537 bounding_values[-x - filter_limit] = -filter_limit + x;
01538 bounding_values[-x] = -x;
01539 bounding_values[x] = x;
01540 bounding_values[x + filter_limit] = filter_limit - x;
01541 }
01542 #endif
01543
01544 for (plane = 0; plane < 3; plane++) {
01545 int width = s->fragment_width >> !!plane;
01546 int height = s->fragment_height >> !!plane;
01547 int fragment = s->fragment_start [plane];
01548 int stride = s->current_frame.linesize[plane];
01549 uint8_t *plane_data = s->current_frame.data [plane];
01550 if (!s->flipped_image) stride = -stride;
01551
01552 for (y = 0; y < height; y++) {
01553
01554 for (x = 0; x < width; x++) {
01555
01556 if ((x > 0) &&
01557 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
01558 s->dsp.vp3_h_loop_filter(
01559 plane_data + s->all_fragments[fragment].first_pixel,
01560 stride, bounding_values);
01561 }
01562
01563
01564 if ((y > 0) &&
01565 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
01566 s->dsp.vp3_v_loop_filter(
01567 plane_data + s->all_fragments[fragment].first_pixel,
01568 stride, bounding_values);
01569 }
01570
01571
01572
01573
01574 if ((x < width - 1) &&
01575 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
01576 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
01577 s->dsp.vp3_h_loop_filter(
01578 plane_data + s->all_fragments[fragment + 1].first_pixel,
01579 stride, bounding_values);
01580 }
01581
01582
01583
01584
01585 if ((y < height - 1) &&
01586 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
01587 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
01588 s->dsp.vp3_v_loop_filter(
01589 plane_data + s->all_fragments[fragment + width].first_pixel,
01590 stride, bounding_values);
01591 }
01592
01593 fragment++;
01594 }
01595 }
01596 }
01597 }
01598
01599
01600
01601
01602
01603
01604 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
01605 {
01606 #define Y_INITIAL(chroma_shift) s->flipped_image ? 1 : s->fragment_height >> chroma_shift
01607 #define Y_FINISHED(chroma_shift) s->flipped_image ? y <= s->fragment_height >> chroma_shift : y > 0
01608
01609 int i, x, y;
01610 const int y_inc = s->flipped_image ? 1 : -1;
01611
01612
01613
01614 i = 0;
01615 for (y = Y_INITIAL(0); Y_FINISHED(0); y += y_inc) {
01616 for (x = 0; x < s->fragment_width; x++) {
01617 s->all_fragments[i++].first_pixel =
01618 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
01619 s->golden_frame.linesize[0] +
01620 x * FRAGMENT_PIXELS;
01621 }
01622 }
01623
01624
01625 i = s->fragment_start[1];
01626 for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
01627 for (x = 0; x < s->fragment_width / 2; x++) {
01628 s->all_fragments[i++].first_pixel =
01629 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
01630 s->golden_frame.linesize[1] +
01631 x * FRAGMENT_PIXELS;
01632 }
01633 }
01634
01635
01636 i = s->fragment_start[2];
01637 for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
01638 for (x = 0; x < s->fragment_width / 2; x++) {
01639 s->all_fragments[i++].first_pixel =
01640 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
01641 s->golden_frame.linesize[2] +
01642 x * FRAGMENT_PIXELS;
01643 }
01644 }
01645 }
01646
01647
01648
01649
01650 static av_cold int vp3_decode_init(AVCodecContext *avctx)
01651 {
01652 Vp3DecodeContext *s = avctx->priv_data;
01653 int i, inter, plane;
01654 int c_width;
01655 int c_height;
01656 int y_superblock_count;
01657 int c_superblock_count;
01658
01659 if (avctx->codec_tag == MKTAG('V','P','3','0'))
01660 s->version = 0;
01661 else
01662 s->version = 1;
01663
01664 s->avctx = avctx;
01665 s->width = (avctx->width + 15) & 0xFFFFFFF0;
01666 s->height = (avctx->height + 15) & 0xFFFFFFF0;
01667 avctx->pix_fmt = PIX_FMT_YUV420P;
01668 if(avctx->idct_algo==FF_IDCT_AUTO)
01669 avctx->idct_algo=FF_IDCT_VP3;
01670 dsputil_init(&s->dsp, avctx);
01671
01672 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
01673
01674
01675
01676 s->quality_index = -1;
01677
01678 s->y_superblock_width = (s->width + 31) / 32;
01679 s->y_superblock_height = (s->height + 31) / 32;
01680 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
01681
01682
01683 c_width = s->width / 2;
01684 c_height = s->height / 2;
01685 s->c_superblock_width = (c_width + 31) / 32;
01686 s->c_superblock_height = (c_height + 31) / 32;
01687 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
01688
01689 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
01690 s->u_superblock_start = y_superblock_count;
01691 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
01692 s->superblock_coding = av_malloc(s->superblock_count);
01693
01694 s->macroblock_width = (s->width + 15) / 16;
01695 s->macroblock_height = (s->height + 15) / 16;
01696 s->macroblock_count = s->macroblock_width * s->macroblock_height;
01697
01698 s->fragment_width = s->width / FRAGMENT_PIXELS;
01699 s->fragment_height = s->height / FRAGMENT_PIXELS;
01700
01701
01702 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
01703 s->fragment_start[1] = s->fragment_width * s->fragment_height;
01704 s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
01705
01706 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
01707 s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts));
01708 s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
01709 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
01710 s->pixel_addresses_initialized = 0;
01711 if (!s->superblock_coding || !s->all_fragments || !s->coeff_counts ||
01712 !s->coeffs || !s->coded_fragment_list) {
01713 vp3_decode_end(avctx);
01714 return -1;
01715 }
01716
01717 if (!s->theora_tables)
01718 {
01719 for (i = 0; i < 64; i++) {
01720 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
01721 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
01722 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
01723 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
01724 s->base_matrix[2][i] = vp31_inter_dequant[i];
01725 s->filter_limit_values[i] = vp31_filter_limit_values[i];
01726 }
01727
01728 for(inter=0; inter<2; inter++){
01729 for(plane=0; plane<3; plane++){
01730 s->qr_count[inter][plane]= 1;
01731 s->qr_size [inter][plane][0]= 63;
01732 s->qr_base [inter][plane][0]=
01733 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
01734 }
01735 }
01736
01737
01738 for (i = 0; i < 16; i++) {
01739
01740
01741 init_vlc(&s->dc_vlc[i], 5, 32,
01742 &dc_bias[i][0][1], 4, 2,
01743 &dc_bias[i][0][0], 4, 2, 0);
01744
01745
01746 init_vlc(&s->ac_vlc_1[i], 5, 32,
01747 &ac_bias_0[i][0][1], 4, 2,
01748 &ac_bias_0[i][0][0], 4, 2, 0);
01749
01750
01751 init_vlc(&s->ac_vlc_2[i], 5, 32,
01752 &ac_bias_1[i][0][1], 4, 2,
01753 &ac_bias_1[i][0][0], 4, 2, 0);
01754
01755
01756 init_vlc(&s->ac_vlc_3[i], 5, 32,
01757 &ac_bias_2[i][0][1], 4, 2,
01758 &ac_bias_2[i][0][0], 4, 2, 0);
01759
01760
01761 init_vlc(&s->ac_vlc_4[i], 5, 32,
01762 &ac_bias_3[i][0][1], 4, 2,
01763 &ac_bias_3[i][0][0], 4, 2, 0);
01764 }
01765 } else {
01766 for (i = 0; i < 16; i++) {
01767
01768
01769 if (init_vlc(&s->dc_vlc[i], 5, 32,
01770 &s->huffman_table[i][0][1], 4, 2,
01771 &s->huffman_table[i][0][0], 4, 2, 0) < 0)
01772 goto vlc_fail;
01773
01774
01775 if (init_vlc(&s->ac_vlc_1[i], 5, 32,
01776 &s->huffman_table[i+16][0][1], 4, 2,
01777 &s->huffman_table[i+16][0][0], 4, 2, 0) < 0)
01778 goto vlc_fail;
01779
01780
01781 if (init_vlc(&s->ac_vlc_2[i], 5, 32,
01782 &s->huffman_table[i+16*2][0][1], 4, 2,
01783 &s->huffman_table[i+16*2][0][0], 4, 2, 0) < 0)
01784 goto vlc_fail;
01785
01786
01787 if (init_vlc(&s->ac_vlc_3[i], 5, 32,
01788 &s->huffman_table[i+16*3][0][1], 4, 2,
01789 &s->huffman_table[i+16*3][0][0], 4, 2, 0) < 0)
01790 goto vlc_fail;
01791
01792
01793 if (init_vlc(&s->ac_vlc_4[i], 5, 32,
01794 &s->huffman_table[i+16*4][0][1], 4, 2,
01795 &s->huffman_table[i+16*4][0][0], 4, 2, 0) < 0)
01796 goto vlc_fail;
01797 }
01798 }
01799
01800 init_vlc(&s->superblock_run_length_vlc, 6, 34,
01801 &superblock_run_length_vlc_table[0][1], 4, 2,
01802 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
01803
01804 init_vlc(&s->fragment_run_length_vlc, 5, 30,
01805 &fragment_run_length_vlc_table[0][1], 4, 2,
01806 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
01807
01808 init_vlc(&s->mode_code_vlc, 3, 8,
01809 &mode_code_vlc_table[0][1], 2, 1,
01810 &mode_code_vlc_table[0][0], 2, 1, 0);
01811
01812 init_vlc(&s->motion_vector_vlc, 6, 63,
01813 &motion_vector_vlc_table[0][1], 2, 1,
01814 &motion_vector_vlc_table[0][0], 2, 1, 0);
01815
01816
01817 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
01818 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
01819 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
01820 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
01821 if (!s->superblock_fragments || !s->superblock_macroblocks ||
01822 !s->macroblock_fragments || !s->macroblock_coding) {
01823 vp3_decode_end(avctx);
01824 return -1;
01825 }
01826 init_block_mapping(s);
01827
01828 for (i = 0; i < 3; i++) {
01829 s->current_frame.data[i] = NULL;
01830 s->last_frame.data[i] = NULL;
01831 s->golden_frame.data[i] = NULL;
01832 }
01833
01834 return 0;
01835
01836 vlc_fail:
01837 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
01838 return -1;
01839 }
01840
01841
01842
01843
01844 static int vp3_decode_frame(AVCodecContext *avctx,
01845 void *data, int *data_size,
01846 const uint8_t *buf, int buf_size)
01847 {
01848 Vp3DecodeContext *s = avctx->priv_data;
01849 GetBitContext gb;
01850 static int counter = 0;
01851 int i;
01852
01853 init_get_bits(&gb, buf, buf_size * 8);
01854
01855 if (s->theora && get_bits1(&gb))
01856 {
01857 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
01858 return -1;
01859 }
01860
01861 s->keyframe = !get_bits1(&gb);
01862 if (!s->theora)
01863 skip_bits(&gb, 1);
01864 s->last_quality_index = s->quality_index;
01865
01866 s->nqis=0;
01867 do{
01868 s->qis[s->nqis++]= get_bits(&gb, 6);
01869 } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
01870
01871 s->quality_index= s->qis[0];
01872
01873 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01874 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
01875 s->keyframe?"key":"", counter, s->quality_index);
01876 counter++;
01877
01878 if (s->quality_index != s->last_quality_index) {
01879 init_dequantizer(s);
01880 init_loop_filter(s);
01881 }
01882
01883 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
01884 return buf_size;
01885
01886 if (s->keyframe) {
01887 if (!s->theora)
01888 {
01889 skip_bits(&gb, 4);
01890 skip_bits(&gb, 4);
01891 if (s->version)
01892 {
01893 s->version = get_bits(&gb, 5);
01894 if (counter == 1)
01895 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
01896 }
01897 }
01898 if (s->version || s->theora)
01899 {
01900 if (get_bits1(&gb))
01901 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
01902 skip_bits(&gb, 2);
01903 }
01904
01905 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
01906 if (s->golden_frame.data[0])
01907 avctx->release_buffer(avctx, &s->golden_frame);
01908 s->last_frame= s->golden_frame;
01909 } else {
01910 if (s->golden_frame.data[0])
01911 avctx->release_buffer(avctx, &s->golden_frame);
01912 if (s->last_frame.data[0])
01913 avctx->release_buffer(avctx, &s->last_frame);
01914 }
01915
01916 s->golden_frame.reference = 3;
01917 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
01918 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
01919 return -1;
01920 }
01921
01922
01923 s->current_frame= s->golden_frame;
01924
01925
01926 if (!s->pixel_addresses_initialized)
01927 {
01928 vp3_calculate_pixel_addresses(s);
01929 s->pixel_addresses_initialized = 1;
01930 }
01931 } else {
01932
01933 s->current_frame.reference = 3;
01934 if (!s->pixel_addresses_initialized) {
01935 av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
01936 return -1;
01937 }
01938 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
01939 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
01940 return -1;
01941 }
01942 }
01943
01944 s->current_frame.qscale_table= s->qscale_table;
01945 s->current_frame.qstride= 0;
01946
01947 init_frame(s, &gb);
01948
01949 if (unpack_superblocks(s, &gb)){
01950 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
01951 return -1;
01952 }
01953 if (unpack_modes(s, &gb)){
01954 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
01955 return -1;
01956 }
01957 if (unpack_vectors(s, &gb)){
01958 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
01959 return -1;
01960 }
01961 if (unpack_dct_coeffs(s, &gb)){
01962 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
01963 return -1;
01964 }
01965
01966 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
01967 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
01968 reverse_dc_prediction(s, s->fragment_start[1],
01969 s->fragment_width / 2, s->fragment_height / 2);
01970 reverse_dc_prediction(s, s->fragment_start[2],
01971 s->fragment_width / 2, s->fragment_height / 2);
01972 }
01973
01974 for (i = 0; i < s->macroblock_height; i++)
01975 render_slice(s, i);
01976
01977 apply_loop_filter(s);
01978
01979 *data_size=sizeof(AVFrame);
01980 *(AVFrame*)data= s->current_frame;
01981
01982
01983
01984 if ((s->last_frame.data[0]) &&
01985 (s->last_frame.data[0] != s->golden_frame.data[0]))
01986 avctx->release_buffer(avctx, &s->last_frame);
01987
01988
01989 s->last_frame= s->current_frame;
01990 s->current_frame.data[0]= NULL;
01991
01992 return buf_size;
01993 }
01994
01995
01996
01997
01998 static av_cold int vp3_decode_end(AVCodecContext *avctx)
01999 {
02000 Vp3DecodeContext *s = avctx->priv_data;
02001 int i;
02002
02003 av_free(s->superblock_coding);
02004 av_free(s->all_fragments);
02005 av_free(s->coeff_counts);
02006 av_free(s->coeffs);
02007 av_free(s->coded_fragment_list);
02008 av_free(s->superblock_fragments);
02009 av_free(s->superblock_macroblocks);
02010 av_free(s->macroblock_fragments);
02011 av_free(s->macroblock_coding);
02012
02013 for (i = 0; i < 16; i++) {
02014 free_vlc(&s->dc_vlc[i]);
02015 free_vlc(&s->ac_vlc_1[i]);
02016 free_vlc(&s->ac_vlc_2[i]);
02017 free_vlc(&s->ac_vlc_3[i]);
02018 free_vlc(&s->ac_vlc_4[i]);
02019 }
02020
02021 free_vlc(&s->superblock_run_length_vlc);
02022 free_vlc(&s->fragment_run_length_vlc);
02023 free_vlc(&s->mode_code_vlc);
02024 free_vlc(&s->motion_vector_vlc);
02025
02026
02027 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
02028 avctx->release_buffer(avctx, &s->golden_frame);
02029 if (s->last_frame.data[0])
02030 avctx->release_buffer(avctx, &s->last_frame);
02031
02032
02033
02034 return 0;
02035 }
02036
02037 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
02038 {
02039 Vp3DecodeContext *s = avctx->priv_data;
02040
02041 if (get_bits1(gb)) {
02042 int token;
02043 if (s->entries >= 32) {
02044 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
02045 return -1;
02046 }
02047 token = get_bits(gb, 5);
02048
02049 s->huffman_table[s->hti][token][0] = s->hbits;
02050 s->huffman_table[s->hti][token][1] = s->huff_code_size;
02051 s->entries++;
02052 }
02053 else {
02054 if (s->huff_code_size >= 32) {
02055 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
02056 return -1;
02057 }
02058 s->huff_code_size++;
02059 s->hbits <<= 1;
02060 if (read_huffman_tree(avctx, gb))
02061 return -1;
02062 s->hbits |= 1;
02063 if (read_huffman_tree(avctx, gb))
02064 return -1;
02065 s->hbits >>= 1;
02066 s->huff_code_size--;
02067 }
02068 return 0;
02069 }
02070
02071 #if CONFIG_THEORA_DECODER
02072 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
02073 {
02074 Vp3DecodeContext *s = avctx->priv_data;
02075 int visible_width, visible_height;
02076
02077 s->theora = get_bits_long(gb, 24);
02078 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
02079
02080
02081
02082 if (s->theora < 0x030200)
02083 {
02084 s->flipped_image = 1;
02085 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
02086 }
02087
02088 visible_width = s->width = get_bits(gb, 16) << 4;
02089 visible_height = s->height = get_bits(gb, 16) << 4;
02090
02091 if(avcodec_check_dimensions(avctx, s->width, s->height)){
02092 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
02093 s->width= s->height= 0;
02094 return -1;
02095 }
02096
02097 if (s->theora >= 0x030400)
02098 {
02099 skip_bits(gb, 32);
02100
02101 skip_bits(gb, 32);
02102 skip_bits(gb, 4);
02103 skip_bits(gb, 32);
02104 }
02105
02106 if (s->theora >= 0x030200) {
02107 visible_width = get_bits_long(gb, 24);
02108 visible_height = get_bits_long(gb, 24);
02109
02110 skip_bits(gb, 8);
02111 skip_bits(gb, 8);
02112 }
02113
02114 skip_bits(gb, 32);
02115 skip_bits(gb, 32);
02116 skip_bits(gb, 24);
02117 skip_bits(gb, 24);
02118
02119 if (s->theora < 0x030200)
02120 skip_bits(gb, 5);
02121 skip_bits(gb, 8);
02122 if (s->theora >= 0x030400)
02123 skip_bits(gb, 2);
02124 skip_bits(gb, 24);
02125
02126 skip_bits(gb, 6);
02127
02128 if (s->theora >= 0x030200)
02129 {
02130 skip_bits(gb, 5);
02131
02132 if (s->theora < 0x030400)
02133 skip_bits(gb, 5);
02134 }
02135
02136
02137
02138 if ( visible_width <= s->width && visible_width > s->width-16
02139 && visible_height <= s->height && visible_height > s->height-16)
02140 avcodec_set_dimensions(avctx, visible_width, visible_height);
02141 else
02142 avcodec_set_dimensions(avctx, s->width, s->height);
02143
02144 return 0;
02145 }
02146
02147 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
02148 {
02149 Vp3DecodeContext *s = avctx->priv_data;
02150 int i, n, matrices, inter, plane;
02151
02152 if (s->theora >= 0x030200) {
02153 n = get_bits(gb, 3);
02154
02155 for (i = 0; i < 64; i++)
02156 s->filter_limit_values[i] = get_bits(gb, n);
02157 }
02158
02159 if (s->theora >= 0x030200)
02160 n = get_bits(gb, 4) + 1;
02161 else
02162 n = 16;
02163
02164 for (i = 0; i < 64; i++)
02165 s->coded_ac_scale_factor[i] = get_bits(gb, n);
02166
02167 if (s->theora >= 0x030200)
02168 n = get_bits(gb, 4) + 1;
02169 else
02170 n = 16;
02171
02172 for (i = 0; i < 64; i++)
02173 s->coded_dc_scale_factor[i] = get_bits(gb, n);
02174
02175 if (s->theora >= 0x030200)
02176 matrices = get_bits(gb, 9) + 1;
02177 else
02178 matrices = 3;
02179
02180 if(matrices > 384){
02181 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
02182 return -1;
02183 }
02184
02185 for(n=0; n<matrices; n++){
02186 for (i = 0; i < 64; i++)
02187 s->base_matrix[n][i]= get_bits(gb, 8);
02188 }
02189
02190 for (inter = 0; inter <= 1; inter++) {
02191 for (plane = 0; plane <= 2; plane++) {
02192 int newqr= 1;
02193 if (inter || plane > 0)
02194 newqr = get_bits1(gb);
02195 if (!newqr) {
02196 int qtj, plj;
02197 if(inter && get_bits1(gb)){
02198 qtj = 0;
02199 plj = plane;
02200 }else{
02201 qtj= (3*inter + plane - 1) / 3;
02202 plj= (plane + 2) % 3;
02203 }
02204 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
02205 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
02206 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
02207 } else {
02208 int qri= 0;
02209 int qi = 0;
02210
02211 for(;;){
02212 i= get_bits(gb, av_log2(matrices-1)+1);
02213 if(i>= matrices){
02214 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
02215 return -1;
02216 }
02217 s->qr_base[inter][plane][qri]= i;
02218 if(qi >= 63)
02219 break;
02220 i = get_bits(gb, av_log2(63-qi)+1) + 1;
02221 s->qr_size[inter][plane][qri++]= i;
02222 qi += i;
02223 }
02224
02225 if (qi > 63) {
02226 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
02227 return -1;
02228 }
02229 s->qr_count[inter][plane]= qri;
02230 }
02231 }
02232 }
02233
02234
02235 for (s->hti = 0; s->hti < 80; s->hti++) {
02236 s->entries = 0;
02237 s->huff_code_size = 1;
02238 if (!get_bits1(gb)) {
02239 s->hbits = 0;
02240 if(read_huffman_tree(avctx, gb))
02241 return -1;
02242 s->hbits = 1;
02243 if(read_huffman_tree(avctx, gb))
02244 return -1;
02245 }
02246 }
02247
02248 s->theora_tables = 1;
02249
02250 return 0;
02251 }
02252
02253 static av_cold int theora_decode_init(AVCodecContext *avctx)
02254 {
02255 Vp3DecodeContext *s = avctx->priv_data;
02256 GetBitContext gb;
02257 int ptype;
02258 uint8_t *header_start[3];
02259 int header_len[3];
02260 int i;
02261
02262 s->theora = 1;
02263
02264 if (!avctx->extradata_size)
02265 {
02266 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
02267 return -1;
02268 }
02269
02270 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
02271 42, header_start, header_len) < 0) {
02272 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
02273 return -1;
02274 }
02275
02276 for(i=0;i<3;i++) {
02277 init_get_bits(&gb, header_start[i], header_len[i] * 8);
02278
02279 ptype = get_bits(&gb, 8);
02280
02281 if (!(ptype & 0x80))
02282 {
02283 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
02284
02285 }
02286
02287
02288 skip_bits(&gb, 6*8);
02289
02290 switch(ptype)
02291 {
02292 case 0x80:
02293 theora_decode_header(avctx, &gb);
02294 break;
02295 case 0x81:
02296
02297
02298 break;
02299 case 0x82:
02300 if (theora_decode_tables(avctx, &gb))
02301 return -1;
02302 break;
02303 default:
02304 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
02305 break;
02306 }
02307 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
02308 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
02309 if (s->theora < 0x030200)
02310 break;
02311 }
02312
02313 vp3_decode_init(avctx);
02314 return 0;
02315 }
02316
02317 AVCodec theora_decoder = {
02318 "theora",
02319 CODEC_TYPE_VIDEO,
02320 CODEC_ID_THEORA,
02321 sizeof(Vp3DecodeContext),
02322 theora_decode_init,
02323 NULL,
02324 vp3_decode_end,
02325 vp3_decode_frame,
02326 0,
02327 NULL,
02328 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
02329 };
02330 #endif
02331
02332 AVCodec vp3_decoder = {
02333 "vp3",
02334 CODEC_TYPE_VIDEO,
02335 CODEC_ID_VP3,
02336 sizeof(Vp3DecodeContext),
02337 vp3_decode_init,
02338 NULL,
02339 vp3_decode_end,
02340 vp3_decode_frame,
02341 0,
02342 NULL,
02343 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
02344 };