Libav
h264_refs.c
Go to the documentation of this file.
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include <inttypes.h>
29 
30 #include "internal.h"
31 #include "avcodec.h"
32 #include "h264.h"
33 #include "h264dec.h"
34 #include "golomb.h"
35 #include "mpegutils.h"
36 
37 #include <assert.h>
38 
39 static void pic_as_field(H264Ref *pic, const int parity)
40 {
41  int i;
42  for (i = 0; i < FF_ARRAY_ELEMS(pic->data); ++i) {
43  if (parity == PICT_BOTTOM_FIELD)
44  pic->data[i] += pic->linesize[i];
45  pic->reference = parity;
46  pic->linesize[i] *= 2;
47  }
48  pic->poc = pic->parent->field_poc[parity == PICT_BOTTOM_FIELD];
49 }
50 
52 {
53  memcpy(dst->data, src->f->data, sizeof(dst->data));
54  memcpy(dst->linesize, src->f->linesize, sizeof(dst->linesize));
55  dst->reference = src->reference;
56  dst->poc = src->poc;
57  dst->pic_id = src->pic_id;
58  dst->parent = src;
59 }
60 
61 static int split_field_copy(H264Ref *dest, H264Picture *src, int parity, int id_add)
62 {
63  int match = !!(src->reference & parity);
64 
65  if (match) {
66  ref_from_h264pic(dest, src);
67  if (parity != PICT_FRAME) {
68  pic_as_field(dest, parity);
69  dest->pic_id *= 2;
70  dest->pic_id += id_add;
71  }
72  }
73 
74  return match;
75 }
76 
77 static int build_def_list(H264Ref *def, int def_len,
78  H264Picture * const *in, int len, int is_long, int sel)
79 {
80  int i[2] = { 0 };
81  int index = 0;
82 
83  while ((i[0] < len || i[1] < len) && index < def_len) {
84  while (i[0] < len && !(in[i[0]] && (in[i[0]]->reference & sel)))
85  i[0]++;
86  while (i[1] < len && !(in[i[1]] && (in[i[1]]->reference & (sel ^ 3))))
87  i[1]++;
88  if (i[0] < len && index < def_len) {
89  in[i[0]]->pic_id = is_long ? i[0] : in[i[0]]->frame_num;
90  split_field_copy(&def[index++], in[i[0]++], sel, 1);
91  }
92  if (i[1] < len && index < def_len) {
93  in[i[1]]->pic_id = is_long ? i[1] : in[i[1]]->frame_num;
94  split_field_copy(&def[index++], in[i[1]++], sel ^ 3, 0);
95  }
96  }
97 
98  return index;
99 }
100 
101 static int add_sorted(H264Picture **sorted, H264Picture * const *src,
102  int len, int limit, int dir)
103 {
104  int i, best_poc;
105  int out_i = 0;
106 
107  for (;;) {
108  best_poc = dir ? INT_MIN : INT_MAX;
109 
110  for (i = 0; i < len; i++) {
111  const int poc = src[i]->poc;
112  if (((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)) {
113  best_poc = poc;
114  sorted[out_i] = src[i];
115  }
116  }
117  if (best_poc == (dir ? INT_MIN : INT_MAX))
118  break;
119  limit = sorted[out_i++]->poc - dir;
120  }
121  return out_i;
122 }
123 
125 {
126  int i, len;
127 
128  if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
129  H264Picture *sorted[32];
130  int cur_poc, list;
131  int lens[2];
132 
133  if (FIELD_PICTURE(h))
135  else
136  cur_poc = h->cur_pic_ptr->poc;
137 
138  for (list = 0; list < 2; list++) {
139  len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);
140  len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);
141  assert(len <= 32);
142 
143  len = build_def_list(sl->ref_list[list], FF_ARRAY_ELEMS(sl->ref_list[0]),
144  sorted, len, 0, h->picture_structure);
145  len += build_def_list(sl->ref_list[list] + len,
146  FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
147  h->long_ref, 16, 1, h->picture_structure);
148 
149  if (len < sl->ref_count[list])
150  memset(&sl->ref_list[list][len], 0, sizeof(H264Ref) * (sl->ref_count[list] - len));
151  lens[list] = len;
152  }
153 
154  if (lens[0] == lens[1] && lens[1] > 1) {
155  for (i = 0; i < lens[0] &&
156  sl->ref_list[0][i].parent->f->buf[0]->buffer ==
157  sl->ref_list[1][i].parent->f->buf[0]->buffer; i++);
158  if (i == lens[0]) {
159  FFSWAP(H264Ref, sl->ref_list[1][0], sl->ref_list[1][1]);
160  }
161  }
162  } else {
163  len = build_def_list(sl->ref_list[0], FF_ARRAY_ELEMS(sl->ref_list[0]),
165  len += build_def_list(sl->ref_list[0] + len,
166  FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
167  h-> long_ref, 16, 1, h->picture_structure);
168 
169  if (len < sl->ref_count[0])
170  memset(&sl->ref_list[0][len], 0, sizeof(H264Ref) * (sl->ref_count[0] - len));
171  }
172 }
173 
177 static void print_short_term(const H264Context *h)
178 {
179  uint32_t i;
180  if (h->avctx->debug & FF_DEBUG_MMCO) {
181  av_log(h->avctx, AV_LOG_DEBUG, "short term list:\n");
182  for (i = 0; i < h->short_ref_count; i++) {
183  H264Picture *pic = h->short_ref[i];
184  av_log(h->avctx, AV_LOG_DEBUG, "%"PRIu32" fn:%d poc:%d %p\n",
185  i, pic->frame_num, pic->poc, pic->f->data[0]);
186  }
187  }
188 }
189 
193 static void print_long_term(const H264Context *h)
194 {
195  uint32_t i;
196  if (h->avctx->debug & FF_DEBUG_MMCO) {
197  av_log(h->avctx, AV_LOG_DEBUG, "long term list:\n");
198  for (i = 0; i < 16; i++) {
199  H264Picture *pic = h->long_ref[i];
200  if (pic) {
201  av_log(h->avctx, AV_LOG_DEBUG, "%"PRIu32" fn:%d poc:%d %p\n",
202  i, pic->frame_num, pic->poc, pic->f->data[0]);
203  }
204  }
205  }
206 }
207 
218 static int pic_num_extract(const H264Context *h, int pic_num, int *structure)
219 {
220  *structure = h->picture_structure;
221  if (FIELD_PICTURE(h)) {
222  if (!(pic_num & 1))
223  /* opposite field */
224  *structure ^= PICT_FRAME;
225  pic_num >>= 1;
226  }
227 
228  return pic_num;
229 }
230 
232 {
233  int list, i, j;
234  for (list = 0; list < sl->list_count; list++) {
235  for (i = 0; i < sl->ref_count[list]; i++) {
236  H264Ref *frame = &sl->ref_list[list][i];
237  H264Ref *field = &sl->ref_list[list][16 + 2 * i];
238 
239  field[0] = *frame;
240 
241  for (j = 0; j < 3; j++)
242  field[0].linesize[j] <<= 1;
243  field[0].reference = PICT_TOP_FIELD;
244  field[0].poc = field[0].parent->field_poc[0];
245 
246  field[1] = field[0];
247 
248  for (j = 0; j < 3; j++)
249  field[1].data[j] += frame->parent->f->linesize[j];
250  field[1].reference = PICT_BOTTOM_FIELD;
251  field[1].poc = field[1].parent->field_poc[1];
252  }
253  }
254 }
255 
257 {
258  int list, index, pic_structure;
259 
260  print_short_term(h);
261  print_long_term(h);
262 
264 
265  for (list = 0; list < sl->list_count; list++) {
266  int pred = sl->curr_pic_num;
267 
268  for (index = 0; index < sl->nb_ref_modifications[list]; index++) {
269  unsigned int modification_of_pic_nums_idc = sl->ref_modifications[list][index].op;
270  unsigned int val = sl->ref_modifications[list][index].val;
271  unsigned int pic_id;
272  int i;
273  H264Picture *ref = NULL;
274 
275  switch (modification_of_pic_nums_idc) {
276  case 0:
277  case 1: {
278  const unsigned int abs_diff_pic_num = val + 1;
279  int frame_num;
280 
281  if (abs_diff_pic_num > sl->max_pic_num) {
283  "abs_diff_pic_num overflow\n");
284  return AVERROR_INVALIDDATA;
285  }
286 
287  if (modification_of_pic_nums_idc == 0)
288  pred -= abs_diff_pic_num;
289  else
290  pred += abs_diff_pic_num;
291  pred &= sl->max_pic_num - 1;
292 
293  frame_num = pic_num_extract(h, pred, &pic_structure);
294 
295  for (i = h->short_ref_count - 1; i >= 0; i--) {
296  ref = h->short_ref[i];
297  assert(ref->reference);
298  assert(!ref->long_ref);
299  if (ref->frame_num == frame_num &&
300  (ref->reference & pic_structure))
301  break;
302  }
303  if (i >= 0)
304  ref->pic_id = pred;
305  break;
306  }
307  case 2: {
308  int long_idx;
309  pic_id = val; // long_term_pic_idx
310 
311  long_idx = pic_num_extract(h, pic_id, &pic_structure);
312 
313  if (long_idx > 31) {
315  "long_term_pic_idx overflow\n");
316  return AVERROR_INVALIDDATA;
317  }
318  ref = h->long_ref[long_idx];
319  assert(!(ref && !ref->reference));
320  if (ref && (ref->reference & pic_structure)) {
321  ref->pic_id = pic_id;
322  assert(ref->long_ref);
323  i = 0;
324  } else {
325  i = -1;
326  }
327  break;
328  }
329  }
330 
331  if (i < 0) {
333  "reference picture missing during reorder\n");
334  memset(&sl->ref_list[list][index], 0, sizeof(sl->ref_list[0][0])); // FIXME
335  } else {
336  for (i = index; i + 1 < sl->ref_count[list]; i++) {
337  if (sl->ref_list[list][i].parent &&
338  ref->long_ref == sl->ref_list[list][i].parent->long_ref &&
339  ref->pic_id == sl->ref_list[list][i].pic_id)
340  break;
341  }
342  for (; i > index; i--) {
343  sl->ref_list[list][i] = sl->ref_list[list][i - 1];
344  }
345  ref_from_h264pic(&sl->ref_list[list][index], ref);
346  if (FIELD_PICTURE(h)) {
347  pic_as_field(&sl->ref_list[list][index], pic_structure);
348  }
349  }
350  }
351  }
352  for (list = 0; list < sl->list_count; list++) {
353  for (index = 0; index < sl->ref_count[list]; index++) {
354  if (!sl->ref_list[list][index].parent) {
355  av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture\n");
356  if (index == 0 || h->avctx->err_recognition & AV_EF_EXPLODE)
357  return AVERROR_INVALIDDATA;
358  else
359  sl->ref_list[list][index] = sl->ref_list[list][index - 1];
360  }
361  }
362  }
363 
364  if (FRAME_MBAFF(h))
366 
367  return 0;
368 }
369 
371 {
372  int list, index;
373 
374  sl->nb_ref_modifications[0] = 0;
375  sl->nb_ref_modifications[1] = 0;
376 
377  for (list = 0; list < sl->list_count; list++) {
378  if (!get_bits1(&sl->gb)) // ref_pic_list_modification_flag_l[01]
379  continue;
380 
381  for (index = 0; ; index++) {
382  unsigned int op = get_ue_golomb_31(&sl->gb);
383 
384  if (op == 3)
385  break;
386 
387  if (index >= sl->ref_count[list]) {
388  av_log(logctx, AV_LOG_ERROR, "reference count overflow\n");
389  return AVERROR_INVALIDDATA;
390  } else if (op > 2) {
391  av_log(logctx, AV_LOG_ERROR,
392  "illegal modification_of_pic_nums_idc %u\n",
393  op);
394  return AVERROR_INVALIDDATA;
395  }
396  sl->ref_modifications[list][index].val = get_ue_golomb(&sl->gb);
397  sl->ref_modifications[list][index].op = op;
398  sl->nb_ref_modifications[list]++;
399  }
400  }
401 
402  return 0;
403 }
404 
416 static inline int unreference_pic(H264Context *h, H264Picture *pic, int refmask)
417 {
418  int i;
419  if (pic->reference &= refmask) {
420  return 0;
421  } else {
422  for(i = 0; h->delayed_pic[i]; i++)
423  if(pic == h->delayed_pic[i]){
424  pic->reference = DELAYED_PIC_REF;
425  break;
426  }
427  return 1;
428  }
429 }
430 
439 static H264Picture *find_short(H264Context *h, int frame_num, int *idx)
440 {
441  int i;
442 
443  for (i = 0; i < h->short_ref_count; i++) {
444  H264Picture *pic = h->short_ref[i];
445  if (h->avctx->debug & FF_DEBUG_MMCO)
446  av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
447  if (pic->frame_num == frame_num) {
448  *idx = i;
449  return pic;
450  }
451  }
452  return NULL;
453 }
454 
461 static void remove_short_at_index(H264Context *h, int i)
462 {
463  assert(i >= 0 && i < h->short_ref_count);
464  h->short_ref[i] = NULL;
465  if (--h->short_ref_count)
466  memmove(&h->short_ref[i], &h->short_ref[i + 1],
467  (h->short_ref_count - i) * sizeof(H264Picture*));
468 }
469 
473 static H264Picture *remove_short(H264Context *h, int frame_num, int ref_mask)
474 {
475  H264Picture *pic;
476  int i;
477 
478  if (h->avctx->debug & FF_DEBUG_MMCO)
479  av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
480 
481  pic = find_short(h, frame_num, &i);
482  if (pic) {
483  if (unreference_pic(h, pic, ref_mask))
484  remove_short_at_index(h, i);
485  }
486 
487  return pic;
488 }
489 
495 static H264Picture *remove_long(H264Context *h, int i, int ref_mask)
496 {
497  H264Picture *pic;
498 
499  pic = h->long_ref[i];
500  if (pic) {
501  if (unreference_pic(h, pic, ref_mask)) {
502  assert(h->long_ref[i]->long_ref == 1);
503  h->long_ref[i]->long_ref = 0;
504  h->long_ref[i] = NULL;
505  h->long_ref_count--;
506  }
507  }
508 
509  return pic;
510 }
511 
513 {
514  int i;
515 
516  for (i = 0; i < 16; i++) {
517  remove_long(h, i, 0);
518  }
519  assert(h->long_ref_count == 0);
520 
521  for (i = 0; i < h->short_ref_count; i++) {
522  unreference_pic(h, h->short_ref[i], 0);
523  h->short_ref[i] = NULL;
524  }
525  h->short_ref_count = 0;
526 }
527 
529 {
530  MMCO *mmco = h->mmco;
531  int nb_mmco = 0;
532 
533  assert(h->long_ref_count + h->short_ref_count <= h->ps.sps->ref_frame_count);
534 
535  if (h->short_ref_count &&
537  !(FIELD_PICTURE(h) && !h->first_field && h->cur_pic_ptr->reference)) {
538  mmco[0].opcode = MMCO_SHORT2UNUSED;
539  mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
540  nb_mmco = 1;
541  if (FIELD_PICTURE(h)) {
542  mmco[0].short_pic_num *= 2;
543  mmco[1].opcode = MMCO_SHORT2UNUSED;
544  mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
545  nb_mmco = 2;
546  }
547  }
548 
549  h->nb_mmco = nb_mmco;
550 }
551 
553 {
554  MMCO *mmco = h->mmco;
555  int mmco_count;
556  int i, av_uninit(j);
557  int current_ref_assigned = 0, err = 0;
558  H264Picture *av_uninit(pic);
559 
560  if (!h->ps.sps) {
561  av_log(h->avctx, AV_LOG_ERROR, "SPS is unset\n");
562  err = AVERROR_INVALIDDATA;
563  goto out;
564  }
565 
566  if (!h->explicit_ref_marking)
568  mmco_count = h->nb_mmco;
569 
570  if ((h->avctx->debug & FF_DEBUG_MMCO) && mmco_count == 0)
571  av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
572 
573  for (i = 0; i < mmco_count; i++) {
574  int av_uninit(structure), av_uninit(frame_num);
575  if (h->avctx->debug & FF_DEBUG_MMCO)
576  av_log(h->avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode,
577  h->mmco[i].short_pic_num, h->mmco[i].long_arg);
578 
579  if (mmco[i].opcode == MMCO_SHORT2UNUSED ||
580  mmco[i].opcode == MMCO_SHORT2LONG) {
581  frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
582  pic = find_short(h, frame_num, &j);
583  if (!pic) {
584  if (mmco[i].opcode != MMCO_SHORT2LONG ||
585  !h->long_ref[mmco[i].long_arg] ||
586  h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
587  av_log(h->avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
588  err = AVERROR_INVALIDDATA;
589  }
590  continue;
591  }
592  }
593 
594  switch (mmco[i].opcode) {
595  case MMCO_SHORT2UNUSED:
596  if (h->avctx->debug & FF_DEBUG_MMCO)
597  av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n",
599  remove_short(h, frame_num, structure ^ PICT_FRAME);
600  break;
601  case MMCO_SHORT2LONG:
602  if (h->long_ref[mmco[i].long_arg] != pic)
603  remove_long(h, mmco[i].long_arg, 0);
604 
605  remove_short_at_index(h, j);
606  h->long_ref[ mmco[i].long_arg ] = pic;
607  if (h->long_ref[mmco[i].long_arg]) {
608  h->long_ref[mmco[i].long_arg]->long_ref = 1;
609  h->long_ref_count++;
610  }
611  break;
612  case MMCO_LONG2UNUSED:
613  j = pic_num_extract(h, mmco[i].long_arg, &structure);
614  pic = h->long_ref[j];
615  if (pic) {
616  remove_long(h, j, structure ^ PICT_FRAME);
617  } else if (h->avctx->debug & FF_DEBUG_MMCO)
618  av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
619  break;
620  case MMCO_LONG:
621  // Comment below left from previous code as it is an interesting note.
622  /* First field in pair is in short term list or
623  * at a different long term index.
624  * This is not allowed; see 7.4.3.3, notes 2 and 3.
625  * Report the problem and keep the pair where it is,
626  * and mark this field valid.
627  */
628  if (h->short_ref[0] == h->cur_pic_ptr)
629  remove_short_at_index(h, 0);
630 
631  /* make sure the current picture is not already assigned as a long ref */
632  if (h->cur_pic_ptr->long_ref) {
633  for (j = 0; j < FF_ARRAY_ELEMS(h->long_ref); j++) {
634  if (h->long_ref[j] == h->cur_pic_ptr)
635  remove_long(h, j, 0);
636  }
637  }
638 
639 
640  if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
641  remove_long(h, mmco[i].long_arg, 0);
642 
643  h->long_ref[mmco[i].long_arg] = h->cur_pic_ptr;
644  h->long_ref[mmco[i].long_arg]->long_ref = 1;
645  h->long_ref_count++;
646  }
647 
649  current_ref_assigned = 1;
650  break;
651  case MMCO_SET_MAX_LONG:
652  assert(mmco[i].long_arg <= 16);
653  // just remove the long term which index is greater than new max
654  for (j = mmco[i].long_arg; j < 16; j++) {
655  remove_long(h, j, 0);
656  }
657  break;
658  case MMCO_RESET:
659  while (h->short_ref_count) {
660  remove_short(h, h->short_ref[0]->frame_num, 0);
661  }
662  for (j = 0; j < 16; j++) {
663  remove_long(h, j, 0);
664  }
665  h->poc.frame_num = h->cur_pic_ptr->frame_num = 0;
666  h->mmco_reset = 1;
667  h->cur_pic_ptr->mmco_reset = 1;
668  break;
669  default: assert(0);
670  }
671  }
672 
673  if (!current_ref_assigned) {
674  /* Second field of complementary field pair; the first field of
675  * which is already referenced. If short referenced, it
676  * should be first entry in short_ref. If not, it must exist
677  * in long_ref; trying to put it on the short list here is an
678  * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
679  */
680  if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
681  /* Just mark the second field valid */
683  } else if (h->cur_pic_ptr->long_ref) {
684  av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
685  "assignment for second field "
686  "in complementary field pair "
687  "(first field is long term)\n");
688  err = AVERROR_INVALIDDATA;
689  } else {
690  pic = remove_short(h, h->cur_pic_ptr->frame_num, 0);
691  if (pic) {
692  av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
693  err = AVERROR_INVALIDDATA;
694  }
695 
696  if (h->short_ref_count)
697  memmove(&h->short_ref[1], &h->short_ref[0],
698  h->short_ref_count * sizeof(H264Picture*));
699 
700  h->short_ref[0] = h->cur_pic_ptr;
701  h->short_ref_count++;
703  }
704  }
705 
706  if (h->long_ref_count + h->short_ref_count -
707  (h->short_ref[0] == h->cur_pic_ptr) > h->ps.sps->ref_frame_count) {
708 
709  /* We have too many reference frames, probably due to corrupted
710  * stream. Need to discard one frame. Prevents overrun of the
711  * short_ref and long_ref buffers.
712  */
714  "number of reference frames (%d+%d) exceeds max (%d; probably "
715  "corrupt input), discarding one\n",
717  err = AVERROR_INVALIDDATA;
718 
719  if (h->long_ref_count && !h->short_ref_count) {
720  for (i = 0; i < 16; ++i)
721  if (h->long_ref[i])
722  break;
723 
724  assert(i < 16);
725  remove_long(h, i, 0);
726  } else {
727  pic = h->short_ref[h->short_ref_count - 1];
728  remove_short(h, pic->frame_num, 0);
729  }
730  }
731 
732  print_short_term(h);
733  print_long_term(h);
734 out:
735  return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
736 }
737 
739  const H2645NAL *nal, void *logctx)
740 {
741  int i;
742  MMCO *mmco = sl->mmco;
743  int nb_mmco = 0;
744 
745  if (nal->type == H264_NAL_IDR_SLICE) { // FIXME fields
746  skip_bits1(gb); // broken_link
747  if (get_bits1(gb)) {
748  mmco[0].opcode = MMCO_LONG;
749  mmco[0].long_arg = 0;
750  nb_mmco = 1;
751  }
752  sl->explicit_ref_marking = 1;
753  } else {
755  if (sl->explicit_ref_marking) {
756  for (i = 0; i < MAX_MMCO_COUNT; i++) {
757  MMCOOpcode opcode = get_ue_golomb_31(gb);
758 
759  mmco[i].opcode = opcode;
760  if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG) {
761  mmco[i].short_pic_num =
762  (sl->curr_pic_num - get_ue_golomb(gb) - 1) &
763  (sl->max_pic_num - 1);
764  }
765  if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
766  opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
767  unsigned int long_arg = get_ue_golomb_31(gb);
768  if (long_arg >= 32 ||
769  (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
770  long_arg == 16) &&
771  !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE(sl)))) {
772  av_log(logctx, AV_LOG_ERROR,
773  "illegal long ref in memory management control "
774  "operation %d\n", opcode);
775  return -1;
776  }
777  mmco[i].long_arg = long_arg;
778  }
779 
780  if (opcode > (unsigned) MMCO_LONG) {
781  av_log(logctx, AV_LOG_ERROR,
782  "illegal memory management control operation %d\n",
783  opcode);
784  return -1;
785  }
786  if (opcode == MMCO_END)
787  break;
788  }
789  nb_mmco = i;
790  }
791  }
792 
793  sl->nb_mmco = nb_mmco;
794 
795  return 0;
796 }
static int split_field_copy(H264Ref *dest, H264Picture *src, int parity, int id_add)
Definition: h264_refs.c:61
static int add_sorted(H264Picture **sorted, H264Picture *const *src, int len, int limit, int dir)
Definition: h264_refs.c:101
Memory management control operation.
Definition: h264dec.h:121
int nb_mmco
Definition: h264dec.h:460
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:153
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
H264POCContext poc
Definition: h264dec.h:448
static H264Picture * remove_short(H264Context *h, int frame_num, int ref_mask)
Definition: h264_refs.c:473
int first_field
Definition: h264dec.h:399
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:308
static void h264_initialise_ref_list(const H264Context *h, H264SliceContext *sl)
Definition: h264_refs.c:124
MMCO mmco[MAX_MMCO_COUNT]
memory management control operations buffer.
Definition: h264dec.h:459
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
H264Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
Definition: h264dec.h:452
#define FF_ARRAY_ELEMS(a)
H264Context.
Definition: h264dec.h:334
AVFrame * f
Definition: h264dec.h:128
int nb_ref_modifications[2]
Definition: h264dec.h:273
H264Picture * long_ref[32]
Definition: h264dec.h:451
int picture_structure
Definition: h264dec.h:398
MMCOOpcode opcode
Definition: h264dec.h:122
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264dec.h:264
MMCO mmco[MAX_MMCO_COUNT]
Definition: h264dec.h:319
void ff_h264_remove_all_refs(H264Context *h)
Definition: h264_refs.c:512
struct H264SliceContext::@20 ref_modifications[2][32]
int short_pic_num
pic_num without wrapping (pic_num & max_pic_num)
Definition: h264dec.h:123
static void print_short_term(const H264Context *h)
print short term list
Definition: h264_refs.c:177
int poc
Definition: h264dec.h:168
int poc
frame POC
Definition: h264dec.h:147
const char data[16]
Definition: mxf.c:70
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
H264Picture * parent
Definition: h264dec.h:171
int ff_h264_decode_ref_pic_list_reordering(H264SliceContext *sl, void *logctx)
Definition: h264_refs.c:370
H.264 common definitions.
#define src
Definition: vp8dsp.c:254
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
int ff_h264_decode_ref_pic_marking(H264SliceContext *sl, GetBitContext *gb, const H2645NAL *nal, void *logctx)
Definition: h264_refs.c:738
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
#define FIELD_PICTURE(h)
Definition: h264dec.h:74
#define PICT_TOP_FIELD
Definition: mpegutils.h:37
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264dec.h:148
MMCOOpcode
Memory management control operation opcode.
Definition: h264dec.h:108
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264dec.h:181
int pic_id
Definition: h264dec.h:169
static int unreference_pic(H264Context *h, H264Picture *pic, int refmask)
Mark a picture as no longer needed for reference.
Definition: h264_refs.c:416
uint8_t * data[3]
Definition: h264dec.h:164
int ref_frame_count
num_ref_frames
Definition: h264_ps.h:56
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2670
int reference
Definition: h264dec.h:159
int long_ref_count
number of actual long term references
Definition: h264dec.h:464
int ff_h264_build_ref_list(const H264Context *h, H264SliceContext *sl)
Definition: h264_refs.c:256
H.264 / AVC / MPEG-4 part10 codec.
int mmco_reset
Definition: h264dec.h:461
#define FF_DEBUG_MMCO
Definition: avcodec.h:2645
#define AV_EF_EXPLODE
Definition: avcodec.h:2681
int reference
Definition: h264dec.h:167
static void remove_short_at_index(H264Context *h, int i)
Remove a picture from the short term reference list by its index in that list.
Definition: h264_refs.c:461
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: mpegutils.h:45
static void ref_from_h264pic(H264Ref *dst, H264Picture *src)
Definition: h264_refs.c:51
int type
NAL unit type.
Definition: h2645_parse.h:50
static H264Picture * find_short(H264Context *h, int frame_num, int *idx)
Find a H264Picture in the short term reference list by frame number.
Definition: h264_refs.c:439
static const float pred[4]
Definition: siprdata.h:259
NULL
Definition: eval.c:55
static int pic_num_extract(const H264Context *h, int pic_num, int *structure)
Extract structure information about the picture described by pic_num in the current decoding context ...
Definition: h264_refs.c:218
AVCodecContext * avctx
Definition: h264dec.h:336
Libavcodec external API header.
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:96
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
H264Picture * short_ref[32]
Definition: h264dec.h:450
int explicit_ref_marking
Definition: h264dec.h:462
int field_poc[2]
top/bottom POC
Definition: h264dec.h:146
int debug
debug
Definition: avcodec.h:2626
int explicit_ref_marking
Definition: h264dec.h:321
static void print_long_term(const H264Context *h)
print long term list
Definition: h264_refs.c:193
AVBuffer * buffer
Definition: buffer.h:82
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:292
int index
Definition: gxfenc.c:72
#define FRAME_MBAFF(h)
Definition: h264dec.h:73
int mmco_reset
MMCO_RESET set this 1.
Definition: h264dec.h:149
static H264Picture * remove_long(H264Context *h, int i, int ref_mask)
Remove a picture from the long term reference list by its index in that list.
Definition: h264_refs.c:495
H264Picture * cur_pic_ptr
Definition: h264dec.h:343
int linesize[3]
Definition: h264dec.h:165
unsigned int list_count
Definition: h264dec.h:265
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:76
int pic_id
pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num) ...
Definition: h264dec.h:151
static void pic_as_field(H264Ref *pic, const int parity)
Definition: h264_refs.c:39
common internal api header.
H264ParamSets ps
Definition: h264dec.h:444
static void h264_fill_mbaff_ref_list(H264SliceContext *sl)
Definition: h264_refs.c:231
Bi-dir predicted.
Definition: avutil.h:262
uint8_t op
Definition: h264dec.h:270
int long_arg
index, pic_num, or num long refs depending on opcode
Definition: h264dec.h:124
#define PICT_FRAME
Definition: mpegutils.h:39
int len
static int build_def_list(H264Ref *def, int def_len, H264Picture *const *in, int len, int is_long, int sel)
Definition: h264_refs.c:77
int ff_h264_execute_ref_pic_marking(H264Context *h)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:552
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264dec.h:266
#define av_uninit(x)
Definition: attributes.h:109
SPS * sps
Definition: h264_ps.h:140
FILE * out
Definition: movenc.c:54
#define FFSWAP(type, a, b)
Definition: common.h:69
exp golomb vlc stuff
static void generate_sliding_window_mmcos(H264Context *h)
Definition: h264_refs.c:528
GetBitContext gb
Definition: h264dec.h:176
uint32_t val
Definition: h264dec.h:271
#define MAX_MMCO_COUNT
Definition: h264dec.h:54
int short_ref_count
number of actual short term references
Definition: h264dec.h:465