2 * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file libavcodec/h264_refs.c
24 * H.264 / AVC / MPEG4 part10 reference picture handling.
25 * @author Michael Niedermayer <michaelni@gmx.at>
38 static void pic_as_field(Picture *pic, const int parity){
40 for (i = 0; i < 4; ++i) {
41 if (parity == PICT_BOTTOM_FIELD)
42 pic->data[i] += pic->linesize[i];
43 pic->reference = parity;
44 pic->linesize[i] *= 2;
46 pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
49 static int split_field_copy(Picture *dest, Picture *src,
50 int parity, int id_add){
51 int match = !!(src->reference & parity);
55 if(parity != PICT_FRAME){
56 pic_as_field(dest, parity);
58 dest->pic_id += id_add;
65 static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
69 while(i[0]<len || i[1]<len){
70 while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
72 while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
75 in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
76 split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
79 in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
80 split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
87 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
92 best_poc= dir ? INT_MIN : INT_MAX;
95 const int poc= src[i]->poc;
96 if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
98 sorted[out_i]= src[i];
101 if(best_poc == (dir ? INT_MIN : INT_MAX))
103 limit= sorted[out_i++]->poc - dir;
108 int ff_h264_fill_default_ref_list(H264Context *h){
109 MpegEncContext * const s = &h->s;
112 if(h->slice_type_nos==FF_B_TYPE){
118 cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
120 cur_poc= s->current_picture_ptr->poc;
122 for(list= 0; list<2; list++){
123 len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
124 len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
126 len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
127 len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
130 if(len < h->ref_count[list])
131 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
135 if(lens[0] == lens[1] && lens[1] > 1){
136 for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
138 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
141 len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
142 len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
144 if(len < h->ref_count[0])
145 memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
148 for (i=0; i<h->ref_count[0]; i++) {
149 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]);
151 if(h->slice_type_nos==FF_B_TYPE){
152 for (i=0; i<h->ref_count[1]; i++) {
153 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]);
160 static void print_short_term(H264Context *h);
161 static void print_long_term(H264Context *h);
164 * Extract structure information about the picture described by pic_num in
165 * the current decoding context (frame or field). Note that pic_num is
166 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
167 * @param pic_num picture number for which to extract structure information
168 * @param structure one of PICT_XXX describing structure of picture
170 * @return frame number (short term) or long term index of picture
171 * described by pic_num
173 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
174 MpegEncContext * const s = &h->s;
176 *structure = s->picture_structure;
180 *structure ^= PICT_FRAME;
187 int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
188 MpegEncContext * const s = &h->s;
189 int list, index, pic_structure;
194 for(list=0; list<h->list_count; list++){
195 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
197 if(get_bits1(&s->gb)){
198 int pred= h->curr_pic_num;
200 for(index=0; ; index++){
201 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
206 if(reordering_of_pic_nums_idc==3)
209 if(index >= h->ref_count[list]){
210 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
214 if(reordering_of_pic_nums_idc<3){
215 if(reordering_of_pic_nums_idc<2){
216 const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
219 if(abs_diff_pic_num > h->max_pic_num){
220 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
224 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
225 else pred+= abs_diff_pic_num;
226 pred &= h->max_pic_num - 1;
228 frame_num = pic_num_extract(h, pred, &pic_structure);
230 for(i= h->short_ref_count-1; i>=0; i--){
231 ref = h->short_ref[i];
232 assert(ref->reference);
233 assert(!ref->long_ref);
235 ref->frame_num == frame_num &&
236 (ref->reference & pic_structure)
244 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
246 long_idx= pic_num_extract(h, pic_id, &pic_structure);
249 av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
252 ref = h->long_ref[long_idx];
253 assert(!(ref && !ref->reference));
254 if(ref && (ref->reference & pic_structure)){
256 assert(ref->long_ref);
264 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
265 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
267 for(i=index; i+1<h->ref_count[list]; i++){
268 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
271 for(; i > index; i--){
272 h->ref_list[list][i]= h->ref_list[list][i-1];
274 h->ref_list[list][index]= *ref;
276 pic_as_field(&h->ref_list[list][index], pic_structure);
280 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
286 for(list=0; list<h->list_count; list++){
287 for(index= 0; index < h->ref_count[list]; index++){
288 if(!h->ref_list[list][index].data[0]){
289 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
290 if(h->default_ref_list[list][0].data[0])
291 h->ref_list[list][index]= h->default_ref_list[list][0];
301 void ff_h264_fill_mbaff_ref_list(H264Context *h){
303 for(list=0; list<2; list++){ //FIXME try list_count
304 for(i=0; i<h->ref_count[list]; i++){
305 Picture *frame = &h->ref_list[list][i];
306 Picture *field = &h->ref_list[list][16+2*i];
309 field[0].linesize[j] <<= 1;
310 field[0].reference = PICT_TOP_FIELD;
311 field[0].poc= field[0].field_poc[0];
314 field[1].data[j] += frame->linesize[j];
315 field[1].reference = PICT_BOTTOM_FIELD;
316 field[1].poc= field[1].field_poc[1];
318 h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
319 h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
321 h->chroma_weight[16+2*i][list][j][0] = h->chroma_weight[16+2*i+1][list][j][0] = h->chroma_weight[i][list][j][0];
322 h->chroma_weight[16+2*i][list][j][1] = h->chroma_weight[16+2*i+1][list][j][1] = h->chroma_weight[i][list][j][1];
326 for(j=0; j<h->ref_count[1]; j++){
327 for(i=0; i<h->ref_count[0]; i++)
328 h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
329 memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
330 memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
335 * Mark a picture as no longer needed for reference. The refmask
336 * argument allows unreferencing of individual fields or the whole frame.
337 * If the picture becomes entirely unreferenced, but is being held for
338 * display purposes, it is marked as such.
339 * @param refmask mask of fields to unreference; the mask is bitwise
340 * anded with the reference marking of pic
341 * @return non-zero if pic becomes entirely unreferenced (except possibly
342 * for display purposes) zero if one of the fields remains in
345 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
347 if (pic->reference &= refmask) {
350 for(i = 0; h->delayed_pic[i]; i++)
351 if(pic == h->delayed_pic[i]){
352 pic->reference=DELAYED_PIC_REF;
360 * Find a Picture in the short term reference list by frame number.
361 * @param frame_num frame number to search for
362 * @param idx the index into h->short_ref where returned picture is found
363 * undefined if no picture found.
364 * @return pointer to the found picture, or NULL if no pic with the provided
365 * frame number is found
367 static Picture * find_short(H264Context *h, int frame_num, int *idx){
368 MpegEncContext * const s = &h->s;
371 for(i=0; i<h->short_ref_count; i++){
372 Picture *pic= h->short_ref[i];
373 if(s->avctx->debug&FF_DEBUG_MMCO)
374 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
375 if(pic->frame_num == frame_num) {
384 * Remove a picture from the short term reference list by its index in
385 * that list. This does no checking on the provided index; it is assumed
386 * to be valid. Other list entries are shifted down.
387 * @param i index into h->short_ref of picture to remove.
389 static void remove_short_at_index(H264Context *h, int i){
390 assert(i >= 0 && i < h->short_ref_count);
391 h->short_ref[i]= NULL;
392 if (--h->short_ref_count)
393 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
398 * @return the removed picture or NULL if an error occurs
400 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
401 MpegEncContext * const s = &h->s;
405 if(s->avctx->debug&FF_DEBUG_MMCO)
406 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
408 pic = find_short(h, frame_num, &i);
410 if(unreference_pic(h, pic, ref_mask))
411 remove_short_at_index(h, i);
418 * Remove a picture from the long term reference list by its index in
420 * @return the removed picture or NULL if an error occurs
422 static Picture * remove_long(H264Context *h, int i, int ref_mask){
427 if(unreference_pic(h, pic, ref_mask)){
428 assert(h->long_ref[i]->long_ref == 1);
429 h->long_ref[i]->long_ref= 0;
430 h->long_ref[i]= NULL;
438 void ff_h264_remove_all_refs(H264Context *h){
442 remove_long(h, i, 0);
444 assert(h->long_ref_count==0);
446 for(i=0; i<h->short_ref_count; i++){
447 unreference_pic(h, h->short_ref[i], 0);
448 h->short_ref[i]= NULL;
450 h->short_ref_count=0;
454 * print short term list
456 static void print_short_term(H264Context *h) {
458 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
459 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
460 for(i=0; i<h->short_ref_count; i++){
461 Picture *pic= h->short_ref[i];
462 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
468 * print long term list
470 static void print_long_term(H264Context *h) {
472 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
473 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
474 for(i = 0; i < 16; i++){
475 Picture *pic= h->long_ref[i];
477 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
483 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
484 MpegEncContext * const s = &h->s;
486 int current_ref_assigned=0;
487 Picture *av_uninit(pic);
489 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
490 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
492 for(i=0; i<mmco_count; i++){
493 int av_uninit(structure), av_uninit(frame_num);
494 if(s->avctx->debug&FF_DEBUG_MMCO)
495 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);
497 if( mmco[i].opcode == MMCO_SHORT2UNUSED
498 || mmco[i].opcode == MMCO_SHORT2LONG){
499 frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
500 pic = find_short(h, frame_num, &j);
502 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
503 || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
504 av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
509 switch(mmco[i].opcode){
510 case MMCO_SHORT2UNUSED:
511 if(s->avctx->debug&FF_DEBUG_MMCO)
512 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);
513 remove_short(h, frame_num, structure ^ PICT_FRAME);
515 case MMCO_SHORT2LONG:
516 if (h->long_ref[mmco[i].long_arg] != pic)
517 remove_long(h, mmco[i].long_arg, 0);
519 remove_short_at_index(h, j);
520 h->long_ref[ mmco[i].long_arg ]= pic;
521 if (h->long_ref[ mmco[i].long_arg ]){
522 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
526 case MMCO_LONG2UNUSED:
527 j = pic_num_extract(h, mmco[i].long_arg, &structure);
528 pic = h->long_ref[j];
530 remove_long(h, j, structure ^ PICT_FRAME);
531 } else if(s->avctx->debug&FF_DEBUG_MMCO)
532 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
535 // Comment below left from previous code as it is an interresting note.
536 /* First field in pair is in short term list or
537 * at a different long term index.
538 * This is not allowed; see 7.4.3.3, notes 2 and 3.
539 * Report the problem and keep the pair where it is,
540 * and mark this field valid.
543 if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
544 remove_long(h, mmco[i].long_arg, 0);
546 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
547 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
551 s->current_picture_ptr->reference |= s->picture_structure;
552 current_ref_assigned=1;
554 case MMCO_SET_MAX_LONG:
555 assert(mmco[i].long_arg <= 16);
556 // just remove the long term which index is greater than new max
557 for(j = mmco[i].long_arg; j<16; j++){
558 remove_long(h, j, 0);
562 while(h->short_ref_count){
563 remove_short(h, h->short_ref[0]->frame_num, 0);
565 for(j = 0; j < 16; j++) {
566 remove_long(h, j, 0);
568 s->current_picture_ptr->poc=
569 s->current_picture_ptr->field_poc[0]=
570 s->current_picture_ptr->field_poc[1]=
574 s->current_picture_ptr->frame_num= 0;
575 s->current_picture_ptr->mmco_reset=1;
581 if (!current_ref_assigned) {
582 /* Second field of complementary field pair; the first field of
583 * which is already referenced. If short referenced, it
584 * should be first entry in short_ref. If not, it must exist
585 * in long_ref; trying to put it on the short list here is an
586 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
588 if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
589 /* Just mark the second field valid */
590 s->current_picture_ptr->reference = PICT_FRAME;
591 } else if (s->current_picture_ptr->long_ref) {
592 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
593 "assignment for second field "
594 "in complementary field pair "
595 "(first field is long term)\n");
597 pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
599 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
602 if(h->short_ref_count)
603 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
605 h->short_ref[0]= s->current_picture_ptr;
606 h->short_ref_count++;
607 s->current_picture_ptr->reference |= s->picture_structure;
611 if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
613 /* We have too many reference frames, probably due to corrupted
614 * stream. Need to discard one frame. Prevents overrun of the
615 * short_ref and long_ref buffers.
617 av_log(h->s.avctx, AV_LOG_ERROR,
618 "number of reference frames exceeds max (probably "
619 "corrupt input), discarding one\n");
621 if (h->long_ref_count && !h->short_ref_count) {
622 for (i = 0; i < 16; ++i)
627 remove_long(h, i, 0);
629 pic = h->short_ref[h->short_ref_count - 1];
630 remove_short(h, pic->frame_num, 0);
639 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
640 MpegEncContext * const s = &h->s;
644 if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
645 s->broken_link= get_bits1(gb) -1;
647 h->mmco[0].opcode= MMCO_LONG;
648 h->mmco[0].long_arg= 0;
652 if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
653 for(i= 0; i<MAX_MMCO_COUNT; i++) {
654 MMCOOpcode opcode= get_ue_golomb_31(gb);
656 h->mmco[i].opcode= opcode;
657 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
658 h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
659 /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
660 av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
664 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
665 unsigned int long_arg= get_ue_golomb_31(gb);
666 if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
667 av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
670 h->mmco[i].long_arg= long_arg;
673 if(opcode > (unsigned)MMCO_LONG){
674 av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
677 if(opcode == MMCO_END)
682 assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
684 if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
685 !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
686 h->mmco[0].opcode= MMCO_SHORT2UNUSED;
687 h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
690 h->mmco[0].short_pic_num *= 2;
691 h->mmco[1].opcode= MMCO_SHORT2UNUSED;
692 h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;