FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parser.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 150 168 89.3%
Functions: 5 5 100.0%
Branches: 97 122 79.5%

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 11912 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
55 AVCodecContext *avctx)
56 {
57 11912 HEVCParserContext *ctx = s->priv_data;
58 11912 HEVCParamSets *ps = &ctx->ps;
59 11912 HEVCSEI *sei = &ctx->sei;
60 11912 GetBitContext *gb = &nal->gb;
61 const HEVCPPS *pps;
62 const HEVCSPS *sps;
63 const HEVCWindow *ow;
64 11912 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 11912 first_slice_in_pic_flag = get_bits1(gb);
70 11912 s->picture_structure = sei->picture_timing.picture_struct;
71 11912 s->field_order = sei->picture_timing.picture_struct;
72
73
3/4
✓ Branch 0 taken 395 times.
✓ Branch 1 taken 11517 times.
✓ Branch 2 taken 395 times.
✗ Branch 3 not taken.
11912 if (IS_IRAP_NAL(nal)) {
74 395 s->key_frame = 1;
75 395 skip_bits1(gb); // no_output_of_prior_pics_flag
76 }
77
78 11912 pps_id = get_ue_golomb(gb);
79
3/4
✓ Branch 0 taken 11912 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 11879 times.
11912 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 11879 pps = ps->pps_list[pps_id];
84 11879 sps = pps->sps;
85
86 11879 ow = &sps->output_window;
87
88 11879 s->coded_width = sps->width;
89 11879 s->coded_height = sps->height;
90 11879 s->width = sps->width - ow->left_offset - ow->right_offset;
91 11879 s->height = sps->height - ow->top_offset - ow->bottom_offset;
92 11879 s->format = sps->pix_fmt;
93 11879 avctx->profile = sps->ptl.general_ptl.profile_idc;
94 11879 avctx->level = sps->ptl.general_ptl.level_idc;
95
96
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 11524 times.
11879 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 11081 times.
11524 } 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 11081 times.
✓ Branch 2 taken 798 times.
✗ Branch 3 not taken.
11879 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 11879 times.
11879 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 11879 dependent_slice_segment_flag = 0;
127
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11879 times.
11879 if (dependent_slice_segment_flag)
129 return 0; /* break; */
130
131
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11879 times.
11882 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
132 3 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
133
134 11879 slice_type = get_ue_golomb_31(gb);
135
5/6
✓ Branch 0 taken 11316 times.
✓ Branch 1 taken 563 times.
✓ Branch 2 taken 8714 times.
✓ Branch 3 taken 2602 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8714 times.
11879 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 3165 times.
✓ Branch 1 taken 8714 times.
15044 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
142
2/2
✓ Branch 0 taken 2602 times.
✓ Branch 1 taken 563 times.
3165 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 11179 times.
11879 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 11879 times.
11879 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 202 times.
✓ Branch 2 taken 11668 times.
✓ Branch 3 taken 9 times.
11879 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 211 s->output_picture_number = ctx->poc = 0;
157
158
2/2
✓ Branch 0 taken 11516 times.
✓ Branch 1 taken 363 times.
11879 if (nal->temporal_id == 0 &&
159
2/2
✓ Branch 0 taken 8461 times.
✓ Branch 1 taken 3055 times.
11516 nal->type != HEVC_NAL_TRAIL_N &&
160
1/2
✓ Branch 0 taken 8461 times.
✗ Branch 1 not taken.
8461 nal->type != HEVC_NAL_TSA_N &&
161
1/2
✓ Branch 0 taken 8461 times.
✗ Branch 1 not taken.
8461 nal->type != HEVC_NAL_STSA_N &&
162
2/2
✓ Branch 0 taken 8429 times.
✓ Branch 1 taken 32 times.
8461 nal->type != HEVC_NAL_RADL_N &&
163
2/2
✓ Branch 0 taken 8010 times.
✓ Branch 1 taken 419 times.
8429 nal->type != HEVC_NAL_RASL_N &&
164
2/2
✓ Branch 0 taken 7982 times.
✓ Branch 1 taken 28 times.
8010 nal->type != HEVC_NAL_RADL_R &&
165
2/2
✓ Branch 0 taken 7365 times.
✓ Branch 1 taken 617 times.
7982 nal->type != HEVC_NAL_RASL_R)
166 7365 ctx->pocTid0 = ctx->poc;
167
168 11879 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 11913 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
180 int buf_size, AVCodecContext *avctx)
181 {
182 11913 HEVCParserContext *ctx = s->priv_data;
183 11913 HEVCParamSets *ps = &ctx->ps;
184 11913 HEVCSEI *sei = &ctx->sei;
185 int ret, i;
186
187 /* set some sane default values */
188 11913 s->pict_type = AV_PICTURE_TYPE_I;
189 11913 s->key_frame = 0;
190 11913 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
191
192 11913 ff_hevc_reset_sei(sei);
193
194 11913 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc,
195 ctx->nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11913 times.
11913 if (ret < 0)
197 return ret;
198
199
2/2
✓ Branch 0 taken 15480 times.
✓ Branch 1 taken 1 times.
15481 for (i = 0; i < ctx->pkt.nb_nals; i++) {
200 15480 H2645NAL *nal = &ctx->pkt.nals[i];
201 15480 GetBitContext *gb = &nal->gb;
202
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15480 times.
15480 if (nal->nuh_layer_id > 0)
204 continue;
205
206
6/6
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 1361 times.
✓ Branch 3 taken 1049 times.
✓ Branch 4 taken 11912 times.
✓ Branch 5 taken 685 times.
15480 switch (nal->type) {
207 237 case HEVC_NAL_VPS:
208 237 ff_hevc_decode_nal_vps(gb, avctx, ps);
209 237 break;
210 236 case HEVC_NAL_SPS:
211 236 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
212 236 break;
213 1361 case HEVC_NAL_PPS:
214 1361 ff_hevc_decode_nal_pps(gb, avctx, ps);
215 1361 break;
216 1049 case HEVC_NAL_SEI_PREFIX:
217 case HEVC_NAL_SEI_SUFFIX:
218 1049 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
219 1049 break;
220 11912 case HEVC_NAL_TRAIL_N:
221 case HEVC_NAL_TRAIL_R:
222 case HEVC_NAL_TSA_N:
223 case HEVC_NAL_TSA_R:
224 case HEVC_NAL_STSA_N:
225 case HEVC_NAL_STSA_R:
226 case HEVC_NAL_BLA_W_LP:
227 case HEVC_NAL_BLA_W_RADL:
228 case HEVC_NAL_BLA_N_LP:
229 case HEVC_NAL_IDR_W_RADL:
230 case HEVC_NAL_IDR_N_LP:
231 case HEVC_NAL_CRA_NUT:
232 case HEVC_NAL_RADL_N:
233 case HEVC_NAL_RADL_R:
234 case HEVC_NAL_RASL_N:
235 case HEVC_NAL_RASL_R:
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11912 times.
11912 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
237 s->repeat_pict = 1;
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11912 times.
11912 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
239 s->repeat_pict = 2;
240 }
241 11912 ret = hevc_parse_slice_header(s, nal, avctx);
242
1/2
✓ Branch 0 taken 11912 times.
✗ Branch 1 not taken.
11912 if (ret)
243 11912 return ret;
244 break;
245 }
246 }
247 /* didn't find a picture! */
248 1 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
249 1 return -1;
250 }
251
252 /**
253 * Find the end of the current frame in the bitstream.
254 * @return the position of the first byte of the next frame, or END_NOT_FOUND
255 */
256 89394 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
257 int buf_size)
258 {
259 89394 HEVCParserContext *ctx = s->priv_data;
260 89394 ParseContext *pc = &ctx->pc;
261 int i;
262
263
2/2
✓ Branch 0 taken 79447030 times.
✓ Branch 1 taken 77791 times.
79524821 for (i = 0; i < buf_size; i++) {
264 int nut;
265
266 79447030 pc->state64 = (pc->state64 << 8) | buf[i];
267
268
2/2
✓ Branch 0 taken 79388416 times.
✓ Branch 1 taken 58614 times.
79447030 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
269 79388416 continue;
270
271 58614 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
272 // Beginning of access unit
273
7/8
✓ Branch 0 taken 13817 times.
✓ Branch 1 taken 44797 times.
✓ Branch 2 taken 9906 times.
✓ Branch 3 taken 3911 times.
✓ Branch 4 taken 53279 times.
✓ Branch 5 taken 1424 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 53279 times.
58614 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
274
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 53279 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
53279 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
275
2/2
✓ Branch 0 taken 1773 times.
✓ Branch 1 taken 3562 times.
5335 if (pc->frame_start_found) {
276 1773 pc->frame_start_found = 0;
277
2/2
✓ Branch 0 taken 1772 times.
✓ Branch 1 taken 1 times.
1773 if (!((pc->state64 >> 6 * 8) & 0xFF))
278 1772 return i - 6;
279 1 return i - 5;
280 }
281
3/4
✓ Branch 0 taken 9519 times.
✓ Branch 1 taken 43760 times.
✓ Branch 2 taken 9519 times.
✗ Branch 3 not taken.
53279 } else if (nut <= HEVC_NAL_RASL_R ||
282
2/2
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 8482 times.
9519 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
283 44797 int first_slice_segment_in_pic_flag = buf[i] >> 7;
284
2/2
✓ Branch 0 taken 21632 times.
✓ Branch 1 taken 23165 times.
44797 if (first_slice_segment_in_pic_flag) {
285
2/2
✓ Branch 0 taken 11802 times.
✓ Branch 1 taken 9830 times.
21632 if (!pc->frame_start_found) {
286 11802 pc->frame_start_found = 1;
287 } else { // First slice of next frame found
288 9830 pc->frame_start_found = 0;
289
2/2
✓ Branch 0 taken 9738 times.
✓ Branch 1 taken 92 times.
9830 if (!((pc->state64 >> 6 * 8) & 0xFF))
290 9738 return i - 6;
291 92 return i - 5;
292 }
293 }
294 }
295 }
296
297 77791 return END_NOT_FOUND;
298 }
299
300 89506 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 89506 HEVCParserContext *ctx = s->priv_data;
306 89506 ParseContext *pc = &ctx->pc;
307 89506 int is_dummy_buf = !buf_size;
308 89506 const uint8_t *dummy_buf = buf;
309
310
4/4
✓ Branch 0 taken 76383 times.
✓ Branch 1 taken 13123 times.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 76194 times.
89506 if (avctx->extradata && !ctx->parsed_extradata) {
311 189 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 189 ctx->parsed_extradata = 1;
315 }
316
317
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 89394 times.
89506 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
318 112 next = buf_size;
319 } else {
320 89394 next = hevc_find_frame_end(s, buf, buf_size);
321
2/2
✓ Branch 1 taken 77391 times.
✓ Branch 2 taken 12003 times.
89394 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
322 77391 *poutbuf = NULL;
323 77391 *poutbuf_size = 0;
324 77391 return buf_size;
325 }
326 }
327
328 12115 is_dummy_buf &= (dummy_buf == buf);
329
330
2/2
✓ Branch 0 taken 11913 times.
✓ Branch 1 taken 202 times.
12115 if (!is_dummy_buf)
331 11913 parse_nal_units(s, buf, buf_size, avctx);
332
333 12115 *poutbuf = buf;
334 12115 *poutbuf_size = buf_size;
335 12115 return next;
336 }
337
338 242 static void hevc_parser_close(AVCodecParserContext *s)
339 {
340 242 HEVCParserContext *ctx = s->priv_data;
341
342 242 ff_hevc_ps_uninit(&ctx->ps);
343 242 ff_h2645_packet_uninit(&ctx->pkt);
344 242 ff_hevc_reset_sei(&ctx->sei);
345
346 242 av_freep(&ctx->pc.buffer);
347 242 }
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