diff libavcodec/h264_sei.c @ 2:897f711a7157

rearrange to work with autoconf
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Tue, 25 Sep 2012 15:55:33 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libavcodec/h264_sei.c	Tue Sep 25 15:55:33 2012 +0200
     1.3 @@ -0,0 +1,191 @@
     1.4 +/*
     1.5 + * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
     1.6 + * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
     1.7 + *
     1.8 + * This file is part of FFmpeg.
     1.9 + *
    1.10 + * FFmpeg is free software; you can redistribute it and/or
    1.11 + * modify it under the terms of the GNU Lesser General Public
    1.12 + * License as published by the Free Software Foundation; either
    1.13 + * version 2.1 of the License, or (at your option) any later version.
    1.14 + *
    1.15 + * FFmpeg is distributed in the hope that it will be useful,
    1.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.18 + * Lesser General Public License for more details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU Lesser General Public
    1.21 + * License along with FFmpeg; if not, write to the Free Software
    1.22 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    1.23 + */
    1.24 +
    1.25 +/**
    1.26 + * @file
    1.27 + * H.264 / AVC / MPEG4 part10 sei decoding.
    1.28 + * @author Michael Niedermayer <michaelni@gmx.at>
    1.29 + */
    1.30 +
    1.31 +#include "avcodec.h"
    1.32 +#include "h264_types.h"
    1.33 +#include "golomb.h"
    1.34 +
    1.35 +//#undef NDEBUG
    1.36 +#include <assert.h>
    1.37 +
    1.38 +static const uint8_t sei_num_clock_ts_table[9]={
    1.39 +    1,  1,  1,  2,  2,  3,  3,  2,  3
    1.40 +};
    1.41 +
    1.42 +void ff_h264_reset_sei(NalContext *n) {
    1.43 +    n->sei_recovery_frame_cnt       = -1;
    1.44 +    n->sei_dpb_output_delay         =  0;
    1.45 +    n->sei_cpb_removal_delay        = -1;
    1.46 +    n->sei_buffering_period_present =  0;
    1.47 +}
    1.48 +
    1.49 +static int decode_picture_timing(NalContext *n, GetBitContext *gb){
    1.50 +    if(n->sps.nal_hrd_parameters_present_flag || n->sps.vcl_hrd_parameters_present_flag){
    1.51 +        n->sei_cpb_removal_delay = get_bits(gb, n->sps.cpb_removal_delay_length);
    1.52 +        n->sei_dpb_output_delay = get_bits(gb, n->sps.dpb_output_delay_length);
    1.53 +    }
    1.54 +    if(n->sps.pic_struct_present_flag){
    1.55 +        unsigned int i, num_clock_ts;
    1.56 +        n->sei_pic_struct = get_bits(gb, 4);
    1.57 +        n->sei_ct_type    = 0;
    1.58 +
    1.59 +        if (n->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
    1.60 +            return -1;
    1.61 +
    1.62 +        num_clock_ts = sei_num_clock_ts_table[n->sei_pic_struct];
    1.63 +
    1.64 +        for (i = 0 ; i < num_clock_ts ; i++){
    1.65 +            if(get_bits(gb, 1)){                  /* clock_timestamp_flag */
    1.66 +                unsigned int full_timestamp_flag;
    1.67 +                n->sei_ct_type |= 1<<get_bits(gb, 2);
    1.68 +                skip_bits(gb, 1);                 /* nuit_field_based_flag */
    1.69 +                skip_bits(gb, 5);                 /* counting_type */
    1.70 +                full_timestamp_flag = get_bits(gb, 1);
    1.71 +                skip_bits(gb, 1);                 /* discontinuity_flag */
    1.72 +                skip_bits(gb, 1);                 /* cnt_dropped_flag */
    1.73 +                skip_bits(gb, 8);                 /* n_frames */
    1.74 +                if(full_timestamp_flag){
    1.75 +                    skip_bits(gb, 6);             /* seconds_value 0..59 */
    1.76 +                    skip_bits(gb, 6);             /* minutes_value 0..59 */
    1.77 +                    skip_bits(gb, 5);             /* hours_value 0..23 */
    1.78 +                }else{
    1.79 +                    if(get_bits(gb, 1)){          /* seconds_flag */
    1.80 +                        skip_bits(gb, 6);         /* seconds_value range 0..59 */
    1.81 +                        if(get_bits(gb, 1)){      /* minutes_flag */
    1.82 +                            skip_bits(gb, 6);     /* minutes_value 0..59 */
    1.83 +                            if(get_bits(gb, 1))   /* hours_flag */
    1.84 +                                skip_bits(gb, 5); /* hours_value 0..23 */
    1.85 +                        }
    1.86 +                    }
    1.87 +                }
    1.88 +                if(n->sps.time_offset_length > 0)
    1.89 +                    skip_bits(gb, n->sps.time_offset_length); /* time_offset */
    1.90 +            }
    1.91 +        }
    1.92 +    }
    1.93 +    return 0;
    1.94 +}
    1.95 +
    1.96 +static int decode_unregistered_user_data(GetBitContext *gb, int size){
    1.97 +    char user_data[16+256];
    1.98 +    int e, build, i;
    1.99 +
   1.100 +    if(size<16)
   1.101 +        return -1;
   1.102 +
   1.103 +    for(i=0; i<(int) sizeof(user_data)-1 && i<size; i++){
   1.104 +        user_data[i]= get_bits(gb, 8);
   1.105 +    }
   1.106 +
   1.107 +    user_data[i]= 0;
   1.108 +    e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
   1.109 +    (void) e;
   1.110 +    for(; i<size; i++)
   1.111 +        skip_bits(gb, 8);
   1.112 +
   1.113 +    return 0;
   1.114 +}
   1.115 +
   1.116 +static int decode_recovery_point(NalContext *n, GetBitContext *gb){
   1.117 +
   1.118 +    n->sei_recovery_frame_cnt = get_ue_golomb(gb);
   1.119 +    skip_bits(gb, 4);       /* 1b exact_match_flag, 1b broken_link_flag, 2b changing_slice_group_idc */
   1.120 +
   1.121 +    return 0;
   1.122 +}
   1.123 +
   1.124 +static int decode_buffering_period(NalContext *n, GetBitContext *gb){
   1.125 +    unsigned int sps_id;
   1.126 +    int sched_sel_idx;
   1.127 +    SPS *sps;
   1.128 +
   1.129 +    sps_id = get_ue_golomb_31(gb);
   1.130 +    if(sps_id > 31 || !n->sps_buffers[sps_id]) {
   1.131 +        av_log(AV_LOG_ERROR, "non-existing SPS %d referenced in buffering period\n", sps_id);
   1.132 +        return -1;
   1.133 +    }
   1.134 +    sps = n->sps_buffers[sps_id];
   1.135 +
   1.136 +    // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
   1.137 +    if (sps->nal_hrd_parameters_present_flag) {
   1.138 +        for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
   1.139 +            n->initial_cpb_removal_delay[sched_sel_idx] = get_bits(gb, sps->initial_cpb_removal_delay_length);
   1.140 +            skip_bits(gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
   1.141 +        }
   1.142 +    }
   1.143 +    if (sps->vcl_hrd_parameters_present_flag) {
   1.144 +        for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
   1.145 +            n->initial_cpb_removal_delay[sched_sel_idx] = get_bits(gb, sps->initial_cpb_removal_delay_length);
   1.146 +            skip_bits(gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
   1.147 +        }
   1.148 +    }
   1.149 +
   1.150 +    n->sei_buffering_period_present = 1;
   1.151 +    return 0;
   1.152 +}
   1.153 +
   1.154 +int ff_h264_decode_sei(NalContext *n, GetBitContext *gb){
   1.155 +    while(get_bits_count(gb) + 16 < gb->size_in_bits){
   1.156 +        int size, type;
   1.157 +
   1.158 +        type=0;
   1.159 +        do{
   1.160 +            type+= show_bits(gb, 8);
   1.161 +        }while(get_bits(gb, 8) == 255);
   1.162 +
   1.163 +        size=0;
   1.164 +        do{
   1.165 +            size+= show_bits(gb, 8);
   1.166 +        }while(get_bits(gb, 8) == 255);
   1.167 +
   1.168 +        switch(type){
   1.169 +        case SEI_TYPE_PIC_TIMING: // Picture timing SEI
   1.170 +            if(decode_picture_timing(n, gb) < 0)
   1.171 +                return -1;
   1.172 +            break;
   1.173 +        case SEI_TYPE_USER_DATA_UNREGISTERED:
   1.174 +            if(decode_unregistered_user_data(gb, size) < 0)
   1.175 +                return -1;
   1.176 +            break;
   1.177 +        case SEI_TYPE_RECOVERY_POINT:
   1.178 +            if(decode_recovery_point(n, gb) < 0)
   1.179 +                return -1;
   1.180 +            break;
   1.181 +        case SEI_BUFFERING_PERIOD:
   1.182 +            if(decode_buffering_period(n, gb) < 0)
   1.183 +                return -1;
   1.184 +            break;
   1.185 +        default:
   1.186 +            skip_bits(gb, 8*size);
   1.187 +        }
   1.188 +
   1.189 +        //FIXME check bits here
   1.190 +        align_get_bits(gb);
   1.191 +    }
   1.192 +
   1.193 +    return 0;
   1.194 +}