You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.8KB

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