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