FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2026-09-28 11:37:26
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 13118 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
57 AVCodecContext *avctx)
58 {
59 13118 HEVCParserContext *ctx = s->priv_data;
60 13118 HEVCParamSets *ps = &ctx->ps;
61 13118 HEVCSEI *sei = &ctx->sei;
62 13118 GetBitContext *gb = &nal->gb;
63 const HEVCPPS *pps;
64 const HEVCSPS *sps;
65 const HEVCWindow *ow;
66 13118 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 13118 first_slice_in_pic_flag = get_bits1(gb);
72 13118 s->picture_structure = sei->picture_timing.picture_struct;
73 13118 s->field_order = sei->picture_timing.picture_struct;
74
75
3/4
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 12600 times.
✓ Branch 2 taken 518 times.
✗ Branch 3 not taken.
13118 if (IS_IRAP_NAL(nal)) {
76 518 s->key_frame = 1;
77 518 skip_bits1(gb); // no_output_of_prior_pics_flag
78 }
79
80 13118 pps_id = get_ue_golomb(gb);
81
3/4
✓ Branch 0 taken 13118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✓ Branch 3 taken 13063 times.
13118 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
82 55 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
83 55 return AVERROR_INVALIDDATA;
84 }
85 13063 pps = ps->pps_list[pps_id];
86 13063 sps = pps->sps;
87
88 13063 ow = &sps->output_window;
89
90 13063 s->coded_width = sps->width;
91 13063 s->coded_height = sps->height;
92 13063 s->width = sps->width - ow->left_offset - ow->right_offset;
93 13063 s->height = sps->height - ow->top_offset - ow->bottom_offset;
94 13063 s->format = sps->pix_fmt;
95 13063 avctx->profile = sps->ptl.general_ptl.profile_idc;
96 13063 avctx->level = sps->ptl.general_ptl.level_idc;
97
98
2/2
✓ Branch 0 taken 365 times.
✓ Branch 1 taken 12698 times.
13063 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 805 times.
✓ Branch 1 taken 11893 times.
12698 } else if (sps->vui.vui_timing_info_present_flag) {
102 805 num = sps->vui.vui_num_units_in_tick;
103 805 den = sps->vui.vui_time_scale;
104 }
105
106
3/4
✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 11893 times.
✓ Branch 2 taken 1170 times.
✗ Branch 3 not taken.
13063 if (num > 0 && den > 0)
107 1170 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
108 num, den, 1 << 30);
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13063 times.
13063 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 13063 dependent_slice_segment_flag = 0;
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13063 times.
13063 if (dependent_slice_segment_flag)
131 ✗ return 0; /* break; */
132
133
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 13063 times.
13598 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
134 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
135
136 13063 slice_type = get_ue_golomb_31(gb);
137
5/6
✓ Branch 0 taken 12377 times.
✓ Branch 1 taken 686 times.
✓ Branch 2 taken 9645 times.
✓ Branch 3 taken 2732 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9645 times.
13063 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 3418 times.
✓ Branch 1 taken 9645 times.
16481 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
144
2/2
✓ Branch 0 taken 2732 times.
✓ Branch 1 taken 686 times.
3418 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 12363 times.
13063 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 13063 times.
13063 if (sps->separate_colour_plane)
151 ✗ skip_bits(gb, 2); // colour_plane_id
152
153
4/4
✓ Branch 0 taken 12831 times.
✓ Branch 1 taken 232 times.
✓ Branch 2 taken 12812 times.
✓ Branch 3 taken 19 times.
13063 if (!IS_IDR_NAL(nal)) {
154 12812 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
155 12812 s->output_picture_number = ctx->poc =
156 12812 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
157 } else
158 251 s->output_picture_number = ctx->poc = 0;
159
160
2/2
✓ Branch 0 taken 12700 times.
✓ Branch 1 taken 363 times.
13063 if (nal->temporal_id == 0 &&
161
2/2
✓ Branch 0 taken 9198 times.
✓ Branch 1 taken 3502 times.
12700 nal->type != HEVC_NAL_TRAIL_N &&
162
1/2
✓ Branch 0 taken 9198 times.
✗ Branch 1 not taken.
9198 nal->type != HEVC_NAL_TSA_N &&
163
1/2
✓ Branch 0 taken 9198 times.
✗ Branch 1 not taken.
9198 nal->type != HEVC_NAL_STSA_N &&
164
2/2
✓ Branch 0 taken 9154 times.
✓ Branch 1 taken 44 times.
9198 nal->type != HEVC_NAL_RADL_N &&
165
2/2
✓ Branch 0 taken 8673 times.
✓ Branch 1 taken 481 times.
9154 nal->type != HEVC_NAL_RASL_N &&
166
2/2
✓ Branch 0 taken 8636 times.
✓ Branch 1 taken 37 times.
8673 nal->type != HEVC_NAL_RADL_R &&
167
2/2
✓ Branch 0 taken 7971 times.
✓ Branch 1 taken 665 times.
8636 nal->type != HEVC_NAL_RASL_R)
168 7971 ctx->pocTid0 = ctx->poc;
169
170 13063 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 13119 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
182 int buf_size, AVCodecContext *avctx)
183 {
184 13119 HEVCParserContext *ctx = s->priv_data;
185 13119 HEVCParamSets *ps = &ctx->ps;
186 13119 HEVCSEI *sei = &ctx->sei;
187
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 12899 times.
13119 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 13119 s->pict_type = AV_PICTURE_TYPE_I;
192 13119 s->key_frame = 0;
193 13119 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
194
195 13119 ff_hevc_reset_sei(sei);
196
197 13119 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 13119 times.
13119 if (ret < 0)
200 ✗ return ret;
201
202
2/2
✓ Branch 0 taken 17449 times.
✓ Branch 1 taken 1 times.
17450 for (i = 0; i < ctx->pkt.nb_nals; i++) {
203 17449 H2645NAL *nal = &ctx->pkt.nals[i];
204 17449 GetBitContext *gb = &nal->gb;
205
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17449 times.
17449 if (nal->nuh_layer_id > 0)
207 ✗ continue;
208
209
6/6
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 277 times.
✓ Branch 2 taken 1531 times.
✓ Branch 3 taken 1330 times.
✓ Branch 4 taken 13118 times.
✓ Branch 5 taken 916 times.
17449 switch (nal->type) {
210 277 case HEVC_NAL_VPS:
211 277 ff_hevc_decode_nal_vps(gb, avctx, ps);
212 277 break;
213 277 case HEVC_NAL_SPS:
214 277 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
215 277 break;
216 1531 case HEVC_NAL_PPS:
217 1531 ff_hevc_decode_nal_pps(gb, avctx, ps);
218 1531 break;
219 1330 case HEVC_NAL_SEI_PREFIX:
220 case HEVC_NAL_SEI_SUFFIX:
221 1330 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
222 1330 break;
223 13118 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 13118 times.
13118 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 13118 times.
13118 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
242 ✗ s->repeat_pict = 2;
243 }
244 13118 ret = hevc_parse_slice_header(s, nal, avctx);
245
1/2
✓ Branch 0 taken 13118 times.
✗ Branch 1 not taken.
13118 if (ret)
246 13118 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 99135 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
260 int buf_size)
261 {
262 99135 HEVCParserContext *ctx = s->priv_data;
263 99135 ParseContext *pc = &ctx->pc;
264 int i;
265
266
2/2
✓ Branch 0 taken 88791218 times.
✓ Branch 1 taken 86463 times.
88877681 for (i = 0; i < buf_size; i++) {
267 int nut, layer_id;
268
269 88791218 pc->state64 = (pc->state64 << 8) | buf[i];
270
271
2/2
✓ Branch 0 taken 88727959 times.
✓ Branch 1 taken 63259 times.
88791218 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
272 88727959 continue;
273
274 63259 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
275
276 63259 layer_id = (pc->state64 >> 11) & 0x3F;
277
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 62915 times.
63259 if (layer_id > 0)
278 344 continue;
279
280 // Beginning of access unit
281
7/8
✓ Branch 0 taken 15345 times.
✓ Branch 1 taken 47570 times.
✓ Branch 2 taken 10601 times.
✓ Branch 3 taken 4744 times.
✓ Branch 4 taken 56460 times.
✓ Branch 5 taken 1711 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 56460 times.
62915 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 56460 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56460 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
283
2/2
✓ Branch 0 taken 2140 times.
✓ Branch 1 taken 4315 times.
6455 if (pc->frame_start_found) {
284 2140 pc->frame_start_found = 0;
285
2/2
✓ Branch 0 taken 2139 times.
✓ Branch 1 taken 1 times.
2140 if (!((pc->state64 >> 6 * 8) & 0xFF))
286 2139 return i - 6;
287 1 return i - 5;
288 }
289
3/4
✓ Branch 0 taken 10168 times.
✓ Branch 1 taken 46292 times.
✓ Branch 2 taken 10168 times.
✗ Branch 3 not taken.
56460 } else if (nut <= HEVC_NAL_RASL_R ||
290
2/2
✓ Branch 0 taken 1278 times.
✓ Branch 1 taken 8890 times.
10168 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
291 47570 int first_slice_segment_in_pic_flag = buf[i] >> 7;
292
2/2
✓ Branch 0 taken 23432 times.
✓ Branch 1 taken 24138 times.
47570 if (first_slice_segment_in_pic_flag) {
293
2/2
✓ Branch 0 taken 12900 times.
✓ Branch 1 taken 10532 times.
23432 if (!pc->frame_start_found) {
294 12900 pc->frame_start_found = 1;
295 } else { // First slice of next frame found
296 10532 pc->frame_start_found = 0;
297
2/2
✓ Branch 0 taken 10348 times.
✓ Branch 1 taken 184 times.
10532 if (!((pc->state64 >> 6 * 8) & 0xFF))
298 10348 return i - 6;
299 184 return i - 5;
300 }
301 }
302 }
303 }
304
305 86463 return END_NOT_FOUND;
306 }
307
308 99362 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 99362 HEVCParserContext *ctx = s->priv_data;
314 99362 ParseContext *pc = &ctx->pc;
315 99362 int is_dummy_buf = !buf_size;
316 99362 const uint8_t *dummy_buf = buf;
317
318
4/4
✓ Branch 0 taken 85098 times.
✓ Branch 1 taken 14264 times.
✓ Branch 2 taken 220 times.
✓ Branch 3 taken 84878 times.
99362 if (avctx->extradata && !ctx->parsed_extradata) {
319 220 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 220 ctx->parsed_extradata = 1;
323 }
324
325
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 99135 times.
99362 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
326 227 next = buf_size;
327 } else {
328 99135 next = hevc_find_frame_end(s, buf, buf_size);
329
2/2
✓ Branch 1 taken 86009 times.
✓ Branch 2 taken 13126 times.
99135 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
330 86009 *poutbuf = NULL;
331 86009 *poutbuf_size = 0;
332 86009 return buf_size;
333 }
334 }
335
336 13353 is_dummy_buf &= (dummy_buf == buf);
337
338
2/2
✓ Branch 0 taken 13119 times.
✓ Branch 1 taken 234 times.
13353 if (!is_dummy_buf)
339 13119 parse_nal_units(s, buf, buf_size, avctx);
340
341 13353 *poutbuf = buf;
342 13353 *poutbuf_size = buf_size;
343 13353 return next;
344 }
345
346 339 static void hevc_parser_close(AVCodecParserContext *s)
347 {
348 339 HEVCParserContext *ctx = s->priv_data;
349
350 339 ff_hevc_ps_uninit(&ctx->ps);
351 339 ff_h2645_packet_uninit(&ctx->pkt);
352 339 ff_hevc_reset_sei(&ctx->sei);
353
354 339 av_freep(&ctx->pc.buffer);
355 339 }
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