FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 reference picture handling.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
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 
162 /**
163  * Extract structure information about the picture described by pic_num in
164  * the current decoding context (frame or field). Note that pic_num is
165  * picture number without wrapping (so, 0<=pic_num<max_pic_num).
166  * @param pic_num picture number for which to extract structure information
167  * @param structure one of PICT_XXX describing structure of picture
168  * with pic_num
169  * @return frame number (short term) or long term index of picture
170  * described by pic_num
171  */
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 
327 /**
328  * Mark a picture as no longer needed for reference. The refmask
329  * argument allows unreferencing of individual fields or the whole frame.
330  * If the picture becomes entirely unreferenced, but is being held for
331  * display purposes, it is marked as such.
332  * @param refmask mask of fields to unreference; the mask is bitwise
333  * anded with the reference marking of pic
334  * @return non-zero if pic becomes entirely unreferenced (except possibly
335  * for display purposes) zero if one of the fields remains in
336  * reference
337  */
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 
352 /**
353  * Find a Picture in the short term reference list by frame number.
354  * @param frame_num frame number to search for
355  * @param idx the index into h->short_ref where returned picture is found
356  * undefined if no picture found.
357  * @return pointer to the found picture, or NULL if no pic with the provided
358  * frame number is found
359  */
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 
375 /**
376  * Remove a picture from the short term reference list by its index in
377  * that list. This does no checking on the provided index; it is assumed
378  * to be valid. Other list entries are shifted down.
379  * @param i index into h->short_ref of picture to remove.
380  */
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 
388 /**
389  *
390  * @return the removed picture or NULL if an error occurs
391  */
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 
408 /**
409  * Remove a picture from the long term reference list by its index in
410  * that list.
411  * @return the removed picture or NULL if an error occurs
412  */
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 
447 /**
448  * print short term list
449  */
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 
462 /**
463  * print long term list
464  */
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, AV_LOG_ERROR, "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 
590  h->long_ref[ mmco[i].long_arg ]= h->cur_pic_ptr;
591  h->long_ref[ mmco[i].long_arg ]->long_ref=1;
592  h->long_ref_count++;
593  }
594 
596  current_ref_assigned=1;
597  break;
598  case MMCO_SET_MAX_LONG:
599  assert(mmco[i].long_arg <= 16);
600  // just remove the long term which index is greater than new max
601  for(j = mmco[i].long_arg; j<16; j++){
602  remove_long(h, j, 0);
603  }
604  break;
605  case MMCO_RESET:
606  while(h->short_ref_count){
607  remove_short(h, h->short_ref[0]->frame_num, 0);
608  }
609  for(j = 0; j < 16; j++) {
610  remove_long(h, j, 0);
611  }
612  h->frame_num=
613  h->cur_pic_ptr->frame_num= 0;
614  h->mmco_reset = 1;
615  h->cur_pic_ptr->mmco_reset=1;
616  for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
617  h->last_pocs[j] = INT_MIN;
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] == h->cur_pic_ptr) {
631  /* Just mark the second field valid */
633  } else if (h->cur_pic_ptr->long_ref) {
634  av_log(h->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 {
640  pic= remove_short(h, h->cur_pic_ptr->frame_num, 0);
641  if(pic){
642  av_log(h->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]= h->cur_pic_ptr;
650  h->short_ref_count++;
652  }
653  }
654 
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 
683  if(err >= 0 && h->long_ref_count==0 && h->short_ref_count<=2 && h->pps.ref_count[0]<=1 + (h->picture_structure != PICT_FRAME) && h->cur_pic_ptr->f.pict_type == AV_PICTURE_TYPE_I){
684  h->cur_pic_ptr->sync |= 1;
685  if(!h->avctx->has_b_frames)
686  h->sync = 2;
687  }
688 
689  return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
690 }
691 
693  int first_slice)
694 {
695  int i, ret;
696  MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
697  int mmco_index = 0;
698 
699  if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
700  skip_bits1(gb); // broken_link
701  if (get_bits1(gb)){
702  mmco[0].opcode = MMCO_LONG;
703  mmco[0].long_arg = 0;
704  mmco_index = 1;
705  }
706  } else {
707  if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
708  for (i = 0; i < MAX_MMCO_COUNT; i++) {
709  MMCOOpcode opcode = get_ue_golomb_31(gb);
710 
711  mmco[i].opcode = opcode;
712  if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG){
713  mmco[i].short_pic_num =
714  (h->curr_pic_num - get_ue_golomb(gb) - 1) &
715  (h->max_pic_num - 1);
716 #if 0
717  if (mmco[i].short_pic_num >= h->short_ref_count ||
718  h->short_ref[ mmco[i].short_pic_num ] == NULL){
719  av_log(s->avctx, AV_LOG_ERROR,
720  "illegal short ref in memory management control "
721  "operation %d\n", mmco);
722  return -1;
723  }
724 #endif
725  }
726  if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
727  opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
728  unsigned int long_arg = get_ue_golomb_31(gb);
729  if (long_arg >= 32 ||
730  (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
731  long_arg == 16) &&
732  !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
734  "illegal long ref in memory management control "
735  "operation %d\n", opcode);
736  return -1;
737  }
738  mmco[i].long_arg = long_arg;
739  }
740 
741  if (opcode > (unsigned) MMCO_LONG){
743  "illegal memory management control operation %d\n",
744  opcode);
745  return -1;
746  }
747  if (opcode == MMCO_END)
748  break;
749  }
750  mmco_index = i;
751  } else {
752  if (first_slice) {
753  ret = ff_generate_sliding_window_mmcos(h, first_slice);
754  if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
755  return ret;
756  }
757  mmco_index = -1;
758  }
759  }
760 
761  if (first_slice && mmco_index != -1) {
762  h->mmco_index = mmco_index;
763  } else if (!first_slice && mmco_index >= 0 &&
764  (mmco_index != h->mmco_index ||
765  (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
767  "Inconsistent MMCO state between slices [%d, %d]\n",
768  mmco_index, h->mmco_index);
769  return AVERROR_INVALIDDATA;
770  }
771 
772  return 0;
773 }