FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/kvag.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 57 68 83.8%
Functions: 6 7 85.7%
Branches: 11 18 61.1%

Line Branch Exec Source
1 /*
2 * Simon & Schuster Interactive VAG (de)muxer
3 *
4 * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.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 "config_components.h"
24
25 #include "libavutil/channel_layout.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "demux.h"
29 #include "internal.h"
30 #include "mux.h"
31 #include "rawenc.h"
32 #include "libavutil/intreadwrite.h"
33
34 #define KVAG_TAG MKTAG('K', 'V', 'A', 'G')
35 #define KVAG_HEADER_SIZE 14
36 #define KVAG_MAX_READ_SIZE 4096
37
38 typedef struct KVAGHeader {
39 uint32_t magic;
40 uint32_t data_size;
41 uint32_t sample_rate;
42 uint16_t stereo;
43 } KVAGHeader;
44
45 #if CONFIG_KVAG_DEMUXER
46 7125 static int kvag_probe(const AVProbeData *p)
47 {
48
2/2
✓ Branch 0 taken 7122 times.
✓ Branch 1 taken 3 times.
7125 if (AV_RL32(p->buf) != KVAG_TAG)
49 7122 return 0;
50
51 3 return AVPROBE_SCORE_EXTENSION + 1;
52 }
53
54 3 static int kvag_read_header(AVFormatContext *s)
55 {
56 int ret;
57 AVStream *st;
58 KVAGHeader hdr;
59 AVCodecParameters *par;
60 uint8_t buf[KVAG_HEADER_SIZE];
61
62
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(st = avformat_new_stream(s, NULL)))
63 return AVERROR(ENOMEM);
64
65
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ffio_read_size(s->pb, buf, KVAG_HEADER_SIZE)) < 0)
66 return ret;
67
68 3 hdr.magic = AV_RL32(buf + 0);
69 3 hdr.data_size = AV_RL32(buf + 4);
70 3 hdr.sample_rate = AV_RL32(buf + 8);
71 3 hdr.stereo = AV_RL16(buf + 12);
72
73 3 par = st->codecpar;
74 3 par->codec_type = AVMEDIA_TYPE_AUDIO;
75 3 par->codec_id = AV_CODEC_ID_ADPCM_IMA_SSI;
76 3 par->format = AV_SAMPLE_FMT_S16;
77
78
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 av_channel_layout_default(&par->ch_layout, !!hdr.stereo + 1);
79 3 par->sample_rate = hdr.sample_rate;
80 3 par->bits_per_coded_sample = 4;
81 3 par->block_align = 1;
82 3 par->bit_rate = par->ch_layout.nb_channels *
83 3 (uint64_t)par->sample_rate *
84 3 par->bits_per_coded_sample;
85
86 3 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
87 3 st->start_time = 0;
88 3 st->duration = hdr.data_size *
89 3 (8 / par->bits_per_coded_sample) /
90 3 par->ch_layout.nb_channels;
91
92 3 return 0;
93 }
94
95 130 static int kvag_read_packet(AVFormatContext *s, AVPacket *pkt)
96 {
97 int ret;
98 130 AVCodecParameters *par = s->streams[0]->codecpar;
99
100
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 127 times.
130 if ((ret = av_get_packet(s->pb, pkt, KVAG_MAX_READ_SIZE)) < 0)
101 3 return ret;
102
103 127 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
104 127 pkt->stream_index = 0;
105 127 pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->ch_layout.nb_channels;
106
107 127 return 0;
108 }
109
110 static int kvag_seek(AVFormatContext *s, int stream_index,
111 int64_t pts, int flags)
112 {
113 if (pts != 0)
114 return AVERROR(EINVAL);
115
116 return avio_seek(s->pb, KVAG_HEADER_SIZE, SEEK_SET);
117 }
118
119 const FFInputFormat ff_kvag_demuxer = {
120 .p.name = "kvag",
121 .p.long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
122 .read_probe = kvag_probe,
123 .read_header = kvag_read_header,
124 .read_packet = kvag_read_packet,
125 .read_seek = kvag_seek,
126 };
127 #endif
128
129 #if CONFIG_KVAG_MUXER
130 1 static int kvag_write_init(AVFormatContext *s)
131 {
132 1 AVCodecParameters *par = s->streams[0]->codecpar;
133
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->ch_layout.nb_channels > 2) {
135 av_log(s, AV_LOG_ERROR, "KVAG files only support up to 2 channels\n");
136 return AVERROR(EINVAL);
137 }
138
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
140 av_log(s, AV_LOG_WARNING, "Stream not seekable, unable to write output file\n");
141 return AVERROR(EINVAL);
142 }
143
144 1 return 0;
145 }
146
147 1 static int kvag_write_header(AVFormatContext *s)
148 {
149 uint8_t buf[KVAG_HEADER_SIZE];
150 1 AVCodecParameters *par = s->streams[0]->codecpar;
151
152 1 AV_WL32(buf + 0, KVAG_TAG);
153 1 AV_WL32(buf + 4, 0); /* Data size, we fix this up later. */
154 1 AV_WL32(buf + 8, par->sample_rate);
155 1 AV_WL16(buf + 12, par->ch_layout.nb_channels == 2);
156
157 1 avio_write(s->pb, buf, sizeof(buf));
158 1 return 0;
159 }
160
161 1 static int kvag_write_trailer(AVFormatContext *s)
162 {
163 int64_t file_size, data_size;
164
165 1 file_size = avio_tell(s->pb);
166 1 data_size = file_size - KVAG_HEADER_SIZE;
167
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (data_size < UINT32_MAX) {
168 1 avio_seek(s->pb, 4, SEEK_SET);
169 1 avio_wl32(s->pb, (uint32_t)data_size);
170 1 avio_seek(s->pb, file_size, SEEK_SET);
171 } else {
172 av_log(s, AV_LOG_WARNING,
173 "Filesize %"PRId64" invalid for KVAG, output file will be broken\n",
174 file_size);
175 }
176
177 1 return 0;
178 }
179
180 const FFOutputFormat ff_kvag_muxer = {
181 .p.name = "kvag",
182 .p.long_name = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
183 .p.extensions = "vag",
184 .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_SSI,
185 .p.video_codec = AV_CODEC_ID_NONE,
186 .p.subtitle_codec = AV_CODEC_ID_NONE,
187 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
188 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
189 .init = kvag_write_init,
190 .write_header = kvag_write_header,
191 .write_packet = ff_raw_write_packet,
192 .write_trailer = kvag_write_trailer
193 };
194 #endif
195