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 ((unsigned) token <= 6U) { 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 if (token >= 0) { 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 } else { 00940 av_log(s->avctx, AV_LOG_ERROR, 00941 "Invalid token %d\n", token); 00942 return -1; 00943 } 00944 } 00945 00946 if (blocks_ended > s->num_coded_frags[plane][coeff_index]) 00947 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n"); 00948 00949 // decrement the number of blocks that have higher coeffecients for each 00950 // EOB run at this level 00951 if (blocks_ended) 00952 for (i = coeff_index+1; i < 64; i++) 00953 s->num_coded_frags[plane][i] -= blocks_ended; 00954 00955 // setup the next buffer 00956 if (plane < 2) 00957 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j; 00958 else if (coeff_index < 63) 00959 s->dct_tokens[0][coeff_index+1] = dct_tokens + j; 00960 00961 return eob_run; 00962 } 00963 00964 static void reverse_dc_prediction(Vp3DecodeContext *s, 00965 int first_fragment, 00966 int fragment_width, 00967 int fragment_height); 00968 /* 00969 * This function unpacks all of the DCT coefficient data from the 00970 * bitstream. 00971 */ 00972 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) 00973 { 00974 int i; 00975 int dc_y_table; 00976 int dc_c_table; 00977 int ac_y_table; 00978 int ac_c_table; 00979 int residual_eob_run = 0; 00980 VLC *y_tables[64]; 00981 VLC *c_tables[64]; 00982 00983 s->dct_tokens[0][0] = s->dct_tokens_base; 00984 00985 /* fetch the DC table indexes */ 00986 dc_y_table = get_bits(gb, 4); 00987 dc_c_table = get_bits(gb, 4); 00988 00989 /* unpack the Y plane DC coefficients */ 00990 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0, 00991 0, residual_eob_run); 00992 if (residual_eob_run < 0) 00993 return residual_eob_run; 00994 00995 /* reverse prediction of the Y-plane DC coefficients */ 00996 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]); 00997 00998 /* unpack the C plane DC coefficients */ 00999 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, 01000 1, residual_eob_run); 01001 if (residual_eob_run < 0) 01002 return residual_eob_run; 01003 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, 01004 2, residual_eob_run); 01005 if (residual_eob_run < 0) 01006 return residual_eob_run; 01007 01008 /* reverse prediction of the C-plane DC coefficients */ 01009 if (!(s->avctx->flags & CODEC_FLAG_GRAY)) 01010 { 01011 reverse_dc_prediction(s, s->fragment_start[1], 01012 s->fragment_width[1], s->fragment_height[1]); 01013 reverse_dc_prediction(s, s->fragment_start[2], 01014 s->fragment_width[1], s->fragment_height[1]); 01015 } 01016 01017 /* fetch the AC table indexes */ 01018 ac_y_table = get_bits(gb, 4); 01019 ac_c_table = get_bits(gb, 4); 01020 01021 /* build tables of AC VLC tables */ 01022 for (i = 1; i <= 5; i++) { 01023 y_tables[i] = &s->ac_vlc_1[ac_y_table]; 01024 c_tables[i] = &s->ac_vlc_1[ac_c_table]; 01025 } 01026 for (i = 6; i <= 14; i++) { 01027 y_tables[i] = &s->ac_vlc_2[ac_y_table]; 01028 c_tables[i] = &s->ac_vlc_2[ac_c_table]; 01029 } 01030 for (i = 15; i <= 27; i++) { 01031 y_tables[i] = &s->ac_vlc_3[ac_y_table]; 01032 c_tables[i] = &s->ac_vlc_3[ac_c_table]; 01033 } 01034 for (i = 28; i <= 63; i++) { 01035 y_tables[i] = &s->ac_vlc_4[ac_y_table]; 01036 c_tables[i] = &s->ac_vlc_4[ac_c_table]; 01037 } 01038 01039 /* decode all AC coefficents */ 01040 for (i = 1; i <= 63; i++) { 01041 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i, 01042 0, residual_eob_run); 01043 if (residual_eob_run < 0) 01044 return residual_eob_run; 01045 01046 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, 01047 1, residual_eob_run); 01048 if (residual_eob_run < 0) 01049 return residual_eob_run; 01050 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, 01051 2, residual_eob_run); 01052 if (residual_eob_run < 0) 01053 return residual_eob_run; 01054 } 01055 01056 return 0; 01057 } 01058 01059 /* 01060 * This function reverses the DC prediction for each coded fragment in 01061 * the frame. Much of this function is adapted directly from the original 01062 * VP3 source code. 01063 */ 01064 #define COMPATIBLE_FRAME(x) \ 01065 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type) 01066 #define DC_COEFF(u) s->all_fragments[u].dc 01067 01068 static void reverse_dc_prediction(Vp3DecodeContext *s, 01069 int first_fragment, 01070 int fragment_width, 01071 int fragment_height) 01072 { 01073 01074 #define PUL 8 01075 #define PU 4 01076 #define PUR 2 01077 #define PL 1 01078 01079 int x, y; 01080 int i = first_fragment; 01081 01082 int predicted_dc; 01083 01084 /* DC values for the left, up-left, up, and up-right fragments */ 01085 int vl, vul, vu, vur; 01086 01087 /* indexes for the left, up-left, up, and up-right fragments */ 01088 int l, ul, u, ur; 01089 01090 /* 01091 * The 6 fields mean: 01092 * 0: up-left multiplier 01093 * 1: up multiplier 01094 * 2: up-right multiplier 01095 * 3: left multiplier 01096 */ 01097 static const int predictor_transform[16][4] = { 01098 { 0, 0, 0, 0}, 01099 { 0, 0, 0,128}, // PL 01100 { 0, 0,128, 0}, // PUR 01101 { 0, 0, 53, 75}, // PUR|PL 01102 { 0,128, 0, 0}, // PU 01103 { 0, 64, 0, 64}, // PU|PL 01104 { 0,128, 0, 0}, // PU|PUR 01105 { 0, 0, 53, 75}, // PU|PUR|PL 01106 {128, 0, 0, 0}, // PUL 01107 { 0, 0, 0,128}, // PUL|PL 01108 { 64, 0, 64, 0}, // PUL|PUR 01109 { 0, 0, 53, 75}, // PUL|PUR|PL 01110 { 0,128, 0, 0}, // PUL|PU 01111 {-104,116, 0,116}, // PUL|PU|PL 01112 { 24, 80, 24, 0}, // PUL|PU|PUR 01113 {-104,116, 0,116} // PUL|PU|PUR|PL 01114 }; 01115 01116 /* This table shows which types of blocks can use other blocks for 01117 * prediction. For example, INTRA is the only mode in this table to 01118 * have a frame number of 0. That means INTRA blocks can only predict 01119 * from other INTRA blocks. There are 2 golden frame coding types; 01120 * blocks encoding in these modes can only predict from other blocks 01121 * that were encoded with these 1 of these 2 modes. */ 01122 static const unsigned char compatible_frame[9] = { 01123 1, /* MODE_INTER_NO_MV */ 01124 0, /* MODE_INTRA */ 01125 1, /* MODE_INTER_PLUS_MV */ 01126 1, /* MODE_INTER_LAST_MV */ 01127 1, /* MODE_INTER_PRIOR_MV */ 01128 2, /* MODE_USING_GOLDEN */ 01129 2, /* MODE_GOLDEN_MV */ 01130 1, /* MODE_INTER_FOUR_MV */ 01131 3 /* MODE_COPY */ 01132 }; 01133 int current_frame_type; 01134 01135 /* there is a last DC predictor for each of the 3 frame types */ 01136 short last_dc[3]; 01137 01138 int transform = 0; 01139 01140 vul = vu = vur = vl = 0; 01141 last_dc[0] = last_dc[1] = last_dc[2] = 0; 01142 01143 /* for each fragment row... */ 01144 for (y = 0; y < fragment_height; y++) { 01145 01146 /* for each fragment in a row... */ 01147 for (x = 0; x < fragment_width; x++, i++) { 01148 01149 /* reverse prediction if this block was coded */ 01150 if (s->all_fragments[i].coding_method != MODE_COPY) { 01151 01152 current_frame_type = 01153 compatible_frame[s->all_fragments[i].coding_method]; 01154 01155 transform= 0; 01156 if(x){ 01157 l= i-1; 01158 vl = DC_COEFF(l); 01159 if(COMPATIBLE_FRAME(l)) 01160 transform |= PL; 01161 } 01162 if(y){ 01163 u= i-fragment_width; 01164 vu = DC_COEFF(u); 01165 if(COMPATIBLE_FRAME(u)) 01166 transform |= PU; 01167 if(x){ 01168 ul= i-fragment_width-1; 01169 vul = DC_COEFF(ul); 01170 if(COMPATIBLE_FRAME(ul)) 01171 transform |= PUL; 01172 } 01173 if(x + 1 < fragment_width){ 01174 ur= i-fragment_width+1; 01175 vur = DC_COEFF(ur); 01176 if(COMPATIBLE_FRAME(ur)) 01177 transform |= PUR; 01178 } 01179 } 01180 01181 if (transform == 0) { 01182 01183 /* if there were no fragments to predict from, use last 01184 * DC saved */ 01185 predicted_dc = last_dc[current_frame_type]; 01186 } else { 01187 01188 /* apply the appropriate predictor transform */ 01189 predicted_dc = 01190 (predictor_transform[transform][0] * vul) + 01191 (predictor_transform[transform][1] * vu) + 01192 (predictor_transform[transform][2] * vur) + 01193 (predictor_transform[transform][3] * vl); 01194 01195 predicted_dc /= 128; 01196 01197 /* check for outranging on the [ul u l] and 01198 * [ul u ur l] predictors */ 01199 if ((transform == 15) || (transform == 13)) { 01200 if (FFABS(predicted_dc - vu) > 128) 01201 predicted_dc = vu; 01202 else if (FFABS(predicted_dc - vl) > 128) 01203 predicted_dc = vl; 01204 else if (FFABS(predicted_dc - vul) > 128) 01205 predicted_dc = vul; 01206 } 01207 } 01208 01209 /* at long last, apply the predictor */ 01210 DC_COEFF(i) += predicted_dc; 01211 /* save the DC */ 01212 last_dc[current_frame_type] = DC_COEFF(i); 01213 } 01214 } 01215 } 01216 } 01217 01218 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend) 01219 { 01220 int x, y; 01221 int *bounding_values= s->bounding_values_array+127; 01222 01223 int width = s->fragment_width[!!plane]; 01224 int height = s->fragment_height[!!plane]; 01225 int fragment = s->fragment_start [plane] + ystart * width; 01226 int stride = s->current_frame.linesize[plane]; 01227 uint8_t *plane_data = s->current_frame.data [plane]; 01228 if (!s->flipped_image) stride = -stride; 01229 plane_data += s->data_offset[plane] + 8*ystart*stride; 01230 01231 for (y = ystart; y < yend; y++) { 01232 01233 for (x = 0; x < width; x++) { 01234 /* This code basically just deblocks on the edges of coded blocks. 01235 * However, it has to be much more complicated because of the 01236 * braindamaged deblock ordering used in VP3/Theora. Order matters 01237 * because some pixels get filtered twice. */ 01238 if( s->all_fragments[fragment].coding_method != MODE_COPY ) 01239 { 01240 /* do not perform left edge filter for left columns frags */ 01241 if (x > 0) { 01242 s->dsp.vp3_h_loop_filter( 01243 plane_data + 8*x, 01244 stride, bounding_values); 01245 } 01246 01247 /* do not perform top edge filter for top row fragments */ 01248 if (y > 0) { 01249 s->dsp.vp3_v_loop_filter( 01250 plane_data + 8*x, 01251 stride, bounding_values); 01252 } 01253 01254 /* do not perform right edge filter for right column 01255 * fragments or if right fragment neighbor is also coded 01256 * in this frame (it will be filtered in next iteration) */ 01257 if ((x < width - 1) && 01258 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) { 01259 s->dsp.vp3_h_loop_filter( 01260 plane_data + 8*x + 8, 01261 stride, bounding_values); 01262 } 01263 01264 /* do not perform bottom edge filter for bottom row 01265 * fragments or if bottom fragment neighbor is also coded 01266 * in this frame (it will be filtered in the next row) */ 01267 if ((y < height - 1) && 01268 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) { 01269 s->dsp.vp3_v_loop_filter( 01270 plane_data + 8*x + 8*stride, 01271 stride, bounding_values); 01272 } 01273 } 01274 01275 fragment++; 01276 } 01277 plane_data += 8*stride; 01278 } 01279 } 01280 01285 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag, 01286 int plane, int inter, DCTELEM block[64]) 01287 { 01288 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane]; 01289 uint8_t *perm = s->scantable.permutated; 01290 int i = 0; 01291 01292 do { 01293 int token = *s->dct_tokens[plane][i]; 01294 switch (token & 3) { 01295 case 0: // EOB 01296 if (--token < 4) // 0-3 are token types, so the EOB run must now be 0 01297 s->dct_tokens[plane][i]++; 01298 else 01299 *s->dct_tokens[plane][i] = token & ~3; 01300 goto end; 01301 case 1: // zero run 01302 s->dct_tokens[plane][i]++; 01303 i += (token >> 2) & 0x7f; 01304 if (i > 63) { 01305 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n"); 01306 return i; 01307 } 01308 block[perm[i]] = (token >> 9) * dequantizer[perm[i]]; 01309 i++; 01310 break; 01311 case 2: // coeff 01312 block[perm[i]] = (token >> 2) * dequantizer[perm[i]]; 01313 s->dct_tokens[plane][i++]++; 01314 break; 01315 default: // shouldn't happen 01316 return i; 01317 } 01318 } while (i < 64); 01319 // return value is expected to be a valid level 01320 i--; 01321 end: 01322 // the actual DC+prediction is in the fragment structure 01323 block[0] = frag->dc * s->qmat[0][inter][plane][0]; 01324 return i; 01325 } 01326 01330 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y) 01331 { 01332 int h, cy; 01333 int offset[4]; 01334 01335 if(s->avctx->draw_horiz_band==NULL) 01336 return; 01337 01338 h= y - s->last_slice_end; 01339 y -= h; 01340 01341 if (!s->flipped_image) { 01342 if (y == 0) 01343 h -= s->height - s->avctx->height; // account for non-mod16 01344 y = s->height - y - h; 01345 } 01346 01347 cy = y >> 1; 01348 offset[0] = s->current_frame.linesize[0]*y; 01349 offset[1] = s->current_frame.linesize[1]*cy; 01350 offset[2] = s->current_frame.linesize[2]*cy; 01351 offset[3] = 0; 01352 01353 emms_c(); 01354 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h); 01355 s->last_slice_end= y + h; 01356 } 01357 01358 /* 01359 * Perform the final rendering for a particular slice of data. 01360 * The slice number ranges from 0..(c_superblock_height - 1). 01361 */ 01362 static void render_slice(Vp3DecodeContext *s, int slice) 01363 { 01364 int x, y, i, j; 01365 LOCAL_ALIGNED_16(DCTELEM, block, [64]); 01366 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef; 01367 int motion_halfpel_index; 01368 uint8_t *motion_source; 01369 int plane, first_pixel; 01370 01371 if (slice >= s->c_superblock_height) 01372 return; 01373 01374 for (plane = 0; plane < 3; plane++) { 01375 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane]; 01376 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane]; 01377 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane]; 01378 int stride = s->current_frame.linesize[plane]; 01379 int plane_width = s->width >> (plane && s->chroma_x_shift); 01380 int plane_height = s->height >> (plane && s->chroma_y_shift); 01381 int8_t (*motion_val)[2] = s->motion_val[!!plane]; 01382 01383 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift); 01384 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift); 01385 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width; 01386 01387 int fragment_width = s->fragment_width[!!plane]; 01388 int fragment_height = s->fragment_height[!!plane]; 01389 int fragment_start = s->fragment_start[plane]; 01390 01391 if (!s->flipped_image) stride = -stride; 01392 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY)) 01393 continue; 01394 01395 01396 if(FFABS(stride) > 2048) 01397 return; //various tables are fixed size 01398 01399 /* for each superblock row in the slice (both of them)... */ 01400 for (; sb_y < slice_height; sb_y++) { 01401 01402 /* for each superblock in a row... */ 01403 for (sb_x = 0; sb_x < slice_width; sb_x++) { 01404 01405 /* for each block in a superblock... */ 01406 for (j = 0; j < 16; j++) { 01407 x = 4*sb_x + hilbert_offset[j][0]; 01408 y = 4*sb_y + hilbert_offset[j][1]; 01409 01410 i = fragment_start + y*fragment_width + x; 01411 01412 // bounds check 01413 if (x >= fragment_width || y >= fragment_height) 01414 continue; 01415 01416 first_pixel = 8*y*stride + 8*x; 01417 01418 /* transform if this block was coded */ 01419 if (s->all_fragments[i].coding_method != MODE_COPY) { 01420 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) || 01421 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) 01422 motion_source= golden_plane; 01423 else 01424 motion_source= last_plane; 01425 01426 motion_source += first_pixel; 01427 motion_halfpel_index = 0; 01428 01429 /* sort out the motion vector if this fragment is coded 01430 * using a motion vector method */ 01431 if ((s->all_fragments[i].coding_method > MODE_INTRA) && 01432 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) { 01433 int src_x, src_y; 01434 motion_x = motion_val[y*fragment_width + x][0]; 01435 motion_y = motion_val[y*fragment_width + x][1]; 01436 01437 src_x= (motion_x>>1) + 8*x; 01438 src_y= (motion_y>>1) + 8*y; 01439 01440 motion_halfpel_index = motion_x & 0x01; 01441 motion_source += (motion_x >> 1); 01442 01443 motion_halfpel_index |= (motion_y & 0x01) << 1; 01444 motion_source += ((motion_y >> 1) * stride); 01445 01446 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){ 01447 uint8_t *temp= s->edge_emu_buffer; 01448 if(stride<0) temp -= 9*stride; 01449 else temp += 9*stride; 01450 01451 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height); 01452 motion_source= temp; 01453 } 01454 } 01455 01456 01457 /* first, take care of copying a block from either the 01458 * previous or the golden frame */ 01459 if (s->all_fragments[i].coding_method != MODE_INTRA) { 01460 /* Note, it is possible to implement all MC cases with 01461 put_no_rnd_pixels_l2 which would look more like the 01462 VP3 source but this would be slower as 01463 put_no_rnd_pixels_tab is better optimzed */ 01464 if(motion_halfpel_index != 3){ 01465 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index]( 01466 output_plane + first_pixel, 01467 motion_source, stride, 8); 01468 }else{ 01469 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1 01470 s->dsp.put_no_rnd_pixels_l2[1]( 01471 output_plane + first_pixel, 01472 motion_source - d, 01473 motion_source + stride + 1 + d, 01474 stride, 8); 01475 } 01476 } 01477 01478 s->dsp.clear_block(block); 01479 01480 /* invert DCT and place (or add) in final output */ 01481 01482 if (s->all_fragments[i].coding_method == MODE_INTRA) { 01483 int index; 01484 index = vp3_dequant(s, s->all_fragments + i, plane, 0, block); 01485 if (index > 63) 01486 continue; 01487 if(s->avctx->idct_algo!=FF_IDCT_VP3) 01488 block[0] += 128<<3; 01489 s->dsp.idct_put( 01490 output_plane + first_pixel, 01491 stride, 01492 block); 01493 } else { 01494 int index = vp3_dequant(s, s->all_fragments + i, plane, 1, block); 01495 if (index > 63) 01496 continue; 01497 if (index > 0) { 01498 s->dsp.idct_add( 01499 output_plane + first_pixel, 01500 stride, 01501 block); 01502 } else { 01503 s->dsp.vp3_idct_dc_add(output_plane + first_pixel, stride, block); 01504 } 01505 } 01506 } else { 01507 01508 /* copy directly from the previous frame */ 01509 s->dsp.put_pixels_tab[1][0]( 01510 output_plane + first_pixel, 01511 last_plane + first_pixel, 01512 stride, 8); 01513 01514 } 01515 } 01516 } 01517 01518 // Filter up to the last row in the superblock row 01519 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1)); 01520 } 01521 } 01522 01523 /* this looks like a good place for slice dispatch... */ 01524 /* algorithm: 01525 * if (slice == s->macroblock_height - 1) 01526 * dispatch (both last slice & 2nd-to-last slice); 01527 * else if (slice > 0) 01528 * dispatch (slice - 1); 01529 */ 01530 01531 vp3_draw_horiz_band(s, FFMIN(64*slice + 64-16, s->height-16)); 01532 } 01533 01534 /* 01535 * This is the ffmpeg/libavcodec API init function. 01536 */ 01537 static av_cold int vp3_decode_init(AVCodecContext *avctx) 01538 { 01539 Vp3DecodeContext *s = avctx->priv_data; 01540 int i, inter, plane; 01541 int c_width; 01542 int c_height; 01543 int y_fragment_count, c_fragment_count; 01544 01545 if (avctx->codec_tag == MKTAG('V','P','3','0')) 01546 s->version = 0; 01547 else 01548 s->version = 1; 01549 01550 s->avctx = avctx; 01551 s->width = FFALIGN(avctx->width, 16); 01552 s->height = FFALIGN(avctx->height, 16); 01553 if (avctx->pix_fmt == PIX_FMT_NONE) 01554 avctx->pix_fmt = PIX_FMT_YUV420P; 01555 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; 01556 if(avctx->idct_algo==FF_IDCT_AUTO) 01557 avctx->idct_algo=FF_IDCT_VP3; 01558 dsputil_init(&s->dsp, avctx); 01559 01560 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); 01561 01562 /* initialize to an impossible value which will force a recalculation 01563 * in the first frame decode */ 01564 for (i = 0; i < 3; i++) 01565 s->qps[i] = -1; 01566 01567 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift); 01568 01569 s->y_superblock_width = (s->width + 31) / 32; 01570 s->y_superblock_height = (s->height + 31) / 32; 01571 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height; 01572 01573 /* work out the dimensions for the C planes */ 01574 c_width = s->width >> s->chroma_x_shift; 01575 c_height = s->height >> s->chroma_y_shift; 01576 s->c_superblock_width = (c_width + 31) / 32; 01577 s->c_superblock_height = (c_height + 31) / 32; 01578 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height; 01579 01580 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2); 01581 s->u_superblock_start = s->y_superblock_count; 01582 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count; 01583 s->superblock_coding = av_malloc(s->superblock_count); 01584 01585 s->macroblock_width = (s->width + 15) / 16; 01586 s->macroblock_height = (s->height + 15) / 16; 01587 s->macroblock_count = s->macroblock_width * s->macroblock_height; 01588 01589 s->fragment_width[0] = s->width / FRAGMENT_PIXELS; 01590 s->fragment_height[0] = s->height / FRAGMENT_PIXELS; 01591 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift; 01592 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift; 01593 01594 /* fragment count covers all 8x8 blocks for all 3 planes */ 01595 y_fragment_count = s->fragment_width[0] * s->fragment_height[0]; 01596 c_fragment_count = s->fragment_width[1] * s->fragment_height[1]; 01597 s->fragment_count = y_fragment_count + 2*c_fragment_count; 01598 s->fragment_start[1] = y_fragment_count; 01599 s->fragment_start[2] = y_fragment_count + c_fragment_count; 01600 01601 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment)); 01602 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int)); 01603 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base)); 01604 s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0])); 01605 s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1])); 01606 01607 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base || 01608 !s->coded_fragment_list[0] || !s->motion_val[0] || !s->motion_val[1]) { 01609 vp3_decode_end(avctx); 01610 return -1; 01611 } 01612 01613 if (!s->theora_tables) 01614 { 01615 for (i = 0; i < 64; i++) { 01616 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; 01617 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i]; 01618 s->base_matrix[0][i] = vp31_intra_y_dequant[i]; 01619 s->base_matrix[1][i] = vp31_intra_c_dequant[i]; 01620 s->base_matrix[2][i] = vp31_inter_dequant[i]; 01621 s->filter_limit_values[i] = vp31_filter_limit_values[i]; 01622 } 01623 01624 for(inter=0; inter<2; inter++){ 01625 for(plane=0; plane<3; plane++){ 01626 s->qr_count[inter][plane]= 1; 01627 s->qr_size [inter][plane][0]= 63; 01628 s->qr_base [inter][plane][0]= 01629 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter; 01630 } 01631 } 01632 01633 /* init VLC tables */ 01634 for (i = 0; i < 16; i++) { 01635 01636 /* DC histograms */ 01637 init_vlc(&s->dc_vlc[i], 11, 32, 01638 &dc_bias[i][0][1], 4, 2, 01639 &dc_bias[i][0][0], 4, 2, 0); 01640 01641 /* group 1 AC histograms */ 01642 init_vlc(&s->ac_vlc_1[i], 11, 32, 01643 &ac_bias_0[i][0][1], 4, 2, 01644 &ac_bias_0[i][0][0], 4, 2, 0); 01645 01646 /* group 2 AC histograms */ 01647 init_vlc(&s->ac_vlc_2[i], 11, 32, 01648 &ac_bias_1[i][0][1], 4, 2, 01649 &ac_bias_1[i][0][0], 4, 2, 0); 01650 01651 /* group 3 AC histograms */ 01652 init_vlc(&s->ac_vlc_3[i], 11, 32, 01653 &ac_bias_2[i][0][1], 4, 2, 01654 &ac_bias_2[i][0][0], 4, 2, 0); 01655 01656 /* group 4 AC histograms */ 01657 init_vlc(&s->ac_vlc_4[i], 11, 32, 01658 &ac_bias_3[i][0][1], 4, 2, 01659 &ac_bias_3[i][0][0], 4, 2, 0); 01660 } 01661 } else { 01662 01663 for (i = 0; i < 16; i++) { 01664 /* DC histograms */ 01665 if (init_vlc(&s->dc_vlc[i], 11, 32, 01666 &s->huffman_table[i][0][1], 8, 4, 01667 &s->huffman_table[i][0][0], 8, 4, 0) < 0) 01668 goto vlc_fail; 01669 01670 /* group 1 AC histograms */ 01671 if (init_vlc(&s->ac_vlc_1[i], 11, 32, 01672 &s->huffman_table[i+16][0][1], 8, 4, 01673 &s->huffman_table[i+16][0][0], 8, 4, 0) < 0) 01674 goto vlc_fail; 01675 01676 /* group 2 AC histograms */ 01677 if (init_vlc(&s->ac_vlc_2[i], 11, 32, 01678 &s->huffman_table[i+16*2][0][1], 8, 4, 01679 &s->huffman_table[i+16*2][0][0], 8, 4, 0) < 0) 01680 goto vlc_fail; 01681 01682 /* group 3 AC histograms */ 01683 if (init_vlc(&s->ac_vlc_3[i], 11, 32, 01684 &s->huffman_table[i+16*3][0][1], 8, 4, 01685 &s->huffman_table[i+16*3][0][0], 8, 4, 0) < 0) 01686 goto vlc_fail; 01687 01688 /* group 4 AC histograms */ 01689 if (init_vlc(&s->ac_vlc_4[i], 11, 32, 01690 &s->huffman_table[i+16*4][0][1], 8, 4, 01691 &s->huffman_table[i+16*4][0][0], 8, 4, 0) < 0) 01692 goto vlc_fail; 01693 } 01694 } 01695 01696 init_vlc(&s->superblock_run_length_vlc, 6, 34, 01697 &superblock_run_length_vlc_table[0][1], 4, 2, 01698 &superblock_run_length_vlc_table[0][0], 4, 2, 0); 01699 01700 init_vlc(&s->fragment_run_length_vlc, 5, 30, 01701 &fragment_run_length_vlc_table[0][1], 4, 2, 01702 &fragment_run_length_vlc_table[0][0], 4, 2, 0); 01703 01704 init_vlc(&s->mode_code_vlc, 3, 8, 01705 &mode_code_vlc_table[0][1], 2, 1, 01706 &mode_code_vlc_table[0][0], 2, 1, 0); 01707 01708 init_vlc(&s->motion_vector_vlc, 6, 63, 01709 &motion_vector_vlc_table[0][1], 2, 1, 01710 &motion_vector_vlc_table[0][0], 2, 1, 0); 01711 01712 /* work out the block mapping tables */ 01713 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int)); 01714 s->macroblock_coding = av_malloc(s->macroblock_count + 1); 01715 if (!s->superblock_fragments || !s->macroblock_coding) { 01716 vp3_decode_end(avctx); 01717 return -1; 01718 } 01719 init_block_mapping(s); 01720 01721 for (i = 0; i < 3; i++) { 01722 s->current_frame.data[i] = NULL; 01723 s->last_frame.data[i] = NULL; 01724 s->golden_frame.data[i] = NULL; 01725 } 01726 01727 return 0; 01728 01729 vlc_fail: 01730 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n"); 01731 return -1; 01732 } 01733 01734 /* 01735 * This is the ffmpeg/libavcodec API frame decode function. 01736 */ 01737 static int vp3_decode_frame(AVCodecContext *avctx, 01738 void *data, int *data_size, 01739 AVPacket *avpkt) 01740 { 01741 const uint8_t *buf = avpkt->data; 01742 int buf_size = avpkt->size; 01743 Vp3DecodeContext *s = avctx->priv_data; 01744 GetBitContext gb; 01745 static int counter = 0; 01746 int i; 01747 01748 init_get_bits(&gb, buf, buf_size * 8); 01749 01750 if (s->theora && get_bits1(&gb)) 01751 { 01752 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n"); 01753 return -1; 01754 } 01755 01756 s->keyframe = !get_bits1(&gb); 01757 if (!s->theora) 01758 skip_bits(&gb, 1); 01759 for (i = 0; i < 3; i++) 01760 s->last_qps[i] = s->qps[i]; 01761 01762 s->nqps=0; 01763 do{ 01764 s->qps[s->nqps++]= get_bits(&gb, 6); 01765 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb)); 01766 for (i = s->nqps; i < 3; i++) 01767 s->qps[i] = -1; 01768 01769 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 01770 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n", 01771 s->keyframe?"key":"", counter, s->qps[0]); 01772 counter++; 01773 01774 if (s->qps[0] != s->last_qps[0]) 01775 init_loop_filter(s); 01776 01777 for (i = 0; i < s->nqps; i++) 01778 // reinit all dequantizers if the first one changed, because 01779 // the DC of the first quantizer must be used for all matrices 01780 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0]) 01781 init_dequantizer(s, i); 01782 01783 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) 01784 return buf_size; 01785 01786 s->current_frame.reference = 3; 01787 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE; 01788 if (avctx->get_buffer(avctx, &s->current_frame) < 0) { 01789 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 01790 goto error; 01791 } 01792 01793 if (s->keyframe) { 01794 if (!s->theora) 01795 { 01796 skip_bits(&gb, 4); /* width code */ 01797 skip_bits(&gb, 4); /* height code */ 01798 if (s->version) 01799 { 01800 s->version = get_bits(&gb, 5); 01801 if (counter == 1) 01802 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version); 01803 } 01804 } 01805 if (s->version || s->theora) 01806 { 01807 if (get_bits1(&gb)) 01808 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n"); 01809 skip_bits(&gb, 2); /* reserved? */ 01810 } 01811 } else { 01812 if (!s->golden_frame.data[0]) { 01813 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n"); 01814 01815 s->golden_frame.reference = 3; 01816 s->golden_frame.pict_type = FF_I_TYPE; 01817 if (avctx->get_buffer(avctx, &s->golden_frame) < 0) { 01818 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 01819 goto error; 01820 } 01821 s->last_frame = s->golden_frame; 01822 s->last_frame.type = FF_BUFFER_TYPE_COPY; 01823 } 01824 } 01825 01826 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame 01827 s->current_frame.qstride= 0; 01828 01829 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment)); 01830 01831 if (unpack_superblocks(s, &gb)){ 01832 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n"); 01833 goto error; 01834 } 01835 if (unpack_modes(s, &gb)){ 01836 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n"); 01837 goto error; 01838 } 01839 if (unpack_vectors(s, &gb)){ 01840 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); 01841 goto error; 01842 } 01843 if (unpack_block_qpis(s, &gb)){ 01844 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); 01845 goto error; 01846 } 01847 if (unpack_dct_coeffs(s, &gb)){ 01848 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); 01849 goto error; 01850 } 01851 01852 for (i = 0; i < 3; i++) { 01853 int height = s->height >> (i && s->chroma_y_shift); 01854 if (s->flipped_image) 01855 s->data_offset[i] = 0; 01856 else 01857 s->data_offset[i] = (height-1) * s->current_frame.linesize[i]; 01858 } 01859 01860 s->last_slice_end = 0; 01861 for (i = 0; i < s->c_superblock_height; i++) 01862 render_slice(s, i); 01863 01864 // filter the last row 01865 for (i = 0; i < 3; i++) { 01866 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1; 01867 apply_loop_filter(s, i, row, row+1); 01868 } 01869 vp3_draw_horiz_band(s, s->height); 01870 01871 *data_size=sizeof(AVFrame); 01872 *(AVFrame*)data= s->current_frame; 01873 01874 /* release the last frame, if it is allocated and if it is not the 01875 * golden frame */ 01876 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY) 01877 avctx->release_buffer(avctx, &s->last_frame); 01878 01879 /* shuffle frames (last = current) */ 01880 s->last_frame= s->current_frame; 01881 01882 if (s->keyframe) { 01883 if (s->golden_frame.data[0]) 01884 avctx->release_buffer(avctx, &s->golden_frame); 01885 s->golden_frame = s->current_frame; 01886 s->last_frame.type = FF_BUFFER_TYPE_COPY; 01887 } 01888 01889 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */ 01890 01891 return buf_size; 01892 01893 error: 01894 if (s->current_frame.data[0]) 01895 avctx->release_buffer(avctx, &s->current_frame); 01896 return -1; 01897 } 01898 01899 /* 01900 * This is the ffmpeg/libavcodec API module cleanup function. 01901 */ 01902 static av_cold int vp3_decode_end(AVCodecContext *avctx) 01903 { 01904 Vp3DecodeContext *s = avctx->priv_data; 01905 int i; 01906 01907 av_free(s->superblock_coding); 01908 av_free(s->all_fragments); 01909 av_free(s->coded_fragment_list[0]); 01910 av_free(s->dct_tokens_base); 01911 av_free(s->superblock_fragments); 01912 av_free(s->macroblock_coding); 01913 av_free(s->motion_val[0]); 01914 av_free(s->motion_val[1]); 01915 01916 for (i = 0; i < 16; i++) { 01917 free_vlc(&s->dc_vlc[i]); 01918 free_vlc(&s->ac_vlc_1[i]); 01919 free_vlc(&s->ac_vlc_2[i]); 01920 free_vlc(&s->ac_vlc_3[i]); 01921 free_vlc(&s->ac_vlc_4[i]); 01922 } 01923 01924 free_vlc(&s->superblock_run_length_vlc); 01925 free_vlc(&s->fragment_run_length_vlc); 01926 free_vlc(&s->mode_code_vlc); 01927 free_vlc(&s->motion_vector_vlc); 01928 01929 /* release all frames */ 01930 if (s->golden_frame.data[0]) 01931 avctx->release_buffer(avctx, &s->golden_frame); 01932 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY) 01933 avctx->release_buffer(avctx, &s->last_frame); 01934 /* no need to release the current_frame since it will always be pointing 01935 * to the same frame as either the golden or last frame */ 01936 01937 return 0; 01938 } 01939 01940 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb) 01941 { 01942 Vp3DecodeContext *s = avctx->priv_data; 01943 01944 if (get_bits1(gb)) { 01945 int token; 01946 if (s->entries >= 32) { /* overflow */ 01947 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); 01948 return -1; 01949 } 01950 token = get_bits(gb, 5); 01951 //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); 01952 s->huffman_table[s->hti][token][0] = s->hbits; 01953 s->huffman_table[s->hti][token][1] = s->huff_code_size; 01954 s->entries++; 01955 } 01956 else { 01957 if (s->huff_code_size >= 32) {/* overflow */ 01958 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); 01959 return -1; 01960 } 01961 s->huff_code_size++; 01962 s->hbits <<= 1; 01963 if (read_huffman_tree(avctx, gb)) 01964 return -1; 01965 s->hbits |= 1; 01966 if (read_huffman_tree(avctx, gb)) 01967 return -1; 01968 s->hbits >>= 1; 01969 s->huff_code_size--; 01970 } 01971 return 0; 01972 } 01973 01974 #if CONFIG_THEORA_DECODER 01975 static const enum PixelFormat theora_pix_fmts[4] = { 01976 PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P 01977 }; 01978 01979 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) 01980 { 01981 Vp3DecodeContext *s = avctx->priv_data; 01982 int visible_width, visible_height, colorspace; 01983 int offset_x = 0, offset_y = 0; 01984 AVRational fps; 01985 01986 s->theora = get_bits_long(gb, 24); 01987 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora); 01988 01989 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */ 01990 /* but previous versions have the image flipped relative to vp3 */ 01991 if (s->theora < 0x030200) 01992 { 01993 s->flipped_image = 1; 01994 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n"); 01995 } 01996 01997 visible_width = s->width = get_bits(gb, 16) << 4; 01998 visible_height = s->height = get_bits(gb, 16) << 4; 01999 02000 if(avcodec_check_dimensions(avctx, s->width, s->height)){ 02001 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height); 02002 s->width= s->height= 0; 02003 return -1; 02004 } 02005 02006 if (s->theora >= 0x030200) { 02007 visible_width = get_bits_long(gb, 24); 02008 visible_height = get_bits_long(gb, 24); 02009 02010 offset_x = get_bits(gb, 8); /* offset x */ 02011 offset_y = get_bits(gb, 8); /* offset y, from bottom */ 02012 } 02013 02014 fps.num = get_bits_long(gb, 32); 02015 fps.den = get_bits_long(gb, 32); 02016 if (fps.num && fps.den) { 02017 av_reduce(&avctx->time_base.num, &avctx->time_base.den, 02018 fps.den, fps.num, 1<<30); 02019 } 02020 02021 avctx->sample_aspect_ratio.num = get_bits_long(gb, 24); 02022 avctx->sample_aspect_ratio.den = get_bits_long(gb, 24); 02023 02024 if (s->theora < 0x030200) 02025 skip_bits(gb, 5); /* keyframe frequency force */ 02026 colorspace = get_bits(gb, 8); 02027 skip_bits(gb, 24); /* bitrate */ 02028 02029 skip_bits(gb, 6); /* quality hint */ 02030 02031 if (s->theora >= 0x030200) 02032 { 02033 skip_bits(gb, 5); /* keyframe frequency force */ 02034 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)]; 02035 skip_bits(gb, 3); /* reserved */ 02036 } 02037 02038 // align_get_bits(gb); 02039 02040 if ( visible_width <= s->width && visible_width > s->width-16 02041 && visible_height <= s->height && visible_height > s->height-16 02042 && !offset_x && (offset_y == s->height - visible_height)) 02043 avcodec_set_dimensions(avctx, visible_width, visible_height); 02044 else 02045 avcodec_set_dimensions(avctx, s->width, s->height); 02046 02047 if (colorspace == 1) { 02048 avctx->color_primaries = AVCOL_PRI_BT470M; 02049 } else if (colorspace == 2) { 02050 avctx->color_primaries = AVCOL_PRI_BT470BG; 02051 } 02052 if (colorspace == 1 || colorspace == 2) { 02053 avctx->colorspace = AVCOL_SPC_BT470BG; 02054 avctx->color_trc = AVCOL_TRC_BT709; 02055 } 02056 02057 return 0; 02058 } 02059 02060 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) 02061 { 02062 Vp3DecodeContext *s = avctx->priv_data; 02063 int i, n, matrices, inter, plane; 02064 02065 if (s->theora >= 0x030200) { 02066 n = get_bits(gb, 3); 02067 /* loop filter limit values table */ 02068 for (i = 0; i < 64; i++) { 02069 s->filter_limit_values[i] = get_bits(gb, n); 02070 if (s->filter_limit_values[i] > 127) { 02071 av_log(avctx, AV_LOG_ERROR, "filter limit value too large (%i > 127), clamping\n", s->filter_limit_values[i]); 02072 s->filter_limit_values[i] = 127; 02073 } 02074 } 02075 } 02076 02077 if (s->theora >= 0x030200) 02078 n = get_bits(gb, 4) + 1; 02079 else 02080 n = 16; 02081 /* quality threshold table */ 02082 for (i = 0; i < 64; i++) 02083 s->coded_ac_scale_factor[i] = get_bits(gb, n); 02084 02085 if (s->theora >= 0x030200) 02086 n = get_bits(gb, 4) + 1; 02087 else 02088 n = 16; 02089 /* dc scale factor table */ 02090 for (i = 0; i < 64; i++) 02091 s->coded_dc_scale_factor[i] = get_bits(gb, n); 02092 02093 if (s->theora >= 0x030200) 02094 matrices = get_bits(gb, 9) + 1; 02095 else 02096 matrices = 3; 02097 02098 if(matrices > 384){ 02099 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n"); 02100 return -1; 02101 } 02102 02103 for(n=0; n<matrices; n++){ 02104 for (i = 0; i < 64; i++) 02105 s->base_matrix[n][i]= get_bits(gb, 8); 02106 } 02107 02108 for (inter = 0; inter <= 1; inter++) { 02109 for (plane = 0; plane <= 2; plane++) { 02110 int newqr= 1; 02111 if (inter || plane > 0) 02112 newqr = get_bits1(gb); 02113 if (!newqr) { 02114 int qtj, plj; 02115 if(inter && get_bits1(gb)){ 02116 qtj = 0; 02117 plj = plane; 02118 }else{ 02119 qtj= (3*inter + plane - 1) / 3; 02120 plj= (plane + 2) % 3; 02121 } 02122 s->qr_count[inter][plane]= s->qr_count[qtj][plj]; 02123 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0])); 02124 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0])); 02125 } else { 02126 int qri= 0; 02127 int qi = 0; 02128 02129 for(;;){ 02130 i= get_bits(gb, av_log2(matrices-1)+1); 02131 if(i>= matrices){ 02132 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n"); 02133 return -1; 02134 } 02135 s->qr_base[inter][plane][qri]= i; 02136 if(qi >= 63) 02137 break; 02138 i = get_bits(gb, av_log2(63-qi)+1) + 1; 02139 s->qr_size[inter][plane][qri++]= i; 02140 qi += i; 02141 } 02142 02143 if (qi > 63) { 02144 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); 02145 return -1; 02146 } 02147 s->qr_count[inter][plane]= qri; 02148 } 02149 } 02150 } 02151 02152 /* Huffman tables */ 02153 for (s->hti = 0; s->hti < 80; s->hti++) { 02154 s->entries = 0; 02155 s->huff_code_size = 1; 02156 if (!get_bits1(gb)) { 02157 s->hbits = 0; 02158 if(read_huffman_tree(avctx, gb)) 02159 return -1; 02160 s->hbits = 1; 02161 if(read_huffman_tree(avctx, gb)) 02162 return -1; 02163 } 02164 } 02165 02166 s->theora_tables = 1; 02167 02168 return 0; 02169 } 02170 02171 static av_cold int theora_decode_init(AVCodecContext *avctx) 02172 { 02173 Vp3DecodeContext *s = avctx->priv_data; 02174 GetBitContext gb; 02175 int ptype; 02176 uint8_t *header_start[3]; 02177 int header_len[3]; 02178 int i; 02179 02180 s->theora = 1; 02181 02182 if (!avctx->extradata_size) 02183 { 02184 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n"); 02185 return -1; 02186 } 02187 02188 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size, 02189 42, header_start, header_len) < 0) { 02190 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n"); 02191 return -1; 02192 } 02193 02194 for(i=0;i<3;i++) { 02195 init_get_bits(&gb, header_start[i], header_len[i] * 8); 02196 02197 ptype = get_bits(&gb, 8); 02198 02199 if (!(ptype & 0x80)) 02200 { 02201 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n"); 02202 // return -1; 02203 } 02204 02205 // FIXME: Check for this as well. 02206 skip_bits_long(&gb, 6*8); /* "theora" */ 02207 02208 switch(ptype) 02209 { 02210 case 0x80: 02211 theora_decode_header(avctx, &gb); 02212 break; 02213 case 0x81: 02214 // FIXME: is this needed? it breaks sometimes 02215 // theora_decode_comments(avctx, gb); 02216 break; 02217 case 0x82: 02218 if (theora_decode_tables(avctx, &gb)) 02219 return -1; 02220 break; 02221 default: 02222 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80); 02223 break; 02224 } 02225 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb)) 02226 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype); 02227 if (s->theora < 0x030200) 02228 break; 02229 } 02230 02231 return vp3_decode_init(avctx); 02232 } 02233 02234 AVCodec theora_decoder = { 02235 "theora", 02236 AVMEDIA_TYPE_VIDEO, 02237 CODEC_ID_THEORA, 02238 sizeof(Vp3DecodeContext), 02239 theora_decode_init, 02240 NULL, 02241 vp3_decode_end, 02242 vp3_decode_frame, 02243 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, 02244 NULL, 02245 .long_name = NULL_IF_CONFIG_SMALL("Theora"), 02246 }; 02247 #endif 02248 02249 AVCodec vp3_decoder = { 02250 "vp3", 02251 AVMEDIA_TYPE_VIDEO, 02252 CODEC_ID_VP3, 02253 sizeof(Vp3DecodeContext), 02254 vp3_decode_init, 02255 NULL, 02256 vp3_decode_end, 02257 vp3_decode_frame, 02258 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, 02259 NULL, 02260 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"), 02261 };