mpegvideo: split ff_draw_horiz_band().
[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
28#include "parser.h"
ff6474dd
IS
29#include "h264data.h"
30#include "golomb.h"
26b4fe82
AJ
31
32#include <assert.h>
33
34
13eb6b90 35static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
26b4fe82
AJ
36{
37 int i;
38 uint32_t state;
39 ParseContext *pc = &(h->s.parse_context);
26b4fe82
AJ
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){
b250f9c6 47#if HAVE_FAST_UNALIGNED
e4f1ec3a
MN
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 */
b250f9c6 51# if HAVE_FAST_64BIT
4d8eb2e8 52 while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
5cb5023c
MN
53 i+=8;
54# else
4d8eb2e8 55 while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
5cb5023c
MN
56 i+=4;
57# endif
58#endif
26b4fe82
AJ
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;
9e85f9c5 71 if(v==6 || v==7 || v==8 || v==9){
26b4fe82
AJ
72 if(pc->frame_start_found){
73 i++;
9aa1cfec 74 goto found;
26b4fe82
AJ
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;
9aa1cfec
DP
92
93found:
94 pc->state=7;
95 pc->frame_start_found= 0;
96 return i-(state&5);
26b4fe82
AJ
97}
98
adbfc605 99/**
ff6474dd
IS
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 */
107static 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;
96c3da93 113 unsigned int pps_id;
ff6474dd 114 unsigned int slice_type;
8fa0ae06 115 int state = -1;
ff6474dd
IS
116 const uint8_t *ptr;
117
118 /* set some sane default values */
975a1447 119 s->pict_type = AV_PICTURE_TYPE_I;
0ed260c7 120 s->key_frame = 0;
ff6474dd
IS
121
122 h->s.avctx= avctx;
0ed260c7 123 h->sei_recovery_frame_cnt = -1;
c733922e
IS
124 h->sei_dpb_output_delay = 0;
125 h->sei_cpb_removal_delay = -1;
126 h->sei_buffering_period_present = 0;
ff6474dd 127
9479415e
BC
128 if (!buf_size)
129 return 0;
130
ff6474dd
IS
131 for(;;) {
132 int src_length, dst_length, consumed;
773375c3 133 buf = avpriv_mpv_find_start_code(buf, buf_end, &state);
ff6474dd
IS
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 }
358ea75e 146 ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
ff6474dd
IS
147 if (ptr==NULL || dst_length < 0)
148 break;
149
150 init_get_bits(&h->s.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->s.gb.size_in_bits);
157 break;
158 case NAL_SEI:
159 ff_h264_decode_sei(h);
160 break;
161 case NAL_IDR_SLICE:
0ed260c7
IS
162 s->key_frame = 1;
163 /* fall through */
ff6474dd
IS
164 case NAL_SLICE:
165 get_ue_golomb(&h->s.gb); // skip first_mb_in_slice
166 slice_type = get_ue_golomb_31(&h->s.gb);
167 s->pict_type = golomb_to_pict_type[slice_type % 5];
0ed260c7
IS
168 if (h->sei_recovery_frame_cnt >= 0) {
169 /* key frame, since recovery_frame_cnt is set */
170 s->key_frame = 1;
171 }
96c3da93
IS
172 pps_id= get_ue_golomb(&h->s.gb);
173 if(pps_id>=MAX_PPS_COUNT) {
174 av_log(h->s.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->s.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->s.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->s.gb, h->sps.log2_max_frame_num);
188
fe9a3fbe 189 avctx->profile = ff_h264_get_profile(&h->sps);
dd0cd3d2
RC
190 avctx->level = h->sps.level_idc;
191
96c3da93
IS
192 if(h->sps.frame_mbs_only_flag){
193 h->s.picture_structure= PICT_FRAME;
194 }else{
195 if(get_bits1(&h->s.gb)) { //field_pic_flag
196 h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag
197 } else {
198 h->s.picture_structure= PICT_FRAME;
199 }
200 }
201
346db3ef
IS
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:
fc9fe428 206 s->repeat_pict = 0;
346db3ef
IS
207 break;
208 case SEI_PIC_STRUCT_FRAME:
209 case SEI_PIC_STRUCT_TOP_BOTTOM:
210 case SEI_PIC_STRUCT_BOTTOM_TOP:
fc9fe428 211 s->repeat_pict = 1;
346db3ef
IS
212 break;
213 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
214 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
fc9fe428 215 s->repeat_pict = 2;
346db3ef
IS
216 break;
217 case SEI_PIC_STRUCT_FRAME_DOUBLING:
fc9fe428 218 s->repeat_pict = 3;
346db3ef
IS
219 break;
220 case SEI_PIC_STRUCT_FRAME_TRIPLING:
fc9fe428 221 s->repeat_pict = 5;
346db3ef
IS
222 break;
223 default:
fc9fe428 224 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
346db3ef
IS
225 break;
226 }
227 } else {
fc9fe428 228 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
346db3ef
IS
229 }
230
ff6474dd
IS
231 return 0; /* no need to evaluate the rest */
232 }
233 buf += consumed;
234 }
235 /* didn't find a picture! */
236 av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit\n");
237 return -1;
238}
239
26b4fe82
AJ
240static 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->s.parse_context;
247 int next;
248
82f1ffc7
HC
249 if (!h->got_first) {
250 h->got_first = 1;
23584bec
HC
251 if (avctx->extradata_size) {
252 h->s.avctx = avctx;
790a367d
RT
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->s.low_delay = 1;
23584bec
HC
260 ff_h264_decode_extradata(h);
261 }
262 }
263
26b4fe82
AJ
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 }
a26ce1e2 279 }
ff6474dd 280
25f05ddb 281 parse_nal_units(s, avctx, buf, buf_size);
c733922e 282
25f05ddb
PL
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 }
26b4fe82
AJ
296
297 *poutbuf = buf;
298 *poutbuf_size = buf_size;
299 return next;
300}
301
302static 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
dd990075 326static void close(AVCodecParserContext *s)
3ee4f5e4
MN
327{
328 H264Context *h = s->priv_data;
329 ParseContext *pc = &h->s.parse_context;
330
331 av_free(pc->buffer);
15861962 332 ff_h264_free_context(h);
3ee4f5e4
MN
333}
334
e9ca315d
RC
335static int init(AVCodecParserContext *s)
336{
337 H264Context *h = s->priv_data;
338 h->thread_context[0] = h;
f907615f 339 h->s.slice_context_count = 1;
e9ca315d
RC
340 return 0;
341}
26b4fe82 342
d36beb3f 343AVCodecParser ff_h264_parser = {
36ef5369 344 .codec_ids = { AV_CODEC_ID_H264 },
5511ad14
AK
345 .priv_data_size = sizeof(H264Context),
346 .parser_init = init,
347 .parser_parse = h264_parse,
348 .parser_close = close,
349 .split = h264_split,
26b4fe82 350};