FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2026-07-22 00:55:30
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 12918 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
57 AVCodecContext *avctx)
58 {
59 12918 HEVCParserContext *ctx = s->priv_data;
60 12918 HEVCParamSets *ps = &ctx->ps;
61 12918 HEVCSEI *sei = &ctx->sei;
62 12918 GetBitContext *gb = &nal->gb;
63 const HEVCPPS *pps;
64 const HEVCSPS *sps;
65 const HEVCWindow *ow;
66 12918 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 12918 first_slice_in_pic_flag = get_bits1(gb);
72 12918 s->picture_structure = sei->picture_timing.picture_struct;
73 12918 s->field_order = sei->picture_timing.picture_struct;
74
75
3/4
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 12411 times.
✓ Branch 2 taken 507 times.
✗ Branch 3 not taken.
12918 if (IS_IRAP_NAL(nal)) {
76 507 s->key_frame = 1;
77 507 skip_bits1(gb); // no_output_of_prior_pics_flag
78 }
79
80 12918 pps_id = get_ue_golomb(gb);
81
3/4
✓ Branch 0 taken 12918 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 12885 times.
12918 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 12885 pps = ps->pps_list[pps_id];
86 12885 sps = pps->sps;
87
88 12885 ow = &sps->output_window;
89
90 12885 s->coded_width = sps->width;
91 12885 s->coded_height = sps->height;
92 12885 s->width = sps->width - ow->left_offset - ow->right_offset;
93 12885 s->height = sps->height - ow->top_offset - ow->bottom_offset;
94 12885 s->format = sps->pix_fmt;
95 12885 avctx->profile = sps->ptl.general_ptl.profile_idc;
96 12885 avctx->level = sps->ptl.general_ptl.level_idc;
97
98
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 12530 times.
12885 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 11743 times.
12530 } 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 11743 times.
✓ Branch 2 taken 1142 times.
✗ Branch 3 not taken.
12885 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 12885 times.
12885 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 12885 dependent_slice_segment_flag = 0;
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12885 times.
12885 if (dependent_slice_segment_flag)
131 return 0; /* break; */
132
133
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 12885 times.
13420 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
134 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
135
136 12885 slice_type = get_ue_golomb_31(gb);
137
5/6
✓ Branch 0 taken 12210 times.
✓ Branch 1 taken 675 times.
✓ Branch 2 taken 9483 times.
✓ Branch 3 taken 2727 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9483 times.
12885 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 3402 times.
✓ Branch 1 taken 9483 times.
16287 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
144
2/2
✓ Branch 0 taken 2727 times.
✓ Branch 1 taken 675 times.
3402 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 12185 times.
12885 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 12885 times.
12885 if (sps->separate_colour_plane)
151 skip_bits(gb, 2); // colour_plane_id
152
153
4/4
✓ Branch 0 taken 12658 times.
✓ Branch 1 taken 227 times.
✓ Branch 2 taken 12639 times.
✓ Branch 3 taken 19 times.
12885 if (!IS_IDR_NAL(nal)) {
154 12639 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
155 12639 s->output_picture_number = ctx->poc =
156 12639 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
157 } else
158 246 s->output_picture_number = ctx->poc = 0;
159
160
2/2
✓ Branch 0 taken 12522 times.
✓ Branch 1 taken 363 times.
12885 if (nal->temporal_id == 0 &&
161
2/2
✓ Branch 0 taken 9085 times.
✓ Branch 1 taken 3437 times.
12522 nal->type != HEVC_NAL_TRAIL_N &&
162
1/2
✓ Branch 0 taken 9085 times.
✗ Branch 1 not taken.
9085 nal->type != HEVC_NAL_TSA_N &&
163
1/2
✓ Branch 0 taken 9085 times.
✗ Branch 1 not taken.
9085 nal->type != HEVC_NAL_STSA_N &&
164
2/2
✓ Branch 0 taken 9041 times.
✓ Branch 1 taken 44 times.
9085 nal->type != HEVC_NAL_RADL_N &&
165
2/2
✓ Branch 0 taken 8578 times.
✓ Branch 1 taken 463 times.
9041 nal->type != HEVC_NAL_RASL_N &&
166
2/2
✓ Branch 0 taken 8541 times.
✓ Branch 1 taken 37 times.
8578 nal->type != HEVC_NAL_RADL_R &&
167
2/2
✓ Branch 0 taken 7891 times.
✓ Branch 1 taken 650 times.
8541 nal->type != HEVC_NAL_RASL_R)
168 7891 ctx->pocTid0 = ctx->poc;
169
170 12885 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 12919 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
182 int buf_size, AVCodecContext *avctx)
183 {
184 12919 HEVCParserContext *ctx = s->priv_data;
185 12919 HEVCParamSets *ps = &ctx->ps;
186 12919 HEVCSEI *sei = &ctx->sei;
187
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 12699 times.
12919 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 12919 s->pict_type = AV_PICTURE_TYPE_I;
192 12919 s->key_frame = 0;
193 12919 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
194
195 12919 ff_hevc_reset_sei(sei);
196
197 12919 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 12919 times.
12919 if (ret < 0)
200 return ret;
201
202
2/2
✓ Branch 0 taken 17034 times.
✓ Branch 1 taken 1 times.
17035 for (i = 0; i < ctx->pkt.nb_nals; i++) {
203 17034 H2645NAL *nal = &ctx->pkt.nals[i];
204 17034 GetBitContext *gb = &nal->gb;
205
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17034 times.
17034 if (nal->nuh_layer_id > 0)
207 continue;
208
209
6/6
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 267 times.
✓ Branch 2 taken 1519 times.
✓ Branch 3 taken 1198 times.
✓ Branch 4 taken 12918 times.
✓ Branch 5 taken 865 times.
17034 switch (nal->type) {
210 267 case HEVC_NAL_VPS:
211 267 ff_hevc_decode_nal_vps(gb, avctx, ps);
212 267 break;
213 267 case HEVC_NAL_SPS:
214 267 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
215 267 break;
216 1519 case HEVC_NAL_PPS:
217 1519 ff_hevc_decode_nal_pps(gb, avctx, ps);
218 1519 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 12918 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 12918 times.
12918 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 12918 times.
12918 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
242 s->repeat_pict = 2;
243 }
244 12918 ret = hevc_parse_slice_header(s, nal, avctx);
245
1/2
✓ Branch 0 taken 12918 times.
✗ Branch 1 not taken.
12918 if (ret)
246 12918 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 97755 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
260 int buf_size)
261 {
262 97755 HEVCParserContext *ctx = s->priv_data;
263 97755 ParseContext *pc = &ctx->pc;
264 int i;
265
266
2/2
✓ Branch 0 taken 87483451 times.
✓ Branch 1 taken 85276 times.
87568727 for (i = 0; i < buf_size; i++) {
267 int nut, layer_id;
268
269 87483451 pc->state64 = (pc->state64 << 8) | buf[i];
270
271
2/2
✓ Branch 0 taken 87421921 times.
✓ Branch 1 taken 61530 times.
87483451 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
272 87421921 continue;
273
274 61530 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
275
276 61530 layer_id = (pc->state64 >> 11) & 0x3F;
277
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 61186 times.
61530 if (layer_id > 0)
278 344 continue;
279
280 // Beginning of access unit
281
7/8
✓ Branch 0 taken 14930 times.
✓ Branch 1 taken 46256 times.
✓ Branch 2 taken 10319 times.
✓ Branch 3 taken 4611 times.
✓ Branch 4 taken 54996 times.
✓ Branch 5 taken 1579 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 54996 times.
61186 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 54996 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
54996 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
283
2/2
✓ Branch 0 taken 2090 times.
✓ Branch 1 taken 4100 times.
6190 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 9955 times.
✓ Branch 1 taken 45041 times.
✓ Branch 2 taken 9955 times.
✗ Branch 3 not taken.
54996 } else if (nut <= HEVC_NAL_RASL_R ||
290
2/2
✓ Branch 0 taken 1215 times.
✓ Branch 1 taken 8740 times.
9955 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
291 46256 int first_slice_segment_in_pic_flag = buf[i] >> 7;
292
2/2
✓ Branch 0 taken 23089 times.
✓ Branch 1 taken 23167 times.
46256 if (first_slice_segment_in_pic_flag) {
293
2/2
✓ Branch 0 taken 12700 times.
✓ Branch 1 taken 10389 times.
23089 if (!pc->frame_start_found) {
294 12700 pc->frame_start_found = 1;
295 } else { // First slice of next frame found
296 10389 pc->frame_start_found = 0;
297
2/2
✓ Branch 0 taken 10205 times.
✓ Branch 1 taken 184 times.
10389 if (!((pc->state64 >> 6 * 8) & 0xFF))
298 10205 return i - 6;
299 184 return i - 5;
300 }
301 }
302 }
303 }
304
305 85276 return END_NOT_FOUND;
306 }
307
308 97982 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 97982 HEVCParserContext *ctx = s->priv_data;
314 97982 ParseContext *pc = &ctx->pc;
315 97982 int is_dummy_buf = !buf_size;
316 97982 const uint8_t *dummy_buf = buf;
317
318
4/4
✓ Branch 0 taken 84075 times.
✓ Branch 1 taken 13907 times.
✓ Branch 2 taken 214 times.
✓ Branch 3 taken 83861 times.
97982 if (avctx->extradata && !ctx->parsed_extradata) {
319 214 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 214 ctx->parsed_extradata = 1;
323 }
324
325
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 97755 times.
97982 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
326 227 next = buf_size;
327 } else {
328 97755 next = hevc_find_frame_end(s, buf, buf_size);
329
2/2
✓ Branch 1 taken 84836 times.
✓ Branch 2 taken 12919 times.
97755 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
330 84836 *poutbuf = NULL;
331 84836 *poutbuf_size = 0;
332 84836 return buf_size;
333 }
334 }
335
336 13146 is_dummy_buf &= (dummy_buf == buf);
337
338
2/2
✓ Branch 0 taken 12919 times.
✓ Branch 1 taken 227 times.
13146 if (!is_dummy_buf)
339 12919 parse_nal_units(s, buf, buf_size, avctx);
340
341 13146 *poutbuf = buf;
342 13146 *poutbuf_size = buf_size;
343 13146 return next;
344 }
345
346 328 static void hevc_parser_close(AVCodecParserContext *s)
347 {
348 328 HEVCParserContext *ctx = s->priv_data;
349
350 328 ff_hevc_ps_uninit(&ctx->ps);
351 328 ff_h2645_packet_uninit(&ctx->pkt);
352 328 ff_hevc_reset_sei(&ctx->sei);
353
354 328 av_freep(&ctx->pc.buffer);
355 328 }
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