comparison h264.c @ 2537:14fef0f3f532 libavcodec

H.264: decode arbitrary frame orders and allow B-frames as references.
author lorenm
date Fri, 04 Mar 2005 12:47:38 +0000
parents 943b0615fc93
children 34b0996cc10d
comparison
equal deleted inserted replaced
2536:943b0615fc93 2537:14fef0f3f532
272 272
273 int redundant_pic_count; 273 int redundant_pic_count;
274 274
275 int direct_spatial_mv_pred; 275 int direct_spatial_mv_pred;
276 int dist_scale_factor[16]; 276 int dist_scale_factor[16];
277 int map_col_to_list0[2][16];
277 278
278 /** 279 /**
279 * num_ref_idx_l0/1_active_minus1 + 1 280 * num_ref_idx_l0/1_active_minus1 + 1
280 */ 281 */
281 int ref_count[2];// FIXME split for AFF 282 int ref_count[2];// FIXME split for AFF
283 Picture *long_ref[16]; 284 Picture *long_ref[16];
284 Picture default_ref_list[2][32]; 285 Picture default_ref_list[2][32];
285 Picture ref_list[2][32]; //FIXME size? 286 Picture ref_list[2][32]; //FIXME size?
286 Picture field_ref_list[2][32]; //FIXME size? 287 Picture field_ref_list[2][32]; //FIXME size?
287 Picture *delayed_pic[16]; //FIXME size? 288 Picture *delayed_pic[16]; //FIXME size?
289 int delayed_output_poc;
288 290
289 /** 291 /**
290 * memory management control operations buffer. 292 * memory management control operations buffer.
291 */ 293 */
292 MMCO mmco[MAX_MMCO_COUNT]; 294 MMCO mmco[MAX_MMCO_COUNT];
1058 int tx = (16384 + (ABS(td) >> 1)) / td; 1060 int tx = (16384 + (ABS(td) >> 1)) / td;
1059 h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023); 1061 h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
1060 } 1062 }
1061 } 1063 }
1062 } 1064 }
1065 static inline void direct_ref_list_init(H264Context * const h){
1066 MpegEncContext * const s = &h->s;
1067 Picture * const ref1 = &h->ref_list[1][0];
1068 Picture * const cur = s->current_picture_ptr;
1069 int list, i, j;
1070 if(cur->pict_type == I_TYPE)
1071 cur->ref_count[0] = 0;
1072 if(cur->pict_type != B_TYPE)
1073 cur->ref_count[1] = 0;
1074 for(list=0; list<2; list++){
1075 cur->ref_count[list] = h->ref_count[list];
1076 for(j=0; j<h->ref_count[list]; j++)
1077 cur->ref_poc[list][j] = h->ref_list[list][j].poc;
1078 }
1079 if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
1080 return;
1081 for(list=0; list<2; list++){
1082 for(i=0; i<ref1->ref_count[list]; i++){
1083 const int poc = ref1->ref_poc[list][i];
1084 h->map_col_to_list0[list][i] = PART_NOT_AVAILABLE;
1085 for(j=0; j<h->ref_count[list]; j++)
1086 if(h->ref_list[list][j].poc == poc){
1087 h->map_col_to_list0[list][i] = j;
1088 break;
1089 }
1090 }
1091 }
1092 }
1063 1093
1064 static inline void pred_direct_motion(H264Context * const h, int *mb_type){ 1094 static inline void pred_direct_motion(H264Context * const h, int *mb_type){
1065 MpegEncContext * const s = &h->s; 1095 MpegEncContext * const s = &h->s;
1066 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; 1096 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
1067 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride; 1097 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1068 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; 1098 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1069 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy]; 1099 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
1070 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy]; 1100 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
1071 const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy]; 1101 const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
1102 const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
1072 const int is_b8x8 = IS_8X8(*mb_type); 1103 const int is_b8x8 = IS_8X8(*mb_type);
1073 int sub_mb_type; 1104 int sub_mb_type;
1074 int i8, i4; 1105 int i8, i4;
1075 1106
1076 if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){ 1107 if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
1176 } 1207 }
1177 } 1208 }
1178 } 1209 }
1179 } 1210 }
1180 }else{ /* direct temporal mv pred */ 1211 }else{ /* direct temporal mv pred */
1181 /* FIXME assumes that L1ref0 used the same ref lists as current frame */
1182 if(IS_16X16(*mb_type)){ 1212 if(IS_16X16(*mb_type)){
1183 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1); 1213 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
1184 if(IS_INTRA(mb_type_col)){ 1214 if(IS_INTRA(mb_type_col)){
1185 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); 1215 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
1186 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4); 1216 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1187 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4); 1217 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1188 }else{ 1218 }else{
1189 const int ref0 = l1ref0[0]; 1219 const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]]
1220 : h->map_col_to_list0[1][l1ref1[0]];
1221 assert(ref0 >= 0);
1190 const int dist_scale_factor = h->dist_scale_factor[ref0]; 1222 const int dist_scale_factor = h->dist_scale_factor[ref0];
1191 const int16_t *mv_col = l1mv0[0]; 1223 const int16_t *mv_col = l1mv0[0];
1192 int mv_l0[2]; 1224 int mv_l0[2];
1193 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8; 1225 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
1194 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8; 1226 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
1212 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4); 1244 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1213 continue; 1245 continue;
1214 } 1246 }
1215 1247
1216 ref0 = l1ref0[x8 + y8*h->b8_stride]; 1248 ref0 = l1ref0[x8 + y8*h->b8_stride];
1249 if(ref0 >= 0)
1250 ref0 = h->map_col_to_list0[0][ref0];
1251 else
1252 ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
1253 assert(ref0 >= 0);
1217 dist_scale_factor = h->dist_scale_factor[ref0]; 1254 dist_scale_factor = h->dist_scale_factor[ref0];
1218 1255
1219 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1); 1256 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
1220 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1); 1257 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1221 for(i4=0; i4<4; i4++){ 1258 for(i4=0; i4<4; i4++){
2959 if(index < h->ref_count[ list ]) 2996 if(index < h->ref_count[ list ])
2960 memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index)); 2997 memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
2961 } 2998 }
2962 }else{ 2999 }else{
2963 int index=0; 3000 int index=0;
2964 for(i=0; i<h->short_ref_count && index < h->ref_count[0]; i++){ 3001 for(i=0; i<h->short_ref_count; i++){
2965 if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit 3002 if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
2966 h->default_ref_list[0][index ]= *h->short_ref[i]; 3003 h->default_ref_list[0][index ]= *h->short_ref[i];
2967 h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num; 3004 h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
2968 } 3005 }
2969 for(i = 0; i < 16 && index < h->ref_count[0]; i++){ 3006 for(i = 0; i < 16; i++){
2970 if(h->long_ref[i] == NULL) continue; 3007 if(h->long_ref[i] == NULL) continue;
2971 if(h->long_ref[i]->reference != 3) continue; 3008 if(h->long_ref[i]->reference != 3) continue;
2972 h->default_ref_list[0][index ]= *h->long_ref[i]; 3009 h->default_ref_list[0][index ]= *h->long_ref[i];
2973 h->default_ref_list[0][index++].pic_id= i;; 3010 h->default_ref_list[0][index++].pic_id= i;;
2974 } 3011 }
3014 3051
3015 for(index=0; ; index++){ 3052 for(index=0; ; index++){
3016 int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb); 3053 int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
3017 int pic_id; 3054 int pic_id;
3018 int i; 3055 int i;
3056 Picture *ref = NULL;
3019 3057
3020 if(reordering_of_pic_nums_idc==3) 3058 if(reordering_of_pic_nums_idc==3)
3021 break; 3059 break;
3022 3060
3023 if(index >= h->ref_count[list]){ 3061 if(index >= h->ref_count[list]){
3036 3074
3037 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num; 3075 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
3038 else pred+= abs_diff_pic_num; 3076 else pred+= abs_diff_pic_num;
3039 pred &= h->max_pic_num - 1; 3077 pred &= h->max_pic_num - 1;
3040 3078
3041 for(i= h->ref_count[list]-1; i>=0; i--){ 3079 for(i= h->short_ref_count-1; i>=0; i--){
3042 if(h->ref_list[list][i].data[0] != NULL && h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0) // ignore non existing pictures by testing data[0] pointer 3080 ref = h->short_ref[i];
3081 if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
3043 break; 3082 break;
3044 } 3083 }
3045 }else{ 3084 }else{
3046 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx 3085 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
3047 3086 ref = h->long_ref[pic_id];
3048 for(i= h->ref_count[list]-1; i>=0; i--){
3049 if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1) // no need to ignore non existing pictures as non existing pictures have long_ref==0
3050 break;
3051 }
3052 } 3087 }
3053 3088
3054 if (i < 0) { 3089 if (i < 0) {
3055 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n"); 3090 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
3056 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME 3091 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
3057 } else if (i != index) /* this test is not necessary, it is only an optimisation to skip double copy of Picture structure in this case */ { 3092 } else {
3058 Picture tmp= h->ref_list[list][i]; 3093 h->ref_list[list][index]= *ref;
3059 if (i < index) {
3060 i = h->ref_count[list];
3061 }
3062 for(; i > index; i--){
3063 h->ref_list[list][i]= h->ref_list[list][i-1];
3064 }
3065 h->ref_list[list][index]= tmp;
3066 } 3094 }
3067 }else{ 3095 }else{
3068 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n"); 3096 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
3069 return -1; 3097 return -1;
3070 } 3098 }
3074 if(h->slice_type!=B_TYPE) break; 3102 if(h->slice_type!=B_TYPE) break;
3075 } 3103 }
3076 3104
3077 if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred) 3105 if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
3078 direct_dist_scale_factor(h); 3106 direct_dist_scale_factor(h);
3107 direct_ref_list_init(h);
3079 return 0; 3108 return 0;
3080 } 3109 }
3081 3110
3082 static int pred_weight_table(H264Context *h){ 3111 static int pred_weight_table(H264Context *h){
3083 MpegEncContext * const s = &h->s; 3112 MpegEncContext * const s = &h->s;
3507 static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE}; 3536 static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
3508 int slice_type; 3537 int slice_type;
3509 int default_ref_list_done = 0; 3538 int default_ref_list_done = 0;
3510 3539
3511 s->current_picture.reference= h->nal_ref_idc != 0; 3540 s->current_picture.reference= h->nal_ref_idc != 0;
3541 s->dropable= h->nal_ref_idc == 0;
3512 3542
3513 first_mb_in_slice= get_ue_golomb(&s->gb); 3543 first_mb_in_slice= get_ue_golomb(&s->gb);
3514 3544
3515 slice_type= get_ue_golomb(&s->gb); 3545 slice_type= get_ue_golomb(&s->gb);
3516 if(slice_type > 9){ 3546 if(slice_type > 9){
6296 av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type); 6326 av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
6297 } 6327 }
6298 6328
6299 //FIXME move after where irt is set 6329 //FIXME move after where irt is set
6300 s->current_picture.pict_type= s->pict_type; 6330 s->current_picture.pict_type= s->pict_type;
6301 s->current_picture.key_frame= s->pict_type == I_TYPE; 6331 s->current_picture.key_frame= s->pict_type == I_TYPE && h->nal_unit_type == NAL_IDR_SLICE;
6302 } 6332 }
6303 6333
6304 if(!s->current_picture_ptr) return buf_index; //no frame 6334 if(!s->current_picture_ptr) return buf_index; //no frame
6305 6335
6306 h->prev_frame_num_offset= h->frame_num_offset; 6336 h->prev_frame_num_offset= h->frame_num_offset;
6421 6451
6422 { 6452 {
6423 //#define DECODE_ORDER 6453 //#define DECODE_ORDER
6424 Picture *out = s->current_picture_ptr; 6454 Picture *out = s->current_picture_ptr;
6425 #ifndef DECODE_ORDER 6455 #ifndef DECODE_ORDER
6426 /* Sort B-frames into display order 6456 /* Sort B-frames into display order */
6427 * FIXME doesn't allow for multiple delayed frames */
6428 Picture *cur = s->current_picture_ptr; 6457 Picture *cur = s->current_picture_ptr;
6429 Picture *prev = h->delayed_pic[0]; 6458 int out_idx = 0;
6430 6459 int pics = 0;
6431 if(s->low_delay 6460 int i;
6432 && (cur->pict_type == B_TYPE 6461 out = NULL;
6433 || (!h->sps.gaps_in_frame_num_allowed_flag 6462
6434 && prev && cur->poc - prev->poc > 2))){ 6463 while(h->delayed_pic[pics]) pics++;
6464 h->delayed_pic[pics++] = cur;
6465 out = h->delayed_pic[0];
6466 for(i=0; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
6467 if(!out || h->delayed_pic[i]->poc < out->poc){
6468 out = h->delayed_pic[i];
6469 out_idx = i;
6470 }
6471 if(cur->reference == 0)
6472 cur->reference = 1;
6473 if(pics > FFMAX(1, s->avctx->has_b_frames)){
6474 if(out->reference == 1)
6475 out->reference = 0;
6476 for(i=out_idx; h->delayed_pic[i]; i++)
6477 h->delayed_pic[i] = h->delayed_pic[i+1];
6478 }
6479
6480 for(i=0; h->delayed_pic[i]; i++)
6481 if(h->delayed_pic[i]->key_frame)
6482 h->delayed_output_poc = -1;
6483
6484 if((h->delayed_output_poc >=0 && h->delayed_output_poc > cur->poc)
6485 || (s->low_delay && (cur->pict_type == B_TYPE
6486 || (!h->sps.gaps_in_frame_num_allowed_flag
6487 && cur->poc - out->poc > 2)))){
6435 s->low_delay = 0; 6488 s->low_delay = 0;
6436 s->avctx->has_b_frames = 1; 6489 s->avctx->has_b_frames++;
6437 if(prev && prev->poc > cur->poc) 6490 }
6438 // too late to display this frame 6491
6439 cur = prev; 6492 h->delayed_output_poc = out->poc;
6440 }
6441
6442 if(s->low_delay || !prev || cur->pict_type == B_TYPE)
6443 out = cur;
6444 else
6445 out = prev;
6446 if(s->low_delay || !prev || out == prev){
6447 if(prev && prev->reference == 1)
6448 prev->reference = 0;
6449 h->delayed_pic[0] = cur;
6450 }
6451 #endif 6493 #endif
6452 6494
6453 *pict= *(AVFrame*)out; 6495 *pict= *(AVFrame*)out;
6454 } 6496 }
6455 6497