FFmpeg
kvag.c
Go to the documentation of this file.
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 
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 static int kvag_probe(const AVProbeData *p)
47 {
48  if (AV_RL32(p->buf) != KVAG_TAG)
49  return 0;
50 
51  return AVPROBE_SCORE_EXTENSION + 1;
52 }
53 
54 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  if (!(st = avformat_new_stream(s, NULL)))
63  return AVERROR(ENOMEM);
64 
65  if ((ret = ffio_read_size(s->pb, buf, KVAG_HEADER_SIZE)) < 0)
66  return ret;
67 
68  hdr.magic = AV_RL32(buf + 0);
69  hdr.data_size = AV_RL32(buf + 4);
70  hdr.sample_rate = AV_RL32(buf + 8);
71  hdr.stereo = AV_RL16(buf + 12);
72 
73  par = st->codecpar;
77 
78  av_channel_layout_default(&par->ch_layout, !!hdr.stereo + 1);
79  par->sample_rate = hdr.sample_rate;
80  par->bits_per_coded_sample = 4;
81  par->block_align = 1;
82  par->bit_rate = par->ch_layout.nb_channels *
83  (uint64_t)par->sample_rate *
85 
86  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
87  st->start_time = 0;
88  st->duration = hdr.data_size *
89  (8 / par->bits_per_coded_sample) /
91 
92  return 0;
93 }
94 
95 static int kvag_read_packet(AVFormatContext *s, AVPacket *pkt)
96 {
97  int ret;
98  AVCodecParameters *par = s->streams[0]->codecpar;
99 
100  if ((ret = av_get_packet(s->pb, pkt, KVAG_MAX_READ_SIZE)) < 0)
101  return ret;
102 
104  pkt->stream_index = 0;
106 
107  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 
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 static int kvag_write_init(AVFormatContext *s)
131 {
132  AVCodecParameters *par = s->streams[0]->codecpar;
133 
134  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  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  return 0;
145 }
146 
147 static int kvag_write_header(AVFormatContext *s)
148 {
149  uint8_t buf[KVAG_HEADER_SIZE];
150  AVCodecParameters *par = s->streams[0]->codecpar;
151 
152  AV_WL32(buf + 0, KVAG_TAG);
153  AV_WL32(buf + 4, 0); /* Data size, we fix this up later. */
154  AV_WL32(buf + 8, par->sample_rate);
155  AV_WL16(buf + 12, par->ch_layout.nb_channels == 2);
156 
157  avio_write(s->pb, buf, sizeof(buf));
158  return 0;
159 }
160 
161 static int kvag_write_trailer(AVFormatContext *s)
162 {
163  int64_t file_size, data_size;
164 
165  file_size = avio_tell(s->pb);
166  data_size = file_size - KVAG_HEADER_SIZE;
167  if (data_size < UINT32_MAX) {
168  avio_seek(s->pb, 4, SEEK_SET);
169  avio_wl32(s->pb, (uint32_t)data_size);
170  avio_seek(s->pb, file_size, SEEK_SET);
171  } else {
173  "Filesize %"PRId64" invalid for KVAG, output file will be broken\n",
174  file_size);
175  }
176 
177  return 0;
178 }
179 
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 |
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
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:542
KVAG_TAG
#define KVAG_TAG
Definition: kvag.c:34
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition: mux.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
ff_kvag_muxer
const FFOutputFormat ff_kvag_muxer
pts
static int64_t pts
Definition: transcode_aac.c:644
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:580
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
KVAGHeader
Definition: kvag.c:38
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
NULL
#define NULL
Definition: coverity.c:32
KVAG_MAX_READ_SIZE
#define KVAG_MAX_READ_SIZE
Definition: kvag.c:36
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
FFOutputFormat
Definition: mux.h:61
KVAGHeader::magic
uint32_t magic
Definition: kvag.c:39
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
KVAGHeader::sample_rate
uint32_t sample_rate
Definition: kvag.c:41
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:410
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:357
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:530
KVAGHeader::stereo
uint16_t stereo
Definition: kvag.c:42
rawenc.h
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:831
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
avio_internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition: mux.h:50
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
demux.h
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:104
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
ff_kvag_demuxer
const FFInputFormat ff_kvag_demuxer
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
channel_layout.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AVPacket::stream_index
int stream_index
Definition: packet.h:526
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AVCodecParameters::format
int format
Definition: codec_par.h:92
AV_CODEC_ID_ADPCM_IMA_SSI
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition: codec_id.h:410
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:501
KVAGHeader::data_size
uint32_t data_size
Definition: kvag.c:40
FFInputFormat
Definition: demux.h:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:662
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:792
KVAG_HEADER_SIZE
#define KVAG_HEADER_SIZE
Definition: kvag.c:35
mux.h