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  MpegEncContext * const s = &h->s;
112  int i, len;
113 
115  Picture *sorted[32];
116  int cur_poc, list;
117  int lens[2];
118 
119  if(FIELD_PICTURE)
121  else
122  cur_poc= s->current_picture_ptr->poc;
123 
124  for(list= 0; list<2; list++){
125  len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
126  len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
127  assert(len<=32);
128 
130  sorted, len, 0, s->picture_structure);
131  len += build_def_list(h->default_ref_list[list] + len,
132  FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
133  h->long_ref, 16, 1, s->picture_structure);
134 
135  if(len < h->ref_count[list])
136  memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
137  lens[list]= len;
138  }
139 
140  if(lens[0] == lens[1] && lens[1] > 1){
141  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++);
142  if(i == lens[0])
143  FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
144  }
145  }else{
148  len += build_def_list(h->default_ref_list[0] + len,
149  FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
150  h-> long_ref, 16, 1, s->picture_structure);
151 
152  if(len < h->ref_count[0])
153  memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
154  }
155 #ifdef TRACE
156  for (i=0; i<h->ref_count[0]; i++) {
157  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]);
158  }
160  for (i=0; i<h->ref_count[1]; i++) {
161  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]);
162  }
163  }
164 #endif
165  return 0;
166 }
167 
168 static void print_short_term(H264Context *h);
169 static void print_long_term(H264Context *h);
170 
181 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
182  MpegEncContext * const s = &h->s;
183 
184  *structure = s->picture_structure;
185  if(FIELD_PICTURE){
186  if (!(pic_num & 1))
187  /* opposite field */
188  *structure ^= PICT_FRAME;
189  pic_num >>= 1;
190  }
191 
192  return pic_num;
193 }
194 
196  MpegEncContext * const s = &h->s;
197  int list, index, pic_structure;
198 
199  print_short_term(h);
200  print_long_term(h);
201 
202  for(list=0; list<h->list_count; list++){
203  memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
204 
205  if(get_bits1(&s->gb)){
206  int pred= h->curr_pic_num;
207 
208  for(index=0; ; index++){
209  unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
210  unsigned int pic_id;
211  int i;
212  Picture *ref = NULL;
213 
214  if(reordering_of_pic_nums_idc==3)
215  break;
216 
217  if(index >= h->ref_count[list]){
218  av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
219  return -1;
220  }
221 
222  if(reordering_of_pic_nums_idc<3){
223  if(reordering_of_pic_nums_idc<2){
224  const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
225  int frame_num;
226 
227  if(abs_diff_pic_num > h->max_pic_num){
228  av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
229  return -1;
230  }
231 
232  if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
233  else pred+= abs_diff_pic_num;
234  pred &= h->max_pic_num - 1;
235 
236  frame_num = pic_num_extract(h, pred, &pic_structure);
237 
238  for(i= h->short_ref_count-1; i>=0; i--){
239  ref = h->short_ref[i];
240  assert(ref->f.reference);
241  assert(!ref->long_ref);
242  if(
243  ref->frame_num == frame_num &&
244  (ref->f.reference & pic_structure)
245  )
246  break;
247  }
248  if(i>=0)
249  ref->pic_id= pred;
250  }else{
251  int long_idx;
252  pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
253 
254  long_idx= pic_num_extract(h, pic_id, &pic_structure);
255 
256  if(long_idx>31){
257  av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
258  return -1;
259  }
260  ref = h->long_ref[long_idx];
261  assert(!(ref && !ref->f.reference));
262  if (ref && (ref->f.reference & pic_structure)) {
263  ref->pic_id= pic_id;
264  assert(ref->long_ref);
265  i=0;
266  }else{
267  i=-1;
268  }
269  }
270 
271  if (i < 0) {
272  av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
273  memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
274  } else {
275  for(i=index; i+1<h->ref_count[list]; i++){
276  if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
277  break;
278  }
279  for(; i > index; i--){
280  h->ref_list[list][i]= h->ref_list[list][i-1];
281  }
282  h->ref_list[list][index]= *ref;
283  if (FIELD_PICTURE){
284  pic_as_field(&h->ref_list[list][index], pic_structure);
285  }
286  }
287  }else{
288  av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
289  return -1;
290  }
291  }
292  }
293  }
294  for(list=0; list<h->list_count; list++){
295  for(index= 0; index < h->ref_count[list]; index++){
296  if (!h->ref_list[list][index].f.data[0]) {
297  av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
298  if (h->default_ref_list[list][0].f.data[0])
299  h->ref_list[list][index]= h->default_ref_list[list][0];
300  else
301  return -1;
302  }
303  }
304  }
305 
306  return 0;
307 }
308 
310  int list, i, j;
311  for(list=0; list<2; list++){ //FIXME try list_count
312  for(i=0; i<h->ref_count[list]; i++){
313  Picture *frame = &h->ref_list[list][i];
314  Picture *field = &h->ref_list[list][16+2*i];
315  field[0] = *frame;
316  for(j=0; j<3; j++)
317  field[0].f.linesize[j] <<= 1;
318  field[0].f.reference = PICT_TOP_FIELD;
319  field[0].poc= field[0].field_poc[0];
320  field[1] = field[0];
321  for(j=0; j<3; j++)
322  field[1].f.data[j] += frame->f.linesize[j];
323  field[1].f.reference = PICT_BOTTOM_FIELD;
324  field[1].poc= field[1].field_poc[1];
325 
326  h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
327  h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
328  for(j=0; j<2; j++){
329  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];
330  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];
331  }
332  }
333  }
334 }
335 
347 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
348  int i;
349  if (pic->f.reference &= refmask) {
350  return 0;
351  } else {
352  for(i = 0; h->delayed_pic[i]; i++)
353  if(pic == h->delayed_pic[i]){
354  pic->f.reference = DELAYED_PIC_REF;
355  break;
356  }
357  return 1;
358  }
359 }
360 
369 static Picture * find_short(H264Context *h, int frame_num, int *idx){
370  MpegEncContext * const s = &h->s;
371  int i;
372 
373  for(i=0; i<h->short_ref_count; i++){
374  Picture *pic= h->short_ref[i];
375  if(s->avctx->debug&FF_DEBUG_MMCO)
376  av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
377  if(pic->frame_num == frame_num) {
378  *idx = i;
379  return pic;
380  }
381  }
382  return NULL;
383 }
384 
391 static void remove_short_at_index(H264Context *h, int i){
392  assert(i >= 0 && i < h->short_ref_count);
393  h->short_ref[i]= NULL;
394  if (--h->short_ref_count)
395  memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
396 }
397 
402 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
403  MpegEncContext * const s = &h->s;
404  Picture *pic;
405  int i;
406 
407  if(s->avctx->debug&FF_DEBUG_MMCO)
408  av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
409 
410  pic = find_short(h, frame_num, &i);
411  if (pic){
412  if(unreference_pic(h, pic, ref_mask))
413  remove_short_at_index(h, i);
414  }
415 
416  return pic;
417 }
418 
424 static Picture * remove_long(H264Context *h, int i, int ref_mask){
425  Picture *pic;
426 
427  pic= h->long_ref[i];
428  if (pic){
429  if(unreference_pic(h, pic, ref_mask)){
430  assert(h->long_ref[i]->long_ref == 1);
431  h->long_ref[i]->long_ref= 0;
432  h->long_ref[i]= NULL;
433  h->long_ref_count--;
434  }
435  }
436 
437  return pic;
438 }
439 
441  int i;
442 
443  for(i=0; i<16; i++){
444  remove_long(h, i, 0);
445  }
446  assert(h->long_ref_count==0);
447 
448  for(i=0; i<h->short_ref_count; i++){
449  unreference_pic(h, h->short_ref[i], 0);
450  h->short_ref[i]= NULL;
451  }
452  h->short_ref_count=0;
453 }
454 
458 static void print_short_term(H264Context *h) {
459  uint32_t i;
460  if(h->s.avctx->debug&FF_DEBUG_MMCO) {
461  av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
462  for(i=0; i<h->short_ref_count; i++){
463  Picture *pic= h->short_ref[i];
464  av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
465  i, pic->frame_num, pic->poc, pic->f.data[0]);
466  }
467  }
468 }
469 
473 static void print_long_term(H264Context *h) {
474  uint32_t i;
475  if(h->s.avctx->debug&FF_DEBUG_MMCO) {
476  av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
477  for(i = 0; i < 16; i++){
478  Picture *pic= h->long_ref[i];
479  if (pic) {
480  av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
481  i, pic->frame_num, pic->poc, pic->f.data[0]);
482  }
483  }
484  }
485 }
486 
487 static int check_opcodes(MMCO *mmco1, MMCO *mmco2, int n_mmcos)
488 {
489  int i;
490 
491  for (i = 0; i < n_mmcos; i++) {
492  if (mmco1[i].opcode != mmco2[i].opcode)
493  return -1;
494  }
495 
496  return 0;
497 }
498 
500 {
501  MpegEncContext * const s = &h->s;
502  MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
503  int mmco_index = 0, i;
504 
505  assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
506 
507  if (h->short_ref_count &&
509  !(FIELD_PICTURE && !s->first_field &&
511  mmco[0].opcode = MMCO_SHORT2UNUSED;
512  mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
513  mmco_index = 1;
514  if (FIELD_PICTURE) {
515  mmco[0].short_pic_num *= 2;
516  mmco[1].opcode = MMCO_SHORT2UNUSED;
517  mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
518  mmco_index = 2;
519  }
520  }
521 
522  if (first_slice) {
523  h->mmco_index = mmco_index;
524  } else if (!first_slice && mmco_index >= 0 &&
525  (mmco_index != h->mmco_index ||
526  (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
528  "Inconsistent MMCO state between slices [%d, %d, %d]\n",
529  mmco_index, h->mmco_index, i);
530  return AVERROR_INVALIDDATA;
531  }
532  return 0;
533 }
534 
535 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
536  MpegEncContext * const s = &h->s;
537  int i, av_uninit(j);
538  int current_ref_assigned=0, err=0;
539  Picture *av_uninit(pic);
540 
541  if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
542  av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
543 
544  for(i=0; i<mmco_count; i++){
545  int av_uninit(structure), av_uninit(frame_num);
546  if(s->avctx->debug&FF_DEBUG_MMCO)
547  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);
548 
549  if( mmco[i].opcode == MMCO_SHORT2UNUSED
550  || mmco[i].opcode == MMCO_SHORT2LONG){
551  frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
552  pic = find_short(h, frame_num, &j);
553  if(!pic){
554  if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
555  || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
556  av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
557  err = AVERROR_INVALIDDATA;
558  }
559  continue;
560  }
561  }
562 
563  switch(mmco[i].opcode){
564  case MMCO_SHORT2UNUSED:
565  if(s->avctx->debug&FF_DEBUG_MMCO)
566  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);
567  remove_short(h, frame_num, structure ^ PICT_FRAME);
568  break;
569  case MMCO_SHORT2LONG:
570  if (h->long_ref[mmco[i].long_arg] != pic)
571  remove_long(h, mmco[i].long_arg, 0);
572 
573  remove_short_at_index(h, j);
574  h->long_ref[ mmco[i].long_arg ]= pic;
575  if (h->long_ref[ mmco[i].long_arg ]){
576  h->long_ref[ mmco[i].long_arg ]->long_ref=1;
577  h->long_ref_count++;
578  }
579  break;
580  case MMCO_LONG2UNUSED:
581  j = pic_num_extract(h, mmco[i].long_arg, &structure);
582  pic = h->long_ref[j];
583  if (pic) {
584  remove_long(h, j, structure ^ PICT_FRAME);
585  } else if(s->avctx->debug&FF_DEBUG_MMCO)
586  av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
587  break;
588  case MMCO_LONG:
589  // Comment below left from previous code as it is an interresting note.
590  /* First field in pair is in short term list or
591  * at a different long term index.
592  * This is not allowed; see 7.4.3.3, notes 2 and 3.
593  * Report the problem and keep the pair where it is,
594  * and mark this field valid.
595  */
596 
597  if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
598  remove_long(h, mmco[i].long_arg, 0);
599 
600  h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
601  h->long_ref[ mmco[i].long_arg ]->long_ref=1;
602  h->long_ref_count++;
603  }
604 
606  current_ref_assigned=1;
607  break;
608  case MMCO_SET_MAX_LONG:
609  assert(mmco[i].long_arg <= 16);
610  // just remove the long term which index is greater than new max
611  for(j = mmco[i].long_arg; j<16; j++){
612  remove_long(h, j, 0);
613  }
614  break;
615  case MMCO_RESET:
616  while(h->short_ref_count){
617  remove_short(h, h->short_ref[0]->frame_num, 0);
618  }
619  for(j = 0; j < 16; j++) {
620  remove_long(h, j, 0);
621  }
622  h->frame_num=
624  h->mmco_reset = 1;
626  break;
627  default: assert(0);
628  }
629  }
630 
631  if (!current_ref_assigned) {
632  /* Second field of complementary field pair; the first field of
633  * which is already referenced. If short referenced, it
634  * should be first entry in short_ref. If not, it must exist
635  * in long_ref; trying to put it on the short list here is an
636  * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
637  */
638  if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
639  /* Just mark the second field valid */
641  } else if (s->current_picture_ptr->long_ref) {
642  av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
643  "assignment for second field "
644  "in complementary field pair "
645  "(first field is long term)\n");
646  err = AVERROR_INVALIDDATA;
647  } else {
649  if(pic){
650  av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
651  err = AVERROR_INVALIDDATA;
652  }
653 
654  if(h->short_ref_count)
655  memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
656 
657  h->short_ref[0]= s->current_picture_ptr;
658  h->short_ref_count++;
660  }
661  }
662 
663  if (h->long_ref_count + h->short_ref_count -
664  (h->short_ref[0] == s->current_picture_ptr) > h->sps.ref_frame_count){
665 
666  /* We have too many reference frames, probably due to corrupted
667  * stream. Need to discard one frame. Prevents overrun of the
668  * short_ref and long_ref buffers.
669  */
671  "number of reference frames (%d+%d) exceeds max (%d; probably "
672  "corrupt input), discarding one\n",
674  err = AVERROR_INVALIDDATA;
675 
676  if (h->long_ref_count && !h->short_ref_count) {
677  for (i = 0; i < 16; ++i)
678  if (h->long_ref[i])
679  break;
680 
681  assert(i < 16);
682  remove_long(h, i, 0);
683  } else {
684  pic = h->short_ref[h->short_ref_count - 1];
685  remove_short(h, pic->frame_num, 0);
686  }
687  }
688 
689  print_short_term(h);
690  print_long_term(h);
691  return (h->s.avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
692 }
693 
695  int first_slice)
696 {
697  MpegEncContext * const s = &h->s;
698  int i, ret;
699  MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
700  int mmco_index = 0;
701 
702  if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
703  s->broken_link = get_bits1(gb) - 1;
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){
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 && s->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  h->mmco_index = mmco_index;
766  } else if (!first_slice && mmco_index >= 0 &&
767  (mmco_index != h->mmco_index ||
768  (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
770  "Inconsistent MMCO state between slices [%d, %d, %d]\n",
771  mmco_index, h->mmco_index, i);
772  return AVERROR_INVALIDDATA;
773  }
774 
775  return 0;
776 }