FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_parser.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 148 169 87.6%
Branches: 96 124 77.4%

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
25 #include "golomb.h"
26 #include "hevc.h"
27 #include "hevc_parse.h"
28 #include "hevc_ps.h"
29 #include "hevc_sei.h"
30 #include "h2645_parse.h"
31 #include "parser.h"
32
33 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
34
35 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
36 #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
37
38 typedef struct HEVCParserContext {
39 ParseContext pc;
40
41 H2645Packet pkt;
42 HEVCParamSets ps;
43 HEVCSEI sei;
44
45 int is_avc;
46 int nal_length_size;
47 int parsed_extradata;
48
49 int poc;
50 int pocTid0;
51 } HEVCParserContext;
52
53 11471 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
54 AVCodecContext *avctx)
55 {
56 11471 HEVCParserContext *ctx = s->priv_data;
57 11471 HEVCParamSets *ps = &ctx->ps;
58 11471 HEVCSEI *sei = &ctx->sei;
59 11471 GetBitContext *gb = &nal->gb;
60 const HEVCWindow *ow;
61 11471 int i, num = 0, den = 0;
62
63 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
64 enum HEVCSliceType slice_type;
65
66 11471 first_slice_in_pic_flag = get_bits1(gb);
67 11471 s->picture_structure = sei->picture_timing.picture_struct;
68 11471 s->field_order = sei->picture_timing.picture_struct;
69
70
3/4
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 11089 times.
✓ Branch 2 taken 382 times.
✗ Branch 3 not taken.
11471 if (IS_IRAP_NAL(nal)) {
71 382 s->key_frame = 1;
72 382 skip_bits1(gb); // no_output_of_prior_pics_flag
73 }
74
75 11471 pps_id = get_ue_golomb(gb);
76
3/4
✓ Branch 0 taken 11471 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 11449 times.
11471 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
77 22 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
78 22 return AVERROR_INVALIDDATA;
79 }
80 11449 ps->pps = (HEVCPPS*)ps->pps_list[pps_id]->data;
81
82
2/4
✓ Branch 0 taken 11449 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11449 times.
11449 if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
83 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
84 return AVERROR_INVALIDDATA;
85 }
86
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 11249 times.
11449 if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
87 200 ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
88 200 ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
89 }
90 11449 ow = &ps->sps->output_window;
91
92 11449 s->coded_width = ps->sps->width;
93 11449 s->coded_height = ps->sps->height;
94 11449 s->width = ps->sps->width - ow->left_offset - ow->right_offset;
95 11449 s->height = ps->sps->height - ow->top_offset - ow->bottom_offset;
96 11449 s->format = ps->sps->pix_fmt;
97 11449 avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
98 11449 avctx->level = ps->sps->ptl.general_ptl.level_idc;
99
100
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 11094 times.
11449 if (ps->vps->vps_timing_info_present_flag) {
101 355 num = ps->vps->vps_num_units_in_tick;
102 355 den = ps->vps->vps_time_scale;
103
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 10786 times.
11094 } else if (ps->sps->vui.vui_timing_info_present_flag) {
104 308 num = ps->sps->vui.vui_num_units_in_tick;
105 308 den = ps->sps->vui.vui_time_scale;
106 }
107
108
3/4
✓ Branch 0 taken 663 times.
✓ Branch 1 taken 10786 times.
✓ Branch 2 taken 663 times.
✗ Branch 3 not taken.
11449 if (num != 0 && den != 0)
109 663 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
110 num, den, 1 << 30);
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11449 times.
11449 if (!first_slice_in_pic_flag) {
113 unsigned int slice_segment_addr;
114 int slice_address_length;
115
116 if (ps->pps->dependent_slice_segments_enabled_flag)
117 dependent_slice_segment_flag = get_bits1(gb);
118 else
119 dependent_slice_segment_flag = 0;
120
121 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
122 ps->sps->ctb_height);
123 slice_segment_addr = get_bitsz(gb, slice_address_length);
124 if (slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
125 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
126 slice_segment_addr);
127 return AVERROR_INVALIDDATA;
128 }
129 } else
130 11449 dependent_slice_segment_flag = 0;
131
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11449 times.
11449 if (dependent_slice_segment_flag)
133 return 0; /* break; */
134
135
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11449 times.
11452 for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
136 3 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
137
138 11449 slice_type = get_ue_golomb_31(gb);
139
5/6
✓ Branch 0 taken 10901 times.
✓ Branch 1 taken 548 times.
✓ Branch 2 taken 8331 times.
✓ Branch 3 taken 2570 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8331 times.
11449 if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
140 slice_type == HEVC_SLICE_B)) {
141 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
142 slice_type);
143 return AVERROR_INVALIDDATA;
144 }
145
2/2
✓ Branch 0 taken 3118 times.
✓ Branch 1 taken 8331 times.
14567 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
146
2/2
✓ Branch 0 taken 2570 times.
✓ Branch 1 taken 548 times.
3118 slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
147 AV_PICTURE_TYPE_I;
148
149
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 10749 times.
11449 if (ps->pps->output_flag_present_flag)
150 700 skip_bits1(gb); // pic_output_flag
151
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11449 times.
11449 if (ps->sps->separate_colour_plane_flag)
153 skip_bits(gb, 2); // colour_plane_id
154
155
4/4
✓ Branch 0 taken 11253 times.
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 11246 times.
✓ Branch 3 taken 7 times.
11449 if (!IS_IDR_NAL(nal)) {
156 11246 int pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
157 11246 s->output_picture_number = ctx->poc =
158 11246 ff_hevc_compute_poc(ps->sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
159 } else
160 203 s->output_picture_number = ctx->poc = 0;
161
162
2/2
✓ Branch 0 taken 11086 times.
✓ Branch 1 taken 363 times.
11449 if (nal->temporal_id == 0 &&
163
2/2
✓ Branch 0 taken 8142 times.
✓ Branch 1 taken 2944 times.
11086 nal->type != HEVC_NAL_TRAIL_N &&
164
1/2
✓ Branch 0 taken 8142 times.
✗ Branch 1 not taken.
8142 nal->type != HEVC_NAL_TSA_N &&
165
1/2
✓ Branch 0 taken 8142 times.
✗ Branch 1 not taken.
8142 nal->type != HEVC_NAL_STSA_N &&
166
2/2
✓ Branch 0 taken 8110 times.
✓ Branch 1 taken 32 times.
8142 nal->type != HEVC_NAL_RADL_N &&
167
2/2
✓ Branch 0 taken 7701 times.
✓ Branch 1 taken 409 times.
8110 nal->type != HEVC_NAL_RASL_N &&
168
2/2
✓ Branch 0 taken 7673 times.
✓ Branch 1 taken 28 times.
7701 nal->type != HEVC_NAL_RADL_R &&
169
2/2
✓ Branch 0 taken 7061 times.
✓ Branch 1 taken 612 times.
7673 nal->type != HEVC_NAL_RASL_R)
170 7061 ctx->pocTid0 = ctx->poc;
171
172 11449 return 1; /* no need to evaluate the rest */
173 }
174
175 /**
176 * Parse NAL units of found picture and decode some basic information.
177 *
178 * @param s parser context.
179 * @param avctx codec context.
180 * @param buf buffer with field/frame data.
181 * @param buf_size size of the buffer.
182 */
183 11472 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
184 int buf_size, AVCodecContext *avctx)
185 {
186 11472 HEVCParserContext *ctx = s->priv_data;
187 11472 HEVCParamSets *ps = &ctx->ps;
188 11472 HEVCSEI *sei = &ctx->sei;
189 int ret, i;
190
191 /* set some sane default values */
192 11472 s->pict_type = AV_PICTURE_TYPE_I;
193 11472 s->key_frame = 0;
194 11472 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
195
196 11472 ff_hevc_reset_sei(sei);
197
198 11472 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc,
199 ctx->nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11472 times.
11472 if (ret < 0)
201 return ret;
202
203
2/2
✓ Branch 0 taken 14743 times.
✓ Branch 1 taken 1 times.
14744 for (i = 0; i < ctx->pkt.nb_nals; i++) {
204 14743 H2645NAL *nal = &ctx->pkt.nals[i];
205 14743 GetBitContext *gb = &nal->gb;
206
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14743 times.
14743 if (nal->nuh_layer_id > 0)
208 continue;
209
210
6/6
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 228 times.
✓ Branch 2 taken 1351 times.
✓ Branch 3 taken 819 times.
✓ Branch 4 taken 11471 times.
✓ Branch 5 taken 645 times.
14743 switch (nal->type) {
211 229 case HEVC_NAL_VPS:
212 229 ff_hevc_decode_nal_vps(gb, avctx, ps);
213 229 break;
214 228 case HEVC_NAL_SPS:
215 228 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
216 228 break;
217 1351 case HEVC_NAL_PPS:
218 1351 ff_hevc_decode_nal_pps(gb, avctx, ps);
219 1351 break;
220 819 case HEVC_NAL_SEI_PREFIX:
221 case HEVC_NAL_SEI_SUFFIX:
222 819 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
223 819 break;
224 11471 case HEVC_NAL_TRAIL_N:
225 case HEVC_NAL_TRAIL_R:
226 case HEVC_NAL_TSA_N:
227 case HEVC_NAL_TSA_R:
228 case HEVC_NAL_STSA_N:
229 case HEVC_NAL_STSA_R:
230 case HEVC_NAL_BLA_W_LP:
231 case HEVC_NAL_BLA_W_RADL:
232 case HEVC_NAL_BLA_N_LP:
233 case HEVC_NAL_IDR_W_RADL:
234 case HEVC_NAL_IDR_N_LP:
235 case HEVC_NAL_CRA_NUT:
236 case HEVC_NAL_RADL_N:
237 case HEVC_NAL_RADL_R:
238 case HEVC_NAL_RASL_N:
239 case HEVC_NAL_RASL_R:
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11471 times.
11471 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
241 s->repeat_pict = 1;
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11471 times.
11471 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
243 s->repeat_pict = 2;
244 }
245 11471 ret = hevc_parse_slice_header(s, nal, avctx);
246
1/2
✓ Branch 0 taken 11471 times.
✗ Branch 1 not taken.
11471 if (ret)
247 11471 return ret;
248 break;
249 }
250 }
251 /* didn't find a picture! */
252 1 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
253 1 return -1;
254 }
255
256 /**
257 * Find the end of the current frame in the bitstream.
258 * @return the position of the first byte of the next frame, or END_NOT_FOUND
259 */
260 88556 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
261 int buf_size)
262 {
263 88556 HEVCParserContext *ctx = s->priv_data;
264 88556 ParseContext *pc = &ctx->pc;
265 int i;
266
267
2/2
✓ Branch 0 taken 78817284 times.
✓ Branch 1 taken 77276 times.
78894560 for (i = 0; i < buf_size; i++) {
268 int nut;
269
270 78817284 pc->state64 = (pc->state64 << 8) | buf[i];
271
272
2/2
✓ Branch 0 taken 78761326 times.
✓ Branch 1 taken 55958 times.
78817284 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
273 78761326 continue;
274
275 55958 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
276 // Beginning of access unit
277
7/8
✓ Branch 0 taken 13102 times.
✓ Branch 1 taken 42856 times.
✓ Branch 2 taken 9295 times.
✓ Branch 3 taken 3807 times.
✓ Branch 4 taken 51047 times.
✓ Branch 5 taken 1104 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 51047 times.
55958 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
278
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 51047 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
51047 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
279
2/2
✓ Branch 0 taken 1640 times.
✓ Branch 1 taken 3271 times.
4911 if (pc->frame_start_found) {
280 1640 pc->frame_start_found = 0;
281 1640 return i - 5;
282 }
283
3/4
✓ Branch 0 taken 9217 times.
✓ Branch 1 taken 41830 times.
✓ Branch 2 taken 9217 times.
✗ Branch 3 not taken.
51047 } else if (nut <= HEVC_NAL_RASL_R ||
284
2/2
✓ Branch 0 taken 1026 times.
✓ Branch 1 taken 8191 times.
9217 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
285 42856 int first_slice_segment_in_pic_flag = buf[i] >> 7;
286
2/2
✓ Branch 0 taken 21111 times.
✓ Branch 1 taken 21745 times.
42856 if (first_slice_segment_in_pic_flag) {
287
2/2
✓ Branch 0 taken 11471 times.
✓ Branch 1 taken 9640 times.
21111 if (!pc->frame_start_found) {
288 11471 pc->frame_start_found = 1;
289 } else { // First slice of next frame found
290 9640 pc->frame_start_found = 0;
291 9640 return i - 5;
292 }
293 }
294 }
295 }
296
297 77276 return END_NOT_FOUND;
298 }
299
300 88556 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
301 const uint8_t **poutbuf, int *poutbuf_size,
302 const uint8_t *buf, int buf_size)
303 {
304 int next;
305 88556 HEVCParserContext *ctx = s->priv_data;
306 88556 ParseContext *pc = &ctx->pc;
307 88556 int is_dummy_buf = !buf_size;
308 88556 const uint8_t *dummy_buf = buf;
309
310
4/4
✓ Branch 0 taken 75567 times.
✓ Branch 1 taken 12989 times.
✓ Branch 2 taken 179 times.
✓ Branch 3 taken 75388 times.
88556 if (avctx->extradata && !ctx->parsed_extradata) {
311 179 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
312 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
313 1, avctx);
314 179 ctx->parsed_extradata = 1;
315 }
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88556 times.
88556 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
318 next = buf_size;
319 } else {
320 88556 next = hevc_find_frame_end(s, buf, buf_size);
321
2/2
✓ Branch 1 taken 76892 times.
✓ Branch 2 taken 11664 times.
88556 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
322 76892 *poutbuf = NULL;
323 76892 *poutbuf_size = 0;
324 76892 return buf_size;
325 }
326 }
327
328 11664 is_dummy_buf &= (dummy_buf == buf);
329
330
2/2
✓ Branch 0 taken 11472 times.
✓ Branch 1 taken 192 times.
11664 if (!is_dummy_buf)
331 11472 parse_nal_units(s, buf, buf_size, avctx);
332
333 11664 *poutbuf = buf;
334 11664 *poutbuf_size = buf_size;
335 11664 return next;
336 }
337
338 207 static void hevc_parser_close(AVCodecParserContext *s)
339 {
340 207 HEVCParserContext *ctx = s->priv_data;
341
342 207 ff_hevc_ps_uninit(&ctx->ps);
343 207 ff_h2645_packet_uninit(&ctx->pkt);
344 207 ff_hevc_reset_sei(&ctx->sei);
345
346 207 av_freep(&ctx->pc.buffer);
347 207 }
348
349 const AVCodecParser ff_hevc_parser = {
350 .codec_ids = { AV_CODEC_ID_HEVC },
351 .priv_data_size = sizeof(HEVCParserContext),
352 .parser_parse = hevc_parse,
353 .parser_close = hevc_parser_close,
354 };
355