FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2025-11-27 19:16:05
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 12734 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
56 AVCodecContext *avctx)
57 {
58 12734 HEVCParserContext *ctx = s->priv_data;
59 12734 HEVCParamSets *ps = &ctx->ps;
60 12734 HEVCSEI *sei = &ctx->sei;
61 12734 GetBitContext *gb = &nal->gb;
62 const HEVCPPS *pps;
63 const HEVCSPS *sps;
64 const HEVCWindow *ow;
65 12734 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 12734 first_slice_in_pic_flag = get_bits1(gb);
71 12734 s->picture_structure = sei->picture_timing.picture_struct;
72 12734 s->field_order = sei->picture_timing.picture_struct;
73
74
3/4
✓ Branch 0 taken 499 times.
✓ Branch 1 taken 12235 times.
✓ Branch 2 taken 499 times.
✗ Branch 3 not taken.
12734 if (IS_IRAP_NAL(nal)) {
75 499 s->key_frame = 1;
76 499 skip_bits1(gb); // no_output_of_prior_pics_flag
77 }
78
79 12734 pps_id = get_ue_golomb(gb);
80
3/4
✓ Branch 0 taken 12734 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 12701 times.
12734 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 12701 pps = ps->pps_list[pps_id];
85 12701 sps = pps->sps;
86
87 12701 ow = &sps->output_window;
88
89 12701 s->coded_width = sps->width;
90 12701 s->coded_height = sps->height;
91 12701 s->width = sps->width - ow->left_offset - ow->right_offset;
92 12701 s->height = sps->height - ow->top_offset - ow->bottom_offset;
93 12701 s->format = sps->pix_fmt;
94 12701 avctx->profile = sps->ptl.general_ptl.profile_idc;
95 12701 avctx->level = sps->ptl.general_ptl.level_idc;
96
97
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 12346 times.
12701 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 603 times.
✓ Branch 1 taken 11743 times.
12346 } else if (sps->vui.vui_timing_info_present_flag) {
101 603 num = sps->vui.vui_num_units_in_tick;
102 603 den = sps->vui.vui_time_scale;
103 }
104
105
3/4
✓ Branch 0 taken 958 times.
✓ Branch 1 taken 11743 times.
✓ Branch 2 taken 958 times.
✗ Branch 3 not taken.
12701 if (num > 0 && den > 0)
106 958 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
107 num, den, 1 << 30);
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12701 times.
12701 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 12701 dependent_slice_segment_flag = 0;
128
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12701 times.
12701 if (dependent_slice_segment_flag)
130 return 0; /* break; */
131
132
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 12701 times.
13236 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
133 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
134
135 12701 slice_type = get_ue_golomb_31(gb);
136
5/6
✓ Branch 0 taken 12034 times.
✓ Branch 1 taken 667 times.
✓ Branch 2 taken 9357 times.
✓ Branch 3 taken 2677 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9357 times.
12701 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 3344 times.
✓ Branch 1 taken 9357 times.
16045 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
143
2/2
✓ Branch 0 taken 2677 times.
✓ Branch 1 taken 667 times.
3344 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 12001 times.
12701 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 12701 times.
12701 if (sps->separate_colour_plane)
150 skip_bits(gb, 2); // colour_plane_id
151
152
4/4
✓ Branch 0 taken 12476 times.
✓ Branch 1 taken 225 times.
✓ Branch 2 taken 12463 times.
✓ Branch 3 taken 13 times.
12701 if (!IS_IDR_NAL(nal)) {
153 12463 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
154 12463 s->output_picture_number = ctx->poc =
155 12463 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
156 } else
157 238 s->output_picture_number = ctx->poc = 0;
158
159
2/2
✓ Branch 0 taken 12338 times.
✓ Branch 1 taken 363 times.
12701 if (nal->temporal_id == 0 &&
160
2/2
✓ Branch 0 taken 8991 times.
✓ Branch 1 taken 3347 times.
12338 nal->type != HEVC_NAL_TRAIL_N &&
161
1/2
✓ Branch 0 taken 8991 times.
✗ Branch 1 not taken.
8991 nal->type != HEVC_NAL_TSA_N &&
162
1/2
✓ Branch 0 taken 8991 times.
✗ Branch 1 not taken.
8991 nal->type != HEVC_NAL_STSA_N &&
163
2/2
✓ Branch 0 taken 8947 times.
✓ Branch 1 taken 44 times.
8991 nal->type != HEVC_NAL_RADL_N &&
164
2/2
✓ Branch 0 taken 8484 times.
✓ Branch 1 taken 463 times.
8947 nal->type != HEVC_NAL_RASL_N &&
165
2/2
✓ Branch 0 taken 8447 times.
✓ Branch 1 taken 37 times.
8484 nal->type != HEVC_NAL_RADL_R &&
166
2/2
✓ Branch 0 taken 7797 times.
✓ Branch 1 taken 650 times.
8447 nal->type != HEVC_NAL_RASL_R)
167 7797 ctx->pocTid0 = ctx->poc;
168
169 12701 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 12735 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
181 int buf_size, AVCodecContext *avctx)
182 {
183 12735 HEVCParserContext *ctx = s->priv_data;
184 12735 HEVCParamSets *ps = &ctx->ps;
185 12735 HEVCSEI *sei = &ctx->sei;
186
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 12515 times.
12735 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 12735 s->pict_type = AV_PICTURE_TYPE_I;
191 12735 s->key_frame = 0;
192 12735 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
193
194 12735 ff_hevc_reset_sei(sei);
195
196 12735 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 12735 times.
12735 if (ret < 0)
199 return ret;
200
201
2/2
✓ Branch 0 taken 16518 times.
✓ Branch 1 taken 1 times.
16519 for (i = 0; i < ctx->pkt.nb_nals; i++) {
202 16518 H2645NAL *nal = &ctx->pkt.nals[i];
203 16518 GetBitContext *gb = &nal->gb;
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16518 times.
16518 if (nal->nuh_layer_id > 0)
206 continue;
207
208
6/6
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 1511 times.
✓ Branch 3 taken 1070 times.
✓ Branch 4 taken 12734 times.
✓ Branch 5 taken 685 times.
16518 switch (nal->type) {
209 259 case HEVC_NAL_VPS:
210 259 ff_hevc_decode_nal_vps(gb, avctx, ps);
211 259 break;
212 259 case HEVC_NAL_SPS:
213 259 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
214 259 break;
215 1511 case HEVC_NAL_PPS:
216 1511 ff_hevc_decode_nal_pps(gb, avctx, ps);
217 1511 break;
218 1070 case HEVC_NAL_SEI_PREFIX:
219 case HEVC_NAL_SEI_SUFFIX:
220 1070 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
221 1070 break;
222 12734 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 12734 times.
12734 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 12734 times.
12734 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
241 s->repeat_pict = 2;
242 }
243 12734 ret = hevc_parse_slice_header(s, nal, avctx);
244
1/2
✓ Branch 0 taken 12734 times.
✗ Branch 1 not taken.
12734 if (ret)
245 12734 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 97379 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
259 int buf_size)
260 {
261 97379 HEVCParserContext *ctx = s->priv_data;
262 97379 ParseContext *pc = &ctx->pc;
263 int i;
264
265
2/2
✓ Branch 0 taken 87215016 times.
✓ Branch 1 taken 85077 times.
87300093 for (i = 0; i < buf_size; i++) {
266 int nut, layer_id;
267
268 87215016 pc->state64 = (pc->state64 << 8) | buf[i];
269
270
2/2
✓ Branch 0 taken 87154179 times.
✓ Branch 1 taken 60837 times.
87215016 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
271 87154179 continue;
272
273 60837 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
274
275 60837 layer_id = (pc->state64 >> 11) & 0x3F;
276
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 60493 times.
60837 if (layer_id > 0)
277 344 continue;
278
279 // Beginning of access unit
280
7/8
✓ Branch 0 taken 14423 times.
✓ Branch 1 taken 46070 times.
✓ Branch 2 taken 10190 times.
✓ Branch 3 taken 4233 times.
✓ Branch 4 taken 54810 times.
✓ Branch 5 taken 1450 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 54810 times.
60493 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 54810 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
54810 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
282
2/2
✓ Branch 0 taken 1915 times.
✓ Branch 1 taken 3768 times.
5683 if (pc->frame_start_found) {
283 1915 pc->frame_start_found = 0;
284
2/2
✓ Branch 0 taken 1914 times.
✓ Branch 1 taken 1 times.
1915 if (!((pc->state64 >> 6 * 8) & 0xFF))
285 1914 return i - 6;
286 1 return i - 5;
287 }
288
3/4
✓ Branch 0 taken 9947 times.
✓ Branch 1 taken 44863 times.
✓ Branch 2 taken 9947 times.
✗ Branch 3 not taken.
54810 } else if (nut <= HEVC_NAL_RASL_R ||
289
2/2
✓ Branch 0 taken 1207 times.
✓ Branch 1 taken 8740 times.
9947 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
290 46070 int first_slice_segment_in_pic_flag = buf[i] >> 7;
291
2/2
✓ Branch 0 taken 22903 times.
✓ Branch 1 taken 23167 times.
46070 if (first_slice_segment_in_pic_flag) {
292
2/2
✓ Branch 0 taken 12516 times.
✓ Branch 1 taken 10387 times.
22903 if (!pc->frame_start_found) {
293 12516 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 85077 return END_NOT_FOUND;
305 }
306
307 97606 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 97606 HEVCParserContext *ctx = s->priv_data;
313 97606 ParseContext *pc = &ctx->pc;
314 97606 int is_dummy_buf = !buf_size;
315 97606 const uint8_t *dummy_buf = buf;
316
317
4/4
✓ Branch 0 taken 83716 times.
✓ Branch 1 taken 13890 times.
✓ Branch 2 taken 207 times.
✓ Branch 3 taken 83509 times.
97606 if (avctx->extradata && !ctx->parsed_extradata) {
318 207 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 207 ctx->parsed_extradata = 1;
322 }
323
324
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 97379 times.
97606 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
325 227 next = buf_size;
326 } else {
327 97379 next = hevc_find_frame_end(s, buf, buf_size);
328
2/2
✓ Branch 1 taken 84651 times.
✓ Branch 2 taken 12728 times.
97379 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
329 84651 *poutbuf = NULL;
330 84651 *poutbuf_size = 0;
331 84651 return buf_size;
332 }
333 }
334
335 12955 is_dummy_buf &= (dummy_buf == buf);
336
337
2/2
✓ Branch 0 taken 12735 times.
✓ Branch 1 taken 220 times.
12955 if (!is_dummy_buf)
338 12735 parse_nal_units(s, buf, buf_size, avctx);
339
340 12955 *poutbuf = buf;
341 12955 *poutbuf_size = buf_size;
342 12955 return next;
343 }
344
345 292 static void hevc_parser_close(AVCodecParserContext *s)
346 {
347 292 HEVCParserContext *ctx = s->priv_data;
348
349 292 ff_hevc_ps_uninit(&ctx->ps);
350 292 ff_h2645_packet_uninit(&ctx->pkt);
351 292 ff_hevc_reset_sei(&ctx->sei);
352
353 292 av_freep(&ctx->pc.buffer);
354 292 }
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