FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2026-09-08 19:51:47
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 12977 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
57 AVCodecContext *avctx)
58 {
59 12977 HEVCParserContext *ctx = s->priv_data;
60 12977 HEVCParamSets *ps = &ctx->ps;
61 12977 HEVCSEI *sei = &ctx->sei;
62 12977 GetBitContext *gb = &nal->gb;
63 const HEVCPPS *pps;
64 const HEVCSPS *sps;
65 const HEVCWindow *ow;
66 12977 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 12977 first_slice_in_pic_flag = get_bits1(gb);
72 12977 s->picture_structure = sei->picture_timing.picture_struct;
73 12977 s->field_order = sei->picture_timing.picture_struct;
74
75
3/4
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 12467 times.
✓ Branch 2 taken 510 times.
✗ Branch 3 not taken.
12977 if (IS_IRAP_NAL(nal)) {
76 510 s->key_frame = 1;
77 510 skip_bits1(gb); // no_output_of_prior_pics_flag
78 }
79
80 12977 pps_id = get_ue_golomb(gb);
81
3/4
✓ Branch 0 taken 12977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 12944 times.
12977 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
82 33 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
83 33 return AVERROR_INVALIDDATA;
84 }
85 12944 pps = ps->pps_list[pps_id];
86 12944 sps = pps->sps;
87
88 12944 ow = &sps->output_window;
89
90 12944 s->coded_width = sps->width;
91 12944 s->coded_height = sps->height;
92 12944 s->width = sps->width - ow->left_offset - ow->right_offset;
93 12944 s->height = sps->height - ow->top_offset - ow->bottom_offset;
94 12944 s->format = sps->pix_fmt;
95 12944 avctx->profile = sps->ptl.general_ptl.profile_idc;
96 12944 avctx->level = sps->ptl.general_ptl.level_idc;
97
98
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 12589 times.
12944 if (sps->vps->vps_timing_info_present_flag) {
99 355 num = sps->vps->vps_num_units_in_tick;
100 355 den = sps->vps->vps_time_scale;
101
2/2
✓ Branch 0 taken 787 times.
✓ Branch 1 taken 11802 times.
12589 } else if (sps->vui.vui_timing_info_present_flag) {
102 787 num = sps->vui.vui_num_units_in_tick;
103 787 den = sps->vui.vui_time_scale;
104 }
105
106
3/4
✓ Branch 0 taken 1142 times.
✓ Branch 1 taken 11802 times.
✓ Branch 2 taken 1142 times.
✗ Branch 3 not taken.
12944 if (num > 0 && den > 0)
107 1142 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
108 num, den, 1 << 30);
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12944 times.
12944 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 12944 dependent_slice_segment_flag = 0;
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12944 times.
12944 if (dependent_slice_segment_flag)
131 return 0; /* break; */
132
133
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 12944 times.
13479 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
134 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
135
136 12944 slice_type = get_ue_golomb_31(gb);
137
5/6
✓ Branch 0 taken 12266 times.
✓ Branch 1 taken 678 times.
✓ Branch 2 taken 9538 times.
✓ Branch 3 taken 2728 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9538 times.
12944 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 3406 times.
✓ Branch 1 taken 9538 times.
16350 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
144
2/2
✓ Branch 0 taken 2728 times.
✓ Branch 1 taken 678 times.
3406 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 12244 times.
12944 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 12944 times.
12944 if (sps->separate_colour_plane)
151 skip_bits(gb, 2); // colour_plane_id
152
153
4/4
✓ Branch 0 taken 12715 times.
✓ Branch 1 taken 229 times.
✓ Branch 2 taken 12696 times.
✓ Branch 3 taken 19 times.
12944 if (!IS_IDR_NAL(nal)) {
154 12696 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
155 12696 s->output_picture_number = ctx->poc =
156 12696 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
157 } else
158 248 s->output_picture_number = ctx->poc = 0;
159
160
2/2
✓ Branch 0 taken 12581 times.
✓ Branch 1 taken 363 times.
12944 if (nal->temporal_id == 0 &&
161
2/2
✓ Branch 0 taken 9119 times.
✓ Branch 1 taken 3462 times.
12581 nal->type != HEVC_NAL_TRAIL_N &&
162
1/2
✓ Branch 0 taken 9119 times.
✗ Branch 1 not taken.
9119 nal->type != HEVC_NAL_TSA_N &&
163
1/2
✓ Branch 0 taken 9119 times.
✗ Branch 1 not taken.
9119 nal->type != HEVC_NAL_STSA_N &&
164
2/2
✓ Branch 0 taken 9075 times.
✓ Branch 1 taken 44 times.
9119 nal->type != HEVC_NAL_RADL_N &&
165
2/2
✓ Branch 0 taken 8608 times.
✓ Branch 1 taken 467 times.
9075 nal->type != HEVC_NAL_RASL_N &&
166
2/2
✓ Branch 0 taken 8571 times.
✓ Branch 1 taken 37 times.
8608 nal->type != HEVC_NAL_RADL_R &&
167
2/2
✓ Branch 0 taken 7918 times.
✓ Branch 1 taken 653 times.
8571 nal->type != HEVC_NAL_RASL_R)
168 7918 ctx->pocTid0 = ctx->poc;
169
170 12944 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 12978 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
182 int buf_size, AVCodecContext *avctx)
183 {
184 12978 HEVCParserContext *ctx = s->priv_data;
185 12978 HEVCParamSets *ps = &ctx->ps;
186 12978 HEVCSEI *sei = &ctx->sei;
187
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 12758 times.
12978 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 12978 s->pict_type = AV_PICTURE_TYPE_I;
192 12978 s->key_frame = 0;
193 12978 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
194
195 12978 ff_hevc_reset_sei(sei);
196
197 12978 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 12978 times.
12978 if (ret < 0)
200 return ret;
201
202
2/2
✓ Branch 0 taken 17099 times.
✓ Branch 1 taken 1 times.
17100 for (i = 0; i < ctx->pkt.nb_nals; i++) {
203 17099 H2645NAL *nal = &ctx->pkt.nals[i];
204 17099 GetBitContext *gb = &nal->gb;
205
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17099 times.
17099 if (nal->nuh_layer_id > 0)
207 continue;
208
209
6/6
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 1521 times.
✓ Branch 3 taken 1198 times.
✓ Branch 4 taken 12977 times.
✓ Branch 5 taken 865 times.
17099 switch (nal->type) {
210 269 case HEVC_NAL_VPS:
211 269 ff_hevc_decode_nal_vps(gb, avctx, ps);
212 269 break;
213 269 case HEVC_NAL_SPS:
214 269 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
215 269 break;
216 1521 case HEVC_NAL_PPS:
217 1521 ff_hevc_decode_nal_pps(gb, avctx, ps);
218 1521 break;
219 1198 case HEVC_NAL_SEI_PREFIX:
220 case HEVC_NAL_SEI_SUFFIX:
221 1198 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
222 1198 break;
223 12977 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 12977 times.
12977 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 12977 times.
12977 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
242 s->repeat_pict = 2;
243 }
244 12977 ret = hevc_parse_slice_header(s, nal, avctx);
245
1/2
✓ Branch 0 taken 12977 times.
✗ Branch 1 not taken.
12977 if (ret)
246 12977 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 98246 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
260 int buf_size)
261 {
262 98246 HEVCParserContext *ctx = s->priv_data;
263 98246 ParseContext *pc = &ctx->pc;
264 int i;
265
266
2/2
✓ Branch 0 taken 87923116 times.
✓ Branch 1 taken 85710 times.
88008826 for (i = 0; i < buf_size; i++) {
267 int nut, layer_id;
268
269 87923116 pc->state64 = (pc->state64 << 8) | buf[i];
270
271
2/2
✓ Branch 0 taken 87860434 times.
✓ Branch 1 taken 62682 times.
87923116 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
272 87860434 continue;
273
274 62682 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
275
276 62682 layer_id = (pc->state64 >> 11) & 0x3F;
277
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 62338 times.
62682 if (layer_id > 0)
278 344 continue;
279
280 // Beginning of access unit
281
7/8
✓ Branch 0 taken 14995 times.
✓ Branch 1 taken 47343 times.
✓ Branch 2 taken 10378 times.
✓ Branch 3 taken 4617 times.
✓ Branch 4 taken 56142 times.
✓ Branch 5 taken 1579 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 56142 times.
62338 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 56142 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56142 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
283
2/2
✓ Branch 0 taken 2090 times.
✓ Branch 1 taken 4106 times.
6196 if (pc->frame_start_found) {
284 2090 pc->frame_start_found = 0;
285
2/2
✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 1 times.
2090 if (!((pc->state64 >> 6 * 8) & 0xFF))
286 2089 return i - 6;
287 1 return i - 5;
288 }
289
3/4
✓ Branch 0 taken 10069 times.
✓ Branch 1 taken 46073 times.
✓ Branch 2 taken 10069 times.
✗ Branch 3 not taken.
56142 } else if (nut <= HEVC_NAL_RASL_R ||
290
2/2
✓ Branch 0 taken 1270 times.
✓ Branch 1 taken 8799 times.
10069 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
291 47343 int first_slice_segment_in_pic_flag = buf[i] >> 7;
292
2/2
✓ Branch 0 taken 23205 times.
✓ Branch 1 taken 24138 times.
47343 if (first_slice_segment_in_pic_flag) {
293
2/2
✓ Branch 0 taken 12759 times.
✓ Branch 1 taken 10446 times.
23205 if (!pc->frame_start_found) {
294 12759 pc->frame_start_found = 1;
295 } else { // First slice of next frame found
296 10446 pc->frame_start_found = 0;
297
2/2
✓ Branch 0 taken 10262 times.
✓ Branch 1 taken 184 times.
10446 if (!((pc->state64 >> 6 * 8) & 0xFF))
298 10262 return i - 6;
299 184 return i - 5;
300 }
301 }
302 }
303 }
304
305 85710 return END_NOT_FOUND;
306 }
307
308 98473 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 98473 HEVCParserContext *ctx = s->priv_data;
314 98473 ParseContext *pc = &ctx->pc;
315 98473 int is_dummy_buf = !buf_size;
316 98473 const uint8_t *dummy_buf = buf;
317
318
4/4
✓ Branch 0 taken 84504 times.
✓ Branch 1 taken 13969 times.
✓ Branch 2 taken 216 times.
✓ Branch 3 taken 84288 times.
98473 if (avctx->extradata && !ctx->parsed_extradata) {
319 216 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 216 ctx->parsed_extradata = 1;
323 }
324
325
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 98246 times.
98473 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
326 227 next = buf_size;
327 } else {
328 98246 next = hevc_find_frame_end(s, buf, buf_size);
329
2/2
✓ Branch 1 taken 85266 times.
✓ Branch 2 taken 12980 times.
98246 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
330 85266 *poutbuf = NULL;
331 85266 *poutbuf_size = 0;
332 85266 return buf_size;
333 }
334 }
335
336 13207 is_dummy_buf &= (dummy_buf == buf);
337
338
2/2
✓ Branch 0 taken 12978 times.
✓ Branch 1 taken 229 times.
13207 if (!is_dummy_buf)
339 12978 parse_nal_units(s, buf, buf_size, avctx);
340
341 13207 *poutbuf = buf;
342 13207 *poutbuf_size = buf_size;
343 13207 return next;
344 }
345
346 330 static void hevc_parser_close(AVCodecParserContext *s)
347 {
348 330 HEVCParserContext *ctx = s->priv_data;
349
350 330 ff_hevc_ps_uninit(&ctx->ps);
351 330 ff_h2645_packet_uninit(&ctx->pkt);
352 330 ff_hevc_reset_sei(&ctx->sei);
353
354 330 av_freep(&ctx->pc.buffer);
355 330 }
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