annotate h264_refs.c @ 11255:f65b3f7186b5 libavcodec

10l trocadero: Indeo 5 decoder did not free custom VLCs for macroblock and block decoding at exit, so prevent that memory leak now.
author kostya
date Tue, 23 Feb 2010 07:41:29 +0000
parents d9c084a0c22b
children a82cdda1f507
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10862
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
1 /*
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
2 * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
4 *
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
5 * This file is part of FFmpeg.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
6 *
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
11 *
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
15 * Lesser General Public License for more details.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
16 *
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
20 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
21
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
22 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
23 * @file libavcodec/h264_refs.c
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
24 * H.264 / AVC / MPEG4 part10 reference picture handling.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
25 * @author Michael Niedermayer <michaelni@gmx.at>
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
26 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
27
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
28 #include "internal.h"
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
29 #include "dsputil.h"
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
30 #include "avcodec.h"
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
31 #include "h264.h"
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
32 #include "golomb.h"
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
33
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
34 //#undef NDEBUG
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
35 #include <assert.h>
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
36
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
37
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
38 static void pic_as_field(Picture *pic, const int parity){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
39 int i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
40 for (i = 0; i < 4; ++i) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
41 if (parity == PICT_BOTTOM_FIELD)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
42 pic->data[i] += pic->linesize[i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
43 pic->reference = parity;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
44 pic->linesize[i] *= 2;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
45 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
46 pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
47 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
48
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
49 static int split_field_copy(Picture *dest, Picture *src,
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
50 int parity, int id_add){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
51 int match = !!(src->reference & parity);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
52
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
53 if (match) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
54 *dest = *src;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
55 if(parity != PICT_FRAME){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
56 pic_as_field(dest, parity);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
57 dest->pic_id *= 2;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
58 dest->pic_id += id_add;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
59 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
60 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
61
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
62 return match;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
63 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
64
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
65 static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
66 int i[2]={0};
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
67 int index=0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
68
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
69 while(i[0]<len || i[1]<len){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
70 while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
71 i[0]++;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
72 while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
73 i[1]++;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
74 if(i[0] < len){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
75 in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
76 split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
77 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
78 if(i[1] < len){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
79 in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
80 split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
81 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
82 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
83
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
84 return index;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
85 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
86
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
87 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
88 int i, best_poc;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
89 int out_i= 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
90
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
91 for(;;){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
92 best_poc= dir ? INT_MIN : INT_MAX;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
93
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
94 for(i=0; i<len; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
95 const int poc= src[i]->poc;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
96 if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
97 best_poc= poc;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
98 sorted[out_i]= src[i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
99 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
100 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
101 if(best_poc == (dir ? INT_MIN : INT_MAX))
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
102 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
103 limit= sorted[out_i++]->poc - dir;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
104 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
105 return out_i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
106 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
107
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
108 int ff_h264_fill_default_ref_list(H264Context *h){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
109 MpegEncContext * const s = &h->s;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
110 int i, len;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
111
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
112 if(h->slice_type_nos==FF_B_TYPE){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
113 Picture *sorted[32];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
114 int cur_poc, list;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
115 int lens[2];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
116
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
117 if(FIELD_PICTURE)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
118 cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
119 else
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
120 cur_poc= s->current_picture_ptr->poc;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
121
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
122 for(list= 0; list<2; list++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
123 len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
124 len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
125 assert(len<=32);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
126 len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
127 len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
128 assert(len<=32);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
129
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
130 if(len < h->ref_count[list])
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
131 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
132 lens[list]= len;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
133 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
134
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
135 if(lens[0] == lens[1] && lens[1] > 1){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
136 for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
137 if(i == lens[0])
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
138 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
139 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
140 }else{
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
141 len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
142 len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
143 assert(len <= 32);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
144 if(len < h->ref_count[0])
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
145 memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
146 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
147 #ifdef TRACE
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
148 for (i=0; i<h->ref_count[0]; i++) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
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]);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
150 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
151 if(h->slice_type_nos==FF_B_TYPE){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
152 for (i=0; i<h->ref_count[1]; i++) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
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]);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
154 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
155 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
156 #endif
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
157 return 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
158 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
159
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
160 static void print_short_term(H264Context *h);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
161 static void print_long_term(H264Context *h);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
162
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
163 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
164 * Extract structure information about the picture described by pic_num in
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
165 * the current decoding context (frame or field). Note that pic_num is
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
166 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
167 * @param pic_num picture number for which to extract structure information
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
168 * @param structure one of PICT_XXX describing structure of picture
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
169 * with pic_num
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
170 * @return frame number (short term) or long term index of picture
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
171 * described by pic_num
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
172 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
173 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
174 MpegEncContext * const s = &h->s;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
175
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
176 *structure = s->picture_structure;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
177 if(FIELD_PICTURE){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
178 if (!(pic_num & 1))
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
179 /* opposite field */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
180 *structure ^= PICT_FRAME;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
181 pic_num >>= 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
182 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
183
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
184 return pic_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
185 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
186
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
187 int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
188 MpegEncContext * const s = &h->s;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
189 int list, index, pic_structure;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
190
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
191 print_short_term(h);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
192 print_long_term(h);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
193
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
194 for(list=0; list<h->list_count; list++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
195 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
196
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
197 if(get_bits1(&s->gb)){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
198 int pred= h->curr_pic_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
199
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
200 for(index=0; ; index++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
201 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
202 unsigned int pic_id;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
203 int i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
204 Picture *ref = NULL;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
205
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
206 if(reordering_of_pic_nums_idc==3)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
207 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
208
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
209 if(index >= h->ref_count[list]){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
210 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
211 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
212 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
213
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
214 if(reordering_of_pic_nums_idc<3){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
215 if(reordering_of_pic_nums_idc<2){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
216 const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
217 int frame_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
218
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
219 if(abs_diff_pic_num > h->max_pic_num){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
220 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
221 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
222 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
223
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
224 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
225 else pred+= abs_diff_pic_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
226 pred &= h->max_pic_num - 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
227
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
228 frame_num = pic_num_extract(h, pred, &pic_structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
229
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
230 for(i= h->short_ref_count-1; i>=0; i--){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
231 ref = h->short_ref[i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
232 assert(ref->reference);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
233 assert(!ref->long_ref);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
234 if(
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
235 ref->frame_num == frame_num &&
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
236 (ref->reference & pic_structure)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
237 )
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
238 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
239 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
240 if(i>=0)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
241 ref->pic_id= pred;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
242 }else{
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
243 int long_idx;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
244 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
245
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
246 long_idx= pic_num_extract(h, pic_id, &pic_structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
247
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
248 if(long_idx>31){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
249 av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
250 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
251 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
252 ref = h->long_ref[long_idx];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
253 assert(!(ref && !ref->reference));
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
254 if(ref && (ref->reference & pic_structure)){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
255 ref->pic_id= pic_id;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
256 assert(ref->long_ref);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
257 i=0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
258 }else{
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
259 i=-1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
260 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
261 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
262
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
263 if (i < 0) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
264 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
265 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
266 } else {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
267 for(i=index; i+1<h->ref_count[list]; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
268 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
269 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
270 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
271 for(; i > index; i--){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
272 h->ref_list[list][i]= h->ref_list[list][i-1];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
273 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
274 h->ref_list[list][index]= *ref;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
275 if (FIELD_PICTURE){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
276 pic_as_field(&h->ref_list[list][index], pic_structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
277 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
278 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
279 }else{
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
280 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
281 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
282 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
283 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
284 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
285 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
286 for(list=0; list<h->list_count; list++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
287 for(index= 0; index < h->ref_count[list]; index++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
288 if(!h->ref_list[list][index].data[0]){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
289 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
290 if(h->default_ref_list[list][0].data[0])
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
291 h->ref_list[list][index]= h->default_ref_list[list][0];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
292 else
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
293 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
294 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
295 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
296 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
297
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
298 return 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
299 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
300
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
301 void ff_h264_fill_mbaff_ref_list(H264Context *h){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
302 int list, i, j;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
303 for(list=0; list<2; list++){ //FIXME try list_count
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
304 for(i=0; i<h->ref_count[list]; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
305 Picture *frame = &h->ref_list[list][i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
306 Picture *field = &h->ref_list[list][16+2*i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
307 field[0] = *frame;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
308 for(j=0; j<3; j++)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
309 field[0].linesize[j] <<= 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
310 field[0].reference = PICT_TOP_FIELD;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
311 field[0].poc= field[0].field_poc[0];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
312 field[1] = field[0];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
313 for(j=0; j<3; j++)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
314 field[1].data[j] += frame->linesize[j];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
315 field[1].reference = PICT_BOTTOM_FIELD;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
316 field[1].poc= field[1].field_poc[1];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
317
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
318 h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
319 h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
320 for(j=0; j<2; j++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
321 h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
322 h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
323 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
324 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
325 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
326 for(j=0; j<h->ref_count[1]; j++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
327 for(i=0; i<h->ref_count[0]; i++)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
328 h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
329 memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
330 memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
331 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
332 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
333
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
334 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
335 * Mark a picture as no longer needed for reference. The refmask
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
336 * argument allows unreferencing of individual fields or the whole frame.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
337 * If the picture becomes entirely unreferenced, but is being held for
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
338 * display purposes, it is marked as such.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
339 * @param refmask mask of fields to unreference; the mask is bitwise
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
340 * anded with the reference marking of pic
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
341 * @return non-zero if pic becomes entirely unreferenced (except possibly
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
342 * for display purposes) zero if one of the fields remains in
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
343 * reference
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
344 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
345 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
346 int i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
347 if (pic->reference &= refmask) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
348 return 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
349 } else {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
350 for(i = 0; h->delayed_pic[i]; i++)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
351 if(pic == h->delayed_pic[i]){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
352 pic->reference=DELAYED_PIC_REF;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
353 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
354 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
355 return 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
356 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
357 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
358
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
359 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
360 * Find a Picture in the short term reference list by frame number.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
361 * @param frame_num frame number to search for
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
362 * @param idx the index into h->short_ref where returned picture is found
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
363 * undefined if no picture found.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
364 * @return pointer to the found picture, or NULL if no pic with the provided
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
365 * frame number is found
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
366 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
367 static Picture * find_short(H264Context *h, int frame_num, int *idx){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
368 MpegEncContext * const s = &h->s;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
369 int i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
370
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
371 for(i=0; i<h->short_ref_count; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
372 Picture *pic= h->short_ref[i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
373 if(s->avctx->debug&FF_DEBUG_MMCO)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
374 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
375 if(pic->frame_num == frame_num) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
376 *idx = i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
377 return pic;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
378 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
379 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
380 return NULL;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
381 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
382
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
383 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
384 * Remove a picture from the short term reference list by its index in
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
385 * that list. This does no checking on the provided index; it is assumed
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
386 * to be valid. Other list entries are shifted down.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
387 * @param i index into h->short_ref of picture to remove.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
388 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
389 static void remove_short_at_index(H264Context *h, int i){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
390 assert(i >= 0 && i < h->short_ref_count);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
391 h->short_ref[i]= NULL;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
392 if (--h->short_ref_count)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
393 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
394 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
395
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
396 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
397 *
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
398 * @return the removed picture or NULL if an error occurs
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
399 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
400 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
401 MpegEncContext * const s = &h->s;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
402 Picture *pic;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
403 int i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
404
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
405 if(s->avctx->debug&FF_DEBUG_MMCO)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
406 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
407
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
408 pic = find_short(h, frame_num, &i);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
409 if (pic){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
410 if(unreference_pic(h, pic, ref_mask))
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
411 remove_short_at_index(h, i);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
412 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
413
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
414 return pic;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
415 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
416
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
417 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
418 * Remove a picture from the long term reference list by its index in
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
419 * that list.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
420 * @return the removed picture or NULL if an error occurs
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
421 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
422 static Picture * remove_long(H264Context *h, int i, int ref_mask){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
423 Picture *pic;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
424
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
425 pic= h->long_ref[i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
426 if (pic){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
427 if(unreference_pic(h, pic, ref_mask)){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
428 assert(h->long_ref[i]->long_ref == 1);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
429 h->long_ref[i]->long_ref= 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
430 h->long_ref[i]= NULL;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
431 h->long_ref_count--;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
432 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
433 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
434
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
435 return pic;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
436 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
437
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
438 void ff_h264_remove_all_refs(H264Context *h){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
439 int i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
440
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
441 for(i=0; i<16; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
442 remove_long(h, i, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
443 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
444 assert(h->long_ref_count==0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
445
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
446 for(i=0; i<h->short_ref_count; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
447 unreference_pic(h, h->short_ref[i], 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
448 h->short_ref[i]= NULL;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
449 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
450 h->short_ref_count=0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
451 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
452
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
453 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
454 * print short term list
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
455 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
456 static void print_short_term(H264Context *h) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
457 uint32_t i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
458 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
459 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
460 for(i=0; i<h->short_ref_count; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
461 Picture *pic= h->short_ref[i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
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]);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
463 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
464 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
465 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
466
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
467 /**
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
468 * print long term list
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
469 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
470 static void print_long_term(H264Context *h) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
471 uint32_t i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
472 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
473 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
474 for(i = 0; i < 16; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
475 Picture *pic= h->long_ref[i];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
476 if (pic) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
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]);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
478 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
479 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
480 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
481 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
482
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
483 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
484 MpegEncContext * const s = &h->s;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
485 int i, av_uninit(j);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
486 int current_ref_assigned=0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
487 Picture *av_uninit(pic);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
488
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
489 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
490 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
491
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
492 for(i=0; i<mmco_count; i++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
493 int av_uninit(structure), av_uninit(frame_num);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
494 if(s->avctx->debug&FF_DEBUG_MMCO)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
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);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
496
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
497 if( mmco[i].opcode == MMCO_SHORT2UNUSED
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
498 || mmco[i].opcode == MMCO_SHORT2LONG){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
499 frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
500 pic = find_short(h, frame_num, &j);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
501 if(!pic){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
502 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
503 || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
504 av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
505 continue;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
506 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
507 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
508
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
509 switch(mmco[i].opcode){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
510 case MMCO_SHORT2UNUSED:
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
511 if(s->avctx->debug&FF_DEBUG_MMCO)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
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);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
513 remove_short(h, frame_num, structure ^ PICT_FRAME);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
514 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
515 case MMCO_SHORT2LONG:
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
516 if (h->long_ref[mmco[i].long_arg] != pic)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
517 remove_long(h, mmco[i].long_arg, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
518
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
519 remove_short_at_index(h, j);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
520 h->long_ref[ mmco[i].long_arg ]= pic;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
521 if (h->long_ref[ mmco[i].long_arg ]){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
522 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
523 h->long_ref_count++;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
524 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
525 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
526 case MMCO_LONG2UNUSED:
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
527 j = pic_num_extract(h, mmco[i].long_arg, &structure);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
528 pic = h->long_ref[j];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
529 if (pic) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
530 remove_long(h, j, structure ^ PICT_FRAME);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
531 } else if(s->avctx->debug&FF_DEBUG_MMCO)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
532 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
533 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
534 case MMCO_LONG:
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
535 // Comment below left from previous code as it is an interresting note.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
536 /* First field in pair is in short term list or
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
537 * at a different long term index.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
538 * This is not allowed; see 7.4.3.3, notes 2 and 3.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
539 * Report the problem and keep the pair where it is,
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
540 * and mark this field valid.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
541 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
542
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
543 if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
544 remove_long(h, mmco[i].long_arg, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
545
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
546 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
547 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
548 h->long_ref_count++;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
549 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
550
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
551 s->current_picture_ptr->reference |= s->picture_structure;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
552 current_ref_assigned=1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
553 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
554 case MMCO_SET_MAX_LONG:
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
555 assert(mmco[i].long_arg <= 16);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
556 // just remove the long term which index is greater than new max
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
557 for(j = mmco[i].long_arg; j<16; j++){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
558 remove_long(h, j, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
559 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
560 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
561 case MMCO_RESET:
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
562 while(h->short_ref_count){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
563 remove_short(h, h->short_ref[0]->frame_num, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
564 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
565 for(j = 0; j < 16; j++) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
566 remove_long(h, j, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
567 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
568 s->current_picture_ptr->poc=
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
569 s->current_picture_ptr->field_poc[0]=
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
570 s->current_picture_ptr->field_poc[1]=
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
571 h->poc_lsb=
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
572 h->poc_msb=
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
573 h->frame_num=
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
574 s->current_picture_ptr->frame_num= 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
575 s->current_picture_ptr->mmco_reset=1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
576 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
577 default: assert(0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
578 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
579 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
580
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
581 if (!current_ref_assigned) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
582 /* Second field of complementary field pair; the first field of
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
583 * which is already referenced. If short referenced, it
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
584 * should be first entry in short_ref. If not, it must exist
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
585 * in long_ref; trying to put it on the short list here is an
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
586 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
587 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
588 if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
589 /* Just mark the second field valid */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
590 s->current_picture_ptr->reference = PICT_FRAME;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
591 } else if (s->current_picture_ptr->long_ref) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
592 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
593 "assignment for second field "
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
594 "in complementary field pair "
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
595 "(first field is long term)\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
596 } else {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
597 pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
598 if(pic){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
599 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
600 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
601
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
602 if(h->short_ref_count)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
603 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
604
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
605 h->short_ref[0]= s->current_picture_ptr;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
606 h->short_ref_count++;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
607 s->current_picture_ptr->reference |= s->picture_structure;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
608 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
609 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
610
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
611 if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
612
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
613 /* We have too many reference frames, probably due to corrupted
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
614 * stream. Need to discard one frame. Prevents overrun of the
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
615 * short_ref and long_ref buffers.
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
616 */
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
617 av_log(h->s.avctx, AV_LOG_ERROR,
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
618 "number of reference frames exceeds max (probably "
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
619 "corrupt input), discarding one\n");
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
620
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
621 if (h->long_ref_count && !h->short_ref_count) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
622 for (i = 0; i < 16; ++i)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
623 if (h->long_ref[i])
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
624 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
625
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
626 assert(i < 16);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
627 remove_long(h, i, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
628 } else {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
629 pic = h->short_ref[h->short_ref_count - 1];
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
630 remove_short(h, pic->frame_num, 0);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
631 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
632 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
633
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
634 print_short_term(h);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
635 print_long_term(h);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
636 return 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
637 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
638
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
639 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
640 MpegEncContext * const s = &h->s;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
641 int i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
642
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
643 h->mmco_index= 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
644 if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
645 s->broken_link= get_bits1(gb) -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
646 if(get_bits1(gb)){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
647 h->mmco[0].opcode= MMCO_LONG;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
648 h->mmco[0].long_arg= 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
649 h->mmco_index= 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
650 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
651 }else{
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
652 if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
653 for(i= 0; i<MAX_MMCO_COUNT; i++) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
654 MMCOOpcode opcode= get_ue_golomb_31(gb);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
655
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
656 h->mmco[i].opcode= opcode;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
657 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
658 h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
659 /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
660 av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
661 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
662 }*/
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
663 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
664 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
665 unsigned int long_arg= get_ue_golomb_31(gb);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
666 if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
667 av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
668 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
669 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
670 h->mmco[i].long_arg= long_arg;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
671 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
672
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
673 if(opcode > (unsigned)MMCO_LONG){
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
674 av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
675 return -1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
676 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
677 if(opcode == MMCO_END)
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
678 break;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
679 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
680 h->mmco_index= i;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
681 }else{
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
682 assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
683
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
684 if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
685 !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
686 h->mmco[0].opcode= MMCO_SHORT2UNUSED;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
687 h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
688 h->mmco_index= 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
689 if (FIELD_PICTURE) {
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
690 h->mmco[0].short_pic_num *= 2;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
691 h->mmco[1].opcode= MMCO_SHORT2UNUSED;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
692 h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
693 h->mmco_index= 2;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
694 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
695 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
696 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
697 }
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
698
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
699 return 0;
d9c084a0c22b Split all the reference picture handling off h264.c.
michael
parents:
diff changeset
700 }