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 Libav.
7 * Libav 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 * Libav 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 Libav; 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>
35 static int ff_h264_find_frame_end(H264Context
*h
, const uint8_t *buf
, int buf_size
)
39 ParseContext
*pc
= &h
->parse_context
;
40 // mb_addr= pc->mb_addr - 1;
45 for(i
=0; i
<buf_size
; i
++){
47 #if HAVE_FAST_UNALIGNED
48 /* we check i<buf_size instead of i+3/7 because its simpler
49 * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
52 while(i
<buf_size
&& !((~*(const uint64_t*)(buf
+i
) & (*(const uint64_t*)(buf
+i
) - 0x0101010101010101ULL
)) & 0x8080808080808080ULL
))
55 while(i
<buf_size
&& !((~*(const uint32_t*)(buf
+i
) & (*(const uint32_t*)(buf
+i
) - 0x01010101U
)) & 0x80808080U
))
59 for(; i
<buf_size
; i
++){
66 if(buf
[i
]==1) state
^= 5; //2->7, 1->4, 0->5
67 else if(buf
[i
]) state
= 7;
68 else state
>>=1; //2->1, 1->0, 0->0
71 if(v
==6 || v
==7 || v
==8 || v
==9){
72 if(pc
->frame_start_found
){
76 }else if(v
==1 || v
==2 || v
==5){
77 if(pc
->frame_start_found
){
81 pc
->frame_start_found
= 1;
95 pc
->frame_start_found
= 0;
100 * Parse NAL units of found picture and decode some basic information.
102 * @param s parser context.
103 * @param avctx codec context.
104 * @param buf buffer with field/frame data.
105 * @param buf_size size of the buffer.
107 static inline int parse_nal_units(AVCodecParserContext
*s
,
108 AVCodecContext
*avctx
,
109 const uint8_t *buf
, int buf_size
)
111 H264Context
*h
= s
->priv_data
;
112 const uint8_t *buf_end
= buf
+ buf_size
;
114 unsigned int slice_type
;
118 /* set some sane default values */
119 s
->pict_type
= AV_PICTURE_TYPE_I
;
123 h
->sei_recovery_frame_cnt
= -1;
124 h
->sei_dpb_output_delay
= 0;
125 h
->sei_cpb_removal_delay
= -1;
126 h
->sei_buffering_period_present
= 0;
132 int src_length
, dst_length
, consumed
;
133 buf
= avpriv_mpv_find_start_code(buf
, buf_end
, &state
);
137 src_length
= buf_end
- buf
;
138 switch (state
& 0x1f) {
141 // Do not walk the whole buffer just to decode slice header
146 ptr
= ff_h264_decode_nal(h
, buf
, &dst_length
, &consumed
, src_length
);
147 if (ptr
==NULL
|| dst_length
< 0)
150 init_get_bits(&h
->gb
, ptr
, 8*dst_length
);
151 switch(h
->nal_unit_type
) {
153 ff_h264_decode_seq_parameter_set(h
);
156 ff_h264_decode_picture_parameter_set(h
, h
->gb
.size_in_bits
);
159 ff_h264_decode_sei(h
);
165 get_ue_golomb(&h
->gb
); // skip first_mb_in_slice
166 slice_type
= get_ue_golomb_31(&h
->gb
);
167 s
->pict_type
= golomb_to_pict_type
[slice_type
% 5];
168 if (h
->sei_recovery_frame_cnt
>= 0) {
169 /* key frame, since recovery_frame_cnt is set */
172 pps_id
= get_ue_golomb(&h
->gb
);
173 if(pps_id
>=MAX_PPS_COUNT
) {
174 av_log(h
->avctx
, AV_LOG_ERROR
, "pps_id out of range\n");
177 if(!h
->pps_buffers
[pps_id
]) {
178 av_log(h
->avctx
, AV_LOG_ERROR
, "non-existing PPS referenced\n");
181 h
->pps
= *h
->pps_buffers
[pps_id
];
182 if(!h
->sps_buffers
[h
->pps
.sps_id
]) {
183 av_log(h
->avctx
, AV_LOG_ERROR
, "non-existing SPS referenced\n");
186 h
->sps
= *h
->sps_buffers
[h
->pps
.sps_id
];
187 h
->frame_num
= get_bits(&h
->gb
, h
->sps
.log2_max_frame_num
);
189 avctx
->profile
= ff_h264_get_profile(&h
->sps
);
190 avctx
->level
= h
->sps
.level_idc
;
192 if(h
->sps
.frame_mbs_only_flag
){
193 h
->picture_structure
= PICT_FRAME
;
195 if(get_bits1(&h
->gb
)) { //field_pic_flag
196 h
->picture_structure
= PICT_TOP_FIELD
+ get_bits1(&h
->gb
); //bottom_field_flag
198 h
->picture_structure
= PICT_FRAME
;
202 if(h
->sps
.pic_struct_present_flag
) {
203 switch (h
->sei_pic_struct
) {
204 case SEI_PIC_STRUCT_TOP_FIELD
:
205 case SEI_PIC_STRUCT_BOTTOM_FIELD
:
208 case SEI_PIC_STRUCT_FRAME
:
209 case SEI_PIC_STRUCT_TOP_BOTTOM
:
210 case SEI_PIC_STRUCT_BOTTOM_TOP
:
213 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP
:
214 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM
:
217 case SEI_PIC_STRUCT_FRAME_DOUBLING
:
220 case SEI_PIC_STRUCT_FRAME_TRIPLING
:
224 s
->repeat_pict
= h
->picture_structure
== PICT_FRAME
? 1 : 0;
228 s
->repeat_pict
= h
->picture_structure
== PICT_FRAME
? 1 : 0;
231 return 0; /* no need to evaluate the rest */
235 /* didn't find a picture! */
236 av_log(h
->avctx
, AV_LOG_ERROR
, "missing picture in access unit\n");
240 static int h264_parse(AVCodecParserContext
*s
,
241 AVCodecContext
*avctx
,
242 const uint8_t **poutbuf
, int *poutbuf_size
,
243 const uint8_t *buf
, int buf_size
)
245 H264Context
*h
= s
->priv_data
;
246 ParseContext
*pc
= &h
->parse_context
;
251 if (avctx
->extradata_size
) {
253 // must be done like in the decoder.
254 // otherwise opening the parser, creating extradata,
255 // and then closing and opening again
256 // will cause has_b_frames to be always set.
257 // NB: estimate_timings_from_pts behaves exactly like this.
258 if (!avctx
->has_b_frames
)
260 ff_h264_decode_extradata(h
);
264 if(s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
){
267 next
= ff_h264_find_frame_end(h
, buf
, buf_size
);
269 if (ff_combine_frame(pc
, next
, &buf
, &buf_size
) < 0) {
275 if(next
<0 && next
!= END_NOT_FOUND
){
276 assert(pc
->last_index
+ next
>= 0 );
277 ff_h264_find_frame_end(h
, &pc
->buffer
[pc
->last_index
+ next
], -next
); //update state
281 parse_nal_units(s
, avctx
, buf
, buf_size
);
283 if (h
->sei_cpb_removal_delay
>= 0) {
284 s
->dts_sync_point
= h
->sei_buffering_period_present
;
285 s
->dts_ref_dts_delta
= h
->sei_cpb_removal_delay
;
286 s
->pts_dts_delta
= h
->sei_dpb_output_delay
;
288 s
->dts_sync_point
= INT_MIN
;
289 s
->dts_ref_dts_delta
= INT_MIN
;
290 s
->pts_dts_delta
= INT_MIN
;
293 if (s
->flags
& PARSER_FLAG_ONCE
) {
294 s
->flags
&= PARSER_FLAG_COMPLETE_FRAMES
;
298 *poutbuf_size
= buf_size
;
302 static int h264_split(AVCodecContext
*avctx
,
303 const uint8_t *buf
, int buf_size
)
309 for(i
=0; i
<=buf_size
; i
++){
310 if((state
&0xFFFFFF1F) == 0x107)
312 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
314 if((state
&0xFFFFFF00) == 0x100 && (state
&0xFFFFFF1F) != 0x107 && (state
&0xFFFFFF1F) != 0x108 && (state
&0xFFFFFF1F) != 0x109){
316 while(i
>4 && buf
[i
-5]==0) i
--;
321 state
= (state
<<8) | buf
[i
];
326 static void close(AVCodecParserContext
*s
)
328 H264Context
*h
= s
->priv_data
;
329 ParseContext
*pc
= &h
->parse_context
;
332 ff_h264_free_context(h
);
335 static int init(AVCodecParserContext
*s
)
337 H264Context
*h
= s
->priv_data
;
338 h
->thread_context
[0] = h
;
339 h
->slice_context_count
= 1;
343 AVCodecParser ff_h264_parser
= {
344 .codec_ids
= { AV_CODEC_ID_H264
},
345 .priv_data_size
= sizeof(H264Context
),
347 .parser_parse
= h264_parse
,
348 .parser_close
= close
,