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