FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2024-09-07 18:49:03
Exec Total Coverage
Lines: 151 169 89.3%
Functions: 5 5 100.0%
Branches: 99 124 79.8%

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 11913 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
55 AVCodecContext *avctx)
56 {
57 11913 HEVCParserContext *ctx = s->priv_data;
58 11913 HEVCParamSets *ps = &ctx->ps;
59 11913 HEVCSEI *sei = &ctx->sei;
60 11913 GetBitContext *gb = &nal->gb;
61 const HEVCPPS *pps;
62 const HEVCSPS *sps;
63 const HEVCWindow *ow;
64 11913 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 11913 first_slice_in_pic_flag = get_bits1(gb);
70 11913 s->picture_structure = sei->picture_timing.picture_struct;
71 11913 s->field_order = sei->picture_timing.picture_struct;
72
73
3/4
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 11517 times.
✓ Branch 2 taken 396 times.
✗ Branch 3 not taken.
11913 if (IS_IRAP_NAL(nal)) {
74 396 s->key_frame = 1;
75 396 skip_bits1(gb); // no_output_of_prior_pics_flag
76 }
77
78 11913 pps_id = get_ue_golomb(gb);
79
3/4
✓ Branch 0 taken 11913 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 11880 times.
11913 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 11880 pps = ps->pps_list[pps_id];
84 11880 sps = pps->sps;
85
86 11880 ow = &sps->output_window;
87
88 11880 s->coded_width = sps->width;
89 11880 s->coded_height = sps->height;
90 11880 s->width = sps->width - ow->left_offset - ow->right_offset;
91 11880 s->height = sps->height - ow->top_offset - ow->bottom_offset;
92 11880 s->format = sps->pix_fmt;
93 11880 avctx->profile = sps->ptl.general_ptl.profile_idc;
94 11880 avctx->level = sps->ptl.general_ptl.level_idc;
95
96
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 11525 times.
11880 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 443 times.
✓ Branch 1 taken 11082 times.
11525 } else if (sps->vui.vui_timing_info_present_flag) {
100 443 num = sps->vui.vui_num_units_in_tick;
101 443 den = sps->vui.vui_time_scale;
102 }
103
104
3/4
✓ Branch 0 taken 798 times.
✓ Branch 1 taken 11082 times.
✓ Branch 2 taken 798 times.
✗ Branch 3 not taken.
11880 if (num > 0 && den > 0)
105 798 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
106 num, den, 1 << 30);
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11880 times.
11880 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 11880 dependent_slice_segment_flag = 0;
127
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11880 times.
11880 if (dependent_slice_segment_flag)
129 return 0; /* break; */
130
131
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11880 times.
11883 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
132 3 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
133
134 11880 slice_type = get_ue_golomb_31(gb);
135
5/6
✓ Branch 0 taken 11316 times.
✓ Branch 1 taken 564 times.
✓ Branch 2 taken 8714 times.
✓ Branch 3 taken 2602 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8714 times.
11880 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 3166 times.
✓ Branch 1 taken 8714 times.
15046 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
142
2/2
✓ Branch 0 taken 2602 times.
✓ Branch 1 taken 564 times.
3166 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 11180 times.
11880 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 11880 times.
11880 if (sps->separate_colour_plane)
149 skip_bits(gb, 2); // colour_plane_id
150
151
4/4
✓ Branch 0 taken 11677 times.
✓ Branch 1 taken 203 times.
✓ Branch 2 taken 11668 times.
✓ Branch 3 taken 9 times.
11880 if (!IS_IDR_NAL(nal)) {
152 11668 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
153 11668 s->output_picture_number = ctx->poc =
154 11668 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
155 } else
156 212 s->output_picture_number = ctx->poc = 0;
157
158
2/2
✓ Branch 0 taken 11517 times.
✓ Branch 1 taken 363 times.
11880 if (nal->temporal_id == 0 &&
159
2/2
✓ Branch 0 taken 8462 times.
✓ Branch 1 taken 3055 times.
11517 nal->type != HEVC_NAL_TRAIL_N &&
160
1/2
✓ Branch 0 taken 8462 times.
✗ Branch 1 not taken.
8462 nal->type != HEVC_NAL_TSA_N &&
161
1/2
✓ Branch 0 taken 8462 times.
✗ Branch 1 not taken.
8462 nal->type != HEVC_NAL_STSA_N &&
162
2/2
✓ Branch 0 taken 8430 times.
✓ Branch 1 taken 32 times.
8462 nal->type != HEVC_NAL_RADL_N &&
163
2/2
✓ Branch 0 taken 8011 times.
✓ Branch 1 taken 419 times.
8430 nal->type != HEVC_NAL_RASL_N &&
164
2/2
✓ Branch 0 taken 7983 times.
✓ Branch 1 taken 28 times.
8011 nal->type != HEVC_NAL_RADL_R &&
165
2/2
✓ Branch 0 taken 7366 times.
✓ Branch 1 taken 617 times.
7983 nal->type != HEVC_NAL_RASL_R)
166 7366 ctx->pocTid0 = ctx->poc;
167
168 11880 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 11914 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
180 int buf_size, AVCodecContext *avctx)
181 {
182 11914 HEVCParserContext *ctx = s->priv_data;
183 11914 HEVCParamSets *ps = &ctx->ps;
184 11914 HEVCSEI *sei = &ctx->sei;
185
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 11804 times.
11914 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 11914 s->pict_type = AV_PICTURE_TYPE_I;
190 11914 s->key_frame = 0;
191 11914 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
192
193 11914 ff_hevc_reset_sei(sei);
194
195 11914 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 11914 times.
11914 if (ret < 0)
198 return ret;
199
200
2/2
✓ Branch 0 taken 15486 times.
✓ Branch 1 taken 1 times.
15487 for (i = 0; i < ctx->pkt.nb_nals; i++) {
201 15486 H2645NAL *nal = &ctx->pkt.nals[i];
202 15486 GetBitContext *gb = &nal->gb;
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15486 times.
15486 if (nal->nuh_layer_id > 0)
205 continue;
206
207
6/6
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 237 times.
✓ Branch 2 taken 1362 times.
✓ Branch 3 taken 1051 times.
✓ Branch 4 taken 11913 times.
✓ Branch 5 taken 685 times.
15486 switch (nal->type) {
208 238 case HEVC_NAL_VPS:
209 238 ff_hevc_decode_nal_vps(gb, avctx, ps);
210 238 break;
211 237 case HEVC_NAL_SPS:
212 237 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
213 237 break;
214 1362 case HEVC_NAL_PPS:
215 1362 ff_hevc_decode_nal_pps(gb, avctx, ps);
216 1362 break;
217 1051 case HEVC_NAL_SEI_PREFIX:
218 case HEVC_NAL_SEI_SUFFIX:
219 1051 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
220 1051 break;
221 11913 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 11913 times.
11913 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 11913 times.
11913 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
240 s->repeat_pict = 2;
241 }
242 11913 ret = hevc_parse_slice_header(s, nal, avctx);
243
1/2
✓ Branch 0 taken 11913 times.
✗ Branch 1 not taken.
11913 if (ret)
244 11913 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 89472 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
258 int buf_size)
259 {
260 89472 HEVCParserContext *ctx = s->priv_data;
261 89472 ParseContext *pc = &ctx->pc;
262 int i;
263
264
2/2
✓ Branch 0 taken 79524748 times.
✓ Branch 1 taken 77869 times.
79602617 for (i = 0; i < buf_size; i++) {
265 int nut;
266
267 79524748 pc->state64 = (pc->state64 << 8) | buf[i];
268
269
2/2
✓ Branch 0 taken 79466127 times.
✓ Branch 1 taken 58621 times.
79524748 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
270 79466127 continue;
271
272 58621 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
273 // Beginning of access unit
274
7/8
✓ Branch 0 taken 13822 times.
✓ Branch 1 taken 44799 times.
✓ Branch 2 taken 9908 times.
✓ Branch 3 taken 3914 times.
✓ Branch 4 taken 53281 times.
✓ Branch 5 taken 1426 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 53281 times.
58621 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
275
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 53281 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
53281 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
276
2/2
✓ Branch 0 taken 1773 times.
✓ Branch 1 taken 3567 times.
5340 if (pc->frame_start_found) {
277 1773 pc->frame_start_found = 0;
278
2/2
✓ Branch 0 taken 1772 times.
✓ Branch 1 taken 1 times.
1773 if (!((pc->state64 >> 6 * 8) & 0xFF))
279 1772 return i - 6;
280 1 return i - 5;
281 }
282
3/4
✓ Branch 0 taken 9521 times.
✓ Branch 1 taken 43760 times.
✓ Branch 2 taken 9521 times.
✗ Branch 3 not taken.
53281 } else if (nut <= HEVC_NAL_RASL_R ||
283
2/2
✓ Branch 0 taken 1039 times.
✓ Branch 1 taken 8482 times.
9521 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
284 44799 int first_slice_segment_in_pic_flag = buf[i] >> 7;
285
2/2
✓ Branch 0 taken 21633 times.
✓ Branch 1 taken 23166 times.
44799 if (first_slice_segment_in_pic_flag) {
286
2/2
✓ Branch 0 taken 11803 times.
✓ Branch 1 taken 9830 times.
21633 if (!pc->frame_start_found) {
287 11803 pc->frame_start_found = 1;
288 } else { // First slice of next frame found
289 9830 pc->frame_start_found = 0;
290
2/2
✓ Branch 0 taken 9738 times.
✓ Branch 1 taken 92 times.
9830 if (!((pc->state64 >> 6 * 8) & 0xFF))
291 9738 return i - 6;
292 92 return i - 5;
293 }
294 }
295 }
296 }
297
298 77869 return END_NOT_FOUND;
299 }
300
301 89584 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
302 const uint8_t **poutbuf, int *poutbuf_size,
303 const uint8_t *buf, int buf_size)
304 {
305 int next;
306 89584 HEVCParserContext *ctx = s->priv_data;
307 89584 ParseContext *pc = &ctx->pc;
308 89584 int is_dummy_buf = !buf_size;
309 89584 const uint8_t *dummy_buf = buf;
310
311
4/4
✓ Branch 0 taken 76383 times.
✓ Branch 1 taken 13201 times.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 76194 times.
89584 if (avctx->extradata && !ctx->parsed_extradata) {
312 189 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
313 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
314 1, avctx);
315 189 ctx->parsed_extradata = 1;
316 }
317
318
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 89472 times.
89584 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
319 112 next = buf_size;
320 } else {
321 89472 next = hevc_find_frame_end(s, buf, buf_size);
322
2/2
✓ Branch 1 taken 77467 times.
✓ Branch 2 taken 12005 times.
89472 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
323 77467 *poutbuf = NULL;
324 77467 *poutbuf_size = 0;
325 77467 return buf_size;
326 }
327 }
328
329 12117 is_dummy_buf &= (dummy_buf == buf);
330
331
2/2
✓ Branch 0 taken 11914 times.
✓ Branch 1 taken 203 times.
12117 if (!is_dummy_buf)
332 11914 parse_nal_units(s, buf, buf_size, avctx);
333
334 12117 *poutbuf = buf;
335 12117 *poutbuf_size = buf_size;
336 12117 return next;
337 }
338
339 243 static void hevc_parser_close(AVCodecParserContext *s)
340 {
341 243 HEVCParserContext *ctx = s->priv_data;
342
343 243 ff_hevc_ps_uninit(&ctx->ps);
344 243 ff_h2645_packet_uninit(&ctx->pkt);
345 243 ff_hevc_reset_sei(&ctx->sei);
346
347 243 av_freep(&ctx->pc.buffer);
348 243 }
349
350 const AVCodecParser ff_hevc_parser = {
351 .codec_ids = { AV_CODEC_ID_HEVC },
352 .priv_data_size = sizeof(HEVCParserContext),
353 .parser_parse = hevc_parse,
354 .parser_close = hevc_parser_close,
355 };
356