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 / MPEG-4 part10 reference picture handling.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include <inttypes.h>
29
37
38 #include <assert.h>
39
41 {
48 }
50 }
51
53 {
60 }
61
63 {
65
66 if (match) {
72 }
73 }
74
75 return match;
76 }
77
80 {
83
85 while (
i[0] <
len && !(in[
i[0]] && (in[
i[0]]->reference & sel)))
87 while (
i[1] <
len && !(in[
i[1]] && (in[
i[1]]->reference & (sel ^ 3))))
93 }
98 }
99 }
100
102 }
103
106 {
108 int out_i = 0;
109
110 for (;;) {
111 best_poc = dir ? INT_MIN : INT_MAX;
112
113 for (
i = 0;
i <
len;
i++) {
114 const int poc =
src[
i]->poc;
115 if (((poc >
limit) ^ dir) && ((poc < best_poc) ^ dir)) {
116 best_poc = poc;
117 sorted[out_i] =
src[
i];
118 }
119 }
120 if (best_poc == (dir ? INT_MIN : INT_MAX))
121 break;
123 }
124 return out_i;
125 }
126
128 {
130 return (
h->cur_pic_ptr->f->width !=
f->width ||
131 h->cur_pic_ptr->f->height !=
f->height ||
132 h->cur_pic_ptr->f->format !=
f->format);
133 }
134
136 {
138 int j;
139
143 int lens[2];
144
147 else
148 cur_poc =
h->cur_pic_ptr->poc;
149
154
156 sorted,
len, 0,
h->picture_structure);
159 h->long_ref, 16, 1,
h->picture_structure);
161
162 if (len < sl->ref_count[
list])
165 }
166
167 if (lens[0] == lens[1] && lens[1] > 1) {
168 for (
i = 0;
i < lens[0] &&
173 }
174 }
175 } else {
177 h->short_ref,
h->short_ref_count, 0,
h->picture_structure);
180 h-> long_ref, 16, 1,
h->picture_structure);
182
183 if (len < sl->ref_count[0])
185 }
186 #ifdef TRACE
188 ff_tlog(
h->avctx,
"List0: %s fn:%d 0x%p\n",
192 }
195 ff_tlog(
h->avctx,
"List1: %s fn:%d 0x%p\n",
199 }
200 }
201 #endif
202
209 }
210 }
211 }
212 }
215 }
216
217 /**
218 * print short term list
219 */
221 {
225 for (
i = 0;
i <
h->short_ref_count;
i++) {
229 }
230 }
231 }
232
233 /**
234 * print long term list
235 */
237 {
241 for (
i = 0;
i < 16;
i++) {
243 if (pic) {
246 }
247 }
248 }
249 }
250
251 /**
252 * Extract structure information about the picture described by pic_num in
253 * the current decoding context (frame or field). Note that pic_num is
254 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
255 * @param pic_num picture number for which to extract structure information
256 * @param structure one of PICT_XXX describing structure of picture
257 * with pic_num
258 * @return frame number (short term) or long term index of picture
259 * described by pic_num
260 */
262 {
263 *structure =
h->picture_structure;
265 if (!(pic_num & 1))
266 /* opposite field */
268 pic_num >>= 1;
269 }
270
271 return pic_num;
272 }
273
275 {
281
283
284 for (j = 0; j < 3; j++)
285 field[0].linesize[j] <<= 1;
288
290
291 for (j = 0; j < 3; j++)
295 }
296 }
297 }
298
300 {
302
305
307
310
314 unsigned int pic_id;
317
318 switch (modification_of_pic_nums_idc) {
319 case 0:
320 case 1: {
321 const unsigned int abs_diff_pic_num =
val + 1;
322 int frame_num;
323
326 "abs_diff_pic_num overflow\n");
328 }
329
330 if (modification_of_pic_nums_idc == 0)
331 pred -= abs_diff_pic_num;
332 else
333 pred += abs_diff_pic_num;
335
337
338 for (
i =
h->short_ref_count - 1;
i >= 0;
i--) {
339 ref =
h->short_ref[
i];
340 assert(
ref->reference);
341 assert(!
ref->long_ref);
342 if (
ref->frame_num == frame_num &&
343 (
ref->reference & pic_structure))
344 break;
345 }
348 break;
349 }
350 case 2: {
351 int long_idx;
352 pic_id =
val;
// long_term_pic_idx
353
355
356 if (long_idx > 31
U) {
358 "long_term_pic_idx overflow\n");
360 }
361 ref =
h->long_ref[long_idx];
362 assert(!(
ref && !
ref->reference));
363 if (
ref && (
ref->reference & pic_structure)) {
364 ref->pic_id = pic_id;
365 assert(
ref->long_ref);
367 } else {
369 }
370 break;
371 }
372 default:
374 }
375
378 i < 0 ?
"reference picture missing during reorder\n" :
379 "mismatching reference\n"
380 );
382 } else {
387 break;
388 }
391 }
395 }
396 }
397 }
398 }
406 h->last_pocs[
i] = INT_MIN;
407 if (
h->default_ref[
list].parent
410 else
411 return -1;
412 }
414 }
415 }
416
419
420 return 0;
421 }
422
424 {
426
429
431 if (!
get_bits1(&sl->
gb))
// ref_pic_list_modification_flag_l[01]
432 continue;
433
436
438 break;
439
445 "illegal modification_of_pic_nums_idc %u\n",
448 }
452 }
453 }
454
455 return 0;
456 }
457
458 /**
459 * Mark a picture as no longer needed for reference. The refmask
460 * argument allows unreferencing of individual fields or the whole frame.
461 * If the picture becomes entirely unreferenced, but is being held for
462 * display purposes, it is marked as such.
463 * @param refmask mask of fields to unreference; the mask is bitwise
464 * anded with the reference marking of pic
465 * @return non-zero if pic becomes entirely unreferenced (except possibly
466 * for display purposes) zero if one of the fields remains in
467 * reference
468 */
470 {
473 return 0;
474 } else {
475 for(
i = 0;
h->delayed_pic[
i];
i++)
476 if(pic ==
h->delayed_pic[
i]){
478 break;
479 }
480 return 1;
481 }
482 }
483
484 /**
485 * Find a H264Picture in the short term reference list by frame number.
486 * @param frame_num frame number to search for
487 * @param idx the index into h->short_ref where returned picture is found
488 * undefined if no picture found.
489 * @return pointer to the found picture, or NULL if no pic with the provided
490 * frame number is found
491 */
493 {
495
496 for (
i = 0;
i <
h->short_ref_count;
i++) {
502 return pic;
503 }
504 }
506 }
507
508 /**
509 * Remove a picture from the short term reference list by its index in
510 * that list. This does no checking on the provided index; it is assumed
511 * to be valid. Other list entries are shifted down.
512 * @param i index into h->short_ref of picture to remove.
513 */
515 {
516 assert(
i >= 0 && i < h->short_ref_count);
518 if (--
h->short_ref_count)
519 memmove(&
h->short_ref[
i], &
h->short_ref[
i + 1],
521 }
522
523 /**
524 * @return the removed picture or NULL if an error occurs
525 */
527 {
530
533
535 if (pic) {
538 }
539
540 return pic;
541 }
542
543 /**
544 * Remove a picture from the long term reference list by its index in
545 * that list.
546 * @return the removed picture or NULL if an error occurs
547 */
549 {
551
552 pic =
h->long_ref[
i];
553 if (pic) {
555 assert(
h->long_ref[
i]->long_ref == 1);
556 h->long_ref[
i]->long_ref = 0;
559 }
560 }
561
562 return pic;
563 }
564
566 {
568
569 for (
i = 0;
i < 16;
i++) {
571 }
572 assert(
h->long_ref_count == 0);
573
574 if (
h->short_ref_count && !
h->last_pic_for_ec.f->data[0]) {
577 }
578
579 for (
i = 0;
i <
h->short_ref_count;
i++) {
582 }
583 h->short_ref_count = 0;
584
585 memset(
h->default_ref, 0,
sizeof(
h->default_ref));
586 }
587
589 {
590 MMCO *mmco =
h->mmco;
591 int nb_mmco = 0;
592
593 if (
h->short_ref_count &&
594 h->long_ref_count +
h->short_ref_count >=
h->ps.sps->ref_frame_count &&
597 mmco[0].
short_pic_num =
h->short_ref[
h->short_ref_count - 1]->frame_num;
598 nb_mmco = 1;
603 nb_mmco = 2;
604 }
605 }
606
607 h->nb_mmco = nb_mmco;
608 }
609
611 {
612 MMCO *mmco =
h->mmco;
613 int mmco_count;
615 int pps_ref_count[2] = {0};
616 int current_ref_assigned = 0, err = 0;
618
623 }
624
625 if (!
h->explicit_ref_marking)
627 mmco_count =
h->nb_mmco;
628
631
632 for (
i = 0;
i < mmco_count;
i++) {
636 h->mmco[
i].short_pic_num,
h->mmco[
i].long_arg);
637
642 if (!pic) {
645 h->long_ref[mmco[
i].
long_arg]->frame_num != frame_num) {
648 }
649 continue;
650 }
651 }
652
653 switch (mmco[
i].opcode) {
657 h->mmco[
i].short_pic_num,
h->short_ref_count);
659 break;
663
669 }
670 break;
673 pic =
h->long_ref[j];
674 if (pic) {
678 break;
680 // Comment below left from previous code as it is an interesting note.
681 /* First field in pair is in short term list or
682 * at a different long term index.
683 * This is not allowed; see 7.4.3.3, notes 2 and 3.
684 * Report the problem and keep the pair where it is,
685 * and mark this field valid.
686 */
687 if (
h->short_ref[0] ==
h->cur_pic_ptr) {
688 av_log(
h->avctx,
AV_LOG_ERROR,
"mmco: cannot assign current picture to short and long at the same time\n");
690 }
691
692 /* make sure the current picture is not already assigned as a long ref */
693 if (
h->cur_pic_ptr->long_ref) {
695 if (
h->long_ref[j] ==
h->cur_pic_ptr) {
696 if (j != mmco[
i].long_arg)
697 av_log(
h->avctx,
AV_LOG_ERROR,
"mmco: cannot assign current picture to 2 long term references\n");
699 }
700 }
701 }
702
703 if (
h->long_ref[mmco[
i].
long_arg] !=
h->cur_pic_ptr) {
706
710 }
711
712 h->cur_pic_ptr->reference |=
h->picture_structure;
713 current_ref_assigned = 1;
714 break;
716 assert(mmco[
i].long_arg <= 16);
717 // just remove the long term which index is greater than new max
718 for (j = mmco[
i].long_arg; j < 16; j++) {
720 }
721 break;
723 while (
h->short_ref_count) {
725 }
726 for (j = 0; j < 16; j++) {
728 }
729 h->poc.frame_num =
h->cur_pic_ptr->frame_num = 0;
731 h->cur_pic_ptr->mmco_reset = 1;
733 h->last_pocs[j] = INT_MIN;
734 break;
736 }
737 }
738
739 if (!current_ref_assigned) {
740 /* Second field of complementary field pair; the first field of
741 * which is already referenced. If short referenced, it
742 * should be first entry in short_ref. If not, it must exist
743 * in long_ref; trying to put it on the short list here is an
744 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
745 */
746 if (
h->short_ref_count &&
h->short_ref[0] ==
h->cur_pic_ptr) {
747 /* Just mark the second field valid */
748 h->cur_pic_ptr->reference |=
h->picture_structure;
749 }
else if (
h->cur_pic_ptr->long_ref) {
751 "assignment for second field "
752 "in complementary field pair "
753 "(first field is long term)\n");
755 } else {
757 if (pic) {
760 }
761
762 if (
h->short_ref_count)
763 memmove(&
h->short_ref[1], &
h->short_ref[0],
765
766 h->short_ref[0] =
h->cur_pic_ptr;
767 h->short_ref_count++;
768 h->cur_pic_ptr->reference |=
h->picture_structure;
769 }
770 }
771
772 if (
h->long_ref_count +
h->short_ref_count >
FFMAX(
h->ps.sps->ref_frame_count, 1)) {
773
774 /* We have too many reference frames, probably due to corrupted
775 * stream. Need to discard one frame. Prevents overrun of the
776 * short_ref and long_ref buffers.
777 */
779 "number of reference frames (%d+%d) exceeds max (%d; probably "
780 "corrupt input), discarding one\n",
781 h->long_ref_count,
h->short_ref_count,
h->ps.sps->ref_frame_count);
783
784 if (
h->long_ref_count && !
h->short_ref_count) {
785 for (
i = 0;
i < 16; ++
i)
787 break;
788
791 } else {
792 pic =
h->short_ref[
h->short_ref_count - 1];
794 }
795 }
796
797 for (
i = 0;
i<
h->short_ref_count;
i++) {
798 pic =
h->short_ref[
i];
799 if (pic->invalid_gap) {
800 int d =
av_mod_uintp2(
h->cur_pic_ptr->frame_num - pic->frame_num,
h->ps.sps->log2_max_frame_num);
801 if (
d >
h->ps.sps->ref_frame_count)
803 }
804 }
805
808
810 if (
h->ps.pps_list[
i]) {
811 const PPS *
pps = (
const PPS *)
h->ps.pps_list[
i]->data;
812 pps_ref_count[0] =
FFMAX(pps_ref_count[0],
pps->ref_count[0]);
813 pps_ref_count[1] =
FFMAX(pps_ref_count[1],
pps->ref_count[1]);
814 }
815 }
816
817 // Detect unmarked random access points
818 if ( err >= 0
819 &&
h->long_ref_count==0
820 && (
h->short_ref_count<=2
821 || pps_ref_count[0] <= 2 && pps_ref_count[1] <= 1 &&
h->avctx->has_b_frames
822 || pps_ref_count[0] <= 1 + (
h->picture_structure !=
PICT_FRAME) && pps_ref_count[1] <= 1)
823 && pps_ref_count[0]<=2 + (
h->picture_structure !=
PICT_FRAME) + (2*!
h->has_recovery_point)
825 h->cur_pic_ptr->recovered |= 1;
826 if(!
h->avctx->has_b_frames)
828 }
829
832 }
833
836 {
839 int nb_mmco = 0;
840
846 nb_mmco = 1;
847 }
849 } else {
854
860 }
864 if (long_arg >= 32 ||
866 long_arg == 16) &&
869 "illegal long ref in memory management control "
870 "operation %d\n", opcode);
872 return -1;
873 }
875 }
876
879 "illegal memory management control operation %d\n",
880 opcode);
882 return -1;
883 }
885 break;
886 }
888 }
889 }
890
892
893 return 0;
894 }