FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/evcdec.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 74 87 85.1%
Functions: 4 4 100.0%
Branches: 40 58 69.0%

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