• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "libavutil/avassert.h"
00029 #include "internal.h"
00030 #include "dsputil.h"
00031 #include "avcodec.h"
00032 #include "h264.h"
00033 #include "golomb.h"
00034 
00035 //#undef NDEBUG
00036 #include <assert.h>
00037 
00038 
00039 static void pic_as_field(Picture *pic, const int parity){
00040     int i;
00041     for (i = 0; i < 4; ++i) {
00042         if (parity == PICT_BOTTOM_FIELD)
00043             pic->data[i] += pic->linesize[i];
00044         pic->reference = parity;
00045         pic->linesize[i] *= 2;
00046     }
00047     pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
00048 }
00049 
00050 static int split_field_copy(Picture *dest, Picture *src,
00051                             int parity, int id_add){
00052     int match = !!(src->reference & parity);
00053 
00054     if (match) {
00055         *dest = *src;
00056         if(parity != PICT_FRAME){
00057             pic_as_field(dest, parity);
00058             dest->pic_id *= 2;
00059             dest->pic_id += id_add;
00060         }
00061     }
00062 
00063     return match;
00064 }
00065 
00066 static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
00067     int i[2]={0};
00068     int index=0;
00069 
00070     while(i[0]<len || i[1]<len){
00071         while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
00072             i[0]++;
00073         while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
00074             i[1]++;
00075         if(i[0] < len){
00076             in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
00077             split_field_copy(&def[index++], in[ i[0]++ ], sel  , 1);
00078         }
00079         if(i[1] < len){
00080             in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
00081             split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
00082         }
00083     }
00084 
00085     return index;
00086 }
00087 
00088 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
00089     int i, best_poc;
00090     int out_i= 0;
00091 
00092     for(;;){
00093         best_poc= dir ? INT_MIN : INT_MAX;
00094 
00095         for(i=0; i<len; i++){
00096             const int poc= src[i]->poc;
00097             if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
00098                 best_poc= poc;
00099                 sorted[out_i]= src[i];
00100             }
00101         }
00102         if(best_poc == (dir ? INT_MIN : INT_MAX))
00103             break;
00104         limit= sorted[out_i++]->poc - dir;
00105     }
00106     return out_i;
00107 }
00108 
00109 int ff_h264_fill_default_ref_list(H264Context *h){
00110     MpegEncContext * const s = &h->s;
00111     int i, len;
00112 
00113     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
00114         Picture *sorted[32];
00115         int cur_poc, list;
00116         int lens[2];
00117 
00118         if(FIELD_PICTURE)
00119             cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
00120         else
00121             cur_poc= s->current_picture_ptr->poc;
00122 
00123         for(list= 0; list<2; list++){
00124             len= add_sorted(sorted    , h->short_ref, h->short_ref_count, cur_poc, 1^list);
00125             len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
00126             assert(len<=32);
00127             len= build_def_list(h->default_ref_list[list]    , sorted     , len, 0, s->picture_structure);
00128             len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
00129             assert(len<=32);
00130 
00131             if(len < h->ref_count[list])
00132                 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
00133             lens[list]= len;
00134         }
00135 
00136         if(lens[0] == lens[1] && lens[1] > 1){
00137             for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
00138             if(i == lens[0])
00139                 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
00140         }
00141     }else{
00142         len = build_def_list(h->default_ref_list[0]    , h->short_ref, h->short_ref_count, 0, s->picture_structure);
00143         len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16                , 1, s->picture_structure);
00144         assert(len <= 32);
00145         if(len < h->ref_count[0])
00146             memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
00147     }
00148 #ifdef TRACE
00149     for (i=0; i<h->ref_count[0]; i++) {
00150         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]);
00151     }
00152     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
00153         for (i=0; i<h->ref_count[1]; i++) {
00154             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]);
00155         }
00156     }
00157 #endif
00158     return 0;
00159 }
00160 
00161 static void print_short_term(H264Context *h);
00162 static void print_long_term(H264Context *h);
00163 
00174 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
00175     MpegEncContext * const s = &h->s;
00176 
00177     *structure = s->picture_structure;
00178     if(FIELD_PICTURE){
00179         if (!(pic_num & 1))
00180             /* opposite field */
00181             *structure ^= PICT_FRAME;
00182         pic_num >>= 1;
00183     }
00184 
00185     return pic_num;
00186 }
00187 
00188 int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
00189     MpegEncContext * const s = &h->s;
00190     int list, index, pic_structure;
00191 
00192     print_short_term(h);
00193     print_long_term(h);
00194 
00195     for(list=0; list<h->list_count; list++){
00196         memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
00197 
00198         if(get_bits1(&s->gb)){
00199             int pred= h->curr_pic_num;
00200 
00201             for(index=0; ; index++){
00202                 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
00203                 unsigned int pic_id;
00204                 int i;
00205                 Picture *ref = NULL;
00206 
00207                 if(reordering_of_pic_nums_idc==3)
00208                     break;
00209 
00210                 if(index >= h->ref_count[list]){
00211                     av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
00212                     return -1;
00213                 }
00214 
00215                 if(reordering_of_pic_nums_idc<3){
00216                     if(reordering_of_pic_nums_idc<2){
00217                         const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
00218                         int frame_num;
00219 
00220                         if(abs_diff_pic_num > h->max_pic_num){
00221                             av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
00222                             return -1;
00223                         }
00224 
00225                         if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
00226                         else                                pred+= abs_diff_pic_num;
00227                         pred &= h->max_pic_num - 1;
00228 
00229                         frame_num = pic_num_extract(h, pred, &pic_structure);
00230 
00231                         for(i= h->short_ref_count-1; i>=0; i--){
00232                             ref = h->short_ref[i];
00233                             assert(ref->reference);
00234                             assert(!ref->long_ref);
00235                             if(
00236                                    ref->frame_num == frame_num &&
00237                                    (ref->reference & pic_structure)
00238                               )
00239                                 break;
00240                         }
00241                         if(i>=0)
00242                             ref->pic_id= pred;
00243                     }else{
00244                         int long_idx;
00245                         pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
00246 
00247                         long_idx= pic_num_extract(h, pic_id, &pic_structure);
00248 
00249                         if(long_idx>31){
00250                             av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
00251                             return -1;
00252                         }
00253                         ref = h->long_ref[long_idx];
00254                         assert(!(ref && !ref->reference));
00255                         if(ref && (ref->reference & pic_structure)){
00256                             ref->pic_id= pic_id;
00257                             assert(ref->long_ref);
00258                             i=0;
00259                         }else{
00260                             i=-1;
00261                         }
00262                     }
00263 
00264                     if (i < 0) {
00265                         av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
00266                         memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
00267                     } else {
00268                         for(i=index; i+1<h->ref_count[list]; i++){
00269                             if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
00270                                 break;
00271                         }
00272                         for(; i > index; i--){
00273                             h->ref_list[list][i]= h->ref_list[list][i-1];
00274                         }
00275                         h->ref_list[list][index]= *ref;
00276                         if (FIELD_PICTURE){
00277                             pic_as_field(&h->ref_list[list][index], pic_structure);
00278                         }
00279                     }
00280                 }else{
00281                     av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
00282                     return -1;
00283                 }
00284             }
00285         }
00286     }
00287     for(list=0; list<h->list_count; list++){
00288         for(index= 0; index < h->ref_count[list]; index++){
00289             if(!h->ref_list[list][index].data[0]){
00290                 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
00291                 if(h->default_ref_list[list][0].data[0])
00292                     h->ref_list[list][index]= h->default_ref_list[list][0];
00293                 else
00294                     return -1;
00295             }
00296         }
00297     }
00298 
00299     return 0;
00300 }
00301 
00302 void ff_h264_fill_mbaff_ref_list(H264Context *h){
00303     int list, i, j;
00304     for(list=0; list<h->list_count; list++){
00305         for(i=0; i<h->ref_count[list]; i++){
00306             Picture *frame = &h->ref_list[list][i];
00307             Picture *field = &h->ref_list[list][16+2*i];
00308             field[0] = *frame;
00309             for(j=0; j<3; j++)
00310                 field[0].linesize[j] <<= 1;
00311             field[0].reference = PICT_TOP_FIELD;
00312             field[0].poc= field[0].field_poc[0];
00313             field[1] = field[0];
00314             for(j=0; j<3; j++)
00315                 field[1].data[j] += frame->linesize[j];
00316             field[1].reference = PICT_BOTTOM_FIELD;
00317             field[1].poc= field[1].field_poc[1];
00318 
00319             h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
00320             h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
00321             for(j=0; j<2; j++){
00322                 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];
00323                 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];
00324             }
00325         }
00326     }
00327 }
00328 
00340 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
00341     int i;
00342     if (pic->reference &= refmask) {
00343         return 0;
00344     } else {
00345         for(i = 0; h->delayed_pic[i]; i++)
00346             if(pic == h->delayed_pic[i]){
00347                 pic->reference=DELAYED_PIC_REF;
00348                 break;
00349             }
00350         return 1;
00351     }
00352 }
00353 
00362 static Picture * find_short(H264Context *h, int frame_num, int *idx){
00363     MpegEncContext * const s = &h->s;
00364     int i;
00365 
00366     for(i=0; i<h->short_ref_count; i++){
00367         Picture *pic= h->short_ref[i];
00368         if(s->avctx->debug&FF_DEBUG_MMCO)
00369             av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
00370         if(pic->frame_num == frame_num) {
00371             *idx = i;
00372             return pic;
00373         }
00374     }
00375     return NULL;
00376 }
00377 
00384 static void remove_short_at_index(H264Context *h, int i){
00385     assert(i >= 0 && i < h->short_ref_count);
00386     h->short_ref[i]= NULL;
00387     if (--h->short_ref_count)
00388         memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
00389 }
00390 
00395 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
00396     MpegEncContext * const s = &h->s;
00397     Picture *pic;
00398     int i;
00399 
00400     if(s->avctx->debug&FF_DEBUG_MMCO)
00401         av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
00402 
00403     pic = find_short(h, frame_num, &i);
00404     if (pic){
00405         if(unreference_pic(h, pic, ref_mask))
00406         remove_short_at_index(h, i);
00407     }
00408 
00409     return pic;
00410 }
00411 
00417 static Picture * remove_long(H264Context *h, int i, int ref_mask){
00418     Picture *pic;
00419 
00420     pic= h->long_ref[i];
00421     if (pic){
00422         if(unreference_pic(h, pic, ref_mask)){
00423             assert(h->long_ref[i]->long_ref == 1);
00424             h->long_ref[i]->long_ref= 0;
00425             h->long_ref[i]= NULL;
00426             h->long_ref_count--;
00427         }
00428     }
00429 
00430     return pic;
00431 }
00432 
00433 void ff_h264_remove_all_refs(H264Context *h){
00434     int i;
00435 
00436     for(i=0; i<16; i++){
00437         remove_long(h, i, 0);
00438     }
00439     assert(h->long_ref_count==0);
00440 
00441     for(i=0; i<h->short_ref_count; i++){
00442         unreference_pic(h, h->short_ref[i], 0);
00443         h->short_ref[i]= NULL;
00444     }
00445     h->short_ref_count=0;
00446 }
00447 
00451 static void print_short_term(H264Context *h) {
00452     uint32_t i;
00453     if(h->s.avctx->debug&FF_DEBUG_MMCO) {
00454         av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
00455         for(i=0; i<h->short_ref_count; i++){
00456             Picture *pic= h->short_ref[i];
00457             av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
00458         }
00459     }
00460 }
00461 
00465 static void print_long_term(H264Context *h) {
00466     uint32_t i;
00467     if(h->s.avctx->debug&FF_DEBUG_MMCO) {
00468         av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
00469         for(i = 0; i < 16; i++){
00470             Picture *pic= h->long_ref[i];
00471             if (pic) {
00472                 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
00473             }
00474         }
00475     }
00476 }
00477 
00478 void ff_generate_sliding_window_mmcos(H264Context *h) {
00479     MpegEncContext * const s = &h->s;
00480     av_assert0(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
00481 
00482     h->mmco_index= 0;
00483     if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
00484             !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
00485         h->mmco[0].opcode= MMCO_SHORT2UNUSED;
00486         h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
00487         h->mmco_index= 1;
00488         if (FIELD_PICTURE) {
00489             h->mmco[0].short_pic_num *= 2;
00490             h->mmco[1].opcode= MMCO_SHORT2UNUSED;
00491             h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
00492             h->mmco_index= 2;
00493         }
00494     }
00495 }
00496 
00497 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
00498     MpegEncContext * const s = &h->s;
00499     int i, av_uninit(j);
00500     int current_ref_assigned=0;
00501     Picture *av_uninit(pic);
00502 
00503     if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
00504         av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
00505 
00506     for(i=0; i<mmco_count; i++){
00507         int av_uninit(structure), av_uninit(frame_num);
00508         if(s->avctx->debug&FF_DEBUG_MMCO)
00509             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);
00510 
00511         if(   mmco[i].opcode == MMCO_SHORT2UNUSED
00512            || mmco[i].opcode == MMCO_SHORT2LONG){
00513             frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
00514             pic = find_short(h, frame_num, &j);
00515             if(!pic){
00516                 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
00517                    || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
00518                 av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
00519                 continue;
00520             }
00521         }
00522 
00523         switch(mmco[i].opcode){
00524         case MMCO_SHORT2UNUSED:
00525             if(s->avctx->debug&FF_DEBUG_MMCO)
00526                 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);
00527             remove_short(h, frame_num, structure ^ PICT_FRAME);
00528             break;
00529         case MMCO_SHORT2LONG:
00530                 if (h->long_ref[mmco[i].long_arg] != pic)
00531                     remove_long(h, mmco[i].long_arg, 0);
00532 
00533                 remove_short_at_index(h, j);
00534                 h->long_ref[ mmco[i].long_arg ]= pic;
00535                 if (h->long_ref[ mmco[i].long_arg ]){
00536                     h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00537                     h->long_ref_count++;
00538                 }
00539             break;
00540         case MMCO_LONG2UNUSED:
00541             j = pic_num_extract(h, mmco[i].long_arg, &structure);
00542             pic = h->long_ref[j];
00543             if (pic) {
00544                 remove_long(h, j, structure ^ PICT_FRAME);
00545             } else if(s->avctx->debug&FF_DEBUG_MMCO)
00546                 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
00547             break;
00548         case MMCO_LONG:
00549                     // Comment below left from previous code as it is an interresting note.
00550                     /* First field in pair is in short term list or
00551                      * at a different long term index.
00552                      * This is not allowed; see 7.4.3.3, notes 2 and 3.
00553                      * Report the problem and keep the pair where it is,
00554                      * and mark this field valid.
00555                      */
00556 
00557             if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
00558                 remove_long(h, mmco[i].long_arg, 0);
00559 
00560                 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
00561                 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00562                 h->long_ref_count++;
00563             }
00564 
00565             s->current_picture_ptr->reference |= s->picture_structure;
00566             current_ref_assigned=1;
00567             break;
00568         case MMCO_SET_MAX_LONG:
00569             assert(mmco[i].long_arg <= 16);
00570             // just remove the long term which index is greater than new max
00571             for(j = mmco[i].long_arg; j<16; j++){
00572                 remove_long(h, j, 0);
00573             }
00574             break;
00575         case MMCO_RESET:
00576             while(h->short_ref_count){
00577                 remove_short(h, h->short_ref[0]->frame_num, 0);
00578             }
00579             for(j = 0; j < 16; j++) {
00580                 remove_long(h, j, 0);
00581             }
00582             s->current_picture_ptr->poc=
00583             s->current_picture_ptr->field_poc[0]=
00584             s->current_picture_ptr->field_poc[1]=
00585             h->poc_lsb=
00586             h->poc_msb=
00587             h->frame_num=
00588             s->current_picture_ptr->frame_num= 0;
00589             s->current_picture_ptr->mmco_reset=1;
00590             break;
00591         default: assert(0);
00592         }
00593     }
00594 
00595     if (!current_ref_assigned) {
00596         /* Second field of complementary field pair; the first field of
00597          * which is already referenced. If short referenced, it
00598          * should be first entry in short_ref. If not, it must exist
00599          * in long_ref; trying to put it on the short list here is an
00600          * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
00601          */
00602         if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
00603             /* Just mark the second field valid */
00604             s->current_picture_ptr->reference = PICT_FRAME;
00605         } else if (s->current_picture_ptr->long_ref) {
00606             av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
00607                                              "assignment for second field "
00608                                              "in complementary field pair "
00609                                              "(first field is long term)\n");
00610         } else {
00611             pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
00612             if(pic){
00613                 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
00614             }
00615 
00616             if(h->short_ref_count)
00617                 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
00618 
00619             h->short_ref[0]= s->current_picture_ptr;
00620             h->short_ref_count++;
00621             s->current_picture_ptr->reference |= s->picture_structure;
00622         }
00623     }
00624 
00625     if (h->long_ref_count + h->short_ref_count > FFMAX(h->sps.ref_frame_count, 1)){
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 }

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1