Commit | Line | Data |
---|---|---|
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" |
e0c16e4e | 33 | #include "mpegutils.h" |
26b4fe82 AJ |
34 | |
35 | #include <assert.h> | |
36 | ||
9404a47a AK |
37 | typedef struct H264ParseContext { |
38 | H264Context h; | |
39 | ParseContext pc; | |
40 | int got_first; | |
41 | } H264ParseContext; | |
26b4fe82 | 42 | |
9404a47a AK |
43 | |
44 | static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf, | |
f8a4d5e9 | 45 | int buf_size) |
26b4fe82 | 46 | { |
9404a47a | 47 | H264Context *h = &p->h; |
26b4fe82 AJ |
48 | int i; |
49 | uint32_t state; | |
9404a47a | 50 | ParseContext *pc = &p->pc; |
26b4fe82 | 51 | // mb_addr= pc->mb_addr - 1; |
f8a4d5e9 LB |
52 | state = pc->state; |
53 | if (state > 13) | |
54 | state = 7; | |
26b4fe82 | 55 | |
f8a4d5e9 LB |
56 | for (i = 0; i < buf_size; i++) { |
57 | if (state == 7) { | |
db7f1c7c | 58 | i += h->h264dsp.startcode_find_candidate(buf + i, buf_size - i); |
218d6844 BA |
59 | if (i < buf_size) |
60 | state = 2; | |
f8a4d5e9 LB |
61 | } else if (state <= 2) { |
62 | if (buf[i] == 1) | |
63 | state ^= 5; // 2->7, 1->4, 0->5 | |
64 | else if (buf[i]) | |
65 | state = 7; | |
66 | else | |
67 | state >>= 1; // 2->1, 1->0, 0->0 | |
68 | } else if (state <= 5) { | |
dc971acf VG |
69 | int nalu_type = buf[i] & 0x1F; |
70 | if (nalu_type == NAL_SEI || nalu_type == NAL_SPS || | |
71 | nalu_type == NAL_PPS || nalu_type == NAL_AUD) { | |
f8a4d5e9 | 72 | if (pc->frame_start_found) { |
26b4fe82 | 73 | i++; |
9aa1cfec | 74 | goto found; |
26b4fe82 | 75 | } |
dc971acf VG |
76 | } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA || |
77 | nalu_type == NAL_IDR_SLICE) { | |
f8a4d5e9 LB |
78 | if (pc->frame_start_found) { |
79 | state += 8; | |
26b4fe82 | 80 | continue; |
f8a4d5e9 | 81 | } else |
26b4fe82 AJ |
82 | pc->frame_start_found = 1; |
83 | } | |
f8a4d5e9 LB |
84 | state = 7; |
85 | } else { | |
58ae8d59 | 86 | // first_mb_in_slice is 0, probably the first nal of a new slice |
f8a4d5e9 | 87 | if (buf[i] & 0x80) |
26b4fe82 | 88 | goto found; |
f8a4d5e9 | 89 | state = 7; |
26b4fe82 AJ |
90 | } |
91 | } | |
f8a4d5e9 | 92 | pc->state = state; |
26b4fe82 | 93 | return END_NOT_FOUND; |
9aa1cfec DP |
94 | |
95 | found: | |
f8a4d5e9 LB |
96 | pc->state = 7; |
97 | pc->frame_start_found = 0; | |
98 | return i - (state & 5); | |
26b4fe82 AJ |
99 | } |
100 | ||
4baba6c8 YN |
101 | static int scan_mmco_reset(AVCodecParserContext *s) |
102 | { | |
9404a47a AK |
103 | H264ParseContext *p = s->priv_data; |
104 | H264Context *h = &p->h; | |
92c6c2a6 | 105 | H264SliceContext *sl = &h->slice_ctx[0]; |
4baba6c8 | 106 | |
56febc99 | 107 | sl->slice_type_nos = s->pict_type & 3; |
4baba6c8 YN |
108 | |
109 | if (h->pps.redundant_pic_cnt_present) | |
110 | get_ue_golomb(&h->gb); // redundant_pic_count | |
111 | ||
56febc99 | 112 | if (ff_set_ref_count(h, sl) < 0) |
4baba6c8 YN |
113 | return AVERROR_INVALIDDATA; |
114 | ||
56febc99 | 115 | if (sl->slice_type_nos != AV_PICTURE_TYPE_I) { |
4baba6c8 | 116 | int list; |
95eb35f3 | 117 | for (list = 0; list < sl->list_count; list++) { |
4baba6c8 YN |
118 | if (get_bits1(&h->gb)) { |
119 | int index; | |
120 | for (index = 0; ; index++) { | |
121 | unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb); | |
122 | ||
123 | if (reordering_of_pic_nums_idc < 3) | |
124 | get_ue_golomb(&h->gb); | |
125 | else if (reordering_of_pic_nums_idc > 3) { | |
126 | av_log(h->avctx, AV_LOG_ERROR, | |
127 | "illegal reordering_of_pic_nums_idc %d\n", | |
128 | reordering_of_pic_nums_idc); | |
129 | return AVERROR_INVALIDDATA; | |
130 | } else | |
131 | break; | |
132 | ||
95eb35f3 | 133 | if (index >= sl->ref_count[list]) { |
73e8fab3 VG |
134 | av_log(h->avctx, AV_LOG_ERROR, |
135 | "reference count %d overflow\n", index); | |
4baba6c8 YN |
136 | return AVERROR_INVALIDDATA; |
137 | } | |
138 | } | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
56febc99 AK |
143 | if ((h->pps.weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) || |
144 | (h->pps.weighted_bipred_idc == 1 && sl->slice_type_nos == AV_PICTURE_TYPE_B)) | |
92c6c2a6 | 145 | ff_pred_weight_table(h, sl); |
4baba6c8 YN |
146 | |
147 | if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag | |
148 | int i; | |
149 | for (i = 0; i < MAX_MMCO_COUNT; i++) { | |
150 | MMCOOpcode opcode = get_ue_golomb_31(&h->gb); | |
151 | if (opcode > (unsigned) MMCO_LONG) { | |
152 | av_log(h->avctx, AV_LOG_ERROR, | |
153 | "illegal memory management control operation %d\n", | |
154 | opcode); | |
155 | return AVERROR_INVALIDDATA; | |
156 | } | |
157 | if (opcode == MMCO_END) | |
158 | return 0; | |
159 | else if (opcode == MMCO_RESET) | |
160 | return 1; | |
161 | ||
162 | if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG) | |
163 | get_ue_golomb(&h->gb); | |
164 | if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED || | |
165 | opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) | |
166 | get_ue_golomb_31(&h->gb); | |
167 | } | |
168 | } | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
adbfc605 | 173 | /** |
ff6474dd IS |
174 | * Parse NAL units of found picture and decode some basic information. |
175 | * | |
176 | * @param s parser context. | |
177 | * @param avctx codec context. | |
178 | * @param buf buffer with field/frame data. | |
179 | * @param buf_size size of the buffer. | |
180 | */ | |
181 | static inline int parse_nal_units(AVCodecParserContext *s, | |
182 | AVCodecContext *avctx, | |
183 | const uint8_t *buf, int buf_size) | |
184 | { | |
9404a47a AK |
185 | H264ParseContext *p = s->priv_data; |
186 | H264Context *h = &p->h; | |
ff6474dd | 187 | const uint8_t *buf_end = buf + buf_size; |
96c3da93 | 188 | unsigned int pps_id; |
ff6474dd | 189 | unsigned int slice_type; |
4baba6c8 | 190 | int state = -1, got_reset = 0; |
ff6474dd | 191 | const uint8_t *ptr; |
3f1a7ceb | 192 | int field_poc[2]; |
ff6474dd IS |
193 | |
194 | /* set some sane default values */ | |
f8a4d5e9 LB |
195 | s->pict_type = AV_PICTURE_TYPE_I; |
196 | s->key_frame = 0; | |
3f1a7ceb | 197 | s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN; |
ff6474dd | 198 | |
103d073d YN |
199 | h->avctx = avctx; |
200 | ff_h264_reset_sei(h); | |
ff6474dd | 201 | |
9479415e BC |
202 | if (!buf_size) |
203 | return 0; | |
204 | ||
f8a4d5e9 | 205 | for (;;) { |
ff6474dd | 206 | int src_length, dst_length, consumed; |
f1e93986 | 207 | buf = avpriv_find_start_code(buf, buf_end, &state); |
f8a4d5e9 | 208 | if (buf >= buf_end) |
ff6474dd IS |
209 | break; |
210 | --buf; | |
211 | src_length = buf_end - buf; | |
212 | switch (state & 0x1f) { | |
213 | case NAL_SLICE: | |
214 | case NAL_IDR_SLICE: | |
215 | // Do not walk the whole buffer just to decode slice header | |
19f53840 | 216 | if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) { |
4baba6c8 YN |
217 | /* IDR or disposable slice |
218 | * No need to decode many bytes because MMCOs shall not be present. */ | |
219 | if (src_length > 60) | |
220 | src_length = 60; | |
221 | } else { | |
222 | /* To decode up to MMCOs */ | |
223 | if (src_length > 1000) | |
224 | src_length = 1000; | |
225 | } | |
ff6474dd IS |
226 | break; |
227 | } | |
f8a4d5e9 | 228 | ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length); |
f929ab05 | 229 | if (!ptr || dst_length < 0) |
ff6474dd IS |
230 | break; |
231 | ||
f8a4d5e9 LB |
232 | init_get_bits(&h->gb, ptr, 8 * dst_length); |
233 | switch (h->nal_unit_type) { | |
ff6474dd IS |
234 | case NAL_SPS: |
235 | ff_h264_decode_seq_parameter_set(h); | |
236 | break; | |
237 | case NAL_PPS: | |
2c541554 | 238 | ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits); |
ff6474dd IS |
239 | break; |
240 | case NAL_SEI: | |
241 | ff_h264_decode_sei(h); | |
242 | break; | |
243 | case NAL_IDR_SLICE: | |
0ed260c7 | 244 | s->key_frame = 1; |
3f1a7ceb YN |
245 | |
246 | h->prev_frame_num = 0; | |
247 | h->prev_frame_num_offset = 0; | |
248 | h->prev_poc_msb = | |
249 | h->prev_poc_lsb = 0; | |
f8a4d5e9 | 250 | /* fall through */ |
ff6474dd | 251 | case NAL_SLICE: |
2c541554 | 252 | get_ue_golomb(&h->gb); // skip first_mb_in_slice |
f8a4d5e9 | 253 | slice_type = get_ue_golomb_31(&h->gb); |
ff6474dd | 254 | s->pict_type = golomb_to_pict_type[slice_type % 5]; |
0ed260c7 IS |
255 | if (h->sei_recovery_frame_cnt >= 0) { |
256 | /* key frame, since recovery_frame_cnt is set */ | |
257 | s->key_frame = 1; | |
258 | } | |
f8a4d5e9 LB |
259 | pps_id = get_ue_golomb(&h->gb); |
260 | if (pps_id >= MAX_PPS_COUNT) { | |
261 | av_log(h->avctx, AV_LOG_ERROR, | |
73e8fab3 | 262 | "pps_id %u out of range\n", pps_id); |
96c3da93 IS |
263 | return -1; |
264 | } | |
f8a4d5e9 LB |
265 | if (!h->pps_buffers[pps_id]) { |
266 | av_log(h->avctx, AV_LOG_ERROR, | |
73e8fab3 | 267 | "non-existing PPS %u referenced\n", pps_id); |
96c3da93 IS |
268 | return -1; |
269 | } | |
f8a4d5e9 LB |
270 | h->pps = *h->pps_buffers[pps_id]; |
271 | if (!h->sps_buffers[h->pps.sps_id]) { | |
272 | av_log(h->avctx, AV_LOG_ERROR, | |
73e8fab3 | 273 | "non-existing SPS %u referenced\n", h->pps.sps_id); |
96c3da93 IS |
274 | return -1; |
275 | } | |
f8a4d5e9 | 276 | h->sps = *h->sps_buffers[h->pps.sps_id]; |
2c541554 | 277 | h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num); |
96c3da93 | 278 | |
31d2039c AK |
279 | s->coded_width = 16 * h->sps.mb_width; |
280 | s->coded_height = 16 * h->sps.mb_height; | |
281 | s->width = s->coded_width - (h->sps.crop_right + h->sps.crop_left); | |
282 | s->height = s->coded_height - (h->sps.crop_top + h->sps.crop_bottom); | |
283 | if (s->width <= 0 || s->height <= 0) { | |
284 | s->width = s->coded_width; | |
285 | s->height = s->coded_height; | |
286 | } | |
287 | ||
288 | switch (h->sps.bit_depth_luma) { | |
289 | case 9: | |
290 | if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P9; | |
291 | else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P9; | |
292 | else s->format = AV_PIX_FMT_YUV420P9; | |
293 | break; | |
294 | case 10: | |
295 | if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P10; | |
296 | else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P10; | |
297 | else s->format = AV_PIX_FMT_YUV420P10; | |
298 | break; | |
299 | case 8: | |
300 | if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P; | |
301 | else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P; | |
302 | else s->format = AV_PIX_FMT_YUV420P; | |
303 | break; | |
304 | default: | |
305 | s->format = AV_PIX_FMT_NONE; | |
306 | } | |
307 | ||
fe9a3fbe | 308 | avctx->profile = ff_h264_get_profile(&h->sps); |
dd0cd3d2 RC |
309 | avctx->level = h->sps.level_idc; |
310 | ||
f8a4d5e9 LB |
311 | if (h->sps.frame_mbs_only_flag) { |
312 | h->picture_structure = PICT_FRAME; | |
313 | } else { | |
314 | if (get_bits1(&h->gb)) { // field_pic_flag | |
315 | h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag | |
96c3da93 | 316 | } else { |
f8a4d5e9 | 317 | h->picture_structure = PICT_FRAME; |
96c3da93 IS |
318 | } |
319 | } | |
320 | ||
3f1a7ceb YN |
321 | if (h->nal_unit_type == NAL_IDR_SLICE) |
322 | get_ue_golomb(&h->gb); /* idr_pic_id */ | |
323 | if (h->sps.poc_type == 0) { | |
324 | h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb); | |
325 | ||
f8a4d5e9 LB |
326 | if (h->pps.pic_order_present == 1 && |
327 | h->picture_structure == PICT_FRAME) | |
3f1a7ceb YN |
328 | h->delta_poc_bottom = get_se_golomb(&h->gb); |
329 | } | |
330 | ||
f8a4d5e9 LB |
331 | if (h->sps.poc_type == 1 && |
332 | !h->sps.delta_pic_order_always_zero_flag) { | |
3f1a7ceb YN |
333 | h->delta_poc[0] = get_se_golomb(&h->gb); |
334 | ||
f8a4d5e9 LB |
335 | if (h->pps.pic_order_present == 1 && |
336 | h->picture_structure == PICT_FRAME) | |
3f1a7ceb YN |
337 | h->delta_poc[1] = get_se_golomb(&h->gb); |
338 | } | |
339 | ||
4baba6c8 YN |
340 | /* Decode POC of this picture. |
341 | * The prev_ values needed for decoding POC of the next picture are not set here. */ | |
b81dbd6c | 342 | field_poc[0] = field_poc[1] = INT_MAX; |
a8b19271 | 343 | ff_init_poc(h, field_poc, &s->output_picture_number); |
3f1a7ceb | 344 | |
4baba6c8 YN |
345 | /* Continue parsing to check if MMCO_RESET is present. |
346 | * FIXME: MMCO_RESET could appear in non-first slice. | |
347 | * Maybe, we should parse all undisposable non-IDR slice of this | |
348 | * picture until encountering MMCO_RESET in a slice of it. */ | |
349 | if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) { | |
350 | got_reset = scan_mmco_reset(s); | |
351 | if (got_reset < 0) | |
352 | return got_reset; | |
353 | } | |
354 | ||
b81dbd6c | 355 | /* Set up the prev_ values for decoding POC of the next picture. */ |
4baba6c8 YN |
356 | h->prev_frame_num = got_reset ? 0 : h->frame_num; |
357 | h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset; | |
b81dbd6c | 358 | if (h->nal_ref_idc != 0) { |
4baba6c8 YN |
359 | if (!got_reset) { |
360 | h->prev_poc_msb = h->poc_msb; | |
361 | h->prev_poc_lsb = h->poc_lsb; | |
362 | } else { | |
363 | h->prev_poc_msb = 0; | |
364 | h->prev_poc_lsb = | |
365 | h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0]; | |
366 | } | |
b81dbd6c YN |
367 | } |
368 | ||
f8a4d5e9 | 369 | if (h->sps.pic_struct_present_flag) { |
346db3ef | 370 | switch (h->sei_pic_struct) { |
f8a4d5e9 LB |
371 | case SEI_PIC_STRUCT_TOP_FIELD: |
372 | case SEI_PIC_STRUCT_BOTTOM_FIELD: | |
373 | s->repeat_pict = 0; | |
374 | break; | |
375 | case SEI_PIC_STRUCT_FRAME: | |
376 | case SEI_PIC_STRUCT_TOP_BOTTOM: | |
377 | case SEI_PIC_STRUCT_BOTTOM_TOP: | |
378 | s->repeat_pict = 1; | |
379 | break; | |
380 | case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: | |
381 | case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: | |
382 | s->repeat_pict = 2; | |
383 | break; | |
384 | case SEI_PIC_STRUCT_FRAME_DOUBLING: | |
385 | s->repeat_pict = 3; | |
386 | break; | |
387 | case SEI_PIC_STRUCT_FRAME_TRIPLING: | |
388 | s->repeat_pict = 5; | |
389 | break; | |
390 | default: | |
391 | s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0; | |
392 | break; | |
346db3ef IS |
393 | } |
394 | } else { | |
2c541554 | 395 | s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0; |
346db3ef IS |
396 | } |
397 | ||
3f1a7ceb YN |
398 | if (h->picture_structure == PICT_FRAME) { |
399 | s->picture_structure = AV_PICTURE_STRUCTURE_FRAME; | |
400 | if (h->sps.pic_struct_present_flag) { | |
401 | switch (h->sei_pic_struct) { | |
f8a4d5e9 LB |
402 | case SEI_PIC_STRUCT_TOP_BOTTOM: |
403 | case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: | |
404 | s->field_order = AV_FIELD_TT; | |
405 | break; | |
406 | case SEI_PIC_STRUCT_BOTTOM_TOP: | |
407 | case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: | |
408 | s->field_order = AV_FIELD_BB; | |
409 | break; | |
410 | default: | |
411 | s->field_order = AV_FIELD_PROGRESSIVE; | |
412 | break; | |
3f1a7ceb YN |
413 | } |
414 | } else { | |
415 | if (field_poc[0] < field_poc[1]) | |
416 | s->field_order = AV_FIELD_TT; | |
417 | else if (field_poc[0] > field_poc[1]) | |
418 | s->field_order = AV_FIELD_BB; | |
419 | else | |
420 | s->field_order = AV_FIELD_PROGRESSIVE; | |
421 | } | |
422 | } else { | |
423 | if (h->picture_structure == PICT_TOP_FIELD) | |
424 | s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD; | |
425 | else | |
426 | s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD; | |
427 | s->field_order = AV_FIELD_UNKNOWN; | |
428 | } | |
429 | ||
ff6474dd IS |
430 | return 0; /* no need to evaluate the rest */ |
431 | } | |
432 | buf += consumed; | |
433 | } | |
434 | /* didn't find a picture! */ | |
2c541554 | 435 | av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n"); |
ff6474dd IS |
436 | return -1; |
437 | } | |
438 | ||
26b4fe82 AJ |
439 | static int h264_parse(AVCodecParserContext *s, |
440 | AVCodecContext *avctx, | |
441 | const uint8_t **poutbuf, int *poutbuf_size, | |
442 | const uint8_t *buf, int buf_size) | |
443 | { | |
9404a47a AK |
444 | H264ParseContext *p = s->priv_data; |
445 | H264Context *h = &p->h; | |
446 | ParseContext *pc = &p->pc; | |
26b4fe82 AJ |
447 | int next; |
448 | ||
9404a47a AK |
449 | if (!p->got_first) { |
450 | p->got_first = 1; | |
23584bec | 451 | if (avctx->extradata_size) { |
2c541554 | 452 | h->avctx = avctx; |
790a367d RT |
453 | // must be done like in the decoder. |
454 | // otherwise opening the parser, creating extradata, | |
455 | // and then closing and opening again | |
456 | // will cause has_b_frames to be always set. | |
457 | // NB: estimate_timings_from_pts behaves exactly like this. | |
458 | if (!avctx->has_b_frames) | |
2c541554 | 459 | h->low_delay = 1; |
23584bec HC |
460 | ff_h264_decode_extradata(h); |
461 | } | |
462 | } | |
463 | ||
f8a4d5e9 LB |
464 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
465 | next = buf_size; | |
466 | } else { | |
9404a47a | 467 | next = h264_find_frame_end(p, buf, buf_size); |
26b4fe82 AJ |
468 | |
469 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { | |
f8a4d5e9 | 470 | *poutbuf = NULL; |
26b4fe82 AJ |
471 | *poutbuf_size = 0; |
472 | return buf_size; | |
473 | } | |
474 | ||
f8a4d5e9 LB |
475 | if (next < 0 && next != END_NOT_FOUND) { |
476 | assert(pc->last_index + next >= 0); | |
9404a47a | 477 | h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state |
26b4fe82 | 478 | } |
a26ce1e2 | 479 | } |
ff6474dd | 480 | |
25f05ddb | 481 | parse_nal_units(s, avctx, buf, buf_size); |
c733922e | 482 | |
25f05ddb PL |
483 | if (h->sei_cpb_removal_delay >= 0) { |
484 | s->dts_sync_point = h->sei_buffering_period_present; | |
485 | s->dts_ref_dts_delta = h->sei_cpb_removal_delay; | |
486 | s->pts_dts_delta = h->sei_dpb_output_delay; | |
487 | } else { | |
488 | s->dts_sync_point = INT_MIN; | |
489 | s->dts_ref_dts_delta = INT_MIN; | |
490 | s->pts_dts_delta = INT_MIN; | |
491 | } | |
492 | ||
493 | if (s->flags & PARSER_FLAG_ONCE) { | |
494 | s->flags &= PARSER_FLAG_COMPLETE_FRAMES; | |
495 | } | |
26b4fe82 | 496 | |
f8a4d5e9 | 497 | *poutbuf = buf; |
26b4fe82 AJ |
498 | *poutbuf_size = buf_size; |
499 | return next; | |
500 | } | |
501 | ||
502 | static int h264_split(AVCodecContext *avctx, | |
503 | const uint8_t *buf, int buf_size) | |
504 | { | |
505 | int i; | |
506 | uint32_t state = -1; | |
f8a4d5e9 LB |
507 | int has_sps = 0; |
508 | ||
509 | for (i = 0; i <= buf_size; i++) { | |
510 | if ((state & 0xFFFFFF1F) == 0x107) | |
511 | has_sps = 1; | |
512 | /* if((state&0xFFFFFF1F) == 0x101 || | |
513 | * (state&0xFFFFFF1F) == 0x102 || | |
514 | * (state&0xFFFFFF1F) == 0x105) { | |
515 | * } | |
516 | */ | |
1b667269 JS |
517 | if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 && |
518 | (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 && | |
519 | (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d && | |
520 | (state & 0xFFFFFF1F) != 0x10f) { | |
f8a4d5e9 LB |
521 | if (has_sps) { |
522 | while (i > 4 && buf[i - 5] == 0) | |
523 | i--; | |
524 | return i - 4; | |
26b4fe82 AJ |
525 | } |
526 | } | |
f8a4d5e9 LB |
527 | if (i < buf_size) |
528 | state = (state << 8) | buf[i]; | |
26b4fe82 AJ |
529 | } |
530 | return 0; | |
531 | } | |
532 | ||
dd990075 | 533 | static void close(AVCodecParserContext *s) |
3ee4f5e4 | 534 | { |
9404a47a AK |
535 | H264ParseContext *p = s->priv_data; |
536 | H264Context *h = &p->h; | |
537 | ParseContext *pc = &p->pc; | |
3ee4f5e4 MN |
538 | |
539 | av_free(pc->buffer); | |
15861962 | 540 | ff_h264_free_context(h); |
3ee4f5e4 MN |
541 | } |
542 | ||
6fee1b90 | 543 | static av_cold int init(AVCodecParserContext *s) |
e9ca315d | 544 | { |
9404a47a AK |
545 | H264ParseContext *p = s->priv_data; |
546 | H264Context *h = &p->h; | |
92c6c2a6 AK |
547 | |
548 | h->slice_ctx = av_mallocz(sizeof(*h->slice_ctx)); | |
549 | if (!h->slice_ctx) | |
550 | return 0; | |
551 | h->nb_slice_ctx = 1; | |
552 | ||
f8a4d5e9 | 553 | h->thread_context[0] = h; |
2c541554 | 554 | h->slice_context_count = 1; |
7a82022e | 555 | ff_h264dsp_init(&h->h264dsp, 8, 1); |
e9ca315d RC |
556 | return 0; |
557 | } | |
26b4fe82 | 558 | |
d36beb3f | 559 | AVCodecParser ff_h264_parser = { |
36ef5369 | 560 | .codec_ids = { AV_CODEC_ID_H264 }, |
9404a47a | 561 | .priv_data_size = sizeof(H264ParseContext), |
5511ad14 AK |
562 | .parser_init = init, |
563 | .parser_parse = h264_parse, | |
564 | .parser_close = close, | |
565 | .split = h264_split, | |
26b4fe82 | 566 | }; |