• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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->f.data[i] += pic->f.linesize[i];
00044         pic->f.reference    = parity;
00045         pic->f.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->f.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] ]->f.reference & sel)))
00072             i[0]++;
00073         while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->f.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].f.data[0] == h->default_ref_list[1][i].f.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->f.reference);
00234                             assert(!ref->long_ref);
00235                             if(
00236                                    ref->frame_num == frame_num &&
00237                                    (ref->f.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->f.reference));
00255                         if (ref && (ref->f.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].f.data[0]) {
00290                 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
00291                 if (h->default_ref_list[list][0].f.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].f.linesize[j] <<= 1;
00311             field[0].f.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].f.data[j] += frame->f.linesize[j];
00316             field[1].f.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->f.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->f.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",
00458                    i, pic->frame_num, pic->poc, pic->f.data[0]);
00459         }
00460     }
00461 }
00462 
00466 static void print_long_term(H264Context *h) {
00467     uint32_t i;
00468     if(h->s.avctx->debug&FF_DEBUG_MMCO) {
00469         av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
00470         for(i = 0; i < 16; i++){
00471             Picture *pic= h->long_ref[i];
00472             if (pic) {
00473                 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
00474                        i, pic->frame_num, pic->poc, pic->f.data[0]);
00475             }
00476         }
00477     }
00478 }
00479 
00480 void ff_generate_sliding_window_mmcos(H264Context *h) {
00481     MpegEncContext * const s = &h->s;
00482 
00483     h->mmco_index= 0;
00484     if(h->short_ref_count && h->long_ref_count + h->short_ref_count >= h->sps.ref_frame_count &&
00485             !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->f.reference)) {
00486         h->mmco[0].opcode= MMCO_SHORT2UNUSED;
00487         h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
00488         h->mmco_index= 1;
00489         if (FIELD_PICTURE) {
00490             h->mmco[0].short_pic_num *= 2;
00491             h->mmco[1].opcode= MMCO_SHORT2UNUSED;
00492             h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
00493             h->mmco_index= 2;
00494         }
00495     }
00496 }
00497 
00498 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
00499     MpegEncContext * const s = &h->s;
00500     int i, av_uninit(j);
00501     int current_ref_assigned=0, err=0;
00502     Picture *av_uninit(pic);
00503 
00504     if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
00505         av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
00506 
00507     for(i=0; i<mmco_count; i++){
00508         int av_uninit(structure), av_uninit(frame_num);
00509         if(s->avctx->debug&FF_DEBUG_MMCO)
00510             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);
00511 
00512         if(   mmco[i].opcode == MMCO_SHORT2UNUSED
00513            || mmco[i].opcode == MMCO_SHORT2LONG){
00514             frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
00515             pic = find_short(h, frame_num, &j);
00516             if(!pic){
00517                 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
00518                    || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
00519                     av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
00520                     err = AVERROR_INVALIDDATA;
00521                 }
00522                 continue;
00523             }
00524         }
00525 
00526         switch(mmco[i].opcode){
00527         case MMCO_SHORT2UNUSED:
00528             if(s->avctx->debug&FF_DEBUG_MMCO)
00529                 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);
00530             remove_short(h, frame_num, structure ^ PICT_FRAME);
00531             break;
00532         case MMCO_SHORT2LONG:
00533                 if (h->long_ref[mmco[i].long_arg] != pic)
00534                     remove_long(h, mmco[i].long_arg, 0);
00535 
00536                 remove_short_at_index(h, j);
00537                 h->long_ref[ mmco[i].long_arg ]= pic;
00538                 if (h->long_ref[ mmco[i].long_arg ]){
00539                     h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00540                     h->long_ref_count++;
00541                 }
00542             break;
00543         case MMCO_LONG2UNUSED:
00544             j = pic_num_extract(h, mmco[i].long_arg, &structure);
00545             pic = h->long_ref[j];
00546             if (pic) {
00547                 remove_long(h, j, structure ^ PICT_FRAME);
00548             } else if(s->avctx->debug&FF_DEBUG_MMCO)
00549                 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
00550             break;
00551         case MMCO_LONG:
00552                     // Comment below left from previous code as it is an interresting note.
00553                     /* First field in pair is in short term list or
00554                      * at a different long term index.
00555                      * This is not allowed; see 7.4.3.3, notes 2 and 3.
00556                      * Report the problem and keep the pair where it is,
00557                      * and mark this field valid.
00558                      */
00559 
00560             if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
00561                 remove_long(h, mmco[i].long_arg, 0);
00562 
00563                 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
00564                 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
00565                 h->long_ref_count++;
00566             }
00567 
00568             s->current_picture_ptr->f.reference |= s->picture_structure;
00569             current_ref_assigned=1;
00570             break;
00571         case MMCO_SET_MAX_LONG:
00572             assert(mmco[i].long_arg <= 16);
00573             // just remove the long term which index is greater than new max
00574             for(j = mmco[i].long_arg; j<16; j++){
00575                 remove_long(h, j, 0);
00576             }
00577             break;
00578         case MMCO_RESET:
00579             while(h->short_ref_count){
00580                 remove_short(h, h->short_ref[0]->frame_num, 0);
00581             }
00582             for(j = 0; j < 16; j++) {
00583                 remove_long(h, j, 0);
00584             }
00585             h->frame_num=
00586             s->current_picture_ptr->frame_num= 0;
00587             h->mmco_reset = 1;
00588             s->current_picture_ptr->mmco_reset=1;
00589             for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
00590                 h->last_pocs[j] = INT_MIN;
00591             break;
00592         default: assert(0);
00593         }
00594     }
00595 
00596     if (!current_ref_assigned) {
00597         /* Second field of complementary field pair; the first field of
00598          * which is already referenced. If short referenced, it
00599          * should be first entry in short_ref. If not, it must exist
00600          * in long_ref; trying to put it on the short list here is an
00601          * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
00602          */
00603         if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
00604             /* Just mark the second field valid */
00605             s->current_picture_ptr->f.reference = PICT_FRAME;
00606         } else if (s->current_picture_ptr->long_ref) {
00607             av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
00608                                              "assignment for second field "
00609                                              "in complementary field pair "
00610                                              "(first field is long term)\n");
00611             err = AVERROR_INVALIDDATA;
00612         } else {
00613             pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
00614             if(pic){
00615                 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
00616                 err = AVERROR_INVALIDDATA;
00617             }
00618 
00619             if(h->short_ref_count)
00620                 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
00621 
00622             h->short_ref[0]= s->current_picture_ptr;
00623             h->short_ref_count++;
00624             s->current_picture_ptr->f.reference |= s->picture_structure;
00625         }
00626     }
00627 
00628     if (h->long_ref_count + h->short_ref_count > FFMAX(h->sps.ref_frame_count, 1)){
00629 
00630         /* We have too many reference frames, probably due to corrupted
00631          * stream. Need to discard one frame. Prevents overrun of the
00632          * short_ref and long_ref buffers.
00633          */
00634         av_log(h->s.avctx, AV_LOG_ERROR,
00635                "number of reference frames (%d+%d) exceeds max (%d; probably "
00636                "corrupt input), discarding one\n",
00637                h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
00638         err = AVERROR_INVALIDDATA;
00639 
00640         if (h->long_ref_count && !h->short_ref_count) {
00641             for (i = 0; i < 16; ++i)
00642                 if (h->long_ref[i])
00643                     break;
00644 
00645             assert(i < 16);
00646             remove_long(h, i, 0);
00647         } else {
00648             pic = h->short_ref[h->short_ref_count - 1];
00649             remove_short(h, pic->frame_num, 0);
00650         }
00651     }
00652 
00653     print_short_term(h);
00654     print_long_term(h);
00655 
00656     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){
00657         s->current_picture_ptr->sync |= 1;
00658         if(!h->s.avctx->has_b_frames)
00659             h->sync = 2;
00660     }
00661 
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 }
Generated on Fri Feb 1 2013 14:34:36 for FFmpeg by doxygen 1.7.1