FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2026-04-30 23: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 "golomb.h"
27 #include "hevc.h"
28 #include "parser_internal.h"
29 #include "parse.h"
30 #include "ps.h"
31 #include "sei.h"
32 #include "h2645_parse.h"
33 #include "parser.h"
34
35 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
36
37 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
38 #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
39
40 typedef struct HEVCParserContext {
41 ParseContext pc;
42
43 H2645Packet pkt;
44 HEVCParamSets ps;
45 HEVCSEI sei;
46
47 int is_avc;
48 int nal_length_size;
49 int parsed_extradata;
50
51 int poc;
52 int pocTid0;
53 } HEVCParserContext;
54
55 12944 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
56 AVCodecContext *avctx)
57 {
58 12944 HEVCParserContext *ctx = s->priv_data;
59 12944 HEVCParamSets *ps = &ctx->ps;
60 12944 HEVCSEI *sei = &ctx->sei;
61 12944 GetBitContext *gb = &nal->gb;
62 const HEVCPPS *pps;
63 const HEVCSPS *sps;
64 const HEVCWindow *ow;
65 12944 int i, num = 0, den = 0;
66
67 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
68 enum HEVCSliceType slice_type;
69
70 12944 first_slice_in_pic_flag = get_bits1(gb);
71 12944 s->picture_structure = sei->picture_timing.picture_struct;
72 12944 s->field_order = sei->picture_timing.picture_struct;
73
74
3/4
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 12438 times.
✓ Branch 2 taken 506 times.
✗ Branch 3 not taken.
12944 if (IS_IRAP_NAL(nal)) {
75 506 s->key_frame = 1;
76 506 skip_bits1(gb); // no_output_of_prior_pics_flag
77 }
78
79 12944 pps_id = get_ue_golomb(gb);
80
3/4
✓ Branch 0 taken 12944 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 12911 times.
12944 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
81 33 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
82 33 return AVERROR_INVALIDDATA;
83 }
84 12911 pps = ps->pps_list[pps_id];
85 12911 sps = pps->sps;
86
87 12911 ow = &sps->output_window;
88
89 12911 s->coded_width = sps->width;
90 12911 s->coded_height = sps->height;
91 12911 s->width = sps->width - ow->left_offset - ow->right_offset;
92 12911 s->height = sps->height - ow->top_offset - ow->bottom_offset;
93 12911 s->format = sps->pix_fmt;
94 12911 avctx->profile = sps->ptl.general_ptl.profile_idc;
95 12911 avctx->level = sps->ptl.general_ptl.level_idc;
96
97
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 12556 times.
12911 if (sps->vps->vps_timing_info_present_flag) {
98 355 num = sps->vps->vps_num_units_in_tick;
99 355 den = sps->vps->vps_time_scale;
100
2/2
✓ Branch 0 taken 813 times.
✓ Branch 1 taken 11743 times.
12556 } else if (sps->vui.vui_timing_info_present_flag) {
101 813 num = sps->vui.vui_num_units_in_tick;
102 813 den = sps->vui.vui_time_scale;
103 }
104
105
3/4
✓ Branch 0 taken 1168 times.
✓ Branch 1 taken 11743 times.
✓ Branch 2 taken 1168 times.
✗ Branch 3 not taken.
12911 if (num > 0 && den > 0)
106 1168 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
107 num, den, 1 << 30);
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12911 times.
12911 if (!first_slice_in_pic_flag) {
110 unsigned int slice_segment_addr;
111 int slice_address_length;
112
113 if (pps->dependent_slice_segments_enabled_flag)
114 dependent_slice_segment_flag = get_bits1(gb);
115 else
116 dependent_slice_segment_flag = 0;
117
118 slice_address_length = av_ceil_log2_c(sps->ctb_width *
119 sps->ctb_height);
120 slice_segment_addr = get_bitsz(gb, slice_address_length);
121 if (slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
122 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
123 slice_segment_addr);
124 return AVERROR_INVALIDDATA;
125 }
126 } else
127 12911 dependent_slice_segment_flag = 0;
128
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12911 times.
12911 if (dependent_slice_segment_flag)
130 return 0; /* break; */
131
132
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 12911 times.
13446 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
133 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
134
135 12911 slice_type = get_ue_golomb_31(gb);
136
5/6
✓ Branch 0 taken 12237 times.
✓ Branch 1 taken 674 times.
✓ Branch 2 taken 9504 times.
✓ Branch 3 taken 2733 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9504 times.
12911 if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
137 slice_type == HEVC_SLICE_B)) {
138 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
139 slice_type);
140 return AVERROR_INVALIDDATA;
141 }
142
2/2
✓ Branch 0 taken 3407 times.
✓ Branch 1 taken 9504 times.
16318 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
143
2/2
✓ Branch 0 taken 2733 times.
✓ Branch 1 taken 674 times.
3407 slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
144 AV_PICTURE_TYPE_I;
145
146
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 12211 times.
12911 if (pps->output_flag_present_flag)
147 700 skip_bits1(gb); // pic_output_flag
148
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12911 times.
12911 if (sps->separate_colour_plane)
150 skip_bits(gb, 2); // colour_plane_id
151
152
4/4
✓ Branch 0 taken 12686 times.
✓ Branch 1 taken 225 times.
✓ Branch 2 taken 12666 times.
✓ Branch 3 taken 20 times.
12911 if (!IS_IDR_NAL(nal)) {
153 12666 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
154 12666 s->output_picture_number = ctx->poc =
155 12666 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
156 } else
157 245 s->output_picture_number = ctx->poc = 0;
158
159
2/2
✓ Branch 0 taken 12548 times.
✓ Branch 1 taken 363 times.
12911 if (nal->temporal_id == 0 &&
160
2/2
✓ Branch 0 taken 9096 times.
✓ Branch 1 taken 3452 times.
12548 nal->type != HEVC_NAL_TRAIL_N &&
161
1/2
✓ Branch 0 taken 9096 times.
✗ Branch 1 not taken.
9096 nal->type != HEVC_NAL_TSA_N &&
162
1/2
✓ Branch 0 taken 9096 times.
✗ Branch 1 not taken.
9096 nal->type != HEVC_NAL_STSA_N &&
163
2/2
✓ Branch 0 taken 9052 times.
✓ Branch 1 taken 44 times.
9096 nal->type != HEVC_NAL_RADL_N &&
164
2/2
✓ Branch 0 taken 8589 times.
✓ Branch 1 taken 463 times.
9052 nal->type != HEVC_NAL_RASL_N &&
165
2/2
✓ Branch 0 taken 8552 times.
✓ Branch 1 taken 37 times.
8589 nal->type != HEVC_NAL_RADL_R &&
166
2/2
✓ Branch 0 taken 7902 times.
✓ Branch 1 taken 650 times.
8552 nal->type != HEVC_NAL_RASL_R)
167 7902 ctx->pocTid0 = ctx->poc;
168
169 12911 return 1; /* no need to evaluate the rest */
170 }
171
172 /**
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 */
180 12945 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
181 int buf_size, AVCodecContext *avctx)
182 {
183 12945 HEVCParserContext *ctx = s->priv_data;
184 12945 HEVCParamSets *ps = &ctx->ps;
185 12945 HEVCSEI *sei = &ctx->sei;
186
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 12725 times.
12945 int flags = (H2645_FLAG_IS_NALFF * !!ctx->is_avc) | H2645_FLAG_SMALL_PADDING;
187 int ret, i;
188
189 /* set some sane default values */
190 12945 s->pict_type = AV_PICTURE_TYPE_I;
191 12945 s->key_frame = 0;
192 12945 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
193
194 12945 ff_hevc_reset_sei(sei);
195
196 12945 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx,
197 ctx->nal_length_size, AV_CODEC_ID_HEVC, flags);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12945 times.
12945 if (ret < 0)
199 return ret;
200
201
2/2
✓ Branch 0 taken 17086 times.
✓ Branch 1 taken 1 times.
17087 for (i = 0; i < ctx->pkt.nb_nals; i++) {
202 17086 H2645NAL *nal = &ctx->pkt.nals[i];
203 17086 GetBitContext *gb = &nal->gb;
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17086 times.
17086 if (nal->nuh_layer_id > 0)
206 continue;
207
208
6/6
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 266 times.
✓ Branch 2 taken 1518 times.
✓ Branch 3 taken 1197 times.
✓ Branch 4 taken 12944 times.
✓ Branch 5 taken 895 times.
17086 switch (nal->type) {
209 266 case HEVC_NAL_VPS:
210 266 ff_hevc_decode_nal_vps(gb, avctx, ps);
211 266 break;
212 266 case HEVC_NAL_SPS:
213 266 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
214 266 break;
215 1518 case HEVC_NAL_PPS:
216 1518 ff_hevc_decode_nal_pps(gb, avctx, ps);
217 1518 break;
218 1197 case HEVC_NAL_SEI_PREFIX:
219 case HEVC_NAL_SEI_SUFFIX:
220 1197 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
221 1197 break;
222 12944 case HEVC_NAL_TRAIL_N:
223 case HEVC_NAL_TRAIL_R:
224 case HEVC_NAL_TSA_N:
225 case HEVC_NAL_TSA_R:
226 case HEVC_NAL_STSA_N:
227 case HEVC_NAL_STSA_R:
228 case HEVC_NAL_BLA_W_LP:
229 case HEVC_NAL_BLA_W_RADL:
230 case HEVC_NAL_BLA_N_LP:
231 case HEVC_NAL_IDR_W_RADL:
232 case HEVC_NAL_IDR_N_LP:
233 case HEVC_NAL_CRA_NUT:
234 case HEVC_NAL_RADL_N:
235 case HEVC_NAL_RADL_R:
236 case HEVC_NAL_RASL_N:
237 case HEVC_NAL_RASL_R:
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12944 times.
12944 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
239 s->repeat_pict = 1;
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12944 times.
12944 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
241 s->repeat_pict = 2;
242 }
243 12944 ret = hevc_parse_slice_header(s, nal, avctx);
244
1/2
✓ Branch 0 taken 12944 times.
✗ Branch 1 not taken.
12944 if (ret)
245 12944 return ret;
246 break;
247 }
248 }
249 /* didn't find a picture! */
250 1 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
251 1 return -1;
252 }
253
254 /**
255 * Find the end of the current frame in the bitstream.
256 * @return the position of the first byte of the next frame, or END_NOT_FOUND
257 */
258 97806 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
259 int buf_size)
260 {
261 97806 HEVCParserContext *ctx = s->priv_data;
262 97806 ParseContext *pc = &ctx->pc;
263 int i;
264
265
2/2
✓ Branch 0 taken 87509354 times.
✓ Branch 1 taken 85301 times.
87594655 for (i = 0; i < buf_size; i++) {
266 int nut, layer_id;
267
268 87509354 pc->state64 = (pc->state64 << 8) | buf[i];
269
270
2/2
✓ Branch 0 taken 87447746 times.
✓ Branch 1 taken 61608 times.
87509354 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
271 87447746 continue;
272
273 61608 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
274
275 61608 layer_id = (pc->state64 >> 11) & 0x3F;
276
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 61264 times.
61608 if (layer_id > 0)
277 344 continue;
278
279 // Beginning of access unit
280
7/8
✓ Branch 0 taken 14984 times.
✓ Branch 1 taken 46280 times.
✓ Branch 2 taken 10317 times.
✓ Branch 3 taken 4667 times.
✓ Branch 4 taken 55020 times.
✓ Branch 5 taken 1577 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 55020 times.
61264 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
281
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 55020 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
55020 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
282
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 4126 times.
6244 if (pc->frame_start_found) {
283 2118 pc->frame_start_found = 0;
284
2/2
✓ Branch 0 taken 2117 times.
✓ Branch 1 taken 1 times.
2118 if (!((pc->state64 >> 6 * 8) & 0xFF))
285 2117 return i - 6;
286 1 return i - 5;
287 }
288
3/4
✓ Branch 0 taken 9954 times.
✓ Branch 1 taken 45066 times.
✓ Branch 2 taken 9954 times.
✗ Branch 3 not taken.
55020 } else if (nut <= HEVC_NAL_RASL_R ||
289
2/2
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 8740 times.
9954 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
290 46280 int first_slice_segment_in_pic_flag = buf[i] >> 7;
291
2/2
✓ Branch 0 taken 23113 times.
✓ Branch 1 taken 23167 times.
46280 if (first_slice_segment_in_pic_flag) {
292
2/2
✓ Branch 0 taken 12726 times.
✓ Branch 1 taken 10387 times.
23113 if (!pc->frame_start_found) {
293 12726 pc->frame_start_found = 1;
294 } else { // First slice of next frame found
295 10387 pc->frame_start_found = 0;
296
2/2
✓ Branch 0 taken 10203 times.
✓ Branch 1 taken 184 times.
10387 if (!((pc->state64 >> 6 * 8) & 0xFF))
297 10203 return i - 6;
298 184 return i - 5;
299 }
300 }
301 }
302 }
303
304 85301 return END_NOT_FOUND;
305 }
306
307 98033 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
308 const uint8_t **poutbuf, int *poutbuf_size,
309 const uint8_t *buf, int buf_size)
310 {
311 int next;
312 98033 HEVCParserContext *ctx = s->priv_data;
313 98033 ParseContext *pc = &ctx->pc;
314 98033 int is_dummy_buf = !buf_size;
315 98033 const uint8_t *dummy_buf = buf;
316
317
4/4
✓ Branch 0 taken 84131 times.
✓ Branch 1 taken 13902 times.
✓ Branch 2 taken 214 times.
✓ Branch 3 taken 83917 times.
98033 if (avctx->extradata && !ctx->parsed_extradata) {
318 214 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
319 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
320 1, avctx);
321 214 ctx->parsed_extradata = 1;
322 }
323
324
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 97806 times.
98033 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
325 227 next = buf_size;
326 } else {
327 97806 next = hevc_find_frame_end(s, buf, buf_size);
328
2/2
✓ Branch 1 taken 84861 times.
✓ Branch 2 taken 12945 times.
97806 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
329 84861 *poutbuf = NULL;
330 84861 *poutbuf_size = 0;
331 84861 return buf_size;
332 }
333 }
334
335 13172 is_dummy_buf &= (dummy_buf == buf);
336
337
2/2
✓ Branch 0 taken 12945 times.
✓ Branch 1 taken 227 times.
13172 if (!is_dummy_buf)
338 12945 parse_nal_units(s, buf, buf_size, avctx);
339
340 13172 *poutbuf = buf;
341 13172 *poutbuf_size = buf_size;
342 13172 return next;
343 }
344
345 299 static void hevc_parser_close(AVCodecParserContext *s)
346 {
347 299 HEVCParserContext *ctx = s->priv_data;
348
349 299 ff_hevc_ps_uninit(&ctx->ps);
350 299 ff_h2645_packet_uninit(&ctx->pkt);
351 299 ff_hevc_reset_sei(&ctx->sei);
352
353 299 av_freep(&ctx->pc.buffer);
354 299 }
355
356 const FFCodecParser ff_hevc_parser = {
357 PARSER_CODEC_LIST(AV_CODEC_ID_HEVC),
358 .priv_data_size = sizeof(HEVCParserContext),
359 .parse = hevc_parse,
360 .close = hevc_parser_close,
361 };
362