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 {
42 int i;
48 }
50 }
51
53 {
60 }
61
63 {
65
66 if (match) {
72 }
73 }
74
75 return match;
76 }
77
80 {
81 int i[2] = { 0 };
83
84 while (i[0] < len || i[1] < len) {
85 while (i[0] < len && !(in[i[0]] && (in[i[0]]->reference & sel)))
86 i[0]++;
87 while (i[1] < len && !(in[i[1]] && (in[i[1]]->reference & (sel ^ 3))))
88 i[1]++;
89 if (i[0] < len) {
93 }
94 if (i[1] < len) {
98 }
99 }
100
102 }
103
105 int len,
int limit,
int dir)
106 {
107 int i, best_poc;
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;
122 limit = sorted[out_i++]->
poc - dir;
123 }
124 return out_i;
125 }
126
128 {
133 }
134
136 {
138 int j;
139
142 int cur_poc, list;
143 int lens[2];
144
147 else
149
150 for (list = 0; list < 2; list++) {
154
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] &&
171 if (i == lens[0]) {
173 }
174 }
175 } else {
182
183 if (len < sl->ref_count[0])
185 }
186 #ifdef TRACE
192 }
199 }
200 }
201 #endif
202
209 }
210 }
211 }
212 }
215 }
216
217 /**
218 * print short term list
219 */
221 {
222 uint32_t i;
229 }
230 }
231 }
232
233 /**
234 * print long term list
235 */
237 {
238 uint32_t i;
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 {
265 if (!(pic_num & 1))
266 /* opposite field */
268 pic_num >>= 1;
269 }
270
271 return pic_num;
272 }
273
275 {
276 int list, i, j;
277 for (list = 0; list < sl->
list_count; list++) {
278 for (i = 0; i < sl->
ref_count[list]; i++) {
281
283
284 for (j = 0; j < 3; j++)
285 field[0].linesize[j] <<= 1;
288
289 field[1] = field[0];
290
291 for (j = 0; j < 3; j++)
295 }
296 }
297 }
298
300 {
301 int list,
index, pic_structure;
302
305
307
308 for (list = 0; list < sl->
list_count; list++) {
310
314 unsigned int pic_id;
315 int i;
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
344 break;
345 }
346 if (i >= 0)
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 }
363 if (ref && (ref->
reference & pic_structure)) {
366 i = 0;
367 } else {
368 i = -1;
369 }
370 break;
371 }
372 default:
374 }
375
376 if (i < 0) {
378 "reference picture missing during reorder\n");
380 } else {
381 for (i = index; i + 1 < sl->
ref_count[list]; i++) {
385 break;
386 }
387 for (; i >
index; i--) {
389 }
393 }
394 }
395 }
396 }
397 for (list = 0; list < sl->
list_count; list++) {
398 for (index = 0; index < sl->
ref_count[list]; index++) {
401 int i;
408 else
409 return -1;
410 }
412 }
413 }
414
417
418 return 0;
419 }
420
422 {
424
427
428 for (list = 0; list < sl->
list_count; list++) {
429 if (!
get_bits1(&sl->
gb))
// ref_pic_list_modification_flag_l[01]
430 continue;
431
432 for (index = 0; ; index++) {
434
435 if (op == 3)
436 break;
437
441 } else if (op > 2) {
443 "illegal modification_of_pic_nums_idc %u\n",
444 op);
446 }
450 }
451 }
452
453 return 0;
454 }
455
456 /**
457 * Mark a picture as no longer needed for reference. The refmask
458 * argument allows unreferencing of individual fields or the whole frame.
459 * If the picture becomes entirely unreferenced, but is being held for
460 * display purposes, it is marked as such.
461 * @param refmask mask of fields to unreference; the mask is bitwise
462 * anded with the reference marking of pic
463 * @return non-zero if pic becomes entirely unreferenced (except possibly
464 * for display purposes) zero if one of the fields remains in
465 * reference
466 */
468 {
469 int i;
471 return 0;
472 } else {
476 break;
477 }
478 return 1;
479 }
480 }
481
482 /**
483 * Find a H264Picture in the short term reference list by frame number.
484 * @param frame_num frame number to search for
485 * @param idx the index into h->short_ref where returned picture is found
486 * undefined if no picture found.
487 * @return pointer to the found picture, or NULL if no pic with the provided
488 * frame number is found
489 */
491 {
492 int i;
493
499 *idx = i;
500 return pic;
501 }
502 }
504 }
505
506 /**
507 * Remove a picture from the short term reference list by its index in
508 * that list. This does no checking on the provided index; it is assumed
509 * to be valid. Other list entries are shifted down.
510 * @param i index into h->short_ref of picture to remove.
511 */
513 {
514 assert(i >= 0 && i < h->short_ref_count);
519 }
520
521 /**
522 * @return the removed picture or NULL if an error occurs
523 */
525 {
527 int i;
528
531
533 if (pic) {
536 }
537
538 return pic;
539 }
540
541 /**
542 * Remove a picture from the long term reference list by its index in
543 * that list.
544 * @return the removed picture or NULL if an error occurs
545 */
547 {
549
551 if (pic) {
557 }
558 }
559
560 return pic;
561 }
562
564 {
565 int i;
566
567 for (i = 0; i < 16; i++) {
569 }
571
575 }
576
580 }
582
584 }
585
587 {
589 int nb_mmco = 0;
590
596 nb_mmco = 1;
601 nb_mmco = 2;
602 }
603 }
604
606 }
607
609 {
611 int mmco_count;
613 int pps_ref_count[2] = {0};
614 int current_ref_assigned = 0, err = 0;
616
621 }
622
626
629
630 for (i = 0; i < mmco_count; i++) {
635
640 if (!pic) {
646 }
647 continue;
648 }
649 }
650
651 switch (mmco[i].opcode) {
657 break;
661
667 }
668 break;
672 if (pic) {
676 break;
678 // Comment below left from previous code as it is an interesting note.
679 /* First field in pair is in short term list or
680 * at a different long term index.
681 * This is not allowed; see 7.4.3.3, notes 2 and 3.
682 * Report the problem and keep the pair where it is,
683 * and mark this field valid.
684 */
688 }
689
690 /* make sure the current picture is not already assigned as a long ref */
694 if (j != mmco[i].long_arg)
697 }
698 }
699 }
700
704
708 }
709
711 current_ref_assigned = 1;
712 break;
714 assert(mmco[i].long_arg <= 16);
715 // just remove the long term which index is greater than new max
716 for (j = mmco[i].long_arg; j < 16; j++) {
718 }
719 break;
723 }
724 for (j = 0; j < 16; j++) {
726 }
732 break;
733 default: assert(0);
734 }
735 }
736
737 if (!current_ref_assigned) {
738 /* Second field of complementary field pair; the first field of
739 * which is already referenced. If short referenced, it
740 * should be first entry in short_ref. If not, it must exist
741 * in long_ref; trying to put it on the short list here is an
742 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
743 */
745 /* Just mark the second field valid */
749 "assignment for second field "
750 "in complementary field pair "
751 "(first field is long term)\n");
753 } else {
755 if (pic) {
758 }
759
763
767 }
768 }
769
771
772 /* We have too many reference frames, probably due to corrupted
773 * stream. Need to discard one frame. Prevents overrun of the
774 * short_ref and long_ref buffers.
775 */
777 "number of reference frames (%d+%d) exceeds max (%d; probably "
778 "corrupt input), discarding one\n",
781
783 for (i = 0; i < 16; ++i)
785 break;
786
787 assert(i < 16);
789 } else {
792 }
793 }
794
797 if (pic->invalid_gap) {
801 }
802 }
803
806
812 }
813 }
814
815 if ( err >= 0
824 }
825
828 }
829
832 {
833 int i;
835 int nb_mmco = 0;
836
842 nb_mmco = 1;
843 }
845 } else {
850
856 }
860 if (long_arg >= 32 ||
862 long_arg == 16) &&
865 "illegal long ref in memory management control "
866 "operation %d\n", opcode);
867 return -1;
868 }
870 }
871
874 "illegal memory management control operation %d\n",
875 opcode);
876 return -1;
877 }
879 break;
880 }
881 nb_mmco = i;
882 }
883 }
884
886
887 return 0;
888 }
static int split_field_copy(H264Ref *dest, H264Picture *src, int parity, int id_add)
static int add_sorted(H264Picture **sorted, H264Picture *const *src, int len, int limit, int dir)
Memory management control operation.
const char const char void * val
int long_ref
1->long term reference 0->short term reference
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static H264Picture * remove_short(H264Context *h, int frame_num, int ref_mask)
int ff_h264_build_ref_list(H264Context *h, H264SliceContext *sl)
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
MMCO mmco[MAX_MMCO_COUNT]
memory management control operations buffer.
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
H264Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
int nb_ref_modifications[2]
H264Picture * long_ref[32]
static int mismatches_ref(const H264Context *h, const H264Picture *pic)
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
MMCO mmco[MAX_MMCO_COUNT]
void ff_h264_remove_all_refs(H264Context *h)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int short_pic_num
pic_num without wrapping (pic_num & max_pic_num)
static void print_short_term(const H264Context *h)
print short term list
int frame_recovered
Initial frame has been completely recovered.
#define PICT_BOTTOM_FIELD
#define FRAME_RECOVERED_SEI
Sufficient number of frames have been decoded since a SEI recovery point, so all the following frames...
int recovered
picture at IDR or recovery point + recovery count
int ff_h264_decode_ref_pic_list_reordering(H264SliceContext *sl, void *logctx)
int last_pocs[MAX_DELAYED_PIC_COUNT]
H.264 common definitions.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int has_b_frames
Size of the frame reordering buffer in the decoder.
int ff_h264_decode_ref_pic_marking(H264SliceContext *sl, GetBitContext *gb, const H2645NAL *nal, void *logctx)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
simple assert() macros that are a bit more flexible than ISO C assert().
int frame_num
frame_num (raw frame_num from slice header)
MMCOOpcode
Memory management control operation opcode.
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
static int unreference_pic(H264Context *h, H264Picture *pic, int refmask)
Mark a picture as no longer needed for reference.
int ref_frame_count
num_ref_frames
enum AVPictureType pict_type
Picture type of the frame.
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
int long_ref_count
number of actual long term references
H.264 / AVC / MPEG-4 part10 codec.
#define AV_EF_EXPLODE
abort decoding on minor error detection
static void remove_short_at_index(H264Context *h, int i)
Remove a picture from the short term reference list by its index in that list.
static void ref_from_h264pic(H264Ref *dst, H264Picture *src)
#define FF_ARRAY_ELEMS(a)
static H264Picture * find_short(H264Context *h, int frame_num, int *idx)
Find a H264Picture in the short term reference list by frame number.
static const float pred[4]
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
static int pic_num_extract(const H264Context *h, int pic_num, int *structure)
Extract structure information about the picture described by pic_num in the current decoding context ...
Libavcodec external API header.
#define MAX_DELAYED_PIC_COUNT
AVBufferRef * pps_list[MAX_PPS_COUNT]
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
H264Picture * short_ref[32]
int field_poc[2]
top/bottom POC
uint8_t * data
The data buffer.
static void print_long_term(const H264Context *h)
print long term list
int av_buffer_get_ref_count(const AVBufferRef *buf)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
int mmco_reset
MMCO_RESET set this 1.
static H264Picture * remove_long(H264Context *h, int i, int ref_mask)
Remove a picture from the long term reference list by its index in that list.
H264Picture * cur_pic_ptr
struct H264SliceContext::@68 ref_modifications[2][32]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
int pic_id
pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num) ...
static void pic_as_field(H264Ref *pic, const int parity)
common internal api header.
static int ref[MAX_W *MAX_W]
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
static void h264_fill_mbaff_ref_list(H264SliceContext *sl)
int long_arg
index, pic_num, or num long refs depending on opcode
static void h264_initialise_ref_list(H264Context *h, H264SliceContext *sl)
static int build_def_list(H264Ref *def, int def_len, H264Picture *const *in, int len, int is_long, int sel)
int ff_h264_execute_ref_pic_marking(H264Context *h)
Execute the reference picture marking (memory management control operations).
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
H264Picture last_pic_for_ec
#define FFSWAP(type, a, b)
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
static void generate_sliding_window_mmcos(H264Context *h)
int short_ref_count
number of actual short term references