vorbis: return meaningful errors
[libav.git] / libavcodec / h264_parser.c
CommitLineData
26b4fe82
AJ
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
2912e87a 5 * This file is part of Libav.
26b4fe82 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
26b4fe82
AJ
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
26b4fe82
AJ
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
26b4fe82 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26b4fe82
AJ
20 */
21
22/**
ba87f080 23 * @file
26b4fe82
AJ
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
6fee1b90 28#include "libavutil/attributes.h"
26b4fe82 29#include "parser.h"
ff6474dd
IS
30#include "h264data.h"
31#include "golomb.h"
f1e93986 32#include "internal.h"
26b4fe82
AJ
33
34#include <assert.h>
35
36
088f38a4 37static int h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
26b4fe82
AJ
38{
39 int i;
40 uint32_t state;
2c541554 41 ParseContext *pc = &h->parse_context;
26b4fe82
AJ
42// mb_addr= pc->mb_addr - 1;
43 state= pc->state;
44 if(state>13)
45 state= 7;
46
47 for(i=0; i<buf_size; i++){
48 if(state==7){
b250f9c6 49#if HAVE_FAST_UNALIGNED
e4f1ec3a
MN
50 /* we check i<buf_size instead of i+3/7 because its simpler
51 * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
52 */
b250f9c6 53# if HAVE_FAST_64BIT
4d8eb2e8 54 while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
5cb5023c
MN
55 i+=8;
56# else
4d8eb2e8 57 while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
5cb5023c
MN
58 i+=4;
59# endif
60#endif
26b4fe82
AJ
61 for(; i<buf_size; i++){
62 if(!buf[i]){
63 state=2;
64 break;
65 }
66 }
67 }else if(state<=2){
68 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
69 else if(buf[i]) state = 7;
70 else state>>=1; //2->1, 1->0, 0->0
71 }else if(state<=5){
72 int v= buf[i] & 0x1F;
9e85f9c5 73 if(v==6 || v==7 || v==8 || v==9){
26b4fe82
AJ
74 if(pc->frame_start_found){
75 i++;
9aa1cfec 76 goto found;
26b4fe82
AJ
77 }
78 }else if(v==1 || v==2 || v==5){
79 if(pc->frame_start_found){
80 state+=8;
81 continue;
82 }else
83 pc->frame_start_found = 1;
84 }
85 state= 7;
86 }else{
87 if(buf[i] & 0x80)
88 goto found;
89 state= 7;
90 }
91 }
92 pc->state= state;
93 return END_NOT_FOUND;
9aa1cfec
DP
94
95found:
96 pc->state=7;
97 pc->frame_start_found= 0;
98 return i-(state&5);
26b4fe82
AJ
99}
100
adbfc605 101/**
ff6474dd
IS
102 * Parse NAL units of found picture and decode some basic information.
103 *
104 * @param s parser context.
105 * @param avctx codec context.
106 * @param buf buffer with field/frame data.
107 * @param buf_size size of the buffer.
108 */
109static inline int parse_nal_units(AVCodecParserContext *s,
110 AVCodecContext *avctx,
111 const uint8_t *buf, int buf_size)
112{
113 H264Context *h = s->priv_data;
114 const uint8_t *buf_end = buf + buf_size;
96c3da93 115 unsigned int pps_id;
ff6474dd 116 unsigned int slice_type;
8fa0ae06 117 int state = -1;
ff6474dd 118 const uint8_t *ptr;
3f1a7ceb 119 int field_poc[2];
ff6474dd
IS
120
121 /* set some sane default values */
975a1447 122 s->pict_type = AV_PICTURE_TYPE_I;
0ed260c7 123 s->key_frame = 0;
3f1a7ceb 124 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
ff6474dd 125
2c541554 126 h->avctx= avctx;
0ed260c7 127 h->sei_recovery_frame_cnt = -1;
c733922e
IS
128 h->sei_dpb_output_delay = 0;
129 h->sei_cpb_removal_delay = -1;
130 h->sei_buffering_period_present = 0;
ff6474dd 131
9479415e
BC
132 if (!buf_size)
133 return 0;
134
ff6474dd
IS
135 for(;;) {
136 int src_length, dst_length, consumed;
f1e93986 137 buf = avpriv_find_start_code(buf, buf_end, &state);
ff6474dd
IS
138 if(buf >= buf_end)
139 break;
140 --buf;
141 src_length = buf_end - buf;
142 switch (state & 0x1f) {
143 case NAL_SLICE:
144 case NAL_IDR_SLICE:
145 // Do not walk the whole buffer just to decode slice header
146 if (src_length > 20)
147 src_length = 20;
148 break;
149 }
358ea75e 150 ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
ff6474dd
IS
151 if (ptr==NULL || dst_length < 0)
152 break;
153
2c541554 154 init_get_bits(&h->gb, ptr, 8*dst_length);
ff6474dd
IS
155 switch(h->nal_unit_type) {
156 case NAL_SPS:
157 ff_h264_decode_seq_parameter_set(h);
158 break;
159 case NAL_PPS:
2c541554 160 ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
ff6474dd
IS
161 break;
162 case NAL_SEI:
163 ff_h264_decode_sei(h);
164 break;
165 case NAL_IDR_SLICE:
0ed260c7 166 s->key_frame = 1;
3f1a7ceb
YN
167
168 h->prev_frame_num = 0;
169 h->prev_frame_num_offset = 0;
170 h->prev_poc_msb =
171 h->prev_poc_lsb = 0;
0ed260c7 172 /* fall through */
ff6474dd 173 case NAL_SLICE:
2c541554
AK
174 get_ue_golomb(&h->gb); // skip first_mb_in_slice
175 slice_type = get_ue_golomb_31(&h->gb);
ff6474dd 176 s->pict_type = golomb_to_pict_type[slice_type % 5];
0ed260c7
IS
177 if (h->sei_recovery_frame_cnt >= 0) {
178 /* key frame, since recovery_frame_cnt is set */
179 s->key_frame = 1;
180 }
2c541554 181 pps_id= get_ue_golomb(&h->gb);
96c3da93 182 if(pps_id>=MAX_PPS_COUNT) {
2c541554 183 av_log(h->avctx, AV_LOG_ERROR, "pps_id out of range\n");
96c3da93
IS
184 return -1;
185 }
186 if(!h->pps_buffers[pps_id]) {
2c541554 187 av_log(h->avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
96c3da93
IS
188 return -1;
189 }
190 h->pps= *h->pps_buffers[pps_id];
191 if(!h->sps_buffers[h->pps.sps_id]) {
2c541554 192 av_log(h->avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
96c3da93
IS
193 return -1;
194 }
195 h->sps = *h->sps_buffers[h->pps.sps_id];
2c541554 196 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
96c3da93 197
fe9a3fbe 198 avctx->profile = ff_h264_get_profile(&h->sps);
dd0cd3d2
RC
199 avctx->level = h->sps.level_idc;
200
96c3da93 201 if(h->sps.frame_mbs_only_flag){
2c541554 202 h->picture_structure= PICT_FRAME;
96c3da93 203 }else{
2c541554
AK
204 if(get_bits1(&h->gb)) { //field_pic_flag
205 h->picture_structure= PICT_TOP_FIELD + get_bits1(&h->gb); //bottom_field_flag
96c3da93 206 } else {
2c541554 207 h->picture_structure= PICT_FRAME;
96c3da93
IS
208 }
209 }
210
3f1a7ceb
YN
211 if (h->nal_unit_type == NAL_IDR_SLICE)
212 get_ue_golomb(&h->gb); /* idr_pic_id */
213 if (h->sps.poc_type == 0) {
214 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
215
216 if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
217 h->delta_poc_bottom = get_se_golomb(&h->gb);
218 }
219
220 if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
221 h->delta_poc[0] = get_se_golomb(&h->gb);
222
223 if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
224 h->delta_poc[1] = get_se_golomb(&h->gb);
225 }
226
227 ff_init_poc(h, field_poc, NULL);
228
346db3ef
IS
229 if(h->sps.pic_struct_present_flag) {
230 switch (h->sei_pic_struct) {
231 case SEI_PIC_STRUCT_TOP_FIELD:
232 case SEI_PIC_STRUCT_BOTTOM_FIELD:
fc9fe428 233 s->repeat_pict = 0;
346db3ef
IS
234 break;
235 case SEI_PIC_STRUCT_FRAME:
236 case SEI_PIC_STRUCT_TOP_BOTTOM:
237 case SEI_PIC_STRUCT_BOTTOM_TOP:
fc9fe428 238 s->repeat_pict = 1;
346db3ef
IS
239 break;
240 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
241 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
fc9fe428 242 s->repeat_pict = 2;
346db3ef
IS
243 break;
244 case SEI_PIC_STRUCT_FRAME_DOUBLING:
fc9fe428 245 s->repeat_pict = 3;
346db3ef
IS
246 break;
247 case SEI_PIC_STRUCT_FRAME_TRIPLING:
fc9fe428 248 s->repeat_pict = 5;
346db3ef
IS
249 break;
250 default:
2c541554 251 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
346db3ef
IS
252 break;
253 }
254 } else {
2c541554 255 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
346db3ef
IS
256 }
257
3f1a7ceb
YN
258 if (h->picture_structure == PICT_FRAME) {
259 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
260 if (h->sps.pic_struct_present_flag) {
261 switch (h->sei_pic_struct) {
262 case SEI_PIC_STRUCT_TOP_BOTTOM:
263 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
264 s->field_order = AV_FIELD_TT;
265 break;
266 case SEI_PIC_STRUCT_BOTTOM_TOP:
267 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
268 s->field_order = AV_FIELD_BB;
269 break;
270 default:
271 s->field_order = AV_FIELD_PROGRESSIVE;
272 break;
273 }
274 } else {
275 if (field_poc[0] < field_poc[1])
276 s->field_order = AV_FIELD_TT;
277 else if (field_poc[0] > field_poc[1])
278 s->field_order = AV_FIELD_BB;
279 else
280 s->field_order = AV_FIELD_PROGRESSIVE;
281 }
282 } else {
283 if (h->picture_structure == PICT_TOP_FIELD)
284 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
285 else
286 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
287 s->field_order = AV_FIELD_UNKNOWN;
288 }
289
ff6474dd
IS
290 return 0; /* no need to evaluate the rest */
291 }
292 buf += consumed;
293 }
294 /* didn't find a picture! */
2c541554 295 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
ff6474dd
IS
296 return -1;
297}
298
26b4fe82
AJ
299static int h264_parse(AVCodecParserContext *s,
300 AVCodecContext *avctx,
301 const uint8_t **poutbuf, int *poutbuf_size,
302 const uint8_t *buf, int buf_size)
303{
304 H264Context *h = s->priv_data;
2c541554 305 ParseContext *pc = &h->parse_context;
26b4fe82
AJ
306 int next;
307
82f1ffc7
HC
308 if (!h->got_first) {
309 h->got_first = 1;
23584bec 310 if (avctx->extradata_size) {
2c541554 311 h->avctx = avctx;
790a367d
RT
312 // must be done like in the decoder.
313 // otherwise opening the parser, creating extradata,
314 // and then closing and opening again
315 // will cause has_b_frames to be always set.
316 // NB: estimate_timings_from_pts behaves exactly like this.
317 if (!avctx->has_b_frames)
2c541554 318 h->low_delay = 1;
23584bec
HC
319 ff_h264_decode_extradata(h);
320 }
321 }
322
26b4fe82
AJ
323 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
324 next= buf_size;
325 }else{
088f38a4 326 next = h264_find_frame_end(h, buf, buf_size);
26b4fe82
AJ
327
328 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
329 *poutbuf = NULL;
330 *poutbuf_size = 0;
331 return buf_size;
332 }
333
334 if(next<0 && next != END_NOT_FOUND){
335 assert(pc->last_index + next >= 0 );
088f38a4 336 h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
26b4fe82 337 }
a26ce1e2 338 }
ff6474dd 339
25f05ddb 340 parse_nal_units(s, avctx, buf, buf_size);
c733922e 341
25f05ddb
PL
342 if (h->sei_cpb_removal_delay >= 0) {
343 s->dts_sync_point = h->sei_buffering_period_present;
344 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
345 s->pts_dts_delta = h->sei_dpb_output_delay;
346 } else {
347 s->dts_sync_point = INT_MIN;
348 s->dts_ref_dts_delta = INT_MIN;
349 s->pts_dts_delta = INT_MIN;
350 }
351
352 if (s->flags & PARSER_FLAG_ONCE) {
353 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
354 }
26b4fe82
AJ
355
356 *poutbuf = buf;
357 *poutbuf_size = buf_size;
358 return next;
359}
360
361static int h264_split(AVCodecContext *avctx,
362 const uint8_t *buf, int buf_size)
363{
364 int i;
365 uint32_t state = -1;
366 int has_sps= 0;
367
368 for(i=0; i<=buf_size; i++){
369 if((state&0xFFFFFF1F) == 0x107)
370 has_sps=1;
371/* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
372 }*/
373 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
374 if(has_sps){
375 while(i>4 && buf[i-5]==0) i--;
376 return i-4;
377 }
378 }
379 if (i<buf_size)
380 state= (state<<8) | buf[i];
381 }
382 return 0;
383}
384
dd990075 385static void close(AVCodecParserContext *s)
3ee4f5e4
MN
386{
387 H264Context *h = s->priv_data;
2c541554 388 ParseContext *pc = &h->parse_context;
3ee4f5e4
MN
389
390 av_free(pc->buffer);
15861962 391 ff_h264_free_context(h);
3ee4f5e4
MN
392}
393
6fee1b90 394static av_cold int init(AVCodecParserContext *s)
e9ca315d
RC
395{
396 H264Context *h = s->priv_data;
397 h->thread_context[0] = h;
2c541554 398 h->slice_context_count = 1;
e9ca315d
RC
399 return 0;
400}
26b4fe82 401
d36beb3f 402AVCodecParser ff_h264_parser = {
36ef5369 403 .codec_ids = { AV_CODEC_ID_H264 },
5511ad14
AK
404 .priv_data_size = sizeof(H264Context),
405 .parser_init = init,
406 .parser_parse = h264_parse,
407 .parser_close = close,
408 .split = h264_split,
26b4fe82 409};