Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
diff libavcodec/h264_nal.c @ 2:897f711a7157
rearrange to work with autoconf
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Tue, 25 Sep 2012 15:55:33 +0200 |
| parents | |
| children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libavcodec/h264_nal.c Tue Sep 25 15:55:33 2012 +0200 1.3 @@ -0,0 +1,628 @@ 1.4 +#include "h264_types.h" 1.5 +#include "h264_data.h" 1.6 + 1.7 +#include "golomb.h" 1.8 +#include "h264_sei.h" 1.9 +#include "h264_refs.h" 1.10 +#include "h264_ps.h" 1.11 +#include "h264_pred_mode.h" 1.12 +#include "h264_misc.h" 1.13 + 1.14 +static int ff_h264_decode_rbsp_trailing(const uint8_t *src){ 1.15 + int v= *src; 1.16 + int r; 1.17 + 1.18 + for(r=1; r<9; r++){ 1.19 + if(v&1) return r; 1.20 + v>>=1; 1.21 + } 1.22 + return 0; 1.23 +} 1.24 + 1.25 +static int pred_weight_table(H264Slice *s, GetBitContext *gb){ 1.26 + int luma_def, chroma_def; 1.27 + 1.28 + s->use_weight= 0; 1.29 + s->use_weight_chroma= 0; 1.30 + s->luma_log2_weight_denom= get_ue_golomb(gb); 1.31 + s->chroma_log2_weight_denom= get_ue_golomb(gb); 1.32 + luma_def = 1<<s->luma_log2_weight_denom; 1.33 + chroma_def = 1<<s->chroma_log2_weight_denom; 1.34 + 1.35 + for(int list=0; list<2; list++){ 1.36 + for(int i=0; i<s->ref_count[list]; i++){ 1.37 + int luma_weight_flag, chroma_weight_flag; 1.38 + 1.39 + luma_weight_flag= get_bits1(gb); 1.40 + if(luma_weight_flag){ 1.41 + s->luma_weight[i][list][0]= get_se_golomb(gb); 1.42 + s->luma_weight[i][list][1]= get_se_golomb(gb); 1.43 + if( s->luma_weight[i][list][0] != luma_def 1.44 + || s->luma_weight[i][list][1] != 0) { 1.45 + s->use_weight= 1; 1.46 + } 1.47 + }else{ 1.48 + s->luma_weight[i][list][0]= luma_def; 1.49 + s->luma_weight[i][list][1]= 0; 1.50 + } 1.51 + 1.52 + chroma_weight_flag= get_bits1(gb); 1.53 + if(chroma_weight_flag){ 1.54 + int j; 1.55 + for(j=0; j<2; j++){ 1.56 + s->chroma_weight[i][list][j][0]= get_se_golomb(gb); 1.57 + s->chroma_weight[i][list][j][1]= get_se_golomb(gb); 1.58 + if( s->chroma_weight[i][list][j][0] != chroma_def 1.59 + || s->chroma_weight[i][list][j][1] != 0) { 1.60 + s->use_weight_chroma= 1; 1.61 + } 1.62 + } 1.63 + }else{ 1.64 + int j; 1.65 + for(j=0; j<2; j++){ 1.66 + s->chroma_weight[i][list][j][0]= chroma_def; 1.67 + s->chroma_weight[i][list][j][1]= 0; 1.68 + } 1.69 + } 1.70 + } 1.71 + if(s->slice_type_nos != FF_B_TYPE) break; 1.72 + } 1.73 + s->use_weight= s->use_weight || s->use_weight_chroma; 1.74 + return 0; 1.75 +} 1.76 + 1.77 +/** 1.78 +* Initialize implicit_weight table. 1.79 +*/ 1.80 +static void implicit_weight_table(H264Slice *s){ 1.81 + int ref0, ref1, cur_poc, ref_start, ref_count0, ref_count1; 1.82 + 1.83 + cur_poc = s->poc; 1.84 + if( s->ref_count[0] == 1 && s->ref_count[1] == 1 && s->ref_list[0][0]->poc + s->ref_list[1][0]->poc == 2*cur_poc){ 1.85 + s->use_weight= 0; 1.86 + s->use_weight_chroma= 0; 1.87 + return; 1.88 + } 1.89 + ref_start= 0; 1.90 + ref_count0= s->ref_count[0]; 1.91 + ref_count1= s->ref_count[1]; 1.92 + 1.93 + s->use_weight= 2; 1.94 + s->use_weight_chroma= 2; 1.95 + s->luma_log2_weight_denom= 5; 1.96 + s->chroma_log2_weight_denom= 5; 1.97 + 1.98 + for(ref0=ref_start; ref0 < ref_count0; ref0++){ 1.99 + int poc0 = s->ref_list[0][ref0]->poc; 1.100 + for(ref1=ref_start; ref1 < ref_count1; ref1++){ 1.101 + int poc1 = s->ref_list[1][ref1]->poc; 1.102 + int td = av_clip(poc1 - poc0, -128, 127); 1.103 + int w= 32; 1.104 + if(td){ 1.105 + int tb = av_clip(cur_poc - poc0, -128, 127); 1.106 + int tx = (16384 + (FFABS(td) >> 1)) / td; 1.107 + int dist_scale_factor = (tb*tx + 32) >> 8; 1.108 + if(dist_scale_factor >= -64 && dist_scale_factor <= 128) 1.109 + w = 64 - dist_scale_factor; 1.110 + } 1.111 + s->implicit_weight[ref0][ref1][0]= 1.112 + s->implicit_weight[ref0][ref1][1]= w; 1.113 + } 1.114 + } 1.115 +} 1.116 + 1.117 +/** 1.118 +* instantaneous decoder refresh. 1.119 +*/ 1.120 +static void idr(NalContext *n, H264Slice *s){ 1.121 + ff_h264_remove_all_refs(n, s); 1.122 + n->prev_frame_num= 0; 1.123 + n->prev_frame_num_offset= 0; 1.124 + n->poc_offset += (n->prev_poc_msb<<16) + n->prev_poc_lsb; 1.125 + n->prev_poc_msb= 1.126 + n->prev_poc_lsb= 0; 1.127 +} 1.128 + 1.129 +static int init_poc(NalContext *n, H264Slice *s, GetBitContext *gb){ 1.130 + const int max_frame_num= 1<<n->sps.log2_max_frame_num; 1.131 + int frame_poc; 1.132 + 1.133 + if(n->sps.poc_type==0){ 1.134 + n->poc_lsb= get_bits(gb, n->sps.log2_max_poc_lsb); 1.135 + } 1.136 + 1.137 + if(n->sps.poc_type==1 && !n->sps.delta_pic_order_always_zero_flag){ 1.138 + n->delta_poc= get_se_golomb(gb); 1.139 + } 1.140 + 1.141 + n->frame_num_offset= n->prev_frame_num_offset; 1.142 + if(n->frame_num < n->prev_frame_num) 1.143 + n->frame_num_offset += max_frame_num; 1.144 + 1.145 + if(n->sps.poc_type==0){ 1.146 + const int max_poc_lsb= 1<<n->sps.log2_max_poc_lsb; 1.147 + 1.148 + if(n->poc_lsb < n->prev_poc_lsb && n->prev_poc_lsb - n->poc_lsb >= max_poc_lsb/2) 1.149 + n->poc_msb = n->prev_poc_msb + max_poc_lsb; 1.150 + else if(n->poc_lsb > n->prev_poc_lsb && n->prev_poc_lsb - n->poc_lsb < -max_poc_lsb/2) 1.151 + n->poc_msb = n->prev_poc_msb - max_poc_lsb; 1.152 + else 1.153 + n->poc_msb = n->prev_poc_msb; 1.154 + 1.155 + frame_poc = n->poc_msb + n->poc_lsb; 1.156 + }else if(n->sps.poc_type==1){ 1.157 + int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc; 1.158 + int i; 1.159 + 1.160 + if(n->sps.poc_cycle_length != 0) 1.161 + abs_frame_num = n->frame_num_offset + n->frame_num; 1.162 + else 1.163 + abs_frame_num = 0; 1.164 + 1.165 + if(s->nal_ref_idc==0 && abs_frame_num > 0) 1.166 + abs_frame_num--; 1.167 + 1.168 + expected_delta_per_poc_cycle = 0; 1.169 + for(i=0; i < n->sps.poc_cycle_length; i++) 1.170 + expected_delta_per_poc_cycle += n->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse 1.171 + 1.172 + if(abs_frame_num > 0){ 1.173 + int poc_cycle_cnt = (abs_frame_num - 1) / n->sps.poc_cycle_length; 1.174 + int frame_num_in_poc_cycle = (abs_frame_num - 1) % n->sps.poc_cycle_length; 1.175 + 1.176 + expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle; 1.177 + for(i = 0; i <= frame_num_in_poc_cycle; i++) 1.178 + expectedpoc = expectedpoc + n->sps.offset_for_ref_frame[ i ]; 1.179 + } else 1.180 + expectedpoc = 0; 1.181 + if(s->nal_ref_idc == 0) 1.182 + expectedpoc = expectedpoc + n->sps.offset_for_non_ref_pic; 1.183 + frame_poc = expectedpoc + n->delta_poc; 1.184 + }else{ 1.185 + int poc= 2*(n->frame_num_offset + n->frame_num); 1.186 + if(!s->nal_ref_idc) 1.187 + poc--; 1.188 + frame_poc= poc; 1.189 + } 1.190 + s->current_picture_info->poc= s->poc = frame_poc + n->poc_offset; 1.191 + s->coded_pic_num = n->coded_pic_num++; 1.192 + 1.193 + return 0; 1.194 +} 1.195 + 1.196 +static void ref2frame(NalContext *n, H264Slice *s){ 1.197 + for(int j=0; j<s->list_count; j++){ 1.198 + int *ref2frm= s->ref2frm[j]; 1.199 + 1.200 + ref2frm[0]= 1.201 + ref2frm[1]= -1; 1.202 + 1.203 + for(int i=0; i<s->ref_count[j]; i++){ 1.204 + ref2frm[i+2]= 15; 1.205 + if(s->ref_list[j][i]->cpn >=0){ 1.206 + int k; 1.207 + for(k=0; k<n->short_ref_count; k++){ 1.208 + if(n->short_ref[k]->cpn == s->ref_list[j][i]->cpn){ 1.209 + ref2frm[i+2]= k; 1.210 + break; 1.211 + } 1.212 + } 1.213 + } 1.214 + } 1.215 + } 1.216 +} 1.217 + 1.218 +/** 1.219 +* decodes a slice header. 1.220 +* This will also call MPV_common_init() and frame_start() as needed. 1.221 +* 1.222 +* @param h h264context 1.223 +* @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding) 1.224 +* 1.225 +* @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded 1.226 +*/ 1.227 +static int decode_slice_header(NalContext *n, H264Slice *s, GetBitContext *gb){ 1.228 + unsigned int first_mb_in_slice; 1.229 + unsigned int pps_id; 1.230 + int num_ref_idx_active_override_flag; 1.231 + unsigned int slice_type, tmp; 1.232 + 1.233 + first_mb_in_slice= get_ue_golomb(gb); 1.234 + (void) first_mb_in_slice; 1.235 + 1.236 + slice_type= get_ue_golomb_31(gb); 1.237 + if(slice_type > 9){ 1.238 + av_log(AV_LOG_ERROR, "slice type too large (%d)\n", s->slice_type); 1.239 + return -1; 1.240 + } 1.241 + if(slice_type > 4) 1.242 + slice_type -= 5; 1.243 + 1.244 + slice_type= golomb_to_pict_type[ slice_type ]; 1.245 + 1.246 + s->slice_type= slice_type; 1.247 + s->slice_type_nos= slice_type & 3; 1.248 + s->current_picture_info->slice_type_nos = s->slice_type_nos; 1.249 + s->current_picture_info->reference= s->nal_ref_idc? 2:0; 1.250 + s->key_frame = s->slice_type == FF_I_TYPE; 1.251 + 1.252 + pps_id= get_ue_golomb(gb); 1.253 + 1.254 + if(pps_id>=MAX_PPS_COUNT){ 1.255 + av_log(AV_LOG_ERROR, "pps_id out of range\n"); 1.256 + return -1; 1.257 + } 1.258 + if(!n->pps_buffers[pps_id]) { 1.259 + av_log(AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id); 1.260 + return -1; 1.261 + } 1.262 + s->pps= *n->pps_buffers[pps_id]; 1.263 + 1.264 + if(!n->sps_buffers[s->pps.sps_id]) { 1.265 + av_log(AV_LOG_ERROR, "non-existing SPS %u referenced\n", s->pps.sps_id); 1.266 + return -1; 1.267 + } 1.268 + n->sps = *n->sps_buffers[s->pps.sps_id]; 1.269 + 1.270 + n->mb_width= n->sps.mb_width; 1.271 + n->mb_height= n->sps.mb_height; 1.272 + 1.273 + int chroma444 = (n->sps.chroma_format_idc == 3); 1.274 + n->width = 16*n->mb_width - (2>>chroma444)*FFMIN(n->sps.crop_right, (8<<chroma444)-1); 1.275 + if(n->sps.frame_mbs_only_flag) 1.276 + n->height= 16*n->mb_height - (2>>chroma444)*FFMIN(n->sps.crop_bottom, (8<<chroma444)-1); 1.277 + else 1.278 + n->height= 16*n->mb_height - (4>>chroma444)*FFMIN(n->sps.crop_bottom, (8<<chroma444)-1); 1.279 + 1.280 + s->direct_8x8_inference_flag = n->sps.direct_8x8_inference_flag; 1.281 + s->transform_bypass = n->sps.transform_bypass; 1.282 + 1.283 + n->frame_num= get_bits(gb, n->sps.log2_max_frame_num); 1.284 + if(n->frame_num != n->prev_frame_num && n->frame_num != (n->prev_frame_num+1)%(1<<n->sps.log2_max_frame_num)){ 1.285 + av_log(AV_LOG_ERROR, "unexpected frame_num \n"); 1.286 + } 1.287 + 1.288 + s->current_picture_info->frame_num= n->frame_num; //FIXME frame_num cleanup 1.289 + n->max_pic_num= 1<< n->sps.log2_max_frame_num; 1.290 + 1.291 + if(s->nal_unit_type == NAL_IDR_SLICE){ 1.292 + get_ue_golomb(gb); /* idr_pic_id */ 1.293 + } 1.294 + 1.295 + init_poc(n, s, gb); 1.296 + 1.297 + if(s->pps.redundant_pic_cnt_present){ 1.298 + n->redundant_pic_count= get_ue_golomb(gb); 1.299 + } 1.300 + 1.301 + //set defaults, might be overridden a few lines later 1.302 + s->ref_count[0]= s->pps.ref_count[0]; 1.303 + s->ref_count[1]= s->pps.ref_count[1]; 1.304 + 1.305 + if(s->slice_type_nos != FF_I_TYPE){ 1.306 + if(s->slice_type_nos == FF_B_TYPE){ 1.307 + s->direct_spatial_mv_pred= get_bits1(gb); 1.308 + } 1.309 + num_ref_idx_active_override_flag= get_bits1(gb); 1.310 + 1.311 + if(num_ref_idx_active_override_flag){ 1.312 + s->ref_count[0]= get_ue_golomb(gb) + 1; 1.313 + if(s->slice_type_nos==FF_B_TYPE) 1.314 + s->ref_count[1]= get_ue_golomb(gb) + 1; 1.315 + 1.316 + if(s->ref_count[0]-1 > 32-1 || s->ref_count[1]-1 > 32-1){ 1.317 + av_log(AV_LOG_ERROR, "reference overflow\n"); 1.318 + s->ref_count[0]= s->ref_count[1]= 1; 1.319 + return -1; 1.320 + } 1.321 + } 1.322 + if(s->slice_type_nos == FF_B_TYPE) 1.323 + s->list_count= 2; 1.324 + else 1.325 + s->list_count= 1; 1.326 + }else 1.327 + s->list_count= 0; 1.328 + 1.329 + 1.330 + if(s->slice_type_nos!=FF_I_TYPE){ 1.331 + ff_h264_fill_default_ref_list(n, s); 1.332 + ff_h264_decode_ref_pic_list_reordering(n, s, gb); 1.333 + ref2frame(n, s); 1.334 + 1.335 + for(int i=0; i<2; i++){ 1.336 + for(int j=0; j<s->ref_count[i]; j++){ 1.337 + if (s->ref_list[i][j]==NULL || s->ref_list[i][j]->reference < 2) // Don't know why sometimes the ref_count=1 while there are no references 1.338 + s->ref_list_cpn[i][j] = -1; 1.339 + else 1.340 + s->ref_list_cpn[i][j] = s->ref_list[i][j]->cpn; 1.341 + } 1.342 + } 1.343 + } 1.344 + 1.345 + if( (s->pps.weighted_pred && s->slice_type_nos == FF_P_TYPE ) 1.346 + || (s->pps.weighted_bipred_idc==1 && s->slice_type_nos== FF_B_TYPE ) ){ 1.347 + pred_weight_table(s, gb); 1.348 + } 1.349 + else if(s->pps.weighted_bipred_idc==2 && s->slice_type_nos== FF_B_TYPE){ 1.350 + implicit_weight_table( s); 1.351 + }else { 1.352 + s->use_weight = 0; 1.353 + } 1.354 + 1.355 + if(s->nal_ref_idc){ 1.356 + ff_h264_ref_pic_marking(n, s, gb); 1.357 + n->prev_poc_msb= n->poc_msb; 1.358 + n->prev_poc_lsb= n->poc_lsb; 1.359 + } 1.360 + 1.361 + n->prev_frame_num_offset= n->frame_num_offset; 1.362 + n->prev_frame_num= n->frame_num; 1.363 + 1.364 + if(s->slice_type_nos != FF_B_TYPE){ 1.365 + s->ip_id= n->ip_id++; 1.366 + } 1.367 + 1.368 + if(s->slice_type_nos==FF_B_TYPE && !s->direct_spatial_mv_pred){ 1.369 + ff_h264_direct_dist_scale_factor(s); 1.370 + } 1.371 + ff_h264_direct_ref_list_init(s); 1.372 + 1.373 + 1.374 + if( s->slice_type_nos != FF_I_TYPE && s->pps.cabac ){ 1.375 + tmp = get_ue_golomb_31(gb); 1.376 + if(tmp > 2){ 1.377 + av_log(AV_LOG_ERROR, "cabac_init_idc overflow\n"); 1.378 + return -1; 1.379 + } 1.380 + s->cabac_init_idc= tmp; 1.381 + } 1.382 + 1.383 + tmp = s->pps.init_qp + get_se_golomb(gb); 1.384 + if(tmp>51){ 1.385 + av_log(AV_LOG_ERROR, "QP %u out of range\n", tmp); 1.386 + return -1; 1.387 + } 1.388 + s->qscale= tmp; 1.389 + 1.390 + //FIXME qscale / qp ... stuff 1.391 + if(s->slice_type == FF_SP_TYPE){ 1.392 + get_bits1(gb); /* sp_for_switch_flag */ 1.393 + } 1.394 + if(s->slice_type==FF_SP_TYPE || s->slice_type == FF_SI_TYPE){ 1.395 + get_se_golomb(gb); /* slice_qs_delta */ 1.396 + } 1.397 + 1.398 + s->slice_alpha_c0_offset = 52; 1.399 + s->slice_beta_offset = 52; 1.400 + if( s->pps.deblocking_filter_parameters_present ) { 1.401 + tmp= get_ue_golomb_31(gb); 1.402 + if(tmp > 1){ 1.403 + av_log(AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp); 1.404 + return -1; 1.405 + } 1.406 + 1.407 + if(tmp < 2) 1.408 + tmp^= 1; // 1<->0 1.409 + 1.410 + if( tmp ) { 1.411 + s->slice_alpha_c0_offset += get_se_golomb(gb) << 1; 1.412 + s->slice_beta_offset += get_se_golomb(gb) << 1; 1.413 + if( (unsigned) s->slice_alpha_c0_offset > 104U 1.414 + ||(unsigned) s->slice_beta_offset > 104U){ 1.415 + av_log(AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", s->slice_alpha_c0_offset, s->slice_beta_offset); 1.416 + return -1; 1.417 + } 1.418 + } 1.419 + } 1.420 + 1.421 + s->qp_thresh= 15 + 52 - FFMIN(s->slice_alpha_c0_offset, s->slice_beta_offset) - FFMAX3(0, s->pps.chroma_qp_index_offset[0], s->pps.chroma_qp_index_offset[1]); 1.422 + 1.423 + return 0; 1.424 +} 1.425 + 1.426 +PictureInfo *get_pib_entry(NalContext *nc, int coded_pic_num){ 1.427 + PictureInfo *pic = NULL; 1.428 + 1.429 + for(int i=0; i<MAX_REF_PIC_COUNT+1; i++){ 1.430 + if(nc->picture[i].reference==0){ 1.431 + pic= &nc->picture[i]; 1.432 + break; 1.433 + } 1.434 + } 1.435 + pic->cpn = coded_pic_num; 1.436 + 1.437 + return pic; 1.438 +} 1.439 + 1.440 +int decode_nal_units(NalContext *n, H264Slice *s, GetBitContext *gb1){ 1.441 + GetBitContext *gb = gb1; 1.442 + uint8_t *buf = gb1->raw; 1.443 + int buf_size = gb1->buf_size; 1.444 + int next_avc = buf_size; 1.445 + int buf_index=0; 1.446 + uint8_t *dst=NULL; 1.447 +// gb->raw = gb1->raw; 1.448 +// gb->rbsp = NULL; 1.449 + s->release_cnt=0; 1.450 + ff_h264_reset_sei(n); 1.451 + 1.452 + s->current_picture_info = get_pib_entry(n, n->coded_pic_num); 1.453 + 1.454 + for(;;){ 1.455 + int consumed; 1.456 + int dst_length; 1.457 + int bit_length; 1.458 + const uint8_t *ptr; 1.459 + int err; 1.460 + 1.461 + if (buf_index >= buf_size){ 1.462 + break; 1.463 + } else { 1.464 + // start code prefix search 1.465 + for(; buf_index + 3 < buf_size; buf_index++){ 1.466 + // This should always succeed in the first iteration. 1.467 + if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1) 1.468 + break; 1.469 + } 1.470 + if(buf_index+3 >= buf_size) break; 1.471 + buf_index+=3; 1.472 + } 1.473 + 1.474 + { 1.475 + int length = next_avc - buf_index; 1.476 + int i, si, di; 1.477 + uint8_t *src= buf+buf_index; 1.478 + // src[0]&0x80; //forbidden bit 1.479 + s->nal_ref_idc= src[0]>>5; 1.480 + s->nal_unit_type= src[0]&0x1F; 1.481 + 1.482 + src++; length--; 1.483 + 1.484 + for(i=0; i+1<length; i+=2){ 1.485 + if(src[i]) continue; 1.486 + if(i>0 && src[i-1]==0) i--; 1.487 + if(i+2<length && src[i+1]==0 && src[i+2]<=3){ 1.488 + if(src[i+2]!=3){ 1.489 + /* startcode, so we must be past the end */ 1.490 + length=i; 1.491 + } 1.492 + break; 1.493 + } 1.494 + } 1.495 + 1.496 + if(i>=length-1){ //no escaped 0 1.497 + dst_length= length; 1.498 + consumed= length+1; //+1 for the header 1.499 + ptr=src; 1.500 + }else{ 1.501 + av_fast_malloc(&gb->rbsp, &gb->rbsp_size, length+FF_INPUT_BUFFER_PADDING_SIZE); 1.502 + dst = gb->rbsp; 1.503 +// if (dst){ 1.504 +// av_free(dst); 1.505 +// } 1.506 +// dst = av_malloc(length+FF_INPUT_BUFFER_PADDING_SIZE); 1.507 + 1.508 + if (dst == NULL){ 1.509 + return -1; 1.510 + } 1.511 + 1.512 + //printf("decoding esc\n"); 1.513 + memcpy(dst, src, i); 1.514 + si=di=i; 1.515 + while(si+2<length){ 1.516 + //remove escapes (very rare 1:2^22) 1.517 + if(src[si+2]>3){ 1.518 + dst[di++]= src[si++]; 1.519 + dst[di++]= src[si++]; 1.520 + }else if(src[si]==0 && src[si+1]==0){ 1.521 + if(src[si+2]==3){ //escape 1.522 + dst[di++]= 0; 1.523 + dst[di++]= 0; 1.524 + si+=3; 1.525 + continue; 1.526 + }else //next start code 1.527 + goto nsc; 1.528 + } 1.529 + 1.530 + dst[di++]= src[si++]; 1.531 + } 1.532 + while(si<length) 1.533 + dst[di++]= src[si++]; 1.534 + nsc: 1.535 + 1.536 + memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE); 1.537 + 1.538 + dst_length= di; 1.539 + consumed= si + 1;//+1 for the header 1.540 + //FIXME store exact number of bits in the getbitcontext (it is needed for decoding) 1.541 + ptr=dst; 1.542 +// gb->rbsp=ptr; 1.543 + } 1.544 + } 1.545 + if (ptr==NULL || dst_length < 0){ 1.546 + return -1; 1.547 + } 1.548 + 1.549 + //error prevention, should not touch dst_length 1.550 + while(ptr[dst_length - 1] == 0 && dst_length > 0) 1.551 + dst_length--; 1.552 + 1.553 + bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(ptr + dst_length - 1)); 1.554 + buf_index += consumed; 1.555 + 1.556 + err = 0; 1.557 + init_get_bits(gb, ptr, bit_length); 1.558 + switch(s->nal_unit_type){ 1.559 + case NAL_IDR_SLICE: 1.560 + idr(n, s); //FIXME ensure we don't loose some frames if there is reordering 1.561 + case NAL_SLICE: 1.562 + if((err = decode_slice_header(n, s, gb))) 1.563 + break; 1.564 + s->key_frame |= (s->nal_unit_type == NAL_IDR_SLICE) || (n->sei_recovery_frame_cnt >= 0); 1.565 + break; 1.566 + case NAL_DPA: 1.567 + case NAL_DPB: 1.568 + case NAL_DPC: 1.569 + av_log(AV_LOG_ERROR,"no slices/data partitioning support\n"); 1.570 + break; 1.571 + case NAL_SEI: 1.572 + ff_h264_decode_sei(n, gb); 1.573 + break; 1.574 + case NAL_SPS: 1.575 + ff_h264_decode_seq_parameter_set(n, gb); 1.576 + break; 1.577 + case NAL_PPS: 1.578 + ff_h264_decode_picture_parameter_set(n, gb, bit_length); 1.579 + break; 1.580 + case NAL_AUD: 1.581 + case NAL_END_SEQUENCE: 1.582 + case NAL_END_STREAM: 1.583 + case NAL_FILLER_DATA: 1.584 + case NAL_SPS_EXT: 1.585 + case NAL_AUXILIARY_SLICE: 1.586 + break; 1.587 + default: 1.588 + av_log(AV_LOG_ERROR, "Unknown NAL code: %d (%d bits)\n", s->nal_unit_type, bit_length); 1.589 + } 1.590 + if (err < 0) 1.591 + av_log(AV_LOG_ERROR, "decode_slice_header error\n"); 1.592 + 1.593 + } 1.594 + 1.595 + return buf_index; 1.596 +} 1.597 + 1.598 +NalContext *get_nal_context(int width, int height){ 1.599 + const int mb_height = (height + 15) / 16; 1.600 + const int mb_width = (width + 15) / 16; 1.601 + const int mb_stride = ((mb_width+1)/16 + 1) *16; //align mb_stride to 16 1.602 + 1.603 + NalContext *nc = av_mallocz(sizeof(NalContext)); 1.604 + nc->width = width; 1.605 + nc->height = height; 1.606 + nc->mb_height = mb_height; 1.607 + nc->mb_width = mb_width; 1.608 + nc->b4_stride = mb_width*4 + 1; 1.609 + nc->mb_stride = mb_stride; 1.610 + nc->outputed_poc = INT_MIN; 1.611 + 1.612 + for(int i=0; i<16; i++){ 1.613 + nc->picture[i].cpn =-1; 1.614 + } 1.615 + 1.616 + return nc; 1.617 +} 1.618 + 1.619 +void free_nal_context(NalContext *nc){ 1.620 + for(int i = 0; i < MAX_SPS_COUNT; i++){ 1.621 + if (nc->sps_buffers[i]){ 1.622 + av_free( nc->sps_buffers[i]); 1.623 + } 1.624 + } 1.625 + for(int i = 0; i < MAX_PPS_COUNT; i++){ 1.626 + if (nc->pps_buffers[i]){ 1.627 + av_free( nc->pps_buffers[i]); 1.628 + } 1.629 + } 1.630 + av_free(nc); 1.631 +}
