FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_parser.c
Date: 2024-04-24 02:45:42
Exec Total Coverage
Lines: 153 173 88.4%
Functions: 5 5 100.0%
Branches: 101 128 78.9%

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 "hevc_parse.h"
29 #include "hevc_ps.h"
30 #include "hevc_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 11816 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
55 AVCodecContext *avctx)
56 {
57 11816 HEVCParserContext *ctx = s->priv_data;
58 11816 HEVCParamSets *ps = &ctx->ps;
59 11816 HEVCSEI *sei = &ctx->sei;
60 11816 GetBitContext *gb = &nal->gb;
61 const HEVCWindow *ow;
62 11816 int i, num = 0, den = 0;
63
64 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
65 enum HEVCSliceType slice_type;
66
67 11816 first_slice_in_pic_flag = get_bits1(gb);
68 11816 s->picture_structure = sei->picture_timing.picture_struct;
69 11816 s->field_order = sei->picture_timing.picture_struct;
70
71
3/4
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 11422 times.
✓ Branch 2 taken 394 times.
✗ Branch 3 not taken.
11816 if (IS_IRAP_NAL(nal)) {
72 394 s->key_frame = 1;
73 394 skip_bits1(gb); // no_output_of_prior_pics_flag
74 }
75
76 11816 pps_id = get_ue_golomb(gb);
77
3/4
✓ Branch 0 taken 11816 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 11783 times.
11816 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
78 33 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
79 33 return AVERROR_INVALIDDATA;
80 }
81 11783 ps->pps = ps->pps_list[pps_id];
82
83
2/4
✓ Branch 0 taken 11783 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11783 times.
11783 if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
84 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
85 return AVERROR_INVALIDDATA;
86 }
87
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 11574 times.
11783 if (ps->sps != ps->sps_list[ps->pps->sps_id]) {
88 209 ps->sps = ps->sps_list[ps->pps->sps_id];
89 209 ps->vps = ps->vps_list[ps->sps->vps_id];
90 }
91 11783 ow = &ps->sps->output_window;
92
93 11783 s->coded_width = ps->sps->width;
94 11783 s->coded_height = ps->sps->height;
95 11783 s->width = ps->sps->width - ow->left_offset - ow->right_offset;
96 11783 s->height = ps->sps->height - ow->top_offset - ow->bottom_offset;
97 11783 s->format = ps->sps->pix_fmt;
98 11783 avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
99 11783 avctx->level = ps->sps->ptl.general_ptl.level_idc;
100
101
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 11428 times.
11783 if (ps->vps->vps_timing_info_present_flag) {
102 355 num = ps->vps->vps_num_units_in_tick;
103 355 den = ps->vps->vps_time_scale;
104
2/2
✓ Branch 0 taken 347 times.
✓ Branch 1 taken 11081 times.
11428 } else if (ps->sps->vui.vui_timing_info_present_flag) {
105 347 num = ps->sps->vui.vui_num_units_in_tick;
106 347 den = ps->sps->vui.vui_time_scale;
107 }
108
109
3/4
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 11081 times.
✓ Branch 2 taken 702 times.
✗ Branch 3 not taken.
11783 if (num != 0 && den != 0)
110 702 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
111 num, den, 1 << 30);
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11783 times.
11783 if (!first_slice_in_pic_flag) {
114 unsigned int slice_segment_addr;
115 int slice_address_length;
116
117 if (ps->pps->dependent_slice_segments_enabled_flag)
118 dependent_slice_segment_flag = get_bits1(gb);
119 else
120 dependent_slice_segment_flag = 0;
121
122 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
123 ps->sps->ctb_height);
124 slice_segment_addr = get_bitsz(gb, slice_address_length);
125 if (slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
126 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
127 slice_segment_addr);
128 return AVERROR_INVALIDDATA;
129 }
130 } else
131 11783 dependent_slice_segment_flag = 0;
132
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11783 times.
11783 if (dependent_slice_segment_flag)
134 return 0; /* break; */
135
136
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11783 times.
11786 for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
137 3 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
138
139 11783 slice_type = get_ue_golomb_31(gb);
140
5/6
✓ Branch 0 taken 11223 times.
✓ Branch 1 taken 560 times.
✓ Branch 2 taken 8621 times.
✓ Branch 3 taken 2602 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8621 times.
11783 if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
141 slice_type == HEVC_SLICE_B)) {
142 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
143 slice_type);
144 return AVERROR_INVALIDDATA;
145 }
146
2/2
✓ Branch 0 taken 3162 times.
✓ Branch 1 taken 8621 times.
14945 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
147
2/2
✓ Branch 0 taken 2602 times.
✓ Branch 1 taken 560 times.
3162 slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
148 AV_PICTURE_TYPE_I;
149
150
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 11083 times.
11783 if (ps->pps->output_flag_present_flag)
151 700 skip_bits1(gb); // pic_output_flag
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11783 times.
11783 if (ps->sps->separate_colour_plane_flag)
154 skip_bits(gb, 2); // colour_plane_id
155
156
4/4
✓ Branch 0 taken 11582 times.
✓ Branch 1 taken 201 times.
✓ Branch 2 taken 11573 times.
✓ Branch 3 taken 9 times.
11783 if (!IS_IDR_NAL(nal)) {
157 11573 int pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
158 11573 s->output_picture_number = ctx->poc =
159 11573 ff_hevc_compute_poc(ps->sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
160 } else
161 210 s->output_picture_number = ctx->poc = 0;
162
163
2/2
✓ Branch 0 taken 11420 times.
✓ Branch 1 taken 363 times.
11783 if (nal->temporal_id == 0 &&
164
2/2
✓ Branch 0 taken 8365 times.
✓ Branch 1 taken 3055 times.
11420 nal->type != HEVC_NAL_TRAIL_N &&
165
1/2
✓ Branch 0 taken 8365 times.
✗ Branch 1 not taken.
8365 nal->type != HEVC_NAL_TSA_N &&
166
1/2
✓ Branch 0 taken 8365 times.
✗ Branch 1 not taken.
8365 nal->type != HEVC_NAL_STSA_N &&
167
2/2
✓ Branch 0 taken 8333 times.
✓ Branch 1 taken 32 times.
8365 nal->type != HEVC_NAL_RADL_N &&
168
2/2
✓ Branch 0 taken 7914 times.
✓ Branch 1 taken 419 times.
8333 nal->type != HEVC_NAL_RASL_N &&
169
2/2
✓ Branch 0 taken 7886 times.
✓ Branch 1 taken 28 times.
7914 nal->type != HEVC_NAL_RADL_R &&
170
2/2
✓ Branch 0 taken 7269 times.
✓ Branch 1 taken 617 times.
7886 nal->type != HEVC_NAL_RASL_R)
171 7269 ctx->pocTid0 = ctx->poc;
172
173 11783 return 1; /* no need to evaluate the rest */
174 }
175
176 /**
177 * Parse NAL units of found picture and decode some basic information.
178 *
179 * @param s parser context.
180 * @param avctx codec context.
181 * @param buf buffer with field/frame data.
182 * @param buf_size size of the buffer.
183 */
184 11817 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
185 int buf_size, AVCodecContext *avctx)
186 {
187 11817 HEVCParserContext *ctx = s->priv_data;
188 11817 HEVCParamSets *ps = &ctx->ps;
189 11817 HEVCSEI *sei = &ctx->sei;
190 int ret, i;
191
192 /* set some sane default values */
193 11817 s->pict_type = AV_PICTURE_TYPE_I;
194 11817 s->key_frame = 0;
195 11817 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
196
197 11817 ff_hevc_reset_sei(sei);
198
199 11817 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc,
200 ctx->nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11817 times.
11817 if (ret < 0)
202 return ret;
203
204
2/2
✓ Branch 0 taken 15278 times.
✓ Branch 1 taken 1 times.
15279 for (i = 0; i < ctx->pkt.nb_nals; i++) {
205 15278 H2645NAL *nal = &ctx->pkt.nals[i];
206 15278 GetBitContext *gb = &nal->gb;
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15278 times.
15278 if (nal->nuh_layer_id > 0)
209 continue;
210
211
6/6
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 235 times.
✓ Branch 2 taken 1360 times.
✓ Branch 3 taken 946 times.
✓ Branch 4 taken 11816 times.
✓ Branch 5 taken 685 times.
15278 switch (nal->type) {
212 236 case HEVC_NAL_VPS:
213 236 ff_hevc_decode_nal_vps(gb, avctx, ps);
214 236 break;
215 235 case HEVC_NAL_SPS:
216 235 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
217 235 break;
218 1360 case HEVC_NAL_PPS:
219 1360 ff_hevc_decode_nal_pps(gb, avctx, ps);
220 1360 break;
221 946 case HEVC_NAL_SEI_PREFIX:
222 case HEVC_NAL_SEI_SUFFIX:
223 946 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
224 946 break;
225 11816 case HEVC_NAL_TRAIL_N:
226 case HEVC_NAL_TRAIL_R:
227 case HEVC_NAL_TSA_N:
228 case HEVC_NAL_TSA_R:
229 case HEVC_NAL_STSA_N:
230 case HEVC_NAL_STSA_R:
231 case HEVC_NAL_BLA_W_LP:
232 case HEVC_NAL_BLA_W_RADL:
233 case HEVC_NAL_BLA_N_LP:
234 case HEVC_NAL_IDR_W_RADL:
235 case HEVC_NAL_IDR_N_LP:
236 case HEVC_NAL_CRA_NUT:
237 case HEVC_NAL_RADL_N:
238 case HEVC_NAL_RADL_R:
239 case HEVC_NAL_RASL_N:
240 case HEVC_NAL_RASL_R:
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11816 times.
11816 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
242 s->repeat_pict = 1;
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11816 times.
11816 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
244 s->repeat_pict = 2;
245 }
246 11816 ret = hevc_parse_slice_header(s, nal, avctx);
247
1/2
✓ Branch 0 taken 11816 times.
✗ Branch 1 not taken.
11816 if (ret)
248 11816 return ret;
249 break;
250 }
251 }
252 /* didn't find a picture! */
253 1 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
254 1 return -1;
255 }
256
257 /**
258 * Find the end of the current frame in the bitstream.
259 * @return the position of the first byte of the next frame, or END_NOT_FOUND
260 */
261 89150 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
262 int buf_size)
263 {
264 89150 HEVCParserContext *ctx = s->priv_data;
265 89150 ParseContext *pc = &ctx->pc;
266 int i;
267
268
2/2
✓ Branch 0 taken 79296856 times.
✓ Branch 1 taken 77642 times.
79374498 for (i = 0; i < buf_size; i++) {
269 int nut;
270
271 79296856 pc->state64 = (pc->state64 << 8) | buf[i];
272
273
2/2
✓ Branch 0 taken 79238923 times.
✓ Branch 1 taken 57933 times.
79296856 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
274 79238923 continue;
275
276 57933 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
277 // Beginning of access unit
278
7/8
✓ Branch 0 taken 13520 times.
✓ Branch 1 taken 44413 times.
✓ Branch 2 taken 9612 times.
✓ Branch 3 taken 3908 times.
✓ Branch 4 taken 52799 times.
✓ Branch 5 taken 1226 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 52799 times.
57933 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
279
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52799 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
52799 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
280
2/2
✓ Branch 0 taken 1678 times.
✓ Branch 1 taken 3456 times.
5134 if (pc->frame_start_found) {
281 1678 pc->frame_start_found = 0;
282
2/2
✓ Branch 0 taken 1677 times.
✓ Branch 1 taken 1 times.
1678 if (!((pc->state64 >> 6 * 8) & 0xFF))
283 1677 return i - 6;
284 1 return i - 5;
285 }
286
3/4
✓ Branch 0 taken 9419 times.
✓ Branch 1 taken 43380 times.
✓ Branch 2 taken 9419 times.
✗ Branch 3 not taken.
52799 } else if (nut <= HEVC_NAL_RASL_R ||
287
2/2
✓ Branch 0 taken 1033 times.
✓ Branch 1 taken 8386 times.
9419 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
288 44413 int first_slice_segment_in_pic_flag = buf[i] >> 7;
289
2/2
✓ Branch 0 taken 21536 times.
✓ Branch 1 taken 22877 times.
44413 if (first_slice_segment_in_pic_flag) {
290
2/2
✓ Branch 0 taken 11706 times.
✓ Branch 1 taken 9830 times.
21536 if (!pc->frame_start_found) {
291 11706 pc->frame_start_found = 1;
292 } else { // First slice of next frame found
293 9830 pc->frame_start_found = 0;
294
2/2
✓ Branch 0 taken 9738 times.
✓ Branch 1 taken 92 times.
9830 if (!((pc->state64 >> 6 * 8) & 0xFF))
295 9738 return i - 6;
296 92 return i - 5;
297 }
298 }
299 }
300 }
301
302 77642 return END_NOT_FOUND;
303 }
304
305 89262 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
306 const uint8_t **poutbuf, int *poutbuf_size,
307 const uint8_t *buf, int buf_size)
308 {
309 int next;
310 89262 HEVCParserContext *ctx = s->priv_data;
311 89262 ParseContext *pc = &ctx->pc;
312 89262 int is_dummy_buf = !buf_size;
313 89262 const uint8_t *dummy_buf = buf;
314
315
4/4
✓ Branch 0 taken 76170 times.
✓ Branch 1 taken 13092 times.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 75982 times.
89262 if (avctx->extradata && !ctx->parsed_extradata) {
316 188 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
317 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
318 1, avctx);
319 188 ctx->parsed_extradata = 1;
320 }
321
322
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 89150 times.
89262 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
323 112 next = buf_size;
324 } else {
325 89150 next = hevc_find_frame_end(s, buf, buf_size);
326
2/2
✓ Branch 1 taken 77244 times.
✓ Branch 2 taken 11906 times.
89150 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
327 77244 *poutbuf = NULL;
328 77244 *poutbuf_size = 0;
329 77244 return buf_size;
330 }
331 }
332
333 12018 is_dummy_buf &= (dummy_buf == buf);
334
335
2/2
✓ Branch 0 taken 11817 times.
✓ Branch 1 taken 201 times.
12018 if (!is_dummy_buf)
336 11817 parse_nal_units(s, buf, buf_size, avctx);
337
338 12018 *poutbuf = buf;
339 12018 *poutbuf_size = buf_size;
340 12018 return next;
341 }
342
343 238 static void hevc_parser_close(AVCodecParserContext *s)
344 {
345 238 HEVCParserContext *ctx = s->priv_data;
346
347 238 ff_hevc_ps_uninit(&ctx->ps);
348 238 ff_h2645_packet_uninit(&ctx->pkt);
349 238 ff_hevc_reset_sei(&ctx->sei);
350
351 238 av_freep(&ctx->pc.buffer);
352 238 }
353
354 const AVCodecParser ff_hevc_parser = {
355 .codec_ids = { AV_CODEC_ID_HEVC },
356 .priv_data_size = sizeof(HEVCParserContext),
357 .parser_parse = hevc_parse,
358 .parser_close = hevc_parser_close,
359 };
360