2 * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
24 * H.264 / AVC / MPEG4 part10 reference picture handling.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "libavutil/avassert.h"
39 static void pic_as_field(Picture *pic, const int parity){
41 for (i = 0; i < 4; ++i) {
42 if (parity == PICT_BOTTOM_FIELD)
43 pic->f.data[i] += pic->f.linesize[i];
44 pic->f.reference = parity;
45 pic->f.linesize[i] *= 2;
47 pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
50 static int split_field_copy(Picture *dest, Picture *src,
51 int parity, int id_add){
52 int match = !!(src->f.reference & parity);
56 if(parity != PICT_FRAME){
57 pic_as_field(dest, parity);
59 dest->pic_id += id_add;
66 static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
70 while(i[0]<len || i[1]<len){
71 while (i[0] < len && !(in[ i[0] ] && (in[ i[0] ]->f.reference & sel)))
73 while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->f.reference & (sel^3))))
76 in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
77 split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
80 in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
81 split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
88 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
93 best_poc= dir ? INT_MIN : INT_MAX;
96 const int poc= src[i]->poc;
97 if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
99 sorted[out_i]= src[i];
102 if(best_poc == (dir ? INT_MIN : INT_MAX))
104 limit= sorted[out_i++]->poc - dir;
109 int ff_h264_fill_default_ref_list(H264Context *h){
110 MpegEncContext * const s = &h->s;
113 if(h->slice_type_nos==AV_PICTURE_TYPE_B){
119 cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
121 cur_poc= s->current_picture_ptr->poc;
123 for(list= 0; list<2; list++){
124 len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
125 len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
127 len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
128 len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
131 if(len < h->ref_count[list])
132 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
136 if(lens[0] == lens[1] && lens[1] > 1){
137 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++);
139 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
142 len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
143 len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
145 if(len < h->ref_count[0])
146 memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
149 for (i=0; i<h->ref_count[0]; i++) {
150 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]);
152 if(h->slice_type_nos==AV_PICTURE_TYPE_B){
153 for (i=0; i<h->ref_count[1]; i++) {
154 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]);
161 static void print_short_term(H264Context *h);
162 static void print_long_term(H264Context *h);
165 * Extract structure information about the picture described by pic_num in
166 * the current decoding context (frame or field). Note that pic_num is
167 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
168 * @param pic_num picture number for which to extract structure information
169 * @param structure one of PICT_XXX describing structure of picture
171 * @return frame number (short term) or long term index of picture
172 * described by pic_num
174 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
175 MpegEncContext * const s = &h->s;
177 *structure = s->picture_structure;
181 *structure ^= PICT_FRAME;
188 int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
189 MpegEncContext * const s = &h->s;
190 int list, index, pic_structure;
195 for(list=0; list<h->list_count; list++){
196 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
198 if(get_bits1(&s->gb)){
199 int pred= h->curr_pic_num;
201 for(index=0; ; index++){
202 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
207 if(reordering_of_pic_nums_idc==3)
210 if(index >= h->ref_count[list]){
211 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
215 if(reordering_of_pic_nums_idc<3){
216 if(reordering_of_pic_nums_idc<2){
217 const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
220 if(abs_diff_pic_num > h->max_pic_num){
221 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
225 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
226 else pred+= abs_diff_pic_num;
227 pred &= h->max_pic_num - 1;
229 frame_num = pic_num_extract(h, pred, &pic_structure);
231 for(i= h->short_ref_count-1; i>=0; i--){
232 ref = h->short_ref[i];
233 assert(ref->f.reference);
234 assert(!ref->long_ref);
236 ref->frame_num == frame_num &&
237 (ref->f.reference & pic_structure)
245 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
247 long_idx= pic_num_extract(h, pic_id, &pic_structure);
250 av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
253 ref = h->long_ref[long_idx];
254 assert(!(ref && !ref->f.reference));
255 if (ref && (ref->f.reference & pic_structure)) {
257 assert(ref->long_ref);
265 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
266 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
268 for(i=index; i+1<h->ref_count[list]; i++){
269 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
272 for(; i > index; i--){
273 h->ref_list[list][i]= h->ref_list[list][i-1];
275 h->ref_list[list][index]= *ref;
277 pic_as_field(&h->ref_list[list][index], pic_structure);
281 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
287 for(list=0; list<h->list_count; list++){
288 for(index= 0; index < h->ref_count[list]; index++){
289 if (!h->ref_list[list][index].f.data[0]) {
291 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture, default is %d\n", h->default_ref_list[list][0].poc);
292 for (i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
293 h->last_pocs[i] = INT_MIN;
294 if (h->default_ref_list[list][0].f.data[0])
295 h->ref_list[list][index]= h->default_ref_list[list][0];
305 void ff_h264_fill_mbaff_ref_list(H264Context *h){
307 for(list=0; list<h->list_count; list++){
308 for(i=0; i<h->ref_count[list]; i++){
309 Picture *frame = &h->ref_list[list][i];
310 Picture *field = &h->ref_list[list][16+2*i];
313 field[0].f.linesize[j] <<= 1;
314 field[0].f.reference = PICT_TOP_FIELD;
315 field[0].poc= field[0].field_poc[0];
318 field[1].f.data[j] += frame->f.linesize[j];
319 field[1].f.reference = PICT_BOTTOM_FIELD;
320 field[1].poc= field[1].field_poc[1];
322 h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
323 h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
325 h->chroma_weight[16+2*i][list][j][0] = h->chroma_weight[16+2*i+1][list][j][0] = h->chroma_weight[i][list][j][0];
326 h->chroma_weight[16+2*i][list][j][1] = h->chroma_weight[16+2*i+1][list][j][1] = h->chroma_weight[i][list][j][1];
333 * Mark a picture as no longer needed for reference. The refmask
334 * argument allows unreferencing of individual fields or the whole frame.
335 * If the picture becomes entirely unreferenced, but is being held for
336 * display purposes, it is marked as such.
337 * @param refmask mask of fields to unreference; the mask is bitwise
338 * anded with the reference marking of pic
339 * @return non-zero if pic becomes entirely unreferenced (except possibly
340 * for display purposes) zero if one of the fields remains in
343 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
345 if (pic->f.reference &= refmask) {
348 for(i = 0; h->delayed_pic[i]; i++)
349 if(pic == h->delayed_pic[i]){
350 pic->f.reference = DELAYED_PIC_REF;
358 * Find a Picture in the short term reference list by frame number.
359 * @param frame_num frame number to search for
360 * @param idx the index into h->short_ref where returned picture is found
361 * undefined if no picture found.
362 * @return pointer to the found picture, or NULL if no pic with the provided
363 * frame number is found
365 static Picture * find_short(H264Context *h, int frame_num, int *idx){
366 MpegEncContext * const s = &h->s;
369 for(i=0; i<h->short_ref_count; i++){
370 Picture *pic= h->short_ref[i];
371 if(s->avctx->debug&FF_DEBUG_MMCO)
372 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
373 if(pic->frame_num == frame_num) {
382 * Remove a picture from the short term reference list by its index in
383 * that list. This does no checking on the provided index; it is assumed
384 * to be valid. Other list entries are shifted down.
385 * @param i index into h->short_ref of picture to remove.
387 static void remove_short_at_index(H264Context *h, int i){
388 assert(i >= 0 && i < h->short_ref_count);
389 h->short_ref[i]= NULL;
390 if (--h->short_ref_count)
391 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
396 * @return the removed picture or NULL if an error occurs
398 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
399 MpegEncContext * const s = &h->s;
403 if(s->avctx->debug&FF_DEBUG_MMCO)
404 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
406 pic = find_short(h, frame_num, &i);
408 if(unreference_pic(h, pic, ref_mask))
409 remove_short_at_index(h, i);
416 * Remove a picture from the long term reference list by its index in
418 * @return the removed picture or NULL if an error occurs
420 static Picture * remove_long(H264Context *h, int i, int ref_mask){
425 if(unreference_pic(h, pic, ref_mask)){
426 assert(h->long_ref[i]->long_ref == 1);
427 h->long_ref[i]->long_ref= 0;
428 h->long_ref[i]= NULL;
436 void ff_h264_remove_all_refs(H264Context *h){
440 remove_long(h, i, 0);
442 assert(h->long_ref_count==0);
444 for(i=0; i<h->short_ref_count; i++){
445 unreference_pic(h, h->short_ref[i], 0);
446 h->short_ref[i]= NULL;
448 h->short_ref_count=0;
450 memset(h->default_ref_list, 0, sizeof(h->default_ref_list));
451 memset(h->ref_list, 0, sizeof(h->ref_list));
455 * print short term list
457 static void print_short_term(H264Context *h) {
459 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
460 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
461 for(i=0; i<h->short_ref_count; i++){
462 Picture *pic= h->short_ref[i];
463 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
464 i, pic->frame_num, pic->poc, pic->f.data[0]);
470 * print long term list
472 static void print_long_term(H264Context *h) {
474 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
475 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
476 for(i = 0; i < 16; i++){
477 Picture *pic= h->long_ref[i];
479 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
480 i, pic->frame_num, pic->poc, pic->f.data[0]);
486 static int check_opcodes(MMCO *mmco1, MMCO *mmco2, int n_mmcos)
490 for (i = 0; i < n_mmcos; i++) {
491 if (mmco1[i].opcode != mmco2[i].opcode) {
492 av_log(NULL, AV_LOG_ERROR, "MMCO opcode [%d, %d] at %d mismatches between slices\n",
493 mmco1[i].opcode, mmco2[i].opcode, i);
501 int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice)
503 MpegEncContext * const s = &h->s;
504 MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
505 int mmco_index = 0, i;
507 if (h->short_ref_count &&
508 h->long_ref_count + h->short_ref_count >= h->sps.ref_frame_count &&
509 !(FIELD_PICTURE && !s->first_field &&
510 s->current_picture_ptr->f.reference)) {
511 mmco[0].opcode = MMCO_SHORT2UNUSED;
512 mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
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;
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)))) {
527 av_log(h->s.avctx, AV_LOG_ERROR,
528 "Inconsistent MMCO state between slices [%d, %d]\n",
529 mmco_index, h->mmco_index);
530 return AVERROR_INVALIDDATA;
535 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
536 MpegEncContext * const s = &h->s;
538 int current_ref_assigned=0, err=0;
539 Picture *av_uninit(pic);
541 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
542 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
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);
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);
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;
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);
569 case MMCO_SHORT2LONG:
570 if (h->long_ref[mmco[i].long_arg] != pic)
571 remove_long(h, mmco[i].long_arg, 0);
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;
580 case MMCO_LONG2UNUSED:
581 j = pic_num_extract(h, mmco[i].long_arg, &structure);
582 pic = h->long_ref[j];
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");
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.
597 if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
598 remove_long(h, mmco[i].long_arg, 0);
600 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
601 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
605 s->current_picture_ptr->f.reference |= s->picture_structure;
606 current_ref_assigned=1;
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);
616 while(h->short_ref_count){
617 remove_short(h, h->short_ref[0]->frame_num, 0);
619 for(j = 0; j < 16; j++) {
620 remove_long(h, j, 0);
623 s->current_picture_ptr->frame_num= 0;
625 s->current_picture_ptr->mmco_reset=1;
626 for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
627 h->last_pocs[j] = INT_MIN;
633 if (!current_ref_assigned) {
634 /* Second field of complementary field pair; the first field of
635 * which is already referenced. If short referenced, it
636 * should be first entry in short_ref. If not, it must exist
637 * in long_ref; trying to put it on the short list here is an
638 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
640 if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
641 /* Just mark the second field valid */
642 s->current_picture_ptr->f.reference = PICT_FRAME;
643 } else if (s->current_picture_ptr->long_ref) {
644 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
645 "assignment for second field "
646 "in complementary field pair "
647 "(first field is long term)\n");
648 err = AVERROR_INVALIDDATA;
650 pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
652 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
653 err = AVERROR_INVALIDDATA;
656 if(h->short_ref_count)
657 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
659 h->short_ref[0]= s->current_picture_ptr;
660 h->short_ref_count++;
661 s->current_picture_ptr->f.reference |= s->picture_structure;
665 if (h->long_ref_count + h->short_ref_count > FFMAX(h->sps.ref_frame_count, 1)){
667 /* We have too many reference frames, probably due to corrupted
668 * stream. Need to discard one frame. Prevents overrun of the
669 * short_ref and long_ref buffers.
671 av_log(h->s.avctx, AV_LOG_ERROR,
672 "number of reference frames (%d+%d) exceeds max (%d; probably "
673 "corrupt input), discarding one\n",
674 h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
675 err = AVERROR_INVALIDDATA;
677 if (h->long_ref_count && !h->short_ref_count) {
678 for (i = 0; i < 16; ++i)
683 remove_long(h, i, 0);
685 pic = h->short_ref[h->short_ref_count - 1];
686 remove_short(h, pic->frame_num, 0);
693 if(err >= 0 && h->long_ref_count==0 && h->short_ref_count<=2 && h->pps.ref_count[0]<=1 + (s->picture_structure != PICT_FRAME) && s->current_picture_ptr->f.pict_type == AV_PICTURE_TYPE_I){
694 s->current_picture_ptr->sync |= 1;
695 if(!h->s.avctx->has_b_frames)
699 return (h->s.avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
702 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
705 MpegEncContext * const s = &h->s;
707 MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
710 if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
711 s->broken_link = get_bits1(gb) - 1;
713 mmco[0].opcode = MMCO_LONG;
714 mmco[0].long_arg = 0;
718 if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
719 for (i = 0; i < MAX_MMCO_COUNT; i++) {
720 MMCOOpcode opcode = get_ue_golomb_31(gb);
722 mmco[i].opcode = opcode;
723 if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG){
724 mmco[i].short_pic_num =
725 (h->curr_pic_num - get_ue_golomb(gb) - 1) &
726 (h->max_pic_num - 1);
728 if (mmco[i].short_pic_num >= h->short_ref_count ||
729 h->short_ref[ mmco[i].short_pic_num ] == NULL){
730 av_log(s->avctx, AV_LOG_ERROR,
731 "illegal short ref in memory management control "
732 "operation %d\n", mmco);
737 if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
738 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
739 unsigned int long_arg = get_ue_golomb_31(gb);
740 if (long_arg >= 32 ||
741 (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
743 !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
744 av_log(h->s.avctx, AV_LOG_ERROR,
745 "illegal long ref in memory management control "
746 "operation %d\n", opcode);
749 mmco[i].long_arg = long_arg;
752 if (opcode > (unsigned) MMCO_LONG){
753 av_log(h->s.avctx, AV_LOG_ERROR,
754 "illegal memory management control operation %d\n",
758 if (opcode == MMCO_END)
764 ret = ff_generate_sliding_window_mmcos(h, first_slice);
765 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
772 if (first_slice && mmco_index != -1) {
773 h->mmco_index = mmco_index;
774 } else if (!first_slice && mmco_index >= 0 &&
775 (mmco_index != h->mmco_index ||
776 (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
777 av_log(h->s.avctx, AV_LOG_ERROR,
778 "Inconsistent MMCO state between slices [%d, %d]\n",
779 mmco_index, h->mmco_index);
780 return AVERROR_INVALIDDATA;