mmf.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Yamaha SMAF format
  3. * Copyright (c) 2005 Vidar Madsen
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "raw.h"
  23. #include "riff.h"
  24. typedef struct {
  25. int64_t atrpos, atsqpos, awapos;
  26. int64_t data_size;
  27. } MMFContext;
  28. static const int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
  29. static int mmf_rate(int code)
  30. {
  31. if((code < 0) || (code > 4))
  32. return -1;
  33. return mmf_rates[code];
  34. }
  35. #if CONFIG_MMF_MUXER
  36. static int mmf_rate_code(int rate)
  37. {
  38. int i;
  39. for(i = 0; i < 5; i++)
  40. if(mmf_rates[i] == rate)
  41. return i;
  42. return -1;
  43. }
  44. /* Copy of end_tag() from avienc.c, but for big-endian chunk size */
  45. static void end_tag_be(ByteIOContext *pb, int64_t start)
  46. {
  47. int64_t pos;
  48. pos = url_ftell(pb);
  49. url_fseek(pb, start - 4, SEEK_SET);
  50. put_be32(pb, (uint32_t)(pos - start));
  51. url_fseek(pb, pos, SEEK_SET);
  52. }
  53. static int mmf_write_header(AVFormatContext *s)
  54. {
  55. MMFContext *mmf = s->priv_data;
  56. ByteIOContext *pb = s->pb;
  57. int64_t pos;
  58. int rate;
  59. rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
  60. if(rate < 0) {
  61. av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n", s->streams[0]->codec->sample_rate);
  62. return -1;
  63. }
  64. put_tag(pb, "MMMD");
  65. put_be32(pb, 0);
  66. pos = start_tag(pb, "CNTI");
  67. put_byte(pb, 0); /* class */
  68. put_byte(pb, 0); /* type */
  69. put_byte(pb, 0); /* code type */
  70. put_byte(pb, 0); /* status */
  71. put_byte(pb, 0); /* counts */
  72. put_tag(pb, "VN:libavcodec,"); /* metadata ("ST:songtitle,VN:version,...") */
  73. end_tag_be(pb, pos);
  74. put_buffer(pb, "ATR\x00", 4);
  75. put_be32(pb, 0);
  76. mmf->atrpos = url_ftell(pb);
  77. put_byte(pb, 0); /* format type */
  78. put_byte(pb, 0); /* sequence type */
  79. put_byte(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
  80. put_byte(pb, 0); /* wave base bit */
  81. put_byte(pb, 2); /* time base d */
  82. put_byte(pb, 2); /* time base g */
  83. put_tag(pb, "Atsq");
  84. put_be32(pb, 16);
  85. mmf->atsqpos = url_ftell(pb);
  86. /* Will be filled on close */
  87. put_buffer(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  88. mmf->awapos = start_tag(pb, "Awa\x01");
  89. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  90. put_flush_packet(pb);
  91. return 0;
  92. }
  93. static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
  94. {
  95. ByteIOContext *pb = s->pb;
  96. put_buffer(pb, pkt->data, pkt->size);
  97. return 0;
  98. }
  99. /* Write a variable-length symbol */
  100. static void put_varlength(ByteIOContext *pb, int val)
  101. {
  102. if(val < 128)
  103. put_byte(pb, val);
  104. else {
  105. val -= 128;
  106. put_byte(pb, 0x80 | val >> 7);
  107. put_byte(pb, 0x7f & val);
  108. }
  109. }
  110. static int mmf_write_trailer(AVFormatContext *s)
  111. {
  112. ByteIOContext *pb = s->pb;
  113. MMFContext *mmf = s->priv_data;
  114. int64_t pos, size;
  115. int gatetime;
  116. if (!url_is_streamed(s->pb)) {
  117. /* Fill in length fields */
  118. end_tag_be(pb, mmf->awapos);
  119. end_tag_be(pb, mmf->atrpos);
  120. end_tag_be(pb, 8);
  121. pos = url_ftell(pb);
  122. size = pos - mmf->awapos;
  123. /* Fill Atsq chunk */
  124. url_fseek(pb, mmf->atsqpos, SEEK_SET);
  125. /* "play wav" */
  126. put_byte(pb, 0); /* start time */
  127. put_byte(pb, 1); /* (channel << 6) | wavenum */
  128. gatetime = size * 500 / s->streams[0]->codec->sample_rate;
  129. put_varlength(pb, gatetime); /* duration */
  130. /* "nop" */
  131. put_varlength(pb, gatetime); /* start time */
  132. put_buffer(pb, "\xff\x00", 2); /* nop */
  133. /* "end of sequence" */
  134. put_buffer(pb, "\x00\x00\x00\x00", 4);
  135. url_fseek(pb, pos, SEEK_SET);
  136. put_flush_packet(pb);
  137. }
  138. return 0;
  139. }
  140. #endif /* CONFIG_MMF_MUXER */
  141. static int mmf_probe(AVProbeData *p)
  142. {
  143. /* check file header */
  144. if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
  145. p->buf[2] == 'M' && p->buf[3] == 'D' &&
  146. p->buf[8] == 'C' && p->buf[9] == 'N' &&
  147. p->buf[10] == 'T' && p->buf[11] == 'I')
  148. return AVPROBE_SCORE_MAX;
  149. else
  150. return 0;
  151. }
  152. /* mmf input */
  153. static int mmf_read_header(AVFormatContext *s,
  154. AVFormatParameters *ap)
  155. {
  156. MMFContext *mmf = s->priv_data;
  157. unsigned int tag;
  158. ByteIOContext *pb = s->pb;
  159. AVStream *st;
  160. int64_t file_size, size;
  161. int rate, params;
  162. tag = get_le32(pb);
  163. if (tag != MKTAG('M', 'M', 'M', 'D'))
  164. return -1;
  165. file_size = get_be32(pb);
  166. /* Skip some unused chunks that may or may not be present */
  167. for(;; url_fseek(pb, size, SEEK_CUR)) {
  168. tag = get_le32(pb);
  169. size = get_be32(pb);
  170. if(tag == MKTAG('C','N','T','I')) continue;
  171. if(tag == MKTAG('O','P','D','A')) continue;
  172. break;
  173. }
  174. /* Tag = "ATRx", where "x" = track number */
  175. if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
  176. av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
  177. return -1;
  178. }
  179. if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
  180. av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
  181. return -1;
  182. }
  183. get_byte(pb); /* format type */
  184. get_byte(pb); /* sequence type */
  185. params = get_byte(pb); /* (channel << 7) | (format << 4) | rate */
  186. rate = mmf_rate(params & 0x0f);
  187. if(rate < 0) {
  188. av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
  189. return -1;
  190. }
  191. get_byte(pb); /* wave base bit */
  192. get_byte(pb); /* time base d */
  193. get_byte(pb); /* time base g */
  194. /* Skip some unused chunks that may or may not be present */
  195. for(;; url_fseek(pb, size, SEEK_CUR)) {
  196. tag = get_le32(pb);
  197. size = get_be32(pb);
  198. if(tag == MKTAG('A','t','s','q')) continue;
  199. if(tag == MKTAG('A','s','p','I')) continue;
  200. break;
  201. }
  202. /* Make sure it's followed by an Awa chunk, aka wave data */
  203. if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
  204. av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
  205. return -1;
  206. }
  207. mmf->data_size = size;
  208. st = av_new_stream(s, 0);
  209. if (!st)
  210. return AVERROR(ENOMEM);
  211. st->codec->codec_type = CODEC_TYPE_AUDIO;
  212. st->codec->codec_id = CODEC_ID_ADPCM_YAMAHA;
  213. st->codec->sample_rate = rate;
  214. st->codec->channels = 1;
  215. st->codec->bits_per_coded_sample = 4;
  216. st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample;
  217. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  218. return 0;
  219. }
  220. #define MAX_SIZE 4096
  221. static int mmf_read_packet(AVFormatContext *s,
  222. AVPacket *pkt)
  223. {
  224. MMFContext *mmf = s->priv_data;
  225. AVStream *st;
  226. int ret, size;
  227. if (url_feof(s->pb))
  228. return AVERROR(EIO);
  229. st = s->streams[0];
  230. size = MAX_SIZE;
  231. if(size > mmf->data_size)
  232. size = mmf->data_size;
  233. if(!size)
  234. return AVERROR(EIO);
  235. if (av_new_packet(pkt, size))
  236. return AVERROR(EIO);
  237. pkt->stream_index = 0;
  238. ret = get_buffer(s->pb, pkt->data, pkt->size);
  239. if (ret < 0)
  240. av_free_packet(pkt);
  241. mmf->data_size -= ret;
  242. pkt->size = ret;
  243. return ret;
  244. }
  245. #if CONFIG_MMF_DEMUXER
  246. AVInputFormat mmf_demuxer = {
  247. "mmf",
  248. NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
  249. sizeof(MMFContext),
  250. mmf_probe,
  251. mmf_read_header,
  252. mmf_read_packet,
  253. NULL,
  254. pcm_read_seek,
  255. };
  256. #endif
  257. #if CONFIG_MMF_MUXER
  258. AVOutputFormat mmf_muxer = {
  259. "mmf",
  260. NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
  261. "application/vnd.smaf",
  262. "mmf",
  263. sizeof(MMFContext),
  264. CODEC_ID_ADPCM_YAMAHA,
  265. CODEC_ID_NONE,
  266. mmf_write_header,
  267. mmf_write_packet,
  268. mmf_write_trailer,
  269. };
  270. #endif