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