• 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 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, 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] ]->f.reference & sel)))
00071             i[0]++;
00072         while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->f.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].f.data[0] == h->default_ref_list[1][i].f.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->f.reference);
00233                             assert(!ref->long_ref);
00234                             if(
00235                                    ref->frame_num == frame_num &&
00236                                    (ref->f.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->f.reference));
00254                         if (ref && (ref->f.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].f.data[0]) {
00289                 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
00290                 if (h->default_ref_list[list][0].f.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].f.linesize[j] <<= 1;
00310             field[0].f.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].f.data[j] += frame->f.linesize[j];
00315             field[1].f.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->f.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->f.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",
00457                    i, pic->frame_num, pic->poc, pic->f.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",
00473                        i, pic->frame_num, pic->poc, pic->f.data[0]);
00474             }
00475         }
00476     }
00477 }
00478 
00479 void ff_generate_sliding_window_mmcos(H264Context *h) {
00480     MpegEncContext * const s = &h->s;
00481     assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
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             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->f.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             err = AVERROR_INVALIDDATA;
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                 err = AVERROR_INVALIDDATA;
00615             }
00616 
00617             if(h->short_ref_count)
00618                 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
00619 
00620             h->short_ref[0]= s->current_picture_ptr;
00621             h->short_ref_count++;
00622             s->current_picture_ptr->f.reference |= s->picture_structure;
00623         }
00624     }
00625 
00626     if (h->long_ref_count + h->short_ref_count -
00627             (h->short_ref[0] == s->current_picture_ptr) > h->sps.ref_frame_count){
00628 
00629         /* We have too many reference frames, probably due to corrupted
00630          * stream. Need to discard one frame. Prevents overrun of the
00631          * short_ref and long_ref buffers.
00632          */
00633         av_log(h->s.avctx, AV_LOG_ERROR,
00634                "number of reference frames (%d+%d) exceeds max (%d; probably "
00635                "corrupt input), discarding one\n",
00636                h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
00637         err = AVERROR_INVALIDDATA;
00638 
00639         if (h->long_ref_count && !h->short_ref_count) {
00640             for (i = 0; i < 16; ++i)
00641                 if (h->long_ref[i])
00642                     break;
00643 
00644             assert(i < 16);
00645             remove_long(h, i, 0);
00646         } else {
00647             pic = h->short_ref[h->short_ref_count - 1];
00648             remove_short(h, pic->frame_num, 0);
00649         }
00650     }
00651 
00652     print_short_term(h);
00653     print_long_term(h);
00654     return (h->s.avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
00655 }
00656 
00657 int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
00658     MpegEncContext * const s = &h->s;
00659     int i;
00660 
00661     h->mmco_index= 0;
00662     if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
00663         s->broken_link= get_bits1(gb) -1;
00664         if(get_bits1(gb)){
00665             h->mmco[0].opcode= MMCO_LONG;
00666             h->mmco[0].long_arg= 0;
00667             h->mmco_index= 1;
00668         }
00669     }else{
00670         if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
00671             for(i= 0; i<MAX_MMCO_COUNT; i++) {
00672                 MMCOOpcode opcode= get_ue_golomb_31(gb);
00673 
00674                 h->mmco[i].opcode= opcode;
00675                 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
00676                     h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
00677 /*                    if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
00678                         av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
00679                         return -1;
00680                     }*/
00681                 }
00682                 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
00683                     unsigned int long_arg= get_ue_golomb_31(gb);
00684                     if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG && long_arg == 16) && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
00685                         av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
00686                         return -1;
00687                     }
00688                     h->mmco[i].long_arg= long_arg;
00689                 }
00690 
00691                 if(opcode > (unsigned)MMCO_LONG){
00692                     av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
00693                     return -1;
00694                 }
00695                 if(opcode == MMCO_END)
00696                     break;
00697             }
00698             h->mmco_index= i;
00699         }else{
00700             ff_generate_sliding_window_mmcos(h);
00701         }
00702     }
00703 
00704     return 0;
00705 }
Generated on Sat Mar 17 2012 12:57:45 for Libav by doxygen 1.7.1