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 <inttypes.h>
29
36
37 #include <assert.h>
38
39
41 int i;
42 for (i = 0; i < 4; ++i) {
47 }
49 }
50
52 {
54
55 if (match) {
61 }
62 }
63
64 return match;
65 }
66
69 {
70 int i[2] = { 0 };
72
73 while (i[0] < len || i[1] < len) {
74 while (i[0] < len && !(in[i[0]] && (in[i[0]]->reference & sel)))
75 i[0]++;
76 while (i[1] < len && !(in[i[1]] && (in[i[1]]->reference & (sel ^ 3))))
77 i[1]++;
78 if (i[0] < len) {
82 }
83 if (i[1] < len) {
87 }
88 }
89
91 }
92
94 {
95 int i, best_poc;
96 int out_i = 0;
97
98 for (;;) {
99 best_poc = dir ? INT_MIN : INT_MAX;
100
101 for (i = 0; i <
len; i++) {
102 const int poc = src[i]->
poc;
103 if (((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)) {
104 best_poc = poc;
105 sorted[out_i] = src[i];
106 }
107 }
108 if (best_poc == (dir ? INT_MIN : INT_MAX))
109 break;
110 limit = sorted[out_i++]->
poc - dir;
111 }
112 return out_i;
113 }
114
116 {
118
121 int cur_poc, list;
122 int lens[2];
123
126 else
128
129 for (list = 0; list < 2; list++) {
133
140
141 if (len < h->ref_count[list])
144 }
145
146 if (lens[0] == lens[1] && lens[1] > 1) {
147 for (i = 0; i < lens[0] &&
150 if (i == lens[0]) {
155 }
156 }
157 } else {
164
165 if (len < h->ref_count[0])
167 }
168 #ifdef TRACE
174 }
181 }
182 }
183 #endif
184 return 0;
185 }
186
189
190 /**
191 * Extract structure information about the picture described by pic_num in
192 * the current decoding context (frame or field). Note that pic_num is
193 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
194 * @param pic_num picture number for which to extract structure information
195 * @param structure one of PICT_XXX describing structure of picture
196 * with pic_num
197 * @return frame number (short term) or long term index of picture
198 * described by pic_num
199 */
201 {
204 if (!(pic_num & 1))
205 /* opposite field */
207 pic_num >>= 1;
208 }
209
210 return pic_num;
211 }
212
214 {
215 int list,
index, pic_structure, i;
216
219
220 for (list = 0; list < h->
list_count; list++) {
223
224 if (
get_bits1(&h->
gb)) {
// ref_pic_list_modification_flag_l[01]
226
227 for (index = 0; ; index++) {
229 unsigned int pic_id;
230 int i;
232
233 if (modification_of_pic_nums_idc == 3)
234 break;
235
238 return -1;
239 }
240
241 switch (modification_of_pic_nums_idc) {
242 case 0:
243 case 1: {
245 int frame_num;
246
249 "abs_diff_pic_num overflow\n");
251 }
252
253 if (modification_of_pic_nums_idc == 0)
254 pred -= abs_diff_pic_num;
255 else
256 pred += abs_diff_pic_num;
258
260
267 break;
268 }
269 if (i >= 0)
271 break;
272 }
273 case 2: {
274 int long_idx;
276
278
279 if (long_idx > 31) {
281 "long_term_pic_idx overflow\n");
283 }
286 if (ref && (ref->
reference & pic_structure)) {
289 i = 0;
290 } else {
291 i = -1;
292 }
293 break;
294 }
295 default:
297 "illegal modification_of_pic_nums_idc %u\n",
298 modification_of_pic_nums_idc);
300 }
301
302 if (i < 0) {
304 "reference picture missing during reorder\n");
306 } else {
307 for (i = index; i + 1 < h->
ref_count[list]; i++) {
310 break;
311 }
312 for (; i >
index; i--) {
314 }
318 }
319 }
320 }
321 }
322 }
323 for (list = 0; list < h->
list_count; list++) {
324 for (index = 0; index < h->
ref_count[list]; index++) {
327 int i;
334 else
335 return -1;
336 }
338 }
339 }
340
341 return 0;
342 }
343
345 {
346 int list, i, j;
347 for (list = 0; list < h->
list_count; list++) {
348 for (i = 0; i < h->
ref_count[list]; i++) {
352 for (j = 0; j < 3; j++)
353 field[0].f.linesize[j] <<= 1;
357 for (j = 0; j < 3; j++)
358 field[1].f.data[j] += frame->
f.
linesize[j];
361
364 for (j = 0; j < 2; j++) {
367 }
368 }
369 }
370 }
371
372 /**
373 * Mark a picture as no longer needed for reference. The refmask
374 * argument allows unreferencing of individual fields or the whole frame.
375 * If the picture becomes entirely unreferenced, but is being held for
376 * display purposes, it is marked as such.
377 * @param refmask mask of fields to unreference; the mask is bitwise
378 * anded with the reference marking of pic
379 * @return non-zero if pic becomes entirely unreferenced (except possibly
380 * for display purposes) zero if one of the fields remains in
381 * reference
382 */
384 {
385 int i;
387 return 0;
388 } else {
392 break;
393 }
394 return 1;
395 }
396 }
397
398 /**
399 * Find a H264Picture in the short term reference list by frame number.
400 * @param frame_num frame number to search for
401 * @param idx the index into h->short_ref where returned picture is found
402 * undefined if no picture found.
403 * @return pointer to the found picture, or NULL if no pic with the provided
404 * frame number is found
405 */
407 {
408 int i;
409
415 *idx = i;
416 return pic;
417 }
418 }
420 }
421
422 /**
423 * Remove a picture from the short term reference list by its index in
424 * that list. This does no checking on the provided index; it is assumed
425 * to be valid. Other list entries are shifted down.
426 * @param i index into h->short_ref of picture to remove.
427 */
429 {
430 assert(i >= 0 && i < h->short_ref_count);
435 }
436
437 /**
438 *
439 * @return the removed picture or NULL if an error occurs
440 */
442 {
444 int i;
445
448
450 if (pic) {
453 }
454
455 return pic;
456 }
457
458 /**
459 * Remove a picture from the long term reference list by its index in
460 * that list.
461 * @return the removed picture or NULL if an error occurs
462 */
464 {
466
468 if (pic) {
474 }
475 }
476
477 return pic;
478 }
479
481 {
482 int i;
483
484 for (i = 0; i < 16; i++) {
486 }
488
492 }
493
497 }
499
502 }
503
504 /**
505 * print short term list
506 */
508 {
509 uint32_t i;
516 }
517 }
518 }
519
520 /**
521 * print long term list
522 */
524 {
525 uint32_t i;
528 for (i = 0; i < 16; i++) {
530 if (pic) {
533 }
534 }
535 }
536 }
537
539 {
540 int i;
541
542 for (i = 0; i < n_mmcos; i++) {
543 if (mmco1[i].opcode != mmco2[i].opcode) {
545 mmco1[i].opcode, mmco2[i].opcode, i);
546 return -1;
547 }
548 }
549
550 return 0;
551 }
552
554 {
556 int mmco_index = 0, i = 0;
557
563 mmco_index = 1;
568 mmco_index = 2;
569 }
570 }
571
572 if (first_slice) {
574 } else if (!first_slice && mmco_index >= 0 &&
578 "Inconsistent MMCO state between slices [%d, %d]\n",
581 }
582 return 0;
583 }
584
586 {
588 int pps_count;
589 int current_ref_assigned = 0, err = 0;
591
594
595 for (i = 0; i < mmco_count; i++) {
600
605 if (!pic) {
611 }
612 continue;
613 }
614 }
615
616 switch (mmco[i].opcode) {
622 break;
626
632 }
633 break;
637 if (pic) {
641 break;
643 // Comment below left from previous code as it is an interresting note.
644 /* First field in pair is in short term list or
645 * at a different long term index.
646 * This is not allowed; see 7.4.3.3, notes 2 and 3.
647 * Report the problem and keep the pair where it is,
648 * and mark this field valid.
649 */
653 }
654
657 for(j=0; j<16; j++) {
661 }
662 }
663 }
666
670 }
671
673 current_ref_assigned = 1;
674 break;
676 assert(mmco[i].long_arg <= 16);
677 // just remove the long term which index is greater than new max
678 for (j = mmco[i].long_arg; j < 16; j++) {
680 }
681 break;
685 }
686 for (j = 0; j < 16; j++) {
688 }
694 break;
695 default: assert(0);
696 }
697 }
698
699 if (!current_ref_assigned) {
700 /* Second field of complementary field pair; the first field of
701 * which is already referenced. If short referenced, it
702 * should be first entry in short_ref. If not, it must exist
703 * in long_ref; trying to put it on the short list here is an
704 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
705 */
707 /* Just mark the second field valid */
711 "assignment for second field "
712 "in complementary field pair "
713 "(first field is long term)\n");
715 } else {
717 if (pic) {
720 }
721
725
729 }
730 }
731
733
734 /* We have too many reference frames, probably due to corrupted
735 * stream. Need to discard one frame. Prevents overrun of the
736 * short_ref and long_ref buffers.
737 */
739 "number of reference frames (%d+%d) exceeds max (%d; probably "
740 "corrupt input), discarding one\n",
743
745 for (i = 0; i < 16; ++i)
747 break;
748
749 assert(i < 16);
751 } else {
754 }
755 }
756
759 if (pic->invalid_gap) {
763 }
764 }
765
768
769 pps_count = 0;
772
781 }
782
784 }
785
787 int first_slice)
788 {
791 int mmco_index = 0;
792
798 mmco_index = 1;
799 }
800 } else {
801 if (
get_bits1(gb)) {
// adaptive_ref_pic_marking_mode_flag
804
810 #if 0
814 "illegal short ref in memory management control "
815 "operation %d\n", mmco);
816 return -1;
817 }
818 #endif
819 }
823 if (long_arg >= 32 ||
825 long_arg == 16) &&
828 "illegal long ref in memory management control "
829 "operation %d\n", opcode);
830 return -1;
831 }
833 }
834
837 "illegal memory management control operation %d\n",
838 opcode);
839 return -1;
840 }
842 break;
843 }
844 mmco_index = i;
845 } else {
846 if (first_slice) {
850 }
851 mmco_index = -1;
852 }
853 }
854
855 if (first_slice && mmco_index != -1) {
856 memcpy(h->
mmco, mmco_temp,
sizeof(h->
mmco));
858 } else if (!first_slice && mmco_index >= 0 &&
862 "Inconsistent MMCO state between slices [%d, %d]\n",
865 }
866
867 return 0;
868 }