Skip to content
Newer
Older
100644 717 lines (631 sloc) 26.1 KB
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
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 /**
ba87f08 @DonDiego Remove explicit filename from Doxygen @file commands.
DonDiego authored Apr 20, 2010
23 * @file
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
24 * H.264 / AVC / MPEG4 part10 reference picture handling.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
7ac85f4 @michaelni H264:use av_assert0() for frame num check
michaelni authored May 1, 2011
28 #include "libavutil/avassert.h"
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
29 #include "internal.h"
30 #include "dsputil.h"
31 #include "avcodec.h"
32 #include "h264.h"
33 #include "golomb.h"
34
35 //#undef NDEBUG
36 #include <assert.h>
37
38
39 static void pic_as_field(Picture *pic, const int parity){
40 int i;
41 for (i = 0; i < 4; ++i) {
42 if (parity == PICT_BOTTOM_FIELD)
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
43 pic->f.data[i] += pic->f.linesize[i];
44 pic->f.reference = parity;
45 pic->f.linesize[i] *= 2;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
46 }
47 pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
48 }
49
50 static int split_field_copy(Picture *dest, Picture *src,
51 int parity, int id_add){
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
52 int match = !!(src->f.reference & parity);
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
53
54 if (match) {
55 *dest = *src;
56 if(parity != PICT_FRAME){
57 pic_as_field(dest, parity);
58 dest->pic_id *= 2;
59 dest->pic_id += id_add;
60 }
61 }
62
63 return match;
64 }
65
66 static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
67 int i[2]={0};
68 int index=0;
69
70 while(i[0]<len || i[1]<len){
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
71 while (i[0] < len && !(in[ i[0] ] && (in[ i[0] ]->f.reference & sel)))
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
72 i[0]++;
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
73 while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->f.reference & (sel^3))))
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
74 i[1]++;
75 if(i[0] < len){
76 in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
77 split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
78 }
79 if(i[1] < len){
80 in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
81 split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
82 }
83 }
84
85 return index;
86 }
87
88 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
89 int i, best_poc;
90 int out_i= 0;
91
92 for(;;){
93 best_poc= dir ? INT_MIN : INT_MAX;
94
95 for(i=0; i<len; i++){
96 const int poc= src[i]->poc;
97 if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
98 best_poc= poc;
99 sorted[out_i]= src[i];
100 }
101 }
102 if(best_poc == (dir ? INT_MIN : INT_MAX))
103 break;
104 limit= sorted[out_i++]->poc - dir;
105 }
106 return out_i;
107 }
108
109 int ff_h264_fill_default_ref_list(H264Context *h){
110 MpegEncContext * const s = &h->s;
111 int i, len;
112
ce5e49b replace deprecated FF_*_TYPE symbols with AV_PICTURE_TYPE_*
Stefano Sabatini authored Apr 28, 2011
113 if(h->slice_type_nos==AV_PICTURE_TYPE_B){
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
114 Picture *sorted[32];
115 int cur_poc, list;
116 int lens[2];
117
118 if(FIELD_PICTURE)
119 cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
120 else
121 cur_poc= s->current_picture_ptr->poc;
122
123 for(list= 0; list<2; list++){
124 len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
125 len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
126 assert(len<=32);
127 len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
128 len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
129 assert(len<=32);
130
131 if(len < h->ref_count[list])
132 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
133 lens[list]= len;
134 }
135
136 if(lens[0] == lens[1] && lens[1] > 1){
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
137 for (i = 0; h->default_ref_list[0][i].f.data[0] == h->default_ref_list[1][i].f.data[0] && i < lens[0]; i++);
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
138 if(i == lens[0])
139 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
140 }
141 }else{
142 len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
143 len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
144 assert(len <= 32);
145 if(len < h->ref_count[0])
146 memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
147 }
148 #ifdef TRACE
149 for (i=0; i<h->ref_count[0]; i++) {
150 tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
151 }
ce5e49b replace deprecated FF_*_TYPE symbols with AV_PICTURE_TYPE_*
Stefano Sabatini authored Apr 27, 2011
152 if(h->slice_type_nos==AV_PICTURE_TYPE_B){
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
153 for (i=0; i<h->ref_count[1]; i++) {
154 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]);
155 }
156 }
157 #endif
158 return 0;
159 }
160
161 static void print_short_term(H264Context *h);
162 static void print_long_term(H264Context *h);
163
164 /**
165 * Extract structure information about the picture described by pic_num in
166 * the current decoding context (frame or field). Note that pic_num is
167 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
168 * @param pic_num picture number for which to extract structure information
169 * @param structure one of PICT_XXX describing structure of picture
170 * with pic_num
171 * @return frame number (short term) or long term index of picture
172 * described by pic_num
173 */
174 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
175 MpegEncContext * const s = &h->s;
176
177 *structure = s->picture_structure;
178 if(FIELD_PICTURE){
179 if (!(pic_num & 1))
180 /* opposite field */
181 *structure ^= PICT_FRAME;
182 pic_num >>= 1;
183 }
184
185 return pic_num;
186 }
187
188 int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
189 MpegEncContext * const s = &h->s;
190 int list, index, pic_structure;
191
192 print_short_term(h);
193 print_long_term(h);
194
195 for(list=0; list<h->list_count; list++){
196 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
197
198 if(get_bits1(&s->gb)){
199 int pred= h->curr_pic_num;
200
201 for(index=0; ; index++){
202 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
203 unsigned int pic_id;
204 int i;
205 Picture *ref = NULL;
206
207 if(reordering_of_pic_nums_idc==3)
208 break;
209
210 if(index >= h->ref_count[list]){
211 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
212 return -1;
213 }
214
215 if(reordering_of_pic_nums_idc<3){
216 if(reordering_of_pic_nums_idc<2){
217 const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
218 int frame_num;
219
220 if(abs_diff_pic_num > h->max_pic_num){
221 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
222 return -1;
223 }
224
225 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
226 else pred+= abs_diff_pic_num;
227 pred &= h->max_pic_num - 1;
228
229 frame_num = pic_num_extract(h, pred, &pic_structure);
230
231 for(i= h->short_ref_count-1; i>=0; i--){
232 ref = h->short_ref[i];
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
233 assert(ref->f.reference);
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
234 assert(!ref->long_ref);
235 if(
236 ref->frame_num == frame_num &&
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
237 (ref->f.reference & pic_structure)
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
238 )
239 break;
240 }
241 if(i>=0)
242 ref->pic_id= pred;
243 }else{
244 int long_idx;
245 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
246
247 long_idx= pic_num_extract(h, pic_id, &pic_structure);
248
249 if(long_idx>31){
250 av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
251 return -1;
252 }
253 ref = h->long_ref[long_idx];
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
254 assert(!(ref && !ref->f.reference));
255 if (ref && (ref->f.reference & pic_structure)) {
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
256 ref->pic_id= pic_id;
257 assert(ref->long_ref);
258 i=0;
259 }else{
260 i=-1;
261 }
262 }
263
264 if (i < 0) {
265 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
266 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
267 } else {
268 for(i=index; i+1<h->ref_count[list]; i++){
269 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
270 break;
271 }
272 for(; i > index; i--){
273 h->ref_list[list][i]= h->ref_list[list][i-1];
274 }
275 h->ref_list[list][index]= *ref;
276 if (FIELD_PICTURE){
277 pic_as_field(&h->ref_list[list][index], pic_structure);
278 }
279 }
280 }else{
281 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
282 return -1;
283 }
284 }
285 }
286 }
287 for(list=0; list<h->list_count; list++){
288 for(index= 0; index < h->ref_count[list]; index++){
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
289 if (!h->ref_list[list][index].f.data[0]) {
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
290 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
291 if (h->default_ref_list[list][0].f.data[0])
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
292 h->ref_list[list][index]= h->default_ref_list[list][0];
293 else
294 return -1;
295 }
296 }
297 }
298
299 return 0;
300 }
301
302 void ff_h264_fill_mbaff_ref_list(H264Context *h){
303 int list, i, j;
237d31e @michaelni h264: fix FIXME and use list_count in ff_h264_fill_mbaff_ref_list()
michaelni authored Sep 28, 2011
304 for(list=0; list<h->list_count; list++){
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
305 for(i=0; i<h->ref_count[list]; i++){
306 Picture *frame = &h->ref_list[list][i];
307 Picture *field = &h->ref_list[list][16+2*i];
308 field[0] = *frame;
309 for(j=0; j<3; j++)
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
310 field[0].f.linesize[j] <<= 1;
311 field[0].f.reference = PICT_TOP_FIELD;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
312 field[0].poc= field[0].field_poc[0];
313 field[1] = field[0];
314 for(j=0; j<3; j++)
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
315 field[1].f.data[j] += frame->f.linesize[j];
316 field[1].f.reference = PICT_BOTTOM_FIELD;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
317 field[1].poc= field[1].field_poc[1];
318
3d9137c @michaelni Reorder indexes in weight tables.
michaelni authored Mar 3, 2010
319 h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
320 h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
321 for(j=0; j<2; j++){
3d9137c @michaelni Reorder indexes in weight tables.
michaelni authored Mar 3, 2010
322 h->chroma_weight[16+2*i][list][j][0] = h->chroma_weight[16+2*i+1][list][j][0] = h->chroma_weight[i][list][j][0];
323 h->chroma_weight[16+2*i][list][j][1] = h->chroma_weight[16+2*i+1][list][j][1] = h->chroma_weight[i][list][j][1];
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
324 }
325 }
326 }
327 }
328
329 /**
330 * Mark a picture as no longer needed for reference. The refmask
331 * argument allows unreferencing of individual fields or the whole frame.
332 * If the picture becomes entirely unreferenced, but is being held for
333 * display purposes, it is marked as such.
334 * @param refmask mask of fields to unreference; the mask is bitwise
335 * anded with the reference marking of pic
336 * @return non-zero if pic becomes entirely unreferenced (except possibly
337 * for display purposes) zero if one of the fields remains in
338 * reference
339 */
340 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
341 int i;
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
342 if (pic->f.reference &= refmask) {
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
343 return 0;
344 } else {
345 for(i = 0; h->delayed_pic[i]; i++)
346 if(pic == h->delayed_pic[i]){
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
347 pic->f.reference = DELAYED_PIC_REF;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
348 break;
349 }
350 return 1;
351 }
352 }
353
354 /**
355 * Find a Picture in the short term reference list by frame number.
356 * @param frame_num frame number to search for
357 * @param idx the index into h->short_ref where returned picture is found
358 * undefined if no picture found.
359 * @return pointer to the found picture, or NULL if no pic with the provided
360 * frame number is found
361 */
362 static Picture * find_short(H264Context *h, int frame_num, int *idx){
363 MpegEncContext * const s = &h->s;
364 int i;
365
366 for(i=0; i<h->short_ref_count; i++){
367 Picture *pic= h->short_ref[i];
368 if(s->avctx->debug&FF_DEBUG_MMCO)
369 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
370 if(pic->frame_num == frame_num) {
371 *idx = i;
372 return pic;
373 }
374 }
375 return NULL;
376 }
377
378 /**
379 * Remove a picture from the short term reference list by its index in
380 * that list. This does no checking on the provided index; it is assumed
381 * to be valid. Other list entries are shifted down.
382 * @param i index into h->short_ref of picture to remove.
383 */
384 static void remove_short_at_index(H264Context *h, int i){
385 assert(i >= 0 && i < h->short_ref_count);
386 h->short_ref[i]= NULL;
387 if (--h->short_ref_count)
388 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
389 }
390
391 /**
392 *
393 * @return the removed picture or NULL if an error occurs
394 */
395 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
396 MpegEncContext * const s = &h->s;
397 Picture *pic;
398 int i;
399
400 if(s->avctx->debug&FF_DEBUG_MMCO)
401 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
402
403 pic = find_short(h, frame_num, &i);
404 if (pic){
405 if(unreference_pic(h, pic, ref_mask))
406 remove_short_at_index(h, i);
407 }
408
409 return pic;
410 }
411
412 /**
413 * Remove a picture from the long term reference list by its index in
414 * that list.
415 * @return the removed picture or NULL if an error occurs
416 */
417 static Picture * remove_long(H264Context *h, int i, int ref_mask){
418 Picture *pic;
419
420 pic= h->long_ref[i];
421 if (pic){
422 if(unreference_pic(h, pic, ref_mask)){
423 assert(h->long_ref[i]->long_ref == 1);
424 h->long_ref[i]->long_ref= 0;
425 h->long_ref[i]= NULL;
426 h->long_ref_count--;
427 }
428 }
429
430 return pic;
431 }
432
433 void ff_h264_remove_all_refs(H264Context *h){
434 int i;
435
436 for(i=0; i<16; i++){
437 remove_long(h, i, 0);
438 }
439 assert(h->long_ref_count==0);
440
441 for(i=0; i<h->short_ref_count; i++){
442 unreference_pic(h, h->short_ref[i], 0);
443 h->short_ref[i]= NULL;
444 }
445 h->short_ref_count=0;
87b9868 @michaelni h264: clear the ref lists when the reference are cleared.
michaelni authored Jul 1, 2012
446
447 memset(h->default_ref_list, 0, sizeof(h->default_ref_list));
448 memset(h->ref_list, 0, sizeof(h->ref_list));
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
449 }
450
451 /**
452 * print short term list
453 */
454 static void print_short_term(H264Context *h) {
455 uint32_t i;
456 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
457 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
458 for(i=0; i<h->short_ref_count; i++){
459 Picture *pic= h->short_ref[i];
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
460 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
461 i, pic->frame_num, pic->poc, pic->f.data[0]);
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
462 }
463 }
464 }
465
466 /**
467 * print long term list
468 */
469 static void print_long_term(H264Context *h) {
470 uint32_t i;
471 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
472 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
473 for(i = 0; i < 16; i++){
474 Picture *pic= h->long_ref[i];
475 if (pic) {
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
476 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
477 i, pic->frame_num, pic->poc, pic->f.data[0]);
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
478 }
479 }
480 }
481 }
482
733f599 @michaelni Factorize ff_generate_sliding_window_mmcos() out.
michaelni authored Jul 5, 2010
483 void ff_generate_sliding_window_mmcos(H264Context *h) {
484 MpegEncContext * const s = &h->s;
485
3d54212 @michaelni Perform sliding window operation during frame gap handling.
michaelni authored Jul 5, 2010
486 h->mmco_index= 0;
f7da257 @michaelni h264: remove assert() on ref_frame_count.
michaelni authored Oct 5, 2011
487 if(h->short_ref_count && h->long_ref_count + h->short_ref_count >= h->sps.ref_frame_count &&
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
488 !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->f.reference)) {
733f599 @michaelni Factorize ff_generate_sliding_window_mmcos() out.
michaelni authored Jul 5, 2010
489 h->mmco[0].opcode= MMCO_SHORT2UNUSED;
490 h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
491 h->mmco_index= 1;
492 if (FIELD_PICTURE) {
493 h->mmco[0].short_pic_num *= 2;
494 h->mmco[1].opcode= MMCO_SHORT2UNUSED;
495 h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
496 h->mmco_index= 2;
497 }
498 }
499 }
500
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
501 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
502 MpegEncContext * const s = &h->s;
503 int i, av_uninit(j);
12fe759 h264: propagate error return values for AV_LOG_ERROR-triggering events
Dustin Brody authored Aug 4, 2011
504 int current_ref_assigned=0, err=0;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
505 Picture *av_uninit(pic);
506
507 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
508 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
509
510 for(i=0; i<mmco_count; i++){
511 int av_uninit(structure), av_uninit(frame_num);
512 if(s->avctx->debug&FF_DEBUG_MMCO)
513 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);
514
515 if( mmco[i].opcode == MMCO_SHORT2UNUSED
516 || mmco[i].opcode == MMCO_SHORT2LONG){
517 frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
518 pic = find_short(h, frame_num, &j);
519 if(!pic){
520 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
28ca701 @rbultje h264: add missing brackets.
rbultje authored Aug 12, 2011
521 || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
522 av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
523 err = AVERROR_INVALIDDATA;
524 }
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
525 continue;
526 }
527 }
528
529 switch(mmco[i].opcode){
530 case MMCO_SHORT2UNUSED:
531 if(s->avctx->debug&FF_DEBUG_MMCO)
532 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);
533 remove_short(h, frame_num, structure ^ PICT_FRAME);
534 break;
535 case MMCO_SHORT2LONG:
536 if (h->long_ref[mmco[i].long_arg] != pic)
537 remove_long(h, mmco[i].long_arg, 0);
538
539 remove_short_at_index(h, j);
540 h->long_ref[ mmco[i].long_arg ]= pic;
541 if (h->long_ref[ mmco[i].long_arg ]){
542 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
543 h->long_ref_count++;
544 }
545 break;
546 case MMCO_LONG2UNUSED:
547 j = pic_num_extract(h, mmco[i].long_arg, &structure);
548 pic = h->long_ref[j];
549 if (pic) {
550 remove_long(h, j, structure ^ PICT_FRAME);
551 } else if(s->avctx->debug&FF_DEBUG_MMCO)
552 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
553 break;
554 case MMCO_LONG:
555 // Comment below left from previous code as it is an interresting note.
556 /* First field in pair is in short term list or
557 * at a different long term index.
558 * This is not allowed; see 7.4.3.3, notes 2 and 3.
559 * Report the problem and keep the pair where it is,
560 * and mark this field valid.
561 */
562
563 if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
564 remove_long(h, mmco[i].long_arg, 0);
565
566 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
567 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
568 h->long_ref_count++;
569 }
570
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
571 s->current_picture_ptr->f.reference |= s->picture_structure;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
572 current_ref_assigned=1;
573 break;
574 case MMCO_SET_MAX_LONG:
575 assert(mmco[i].long_arg <= 16);
576 // just remove the long term which index is greater than new max
577 for(j = mmco[i].long_arg; j<16; j++){
578 remove_long(h, j, 0);
579 }
580 break;
581 case MMCO_RESET:
582 while(h->short_ref_count){
583 remove_short(h, h->short_ref[0]->frame_num, 0);
584 }
585 for(j = 0; j < 16; j++) {
586 remove_long(h, j, 0);
587 }
588 h->frame_num=
589 s->current_picture_ptr->frame_num= 0;
adedd84 @rbultje h264: fix frame reordering code.
rbultje authored Nov 29, 2011
590 h->mmco_reset = 1;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
591 s->current_picture_ptr->mmco_reset=1;
5b2873c @michaelni h264: switch from qatars has_b_frames calculation back to mine.
michaelni authored Dec 10, 2011
592 for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
593 h->last_pocs[j] = INT_MIN;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
594 break;
595 default: assert(0);
596 }
597 }
598
599 if (!current_ref_assigned) {
600 /* Second field of complementary field pair; the first field of
601 * which is already referenced. If short referenced, it
602 * should be first entry in short_ref. If not, it must exist
603 * in long_ref; trying to put it on the short list here is an
604 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
605 */
606 if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
607 /* Just mark the second field valid */
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
608 s->current_picture_ptr->f.reference = PICT_FRAME;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
609 } else if (s->current_picture_ptr->long_ref) {
610 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
611 "assignment for second field "
612 "in complementary field pair "
613 "(first field is long term)\n");
12fe759 h264: propagate error return values for AV_LOG_ERROR-triggering events
Dustin Brody authored Aug 4, 2011
614 err = AVERROR_INVALIDDATA;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
615 } else {
616 pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
617 if(pic){
618 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
12fe759 h264: propagate error return values for AV_LOG_ERROR-triggering events
Dustin Brody authored Aug 4, 2011
619 err = AVERROR_INVALIDDATA;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
620 }
621
622 if(h->short_ref_count)
623 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
624
625 h->short_ref[0]= s->current_picture_ptr;
626 h->short_ref_count++;
657ccb5 @DonDiego Eliminate FF_COMMON_FRAME macro.
DonDiego authored Jul 6, 2011
627 s->current_picture_ptr->f.reference |= s->picture_structure;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
628 }
629 }
630
c059d7a @michaelni H264: Fix intra only decoding.
michaelni authored May 1, 2011
631 if (h->long_ref_count + h->short_ref_count > FFMAX(h->sps.ref_frame_count, 1)){
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
632
633 /* We have too many reference frames, probably due to corrupted
634 * stream. Need to discard one frame. Prevents overrun of the
635 * short_ref and long_ref buffers.
636 */
637 av_log(h->s.avctx, AV_LOG_ERROR,
e86fbe1 @rbultje h264: do not print "too many references" warning for intra-only.
rbultje authored May 6, 2011
638 "number of reference frames (%d+%d) exceeds max (%d; probably "
639 "corrupt input), discarding one\n",
640 h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
12fe759 h264: propagate error return values for AV_LOG_ERROR-triggering events
Dustin Brody authored Aug 4, 2011
641 err = AVERROR_INVALIDDATA;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
642
643 if (h->long_ref_count && !h->short_ref_count) {
644 for (i = 0; i < 16; ++i)
645 if (h->long_ref[i])
646 break;
647
648 assert(i < 16);
649 remove_long(h, i, 0);
650 } else {
651 pic = h->short_ref[h->short_ref_count - 1];
652 remove_short(h, pic->frame_num, 0);
653 }
654 }
655
656 print_short_term(h);
657 print_long_term(h);
c881df3 @michaelni h264: Guess receovery points.
michaelni authored Oct 26, 2011
658
9d9316a @michaelni h264: Fix seeking in 00017.MTS
michaelni authored Dec 31, 2011
659 if(err >= 0 && h->long_ref_count==0 && h->short_ref_count<=2 && h->pps.ref_count[0]<=1 + (s->picture_structure != PICT_FRAME) && s->current_picture_ptr->f.pict_type == AV_PICTURE_TYPE_I){
9ba43e5 @michaelni h264: Fix frame sync / random access handling.
michaelni authored Dec 31, 2011
660 s->current_picture_ptr->sync |= 1;
3360b85 @michaelni h264: fix seeking in low delay streams without IDR
michaelni authored Apr 4, 2012
661 if(!h->s.avctx->has_b_frames)
662 h->sync = 2;
c881df3 @michaelni h264: Guess receovery points.
michaelni authored Oct 26, 2011
663 }
664
9abc987 lavc: replace references to deprecated AVCodecContext.error_recogniti…
Dustin Brody authored Oct 22, 2011
665 return (h->s.avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
666 }
667
668 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
669 MpegEncContext * const s = &h->s;
670 int i;
671
672 h->mmco_index= 0;
673 if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
674 s->broken_link= get_bits1(gb) -1;
675 if(get_bits1(gb)){
676 h->mmco[0].opcode= MMCO_LONG;
677 h->mmco[0].long_arg= 0;
678 h->mmco_index= 1;
679 }
680 }else{
681 if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
682 for(i= 0; i<MAX_MMCO_COUNT; i++) {
683 MMCOOpcode opcode= get_ue_golomb_31(gb);
684
685 h->mmco[i].opcode= opcode;
686 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
687 h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
688 /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
689 av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
690 return -1;
691 }*/
692 }
693 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
694 unsigned int long_arg= get_ue_golomb_31(gb);
e80e2d5 h264 - correct the check for invalid long term frame index in MMCO de…
Jeff Downs authored Jul 5, 2011
695 if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG && long_arg == 16) && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
696 av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
697 return -1;
698 }
699 h->mmco[i].long_arg= long_arg;
700 }
701
702 if(opcode > (unsigned)MMCO_LONG){
703 av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
704 return -1;
705 }
706 if(opcode == MMCO_END)
707 break;
708 }
709 h->mmco_index= i;
710 }else{
733f599 @michaelni Factorize ff_generate_sliding_window_mmcos() out.
michaelni authored Jul 5, 2010
711 ff_generate_sliding_window_mmcos(h);
ea6f00c @michaelni Split all the reference picture handling off h264.c.
michaelni authored Jan 12, 2010
712 }
713 }
714
715 return 0;
716 }
Something went wrong with that request. Please try again.