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

libavcodec/h264_sei.c

Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
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 "avcodec.h"
00030 #include "h264.h"
00031 #include "golomb.h"
00032 
00033 //#undef NDEBUG
00034 #include <assert.h>
00035 
00036 static const uint8_t sei_num_clock_ts_table[9]={
00037     1,  1,  1,  2,  2,  3,  3,  2,  3
00038 };
00039 
00040 void ff_h264_reset_sei(H264Context *h) {
00041     h->sei_recovery_frame_cnt       = -1;
00042     h->sei_dpb_output_delay         =  0;
00043     h->sei_cpb_removal_delay        = -1;
00044     h->sei_buffering_period_present =  0;
00045 }
00046 
00047 static int decode_picture_timing(H264Context *h){
00048     MpegEncContext * const s = &h->s;
00049     if(h->sps.nal_hrd_parameters_present_flag || h->sps.vcl_hrd_parameters_present_flag){
00050         h->sei_cpb_removal_delay = get_bits(&s->gb, h->sps.cpb_removal_delay_length);
00051         h->sei_dpb_output_delay = get_bits(&s->gb, h->sps.dpb_output_delay_length);
00052     }
00053     if(h->sps.pic_struct_present_flag){
00054         unsigned int i, num_clock_ts;
00055         h->sei_pic_struct = get_bits(&s->gb, 4);
00056         h->sei_ct_type    = 0;
00057 
00058         if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
00059             return -1;
00060 
00061         num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
00062 
00063         for (i = 0 ; i < num_clock_ts ; i++){
00064             if(get_bits(&s->gb, 1)){                  /* clock_timestamp_flag */
00065                 unsigned int full_timestamp_flag;
00066                 h->sei_ct_type |= 1<<get_bits(&s->gb, 2);
00067                 skip_bits(&s->gb, 1);                 /* nuit_field_based_flag */
00068                 skip_bits(&s->gb, 5);                 /* counting_type */
00069                 full_timestamp_flag = get_bits(&s->gb, 1);
00070                 skip_bits(&s->gb, 1);                 /* discontinuity_flag */
00071                 skip_bits(&s->gb, 1);                 /* cnt_dropped_flag */
00072                 skip_bits(&s->gb, 8);                 /* n_frames */
00073                 if(full_timestamp_flag){
00074                     skip_bits(&s->gb, 6);             /* seconds_value 0..59 */
00075                     skip_bits(&s->gb, 6);             /* minutes_value 0..59 */
00076                     skip_bits(&s->gb, 5);             /* hours_value 0..23 */
00077                 }else{
00078                     if(get_bits(&s->gb, 1)){          /* seconds_flag */
00079                         skip_bits(&s->gb, 6);         /* seconds_value range 0..59 */
00080                         if(get_bits(&s->gb, 1)){      /* minutes_flag */
00081                             skip_bits(&s->gb, 6);     /* minutes_value 0..59 */
00082                             if(get_bits(&s->gb, 1))   /* hours_flag */
00083                                 skip_bits(&s->gb, 5); /* hours_value 0..23 */
00084                         }
00085                     }
00086                 }
00087                 if(h->sps.time_offset_length > 0)
00088                     skip_bits(&s->gb, h->sps.time_offset_length); /* time_offset */
00089             }
00090         }
00091 
00092         if(s->avctx->debug & FF_DEBUG_PICT_INFO)
00093             av_log(s->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n", h->sei_ct_type, h->sei_pic_struct);
00094     }
00095     return 0;
00096 }
00097 
00098 static int decode_unregistered_user_data(H264Context *h, int size){
00099     MpegEncContext * const s = &h->s;
00100     uint8_t user_data[16+256];
00101     int e, build, i;
00102 
00103     if(size<16)
00104         return -1;
00105 
00106     for(i=0; i<sizeof(user_data)-1 && i<size; i++){
00107         user_data[i]= get_bits(&s->gb, 8);
00108     }
00109 
00110     user_data[i]= 0;
00111     e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
00112     if(e==1 && build>0)
00113         h->x264_build= build;
00114 
00115     if(s->avctx->debug & FF_DEBUG_BUGS)
00116         av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
00117 
00118     for(; i<size; i++)
00119         skip_bits(&s->gb, 8);
00120 
00121     return 0;
00122 }
00123 
00124 static int decode_recovery_point(H264Context *h){
00125     MpegEncContext * const s = &h->s;
00126 
00127     h->sei_recovery_frame_cnt = get_ue_golomb(&s->gb);
00128     skip_bits(&s->gb, 4);       /* 1b exact_match_flag, 1b broken_link_flag, 2b changing_slice_group_idc */
00129 
00130     return 0;
00131 }
00132 
00133 static int decode_buffering_period(H264Context *h){
00134     MpegEncContext * const s = &h->s;
00135     unsigned int sps_id;
00136     int sched_sel_idx;
00137     SPS *sps;
00138 
00139     sps_id = get_ue_golomb_31(&s->gb);
00140     if(sps_id > 31 || !h->sps_buffers[sps_id]) {
00141         av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %d referenced in buffering period\n", sps_id);
00142         return -1;
00143     }
00144     sps = h->sps_buffers[sps_id];
00145 
00146     // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
00147     if (sps->nal_hrd_parameters_present_flag) {
00148         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
00149             h->initial_cpb_removal_delay[sched_sel_idx] = get_bits(&s->gb, sps->initial_cpb_removal_delay_length);
00150             skip_bits(&s->gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
00151         }
00152     }
00153     if (sps->vcl_hrd_parameters_present_flag) {
00154         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
00155             h->initial_cpb_removal_delay[sched_sel_idx] = get_bits(&s->gb, sps->initial_cpb_removal_delay_length);
00156             skip_bits(&s->gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
00157         }
00158     }
00159 
00160     h->sei_buffering_period_present = 1;
00161     return 0;
00162 }
00163 
00164 int ff_h264_decode_sei(H264Context *h){
00165     MpegEncContext * const s = &h->s;
00166 
00167     while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
00168         int size, type;
00169 
00170         type=0;
00171         do{
00172             type+= show_bits(&s->gb, 8);
00173         }while(get_bits(&s->gb, 8) == 255);
00174 
00175         size=0;
00176         do{
00177             size+= show_bits(&s->gb, 8);
00178         }while(get_bits(&s->gb, 8) == 255);
00179 
00180         switch(type){
00181         case SEI_TYPE_PIC_TIMING: // Picture timing SEI
00182             if(decode_picture_timing(h) < 0)
00183                 return -1;
00184             break;
00185         case SEI_TYPE_USER_DATA_UNREGISTERED:
00186             if(decode_unregistered_user_data(h, size) < 0)
00187                 return -1;
00188             break;
00189         case SEI_TYPE_RECOVERY_POINT:
00190             if(decode_recovery_point(h) < 0)
00191                 return -1;
00192             break;
00193         case SEI_BUFFERING_PERIOD:
00194             if(decode_buffering_period(h) < 0)
00195                 return -1;
00196             break;
00197         default:
00198             skip_bits(&s->gb, 8*size);
00199         }
00200 
00201         //FIXME check bits here
00202         align_get_bits(&s->gb);
00203     }
00204 
00205     return 0;
00206 }

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