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

Generated on Fri Sep 16 2011 17:17:37 for FFmpeg by  doxygen 1.7.1