]> git.sesse.net Git - ffmpeg/blob - libavformat/libgme.c
avfilter/vf_identity: remove unnecessary check
[ffmpeg] / libavformat / libgme.c
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 "internal.h"
30
31 typedef struct GMEContext {
32     const AVClass *class;
33     Music_Emu *music_emu;
34
35     /* options */
36     int track_index;
37     int sample_rate;
38     int64_t max_size;
39 } GMEContext;
40
41 #define OFFSET(x) offsetof(GMEContext, x)
42 #define A AV_OPT_FLAG_AUDIO_PARAM
43 #define D AV_OPT_FLAG_DECODING_PARAM
44 static const AVOption options[] = {
45     {"track_index", "set track that should be played",        OFFSET(track_index), AV_OPT_TYPE_INT,   {.i64 = 0},                0,    INT_MAX,  A|D},
46     {"sample_rate", "set sample rate",                        OFFSET(sample_rate), AV_OPT_TYPE_INT,   {.i64 = 44100},            1000, 999999,   A|D},
47     {"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},
48     {NULL}
49 };
50
51 static void add_meta(AVFormatContext *s, const char *name, const char *value)
52 {
53     if (value && value[0])
54         av_dict_set(&s->metadata, name, value, 0);
55 }
56
57 static int load_metadata(AVFormatContext *s, int64_t *duration)
58 {
59     GMEContext *gme = s->priv_data;
60     gme_info_t *info  = NULL;
61     char buf[30];
62
63     if (gme_track_info(gme->music_emu, &info, gme->track_index))
64         return AVERROR_STREAM_NOT_FOUND;
65
66     *duration = info->length;
67     add_meta(s, "system",       info->system);
68     add_meta(s, "game",         info->game);
69     add_meta(s, "song",         info->song);
70     add_meta(s, "author",       info->author);
71     add_meta(s, "copyright",    info->copyright);
72     add_meta(s, "comment",      info->comment);
73     add_meta(s, "dumper",       info->dumper);
74
75     snprintf(buf, sizeof(buf), "%d", (int)gme_track_count(gme->music_emu));
76     add_meta(s, "tracks", buf);
77     gme_free_info(info);
78
79     return 0;
80 }
81
82 #define AUDIO_PKT_SIZE 512
83
84 static int read_close_gme(AVFormatContext *s)
85 {
86     GMEContext *gme = s->priv_data;
87     gme_delete(gme->music_emu);
88     return 0;
89 }
90
91 static int read_header_gme(AVFormatContext *s)
92 {
93     AVStream *st;
94     AVIOContext *pb = s->pb;
95     GMEContext *gme = s->priv_data;
96     int64_t sz = avio_size(pb);
97     int64_t duration;
98     char *buf;
99     char dummy;
100     int ret;
101
102     if (sz < 0) {
103         av_log(s, AV_LOG_WARNING, "Could not determine file size\n");
104         sz = gme->max_size;
105     } else if (gme->max_size && sz > gme->max_size) {
106         sz = gme->max_size;
107     }
108
109     buf = av_malloc(sz);
110     if (!buf)
111         return AVERROR(ENOMEM);
112     sz = avio_read(pb, buf, sz);
113
114     // Data left means our buffer (the max_size option) is too small
115     if (avio_read(pb, &dummy, 1) == 1) {
116         av_log(s, AV_LOG_ERROR, "File size is larger than max_size option "
117                "value %"PRIi64", consider increasing the max_size option\n",
118                gme->max_size);
119         av_freep(&buf);
120         return AVERROR_BUFFER_TOO_SMALL;
121     }
122
123     if (gme_open_data(buf, sz, &gme->music_emu, gme->sample_rate)) {
124         av_freep(&buf);
125         return AVERROR_INVALIDDATA;
126     }
127     av_freep(&buf);
128
129     ret = load_metadata(s, &duration);
130     if (ret < 0) {
131         read_close_gme(s);
132         return ret;
133     }
134     if (gme_start_track(gme->music_emu, gme->track_index)) {
135         read_close_gme(s);
136         return AVERROR_UNKNOWN;
137     }
138
139     st = avformat_new_stream(s, NULL);
140     if (!st) {
141         read_close_gme(s);
142         return AVERROR(ENOMEM);
143     }
144     avpriv_set_pts_info(st, 64, 1, 1000);
145     if (duration > 0)
146         st->duration = duration;
147     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
148     st->codecpar->codec_id    = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE);
149     st->codecpar->channels    = 2;
150     st->codecpar->sample_rate = gme->sample_rate;
151
152     return 0;
153 }
154
155 static int read_packet_gme(AVFormatContext *s, AVPacket *pkt)
156 {
157     GMEContext *gme = s->priv_data;
158     int n_samples = AUDIO_PKT_SIZE / 2;
159     int ret;
160
161     if (gme_track_ended(gme->music_emu))
162         return AVERROR_EOF;
163
164     if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
165         return ret;
166
167     if (gme_play(gme->music_emu, n_samples, (short *)pkt->data))
168         return AVERROR_EXTERNAL;
169
170     return 0;
171 }
172
173 static int read_seek_gme(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
174 {
175     GMEContext *gme = s->priv_data;
176     if (!gme_seek(gme->music_emu, (int)ts))
177         return AVERROR_EXTERNAL;
178     return 0;
179 }
180
181 static int probe_gme(const AVProbeData *p)
182 {
183     // Reads 4 bytes - returns "" if unknown format.
184     if (gme_identify_header(p->buf)[0]) {
185         if (p->buf_size < 16384)
186             return AVPROBE_SCORE_MAX / 4 ;
187         else
188             return AVPROBE_SCORE_MAX / 2;
189     }
190     return 0;
191 }
192
193 static const AVClass class_gme = {
194     .class_name = "Game Music Emu demuxer",
195     .item_name  = av_default_item_name,
196     .option     = options,
197     .version    = LIBAVUTIL_VERSION_INT,
198 };
199
200 const AVInputFormat ff_libgme_demuxer = {
201     .name           = "libgme",
202     .long_name      = NULL_IF_CONFIG_SMALL("Game Music Emu demuxer"),
203     .priv_data_size = sizeof(GMEContext),
204     .read_probe     = probe_gme,
205     .read_header    = read_header_gme,
206     .read_packet    = read_packet_gme,
207     .read_close     = read_close_gme,
208     .read_seek      = read_seek_gme,
209     .priv_class     = &class_gme,
210 };