FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/evcdec.c
Date: 2026-04-19 20:43:40
Exec Total Coverage
Lines: 75 89 84.3%
Functions: 4 4 100.0%
Branches: 41 60 68.3%

Line Branch Exec Source
1 /*
2 * RAW EVC video demuxer
3 *
4 * Copyright (c) 2021 Dawid Kozinski <d.kozinski@samsung.com>
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 "libavcodec/evc.h"
24 #include "libavcodec/bsf.h"
25
26 #include "libavutil/opt.h"
27
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "demux.h"
31 #include "evc.h"
32 #include "internal.h"
33
34
35 #define RAW_PACKET_SIZE 1024
36
37 typedef struct EVCDemuxContext {
38 const AVClass *class;
39 AVRational framerate;
40
41 AVBSFContext *bsf;
42
43 } EVCDemuxContext;
44
45 #define DEC AV_OPT_FLAG_DECODING_PARAM
46 #define OFFSET(x) offsetof(EVCDemuxContext, x)
47 static const AVOption evc_options[] = {
48 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
49 { NULL },
50 };
51 #undef OFFSET
52
53 static const AVClass evc_demuxer_class = {
54 .class_name = "EVC Annex B demuxer",
55 .item_name = av_default_item_name,
56 .option = evc_options,
57 .version = LIBAVUTIL_VERSION_INT,
58 };
59
60 7467 static int annexb_probe(const AVProbeData *p)
61 {
62 int nalu_type;
63 size_t nalu_size;
64 7467 int got_sps = 0, got_pps = 0, got_idr = 0, got_nonidr = 0;
65 7467 const unsigned char *bits = p->buf;
66 7467 int bytes_to_read = p->buf_size;
67
68
1/2
✓ Branch 0 taken 8874 times.
✗ Branch 1 not taken.
8874 while (bytes_to_read > EVC_NALU_LENGTH_PREFIX_SIZE) {
69
70 8874 nalu_size = evc_read_nal_unit_length(bits, EVC_NALU_LENGTH_PREFIX_SIZE);
71
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 8822 times.
8874 if (nalu_size == 0) break;
72
73 8822 bits += EVC_NALU_LENGTH_PREFIX_SIZE;
74 8822 bytes_to_read -= EVC_NALU_LENGTH_PREFIX_SIZE;
75
76
2/2
✓ Branch 0 taken 7415 times.
✓ Branch 1 taken 1407 times.
8822 if(bytes_to_read < nalu_size) break;
77
78 1407 nalu_type = evc_get_nalu_type(bits, bytes_to_read);
79
80
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1404 times.
1407 if (nalu_type == EVC_SPS_NUT)
81 3 got_sps++;
82
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1401 times.
1404 else if (nalu_type == EVC_PPS_NUT)
83 3 got_pps++;
84
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1400 times.
1401 else if (nalu_type == EVC_IDR_NUT )
85 1 got_idr++;
86
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 1338 times.
1400 else if (nalu_type == EVC_NOIDR_NUT)
87 62 got_nonidr++;
88
89 1407 bits += nalu_size;
90 1407 bytes_to_read -= nalu_size;
91 }
92
93
6/8
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7464 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
7467 if (got_sps && got_pps && (got_idr || got_nonidr > 3))
94 1 return AVPROBE_SCORE_EXTENSION + 1; // 1 more than .mpg
95
96 7466 return 0;
97 }
98
99 1 static int evc_read_header(AVFormatContext *s)
100 {
101 AVStream *st;
102 FFStream *sti;
103 1 const AVBitStreamFilter *filter = av_bsf_get_by_name("evc_frame_merge");
104 1 EVCDemuxContext *c = s->priv_data;
105 1 int ret = 0;
106
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!filter)
108 return AVERROR_BUG;
109
110 1 st = avformat_new_stream(s, NULL);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st) {
112 ret = AVERROR(ENOMEM);
113 goto fail;
114 }
115 1 sti = ffstream(st);
116
117 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
118 1 st->codecpar->codec_id = AV_CODEC_ID_EVC;
119
120 // This causes sending to the parser full frames, not chunks of data
121 // The flag PARSER_FLAG_COMPLETE_FRAMES will be set in demux.c (demux.c: 1316)
122 1 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
123
124 1 st->avg_frame_rate = c->framerate;
125
126 // taken from rawvideo demuxers
127 1 avpriv_set_pts_info(st, 64, 1, 1200000);
128
129 1 ret = av_bsf_alloc(filter, &c->bsf);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
131 return ret;
132
133 1 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
135 return ret;
136
137 1 ret = av_bsf_init(c->bsf);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
139 return ret;
140
141 1 fail:
142 1 return ret;
143 }
144
145 301 static int evc_read_packet(AVFormatContext *s, AVPacket *pkt)
146 {
147 int ret;
148 uint32_t nalu_size;
149 301 int au_end_found = 0;
150 301 EVCDemuxContext *const c = s->priv_data;
151
152
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 300 times.
604 while(!au_end_found) {
153 uint8_t buf[EVC_NALU_LENGTH_PREFIX_SIZE];
154
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
304 if (avio_feof(s->pb))
156 goto end;
157
158 304 ret = ffio_ensure_seekback(s->pb, EVC_NALU_LENGTH_PREFIX_SIZE);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (ret < 0)
160 1 return ret;
161
162 304 ret = avio_read(s->pb, buf, EVC_NALU_LENGTH_PREFIX_SIZE);
163
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 303 times.
304 if (ret < 0)
164 1 return ret;
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 if (ret != EVC_NALU_LENGTH_PREFIX_SIZE)
166 return AVERROR_INVALIDDATA;
167
168 303 nalu_size = evc_read_nal_unit_length(buf, EVC_NALU_LENGTH_PREFIX_SIZE);
169
2/4
✓ Branch 0 taken 303 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 303 times.
303 if (!nalu_size || nalu_size > INT_MAX)
170 return AVERROR_INVALIDDATA;
171
172 303 avio_seek(s->pb, -EVC_NALU_LENGTH_PREFIX_SIZE, SEEK_CUR);
173
174 303 ret = av_get_packet(s->pb, pkt, nalu_size + EVC_NALU_LENGTH_PREFIX_SIZE);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 if (ret < 0)
176 return ret;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 if (ret != (nalu_size + EVC_NALU_LENGTH_PREFIX_SIZE))
178 return AVERROR_INVALIDDATA;
179
180 303 end:
181 303 ret = av_bsf_send_packet(c->bsf, pkt);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 if (ret < 0) {
183 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
184 "evc_frame_merge filter\n");
185 return ret;
186 }
187
188 303 ret = av_bsf_receive_packet(c->bsf, pkt);
189
3/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
303 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
190 av_log(s, AV_LOG_ERROR, "evc_frame_merge filter failed to "
191 "send output packet\n");
192
193
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 3 times.
303 if (ret != AVERROR(EAGAIN))
194 300 au_end_found = 1;
195 }
196
197 300 return ret;
198 }
199
200 1 static int evc_read_close(AVFormatContext *s)
201 {
202 1 EVCDemuxContext *const c = s->priv_data;
203
204 1 av_bsf_free(&c->bsf);
205 1 return 0;
206 }
207
208 const FFInputFormat ff_evc_demuxer = {
209 .p.name = "evc",
210 .p.long_name = NULL_IF_CONFIG_SMALL("EVC Annex B"),
211 .p.extensions = "evc",
212 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
213 .p.priv_class = &evc_demuxer_class,
214 .read_probe = annexb_probe,
215 .read_header = evc_read_header, // annexb_read_header
216 .read_packet = evc_read_packet, // annexb_read_packet
217 .read_close = evc_read_close,
218 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
219 .raw_codec_id = AV_CODEC_ID_EVC,
220 .priv_data_size = sizeof(EVCDemuxContext),
221 };
222