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