Libav 0.7.1
libavcodec/h264_refs.c
Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling
00003  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "internal.h"
00029 #include "dsputil.h"
00030 #include "avcodec.h"
00031 #include "h264.h"
00032 #include "golomb.h"
00033 
00034 //#undef NDEBUG
00035 #include <assert.h>
00036 
00037 
00038 static void pic_as_field(Picture *pic, const int parity){
00039     int i;
00040     for (i = 0; i < 4; ++i) {
00041         if (parity == PICT_BOTTOM_FIELD)
00042             pic->data[i] += pic->linesize[i];
00043         pic->reference = parity;
00044         pic->linesize[i] *= 2;
00045     }
00046     pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
00047 }
00048 
00049 static int split_field_copy(Picture *dest, Picture *src,
00050                             int parity, int id_add){
00051     int match = !!(src->reference & parity);
00052 
00053     if (match) {
00054         *dest = *src;
00055         if(parity != PICT_FRAME){
00056             pic_as_field(dest, parity);
00057             dest->pic_id *= 2;
00058             dest->pic_id += id_add;
00059         }
00060     }
00061 
00062     return match;
00063 }
00064 
00065 static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
00066     int i[2]={0};
00067     int index=0;
00068 
00069     while(i[0]<len || i[1]<len){
00070         while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
00071             i[0]++;
00072         while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
00073             i[1]++;
00074         if(i[0] < len){
00075             in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
00076             split_field_copy(&def[index++], in[ i[0]++ ], sel  , 1);
00077         }
00078         if(i[1] < len){
00079             in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
00080             split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
00081         }
00082     }
00083 
00084     return index;
00085 }
00086 
00087 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
00088     int i, best_poc;
00089     int out_i= 0;
00090 
00091     for(;;){
00092         best_poc= dir ? INT_MIN : INT_MAX;
00093 
00094         for(i=0; i<len; i++){
00095             const int poc= src[i]->poc;
00096             if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
00097                 best_poc= poc;
00098                 sorted[out_i]= src[i];
00099             }
00100         }
00101         if(best_poc == (dir ? INT_MIN : INT_MAX))
00102             break;
00103         limit= sorted[out_i++]->poc - dir;
00104     }
00105     return out_i;
00106 }
00107 
00108 int ff_h264_fill_default_ref_list(H264Context *h){
00109     MpegEncContext * const s = &h->s;
00110     int i, len;
00111 
00112     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
00113         Picture *sorted[32];
00114         int cur_poc, list;
00115         int lens[2];
00116 
00117         if(FIELD_PICTURE)
00118             cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
00119         else
00120             cur_poc= s->current_picture_ptr->poc;
00121 
00122         for(list= 0; list<2; list++){
00123             len= add_sorted(sorted    , h->short_ref, h->short_ref_count, cur_poc, 1^list);
00124             len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
00125             assert(len<=32);
00126             len= build_def_list(h->default_ref_list[list]    , sorted     , len, 0, s->picture_structure);
00127             len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
00128             assert(len<=32);
00129 
00130             if(len < h->ref_count[list])
00131                 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
00132             lens[list]= len;
00133         }
00134 
00135         if(lens[0] == lens[1] && lens[1] > 1){
00136             for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
00137             if(i == lens[0])
00138                 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
00139         }
00140     }else{
00141         len = build_def_list(h->default_ref_list[0]    , h->short_ref, h->short_ref_count, 0, s->picture_structure);
00142         len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16                , 1, s->picture_structure);
00143         assert(len <= 32);
00144         if(len < h->ref_count[0])
00145             memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
00146     }
00147 #ifdef TRACE
00148     for (i=0; i<h->ref_count[0]; i++) {
00149         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].data[0]);
00150     }
00151     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
00152         for (i=0; i<h->ref_count[1]; i++) {
00153             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].data[0]);
00154         }
00155     }
00156 #endif
00157     return 0;
00158 }
00159 
00160 static void print_short_term(H264Context *h);
00161 static void print_long_term(H264Context *h);
00162 
00173 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
00174     MpegEncContext * const s = &h->s;
00175 
00176     *structure = s->picture_structure;
00177     if(FIELD_PICTURE){
00178         if (!(pic_num & 1))
00179             /* opposite field */
00180             *structure ^= PICT_FRAME;
00181         pic_num >>= 1;
00182     }
00183 
00184     return pic_num;
00185 }
00186 
00187 int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
00188     MpegEncContext * const s = &h->s;
00189     int list, index, pic_structure;
00190 
00191     print_short_term(h);
00192     print_long_term(h);
00193 
00194     for(list=0; list<h->list_count; list++){
00195         memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
00196 
00197         if(get_bits1(&s->gb)){
00198             int pred= h->curr_pic_num;
00199 
00200             for(index=0; ; index++){
00201                 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
00202                 unsigned int pic_id;
00203                 int i;
00204                 Picture *ref = NULL;
00205 
00206                 if(reordering_of_pic_nums_idc==3)
00207                     break;
00208 
00209                 if(index >= h->ref_count[list]){
00210                     av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
00211                     return -1;
00212                 }
00213 
00214                 if(reordering_of_pic_nums_idc<3){
00215                     if(reordering_of_pic_nums_idc<2){
00216                         const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
00217                         int frame_num;
00218 
00219                         if(abs_diff_pic_num > h->max_pic_num){
00220                             av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
00221                             return -1;
00222                         }
00223 
00224                         if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
00225                         else                                pred+= abs_diff_pic_num;
00226                         pred &= h->max_pic_num - 1;
00227 
00228                         frame_num = pic_num_extract(h, pred, &pic_structure);
00229 
00230                         for(i= h->short_ref_count-1; i>=0; i--){
00231                             ref = h->short_ref[i];
00232                             assert(ref->reference);
00233                             assert(!ref->long_ref);
00234                             if(
00235                                    ref->frame_num == frame_num &&
00236                                    (ref->reference & pic_structure)
00237                               )
00238                                 break;
00239                         }
00240                         if(i>=0)
00241                             ref->pic_id= pred;
00242                     }else{
00243                         int long_idx;
00244                         pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
00245 
00246                         long_idx= pic_num_extract(h, pic_id, &pic_structure);
00247 
00248                         if(long_idx>31){
00249                             av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
00250                             return -1;
00251                         }
00252                         ref = h->long_ref[long_idx];
00253                         assert(!(ref && !ref->reference));
00254                         if(ref && (ref->reference & pic_structure)){
00255                             ref->pic_id= pic_id;
00256                             assert(ref->long_ref);
00257                             i=0;
00258                         }else{
00259                             i=-1;
00260                         }
00261                     }
00262 
00263                     if (i < 0) {
00264                         av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
00265                         memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
00266                     } else {
00267                         for(i=index; i+1<h->ref_count[list]; i++){
00268                             if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
00269                                 break;
00270                         }
00271                         for(; i > index; i--){
00272                             h->ref_list[list][i]= h->ref_list[list][i-1];
00273                         }
00274                         h->ref_list[list][index]= *ref;
00275                         if (FIELD_PICTURE){
00276                             pic_as_field(&h->ref_list[list][index], pic_structure);
00277                         }
00278                     }
00279                 }else{
00280                     av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
00281                     return -1;
00282                 }
00283             }
00284         }
00285     }
00286     for(list=0; list<h->list_count; list++){
00287         for(index= 0; index < h->ref_count[list]; index++){
00288             if(!h->ref_list[list][index].data[0]){
00289                 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
00290                 if(h->default_ref_list[list][0].data[0])
00291                     h->ref_list[list][index]= h->default_ref_list[list][0];
00292                 else
00293                     return -1;
00294             }
00295         }
00296     }
00297 
00298     return 0;
00299 }
00300 
00301 void ff_h264_fill_mbaff_ref_list(H264Context *h){
00302     int list, i, j;
00303     for(list=0; list<2; list++){ //FIXME try list_count
00304         for(i=0; i<h->ref_count[list]; i++){
00305             Picture *frame = &h->ref_list[list][i];
00306             Picture *field = &h->ref_list[list][16+2*i];
00307             field[0] = *frame;
00308             for(j=0; j<3; j++)
00309                 field[0].linesize[j] <<= 1;
00310             field[0].reference = PICT_TOP_FIELD;
00311             field[0].poc= field[0].field_poc[0];
00312             field[1] = field[0];
00313             for(j=0; j<3; j++)
00314                 field[1].data[j] += frame->linesize[j];
00315             field[1].reference = PICT_BOTTOM_FIELD;
00316             field[1].poc= field[1].field_poc[1];
00317 
00318             h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
00319             h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
00320             for(j=0; j<2; j++){
00321                 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];
00322                 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];
00323             }
00324         }
00325     }
00326 }
00327 
00339 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
00340     int i;
00341     if (pic->reference &= refmask) {
00342         return 0;
00343     } else {
00344         for(i = 0; h->delayed_pic[i]; i++)
00345             if(pic == h->delayed_pic[i]){
00346                 pic->reference=DELAYED_PIC_REF;
00347                 break;
00348             }
00349         return 1;
00350     }
00351 }
00352 
00361 static Picture * find_short(H264Context *h, int frame_num, int *idx){
00362     MpegEncContext * const s = &h->s;
00363     int i;
00364 
00365     for(i=0; i<h->short_ref_count; i++){
00366         Picture *pic= h->short_ref[i];
00367         if(s->avctx->debug&FF_DEBUG_MMCO)
00368             av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
00369         if(pic->frame_num == frame_num) {
00370             *idx = i;
00371             return pic;
00372         }
00373     }
00374     return NULL;
00375 }
00376 
00383 static void remove_short_at_index(H264Context *h, int i){
00384     assert(i >= 0 && i < h->short_ref_count);
00385     h->short_ref[i]= NULL;
00386     if (--h->short_ref_count)
00387         memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
00388 }
00389 
00394 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
00395     MpegEncContext * const s = &h->s;
00396     Picture *pic;
00397     int i;
00398 
00399     if(s->avctx->debug&FF_DEBUG_MMCO)
00400         av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
00401 
00402     pic = find_short(h, frame_num, &i);
00403     if (pic){
00404         if(unreference_pic(h, pic, ref_mask))
00405         remove_short_at_index(h, i);
00406     }
00407 
00408     return pic;
00409 }
00410 
00416 static Picture * remove_long(H264Context *h, int i, int ref_mask){
00417     Picture *pic;
00418 
00419     pic= h->long_ref[i];
00420     if (pic){
00421         if(unreference_pic(h, pic, ref_mask)){
00422             assert(h->long_ref[i]->long_ref == 1);
00423             h->long_ref[i]->long_ref= 0;
00424             h->long_ref[i]= NULL;
00425             h->long_ref_count--;
00426         }
00427     }
00428 
00429     return pic;
00430 }
00431 
00432 void ff_h264_remove_all_refs(H264Context *h){
00433     int i;
00434 
00435     for(i=0; i<16; i++){
00436         remove_long(h, i, 0);
00437     }
00438     assert(h->long_ref_count==0);
00439 
00440     for(i=0; i<h->short_ref_count; i++){
00441         unreference_pic(h, h->short_ref[i], 0);
00442         h->short_ref[i]= NULL;
00443     }
00444     h->short_ref_count=0;
00445 }
00446 
00450 static void print_short_term(H264Context *h) {
00451     uint32_t i;
00452     if(h->s.avctx->debug&FF_DEBUG_MMCO) {
00453         av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
00454         for(i=0; i<h->short_ref_count; i++){
00455             Picture *pic= h->short_ref[i];
00456             av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
00457         }
00458     }
00459 }
00460 
00464 static void print_long_term(H264Context *h) {
00465     uint32_t i;
00466     if(h->s.avctx->debug&FF_DEBUG_MMCO) {
00467         av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
00468         for(i = 0; i < 16; i++){
00469             Picture *pic= h->long_ref[i];
00470             if (pic) {
00471                 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
00472             }
00473         }
00474     }
00475 }
00476 
00477 void ff_generate_sliding_window_mmcos(H264Context *h) {
00478     MpegEncContext * const s = &h->s;
00479     assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
00480 
00481     h->mmco_index= 0;
00482     if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
00483             !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
00484         h->mmco[0].opcode= MMCO_SHORT2UNUSED;
00485         h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
00486         h->mmco_index= 1;
00487         if (FIELD_PICTURE) {
00488             h->mmco[0].short_pic_num *= 2;
00489             h->mmco[1].opcode= MMCO_SHORT2UNUSED;
00490             h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
00491             h->mmco_index= 2;
00492         }
00493     }
00494 }
00495 
00496 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
00497     MpegEncContext * const s = &h->s;
00498     int i, av_uninit(j);
00499     int current_ref_assigned=0;
00500     Picture *av_uninit(pic);
00501 
00502     if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
00503         av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
00504 
00505     for(i=0; i<mmco_count; i++){
00506         int av_uninit(structure), av_uninit(frame_num);
00507         if(s->avctx->debug&FF_DEBUG_MMCO)
00508             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);
00509 
00510         if(   mmco[i].opcode == MMCO_SHORT2UNUSED
00511            || mmco[i].opcode == MMCO_SHORT2LONG){
00512             frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
00513             pic = find_short(h, frame_num, &j);
00514             if(!pic){
00515                 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
00516                    || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
00517                 av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
00518                 continue;
00519             }
00520         }
00521 
00522         switch(mmco[i].opcode){
00523         case MMCO_SHORT2UNUSED:
00524             if(s->avctx->debug&FF_DEBUG_MMCO)
00525                 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);
00526             remove_short(h, frame_num, structure ^ PICT_FRAME);
00527             break;
00528         case MMCO_SHORT2LONG:
00529                 if (h->long_ref[mmco[i].long_arg] != pic)
00530                     remove_long(h, mmco[i].long_arg, 0);
00531 
00532                 remove_short_at_index(h, j);
00533                 h->long_ref[ mmco[i].long_arg ]= pic;
00534                 if (h->long_ref[ mmco[i].long_arg ]){
00535                     h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00536                     h->long_ref_count++;
00537                 }
00538             break;
00539         case MMCO_LONG2UNUSED:
00540             j = pic_num_extract(h, mmco[i].long_arg, &structure);
00541             pic = h->long_ref[j];
00542             if (pic) {
00543                 remove_long(h, j, structure ^ PICT_FRAME);
00544             } else if(s->avctx->debug&FF_DEBUG_MMCO)
00545                 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
00546             break;
00547         case MMCO_LONG:
00548                     // Comment below left from previous code as it is an interresting note.
00549                     /* First field in pair is in short term list or
00550                      * at a different long term index.
00551                      * This is not allowed; see 7.4.3.3, notes 2 and 3.
00552                      * Report the problem and keep the pair where it is,
00553                      * and mark this field valid.
00554                      */
00555 
00556             if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
00557                 remove_long(h, mmco[i].long_arg, 0);
00558 
00559                 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
00560                 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00561                 h->long_ref_count++;
00562             }
00563 
00564             s->current_picture_ptr->reference |= s->picture_structure;
00565             current_ref_assigned=1;
00566             break;
00567         case MMCO_SET_MAX_LONG:
00568             assert(mmco[i].long_arg <= 16);
00569             // just remove the long term which index is greater than new max
00570             for(j = mmco[i].long_arg; j<16; j++){
00571                 remove_long(h, j, 0);
00572             }
00573             break;
00574         case MMCO_RESET:
00575             while(h->short_ref_count){
00576                 remove_short(h, h->short_ref[0]->frame_num, 0);
00577             }
00578             for(j = 0; j < 16; j++) {
00579                 remove_long(h, j, 0);
00580             }
00581             s->current_picture_ptr->poc=
00582             s->current_picture_ptr->field_poc[0]=
00583             s->current_picture_ptr->field_poc[1]=
00584             h->poc_lsb=
00585             h->poc_msb=
00586             h->frame_num=
00587             s->current_picture_ptr->frame_num= 0;
00588             s->current_picture_ptr->mmco_reset=1;
00589             break;
00590         default: assert(0);
00591         }
00592     }
00593 
00594     if (!current_ref_assigned) {
00595         /* Second field of complementary field pair; the first field of
00596          * which is already referenced. If short referenced, it
00597          * should be first entry in short_ref. If not, it must exist
00598          * in long_ref; trying to put it on the short list here is an
00599          * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
00600          */
00601         if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
00602             /* Just mark the second field valid */
00603             s->current_picture_ptr->reference = PICT_FRAME;
00604         } else if (s->current_picture_ptr->long_ref) {
00605             av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
00606                                              "assignment for second field "
00607                                              "in complementary field pair "
00608                                              "(first field is long term)\n");
00609         } else {
00610             pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
00611             if(pic){
00612                 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
00613             }
00614 
00615             if(h->short_ref_count)
00616                 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
00617 
00618             h->short_ref[0]= s->current_picture_ptr;
00619             h->short_ref_count++;
00620             s->current_picture_ptr->reference |= s->picture_structure;
00621         }
00622     }
00623 
00624     if (h->long_ref_count + h->short_ref_count -
00625             (h->short_ref[0] == s->current_picture_ptr) > h->sps.ref_frame_count){
00626 
00627         /* We have too many reference frames, probably due to corrupted
00628          * stream. Need to discard one frame. Prevents overrun of the
00629          * short_ref and long_ref buffers.
00630          */
00631         av_log(h->s.avctx, AV_LOG_ERROR,
00632                "number of reference frames (%d+%d) exceeds max (%d; probably "
00633                "corrupt input), discarding one\n",
00634                h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
00635 
00636         if (h->long_ref_count && !h->short_ref_count) {
00637             for (i = 0; i < 16; ++i)
00638                 if (h->long_ref[i])
00639                     break;
00640 
00641             assert(i < 16);
00642             remove_long(h, i, 0);
00643         } else {
00644             pic = h->short_ref[h->short_ref_count - 1];
00645             remove_short(h, pic->frame_num, 0);
00646         }
00647     }
00648 
00649     print_short_term(h);
00650     print_long_term(h);
00651     return 0;
00652 }
00653 
00654 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
00655     MpegEncContext * const s = &h->s;
00656     int i;
00657 
00658     h->mmco_index= 0;
00659     if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
00660         s->broken_link= get_bits1(gb) -1;
00661         if(get_bits1(gb)){
00662             h->mmco[0].opcode= MMCO_LONG;
00663             h->mmco[0].long_arg= 0;
00664             h->mmco_index= 1;
00665         }
00666     }else{
00667         if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
00668             for(i= 0; i<MAX_MMCO_COUNT; i++) {
00669                 MMCOOpcode opcode= get_ue_golomb_31(gb);
00670 
00671                 h->mmco[i].opcode= opcode;
00672                 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
00673                     h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
00674 /*                    if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
00675                         av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
00676                         return -1;
00677                     }*/
00678                 }
00679                 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
00680                     unsigned int long_arg= get_ue_golomb_31(gb);
00681                     if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG && long_arg == 16) && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
00682                         av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
00683                         return -1;
00684                     }
00685                     h->mmco[i].long_arg= long_arg;
00686                 }
00687 
00688                 if(opcode > (unsigned)MMCO_LONG){
00689                     av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
00690                     return -1;
00691                 }
00692                 if(opcode == MMCO_END)
00693                     break;
00694             }
00695             h->mmco_index= i;
00696         }else{
00697             ff_generate_sliding_window_mmcos(h);
00698         }
00699     }
00700 
00701     return 0;
00702 }