Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
comparison libavcodec/h264_refs.c @ 4:96e628866d41
naming some tasks to help debugging
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 19 Dec 2012 15:40:26 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:9f4ee0134057 |
|---|---|
| 1 /* | |
| 2 * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling | |
| 3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> | |
| 4 * | |
| 5 * This file is part of FFmpeg. | |
| 6 * | |
| 7 * FFmpeg is free software; you can redistribute it and/or | |
| 8 * modify it under the terms of the GNU Lesser General Public | |
| 9 * License as published by the Free Software Foundation; either | |
| 10 * version 2.1 of the License, or (at your option) any later version. | |
| 11 * | |
| 12 * FFmpeg is distributed in the hope that it will be useful, | |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
| 18 * License along with FFmpeg; if not, write to the Free Software | |
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 20 */ | |
| 21 | |
| 22 /** | |
| 23 * @file | |
| 24 * H.264 / AVC / MPEG4 part10 reference picture handling. | |
| 25 * @author Michael Niedermayer <michaelni@gmx.at> | |
| 26 */ | |
| 27 | |
| 28 #include "dsputil.h" | |
| 29 #include "h264_types.h" | |
| 30 #include "golomb.h" | |
| 31 | |
| 32 //#undef NDEBUG | |
| 33 #include <assert.h> | |
| 34 | |
| 35 static int build_def_list(PictureInfo **def, PictureInfo **in, int len, int is_long){ | |
| 36 int i[2]={0}; | |
| 37 int index=0; | |
| 38 | |
| 39 while(i[0]<len || i[1]<len){ | |
| 40 while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference))) | |
| 41 i[0]++; | |
| 42 while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & 0))) | |
| 43 i[1]++; | |
| 44 if(i[0] < len){ | |
| 45 in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num; | |
| 46 def[index++]= in[ i[0]++ ]; | |
| 47 } | |
| 48 if(i[1] < len){ | |
| 49 in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num; | |
| 50 def[index++]= in[ i[1]++ ]; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 return index; | |
| 55 } | |
| 56 | |
| 57 static int add_sorted(PictureInfo **sorted, PictureInfo **src, int len, int limit, int dir){ | |
| 58 int i, best_poc; | |
| 59 int out_i= 0; | |
| 60 | |
| 61 for(;;){ | |
| 62 best_poc= dir ? INT_MIN : INT_MAX; | |
| 63 | |
| 64 for(i=0; i<len; i++){ | |
| 65 const int poc= src[i]->poc; | |
| 66 if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){ | |
| 67 best_poc= poc; | |
| 68 sorted[out_i]= src[i]; | |
| 69 } | |
| 70 } | |
| 71 if(best_poc == (dir ? INT_MIN : INT_MAX)) | |
| 72 break; | |
| 73 limit= sorted[out_i++]->poc - dir; | |
| 74 } | |
| 75 return out_i; | |
| 76 } | |
| 77 | |
| 78 int ff_h264_fill_default_ref_list(NalContext *n, H264Slice *s){ | |
| 79 int i,len; | |
| 80 | |
| 81 if(s->slice_type_nos==FF_B_TYPE){ | |
| 82 PictureInfo *sorted[32]; | |
| 83 int cur_poc, list; | |
| 84 int lens[2]; | |
| 85 | |
| 86 cur_poc= s->poc; | |
| 87 | |
| 88 for(list= 0; list<2; list++){ | |
| 89 len= add_sorted(sorted, n->short_ref, n->short_ref_count, cur_poc, !list); | |
| 90 len+=add_sorted(sorted+len, n->short_ref, n->short_ref_count, cur_poc, list); | |
| 91 assert(len<=32); | |
| 92 len= build_def_list(s->ref_list[list], sorted, len, 0); | |
| 93 len+=build_def_list(s->ref_list[list] +len, n->long_ref, 16 , 1); | |
| 94 assert(len<=32); | |
| 95 | |
| 96 for(int i=len; i<s->ref_count[list]; i++) | |
| 97 s->ref_list[list][i] = NULL; | |
| 98 | |
| 99 lens[list]= len; | |
| 100 } | |
| 101 | |
| 102 if(lens[0] == lens[1] && lens[1] > 1){ | |
| 103 for(i=0; s->ref_list[0][i]->poc == s->ref_list[1][i]->poc && i<lens[0]; i++); | |
| 104 | |
| 105 if(i == lens[0]) | |
| 106 FFSWAP(PictureInfo *, s->ref_list[1][0], s->ref_list[1][1]); | |
| 107 } | |
| 108 }else{ | |
| 109 len = build_def_list(s->ref_list[0], n->short_ref, n->short_ref_count, 0); | |
| 110 len+= build_def_list(s->ref_list[0] +len, n->long_ref, 16, 1); | |
| 111 assert(len <= 32); | |
| 112 for(i=len; i<s->ref_count[0]; i++) | |
| 113 s->ref_list[0][i] = NULL; | |
| 114 } | |
| 115 | |
| 116 return 0; | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * print short term list | |
| 121 */ | |
| 122 static void print_short_term(NalContext *n) { | |
| 123 av_log(AV_LOG_DEBUG, "short term list:\n"); | |
| 124 for(int i=0; i<n->short_ref_count; i++){ | |
| 125 PictureInfo *pic= n->short_ref[i]; | |
| 126 av_log(AV_LOG_DEBUG, "%d fn:%d poc:%d ref:%d \n", i, pic->frame_num, pic->poc, pic->reference); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 /** | |
| 131 * print long term list | |
| 132 */ | |
| 133 static void print_long_term(NalContext *n) { | |
| 134 uint32_t i; | |
| 135 | |
| 136 av_log(AV_LOG_DEBUG, "long term list:\n"); | |
| 137 for(i = 0; i < 16; i++){ | |
| 138 PictureInfo *pic= n->long_ref[i]; | |
| 139 if (pic) { | |
| 140 av_log(AV_LOG_DEBUG, "%d fn:%d poc:%d\n", i, pic->frame_num, pic->poc); | |
| 141 } | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 int ff_h264_decode_ref_pic_list_reordering(NalContext *n, H264Slice *s, GetBitContext *gb){ | |
| 146 int list, index; | |
| 147 | |
| 148 print_short_term(n); | |
| 149 print_long_term(n); | |
| 150 | |
| 151 for(list=0; list<s->list_count; list++){ | |
| 152 | |
| 153 if(get_bits1(gb)){ | |
| 154 int frame_num = n->frame_num; | |
| 155 unsigned int abs_diff_pic_num; | |
| 156 for(index=0; ; index++){ | |
| 157 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(gb); | |
| 158 int i=0; | |
| 159 PictureInfo *ref = NULL; | |
| 160 | |
| 161 if(reordering_of_pic_nums_idc==3){ | |
| 162 break; | |
| 163 } | |
| 164 if(index >= s->ref_count[list]){ | |
| 165 av_log(AV_LOG_ERROR, "reference count overflow\n"); | |
| 166 return -1; | |
| 167 } | |
| 168 | |
| 169 if (reordering_of_pic_nums_idc>2){ | |
| 170 av_log(AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n"); | |
| 171 return -1; | |
| 172 } | |
| 173 | |
| 174 if (reordering_of_pic_nums_idc<2){ | |
| 175 //av_log(AV_LOG_ERROR, "long term pic not supported\n"); | |
| 176 | |
| 177 abs_diff_pic_num= get_ue_golomb(gb) + 1; | |
| 178 if(abs_diff_pic_num > (unsigned) n->max_pic_num){ | |
| 179 av_log(AV_LOG_ERROR, "abs_diff_pic_num overflow\n"); | |
| 180 return -1; | |
| 181 } | |
| 182 | |
| 183 if(reordering_of_pic_nums_idc == 0) | |
| 184 frame_num-= abs_diff_pic_num; | |
| 185 else | |
| 186 frame_num+= abs_diff_pic_num; | |
| 187 frame_num &= n->max_pic_num - 1; | |
| 188 | |
| 189 for(i= 0 ; i<n->short_ref_count; i++){ | |
| 190 ref = n->short_ref[i]; | |
| 191 if(ref->frame_num == frame_num && ref->reference){ | |
| 192 break; | |
| 193 } | |
| 194 } | |
| 195 ref->pic_id= frame_num; | |
| 196 }else{ | |
| 197 int long_idx; | |
| 198 long_idx= get_ue_golomb(gb); //long_term_pic_idx | |
| 199 | |
| 200 if(long_idx>31){ | |
| 201 av_log(AV_LOG_ERROR, "long_term_pic_idx overflow\n"); | |
| 202 return -1; | |
| 203 } | |
| 204 ref = n->long_ref[long_idx]; | |
| 205 assert(!(ref && !ref->reference)); | |
| 206 if(ref && (ref->reference)){ | |
| 207 ref->pic_id= long_idx; | |
| 208 assert(ref->long_ref); | |
| 209 }else{ | |
| 210 av_log(AV_LOG_ERROR, "reference picture missing during reorder\n"); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 if (i >= n->short_ref_count) { | |
| 215 av_log(AV_LOG_ERROR, "reference picture missing during reorder\n"); | |
| 216 return -1; | |
| 217 } else { | |
| 218 for(i=index; i+1 <s->ref_count[list]; i++){ | |
| 219 | |
| 220 // if(ref->frame_num == s->ref_list[list][i]->frame_num) | |
| 221 // break; | |
| 222 ///there is probably no need for a separate pic_id and frame_num | |
| 223 if (s->ref_list[list][i]){ | |
| 224 | |
| 225 if(ref->long_ref == s->ref_list[list][i]->long_ref && ref->pic_id == s->ref_list[list][i]->pic_id) | |
| 226 break; | |
| 227 } | |
| 228 } | |
| 229 for(; i > index; i--){ | |
| 230 s->ref_list[list][i]= s->ref_list[list][i-1]; | |
| 231 } | |
| 232 s->ref_list[list][index]= ref; | |
| 233 } | |
| 234 } | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 // //Check if everything went well | |
| 239 // for(list=0; list<s->list_count; list++){ | |
| 240 // //printf("ref_count %d list %d\n", s->ref_count[list], list); | |
| 241 // for(index= 0; index < s->ref_count[list]; index++){ | |
| 242 // //printf("%d\n", s->ref_list[list][index]->pic_id); | |
| 243 // if(!s->ref_list[list][index]->data[0]){ | |
| 244 // av_log(AV_LOG_ERROR, "Missing reference picture\n"); | |
| 245 // return -1; | |
| 246 // } | |
| 247 // } | |
| 248 // } | |
| 249 | |
| 250 return 0; | |
| 251 } | |
| 252 | |
| 253 static PictureInfo *find_short(NalContext *n, int frame_num){ | |
| 254 int i; | |
| 255 for(i=0; i<n->short_ref_count; i++){ | |
| 256 if(n->short_ref[i]->frame_num == frame_num) { | |
| 257 return n->short_ref[i]; | |
| 258 } | |
| 259 } | |
| 260 return NULL; | |
| 261 } | |
| 262 | |
| 263 static int remove_short(NalContext *n, H264Slice *s, int frame_num, int release){ | |
| 264 int i; | |
| 265 | |
| 266 for (i=0; i<n->short_ref_count; i++){ | |
| 267 if (n->short_ref[i]->frame_num == frame_num){ | |
| 268 if (release){ | |
| 269 s->release_ref_cpn[s->release_cnt++] = n->short_ref[i]->cpn; | |
| 270 n->short_ref[i]->reference &= ~2; | |
| 271 } | |
| 272 n->short_ref[i] = NULL; | |
| 273 if (--n->short_ref_count) | |
| 274 memmove(&n->short_ref[i], &n->short_ref[i+1], (n->short_ref_count - i)*sizeof(PictureInfo *)); | |
| 275 return 0; | |
| 276 } | |
| 277 } | |
| 278 return -1; | |
| 279 } | |
| 280 | |
| 281 static void remove_long(NalContext *n, H264Slice *s, int i){ | |
| 282 | |
| 283 if (n->long_ref[i]){ | |
| 284 s->release_ref_cpn[s->release_cnt++] = n->long_ref[i]->cpn; | |
| 285 n->long_ref[i]->reference &= ~2; | |
| 286 n->long_ref[i]->long_ref = 0; | |
| 287 n->long_ref_count--; | |
| 288 n->long_ref[i] = NULL; | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 void ff_h264_remove_all_refs(NalContext *n, H264Slice *s){ | |
| 293 int i; | |
| 294 | |
| 295 while (n->short_ref[0]) | |
| 296 remove_short(n, s, n->short_ref[0]->frame_num, 1); | |
| 297 | |
| 298 for(i=0; i<16; i++){ | |
| 299 remove_long(n, s, i); | |
| 300 } | |
| 301 assert(n->short_ref_count==0); | |
| 302 assert(n->long_ref_count==0); | |
| 303 } | |
| 304 | |
| 305 int ff_h264_ref_pic_marking(NalContext *n, H264Slice *s, GetBitContext *gb){ | |
| 306 | |
| 307 if(s->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields | |
| 308 get_bits1(gb); //get_bits1(gb) -1; //broken link | |
| 309 if(get_bits1(gb)){ | |
| 310 av_log(AV_LOG_ERROR, "MMCO_LONG reference management not supported\n"); | |
| 311 } | |
| 312 }else{ | |
| 313 if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag | |
| 314 int i,j; | |
| 315 for(i= 0; i<MAX_MMCO_COUNT; i++) { | |
| 316 PictureInfo *pic; | |
| 317 int short_pic_num=0; | |
| 318 unsigned int long_arg=0; | |
| 319 MMCOOpcode opcode= get_ue_golomb_31(gb); | |
| 320 | |
| 321 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){ | |
| 322 short_pic_num= (n->frame_num - get_ue_golomb(gb) - 1) & (n->max_pic_num - 1); | |
| 323 } | |
| 324 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){ | |
| 325 long_arg= get_ue_golomb_31(gb); | |
| 326 if(long_arg >= 16){ | |
| 327 av_log(AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode); | |
| 328 return -1; | |
| 329 } | |
| 330 } | |
| 331 | |
| 332 if(opcode > (unsigned)MMCO_LONG){ | |
| 333 av_log(AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode); | |
| 334 return -1; | |
| 335 } | |
| 336 if(opcode == MMCO_END) | |
| 337 break; | |
| 338 | |
| 339 switch (opcode){ | |
| 340 case MMCO_SHORT2UNUSED: | |
| 341 remove_short(n, s, short_pic_num, 1); | |
| 342 break; | |
| 343 case MMCO_SHORT2LONG: | |
| 344 pic = find_short(n, short_pic_num); | |
| 345 if (n->long_ref[long_arg] != pic) | |
| 346 remove_long(n, s, long_arg); | |
| 347 remove_short(n, s, short_pic_num, 0); | |
| 348 n->long_ref[long_arg]= pic; | |
| 349 if (pic){ | |
| 350 pic->long_ref=1; | |
| 351 n->long_ref[long_arg]= pic; | |
| 352 n->long_ref_count++; | |
| 353 } | |
| 354 break; | |
| 355 case MMCO_LONG2UNUSED: | |
| 356 assert(n->long_ref[long_arg]); | |
| 357 remove_long(n, s, long_arg); | |
| 358 break; | |
| 359 case MMCO_SET_MAX_LONG: | |
| 360 for(j=long_arg; j<16; j++) | |
| 361 remove_long(n, s, j); | |
| 362 break; | |
| 363 case MMCO_RESET: | |
| 364 while(n->short_ref_count) | |
| 365 remove_short(n, s, n->short_ref[0]->frame_num, 1); | |
| 366 | |
| 367 for(j=0; j < 16; j++) | |
| 368 remove_long(n, s, j); | |
| 369 | |
| 370 s->current_picture_info->poc= | |
| 371 s->poc = | |
| 372 n->poc_lsb= | |
| 373 n->poc_msb= | |
| 374 n->frame_num= | |
| 375 s->current_picture_info->frame_num= 0; | |
| 376 break; | |
| 377 case MMCO_END: | |
| 378 case MMCO_LONG: | |
| 379 break; | |
| 380 } | |
| 381 } | |
| 382 }else{// sliding window ref picture marking | |
| 383 if(n->short_ref_count == n->sps.ref_frame_count) { | |
| 384 s->release_ref_cpn[s->release_cnt++] = n->short_ref[n->short_ref_count - 1]->cpn; | |
| 385 n->short_ref[n->short_ref_count - 1]->reference &= ~2; | |
| 386 n->short_ref[ n->short_ref_count - 1 ] =NULL; | |
| 387 n->short_ref_count--; | |
| 388 } | |
| 389 } | |
| 390 } | |
| 391 | |
| 392 if(n->short_ref_count) | |
| 393 memmove(&n->short_ref[1], &n->short_ref[0], n->short_ref_count*sizeof(PictureInfo *)); | |
| 394 | |
| 395 n->short_ref[0]= s->current_picture_info; | |
| 396 n->short_ref_count++; | |
| 397 | |
| 398 return 0; | |
| 399 } | |
| 400 | |
| 401 static int get_scale_factor(H264Slice *s, int poc, int poc1, int i){ | |
| 402 int poc0 = s->ref_list[0][i]->poc; | |
| 403 int td = av_clip(poc1 - poc0, -128, 127); | |
| 404 if(td == 0 || s->ref_list[0][i]->long_ref){ | |
| 405 return 256; | |
| 406 }else{ | |
| 407 int tb = av_clip(poc - poc0, -128, 127); | |
| 408 int tx = (16384 + (FFABS(td) >> 1)) / td; | |
| 409 return av_clip((tb*tx + 32) >> 6, -1024, 1023); | |
| 410 } | |
| 411 } | |
| 412 | |
| 413 void ff_h264_direct_dist_scale_factor(H264Slice *s){ | |
| 414 const int poc = s->current_picture_info->poc; | |
| 415 const int poc1 = s->ref_list[1][0]->poc; | |
| 416 | |
| 417 for(int i=0; i<s->ref_count[0]; i++){ | |
| 418 s->dist_scale_factor[i] = get_scale_factor(s, poc, poc1, i); | |
| 419 } | |
| 420 } | |
| 421 | |
| 422 static void fill_colmap(H264Slice *s, int map[2][16], int list){ | |
| 423 PictureInfo * const ref1 = s->ref_list[1][0]; | |
| 424 int old_ref, rfield; | |
| 425 | |
| 426 /* bogus; fills in for missing frames */ | |
| 427 memset(map[list], 0, sizeof(map[list])); | |
| 428 | |
| 429 for(rfield=0; rfield<2; rfield++){ | |
| 430 for(old_ref=0; old_ref < ref1->ref_count[list]; old_ref++){ | |
| 431 int poc = ref1->ref_poc[list][old_ref]; | |
| 432 | |
| 433 for(int j=0; j<s->ref_count[0]; j++){ | |
| 434 if(s->ref_list[0][j]->poc == poc){ | |
| 435 map[list][old_ref] = j; | |
| 436 break; | |
| 437 } | |
| 438 } | |
| 439 } | |
| 440 } | |
| 441 } | |
| 442 | |
| 443 void ff_h264_direct_ref_list_init(H264Slice *s){ | |
| 444 PictureInfo * const cur = s->current_picture_info; | |
| 445 int list; | |
| 446 | |
| 447 for(list=0; list<2; list++){ | |
| 448 cur->ref_count[list] = s->ref_count[list]; | |
| 449 for(int j=0; j<s->ref_count[list]; j++){ | |
| 450 cur->ref_poc[list][j] = s->ref_list[list][j] ? s->ref_list[list][j]->poc : 0; | |
| 451 } | |
| 452 } | |
| 453 | |
| 454 if(s->slice_type_nos != FF_B_TYPE || s->direct_spatial_mv_pred) | |
| 455 return; | |
| 456 | |
| 457 for(list=0; list<2; list++){ | |
| 458 fill_colmap(s, s->map_col_to_list0, list); | |
| 459 } | |
| 460 } | |
| 461 |
