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