00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "dsputil.h"
00029 #include "avcodec.h"
00030 #include "mpegvideo.h"
00031 #include "h264.h"
00032 #include "h264data.h"
00033 #include "h264_parser.h"
00034 #include "golomb.h"
00035 #include "rectangle.h"
00036
00037 #include "cabac.h"
00038
00039
00040 #include <assert.h>
00041
00046 #define DELAYED_PIC_REF 4
00047
00048 static VLC coeff_token_vlc[4];
00049 static VLC chroma_dc_coeff_token_vlc;
00050
00051 static VLC total_zeros_vlc[15];
00052 static VLC chroma_dc_total_zeros_vlc[3];
00053
00054 static VLC run_vlc[6];
00055 static VLC run7_vlc;
00056
00057 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
00058 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
00059 static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
00060 static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
00061
00062 static av_always_inline uint32_t pack16to32(int a, int b){
00063 #ifdef WORDS_BIGENDIAN
00064 return (b&0xFFFF) + (a<<16);
00065 #else
00066 return (a&0xFFFF) + (b<<16);
00067 #endif
00068 }
00069
00070 const uint8_t ff_rem6[52]={
00071 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
00072 };
00073
00074 const uint8_t ff_div6[52]={
00075 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
00076 };
00077
00078
00079 static void fill_caches(H264Context *h, int mb_type, int for_deblock){
00080 MpegEncContext * const s = &h->s;
00081 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
00082 int topleft_xy, top_xy, topright_xy, left_xy[2];
00083 int topleft_type, top_type, topright_type, left_type[2];
00084 int left_block[8];
00085 int topleft_partition= -1;
00086 int i;
00087
00088 top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
00089
00090
00091 if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)
00092 return;
00093
00094
00095
00096 topleft_xy = top_xy - 1;
00097 topright_xy= top_xy + 1;
00098 left_xy[1] = left_xy[0] = mb_xy-1;
00099 left_block[0]= 0;
00100 left_block[1]= 1;
00101 left_block[2]= 2;
00102 left_block[3]= 3;
00103 left_block[4]= 7;
00104 left_block[5]= 10;
00105 left_block[6]= 8;
00106 left_block[7]= 11;
00107 if(FRAME_MBAFF){
00108 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
00109 const int top_pair_xy = pair_xy - s->mb_stride;
00110 const int topleft_pair_xy = top_pair_xy - 1;
00111 const int topright_pair_xy = top_pair_xy + 1;
00112 const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
00113 const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
00114 const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
00115 const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
00116 const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
00117 const int bottom = (s->mb_y & 1);
00118 tprintf(s->avctx, "fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
00119 if (bottom
00120 ? !curr_mb_frame_flag
00121 : (!curr_mb_frame_flag && !top_mb_frame_flag)
00122 ) {
00123 top_xy -= s->mb_stride;
00124 }
00125 if (bottom
00126 ? !curr_mb_frame_flag
00127 : (!curr_mb_frame_flag && !topleft_mb_frame_flag)
00128 ) {
00129 topleft_xy -= s->mb_stride;
00130 } else if(bottom && curr_mb_frame_flag && !left_mb_frame_flag) {
00131 topleft_xy += s->mb_stride;
00132
00133 topleft_partition = 0;
00134 }
00135 if (bottom
00136 ? !curr_mb_frame_flag
00137 : (!curr_mb_frame_flag && !topright_mb_frame_flag)
00138 ) {
00139 topright_xy -= s->mb_stride;
00140 }
00141 if (left_mb_frame_flag != curr_mb_frame_flag) {
00142 left_xy[1] = left_xy[0] = pair_xy - 1;
00143 if (curr_mb_frame_flag) {
00144 if (bottom) {
00145 left_block[0]= 2;
00146 left_block[1]= 2;
00147 left_block[2]= 3;
00148 left_block[3]= 3;
00149 left_block[4]= 8;
00150 left_block[5]= 11;
00151 left_block[6]= 8;
00152 left_block[7]= 11;
00153 } else {
00154 left_block[0]= 0;
00155 left_block[1]= 0;
00156 left_block[2]= 1;
00157 left_block[3]= 1;
00158 left_block[4]= 7;
00159 left_block[5]= 10;
00160 left_block[6]= 7;
00161 left_block[7]= 10;
00162 }
00163 } else {
00164 left_xy[1] += s->mb_stride;
00165
00166 left_block[1]= 2;
00167 left_block[2]= 0;
00168 left_block[3]= 2;
00169
00170 left_block[5]= 10;
00171 left_block[6]= 7;
00172 left_block[7]= 10;
00173 }
00174 }
00175 }
00176
00177 h->top_mb_xy = top_xy;
00178 h->left_mb_xy[0] = left_xy[0];
00179 h->left_mb_xy[1] = left_xy[1];
00180 if(for_deblock){
00181 topleft_type = 0;
00182 topright_type = 0;
00183 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
00184 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
00185 left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
00186
00187 if(FRAME_MBAFF && !IS_INTRA(mb_type)){
00188 int list;
00189 int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
00190 for(i=0; i<16; i++)
00191 h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
00192 for(list=0; list<h->list_count; list++){
00193 if(USES_LIST(mb_type,list)){
00194 uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
00195 uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
00196 int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
00197 for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
00198 dst[0] = src[0];
00199 dst[1] = src[1];
00200 dst[2] = src[2];
00201 dst[3] = src[3];
00202 }
00203 *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
00204 *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
00205 ref += h->b8_stride;
00206 *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
00207 *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
00208 }else{
00209 fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
00210 fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
00211 }
00212 }
00213 }
00214 }else{
00215 topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
00216 top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
00217 topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
00218 left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
00219 left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
00220 }
00221
00222 if(IS_INTRA(mb_type)){
00223 h->topleft_samples_available=
00224 h->top_samples_available=
00225 h->left_samples_available= 0xFFFF;
00226 h->topright_samples_available= 0xEEEA;
00227
00228 if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
00229 h->topleft_samples_available= 0xB3FF;
00230 h->top_samples_available= 0x33FF;
00231 h->topright_samples_available= 0x26EA;
00232 }
00233 for(i=0; i<2; i++){
00234 if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
00235 h->topleft_samples_available&= 0xDF5F;
00236 h->left_samples_available&= 0x5F5F;
00237 }
00238 }
00239
00240 if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
00241 h->topleft_samples_available&= 0x7FFF;
00242
00243 if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
00244 h->topright_samples_available&= 0xFBFF;
00245
00246 if(IS_INTRA4x4(mb_type)){
00247 if(IS_INTRA4x4(top_type)){
00248 h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
00249 h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
00250 h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
00251 h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
00252 }else{
00253 int pred;
00254 if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
00255 pred= -1;
00256 else{
00257 pred= 2;
00258 }
00259 h->intra4x4_pred_mode_cache[4+8*0]=
00260 h->intra4x4_pred_mode_cache[5+8*0]=
00261 h->intra4x4_pred_mode_cache[6+8*0]=
00262 h->intra4x4_pred_mode_cache[7+8*0]= pred;
00263 }
00264 for(i=0; i<2; i++){
00265 if(IS_INTRA4x4(left_type[i])){
00266 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
00267 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
00268 }else{
00269 int pred;
00270 if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
00271 pred= -1;
00272 else{
00273 pred= 2;
00274 }
00275 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
00276 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
00277 }
00278 }
00279 }
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 if(top_type){
00293 h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
00294 h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
00295 h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
00296 h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
00297
00298 h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
00299 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
00300
00301 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
00302 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
00303
00304 }else{
00305 h->non_zero_count_cache[4+8*0]=
00306 h->non_zero_count_cache[5+8*0]=
00307 h->non_zero_count_cache[6+8*0]=
00308 h->non_zero_count_cache[7+8*0]=
00309
00310 h->non_zero_count_cache[1+8*0]=
00311 h->non_zero_count_cache[2+8*0]=
00312
00313 h->non_zero_count_cache[1+8*3]=
00314 h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
00315
00316 }
00317
00318 for (i=0; i<2; i++) {
00319 if(left_type[i]){
00320 h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
00321 h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
00322 h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
00323 h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
00324 }else{
00325 h->non_zero_count_cache[3+8*1 + 2*8*i]=
00326 h->non_zero_count_cache[3+8*2 + 2*8*i]=
00327 h->non_zero_count_cache[0+8*1 + 8*i]=
00328 h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
00329 }
00330 }
00331
00332 if( h->pps.cabac ) {
00333
00334 if(top_type) {
00335 h->top_cbp = h->cbp_table[top_xy];
00336 } else if(IS_INTRA(mb_type)) {
00337 h->top_cbp = 0x1C0;
00338 } else {
00339 h->top_cbp = 0;
00340 }
00341
00342 if (left_type[0]) {
00343 h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
00344 } else if(IS_INTRA(mb_type)) {
00345 h->left_cbp = 0x1C0;
00346 } else {
00347 h->left_cbp = 0;
00348 }
00349 if (left_type[0]) {
00350 h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
00351 }
00352 if (left_type[1]) {
00353 h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
00354 }
00355 }
00356
00357 #if 1
00358 if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
00359 int list;
00360 for(list=0; list<h->list_count; list++){
00361 if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
00362
00363
00364
00365
00366
00367 continue;
00368 }
00369 h->mv_cache_clean[list]= 0;
00370
00371 if(USES_LIST(top_type, list)){
00372 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
00373 const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
00374 *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
00375 *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
00376 *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
00377 *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
00378 h->ref_cache[list][scan8[0] + 0 - 1*8]=
00379 h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
00380 h->ref_cache[list][scan8[0] + 2 - 1*8]=
00381 h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
00382 }else{
00383 *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
00384 *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
00385 *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
00386 *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
00387 *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
00388 }
00389
00390 for(i=0; i<2; i++){
00391 int cache_idx = scan8[0] - 1 + i*2*8;
00392 if(USES_LIST(left_type[i], list)){
00393 const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
00394 const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
00395 *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
00396 *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
00397 h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
00398 h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
00399 }else{
00400 *(uint32_t*)h->mv_cache [list][cache_idx ]=
00401 *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
00402 h->ref_cache[list][cache_idx ]=
00403 h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00404 }
00405 }
00406
00407 if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
00408 continue;
00409
00410 if(USES_LIST(topleft_type, list)){
00411 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
00412 const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
00413 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
00414 h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
00415 }else{
00416 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
00417 h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00418 }
00419
00420 if(USES_LIST(topright_type, list)){
00421 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
00422 const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
00423 *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
00424 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
00425 }else{
00426 *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
00427 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00428 }
00429
00430 if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
00431 continue;
00432
00433 h->ref_cache[list][scan8[5 ]+1] =
00434 h->ref_cache[list][scan8[7 ]+1] =
00435 h->ref_cache[list][scan8[13]+1] =
00436 h->ref_cache[list][scan8[4 ]] =
00437 h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
00438 *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
00439 *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
00440 *(uint32_t*)h->mv_cache [list][scan8[13]+1]=
00441 *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
00442 *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
00443
00444 if( h->pps.cabac ) {
00445
00446 if(USES_LIST(top_type, list)){
00447 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
00448 *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
00449 *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
00450 *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
00451 *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
00452 }else{
00453 *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
00454 *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
00455 *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
00456 *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
00457 }
00458 if(USES_LIST(left_type[0], list)){
00459 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
00460 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
00461 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
00462 }else{
00463 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
00464 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
00465 }
00466 if(USES_LIST(left_type[1], list)){
00467 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
00468 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
00469 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
00470 }else{
00471 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
00472 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
00473 }
00474 *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
00475 *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
00476 *(uint32_t*)h->mvd_cache [list][scan8[13]+1]=
00477 *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
00478 *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
00479
00480 if(h->slice_type == B_TYPE){
00481 fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
00482
00483 if(IS_DIRECT(top_type)){
00484 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
00485 }else if(IS_8X8(top_type)){
00486 int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
00487 h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
00488 h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
00489 }else{
00490 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
00491 }
00492
00493 if(IS_DIRECT(left_type[0]))
00494 h->direct_cache[scan8[0] - 1 + 0*8]= 1;
00495 else if(IS_8X8(left_type[0]))
00496 h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
00497 else
00498 h->direct_cache[scan8[0] - 1 + 0*8]= 0;
00499
00500 if(IS_DIRECT(left_type[1]))
00501 h->direct_cache[scan8[0] - 1 + 2*8]= 1;
00502 else if(IS_8X8(left_type[1]))
00503 h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
00504 else
00505 h->direct_cache[scan8[0] - 1 + 2*8]= 0;
00506 }
00507 }
00508
00509 if(FRAME_MBAFF){
00510 #define MAP_MVS\
00511 MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
00512 MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
00513 MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
00514 MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
00515 MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
00516 MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
00517 MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
00518 MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
00519 MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
00520 MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
00521 if(MB_FIELD){
00522 #define MAP_F2F(idx, mb_type)\
00523 if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
00524 h->ref_cache[list][idx] <<= 1;\
00525 h->mv_cache[list][idx][1] /= 2;\
00526 h->mvd_cache[list][idx][1] /= 2;\
00527 }
00528 MAP_MVS
00529 #undef MAP_F2F
00530 }else{
00531 #define MAP_F2F(idx, mb_type)\
00532 if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
00533 h->ref_cache[list][idx] >>= 1;\
00534 h->mv_cache[list][idx][1] <<= 1;\
00535 h->mvd_cache[list][idx][1] <<= 1;\
00536 }
00537 MAP_MVS
00538 #undef MAP_F2F
00539 }
00540 }
00541 }
00542 }
00543 #endif
00544
00545 h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
00546 }
00547
00548 static inline void write_back_intra_pred_mode(H264Context *h){
00549 MpegEncContext * const s = &h->s;
00550 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
00551
00552 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
00553 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
00554 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
00555 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
00556 h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
00557 h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
00558 h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
00559 }
00560
00564 static inline int check_intra4x4_pred_mode(H264Context *h){
00565 MpegEncContext * const s = &h->s;
00566 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
00567 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
00568 int i;
00569
00570 if(!(h->top_samples_available&0x8000)){
00571 for(i=0; i<4; i++){
00572 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
00573 if(status<0){
00574 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
00575 return -1;
00576 } else if(status){
00577 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
00578 }
00579 }
00580 }
00581
00582 if(!(h->left_samples_available&0x8000)){
00583 for(i=0; i<4; i++){
00584 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
00585 if(status<0){
00586 av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
00587 return -1;
00588 } else if(status){
00589 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
00590 }
00591 }
00592 }
00593
00594 return 0;
00595 }
00596
00600 static inline int check_intra_pred_mode(H264Context *h, int mode){
00601 MpegEncContext * const s = &h->s;
00602 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
00603 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
00604
00605 if(mode > 6U) {
00606 av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
00607 return -1;
00608 }
00609
00610 if(!(h->top_samples_available&0x8000)){
00611 mode= top[ mode ];
00612 if(mode<0){
00613 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
00614 return -1;
00615 }
00616 }
00617
00618 if(!(h->left_samples_available&0x8000)){
00619 mode= left[ mode ];
00620 if(mode<0){
00621 av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
00622 return -1;
00623 }
00624 }
00625
00626 return mode;
00627 }
00628
00632 static inline int pred_intra_mode(H264Context *h, int n){
00633 const int index8= scan8[n];
00634 const int left= h->intra4x4_pred_mode_cache[index8 - 1];
00635 const int top = h->intra4x4_pred_mode_cache[index8 - 8];
00636 const int min= FFMIN(left, top);
00637
00638 tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
00639
00640 if(min<0) return DC_PRED;
00641 else return min;
00642 }
00643
00644 static inline void write_back_non_zero_count(H264Context *h){
00645 MpegEncContext * const s = &h->s;
00646 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
00647
00648 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
00649 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
00650 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
00651 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
00652 h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
00653 h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
00654 h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
00655
00656 h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
00657 h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
00658 h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
00659
00660 h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
00661 h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
00662 h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
00663
00664 if(FRAME_MBAFF){
00665
00666 int v = 0, i;
00667 for(i=0; i<16; i++)
00668 v += (!!h->non_zero_count_cache[scan8[i]]) << i;
00669 *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
00670 }
00671 }
00672
00677 static inline int pred_non_zero_count(H264Context *h, int n){
00678 const int index8= scan8[n];
00679 const int left= h->non_zero_count_cache[index8 - 1];
00680 const int top = h->non_zero_count_cache[index8 - 8];
00681 int i= left + top;
00682
00683 if(i<64) i= (i+1)>>1;
00684
00685 tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
00686
00687 return i&31;
00688 }
00689
00690 static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
00691 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
00692 MpegEncContext *s = &h->s;
00693
00694
00695
00696 if(FRAME_MBAFF){
00697 const uint32_t *mb_types = s->current_picture_ptr->mb_type;
00698 const int16_t *mv;
00699 *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
00700 *C = h->mv_cache[list][scan8[0]-2];
00701
00702 if(!MB_FIELD
00703 && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
00704 int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
00705 if(IS_INTERLACED(mb_types[topright_xy])){
00706 #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
00707 const int x4 = X4, y4 = Y4;\
00708 const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
00709 if(!USES_LIST(mb_type,list))\
00710 return LIST_NOT_USED;\
00711 mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
00712 h->mv_cache[list][scan8[0]-2][0] = mv[0];\
00713 h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
00714 return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
00715
00716 SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
00717 }
00718 }
00719 if(topright_ref == PART_NOT_AVAILABLE
00720 && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
00721 && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
00722 if(!MB_FIELD
00723 && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
00724 SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
00725 }
00726 if(MB_FIELD
00727 && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
00728 && i >= scan8[0]+8){
00729
00730 SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
00731 }
00732 }
00733 #undef SET_DIAG_MV
00734 }
00735
00736 if(topright_ref != PART_NOT_AVAILABLE){
00737 *C= h->mv_cache[list][ i - 8 + part_width ];
00738 return topright_ref;
00739 }else{
00740 tprintf(s->avctx, "topright MV not available\n");
00741
00742 *C= h->mv_cache[list][ i - 8 - 1 ];
00743 return h->ref_cache[list][ i - 8 - 1 ];
00744 }
00745 }
00746
00754 static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
00755 const int index8= scan8[n];
00756 const int top_ref= h->ref_cache[list][ index8 - 8 ];
00757 const int left_ref= h->ref_cache[list][ index8 - 1 ];
00758 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
00759 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
00760 const int16_t * C;
00761 int diagonal_ref, match_count;
00762
00763 assert(part_width==1 || part_width==2 || part_width==4);
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
00774 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
00775 tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
00776 if(match_count > 1){
00777 *mx= mid_pred(A[0], B[0], C[0]);
00778 *my= mid_pred(A[1], B[1], C[1]);
00779 }else if(match_count==1){
00780 if(left_ref==ref){
00781 *mx= A[0];
00782 *my= A[1];
00783 }else if(top_ref==ref){
00784 *mx= B[0];
00785 *my= B[1];
00786 }else{
00787 *mx= C[0];
00788 *my= C[1];
00789 }
00790 }else{
00791 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
00792 *mx= A[0];
00793 *my= A[1];
00794 }else{
00795 *mx= mid_pred(A[0], B[0], C[0]);
00796 *my= mid_pred(A[1], B[1], C[1]);
00797 }
00798 }
00799
00800 tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
00801 }
00802
00809 static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
00810 if(n==0){
00811 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
00812 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
00813
00814 tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
00815
00816 if(top_ref == ref){
00817 *mx= B[0];
00818 *my= B[1];
00819 return;
00820 }
00821 }else{
00822 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
00823 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
00824
00825 tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
00826
00827 if(left_ref == ref){
00828 *mx= A[0];
00829 *my= A[1];
00830 return;
00831 }
00832 }
00833
00834
00835 pred_motion(h, n, 4, list, ref, mx, my);
00836 }
00837
00844 static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
00845 if(n==0){
00846 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
00847 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
00848
00849 tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
00850
00851 if(left_ref == ref){
00852 *mx= A[0];
00853 *my= A[1];
00854 return;
00855 }
00856 }else{
00857 const int16_t * C;
00858 int diagonal_ref;
00859
00860 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
00861
00862 tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
00863
00864 if(diagonal_ref == ref){
00865 *mx= C[0];
00866 *my= C[1];
00867 return;
00868 }
00869 }
00870
00871
00872 pred_motion(h, n, 2, list, ref, mx, my);
00873 }
00874
00875 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
00876 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
00877 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
00878
00879 tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
00880
00881 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
00882 || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
00883 || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
00884
00885 *mx = *my = 0;
00886 return;
00887 }
00888
00889 pred_motion(h, 0, 4, 0, 0, mx, my);
00890
00891 return;
00892 }
00893
00894 static inline void direct_dist_scale_factor(H264Context * const h){
00895 const int poc = h->s.current_picture_ptr->poc;
00896 const int poc1 = h->ref_list[1][0].poc;
00897 int i;
00898 for(i=0; i<h->ref_count[0]; i++){
00899 int poc0 = h->ref_list[0][i].poc;
00900 int td = av_clip(poc1 - poc0, -128, 127);
00901 if(td == 0 ){
00902 h->dist_scale_factor[i] = 256;
00903 }else{
00904 int tb = av_clip(poc - poc0, -128, 127);
00905 int tx = (16384 + (FFABS(td) >> 1)) / td;
00906 h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
00907 }
00908 }
00909 if(FRAME_MBAFF){
00910 for(i=0; i<h->ref_count[0]; i++){
00911 h->dist_scale_factor_field[2*i] =
00912 h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
00913 }
00914 }
00915 }
00916 static inline void direct_ref_list_init(H264Context * const h){
00917 MpegEncContext * const s = &h->s;
00918 Picture * const ref1 = &h->ref_list[1][0];
00919 Picture * const cur = s->current_picture_ptr;
00920 int list, i, j;
00921 if(cur->pict_type == I_TYPE)
00922 cur->ref_count[0] = 0;
00923 if(cur->pict_type != B_TYPE)
00924 cur->ref_count[1] = 0;
00925 for(list=0; list<2; list++){
00926 cur->ref_count[list] = h->ref_count[list];
00927 for(j=0; j<h->ref_count[list]; j++)
00928 cur->ref_poc[list][j] = h->ref_list[list][j].poc;
00929 }
00930 if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
00931 return;
00932 for(list=0; list<2; list++){
00933 for(i=0; i<ref1->ref_count[list]; i++){
00934 const int poc = ref1->ref_poc[list][i];
00935 h->map_col_to_list0[list][i] = 0;
00936 for(j=0; j<h->ref_count[list]; j++)
00937 if(h->ref_list[list][j].poc == poc){
00938 h->map_col_to_list0[list][i] = j;
00939 break;
00940 }
00941 }
00942 }
00943 if(FRAME_MBAFF){
00944 for(list=0; list<2; list++){
00945 for(i=0; i<ref1->ref_count[list]; i++){
00946 j = h->map_col_to_list0[list][i];
00947 h->map_col_to_list0_field[list][2*i] = 2*j;
00948 h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
00949 }
00950 }
00951 }
00952 }
00953
00954 static inline void pred_direct_motion(H264Context * const h, int *mb_type){
00955 MpegEncContext * const s = &h->s;
00956 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
00957 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
00958 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
00959 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
00960 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
00961 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
00962 const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
00963 const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
00964 const int is_b8x8 = IS_8X8(*mb_type);
00965 unsigned int sub_mb_type;
00966 int i8, i4;
00967
00968 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
00969 if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
00970
00971
00972 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00973 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
00974 }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
00975 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00976 *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00977 }else{
00978 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00979 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
00980 }
00981 if(!is_b8x8)
00982 *mb_type |= MB_TYPE_DIRECT2;
00983 if(MB_FIELD)
00984 *mb_type |= MB_TYPE_INTERLACED;
00985
00986 tprintf(s->avctx, "mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
00987
00988 if(h->direct_spatial_mv_pred){
00989 int ref[2];
00990 int mv[2][2];
00991 int list;
00992
00993
00994
00995
00996 for(list=0; list<2; list++){
00997 int refa = h->ref_cache[list][scan8[0] - 1];
00998 int refb = h->ref_cache[list][scan8[0] - 8];
00999 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
01000 if(refc == -2)
01001 refc = h->ref_cache[list][scan8[0] - 8 - 1];
01002 ref[list] = refa;
01003 if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
01004 ref[list] = refb;
01005 if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
01006 ref[list] = refc;
01007 if(ref[list] < 0)
01008 ref[list] = -1;
01009 }
01010
01011 if(ref[0] < 0 && ref[1] < 0){
01012 ref[0] = ref[1] = 0;
01013 mv[0][0] = mv[0][1] =
01014 mv[1][0] = mv[1][1] = 0;
01015 }else{
01016 for(list=0; list<2; list++){
01017 if(ref[list] >= 0)
01018 pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
01019 else
01020 mv[list][0] = mv[list][1] = 0;
01021 }
01022 }
01023
01024 if(ref[1] < 0){
01025 if(!is_b8x8)
01026 *mb_type &= ~MB_TYPE_L1;
01027 sub_mb_type &= ~MB_TYPE_L1;
01028 }else if(ref[0] < 0){
01029 if(!is_b8x8)
01030 *mb_type &= ~MB_TYPE_L0;
01031 sub_mb_type &= ~MB_TYPE_L0;
01032 }
01033
01034 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
01035 int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
01036 int mb_types_col[2];
01037 int b8_stride = h->b8_stride;
01038 int b4_stride = h->b_stride;
01039
01040 *mb_type = (*mb_type & ~MB_TYPE_16x16) | MB_TYPE_8x8;
01041
01042 if(IS_INTERLACED(*mb_type)){
01043 mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
01044 mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
01045 if(s->mb_y&1){
01046 l1ref0 -= 2*b8_stride;
01047 l1ref1 -= 2*b8_stride;
01048 l1mv0 -= 4*b4_stride;
01049 l1mv1 -= 4*b4_stride;
01050 }
01051 b8_stride *= 3;
01052 b4_stride *= 6;
01053 }else{
01054 int cur_poc = s->current_picture_ptr->poc;
01055 int *col_poc = h->ref_list[1]->field_poc;
01056 int col_parity = FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc);
01057 int dy = 2*col_parity - (s->mb_y&1);
01058 mb_types_col[0] =
01059 mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy + col_parity*s->mb_stride];
01060 l1ref0 += dy*b8_stride;
01061 l1ref1 += dy*b8_stride;
01062 l1mv0 += 2*dy*b4_stride;
01063 l1mv1 += 2*dy*b4_stride;
01064 b8_stride = 0;
01065 }
01066
01067 for(i8=0; i8<4; i8++){
01068 int x8 = i8&1;
01069 int y8 = i8>>1;
01070 int xy8 = x8+y8*b8_stride;
01071 int xy4 = 3*x8+y8*b4_stride;
01072 int a=0, b=0;
01073
01074 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
01075 continue;
01076 h->sub_mb_type[i8] = sub_mb_type;
01077
01078 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
01079 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
01080 if(!IS_INTRA(mb_types_col[y8])
01081 && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
01082 || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
01083 if(ref[0] > 0)
01084 a= pack16to32(mv[0][0],mv[0][1]);
01085 if(ref[1] > 0)
01086 b= pack16to32(mv[1][0],mv[1][1]);
01087 }else{
01088 a= pack16to32(mv[0][0],mv[0][1]);
01089 b= pack16to32(mv[1][0],mv[1][1]);
01090 }
01091 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
01092 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
01093 }
01094 }else if(IS_16X16(*mb_type)){
01095 int a=0, b=0;
01096
01097 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
01098 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
01099 if(!IS_INTRA(mb_type_col)
01100 && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
01101 || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
01102 && (h->x264_build>33 || !h->x264_build)))){
01103 if(ref[0] > 0)
01104 a= pack16to32(mv[0][0],mv[0][1]);
01105 if(ref[1] > 0)
01106 b= pack16to32(mv[1][0],mv[1][1]);
01107 }else{
01108 a= pack16to32(mv[0][0],mv[0][1]);
01109 b= pack16to32(mv[1][0],mv[1][1]);
01110 }
01111 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
01112 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
01113 }else{
01114 for(i8=0; i8<4; i8++){
01115 const int x8 = i8&1;
01116 const int y8 = i8>>1;
01117
01118 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
01119 continue;
01120 h->sub_mb_type[i8] = sub_mb_type;
01121
01122 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
01123 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
01124 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
01125 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
01126
01127
01128 if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
01129 || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
01130 && (h->x264_build>33 || !h->x264_build)))){
01131 const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
01132 if(IS_SUB_8X8(sub_mb_type)){
01133 const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
01134 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
01135 if(ref[0] == 0)
01136 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
01137 if(ref[1] == 0)
01138 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
01139 }
01140 }else
01141 for(i4=0; i4<4; i4++){
01142 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
01143 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
01144 if(ref[0] == 0)
01145 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
01146 if(ref[1] == 0)
01147 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
01148 }
01149 }
01150 }
01151 }
01152 }
01153 }else{
01154 const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
01155 const int *dist_scale_factor = h->dist_scale_factor;
01156
01157 if(FRAME_MBAFF){
01158 if(IS_INTERLACED(*mb_type)){
01159 map_col_to_list0[0] = h->map_col_to_list0_field[0];
01160 map_col_to_list0[1] = h->map_col_to_list0_field[1];
01161 dist_scale_factor = h->dist_scale_factor_field;
01162 }
01163 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
01164
01165 const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
01166 int mb_types_col[2];
01167 int y_shift;
01168
01169 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
01170 | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
01171 | (*mb_type & MB_TYPE_INTERLACED);
01172 sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
01173
01174 if(IS_INTERLACED(*mb_type)){
01175
01176 mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
01177 mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
01178 if(s->mb_y&1){
01179 l1ref0 -= 2*h->b8_stride;
01180 l1ref1 -= 2*h->b8_stride;
01181 l1mv0 -= 4*h->b_stride;
01182 l1mv1 -= 4*h->b_stride;
01183 }
01184 y_shift = 0;
01185
01186 if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
01187 && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
01188 && !is_b8x8)
01189 *mb_type |= MB_TYPE_16x8;
01190 else
01191 *mb_type |= MB_TYPE_8x8;
01192 }else{
01193
01194
01195
01196 int dy = (s->mb_y&1) ? 1 : 2;
01197 mb_types_col[0] =
01198 mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
01199 l1ref0 += dy*h->b8_stride;
01200 l1ref1 += dy*h->b8_stride;
01201 l1mv0 += 2*dy*h->b_stride;
01202 l1mv1 += 2*dy*h->b_stride;
01203 y_shift = 2;
01204
01205 if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
01206 && !is_b8x8)
01207 *mb_type |= MB_TYPE_16x16;
01208 else
01209 *mb_type |= MB_TYPE_8x8;
01210 }
01211
01212 for(i8=0; i8<4; i8++){
01213 const int x8 = i8&1;
01214 const int y8 = i8>>1;
01215 int ref0, scale;
01216 const int16_t (*l1mv)[2]= l1mv0;
01217
01218 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
01219 continue;
01220 h->sub_mb_type[i8] = sub_mb_type;
01221
01222 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
01223 if(IS_INTRA(mb_types_col[y8])){
01224 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
01225 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
01226 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
01227 continue;
01228 }
01229
01230 ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
01231 if(ref0 >= 0)
01232 ref0 = map_col_to_list0[0][ref0*2>>y_shift];
01233 else{
01234 ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
01235 l1mv= l1mv1;
01236 }
01237 scale = dist_scale_factor[ref0];
01238 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
01239
01240 {
01241 const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
01242 int my_col = (mv_col[1]<<y_shift)/2;
01243 int mx = (scale * mv_col[0] + 128) >> 8;
01244 int my = (scale * my_col + 128) >> 8;
01245 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
01246 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
01247 }
01248 }
01249 return;
01250 }
01251 }
01252
01253
01254
01255 if(IS_16X16(*mb_type)){
01256 int ref, mv0, mv1;
01257
01258 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
01259 if(IS_INTRA(mb_type_col)){
01260 ref=mv0=mv1=0;
01261 }else{
01262 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
01263 : map_col_to_list0[1][l1ref1[0]];
01264 const int scale = dist_scale_factor[ref0];
01265 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
01266 int mv_l0[2];
01267 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
01268 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
01269 ref= ref0;
01270 mv0= pack16to32(mv_l0[0],mv_l0[1]);
01271 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
01272 }
01273 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
01274 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
01275 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
01276 }else{
01277 for(i8=0; i8<4; i8++){
01278 const int x8 = i8&1;
01279 const int y8 = i8>>1;
01280 int ref0, scale;
01281 const int16_t (*l1mv)[2]= l1mv0;
01282
01283 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
01284 continue;
01285 h->sub_mb_type[i8] = sub_mb_type;
01286 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
01287 if(IS_INTRA(mb_type_col)){
01288 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
01289 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
01290 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
01291 continue;
01292 }
01293
01294 ref0 = l1ref0[x8 + y8*h->b8_stride];
01295 if(ref0 >= 0)
01296 ref0 = map_col_to_list0[0][ref0];
01297 else{
01298 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
01299 l1mv= l1mv1;
01300 }
01301 scale = dist_scale_factor[ref0];
01302
01303 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
01304 if(IS_SUB_8X8(sub_mb_type)){
01305 const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
01306 int mx = (scale * mv_col[0] + 128) >> 8;
01307 int my = (scale * mv_col[1] + 128) >> 8;
01308 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
01309 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
01310 }else
01311 for(i4=0; i4<4; i4++){
01312 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
01313 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
01314 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
01315 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
01316 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
01317 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
01318 }
01319 }
01320 }
01321 }
01322 }
01323
01324 static inline void write_back_motion(H264Context *h, int mb_type){
01325 MpegEncContext * const s = &h->s;
01326 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
01327 const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
01328 int list;
01329
01330 if(!USES_LIST(mb_type, 0))
01331 fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
01332
01333 for(list=0; list<h->list_count; list++){
01334 int y;
01335 if(!USES_LIST(mb_type, list))
01336 continue;
01337
01338 for(y=0; y<4; y++){
01339 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
01340 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
01341 }
01342 if( h->pps.cabac ) {
01343 if(IS_SKIP(mb_type))
01344 fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
01345 else
01346 for(y=0; y<4; y++){
01347 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
01348 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
01349 }
01350 }
01351
01352 {
01353 int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
01354 ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
01355 ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
01356 ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
01357 ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
01358 }
01359 }
01360
01361 if(h->slice_type == B_TYPE && h->pps.cabac){
01362 if(IS_8X8(mb_type)){
01363 uint8_t *direct_table = &h->direct_table[b8_xy];
01364 direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
01365 direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
01366 direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
01367 }
01368 }
01369 }
01370
01378 static const uint8_t *decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
01379 int i, si, di;
01380 uint8_t *dst;
01381 int bufidx;
01382
01383
01384 h->nal_ref_idc= src[0]>>5;
01385 h->nal_unit_type= src[0]&0x1F;
01386
01387 src++; length--;
01388 #if 0
01389 for(i=0; i<length; i++)
01390 printf("%2X ", src[i]);
01391 #endif
01392 for(i=0; i+1<length; i+=2){
01393 if(src[i]) continue;
01394 if(i>0 && src[i-1]==0) i--;
01395 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
01396 if(src[i+2]!=3){
01397
01398 length=i;
01399 }
01400 break;
01401 }
01402 }
01403
01404 if(i>=length-1){
01405 *dst_length= length;
01406 *consumed= length+1;
01407 return src;
01408 }
01409
01410 bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
01411 h->rbsp_buffer[bufidx]= av_fast_realloc(h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length);
01412 dst= h->rbsp_buffer[bufidx];
01413
01414 if (dst == NULL){
01415 return NULL;
01416 }
01417
01418
01419 si=di=0;
01420 while(si<length){
01421
01422 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
01423 if(src[si+2]==3){
01424 dst[di++]= 0;
01425 dst[di++]= 0;
01426 si+=3;
01427 continue;
01428 }else
01429 break;
01430 }
01431
01432 dst[di++]= src[si++];
01433 }
01434
01435 *dst_length= di;
01436 *consumed= si + 1;
01437
01438 return dst;
01439 }
01440
01445 static int decode_rbsp_trailing(H264Context *h, const uint8_t *src){
01446 int v= *src;
01447 int r;
01448
01449 tprintf(h->s.avctx, "rbsp trailing %X\n", v);
01450
01451 for(r=1; r<9; r++){
01452 if(v&1) return r;
01453 v>>=1;
01454 }
01455 return 0;
01456 }
01457
01462 static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
01463 #define stride 16
01464 int i;
01465 int temp[16];
01466 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
01467 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
01468
01469
01470
01471 for(i=0; i<4; i++){
01472 const int offset= y_offset[i];
01473 const int z0= block[offset+stride*0] + block[offset+stride*4];
01474 const int z1= block[offset+stride*0] - block[offset+stride*4];
01475 const int z2= block[offset+stride*1] - block[offset+stride*5];
01476 const int z3= block[offset+stride*1] + block[offset+stride*5];
01477
01478 temp[4*i+0]= z0+z3;
01479 temp[4*i+1]= z1+z2;
01480 temp[4*i+2]= z1-z2;
01481 temp[4*i+3]= z0-z3;
01482 }
01483
01484 for(i=0; i<4; i++){
01485 const int offset= x_offset[i];
01486 const int z0= temp[4*0+i] + temp[4*2+i];
01487 const int z1= temp[4*0+i] - temp[4*2+i];
01488 const int z2= temp[4*1+i] - temp[4*3+i];
01489 const int z3= temp[4*1+i] + temp[4*3+i];
01490
01491 block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8));
01492 block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
01493 block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
01494 block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
01495 }
01496 }
01497
01498 #if 0
01499
01503 static void h264_luma_dc_dct_c(DCTELEM *block){
01504
01505 int i;
01506 int temp[16];
01507 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
01508 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
01509
01510 for(i=0; i<4; i++){
01511 const int offset= y_offset[i];
01512 const int z0= block[offset+stride*0] + block[offset+stride*4];
01513 const int z1= block[offset+stride*0] - block[offset+stride*4];
01514 const int z2= block[offset+stride*1] - block[offset+stride*5];
01515 const int z3= block[offset+stride*1] + block[offset+stride*5];
01516
01517 temp[4*i+0]= z0+z3;
01518 temp[4*i+1]= z1+z2;
01519 temp[4*i+2]= z1-z2;
01520 temp[4*i+3]= z0-z3;
01521 }
01522
01523 for(i=0; i<4; i++){
01524 const int offset= x_offset[i];
01525 const int z0= temp[4*0+i] + temp[4*2+i];
01526 const int z1= temp[4*0+i] - temp[4*2+i];
01527 const int z2= temp[4*1+i] - temp[4*3+i];
01528 const int z3= temp[4*1+i] + temp[4*3+i];
01529
01530 block[stride*0 +offset]= (z0 + z3)>>1;
01531 block[stride*2 +offset]= (z1 + z2)>>1;
01532 block[stride*8 +offset]= (z1 - z2)>>1;
01533 block[stride*10+offset]= (z0 - z3)>>1;
01534 }
01535 }
01536 #endif
01537
01538 #undef xStride
01539 #undef stride
01540
01541 static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
01542 const int stride= 16*2;
01543 const int xStride= 16;
01544 int a,b,c,d,e;
01545
01546 a= block[stride*0 + xStride*0];
01547 b= block[stride*0 + xStride*1];
01548 c= block[stride*1 + xStride*0];
01549 d= block[stride*1 + xStride*1];
01550
01551 e= a-b;
01552 a= a+b;
01553 b= c-d;
01554 c= c+d;
01555
01556 block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
01557 block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
01558 block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
01559 block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
01560 }
01561
01562 #if 0
01563 static void chroma_dc_dct_c(DCTELEM *block){
01564 const int stride= 16*2;
01565 const int xStride= 16;
01566 int a,b,c,d,e;
01567
01568 a= block[stride*0 + xStride*0];
01569 b= block[stride*0 + xStride*1];
01570 c= block[stride*1 + xStride*0];
01571 d= block[stride*1 + xStride*1];
01572
01573 e= a-b;
01574 a= a+b;
01575 b= c-d;
01576 c= c+d;
01577
01578 block[stride*0 + xStride*0]= (a+c);
01579 block[stride*0 + xStride*1]= (e+b);
01580 block[stride*1 + xStride*0]= (a-c);
01581 block[stride*1 + xStride*1]= (e-b);
01582 }
01583 #endif
01584
01588 static inline int get_chroma_qp(H264Context *h, int t, int qscale){
01589 return h->pps.chroma_qp_table[t][qscale & 0xff];
01590 }
01591
01592
01593
01594 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
01595 int i;
01596 const int * const quant_table= quant_coeff[qscale];
01597 const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
01598 const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
01599 const unsigned int threshold2= (threshold1<<1);
01600 int last_non_zero;
01601
01602 if(separate_dc){
01603 if(qscale<=18){
01604
01605 const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
01606 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
01607 const unsigned int dc_threshold2= (dc_threshold1<<1);
01608
01609 int level= block[0]*quant_coeff[qscale+18][0];
01610 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
01611 if(level>0){
01612 level= (dc_bias + level)>>(QUANT_SHIFT-2);
01613 block[0]= level;
01614 }else{
01615 level= (dc_bias - level)>>(QUANT_SHIFT-2);
01616 block[0]= -level;
01617 }
01618
01619 }else{
01620 block[0]=0;
01621 }
01622 }else{
01623 const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
01624 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
01625 const unsigned int dc_threshold2= (dc_threshold1<<1);
01626
01627 int level= block[0]*quant_table[0];
01628 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
01629 if(level>0){
01630 level= (dc_bias + level)>>(QUANT_SHIFT+1);
01631 block[0]= level;
01632 }else{
01633 level= (dc_bias - level)>>(QUANT_SHIFT+1);
01634 block[0]= -level;
01635 }
01636
01637 }else{
01638 block[0]=0;
01639 }
01640 }
01641 last_non_zero= 0;
01642 i=1;
01643 }else{
01644 last_non_zero= -1;
01645 i=0;
01646 }
01647
01648 for(; i<16; i++){
01649 const int j= scantable[i];
01650 int level= block[j]*quant_table[j];
01651
01652
01653
01654 if(((unsigned)(level+threshold1))>threshold2){
01655 if(level>0){
01656 level= (bias + level)>>QUANT_SHIFT;
01657 block[j]= level;
01658 }else{
01659 level= (bias - level)>>QUANT_SHIFT;
01660 block[j]= -level;
01661 }
01662 last_non_zero = i;
01663 }else{
01664 block[j]=0;
01665 }
01666 }
01667
01668 return last_non_zero;
01669 }
01670
01671 static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
01672 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
01673 int src_x_offset, int src_y_offset,
01674 qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
01675 MpegEncContext * const s = &h->s;
01676 const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
01677 int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
01678 const int luma_xy= (mx&3) + ((my&3)<<2);
01679 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
01680 uint8_t * src_cb, * src_cr;
01681 int extra_width= h->emu_edge_width;
01682 int extra_height= h->emu_edge_height;
01683 int emu=0;
01684 const int full_mx= mx>>2;
01685 const int full_my= my>>2;
01686 const int pic_width = 16*s->mb_width;
01687 const int pic_height = 16*s->mb_height >> MB_FIELD;
01688
01689 if(!pic->data[0])
01690 return;
01691
01692 if(mx&7) extra_width -= 3;
01693 if(my&7) extra_height -= 3;
01694
01695 if( full_mx < 0-extra_width
01696 || full_my < 0-extra_height
01697 || full_mx + 16 > pic_width + extra_width
01698 || full_my + 16 > pic_height + extra_height){
01699 ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5, full_mx-2, full_my-2, pic_width, pic_height);
01700 src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
01701 emu=1;
01702 }
01703
01704 qpix_op[luma_xy](dest_y, src_y, h->mb_linesize);
01705 if(!square){
01706 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
01707 }
01708
01709 if(ENABLE_GRAY && s->flags&CODEC_FLAG_GRAY) return;
01710
01711 if(MB_FIELD){
01712
01713 my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
01714 emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
01715 }
01716 src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
01717 src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
01718
01719 if(emu){
01720 ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
01721 src_cb= s->edge_emu_buffer;
01722 }
01723 chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
01724
01725 if(emu){
01726 ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
01727 src_cr= s->edge_emu_buffer;
01728 }
01729 chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
01730 }
01731
01732 static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
01733 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
01734 int x_offset, int y_offset,
01735 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
01736 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
01737 int list0, int list1){
01738 MpegEncContext * const s = &h->s;
01739 qpel_mc_func *qpix_op= qpix_put;
01740 h264_chroma_mc_func chroma_op= chroma_put;
01741
01742 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
01743 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
01744 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
01745 x_offset += 8*s->mb_x;
01746 y_offset += 8*(s->mb_y >> MB_FIELD);
01747
01748 if(list0){
01749 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
01750 mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
01751 dest_y, dest_cb, dest_cr, x_offset, y_offset,
01752 qpix_op, chroma_op);
01753
01754 qpix_op= qpix_avg;
01755 chroma_op= chroma_avg;
01756 }
01757
01758 if(list1){
01759 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
01760 mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
01761 dest_y, dest_cb, dest_cr, x_offset, y_offset,
01762 qpix_op, chroma_op);
01763 }
01764 }
01765
01766 static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
01767 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
01768 int x_offset, int y_offset,
01769 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
01770 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
01771 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
01772 int list0, int list1){
01773 MpegEncContext * const s = &h->s;
01774
01775 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
01776 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
01777 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
01778 x_offset += 8*s->mb_x;
01779 y_offset += 8*(s->mb_y >> MB_FIELD);
01780
01781 if(list0 && list1){
01782
01783
01784 uint8_t *tmp_cb = s->obmc_scratchpad;
01785 uint8_t *tmp_cr = s->obmc_scratchpad + 8;
01786 uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
01787 int refn0 = h->ref_cache[0][ scan8[n] ];
01788 int refn1 = h->ref_cache[1][ scan8[n] ];
01789
01790 mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
01791 dest_y, dest_cb, dest_cr,
01792 x_offset, y_offset, qpix_put, chroma_put);
01793 mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
01794 tmp_y, tmp_cb, tmp_cr,
01795 x_offset, y_offset, qpix_put, chroma_put);
01796
01797 if(h->use_weight == 2){
01798 int weight0 = h->implicit_weight[refn0][refn1];
01799 int weight1 = 64 - weight0;
01800 luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
01801 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
01802 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
01803 }else{
01804 luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
01805 h->luma_weight[0][refn0], h->luma_weight[1][refn1],
01806 h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
01807 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
01808 h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
01809 h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
01810 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
01811 h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
01812 h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
01813 }
01814 }else{
01815 int list = list1 ? 1 : 0;
01816 int refn = h->ref_cache[list][ scan8[n] ];
01817 Picture *ref= &h->ref_list[list][refn];
01818 mc_dir_part(h, ref, n, square, chroma_height, delta, list,
01819 dest_y, dest_cb, dest_cr, x_offset, y_offset,
01820 qpix_put, chroma_put);
01821
01822 luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
01823 h->luma_weight[list][refn], h->luma_offset[list][refn]);
01824 if(h->use_weight_chroma){
01825 chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
01826 h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
01827 chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
01828 h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
01829 }
01830 }
01831 }
01832
01833 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
01834 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
01835 int x_offset, int y_offset,
01836 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
01837 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
01838 h264_weight_func *weight_op, h264_biweight_func *weight_avg,
01839 int list0, int list1){
01840 if((h->use_weight==2 && list0 && list1
01841 && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
01842 || h->use_weight==1)
01843 mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
01844 x_offset, y_offset, qpix_put, chroma_put,
01845 weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
01846 else
01847 mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
01848 x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
01849 }
01850
01851 static inline void prefetch_motion(H264Context *h, int list){
01852
01853
01854 MpegEncContext * const s = &h->s;
01855 const int refn = h->ref_cache[list][scan8[0]];
01856 if(refn >= 0){
01857 const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
01858 const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
01859 uint8_t **src= h->ref_list[list][refn].data;
01860 int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
01861 s->dsp.prefetch(src[0]+off, s->linesize, 4);
01862 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
01863 s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
01864 }
01865 }
01866
01867 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
01868 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
01869 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
01870 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
01871 MpegEncContext * const s = &h->s;
01872 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
01873 const int mb_type= s->current_picture.mb_type[mb_xy];
01874
01875 assert(IS_INTER(mb_type));
01876
01877 prefetch_motion(h, 0);
01878
01879 if(IS_16X16(mb_type)){
01880 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
01881 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
01882 &weight_op[0], &weight_avg[0],
01883 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
01884 }else if(IS_16X8(mb_type)){
01885 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
01886 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
01887 &weight_op[1], &weight_avg[1],
01888 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
01889 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
01890 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
01891 &weight_op[1], &weight_avg[1],
01892 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
01893 }else if(IS_8X16(mb_type)){
01894 mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
01895 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
01896 &weight_op[2], &weight_avg[2],
01897 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
01898 mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
01899 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
01900 &weight_op[2], &weight_avg[2],
01901 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
01902 }else{
01903 int i;
01904
01905 assert(IS_8X8(mb_type));
01906
01907 for(i=0; i<4; i++){
01908 const int sub_mb_type= h->sub_mb_type[i];
01909 const int n= 4*i;
01910 int x_offset= (i&1)<<2;
01911 int y_offset= (i&2)<<1;
01912
01913 if(IS_SUB_8X8(sub_mb_type)){
01914 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
01915 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
01916 &weight_op[3], &weight_avg[3],
01917 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
01918 }else if(IS_SUB_8X4(sub_mb_type)){
01919 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
01920 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
01921 &weight_op[4], &weight_avg[4],
01922 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
01923 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
01924 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
01925 &weight_op[4], &weight_avg[4],
01926 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
01927 }else if(IS_SUB_4X8(sub_mb_type)){
01928 mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
01929 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
01930 &weight_op[5], &weight_avg[5],
01931 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
01932 mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
01933 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
01934 &weight_op[5], &weight_avg[5],
01935 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
01936 }else{
01937 int j;
01938 assert(IS_SUB_4X4(sub_mb_type));
01939 for(j=0; j<4; j++){
01940 int sub_x_offset= x_offset + 2*(j&1);
01941 int sub_y_offset= y_offset + (j&2);
01942 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
01943 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
01944 &weight_op[6], &weight_avg[6],
01945 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
01946 }
01947 }
01948 }
01949 }
01950
01951 prefetch_motion(h, 1);
01952 }
01953
01954 static void decode_init_vlc(void){
01955 static int done = 0;
01956
01957 if (!done) {
01958 int i;
01959 done = 1;
01960
01961 init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
01962 &chroma_dc_coeff_token_len [0], 1, 1,
01963 &chroma_dc_coeff_token_bits[0], 1, 1, 1);
01964
01965 for(i=0; i<4; i++){
01966 init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
01967 &coeff_token_len [i][0], 1, 1,
01968 &coeff_token_bits[i][0], 1, 1, 1);
01969 }
01970
01971 for(i=0; i<3; i++){
01972 init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
01973 &chroma_dc_total_zeros_len [i][0], 1, 1,
01974 &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
01975 }
01976 for(i=0; i<15; i++){
01977 init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
01978 &total_zeros_len [i][0], 1, 1,
01979 &total_zeros_bits[i][0], 1, 1, 1);
01980 }
01981
01982 for(i=0; i<6; i++){
01983 init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
01984 &run_len [i][0], 1, 1,
01985 &run_bits[i][0], 1, 1, 1);
01986 }
01987 init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
01988 &run_len [6][0], 1, 1,
01989 &run_bits[6][0], 1, 1, 1);
01990 }
01991 }
01992
01993 static void free_tables(H264Context *h){
01994 int i;
01995 H264Context *hx;
01996 av_freep(&h->intra4x4_pred_mode);
01997 av_freep(&h->chroma_pred_mode_table);
01998 av_freep(&h->cbp_table);
01999 av_freep(&h->mvd_table[0]);
02000 av_freep(&h->mvd_table[1]);
02001 av_freep(&h->direct_table);
02002 av_freep(&h->non_zero_count);
02003 av_freep(&h->slice_table_base);
02004 h->slice_table= NULL;
02005
02006 av_freep(&h->mb2b_xy);
02007 av_freep(&h->mb2b8_xy);
02008
02009 for(i = 0; i < MAX_SPS_COUNT; i++)
02010 av_freep(h->sps_buffers + i);
02011
02012 for(i = 0; i < MAX_PPS_COUNT; i++)
02013 av_freep(h->pps_buffers + i);
02014
02015 for(i = 0; i < h->s.avctx->thread_count; i++) {
02016 hx = h->thread_context[i];
02017 if(!hx) continue;
02018 av_freep(&hx->top_borders[1]);
02019 av_freep(&hx->top_borders[0]);
02020 av_freep(&hx->s.obmc_scratchpad);
02021 }
02022 }
02023
02024 static void init_dequant8_coeff_table(H264Context *h){
02025 int i,q,x;
02026 const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c);
02027 h->dequant8_coeff[0] = h->dequant8_buffer[0];
02028 h->dequant8_coeff[1] = h->dequant8_buffer[1];
02029
02030 for(i=0; i<2; i++ ){
02031 if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
02032 h->dequant8_coeff[1] = h->dequant8_buffer[0];
02033 break;
02034 }
02035
02036 for(q=0; q<52; q++){
02037 int shift = ff_div6[q];
02038 int idx = ff_rem6[q];
02039 for(x=0; x<64; x++)
02040 h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
02041 ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
02042 h->pps.scaling_matrix8[i][x]) << shift;
02043 }
02044 }
02045 }
02046
02047 static void init_dequant4_coeff_table(H264Context *h){
02048 int i,j,q,x;
02049 const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c);
02050 for(i=0; i<6; i++ ){
02051 h->dequant4_coeff[i] = h->dequant4_buffer[i];
02052 for(j=0; j<i; j++){
02053 if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
02054 h->dequant4_coeff[i] = h->dequant4_buffer[j];
02055 break;
02056 }
02057 }
02058 if(j<i)
02059 continue;
02060
02061 for(q=0; q<52; q++){
02062 int shift = ff_div6[q] + 2;
02063 int idx = ff_rem6[q];
02064 for(x=0; x<16; x++)
02065 h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
02066 ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
02067 h->pps.scaling_matrix4[i][x]) << shift;
02068 }
02069 }
02070 }
02071
02072 static void init_dequant_tables(H264Context *h){
02073 int i,x;
02074 init_dequant4_coeff_table(h);
02075 if(h->pps.transform_8x8_mode)
02076 init_dequant8_coeff_table(h);
02077 if(h->sps.transform_bypass){
02078 for(i=0; i<6; i++)
02079 for(x=0; x<16; x++)
02080 h->dequant4_coeff[i][0][x] = 1<<6;
02081 if(h->pps.transform_8x8_mode)
02082 for(i=0; i<2; i++)
02083 for(x=0; x<64; x++)
02084 h->dequant8_coeff[i][0][x] = 1<<6;
02085 }
02086 }
02087
02088
02093 static int alloc_tables(H264Context *h){
02094 MpegEncContext * const s = &h->s;
02095 const int big_mb_num= s->mb_stride * (s->mb_height+1);
02096 int x,y;
02097
02098 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
02099
02100 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
02101 CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
02102 CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
02103
02104 CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
02105 CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
02106 CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
02107 CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
02108
02109 memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
02110 h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
02111
02112 CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
02113 CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
02114 for(y=0; y<s->mb_height; y++){
02115 for(x=0; x<s->mb_width; x++){
02116 const int mb_xy= x + y*s->mb_stride;
02117 const int b_xy = 4*x + 4*y*h->b_stride;
02118 const int b8_xy= 2*x + 2*y*h->b8_stride;
02119
02120 h->mb2b_xy [mb_xy]= b_xy;
02121 h->mb2b8_xy[mb_xy]= b8_xy;
02122 }
02123 }
02124
02125 s->obmc_scratchpad = NULL;
02126
02127 if(!h->dequant4_coeff[0])
02128 init_dequant_tables(h);
02129
02130 return 0;
02131 fail:
02132 free_tables(h);
02133 return -1;
02134 }
02135
02139 static void clone_tables(H264Context *dst, H264Context *src){
02140 dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
02141 dst->non_zero_count = src->non_zero_count;
02142 dst->slice_table = src->slice_table;
02143 dst->cbp_table = src->cbp_table;
02144 dst->mb2b_xy = src->mb2b_xy;
02145 dst->mb2b8_xy = src->mb2b8_xy;
02146 dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
02147 dst->mvd_table[0] = src->mvd_table[0];
02148 dst->mvd_table[1] = src->mvd_table[1];
02149 dst->direct_table = src->direct_table;
02150
02151 dst->s.obmc_scratchpad = NULL;
02152 ff_h264_pred_init(&dst->hpc, src->s.codec_id);
02153 }
02154
02159 static int context_init(H264Context *h){
02160 CHECKED_ALLOCZ(h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
02161 CHECKED_ALLOCZ(h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
02162
02163 return 0;
02164 fail:
02165 return -1;
02166 }
02167
02168 static void common_init(H264Context *h){
02169 MpegEncContext * const s = &h->s;
02170
02171 s->width = s->avctx->width;
02172 s->height = s->avctx->height;
02173 s->codec_id= s->avctx->codec->id;
02174
02175 ff_h264_pred_init(&h->hpc, s->codec_id);
02176
02177 h->dequant_coeff_pps= -1;
02178 s->unrestricted_mv=1;
02179 s->decode=1;
02180
02181 memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
02182 memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
02183 }
02184
02185 static int decode_init(AVCodecContext *avctx){
02186 H264Context *h= avctx->priv_data;
02187 MpegEncContext * const s = &h->s;
02188
02189 MPV_decode_defaults(s);
02190
02191 s->avctx = avctx;
02192 common_init(h);
02193
02194 s->out_format = FMT_H264;
02195 s->workaround_bugs= avctx->workaround_bugs;
02196
02197
02198
02199 s->quarter_sample = 1;
02200 s->low_delay= 1;
02201 avctx->pix_fmt= PIX_FMT_YUV420P;
02202
02203 decode_init_vlc();
02204
02205 if(avctx->extradata_size > 0 && avctx->extradata &&
02206 *(char *)avctx->extradata == 1){
02207 h->is_avc = 1;
02208 h->got_avcC = 0;
02209 } else {
02210 h->is_avc = 0;
02211 }
02212
02213 h->thread_context[0] = h;
02214 return 0;
02215 }
02216
02217 static int frame_start(H264Context *h){
02218 MpegEncContext * const s = &h->s;
02219 int i;
02220
02221 if(MPV_frame_start(s, s->avctx) < 0)
02222 return -1;
02223 ff_er_frame_start(s);
02224
02225
02226
02227
02228
02229
02230 s->current_picture_ptr->key_frame= 0;
02231
02232 assert(s->linesize && s->uvlinesize);
02233
02234 for(i=0; i<16; i++){
02235 h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
02236 h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
02237 }
02238 for(i=0; i<4; i++){
02239 h->block_offset[16+i]=
02240 h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
02241 h->block_offset[24+16+i]=
02242 h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
02243 }
02244
02245
02246
02247 for(i = 0; i < s->avctx->thread_count; i++)
02248 if(!h->thread_context[i]->s.obmc_scratchpad)
02249 h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
02250
02251
02252 if(FRAME_MBAFF || s->avctx->thread_count > 1)
02253 memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
02254
02255
02256 return 0;
02257 }
02258
02259 static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
02260 MpegEncContext * const s = &h->s;
02261 int i;
02262
02263 src_y -= linesize;
02264 src_cb -= uvlinesize;
02265 src_cr -= uvlinesize;
02266
02267
02268
02269 h->left_border[0]= h->top_borders[0][s->mb_x][15];
02270 for(i=1; i<17; i++){
02271 h->left_border[i]= src_y[15+i* linesize];
02272 }
02273
02274 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
02275 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
02276
02277 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02278 h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
02279 h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
02280 for(i=1; i<9; i++){
02281 h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
02282 h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
02283 }
02284 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
02285 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
02286 }
02287 }
02288
02289 static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
02290 MpegEncContext * const s = &h->s;
02291 int temp8, i;
02292 uint64_t temp64;
02293 int deblock_left;
02294 int deblock_top;
02295 int mb_xy;
02296
02297 if(h->deblocking_filter == 2) {
02298 mb_xy = s->mb_x + s->mb_y*s->mb_stride;
02299 deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
02300 deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
02301 } else {
02302 deblock_left = (s->mb_x > 0);
02303 deblock_top = (s->mb_y > 0);
02304 }
02305
02306 src_y -= linesize + 1;
02307 src_cb -= uvlinesize + 1;
02308 src_cr -= uvlinesize + 1;
02309
02310 #define XCHG(a,b,t,xchg)\
02311 t= a;\
02312 if(xchg)\
02313 a= b;\
02314 b= t;
02315
02316 if(deblock_left){
02317 for(i = !deblock_top; i<17; i++){
02318 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
02319 }
02320 }
02321
02322 if(deblock_top){
02323 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
02324 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
02325 if(s->mb_x+1 < s->mb_width){
02326 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
02327 }
02328 }
02329
02330 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02331 if(deblock_left){
02332 for(i = !deblock_top; i<9; i++){
02333 XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
02334 XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
02335 }
02336 }
02337 if(deblock_top){
02338 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
02339 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
02340 }
02341 }
02342 }
02343
02344 static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
02345 MpegEncContext * const s = &h->s;
02346 int i;
02347
02348 src_y -= 2 * linesize;
02349 src_cb -= 2 * uvlinesize;
02350 src_cr -= 2 * uvlinesize;
02351
02352
02353
02354 h->left_border[0]= h->top_borders[0][s->mb_x][15];
02355 h->left_border[1]= h->top_borders[1][s->mb_x][15];
02356 for(i=2; i<34; i++){
02357 h->left_border[i]= src_y[15+i* linesize];
02358 }
02359
02360 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
02361 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
02362 *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
02363 *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
02364
02365 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02366 h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
02367 h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
02368 h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
02369 h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
02370 for(i=2; i<18; i++){
02371 h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
02372 h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
02373 }
02374 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
02375 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
02376 *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
02377 *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
02378 }
02379 }
02380
02381 static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
02382 MpegEncContext * const s = &h->s;
02383 int temp8, i;
02384 uint64_t temp64;
02385 int deblock_left = (s->mb_x > 0);
02386 int deblock_top = (s->mb_y > 1);
02387
02388 tprintf(s->avctx, "xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
02389
02390 src_y -= 2 * linesize + 1;
02391 src_cb -= 2 * uvlinesize + 1;
02392 src_cr -= 2 * uvlinesize + 1;
02393
02394 #define XCHG(a,b,t,xchg)\
02395 t= a;\
02396 if(xchg)\
02397 a= b;\
02398 b= t;
02399
02400 if(deblock_left){
02401 for(i = (!deblock_top)<<1; i<34; i++){
02402 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
02403 }
02404 }
02405
02406 if(deblock_top){
02407 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
02408 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
02409 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
02410 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
02411 if(s->mb_x+1 < s->mb_width){
02412 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
02413 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
02414 }
02415 }
02416
02417 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02418 if(deblock_left){
02419 for(i = (!deblock_top) << 1; i<18; i++){
02420 XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
02421 XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
02422 }
02423 }
02424 if(deblock_top){
02425 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
02426 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
02427 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
02428 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
02429 }
02430 }
02431 }
02432
02433 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
02434 MpegEncContext * const s = &h->s;
02435 const int mb_x= s->mb_x;
02436 const int mb_y= s->mb_y;
02437 const int mb_xy= mb_x + mb_y*s->mb_stride;
02438 const int mb_type= s->current_picture.mb_type[mb_xy];
02439 uint8_t *dest_y, *dest_cb, *dest_cr;
02440 int linesize, uvlinesize ;
02441 int i;
02442 int *block_offset = &h->block_offset[0];
02443 const unsigned int bottom = mb_y & 1;
02444 const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
02445 void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
02446 void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
02447
02448 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
02449 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
02450 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
02451
02452 s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
02453 s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
02454
02455 if (!simple && MB_FIELD) {
02456 linesize = h->mb_linesize = s->linesize * 2;
02457 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
02458 block_offset = &h->block_offset[24];
02459 if(mb_y&1){
02460 dest_y -= s->linesize*15;
02461 dest_cb-= s->uvlinesize*7;
02462 dest_cr-= s->uvlinesize*7;
02463 }
02464 if(FRAME_MBAFF) {
02465 int list;
02466 for(list=0; list<h->list_count; list++){
02467 if(!USES_LIST(mb_type, list))
02468 continue;
02469 if(IS_16X16(mb_type)){
02470 int8_t *ref = &h->ref_cache[list][scan8[0]];
02471 fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
02472 }else{
02473 for(i=0; i<16; i+=4){
02474
02475 int ref = h->ref_cache[list][scan8[i]];
02476 if(ref >= 0)
02477 fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
02478 }
02479 }
02480 }
02481 }
02482 } else {
02483 linesize = h->mb_linesize = s->linesize;
02484 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
02485
02486 }
02487
02488 if(transform_bypass){
02489 idct_dc_add =
02490 idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
02491 }else if(IS_8x8DCT(mb_type)){
02492 idct_dc_add = s->dsp.h264_idct8_dc_add;
02493 idct_add = s->dsp.h264_idct8_add;
02494 }else{
02495 idct_dc_add = s->dsp.h264_idct_dc_add;
02496 idct_add = s->dsp.h264_idct_add;
02497 }
02498
02499 if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
02500 && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
02501 int mbt_y = mb_y&~1;
02502 uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
02503 uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
02504 uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
02505 xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
02506 }
02507
02508 if (!simple && IS_INTRA_PCM(mb_type)) {
02509 unsigned int x, y;
02510
02511
02512
02513 for(i=0; i<16; i++) {
02514 for (y=0; y<4; y++) {
02515 for (x=0; x<4; x++) {
02516 *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
02517 }
02518 }
02519 }
02520 for(i=16; i<16+4; i++) {
02521 for (y=0; y<4; y++) {
02522 for (x=0; x<4; x++) {
02523 *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
02524 }
02525 }
02526 }
02527 for(i=20; i<20+4; i++) {
02528 for (y=0; y<4; y++) {
02529 for (x=0; x<4; x++) {
02530 *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
02531 }
02532 }
02533 }
02534 } else {
02535 if(IS_INTRA(mb_type)){
02536 if(h->deblocking_filter && (simple || !FRAME_MBAFF))
02537 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
02538
02539 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02540 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
02541 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
02542 }
02543
02544 if(IS_INTRA4x4(mb_type)){
02545 if(simple || !s->encoding){
02546 if(IS_8x8DCT(mb_type)){
02547 for(i=0; i<16; i+=4){
02548 uint8_t * const ptr= dest_y + block_offset[i];
02549 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
02550 const int nnz = h->non_zero_count_cache[ scan8[i] ];
02551 h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
02552 (h->topright_samples_available<<i)&0x4000, linesize);
02553 if(nnz){
02554 if(nnz == 1 && h->mb[i*16])
02555 idct_dc_add(ptr, h->mb + i*16, linesize);
02556 else
02557 idct_add(ptr, h->mb + i*16, linesize);
02558 }
02559 }
02560 }else
02561 for(i=0; i<16; i++){
02562 uint8_t * const ptr= dest_y + block_offset[i];
02563 uint8_t *topright;
02564 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
02565 int nnz, tr;
02566
02567 if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
02568 const int topright_avail= (h->topright_samples_available<<i)&0x8000;
02569 assert(mb_y || linesize <= block_offset[i]);
02570 if(!topright_avail){
02571 tr= ptr[3 - linesize]*0x01010101;
02572 topright= (uint8_t*) &tr;
02573 }else
02574 topright= ptr + 4 - linesize;
02575 }else
02576 topright= NULL;
02577
02578 h->hpc.pred4x4[ dir ](ptr, topright, linesize);
02579 nnz = h->non_zero_count_cache[ scan8[i] ];
02580 if(nnz){
02581 if(is_h264){
02582 if(nnz == 1 && h->mb[i*16])
02583 idct_dc_add(ptr, h->mb + i*16, linesize);
02584 else
02585 idct_add(ptr, h->mb + i*16, linesize);
02586 }else
02587 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
02588 }
02589 }
02590 }
02591 }else{
02592 h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
02593 if(is_h264){
02594 if(!transform_bypass)
02595 h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
02596 }else
02597 svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
02598 }
02599 if(h->deblocking_filter && (simple || !FRAME_MBAFF))
02600 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
02601 }else if(is_h264){
02602 hl_motion(h, dest_y, dest_cb, dest_cr,
02603 s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
02604 s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
02605 s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
02606 }
02607
02608
02609 if(!IS_INTRA4x4(mb_type)){
02610 if(is_h264){
02611 if(IS_INTRA16x16(mb_type)){
02612 for(i=0; i<16; i++){
02613 if(h->non_zero_count_cache[ scan8[i] ])
02614 idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
02615 else if(h->mb[i*16])
02616 idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
02617 }
02618 }else{
02619 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
02620 for(i=0; i<16; i+=di){
02621 int nnz = h->non_zero_count_cache[ scan8[i] ];
02622 if(nnz){
02623 if(nnz==1 && h->mb[i*16])
02624 idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
02625 else
02626 idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
02627 }
02628 }
02629 }
02630 }else{
02631 for(i=0; i<16; i++){
02632 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
02633 uint8_t * const ptr= dest_y + block_offset[i];
02634 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
02635 }
02636 }
02637 }
02638 }
02639
02640 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02641 uint8_t *dest[2] = {dest_cb, dest_cr};
02642 if(transform_bypass){
02643 idct_add = idct_dc_add = s->dsp.add_pixels4;
02644 }else{
02645 idct_add = s->dsp.h264_idct_add;
02646 idct_dc_add = s->dsp.h264_idct_dc_add;
02647 chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
02648 chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
02649 }
02650 if(is_h264){
02651 for(i=16; i<16+8; i++){
02652 if(h->non_zero_count_cache[ scan8[i] ])
02653 idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
02654 else if(h->mb[i*16])
02655 idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
02656 }
02657 }else{
02658 for(i=16; i<16+8; i++){
02659 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
02660 uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
02661 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
02662 }
02663 }
02664 }
02665 }
02666 }
02667 if(h->deblocking_filter) {
02668 if (!simple && FRAME_MBAFF) {
02669
02670
02671 const int mb_y = s->mb_y - 1;
02672 uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
02673 const int mb_xy= mb_x + mb_y*s->mb_stride;
02674 const int mb_type_top = s->current_picture.mb_type[mb_xy];
02675 const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
02676 if (!bottom) return;
02677 pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
02678 pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
02679 pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
02680
02681 if(IS_INTRA(mb_type_top | mb_type_bottom))
02682 xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
02683
02684 backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
02685
02686
02687 s->mb_y--;
02688 tprintf(h->s.avctx, "call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
02689 fill_caches(h, mb_type_top, 1);
02690 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
02691 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
02692 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
02693
02694 s->mb_y++;
02695 tprintf(h->s.avctx, "call mbaff filter_mb\n");
02696 fill_caches(h, mb_type_bottom, 1);
02697 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
02698 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
02699 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
02700 } else {
02701 tprintf(h->s.avctx, "call filter_mb\n");
02702 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
02703 fill_caches(h, mb_type, 1);
02704 filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
02705 }
02706 }
02707 }
02708
02712 static void hl_decode_mb_simple(H264Context *h){
02713 hl_decode_mb_internal(h, 1);
02714 }
02715
02719 static void av_noinline hl_decode_mb_complex(H264Context *h){
02720 hl_decode_mb_internal(h, 0);
02721 }
02722
02723 static void hl_decode_mb(H264Context *h){
02724 MpegEncContext * const s = &h->s;
02725 const int mb_x= s->mb_x;
02726 const int mb_y= s->mb_y;
02727 const int mb_xy= mb_x + mb_y*s->mb_stride;
02728 const int mb_type= s->current_picture.mb_type[mb_xy];
02729 int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (ENABLE_GRAY && (s->flags&CODEC_FLAG_GRAY)) || s->encoding;
02730
02731 if(!s->decode)
02732 return;
02733
02734 if (is_complex)
02735 hl_decode_mb_complex(h);
02736 else hl_decode_mb_simple(h);
02737 }
02738
02739 static void pic_as_field(Picture *pic, const int parity){
02740 int i;
02741 for (i = 0; i < 4; ++i) {
02742 if (parity == PICT_BOTTOM_FIELD)
02743 pic->data[i] += pic->linesize[i];
02744 pic->reference = parity;
02745 pic->linesize[i] *= 2;
02746 }
02747 }
02748
02749 static int split_field_copy(Picture *dest, Picture *src,
02750 int parity, int id_add){
02751 int match = !!(src->reference & parity);
02752
02753 if (match) {
02754 *dest = *src;
02755 pic_as_field(dest, parity);
02756 dest->pic_id *= 2;
02757 dest->pic_id += id_add;
02758 }
02759
02760 return match;
02761 }
02762
02778 static int split_field_half_ref_list(Picture *dest, int dest_len,
02779 Picture *src, int src_len, int parity){
02780 int same_parity = 1;
02781 int same_i = 0;
02782 int opp_i = 0;
02783 int out_i;
02784 int field_output;
02785
02786 for (out_i = 0; out_i < dest_len; out_i += field_output) {
02787 if (same_parity && same_i < src_len) {
02788 field_output = split_field_copy(dest + out_i, src + same_i,
02789 parity, 1);
02790 same_parity = !field_output;
02791 same_i++;
02792
02793 } else if (opp_i < src_len) {
02794 field_output = split_field_copy(dest + out_i, src + opp_i,
02795 PICT_FRAME - parity, 0);
02796 same_parity = field_output;
02797 opp_i++;
02798
02799 } else {
02800 break;
02801 }
02802 }
02803
02804 return out_i;
02805 }
02806
02825 static int split_field_ref_list(Picture *dest, int dest_len,
02826 Picture *src, int src_len,
02827 int parity, int long_i){
02828
02829 int i = split_field_half_ref_list(dest, dest_len, src, long_i, parity);
02830 dest += i;
02831 dest_len -= i;
02832
02833 i += split_field_half_ref_list(dest, dest_len, src + long_i,
02834 src_len - long_i, parity);
02835 return i;
02836 }
02837
02841 static int fill_default_ref_list(H264Context *h){
02842 MpegEncContext * const s = &h->s;
02843 int i;
02844 int smallest_poc_greater_than_current = -1;
02845 int structure_sel;
02846 Picture sorted_short_ref[32];
02847 Picture field_entry_list[2][32];
02848 Picture *frame_list[2];
02849
02850 if (FIELD_PICTURE) {
02851 structure_sel = PICT_FRAME;
02852 frame_list[0] = field_entry_list[0];
02853 frame_list[1] = field_entry_list[1];
02854 } else {
02855 structure_sel = 0;
02856 frame_list[0] = h->default_ref_list[0];
02857 frame_list[1] = h->default_ref_list[1];
02858 }
02859
02860 if(h->slice_type==B_TYPE){
02861 int list;
02862 int len[2];
02863 int short_len[2];
02864 int out_i;
02865 int limit= INT_MIN;
02866
02867
02868 for(out_i=0; out_i<h->short_ref_count; out_i++){
02869 int best_i=INT_MIN;
02870 int best_poc=INT_MAX;
02871
02872 for(i=0; i<h->short_ref_count; i++){
02873 const int poc= h->short_ref[i]->poc;
02874 if(poc > limit && poc < best_poc){
02875 best_poc= poc;
02876 best_i= i;
02877 }
02878 }
02879
02880 assert(best_i != INT_MIN);
02881
02882 limit= best_poc;
02883 sorted_short_ref[out_i]= *h->short_ref[best_i];
02884 tprintf(h->s.avctx, "sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
02885 if (-1 == smallest_poc_greater_than_current) {
02886 if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
02887 smallest_poc_greater_than_current = out_i;
02888 }
02889 }
02890 }
02891
02892 tprintf(h->s.avctx, "current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
02893
02894
02895 for(list=0; list<2; list++){
02896 int index = 0;
02897 int j= -99;
02898 int step= list ? -1 : 1;
02899
02900 for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
02901 int sel;
02902 while(j<0 || j>= h->short_ref_count){
02903 if(j != -99 && step == (list ? -1 : 1))
02904 return -1;
02905 step = -step;
02906 j= smallest_poc_greater_than_current + (step>>1);
02907 }
02908 sel = sorted_short_ref[j].reference | structure_sel;
02909 if(sel != PICT_FRAME) continue;
02910 frame_list[list][index ]= sorted_short_ref[j];
02911 frame_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
02912 }
02913 short_len[list] = index;
02914
02915 for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
02916 int sel;
02917 if(h->long_ref[i] == NULL) continue;
02918 sel = h->long_ref[i]->reference | structure_sel;
02919 if(sel != PICT_FRAME) continue;
02920
02921 frame_list[ list ][index ]= *h->long_ref[i];
02922 frame_list[ list ][index++].pic_id= i;;
02923 }
02924 len[list] = index;
02925 }
02926
02927 for(list=0; list<2; list++){
02928 if (FIELD_PICTURE)
02929 len[list] = split_field_ref_list(h->default_ref_list[list],
02930 h->ref_count[list],
02931 frame_list[list],
02932 len[list],
02933 s->picture_structure,
02934 short_len[list]);
02935
02936
02937 if(list && len[0] > 1 && len[0] == len[1])
02938 for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0]; i++)
02939 if(i == len[0]){
02940 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
02941 break;
02942 }
02943
02944 if(len[list] < h->ref_count[ list ])
02945 memset(&h->default_ref_list[list][len[list]], 0, sizeof(Picture)*(h->ref_count[ list ] - len[list]));
02946 }
02947
02948
02949 }else{
02950 int index=0;
02951 int short_len;
02952 for(i=0; i<h->short_ref_count; i++){
02953 int sel;
02954 sel = h->short_ref[i]->reference | structure_sel;
02955 if(sel != PICT_FRAME) continue;
02956 frame_list[0][index ]= *h->short_ref[i];
02957 frame_list[0][index++].pic_id= h->short_ref[i]->frame_num;
02958 }
02959 short_len = index;
02960 for(i = 0; i < 16; i++){
02961 int sel;
02962 if(h->long_ref[i] == NULL) continue;
02963 sel = h->long_ref[i]->reference | structure_sel;
02964 if(sel != PICT_FRAME) continue;
02965 frame_list[0][index ]= *h->long_ref[i];
02966 frame_list[0][index++].pic_id= i;;
02967 }
02968
02969 if (FIELD_PICTURE)
02970 index = split_field_ref_list(h->default_ref_list[0],
02971 h->ref_count[0], frame_list[0],
02972 index, s->picture_structure,
02973 short_len);
02974
02975 if(index < h->ref_count[0])
02976 memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
02977 }
02978 #ifdef TRACE
02979 for (i=0; i<h->ref_count[0]; i++) {
02980 tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
02981 }
02982 if(h->slice_type==B_TYPE){
02983 for (i=0; i<h->ref_count[1]; i++) {
02984 tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].data[0]);
02985 }
02986 }
02987 #endif
02988 return 0;
02989 }
02990
02991 static void print_short_term(H264Context *h);
02992 static void print_long_term(H264Context *h);
02993
03004 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
03005 MpegEncContext * const s = &h->s;
03006
03007 *structure = s->picture_structure;
03008 if(FIELD_PICTURE){
03009 if (!(pic_num & 1))
03010
03011 *structure ^= PICT_FRAME;
03012 pic_num >>= 1;
03013 }
03014
03015 return pic_num;
03016 }
03017
03018 static int decode_ref_pic_list_reordering(H264Context *h){
03019 MpegEncContext * const s = &h->s;
03020 int list, index, pic_structure;
03021
03022 print_short_term(h);
03023 print_long_term(h);
03024 if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0;
03025
03026 for(list=0; list<h->list_count; list++){
03027 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
03028
03029 if(get_bits1(&s->gb)){
03030 int pred= h->curr_pic_num;
03031
03032 for(index=0; ; index++){
03033 unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
03034 unsigned int pic_id;
03035 int i;
03036 Picture *ref = NULL;
03037
03038 if(reordering_of_pic_nums_idc==3)
03039 break;
03040
03041 if(index >= h->ref_count[list]){
03042 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
03043 return -1;
03044 }
03045
03046 if(reordering_of_pic_nums_idc<3){
03047 if(reordering_of_pic_nums_idc<2){
03048 const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
03049 int frame_num;
03050
03051 if(abs_diff_pic_num > h->max_pic_num){
03052 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
03053 return -1;
03054 }
03055
03056 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
03057 else pred+= abs_diff_pic_num;
03058 pred &= h->max_pic_num - 1;
03059
03060 frame_num = pic_num_extract(h, pred, &pic_structure);
03061
03062 for(i= h->short_ref_count-1; i>=0; i--){
03063 ref = h->short_ref[i];
03064 assert(ref->reference);
03065 assert(!ref->long_ref);
03066 if(ref->data[0] != NULL &&
03067 ref->frame_num == frame_num &&
03068 (ref->reference & pic_structure) &&
03069 ref->long_ref == 0)
03070 break;
03071 }
03072 if(i>=0)
03073 ref->pic_id= pred;
03074 }else{
03075 int long_idx;
03076 pic_id= get_ue_golomb(&s->gb);
03077
03078 long_idx= pic_num_extract(h, pic_id, &pic_structure);
03079
03080 if(long_idx>31){
03081 av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
03082 return -1;
03083 }
03084 ref = h->long_ref[long_idx];
03085 assert(!(ref && !ref->reference));
03086 if(ref && (ref->reference & pic_structure)){
03087 ref->pic_id= pic_id;
03088 assert(ref->long_ref);
03089 i=0;
03090 }else{
03091 i=-1;
03092 }
03093 }
03094
03095 if (i < 0) {
03096 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
03097 memset(&h->ref_list[list][index], 0, sizeof(Picture));
03098 } else {
03099 for(i=index; i+1<h->ref_count[list]; i++){
03100 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
03101 break;
03102 }
03103 for(; i > index; i--){
03104 h->ref_list[list][i]= h->ref_list[list][i-1];
03105 }
03106 h->ref_list[list][index]= *ref;
03107 if (FIELD_PICTURE){
03108 pic_as_field(&h->ref_list[list][index], pic_structure);
03109 }
03110 }
03111 }else{
03112 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
03113 return -1;
03114 }
03115 }
03116 }
03117 }
03118 for(list=0; list<h->list_count; list++){
03119 for(index= 0; index < h->ref_count[list]; index++){
03120 if(!h->ref_list[list][index].data[0])
03121 h->ref_list[list][index]= s->current_picture;
03122 }
03123 }
03124
03125 if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
03126 direct_dist_scale_factor(h);
03127 direct_ref_list_init(h);
03128 return 0;
03129 }
03130
03131 static void fill_mbaff_ref_list(H264Context *h){
03132 int list, i, j;
03133 for(list=0; list<2; list++){
03134 for(i=0; i<h->ref_count[list]; i++){
03135 Picture *frame = &h->ref_list[list][i];
03136 Picture *field = &h->ref_list[list][16+2*i];
03137 field[0] = *frame;
03138 for(j=0; j<3; j++)
03139 field[0].linesize[j] <<= 1;
03140 field[0].reference = PICT_TOP_FIELD;
03141 field[1] = field[0];
03142 for(j=0; j<3; j++)
03143 field[1].data[j] += frame->linesize[j];
03144 field[1].reference = PICT_BOTTOM_FIELD;
03145
03146 h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
03147 h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
03148 for(j=0; j<2; j++){
03149 h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
03150 h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
03151 }
03152 }
03153 }
03154 for(j=0; j<h->ref_count[1]; j++){
03155 for(i=0; i<h->ref_count[0]; i++)
03156 h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
03157 memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
03158 memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
03159 }
03160 }
03161
03162 static int pred_weight_table(H264Context *h){
03163 MpegEncContext * const s = &h->s;
03164 int list, i;
03165 int luma_def, chroma_def;
03166
03167 h->use_weight= 0;
03168 h->use_weight_chroma= 0;
03169 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
03170 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
03171 luma_def = 1<<h->luma_log2_weight_denom;
03172 chroma_def = 1<<h->chroma_log2_weight_denom;
03173
03174 for(list=0; list<2; list++){
03175 for(i=0; i<h->ref_count[list]; i++){
03176 int luma_weight_flag, chroma_weight_flag;
03177
03178 luma_weight_flag= get_bits1(&s->gb);
03179 if(luma_weight_flag){
03180 h->luma_weight[list][i]= get_se_golomb(&s->gb);
03181 h->luma_offset[list][i]= get_se_golomb(&s->gb);
03182 if( h->luma_weight[list][i] != luma_def
03183 || h->luma_offset[list][i] != 0)
03184 h->use_weight= 1;
03185 }else{
03186 h->luma_weight[list][i]= luma_def;
03187 h->luma_offset[list][i]= 0;
03188 }
03189
03190 chroma_weight_flag= get_bits1(&s->gb);
03191 if(chroma_weight_flag){
03192 int j;
03193 for(j=0; j<2; j++){
03194 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
03195 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
03196 if( h->chroma_weight[list][i][j] != chroma_def
03197 || h->chroma_offset[list][i][j] != 0)
03198 h->use_weight_chroma= 1;
03199 }
03200 }else{
03201 int j;
03202 for(j=0; j<2; j++){
03203 h->chroma_weight[list][i][j]= chroma_def;
03204 h->chroma_offset[list][i][j]= 0;
03205 }
03206 }
03207 }
03208 if(h->slice_type != B_TYPE) break;
03209 }
03210 h->use_weight= h->use_weight || h->use_weight_chroma;
03211 return 0;
03212 }
03213
03214 static void implicit_weight_table(H264Context *h){
03215 MpegEncContext * const s = &h->s;
03216 int ref0, ref1;
03217 int cur_poc = s->current_picture_ptr->poc;
03218
03219 if( h->ref_count[0] == 1 && h->ref_count[1] == 1
03220 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
03221 h->use_weight= 0;
03222 h->use_weight_chroma= 0;
03223 return;
03224 }
03225
03226 h->use_weight= 2;
03227 h->use_weight_chroma= 2;
03228 h->luma_log2_weight_denom= 5;
03229 h->chroma_log2_weight_denom= 5;
03230
03231 for(ref0=0; ref0 < h->ref_count[0]; ref0++){
03232 int poc0 = h->ref_list[0][ref0].poc;
03233 for(ref1=0; ref1 < h->ref_count[1]; ref1++){
03234 int poc1 = h->ref_list[1][ref1].poc;
03235 int td = av_clip(poc1 - poc0, -128, 127);
03236 if(td){
03237 int tb = av_clip(cur_poc - poc0, -128, 127);
03238 int tx = (16384 + (FFABS(td) >> 1)) / td;
03239 int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
03240 if(dist_scale_factor < -64 || dist_scale_factor > 128)
03241 h->implicit_weight[ref0][ref1] = 32;
03242 else
03243 h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
03244 }else
03245 h->implicit_weight[ref0][ref1] = 32;
03246 }
03247 }
03248 }
03249
03261 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
03262 int i;
03263 if (pic->reference &= refmask) {
03264 return 0;
03265 } else {
03266 if(pic == h->delayed_output_pic)
03267 pic->reference=DELAYED_PIC_REF;
03268 else{
03269 for(i = 0; h->delayed_pic[i]; i++)
03270 if(pic == h->delayed_pic[i]){
03271 pic->reference=DELAYED_PIC_REF;
03272 break;
03273 }
03274 }
03275 return 1;
03276 }
03277 }
03278
03282 static void idr(H264Context *h){
03283 int i;
03284
03285 for(i=0; i<16; i++){
03286 if (h->long_ref[i] != NULL) {
03287 unreference_pic(h, h->long_ref[i], 0);
03288 h->long_ref[i]= NULL;
03289 }
03290 }
03291 h->long_ref_count=0;
03292
03293 for(i=0; i<h->short_ref_count; i++){
03294 unreference_pic(h, h->short_ref[i], 0);
03295 h->short_ref[i]= NULL;
03296 }
03297 h->short_ref_count=0;
03298 }
03299
03300
03301 static void flush_dpb(AVCodecContext *avctx){
03302 H264Context *h= avctx->priv_data;
03303 int i;
03304 for(i=0; i<16; i++) {
03305 if(h->delayed_pic[i])
03306 h->delayed_pic[i]->reference= 0;
03307 h->delayed_pic[i]= NULL;
03308 }
03309 if(h->delayed_output_pic)
03310 h->delayed_output_pic->reference= 0;
03311 h->delayed_output_pic= NULL;
03312 idr(h);
03313 if(h->s.current_picture_ptr)
03314 h->s.current_picture_ptr->reference= 0;
03315 h->s.first_field= 0;
03316 ff_mpeg_flush(avctx);
03317 }
03318
03327 static Picture * find_short(H264Context *h, int frame_num, int *idx){
03328 MpegEncContext * const s = &h->s;
03329 int i;
03330
03331 for(i=0; i<h->short_ref_count; i++){
03332 Picture *pic= h->short_ref[i];
03333 if(s->avctx->debug&FF_DEBUG_MMCO)
03334 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
03335 if(pic->frame_num == frame_num) {
03336 *idx = i;
03337 return pic;
03338 }
03339 }
03340 return NULL;
03341 }
03342
03349 static void remove_short_at_index(H264Context *h, int i){
03350 assert(i > 0 && i < h->short_ref_count);
03351 h->short_ref[i]= NULL;
03352 if (--h->short_ref_count)
03353 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
03354 }
03355
03360 static Picture * remove_short(H264Context *h, int frame_num){
03361 MpegEncContext * const s = &h->s;
03362 Picture *pic;
03363 int i;
03364
03365 if(s->avctx->debug&FF_DEBUG_MMCO)
03366 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
03367
03368 pic = find_short(h, frame_num, &i);
03369 if (pic)
03370 remove_short_at_index(h, i);
03371
03372 return pic;
03373 }
03374
03381 static void remove_long_at_index(H264Context *h, int i){
03382 h->long_ref[i]= NULL;
03383 h->long_ref_count--;
03384 }
03385
03390 static Picture * remove_long(H264Context *h, int i){
03391 Picture *pic;
03392
03393 pic= h->long_ref[i];
03394 if (pic)
03395 remove_long_at_index(h, i);
03396
03397 return pic;
03398 }
03399
03403 static void print_short_term(H264Context *h) {
03404 uint32_t i;
03405 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
03406 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
03407 for(i=0; i<h->short_ref_count; i++){
03408 Picture *pic= h->short_ref[i];
03409 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
03410 }
03411 }
03412 }
03413
03417 static void print_long_term(H264Context *h) {
03418 uint32_t i;
03419 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
03420 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
03421 for(i = 0; i < 16; i++){
03422 Picture *pic= h->long_ref[i];
03423 if (pic) {
03424 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
03425 }
03426 }
03427 }
03428 }
03429
03433 static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
03434 MpegEncContext * const s = &h->s;
03435 int i, j;
03436 int current_ref_assigned=0;
03437 Picture *pic;
03438
03439 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
03440 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
03441
03442 for(i=0; i<mmco_count; i++){
03443 int structure, frame_num, unref_pic;
03444 if(s->avctx->debug&FF_DEBUG_MMCO)
03445 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
03446
03447 switch(mmco[i].opcode){
03448 case MMCO_SHORT2UNUSED:
03449 if(s->avctx->debug&FF_DEBUG_MMCO)
03450 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
03451 frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
03452 pic = find_short(h, frame_num, &j);
03453 if (pic) {
03454 if (unreference_pic(h, pic, structure ^ PICT_FRAME))
03455 remove_short_at_index(h, j);
03456 } else if(s->avctx->debug&FF_DEBUG_MMCO)
03457 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short failure\n");
03458 break;
03459 case MMCO_SHORT2LONG:
03460 if (FIELD_PICTURE && mmco[i].long_arg < h->long_ref_count &&
03461 h->long_ref[mmco[i].long_arg]->frame_num ==
03462 mmco[i].short_pic_num / 2) {
03463
03464 } else {
03465 int frame_num = mmco[i].short_pic_num >> FIELD_PICTURE;
03466
03467 pic= remove_long(h, mmco[i].long_arg);
03468 if(pic) unreference_pic(h, pic, 0);
03469
03470 h->long_ref[ mmco[i].long_arg ]= remove_short(h, frame_num);
03471 if (h->long_ref[ mmco[i].long_arg ]){
03472 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
03473 h->long_ref_count++;
03474 }
03475 }
03476 break;
03477 case MMCO_LONG2UNUSED:
03478 j = pic_num_extract(h, mmco[i].long_arg, &structure);
03479 pic = h->long_ref[j];
03480 if (pic) {
03481 if (unreference_pic(h, pic, structure ^ PICT_FRAME))
03482 remove_long_at_index(h, j);
03483 } else if(s->avctx->debug&FF_DEBUG_MMCO)
03484 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
03485 break;
03486 case MMCO_LONG:
03487 unref_pic = 1;
03488 if (FIELD_PICTURE && !s->first_field) {
03489 if (h->long_ref[mmco[i].long_arg] == s->current_picture_ptr) {
03490
03491 unref_pic = 0;
03492 } else if (s->current_picture_ptr->reference) {
03493
03494
03495
03496
03497
03498
03499 av_log(h->s.avctx, AV_LOG_ERROR,
03500 "illegal long term reference assignment for second "
03501 "field in complementary field pair (first field is "
03502 "short term or has non-matching long index)\n");
03503 unref_pic = 0;
03504 }
03505 }
03506
03507 if (unref_pic) {
03508 pic= remove_long(h, mmco[i].long_arg);
03509 if(pic) unreference_pic(h, pic, 0);
03510
03511 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
03512 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
03513 h->long_ref_count++;
03514 }
03515
03516 s->current_picture_ptr->reference |= s->picture_structure;
03517 current_ref_assigned=1;
03518 break;
03519 case MMCO_SET_MAX_LONG:
03520 assert(mmco[i].long_arg <= 16);
03521
03522 for(j = mmco[i].long_arg; j<16; j++){
03523 pic = remove_long(h, j);
03524 if (pic) unreference_pic(h, pic, 0);
03525 }
03526 break;
03527 case MMCO_RESET:
03528 while(h->short_ref_count){
03529 pic= remove_short(h, h->short_ref[0]->frame_num);
03530 if(pic) unreference_pic(h, pic, 0);
03531 }
03532 for(j = 0; j < 16; j++) {
03533 pic= remove_long(h, j);
03534 if(pic) unreference_pic(h, pic, 0);
03535 }
03536 break;
03537 default: assert(0);
03538 }
03539 }
03540
03541 if (!current_ref_assigned && FIELD_PICTURE &&
03542 !s->first_field && s->current_picture_ptr->reference) {
03543
03544
03545
03546
03547
03548
03549
03550 if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
03551
03552 s->current_picture_ptr->reference = PICT_FRAME;
03553 } else if (s->current_picture_ptr->long_ref) {
03554 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
03555 "assignment for second field "
03556 "in complementary field pair "
03557 "(first field is long term)\n");
03558 } else {
03559
03560
03561
03562
03563
03564 assert(0);
03565 }
03566 current_ref_assigned = 1;
03567 }
03568
03569 if(!current_ref_assigned){
03570 pic= remove_short(h, s->current_picture_ptr->frame_num);
03571 if(pic){
03572 unreference_pic(h, pic, 0);
03573 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
03574 }
03575
03576 if(h->short_ref_count)
03577 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
03578
03579 h->short_ref[0]= s->current_picture_ptr;
03580 h->short_ref[0]->long_ref=0;
03581 h->short_ref_count++;
03582 s->current_picture_ptr->reference |= s->picture_structure;
03583 }
03584
03585 if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
03586
03587
03588
03589
03590
03591 av_log(h->s.avctx, AV_LOG_ERROR,
03592 "number of reference frames exceeds max (probably "
03593 "corrupt input), discarding one\n");
03594
03595 if (h->long_ref_count) {
03596 for (i = 0; i < 16; ++i)
03597 if (h->long_ref[i])
03598 break;
03599
03600 assert(i < 16);
03601 pic = h->long_ref[i];
03602 remove_long_at_index(h, i);
03603 } else {
03604 pic = h->short_ref[h->short_ref_count - 1];
03605 remove_short_at_index(h, h->short_ref_count - 1);
03606 }
03607 unreference_pic(h, pic, 0);
03608 }
03609
03610 print_short_term(h);
03611 print_long_term(h);
03612 return 0;
03613 }
03614
03615 static int decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
03616 MpegEncContext * const s = &h->s;
03617 int i;
03618
03619 if(h->nal_unit_type == NAL_IDR_SLICE){
03620 s->broken_link= get_bits1(gb) -1;
03621 h->mmco[0].long_arg= get_bits1(gb) - 1;
03622 if(h->mmco[0].long_arg == -1)
03623 h->mmco_index= 0;
03624 else{
03625 h->mmco[0].opcode= MMCO_LONG;
03626 h->mmco_index= 1;
03627 }
03628 }else{
03629 if(get_bits1(gb)){
03630 for(i= 0; i<MAX_MMCO_COUNT; i++) {
03631 MMCOOpcode opcode= get_ue_golomb(gb);
03632
03633 h->mmco[i].opcode= opcode;
03634 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
03635 h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
03636
03637
03638
03639
03640 }
03641 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
03642 unsigned int long_arg= get_ue_golomb(gb);
03643 if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
03644 av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
03645 return -1;
03646 }
03647 h->mmco[i].long_arg= long_arg;
03648 }
03649
03650 if(opcode > (unsigned)MMCO_LONG){
03651 av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
03652 return -1;
03653 }
03654 if(opcode == MMCO_END)
03655 break;
03656 }
03657 h->mmco_index= i;
03658 }else{
03659 assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
03660
03661 if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
03662 !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
03663 h->mmco[0].opcode= MMCO_SHORT2UNUSED;
03664 h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
03665 h->mmco_index= 1;
03666 if (FIELD_PICTURE) {
03667 h->mmco[0].short_pic_num *= 2;
03668 h->mmco[1].opcode= MMCO_SHORT2UNUSED;
03669 h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
03670 h->mmco_index= 2;
03671 }
03672 }else
03673 h->mmco_index= 0;
03674 }
03675 }
03676
03677 return 0;
03678 }
03679
03680 static int init_poc(H264Context *h){
03681 MpegEncContext * const s = &h->s;
03682 const int max_frame_num= 1<<h->sps.log2_max_frame_num;
03683 int field_poc[2];
03684
03685 if(h->nal_unit_type == NAL_IDR_SLICE){
03686 h->frame_num_offset= 0;
03687 }else{
03688 if(h->frame_num < h->prev_frame_num)
03689 h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
03690 else
03691 h->frame_num_offset= h->prev_frame_num_offset;
03692 }
03693
03694 if(h->sps.poc_type==0){
03695 const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
03696
03697 if(h->nal_unit_type == NAL_IDR_SLICE){
03698 h->prev_poc_msb=
03699 h->prev_poc_lsb= 0;
03700 }
03701
03702 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
03703 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
03704 else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
03705 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
03706 else
03707 h->poc_msb = h->prev_poc_msb;
03708
03709 field_poc[0] =
03710 field_poc[1] = h->poc_msb + h->poc_lsb;
03711 if(s->picture_structure == PICT_FRAME)
03712 field_poc[1] += h->delta_poc_bottom;
03713 }else if(h->sps.poc_type==1){
03714 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
03715 int i;
03716
03717 if(h->sps.poc_cycle_length != 0)
03718 abs_frame_num = h->frame_num_offset + h->frame_num;
03719 else
03720 abs_frame_num = 0;
03721
03722 if(h->nal_ref_idc==0 && abs_frame_num > 0)
03723 abs_frame_num--;
03724
03725 expected_delta_per_poc_cycle = 0;
03726 for(i=0; i < h->sps.poc_cycle_length; i++)
03727 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ];
03728
03729 if(abs_frame_num > 0){
03730 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
03731 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
03732
03733 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
03734 for(i = 0; i <= frame_num_in_poc_cycle; i++)
03735 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
03736 } else
03737 expectedpoc = 0;
03738
03739 if(h->nal_ref_idc == 0)
03740 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
03741
03742 field_poc[0] = expectedpoc + h->delta_poc[0];
03743 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
03744
03745 if(s->picture_structure == PICT_FRAME)
03746 field_poc[1] += h->delta_poc[1];
03747 }else{
03748 int poc;
03749 if(h->nal_unit_type == NAL_IDR_SLICE){
03750 poc= 0;
03751 }else{
03752 if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
03753 else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
03754 }
03755 field_poc[0]= poc;
03756 field_poc[1]= poc;
03757 }
03758
03759 if(s->picture_structure != PICT_BOTTOM_FIELD) {
03760 s->current_picture_ptr->field_poc[0]= field_poc[0];
03761 s->current_picture_ptr->poc = field_poc[0];
03762 }
03763 if(s->picture_structure != PICT_TOP_FIELD) {
03764 s->current_picture_ptr->field_poc[1]= field_poc[1];
03765 s->current_picture_ptr->poc = field_poc[1];
03766 }
03767 if(!FIELD_PICTURE || !s->first_field) {
03768 Picture *cur = s->current_picture_ptr;
03769 cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
03770 }
03771
03772 return 0;
03773 }
03774
03775
03779 static void init_scan_tables(H264Context *h){
03780 MpegEncContext * const s = &h->s;
03781 int i;
03782 if(s->dsp.h264_idct_add == ff_h264_idct_add_c){
03783 memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
03784 memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
03785 }else{
03786 for(i=0; i<16; i++){
03787 #define T(x) (x>>2) | ((x<<2) & 0xF)
03788 h->zigzag_scan[i] = T(zigzag_scan[i]);
03789 h-> field_scan[i] = T( field_scan[i]);
03790 #undef T
03791 }
03792 }
03793 if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
03794 memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
03795 memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
03796 memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
03797 memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
03798 }else{
03799 for(i=0; i<64; i++){
03800 #define T(x) (x>>3) | ((x&7)<<3)
03801 h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
03802 h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
03803 h->field_scan8x8[i] = T(field_scan8x8[i]);
03804 h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
03805 #undef T
03806 }
03807 }
03808 if(h->sps.transform_bypass){
03809 h->zigzag_scan_q0 = zigzag_scan;
03810 h->zigzag_scan8x8_q0 = zigzag_scan8x8;
03811 h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
03812 h->field_scan_q0 = field_scan;
03813 h->field_scan8x8_q0 = field_scan8x8;
03814 h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
03815 }else{
03816 h->zigzag_scan_q0 = h->zigzag_scan;
03817 h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
03818 h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
03819 h->field_scan_q0 = h->field_scan;
03820 h->field_scan8x8_q0 = h->field_scan8x8;
03821 h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
03822 }
03823 }
03824
03828 static void clone_slice(H264Context *dst, H264Context *src)
03829 {
03830 memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
03831 dst->s.current_picture_ptr = src->s.current_picture_ptr;
03832 dst->s.current_picture = src->s.current_picture;
03833 dst->s.linesize = src->s.linesize;
03834 dst->s.uvlinesize = src->s.uvlinesize;
03835 dst->s.first_field = src->s.first_field;
03836
03837 dst->prev_poc_msb = src->prev_poc_msb;
03838 dst->prev_poc_lsb = src->prev_poc_lsb;
03839 dst->prev_frame_num_offset = src->prev_frame_num_offset;
03840 dst->prev_frame_num = src->prev_frame_num;
03841 dst->short_ref_count = src->short_ref_count;
03842
03843 memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
03844 memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
03845 memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
03846 memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
03847
03848 memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
03849 memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
03850 }
03851
03861 static int decode_slice_header(H264Context *h, H264Context *h0){
03862 MpegEncContext * const s = &h->s;
03863 MpegEncContext * const s0 = &h0->s;
03864 unsigned int first_mb_in_slice;
03865 unsigned int pps_id;
03866 int num_ref_idx_active_override_flag;
03867 static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
03868 unsigned int slice_type, tmp, i;
03869 int default_ref_list_done = 0;
03870 int last_pic_structure;
03871
03872 s->dropable= h->nal_ref_idc == 0;
03873
03874 if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
03875 s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
03876 s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
03877 }else{
03878 s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
03879 s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
03880 }
03881
03882 first_mb_in_slice= get_ue_golomb(&s->gb);
03883
03884 if((s->flags2 & CODEC_FLAG2_CHUNKS) && first_mb_in_slice == 0){
03885 h0->current_slice = 0;
03886 if (!s0->first_field)
03887 s->current_picture_ptr= NULL;
03888 }
03889
03890 slice_type= get_ue_golomb(&s->gb);
03891 if(slice_type > 9){
03892 av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
03893 return -1;
03894 }
03895 if(slice_type > 4){
03896 slice_type -= 5;
03897 h->slice_type_fixed=1;
03898 }else
03899 h->slice_type_fixed=0;
03900
03901 slice_type= slice_type_map[ slice_type ];
03902 if (slice_type == I_TYPE
03903 || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
03904 default_ref_list_done = 1;
03905 }
03906 h->slice_type= slice_type;
03907
03908 s->pict_type= h->slice_type;
03909 if (s->pict_type == B_TYPE && s0->last_picture_ptr == NULL) {
03910 av_log(h->s.avctx, AV_LOG_ERROR,
03911 "B picture before any references, skipping\n");
03912 return -1;
03913 }
03914
03915 pps_id= get_ue_golomb(&s->gb);
03916 if(pps_id>=MAX_PPS_COUNT){
03917 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
03918 return -1;
03919 }
03920 if(!h0->pps_buffers[pps_id]) {
03921 av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
03922 return -1;
03923 }
03924 h->pps= *h0->pps_buffers[pps_id];
03925
03926 if(!h0->sps_buffers[h->pps.sps_id]) {
03927 av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
03928 return -1;
03929 }
03930 h->sps = *h0->sps_buffers[h->pps.sps_id];
03931
03932 if(h == h0 && h->dequant_coeff_pps != pps_id){
03933 h->dequant_coeff_pps = pps_id;
03934 init_dequant_tables(h);
03935 }
03936
03937 s->mb_width= h->sps.mb_width;
03938 s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
03939
03940 h->b_stride= s->mb_width*4;
03941 h->b8_stride= s->mb_width*2;
03942
03943 s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
03944 if(h->sps.frame_mbs_only_flag)
03945 s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
03946 else
03947 s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom);
03948
03949 if (s->context_initialized
03950 && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
03951 if(h != h0)
03952 return -1;
03953 free_tables(h);
03954 MPV_common_end(s);
03955 }
03956 if (!s->context_initialized) {
03957 if(h != h0)
03958 return -1;
03959 if (MPV_common_init(s) < 0)
03960 return -1;
03961 s->first_field = 0;
03962
03963 init_scan_tables(h);
03964 alloc_tables(h);
03965
03966 for(i = 1; i < s->avctx->thread_count; i++) {
03967 H264Context *c;
03968 c = h->thread_context[i] = av_malloc(sizeof(H264Context));
03969 memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
03970 memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
03971 c->sps = h->sps;
03972 c->pps = h->pps;
03973 init_scan_tables(c);
03974 clone_tables(c, h);
03975 }
03976
03977 for(i = 0; i < s->avctx->thread_count; i++)
03978 if(context_init(h->thread_context[i]) < 0)
03979 return -1;
03980
03981 s->avctx->width = s->width;
03982 s->avctx->height = s->height;
03983 s->avctx->sample_aspect_ratio= h->sps.sar;
03984 if(!s->avctx->sample_aspect_ratio.den)
03985 s->avctx->sample_aspect_ratio.den = 1;
03986
03987 if(h->sps.timing_info_present_flag){
03988 s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
03989 if(h->x264_build > 0 && h->x264_build < 44)
03990 s->avctx->time_base.den *= 2;
03991 av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
03992 s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
03993 }
03994 }
03995
03996 h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
03997
03998 h->mb_mbaff = 0;
03999 h->mb_aff_frame = 0;
04000 last_pic_structure = s0->picture_structure;
04001 if(h->sps.frame_mbs_only_flag){
04002 s->picture_structure= PICT_FRAME;
04003 }else{
04004 if(get_bits1(&s->gb)) {
04005 s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb);
04006 } else {
04007 s->picture_structure= PICT_FRAME;
04008 h->mb_aff_frame = h->sps.mb_aff;
04009 }
04010 }
04011
04012 if(h0->current_slice == 0){
04013
04014 if (s0->first_field) {
04015 assert(s0->current_picture_ptr);
04016 assert(s0->current_picture_ptr->data[0]);
04017 assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
04018
04019
04020 if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
04021
04022
04023
04024
04025 s0->current_picture_ptr = NULL;
04026 s0->first_field = FIELD_PICTURE;
04027
04028 } else {
04029 if (h->nal_ref_idc &&
04030 s0->current_picture_ptr->reference &&
04031 s0->current_picture_ptr->frame_num != h->frame_num) {
04032
04033
04034
04035
04036
04037
04038 s0->first_field = 1;
04039 s0->current_picture_ptr = NULL;
04040
04041 } else {
04042
04043 s0->first_field = 0;
04044 }
04045 }
04046
04047 } else {
04048
04049 assert(!s0->current_picture_ptr);
04050 s0->first_field = FIELD_PICTURE;
04051 }
04052
04053 if((!FIELD_PICTURE || s0->first_field) && frame_start(h) < 0) {
04054 s0->first_field = 0;
04055 return -1;
04056 }
04057 }
04058 if(h != h0)
04059 clone_slice(h, h0);
04060
04061 s->current_picture_ptr->frame_num= h->frame_num;
04062
04063 assert(s->mb_num == s->mb_width * s->mb_height);
04064 if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
04065 first_mb_in_slice >= s->mb_num){
04066 av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
04067 return -1;
04068 }
04069 s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
04070 s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
04071 if (s->picture_structure == PICT_BOTTOM_FIELD)
04072 s->resync_mb_y = s->mb_y = s->mb_y + 1;
04073 assert(s->mb_y < s->mb_height);
04074
04075 if(s->picture_structure==PICT_FRAME){
04076 h->curr_pic_num= h->frame_num;
04077 h->max_pic_num= 1<< h->sps.log2_max_frame_num;
04078 }else{
04079 h->curr_pic_num= 2*h->frame_num + 1;
04080 h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
04081 }
04082
04083 if(h->nal_unit_type == NAL_IDR_SLICE){
04084 get_ue_golomb(&s->gb);
04085 }
04086
04087 if(h->sps.poc_type==0){
04088 h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
04089
04090 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
04091 h->delta_poc_bottom= get_se_golomb(&s->gb);
04092 }
04093 }
04094
04095 if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
04096 h->delta_poc[0]= get_se_golomb(&s->gb);
04097
04098 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
04099 h->delta_poc[1]= get_se_golomb(&s->gb);
04100 }
04101
04102 init_poc(h);
04103
04104 if(h->pps.redundant_pic_cnt_present){
04105 h->redundant_pic_count= get_ue_golomb(&s->gb);
04106 }
04107
04108
04109 h->ref_count[0]= h->pps.ref_count[0];
04110 h->ref_count[1]= h->pps.ref_count[1];
04111
04112 if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
04113 if(h->slice_type == B_TYPE){
04114 h->direct_spatial_mv_pred= get_bits1(&s->gb);
04115 }
04116 num_ref_idx_active_override_flag= get_bits1(&s->gb);
04117
04118 if(num_ref_idx_active_override_flag){
04119 h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
04120 if(h->slice_type==B_TYPE)
04121 h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
04122
04123 if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
04124 av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
04125 h->ref_count[0]= h->ref_count[1]= 1;
04126 return -1;
04127 }
04128 }
04129 if(h->slice_type == B_TYPE)
04130 h->list_count= 2;
04131 else
04132 h->list_count= 1;
04133 }else
04134 h->list_count= 0;
04135
04136 if(!default_ref_list_done){
04137 fill_default_ref_list(h);
04138 }
04139
04140 if(decode_ref_pic_list_reordering(h) < 0)
04141 return -1;
04142
04143 if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
04144 || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
04145 pred_weight_table(h);
04146 else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
04147 implicit_weight_table(h);
04148 else
04149 h->use_weight = 0;
04150
04151 if(h->nal_ref_idc)
04152 decode_ref_pic_marking(h0, &s->gb);
04153
04154 if(FRAME_MBAFF)
04155 fill_mbaff_ref_list(h);
04156
04157 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ){
04158 tmp = get_ue_golomb(&s->gb);
04159 if(tmp > 2){
04160 av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
04161 return -1;
04162 }
04163 h->cabac_init_idc= tmp;
04164 }
04165
04166 h->last_qscale_diff = 0;
04167 tmp = h->pps.init_qp + get_se_golomb(&s->gb);
04168 if(tmp>51){
04169 av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
04170 return -1;
04171 }
04172 s->qscale= tmp;
04173 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
04174 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
04175
04176 if(h->slice_type == SP_TYPE){
04177 get_bits1(&s->gb);
04178 }
04179 if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
04180 get_se_golomb(&s->gb);
04181 }
04182
04183 h->deblocking_filter = 1;
04184 h->slice_alpha_c0_offset = 0;
04185 h->slice_beta_offset = 0;
04186 if( h->pps.deblocking_filter_parameters_present ) {
04187 tmp= get_ue_golomb(&s->gb);
04188 if(tmp > 2){
04189 av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
04190 return -1;
04191 }
04192 h->deblocking_filter= tmp;
04193 if(h->deblocking_filter < 2)
04194 h->deblocking_filter^= 1;
04195
04196 if( h->deblocking_filter ) {
04197 h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
04198 h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
04199 }
04200 }
04201
04202 if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
04203 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
04204 ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
04205 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
04206 h->deblocking_filter= 0;
04207
04208 if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
04209 if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
04210
04211
04212 h->deblocking_filter = 2;
04213 } else {
04214 h0->max_contexts = 1;
04215 if(!h0->single_decode_warning) {
04216 av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
04217 h0->single_decode_warning = 1;
04218 }
04219 if(h != h0)
04220 return 1;
04221 }
04222 }
04223
04224 #if 0 //FMO
04225 if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
04226 slice_group_change_cycle= get_bits(&s->gb, ?);
04227 #endif
04228
04229 h0->last_slice_type = slice_type;
04230 h->slice_num = ++h0->current_slice;
04231
04232 h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
04233 h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
04234
04235 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
04236 av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
04237 h->slice_num,
04238 (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
04239 first_mb_in_slice,
04240 av_get_pict_type_char(h->slice_type),
04241 pps_id, h->frame_num,
04242 s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
04243 h->ref_count[0], h->ref_count[1],
04244 s->qscale,
04245 h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
04246 h->use_weight,
04247 h->use_weight==1 && h->use_weight_chroma ? "c" : ""
04248 );
04249 }
04250
04251 return 0;
04252 }
04253
04257 static inline int get_level_prefix(GetBitContext *gb){
04258 unsigned int buf;
04259 int log;
04260
04261 OPEN_READER(re, gb);
04262 UPDATE_CACHE(re, gb);
04263 buf=GET_CACHE(re, gb);
04264
04265 log= 32 - av_log2(buf);
04266 #ifdef TRACE
04267 print_bin(buf>>(32-log), log);
04268 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
04269 #endif
04270
04271 LAST_SKIP_BITS(re, gb, log);
04272 CLOSE_READER(re, gb);
04273
04274 return log-1;
04275 }
04276
04277 static inline int get_dct8x8_allowed(H264Context *h){
04278 int i;
04279 for(i=0; i<4; i++){
04280 if(!IS_SUB_8X8(h->sub_mb_type[i])
04281 || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
04282 return 0;
04283 }
04284 return 1;
04285 }
04286
04294 static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
04295 MpegEncContext * const s = &h->s;
04296 static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
04297 int level[16];
04298 int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
04299
04300
04301
04302 if(n == CHROMA_DC_BLOCK_INDEX){
04303 coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
04304 total_coeff= coeff_token>>2;
04305 }else{
04306 if(n == LUMA_DC_BLOCK_INDEX){
04307 total_coeff= pred_non_zero_count(h, 0);
04308 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
04309 total_coeff= coeff_token>>2;
04310 }else{
04311 total_coeff= pred_non_zero_count(h, n);
04312 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
04313 total_coeff= coeff_token>>2;
04314 h->non_zero_count_cache[ scan8[n] ]= total_coeff;
04315 }
04316 }
04317
04318
04319
04320 if(total_coeff==0)
04321 return 0;
04322 if(total_coeff > (unsigned)max_coeff) {
04323 av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
04324 return -1;
04325 }
04326
04327 trailing_ones= coeff_token&3;
04328 tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
04329 assert(total_coeff<=16);
04330
04331 for(i=0; i<trailing_ones; i++){
04332 level[i]= 1 - 2*get_bits1(gb);
04333 }
04334
04335 if(i<total_coeff) {
04336 int level_code, mask;
04337 int suffix_length = total_coeff > 10 && trailing_ones < 3;
04338 int prefix= get_level_prefix(gb);
04339
04340
04341 if(prefix<14){
04342 if(suffix_length)
04343 level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length);
04344 else
04345 level_code= (prefix<<suffix_length);
04346 }else if(prefix==14){
04347 if(suffix_length)
04348 level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length);
04349 else
04350 level_code= prefix + get_bits(gb, 4);
04351 }else if(prefix==15){
04352 level_code= (prefix<<suffix_length) + get_bits(gb, 12);
04353 if(suffix_length==0) level_code+=15;
04354 }else{
04355 av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
04356 return -1;
04357 }
04358
04359 if(trailing_ones < 3) level_code += 2;
04360
04361 suffix_length = 1;
04362 if(level_code > 5)
04363 suffix_length++;
04364 mask= -(level_code&1);
04365 level[i]= (((2+level_code)>>1) ^ mask) - mask;
04366 i++;
04367
04368
04369 for(;i<total_coeff;i++) {
04370 static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
04371 prefix = get_level_prefix(gb);
04372 if(prefix<15){
04373 level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
04374 }else if(prefix==15){
04375 level_code = (prefix<<suffix_length) + get_bits(gb, 12);
04376 }else{
04377 av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
04378 return -1;
04379 }
04380 mask= -(level_code&1);
04381 level[i]= (((2+level_code)>>1) ^ mask) - mask;
04382 if(level_code > suffix_limit[suffix_length])
04383 suffix_length++;
04384 }
04385 }
04386
04387 if(total_coeff == max_coeff)
04388 zeros_left=0;
04389 else{
04390 if(n == CHROMA_DC_BLOCK_INDEX)
04391 zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
04392 else
04393 zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
04394 }
04395
04396 coeff_num = zeros_left + total_coeff - 1;
04397 j = scantable[coeff_num];
04398 if(n > 24){
04399 block[j] = level[0];
04400 for(i=1;i<total_coeff;i++) {
04401 if(zeros_left <= 0)
04402 run_before = 0;
04403 else if(zeros_left < 7){
04404 run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
04405 }else{
04406 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
04407 }
04408 zeros_left -= run_before;
04409 coeff_num -= 1 + run_before;
04410 j= scantable[ coeff_num ];
04411
04412 block[j]= level[i];
04413 }
04414 }else{
04415 block[j] = (level[0] * qmul[j] + 32)>>6;
04416 for(i=1;i<total_coeff;i++) {
04417 if(zeros_left <= 0)
04418 run_before = 0;
04419 else if(zeros_left < 7){
04420 run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
04421 }else{
04422 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
04423 }
04424 zeros_left -= run_before;
04425 coeff_num -= 1 + run_before;
04426 j= scantable[ coeff_num ];
04427
04428 block[j]= (level[i] * qmul[j] + 32)>>6;
04429 }
04430 }
04431
04432 if(zeros_left<0){
04433 av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
04434 return -1;
04435 }
04436
04437 return 0;
04438 }
04439
04440 static void predict_field_decoding_flag(H264Context *h){
04441 MpegEncContext * const s = &h->s;
04442 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
04443 int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
04444 ? s->current_picture.mb_type[mb_xy-1]
04445 : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
04446 ? s->current_picture.mb_type[mb_xy-s->mb_stride]
04447 : 0;
04448 h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
04449 }
04450
04454 static void decode_mb_skip(H264Context *h){
04455 MpegEncContext * const s = &h->s;
04456 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
04457 int mb_type=0;
04458
04459 memset(h->non_zero_count[mb_xy], 0, 16);
04460 memset(h->non_zero_count_cache + 8, 0, 8*5);
04461
04462 if(MB_FIELD)
04463 mb_type|= MB_TYPE_INTERLACED;
04464
04465 if( h->slice_type == B_TYPE )
04466 {
04467
04468 mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
04469
04470 fill_caches(h, mb_type, 0);
04471 pred_direct_motion(h, &mb_type);
04472 mb_type|= MB_TYPE_SKIP;
04473 }
04474 else
04475 {
04476 int mx, my;
04477 mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
04478
04479 fill_caches(h, mb_type, 0);
04480 pred_pskip_motion(h, &mx, &my);
04481 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
04482 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
04483 }
04484
04485 write_back_motion(h, mb_type);
04486 s->current_picture.mb_type[mb_xy]= mb_type;
04487 s->current_picture.qscale_table[mb_xy]= s->qscale;
04488 h->slice_table[ mb_xy ]= h->slice_num;
04489 h->prev_mb_skipped= 1;
04490 }
04491
04496 static int decode_mb_cavlc(H264Context *h){
04497 MpegEncContext * const s = &h->s;
04498 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
04499 int partition_count;
04500 unsigned int mb_type, cbp;
04501 int dct8x8_allowed= h->pps.transform_8x8_mode;
04502
04503 s->dsp.clear_blocks(h->mb);
04504
04505 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
04506 cbp = 0;
04507
04508 if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
04509 if(s->mb_skip_run==-1)
04510 s->mb_skip_run= get_ue_golomb(&s->gb);
04511
04512 if (s->mb_skip_run--) {
04513 if(FRAME_MBAFF && (s->mb_y&1) == 0){
04514 if(s->mb_skip_run==0)
04515 h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
04516 else
04517 predict_field_decoding_flag(h);
04518 }
04519 decode_mb_skip(h);
04520 return 0;
04521 }
04522 }
04523 if(FRAME_MBAFF){
04524 if( (s->mb_y&1) == 0 )
04525 h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
04526 }else
04527 h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
04528
04529 h->prev_mb_skipped= 0;
04530
04531 mb_type= get_ue_golomb(&s->gb);
04532 if(h->slice_type == B_TYPE){
04533 if(mb_type < 23){
04534 partition_count= b_mb_type_info[mb_type].partition_count;
04535 mb_type= b_mb_type_info[mb_type].type;
04536 }else{
04537 mb_type -= 23;
04538 goto decode_intra_mb;
04539 }
04540 }else if(h->slice_type == P_TYPE ){
04541 if(mb_type < 5){
04542 partition_count= p_mb_type_info[mb_type].partition_count;
04543 mb_type= p_mb_type_info[mb_type].type;
04544 }else{
04545 mb_type -= 5;
04546 goto decode_intra_mb;
04547 }
04548 }else{
04549 assert(h->slice_type == I_TYPE);
04550 decode_intra_mb:
04551 if(mb_type > 25){
04552 av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
04553 return -1;
04554 }
04555 partition_count=0;
04556 cbp= i_mb_type_info[mb_type].cbp;
04557 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
04558 mb_type= i_mb_type_info[mb_type].type;
04559 }
04560
04561 if(MB_FIELD)
04562 mb_type |= MB_TYPE_INTERLACED;
04563
04564 h->slice_table[ mb_xy ]= h->slice_num;
04565
04566 if(IS_INTRA_PCM(mb_type)){
04567 unsigned int x, y;
04568
04569
04570 align_get_bits(&s->gb);
04571
04572
04573 for(y=0; y<16; y++){
04574 const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
04575 for(x=0; x<16; x++){
04576 tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
04577 h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
04578 }
04579 }
04580 for(y=0; y<8; y++){
04581 const int index= 256 + 4*(y&3) + 32*(y>>2);
04582 for(x=0; x<8; x++){
04583 tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
04584 h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
04585 }
04586 }
04587 for(y=0; y<8; y++){
04588 const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
04589 for(x=0; x<8; x++){
04590 tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
04591 h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
04592 }
04593 }
04594
04595
04596 s->current_picture.qscale_table[mb_xy]= 0;
04597 h->chroma_qp[0] = get_chroma_qp(h, 0, 0);
04598 h->chroma_qp[1] = get_chroma_qp(h, 1, 0);
04599
04600 memset(h->non_zero_count[mb_xy], 16, 16);
04601
04602 s->current_picture.mb_type[mb_xy]= mb_type;
04603 return 0;
04604 }
04605
04606 if(MB_MBAFF){
04607 h->ref_count[0] <<= 1;
04608 h->ref_count[1] <<= 1;
04609 }
04610
04611 fill_caches(h, mb_type, 0);
04612
04613
04614 if(IS_INTRA(mb_type)){
04615 int pred_mode;
04616
04617 if(IS_INTRA4x4(mb_type)){
04618 int i;
04619 int di = 1;
04620 if(dct8x8_allowed && get_bits1(&s->gb)){
04621 mb_type |= MB_TYPE_8x8DCT;
04622 di = 4;
04623 }
04624
04625
04626 for(i=0; i<16; i+=di){
04627 int mode= pred_intra_mode(h, i);
04628
04629 if(!get_bits1(&s->gb)){
04630 const int rem_mode= get_bits(&s->gb, 3);
04631 mode = rem_mode + (rem_mode >= mode);
04632 }
04633
04634 if(di==4)
04635 fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
04636 else
04637 h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
04638 }
04639 write_back_intra_pred_mode(h);
04640 if( check_intra4x4_pred_mode(h) < 0)
04641 return -1;
04642 }else{
04643 h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
04644 if(h->intra16x16_pred_mode < 0)
04645 return -1;
04646 }
04647
04648 pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
04649 if(pred_mode < 0)
04650 return -1;
04651 h->chroma_pred_mode= pred_mode;
04652 }else if(partition_count==4){
04653 int i, j, sub_partition_count[4], list, ref[2][4];
04654
04655 if(h->slice_type == B_TYPE){
04656 for(i=0; i<4; i++){
04657 h->sub_mb_type[i]= get_ue_golomb(&s->gb);
04658 if(h->sub_mb_type[i] >=13){
04659 av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
04660 return -1;
04661 }
04662 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
04663 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
04664 }
04665 if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
04666 || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
04667 pred_direct_motion(h, &mb_type);
04668 h->ref_cache[0][scan8[4]] =
04669 h->ref_cache[1][scan8[4]] =
04670 h->ref_cache[0][scan8[12]] =
04671 h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
04672 }
04673 }else{
04674 assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE);
04675 for(i=0; i<4; i++){
04676 h->sub_mb_type[i]= get_ue_golomb(&s->gb);
04677 if(h->sub_mb_type[i] >=4){
04678 av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
04679 return -1;
04680 }
04681 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
04682 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
04683 }
04684 }
04685
04686 for(list=0; list<h->list_count; list++){
04687 int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
04688 for(i=0; i<4; i++){
04689 if(IS_DIRECT(h->sub_mb_type[i])) continue;
04690 if(IS_DIR(h->sub_mb_type[i], 0, list)){
04691 unsigned int tmp = get_te0_golomb(&s->gb, ref_count);
04692 if(tmp>=ref_count){
04693 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
04694 return -1;
04695 }
04696 ref[list][i]= tmp;
04697 }else{
04698
04699 ref[list][i] = -1;
04700 }
04701 }
04702 }
04703
04704 if(dct8x8_allowed)
04705 dct8x8_allowed = get_dct8x8_allowed(h);
04706
04707 for(list=0; list<h->list_count; list++){
04708 for(i=0; i<4; i++){
04709 if(IS_DIRECT(h->sub_mb_type[i])) {
04710 h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
04711 continue;
04712 }
04713 h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
04714 h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
04715
04716 if(IS_DIR(h->sub_mb_type[i], 0, list)){
04717 const int sub_mb_type= h->sub_mb_type[i];
04718 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
04719 for(j=0; j<sub_partition_count[i]; j++){
04720 int mx, my;
04721 const int index= 4*i + block_width*j;
04722 int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
04723 pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
04724 mx += get_se_golomb(&s->gb);
04725 my += get_se_golomb(&s->gb);
04726 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
04727
04728 if(IS_SUB_8X8(sub_mb_type)){
04729 mv_cache[ 1 ][0]=
04730 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
04731 mv_cache[ 1 ][1]=
04732 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
04733 }else if(IS_SUB_8X4(sub_mb_type)){
04734 mv_cache[ 1 ][0]= mx;
04735 mv_cache[ 1 ][1]= my;
04736 }else if(IS_SUB_4X8(sub_mb_type)){
04737 mv_cache[ 8 ][0]= mx;
04738 mv_cache[ 8 ][1]= my;
04739 }
04740 mv_cache[ 0 ][0]= mx;
04741 mv_cache[ 0 ][1]= my;
04742 }
04743 }else{
04744 uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
04745 p[0] = p[1]=
04746 p[8] = p[9]= 0;
04747 }
04748 }
04749 }
04750 }else if(IS_DIRECT(mb_type)){
04751 pred_direct_motion(h, &mb_type);
04752 dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
04753 }else{
04754 int list, mx, my, i;
04755
04756 if(IS_16X16(mb_type)){
04757 for(list=0; list<h->list_count; list++){
04758 unsigned int val;
04759 if(IS_DIR(mb_type, 0, list)){
04760 val= get_te0_golomb(&s->gb, h->ref_count[list]);
04761 if(val >= h->ref_count[list]){
04762 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
04763 return -1;
04764 }
04765 }else
04766 val= LIST_NOT_USED&0xFF;
04767 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
04768 }
04769 for(list=0; list<h->list_count; list++){
04770 unsigned int val;
04771 if(IS_DIR(mb_type, 0, list)){
04772 pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
04773 mx += get_se_golomb(&s->gb);
04774 my += get_se_golomb(&s->gb);
04775 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
04776
04777 val= pack16to32(mx,my);
04778 }else
04779 val=0;
04780 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
04781 }
04782 }
04783 else if(IS_16X8(mb_type)){
04784 for(list=0; list<h->list_count; list++){
04785 for(i=0; i<2; i++){
04786 unsigned int val;
04787 if(IS_DIR(mb_type, i, list)){
04788 val= get_te0_golomb(&s->gb, h->ref_count[list]);
04789 if(val >= h->ref_count[list]){
04790 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
04791 return -1;
04792 }
04793 }else
04794 val= LIST_NOT_USED&0xFF;
04795 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
04796 }
04797 }
04798 for(list=0; list<h->list_count; list++){
04799 for(i=0; i<2; i++){
04800 unsigned int val;
04801 if(IS_DIR(mb_type, i, list)){
04802 pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
04803 mx += get_se_golomb(&s->gb);
04804 my += get_se_golomb(&s->gb);
04805 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
04806
04807 val= pack16to32(mx,my);
04808 }else
04809 val=0;
04810 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
04811 }
04812 }
04813 }else{
04814 assert(IS_8X16(mb_type));
04815 for(list=0; list<h->list_count; list++){
04816 for(i=0; i<2; i++){
04817 unsigned int val;
04818 if(IS_DIR(mb_type, i, list)){
04819 val= get_te0_golomb(&s->gb, h->ref_count[list]);
04820 if(val >= h->ref_count[list]){
04821 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
04822 return -1;
04823 }
04824 }else
04825 val= LIST_NOT_USED&0xFF;
04826 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
04827 }
04828 }
04829 for(list=0; list<h->list_count; list++){
04830 for(i=0; i<2; i++){
04831 unsigned int val;
04832 if(IS_DIR(mb_type, i, list)){
04833 pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
04834 mx += get_se_golomb(&s->gb);
04835 my += get_se_golomb(&s->gb);
04836 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
04837
04838 val= pack16to32(mx,my);
04839 }else
04840 val=0;
04841 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
04842 }
04843 }
04844 }
04845 }
04846
04847 if(IS_INTER(mb_type))
04848 write_back_motion(h, mb_type);
04849
04850 if(!IS_INTRA16x16(mb_type)){
04851 cbp= get_ue_golomb(&s->gb);
04852 if(cbp > 47){
04853 av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
04854 return -1;
04855 }
04856
04857 if(IS_INTRA4x4(mb_type))
04858 cbp= golomb_to_intra4x4_cbp[cbp];
04859 else
04860 cbp= golomb_to_inter_cbp[cbp];
04861 }
04862 h->cbp = cbp;
04863
04864 if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
04865 if(get_bits1(&s->gb))
04866 mb_type |= MB_TYPE_8x8DCT;
04867 }
04868 s->current_picture.mb_type[mb_xy]= mb_type;
04869
04870 if(cbp || IS_INTRA16x16(mb_type)){
04871 int i8x8, i4x4, chroma_idx;
04872 int dquant;
04873 GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
04874 const uint8_t *scan, *scan8x8, *dc_scan;
04875
04876
04877
04878 if(IS_INTERLACED(mb_type)){
04879 scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
04880 scan= s->qscale ? h->field_scan : h->field_scan_q0;
04881 dc_scan= luma_dc_field_scan;
04882 }else{
04883 scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
04884 scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
04885 dc_scan= luma_dc_zigzag_scan;
04886 }
04887
04888 dquant= get_se_golomb(&s->gb);
04889
04890 if( dquant > 25 || dquant < -26 ){
04891 av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
04892 return -1;
04893 }
04894
04895 s->qscale += dquant;
04896 if(((unsigned)s->qscale) > 51){
04897 if(s->qscale<0) s->qscale+= 52;
04898 else s->qscale-= 52;
04899 }
04900
04901 h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
04902 h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
04903 if(IS_INTRA16x16(mb_type)){
04904 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
04905 return -1;
04906 }
04907
04908 assert((cbp&15) == 0 || (cbp&15) == 15);
04909
04910 if(cbp&15){
04911 for(i8x8=0; i8x8<4; i8x8++){
04912 for(i4x4=0; i4x4<4; i4x4++){
04913 const int index= i4x4 + 4*i8x8;
04914 if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
04915 return -1;
04916 }
04917 }
04918 }
04919 }else{
04920 fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
04921 }
04922 }else{
04923 for(i8x8=0; i8x8<4; i8x8++){
04924 if(cbp & (1<<i8x8)){
04925 if(IS_8x8DCT(mb_type)){
04926 DCTELEM *buf = &h->mb[64*i8x8];
04927 uint8_t *nnz;
04928 for(i4x4=0; i4x4<4; i4x4++){
04929 if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
04930 h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
04931 return -1;
04932 }
04933 nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
04934 nnz[0] += nnz[1] + nnz[8] + nnz[9];
04935 }else{
04936 for(i4x4=0; i4x4<4; i4x4++){
04937 const int index= i4x4 + 4*i8x8;
04938
04939 if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
04940 return -1;
04941 }
04942 }
04943 }
04944 }else{
04945 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
04946 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
04947 }
04948 }
04949 }
04950
04951 if(cbp&0x30){
04952 for(chroma_idx=0; chroma_idx<2; chroma_idx++)
04953 if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
04954 return -1;
04955 }
04956 }
04957
04958 if(cbp&0x20){
04959 for(chroma_idx=0; chroma_idx<2; chroma_idx++){
04960 const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
04961 for(i4x4=0; i4x4<4; i4x4++){
04962 const int index= 16 + 4*chroma_idx + i4x4;
04963 if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
04964 return -1;
04965 }
04966 }
04967 }
04968 }else{
04969 uint8_t * const nnz= &h->non_zero_count_cache[0];
04970 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
04971 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
04972 }
04973 }else{
04974 uint8_t * const nnz= &h->non_zero_count_cache[0];
04975 fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
04976 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
04977 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
04978 }
04979 s->current_picture.qscale_table[mb_xy]= s->qscale;
04980 write_back_non_zero_count(h);
04981
04982 if(MB_MBAFF){
04983 h->ref_count[0] >>= 1;
04984 h->ref_count[1] >>= 1;
04985 }
04986
04987 return 0;
04988 }
04989
04990 static int decode_cabac_field_decoding_flag(H264Context *h) {
04991 MpegEncContext * const s = &h->s;
04992 const int mb_x = s->mb_x;
04993 const int mb_y = s->mb_y & ~1;
04994 const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
04995 const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
04996
04997 unsigned int ctx = 0;
04998
04999 if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
05000 ctx += 1;
05001 }
05002 if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
05003 ctx += 1;
05004 }
05005
05006 return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
05007 }
05008
05009 static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
05010 uint8_t *state= &h->cabac_state[ctx_base];
05011 int mb_type;
05012
05013 if(intra_slice){
05014 MpegEncContext * const s = &h->s;
05015 const int mba_xy = h->left_mb_xy[0];
05016 const int mbb_xy = h->top_mb_xy;
05017 int ctx=0;
05018 if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
05019 ctx++;
05020 if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
05021 ctx++;
05022 if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
05023 return 0;
05024 state += 2;
05025 }else{
05026 if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
05027 return 0;
05028 }
05029
05030 if( get_cabac_terminate( &h->cabac ) )
05031 return 25;
05032
05033 mb_type = 1;
05034 mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] );
05035 if( get_cabac_noinline( &h->cabac, &state[2] ) )
05036 mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
05037 mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
05038 mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
05039 return mb_type;
05040 }
05041
05042 static int decode_cabac_mb_type( H264Context *h ) {
05043 MpegEncContext * const s = &h->s;
05044
05045 if( h->slice_type == I_TYPE ) {
05046 return decode_cabac_intra_mb_type(h, 3, 1);
05047 } else if( h->slice_type == P_TYPE ) {
05048 if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
05049
05050 if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
05051
05052 return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
05053 } else {
05054
05055 return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
05056 }
05057 } else {
05058 return decode_cabac_intra_mb_type(h, 17, 0) + 5;
05059 }
05060 } else if( h->slice_type == B_TYPE ) {
05061 const int mba_xy = h->left_mb_xy[0];
05062 const int mbb_xy = h->top_mb_xy;
05063 int ctx = 0;
05064 int bits;
05065
05066 if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
05067 ctx++;
05068 if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
05069 ctx++;
05070
05071 if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
05072 return 0;
05073
05074 if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
05075 return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
05076 }
05077
05078 bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
05079 bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
05080 bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
05081 bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
05082 if( bits < 8 )
05083 return bits + 3;
05084 else if( bits == 13 ) {
05085 return decode_cabac_intra_mb_type(h, 32, 0) + 23;
05086 } else if( bits == 14 )
05087 return 11;
05088 else if( bits == 15 )
05089 return 22;
05090
05091 bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
05092 return bits - 4;
05093 } else {
05094
05095 return -1;
05096 }
05097 }
05098
05099 static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
05100 MpegEncContext * const s = &h->s;
05101 int mba_xy, mbb_xy;
05102 int ctx = 0;
05103
05104 if(FRAME_MBAFF){
05105 int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
05106 mba_xy = mb_xy - 1;
05107 if( (mb_y&1)
05108 && h->slice_table[mba_xy] == h->slice_num
05109 && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
05110 mba_xy += s->mb_stride;
05111 if( MB_FIELD ){
05112 mbb_xy = mb_xy - s->mb_stride;
05113 if( !(mb_y&1)
05114 && h->slice_table[mbb_xy] == h->slice_num
05115 && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
05116 mbb_xy -= s->mb_stride;
05117 }else
05118 mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
05119 }else{
05120 int mb_xy = mb_x + mb_y*s->mb_stride;
05121 mba_xy = mb_xy - 1;
05122 mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
05123 }
05124
05125 if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
05126 ctx++;
05127 if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
05128 ctx++;
05129
05130 if( h->slice_type == B_TYPE )
05131 ctx += 13;
05132 return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
05133 }
05134
05135 static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
05136 int mode = 0;
05137
05138 if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
05139 return pred_mode;
05140
05141 mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
05142 mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
05143 mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
05144
05145 if( mode >= pred_mode )
05146 return mode + 1;
05147 else
05148 return mode;
05149 }
05150
05151 static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
05152 const int mba_xy = h->left_mb_xy[0];
05153 const int mbb_xy = h->top_mb_xy;
05154
05155 int ctx = 0;
05156
05157
05158 if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
05159 ctx++;
05160
05161 if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
05162 ctx++;
05163
05164 if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
05165 return 0;
05166
05167 if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
05168 return 1;
05169 if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
05170 return 2;
05171 else
05172 return 3;
05173 }
05174
05175 static int decode_cabac_mb_cbp_luma( H264Context *h) {
05176 int cbp_b, cbp_a, ctx, cbp = 0;
05177
05178 cbp_a = h->slice_table[h->left_mb_xy[0]] == h->slice_num ? h->left_cbp : -1;
05179 cbp_b = h->slice_table[h->top_mb_xy] == h->slice_num ? h->top_cbp : -1;
05180
05181 ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04);
05182 cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]);
05183 ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08);
05184 cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1;
05185 ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01);
05186 cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2;
05187 ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02);
05188 cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3;
05189 return cbp;
05190 }
05191 static int decode_cabac_mb_cbp_chroma( H264Context *h) {
05192 int ctx;
05193 int cbp_a, cbp_b;
05194
05195 cbp_a = (h->left_cbp>>4)&0x03;
05196 cbp_b = (h-> top_cbp>>4)&0x03;
05197
05198 ctx = 0;
05199 if( cbp_a > 0 ) ctx++;
05200 if( cbp_b > 0 ) ctx += 2;
05201 if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
05202 return 0;
05203
05204 ctx = 4;
05205 if( cbp_a == 2 ) ctx++;
05206 if( cbp_b == 2 ) ctx += 2;
05207 return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
05208 }
05209 static int decode_cabac_mb_dqp( H264Context *h) {
05210 int ctx = 0;
05211 int val = 0;
05212
05213 if( h->last_qscale_diff != 0 )
05214 ctx++;
05215
05216 while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
05217 if( ctx < 2 )
05218 ctx = 2;
05219 else
05220 ctx = 3;
05221 val++;
05222 if(val > 102)
05223 return INT_MIN;
05224 }
05225
05226 if( val&0x01 )
05227 return (val + 1)/2;
05228 else
05229 return -(val + 1)/2;
05230 }
05231 static int decode_cabac_p_mb_sub_type( H264Context *h ) {
05232 if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
05233 return 0;
05234 if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
05235 return 1;
05236 if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
05237 return 2;
05238 return 3;
05239 }
05240 static int decode_cabac_b_mb_sub_type( H264Context *h ) {
05241 int type;
05242 if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
05243 return 0;
05244 if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
05245 return 1 + get_cabac( &h->cabac, &h->cabac_state[39] );
05246 type = 3;
05247 if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
05248 if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
05249 return 11 + get_cabac( &h->cabac, &h->cabac_state[39] );
05250 type += 4;
05251 }
05252 type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
05253 type += get_cabac( &h->cabac, &h->cabac_state[39] );
05254 return type;
05255 }
05256
05257 static inline int decode_cabac_mb_transform_size( H264Context *h ) {
05258 return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
05259 }
05260
05261 static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
05262 int refa = h->ref_cache[list][scan8[n] - 1];
05263 int refb = h->ref_cache[list][scan8[n] - 8];
05264 int ref = 0;
05265 int ctx = 0;
05266
05267 if( h->slice_type == B_TYPE) {
05268 if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
05269 ctx++;
05270 if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
05271 ctx += 2;
05272 } else {
05273 if( refa > 0 )
05274 ctx++;
05275 if( refb > 0 )
05276 ctx += 2;
05277 }
05278
05279 while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
05280 ref++;
05281 if( ctx < 4 )
05282 ctx = 4;
05283 else
05284 ctx = 5;
05285 if(ref >= 32 ){
05286 av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
05287 return 0;
05288 }
05289 }
05290 return ref;
05291 }
05292
05293 static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
05294 int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
05295 abs( h->mvd_cache[list][scan8[n] - 8][l] );
05296 int ctxbase = (l == 0) ? 40 : 47;
05297 int ctx, mvd;
05298
05299 if( amvd < 3 )
05300 ctx = 0;
05301 else if( amvd > 32 )
05302 ctx = 2;
05303 else
05304 ctx = 1;
05305
05306 if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
05307 return 0;
05308
05309 mvd= 1;
05310 ctx= 3;
05311 while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
05312 mvd++;
05313 if( ctx < 6 )
05314 ctx++;
05315 }
05316
05317 if( mvd >= 9 ) {
05318 int k = 3;
05319 while( get_cabac_bypass( &h->cabac ) ) {
05320 mvd += 1 << k;
05321 k++;
05322 if(k>24){
05323 av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
05324 return INT_MIN;
05325 }
05326 }
05327 while( k-- ) {
05328 if( get_cabac_bypass( &h->cabac ) )
05329 mvd += 1 << k;
05330 }
05331 }
05332 return get_cabac_bypass_sign( &h->cabac, -mvd );
05333 }
05334
05335 static inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
05336 int nza, nzb;
05337 int ctx = 0;
05338
05339 if( cat == 0 ) {
05340 nza = h->left_cbp&0x100;
05341 nzb = h-> top_cbp&0x100;
05342 } else if( cat == 1 || cat == 2 ) {
05343 nza = h->non_zero_count_cache[scan8[idx] - 1];
05344 nzb = h->non_zero_count_cache[scan8[idx] - 8];
05345 } else if( cat == 3 ) {
05346 nza = (h->left_cbp>>(6+idx))&0x01;
05347 nzb = (h-> top_cbp>>(6+idx))&0x01;
05348 } else {
05349 assert(cat == 4);
05350 nza = h->non_zero_count_cache[scan8[16+idx] - 1];
05351 nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
05352 }
05353
05354 if( nza > 0 )
05355 ctx++;
05356
05357 if( nzb > 0 )
05358 ctx += 2;
05359
05360 return ctx + 4 * cat;
05361 }
05362
05363 DECLARE_ASM_CONST(1, const uint8_t, last_coeff_flag_offset_8x8[63]) = {
05364 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
05365 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
05366 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
05367 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
05368 };
05369
05370 static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
05371 const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
05372 static const int significant_coeff_flag_offset[2][6] = {
05373 { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
05374 { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
05375 };
05376 static const int last_coeff_flag_offset[2][6] = {
05377 { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
05378 { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
05379 };
05380 static const int coeff_abs_level_m1_offset[6] = {
05381 227+0, 227+10, 227+20, 227+30, 227+39, 426
05382 };
05383 static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
05384 { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
05385 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
05386 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
05387 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
05388 { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
05389 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
05390 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
05391 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
05392 };
05393
05394 int index[64];
05395
05396 int av_unused last;
05397 int coeff_count = 0;
05398
05399 int abslevel1 = 1;
05400 int abslevelgt1 = 0;
05401
05402 uint8_t *significant_coeff_ctx_base;
05403 uint8_t *last_coeff_ctx_base;
05404 uint8_t *abs_level_m1_ctx_base;
05405
05406 #ifndef ARCH_X86
05407 #define CABAC_ON_STACK
05408 #endif
05409 #ifdef CABAC_ON_STACK
05410 #define CC &cc
05411 CABACContext cc;
05412 cc.range = h->cabac.range;
05413 cc.low = h->cabac.low;
05414 cc.bytestream= h->cabac.bytestream;
05415 #else
05416 #define CC &h->cabac
05417 #endif
05418
05419
05420
05421
05422
05423
05424
05425
05426
05427
05428
05429 if( cat != 5 ) {
05430 if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
05431 if( cat == 1 || cat == 2 )
05432 h->non_zero_count_cache[scan8[n]] = 0;
05433 else if( cat == 4 )
05434 h->non_zero_count_cache[scan8[16+n]] = 0;
05435 #ifdef CABAC_ON_STACK
05436 h->cabac.range = cc.range ;
05437 h->cabac.low = cc.low ;
05438 h->cabac.bytestream= cc.bytestream;
05439 #endif
05440 return;
05441 }
05442 }
05443
05444 significant_coeff_ctx_base = h->cabac_state
05445 + significant_coeff_flag_offset[MB_FIELD][cat];
05446 last_coeff_ctx_base = h->cabac_state
05447 + last_coeff_flag_offset[MB_FIELD][cat];
05448 abs_level_m1_ctx_base = h->cabac_state
05449 + coeff_abs_level_m1_offset[cat];
05450
05451 if( cat == 5 ) {
05452 #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
05453 for(last= 0; last < coefs; last++) { \
05454 uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
05455 if( get_cabac( CC, sig_ctx )) { \
05456 uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
05457 index[coeff_count++] = last; \
05458 if( get_cabac( CC, last_ctx ) ) { \
05459 last= max_coeff; \
05460 break; \
05461 } \
05462 } \
05463 }\
05464 if( last == max_coeff -1 ) {\
05465 index[coeff_count++] = last;\
05466 }
05467 const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
05468 #if defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
05469 coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
05470 } else {
05471 coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
05472 #else
05473 DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
05474 } else {
05475 DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
05476 #endif
05477 }
05478 assert(coeff_count > 0);
05479
05480 if( cat == 0 )
05481 h->cbp_table[mb_xy] |= 0x100;
05482 else if( cat == 1 || cat == 2 )
05483 h->non_zero_count_cache[scan8[n]] = coeff_count;
05484 else if( cat == 3 )
05485 h->cbp_table[mb_xy] |= 0x40 << n;
05486 else if( cat == 4 )
05487 h->non_zero_count_cache[scan8[16+n]] = coeff_count;
05488 else {
05489 assert( cat == 5 );
05490 fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
05491 }
05492
05493 for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
05494 uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
05495 int j= scantable[index[coeff_count]];
05496
05497 if( get_cabac( CC, ctx ) == 0 ) {
05498 if( !qmul ) {
05499 block[j] = get_cabac_bypass_sign( CC, -1);
05500 }else{
05501 block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
05502 }
05503
05504 abslevel1++;
05505 } else {
05506 int coeff_abs = 2;
05507 ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
05508 while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
05509 coeff_abs++;
05510 }
05511
05512 if( coeff_abs >= 15 ) {
05513 int j = 0;
05514 while( get_cabac_bypass( CC ) ) {
05515 j++;
05516 }
05517
05518 coeff_abs=1;
05519 while( j-- ) {
05520 coeff_abs += coeff_abs + get_cabac_bypass( CC );
05521 }
05522 coeff_abs+= 14;
05523 }
05524
05525 if( !qmul ) {
05526 if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
05527 else block[j] = coeff_abs;
05528 }else{
05529 if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
05530 else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
05531 }
05532
05533 abslevelgt1++;
05534 }
05535 }
05536 #ifdef CABAC_ON_STACK
05537 h->cabac.range = cc.range ;
05538 h->cabac.low = cc.low ;
05539 h->cabac.bytestream= cc.bytestream;
05540 #endif
05541
05542 }
05543
05544 static inline void compute_mb_neighbors(H264Context *h)
05545 {
05546 MpegEncContext * const s = &h->s;
05547 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
05548 h->top_mb_xy = mb_xy - s->mb_stride;
05549 h->left_mb_xy[0] = mb_xy - 1;
05550 if(FRAME_MBAFF){
05551 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
05552 const int top_pair_xy = pair_xy - s->mb_stride;
05553 const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
05554 const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
05555 const int curr_mb_frame_flag = !MB_FIELD;
05556 const int bottom = (s->mb_y & 1);
05557 if (bottom
05558 ? !curr_mb_frame_flag
05559 : (!curr_mb_frame_flag && !top_mb_frame_flag)
05560 ) {
05561 h->top_mb_xy -= s->mb_stride;
05562 }
05563 if (left_mb_frame_flag != curr_mb_frame_flag) {
05564 h->left_mb_xy[0] = pair_xy - 1;
05565 }
05566 } else if (FIELD_PICTURE) {
05567 h->top_mb_xy -= s->mb_stride;
05568 }
05569 return;
05570 }
05571
05576 static int decode_mb_cabac(H264Context *h) {
05577 MpegEncContext * const s = &h->s;
05578 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
05579 int mb_type, partition_count, cbp = 0;
05580 int dct8x8_allowed= h->pps.transform_8x8_mode;
05581
05582 s->dsp.clear_blocks(h->mb);
05583
05584 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
05585 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
05586 int skip;
05587
05588 if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
05589 predict_field_decoding_flag(h);
05590 if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
05591 skip = h->next_mb_skipped;
05592 else
05593 skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
05594
05595 if( skip ) {
05596 if( FRAME_MBAFF && (s->mb_y&1)==0 ){
05597 s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
05598 h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
05599 if(h->next_mb_skipped)
05600 predict_field_decoding_flag(h);
05601 else
05602 h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
05603 }
05604
05605 decode_mb_skip(h);
05606
05607 h->cbp_table[mb_xy] = 0;
05608 h->chroma_pred_mode_table[mb_xy] = 0;
05609 h->last_qscale_diff = 0;
05610
05611 return 0;
05612
05613 }
05614 }
05615 if(FRAME_MBAFF){
05616 if( (s->mb_y&1) == 0 )
05617 h->mb_mbaff =
05618 h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
05619 }else
05620 h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
05621
05622 h->prev_mb_skipped = 0;
05623
05624 compute_mb_neighbors(h);
05625 if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
05626 av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
05627 return -1;
05628 }
05629
05630 if( h->slice_type == B_TYPE ) {
05631 if( mb_type < 23 ){
05632 partition_count= b_mb_type_info[mb_type].partition_count;
05633 mb_type= b_mb_type_info[mb_type].type;
05634 }else{
05635 mb_type -= 23;
05636 goto decode_intra_mb;
05637 }
05638 } else if( h->slice_type == P_TYPE ) {
05639 if( mb_type < 5) {
05640 partition_count= p_mb_type_info[mb_type].partition_count;
05641 mb_type= p_mb_type_info[mb_type].type;
05642 } else {
05643 mb_type -= 5;
05644 goto decode_intra_mb;
05645 }
05646 } else {
05647 assert(h->slice_type == I_TYPE);
05648 decode_intra_mb:
05649 partition_count = 0;
05650 cbp= i_mb_type_info[mb_type].cbp;
05651 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
05652 mb_type= i_mb_type_info[mb_type].type;
05653 }
05654 if(MB_FIELD)
05655 mb_type |= MB_TYPE_INTERLACED;
05656
05657 h->slice_table[ mb_xy ]= h->slice_num;
05658
05659 if(IS_INTRA_PCM(mb_type)) {
05660 const uint8_t *ptr;
05661 unsigned int x, y;
05662
05663
05664
05665
05666 ptr= h->cabac.bytestream;
05667 if(h->cabac.low&0x1) ptr--;
05668 if(CABAC_BITS==16){
05669 if(h->cabac.low&0x1FF) ptr--;
05670 }
05671
05672
05673 for(y=0; y<16; y++){
05674 const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
05675 for(x=0; x<16; x++){
05676 tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", *ptr);
05677 h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
05678 }
05679 }
05680 for(y=0; y<8; y++){
05681 const int index= 256 + 4*(y&3) + 32*(y>>2);
05682 for(x=0; x<8; x++){
05683 tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", *ptr);
05684 h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
05685 }
05686 }
05687 for(y=0; y<8; y++){
05688 const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
05689 for(x=0; x<8; x++){
05690 tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", *ptr);
05691 h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
05692 }
05693 }
05694
05695 ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
05696
05697
05698 h->cbp_table[mb_xy] = 0x1ef;
05699 h->chroma_pred_mode_table[mb_xy] = 0;
05700
05701 s->current_picture.qscale_table[mb_xy]= 0;
05702 h->chroma_qp[0] = get_chroma_qp(h, 0, 0);
05703 h->chroma_qp[1] = get_chroma_qp(h, 1, 0);
05704
05705 memset(h->non_zero_count[mb_xy], 16, 16);
05706 s->current_picture.mb_type[mb_xy]= mb_type;
05707 return 0;
05708 }
05709
05710 if(MB_MBAFF){
05711 h->ref_count[0] <<= 1;
05712 h->ref_count[1] <<= 1;
05713 }
05714
05715 fill_caches(h, mb_type, 0);
05716
05717 if( IS_INTRA( mb_type ) ) {
05718 int i, pred_mode;
05719 if( IS_INTRA4x4( mb_type ) ) {
05720 if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
05721 mb_type |= MB_TYPE_8x8DCT;
05722 for( i = 0; i < 16; i+=4 ) {
05723 int pred = pred_intra_mode( h, i );
05724 int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
05725 fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
05726 }
05727 } else {
05728 for( i = 0; i < 16; i++ ) {
05729 int pred = pred_intra_mode( h, i );
05730 h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
05731
05732
05733 }
05734 }
05735 write_back_intra_pred_mode(h);
05736 if( check_intra4x4_pred_mode(h) < 0 ) return -1;
05737 } else {
05738 h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
05739 if( h->intra16x16_pred_mode < 0 ) return -1;
05740 }
05741 h->chroma_pred_mode_table[mb_xy] =
05742 pred_mode = decode_cabac_mb_chroma_pre_mode( h );
05743
05744 pred_mode= check_intra_pred_mode( h, pred_mode );
05745 if( pred_mode < 0 ) return -1;
05746 h->chroma_pred_mode= pred_mode;
05747 } else if( partition_count == 4 ) {
05748 int i, j, sub_partition_count[4], list, ref[2][4];
05749
05750 if( h->slice_type == B_TYPE ) {
05751 for( i = 0; i < 4; i++ ) {
05752 h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
05753 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
05754 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
05755 }
05756 if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
05757 h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
05758 pred_direct_motion(h, &mb_type);
05759 h->ref_cache[0][scan8[4]] =
05760 h->ref_cache[1][scan8[4]] =
05761 h->ref_cache[0][scan8[12]] =
05762 h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
05763 if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
05764 for( i = 0; i < 4; i++ )
05765 if( IS_DIRECT(h->sub_mb_type[i]) )
05766 fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
05767 }
05768 }
05769 } else {
05770 for( i = 0; i < 4; i++ ) {
05771 h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
05772 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
05773 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
05774 }
05775 }
05776
05777 for( list = 0; list < h->list_count; list++ ) {
05778 for( i = 0; i < 4; i++ ) {
05779 if(IS_DIRECT(h->sub_mb_type[i])) continue;
05780 if(IS_DIR(h->sub_mb_type[i], 0, list)){
05781 if( h->ref_count[list] > 1 )
05782 ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
05783 else
05784 ref[list][i] = 0;
05785 } else {
05786 ref[list][i] = -1;
05787 }
05788 h->ref_cache[list][ scan8[4*i]+1 ]=
05789 h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
05790 }
05791 }
05792
05793 if(dct8x8_allowed)
05794 dct8x8_allowed = get_dct8x8_allowed(h);
05795
05796 for(list=0; list<h->list_count; list++){
05797 for(i=0; i<4; i++){
05798 h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
05799 if(IS_DIRECT(h->sub_mb_type[i])){
05800 fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
05801 continue;
05802 }
05803
05804 if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
05805 const int sub_mb_type= h->sub_mb_type[i];
05806 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
05807 for(j=0; j<sub_partition_count[i]; j++){
05808 int mpx, mpy;
05809 int mx, my;
05810 const int index= 4*i + block_width*j;
05811 int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
05812 int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
05813 pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
05814
05815 mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
05816 my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
05817 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
05818
05819 if(IS_SUB_8X8(sub_mb_type)){
05820 mv_cache[ 1 ][0]=
05821 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
05822 mv_cache[ 1 ][1]=
05823 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
05824
05825 mvd_cache[ 1 ][0]=
05826 mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
05827 mvd_cache[ 1 ][1]=
05828 mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
05829 }else if(IS_SUB_8X4(sub_mb_type)){
05830 mv_cache[ 1 ][0]= mx;
05831 mv_cache[ 1 ][1]= my;
05832
05833 mvd_cache[ 1 ][0]= mx - mpx;
05834 mvd_cache[ 1 ][1]= my - mpy;
05835 }else if(IS_SUB_4X8(sub_mb_type)){
05836 mv_cache[ 8 ][0]= mx;
05837 mv_cache[ 8 ][1]= my;
05838
05839 mvd_cache[ 8 ][0]= mx - mpx;
05840 mvd_cache[ 8 ][1]= my - mpy;
05841 }
05842 mv_cache[ 0 ][0]= mx;
05843 mv_cache[ 0 ][1]= my;
05844
05845 mvd_cache[ 0 ][0]= mx - mpx;
05846 mvd_cache[ 0 ][1]= my - mpy;
05847 }
05848 }else{
05849 uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
05850 uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
05851 p[0] = p[1] = p[8] = p[9] = 0;
05852 pd[0]= pd[1]= pd[8]= pd[9]= 0;
05853 }
05854 }
05855 }
05856 } else if( IS_DIRECT(mb_type) ) {
05857 pred_direct_motion(h, &mb_type);
05858 fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
05859 fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
05860 dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
05861 } else {
05862 int list, mx, my, i, mpx, mpy;
05863 if(IS_16X16(mb_type)){
05864 for(list=0; list<h->list_count; list++){
05865 if(IS_DIR(mb_type, 0, list)){
05866 const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
05867 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
05868 }else
05869 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
05870 }
05871 for(list=0; list<h->list_count; list++){
05872 if(IS_DIR(mb_type, 0, list)){
05873 pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
05874
05875 mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
05876 my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
05877 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
05878
05879 fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
05880 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
05881 }else
05882 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
05883 }
05884 }
05885 else if(IS_16X8(mb_type)){
05886 for(list=0; list<h->list_count; list++){
05887 for(i=0; i<2; i++){
05888 if(IS_DIR(mb_type, i, list)){
05889 const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
05890 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
05891 }else
05892 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
05893 }
05894 }
05895 for(list=0; list<h->list_count; list++){
05896 for(i=0; i<2; i++){
05897 if(IS_DIR(mb_type, i, list)){
05898 pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
05899 mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
05900 my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
05901 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
05902
05903 fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
05904 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
05905 }else{
05906 fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
05907 fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
05908 }
05909 }
05910 }
05911 }else{
05912 assert(IS_8X16(mb_type));
05913 for(list=0; list<h->list_count; list++){
05914 for(i=0; i<2; i++){
05915 if(IS_DIR(mb_type, i, list)){
05916 const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
05917 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
05918 }else
05919 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
05920 }
05921 }
05922 for(list=0; list<h->list_count; list++){
05923 for(i=0; i<2; i++){
05924 if(IS_DIR(mb_type, i, list)){
05925 pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
05926 mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
05927 my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
05928
05929 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
05930 fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
05931 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
05932 }else{
05933 fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
05934 fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
05935 }
05936 }
05937 }
05938 }
05939 }
05940
05941 if( IS_INTER( mb_type ) ) {
05942 h->chroma_pred_mode_table[mb_xy] = 0;
05943 write_back_motion( h, mb_type );
05944 }
05945
05946 if( !IS_INTRA16x16( mb_type ) ) {
05947 cbp = decode_cabac_mb_cbp_luma( h );
05948 cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
05949 }
05950
05951 h->cbp_table[mb_xy] = h->cbp = cbp;
05952
05953 if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
05954 if( decode_cabac_mb_transform_size( h ) )
05955 mb_type |= MB_TYPE_8x8DCT;
05956 }
05957 s->current_picture.mb_type[mb_xy]= mb_type;
05958
05959 if( cbp || IS_INTRA16x16( mb_type ) ) {
05960 const uint8_t *scan, *scan8x8, *dc_scan;
05961 const uint32_t *qmul;
05962 int dqp;
05963
05964 if(IS_INTERLACED(mb_type)){
05965 scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
05966 scan= s->qscale ? h->field_scan : h->field_scan_q0;
05967 dc_scan= luma_dc_field_scan;
05968 }else{
05969 scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
05970 scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
05971 dc_scan= luma_dc_zigzag_scan;
05972 }
05973
05974 h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
05975 if( dqp == INT_MIN ){
05976 av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
05977 return -1;
05978 }
05979 s->qscale += dqp;
05980 if(((unsigned)s->qscale) > 51){
05981 if(s->qscale<0) s->qscale+= 52;
05982 else s->qscale-= 52;
05983 }
05984 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
05985 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
05986
05987 if( IS_INTRA16x16( mb_type ) ) {
05988 int i;
05989
05990 decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16);
05991
05992 if( cbp&15 ) {
05993 qmul = h->dequant4_coeff[0][s->qscale];
05994 for( i = 0; i < 16; i++ ) {
05995
05996 decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15);
05997 }
05998 } else {
05999 fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
06000 }
06001 } else {
06002 int i8x8, i4x4;
06003 for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
06004 if( cbp & (1<<i8x8) ) {
06005 if( IS_8x8DCT(mb_type) ) {
06006 decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
06007 scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64);
06008 } else {
06009 qmul = h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale];
06010 for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
06011 const int index = 4*i8x8 + i4x4;
06012
06013
06014 decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, qmul, 16);
06015
06016 }
06017 }
06018 } else {
06019 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
06020 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
06021 }
06022 }
06023 }
06024
06025 if( cbp&0x30 ){
06026 int c;
06027 for( c = 0; c < 2; c++ ) {
06028
06029 decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4);
06030 }
06031 }
06032
06033 if( cbp&0x20 ) {
06034 int c, i;
06035 for( c = 0; c < 2; c++ ) {
06036 qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];
06037 for( i = 0; i < 4; i++ ) {
06038 const int index = 16 + 4 * c + i;
06039
06040 decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, qmul, 15);
06041 }
06042 }
06043 } else {
06044 uint8_t * const nnz= &h->non_zero_count_cache[0];
06045 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
06046 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
06047 }
06048 } else {
06049 uint8_t * const nnz= &h->non_zero_count_cache[0];
06050 fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
06051 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
06052 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
06053 h->last_qscale_diff = 0;
06054 }
06055
06056 s->current_picture.qscale_table[mb_xy]= s->qscale;
06057 write_back_non_zero_count(h);
06058
06059 if(MB_MBAFF){
06060 h->ref_count[0] >>= 1;
06061 h->ref_count[1] >>= 1;
06062 }
06063
06064 return 0;
06065 }
06066
06067
06068 static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
06069 int i, d;
06070 const int index_a = qp + h->slice_alpha_c0_offset;
06071 const int alpha = (alpha_table+52)[index_a];
06072 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
06073
06074 if( bS[0] < 4 ) {
06075 int8_t tc[4];
06076 for(i=0; i<4; i++)
06077 tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
06078 h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
06079 } else {
06080
06081
06082 for( d = 0; d < 16; d++ ) {
06083 const int p0 = pix[-1];
06084 const int p1 = pix[-2];
06085 const int p2 = pix[-3];
06086
06087 const int q0 = pix[0];
06088 const int q1 = pix[1];
06089 const int q2 = pix[2];
06090
06091 if( FFABS( p0 - q0 ) < alpha &&
06092 FFABS( p1 - p0 ) < beta &&
06093 FFABS( q1 - q0 ) < beta ) {
06094
06095 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
06096 if( FFABS( p2 - p0 ) < beta)
06097 {
06098 const int p3 = pix[-4];
06099
06100 pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
06101 pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
06102 pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
06103 } else {
06104
06105 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06106 }
06107 if( FFABS( q2 - q0 ) < beta)
06108 {
06109 const int q3 = pix[3];
06110
06111 pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
06112 pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
06113 pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
06114 } else {
06115
06116 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06117 }
06118 }else{
06119
06120 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06121 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06122 }
06123 tprintf(h->s.avctx, "filter_mb_edgev i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, p2, p1, p0, q0, q1, q2, pix[-2], pix[-1], pix[0], pix[1]);
06124 }
06125 pix += stride;
06126 }
06127 }
06128 }
06129 static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
06130 int i;
06131 const int index_a = qp + h->slice_alpha_c0_offset;
06132 const int alpha = (alpha_table+52)[index_a];
06133 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
06134
06135 if( bS[0] < 4 ) {
06136 int8_t tc[4];
06137 for(i=0; i<4; i++)
06138 tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
06139 h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
06140 } else {
06141 h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
06142 }
06143 }
06144
06145 static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
06146 int i;
06147 for( i = 0; i < 16; i++, pix += stride) {
06148 int index_a;
06149 int alpha;
06150 int beta;
06151
06152 int qp_index;
06153 int bS_index = (i >> 1);
06154 if (!MB_FIELD) {
06155 bS_index &= ~1;
06156 bS_index |= (i & 1);
06157 }
06158
06159 if( bS[bS_index] == 0 ) {
06160 continue;
06161 }
06162
06163 qp_index = MB_FIELD ? (i >> 3) : (i & 1);
06164 index_a = qp[qp_index] + h->slice_alpha_c0_offset;
06165 alpha = (alpha_table+52)[index_a];
06166 beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
06167
06168 if( bS[bS_index] < 4 ) {
06169 const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
06170 const int p0 = pix[-1];
06171 const int p1 = pix[-2];
06172 const int p2 = pix[-3];
06173 const int q0 = pix[0];
06174 const int q1 = pix[1];
06175 const int q2 = pix[2];
06176
06177 if( FFABS( p0 - q0 ) < alpha &&
06178 FFABS( p1 - p0 ) < beta &&
06179 FFABS( q1 - q0 ) < beta ) {
06180 int tc = tc0;
06181 int i_delta;
06182
06183 if( FFABS( p2 - p0 ) < beta ) {
06184 pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
06185 tc++;
06186 }
06187 if( FFABS( q2 - q0 ) < beta ) {
06188 pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
06189 tc++;
06190 }
06191
06192 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
06193 pix[-1] = av_clip_uint8( p0 + i_delta );
06194 pix[0] = av_clip_uint8( q0 - i_delta );
06195 tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
06196 }
06197 }else{
06198 const int p0 = pix[-1];
06199 const int p1 = pix[-2];
06200 const int p2 = pix[-3];
06201
06202 const int q0 = pix[0];
06203 const int q1 = pix[1];
06204 const int q2 = pix[2];
06205
06206 if( FFABS( p0 - q0 ) < alpha &&
06207 FFABS( p1 - p0 ) < beta &&
06208 FFABS( q1 - q0 ) < beta ) {
06209
06210 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
06211 if( FFABS( p2 - p0 ) < beta)
06212 {
06213 const int p3 = pix[-4];
06214
06215 pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
06216 pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
06217 pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
06218 } else {
06219
06220 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06221 }
06222 if( FFABS( q2 - q0 ) < beta)
06223 {
06224 const int q3 = pix[3];
06225
06226 pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
06227 pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
06228 pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
06229 } else {
06230
06231 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06232 }
06233 }else{
06234
06235 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06236 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06237 }
06238 tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
06239 }
06240 }
06241 }
06242 }
06243 static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
06244 int i;
06245 for( i = 0; i < 8; i++, pix += stride) {
06246 int index_a;
06247 int alpha;
06248 int beta;
06249
06250 int qp_index;
06251 int bS_index = i;
06252
06253 if( bS[bS_index] == 0 ) {
06254 continue;
06255 }
06256
06257 qp_index = MB_FIELD ? (i >> 2) : (i & 1);
06258 index_a = qp[qp_index] + h->slice_alpha_c0_offset;
06259 alpha = (alpha_table+52)[index_a];
06260 beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
06261
06262 if( bS[bS_index] < 4 ) {
06263 const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
06264 const int p0 = pix[-1];
06265 const int p1 = pix[-2];
06266 const int q0 = pix[0];
06267 const int q1 = pix[1];
06268
06269 if( FFABS( p0 - q0 ) < alpha &&
06270 FFABS( p1 - p0 ) < beta &&
06271 FFABS( q1 - q0 ) < beta ) {
06272 const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
06273
06274 pix[-1] = av_clip_uint8( p0 + i_delta );
06275 pix[0] = av_clip_uint8( q0 - i_delta );
06276 tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
06277 }
06278 }else{
06279 const int p0 = pix[-1];
06280 const int p1 = pix[-2];
06281 const int q0 = pix[0];
06282 const int q1 = pix[1];
06283
06284 if( FFABS( p0 - q0 ) < alpha &&
06285 FFABS( p1 - p0 ) < beta &&
06286 FFABS( q1 - q0 ) < beta ) {
06287
06288 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06289 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06290 tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
06291 }
06292 }
06293 }
06294 }
06295
06296 static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
06297 int i, d;
06298 const int index_a = qp + h->slice_alpha_c0_offset;
06299 const int alpha = (alpha_table+52)[index_a];
06300 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
06301 const int pix_next = stride;
06302
06303 if( bS[0] < 4 ) {
06304 int8_t tc[4];
06305 for(i=0; i<4; i++)
06306 tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
06307 h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
06308 } else {
06309
06310 for( d = 0; d < 16; d++ ) {
06311 const int p0 = pix[-1*pix_next];
06312 const int p1 = pix[-2*pix_next];
06313 const int p2 = pix[-3*pix_next];
06314 const int q0 = pix[0];
06315 const int q1 = pix[1*pix_next];
06316 const int q2 = pix[2*pix_next];
06317
06318 if( FFABS( p0 - q0 ) < alpha &&
06319 FFABS( p1 - p0 ) < beta &&
06320 FFABS( q1 - q0 ) < beta ) {
06321
06322 const int p3 = pix[-4*pix_next];
06323 const int q3 = pix[ 3*pix_next];
06324
06325 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
06326 if( FFABS( p2 - p0 ) < beta) {
06327
06328 pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
06329 pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
06330 pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
06331 } else {
06332
06333 pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06334 }
06335 if( FFABS( q2 - q0 ) < beta) {
06336
06337 pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
06338 pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
06339 pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
06340 } else {
06341
06342 pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06343 }
06344 }else{
06345
06346 pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
06347 pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
06348 }
06349 tprintf(h->s.avctx, "filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
06350 }
06351 pix++;
06352 }
06353 }
06354 }
06355
06356 static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
06357 int i;
06358 const int index_a = qp + h->slice_alpha_c0_offset;
06359 const int alpha = (alpha_table+52)[index_a];
06360 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
06361
06362 if( bS[0] < 4 ) {
06363 int8_t tc[4];
06364 for(i=0; i<4; i++)
06365 tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
06366 h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
06367 } else {
06368 h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
06369 }
06370 }
06371
06372 static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
06373 MpegEncContext * const s = &h->s;
06374 int mb_y_firstrow = s->picture_structure == PICT_BOTTOM_FIELD;
06375 int mb_xy, mb_type;
06376 int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
06377
06378 mb_xy = mb_x + mb_y*s->mb_stride;
06379
06380 if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
06381 (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] ||
06382 h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
06383 filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
06384 return;
06385 }
06386 assert(!FRAME_MBAFF);
06387
06388 mb_type = s->current_picture.mb_type[mb_xy];
06389 qp = s->current_picture.qscale_table[mb_xy];
06390 qp0 = s->current_picture.qscale_table[mb_xy-1];
06391 qp1 = s->current_picture.qscale_table[h->top_mb_xy];
06392 qpc = get_chroma_qp( h, 0, qp );
06393 qpc0 = get_chroma_qp( h, 0, qp0 );
06394 qpc1 = get_chroma_qp( h, 0, qp1 );
06395 qp0 = (qp + qp0 + 1) >> 1;
06396 qp1 = (qp + qp1 + 1) >> 1;
06397 qpc0 = (qpc + qpc0 + 1) >> 1;
06398 qpc1 = (qpc + qpc1 + 1) >> 1;
06399 qp_thresh = 15 - h->slice_alpha_c0_offset;
06400 if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
06401 qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
06402 return;
06403
06404 if( IS_INTRA(mb_type) ) {
06405 int16_t bS4[4] = {4,4,4,4};
06406 int16_t bS3[4] = {3,3,3,3};
06407 int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
06408 if( IS_8x8DCT(mb_type) ) {
06409 filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
06410 filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
06411 filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
06412 filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
06413 } else {
06414 filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
06415 filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
06416 filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
06417 filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
06418 filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
06419 filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
06420 filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
06421 filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
06422 }
06423 filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
06424 filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
06425 filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
06426 filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
06427 filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
06428 filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
06429 filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
06430 filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
06431 return;
06432 } else {
06433 DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
06434 uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
06435 int edges;
06436 if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
06437 edges = 4;
06438 bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
06439 } else {
06440 int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
06441 (mb_type & MB_TYPE_16x8) ? 1 : 0;
06442 int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
06443 && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
06444 ? 3 : 0;
06445 int step = IS_8x8DCT(mb_type) ? 2 : 1;
06446 edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
06447 s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
06448 (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
06449 }
06450 if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
06451 bSv[0][0] = 0x0004000400040004ULL;
06452 if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
06453 bSv[1][0] = FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL;
06454
06455 #define FILTER(hv,dir,edge)\
06456 if(bSv[dir][edge]) {\
06457 filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
06458 if(!(edge&1)) {\
06459 filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
06460 filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
06461 }\
06462 }
06463 if( edges == 1 ) {
06464 FILTER(v,0,0);
06465 FILTER(h,1,0);
06466 } else if( IS_8x8DCT(mb_type) ) {
06467 FILTER(v,0,0);
06468 FILTER(v,0,2);
06469 FILTER(h,1,0);
06470 FILTER(h,1,2);
06471 } else {
06472 FILTER(v,0,0);
06473 FILTER(v,0,1);
06474 FILTER(v,0,2);
06475 FILTER(v,0,3);
06476 FILTER(h,1,0);
06477 FILTER(h,1,1);
06478 FILTER(h,1,2);
06479 FILTER(h,1,3);
06480 }
06481 #undef FILTER
06482 }
06483 }
06484
06485 static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
06486 MpegEncContext * const s = &h->s;
06487 const int mb_xy= mb_x + mb_y*s->mb_stride;
06488 const int mb_type = s->current_picture.mb_type[mb_xy];
06489 const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
06490 int first_vertical_edge_done = 0;
06491 int dir;
06492
06493
06494
06495 static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
06496 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
06497
06498
06499
06500 if(!FRAME_MBAFF){
06501 int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, FFMAX(h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]));
06502 int qp = s->current_picture.qscale_table[mb_xy];
06503 if(qp <= qp_thresh
06504 && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
06505 && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
06506 return;
06507 }
06508 }
06509
06510 if (FRAME_MBAFF
06511
06512 && h->slice_table[mb_xy-1] != 255
06513
06514 && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
06515
06516 && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
06517
06518
06519
06520 const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
06521 const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
06522 int16_t bS[8];
06523 int qp[2];
06524 int bqp[2];
06525 int rqp[2];
06526 int mb_qp, mbn0_qp, mbn1_qp;
06527 int i;
06528 first_vertical_edge_done = 1;
06529
06530 if( IS_INTRA(mb_type) )
06531 bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
06532 else {
06533 for( i = 0; i < 8; i++ ) {
06534 int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
06535
06536 if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
06537 bS[i] = 4;
06538 else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
06539
06540 h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
06541 bS[i] = 2;
06542 else
06543 bS[i] = 1;
06544 }
06545 }
06546
06547 mb_qp = s->current_picture.qscale_table[mb_xy];
06548 mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
06549 mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
06550 qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
06551 bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
06552 get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
06553 rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
06554 get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
06555 qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
06556 bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
06557 get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
06558 rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
06559 get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
06560
06561
06562 tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);
06563 { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
06564 filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
06565 filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, bqp );
06566 filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, rqp );
06567 }
06568
06569 for( dir = 0; dir < 2; dir++ )
06570 {
06571 int edge;
06572 const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
06573 const int mbm_type = s->current_picture.mb_type[mbm_xy];
06574 int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
06575
06576 const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
06577 == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
06578
06579 const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
06580 (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
06581
06582 const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
06583
06584 if (first_vertical_edge_done) {
06585 start = 1;
06586 first_vertical_edge_done = 0;
06587 }
06588
06589 if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
06590 start = 1;
06591
06592 if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
06593 && !IS_INTERLACED(mb_type)
06594 && IS_INTERLACED(mbm_type)
06595 ) {
06596
06597
06598
06599
06600 static const int nnz_idx[4] = {4,5,6,3};
06601 unsigned int tmp_linesize = 2 * linesize;
06602 unsigned int tmp_uvlinesize = 2 * uvlinesize;
06603 int mbn_xy = mb_xy - 2 * s->mb_stride;
06604 int qp;
06605 int i, j;
06606 int16_t bS[4];
06607
06608 for(j=0; j<2; j++, mbn_xy += s->mb_stride){
06609 if( IS_INTRA(mb_type) ||
06610 IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
06611 bS[0] = bS[1] = bS[2] = bS[3] = 3;
06612 } else {
06613 const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
06614 for( i = 0; i < 4; i++ ) {
06615 if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
06616 mbn_nnz[nnz_idx[i]] != 0 )
06617 bS[i] = 2;
06618 else
06619 bS[i] = 1;
06620 }
06621 }
06622
06623
06624 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
06625 tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
06626 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
06627 filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
06628 filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
06629 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
06630 filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
06631 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
06632 }
06633
06634 start = 1;
06635 }
06636
06637
06638 for( edge = start; edge < edges; edge++ ) {
06639
06640 const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
06641 const int mbn_type = s->current_picture.mb_type[mbn_xy];
06642 int16_t bS[4];
06643 int qp;
06644
06645 if( (edge&1) && IS_8x8DCT(mb_type) )
06646 continue;
06647
06648 if( IS_INTRA(mb_type) ||
06649 IS_INTRA(mbn_type) ) {
06650 int value;
06651 if (edge == 0) {
06652 if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
06653 || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
06654 ) {
06655 value = 4;
06656 } else {
06657 value = 3;
06658 }
06659 } else {
06660 value = 3;
06661 }
06662 bS[0] = bS[1] = bS[2] = bS[3] = value;
06663 } else {
06664 int i, l;
06665 int mv_done;
06666
06667 if( edge & mask_edge ) {
06668 bS[0] = bS[1] = bS[2] = bS[3] = 0;
06669 mv_done = 1;
06670 }
06671 else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
06672 bS[0] = bS[1] = bS[2] = bS[3] = 1;
06673 mv_done = 1;
06674 }
06675 else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
06676 int b_idx= 8 + 4 + edge * (dir ? 8:1);
06677 int bn_idx= b_idx - (dir ? 8:1);
06678 int v = 0;
06679 for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
06680 v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
06681 FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
06682 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
06683 }
06684 bS[0] = bS[1] = bS[2] = bS[3] = v;
06685 mv_done = 1;
06686 }
06687 else
06688 mv_done = 0;
06689
06690 for( i = 0; i < 4; i++ ) {
06691 int x = dir == 0 ? edge : i;
06692 int y = dir == 0 ? i : edge;
06693 int b_idx= 8 + 4 + x + 8*y;
06694 int bn_idx= b_idx - (dir ? 8:1);
06695
06696 if( h->non_zero_count_cache[b_idx] != 0 ||
06697 h->non_zero_count_cache[bn_idx] != 0 ) {
06698 bS[i] = 2;
06699 }
06700 else if(!mv_done)
06701 {
06702 bS[i] = 0;
06703 for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
06704 if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
06705 FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
06706 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
06707 bS[i] = 1;
06708 break;
06709 }
06710 }
06711 }
06712 }
06713
06714 if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
06715 continue;
06716 }
06717
06718
06719
06720
06721 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
06722
06723 tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
06724 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
06725 if( dir == 0 ) {
06726 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
06727 if( (edge&1) == 0 ) {
06728 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS,
06729 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
06730 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS,
06731 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
06732 }
06733 } else {
06734 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
06735 if( (edge&1) == 0 ) {
06736 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS,
06737 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
06738 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS,
06739 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
06740 }
06741 }
06742 }
06743 }
06744 }
06745
06746 static int decode_slice(struct AVCodecContext *avctx, H264Context *h){
06747 MpegEncContext * const s = &h->s;
06748 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
06749
06750 s->mb_skip_run= -1;
06751
06752 if( h->pps.cabac ) {
06753 int i;
06754
06755
06756 align_get_bits( &s->gb );
06757
06758
06759 ff_init_cabac_states( &h->cabac);
06760 ff_init_cabac_decoder( &h->cabac,
06761 s->gb.buffer + get_bits_count(&s->gb)/8,
06762 ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
06763
06764 for( i= 0; i < 460; i++ ) {
06765 int pre;
06766 if( h->slice_type == I_TYPE )
06767 pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
06768 else
06769 pre = av_clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
06770
06771 if( pre <= 63 )
06772 h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
06773 else
06774 h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
06775 }
06776
06777 for(;;){
06778
06779 int ret = decode_mb_cabac(h);
06780 int eos;
06781
06782
06783 if(ret>=0) hl_decode_mb(h);
06784
06785 if( ret >= 0 && FRAME_MBAFF ) {
06786 s->mb_y++;
06787
06788 if(ret>=0) ret = decode_mb_cabac(h);
06789
06790 if(ret>=0) hl_decode_mb(h);
06791 s->mb_y--;
06792 }
06793 eos = get_cabac_terminate( &h->cabac );
06794
06795 if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
06796 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
06797 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06798 return -1;
06799 }
06800
06801 if( ++s->mb_x >= s->mb_width ) {
06802 s->mb_x = 0;
06803 ff_draw_horiz_band(s, 16*s->mb_y, 16);
06804 ++s->mb_y;
06805 if(FIELD_OR_MBAFF_PICTURE) {
06806 ++s->mb_y;
06807 }
06808 }
06809
06810 if( eos || s->mb_y >= s->mb_height ) {
06811 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
06812 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06813 return 0;
06814 }
06815 }
06816
06817 } else {
06818 for(;;){
06819 int ret = decode_mb_cavlc(h);
06820
06821 if(ret>=0) hl_decode_mb(h);
06822
06823 if(ret>=0 && FRAME_MBAFF){
06824 s->mb_y++;
06825 ret = decode_mb_cavlc(h);
06826
06827 if(ret>=0) hl_decode_mb(h);
06828 s->mb_y--;
06829 }
06830
06831 if(ret<0){
06832 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
06833 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06834
06835 return -1;
06836 }
06837
06838 if(++s->mb_x >= s->mb_width){
06839 s->mb_x=0;
06840 ff_draw_horiz_band(s, 16*s->mb_y, 16);
06841 ++s->mb_y;
06842 if(FIELD_OR_MBAFF_PICTURE) {
06843 ++s->mb_y;
06844 }
06845 if(s->mb_y >= s->mb_height){
06846 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
06847
06848 if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
06849 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06850
06851 return 0;
06852 }else{
06853 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06854
06855 return -1;
06856 }
06857 }
06858 }
06859
06860 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
06861 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
06862 if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
06863 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06864
06865 return 0;
06866 }else{
06867 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06868
06869 return -1;
06870 }
06871 }
06872 }
06873 }
06874
06875 #if 0
06876 for(;s->mb_y < s->mb_height; s->mb_y++){
06877 for(;s->mb_x < s->mb_width; s->mb_x++){
06878 int ret= decode_mb(h);
06879
06880 hl_decode_mb(h);
06881
06882 if(ret<0){
06883 av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
06884 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06885
06886 return -1;
06887 }
06888
06889 if(++s->mb_x >= s->mb_width){
06890 s->mb_x=0;
06891 if(++s->mb_y >= s->mb_height){
06892 if(get_bits_count(s->gb) == s->gb.size_in_bits){
06893 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06894
06895 return 0;
06896 }else{
06897 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06898
06899 return -1;
06900 }
06901 }
06902 }
06903
06904 if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
06905 if(get_bits_count(s->gb) == s->gb.size_in_bits){
06906 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
06907
06908 return 0;
06909 }else{
06910 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
06911
06912 return -1;
06913 }
06914 }
06915 }
06916 s->mb_x=0;
06917 ff_draw_horiz_band(s, 16*s->mb_y, 16);
06918 }
06919 #endif
06920 return -1;
06921 }
06922
06923 static int decode_unregistered_user_data(H264Context *h, int size){
06924 MpegEncContext * const s = &h->s;
06925 uint8_t user_data[16+256];
06926 int e, build, i;
06927
06928 if(size<16)
06929 return -1;
06930
06931 for(i=0; i<sizeof(user_data)-1 && i<size; i++){
06932 user_data[i]= get_bits(&s->gb, 8);
06933 }
06934
06935 user_data[i]= 0;
06936 e= sscanf(user_data+16, "x264 - core %d", &build);
06937 if(e==1 && build>=0)
06938 h->x264_build= build;
06939
06940 if(s->avctx->debug & FF_DEBUG_BUGS)
06941 av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
06942
06943 for(; i<size; i++)
06944 skip_bits(&s->gb, 8);
06945
06946 return 0;
06947 }
06948
06949 static int decode_sei(H264Context *h){
06950 MpegEncContext * const s = &h->s;
06951
06952 while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
06953 int size, type;
06954
06955 type=0;
06956 do{
06957 type+= show_bits(&s->gb, 8);
06958 }while(get_bits(&s->gb, 8) == 255);
06959
06960 size=0;
06961 do{
06962 size+= show_bits(&s->gb, 8);
06963 }while(get_bits(&s->gb, 8) == 255);
06964
06965 switch(type){
06966 case 5:
06967 if(decode_unregistered_user_data(h, size) < 0)
06968 return -1;
06969 break;
06970 default:
06971 skip_bits(&s->gb, 8*size);
06972 }
06973
06974
06975 align_get_bits(&s->gb);
06976 }
06977
06978 return 0;
06979 }
06980
06981 static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
06982 MpegEncContext * const s = &h->s;
06983 int cpb_count, i;
06984 cpb_count = get_ue_golomb(&s->gb) + 1;
06985 get_bits(&s->gb, 4);
06986 get_bits(&s->gb, 4);
06987 for(i=0; i<cpb_count; i++){
06988 get_ue_golomb(&s->gb);
06989 get_ue_golomb(&s->gb);
06990 get_bits1(&s->gb);
06991 }
06992 get_bits(&s->gb, 5);
06993 get_bits(&s->gb, 5);
06994 get_bits(&s->gb, 5);
06995 get_bits(&s->gb, 5);
06996 }
06997
06998 static inline int decode_vui_parameters(H264Context *h, SPS *sps){
06999 MpegEncContext * const s = &h->s;
07000 int aspect_ratio_info_present_flag;
07001 unsigned int aspect_ratio_idc;
07002 int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
07003
07004 aspect_ratio_info_present_flag= get_bits1(&s->gb);
07005
07006 if( aspect_ratio_info_present_flag ) {
07007 aspect_ratio_idc= get_bits(&s->gb, 8);
07008 if( aspect_ratio_idc == EXTENDED_SAR ) {
07009 sps->sar.num= get_bits(&s->gb, 16);
07010 sps->sar.den= get_bits(&s->gb, 16);
07011 }else if(aspect_ratio_idc < 14){
07012 sps->sar= pixel_aspect[aspect_ratio_idc];
07013 }else{
07014 av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
07015 return -1;
07016 }
07017 }else{
07018 sps->sar.num=
07019 sps->sar.den= 0;
07020 }
07021
07022
07023 if(get_bits1(&s->gb)){
07024 get_bits1(&s->gb);
07025 }
07026
07027 if(get_bits1(&s->gb)){
07028 get_bits(&s->gb, 3);
07029 get_bits1(&s->gb);
07030 if(get_bits1(&s->gb)){
07031 get_bits(&s->gb, 8);
07032 get_bits(&s->gb, 8);
07033 get_bits(&s->gb, 8);
07034 }
07035 }
07036
07037 if(get_bits1(&s->gb)){
07038 get_ue_golomb(&s->gb);
07039 get_ue_golomb(&s->gb);
07040 }
07041
07042 sps->timing_info_present_flag = get_bits1(&s->gb);
07043 if(sps->timing_info_present_flag){
07044 sps->num_units_in_tick = get_bits_long(&s->gb, 32);
07045 sps->time_scale = get_bits_long(&s->gb, 32);
07046 sps->fixed_frame_rate_flag = get_bits1(&s->gb);
07047 }
07048
07049 nal_hrd_parameters_present_flag = get_bits1(&s->gb);
07050 if(nal_hrd_parameters_present_flag)
07051 decode_hrd_parameters(h, sps);
07052 vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
07053 if(vcl_hrd_parameters_present_flag)
07054 decode_hrd_parameters(h, sps);
07055 if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
07056 get_bits1(&s->gb);
07057 get_bits1(&s->gb);
07058
07059 sps->bitstream_restriction_flag = get_bits1(&s->gb);
07060 if(sps->bitstream_restriction_flag){
07061 unsigned int num_reorder_frames;
07062 get_bits1(&s->gb);
07063 get_ue_golomb(&s->gb);
07064 get_ue_golomb(&s->gb);
07065 get_ue_golomb(&s->gb);
07066 get_ue_golomb(&s->gb);
07067 num_reorder_frames= get_ue_golomb(&s->gb);
07068 get_ue_golomb(&s->gb);
07069
07070 if(num_reorder_frames > 16 ){
07071 av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
07072 return -1;
07073 }
07074
07075 sps->num_reorder_frames= num_reorder_frames;
07076 }
07077
07078 return 0;
07079 }
07080
07081 static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
07082 const uint8_t *jvt_list, const uint8_t *fallback_list){
07083 MpegEncContext * const s = &h->s;
07084 int i, last = 8, next = 8;
07085 const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
07086 if(!get_bits1(&s->gb))
07087 memcpy(factors, fallback_list, size*sizeof(uint8_t));
07088 else
07089 for(i=0;i<size;i++){
07090 if(next)
07091 next = (last + get_se_golomb(&s->gb)) & 0xff;
07092 if(!i && !next){
07093 memcpy(factors, jvt_list, size*sizeof(uint8_t));
07094 break;
07095 }
07096 last = factors[scan[i]] = next ? next : last;
07097 }
07098 }
07099
07100 static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
07101 uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
07102 MpegEncContext * const s = &h->s;
07103 int fallback_sps = !is_sps && sps->scaling_matrix_present;
07104 const uint8_t *fallback[4] = {
07105 fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
07106 fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
07107 fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
07108 fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
07109 };
07110 if(get_bits1(&s->gb)){
07111 sps->scaling_matrix_present |= is_sps;
07112 decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]);
07113 decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]);
07114 decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]);
07115 decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]);
07116 decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]);
07117 decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]);
07118 if(is_sps || pps->transform_8x8_mode){
07119 decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]);
07120 decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]);
07121 }
07122 } else if(fallback_sps) {
07123 memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
07124 memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
07125 }
07126 }
07127
07131 static void *
07132 alloc_parameter_set(H264Context *h, void **vec, const unsigned int id, const unsigned int max,
07133 const size_t size, const char *name)
07134 {
07135 if(id>=max) {
07136 av_log(h->s.avctx, AV_LOG_ERROR, "%s_id (%d) out of range\n", name, id);
07137 return NULL;
07138 }
07139
07140 if(!vec[id]) {
07141 vec[id] = av_mallocz(size);
07142 if(vec[id] == NULL)
07143 av_log(h->s.avctx, AV_LOG_ERROR, "cannot allocate memory for %s\n", name);
07144 }
07145 return vec[id];
07146 }
07147
07148 static inline int decode_seq_parameter_set(H264Context *h){
07149 MpegEncContext * const s = &h->s;
07150 int profile_idc, level_idc;
07151 unsigned int sps_id, tmp, mb_width, mb_height;
07152 int i;
07153 SPS *sps;
07154
07155 profile_idc= get_bits(&s->gb, 8);
07156 get_bits1(&s->gb);
07157 get_bits1(&s->gb);
07158 get_bits1(&s->gb);
07159 get_bits1(&s->gb);
07160 get_bits(&s->gb, 4);
07161 level_idc= get_bits(&s->gb, 8);
07162 sps_id= get_ue_golomb(&s->gb);
07163
07164 sps = alloc_parameter_set(h, (void **)h->sps_buffers, sps_id, MAX_SPS_COUNT, sizeof(SPS), "sps");
07165 if(sps == NULL)
07166 return -1;
07167
07168 sps->profile_idc= profile_idc;
07169 sps->level_idc= level_idc;
07170
07171 if(sps->profile_idc >= 100){
07172 if(get_ue_golomb(&s->gb) == 3)
07173 get_bits1(&s->gb);
07174 get_ue_golomb(&s->gb);
07175 get_ue_golomb(&s->gb);
07176 sps->transform_bypass = get_bits1(&s->gb);
07177 decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
07178 }else
07179 sps->scaling_matrix_present = 0;
07180
07181 sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
07182 sps->poc_type= get_ue_golomb(&s->gb);
07183
07184 if(sps->poc_type == 0){
07185 sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
07186 } else if(sps->poc_type == 1){
07187 sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
07188 sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
07189 sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
07190 tmp= get_ue_golomb(&s->gb);
07191
07192 if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
07193 av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
07194 return -1;
07195 }
07196 sps->poc_cycle_length= tmp;
07197
07198 for(i=0; i<sps->poc_cycle_length; i++)
07199 sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
07200 }else if(sps->poc_type != 2){
07201 av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
07202 return -1;
07203 }
07204
07205 tmp= get_ue_golomb(&s->gb);
07206 if(tmp > MAX_PICTURE_COUNT-2 || tmp >= 32){
07207 av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
07208 return -1;
07209 }
07210 sps->ref_frame_count= tmp;
07211 sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
07212 mb_width= get_ue_golomb(&s->gb) + 1;
07213 mb_height= get_ue_golomb(&s->gb) + 1;
07214 if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
07215 avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
07216 av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
07217 return -1;
07218 }
07219 sps->mb_width = mb_width;
07220 sps->mb_height= mb_height;
07221
07222 sps->frame_mbs_only_flag= get_bits1(&s->gb);
07223 if(!sps->frame_mbs_only_flag)
07224 sps->mb_aff= get_bits1(&s->gb);
07225 else
07226 sps->mb_aff= 0;
07227
07228 sps->direct_8x8_inference_flag= get_bits1(&s->gb);
07229
07230 #ifndef ALLOW_INTERLACE
07231 if(sps->mb_aff)
07232 av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
07233 #endif
07234 if(!sps->direct_8x8_inference_flag && sps->mb_aff)
07235 av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
07236
07237 sps->crop= get_bits1(&s->gb);
07238 if(sps->crop){
07239 sps->crop_left = get_ue_golomb(&s->gb);
07240 sps->crop_right = get_ue_golomb(&s->gb);
07241 sps->crop_top = get_ue_golomb(&s->gb);
07242 sps->crop_bottom= get_ue_golomb(&s->gb);
07243 if(sps->crop_left || sps->crop_top){
07244 av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
07245 }
07246 }else{
07247 sps->crop_left =
07248 sps->crop_right =
07249 sps->crop_top =
07250 sps->crop_bottom= 0;
07251 }
07252
07253 sps->vui_parameters_present_flag= get_bits1(&s->gb);
07254 if( sps->vui_parameters_present_flag )
07255 decode_vui_parameters(h, sps);
07256
07257 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
07258 av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
07259 sps_id, sps->profile_idc, sps->level_idc,
07260 sps->poc_type,
07261 sps->ref_frame_count,
07262 sps->mb_width, sps->mb_height,
07263 sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
07264 sps->direct_8x8_inference_flag ? "8B8" : "",
07265 sps->crop_left, sps->crop_right,
07266 sps->crop_top, sps->crop_bottom,
07267 sps->vui_parameters_present_flag ? "VUI" : ""
07268 );
07269 }
07270 return 0;
07271 }
07272
07273 static void
07274 build_qp_table(PPS *pps, int t, int index)
07275 {
07276 int i;
07277 for(i = 0; i < 255; i++)
07278 pps->chroma_qp_table[t][i & 0xff] = chroma_qp[av_clip(i + index, 0, 51)];
07279 }
07280
07281 static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
07282 MpegEncContext * const s = &h->s;
07283 unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
07284 PPS *pps;
07285
07286 pps = alloc_parameter_set(h, (void **)h->pps_buffers, pps_id, MAX_PPS_COUNT, sizeof(PPS), "pps");
07287 if(pps == NULL)
07288 return -1;
07289
07290 tmp= get_ue_golomb(&s->gb);
07291 if(tmp>=MAX_SPS_COUNT || h->sps_buffers[tmp] == NULL){
07292 av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
07293 return -1;
07294 }
07295 pps->sps_id= tmp;
07296
07297 pps->cabac= get_bits1(&s->gb);
07298 pps->pic_order_present= get_bits1(&s->gb);
07299 pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
07300 if(pps->slice_group_count > 1 ){
07301 pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
07302 av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
07303 switch(pps->mb_slice_group_map_type){
07304 case 0:
07305 #if 0
07306 | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
07307 | run_length[ i ] |1 |ue(v) |
07308 #endif
07309 break;
07310 case 2:
07311 #if 0
07312 | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
07313 |{ | | |
07314 | top_left_mb[ i ] |1 |ue(v) |
07315 | bottom_right_mb[ i ] |1 |ue(v) |
07316 | } | | |
07317 #endif
07318 break;
07319 case 3:
07320 case 4:
07321 case 5:
07322 #if 0
07323 | slice_group_change_direction_flag |1 |u(1) |
07324 | slice_group_change_rate_minus1 |1 |ue(v) |
07325 #endif
07326 break;
07327 case 6:
07328 #if 0
07329 | slice_group_id_cnt_minus1 |1 |ue(v) |
07330 | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
07331 |) | | |
07332 | slice_group_id[ i ] |1 |u(v) |
07333 #endif
07334 break;
07335 }
07336 }
07337 pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
07338 pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
07339 if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
07340 av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
07341 pps->ref_count[0]= pps->ref_count[1]= 1;
07342 return -1;
07343 }
07344
07345 pps->weighted_pred= get_bits1(&s->gb);
07346 pps->weighted_bipred_idc= get_bits(&s->gb, 2);
07347 pps->init_qp= get_se_golomb(&s->gb) + 26;
07348 pps->init_qs= get_se_golomb(&s->gb) + 26;
07349 pps->chroma_qp_index_offset[0]= get_se_golomb(&s->gb);
07350 pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
07351 pps->constrained_intra_pred= get_bits1(&s->gb);
07352 pps->redundant_pic_cnt_present = get_bits1(&s->gb);
07353
07354 pps->transform_8x8_mode= 0;
07355 h->dequant_coeff_pps= -1;
07356 memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
07357 memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
07358
07359 if(get_bits_count(&s->gb) < bit_length){
07360 pps->transform_8x8_mode= get_bits1(&s->gb);
07361 decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
07362 pps->chroma_qp_index_offset[1]= get_se_golomb(&s->gb);
07363 } else {
07364 pps->chroma_qp_index_offset[1]= pps->chroma_qp_index_offset[0];
07365 }
07366
07367 build_qp_table(pps, 0, pps->chroma_qp_index_offset[0]);
07368 if(pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1]) {
07369 build_qp_table(pps, 1, pps->chroma_qp_index_offset[1]);
07370 h->pps.chroma_qp_diff= 1;
07371 } else
07372 memcpy(pps->chroma_qp_table[1], pps->chroma_qp_table[0], 256);
07373
07374 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
07375 av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%u sps:%u %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d/%d %s %s %s %s\n",
07376 pps_id, pps->sps_id,
07377 pps->cabac ? "CABAC" : "CAVLC",
07378 pps->slice_group_count,
07379 pps->ref_count[0], pps->ref_count[1],
07380 pps->weighted_pred ? "weighted" : "",
07381 pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
07382 pps->deblocking_filter_parameters_present ? "LPAR" : "",
07383 pps->constrained_intra_pred ? "CONSTR" : "",
07384 pps->redundant_pic_cnt_present ? "REDU" : "",
07385 pps->transform_8x8_mode ? "8x8DCT" : ""
07386 );
07387 }
07388
07389 return 0;
07390 }
07391
07398 static void execute_decode_slices(H264Context *h, int context_count){
07399 MpegEncContext * const s = &h->s;
07400 AVCodecContext * const avctx= s->avctx;
07401 H264Context *hx;
07402 int i;
07403
07404 if(context_count == 1) {
07405 decode_slice(avctx, h);
07406 } else {
07407 for(i = 1; i < context_count; i++) {
07408 hx = h->thread_context[i];
07409 hx->s.error_resilience = avctx->error_resilience;
07410 hx->s.error_count = 0;
07411 }
07412
07413 avctx->execute(avctx, (void *)decode_slice,
07414 (void **)h->thread_context, NULL, context_count);
07415
07416
07417 hx = h->thread_context[context_count - 1];
07418 s->mb_x = hx->s.mb_x;
07419 s->mb_y = hx->s.mb_y;
07420 s->dropable = hx->s.dropable;
07421 s->picture_structure = hx->s.picture_structure;
07422 for(i = 1; i < context_count; i++)
07423 h->s.error_count += h->thread_context[i]->s.error_count;
07424 }
07425 }
07426
07427
07428 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
07429 MpegEncContext * const s = &h->s;
07430 AVCodecContext * const avctx= s->avctx;
07431 int buf_index=0;
07432 H264Context *hx;
07433 int context_count = 0;
07434
07435 h->max_contexts = avctx->thread_count;
07436 #if 0
07437 int i;
07438 for(i=0; i<50; i++){
07439 av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
07440 }
07441 #endif
07442 if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
07443 h->current_slice = 0;
07444 if (!s->first_field)
07445 s->current_picture_ptr= NULL;
07446 }
07447
07448 for(;;){
07449 int consumed;
07450 int dst_length;
07451 int bit_length;
07452 const uint8_t *ptr;
07453 int i, nalsize = 0;
07454 int err;
07455
07456 if(h->is_avc) {
07457 if(buf_index >= buf_size) break;
07458 nalsize = 0;
07459 for(i = 0; i < h->nal_length_size; i++)
07460 nalsize = (nalsize << 8) | buf[buf_index++];
07461 if(nalsize <= 1 || (nalsize+buf_index > buf_size)){
07462 if(nalsize == 1){
07463 buf_index++;
07464 continue;
07465 }else{
07466 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
07467 break;
07468 }
07469 }
07470 } else {
07471
07472 for(; buf_index + 3 < buf_size; buf_index++){
07473
07474 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
07475 break;
07476 }
07477
07478 if(buf_index+3 >= buf_size) break;
07479
07480 buf_index+=3;
07481 }
07482
07483 hx = h->thread_context[context_count];
07484
07485 ptr= decode_nal(hx, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
07486 if (ptr==NULL || dst_length < 0){
07487 return -1;
07488 }
07489 while(ptr[dst_length - 1] == 0 && dst_length > 0)
07490 dst_length--;
07491 bit_length= !dst_length ? 0 : (8*dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1));
07492
07493 if(s->avctx->debug&FF_DEBUG_STARTCODE){
07494 av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
07495 }
07496
07497 if (h->is_avc && (nalsize != consumed))
07498 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
07499
07500 buf_index += consumed;
07501
07502 if( (s->hurry_up == 1 && h->nal_ref_idc == 0)
07503 ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
07504 continue;
07505
07506 again:
07507 err = 0;
07508 switch(hx->nal_unit_type){
07509 case NAL_IDR_SLICE:
07510 if (h->nal_unit_type != NAL_IDR_SLICE) {
07511 av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
07512 return -1;
07513 }
07514 idr(h);
07515 case NAL_SLICE:
07516 init_get_bits(&hx->s.gb, ptr, bit_length);
07517 hx->intra_gb_ptr=
07518 hx->inter_gb_ptr= &hx->s.gb;
07519 hx->s.data_partitioning = 0;
07520
07521 if((err = decode_slice_header(hx, h)))
07522 break;
07523
07524 s->current_picture_ptr->key_frame|= (hx->nal_unit_type == NAL_IDR_SLICE);
07525 if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
07526 && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
07527 && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type!=B_TYPE)
07528 && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type==I_TYPE)
07529 && avctx->skip_frame < AVDISCARD_ALL)
07530 context_count++;
07531 break;
07532 case NAL_DPA:
07533 init_get_bits(&hx->s.gb, ptr, bit_length);
07534 hx->intra_gb_ptr=
07535 hx->inter_gb_ptr= NULL;
07536 hx->s.data_partitioning = 1;
07537
07538 err = decode_slice_header(hx, h);
07539 break;
07540 case NAL_DPB:
07541 init_get_bits(&hx->intra_gb, ptr, bit_length);
07542 hx->intra_gb_ptr= &hx->intra_gb;
07543 break;
07544 case NAL_DPC:
07545 init_get_bits(&hx->inter_gb, ptr, bit_length);
07546 hx->inter_gb_ptr= &hx->inter_gb;
07547
07548 if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
07549 && s->context_initialized
07550 && s->hurry_up < 5
07551 && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
07552 && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type!=B_TYPE)
07553 && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type==I_TYPE)
07554 && avctx->skip_frame < AVDISCARD_ALL)
07555 context_count++;
07556 break;
07557 case NAL_SEI:
07558 init_get_bits(&s->gb, ptr, bit_length);
07559 decode_sei(h);
07560 break;
07561 case NAL_SPS:
07562 init_get_bits(&s->gb, ptr, bit_length);
07563 decode_seq_parameter_set(h);
07564
07565 if(s->flags& CODEC_FLAG_LOW_DELAY)
07566 s->low_delay=1;
07567
07568 if(avctx->has_b_frames < 2)
07569 avctx->has_b_frames= !s->low_delay;
07570 break;
07571 case NAL_PPS:
07572 init_get_bits(&s->gb, ptr, bit_length);
07573
07574 decode_picture_parameter_set(h, bit_length);
07575
07576 break;
07577 case NAL_AUD:
07578 case NAL_END_SEQUENCE:
07579 case NAL_END_STREAM:
07580 case NAL_FILLER_DATA:
07581 case NAL_SPS_EXT:
07582 case NAL_AUXILIARY_SLICE:
07583 break;
07584 default:
07585 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", h->nal_unit_type, bit_length);
07586 }
07587
07588 if(context_count == h->max_contexts) {
07589 execute_decode_slices(h, context_count);
07590 context_count = 0;
07591 }
07592
07593 if (err < 0)
07594 av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
07595 else if(err == 1) {
07596
07597
07598
07599
07600 h->nal_unit_type = hx->nal_unit_type;
07601 h->nal_ref_idc = hx->nal_ref_idc;
07602 hx = h;
07603 goto again;
07604 }
07605 }
07606 if(context_count)
07607 execute_decode_slices(h, context_count);
07608 return buf_index;
07609 }
07610
07614 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
07615 if(s->flags&CODEC_FLAG_TRUNCATED){
07616 pos -= s->parse_context.last_index;
07617 if(pos<0) pos=0;
07618
07619 return pos;
07620 }else{
07621 if(pos==0) pos=1;
07622 if(pos+10>buf_size) pos=buf_size;
07623
07624 return pos;
07625 }
07626 }
07627
07628 static int decode_frame(AVCodecContext *avctx,
07629 void *data, int *data_size,
07630 const uint8_t *buf, int buf_size)
07631 {
07632 H264Context *h = avctx->priv_data;
07633 MpegEncContext *s = &h->s;
07634 AVFrame *pict = data;
07635 int buf_index;
07636
07637 s->flags= avctx->flags;
07638 s->flags2= avctx->flags2;
07639
07640
07641 if (buf_size == 0) {
07642 Picture *out;
07643 int i, out_idx;
07644
07645
07646 out = h->delayed_pic[0];
07647 out_idx = 0;
07648 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
07649 if(h->delayed_pic[i]->poc < out->poc){
07650 out = h->delayed_pic[i];
07651 out_idx = i;
07652 }
07653
07654 for(i=out_idx; h->delayed_pic[i]; i++)
07655 h->delayed_pic[i] = h->delayed_pic[i+1];
07656
07657 if(out){
07658 *data_size = sizeof(AVFrame);
07659 *pict= *(AVFrame*)out;
07660 }
07661
07662 return 0;
07663 }
07664
07665 if(s->flags&CODEC_FLAG_TRUNCATED){
07666 int next= ff_h264_find_frame_end(h, buf, buf_size);
07667
07668 if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
07669 return buf_size;
07670
07671 }
07672
07673 if(h->is_avc && !h->got_avcC) {
07674 int i, cnt, nalsize;
07675 unsigned char *p = avctx->extradata;
07676 if(avctx->extradata_size < 7) {
07677 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
07678 return -1;
07679 }
07680 if(*p != 1) {
07681 av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
07682 return -1;
07683 }
07684
07685
07686 h->nal_length_size = 2;
07687
07688 cnt = *(p+5) & 0x1f;
07689 p += 6;
07690 for (i = 0; i < cnt; i++) {
07691 nalsize = AV_RB16(p) + 2;
07692 if(decode_nal_units(h, p, nalsize) < 0) {
07693 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
07694 return -1;
07695 }
07696 p += nalsize;
07697 }
07698
07699 cnt = *(p++);
07700 for (i = 0; i < cnt; i++) {
07701 nalsize = AV_RB16(p) + 2;
07702 if(decode_nal_units(h, p, nalsize) != nalsize) {
07703 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
07704 return -1;
07705 }
07706 p += nalsize;
07707 }
07708
07709 h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
07710
07711 h->got_avcC = 1;
07712 }
07713
07714 if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
07715 if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
07716 return -1;
07717 }
07718
07719 buf_index=decode_nal_units(h, buf, buf_size);
07720 if(buf_index < 0)
07721 return -1;
07722
07723 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
07724 if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
07725 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
07726 return -1;
07727 }
07728
07729 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
07730 Picture *out = s->current_picture_ptr;
07731 Picture *cur = s->current_picture_ptr;
07732 Picture *prev = h->delayed_output_pic;
07733 int i, pics, cross_idr, out_of_order, out_idx;
07734
07735 s->mb_y= 0;
07736
07737 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
07738 s->current_picture_ptr->pict_type= s->pict_type;
07739
07740 h->prev_frame_num_offset= h->frame_num_offset;
07741 h->prev_frame_num= h->frame_num;
07742 if(!s->dropable) {
07743 h->prev_poc_msb= h->poc_msb;
07744 h->prev_poc_lsb= h->poc_lsb;
07745 execute_ref_pic_marking(h, h->mmco, h->mmco_index);
07746 }
07747
07748
07749
07750
07751
07752
07753
07754
07755
07756
07757
07758
07759
07760 if (!FIELD_PICTURE)
07761 ff_er_frame_end(s);
07762
07763 MPV_frame_end(s);
07764
07765 if (s->first_field) {
07766
07767 *data_size = 0;
07768
07769 } else {
07770 cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
07771
07772 cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
07773
07774
07775
07776 #if 0 //decode order
07777 *data_size = sizeof(AVFrame);
07778 #else
07779
07780
07781 if(h->sps.bitstream_restriction_flag
07782 && s->avctx->has_b_frames < h->sps.num_reorder_frames){
07783 s->avctx->has_b_frames = h->sps.num_reorder_frames;
07784 s->low_delay = 0;
07785 }
07786
07787 pics = 0;
07788 while(h->delayed_pic[pics]) pics++;
07789
07790 assert(pics+1 < sizeof(h->delayed_pic) / sizeof(h->delayed_pic[0]));
07791
07792 h->delayed_pic[pics++] = cur;
07793 if(cur->reference == 0)
07794 cur->reference = DELAYED_PIC_REF;
07795
07796 cross_idr = 0;
07797 for(i=0; h->delayed_pic[i]; i++)
07798 if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
07799 cross_idr = 1;
07800
07801 out = h->delayed_pic[0];
07802 out_idx = 0;
07803 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
07804 if(h->delayed_pic[i]->poc < out->poc){
07805 out = h->delayed_pic[i];
07806 out_idx = i;
07807 }
07808
07809 out_of_order = !cross_idr && prev && out->poc < prev->poc;
07810 if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
07811 { }
07812 else if(prev && pics <= s->avctx->has_b_frames)
07813 out = prev;
07814 else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
07815 || (s->low_delay &&
07816 ((!cross_idr && prev && out->poc > prev->poc + 2)
07817 || cur->pict_type == B_TYPE)))
07818 {
07819 s->low_delay = 0;
07820 s->avctx->has_b_frames++;
07821 out = prev;
07822 }
07823 else if(out_of_order)
07824 out = prev;
07825
07826 if(out_of_order || pics > s->avctx->has_b_frames){
07827 for(i=out_idx; h->delayed_pic[i]; i++)
07828 h->delayed_pic[i] = h->delayed_pic[i+1];
07829 }
07830
07831 if(prev == out)
07832 *data_size = 0;
07833 else
07834 *data_size = sizeof(AVFrame);
07835 if(prev && prev != out && prev->reference == DELAYED_PIC_REF)
07836 prev->reference = 0;
07837 h->delayed_output_pic = out;
07838 #endif
07839
07840 if(out)
07841 *pict= *(AVFrame*)out;
07842 else
07843 av_log(avctx, AV_LOG_DEBUG, "no picture\n");
07844 }
07845 }
07846
07847 assert(pict->data[0] || !*data_size);
07848 ff_print_debug_info(s, pict);
07849
07850 #if 0 //?
07851
07852
07853
07854 avctx->frame_number = s->picture_number - 1;
07855 #endif
07856 return get_consumed_bytes(s, buf_index, buf_size);
07857 }
07858 #if 0
07859 static inline void fill_mb_avail(H264Context *h){
07860 MpegEncContext * const s = &h->s;
07861 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
07862
07863 if(s->mb_y){
07864 h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
07865 h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
07866 h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
07867 }else{
07868 h->mb_avail[0]=
07869 h->mb_avail[1]=
07870 h->mb_avail[2]= 0;
07871 }
07872 h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
07873 h->mb_avail[4]= 1;
07874 h->mb_avail[5]= 0;
07875 }
07876 #endif
07877
07878 #ifdef TEST
07879 #undef printf
07880 #undef random
07881 #define COUNT 8000
07882 #define SIZE (COUNT*40)
07883 int main(void){
07884 int i;
07885 uint8_t temp[SIZE];
07886 PutBitContext pb;
07887 GetBitContext gb;
07888
07889 DSPContext dsp;
07890 AVCodecContext avctx;
07891
07892 dsputil_init(&dsp, &avctx);
07893
07894 init_put_bits(&pb, temp, SIZE);
07895 printf("testing unsigned exp golomb\n");
07896 for(i=0; i<COUNT; i++){
07897 START_TIMER
07898 set_ue_golomb(&pb, i);
07899 STOP_TIMER("set_ue_golomb");
07900 }
07901 flush_put_bits(&pb);
07902
07903 init_get_bits(&gb, temp, 8*SIZE);
07904 for(i=0; i<COUNT; i++){
07905 int j, s;
07906
07907 s= show_bits(&gb, 24);
07908
07909 START_TIMER
07910 j= get_ue_golomb(&gb);
07911 if(j != i){
07912 printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
07913
07914 }
07915 STOP_TIMER("get_ue_golomb");
07916 }
07917
07918
07919 init_put_bits(&pb, temp, SIZE);
07920 printf("testing signed exp golomb\n");
07921 for(i=0; i<COUNT; i++){
07922 START_TIMER
07923 set_se_golomb(&pb, i - COUNT/2);
07924 STOP_TIMER("set_se_golomb");
07925 }
07926 flush_put_bits(&pb);
07927
07928 init_get_bits(&gb, temp, 8*SIZE);
07929 for(i=0; i<COUNT; i++){
07930 int j, s;
07931
07932 s= show_bits(&gb, 24);
07933
07934 START_TIMER
07935 j= get_se_golomb(&gb);
07936 if(j != i - COUNT/2){
07937 printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
07938
07939 }
07940 STOP_TIMER("get_se_golomb");
07941 }
07942
07943 #if 0
07944 printf("testing 4x4 (I)DCT\n");
07945
07946 DCTELEM block[16];
07947 uint8_t src[16], ref[16];
07948 uint64_t error= 0, max_error=0;
07949
07950 for(i=0; i<COUNT; i++){
07951 int j;
07952
07953 for(j=0; j<16; j++){
07954 ref[j]= random()%255;
07955 src[j]= random()%255;
07956 }
07957
07958 h264_diff_dct_c(block, src, ref, 4);
07959
07960
07961 for(j=0; j<16; j++){
07962
07963 block[j]= block[j]*4;
07964 if(j&1) block[j]= (block[j]*4 + 2)/5;
07965 if(j&4) block[j]= (block[j]*4 + 2)/5;
07966 }
07967
07968
07969 s->dsp.h264_idct_add(ref, block, 4);
07970
07971
07972
07973
07974
07975 for(j=0; j<16; j++){
07976 int diff= FFABS(src[j] - ref[j]);
07977
07978 error+= diff*diff;
07979 max_error= FFMAX(max_error, diff);
07980 }
07981 }
07982 printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
07983 printf("testing quantizer\n");
07984 for(qp=0; qp<52; qp++){
07985 for(i=0; i<16; i++)
07986 src1_block[i]= src2_block[i]= random()%255;
07987
07988 }
07989 printf("Testing NAL layer\n");
07990
07991 uint8_t bitstream[COUNT];
07992 uint8_t nal[COUNT*2];
07993 H264Context h;
07994 memset(&h, 0, sizeof(H264Context));
07995
07996 for(i=0; i<COUNT; i++){
07997 int zeros= i;
07998 int nal_length;
07999 int consumed;
08000 int out_length;
08001 uint8_t *out;
08002 int j;
08003
08004 for(j=0; j<COUNT; j++){
08005 bitstream[j]= (random() % 255) + 1;
08006 }
08007
08008 for(j=0; j<zeros; j++){
08009 int pos= random() % COUNT;
08010 while(bitstream[pos] == 0){
08011 pos++;
08012 pos %= COUNT;
08013 }
08014 bitstream[pos]=0;
08015 }
08016
08017 START_TIMER
08018
08019 nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
08020 if(nal_length<0){
08021 printf("encoding failed\n");
08022 return -1;
08023 }
08024
08025 out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
08026
08027 STOP_TIMER("NAL")
08028
08029 if(out_length != COUNT){
08030 printf("incorrect length %d %d\n", out_length, COUNT);
08031 return -1;
08032 }
08033
08034 if(consumed != nal_length){
08035 printf("incorrect consumed length %d %d\n", nal_length, consumed);
08036 return -1;
08037 }
08038
08039 if(memcmp(bitstream, out, COUNT)){
08040 printf("mismatch\n");
08041 return -1;
08042 }
08043 }
08044 #endif
08045
08046 printf("Testing RBSP\n");
08047
08048
08049 return 0;
08050 }
08051 #endif
08052
08053
08054 static int decode_end(AVCodecContext *avctx)
08055 {
08056 H264Context *h = avctx->priv_data;
08057 MpegEncContext *s = &h->s;
08058
08059 av_freep(&h->rbsp_buffer[0]);
08060 av_freep(&h->rbsp_buffer[1]);
08061 free_tables(h);
08062 MPV_common_end(s);
08063
08064
08065
08066 return 0;
08067 }
08068
08069
08070 AVCodec h264_decoder = {
08071 "h264",
08072 CODEC_TYPE_VIDEO,
08073 CODEC_ID_H264,
08074 sizeof(H264Context),
08075 decode_init,
08076 NULL,
08077 decode_end,
08078 decode_frame,
08079 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
08080 .flush= flush_dpb,
08081 };
08082
08083 #include "svq3.c"