annotate libavcodec/h264_sei.c @ 3:0b056460c67d

changed code to use VSs
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Mon, 29 Oct 2012 16:44:27 +0100
parents
children
rev   line source
nengel@2 1 /*
nengel@2 2 * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
nengel@2 3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
nengel@2 4 *
nengel@2 5 * This file is part of FFmpeg.
nengel@2 6 *
nengel@2 7 * FFmpeg is free software; you can redistribute it and/or
nengel@2 8 * modify it under the terms of the GNU Lesser General Public
nengel@2 9 * License as published by the Free Software Foundation; either
nengel@2 10 * version 2.1 of the License, or (at your option) any later version.
nengel@2 11 *
nengel@2 12 * FFmpeg is distributed in the hope that it will be useful,
nengel@2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nengel@2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
nengel@2 15 * Lesser General Public License for more details.
nengel@2 16 *
nengel@2 17 * You should have received a copy of the GNU Lesser General Public
nengel@2 18 * License along with FFmpeg; if not, write to the Free Software
nengel@2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
nengel@2 20 */
nengel@2 21
nengel@2 22 /**
nengel@2 23 * @file
nengel@2 24 * H.264 / AVC / MPEG4 part10 sei decoding.
nengel@2 25 * @author Michael Niedermayer <michaelni@gmx.at>
nengel@2 26 */
nengel@2 27
nengel@2 28 #include "avcodec.h"
nengel@2 29 #include "h264_types.h"
nengel@2 30 #include "golomb.h"
nengel@2 31
nengel@2 32 //#undef NDEBUG
nengel@2 33 #include <assert.h>
nengel@2 34
nengel@2 35 static const uint8_t sei_num_clock_ts_table[9]={
nengel@2 36 1, 1, 1, 2, 2, 3, 3, 2, 3
nengel@2 37 };
nengel@2 38
nengel@2 39 void ff_h264_reset_sei(NalContext *n) {
nengel@2 40 n->sei_recovery_frame_cnt = -1;
nengel@2 41 n->sei_dpb_output_delay = 0;
nengel@2 42 n->sei_cpb_removal_delay = -1;
nengel@2 43 n->sei_buffering_period_present = 0;
nengel@2 44 }
nengel@2 45
nengel@2 46 static int decode_picture_timing(NalContext *n, GetBitContext *gb){
nengel@2 47 if(n->sps.nal_hrd_parameters_present_flag || n->sps.vcl_hrd_parameters_present_flag){
nengel@2 48 n->sei_cpb_removal_delay = get_bits(gb, n->sps.cpb_removal_delay_length);
nengel@2 49 n->sei_dpb_output_delay = get_bits(gb, n->sps.dpb_output_delay_length);
nengel@2 50 }
nengel@2 51 if(n->sps.pic_struct_present_flag){
nengel@2 52 unsigned int i, num_clock_ts;
nengel@2 53 n->sei_pic_struct = get_bits(gb, 4);
nengel@2 54 n->sei_ct_type = 0;
nengel@2 55
nengel@2 56 if (n->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
nengel@2 57 return -1;
nengel@2 58
nengel@2 59 num_clock_ts = sei_num_clock_ts_table[n->sei_pic_struct];
nengel@2 60
nengel@2 61 for (i = 0 ; i < num_clock_ts ; i++){
nengel@2 62 if(get_bits(gb, 1)){ /* clock_timestamp_flag */
nengel@2 63 unsigned int full_timestamp_flag;
nengel@2 64 n->sei_ct_type |= 1<<get_bits(gb, 2);
nengel@2 65 skip_bits(gb, 1); /* nuit_field_based_flag */
nengel@2 66 skip_bits(gb, 5); /* counting_type */
nengel@2 67 full_timestamp_flag = get_bits(gb, 1);
nengel@2 68 skip_bits(gb, 1); /* discontinuity_flag */
nengel@2 69 skip_bits(gb, 1); /* cnt_dropped_flag */
nengel@2 70 skip_bits(gb, 8); /* n_frames */
nengel@2 71 if(full_timestamp_flag){
nengel@2 72 skip_bits(gb, 6); /* seconds_value 0..59 */
nengel@2 73 skip_bits(gb, 6); /* minutes_value 0..59 */
nengel@2 74 skip_bits(gb, 5); /* hours_value 0..23 */
nengel@2 75 }else{
nengel@2 76 if(get_bits(gb, 1)){ /* seconds_flag */
nengel@2 77 skip_bits(gb, 6); /* seconds_value range 0..59 */
nengel@2 78 if(get_bits(gb, 1)){ /* minutes_flag */
nengel@2 79 skip_bits(gb, 6); /* minutes_value 0..59 */
nengel@2 80 if(get_bits(gb, 1)) /* hours_flag */
nengel@2 81 skip_bits(gb, 5); /* hours_value 0..23 */
nengel@2 82 }
nengel@2 83 }
nengel@2 84 }
nengel@2 85 if(n->sps.time_offset_length > 0)
nengel@2 86 skip_bits(gb, n->sps.time_offset_length); /* time_offset */
nengel@2 87 }
nengel@2 88 }
nengel@2 89 }
nengel@2 90 return 0;
nengel@2 91 }
nengel@2 92
nengel@2 93 static int decode_unregistered_user_data(GetBitContext *gb, int size){
nengel@2 94 char user_data[16+256];
nengel@2 95 int e, build, i;
nengel@2 96
nengel@2 97 if(size<16)
nengel@2 98 return -1;
nengel@2 99
nengel@2 100 for(i=0; i<(int) sizeof(user_data)-1 && i<size; i++){
nengel@2 101 user_data[i]= get_bits(gb, 8);
nengel@2 102 }
nengel@2 103
nengel@2 104 user_data[i]= 0;
nengel@2 105 e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
nengel@2 106 (void) e;
nengel@2 107 for(; i<size; i++)
nengel@2 108 skip_bits(gb, 8);
nengel@2 109
nengel@2 110 return 0;
nengel@2 111 }
nengel@2 112
nengel@2 113 static int decode_recovery_point(NalContext *n, GetBitContext *gb){
nengel@2 114
nengel@2 115 n->sei_recovery_frame_cnt = get_ue_golomb(gb);
nengel@2 116 skip_bits(gb, 4); /* 1b exact_match_flag, 1b broken_link_flag, 2b changing_slice_group_idc */
nengel@2 117
nengel@2 118 return 0;
nengel@2 119 }
nengel@2 120
nengel@2 121 static int decode_buffering_period(NalContext *n, GetBitContext *gb){
nengel@2 122 unsigned int sps_id;
nengel@2 123 int sched_sel_idx;
nengel@2 124 SPS *sps;
nengel@2 125
nengel@2 126 sps_id = get_ue_golomb_31(gb);
nengel@2 127 if(sps_id > 31 || !n->sps_buffers[sps_id]) {
nengel@2 128 av_log(AV_LOG_ERROR, "non-existing SPS %d referenced in buffering period\n", sps_id);
nengel@2 129 return -1;
nengel@2 130 }
nengel@2 131 sps = n->sps_buffers[sps_id];
nengel@2 132
nengel@2 133 // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
nengel@2 134 if (sps->nal_hrd_parameters_present_flag) {
nengel@2 135 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
nengel@2 136 n->initial_cpb_removal_delay[sched_sel_idx] = get_bits(gb, sps->initial_cpb_removal_delay_length);
nengel@2 137 skip_bits(gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
nengel@2 138 }
nengel@2 139 }
nengel@2 140 if (sps->vcl_hrd_parameters_present_flag) {
nengel@2 141 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
nengel@2 142 n->initial_cpb_removal_delay[sched_sel_idx] = get_bits(gb, sps->initial_cpb_removal_delay_length);
nengel@2 143 skip_bits(gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
nengel@2 144 }
nengel@2 145 }
nengel@2 146
nengel@2 147 n->sei_buffering_period_present = 1;
nengel@2 148 return 0;
nengel@2 149 }
nengel@2 150
nengel@2 151 int ff_h264_decode_sei(NalContext *n, GetBitContext *gb){
nengel@2 152 while(get_bits_count(gb) + 16 < gb->size_in_bits){
nengel@2 153 int size, type;
nengel@2 154
nengel@2 155 type=0;
nengel@2 156 do{
nengel@2 157 type+= show_bits(gb, 8);
nengel@2 158 }while(get_bits(gb, 8) == 255);
nengel@2 159
nengel@2 160 size=0;
nengel@2 161 do{
nengel@2 162 size+= show_bits(gb, 8);
nengel@2 163 }while(get_bits(gb, 8) == 255);
nengel@2 164
nengel@2 165 switch(type){
nengel@2 166 case SEI_TYPE_PIC_TIMING: // Picture timing SEI
nengel@2 167 if(decode_picture_timing(n, gb) < 0)
nengel@2 168 return -1;
nengel@2 169 break;
nengel@2 170 case SEI_TYPE_USER_DATA_UNREGISTERED:
nengel@2 171 if(decode_unregistered_user_data(gb, size) < 0)
nengel@2 172 return -1;
nengel@2 173 break;
nengel@2 174 case SEI_TYPE_RECOVERY_POINT:
nengel@2 175 if(decode_recovery_point(n, gb) < 0)
nengel@2 176 return -1;
nengel@2 177 break;
nengel@2 178 case SEI_BUFFERING_PERIOD:
nengel@2 179 if(decode_buffering_period(n, gb) < 0)
nengel@2 180 return -1;
nengel@2 181 break;
nengel@2 182 default:
nengel@2 183 skip_bits(gb, 8*size);
nengel@2 184 }
nengel@2 185
nengel@2 186 //FIXME check bits here
nengel@2 187 align_get_bits(gb);
nengel@2 188 }
nengel@2 189
nengel@2 190 return 0;
nengel@2 191 }