FFmpeg
libgme.c
Go to the documentation of this file.
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 /**
20 * @file
21 * libgme demuxer
22 */
23 
24 #include <gme/gme.h>
25 #include "libavutil/avstring.h"
26 #include "libavutil/eval.h"
27 #include "libavutil/opt.h"
28 #include "avformat.h"
29 #include "demux.h"
30 #include "internal.h"
31 
32 typedef struct GMEContext {
33  const AVClass *class;
34  Music_Emu *music_emu;
35 
36  /* options */
40 } GMEContext;
41 
42 #define OFFSET(x) offsetof(GMEContext, x)
43 #define A AV_OPT_FLAG_AUDIO_PARAM
44 #define D AV_OPT_FLAG_DECODING_PARAM
45 static const AVOption options[] = {
46  {"track_index", "set track that should be played", OFFSET(track_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, A|D},
47  {"sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 44100}, 1000, 999999, A|D},
48  {"max_size", "set max file size supported (in bytes)", OFFSET(max_size), AV_OPT_TYPE_INT64, {.i64 = 50 * 1024 * 1024}, 0, SIZE_MAX, A|D},
49  {NULL}
50 };
51 
52 static void add_meta(AVFormatContext *s, const char *name, const char *value)
53 {
54  if (value && value[0])
55  av_dict_set(&s->metadata, name, value, 0);
56 }
57 
59 {
60  GMEContext *gme = s->priv_data;
61  gme_info_t *info = NULL;
62  char buf[30];
63 
64  if (gme_track_info(gme->music_emu, &info, gme->track_index))
66 
67  *duration = info->length;
68  add_meta(s, "system", info->system);
69  add_meta(s, "game", info->game);
70  add_meta(s, "song", info->song);
71  add_meta(s, "author", info->author);
72  add_meta(s, "copyright", info->copyright);
73  add_meta(s, "comment", info->comment);
74  add_meta(s, "dumper", info->dumper);
75 
76  snprintf(buf, sizeof(buf), "%d", (int)gme_track_count(gme->music_emu));
77  add_meta(s, "tracks", buf);
78  gme_free_info(info);
79 
80  return 0;
81 }
82 
83 #define AUDIO_PKT_SIZE 512
84 
86 {
87  GMEContext *gme = s->priv_data;
88  if (gme->music_emu)
89  gme_delete(gme->music_emu);
90  return 0;
91 }
92 
94 {
95  AVStream *st;
96  AVIOContext *pb = s->pb;
97  GMEContext *gme = s->priv_data;
98  int64_t sz = avio_size(pb);
100  char *buf;
101  char dummy;
102  int ret;
103 
104  if (sz < 0) {
105  av_log(s, AV_LOG_WARNING, "Could not determine file size\n");
106  sz = gme->max_size;
107  } else if (gme->max_size && sz > gme->max_size) {
108  sz = gme->max_size;
109  }
110 
111  buf = av_malloc(sz);
112  if (!buf)
113  return AVERROR(ENOMEM);
114  sz = avio_read(pb, buf, sz);
115 
116  // Data left means our buffer (the max_size option) is too small
117  if (avio_read(pb, &dummy, 1) == 1) {
118  av_log(s, AV_LOG_ERROR, "File size is larger than max_size option "
119  "value %"PRIi64", consider increasing the max_size option\n",
120  gme->max_size);
121  av_freep(&buf);
123  }
124 
125  if (gme_open_data(buf, sz, &gme->music_emu, gme->sample_rate)) {
126  gme->music_emu = NULL; /* Just for safety */
127  av_freep(&buf);
128  return AVERROR_INVALIDDATA;
129  }
130  av_freep(&buf);
131 
133  if (ret < 0)
134  return ret;
135  if (gme_start_track(gme->music_emu, gme->track_index))
136  return AVERROR_UNKNOWN;
137 
138  st = avformat_new_stream(s, NULL);
139  if (!st)
140  return AVERROR(ENOMEM);
141  avpriv_set_pts_info(st, 64, 1, 1000);
142  if (duration > 0)
143  st->duration = duration;
146  st->codecpar->ch_layout.nb_channels = 2;
147  st->codecpar->sample_rate = gme->sample_rate;
148 
149  return 0;
150 }
151 
153 {
154  GMEContext *gme = s->priv_data;
155  int n_samples = AUDIO_PKT_SIZE / 2;
156  int ret;
157 
158  if (gme_track_ended(gme->music_emu))
159  return AVERROR_EOF;
160 
161  if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
162  return ret;
163 
164  if (gme_play(gme->music_emu, n_samples, (short *)pkt->data))
165  return AVERROR_EXTERNAL;
166 
167  return 0;
168 }
169 
170 static int read_seek_gme(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
171 {
172  GMEContext *gme = s->priv_data;
173  if (!gme_seek(gme->music_emu, (int)ts))
174  return AVERROR_EXTERNAL;
175  return 0;
176 }
177 
178 static int probe_gme(const AVProbeData *p)
179 {
180  // Reads 4 bytes - returns "" if unknown format.
181  if (gme_identify_header(p->buf)[0]) {
182  if (p->buf_size < 16384)
183  return AVPROBE_SCORE_MAX / 4 ;
184  else
185  return AVPROBE_SCORE_MAX / 2;
186  }
187  return 0;
188 }
189 
190 static const AVClass class_gme = {
191  .class_name = "Game Music Emu demuxer",
192  .item_name = av_default_item_name,
193  .option = options,
194  .version = LIBAVUTIL_VERSION_INT,
195 };
196 
198  .p.name = "libgme",
199  .p.long_name = NULL_IF_CONFIG_SMALL("Game Music Emu demuxer"),
200  .p.priv_class = &class_gme,
201  .priv_data_size = sizeof(GMEContext),
202  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
208 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:328
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
read_packet_gme
static int read_packet_gme(AVFormatContext *s, AVPacket *pkt)
Definition: libgme.c:152
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
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
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
add_meta
static void add_meta(AVFormatContext *s, const char *name, const char *value)
Definition: libgme.c:52
int64_t
long long int64_t
Definition: coverity.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:522
read_header_gme
static int read_header_gme(AVFormatContext *s)
Definition: libgme.c:93
AVOption
AVOption.
Definition: opt.h:346
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
track_index
static int track_index(VividasDemuxContext *viv, AVFormatContext *s, const uint8_t *buf, unsigned size)
Definition: vividas.c:437
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:322
sample_rate
sample_rate
Definition: ffmpeg_filter.c:409
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
GMEContext::sample_rate
int sample_rate
Definition: libgme.c:38
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
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:329
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
dummy
int dummy
Definition: motion.c:66
GMEContext
Definition: libgme.c:32
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
load_metadata
static int load_metadata(AVFormatContext *s, int64_t *duration)
Definition: libgme.c:58
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
D
#define D
Definition: libgme.c:44
duration
int64_t duration
Definition: movenc.c:64
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
OFFSET
#define OFFSET(x)
Definition: libgme.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_NE
#define AV_NE(be, le)
Definition: macros.h:33
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:98
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
info
MIPS optimizations info
Definition: mips.txt:2
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:236
AUDIO_PKT_SIZE
#define AUDIO_PKT_SIZE
Definition: libgme.c:83
GMEContext::track_index
int track_index
Definition: libgme.c:37
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
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
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
options
static const AVOption options[]
Definition: libgme.c:45
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
GMEContext::music_emu
Music_Emu * music_emu
Definition: libgme.c:34
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
ff_libgme_demuxer
const FFInputFormat ff_libgme_demuxer
Definition: libgme.c:197
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
class_gme
static const AVClass class_gme
Definition: libgme.c:190
eval.h
read_close_gme
static int read_close_gme(AVFormatContext *s)
Definition: libgme.c:85
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
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:106
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
GMEContext::max_size
int64_t max_size
Definition: libgme.c:39
demux.h
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
avformat.h
AVERROR_STREAM_NOT_FOUND
#define AVERROR_STREAM_NOT_FOUND
Stream not found.
Definition: error.h:67
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
probe_gme
static int probe_gme(const AVProbeData *p)
Definition: libgme.c:178
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
read_seek_gme
static int read_seek_gme(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
Definition: libgme.c:170
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:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
FFInputFormat
Definition: demux.h:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
avstring.h
A
#define A
Definition: libgme.c:43
snprintf
#define snprintf
Definition: snprintf.h:34