2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #define UNCHECKED_BITSTREAM_READER 1
35 static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
39 ParseContext *pc = &(h->s.parse_context);
40 int next_avc= h->is_avc ? 0 : buf_size;
42 // mb_addr= pc->mb_addr - 1;
47 if(h->is_avc && !h->nal_length_size)
48 av_log(h->s.avctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
50 for(i=0; i<buf_size; i++){
54 for(j = 0; j < h->nal_length_size; j++)
55 nalsize = (nalsize << 8) | buf[i++];
56 if(nalsize <= 0 || nalsize > buf_size - i){
57 av_log(h->s.avctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
60 next_avc= i + nalsize;
65 #if HAVE_FAST_UNALIGNED
66 /* we check i<buf_size instead of i+3/7 because its simpler
67 * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
70 while(i<next_avc && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
73 while(i<next_avc && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
77 for(; i<next_avc; i++){
84 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
85 else if(buf[i]) state = 7;
86 else state>>=1; //2->1, 1->0, 0->0
89 if(v==6 || v==7 || v==8 || v==9){
90 if(pc->frame_start_found){
94 }else if(v==1 || v==2 || v==5){
100 h->parse_history[h->parse_history_count++]= buf[i];
101 if(h->parse_history_count>3){
102 unsigned int mb, last_mb= h->parse_last_mb;
105 init_get_bits(&gb, h->parse_history, 8*h->parse_history_count);
106 h->parse_history_count=0;
107 mb= get_ue_golomb_long(&gb);
108 last_mb= h->parse_last_mb;
109 h->parse_last_mb= mb;
110 if(pc->frame_start_found){
114 pc->frame_start_found = 1;
122 return END_NOT_FOUND;
126 pc->frame_start_found= 0;
129 return i-(state&5) - 3*(state>7);
133 * Parse NAL units of found picture and decode some basic information.
135 * @param s parser context.
136 * @param avctx codec context.
137 * @param buf buffer with field/frame data.
138 * @param buf_size size of the buffer.
140 static inline int parse_nal_units(AVCodecParserContext *s,
141 AVCodecContext *avctx,
142 const uint8_t *buf, int buf_size)
144 H264Context *h = s->priv_data;
145 const uint8_t *buf_end = buf + buf_size;
147 unsigned int slice_type;
150 int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
152 /* set some sane default values */
153 s->pict_type = AV_PICTURE_TYPE_I;
157 h->sei_recovery_frame_cnt = -1;
158 h->sei_dpb_output_delay = 0;
159 h->sei_cpb_removal_delay = -1;
160 h->sei_buffering_period_present = 0;
166 int src_length, dst_length, consumed, nalsize = 0;
169 if (h->nal_length_size >= buf_end - buf) break;
171 for (i = 0; i < h->nal_length_size; i++)
172 nalsize = (nalsize << 8) | *buf++;
173 if (nalsize <= 0 || nalsize > buf_end - buf) {
174 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
177 src_length = nalsize;
179 buf = avpriv_mpv_find_start_code(buf, buf_end, &state);
183 src_length = buf_end - buf;
185 switch (state & 0x1f) {
188 // Do not walk the whole buffer just to decode slice header
193 ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
194 if (ptr==NULL || dst_length < 0)
197 init_get_bits(&h->s.gb, ptr, 8*dst_length);
198 switch(h->nal_unit_type) {
200 ff_h264_decode_seq_parameter_set(h);
203 ff_h264_decode_picture_parameter_set(h, h->s.gb.size_in_bits);
206 ff_h264_decode_sei(h);
212 get_ue_golomb_long(&h->s.gb); // skip first_mb_in_slice
213 slice_type = get_ue_golomb_31(&h->s.gb);
214 s->pict_type = golomb_to_pict_type[slice_type % 5];
215 if (h->sei_recovery_frame_cnt >= 0) {
216 /* key frame, since recovery_frame_cnt is set */
219 pps_id= get_ue_golomb(&h->s.gb);
220 if(pps_id>=MAX_PPS_COUNT) {
221 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
224 if(!h->pps_buffers[pps_id]) {
225 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
228 h->pps= *h->pps_buffers[pps_id];
229 if(!h->sps_buffers[h->pps.sps_id]) {
230 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
233 h->sps = *h->sps_buffers[h->pps.sps_id];
234 h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num);
236 avctx->profile = ff_h264_get_profile(&h->sps);
237 avctx->level = h->sps.level_idc;
239 if(h->sps.frame_mbs_only_flag){
240 h->s.picture_structure= PICT_FRAME;
242 if(get_bits1(&h->s.gb)) { //field_pic_flag
243 h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag
245 h->s.picture_structure= PICT_FRAME;
249 if(h->sps.pic_struct_present_flag) {
250 switch (h->sei_pic_struct) {
251 case SEI_PIC_STRUCT_TOP_FIELD:
252 case SEI_PIC_STRUCT_BOTTOM_FIELD:
255 case SEI_PIC_STRUCT_FRAME:
256 case SEI_PIC_STRUCT_TOP_BOTTOM:
257 case SEI_PIC_STRUCT_BOTTOM_TOP:
260 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
261 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
264 case SEI_PIC_STRUCT_FRAME_DOUBLING:
267 case SEI_PIC_STRUCT_FRAME_TRIPLING:
271 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
275 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
278 return 0; /* no need to evaluate the rest */
280 buf += h->is_avc ? nalsize : consumed;
284 /* didn't find a picture! */
285 av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
289 static int h264_parse(AVCodecParserContext *s,
290 AVCodecContext *avctx,
291 const uint8_t **poutbuf, int *poutbuf_size,
292 const uint8_t *buf, int buf_size)
294 H264Context *h = s->priv_data;
295 ParseContext *pc = &h->s.parse_context;
300 if (avctx->extradata_size) {
302 // must be done like in decoder, otherwise opening the parser,
303 // letting it create extradata and then closing and opening again
304 // will cause has_b_frames to be always set.
305 // Note that estimate_timings_from_pts does exactly this.
306 if (!avctx->has_b_frames)
308 ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
312 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
315 next= ff_h264_find_frame_end(h, buf, buf_size);
317 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
323 if(next<0 && next != END_NOT_FOUND){
324 av_assert1(pc->last_index + next >= 0 );
325 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
329 parse_nal_units(s, avctx, buf, buf_size);
331 if (h->sei_cpb_removal_delay >= 0) {
332 s->dts_sync_point = h->sei_buffering_period_present;
333 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
334 s->pts_dts_delta = h->sei_dpb_output_delay;
336 s->dts_sync_point = INT_MIN;
337 s->dts_ref_dts_delta = INT_MIN;
338 s->pts_dts_delta = INT_MIN;
341 if (s->flags & PARSER_FLAG_ONCE) {
342 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
346 *poutbuf_size = buf_size;
350 static int h264_split(AVCodecContext *avctx,
351 const uint8_t *buf, int buf_size)
357 for(i=0; i<=buf_size; i++){
358 if((state&0xFFFFFF1F) == 0x107)
360 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
362 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
364 while(i>4 && buf[i-5]==0) i--;
369 state= (state<<8) | buf[i];
374 static void close(AVCodecParserContext *s)
376 H264Context *h = s->priv_data;
377 ParseContext *pc = &h->s.parse_context;
380 ff_h264_free_context(h);
383 static int init(AVCodecParserContext *s)
385 H264Context *h = s->priv_data;
386 h->thread_context[0] = h;
387 h->s.slice_context_count = 1;
391 AVCodecParser ff_h264_parser = {
392 .codec_ids = { AV_CODEC_ID_H264 },
393 .priv_data_size = sizeof(H264Context),
395 .parser_parse = h264_parse,
396 .parser_close = close,