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->f.data[i] += pic->f.linesize[i];
00043         pic->f.reference    = parity;
00044         pic->f.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->f.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, int def_len,
00066                           Picture **in, int len, int is_long, int sel)
00067 {
00068     int i[2]={0};
00069     int index=0;
00070 
00071     while ((i[0] < len || i[1] < len) && index < def_len) {
00072         while (i[0] < len && !(in[ i[0] ] && (in[ i[0] ]->f.reference & sel)))
00073             i[0]++;
00074         while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->f.reference & (sel^3))))
00075             i[1]++;
00076         if (i[0] < len && index < def_len) {
00077             in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
00078             split_field_copy(&def[index++], in[ i[0]++ ], sel  , 1);
00079         }
00080         if (i[1] < len && index < def_len) {
00081             in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
00082             split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
00083         }
00084     }
00085 
00086     return index;
00087 }
00088 
00089 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
00090     int i, best_poc;
00091     int out_i= 0;
00092 
00093     for(;;){
00094         best_poc= dir ? INT_MIN : INT_MAX;
00095 
00096         for(i=0; i<len; i++){
00097             const int poc= src[i]->poc;
00098             if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
00099                 best_poc= poc;
00100                 sorted[out_i]= src[i];
00101             }
00102         }
00103         if(best_poc == (dir ? INT_MIN : INT_MAX))
00104             break;
00105         limit= sorted[out_i++]->poc - dir;
00106     }
00107     return out_i;
00108 }
00109 
00110 int ff_h264_fill_default_ref_list(H264Context *h){
00111     MpegEncContext * const s = &h->s;
00112     int i, len;
00113 
00114     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
00115         Picture *sorted[32];
00116         int cur_poc, list;
00117         int lens[2];
00118 
00119         if(FIELD_PICTURE)
00120             cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
00121         else
00122             cur_poc= s->current_picture_ptr->poc;
00123 
00124         for(list= 0; list<2; list++){
00125             len= add_sorted(sorted    , h->short_ref, h->short_ref_count, cur_poc, 1^list);
00126             len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
00127             assert(len<=32);
00128 
00129             len  = build_def_list(h->default_ref_list[list], FF_ARRAY_ELEMS(h->default_ref_list[0]),
00130                                   sorted, len, 0, s->picture_structure);
00131             len += build_def_list(h->default_ref_list[list] + len,
00132                                   FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
00133                                   h->long_ref, 16, 1, s->picture_structure);
00134 
00135             if(len < h->ref_count[list])
00136                 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
00137             lens[list]= len;
00138         }
00139 
00140         if(lens[0] == lens[1] && lens[1] > 1){
00141             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++);
00142             if(i == lens[0])
00143                 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
00144         }
00145     }else{
00146         len  = build_def_list(h->default_ref_list[0], FF_ARRAY_ELEMS(h->default_ref_list[0]),
00147                               h->short_ref, h->short_ref_count, 0, s->picture_structure);
00148         len += build_def_list(h->default_ref_list[0] + len,
00149                               FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
00150                               h-> long_ref, 16, 1, s->picture_structure);
00151 
00152         if(len < h->ref_count[0])
00153             memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
00154     }
00155 #ifdef TRACE
00156     for (i=0; i<h->ref_count[0]; i++) {
00157         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]);
00158     }
00159     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
00160         for (i=0; i<h->ref_count[1]; i++) {
00161             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]);
00162         }
00163     }
00164 #endif
00165     return 0;
00166 }
00167 
00168 static void print_short_term(H264Context *h);
00169 static void print_long_term(H264Context *h);
00170 
00181 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
00182     MpegEncContext * const s = &h->s;
00183 
00184     *structure = s->picture_structure;
00185     if(FIELD_PICTURE){
00186         if (!(pic_num & 1))
00187             /* opposite field */
00188             *structure ^= PICT_FRAME;
00189         pic_num >>= 1;
00190     }
00191 
00192     return pic_num;
00193 }
00194 
00195 int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
00196     MpegEncContext * const s = &h->s;
00197     int list, index, pic_structure;
00198 
00199     print_short_term(h);
00200     print_long_term(h);
00201 
00202     for(list=0; list<h->list_count; list++){
00203         memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
00204 
00205         if(get_bits1(&s->gb)){
00206             int pred= h->curr_pic_num;
00207 
00208             for(index=0; ; index++){
00209                 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
00210                 unsigned int pic_id;
00211                 int i;
00212                 Picture *ref = NULL;
00213 
00214                 if(reordering_of_pic_nums_idc==3)
00215                     break;
00216 
00217                 if(index >= h->ref_count[list]){
00218                     av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
00219                     return -1;
00220                 }
00221 
00222                 if(reordering_of_pic_nums_idc<3){
00223                     if(reordering_of_pic_nums_idc<2){
00224                         const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
00225                         int frame_num;
00226 
00227                         if(abs_diff_pic_num > h->max_pic_num){
00228                             av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
00229                             return -1;
00230                         }
00231 
00232                         if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
00233                         else                                pred+= abs_diff_pic_num;
00234                         pred &= h->max_pic_num - 1;
00235 
00236                         frame_num = pic_num_extract(h, pred, &pic_structure);
00237 
00238                         for(i= h->short_ref_count-1; i>=0; i--){
00239                             ref = h->short_ref[i];
00240                             assert(ref->f.reference);
00241                             assert(!ref->long_ref);
00242                             if(
00243                                    ref->frame_num == frame_num &&
00244                                    (ref->f.reference & pic_structure)
00245                               )
00246                                 break;
00247                         }
00248                         if(i>=0)
00249                             ref->pic_id= pred;
00250                     }else{
00251                         int long_idx;
00252                         pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
00253 
00254                         long_idx= pic_num_extract(h, pic_id, &pic_structure);
00255 
00256                         if(long_idx>31){
00257                             av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
00258                             return -1;
00259                         }
00260                         ref = h->long_ref[long_idx];
00261                         assert(!(ref && !ref->f.reference));
00262                         if (ref && (ref->f.reference & pic_structure)) {
00263                             ref->pic_id= pic_id;
00264                             assert(ref->long_ref);
00265                             i=0;
00266                         }else{
00267                             i=-1;
00268                         }
00269                     }
00270 
00271                     if (i < 0) {
00272                         av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
00273                         memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
00274                     } else {
00275                         for(i=index; i+1<h->ref_count[list]; i++){
00276                             if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
00277                                 break;
00278                         }
00279                         for(; i > index; i--){
00280                             h->ref_list[list][i]= h->ref_list[list][i-1];
00281                         }
00282                         h->ref_list[list][index]= *ref;
00283                         if (FIELD_PICTURE){
00284                             pic_as_field(&h->ref_list[list][index], pic_structure);
00285                         }
00286                     }
00287                 }else{
00288                     av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
00289                     return -1;
00290                 }
00291             }
00292         }
00293     }
00294     for(list=0; list<h->list_count; list++){
00295         for(index= 0; index < h->ref_count[list]; index++){
00296             if (!h->ref_list[list][index].f.data[0]) {
00297                 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
00298                 if (h->default_ref_list[list][0].f.data[0])
00299                     h->ref_list[list][index]= h->default_ref_list[list][0];
00300                 else
00301                     return -1;
00302             }
00303         }
00304     }
00305 
00306     return 0;
00307 }
00308 
00309 void ff_h264_fill_mbaff_ref_list(H264Context *h){
00310     int list, i, j;
00311     for(list=0; list<2; list++){ //FIXME try list_count
00312         for(i=0; i<h->ref_count[list]; i++){
00313             Picture *frame = &h->ref_list[list][i];
00314             Picture *field = &h->ref_list[list][16+2*i];
00315             field[0] = *frame;
00316             for(j=0; j<3; j++)
00317                 field[0].f.linesize[j] <<= 1;
00318             field[0].f.reference = PICT_TOP_FIELD;
00319             field[0].poc= field[0].field_poc[0];
00320             field[1] = field[0];
00321             for(j=0; j<3; j++)
00322                 field[1].f.data[j] += frame->f.linesize[j];
00323             field[1].f.reference = PICT_BOTTOM_FIELD;
00324             field[1].poc= field[1].field_poc[1];
00325 
00326             h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
00327             h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
00328             for(j=0; j<2; j++){
00329                 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];
00330                 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];
00331             }
00332         }
00333     }
00334 }
00335 
00347 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
00348     int i;
00349     if (pic->f.reference &= refmask) {
00350         return 0;
00351     } else {
00352         for(i = 0; h->delayed_pic[i]; i++)
00353             if(pic == h->delayed_pic[i]){
00354                 pic->f.reference = DELAYED_PIC_REF;
00355                 break;
00356             }
00357         return 1;
00358     }
00359 }
00360 
00369 static Picture * find_short(H264Context *h, int frame_num, int *idx){
00370     MpegEncContext * const s = &h->s;
00371     int i;
00372 
00373     for(i=0; i<h->short_ref_count; i++){
00374         Picture *pic= h->short_ref[i];
00375         if(s->avctx->debug&FF_DEBUG_MMCO)
00376             av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
00377         if(pic->frame_num == frame_num) {
00378             *idx = i;
00379             return pic;
00380         }
00381     }
00382     return NULL;
00383 }
00384 
00391 static void remove_short_at_index(H264Context *h, int i){
00392     assert(i >= 0 && i < h->short_ref_count);
00393     h->short_ref[i]= NULL;
00394     if (--h->short_ref_count)
00395         memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
00396 }
00397 
00402 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
00403     MpegEncContext * const s = &h->s;
00404     Picture *pic;
00405     int i;
00406 
00407     if(s->avctx->debug&FF_DEBUG_MMCO)
00408         av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
00409 
00410     pic = find_short(h, frame_num, &i);
00411     if (pic){
00412         if(unreference_pic(h, pic, ref_mask))
00413         remove_short_at_index(h, i);
00414     }
00415 
00416     return pic;
00417 }
00418 
00424 static Picture * remove_long(H264Context *h, int i, int ref_mask){
00425     Picture *pic;
00426 
00427     pic= h->long_ref[i];
00428     if (pic){
00429         if(unreference_pic(h, pic, ref_mask)){
00430             assert(h->long_ref[i]->long_ref == 1);
00431             h->long_ref[i]->long_ref= 0;
00432             h->long_ref[i]= NULL;
00433             h->long_ref_count--;
00434         }
00435     }
00436 
00437     return pic;
00438 }
00439 
00440 void ff_h264_remove_all_refs(H264Context *h){
00441     int i;
00442 
00443     for(i=0; i<16; i++){
00444         remove_long(h, i, 0);
00445     }
00446     assert(h->long_ref_count==0);
00447 
00448     for(i=0; i<h->short_ref_count; i++){
00449         unreference_pic(h, h->short_ref[i], 0);
00450         h->short_ref[i]= NULL;
00451     }
00452     h->short_ref_count=0;
00453 }
00454 
00458 static void print_short_term(H264Context *h) {
00459     uint32_t i;
00460     if(h->s.avctx->debug&FF_DEBUG_MMCO) {
00461         av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
00462         for(i=0; i<h->short_ref_count; i++){
00463             Picture *pic= h->short_ref[i];
00464             av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
00465                    i, pic->frame_num, pic->poc, pic->f.data[0]);
00466         }
00467     }
00468 }
00469 
00473 static void print_long_term(H264Context *h) {
00474     uint32_t i;
00475     if(h->s.avctx->debug&FF_DEBUG_MMCO) {
00476         av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
00477         for(i = 0; i < 16; i++){
00478             Picture *pic= h->long_ref[i];
00479             if (pic) {
00480                 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
00481                        i, pic->frame_num, pic->poc, pic->f.data[0]);
00482             }
00483         }
00484     }
00485 }
00486 
00487 void ff_generate_sliding_window_mmcos(H264Context *h) {
00488     MpegEncContext * const s = &h->s;
00489     assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
00490 
00491     h->mmco_index= 0;
00492     if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
00493             !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->f.reference)) {
00494         h->mmco[0].opcode= MMCO_SHORT2UNUSED;
00495         h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
00496         h->mmco_index= 1;
00497         if (FIELD_PICTURE) {
00498             h->mmco[0].short_pic_num *= 2;
00499             h->mmco[1].opcode= MMCO_SHORT2UNUSED;
00500             h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
00501             h->mmco_index= 2;
00502         }
00503     }
00504 }
00505 
00506 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
00507     MpegEncContext * const s = &h->s;
00508     int i, av_uninit(j);
00509     int current_ref_assigned=0, err=0;
00510     Picture *av_uninit(pic);
00511 
00512     if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
00513         av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
00514 
00515     for(i=0; i<mmco_count; i++){
00516         int av_uninit(structure), av_uninit(frame_num);
00517         if(s->avctx->debug&FF_DEBUG_MMCO)
00518             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);
00519 
00520         if(   mmco[i].opcode == MMCO_SHORT2UNUSED
00521            || mmco[i].opcode == MMCO_SHORT2LONG){
00522             frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
00523             pic = find_short(h, frame_num, &j);
00524             if(!pic){
00525                 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
00526                    || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
00527                     av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
00528                     err = AVERROR_INVALIDDATA;
00529                 }
00530                 continue;
00531             }
00532         }
00533 
00534         switch(mmco[i].opcode){
00535         case MMCO_SHORT2UNUSED:
00536             if(s->avctx->debug&FF_DEBUG_MMCO)
00537                 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);
00538             remove_short(h, frame_num, structure ^ PICT_FRAME);
00539             break;
00540         case MMCO_SHORT2LONG:
00541                 if (h->long_ref[mmco[i].long_arg] != pic)
00542                     remove_long(h, mmco[i].long_arg, 0);
00543 
00544                 remove_short_at_index(h, j);
00545                 h->long_ref[ mmco[i].long_arg ]= pic;
00546                 if (h->long_ref[ mmco[i].long_arg ]){
00547                     h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00548                     h->long_ref_count++;
00549                 }
00550             break;
00551         case MMCO_LONG2UNUSED:
00552             j = pic_num_extract(h, mmco[i].long_arg, &structure);
00553             pic = h->long_ref[j];
00554             if (pic) {
00555                 remove_long(h, j, structure ^ PICT_FRAME);
00556             } else if(s->avctx->debug&FF_DEBUG_MMCO)
00557                 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
00558             break;
00559         case MMCO_LONG:
00560                     // Comment below left from previous code as it is an interresting note.
00561                     /* First field in pair is in short term list or
00562                      * at a different long term index.
00563                      * This is not allowed; see 7.4.3.3, notes 2 and 3.
00564                      * Report the problem and keep the pair where it is,
00565                      * and mark this field valid.
00566                      */
00567 
00568             if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
00569                 remove_long(h, mmco[i].long_arg, 0);
00570 
00571                 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
00572                 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00573                 h->long_ref_count++;
00574             }
00575 
00576             s->current_picture_ptr->f.reference |= s->picture_structure;
00577             current_ref_assigned=1;
00578             break;
00579         case MMCO_SET_MAX_LONG:
00580             assert(mmco[i].long_arg <= 16);
00581             // just remove the long term which index is greater than new max
00582             for(j = mmco[i].long_arg; j<16; j++){
00583                 remove_long(h, j, 0);
00584             }
00585             break;
00586         case MMCO_RESET:
00587             while(h->short_ref_count){
00588                 remove_short(h, h->short_ref[0]->frame_num, 0);
00589             }
00590             for(j = 0; j < 16; j++) {
00591                 remove_long(h, j, 0);
00592             }
00593             h->frame_num=
00594             s->current_picture_ptr->frame_num= 0;
00595             h->mmco_reset = 1;
00596             s->current_picture_ptr->mmco_reset=1;
00597             break;
00598         default: assert(0);
00599         }
00600     }
00601 
00602     if (!current_ref_assigned) {
00603         /* Second field of complementary field pair; the first field of
00604          * which is already referenced. If short referenced, it
00605          * should be first entry in short_ref. If not, it must exist
00606          * in long_ref; trying to put it on the short list here is an
00607          * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
00608          */
00609         if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
00610             /* Just mark the second field valid */
00611             s->current_picture_ptr->f.reference = PICT_FRAME;
00612         } else if (s->current_picture_ptr->long_ref) {
00613             av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
00614                                              "assignment for second field "
00615                                              "in complementary field pair "
00616                                              "(first field is long term)\n");
00617             err = AVERROR_INVALIDDATA;
00618         } else {
00619             pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
00620             if(pic){
00621                 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
00622                 err = AVERROR_INVALIDDATA;
00623             }
00624 
00625             if(h->short_ref_count)
00626                 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
00627 
00628             h->short_ref[0]= s->current_picture_ptr;
00629             h->short_ref_count++;
00630             s->current_picture_ptr->f.reference |= s->picture_structure;
00631         }
00632     }
00633 
00634     if (h->long_ref_count + h->short_ref_count -
00635             (h->short_ref[0] == s->current_picture_ptr) > h->sps.ref_frame_count){
00636 
00637         /* We have too many reference frames, probably due to corrupted
00638          * stream. Need to discard one frame. Prevents overrun of the
00639          * short_ref and long_ref buffers.
00640          */
00641         av_log(h->s.avctx, AV_LOG_ERROR,
00642                "number of reference frames (%d+%d) exceeds max (%d; probably "
00643                "corrupt input), discarding one\n",
00644                h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
00645         err = AVERROR_INVALIDDATA;
00646 
00647         if (h->long_ref_count && !h->short_ref_count) {
00648             for (i = 0; i < 16; ++i)
00649                 if (h->long_ref[i])
00650                     break;
00651 
00652             assert(i < 16);
00653             remove_long(h, i, 0);
00654         } else {
00655             pic = h->short_ref[h->short_ref_count - 1];
00656             remove_short(h, pic->frame_num, 0);
00657         }
00658     }
00659 
00660     print_short_term(h);
00661     print_long_term(h);
00662     return (h->s.avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
00663 }
00664 
00665 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
00666     MpegEncContext * const s = &h->s;
00667     int i;
00668 
00669     h->mmco_index= 0;
00670     if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
00671         s->broken_link= get_bits1(gb) -1;
00672         if(get_bits1(gb)){
00673             h->mmco[0].opcode= MMCO_LONG;
00674             h->mmco[0].long_arg= 0;
00675             h->mmco_index= 1;
00676         }
00677     }else{
00678         if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
00679             for(i= 0; i<MAX_MMCO_COUNT; i++) {
00680                 MMCOOpcode opcode= get_ue_golomb_31(gb);
00681 
00682                 h->mmco[i].opcode= opcode;
00683                 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
00684                     h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
00685 /*                    if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
00686                         av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
00687                         return -1;
00688                     }*/
00689                 }
00690                 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
00691                     unsigned int long_arg= get_ue_golomb_31(gb);
00692                     if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG && long_arg == 16) && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
00693                         av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
00694                         return -1;
00695                     }
00696                     h->mmco[i].long_arg= long_arg;
00697                 }
00698 
00699                 if(opcode > (unsigned)MMCO_LONG){
00700                     av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
00701                     return -1;
00702                 }
00703                 if(opcode == MMCO_END)
00704                     break;
00705             }
00706             h->mmco_index= i;
00707         }else{
00708             ff_generate_sliding_window_mmcos(h);
00709         }
00710     }
00711 
00712     return 0;
00713 }