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 "internal.h"
29 #include "dsputil.h"
30 #include "avcodec.h"
31 #include "h264.h"
32 #include "golomb.h"
33 
34 //#undef NDEBUG
35 #include <assert.h>
36 
37 
38 static void pic_as_field(Picture *pic, const int parity){
39  int i;
40  for (i = 0; i < 4; ++i) {
41  if (parity == PICT_BOTTOM_FIELD)
42  pic->f.data[i] += pic->f.linesize[i];
43  pic->f.reference = parity;
44  pic->f.linesize[i] *= 2;
45  }
46  pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
47 }
48 
49 static int split_field_copy(Picture *dest, Picture *src,
50  int parity, int id_add){
51  int match = !!(src->f.reference & parity);
52 
53  if (match) {
54  *dest = *src;
55  if(parity != PICT_FRAME){
56  pic_as_field(dest, parity);
57  dest->pic_id *= 2;
58  dest->pic_id += id_add;
59  }
60  }
61 
62  return match;
63 }
64 
65 static int build_def_list(Picture *def, int def_len,
66  Picture **in, int len, int is_long, int sel)
67 {
68  int i[2]={0};
69  int index=0;
70 
71  while ((i[0] < len || i[1] < len) && index < def_len) {
72  while (i[0] < len && !(in[ i[0] ] && (in[ i[0] ]->f.reference & sel)))
73  i[0]++;
74  while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->f.reference & (sel^3))))
75  i[1]++;
76  if (i[0] < len && index < def_len) {
77  in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
78  split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
79  }
80  if (i[1] < len && index < def_len) {
81  in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
82  split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
83  }
84  }
85 
86  return index;
87 }
88 
89 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
90  int i, best_poc;
91  int out_i= 0;
92 
93  for(;;){
94  best_poc= dir ? INT_MIN : INT_MAX;
95 
96  for(i=0; i<len; i++){
97  const int poc= src[i]->poc;
98  if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
99  best_poc= poc;
100  sorted[out_i]= src[i];
101  }
102  }
103  if(best_poc == (dir ? INT_MIN : INT_MAX))
104  break;
105  limit= sorted[out_i++]->poc - dir;
106  }
107  return out_i;
108 }
109 
111  int i, len;
112 
114  Picture *sorted[32];
115  int cur_poc, list;
116  int lens[2];
117 
118  if(FIELD_PICTURE)
120  else
121  cur_poc= h->cur_pic_ptr->poc;
122 
123  for(list= 0; list<2; list++){
124  len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
125  len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
126  assert(len<=32);
127 
129  sorted, len, 0, h->picture_structure);
130  len += build_def_list(h->default_ref_list[list] + len,
131  FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
132  h->long_ref, 16, 1, h->picture_structure);
133 
134  if(len < h->ref_count[list])
135  memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
136  lens[list]= len;
137  }
138 
139  if(lens[0] == lens[1] && lens[1] > 1){
140  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++);
141  if(i == lens[0])
142  FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
143  }
144  }else{
147  len += build_def_list(h->default_ref_list[0] + len,
148  FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
149  h-> long_ref, 16, 1, h->picture_structure);
150 
151  if(len < h->ref_count[0])
152  memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
153  }
154 #ifdef TRACE
155  for (i=0; i<h->ref_count[0]; i++) {
156  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]);
157  }
159  for (i=0; i<h->ref_count[1]; i++) {
160  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]);
161  }
162  }
163 #endif
164  return 0;
165 }
166 
167 static void print_short_term(H264Context *h);
168 static void print_long_term(H264Context *h);
169 
180 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
181  *structure = h->picture_structure;
182  if(FIELD_PICTURE){
183  if (!(pic_num & 1))
184  /* opposite field */
185  *structure ^= PICT_FRAME;
186  pic_num >>= 1;
187  }
188 
189  return pic_num;
190 }
191 
193  int list, index, pic_structure;
194 
195  print_short_term(h);
196  print_long_term(h);
197 
198  for(list=0; list<h->list_count; list++){
199  memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
200 
201  if(get_bits1(&h->gb)){
202  int pred= h->curr_pic_num;
203 
204  for(index=0; ; index++){
205  unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&h->gb);
206  unsigned int pic_id;
207  int i;
208  Picture *ref = NULL;
209 
210  if(reordering_of_pic_nums_idc==3)
211  break;
212 
213  if(index >= h->ref_count[list]){
214  av_log(h->avctx, AV_LOG_ERROR, "reference count overflow\n");
215  return -1;
216  }
217 
218  if(reordering_of_pic_nums_idc<3){
219  if(reordering_of_pic_nums_idc<2){
220  const unsigned int abs_diff_pic_num= get_ue_golomb(&h->gb) + 1;
221  int frame_num;
222 
223  if(abs_diff_pic_num > h->max_pic_num){
224  av_log(h->avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
225  return -1;
226  }
227 
228  if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
229  else pred+= abs_diff_pic_num;
230  pred &= h->max_pic_num - 1;
231 
232  frame_num = pic_num_extract(h, pred, &pic_structure);
233 
234  for(i= h->short_ref_count-1; i>=0; i--){
235  ref = h->short_ref[i];
236  assert(ref->f.reference);
237  assert(!ref->long_ref);
238  if(
239  ref->frame_num == frame_num &&
240  (ref->f.reference & pic_structure)
241  )
242  break;
243  }
244  if(i>=0)
245  ref->pic_id= pred;
246  }else{
247  int long_idx;
248  pic_id= get_ue_golomb(&h->gb); //long_term_pic_idx
249 
250  long_idx= pic_num_extract(h, pic_id, &pic_structure);
251 
252  if(long_idx>31){
253  av_log(h->avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
254  return -1;
255  }
256  ref = h->long_ref[long_idx];
257  assert(!(ref && !ref->f.reference));
258  if (ref && (ref->f.reference & pic_structure)) {
259  ref->pic_id= pic_id;
260  assert(ref->long_ref);
261  i=0;
262  }else{
263  i=-1;
264  }
265  }
266 
267  if (i < 0) {
268  av_log(h->avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
269  memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
270  } else {
271  for(i=index; i+1<h->ref_count[list]; i++){
272  if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
273  break;
274  }
275  for(; i > index; i--){
276  h->ref_list[list][i]= h->ref_list[list][i-1];
277  }
278  h->ref_list[list][index]= *ref;
279  if (FIELD_PICTURE){
280  pic_as_field(&h->ref_list[list][index], pic_structure);
281  }
282  }
283  }else{
284  av_log(h->avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
285  return -1;
286  }
287  }
288  }
289  }
290  for(list=0; list<h->list_count; list++){
291  for(index= 0; index < h->ref_count[list]; index++){
292  if (!h->ref_list[list][index].f.data[0]) {
293  av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture\n");
294  if (h->default_ref_list[list][0].f.data[0])
295  h->ref_list[list][index]= h->default_ref_list[list][0];
296  else
297  return -1;
298  }
299  }
300  }
301 
302  return 0;
303 }
304 
306  int list, i, j;
307  for(list=0; list<2; list++){ //FIXME try list_count
308  for(i=0; i<h->ref_count[list]; i++){
309  Picture *frame = &h->ref_list[list][i];
310  Picture *field = &h->ref_list[list][16+2*i];
311  field[0] = *frame;
312  for(j=0; j<3; j++)
313  field[0].f.linesize[j] <<= 1;
314  field[0].f.reference = PICT_TOP_FIELD;
315  field[0].poc= field[0].field_poc[0];
316  field[1] = field[0];
317  for(j=0; j<3; j++)
318  field[1].f.data[j] += frame->f.linesize[j];
319  field[1].f.reference = PICT_BOTTOM_FIELD;
320  field[1].poc= field[1].field_poc[1];
321 
322  h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
323  h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
324  for(j=0; j<2; j++){
325  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];
326  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];
327  }
328  }
329  }
330 }
331 
343 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
344  int i;
345  if (pic->f.reference &= refmask) {
346  return 0;
347  } else {
348  for(i = 0; h->delayed_pic[i]; i++)
349  if(pic == h->delayed_pic[i]){
350  pic->f.reference = DELAYED_PIC_REF;
351  break;
352  }
353  return 1;
354  }
355 }
356 
365 static Picture * find_short(H264Context *h, int frame_num, int *idx){
366  int i;
367 
368  for(i=0; i<h->short_ref_count; i++){
369  Picture *pic= h->short_ref[i];
370  if(h->avctx->debug&FF_DEBUG_MMCO)
371  av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
372  if(pic->frame_num == frame_num) {
373  *idx = i;
374  return pic;
375  }
376  }
377  return NULL;
378 }
379 
386 static void remove_short_at_index(H264Context *h, int i){
387  assert(i >= 0 && i < h->short_ref_count);
388  h->short_ref[i]= NULL;
389  if (--h->short_ref_count)
390  memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
391 }
392 
397 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
398  Picture *pic;
399  int i;
400 
401  if(h->avctx->debug&FF_DEBUG_MMCO)
402  av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
403 
404  pic = find_short(h, frame_num, &i);
405  if (pic){
406  if(unreference_pic(h, pic, ref_mask))
407  remove_short_at_index(h, i);
408  }
409 
410  return pic;
411 }
412 
418 static Picture * remove_long(H264Context *h, int i, int ref_mask){
419  Picture *pic;
420 
421  pic= h->long_ref[i];
422  if (pic){
423  if(unreference_pic(h, pic, ref_mask)){
424  assert(h->long_ref[i]->long_ref == 1);
425  h->long_ref[i]->long_ref= 0;
426  h->long_ref[i]= NULL;
427  h->long_ref_count--;
428  }
429  }
430 
431  return pic;
432 }
433 
435  int i;
436 
437  for(i=0; i<16; i++){
438  remove_long(h, i, 0);
439  }
440  assert(h->long_ref_count==0);
441 
442  for(i=0; i<h->short_ref_count; i++){
443  unreference_pic(h, h->short_ref[i], 0);
444  h->short_ref[i]= NULL;
445  }
446  h->short_ref_count=0;
447 }
448 
452 static void print_short_term(H264Context *h) {
453  uint32_t i;
454  if(h->avctx->debug&FF_DEBUG_MMCO) {
455  av_log(h->avctx, AV_LOG_DEBUG, "short term list:\n");
456  for(i=0; i<h->short_ref_count; i++){
457  Picture *pic= h->short_ref[i];
458  av_log(h->avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
459  i, pic->frame_num, pic->poc, pic->f.data[0]);
460  }
461  }
462 }
463 
467 static void print_long_term(H264Context *h) {
468  uint32_t i;
469  if(h->avctx->debug&FF_DEBUG_MMCO) {
470  av_log(h->avctx, AV_LOG_DEBUG, "long term list:\n");
471  for(i = 0; i < 16; i++){
472  Picture *pic= h->long_ref[i];
473  if (pic) {
474  av_log(h->avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
475  i, pic->frame_num, pic->poc, pic->f.data[0]);
476  }
477  }
478  }
479 }
480 
481 static int check_opcodes(MMCO *mmco1, MMCO *mmco2, int n_mmcos)
482 {
483  int i;
484 
485  for (i = 0; i < n_mmcos; i++) {
486  if (mmco1[i].opcode != mmco2[i].opcode)
487  return -1;
488  }
489 
490  return 0;
491 }
492 
494 {
495  MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
496  int mmco_index = 0, i;
497 
498  assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
499 
500  if (h->short_ref_count &&
502  !(FIELD_PICTURE && !h->first_field && h->cur_pic_ptr->f.reference)) {
503  mmco[0].opcode = MMCO_SHORT2UNUSED;
504  mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
505  mmco_index = 1;
506  if (FIELD_PICTURE) {
507  mmco[0].short_pic_num *= 2;
508  mmco[1].opcode = MMCO_SHORT2UNUSED;
509  mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
510  mmco_index = 2;
511  }
512  }
513 
514  if (first_slice) {
515  h->mmco_index = mmco_index;
516  } else if (!first_slice && mmco_index >= 0 &&
517  (mmco_index != h->mmco_index ||
518  (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
520  "Inconsistent MMCO state between slices [%d, %d, %d]\n",
521  mmco_index, h->mmco_index, i);
522  return AVERROR_INVALIDDATA;
523  }
524  return 0;
525 }
526 
527 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
528  int i, av_uninit(j);
529  int current_ref_assigned=0, err=0;
530  Picture *av_uninit(pic);
531 
532  if((h->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
533  av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
534 
535  for(i=0; i<mmco_count; i++){
536  int av_uninit(structure), av_uninit(frame_num);
537  if(h->avctx->debug&FF_DEBUG_MMCO)
538  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);
539 
540  if( mmco[i].opcode == MMCO_SHORT2UNUSED
541  || mmco[i].opcode == MMCO_SHORT2LONG){
542  frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
543  pic = find_short(h, frame_num, &j);
544  if(!pic){
545  if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
546  || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
547  av_log(h->avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
548  err = AVERROR_INVALIDDATA;
549  }
550  continue;
551  }
552  }
553 
554  switch(mmco[i].opcode){
555  case MMCO_SHORT2UNUSED:
556  if(h->avctx->debug&FF_DEBUG_MMCO)
557  av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
558  remove_short(h, frame_num, structure ^ PICT_FRAME);
559  break;
560  case MMCO_SHORT2LONG:
561  if (h->long_ref[mmco[i].long_arg] != pic)
562  remove_long(h, mmco[i].long_arg, 0);
563 
564  remove_short_at_index(h, j);
565  h->long_ref[ mmco[i].long_arg ]= pic;
566  if (h->long_ref[ mmco[i].long_arg ]){
567  h->long_ref[ mmco[i].long_arg ]->long_ref=1;
568  h->long_ref_count++;
569  }
570  break;
571  case MMCO_LONG2UNUSED:
572  j = pic_num_extract(h, mmco[i].long_arg, &structure);
573  pic = h->long_ref[j];
574  if (pic) {
575  remove_long(h, j, structure ^ PICT_FRAME);
576  } else if(h->avctx->debug&FF_DEBUG_MMCO)
577  av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
578  break;
579  case MMCO_LONG:
580  // Comment below left from previous code as it is an interresting note.
581  /* First field in pair is in short term list or
582  * at a different long term index.
583  * This is not allowed; see 7.4.3.3, notes 2 and 3.
584  * Report the problem and keep the pair where it is,
585  * and mark this field valid.
586  */
587 
588  if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
589  remove_long(h, mmco[i].long_arg, 0);
590 
591  h->long_ref[ mmco[i].long_arg ]= h->cur_pic_ptr;
592  h->long_ref[ mmco[i].long_arg ]->long_ref=1;
593  h->long_ref_count++;
594  }
595 
597  current_ref_assigned=1;
598  break;
599  case MMCO_SET_MAX_LONG:
600  assert(mmco[i].long_arg <= 16);
601  // just remove the long term which index is greater than new max
602  for(j = mmco[i].long_arg; j<16; j++){
603  remove_long(h, j, 0);
604  }
605  break;
606  case MMCO_RESET:
607  while(h->short_ref_count){
608  remove_short(h, h->short_ref[0]->frame_num, 0);
609  }
610  for(j = 0; j < 16; j++) {
611  remove_long(h, j, 0);
612  }
613  h->frame_num=
614  h->cur_pic_ptr->frame_num= 0;
615  h->mmco_reset = 1;
616  h->cur_pic_ptr->mmco_reset=1;
617  break;
618  default: assert(0);
619  }
620  }
621 
622  if (!current_ref_assigned) {
623  /* Second field of complementary field pair; the first field of
624  * which is already referenced. If short referenced, it
625  * should be first entry in short_ref. If not, it must exist
626  * in long_ref; trying to put it on the short list here is an
627  * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
628  */
629  if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
630  /* Just mark the second field valid */
632  } else if (h->cur_pic_ptr->long_ref) {
633  av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
634  "assignment for second field "
635  "in complementary field pair "
636  "(first field is long term)\n");
637  err = AVERROR_INVALIDDATA;
638  } else {
639  pic= remove_short(h, h->cur_pic_ptr->frame_num, 0);
640  if(pic){
641  av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
642  err = AVERROR_INVALIDDATA;
643  }
644 
645  if(h->short_ref_count)
646  memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
647 
648  h->short_ref[0]= h->cur_pic_ptr;
649  h->short_ref_count++;
651  }
652  }
653 
654  if (h->long_ref_count + h->short_ref_count -
655  (h->short_ref[0] == h->cur_pic_ptr) > h->sps.ref_frame_count){
656 
657  /* We have too many reference frames, probably due to corrupted
658  * stream. Need to discard one frame. Prevents overrun of the
659  * short_ref and long_ref buffers.
660  */
662  "number of reference frames (%d+%d) exceeds max (%d; probably "
663  "corrupt input), discarding one\n",
665  err = AVERROR_INVALIDDATA;
666 
667  if (h->long_ref_count && !h->short_ref_count) {
668  for (i = 0; i < 16; ++i)
669  if (h->long_ref[i])
670  break;
671 
672  assert(i < 16);
673  remove_long(h, i, 0);
674  } else {
675  pic = h->short_ref[h->short_ref_count - 1];
676  remove_short(h, pic->frame_num, 0);
677  }
678  }
679 
680  print_short_term(h);
681  print_long_term(h);
682  return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
683 }
684 
686  int first_slice)
687 {
688  int i, ret;
689  MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
690  int mmco_index = 0;
691 
692  if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
693  skip_bits1(gb); // broken_link
694  if (get_bits1(gb)){
695  mmco[0].opcode = MMCO_LONG;
696  mmco[0].long_arg = 0;
697  mmco_index = 1;
698  }
699  } else {
700  if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
701  for (i = 0; i < MAX_MMCO_COUNT; i++) {
702  MMCOOpcode opcode = get_ue_golomb_31(gb);
703 
704  mmco[i].opcode = opcode;
705  if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG){
706  mmco[i].short_pic_num =
707  (h->curr_pic_num - get_ue_golomb(gb) - 1) &
708  (h->max_pic_num - 1);
709 #if 0
710  if (mmco[i].short_pic_num >= h->short_ref_count ||
711  h->short_ref[ mmco[i].short_pic_num ] == NULL){
712  av_log(s->avctx, AV_LOG_ERROR,
713  "illegal short ref in memory management control "
714  "operation %d\n", mmco);
715  return -1;
716  }
717 #endif
718  }
719  if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
720  opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
721  unsigned int long_arg = get_ue_golomb_31(gb);
722  if (long_arg >= 32 ||
723  (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
724  long_arg == 16) &&
725  !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
727  "illegal long ref in memory management control "
728  "operation %d\n", opcode);
729  return -1;
730  }
731  mmco[i].long_arg = long_arg;
732  }
733 
734  if (opcode > (unsigned) MMCO_LONG){
736  "illegal memory management control operation %d\n",
737  opcode);
738  return -1;
739  }
740  if (opcode == MMCO_END)
741  break;
742  }
743  mmco_index = i;
744  } else {
745  if (first_slice) {
746  ret = ff_generate_sliding_window_mmcos(h, first_slice);
747  if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
748  return ret;
749  }
750  mmco_index = -1;
751  }
752  }
753 
754  if (first_slice && mmco_index != -1) {
755  h->mmco_index = mmco_index;
756  } else if (!first_slice && mmco_index >= 0 &&
757  (mmco_index != h->mmco_index ||
758  (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
760  "Inconsistent MMCO state between slices [%d, %d, %d]\n",
761  mmco_index, h->mmco_index, i);
762  return AVERROR_INVALIDDATA;
763  }
764 
765  return 0;
766 }
Picture default_ref_list[2][32]
base reference list for all slices of a coded picture
Definition: h264.h:521
#define PICT_BOTTOM_FIELD
Definition: mpegvideo.h:628
Memory management control operation.
Definition: h264.h:243
static int pic_num_extract(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:180
#define PICT_TOP_FIELD
Definition: mpegvideo.h:627
GetBitContext gb
Definition: h264.h:259
int first_field
Definition: h264.h:377
static int check_opcodes(MMCO *mmco1, MMCO *mmco2, int n_mmcos)
Definition: h264_refs.c:481
MMCO mmco[MAX_MMCO_COUNT]
memory management control operations buffer.
Definition: h264.h:531
int ff_h264_fill_default_ref_list(H264Context *h)
Fill the default_ref_list.
Definition: h264_refs.c:110
static int build_def_list(Picture *def, int def_len, Picture **in, int len, int is_long, int sel)
Definition: h264_refs.c:65
void ff_h264_fill_mbaff_ref_list(H264Context *h)
Definition: h264_refs.c:305
int mmco_index
Definition: h264.h:532
int ff_h264_decode_ref_pic_list_reordering(H264Context *h)
Definition: h264_refs.c:192
MMCOOpcode
Memory management control operation opcode.
Definition: h264.h:230
H264Context.
Definition: h264.h:252
int mmco_reset
h264 MMCO_RESET set this 1. Reordering code must not mix pictures before and after MMCO_RESET...
Definition: mpegvideo.h:133
int picture_structure
Definition: h264.h:376
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264.h:369
MMCOOpcode opcode
Definition: h264.h:244
void ff_h264_remove_all_refs(H264Context *h)
Definition: h264_refs.c:434
int long_ref
1->long term reference 0->short term reference
Definition: mpegvideo.h:136
int short_pic_num
pic_num without wrapping (pic_num & max_pic_num)
Definition: h264.h:245
#define PICT_FRAME
Definition: mpegvideo.h:629
int luma_weight[48][2][2]
Definition: h264.h:387
Picture ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:405
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:402
int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb, int first_slice)
Definition: h264_refs.c:685
int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice)
Definition: h264_refs.c:493
int chroma_weight[48][2][2][2]
Definition: h264.h:388
H.264 / AVC / MPEG4 part10 codec.
int frame_num
Definition: h264.h:500
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
int nal_unit_type
Definition: h264.h:474
Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
Definition: h264.h:522
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
static void pic_as_field(Picture *pic, const int parity)
Definition: h264_refs.c:38
static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir)
Definition: h264_refs.c:89
int ref_frame_count
num_ref_frames
Definition: h264.h:157
Picture * long_ref[32]
Definition: h264.h:520
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2602
int poc
h264 frame POC
Definition: mpegvideo.h:131
static Picture * remove_short(H264Context *h, int frame_num, int ref_mask)
Definition: h264_refs.c:397
int long_ref_count
number of actual long term references
Definition: h264.h:535
Picture.
Definition: mpegvideo.h:95
SPS sps
current sps
Definition: h264.h:354
int frame_num
h264 frame_num (raw frame_num from slice header)
Definition: mpegvideo.h:132
int mmco_reset
Definition: h264.h:533
int max_pic_num
max_frame_num or 2 * max_frame_num for field pics.
Definition: h264.h:515
int curr_pic_num
frame_num for frames or 2 * frame_num + 1 for field pics.
Definition: h264.h:510
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:386
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: mpegvideo.h:88
unsigned int list_count
Definition: h264.h:403
static const float pred[4]
Definition: siprdata.h:259
NULL
Definition: eval.c:52
AVCodecContext * avctx
Definition: h264.h:253
int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:527
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]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
int debug
debug
Definition: avcodec.h:2568
Definition: h264.h:231
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:293
Picture * short_ref[32]
Definition: h264.h:519
int index
Definition: gxfenc.c:72
#define MAX_MMCO_COUNT
Definition: h264.h:43
static int split_field_copy(Picture *dest, Picture *src, int parity, int id_add)
Definition: h264_refs.c:49
static Picture * find_short(H264Context *h, int frame_num, int *idx)
Find a Picture in the short term reference list by frame number.
Definition: h264_refs.c:365
int field_poc[2]
h264 top/bottom POC
Definition: mpegvideo.h:130
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
#define tprintf(p,...)
Definition: get_bits.h:613
common internal api header.
static Picture * 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:418
#define FIELD_PICTURE
Definition: h264.h:63
Bi-dir predicted.
Definition: avutil.h:247
int long_arg
index, pic_num, or num long refs depending on opcode
Definition: h264.h:246
DSP utils.
Picture * cur_pic_ptr
Definition: h264.h:263
int len
int pic_id
h264 pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num) ...
Definition: mpegvideo.h:134
static void print_long_term(H264Context *h)
print long term list
Definition: h264_refs.c:467
struct AVFrame f
Definition: mpegvideo.h:96
static int unreference_pic(H264Context *h, Picture *pic, int refmask)
Mark a picture as no longer needed for reference.
Definition: h264_refs.c:343
exp golomb vlc stuff
static void print_short_term(H264Context *h)
print short term list
Definition: h264_refs.c:452
int short_ref_count
number of actual short term references
Definition: h264.h:536