FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2025-04-25 22:50:00
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 "parse.h"
29 #include "ps.h"
30 #include "sei.h"
31 #include "h2645_parse.h"
32 #include "parser.h"
33
34 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
35
36 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
37 #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
38
39 typedef struct HEVCParserContext {
40 ParseContext pc;
41
42 H2645Packet pkt;
43 HEVCParamSets ps;
44 HEVCSEI sei;
45
46 int is_avc;
47 int nal_length_size;
48 int parsed_extradata;
49
50 int poc;
51 int pocTid0;
52 } HEVCParserContext;
53
54 12359 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
55 AVCodecContext *avctx)
56 {
57 12359 HEVCParserContext *ctx = s->priv_data;
58 12359 HEVCParamSets *ps = &ctx->ps;
59 12359 HEVCSEI *sei = &ctx->sei;
60 12359 GetBitContext *gb = &nal->gb;
61 const HEVCPPS *pps;
62 const HEVCSPS *sps;
63 const HEVCWindow *ow;
64 12359 int i, num = 0, den = 0;
65
66 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
67 enum HEVCSliceType slice_type;
68
69 12359 first_slice_in_pic_flag = get_bits1(gb);
70 12359 s->picture_structure = sei->picture_timing.picture_struct;
71 12359 s->field_order = sei->picture_timing.picture_struct;
72
73
3/4
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 11876 times.
✓ Branch 2 taken 483 times.
✗ Branch 3 not taken.
12359 if (IS_IRAP_NAL(nal)) {
74 483 s->key_frame = 1;
75 483 skip_bits1(gb); // no_output_of_prior_pics_flag
76 }
77
78 12359 pps_id = get_ue_golomb(gb);
79
3/4
✓ Branch 0 taken 12359 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 12326 times.
12359 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
80 33 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
81 33 return AVERROR_INVALIDDATA;
82 }
83 12326 pps = ps->pps_list[pps_id];
84 12326 sps = pps->sps;
85
86 12326 ow = &sps->output_window;
87
88 12326 s->coded_width = sps->width;
89 12326 s->coded_height = sps->height;
90 12326 s->width = sps->width - ow->left_offset - ow->right_offset;
91 12326 s->height = sps->height - ow->top_offset - ow->bottom_offset;
92 12326 s->format = sps->pix_fmt;
93 12326 avctx->profile = sps->ptl.general_ptl.profile_idc;
94 12326 avctx->level = sps->ptl.general_ptl.level_idc;
95
96
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 11971 times.
12326 if (sps->vps->vps_timing_info_present_flag) {
97 355 num = sps->vps->vps_num_units_in_tick;
98 355 den = sps->vps->vps_time_scale;
99
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 11410 times.
11971 } else if (sps->vui.vui_timing_info_present_flag) {
100 561 num = sps->vui.vui_num_units_in_tick;
101 561 den = sps->vui.vui_time_scale;
102 }
103
104
3/4
✓ Branch 0 taken 916 times.
✓ Branch 1 taken 11410 times.
✓ Branch 2 taken 916 times.
✗ Branch 3 not taken.
12326 if (num > 0 && den > 0)
105 916 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
106 num, den, 1 << 30);
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12326 times.
12326 if (!first_slice_in_pic_flag) {
109 unsigned int slice_segment_addr;
110 int slice_address_length;
111
112 if (pps->dependent_slice_segments_enabled_flag)
113 dependent_slice_segment_flag = get_bits1(gb);
114 else
115 dependent_slice_segment_flag = 0;
116
117 slice_address_length = av_ceil_log2_c(sps->ctb_width *
118 sps->ctb_height);
119 slice_segment_addr = get_bitsz(gb, slice_address_length);
120 if (slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
121 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
122 slice_segment_addr);
123 return AVERROR_INVALIDDATA;
124 }
125 } else
126 12326 dependent_slice_segment_flag = 0;
127
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12326 times.
12326 if (dependent_slice_segment_flag)
129 return 0; /* break; */
130
131
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 12326 times.
12861 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
132 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
133
134 12326 slice_type = get_ue_golomb_31(gb);
135
5/6
✓ Branch 0 taken 11675 times.
✓ Branch 1 taken 651 times.
✓ Branch 2 taken 9037 times.
✓ Branch 3 taken 2638 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9037 times.
12326 if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
136 slice_type == HEVC_SLICE_B)) {
137 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
138 slice_type);
139 return AVERROR_INVALIDDATA;
140 }
141
2/2
✓ Branch 0 taken 3289 times.
✓ Branch 1 taken 9037 times.
15615 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
142
2/2
✓ Branch 0 taken 2638 times.
✓ Branch 1 taken 651 times.
3289 slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
143 AV_PICTURE_TYPE_I;
144
145
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 11626 times.
12326 if (pps->output_flag_present_flag)
146 700 skip_bits1(gb); // pic_output_flag
147
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12326 times.
12326 if (sps->separate_colour_plane)
149 skip_bits(gb, 2); // colour_plane_id
150
151
4/4
✓ Branch 0 taken 12110 times.
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 12097 times.
✓ Branch 3 taken 13 times.
12326 if (!IS_IDR_NAL(nal)) {
152 12097 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
153 12097 s->output_picture_number = ctx->poc =
154 12097 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
155 } else
156 229 s->output_picture_number = ctx->poc = 0;
157
158
2/2
✓ Branch 0 taken 11963 times.
✓ Branch 1 taken 363 times.
12326 if (nal->temporal_id == 0 &&
159
2/2
✓ Branch 0 taken 8745 times.
✓ Branch 1 taken 3218 times.
11963 nal->type != HEVC_NAL_TRAIL_N &&
160
1/2
✓ Branch 0 taken 8745 times.
✗ Branch 1 not taken.
8745 nal->type != HEVC_NAL_TSA_N &&
161
1/2
✓ Branch 0 taken 8745 times.
✗ Branch 1 not taken.
8745 nal->type != HEVC_NAL_STSA_N &&
162
2/2
✓ Branch 0 taken 8709 times.
✓ Branch 1 taken 36 times.
8745 nal->type != HEVC_NAL_RADL_N &&
163
2/2
✓ Branch 0 taken 8274 times.
✓ Branch 1 taken 435 times.
8709 nal->type != HEVC_NAL_RASL_N &&
164
2/2
✓ Branch 0 taken 8243 times.
✓ Branch 1 taken 31 times.
8274 nal->type != HEVC_NAL_RADL_R &&
165
2/2
✓ Branch 0 taken 7614 times.
✓ Branch 1 taken 629 times.
8243 nal->type != HEVC_NAL_RASL_R)
166 7614 ctx->pocTid0 = ctx->poc;
167
168 12326 return 1; /* no need to evaluate the rest */
169 }
170
171 /**
172 * Parse NAL units of found picture and decode some basic information.
173 *
174 * @param s parser context.
175 * @param avctx codec context.
176 * @param buf buffer with field/frame data.
177 * @param buf_size size of the buffer.
178 */
179 12360 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
180 int buf_size, AVCodecContext *avctx)
181 {
182 12360 HEVCParserContext *ctx = s->priv_data;
183 12360 HEVCParamSets *ps = &ctx->ps;
184 12360 HEVCSEI *sei = &ctx->sei;
185
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 12140 times.
12360 int flags = (H2645_FLAG_IS_NALFF * !!ctx->is_avc) | H2645_FLAG_SMALL_PADDING;
186 int ret, i;
187
188 /* set some sane default values */
189 12360 s->pict_type = AV_PICTURE_TYPE_I;
190 12360 s->key_frame = 0;
191 12360 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
192
193 12360 ff_hevc_reset_sei(sei);
194
195 12360 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx,
196 ctx->nal_length_size, AV_CODEC_ID_HEVC, flags);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12360 times.
12360 if (ret < 0)
198 return ret;
199
200
2/2
✓ Branch 0 taken 15990 times.
✓ Branch 1 taken 1 times.
15991 for (i = 0; i < ctx->pkt.nb_nals; i++) {
201 15990 H2645NAL *nal = &ctx->pkt.nals[i];
202 15990 GetBitContext *gb = &nal->gb;
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15990 times.
15990 if (nal->nuh_layer_id > 0)
205 continue;
206
207
6/6
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 250 times.
✓ Branch 2 taken 1375 times.
✓ Branch 3 taken 1070 times.
✓ Branch 4 taken 12359 times.
✓ Branch 5 taken 685 times.
15990 switch (nal->type) {
208 251 case HEVC_NAL_VPS:
209 251 ff_hevc_decode_nal_vps(gb, avctx, ps);
210 251 break;
211 250 case HEVC_NAL_SPS:
212 250 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
213 250 break;
214 1375 case HEVC_NAL_PPS:
215 1375 ff_hevc_decode_nal_pps(gb, avctx, ps);
216 1375 break;
217 1070 case HEVC_NAL_SEI_PREFIX:
218 case HEVC_NAL_SEI_SUFFIX:
219 1070 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
220 1070 break;
221 12359 case HEVC_NAL_TRAIL_N:
222 case HEVC_NAL_TRAIL_R:
223 case HEVC_NAL_TSA_N:
224 case HEVC_NAL_TSA_R:
225 case HEVC_NAL_STSA_N:
226 case HEVC_NAL_STSA_R:
227 case HEVC_NAL_BLA_W_LP:
228 case HEVC_NAL_BLA_W_RADL:
229 case HEVC_NAL_BLA_N_LP:
230 case HEVC_NAL_IDR_W_RADL:
231 case HEVC_NAL_IDR_N_LP:
232 case HEVC_NAL_CRA_NUT:
233 case HEVC_NAL_RADL_N:
234 case HEVC_NAL_RADL_R:
235 case HEVC_NAL_RASL_N:
236 case HEVC_NAL_RASL_R:
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12359 times.
12359 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
238 s->repeat_pict = 1;
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12359 times.
12359 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
240 s->repeat_pict = 2;
241 }
242 12359 ret = hevc_parse_slice_header(s, nal, avctx);
243
1/2
✓ Branch 0 taken 12359 times.
✗ Branch 1 not taken.
12359 if (ret)
244 12359 return ret;
245 break;
246 }
247 }
248 /* didn't find a picture! */
249 1 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
250 1 return -1;
251 }
252
253 /**
254 * Find the end of the current frame in the bitstream.
255 * @return the position of the first byte of the next frame, or END_NOT_FOUND
256 */
257 92253 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
258 int buf_size)
259 {
260 92253 HEVCParserContext *ctx = s->priv_data;
261 92253 ParseContext *pc = &ctx->pc;
262 int i;
263
264
2/2
✓ Branch 0 taken 82018028 times.
✓ Branch 1 taken 80322 times.
82098350 for (i = 0; i < buf_size; i++) {
265 int nut, layer_id;
266
267 82018028 pc->state64 = (pc->state64 << 8) | buf[i];
268
269
2/2
✓ Branch 0 taken 81958222 times.
✓ Branch 1 taken 59806 times.
82018028 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
270 81958222 continue;
271
272 59806 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
273
274 59806 layer_id = (pc->state64 >> 11) & 0x3F;
275
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 59462 times.
59806 if (layer_id > 0)
276 344 continue;
277
278 // Beginning of access unit
279
7/8
✓ Branch 0 taken 14010 times.
✓ Branch 1 taken 45452 times.
✓ Branch 2 taken 10060 times.
✓ Branch 3 taken 3950 times.
✓ Branch 4 taken 54062 times.
✓ Branch 5 taken 1450 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 54062 times.
59462 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
280
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 54062 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
54062 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
281
2/2
✓ Branch 0 taken 1785 times.
✓ Branch 1 taken 3615 times.
5400 if (pc->frame_start_found) {
282 1785 pc->frame_start_found = 0;
283
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 1 times.
1785 if (!((pc->state64 >> 6 * 8) & 0xFF))
284 1784 return i - 6;
285 1 return i - 5;
286 }
287
3/4
✓ Branch 0 taken 9797 times.
✓ Branch 1 taken 44265 times.
✓ Branch 2 taken 9797 times.
✗ Branch 3 not taken.
54062 } else if (nut <= HEVC_NAL_RASL_R ||
288
2/2
✓ Branch 0 taken 1187 times.
✓ Branch 1 taken 8610 times.
9797 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
289 45452 int first_slice_segment_in_pic_flag = buf[i] >> 7;
290
2/2
✓ Branch 0 taken 22285 times.
✓ Branch 1 taken 23167 times.
45452 if (first_slice_segment_in_pic_flag) {
291
2/2
✓ Branch 0 taken 12139 times.
✓ Branch 1 taken 10146 times.
22285 if (!pc->frame_start_found) {
292 12139 pc->frame_start_found = 1;
293 } else { // First slice of next frame found
294 10146 pc->frame_start_found = 0;
295
2/2
✓ Branch 0 taken 10054 times.
✓ Branch 1 taken 92 times.
10146 if (!((pc->state64 >> 6 * 8) & 0xFF))
296 10054 return i - 6;
297 92 return i - 5;
298 }
299 }
300 }
301 }
302
303 80322 return END_NOT_FOUND;
304 }
305
306 92480 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
307 const uint8_t **poutbuf, int *poutbuf_size,
308 const uint8_t *buf, int buf_size)
309 {
310 int next;
311 92480 HEVCParserContext *ctx = s->priv_data;
312 92480 ParseContext *pc = &ctx->pc;
313 92480 int is_dummy_buf = !buf_size;
314 92480 const uint8_t *dummy_buf = buf;
315
316
4/4
✓ Branch 0 taken 78993 times.
✓ Branch 1 taken 13487 times.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 78792 times.
92480 if (avctx->extradata && !ctx->parsed_extradata) {
317 201 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
318 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
319 1, avctx);
320 201 ctx->parsed_extradata = 1;
321 }
322
323
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 92253 times.
92480 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
324 227 next = buf_size;
325 } else {
326 92253 next = hevc_find_frame_end(s, buf, buf_size);
327
2/2
✓ Branch 1 taken 79904 times.
✓ Branch 2 taken 12349 times.
92253 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
328 79904 *poutbuf = NULL;
329 79904 *poutbuf_size = 0;
330 79904 return buf_size;
331 }
332 }
333
334 12576 is_dummy_buf &= (dummy_buf == buf);
335
336
2/2
✓ Branch 0 taken 12360 times.
✓ Branch 1 taken 216 times.
12576 if (!is_dummy_buf)
337 12360 parse_nal_units(s, buf, buf_size, avctx);
338
339 12576 *poutbuf = buf;
340 12576 *poutbuf_size = buf_size;
341 12576 return next;
342 }
343
344 262 static void hevc_parser_close(AVCodecParserContext *s)
345 {
346 262 HEVCParserContext *ctx = s->priv_data;
347
348 262 ff_hevc_ps_uninit(&ctx->ps);
349 262 ff_h2645_packet_uninit(&ctx->pkt);
350 262 ff_hevc_reset_sei(&ctx->sei);
351
352 262 av_freep(&ctx->pc.buffer);
353 262 }
354
355 const AVCodecParser ff_hevc_parser = {
356 .codec_ids = { AV_CODEC_ID_HEVC },
357 .priv_data_size = sizeof(HEVCParserContext),
358 .parser_parse = hevc_parse,
359 .parser_close = hevc_parser_close,
360 };
361