FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lcevc_parser.c
Date: 2026-10-19 01:23:10
Exec Total Coverage
Lines: 91 119 76.5%
Functions: 5 5 100.0%
Branches: 38 58 65.5%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdint.h>
20
21 #include "libavutil/attributes.h"
22 #include "libavutil/mem.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "get_bits.h"
27 #include "h2645_parse.h"
28 #include "lcevc.h"
29 #include "lcevc_parse.h"
30 #include "lcevctab.h"
31 #include "parser.h"
32 #include "parser_internal.h"
33
34 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
35
36 typedef struct LCEVCParserContext {
37 ParseContext pc;
38
39 H2645Packet pkt;
40
41 int parsed_extradata;
42 int is_lvcc;
43 int nal_length_size;
44 } LCEVCParserContext;
45
46 915 static int lcevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
47 int buf_size)
48 {
49 915 LCEVCParserContext *ctx = s->priv_data;
50 915 ParseContext *pc = &ctx->pc;
51
52
2/2
✓ Branch 0 taken 214752 times.
✓ Branch 1 taken 480 times.
215232 for (int i = 0; i < buf_size; i++) {
53 int nut;
54
55 214752 pc->state = (pc->state << 8) | buf[i];
56
57
2/2
✓ Branch 0 taken 213867 times.
✓ Branch 1 taken 885 times.
214752 if (((pc->state >> 8) & 0xFFFFFF) != START_CODE)
58 213867 continue;
59
60 885 nut = (pc->state >> 1) & 0x1F;
61
62 // Beginning of access unit
63
3/4
✓ Branch 0 taken 870 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 870 times.
✗ Branch 3 not taken.
885 if (nut == LCEVC_IDR_NUT || nut == LCEVC_NON_IDR_NUT) {
64
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 435 times.
885 if (!pc->frame_start_found)
65 450 pc->frame_start_found = 1;
66 else {
67 435 pc->frame_start_found = 0;
68 435 return i - 3;
69 }
70 }
71 }
72
73 480 return END_NOT_FOUND;
74 }
75
76 480 static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx,
77 const H2645NAL *nal)
78 {
79 GetByteContext gbc;
80 480 bytestream2_init(&gbc, nal->data, nal->size);
81 480 bytestream2_skip(&gbc, 2);
82
83
2/2
✓ Branch 1 taken 1024 times.
✓ Branch 2 taken 480 times.
1504 while (bytestream2_get_bytes_left(&gbc) > 1) {
84 GetBitContext gb;
85 uint64_t payload_size;
86 int payload_size_type, payload_type;
87 int block_size;
88
89 1024 int ret = init_get_bits8(&gb, gbc.buffer, bytestream2_get_bytes_left(&gbc));
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1024 times.
1024 if (ret < 0)
91 ✗ return ret;
92
93 1024 payload_size_type = get_bits(&gb, 3);
94 1024 payload_type = get_bits(&gb, 5);
95 1024 payload_size = payload_size_type;
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1024 times.
1024 if (payload_size_type == 6)
97 ✗ return AVERROR_PATCHWELCOME;
98
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 512 times.
1024 if (payload_size_type == 7)
99 512 payload_size = get_mb(&gb);
100
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1024 times.
1024 if (payload_size > INT_MAX - (get_bits_count(&gb) >> 3))
102 ✗ return AVERROR_INVALIDDATA;
103
104 1024 block_size = payload_size + (get_bits_count(&gb) >> 3);
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1024 times.
1024 if (block_size >= bytestream2_get_bytes_left(&gbc))
106 ✗ return AVERROR_INVALIDDATA;
107
108
3/3
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 992 times.
1024 switch (payload_type) {
109 16 case LCEVC_PAYLOAD_TYPE_SEQUENCE_CONFIG:
110 16 avctx->profile = get_bits(&gb, 4);
111 16 avctx->level = get_bits(&gb, 4);
112 16 break;
113 16 case LCEVC_PAYLOAD_TYPE_GLOBAL_CONFIG: {
114 int resolution_type, chroma_format_idc, bit_depth;
115 int processed_planes_type_flag;
116
117 16 processed_planes_type_flag = get_bits1(&gb);
118 16 resolution_type = get_bits(&gb, 6);
119 16 skip_bits1(&gb);
120 16 chroma_format_idc = get_bits(&gb, 2);
121 16 skip_bits(&gb, 2);
122 16 bit_depth = get_bits(&gb, 2); // enhancement_depth_type
123
124 16 s->format = ff_lcevc_depth_type[bit_depth][chroma_format_idc];
125
126
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (resolution_type < 63) {
127 16 s->width = ff_lcevc_resolution_type[resolution_type].width;
128 16 s->height = ff_lcevc_resolution_type[resolution_type].height;
129 } else {
130 int upsample_type, tile_dimensions_type;
131 int temporal_step_width_modifier_signalled_flag, level1_filtering_signalled_flag;
132 // Skip syntax elements until we get to the custom dimension ones
133 ✗ temporal_step_width_modifier_signalled_flag = get_bits1(&gb);
134 ✗ skip_bits(&gb, 3);
135 ✗ upsample_type = get_bits(&gb, 3);
136 ✗ level1_filtering_signalled_flag = get_bits1(&gb);
137 ✗ skip_bits(&gb, 4);
138 ✗ tile_dimensions_type = get_bits(&gb, 2);
139 ✗ skip_bits(&gb, 4);
140 ✗ if (processed_planes_type_flag)
141 ✗ skip_bits(&gb, 4);
142 ✗ if (temporal_step_width_modifier_signalled_flag)
143 ✗ skip_bits(&gb, 8);
144 ✗ if (upsample_type)
145 ✗ skip_bits_long(&gb, 64);
146 ✗ if (level1_filtering_signalled_flag)
147 ✗ skip_bits(&gb, 8);
148 ✗ if (tile_dimensions_type) {
149 ✗ if (tile_dimensions_type == 3)
150 ✗ skip_bits_long(&gb, 32);
151 ✗ skip_bits(&gb, 8);
152 }
153
154 ✗ s->width = get_bits(&gb, 16);
155 ✗ s->height = get_bits(&gb, 16);
156 }
157 16 break;
158 }
159 992 default:
160 992 break;
161 }
162
163 1024 bytestream2_skip(&gbc, block_size);
164 }
165
166 480 return 0;
167 }
168
169 496 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
170 int buf_size, AVCodecContext *avctx)
171 {
172 496 LCEVCParserContext *ctx = s->priv_data;
173
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 465 times.
496 int flags = (H2645_FLAG_IS_NALFF * !!ctx->is_lvcc) | H2645_FLAG_SMALL_PADDING;
174 int ret, i;
175
176 /* set some sane default values */
177 496 s->pict_type = AV_PICTURE_TYPE_NONE;
178 496 s->key_frame = 0;
179 496 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
180
181 496 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx,
182 ctx->nal_length_size, AV_CODEC_ID_LCEVC, flags);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 496 times.
496 if (ret < 0)
184 ✗ return ret;
185
186
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 496 times.
976 for (i = 0; i < ctx->pkt.nb_nals; i++) {
187 480 H2645NAL *nal = &ctx->pkt.nals[i];
188
189
2/3
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 464 times.
✗ Branch 2 not taken.
480 switch (nal->type) {
190 16 case LCEVC_IDR_NUT:
191 16 s->key_frame = 1;
192 av_fallthrough;
193 480 case LCEVC_NON_IDR_NUT:
194 480 parse_nal_unit(s, avctx, nal);
195 480 break;
196 ✗ default:
197 ✗ break;
198 }
199 }
200
201 496 return 0;
202 }
203
204 946 static int lcevc_parse(AVCodecParserContext *s,
205 AVCodecContext *avctx,
206 const uint8_t **poutbuf, int *poutbuf_size,
207 const uint8_t *buf, int buf_size)
208 {
209 946 LCEVCParserContext *ctx = s->priv_data;
210 946 ParseContext *pc = &ctx->pc;
211 int next;
212
213
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 903 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 27 times.
946 if (!ctx->parsed_extradata && avctx->extradata_size > 4) {
214 16 ctx->parsed_extradata = 1;
215 16 ctx->is_lvcc = !!avctx->extradata[0];
216
217
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (ctx->is_lvcc)
218 1 ctx->nal_length_size = (avctx->extradata[4] >> 6) + 1;
219 }
220
221
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 915 times.
946 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
222 31 next = buf_size;
223 } else {
224 915 next = lcevc_find_frame_end(s, buf, buf_size);
225
2/2
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 465 times.
915 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
226 450 *poutbuf = NULL;
227 450 *poutbuf_size = 0;
228 450 return buf_size;
229 }
230 }
231
232 496 parse_nal_units(s, buf, buf_size, avctx);
233
234 496 *poutbuf = buf;
235 496 *poutbuf_size = buf_size;
236 496 return next;
237 }
238
239 16 static void lcevc_parser_close(AVCodecParserContext *s)
240 {
241 16 LCEVCParserContext *ctx = s->priv_data;
242
243 16 ff_h2645_packet_uninit(&ctx->pkt);
244
245 16 av_freep(&ctx->pc.buffer);
246 16 }
247
248 const FFCodecParser ff_lcevc_parser = {
249 PARSER_CODEC_LIST(AV_CODEC_ID_LCEVC),
250 .priv_data_size = sizeof(LCEVCParserContext),
251 .parse = lcevc_parse,
252 .close = lcevc_parser_close,
253 };
254