FFmpeg  1.2.12
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "libavutil/avassert.h"
29 #include "internal.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  int i, len;
110 
112  Picture *sorted[32];
113  int cur_poc, list;
114  int lens[2];
115 
116  if(FIELD_PICTURE)
118  else
119  cur_poc= h->cur_pic_ptr->poc;
120 
121  for(list= 0; list<2; list++){
122  len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
123  len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
124  av_assert0(len<=32);
125  len= build_def_list(h->default_ref_list[list] , sorted , len, 0, h->picture_structure);
126  len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, h->picture_structure);
127  av_assert0(len<=32);
128 
129  if(len < h->ref_count[list])
130  memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
131  lens[list]= len;
132  }
133 
134  if(lens[0] == lens[1] && lens[1] > 1){
135  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++);
136  if(i == lens[0])
137  FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
138  }
139  }else{
141  len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, h->picture_structure);
142  av_assert0(len<=32);
143  if(len < h->ref_count[0])
144  memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
145  }
146 #ifdef TRACE
147  for (i=0; i<h->ref_count[0]; i++) {
148  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]);
149  }
151  for (i=0; i<h->ref_count[1]; i++) {
152  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]);
153  }
154  }
155 #endif
156  return 0;
157 }
158 
159 static void print_short_term(H264Context *h);
160 static void print_long_term(H264Context *h);
161 
172 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
173  *structure = h->picture_structure;
174  if(FIELD_PICTURE){
175  if (!(pic_num & 1))
176  /* opposite field */
177  *structure ^= PICT_FRAME;
178  pic_num >>= 1;
179  }
180 
181  return pic_num;
182 }
183 
185  int list, index, pic_structure;
186 
187  print_short_term(h);
188  print_long_term(h);
189 
190  for(list=0; list<h->list_count; list++){
191  memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
192 
193  if(get_bits1(&h->gb)){
194  int pred= h->curr_pic_num;
195 
196  for(index=0; ; index++){
197  unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&h->gb);
198  unsigned int pic_id;
199  int i;
200  Picture *ref = NULL;
201 
202  if(reordering_of_pic_nums_idc==3)
203  break;
204 
205  if(index >= h->ref_count[list]){
206  av_log(h->avctx, AV_LOG_ERROR, "reference count overflow\n");
207  return -1;
208  }
209 
210  if(reordering_of_pic_nums_idc<3){
211  if(reordering_of_pic_nums_idc<2){
212  const unsigned int abs_diff_pic_num= get_ue_golomb(&h->gb) + 1;
213  int frame_num;
214 
215  if(abs_diff_pic_num > h->max_pic_num){
216  av_log(h->avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
217  return -1;
218  }
219 
220  if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
221  else pred+= abs_diff_pic_num;
222  pred &= h->max_pic_num - 1;
223 
224  frame_num = pic_num_extract(h, pred, &pic_structure);
225 
226  for(i= h->short_ref_count-1; i>=0; i--){
227  ref = h->short_ref[i];
228  assert(ref->f.reference);
229  assert(!ref->long_ref);
230  if(
231  ref->frame_num == frame_num &&
232  (ref->f.reference & pic_structure)
233  )
234  break;
235  }
236  if(i>=0)
237  ref->pic_id= pred;
238  }else{
239  int long_idx;
240  pic_id= get_ue_golomb(&h->gb); //long_term_pic_idx
241 
242  long_idx= pic_num_extract(h, pic_id, &pic_structure);
243 
244  if(long_idx>31){
245  av_log(h->avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
246  return -1;
247  }
248  ref = h->long_ref[long_idx];
249  assert(!(ref && !ref->f.reference));
250  if (ref && (ref->f.reference & pic_structure)) {
251  ref->pic_id= pic_id;
252  assert(ref->long_ref);
253  i=0;
254  }else{
255  i=-1;
256  }
257  }
258 
259  if (i < 0) {
260  av_log(h->avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
261  memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
262  } else {
263  for(i=index; i+1<h->ref_count[list]; i++){
264  if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
265  break;
266  }
267  for(; i > index; i--){
268  h->ref_list[list][i]= h->ref_list[list][i-1];
269  }
270  h->ref_list[list][index]= *ref;
271  if (FIELD_PICTURE){
272  pic_as_field(&h->ref_list[list][index], pic_structure);
273  }
274  }
275  }else{
276  av_log(h->avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
277  return -1;
278  }
279  }
280  }
281  }
282  for(list=0; list<h->list_count; list++){
283  for(index= 0; index < h->ref_count[list]; index++){
284  if (!h->ref_list[list][index].f.data[0]) {
285  int i;
286  av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture, default is %d\n", h->default_ref_list[list][0].poc);
287  for (i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
288  h->last_pocs[i] = INT_MIN;
289  if (h->default_ref_list[list][0].f.data[0])
290  h->ref_list[list][index]= h->default_ref_list[list][0];
291  else
292  return -1;
293  }
294  }
295  }
296 
297  return 0;
298 }
299 
301  int list, i, j;
302  for(list=0; list<h->list_count; list++){
303  for(i=0; i<h->ref_count[list]; i++){
304  Picture *frame = &h->ref_list[list][i];
305  Picture *field = &h->ref_list[list][16+2*i];
306  field[0] = *frame;
307  for(j=0; j<3; j++)
308  field[0].f.linesize[j] <<= 1;
309  field[0].f.reference = PICT_TOP_FIELD;
310  field[0].poc= field[0].field_poc[0];
311  field[1] = field[0];
312  for(j=0; j<3; j++)
313  field[1].f.data[j] += frame->f.linesize[j];
314  field[1].f.reference = PICT_BOTTOM_FIELD;
315  field[1].poc= field[1].field_poc[1];
316 
317  h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
318  h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
319  for(j=0; j<2; j++){
320  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];
321  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];
322  }
323  }
324  }
325 }
326 
338 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
339  int i;
340  if (pic->f.reference &= refmask) {
341  return 0;
342  } else {
343  for(i = 0; h->delayed_pic[i]; i++)
344  if(pic == h->delayed_pic[i]){
345  pic->f.reference = DELAYED_PIC_REF;
346  break;
347  }
348  return 1;
349  }
350 }
351 
360 static Picture * find_short(H264Context *h, int frame_num, int *idx){
361  int i;
362 
363  for(i=0; i<h->short_ref_count; i++){
364  Picture *pic= h->short_ref[i];
365  if(h->avctx->debug&FF_DEBUG_MMCO)
366  av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
367  if(pic->frame_num == frame_num) {
368  *idx = i;
369  return pic;
370  }
371  }
372  return NULL;
373 }
374 
381 static void remove_short_at_index(H264Context *h, int i){
382  assert(i >= 0 && i < h->short_ref_count);
383  h->short_ref[i]= NULL;
384  if (--h->short_ref_count)
385  memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
386 }
387 
392 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
393  Picture *pic;
394  int i;
395 
396  if(h->avctx->debug&FF_DEBUG_MMCO)
397  av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
398 
399  pic = find_short(h, frame_num, &i);
400  if (pic){
401  if(unreference_pic(h, pic, ref_mask))
402  remove_short_at_index(h, i);
403  }
404 
405  return pic;
406 }
407 
413 static Picture * remove_long(H264Context *h, int i, int ref_mask){
414  Picture *pic;
415 
416  pic= h->long_ref[i];
417  if (pic){
418  if(unreference_pic(h, pic, ref_mask)){
419  assert(h->long_ref[i]->long_ref == 1);
420  h->long_ref[i]->long_ref= 0;
421  h->long_ref[i]= NULL;
422  h->long_ref_count--;
423  }
424  }
425 
426  return pic;
427 }
428 
430  int i;
431 
432  for(i=0; i<16; i++){
433  remove_long(h, i, 0);
434  }
435  assert(h->long_ref_count==0);
436 
437  for(i=0; i<h->short_ref_count; i++){
438  unreference_pic(h, h->short_ref[i], 0);
439  h->short_ref[i]= NULL;
440  }
441  h->short_ref_count=0;
442 
443  memset(h->default_ref_list, 0, sizeof(h->default_ref_list));
444  memset(h->ref_list, 0, sizeof(h->ref_list));
445 }
446 
450 static void print_short_term(H264Context *h) {
451  uint32_t i;
452  if(h->avctx->debug&FF_DEBUG_MMCO) {
453  av_log(h->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->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->avctx->debug&FF_DEBUG_MMCO) {
468  av_log(h->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->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  av_log(NULL, AV_LOG_ERROR, "MMCO opcode [%d, %d] at %d mismatches between slices\n",
486  mmco1[i].opcode, mmco2[i].opcode, i);
487  return -1;
488  }
489  }
490 
491  return 0;
492 }
493 
495 {
496  MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
497  int mmco_index = 0, i;
498 
499  if (h->short_ref_count &&
501  !(FIELD_PICTURE && !h->first_field && h->cur_pic_ptr->f.reference)) {
502  mmco[0].opcode = MMCO_SHORT2UNUSED;
503  mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
504  mmco_index = 1;
505  if (FIELD_PICTURE) {
506  mmco[0].short_pic_num *= 2;
507  mmco[1].opcode = MMCO_SHORT2UNUSED;
508  mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
509  mmco_index = 2;
510  }
511  }
512 
513  if (first_slice) {
514  h->mmco_index = mmco_index;
515  } else if (!first_slice && mmco_index >= 0 &&
516  (mmco_index != h->mmco_index ||
517  (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
519  "Inconsistent MMCO state between slices [%d, %d]\n",
520  mmco_index, h->mmco_index);
521  return AVERROR_INVALIDDATA;
522  }
523  return 0;
524 }
525 
526 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
527  int i, av_uninit(j);
528  int current_ref_assigned=0, err=0;
529  Picture *av_uninit(pic);
530 
531  if((h->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
532  av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
533 
534  for(i=0; i<mmco_count; i++){
535  int av_uninit(structure), av_uninit(frame_num);
536  if(h->avctx->debug&FF_DEBUG_MMCO)
537  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);
538 
539  if( mmco[i].opcode == MMCO_SHORT2UNUSED
540  || mmco[i].opcode == MMCO_SHORT2LONG){
541  frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
542  pic = find_short(h, frame_num, &j);
543  if(!pic){
544  if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
545  || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
546  av_log(h->avctx, h->short_ref_count ? AV_LOG_ERROR : AV_LOG_DEBUG, "mmco: unref short failure\n");
547  err = AVERROR_INVALIDDATA;
548  }
549  continue;
550  }
551  }
552 
553  switch(mmco[i].opcode){
554  case MMCO_SHORT2UNUSED:
555  if(h->avctx->debug&FF_DEBUG_MMCO)
556  av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
557  remove_short(h, frame_num, structure ^ PICT_FRAME);
558  break;
559  case MMCO_SHORT2LONG:
560  if (h->long_ref[mmco[i].long_arg] != pic)
561  remove_long(h, mmco[i].long_arg, 0);
562 
563  remove_short_at_index(h, j);
564  h->long_ref[ mmco[i].long_arg ]= pic;
565  if (h->long_ref[ mmco[i].long_arg ]){
566  h->long_ref[ mmco[i].long_arg ]->long_ref=1;
567  h->long_ref_count++;
568  }
569  break;
570  case MMCO_LONG2UNUSED:
571  j = pic_num_extract(h, mmco[i].long_arg, &structure);
572  pic = h->long_ref[j];
573  if (pic) {
574  remove_long(h, j, structure ^ PICT_FRAME);
575  } else if(h->avctx->debug&FF_DEBUG_MMCO)
576  av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
577  break;
578  case MMCO_LONG:
579  // Comment below left from previous code as it is an interresting note.
580  /* First field in pair is in short term list or
581  * at a different long term index.
582  * This is not allowed; see 7.4.3.3, notes 2 and 3.
583  * Report the problem and keep the pair where it is,
584  * and mark this field valid.
585  */
586 
587  if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
588  remove_long(h, mmco[i].long_arg, 0);
589  if (remove_short(h, h->cur_pic_ptr->frame_num, 0)) {
590  av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to short and long at the same time\n");
591  }
592 
593  h->long_ref[ mmco[i].long_arg ]= h->cur_pic_ptr;
594  h->long_ref[ mmco[i].long_arg ]->long_ref=1;
595  h->long_ref_count++;
596  }
597 
599  current_ref_assigned=1;
600  break;
601  case MMCO_SET_MAX_LONG:
602  assert(mmco[i].long_arg <= 16);
603  // just remove the long term which index is greater than new max
604  for(j = mmco[i].long_arg; j<16; j++){
605  remove_long(h, j, 0);
606  }
607  break;
608  case MMCO_RESET:
609  while(h->short_ref_count){
610  remove_short(h, h->short_ref[0]->frame_num, 0);
611  }
612  for(j = 0; j < 16; j++) {
613  remove_long(h, j, 0);
614  }
615  h->frame_num=
616  h->cur_pic_ptr->frame_num= 0;
617  h->mmco_reset = 1;
618  h->cur_pic_ptr->mmco_reset=1;
619  for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
620  h->last_pocs[j] = INT_MIN;
621  break;
622  default: assert(0);
623  }
624  }
625 
626  if (!current_ref_assigned) {
627  /* Second field of complementary field pair; the first field of
628  * which is already referenced. If short referenced, it
629  * should be first entry in short_ref. If not, it must exist
630  * in long_ref; trying to put it on the short list here is an
631  * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
632  */
633  if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
634  /* Just mark the second field valid */
636  } else if (h->cur_pic_ptr->long_ref) {
637  av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
638  "assignment for second field "
639  "in complementary field pair "
640  "(first field is long term)\n");
641  err = AVERROR_INVALIDDATA;
642  } else {
643  pic= remove_short(h, h->cur_pic_ptr->frame_num, 0);
644  if(pic){
645  av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
646  err = AVERROR_INVALIDDATA;
647  }
648 
649  if(h->short_ref_count)
650  memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
651 
652  h->short_ref[0]= h->cur_pic_ptr;
653  h->short_ref_count++;
655  }
656  }
657 
659 
660  /* We have too many reference frames, probably due to corrupted
661  * stream. Need to discard one frame. Prevents overrun of the
662  * short_ref and long_ref buffers.
663  */
665  "number of reference frames (%d+%d) exceeds max (%d; probably "
666  "corrupt input), discarding one\n",
668  err = AVERROR_INVALIDDATA;
669 
670  if (h->long_ref_count && !h->short_ref_count) {
671  for (i = 0; i < 16; ++i)
672  if (h->long_ref[i])
673  break;
674 
675  assert(i < 16);
676  remove_long(h, i, 0);
677  } else {
678  pic = h->short_ref[h->short_ref_count - 1];
679  remove_short(h, pic->frame_num, 0);
680  }
681  }
682 
683  print_short_term(h);
684  print_long_term(h);
685 
686  if(err >= 0 && h->long_ref_count==0 && h->short_ref_count<=2 && h->pps.ref_count[0]<=2 + (h->picture_structure != PICT_FRAME) && h->cur_pic_ptr->f.pict_type == AV_PICTURE_TYPE_I){
687  h->cur_pic_ptr->sync |= 1;
688  if(!h->avctx->has_b_frames)
689  h->sync = 2;
690  }
691 
692  return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
693 }
694 
696  int first_slice)
697 {
698  int i, ret;
699  MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = mmco_temp;
700  int mmco_index = 0;
701 
702  if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
703  skip_bits1(gb); // broken_link
704  if (get_bits1(gb)){
705  mmco[0].opcode = MMCO_LONG;
706  mmco[0].long_arg = 0;
707  mmco_index = 1;
708  }
709  } else {
710  if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
711  for (i = 0; i < MAX_MMCO_COUNT; i++) {
712  MMCOOpcode opcode = get_ue_golomb_31(gb);
713 
714  mmco[i].opcode = opcode;
715  if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG){
716  mmco[i].short_pic_num =
717  (h->curr_pic_num - get_ue_golomb(gb) - 1) &
718  (h->max_pic_num - 1);
719 #if 0
720  if (mmco[i].short_pic_num >= h->short_ref_count ||
721  h->short_ref[ mmco[i].short_pic_num ] == NULL){
722  av_log(s->avctx, AV_LOG_ERROR,
723  "illegal short ref in memory management control "
724  "operation %d\n", mmco);
725  return -1;
726  }
727 #endif
728  }
729  if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
730  opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
731  unsigned int long_arg = get_ue_golomb_31(gb);
732  if (long_arg >= 32 ||
733  (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
734  long_arg == 16) &&
735  !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
737  "illegal long ref in memory management control "
738  "operation %d\n", opcode);
739  return -1;
740  }
741  mmco[i].long_arg = long_arg;
742  }
743 
744  if (opcode > (unsigned) MMCO_LONG){
746  "illegal memory management control operation %d\n",
747  opcode);
748  return -1;
749  }
750  if (opcode == MMCO_END)
751  break;
752  }
753  mmco_index = i;
754  } else {
755  if (first_slice) {
756  ret = ff_generate_sliding_window_mmcos(h, first_slice);
757  if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
758  return ret;
759  }
760  mmco_index = -1;
761  }
762  }
763 
764  if (first_slice && mmco_index != -1) {
765  memcpy(h->mmco, mmco_temp, sizeof(h->mmco));
766  h->mmco_index = mmco_index;
767  } else if (!first_slice && mmco_index >= 0 &&
768  (mmco_index != h->mmco_index ||
769  (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
771  "Inconsistent MMCO state between slices [%d, %d]\n",
772  mmco_index, h->mmco_index);
773  return AVERROR_INVALIDDATA;
774  }
775 
776  return 0;
777 }