FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2024-11-20 23:03:26
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 12248 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
55 AVCodecContext *avctx)
56 {
57 12248 HEVCParserContext *ctx = s->priv_data;
58 12248 HEVCParamSets *ps = &ctx->ps;
59 12248 HEVCSEI *sei = &ctx->sei;
60 12248 GetBitContext *gb = &nal->gb;
61 const HEVCPPS *pps;
62 const HEVCSPS *sps;
63 const HEVCWindow *ow;
64 12248 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 12248 first_slice_in_pic_flag = get_bits1(gb);
70 12248 s->picture_structure = sei->picture_timing.picture_struct;
71 12248 s->field_order = sei->picture_timing.picture_struct;
72
73
3/4
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 11774 times.
✓ Branch 2 taken 474 times.
✗ Branch 3 not taken.
12248 if (IS_IRAP_NAL(nal)) {
74 474 s->key_frame = 1;
75 474 skip_bits1(gb); // no_output_of_prior_pics_flag
76 }
77
78 12248 pps_id = get_ue_golomb(gb);
79
3/4
✓ Branch 0 taken 12248 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 12215 times.
12248 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 12215 pps = ps->pps_list[pps_id];
84 12215 sps = pps->sps;
85
86 12215 ow = &sps->output_window;
87
88 12215 s->coded_width = sps->width;
89 12215 s->coded_height = sps->height;
90 12215 s->width = sps->width - ow->left_offset - ow->right_offset;
91 12215 s->height = sps->height - ow->top_offset - ow->bottom_offset;
92 12215 s->format = sps->pix_fmt;
93 12215 avctx->profile = sps->ptl.general_ptl.profile_idc;
94 12215 avctx->level = sps->ptl.general_ptl.level_idc;
95
96
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 11860 times.
12215 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 453 times.
✓ Branch 1 taken 11407 times.
11860 } else if (sps->vui.vui_timing_info_present_flag) {
100 453 num = sps->vui.vui_num_units_in_tick;
101 453 den = sps->vui.vui_time_scale;
102 }
103
104
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 11407 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
12215 if (num > 0 && den > 0)
105 808 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
106 num, den, 1 << 30);
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12215 times.
12215 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 12215 dependent_slice_segment_flag = 0;
127
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12215 times.
12215 if (dependent_slice_segment_flag)
129 return 0; /* break; */
130
131
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 12215 times.
12750 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
132 535 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
133
134 12215 slice_type = get_ue_golomb_31(gb);
135
5/6
✓ Branch 0 taken 11573 times.
✓ Branch 1 taken 642 times.
✓ Branch 2 taken 8962 times.
✓ Branch 3 taken 2611 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8962 times.
12215 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 3253 times.
✓ Branch 1 taken 8962 times.
15468 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
142
2/2
✓ Branch 0 taken 2611 times.
✓ Branch 1 taken 642 times.
3253 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 11515 times.
12215 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 12215 times.
12215 if (sps->separate_colour_plane)
149 skip_bits(gb, 2); // colour_plane_id
150
151
4/4
✓ Branch 0 taken 12002 times.
✓ Branch 1 taken 213 times.
✓ Branch 2 taken 11992 times.
✓ Branch 3 taken 10 times.
12215 if (!IS_IDR_NAL(nal)) {
152 11992 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
153 11992 s->output_picture_number = ctx->poc =
154 11992 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
155 } else
156 223 s->output_picture_number = ctx->poc = 0;
157
158
2/2
✓ Branch 0 taken 11852 times.
✓ Branch 1 taken 363 times.
12215 if (nal->temporal_id == 0 &&
159
2/2
✓ Branch 0 taken 8688 times.
✓ Branch 1 taken 3164 times.
11852 nal->type != HEVC_NAL_TRAIL_N &&
160
1/2
✓ Branch 0 taken 8688 times.
✗ Branch 1 not taken.
8688 nal->type != HEVC_NAL_TSA_N &&
161
1/2
✓ Branch 0 taken 8688 times.
✗ Branch 1 not taken.
8688 nal->type != HEVC_NAL_STSA_N &&
162
2/2
✓ Branch 0 taken 8652 times.
✓ Branch 1 taken 36 times.
8688 nal->type != HEVC_NAL_RADL_N &&
163
2/2
✓ Branch 0 taken 8217 times.
✓ Branch 1 taken 435 times.
8652 nal->type != HEVC_NAL_RASL_N &&
164
2/2
✓ Branch 0 taken 8186 times.
✓ Branch 1 taken 31 times.
8217 nal->type != HEVC_NAL_RADL_R &&
165
2/2
✓ Branch 0 taken 7557 times.
✓ Branch 1 taken 629 times.
8186 nal->type != HEVC_NAL_RASL_R)
166 7557 ctx->pocTid0 = ctx->poc;
167
168 12215 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 12249 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
180 int buf_size, AVCodecContext *avctx)
181 {
182 12249 HEVCParserContext *ctx = s->priv_data;
183 12249 HEVCParamSets *ps = &ctx->ps;
184 12249 HEVCSEI *sei = &ctx->sei;
185
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 12139 times.
12249 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 12249 s->pict_type = AV_PICTURE_TYPE_I;
190 12249 s->key_frame = 0;
191 12249 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
192
193 12249 ff_hevc_reset_sei(sei);
194
195 12249 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 12249 times.
12249 if (ret < 0)
198 return ret;
199
200
2/2
✓ Branch 0 taken 15864 times.
✓ Branch 1 taken 1 times.
15865 for (i = 0; i < ctx->pkt.nb_nals; i++) {
201 15864 H2645NAL *nal = &ctx->pkt.nals[i];
202 15864 GetBitContext *gb = &nal->gb;
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15864 times.
15864 if (nal->nuh_layer_id > 0)
205 continue;
206
207
6/6
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 247 times.
✓ Branch 2 taken 1372 times.
✓ Branch 3 taken 1064 times.
✓ Branch 4 taken 12248 times.
✓ Branch 5 taken 685 times.
15864 switch (nal->type) {
208 248 case HEVC_NAL_VPS:
209 248 ff_hevc_decode_nal_vps(gb, avctx, ps);
210 248 break;
211 247 case HEVC_NAL_SPS:
212 247 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
213 247 break;
214 1372 case HEVC_NAL_PPS:
215 1372 ff_hevc_decode_nal_pps(gb, avctx, ps);
216 1372 break;
217 1064 case HEVC_NAL_SEI_PREFIX:
218 case HEVC_NAL_SEI_SUFFIX:
219 1064 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
220 1064 break;
221 12248 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 12248 times.
12248 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 12248 times.
12248 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
240 s->repeat_pict = 2;
241 }
242 12248 ret = hevc_parse_slice_header(s, nal, avctx);
243
1/2
✓ Branch 0 taken 12248 times.
✗ Branch 1 not taken.
12248 if (ret)
244 12248 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 92175 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
258 int buf_size)
259 {
260 92175 HEVCParserContext *ctx = s->priv_data;
261 92175 ParseContext *pc = &ctx->pc;
262 int i;
263
264
2/2
✓ Branch 0 taken 81940310 times.
✓ Branch 1 taken 80244 times.
82020554 for (i = 0; i < buf_size; i++) {
265 int nut, layer_id;
266
267 81940310 pc->state64 = (pc->state64 << 8) | buf[i];
268
269
2/2
✓ Branch 0 taken 81880511 times.
✓ Branch 1 taken 59799 times.
81940310 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
270 81880511 continue;
271
272 59799 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
273
274 59799 layer_id = (pc->state64 >> 11) & 0x3F;
275
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 59455 times.
59799 if (layer_id > 0)
276 344 continue;
277
278 // Beginning of access unit
279
7/8
✓ Branch 0 taken 14005 times.
✓ Branch 1 taken 45450 times.
✓ Branch 2 taken 10058 times.
✓ Branch 3 taken 3947 times.
✓ Branch 4 taken 54060 times.
✓ Branch 5 taken 1448 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 54060 times.
59455 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 54060 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
54060 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
281
2/2
✓ Branch 0 taken 1785 times.
✓ Branch 1 taken 3610 times.
5395 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 9795 times.
✓ Branch 1 taken 44265 times.
✓ Branch 2 taken 9795 times.
✗ Branch 3 not taken.
54060 } else if (nut <= HEVC_NAL_RASL_R ||
288
2/2
✓ Branch 0 taken 1185 times.
✓ Branch 1 taken 8610 times.
9795 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
289 45450 int first_slice_segment_in_pic_flag = buf[i] >> 7;
290
2/2
✓ Branch 0 taken 22284 times.
✓ Branch 1 taken 23166 times.
45450 if (first_slice_segment_in_pic_flag) {
291
2/2
✓ Branch 0 taken 12138 times.
✓ Branch 1 taken 10146 times.
22284 if (!pc->frame_start_found) {
292 12138 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 80244 return END_NOT_FOUND;
304 }
305
306 92287 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 92287 HEVCParserContext *ctx = s->priv_data;
312 92287 ParseContext *pc = &ctx->pc;
313 92287 int is_dummy_buf = !buf_size;
314 92287 const uint8_t *dummy_buf = buf;
315
316
4/4
✓ Branch 0 taken 78878 times.
✓ Branch 1 taken 13409 times.
✓ Branch 2 taken 196 times.
✓ Branch 3 taken 78682 times.
92287 if (avctx->extradata && !ctx->parsed_extradata) {
317 196 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 196 ctx->parsed_extradata = 1;
321 }
322
323
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 92175 times.
92287 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
324 112 next = buf_size;
325 } else {
326 92175 next = hevc_find_frame_end(s, buf, buf_size);
327
2/2
✓ Branch 1 taken 79828 times.
✓ Branch 2 taken 12347 times.
92175 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
328 79828 *poutbuf = NULL;
329 79828 *poutbuf_size = 0;
330 79828 return buf_size;
331 }
332 }
333
334 12459 is_dummy_buf &= (dummy_buf == buf);
335
336
2/2
✓ Branch 0 taken 12249 times.
✓ Branch 1 taken 210 times.
12459 if (!is_dummy_buf)
337 12249 parse_nal_units(s, buf, buf_size, avctx);
338
339 12459 *poutbuf = buf;
340 12459 *poutbuf_size = buf_size;
341 12459 return next;
342 }
343
344 255 static void hevc_parser_close(AVCodecParserContext *s)
345 {
346 255 HEVCParserContext *ctx = s->priv_data;
347
348 255 ff_hevc_ps_uninit(&ctx->ps);
349 255 ff_h2645_packet_uninit(&ctx->pkt);
350 255 ff_hevc_reset_sei(&ctx->sei);
351
352 255 av_freep(&ctx->pc.buffer);
353 255 }
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