Libav
|
00001 /* 00002 * Copyright (C) 2003-2004 the ffmpeg project 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * FFmpeg is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00032 #include <stdio.h> 00033 #include <stdlib.h> 00034 #include <string.h> 00035 00036 #include "avcodec.h" 00037 #include "dsputil.h" 00038 #include "get_bits.h" 00039 00040 #include "vp3data.h" 00041 #include "xiph.h" 00042 00043 #define FRAGMENT_PIXELS 8 00044 00045 static av_cold int vp3_decode_end(AVCodecContext *avctx); 00046 00047 //FIXME split things out into their own arrays 00048 typedef struct Vp3Fragment { 00049 int16_t dc; 00050 uint8_t coding_method; 00051 uint8_t qpi; 00052 } Vp3Fragment; 00053 00054 #define SB_NOT_CODED 0 00055 #define SB_PARTIALLY_CODED 1 00056 #define SB_FULLY_CODED 2 00057 00058 // This is the maximum length of a single long bit run that can be encoded 00059 // for superblock coding or block qps. Theora special-cases this to read a 00060 // bit instead of flipping the current bit to allow for runs longer than 4129. 00061 #define MAXIMUM_LONG_BIT_RUN 4129 00062 00063 #define MODE_INTER_NO_MV 0 00064 #define MODE_INTRA 1 00065 #define MODE_INTER_PLUS_MV 2 00066 #define MODE_INTER_LAST_MV 3 00067 #define MODE_INTER_PRIOR_LAST 4 00068 #define MODE_USING_GOLDEN 5 00069 #define MODE_GOLDEN_MV 6 00070 #define MODE_INTER_FOURMV 7 00071 #define CODING_MODE_COUNT 8 00072 00073 /* special internal mode */ 00074 #define MODE_COPY 8 00075 00076 /* There are 6 preset schemes, plus a free-form scheme */ 00077 static const int ModeAlphabet[6][CODING_MODE_COUNT] = 00078 { 00079 /* scheme 1: Last motion vector dominates */ 00080 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, 00081 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV, 00082 MODE_INTRA, MODE_USING_GOLDEN, 00083 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 00084 00085 /* scheme 2 */ 00086 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, 00087 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV, 00088 MODE_INTRA, MODE_USING_GOLDEN, 00089 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 00090 00091 /* scheme 3 */ 00092 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV, 00093 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV, 00094 MODE_INTRA, MODE_USING_GOLDEN, 00095 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 00096 00097 /* scheme 4 */ 00098 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV, 00099 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST, 00100 MODE_INTRA, MODE_USING_GOLDEN, 00101 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 00102 00103 /* scheme 5: No motion vector dominates */ 00104 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV, 00105 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV, 00106 MODE_INTRA, MODE_USING_GOLDEN, 00107 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 00108 00109 /* scheme 6 */ 00110 { MODE_INTER_NO_MV, MODE_USING_GOLDEN, 00111 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, 00112 MODE_INTER_PLUS_MV, MODE_INTRA, 00113 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 00114 00115 }; 00116 00117 static const uint8_t hilbert_offset[16][2] = { 00118 {0,0}, {1,0}, {1,1}, {0,1}, 00119 {0,2}, {0,3}, {1,3}, {1,2}, 00120 {2,2}, {2,3}, {3,3}, {3,2}, 00121 {3,1}, {2,1}, {2,0}, {3,0} 00122 }; 00123 00124 #define MIN_DEQUANT_VAL 2 00125 00126 typedef struct Vp3DecodeContext { 00127 AVCodecContext *avctx; 00128 int theora, theora_tables; 00129 int version; 00130 int width, height; 00131 int chroma_x_shift, chroma_y_shift; 00132 AVFrame golden_frame; 00133 AVFrame last_frame; 00134 AVFrame current_frame; 00135 int keyframe; 00136 DSPContext dsp; 00137 int flipped_image; 00138 int last_slice_end; 00139 00140 int qps[3]; 00141 int nqps; 00142 int last_qps[3]; 00143 00144 int superblock_count; 00145 int y_superblock_width; 00146 int y_superblock_height; 00147 int y_superblock_count; 00148 int c_superblock_width; 00149 int c_superblock_height; 00150 int c_superblock_count; 00151 int u_superblock_start; 00152 int v_superblock_start; 00153 unsigned char *superblock_coding; 00154 00155 int macroblock_count; 00156 int macroblock_width; 00157 int macroblock_height; 00158 00159 int fragment_count; 00160 int fragment_width[2]; 00161 int fragment_height[2]; 00162 00163 Vp3Fragment *all_fragments; 00164 int fragment_start[3]; 00165 int data_offset[3]; 00166 00167 int8_t (*motion_val[2])[2]; 00168 00169 ScanTable scantable; 00170 00171 /* tables */ 00172 uint16_t coded_dc_scale_factor[64]; 00173 uint32_t coded_ac_scale_factor[64]; 00174 uint8_t base_matrix[384][64]; 00175 uint8_t qr_count[2][3]; 00176 uint8_t qr_size [2][3][64]; 00177 uint16_t qr_base[2][3][64]; 00178 00196 int16_t *dct_tokens[3][64]; 00197 int16_t *dct_tokens_base; 00198 #define TOKEN_EOB(eob_run) ((eob_run) << 2) 00199 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1) 00200 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2) 00201 00205 int num_coded_frags[3][64]; 00206 int total_num_coded_frags; 00207 00208 /* this is a list of indexes into the all_fragments array indicating 00209 * which of the fragments are coded */ 00210 int *coded_fragment_list[3]; 00211 00212 VLC dc_vlc[16]; 00213 VLC ac_vlc_1[16]; 00214 VLC ac_vlc_2[16]; 00215 VLC ac_vlc_3[16]; 00216 VLC ac_vlc_4[16]; 00217 00218 VLC superblock_run_length_vlc; 00219 VLC fragment_run_length_vlc; 00220 VLC mode_code_vlc; 00221 VLC motion_vector_vlc; 00222 00223 /* these arrays need to be on 16-byte boundaries since SSE2 operations 00224 * index into them */ 00225 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][plane] 00226 00227 /* This table contains superblock_count * 16 entries. Each set of 16 00228 * numbers corresponds to the fragment indexes 0..15 of the superblock. 00229 * An entry will be -1 to indicate that no entry corresponds to that 00230 * index. */ 00231 int *superblock_fragments; 00232 00233 /* This is an array that indicates how a particular macroblock 00234 * is coded. */ 00235 unsigned char *macroblock_coding; 00236 00237 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc 00238 int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16 00239 00240 /* Huffman decode */ 00241 int hti; 00242 unsigned int hbits; 00243 int entries; 00244 int huff_code_size; 00245 uint32_t huffman_table[80][32][2]; 00246 00247 uint8_t filter_limit_values[64]; 00248 DECLARE_ALIGNED(8, int, bounding_values_array)[256+2]; 00249 } Vp3DecodeContext; 00250 00251 /************************************************************************ 00252 * VP3 specific functions 00253 ************************************************************************/ 00254 00255 /* 00256 * This function sets up all of the various blocks mappings: 00257 * superblocks <-> fragments, macroblocks <-> fragments, 00258 * superblocks <-> macroblocks 00259 * 00260 * Returns 0 is successful; returns 1 if *anything* went wrong. 00261 */ 00262 static int init_block_mapping(Vp3DecodeContext *s) 00263 { 00264 int sb_x, sb_y, plane; 00265 int x, y, i, j = 0; 00266 00267 for (plane = 0; plane < 3; plane++) { 00268 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width; 00269 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height; 00270 int frag_width = s->fragment_width[!!plane]; 00271 int frag_height = s->fragment_height[!!plane]; 00272 00273 for (sb_y = 0; sb_y < sb_height; sb_y++) 00274 for (sb_x = 0; sb_x < sb_width; sb_x++) 00275 for (i = 0; i < 16; i++) { 00276 x = 4*sb_x + hilbert_offset[i][0]; 00277 y = 4*sb_y + hilbert_offset[i][1]; 00278 00279 if (x < frag_width && y < frag_height) 00280 s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x; 00281 else 00282 s->superblock_fragments[j++] = -1; 00283 } 00284 } 00285 00286 return 0; /* successful path out */ 00287 } 00288 00289 /* 00290 * This function sets up the dequantization tables used for a particular 00291 * frame. 00292 */ 00293 static void init_dequantizer(Vp3DecodeContext *s, int qpi) 00294 { 00295 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]]; 00296 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]]; 00297 int i, plane, inter, qri, bmi, bmj, qistart; 00298 00299 for(inter=0; inter<2; inter++){ 00300 for(plane=0; plane<3; plane++){ 00301 int sum=0; 00302 for(qri=0; qri<s->qr_count[inter][plane]; qri++){ 00303 sum+= s->qr_size[inter][plane][qri]; 00304 if(s->qps[qpi] <= sum) 00305 break; 00306 } 00307 qistart= sum - s->qr_size[inter][plane][qri]; 00308 bmi= s->qr_base[inter][plane][qri ]; 00309 bmj= s->qr_base[inter][plane][qri+1]; 00310 for(i=0; i<64; i++){ 00311 int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i] 00312 - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i] 00313 + s->qr_size[inter][plane][qri]) 00314 / (2*s->qr_size[inter][plane][qri]); 00315 00316 int qmin= 8<<(inter + !i); 00317 int qscale= i ? ac_scale_factor : dc_scale_factor; 00318 00319 s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096); 00320 } 00321 // all DC coefficients use the same quant so as not to interfere with DC prediction 00322 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0]; 00323 } 00324 } 00325 00326 memset(s->qscale_table, (FFMAX(s->qmat[0][0][0][1], s->qmat[0][0][1][1])+8)/16, 512); //FIXME finetune 00327 } 00328 00329 /* 00330 * This function initializes the loop filter boundary limits if the frame's 00331 * quality index is different from the previous frame's. 00332 * 00333 * The filter_limit_values may not be larger than 127. 00334 */ 00335 static void init_loop_filter(Vp3DecodeContext *s) 00336 { 00337 int *bounding_values= s->bounding_values_array+127; 00338 int filter_limit; 00339 int x; 00340 int value; 00341 00342 filter_limit = s->filter_limit_values[s->qps[0]]; 00343 00344 /* set up the bounding values */ 00345 memset(s->bounding_values_array, 0, 256 * sizeof(int)); 00346 for (x = 0; x < filter_limit; x++) { 00347 bounding_values[-x] = -x; 00348 bounding_values[x] = x; 00349 } 00350 for (x = value = filter_limit; x < 128 && value; x++, value--) { 00351 bounding_values[ x] = value; 00352 bounding_values[-x] = -value; 00353 } 00354 if (value) 00355 bounding_values[128] = value; 00356 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202; 00357 } 00358 00359 /* 00360 * This function unpacks all of the superblock/macroblock/fragment coding 00361 * information from the bitstream. 00362 */ 00363 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) 00364 { 00365 int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start }; 00366 int bit = 0; 00367 int current_superblock = 0; 00368 int current_run = 0; 00369 int num_partial_superblocks = 0; 00370 00371 int i, j; 00372 int current_fragment; 00373 int plane; 00374 00375 if (s->keyframe) { 00376 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count); 00377 00378 } else { 00379 00380 /* unpack the list of partially-coded superblocks */ 00381 bit = get_bits1(gb); 00382 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) { 00383 current_run = get_vlc2(gb, 00384 s->superblock_run_length_vlc.table, 6, 2) + 1; 00385 if (current_run == 34) 00386 current_run += get_bits(gb, 12); 00387 00388 if (current_superblock + current_run > s->superblock_count) { 00389 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n"); 00390 return -1; 00391 } 00392 00393 memset(s->superblock_coding + current_superblock, bit, current_run); 00394 00395 current_superblock += current_run; 00396 if (bit) 00397 num_partial_superblocks += current_run; 00398 00399 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN) 00400 bit = get_bits1(gb); 00401 else 00402 bit ^= 1; 00403 } 00404 00405 /* unpack the list of fully coded superblocks if any of the blocks were 00406 * not marked as partially coded in the previous step */ 00407 if (num_partial_superblocks < s->superblock_count) { 00408 int superblocks_decoded = 0; 00409 00410 current_superblock = 0; 00411 bit = get_bits1(gb); 00412 while (superblocks_decoded < s->superblock_count - num_partial_superblocks 00413 && get_bits_left(gb) > 0) { 00414 current_run = get_vlc2(gb, 00415 s->superblock_run_length_vlc.table, 6, 2) + 1; 00416 if (current_run == 34) 00417 current_run += get_bits(gb, 12); 00418 00419 for (j = 0; j < current_run; current_superblock++) { 00420 if (current_superblock >= s->superblock_count) { 00421 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n"); 00422 return -1; 00423 } 00424 00425 /* skip any superblocks already marked as partially coded */ 00426 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) { 00427 s->superblock_coding[current_superblock] = 2*bit; 00428 j++; 00429 } 00430 } 00431 superblocks_decoded += current_run; 00432 00433 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN) 00434 bit = get_bits1(gb); 00435 else 00436 bit ^= 1; 00437 } 00438 } 00439 00440 /* if there were partial blocks, initialize bitstream for 00441 * unpacking fragment codings */ 00442 if (num_partial_superblocks) { 00443 00444 current_run = 0; 00445 bit = get_bits1(gb); 00446 /* toggle the bit because as soon as the first run length is 00447 * fetched the bit will be toggled again */ 00448 bit ^= 1; 00449 } 00450 } 00451 00452 /* figure out which fragments are coded; iterate through each 00453 * superblock (all planes) */ 00454 s->total_num_coded_frags = 0; 00455 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count); 00456 00457 for (plane = 0; plane < 3; plane++) { 00458 int sb_start = superblock_starts[plane]; 00459 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count); 00460 int num_coded_frags = 0; 00461 00462 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) { 00463 00464 /* iterate through all 16 fragments in a superblock */ 00465 for (j = 0; j < 16; j++) { 00466 00467 /* if the fragment is in bounds, check its coding status */ 00468 current_fragment = s->superblock_fragments[i * 16 + j]; 00469 if (current_fragment != -1) { 00470 int coded = s->superblock_coding[i]; 00471 00472 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) { 00473 00474 /* fragment may or may not be coded; this is the case 00475 * that cares about the fragment coding runs */ 00476 if (current_run-- == 0) { 00477 bit ^= 1; 00478 current_run = get_vlc2(gb, 00479 s->fragment_run_length_vlc.table, 5, 2); 00480 } 00481 coded = bit; 00482 } 00483 00484 if (coded) { 00485 /* default mode; actual mode will be decoded in 00486 * the next phase */ 00487 s->all_fragments[current_fragment].coding_method = 00488 MODE_INTER_NO_MV; 00489 s->coded_fragment_list[plane][num_coded_frags++] = 00490 current_fragment; 00491 } else { 00492 /* not coded; copy this fragment from the prior frame */ 00493 s->all_fragments[current_fragment].coding_method = 00494 MODE_COPY; 00495 } 00496 } 00497 } 00498 } 00499 s->total_num_coded_frags += num_coded_frags; 00500 for (i = 0; i < 64; i++) 00501 s->num_coded_frags[plane][i] = num_coded_frags; 00502 if (plane < 2) 00503 s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags; 00504 } 00505 return 0; 00506 } 00507 00508 /* 00509 * This function unpacks all the coding mode data for individual macroblocks 00510 * from the bitstream. 00511 */ 00512 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) 00513 { 00514 int i, j, k, sb_x, sb_y; 00515 int scheme; 00516 int current_macroblock; 00517 int current_fragment; 00518 int coding_mode; 00519 int custom_mode_alphabet[CODING_MODE_COUNT]; 00520 const int *alphabet; 00521 Vp3Fragment *frag; 00522 00523 if (s->keyframe) { 00524 for (i = 0; i < s->fragment_count; i++) 00525 s->all_fragments[i].coding_method = MODE_INTRA; 00526 00527 } else { 00528 00529 /* fetch the mode coding scheme for this frame */ 00530 scheme = get_bits(gb, 3); 00531 00532 /* is it a custom coding scheme? */ 00533 if (scheme == 0) { 00534 for (i = 0; i < 8; i++) 00535 custom_mode_alphabet[i] = MODE_INTER_NO_MV; 00536 for (i = 0; i < 8; i++) 00537 custom_mode_alphabet[get_bits(gb, 3)] = i; 00538 alphabet = custom_mode_alphabet; 00539 } else 00540 alphabet = ModeAlphabet[scheme-1]; 00541 00542 /* iterate through all of the macroblocks that contain 1 or more 00543 * coded fragments */ 00544 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { 00545 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { 00546 if (get_bits_left(gb) <= 0) 00547 return -1; 00548 00549 for (j = 0; j < 4; j++) { 00550 int mb_x = 2*sb_x + (j>>1); 00551 int mb_y = 2*sb_y + (((j>>1)+j)&1); 00552 current_macroblock = mb_y * s->macroblock_width + mb_x; 00553 00554 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height) 00555 continue; 00556 00557 #define BLOCK_X (2*mb_x + (k&1)) 00558 #define BLOCK_Y (2*mb_y + (k>>1)) 00559 /* coding modes are only stored if the macroblock has at least one 00560 * luma block coded, otherwise it must be INTER_NO_MV */ 00561 for (k = 0; k < 4; k++) { 00562 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X; 00563 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) 00564 break; 00565 } 00566 if (k == 4) { 00567 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV; 00568 continue; 00569 } 00570 00571 /* mode 7 means get 3 bits for each coding mode */ 00572 if (scheme == 7) 00573 coding_mode = get_bits(gb, 3); 00574 else 00575 coding_mode = alphabet 00576 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)]; 00577 00578 s->macroblock_coding[current_macroblock] = coding_mode; 00579 for (k = 0; k < 4; k++) { 00580 frag = s->all_fragments + BLOCK_Y*s->fragment_width[0] + BLOCK_X; 00581 if (frag->coding_method != MODE_COPY) 00582 frag->coding_method = coding_mode; 00583 } 00584 00585 #define SET_CHROMA_MODES \ 00586 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \ 00587 frag[s->fragment_start[1]].coding_method = coding_mode;\ 00588 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \ 00589 frag[s->fragment_start[2]].coding_method = coding_mode; 00590 00591 if (s->chroma_y_shift) { 00592 frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x; 00593 SET_CHROMA_MODES 00594 } else if (s->chroma_x_shift) { 00595 frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x; 00596 for (k = 0; k < 2; k++) { 00597 SET_CHROMA_MODES 00598 frag += s->fragment_width[1]; 00599 } 00600 } else { 00601 for (k = 0; k < 4; k++) { 00602 frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X; 00603 SET_CHROMA_MODES 00604 } 00605 } 00606 } 00607 } 00608 } 00609 } 00610 00611 return 0; 00612 } 00613 00614 /* 00615 * This function unpacks all the motion vectors for the individual 00616 * macroblocks from the bitstream. 00617 */ 00618 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) 00619 { 00620 int j, k, sb_x, sb_y; 00621 int coding_mode; 00622 int motion_x[4]; 00623 int motion_y[4]; 00624 int last_motion_x = 0; 00625 int last_motion_y = 0; 00626 int prior_last_motion_x = 0; 00627 int prior_last_motion_y = 0; 00628 int current_macroblock; 00629 int current_fragment; 00630 int frag; 00631 00632 if (s->keyframe) 00633 return 0; 00634 00635 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */ 00636 coding_mode = get_bits1(gb); 00637 00638 /* iterate through all of the macroblocks that contain 1 or more 00639 * coded fragments */ 00640 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { 00641 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { 00642 if (get_bits_left(gb) <= 0) 00643 return -1; 00644 00645 for (j = 0; j < 4; j++) { 00646 int mb_x = 2*sb_x + (j>>1); 00647 int mb_y = 2*sb_y + (((j>>1)+j)&1); 00648 current_macroblock = mb_y * s->macroblock_width + mb_x; 00649 00650 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height || 00651 (s->macroblock_coding[current_macroblock] == MODE_COPY)) 00652 continue; 00653 00654 switch (s->macroblock_coding[current_macroblock]) { 00655 00656 case MODE_INTER_PLUS_MV: 00657 case MODE_GOLDEN_MV: 00658 /* all 6 fragments use the same motion vector */ 00659 if (coding_mode == 0) { 00660 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; 00661 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; 00662 } else { 00663 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)]; 00664 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)]; 00665 } 00666 00667 /* vector maintenance, only on MODE_INTER_PLUS_MV */ 00668 if (s->macroblock_coding[current_macroblock] == 00669 MODE_INTER_PLUS_MV) { 00670 prior_last_motion_x = last_motion_x; 00671 prior_last_motion_y = last_motion_y; 00672 last_motion_x = motion_x[0]; 00673 last_motion_y = motion_y[0]; 00674 } 00675 break; 00676 00677 case MODE_INTER_FOURMV: 00678 /* vector maintenance */ 00679 prior_last_motion_x = last_motion_x; 00680 prior_last_motion_y = last_motion_y; 00681 00682 /* fetch 4 vectors from the bitstream, one for each 00683 * Y fragment, then average for the C fragment vectors */ 00684 for (k = 0; k < 4; k++) { 00685 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X; 00686 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) { 00687 if (coding_mode == 0) { 00688 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; 00689 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; 00690 } else { 00691 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)]; 00692 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)]; 00693 } 00694 last_motion_x = motion_x[k]; 00695 last_motion_y = motion_y[k]; 00696 } else { 00697 motion_x[k] = 0; 00698 motion_y[k] = 0; 00699 } 00700 } 00701 break; 00702 00703 case MODE_INTER_LAST_MV: 00704 /* all 6 fragments use the last motion vector */ 00705 motion_x[0] = last_motion_x; 00706 motion_y[0] = last_motion_y; 00707 00708 /* no vector maintenance (last vector remains the 00709 * last vector) */ 00710 break; 00711 00712 case MODE_INTER_PRIOR_LAST: 00713 /* all 6 fragments use the motion vector prior to the 00714 * last motion vector */ 00715 motion_x[0] = prior_last_motion_x; 00716 motion_y[0] = prior_last_motion_y; 00717 00718 /* vector maintenance */ 00719 prior_last_motion_x = last_motion_x; 00720 prior_last_motion_y = last_motion_y; 00721 last_motion_x = motion_x[0]; 00722 last_motion_y = motion_y[0]; 00723 break; 00724 00725 default: 00726 /* covers intra, inter without MV, golden without MV */ 00727 motion_x[0] = 0; 00728 motion_y[0] = 0; 00729 00730 /* no vector maintenance */ 00731 break; 00732 } 00733 00734 /* assign the motion vectors to the correct fragments */ 00735 for (k = 0; k < 4; k++) { 00736 current_fragment = 00737 BLOCK_Y*s->fragment_width[0] + BLOCK_X; 00738 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { 00739 s->motion_val[0][current_fragment][0] = motion_x[k]; 00740 s->motion_val[0][current_fragment][1] = motion_y[k]; 00741 } else { 00742 s->motion_val[0][current_fragment][0] = motion_x[0]; 00743 s->motion_val[0][current_fragment][1] = motion_y[0]; 00744 } 00745 } 00746 00747 if (s->chroma_y_shift) { 00748 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { 00749 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2); 00750 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2); 00751 } 00752 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1); 00753 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1); 00754 frag = mb_y*s->fragment_width[1] + mb_x; 00755 s->motion_val[1][frag][0] = motion_x[0]; 00756 s->motion_val[1][frag][1] = motion_y[0]; 00757 } else if (s->chroma_x_shift) { 00758 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { 00759 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1); 00760 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1); 00761 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1); 00762 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1); 00763 } else { 00764 motion_x[1] = motion_x[0]; 00765 motion_y[1] = motion_y[0]; 00766 } 00767 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1); 00768 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1); 00769 00770 frag = 2*mb_y*s->fragment_width[1] + mb_x; 00771 for (k = 0; k < 2; k++) { 00772 s->motion_val[1][frag][0] = motion_x[k]; 00773 s->motion_val[1][frag][1] = motion_y[k]; 00774 frag += s->fragment_width[1]; 00775 } 00776 } else { 00777 for (k = 0; k < 4; k++) { 00778 frag = BLOCK_Y*s->fragment_width[1] + BLOCK_X; 00779 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { 00780 s->motion_val[1][frag][0] = motion_x[k]; 00781 s->motion_val[1][frag][1] = motion_y[k]; 00782 } else { 00783 s->motion_val[1][frag][0] = motion_x[0]; 00784 s->motion_val[1][frag][1] = motion_y[0]; 00785 } 00786 } 00787 } 00788 } 00789 } 00790 } 00791 00792 return 0; 00793 } 00794 00795 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb) 00796 { 00797 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi; 00798 int num_blocks = s->total_num_coded_frags; 00799 00800 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) { 00801 i = blocks_decoded = num_blocks_at_qpi = 0; 00802 00803 bit = get_bits1(gb); 00804 00805 do { 00806 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1; 00807 if (run_length == 34) 00808 run_length += get_bits(gb, 12); 00809 blocks_decoded += run_length; 00810 00811 if (!bit) 00812 num_blocks_at_qpi += run_length; 00813 00814 for (j = 0; j < run_length; i++) { 00815 if (i >= s->total_num_coded_frags) 00816 return -1; 00817 00818 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) { 00819 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit; 00820 j++; 00821 } 00822 } 00823 00824 if (run_length == MAXIMUM_LONG_BIT_RUN) 00825 bit = get_bits1(gb); 00826 else 00827 bit ^= 1; 00828 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0); 00829 00830 num_blocks -= num_blocks_at_qpi; 00831 } 00832 00833 return 0; 00834 } 00835 00836 /* 00837 * This function is called by unpack_dct_coeffs() to extract the VLCs from 00838 * the bitstream. The VLCs encode tokens which are used to unpack DCT 00839 * data. This function unpacks all the VLCs for either the Y plane or both 00840 * C planes, and is called for DC coefficients or different AC coefficient 00841 * levels (since different coefficient types require different VLC tables. 00842 * 00843 * This function returns a residual eob run. E.g, if a particular token gave 00844 * instructions to EOB the next 5 fragments and there were only 2 fragments 00845 * left in the current fragment range, 3 would be returned so that it could 00846 * be passed into the next call to this same function. 00847 */ 00848 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, 00849 VLC *table, int coeff_index, 00850 int plane, 00851 int eob_run) 00852 { 00853 int i, j = 0; 00854 int token; 00855 int zero_run = 0; 00856 DCTELEM coeff = 0; 00857 int bits_to_get; 00858 int blocks_ended; 00859 int coeff_i = 0; 00860 int num_coeffs = s->num_coded_frags[plane][coeff_index]; 00861 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index]; 00862 00863 /* local references to structure members to avoid repeated deferences */ 00864 int *coded_fragment_list = s->coded_fragment_list[plane]; 00865 Vp3Fragment *all_fragments = s->all_fragments; 00866 VLC_TYPE (*vlc_table)[2] = table->table; 00867 00868 if (num_coeffs < 0) 00869 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index); 00870 00871 if (eob_run > num_coeffs) { 00872 coeff_i = blocks_ended = num_coeffs; 00873 eob_run -= num_coeffs; 00874 } else { 00875 coeff_i = blocks_ended = eob_run; 00876 eob_run = 0; 00877 } 00878 00879 // insert fake EOB token to cover the split between planes or zzi 00880 if (blocks_ended) 00881 dct_tokens[j++] = blocks_ended << 2; 00882 00883 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) { 00884 /* decode a VLC into a token */ 00885 token = get_vlc2(gb, vlc_table, 11, 3); 00886 /* use the token to get a zero run, a coefficient, and an eob run */ 00887 if (token <= 6) { 00888 eob_run = eob_run_base[token]; 00889 if (eob_run_get_bits[token]) 00890 eob_run += get_bits(gb, eob_run_get_bits[token]); 00891 00892 // record only the number of blocks ended in this plane, 00893 // any spill will be recorded in the next plane. 00894 if (eob_run > num_coeffs - coeff_i) { 00895 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i); 00896 blocks_ended += num_coeffs - coeff_i; 00897 eob_run -= num_coeffs - coeff_i; 00898 coeff_i = num_coeffs; 00899 } else { 00900 dct_tokens[j++] = TOKEN_EOB(eob_run); 00901 blocks_ended += eob_run; 00902 coeff_i += eob_run; 00903 eob_run = 0; 00904 } 00905 } else { 00906 bits_to_get = coeff_get_bits[token]; 00907 if (bits_to_get) 00908 bits_to_get = get_bits(gb, bits_to_get); 00909 coeff = coeff_tables[token][bits_to_get]; 00910 00911 zero_run = zero_run_base[token]; 00912 if (zero_run_get_bits[token]) 00913 zero_run += get_bits(gb, zero_run_get_bits[token]); 00914 00915 if (zero_run) { 00916 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run); 00917 } else { 00918 // Save DC into the fragment structure. DC prediction is 00919 // done in raster order, so the actual DC can't be in with 00920 // other tokens. We still need the token in dct_tokens[] 00921 // however, or else the structure collapses on itself. 00922 if (!coeff_index) 00923 all_fragments[coded_fragment_list[coeff_i]].dc = coeff; 00924 00925 dct_tokens[j++] = TOKEN_COEFF(coeff); 00926 } 00927 00928 if (coeff_index + zero_run > 64) { 00929 av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with" 00930 " %d coeffs left\n", zero_run, 64-coeff_index); 00931 zero_run = 64 - coeff_index; 00932 } 00933 00934 // zero runs code multiple coefficients, 00935 // so don't try to decode coeffs for those higher levels 00936 for (i = coeff_index+1; i <= coeff_index+zero_run; i++) 00937 s->num_coded_frags[plane][i]--; 00938 coeff_i++; 00939 } 00940 } 00941 00942 if (blocks_ended > s->num_coded_frags[plane][coeff_index]) 00943 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n"); 00944 00945 // decrement the number of blocks that have higher coeffecients for each 00946 // EOB run at this level 00947 if (blocks_ended) 00948 for (i = coeff_index+1; i < 64; i++) 00949 s->num_coded_frags[plane][i] -= blocks_ended; 00950 00951 // setup the next buffer 00952 if (plane < 2) 00953 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j; 00954 else if (coeff_index < 63) 00955 s->dct_tokens[0][coeff_index+1] = dct_tokens + j; 00956 00957 return eob_run; 00958 } 00959 00960 static void reverse_dc_prediction(Vp3DecodeContext *s, 00961 int first_fragment, 00962 int fragment_width, 00963 int fragment_height); 00964 /* 00965 * This function unpacks all of the DCT coefficient data from the 00966 * bitstream. 00967 */ 00968 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) 00969 { 00970 int i; 00971 int dc_y_table; 00972 int dc_c_table; 00973 int ac_y_table; 00974 int ac_c_table; 00975 int residual_eob_run = 0; 00976 VLC *y_tables[64]; 00977 VLC *c_tables[64]; 00978 00979 s->dct_tokens[0][0] = s->dct_tokens_base; 00980 00981 /* fetch the DC table indexes */ 00982 dc_y_table = get_bits(gb, 4); 00983 dc_c_table = get_bits(gb, 4); 00984 00985 /* unpack the Y plane DC coefficients */ 00986 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0, 00987 0, residual_eob_run); 00988 00989 /* reverse prediction of the Y-plane DC coefficients */ 00990 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]); 00991 00992 /* unpack the C plane DC coefficients */ 00993 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, 00994 1, residual_eob_run); 00995 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, 00996 2, residual_eob_run); 00997 00998 /* reverse prediction of the C-plane DC coefficients */ 00999 if (!(s->avctx->flags & CODEC_FLAG_GRAY)) 01000 { 01001 reverse_dc_prediction(s, s->fragment_start[1], 01002 s->fragment_width[1], s->fragment_height[1]); 01003 reverse_dc_prediction(s, s->fragment_start[2], 01004 s->fragment_width[1], s->fragment_height[1]); 01005 } 01006 01007 /* fetch the AC table indexes */ 01008 ac_y_table = get_bits(gb, 4); 01009 ac_c_table = get_bits(gb, 4); 01010 01011 /* build tables of AC VLC tables */ 01012 for (i = 1; i <= 5; i++) { 01013 y_tables[i] = &s->ac_vlc_1[ac_y_table]; 01014 c_tables[i] = &s->ac_vlc_1[ac_c_table]; 01015 } 01016 for (i = 6; i <= 14; i++) { 01017 y_tables[i] = &s->ac_vlc_2[ac_y_table]; 01018 c_tables[i] = &s->ac_vlc_2[ac_c_table]; 01019 } 01020 for (i = 15; i <= 27; i++) { 01021 y_tables[i] = &s->ac_vlc_3[ac_y_table]; 01022 c_tables[i] = &s->ac_vlc_3[ac_c_table]; 01023 } 01024 for (i = 28; i <= 63; i++) { 01025 y_tables[i] = &s->ac_vlc_4[ac_y_table]; 01026 c_tables[i] = &s->ac_vlc_4[ac_c_table]; 01027 } 01028 01029 /* decode all AC coefficents */ 01030 for (i = 1; i <= 63; i++) { 01031 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i, 01032 0, residual_eob_run); 01033 01034 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, 01035 1, residual_eob_run); 01036 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, 01037 2, residual_eob_run); 01038 } 01039 01040 return 0; 01041 } 01042 01043 /* 01044 * This function reverses the DC prediction for each coded fragment in 01045 * the frame. Much of this function is adapted directly from the original 01046 * VP3 source code. 01047 */ 01048 #define COMPATIBLE_FRAME(x) \ 01049 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type) 01050 #define DC_COEFF(u) s->all_fragments[u].dc 01051 01052 static void reverse_dc_prediction(Vp3DecodeContext *s, 01053 int first_fragment, 01054 int fragment_width, 01055 int fragment_height) 01056 { 01057 01058 #define PUL 8 01059 #define PU 4 01060 #define PUR 2 01061 #define PL 1 01062 01063 int x, y; 01064 int i = first_fragment; 01065 01066 int predicted_dc; 01067 01068 /* DC values for the left, up-left, up, and up-right fragments */ 01069 int vl, vul, vu, vur; 01070 01071 /* indexes for the left, up-left, up, and up-right fragments */ 01072 int l, ul, u, ur; 01073 01074 /* 01075 * The 6 fields mean: 01076 * 0: up-left multiplier 01077 * 1: up multiplier 01078 * 2: up-right multiplier 01079 * 3: left multiplier 01080 */ 01081 static const int predictor_transform[16][4] = { 01082 { 0, 0, 0, 0}, 01083 { 0, 0, 0,128}, // PL 01084 { 0, 0,128, 0}, // PUR 01085 { 0, 0, 53, 75}, // PUR|PL 01086 { 0,128, 0, 0}, // PU 01087 { 0, 64, 0, 64}, // PU|PL 01088 { 0,128, 0, 0}, // PU|PUR 01089 { 0, 0, 53, 75}, // PU|PUR|PL 01090 {128, 0, 0, 0}, // PUL 01091 { 0, 0, 0,128}, // PUL|PL 01092 { 64, 0, 64, 0}, // PUL|PUR 01093 { 0, 0, 53, 75}, // PUL|PUR|PL 01094 { 0,128, 0, 0}, // PUL|PU 01095 {-104,116, 0,116}, // PUL|PU|PL 01096 { 24, 80, 24, 0}, // PUL|PU|PUR 01097 {-104,116, 0,116} // PUL|PU|PUR|PL 01098 }; 01099 01100 /* This table shows which types of blocks can use other blocks for 01101 * prediction. For example, INTRA is the only mode in this table to 01102 * have a frame number of 0. That means INTRA blocks can only predict 01103 * from other INTRA blocks. There are 2 golden frame coding types; 01104 * blocks encoding in these modes can only predict from other blocks 01105 * that were encoded with these 1 of these 2 modes. */ 01106 static const unsigned char compatible_frame[9] = { 01107 1, /* MODE_INTER_NO_MV */ 01108 0, /* MODE_INTRA */ 01109 1, /* MODE_INTER_PLUS_MV */ 01110 1, /* MODE_INTER_LAST_MV */ 01111 1, /* MODE_INTER_PRIOR_MV */ 01112 2, /* MODE_USING_GOLDEN */ 01113 2, /* MODE_GOLDEN_MV */ 01114 1, /* MODE_INTER_FOUR_MV */ 01115 3 /* MODE_COPY */ 01116 }; 01117 int current_frame_type; 01118 01119 /* there is a last DC predictor for each of the 3 frame types */ 01120 short last_dc[3]; 01121 01122 int transform = 0; 01123 01124 vul = vu = vur = vl = 0; 01125 last_dc[0] = last_dc[1] = last_dc[2] = 0; 01126 01127 /* for each fragment row... */ 01128 for (y = 0; y < fragment_height; y++) { 01129 01130 /* for each fragment in a row... */ 01131 for (x = 0; x < fragment_width; x++, i++) { 01132 01133 /* reverse prediction if this block was coded */ 01134 if (s->all_fragments[i].coding_method != MODE_COPY) { 01135 01136 current_frame_type = 01137 compatible_frame[s->all_fragments[i].coding_method]; 01138 01139 transform= 0; 01140 if(x){ 01141 l= i-1; 01142 vl = DC_COEFF(l); 01143 if(COMPATIBLE_FRAME(l)) 01144 transform |= PL; 01145 } 01146 if(y){ 01147 u= i-fragment_width; 01148 vu = DC_COEFF(u); 01149 if(COMPATIBLE_FRAME(u)) 01150 transform |= PU; 01151 if(x){ 01152 ul= i-fragment_width-1; 01153 vul = DC_COEFF(ul); 01154 if(COMPATIBLE_FRAME(ul)) 01155 transform |= PUL; 01156 } 01157 if(x + 1 < fragment_width){ 01158 ur= i-fragment_width+1; 01159 vur = DC_COEFF(ur); 01160 if(COMPATIBLE_FRAME(ur)) 01161 transform |= PUR; 01162 } 01163 } 01164 01165 if (transform == 0) { 01166 01167 /* if there were no fragments to predict from, use last 01168 * DC saved */ 01169 predicted_dc = last_dc[current_frame_type]; 01170 } else { 01171 01172 /* apply the appropriate predictor transform */ 01173 predicted_dc = 01174 (predictor_transform[transform][0] * vul) + 01175 (predictor_transform[transform][1] * vu) + 01176 (predictor_transform[transform][2] * vur) + 01177 (predictor_transform[transform][3] * vl); 01178 01179 predicted_dc /= 128; 01180 01181 /* check for outranging on the [ul u l] and 01182 * [ul u ur l] predictors */ 01183 if ((transform == 15) || (transform == 13)) { 01184 if (FFABS(predicted_dc - vu) > 128) 01185 predicted_dc = vu; 01186 else if (FFABS(predicted_dc - vl) > 128) 01187 predicted_dc = vl; 01188 else if (FFABS(predicted_dc - vul) > 128) 01189 predicted_dc = vul; 01190 } 01191 } 01192 01193 /* at long last, apply the predictor */ 01194 DC_COEFF(i) += predicted_dc; 01195 /* save the DC */ 01196 last_dc[current_frame_type] = DC_COEFF(i); 01197 } 01198 } 01199 } 01200 } 01201 01202 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend) 01203 { 01204 int x, y; 01205 int *bounding_values= s->bounding_values_array+127; 01206 01207 int width = s->fragment_width[!!plane]; 01208 int height = s->fragment_height[!!plane]; 01209 int fragment = s->fragment_start [plane] + ystart * width; 01210 int stride = s->current_frame.linesize[plane]; 01211 uint8_t *plane_data = s->current_frame.data [plane]; 01212 if (!s->flipped_image) stride = -stride; 01213 plane_data += s->data_offset[plane] + 8*ystart*stride; 01214 01215 for (y = ystart; y < yend; y++) { 01216 01217 for (x = 0; x < width; x++) { 01218 /* This code basically just deblocks on the edges of coded blocks. 01219 * However, it has to be much more complicated because of the 01220 * braindamaged deblock ordering used in VP3/Theora. Order matters 01221 * because some pixels get filtered twice. */ 01222 if( s->all_fragments[fragment].coding_method != MODE_COPY ) 01223 { 01224 /* do not perform left edge filter for left columns frags */ 01225 if (x > 0) { 01226 s->dsp.vp3_h_loop_filter( 01227 plane_data + 8*x, 01228 stride, bounding_values); 01229 } 01230 01231 /* do not perform top edge filter for top row fragments */ 01232 if (y > 0) { 01233 s->dsp.vp3_v_loop_filter( 01234 plane_data + 8*x, 01235 stride, bounding_values); 01236 } 01237 01238 /* do not perform right edge filter for right column 01239 * fragments or if right fragment neighbor is also coded 01240 * in this frame (it will be filtered in next iteration) */ 01241 if ((x < width - 1) && 01242 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) { 01243 s->dsp.vp3_h_loop_filter( 01244 plane_data + 8*x + 8, 01245 stride, bounding_values); 01246 } 01247 01248 /* do not perform bottom edge filter for bottom row 01249 * fragments or if bottom fragment neighbor is also coded 01250 * in this frame (it will be filtered in the next row) */ 01251 if ((y < height - 1) && 01252 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) { 01253 s->dsp.vp3_v_loop_filter( 01254 plane_data + 8*x + 8*stride, 01255 stride, bounding_values); 01256 } 01257 } 01258 01259 fragment++; 01260 } 01261 plane_data += 8*stride; 01262 } 01263 } 01264 01269 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag, 01270 int plane, int inter, DCTELEM block[64]) 01271 { 01272 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane]; 01273 uint8_t *perm = s->scantable.permutated; 01274 int i = 0; 01275 01276 do { 01277 int token = *s->dct_tokens[plane][i]; 01278 switch (token & 3) { 01279 case 0: // EOB 01280 if (--token < 4) // 0-3 are token types, so the EOB run must now be 0 01281 s->dct_tokens[plane][i]++; 01282 else 01283 *s->dct_tokens[plane][i] = token & ~3; 01284 goto end; 01285 case 1: // zero run 01286 s->dct_tokens[plane][i]++; 01287 i += (token >> 2) & 0x7f; 01288 if (i > 63) { 01289 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n"); 01290 return i; 01291 } 01292 block[perm[i]] = (token >> 9) * dequantizer[perm[i]]; 01293 i++; 01294 break; 01295 case 2: // coeff 01296 block[perm[i]] = (token >> 2) * dequantizer[perm[i]]; 01297 s->dct_tokens[plane][i++]++; 01298 break; 01299 default: // shouldn't happen 01300 return i; 01301 } 01302 } while (i < 64); 01303 end: 01304 // the actual DC+prediction is in the fragment structure 01305 block[0] = frag->dc * s->qmat[0][inter][plane][0]; 01306 return i; 01307 } 01308 01312 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y) 01313 { 01314 int h, cy; 01315 int offset[4]; 01316 01317 if(s->avctx->draw_horiz_band==NULL) 01318 return; 01319 01320 h= y - s->last_slice_end; 01321 y -= h; 01322 01323 if (!s->flipped_image) { 01324 if (y == 0) 01325 h -= s->height - s->avctx->height; // account for non-mod16 01326 y = s->height - y - h; 01327 } 01328 01329 cy = y >> 1; 01330 offset[0] = s->current_frame.linesize[0]*y; 01331 offset[1] = s->current_frame.linesize[1]*cy; 01332 offset[2] = s->current_frame.linesize[2]*cy; 01333 offset[3] = 0; 01334 01335 emms_c(); 01336 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h); 01337 s->last_slice_end= y + h; 01338 } 01339 01340 /* 01341 * Perform the final rendering for a particular slice of data. 01342 * The slice number ranges from 0..(c_superblock_height - 1). 01343 */ 01344 static void render_slice(Vp3DecodeContext *s, int slice) 01345 { 01346 int x, y, i, j; 01347 LOCAL_ALIGNED_16(DCTELEM, block, [64]); 01348 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef; 01349 int motion_halfpel_index; 01350 uint8_t *motion_source; 01351 int plane, first_pixel; 01352 01353 if (slice >= s->c_superblock_height) 01354 return; 01355 01356 for (plane = 0; plane < 3; plane++) { 01357 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane]; 01358 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane]; 01359 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane]; 01360 int stride = s->current_frame.linesize[plane]; 01361 int plane_width = s->width >> (plane && s->chroma_x_shift); 01362 int plane_height = s->height >> (plane && s->chroma_y_shift); 01363 int8_t (*motion_val)[2] = s->motion_val[!!plane]; 01364 01365 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift); 01366 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift); 01367 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width; 01368 01369 int fragment_width = s->fragment_width[!!plane]; 01370 int fragment_height = s->fragment_height[!!plane]; 01371 int fragment_start = s->fragment_start[plane]; 01372 01373 if (!s->flipped_image) stride = -stride; 01374 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY)) 01375 continue; 01376 01377 01378 if(FFABS(stride) > 2048) 01379 return; //various tables are fixed size 01380 01381 /* for each superblock row in the slice (both of them)... */ 01382 for (; sb_y < slice_height; sb_y++) { 01383 01384 /* for each superblock in a row... */ 01385 for (sb_x = 0; sb_x < slice_width; sb_x++) { 01386 01387 /* for each block in a superblock... */ 01388 for (j = 0; j < 16; j++) { 01389 x = 4*sb_x + hilbert_offset[j][0]; 01390 y = 4*sb_y + hilbert_offset[j][1]; 01391 01392 i = fragment_start + y*fragment_width + x; 01393 01394 // bounds check 01395 if (x >= fragment_width || y >= fragment_height) 01396 continue; 01397 01398 first_pixel = 8*y*stride + 8*x; 01399 01400 /* transform if this block was coded */ 01401 if (s->all_fragments[i].coding_method != MODE_COPY) { 01402 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) || 01403 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) 01404 motion_source= golden_plane; 01405 else 01406 motion_source= last_plane; 01407 01408 motion_source += first_pixel; 01409 motion_halfpel_index = 0; 01410 01411 /* sort out the motion vector if this fragment is coded 01412 * using a motion vector method */ 01413 if ((s->all_fragments[i].coding_method > MODE_INTRA) && 01414 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) { 01415 int src_x, src_y; 01416 motion_x = motion_val[y*fragment_width + x][0]; 01417 motion_y = motion_val[y*fragment_width + x][1]; 01418 01419 src_x= (motion_x>>1) + 8*x; 01420 src_y= (motion_y>>1) + 8*y; 01421 01422 motion_halfpel_index = motion_x & 0x01; 01423 motion_source += (motion_x >> 1); 01424 01425 motion_halfpel_index |= (motion_y & 0x01) << 1; 01426 motion_source += ((motion_y >> 1) * stride); 01427 01428 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){ 01429 uint8_t *temp= s->edge_emu_buffer; 01430 if(stride<0) temp -= 9*stride; 01431 else temp += 9*stride; 01432 01433 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height); 01434 motion_source= temp; 01435 } 01436 } 01437 01438 01439 /* first, take care of copying a block from either the 01440 * previous or the golden frame */ 01441 if (s->all_fragments[i].coding_method != MODE_INTRA) { 01442 /* Note, it is possible to implement all MC cases with 01443 put_no_rnd_pixels_l2 which would look more like the 01444 VP3 source but this would be slower as 01445 put_no_rnd_pixels_tab is better optimzed */ 01446 if(motion_halfpel_index != 3){ 01447 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index]( 01448 output_plane + first_pixel, 01449 motion_source, stride, 8); 01450 }else{ 01451 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1 01452 s->dsp.put_no_rnd_pixels_l2[1]( 01453 output_plane + first_pixel, 01454 motion_source - d, 01455 motion_source + stride + 1 + d, 01456 stride, 8); 01457 } 01458 } 01459 01460 s->dsp.clear_block(block); 01461 01462 /* invert DCT and place (or add) in final output */ 01463 01464 if (s->all_fragments[i].coding_method == MODE_INTRA) { 01465 int index; 01466 index = vp3_dequant(s, s->all_fragments + i, plane, 0, block); 01467 if (index > 63) 01468 continue; 01469 if(s->avctx->idct_algo!=FF_IDCT_VP3) 01470 block[0] += 128<<3; 01471 s->dsp.idct_put( 01472 output_plane + first_pixel, 01473 stride, 01474 block); 01475 } else { 01476 int index = vp3_dequant(s, s->all_fragments + i, plane, 1, block); 01477 if (index > 63) 01478 continue; 01479 if (index > 0) { 01480 s->dsp.idct_add( 01481 output_plane + first_pixel, 01482 stride, 01483 block); 01484 } else { 01485 s->dsp.vp3_idct_dc_add(output_plane + first_pixel, stride, block); 01486 } 01487 } 01488 } else { 01489 01490 /* copy directly from the previous frame */ 01491 s->dsp.put_pixels_tab[1][0]( 01492 output_plane + first_pixel, 01493 last_plane + first_pixel, 01494 stride, 8); 01495 01496 } 01497 } 01498 } 01499 01500 // Filter up to the last row in the superblock row 01501 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1)); 01502 } 01503 } 01504 01505 /* this looks like a good place for slice dispatch... */ 01506 /* algorithm: 01507 * if (slice == s->macroblock_height - 1) 01508 * dispatch (both last slice & 2nd-to-last slice); 01509 * else if (slice > 0) 01510 * dispatch (slice - 1); 01511 */ 01512 01513 vp3_draw_horiz_band(s, FFMIN(64*slice + 64-16, s->height-16)); 01514 } 01515 01516 /* 01517 * This is the ffmpeg/libavcodec API init function. 01518 */ 01519 static av_cold int vp3_decode_init(AVCodecContext *avctx) 01520 { 01521 Vp3DecodeContext *s = avctx->priv_data; 01522 int i, inter, plane; 01523 int c_width; 01524 int c_height; 01525 int y_fragment_count, c_fragment_count; 01526 01527 if (avctx->codec_tag == MKTAG('V','P','3','0')) 01528 s->version = 0; 01529 else 01530 s->version = 1; 01531 01532 s->avctx = avctx; 01533 s->width = FFALIGN(avctx->width, 16); 01534 s->height = FFALIGN(avctx->height, 16); 01535 if (avctx->pix_fmt == PIX_FMT_NONE) 01536 avctx->pix_fmt = PIX_FMT_YUV420P; 01537 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; 01538 if(avctx->idct_algo==FF_IDCT_AUTO) 01539 avctx->idct_algo=FF_IDCT_VP3; 01540 dsputil_init(&s->dsp, avctx); 01541 01542 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); 01543 01544 /* initialize to an impossible value which will force a recalculation 01545 * in the first frame decode */ 01546 for (i = 0; i < 3; i++) 01547 s->qps[i] = -1; 01548 01549 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift); 01550 01551 s->y_superblock_width = (s->width + 31) / 32; 01552 s->y_superblock_height = (s->height + 31) / 32; 01553 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height; 01554 01555 /* work out the dimensions for the C planes */ 01556 c_width = s->width >> s->chroma_x_shift; 01557 c_height = s->height >> s->chroma_y_shift; 01558 s->c_superblock_width = (c_width + 31) / 32; 01559 s->c_superblock_height = (c_height + 31) / 32; 01560 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height; 01561 01562 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2); 01563 s->u_superblock_start = s->y_superblock_count; 01564 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count; 01565 s->superblock_coding = av_malloc(s->superblock_count); 01566 01567 s->macroblock_width = (s->width + 15) / 16; 01568 s->macroblock_height = (s->height + 15) / 16; 01569 s->macroblock_count = s->macroblock_width * s->macroblock_height; 01570 01571 s->fragment_width[0] = s->width / FRAGMENT_PIXELS; 01572 s->fragment_height[0] = s->height / FRAGMENT_PIXELS; 01573 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift; 01574 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift; 01575 01576 /* fragment count covers all 8x8 blocks for all 3 planes */ 01577 y_fragment_count = s->fragment_width[0] * s->fragment_height[0]; 01578 c_fragment_count = s->fragment_width[1] * s->fragment_height[1]; 01579 s->fragment_count = y_fragment_count + 2*c_fragment_count; 01580 s->fragment_start[1] = y_fragment_count; 01581 s->fragment_start[2] = y_fragment_count + c_fragment_count; 01582 01583 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment)); 01584 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int)); 01585 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base)); 01586 s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0])); 01587 s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1])); 01588 01589 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base || 01590 !s->coded_fragment_list[0] || !s->motion_val[0] || !s->motion_val[1]) { 01591 vp3_decode_end(avctx); 01592 return -1; 01593 } 01594 01595 if (!s->theora_tables) 01596 { 01597 for (i = 0; i < 64; i++) { 01598 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; 01599 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i]; 01600 s->base_matrix[0][i] = vp31_intra_y_dequant[i]; 01601 s->base_matrix[1][i] = vp31_intra_c_dequant[i]; 01602 s->base_matrix[2][i] = vp31_inter_dequant[i]; 01603 s->filter_limit_values[i] = vp31_filter_limit_values[i]; 01604 } 01605 01606 for(inter=0; inter<2; inter++){ 01607 for(plane=0; plane<3; plane++){ 01608 s->qr_count[inter][plane]= 1; 01609 s->qr_size [inter][plane][0]= 63; 01610 s->qr_base [inter][plane][0]= 01611 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter; 01612 } 01613 } 01614 01615 /* init VLC tables */ 01616 for (i = 0; i < 16; i++) { 01617 01618 /* DC histograms */ 01619 init_vlc(&s->dc_vlc[i], 11, 32, 01620 &dc_bias[i][0][1], 4, 2, 01621 &dc_bias[i][0][0], 4, 2, 0); 01622 01623 /* group 1 AC histograms */ 01624 init_vlc(&s->ac_vlc_1[i], 11, 32, 01625 &ac_bias_0[i][0][1], 4, 2, 01626 &ac_bias_0[i][0][0], 4, 2, 0); 01627 01628 /* group 2 AC histograms */ 01629 init_vlc(&s->ac_vlc_2[i], 11, 32, 01630 &ac_bias_1[i][0][1], 4, 2, 01631 &ac_bias_1[i][0][0], 4, 2, 0); 01632 01633 /* group 3 AC histograms */ 01634 init_vlc(&s->ac_vlc_3[i], 11, 32, 01635 &ac_bias_2[i][0][1], 4, 2, 01636 &ac_bias_2[i][0][0], 4, 2, 0); 01637 01638 /* group 4 AC histograms */ 01639 init_vlc(&s->ac_vlc_4[i], 11, 32, 01640 &ac_bias_3[i][0][1], 4, 2, 01641 &ac_bias_3[i][0][0], 4, 2, 0); 01642 } 01643 } else { 01644 01645 for (i = 0; i < 16; i++) { 01646 /* DC histograms */ 01647 if (init_vlc(&s->dc_vlc[i], 11, 32, 01648 &s->huffman_table[i][0][1], 8, 4, 01649 &s->huffman_table[i][0][0], 8, 4, 0) < 0) 01650 goto vlc_fail; 01651 01652 /* group 1 AC histograms */ 01653 if (init_vlc(&s->ac_vlc_1[i], 11, 32, 01654 &s->huffman_table[i+16][0][1], 8, 4, 01655 &s->huffman_table[i+16][0][0], 8, 4, 0) < 0) 01656 goto vlc_fail; 01657 01658 /* group 2 AC histograms */ 01659 if (init_vlc(&s->ac_vlc_2[i], 11, 32, 01660 &s->huffman_table[i+16*2][0][1], 8, 4, 01661 &s->huffman_table[i+16*2][0][0], 8, 4, 0) < 0) 01662 goto vlc_fail; 01663 01664 /* group 3 AC histograms */ 01665 if (init_vlc(&s->ac_vlc_3[i], 11, 32, 01666 &s->huffman_table[i+16*3][0][1], 8, 4, 01667 &s->huffman_table[i+16*3][0][0], 8, 4, 0) < 0) 01668 goto vlc_fail; 01669 01670 /* group 4 AC histograms */ 01671 if (init_vlc(&s->ac_vlc_4[i], 11, 32, 01672 &s->huffman_table[i+16*4][0][1], 8, 4, 01673 &s->huffman_table[i+16*4][0][0], 8, 4, 0) < 0) 01674 goto vlc_fail; 01675 } 01676 } 01677 01678 init_vlc(&s->superblock_run_length_vlc, 6, 34, 01679 &superblock_run_length_vlc_table[0][1], 4, 2, 01680 &superblock_run_length_vlc_table[0][0], 4, 2, 0); 01681 01682 init_vlc(&s->fragment_run_length_vlc, 5, 30, 01683 &fragment_run_length_vlc_table[0][1], 4, 2, 01684 &fragment_run_length_vlc_table[0][0], 4, 2, 0); 01685 01686 init_vlc(&s->mode_code_vlc, 3, 8, 01687 &mode_code_vlc_table[0][1], 2, 1, 01688 &mode_code_vlc_table[0][0], 2, 1, 0); 01689 01690 init_vlc(&s->motion_vector_vlc, 6, 63, 01691 &motion_vector_vlc_table[0][1], 2, 1, 01692 &motion_vector_vlc_table[0][0], 2, 1, 0); 01693 01694 /* work out the block mapping tables */ 01695 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int)); 01696 s->macroblock_coding = av_malloc(s->macroblock_count + 1); 01697 if (!s->superblock_fragments || !s->macroblock_coding) { 01698 vp3_decode_end(avctx); 01699 return -1; 01700 } 01701 init_block_mapping(s); 01702 01703 for (i = 0; i < 3; i++) { 01704 s->current_frame.data[i] = NULL; 01705 s->last_frame.data[i] = NULL; 01706 s->golden_frame.data[i] = NULL; 01707 } 01708 01709 return 0; 01710 01711 vlc_fail: 01712 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n"); 01713 return -1; 01714 } 01715 01716 /* 01717 * This is the ffmpeg/libavcodec API frame decode function. 01718 */ 01719 static int vp3_decode_frame(AVCodecContext *avctx, 01720 void *data, int *data_size, 01721 AVPacket *avpkt) 01722 { 01723 const uint8_t *buf = avpkt->data; 01724 int buf_size = avpkt->size; 01725 Vp3DecodeContext *s = avctx->priv_data; 01726 GetBitContext gb; 01727 static int counter = 0; 01728 int i; 01729 01730 init_get_bits(&gb, buf, buf_size * 8); 01731 01732 if (s->theora && get_bits1(&gb)) 01733 { 01734 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n"); 01735 return -1; 01736 } 01737 01738 s->keyframe = !get_bits1(&gb); 01739 if (!s->theora) 01740 skip_bits(&gb, 1); 01741 for (i = 0; i < 3; i++) 01742 s->last_qps[i] = s->qps[i]; 01743 01744 s->nqps=0; 01745 do{ 01746 s->qps[s->nqps++]= get_bits(&gb, 6); 01747 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb)); 01748 for (i = s->nqps; i < 3; i++) 01749 s->qps[i] = -1; 01750 01751 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 01752 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n", 01753 s->keyframe?"key":"", counter, s->qps[0]); 01754 counter++; 01755 01756 if (s->qps[0] != s->last_qps[0]) 01757 init_loop_filter(s); 01758 01759 for (i = 0; i < s->nqps; i++) 01760 // reinit all dequantizers if the first one changed, because 01761 // the DC of the first quantizer must be used for all matrices 01762 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0]) 01763 init_dequantizer(s, i); 01764 01765 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) 01766 return buf_size; 01767 01768 s->current_frame.reference = 3; 01769 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE; 01770 if (avctx->get_buffer(avctx, &s->current_frame) < 0) { 01771 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 01772 goto error; 01773 } 01774 01775 if (s->keyframe) { 01776 if (!s->theora) 01777 { 01778 skip_bits(&gb, 4); /* width code */ 01779 skip_bits(&gb, 4); /* height code */ 01780 if (s->version) 01781 { 01782 s->version = get_bits(&gb, 5); 01783 if (counter == 1) 01784 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version); 01785 } 01786 } 01787 if (s->version || s->theora) 01788 { 01789 if (get_bits1(&gb)) 01790 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n"); 01791 skip_bits(&gb, 2); /* reserved? */ 01792 } 01793 } else { 01794 if (!s->golden_frame.data[0]) { 01795 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n"); 01796 01797 s->golden_frame.reference = 3; 01798 s->golden_frame.pict_type = FF_I_TYPE; 01799 if (avctx->get_buffer(avctx, &s->golden_frame) < 0) { 01800 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 01801 goto error; 01802 } 01803 s->last_frame = s->golden_frame; 01804 s->last_frame.type = FF_BUFFER_TYPE_COPY; 01805 } 01806 } 01807 01808 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame 01809 s->current_frame.qstride= 0; 01810 01811 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment)); 01812 01813 if (unpack_superblocks(s, &gb)){ 01814 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n"); 01815 goto error; 01816 } 01817 if (unpack_modes(s, &gb)){ 01818 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n"); 01819 goto error; 01820 } 01821 if (unpack_vectors(s, &gb)){ 01822 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); 01823 goto error; 01824 } 01825 if (unpack_block_qpis(s, &gb)){ 01826 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); 01827 goto error; 01828 } 01829 if (unpack_dct_coeffs(s, &gb)){ 01830 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); 01831 goto error; 01832 } 01833 01834 for (i = 0; i < 3; i++) { 01835 int height = s->height >> (i && s->chroma_y_shift); 01836 if (s->flipped_image) 01837 s->data_offset[i] = 0; 01838 else 01839 s->data_offset[i] = (height-1) * s->current_frame.linesize[i]; 01840 } 01841 01842 s->last_slice_end = 0; 01843 for (i = 0; i < s->c_superblock_height; i++) 01844 render_slice(s, i); 01845 01846 // filter the last row 01847 for (i = 0; i < 3; i++) { 01848 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1; 01849 apply_loop_filter(s, i, row, row+1); 01850 } 01851 vp3_draw_horiz_band(s, s->height); 01852 01853 *data_size=sizeof(AVFrame); 01854 *(AVFrame*)data= s->current_frame; 01855 01856 /* release the last frame, if it is allocated and if it is not the 01857 * golden frame */ 01858 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY) 01859 avctx->release_buffer(avctx, &s->last_frame); 01860 01861 /* shuffle frames (last = current) */ 01862 s->last_frame= s->current_frame; 01863 01864 if (s->keyframe) { 01865 if (s->golden_frame.data[0]) 01866 avctx->release_buffer(avctx, &s->golden_frame); 01867 s->golden_frame = s->current_frame; 01868 s->last_frame.type = FF_BUFFER_TYPE_COPY; 01869 } 01870 01871 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */ 01872 01873 return buf_size; 01874 01875 error: 01876 if (s->current_frame.data[0]) 01877 avctx->release_buffer(avctx, &s->current_frame); 01878 return -1; 01879 } 01880 01881 /* 01882 * This is the ffmpeg/libavcodec API module cleanup function. 01883 */ 01884 static av_cold int vp3_decode_end(AVCodecContext *avctx) 01885 { 01886 Vp3DecodeContext *s = avctx->priv_data; 01887 int i; 01888 01889 av_free(s->superblock_coding); 01890 av_free(s->all_fragments); 01891 av_free(s->coded_fragment_list[0]); 01892 av_free(s->dct_tokens_base); 01893 av_free(s->superblock_fragments); 01894 av_free(s->macroblock_coding); 01895 av_free(s->motion_val[0]); 01896 av_free(s->motion_val[1]); 01897 01898 for (i = 0; i < 16; i++) { 01899 free_vlc(&s->dc_vlc[i]); 01900 free_vlc(&s->ac_vlc_1[i]); 01901 free_vlc(&s->ac_vlc_2[i]); 01902 free_vlc(&s->ac_vlc_3[i]); 01903 free_vlc(&s->ac_vlc_4[i]); 01904 } 01905 01906 free_vlc(&s->superblock_run_length_vlc); 01907 free_vlc(&s->fragment_run_length_vlc); 01908 free_vlc(&s->mode_code_vlc); 01909 free_vlc(&s->motion_vector_vlc); 01910 01911 /* release all frames */ 01912 if (s->golden_frame.data[0]) 01913 avctx->release_buffer(avctx, &s->golden_frame); 01914 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY) 01915 avctx->release_buffer(avctx, &s->last_frame); 01916 /* no need to release the current_frame since it will always be pointing 01917 * to the same frame as either the golden or last frame */ 01918 01919 return 0; 01920 } 01921 01922 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb) 01923 { 01924 Vp3DecodeContext *s = avctx->priv_data; 01925 01926 if (get_bits1(gb)) { 01927 int token; 01928 if (s->entries >= 32) { /* overflow */ 01929 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); 01930 return -1; 01931 } 01932 token = get_bits(gb, 5); 01933 //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size); 01934 s->huffman_table[s->hti][token][0] = s->hbits; 01935 s->huffman_table[s->hti][token][1] = s->huff_code_size; 01936 s->entries++; 01937 } 01938 else { 01939 if (s->huff_code_size >= 32) {/* overflow */ 01940 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); 01941 return -1; 01942 } 01943 s->huff_code_size++; 01944 s->hbits <<= 1; 01945 if (read_huffman_tree(avctx, gb)) 01946 return -1; 01947 s->hbits |= 1; 01948 if (read_huffman_tree(avctx, gb)) 01949 return -1; 01950 s->hbits >>= 1; 01951 s->huff_code_size--; 01952 } 01953 return 0; 01954 } 01955 01956 #if CONFIG_THEORA_DECODER 01957 static const enum PixelFormat theora_pix_fmts[4] = { 01958 PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P 01959 }; 01960 01961 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) 01962 { 01963 Vp3DecodeContext *s = avctx->priv_data; 01964 int visible_width, visible_height, colorspace; 01965 int offset_x = 0, offset_y = 0; 01966 AVRational fps; 01967 01968 s->theora = get_bits_long(gb, 24); 01969 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora); 01970 01971 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */ 01972 /* but previous versions have the image flipped relative to vp3 */ 01973 if (s->theora < 0x030200) 01974 { 01975 s->flipped_image = 1; 01976 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n"); 01977 } 01978 01979 visible_width = s->width = get_bits(gb, 16) << 4; 01980 visible_height = s->height = get_bits(gb, 16) << 4; 01981 01982 if(avcodec_check_dimensions(avctx, s->width, s->height)){ 01983 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height); 01984 s->width= s->height= 0; 01985 return -1; 01986 } 01987 01988 if (s->theora >= 0x030200) { 01989 visible_width = get_bits_long(gb, 24); 01990 visible_height = get_bits_long(gb, 24); 01991 01992 offset_x = get_bits(gb, 8); /* offset x */ 01993 offset_y = get_bits(gb, 8); /* offset y, from bottom */ 01994 } 01995 01996 fps.num = get_bits_long(gb, 32); 01997 fps.den = get_bits_long(gb, 32); 01998 if (fps.num && fps.den) { 01999 av_reduce(&avctx->time_base.num, &avctx->time_base.den, 02000 fps.den, fps.num, 1<<30); 02001 } 02002 02003 avctx->sample_aspect_ratio.num = get_bits_long(gb, 24); 02004 avctx->sample_aspect_ratio.den = get_bits_long(gb, 24); 02005 02006 if (s->theora < 0x030200) 02007 skip_bits(gb, 5); /* keyframe frequency force */ 02008 colorspace = get_bits(gb, 8); 02009 skip_bits(gb, 24); /* bitrate */ 02010 02011 skip_bits(gb, 6); /* quality hint */ 02012 02013 if (s->theora >= 0x030200) 02014 { 02015 skip_bits(gb, 5); /* keyframe frequency force */ 02016 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)]; 02017 skip_bits(gb, 3); /* reserved */ 02018 } 02019 02020 // align_get_bits(gb); 02021 02022 if ( visible_width <= s->width && visible_width > s->width-16 02023 && visible_height <= s->height && visible_height > s->height-16 02024 && !offset_x && (offset_y == s->height - visible_height)) 02025 avcodec_set_dimensions(avctx, visible_width, visible_height); 02026 else 02027 avcodec_set_dimensions(avctx, s->width, s->height); 02028 02029 if (colorspace == 1) { 02030 avctx->color_primaries = AVCOL_PRI_BT470M; 02031 } else if (colorspace == 2) { 02032 avctx->color_primaries = AVCOL_PRI_BT470BG; 02033 } 02034 if (colorspace == 1 || colorspace == 2) { 02035 avctx->colorspace = AVCOL_SPC_BT470BG; 02036 avctx->color_trc = AVCOL_TRC_BT709; 02037 } 02038 02039 return 0; 02040 } 02041 02042 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) 02043 { 02044 Vp3DecodeContext *s = avctx->priv_data; 02045 int i, n, matrices, inter, plane; 02046 02047 if (s->theora >= 0x030200) { 02048 n = get_bits(gb, 3); 02049 /* loop filter limit values table */ 02050 for (i = 0; i < 64; i++) { 02051 s->filter_limit_values[i] = get_bits(gb, n); 02052 if (s->filter_limit_values[i] > 127) { 02053 av_log(avctx, AV_LOG_ERROR, "filter limit value too large (%i > 127), clamping\n", s->filter_limit_values[i]); 02054 s->filter_limit_values[i] = 127; 02055 } 02056 } 02057 } 02058 02059 if (s->theora >= 0x030200) 02060 n = get_bits(gb, 4) + 1; 02061 else 02062 n = 16; 02063 /* quality threshold table */ 02064 for (i = 0; i < 64; i++) 02065 s->coded_ac_scale_factor[i] = get_bits(gb, n); 02066 02067 if (s->theora >= 0x030200) 02068 n = get_bits(gb, 4) + 1; 02069 else 02070 n = 16; 02071 /* dc scale factor table */ 02072 for (i = 0; i < 64; i++) 02073 s->coded_dc_scale_factor[i] = get_bits(gb, n); 02074 02075 if (s->theora >= 0x030200) 02076 matrices = get_bits(gb, 9) + 1; 02077 else 02078 matrices = 3; 02079 02080 if(matrices > 384){ 02081 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n"); 02082 return -1; 02083 } 02084 02085 for(n=0; n<matrices; n++){ 02086 for (i = 0; i < 64; i++) 02087 s->base_matrix[n][i]= get_bits(gb, 8); 02088 } 02089 02090 for (inter = 0; inter <= 1; inter++) { 02091 for (plane = 0; plane <= 2; plane++) { 02092 int newqr= 1; 02093 if (inter || plane > 0) 02094 newqr = get_bits1(gb); 02095 if (!newqr) { 02096 int qtj, plj; 02097 if(inter && get_bits1(gb)){ 02098 qtj = 0; 02099 plj = plane; 02100 }else{ 02101 qtj= (3*inter + plane - 1) / 3; 02102 plj= (plane + 2) % 3; 02103 } 02104 s->qr_count[inter][plane]= s->qr_count[qtj][plj]; 02105 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0])); 02106 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0])); 02107 } else { 02108 int qri= 0; 02109 int qi = 0; 02110 02111 for(;;){ 02112 i= get_bits(gb, av_log2(matrices-1)+1); 02113 if(i>= matrices){ 02114 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n"); 02115 return -1; 02116 } 02117 s->qr_base[inter][plane][qri]= i; 02118 if(qi >= 63) 02119 break; 02120 i = get_bits(gb, av_log2(63-qi)+1) + 1; 02121 s->qr_size[inter][plane][qri++]= i; 02122 qi += i; 02123 } 02124 02125 if (qi > 63) { 02126 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); 02127 return -1; 02128 } 02129 s->qr_count[inter][plane]= qri; 02130 } 02131 } 02132 } 02133 02134 /* Huffman tables */ 02135 for (s->hti = 0; s->hti < 80; s->hti++) { 02136 s->entries = 0; 02137 s->huff_code_size = 1; 02138 if (!get_bits1(gb)) { 02139 s->hbits = 0; 02140 if(read_huffman_tree(avctx, gb)) 02141 return -1; 02142 s->hbits = 1; 02143 if(read_huffman_tree(avctx, gb)) 02144 return -1; 02145 } 02146 } 02147 02148 s->theora_tables = 1; 02149 02150 return 0; 02151 } 02152 02153 static av_cold int theora_decode_init(AVCodecContext *avctx) 02154 { 02155 Vp3DecodeContext *s = avctx->priv_data; 02156 GetBitContext gb; 02157 int ptype; 02158 uint8_t *header_start[3]; 02159 int header_len[3]; 02160 int i; 02161 02162 s->theora = 1; 02163 02164 if (!avctx->extradata_size) 02165 { 02166 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n"); 02167 return -1; 02168 } 02169 02170 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size, 02171 42, header_start, header_len) < 0) { 02172 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n"); 02173 return -1; 02174 } 02175 02176 for(i=0;i<3;i++) { 02177 init_get_bits(&gb, header_start[i], header_len[i] * 8); 02178 02179 ptype = get_bits(&gb, 8); 02180 02181 if (!(ptype & 0x80)) 02182 { 02183 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n"); 02184 // return -1; 02185 } 02186 02187 // FIXME: Check for this as well. 02188 skip_bits_long(&gb, 6*8); /* "theora" */ 02189 02190 switch(ptype) 02191 { 02192 case 0x80: 02193 theora_decode_header(avctx, &gb); 02194 break; 02195 case 0x81: 02196 // FIXME: is this needed? it breaks sometimes 02197 // theora_decode_comments(avctx, gb); 02198 break; 02199 case 0x82: 02200 if (theora_decode_tables(avctx, &gb)) 02201 return -1; 02202 break; 02203 default: 02204 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80); 02205 break; 02206 } 02207 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb)) 02208 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype); 02209 if (s->theora < 0x030200) 02210 break; 02211 } 02212 02213 return vp3_decode_init(avctx); 02214 } 02215 02216 AVCodec theora_decoder = { 02217 "theora", 02218 AVMEDIA_TYPE_VIDEO, 02219 CODEC_ID_THEORA, 02220 sizeof(Vp3DecodeContext), 02221 theora_decode_init, 02222 NULL, 02223 vp3_decode_end, 02224 vp3_decode_frame, 02225 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, 02226 NULL, 02227 .long_name = NULL_IF_CONFIG_SMALL("Theora"), 02228 }; 02229 #endif 02230 02231 AVCodec vp3_decoder = { 02232 "vp3", 02233 AVMEDIA_TYPE_VIDEO, 02234 CODEC_ID_VP3, 02235 sizeof(Vp3DecodeContext), 02236 vp3_decode_init, 02237 NULL, 02238 vp3_decode_end, 02239 vp3_decode_frame, 02240 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, 02241 NULL, 02242 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"), 02243 };