8c84d439d8a5a134f785854207e72dd0ea3a5ed4
[libav.git] / libavcodec / h264_parser.c
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "parser.h"
29 #include "h264data.h"
30 #include "golomb.h"
31
32 #include <assert.h>
33
34
35 static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
36 {
37 int i;
38 uint32_t state;
39 ParseContext *pc = &h->parse_context;
40 // mb_addr= pc->mb_addr - 1;
41 state= pc->state;
42 if(state>13)
43 state= 7;
44
45 for(i=0; i<buf_size; i++){
46 if(state==7){
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
50 */
51 # if HAVE_FAST_64BIT
52 while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
53 i+=8;
54 # else
55 while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
56 i+=4;
57 # endif
58 #endif
59 for(; i<buf_size; i++){
60 if(!buf[i]){
61 state=2;
62 break;
63 }
64 }
65 }else if(state<=2){
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
69 }else if(state<=5){
70 int v= buf[i] & 0x1F;
71 if(v==6 || v==7 || v==8 || v==9){
72 if(pc->frame_start_found){
73 i++;
74 goto found;
75 }
76 }else if(v==1 || v==2 || v==5){
77 if(pc->frame_start_found){
78 state+=8;
79 continue;
80 }else
81 pc->frame_start_found = 1;
82 }
83 state= 7;
84 }else{
85 if(buf[i] & 0x80)
86 goto found;
87 state= 7;
88 }
89 }
90 pc->state= state;
91 return END_NOT_FOUND;
92
93 found:
94 pc->state=7;
95 pc->frame_start_found= 0;
96 return i-(state&5);
97 }
98
99 /**
100 * Parse NAL units of found picture and decode some basic information.
101 *
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.
106 */
107 static inline int parse_nal_units(AVCodecParserContext *s,
108 AVCodecContext *avctx,
109 const uint8_t *buf, int buf_size)
110 {
111 H264Context *h = s->priv_data;
112 const uint8_t *buf_end = buf + buf_size;
113 unsigned int pps_id;
114 unsigned int slice_type;
115 int state = -1;
116 const uint8_t *ptr;
117
118 /* set some sane default values */
119 s->pict_type = AV_PICTURE_TYPE_I;
120 s->key_frame = 0;
121
122 h->avctx= avctx;
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;
127
128 if (!buf_size)
129 return 0;
130
131 for(;;) {
132 int src_length, dst_length, consumed;
133 buf = avpriv_mpv_find_start_code(buf, buf_end, &state);
134 if(buf >= buf_end)
135 break;
136 --buf;
137 src_length = buf_end - buf;
138 switch (state & 0x1f) {
139 case NAL_SLICE:
140 case NAL_IDR_SLICE:
141 // Do not walk the whole buffer just to decode slice header
142 if (src_length > 20)
143 src_length = 20;
144 break;
145 }
146 ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
147 if (ptr==NULL || dst_length < 0)
148 break;
149
150 init_get_bits(&h->gb, ptr, 8*dst_length);
151 switch(h->nal_unit_type) {
152 case NAL_SPS:
153 ff_h264_decode_seq_parameter_set(h);
154 break;
155 case NAL_PPS:
156 ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
157 break;
158 case NAL_SEI:
159 ff_h264_decode_sei(h);
160 break;
161 case NAL_IDR_SLICE:
162 s->key_frame = 1;
163 /* fall through */
164 case NAL_SLICE:
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 */
170 s->key_frame = 1;
171 }
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");
175 return -1;
176 }
177 if(!h->pps_buffers[pps_id]) {
178 av_log(h->avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
179 return -1;
180 }
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");
184 return -1;
185 }
186 h->sps = *h->sps_buffers[h->pps.sps_id];
187 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
188
189 avctx->profile = ff_h264_get_profile(&h->sps);
190 avctx->level = h->sps.level_idc;
191
192 if(h->sps.frame_mbs_only_flag){
193 h->picture_structure= PICT_FRAME;
194 }else{
195 if(get_bits1(&h->gb)) { //field_pic_flag
196 h->picture_structure= PICT_TOP_FIELD + get_bits1(&h->gb); //bottom_field_flag
197 } else {
198 h->picture_structure= PICT_FRAME;
199 }
200 }
201
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:
206 s->repeat_pict = 0;
207 break;
208 case SEI_PIC_STRUCT_FRAME:
209 case SEI_PIC_STRUCT_TOP_BOTTOM:
210 case SEI_PIC_STRUCT_BOTTOM_TOP:
211 s->repeat_pict = 1;
212 break;
213 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
214 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
215 s->repeat_pict = 2;
216 break;
217 case SEI_PIC_STRUCT_FRAME_DOUBLING:
218 s->repeat_pict = 3;
219 break;
220 case SEI_PIC_STRUCT_FRAME_TRIPLING:
221 s->repeat_pict = 5;
222 break;
223 default:
224 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
225 break;
226 }
227 } else {
228 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
229 }
230
231 return 0; /* no need to evaluate the rest */
232 }
233 buf += consumed;
234 }
235 /* didn't find a picture! */
236 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
237 return -1;
238 }
239
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)
244 {
245 H264Context *h = s->priv_data;
246 ParseContext *pc = &h->parse_context;
247 int next;
248
249 if (!h->got_first) {
250 h->got_first = 1;
251 if (avctx->extradata_size) {
252 h->avctx = avctx;
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)
259 h->low_delay = 1;
260 ff_h264_decode_extradata(h);
261 }
262 }
263
264 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
265 next= buf_size;
266 }else{
267 next= ff_h264_find_frame_end(h, buf, buf_size);
268
269 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
270 *poutbuf = NULL;
271 *poutbuf_size = 0;
272 return buf_size;
273 }
274
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
278 }
279 }
280
281 parse_nal_units(s, avctx, buf, buf_size);
282
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;
287 } else {
288 s->dts_sync_point = INT_MIN;
289 s->dts_ref_dts_delta = INT_MIN;
290 s->pts_dts_delta = INT_MIN;
291 }
292
293 if (s->flags & PARSER_FLAG_ONCE) {
294 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
295 }
296
297 *poutbuf = buf;
298 *poutbuf_size = buf_size;
299 return next;
300 }
301
302 static int h264_split(AVCodecContext *avctx,
303 const uint8_t *buf, int buf_size)
304 {
305 int i;
306 uint32_t state = -1;
307 int has_sps= 0;
308
309 for(i=0; i<=buf_size; i++){
310 if((state&0xFFFFFF1F) == 0x107)
311 has_sps=1;
312 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
313 }*/
314 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
315 if(has_sps){
316 while(i>4 && buf[i-5]==0) i--;
317 return i-4;
318 }
319 }
320 if (i<buf_size)
321 state= (state<<8) | buf[i];
322 }
323 return 0;
324 }
325
326 static void close(AVCodecParserContext *s)
327 {
328 H264Context *h = s->priv_data;
329 ParseContext *pc = &h->parse_context;
330
331 av_free(pc->buffer);
332 ff_h264_free_context(h);
333 }
334
335 static int init(AVCodecParserContext *s)
336 {
337 H264Context *h = s->priv_data;
338 h->thread_context[0] = h;
339 h->slice_context_count = 1;
340 return 0;
341 }
342
343 AVCodecParser ff_h264_parser = {
344 .codec_ids = { AV_CODEC_ID_H264 },
345 .priv_data_size = sizeof(H264Context),
346 .parser_init = init,
347 .parser_parse = h264_parse,
348 .parser_close = close,
349 .split = h264_split,
350 };