FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2026-10-16 04:22:46
Exec Total Coverage
Lines: 154 172 89.5%
Functions: 5 5 100.0%
Branches: 101 126 80.2%

Line Branch Exec Source
1 /*
2 * HEVC Annex B format parser
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "libavutil/common.h"
24 #include "libavutil/mem.h"
25
26 #include "libavcodec/golomb.h"
27 #include "libavcodec/parser_internal.h"
28 #include "libavcodec/h2645_parse.h"
29 #include "libavcodec/parser.h"
30
31 #include "hevc.h"
32 #include "parse.h"
33 #include "ps.h"
34 #include "sei.h"
35
36 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
37
38 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
39 #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
40
41 typedef struct HEVCParserContext {
42 ParseContext pc;
43
44 H2645Packet pkt;
45 HEVCParamSets ps;
46 HEVCSEI sei;
47
48 int is_avc;
49 int nal_length_size;
50 int parsed_extradata;
51
52 int poc;
53 int pocTid0;
54 } HEVCParserContext;
55
56 13256 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
57 AVCodecContext *avctx)
58 {
59 13256 HEVCParserContext *ctx = s->priv_data;
60 13256 HEVCParamSets *ps = &ctx->ps;
61 13256 HEVCSEI *sei = &ctx->sei;
62 13256 GetBitContext *gb = &nal->gb;
63 const HEVCPPS *pps;
64 const HEVCSPS *sps;
65 const HEVCWindow *ow;
66 13256 int i, num = 0, den = 0;
67
68 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
69 enum HEVCSliceType slice_type;
70
71 13256 first_slice_in_pic_flag = get_bits1(gb);
72 13256 s->picture_structure = sei->picture_timing.picture_struct;
73 13256 s->field_order = sei->picture_timing.picture_struct;
74
75
3/4
✓ Branch 0 taken 521 times.
✓ Branch 1 taken 12735 times.
✓ Branch 2 taken 521 times.
✗ Branch 3 not taken.
13256 if (IS_IRAP_NAL(nal)) {
76 521 s->key_frame = 1;
77 521 skip_bits1(gb); // no_output_of_prior_pics_flag
78 }
79
80 13256 pps_id = get_ue_golomb(gb);
81
3/4
✓ Branch 0 taken 13256 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 13190 times.
13256 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
82 66 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
83 66 return AVERROR_INVALIDDATA;
84 }
85 13190 pps = ps->pps_list[pps_id];
86 13190 sps = pps->sps;
87
88 13190 ow = &sps->output_window;
89
90 13190 s->coded_width = sps->width;
91 13190 s->coded_height = sps->height;
92 13190 s->width = sps->width - ow->left_offset - ow->right_offset;
93 13190 s->height = sps->height - ow->top_offset - ow->bottom_offset;
94 13190 s->format = sps->pix_fmt;
95 13190 avctx->profile = sps->ptl.general_ptl.profile_idc;
96 13190 avctx->level = sps->ptl.general_ptl.level_idc;
97
98
2/2
✓ Branch 0 taken 365 times.
✓ Branch 1 taken 12825 times.
13190 if (sps->vps->vps_timing_info_present_flag) {
99 365 num = sps->vps->vps_num_units_in_tick;
100 365 den = sps->vps->vps_time_scale;
101
2/2
✓ Branch 0 taken 832 times.
✓ Branch 1 taken 11993 times.
12825 } else if (sps->vui.vui_timing_info_present_flag) {
102 832 num = sps->vui.vui_num_units_in_tick;
103 832 den = sps->vui.vui_time_scale;
104 }
105
106
3/4
✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 11993 times.
✓ Branch 2 taken 1197 times.
✗ Branch 3 not taken.
13190 if (num > 0 && den > 0)
107 1197 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
108 num, den, 1 << 30);
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13190 times.
13190 if (!first_slice_in_pic_flag) {
111 unsigned int slice_segment_addr;
112 int slice_address_length;
113
114 ✗ if (pps->dependent_slice_segments_enabled_flag)
115 ✗ dependent_slice_segment_flag = get_bits1(gb);
116 else
117 ✗ dependent_slice_segment_flag = 0;
118
119 ✗ slice_address_length = av_ceil_log2_c(sps->ctb_width *
120 ✗ sps->ctb_height);
121 ✗ slice_segment_addr = get_bitsz(gb, slice_address_length);
122 ✗ if (slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
123 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
124 slice_segment_addr);
125 ✗ return AVERROR_INVALIDDATA;
126 }
127 } else
128 13190 dependent_slice_segment_flag = 0;
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13190 times.
13190 if (dependent_slice_segment_flag)
131 ✗ return 0; /* break; */
132
133
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 13190 times.
13725 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
134 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
135
136 13190 slice_type = get_ue_golomb_31(gb);
137
5/6
✓ Branch 0 taken 12501 times.
✓ Branch 1 taken 689 times.
✓ Branch 2 taken 9665 times.
✓ Branch 3 taken 2836 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9665 times.
13190 if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
138 slice_type == HEVC_SLICE_B)) {
139 ✗ av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
140 slice_type);
141 ✗ return AVERROR_INVALIDDATA;
142 }
143
2/2
✓ Branch 0 taken 3525 times.
✓ Branch 1 taken 9665 times.
16715 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
144
2/2
✓ Branch 0 taken 2836 times.
✓ Branch 1 taken 689 times.
3525 slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
145 AV_PICTURE_TYPE_I;
146
147
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 12490 times.
13190 if (pps->output_flag_present_flag)
148 700 skip_bits1(gb); // pic_output_flag
149
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13190 times.
13190 if (sps->separate_colour_plane)
151 ✗ skip_bits(gb, 2); // colour_plane_id
152
153
4/4
✓ Branch 0 taken 12956 times.
✓ Branch 1 taken 234 times.
✓ Branch 2 taken 12937 times.
✓ Branch 3 taken 19 times.
13190 if (!IS_IDR_NAL(nal)) {
154 12937 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
155 12937 s->output_picture_number = ctx->poc =
156 12937 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
157 } else
158 253 s->output_picture_number = ctx->poc = 0;
159
160
2/2
✓ Branch 0 taken 12827 times.
✓ Branch 1 taken 363 times.
13190 if (nal->temporal_id == 0 &&
161
2/2
✓ Branch 0 taken 9321 times.
✓ Branch 1 taken 3506 times.
12827 nal->type != HEVC_NAL_TRAIL_N &&
162
1/2
✓ Branch 0 taken 9321 times.
✗ Branch 1 not taken.
9321 nal->type != HEVC_NAL_TSA_N &&
163
1/2
✓ Branch 0 taken 9321 times.
✗ Branch 1 not taken.
9321 nal->type != HEVC_NAL_STSA_N &&
164
2/2
✓ Branch 0 taken 9277 times.
✓ Branch 1 taken 44 times.
9321 nal->type != HEVC_NAL_RADL_N &&
165
2/2
✓ Branch 0 taken 8794 times.
✓ Branch 1 taken 483 times.
9277 nal->type != HEVC_NAL_RASL_N &&
166
2/2
✓ Branch 0 taken 8757 times.
✓ Branch 1 taken 37 times.
8794 nal->type != HEVC_NAL_RADL_R &&
167
2/2
✓ Branch 0 taken 8091 times.
✓ Branch 1 taken 666 times.
8757 nal->type != HEVC_NAL_RASL_R)
168 8091 ctx->pocTid0 = ctx->poc;
169
170 13190 return 1; /* no need to evaluate the rest */
171 }
172
173 /**
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 13257 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
182 int buf_size, AVCodecContext *avctx)
183 {
184 13257 HEVCParserContext *ctx = s->priv_data;
185 13257 HEVCParamSets *ps = &ctx->ps;
186 13257 HEVCSEI *sei = &ctx->sei;
187
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 13037 times.
13257 int flags = (H2645_FLAG_IS_NALFF * !!ctx->is_avc) | H2645_FLAG_SMALL_PADDING;
188 int ret, i;
189
190 /* set some sane default values */
191 13257 s->pict_type = AV_PICTURE_TYPE_I;
192 13257 s->key_frame = 0;
193 13257 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
194
195 13257 ff_hevc_reset_sei(sei);
196
197 13257 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx,
198 ctx->nal_length_size, AV_CODEC_ID_HEVC, flags);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13257 times.
13257 if (ret < 0)
200 ✗ return ret;
201
202
2/2
✓ Branch 0 taken 17790 times.
✓ Branch 1 taken 1 times.
17791 for (i = 0; i < ctx->pkt.nb_nals; i++) {
203 17790 H2645NAL *nal = &ctx->pkt.nals[i];
204 17790 GetBitContext *gb = &nal->gb;
205
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17790 times.
17790 if (nal->nuh_layer_id > 0)
207 ✗ continue;
208
209
6/6
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 280 times.
✓ Branch 2 taken 1634 times.
✓ Branch 3 taken 1401 times.
✓ Branch 4 taken 13256 times.
✓ Branch 5 taken 939 times.
17790 switch (nal->type) {
210 280 case HEVC_NAL_VPS:
211 280 ff_hevc_decode_nal_vps(gb, avctx, ps);
212 280 break;
213 280 case HEVC_NAL_SPS:
214 280 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
215 280 break;
216 1634 case HEVC_NAL_PPS:
217 1634 ff_hevc_decode_nal_pps(gb, avctx, ps);
218 1634 break;
219 1401 case HEVC_NAL_SEI_PREFIX:
220 case HEVC_NAL_SEI_SUFFIX:
221 1401 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
222 1401 break;
223 13256 case HEVC_NAL_TRAIL_N:
224 case HEVC_NAL_TRAIL_R:
225 case HEVC_NAL_TSA_N:
226 case HEVC_NAL_TSA_R:
227 case HEVC_NAL_STSA_N:
228 case HEVC_NAL_STSA_R:
229 case HEVC_NAL_BLA_W_LP:
230 case HEVC_NAL_BLA_W_RADL:
231 case HEVC_NAL_BLA_N_LP:
232 case HEVC_NAL_IDR_W_RADL:
233 case HEVC_NAL_IDR_N_LP:
234 case HEVC_NAL_CRA_NUT:
235 case HEVC_NAL_RADL_N:
236 case HEVC_NAL_RADL_R:
237 case HEVC_NAL_RASL_N:
238 case HEVC_NAL_RASL_R:
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13256 times.
13256 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
240 ✗ s->repeat_pict = 1;
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13256 times.
13256 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
242 ✗ s->repeat_pict = 2;
243 }
244 13256 ret = hevc_parse_slice_header(s, nal, avctx);
245
1/2
✓ Branch 0 taken 13256 times.
✗ Branch 1 not taken.
13256 if (ret)
246 13256 return ret;
247 ✗ break;
248 }
249 }
250 /* didn't find a picture! */
251 1 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
252 1 return -1;
253 }
254
255 /**
256 * Find the end of the current frame in the bitstream.
257 * @return the position of the first byte of the next frame, or END_NOT_FOUND
258 */
259 100040 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
260 int buf_size)
261 {
262 100040 HEVCParserContext *ctx = s->priv_data;
263 100040 ParseContext *pc = &ctx->pc;
264 int i;
265
266
2/2
✓ Branch 0 taken 89632243 times.
✓ Branch 1 taken 87233 times.
89719476 for (i = 0; i < buf_size; i++) {
267 int nut, layer_id;
268
269 89632243 pc->state64 = (pc->state64 << 8) | buf[i];
270
271
2/2
✓ Branch 0 taken 89568488 times.
✓ Branch 1 taken 63755 times.
89632243 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
272 89568488 continue;
273
274 63755 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
275
276 63755 layer_id = (pc->state64 >> 11) & 0x3F;
277
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 63411 times.
63755 if (layer_id > 0)
278 344 continue;
279
280 // Beginning of access unit
281
7/8
✓ Branch 0 taken 15688 times.
✓ Branch 1 taken 47723 times.
✓ Branch 2 taken 10689 times.
✓ Branch 3 taken 4999 times.
✓ Branch 4 taken 56628 times.
✓ Branch 5 taken 1784 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 56628 times.
63411 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
282
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56628 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56628 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
283
2/2
✓ Branch 0 taken 2261 times.
✓ Branch 1 taken 4522 times.
6783 if (pc->frame_start_found) {
284 2261 pc->frame_start_found = 0;
285
2/2
✓ Branch 0 taken 2260 times.
✓ Branch 1 taken 1 times.
2261 if (!((pc->state64 >> 6 * 8) & 0xFF))
286 2260 return i - 6;
287 1 return i - 5;
288 }
289
3/4
✓ Branch 0 taken 10186 times.
✓ Branch 1 taken 46442 times.
✓ Branch 2 taken 10186 times.
✗ Branch 3 not taken.
56628 } else if (nut <= HEVC_NAL_RASL_R ||
290
2/2
✓ Branch 0 taken 1281 times.
✓ Branch 1 taken 8905 times.
10186 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
291 47723 int first_slice_segment_in_pic_flag = buf[i] >> 7;
292
2/2
✓ Branch 0 taken 23585 times.
✓ Branch 1 taken 24138 times.
47723 if (first_slice_segment_in_pic_flag) {
293
2/2
✓ Branch 0 taken 13039 times.
✓ Branch 1 taken 10546 times.
23585 if (!pc->frame_start_found) {
294 13039 pc->frame_start_found = 1;
295 } else { // First slice of next frame found
296 10546 pc->frame_start_found = 0;
297
2/2
✓ Branch 0 taken 10362 times.
✓ Branch 1 taken 184 times.
10546 if (!((pc->state64 >> 6 * 8) & 0xFF))
298 10362 return i - 6;
299 184 return i - 5;
300 }
301 }
302 }
303 }
304
305 87233 return END_NOT_FOUND;
306 }
307
308 100267 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
309 const uint8_t **poutbuf, int *poutbuf_size,
310 const uint8_t *buf, int buf_size)
311 {
312 int next;
313 100267 HEVCParserContext *ctx = s->priv_data;
314 100267 ParseContext *pc = &ctx->pc;
315 100267 int is_dummy_buf = !buf_size;
316 100267 const uint8_t *dummy_buf = buf;
317
318
4/4
✓ Branch 0 taken 85837 times.
✓ Branch 1 taken 14430 times.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 85613 times.
100267 if (avctx->extradata && !ctx->parsed_extradata) {
319 224 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
320 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
321 1, avctx);
322 224 ctx->parsed_extradata = 1;
323 }
324
325
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 100040 times.
100267 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
326 227 next = buf_size;
327 } else {
328 100040 next = hevc_find_frame_end(s, buf, buf_size);
329
2/2
✓ Branch 1 taken 86773 times.
✓ Branch 2 taken 13267 times.
100040 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
330 86773 *poutbuf = NULL;
331 86773 *poutbuf_size = 0;
332 86773 return buf_size;
333 }
334 }
335
336 13494 is_dummy_buf &= (dummy_buf == buf);
337
338
2/2
✓ Branch 0 taken 13257 times.
✓ Branch 1 taken 237 times.
13494 if (!is_dummy_buf)
339 13257 parse_nal_units(s, buf, buf_size, avctx);
340
341 13494 *poutbuf = buf;
342 13494 *poutbuf_size = buf_size;
343 13494 return next;
344 }
345
346 353 static void hevc_parser_close(AVCodecParserContext *s)
347 {
348 353 HEVCParserContext *ctx = s->priv_data;
349
350 353 ff_hevc_ps_uninit(&ctx->ps);
351 353 ff_h2645_packet_uninit(&ctx->pkt);
352 353 ff_hevc_reset_sei(&ctx->sei);
353
354 353 av_freep(&ctx->pc.buffer);
355 353 }
356
357 const FFCodecParser ff_hevc_parser = {
358 PARSER_CODEC_LIST(AV_CODEC_ID_HEVC),
359 .priv_data_size = sizeof(HEVCParserContext),
360 .parse = hevc_parse,
361 .close = hevc_parser_close,
362 };
363