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.

330 lines
8.5KB

  1. /*
  2. * Yamaha SMAF format
  3. * Copyright (c) 2005 Vidar Madsen
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "allformats.h"
  21. #include "riff.h"
  22. typedef struct {
  23. offset_t atrpos, atsqpos, awapos;
  24. offset_t data_size;
  25. } MMFContext;
  26. static int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
  27. static int mmf_rate_code(int rate)
  28. {
  29. int i;
  30. for(i = 0; i < 5; i++)
  31. if(mmf_rates[i] == rate)
  32. return i;
  33. return -1;
  34. }
  35. static int mmf_rate(int code)
  36. {
  37. if((code < 0) || (code > 4))
  38. return -1;
  39. return mmf_rates[code];
  40. }
  41. #ifdef CONFIG_MUXERS
  42. /* Copy of end_tag() from avienc.c, but for big-endian chunk size */
  43. static void end_tag_be(ByteIOContext *pb, offset_t start)
  44. {
  45. offset_t pos;
  46. pos = url_ftell(pb);
  47. url_fseek(pb, start - 4, SEEK_SET);
  48. put_be32(pb, (uint32_t)(pos - start));
  49. url_fseek(pb, pos, SEEK_SET);
  50. }
  51. static int mmf_write_header(AVFormatContext *s)
  52. {
  53. MMFContext *mmf = s->priv_data;
  54. ByteIOContext *pb = &s->pb;
  55. offset_t pos;
  56. int rate;
  57. rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
  58. if(rate < 0) {
  59. av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n", s->streams[0]->codec->sample_rate);
  60. return -1;
  61. }
  62. put_tag(pb, "MMMD");
  63. put_be32(pb, 0);
  64. pos = start_tag(pb, "CNTI");
  65. put_byte(pb, 0); /* class */
  66. put_byte(pb, 0); /* type */
  67. put_byte(pb, 0); /* code type */
  68. put_byte(pb, 0); /* status */
  69. put_byte(pb, 0); /* counts */
  70. put_tag(pb, "VN:libavcodec,"); /* metadata ("ST:songtitle,VN:version,...") */
  71. end_tag_be(pb, pos);
  72. put_buffer(pb, "ATR\x00", 4);
  73. put_be32(pb, 0);
  74. mmf->atrpos = url_ftell(pb);
  75. put_byte(pb, 0); /* format type */
  76. put_byte(pb, 0); /* sequence type */
  77. put_byte(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
  78. put_byte(pb, 0); /* wave base bit */
  79. put_byte(pb, 2); /* time base d */
  80. put_byte(pb, 2); /* time base g */
  81. put_tag(pb, "Atsq");
  82. put_be32(pb, 16);
  83. mmf->atsqpos = url_ftell(pb);
  84. /* Will be filled on close */
  85. put_buffer(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  86. mmf->awapos = start_tag(pb, "Awa\x01");
  87. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  88. put_flush_packet(pb);
  89. return 0;
  90. }
  91. static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
  92. {
  93. ByteIOContext *pb = &s->pb;
  94. put_buffer(pb, pkt->data, pkt->size);
  95. return 0;
  96. }
  97. /* Write a variable-length symbol */
  98. static void put_varlength(ByteIOContext *pb, int val)
  99. {
  100. if(val < 128)
  101. put_byte(pb, val);
  102. else {
  103. val -= 128;
  104. put_byte(pb, 0x80 | val >> 7);
  105. put_byte(pb, 0x7f & val);
  106. }
  107. }
  108. static int mmf_write_trailer(AVFormatContext *s)
  109. {
  110. ByteIOContext *pb = &s->pb;
  111. MMFContext *mmf = s->priv_data;
  112. offset_t pos, size;
  113. int gatetime;
  114. if (!url_is_streamed(&s->pb)) {
  115. /* Fill in length fields */
  116. end_tag_be(pb, mmf->awapos);
  117. end_tag_be(pb, mmf->atrpos);
  118. end_tag_be(pb, 8);
  119. pos = url_ftell(pb);
  120. size = pos - mmf->awapos;
  121. /* Fill Atsq chunk */
  122. url_fseek(pb, mmf->atsqpos, SEEK_SET);
  123. /* "play wav" */
  124. put_byte(pb, 0); /* start time */
  125. put_byte(pb, 1); /* (channel << 6) | wavenum */
  126. gatetime = size * 500 / s->streams[0]->codec->sample_rate;
  127. put_varlength(pb, gatetime); /* duration */
  128. /* "nop" */
  129. put_varlength(pb, gatetime); /* start time */
  130. put_buffer(pb, "\xff\x00", 2); /* nop */
  131. /* "end of sequence" */
  132. put_buffer(pb, "\x00\x00\x00\x00", 4);
  133. url_fseek(pb, pos, SEEK_SET);
  134. put_flush_packet(pb);
  135. }
  136. return 0;
  137. }
  138. #endif //CONFIG_MUXERS
  139. static int mmf_probe(AVProbeData *p)
  140. {
  141. /* check file header */
  142. if (p->buf_size <= 32)
  143. return 0;
  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. offset_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_NOMEM;
  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_sample = 4;
  216. st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_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_IO;
  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_IO;
  235. if (av_new_packet(pkt, size))
  236. return AVERROR_IO;
  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. static int mmf_read_close(AVFormatContext *s)
  246. {
  247. return 0;
  248. }
  249. static int mmf_read_seek(AVFormatContext *s,
  250. int stream_index, int64_t timestamp, int flags)
  251. {
  252. return pcm_read_seek(s, stream_index, timestamp, flags);
  253. }
  254. #ifdef CONFIG_MMF_DEMUXER
  255. AVInputFormat mmf_demuxer = {
  256. "mmf",
  257. "mmf format",
  258. sizeof(MMFContext),
  259. mmf_probe,
  260. mmf_read_header,
  261. mmf_read_packet,
  262. mmf_read_close,
  263. mmf_read_seek,
  264. };
  265. #endif
  266. #ifdef CONFIG_MMF_MUXER
  267. AVOutputFormat mmf_muxer = {
  268. "mmf",
  269. "mmf format",
  270. "application/vnd.smaf",
  271. "mmf",
  272. sizeof(MMFContext),
  273. CODEC_ID_ADPCM_YAMAHA,
  274. CODEC_ID_NONE,
  275. mmf_write_header,
  276. mmf_write_packet,
  277. mmf_write_trailer,
  278. };
  279. #endif