FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_parser.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 340 391 87.0%
Branches: 248 297 83.5%

Line Branch Exec Source
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 FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 / MPEG-4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 #include <assert.h>
31 #include <stdint.h>
32
33 #include "libavutil/avutil.h"
34 #include "libavutil/error.h"
35 #include "libavutil/log.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/pixfmt.h"
38
39 #include "avcodec.h"
40 #include "get_bits.h"
41 #include "golomb.h"
42 #include "h264.h"
43 #include "h264_sei.h"
44 #include "h264_ps.h"
45 #include "h264data.h"
46 #include "internal.h"
47 #include "mpegutils.h"
48 #include "parser.h"
49
50 typedef struct H264ParseContext {
51 ParseContext pc;
52 H264ParamSets ps;
53 H264DSPContext h264dsp;
54 H264POCContext poc;
55 H264SEIContext sei;
56 int is_avc;
57 int nal_length_size;
58 int got_first;
59 int picture_structure;
60 uint8_t parse_history[6];
61 int parse_history_count;
62 int parse_last_mb;
63 int64_t reference_dts;
64 int last_frame_num, last_picture_structure;
65 } H264ParseContext;
66
67
68 179997 static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
69 int buf_size, void *logctx)
70 {
71 int i, j;
72 uint32_t state;
73 179997 ParseContext *pc = &p->pc;
74
75
1/2
✓ Branch 0 taken 179997 times.
✗ Branch 1 not taken.
179997 int next_avc = p->is_avc ? 0 : buf_size;
76 // mb_addr= pc->mb_addr - 1;
77 179997 state = pc->state;
78
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 179912 times.
179997 if (state > 13)
79 85 state = 7;
80
81
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 179997 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
179997 if (p->is_avc && !p->nal_length_size)
82 av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
83
84
2/2
✓ Branch 0 taken 2495860 times.
✓ Branch 1 taken 155627 times.
2651487 for (i = 0; i < buf_size; i++) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2495860 times.
2495860 if (i >= next_avc) {
86 int64_t nalsize = 0;
87 i = next_avc;
88 for (j = 0; j < p->nal_length_size; j++)
89 nalsize = (nalsize << 8) | buf[i++];
90 if (!nalsize || nalsize > buf_size - i) {
91 av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal size %"PRId64" "
92 "remaining %d\n", nalsize, buf_size - i);
93 return buf_size;
94 }
95 next_avc = i + nalsize;
96 state = 5;
97 }
98
99
2/2
✓ Branch 0 taken 1179946 times.
✓ Branch 1 taken 1315914 times.
2495860 if (state == 7) {
100 1179946 i += p->h264dsp.startcode_find_candidate(buf + i, next_avc - i);
101
2/2
✓ Branch 0 taken 1027014 times.
✓ Branch 1 taken 152932 times.
1179946 if (i < next_avc)
102 1027014 state = 2;
103
2/2
✓ Branch 0 taken 1186706 times.
✓ Branch 1 taken 129208 times.
1315914 } else if (state <= 2) {
104
2/2
✓ Branch 0 taken 93957 times.
✓ Branch 1 taken 1092749 times.
1186706 if (buf[i] == 1)
105 93957 state ^= 5; // 2->7, 1->4, 0->5
106
2/2
✓ Branch 0 taken 933301 times.
✓ Branch 1 taken 159448 times.
1092749 else if (buf[i])
107 933301 state = 7;
108 else
109 159448 state >>= 1; // 2->1, 1->0, 0->0
110
2/2
✓ Branch 0 taken 69203 times.
✓ Branch 1 taken 60005 times.
129208 } else if (state <= 5) {
111 69203 int nalu_type = buf[i] & 0x1F;
112
6/6
✓ Branch 0 taken 67067 times.
✓ Branch 1 taken 2136 times.
✓ Branch 2 taken 65802 times.
✓ Branch 3 taken 1265 times.
✓ Branch 4 taken 54114 times.
✓ Branch 5 taken 11688 times.
69203 if (nalu_type == H264_NAL_SEI || nalu_type == H264_NAL_SPS ||
113
2/2
✓ Branch 0 taken 4463 times.
✓ Branch 1 taken 49651 times.
54114 nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD) {
114
2/2
✓ Branch 0 taken 8070 times.
✓ Branch 1 taken 11482 times.
19552 if (pc->frame_start_found) {
115 8070 i++;
116 8070 goto found;
117 }
118
5/6
✓ Branch 0 taken 1759 times.
✓ Branch 1 taken 47892 times.
✓ Branch 2 taken 1759 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1290 times.
✓ Branch 5 taken 469 times.
49651 } else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||
119 nalu_type == H264_NAL_IDR_SLICE) {
120 49182 state += 8;
121 49182 continue;
122 }
123 11951 state = 7;
124 } else {
125 60005 unsigned int mb, last_mb = p->parse_last_mb;
126 GetBitContext gb;
127 60005 p->parse_history[p->parse_history_count++] = buf[i];
128
129 60005 init_get_bits(&gb, p->parse_history, 8*p->parse_history_count);
130 60005 mb= get_ue_golomb_long(&gb);
131
3/4
✓ Branch 1 taken 10823 times.
✓ Branch 2 taken 49182 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10823 times.
60005 if (get_bits_left(&gb) > 0 || p->parse_history_count > 5) {
132 49182 p->parse_last_mb = mb;
133
2/2
✓ Branch 0 taken 24586 times.
✓ Branch 1 taken 24596 times.
49182 if (pc->frame_start_found) {
134
2/2
✓ Branch 0 taken 16300 times.
✓ Branch 1 taken 8286 times.
24586 if (mb <= last_mb) {
135 16300 i -= p->parse_history_count - 1;
136 16300 p->parse_history_count = 0;
137 16300 goto found;
138 }
139 } else
140 24596 pc->frame_start_found = 1;
141 32882 p->parse_history_count = 0;
142 32882 state = 7;
143 }
144 }
145 }
146 155627 pc->state = state;
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155627 times.
155627 if (p->is_avc)
148 return next_avc;
149 155627 return END_NOT_FOUND;
150
151 24370 found:
152 24370 pc->state = 7;
153 24370 pc->frame_start_found = 0;
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24370 times.
24370 if (p->is_avc)
155 return next_avc;
156 24370 return i - (state & 5);
157 }
158
159 18253 static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb,
160 void *logctx)
161 {
162 H264PredWeightTable pwt;
163 18253 int slice_type_nos = s->pict_type & 3;
164 18253 H264ParseContext *p = s->priv_data;
165 int list_count, ref_count[2];
166
167
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18253 times.
18253 if (p->ps.pps->redundant_pic_cnt_present)
169 get_ue_golomb(gb); // redundant_pic_count
170
171
2/2
✓ Branch 0 taken 850 times.
✓ Branch 1 taken 17403 times.
18253 if (slice_type_nos == AV_PICTURE_TYPE_B)
172 850 get_bits1(gb); // direct_spatial_mv_pred
173
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18253 times.
18253 if (ff_h264_parse_ref_count(&list_count, ref_count, gb, p->ps.pps,
175 slice_type_nos, p->picture_structure, logctx) < 0)
176 return AVERROR_INVALIDDATA;
177
178
2/2
✓ Branch 0 taken 16494 times.
✓ Branch 1 taken 1759 times.
18253 if (slice_type_nos != AV_PICTURE_TYPE_I) {
179 int list;
180
2/2
✓ Branch 0 taken 17344 times.
✓ Branch 1 taken 16494 times.
33838 for (list = 0; list < list_count; list++) {
181
2/2
✓ Branch 1 taken 2813 times.
✓ Branch 2 taken 14531 times.
17344 if (get_bits1(gb)) {
182 int index;
183 13609 for (index = 0; ; index++) {
184 13609 unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(gb);
185
186
2/2
✓ Branch 0 taken 10796 times.
✓ Branch 1 taken 2813 times.
13609 if (reordering_of_pic_nums_idc < 3)
187 10796 get_ue_golomb_long(gb);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2813 times.
2813 else if (reordering_of_pic_nums_idc > 3) {
189 av_log(logctx, AV_LOG_ERROR,
190 "illegal reordering_of_pic_nums_idc %d\n",
191 reordering_of_pic_nums_idc);
192 return AVERROR_INVALIDDATA;
193 } else
194 2813 break;
195
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10796 times.
10796 if (index >= ref_count[list]) {
197 av_log(logctx, AV_LOG_ERROR,
198 "reference count %d overflow\n", index);
199 return AVERROR_INVALIDDATA;
200 }
201 }
202 }
203 }
204 }
205
206
4/4
✓ Branch 0 taken 2554 times.
✓ Branch 1 taken 15699 times.
✓ Branch 2 taken 481 times.
✓ Branch 3 taken 2073 times.
18253 if ((p->ps.pps->weighted_pred && slice_type_nos == AV_PICTURE_TYPE_P) ||
207
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 16162 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
16180 (p->ps.pps->weighted_bipred_idc == 1 && slice_type_nos == AV_PICTURE_TYPE_B))
208 2091 ff_h264_pred_weight_table(gb, p->ps.sps, ref_count, slice_type_nos,
209 &pwt, p->picture_structure, logctx);
210
211
2/2
✓ Branch 1 taken 1928 times.
✓ Branch 2 taken 16325 times.
18253 if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
212 int i;
213
1/2
✓ Branch 0 taken 5527 times.
✗ Branch 1 not taken.
5527 for (i = 0; i < MAX_MMCO_COUNT; i++) {
214 5527 MMCOOpcode opcode = get_ue_golomb_31(gb);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5527 times.
5527 if (opcode > (unsigned) MMCO_LONG) {
216 av_log(logctx, AV_LOG_ERROR,
217 "illegal memory management control operation %d\n",
218 opcode);
219 return AVERROR_INVALIDDATA;
220 }
221
2/2
✓ Branch 0 taken 1878 times.
✓ Branch 1 taken 3649 times.
5527 if (opcode == MMCO_END)
222 1878 return 0;
223
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 3599 times.
3649 else if (opcode == MMCO_RESET)
224 50 return 1;
225
226
4/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 2791 times.
✓ Branch 2 taken 333 times.
✓ Branch 3 taken 475 times.
3599 if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
227 3124 get_ue_golomb_long(gb); // difference_of_pic_nums_minus1
228
6/6
✓ Branch 0 taken 3266 times.
✓ Branch 1 taken 333 times.
✓ Branch 2 taken 3186 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 3170 times.
✓ Branch 5 taken 16 times.
3599 if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
229
2/2
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 2791 times.
3170 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
230 808 get_ue_golomb_31(gb);
231 }
232 }
233
234 16325 return 0;
235 }
236
237 /**
238 * Parse NAL units of found picture and decode some basic information.
239 *
240 * @param s parser context.
241 * @param avctx codec context.
242 * @param buf buffer with field/frame data.
243 * @param buf_size size of the buffer.
244 */
245 30533 static inline int parse_nal_units(AVCodecParserContext *s,
246 AVCodecContext *avctx,
247 const uint8_t * const buf, int buf_size)
248 {
249 30533 H264ParseContext *p = s->priv_data;
250 30533 H264Context *h = avctx->priv_data;
251 30533 H2645RBSP rbsp = { NULL };
252 30533 H2645NAL nal = { NULL };
253 int buf_index, next_avc;
254 unsigned int pps_id;
255 unsigned int slice_type;
256 30533 int state = -1, got_reset = 0;
257
3/4
✓ Branch 0 taken 30240 times.
✓ Branch 1 taken 293 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30240 times.
30533 int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
258 int field_poc[2];
259 int ret;
260
261 /* set some sane default values */
262 30533 s->pict_type = AV_PICTURE_TYPE_I;
263 30533 s->key_frame = 0;
264 30533 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
265
266 30533 ff_h264_sei_uninit(&p->sei);
267 30533 p->sei.frame_packing.arrangement_cancel_flag = -1;
268 30533 p->sei.unregistered.x264_build = -1;
269
270
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 30240 times.
30533 if (!buf_size)
271 293 return 0;
272
273 30240 av_fast_padded_malloc(&rbsp.rbsp_buffer, &rbsp.rbsp_buffer_alloc_size, buf_size);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30240 times.
30240 if (!rbsp.rbsp_buffer)
275 return AVERROR(ENOMEM);
276
277 30240 buf_index = 0;
278
2/2
✓ Branch 0 taken 24680 times.
✓ Branch 1 taken 5560 times.
30240 next_avc = p->is_avc ? 0 : buf_size;
279 14518 for (;;) {
280 const SPS *sps;
281 44758 int src_length, consumed, nalsize = 0;
282
283
2/2
✓ Branch 0 taken 8407 times.
✓ Branch 1 taken 36351 times.
44758 if (buf_index >= next_avc) {
284 8407 nalsize = get_nalsize(p->nal_length_size, buf, buf_size, &buf_index, avctx);
285
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 8075 times.
8407 if (nalsize < 0)
286 332 break;
287 8075 next_avc = buf_index + nalsize;
288 } else {
289 36351 buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36351 times.
36351 if (buf_index >= buf_size)
291 break;
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36351 times.
36351 if (buf_index >= next_avc)
293 continue;
294 }
295 44426 src_length = next_avc - buf_index;
296
297 44426 state = buf[buf_index];
298
2/2
✓ Branch 0 taken 29908 times.
✓ Branch 1 taken 14518 times.
44426 switch (state & 0x1f) {
299 29908 case H264_NAL_SLICE:
300 case H264_NAL_IDR_SLICE:
301 // Do not walk the whole buffer just to decode slice header
302
4/4
✓ Branch 0 taken 29114 times.
✓ Branch 1 taken 794 times.
✓ Branch 2 taken 10319 times.
✓ Branch 3 taken 18795 times.
29908 if ((state & 0x1f) == H264_NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
303 /* IDR or disposable slice
304 * No need to decode many bytes because MMCOs shall not be present. */
305
2/2
✓ Branch 0 taken 10790 times.
✓ Branch 1 taken 323 times.
11113 if (src_length > 60)
306 10790 src_length = 60;
307 } else {
308 /* To decode up to MMCOs */
309
2/2
✓ Branch 0 taken 10648 times.
✓ Branch 1 taken 8147 times.
18795 if (src_length > 1000)
310 10648 src_length = 1000;
311 }
312 29908 break;
313 }
314 44426 consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &rbsp, &nal, 1);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44426 times.
44426 if (consumed < 0)
316 break;
317
318 44426 buf_index += consumed;
319
320 44426 ret = init_get_bits8(&nal.gb, nal.data, nal.size);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44426 times.
44426 if (ret < 0)
322 goto fail;
323 44426 get_bits1(&nal.gb);
324 44426 nal.ref_idc = get_bits(&nal.gb, 2);
325 44426 nal.type = get_bits(&nal.gb, 5);
326
327
6/6
✓ Branch 0 taken 838 times.
✓ Branch 1 taken 6979 times.
✓ Branch 2 taken 3431 times.
✓ Branch 3 taken 794 times.
✓ Branch 4 taken 29114 times.
✓ Branch 5 taken 3270 times.
44426 switch (nal.type) {
328 838 case H264_NAL_SPS:
329 838 ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps, 0);
330 838 break;
331 6979 case H264_NAL_PPS:
332 6979 ff_h264_decode_picture_parameter_set(&nal.gb, avctx, &p->ps,
333 nal.size_bits);
334 6979 break;
335 3431 case H264_NAL_SEI:
336 3431 ff_h264_sei_decode(&p->sei, &nal.gb, &p->ps, avctx);
337 3431 break;
338 794 case H264_NAL_IDR_SLICE:
339 794 s->key_frame = 1;
340
341 794 p->poc.prev_frame_num = 0;
342 794 p->poc.prev_frame_num_offset = 0;
343 794 p->poc.prev_poc_msb =
344 794 p->poc.prev_poc_lsb = 0;
345 /* fall through */
346 29908 case H264_NAL_SLICE:
347 29908 get_ue_golomb_long(&nal.gb); // skip first_mb_in_slice
348 29908 slice_type = get_ue_golomb_31(&nal.gb);
349 29908 s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
350
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 29750 times.
29908 if (p->sei.recovery_point.recovery_frame_cnt >= 0) {
351 /* key frame, since recovery_frame_cnt is set */
352 158 s->key_frame = 1;
353 }
354 29908 pps_id = get_ue_golomb(&nal.gb);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29908 times.
29908 if (pps_id >= MAX_PPS_COUNT) {
356 av_log(avctx, AV_LOG_ERROR,
357 "pps_id %u out of range\n", pps_id);
358 goto fail;
359 }
360
2/2
✓ Branch 0 taken 568 times.
✓ Branch 1 taken 29340 times.
29908 if (!p->ps.pps_list[pps_id]) {
361 568 av_log(avctx, AV_LOG_ERROR,
362 "non-existing PPS %u referenced\n", pps_id);
363 568 goto fail;
364 }
365
366 29340 av_buffer_unref(&p->ps.pps_ref);
367 29340 p->ps.pps = NULL;
368 29340 p->ps.sps = NULL;
369 29340 p->ps.pps_ref = av_buffer_ref(p->ps.pps_list[pps_id]);
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29340 times.
29340 if (!p->ps.pps_ref)
371 goto fail;
372 29340 p->ps.pps = (const PPS*)p->ps.pps_ref->data;
373 29340 p->ps.sps = p->ps.pps->sps;
374 29340 sps = p->ps.sps;
375
376 // heuristic to detect non marked keyframes
377
6/6
✓ Branch 0 taken 8321 times.
✓ Branch 1 taken 21019 times.
✓ Branch 2 taken 8187 times.
✓ Branch 3 taken 134 times.
✓ Branch 4 taken 1021 times.
✓ Branch 5 taken 7166 times.
29340 if (p->ps.sps->ref_frame_count <= 1 && p->ps.pps->ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
378 1021 s->key_frame = 1;
379
380 29340 p->poc.frame_num = get_bits(&nal.gb, sps->log2_max_frame_num);
381
382 29340 s->coded_width = 16 * sps->mb_width;
383 29340 s->coded_height = 16 * sps->mb_height;
384 29340 s->width = s->coded_width - (sps->crop_right + sps->crop_left);
385 29340 s->height = s->coded_height - (sps->crop_top + sps->crop_bottom);
386
2/4
✓ Branch 0 taken 29340 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29340 times.
29340 if (s->width <= 0 || s->height <= 0) {
387 s->width = s->coded_width;
388 s->height = s->coded_height;
389 }
390
391
3/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 219 times.
✓ Branch 2 taken 28971 times.
✗ Branch 3 not taken.
29340 switch (sps->bit_depth_luma) {
392 150 case 9:
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P9;
394
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 100 times.
150 else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P9;
395 100 else s->format = AV_PIX_FMT_YUV420P9;
396 150 break;
397 219 case 10:
398
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 169 times.
219 if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P10;
399
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 82 times.
169 else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P10;
400 82 else s->format = AV_PIX_FMT_YUV420P10;
401 219 break;
402 28971 case 8:
403
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 28155 times.
28971 if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P;
404
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 28145 times.
28155 else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P;
405 28145 else s->format = AV_PIX_FMT_YUV420P;
406 28971 break;
407 default:
408 s->format = AV_PIX_FMT_NONE;
409 }
410
411 29340 avctx->profile = ff_h264_get_profile(sps);
412 29340 avctx->level = sps->level_idc;
413
414
2/2
✓ Branch 0 taken 20726 times.
✓ Branch 1 taken 8614 times.
29340 if (sps->frame_mbs_only_flag) {
415 20726 p->picture_structure = PICT_FRAME;
416 } else {
417
2/2
✓ Branch 1 taken 6222 times.
✓ Branch 2 taken 2392 times.
8614 if (get_bits1(&nal.gb)) { // field_pic_flag
418 6222 p->picture_structure = PICT_TOP_FIELD + get_bits1(&nal.gb); // bottom_field_flag
419 } else {
420 2392 p->picture_structure = PICT_FRAME;
421 }
422 }
423
424
2/2
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 28548 times.
29340 if (nal.type == H264_NAL_IDR_SLICE)
425 792 get_ue_golomb_long(&nal.gb); /* idr_pic_id */
426
2/2
✓ Branch 0 taken 22389 times.
✓ Branch 1 taken 6951 times.
29340 if (sps->poc_type == 0) {
427 22389 p->poc.poc_lsb = get_bits(&nal.gb, sps->log2_max_poc_lsb);
428
429
2/2
✓ Branch 0 taken 3552 times.
✓ Branch 1 taken 18837 times.
22389 if (p->ps.pps->pic_order_present == 1 &&
430
2/2
✓ Branch 0 taken 2655 times.
✓ Branch 1 taken 897 times.
3552 p->picture_structure == PICT_FRAME)
431 2655 p->poc.delta_poc_bottom = get_se_golomb(&nal.gb);
432 }
433
434
2/2
✓ Branch 0 taken 3245 times.
✓ Branch 1 taken 26095 times.
29340 if (sps->poc_type == 1 &&
435
2/2
✓ Branch 0 taken 3071 times.
✓ Branch 1 taken 174 times.
3245 !sps->delta_pic_order_always_zero_flag) {
436 3071 p->poc.delta_poc[0] = get_se_golomb(&nal.gb);
437
438
2/2
✓ Branch 0 taken 692 times.
✓ Branch 1 taken 2379 times.
3071 if (p->ps.pps->pic_order_present == 1 &&
439
2/2
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 286 times.
692 p->picture_structure == PICT_FRAME)
440 406 p->poc.delta_poc[1] = get_se_golomb(&nal.gb);
441 }
442
443 /* Decode POC of this picture.
444 * The prev_ values needed for decoding POC of the next picture are not set here. */
445 29340 field_poc[0] = field_poc[1] = INT_MAX;
446 29340 ret = ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
447 &p->poc, p->picture_structure, nal.ref_idc);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29340 times.
29340 if (ret < 0)
449 goto fail;
450
451 /* Continue parsing to check if MMCO_RESET is present.
452 * FIXME: MMCO_RESET could appear in non-first slice.
453 * Maybe, we should parse all undisposable non-IDR slice of this
454 * picture until encountering MMCO_RESET in a slice of it. */
455
4/4
✓ Branch 0 taken 19045 times.
✓ Branch 1 taken 10295 times.
✓ Branch 2 taken 18253 times.
✓ Branch 3 taken 792 times.
29340 if (nal.ref_idc && nal.type != H264_NAL_IDR_SLICE) {
456 18253 got_reset = scan_mmco_reset(s, &nal.gb, avctx);
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18253 times.
18253 if (got_reset < 0)
458 goto fail;
459 }
460
461 /* Set up the prev_ values for decoding POC of the next picture. */
462
2/2
✓ Branch 0 taken 29290 times.
✓ Branch 1 taken 50 times.
29340 p->poc.prev_frame_num = got_reset ? 0 : p->poc.frame_num;
463
2/2
✓ Branch 0 taken 29290 times.
✓ Branch 1 taken 50 times.
29340 p->poc.prev_frame_num_offset = got_reset ? 0 : p->poc.frame_num_offset;
464
2/2
✓ Branch 0 taken 19045 times.
✓ Branch 1 taken 10295 times.
29340 if (nal.ref_idc != 0) {
465
2/2
✓ Branch 0 taken 18995 times.
✓ Branch 1 taken 50 times.
19045 if (!got_reset) {
466 18995 p->poc.prev_poc_msb = p->poc.poc_msb;
467 18995 p->poc.prev_poc_lsb = p->poc.poc_lsb;
468 } else {
469 50 p->poc.prev_poc_msb = 0;
470 50 p->poc.prev_poc_lsb =
471
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 p->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
472 }
473 }
474
475
2/2
✓ Branch 0 taken 2384 times.
✓ Branch 1 taken 26956 times.
29340 if (p->sei.picture_timing.present) {
476 2384 ret = ff_h264_sei_process_picture_timing(&p->sei.picture_timing,
477 sps, avctx);
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2384 times.
2384 if (ret < 0) {
479 av_log(avctx, AV_LOG_ERROR, "Error processing the picture timing SEI\n");
480 p->sei.picture_timing.present = 0;
481 }
482 }
483
484
4/4
✓ Branch 0 taken 2780 times.
✓ Branch 1 taken 26560 times.
✓ Branch 2 taken 2372 times.
✓ Branch 3 taken 408 times.
29340 if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
485
3/6
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 1472 times.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4744 switch (p->sei.picture_timing.pic_struct) {
486 762 case H264_SEI_PIC_STRUCT_TOP_FIELD:
487 case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
488 762 s->repeat_pict = 0;
489 762 break;
490 1472 case H264_SEI_PIC_STRUCT_FRAME:
491 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
492 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
493 1472 s->repeat_pict = 1;
494 1472 break;
495 138 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
496 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
497 138 s->repeat_pict = 2;
498 138 break;
499 case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
500 s->repeat_pict = 3;
501 break;
502 case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
503 s->repeat_pict = 5;
504 break;
505 default:
506 s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
507 break;
508 }
509 } else {
510 26968 s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
511 }
512
513
2/2
✓ Branch 0 taken 23118 times.
✓ Branch 1 taken 6222 times.
29340 if (p->picture_structure == PICT_FRAME) {
514 23118 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
515
4/4
✓ Branch 0 taken 1689 times.
✓ Branch 1 taken 21429 times.
✓ Branch 2 taken 1610 times.
✓ Branch 3 taken 79 times.
23118 if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
516
3/3
✓ Branch 0 taken 885 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 575 times.
3220 switch (p->sei.picture_timing.pic_struct) {
517 885 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
518 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
519 885 s->field_order = AV_FIELD_TT;
520 885 break;
521 150 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
522 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
523 150 s->field_order = AV_FIELD_BB;
524 150 break;
525 575 default:
526 575 s->field_order = AV_FIELD_PROGRESSIVE;
527 575 break;
528 }
529 } else {
530
2/2
✓ Branch 0 taken 1041 times.
✓ Branch 1 taken 20467 times.
21508 if (field_poc[0] < field_poc[1])
531 1041 s->field_order = AV_FIELD_TT;
532
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 20464 times.
20467 else if (field_poc[0] > field_poc[1])
533 3 s->field_order = AV_FIELD_BB;
534 else
535 20464 s->field_order = AV_FIELD_PROGRESSIVE;
536 }
537 } else {
538
2/2
✓ Branch 0 taken 3203 times.
✓ Branch 1 taken 3019 times.
6222 if (p->picture_structure == PICT_TOP_FIELD)
539 3203 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
540 else
541 3019 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
542
2/2
✓ Branch 0 taken 4714 times.
✓ Branch 1 taken 1508 times.
6222 if (p->poc.frame_num == p->last_frame_num &&
543
1/2
✓ Branch 0 taken 4714 times.
✗ Branch 1 not taken.
4714 p->last_picture_structure != AV_PICTURE_STRUCTURE_UNKNOWN &&
544
1/2
✓ Branch 0 taken 4714 times.
✗ Branch 1 not taken.
4714 p->last_picture_structure != AV_PICTURE_STRUCTURE_FRAME &&
545
2/2
✓ Branch 0 taken 4597 times.
✓ Branch 1 taken 117 times.
4714 p->last_picture_structure != s->picture_structure) {
546
2/2
✓ Branch 0 taken 3013 times.
✓ Branch 1 taken 1584 times.
4597 if (p->last_picture_structure == AV_PICTURE_STRUCTURE_TOP_FIELD)
547 3013 s->field_order = AV_FIELD_TT;
548 else
549 1584 s->field_order = AV_FIELD_BB;
550 } else {
551 1625 s->field_order = AV_FIELD_UNKNOWN;
552 }
553 6222 p->last_picture_structure = s->picture_structure;
554 6222 p->last_frame_num = p->poc.frame_num;
555 }
556
4/4
✓ Branch 0 taken 15453 times.
✓ Branch 1 taken 13887 times.
✓ Branch 2 taken 2053 times.
✓ Branch 3 taken 13400 times.
29340 if (h && sps->timing_info_present_flag) {
557 2053 int64_t den = sps->time_scale;
558
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 2019 times.
2053 if (p->sei.unregistered.x264_build >= 0)
559 34 h->x264_build = p->sei.unregistered.x264_build;
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2053 times.
2053 if (h->x264_build < 44U)
561 den *= 2;
562 2053 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
563 2053 sps->num_units_in_tick * avctx->ticks_per_frame, den, 1 << 30);
564 }
565
566 29340 av_freep(&rbsp.rbsp_buffer);
567 29340 return 0; /* no need to evaluate the rest */
568 }
569 }
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 if (q264) {
571 av_freep(&rbsp.rbsp_buffer);
572 return 0;
573 }
574 /* didn't find a picture! */
575 332 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
576 900 fail:
577 900 av_freep(&rbsp.rbsp_buffer);
578 900 return -1;
579 }
580
581 185604 static int h264_parse(AVCodecParserContext *s,
582 AVCodecContext *avctx,
583 const uint8_t **poutbuf, int *poutbuf_size,
584 const uint8_t *buf, int buf_size)
585 {
586 185604 H264ParseContext *p = s->priv_data;
587 185604 ParseContext *pc = &p->pc;
588 int next;
589
590
2/2
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 185198 times.
185604 if (!p->got_first) {
591 406 p->got_first = 1;
592
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 222 times.
406 if (avctx->extradata_size) {
593 184 ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
594 &p->ps, &p->is_avc, &p->nal_length_size,
595 avctx->err_recognition, avctx);
596 }
597 }
598
599
2/2
✓ Branch 0 taken 5715 times.
✓ Branch 1 taken 179889 times.
185604 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
600 5715 next = buf_size;
601 } else {
602 179889 next = h264_find_frame_end(p, buf, buf_size, avctx);
603
604
2/2
✓ Branch 1 taken 155071 times.
✓ Branch 2 taken 24818 times.
179889 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
605 155071 *poutbuf = NULL;
606 155071 *poutbuf_size = 0;
607 155071 return buf_size;
608 }
609
610
4/4
✓ Branch 0 taken 556 times.
✓ Branch 1 taken 24262 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 448 times.
24818 if (next < 0 && next != END_NOT_FOUND) {
611 av_assert1(pc->last_index + next >= 0);
612 108 h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next, avctx); // update state
613 }
614 }
615
616 30533 parse_nal_units(s, avctx, buf, buf_size);
617
618
2/2
✓ Branch 0 taken 29655 times.
✓ Branch 1 taken 878 times.
30533 if (avctx->framerate.num)
619 29655 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
620
2/2
✓ Branch 0 taken 1431 times.
✓ Branch 1 taken 29102 times.
30533 if (p->sei.picture_timing.cpb_removal_delay >= 0) {
621 1431 s->dts_sync_point = p->sei.buffering_period.present;
622 1431 s->dts_ref_dts_delta = p->sei.picture_timing.cpb_removal_delay;
623 1431 s->pts_dts_delta = p->sei.picture_timing.dpb_output_delay;
624 } else {
625 29102 s->dts_sync_point = INT_MIN;
626 29102 s->dts_ref_dts_delta = INT_MIN;
627 29102 s->pts_dts_delta = INT_MIN;
628 }
629
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30533 times.
30533 if (s->flags & PARSER_FLAG_ONCE) {
631 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
632 }
633
634
2/2
✓ Branch 0 taken 1431 times.
✓ Branch 1 taken 29102 times.
30533 if (s->dts_sync_point >= 0) {
635 1431 int64_t den = avctx->time_base.den * (int64_t)avctx->pkt_timebase.num;
636
1/2
✓ Branch 0 taken 1431 times.
✗ Branch 1 not taken.
1431 if (den > 0) {
637 1431 int64_t num = avctx->time_base.num * (int64_t)avctx->pkt_timebase.den;
638
2/2
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 402 times.
1431 if (s->dts != AV_NOPTS_VALUE) {
639 // got DTS from the stream, update reference timestamp
640 1029 p->reference_dts = s->dts - av_rescale(s->dts_ref_dts_delta, num, den);
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 402 times.
402 } else if (p->reference_dts != AV_NOPTS_VALUE) {
642 // compute DTS based on reference timestamp
643 s->dts = p->reference_dts + av_rescale(s->dts_ref_dts_delta, num, den);
644 }
645
646
3/4
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 402 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1029 times.
1431 if (p->reference_dts != AV_NOPTS_VALUE && s->pts == AV_NOPTS_VALUE)
647 s->pts = s->dts + av_rescale(s->pts_dts_delta, num, den);
648
649
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 1322 times.
1431 if (s->dts_sync_point > 0)
650 109 p->reference_dts = s->dts; // new reference
651 }
652 }
653
654 30533 *poutbuf = buf;
655 30533 *poutbuf_size = buf_size;
656 30533 return next;
657 }
658
659 407 static void h264_close(AVCodecParserContext *s)
660 {
661 407 H264ParseContext *p = s->priv_data;
662 407 ParseContext *pc = &p->pc;
663
664 407 av_freep(&pc->buffer);
665
666 407 ff_h264_sei_uninit(&p->sei);
667 407 ff_h264_ps_uninit(&p->ps);
668 407 }
669
670 407 static av_cold int init(AVCodecParserContext *s)
671 {
672 407 H264ParseContext *p = s->priv_data;
673
674 407 p->reference_dts = AV_NOPTS_VALUE;
675 407 p->last_frame_num = INT_MAX;
676 407 ff_h264dsp_init(&p->h264dsp, 8, 1);
677 407 return 0;
678 }
679
680 const AVCodecParser ff_h264_parser = {
681 .codec_ids = { AV_CODEC_ID_H264 },
682 .priv_data_size = sizeof(H264ParseContext),
683 .parser_init = init,
684 .parser_parse = h264_parse,
685 .parser_close = h264_close,
686 };
687