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