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.

329 lines
8.9KB

  1. /*
  2. * Yamaha SMAF format
  3. * Copyright (c) 2005 Vidar Madsen
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/channel_layout.h"
  22. #include "avformat.h"
  23. #include "avio_internal.h"
  24. #include "internal.h"
  25. #include "pcm.h"
  26. #include "riff.h"
  27. typedef struct MMFContext {
  28. int64_t atrpos, atsqpos, awapos;
  29. int64_t data_size;
  30. } MMFContext;
  31. static const int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
  32. static int mmf_rate(int code)
  33. {
  34. if ((code < 0) || (code > 4))
  35. return -1;
  36. return mmf_rates[code];
  37. }
  38. #if CONFIG_MMF_MUXER
  39. static int mmf_rate_code(int rate)
  40. {
  41. int i;
  42. for (i = 0; i < 5; i++)
  43. if (mmf_rates[i] == rate)
  44. return i;
  45. return -1;
  46. }
  47. /* Copy of end_tag() from avienc.c, but for big-endian chunk size */
  48. static void end_tag_be(AVIOContext *pb, int64_t start)
  49. {
  50. int64_t pos;
  51. pos = avio_tell(pb);
  52. avio_seek(pb, start - 4, SEEK_SET);
  53. avio_wb32(pb, (uint32_t)(pos - start));
  54. avio_seek(pb, pos, SEEK_SET);
  55. }
  56. static int mmf_write_header(AVFormatContext *s)
  57. {
  58. MMFContext *mmf = s->priv_data;
  59. AVIOContext *pb = s->pb;
  60. int64_t pos;
  61. int rate;
  62. rate = mmf_rate_code(s->streams[0]->codecpar->sample_rate);
  63. if (rate < 0) {
  64. av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n",
  65. s->streams[0]->codecpar->sample_rate);
  66. return -1;
  67. }
  68. ffio_wfourcc(pb, "MMMD");
  69. avio_wb32(pb, 0);
  70. pos = ff_start_tag(pb, "CNTI");
  71. avio_w8(pb, 0); /* class */
  72. avio_w8(pb, 0); /* type */
  73. avio_w8(pb, 0); /* code type */
  74. avio_w8(pb, 0); /* status */
  75. avio_w8(pb, 0); /* counts */
  76. end_tag_be(pb, pos);
  77. pos = ff_start_tag(pb, "OPDA");
  78. avio_write(pb, "VN:libavcodec,", sizeof("VN:libavcodec,") -1); /* metadata ("ST:songtitle,VN:version,...") */
  79. end_tag_be(pb, pos);
  80. avio_write(pb, "ATR\x00", 4);
  81. avio_wb32(pb, 0);
  82. mmf->atrpos = avio_tell(pb);
  83. avio_w8(pb, 0); /* format type */
  84. avio_w8(pb, 0); /* sequence type */
  85. avio_w8(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
  86. avio_w8(pb, 0); /* wave base bit */
  87. avio_w8(pb, 2); /* time base d */
  88. avio_w8(pb, 2); /* time base g */
  89. ffio_wfourcc(pb, "Atsq");
  90. avio_wb32(pb, 16);
  91. mmf->atsqpos = avio_tell(pb);
  92. /* Will be filled on close */
  93. avio_write(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  94. mmf->awapos = ff_start_tag(pb, "Awa\x01");
  95. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
  96. avio_flush(pb);
  97. return 0;
  98. }
  99. static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
  100. {
  101. AVIOContext *pb = s->pb;
  102. avio_write(pb, pkt->data, pkt->size);
  103. return 0;
  104. }
  105. /* Write a variable-length symbol */
  106. static void put_varlength(AVIOContext *pb, int val)
  107. {
  108. if (val < 128)
  109. avio_w8(pb, val);
  110. else {
  111. val -= 128;
  112. avio_w8(pb, 0x80 | val >> 7);
  113. avio_w8(pb, 0x7f & val);
  114. }
  115. }
  116. static int mmf_write_trailer(AVFormatContext *s)
  117. {
  118. AVIOContext *pb = s->pb;
  119. MMFContext *mmf = s->priv_data;
  120. int64_t pos, size;
  121. int gatetime;
  122. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  123. /* Fill in length fields */
  124. end_tag_be(pb, mmf->awapos);
  125. end_tag_be(pb, mmf->atrpos);
  126. end_tag_be(pb, 8);
  127. pos = avio_tell(pb);
  128. size = pos - mmf->awapos;
  129. /* Fill Atsq chunk */
  130. avio_seek(pb, mmf->atsqpos, SEEK_SET);
  131. /* "play wav" */
  132. avio_w8(pb, 0); /* start time */
  133. avio_w8(pb, 1); /* (channel << 6) | wavenum */
  134. gatetime = size * 500 / s->streams[0]->codecpar->sample_rate;
  135. put_varlength(pb, gatetime); /* duration */
  136. /* "nop" */
  137. put_varlength(pb, gatetime); /* start time */
  138. avio_write(pb, "\xff\x00", 2); /* nop */
  139. /* "end of sequence" */
  140. avio_write(pb, "\x00\x00\x00\x00", 4);
  141. avio_seek(pb, pos, SEEK_SET);
  142. avio_flush(pb);
  143. }
  144. return 0;
  145. }
  146. #endif /* CONFIG_MMF_MUXER */
  147. static int mmf_probe(AVProbeData *p)
  148. {
  149. /* check file header */
  150. if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
  151. p->buf[2] == 'M' && p->buf[3] == 'D' &&
  152. p->buf[8] == 'C' && p->buf[9] == 'N' &&
  153. p->buf[10] == 'T' && p->buf[11] == 'I')
  154. return AVPROBE_SCORE_MAX;
  155. else
  156. return 0;
  157. }
  158. /* mmf input */
  159. static int mmf_read_header(AVFormatContext *s)
  160. {
  161. MMFContext *mmf = s->priv_data;
  162. unsigned int tag;
  163. AVIOContext *pb = s->pb;
  164. AVStream *st;
  165. int64_t size;
  166. int rate, params;
  167. tag = avio_rl32(pb);
  168. if (tag != MKTAG('M', 'M', 'M', 'D'))
  169. return -1;
  170. avio_skip(pb, 4); /* file_size */
  171. /* Skip some unused chunks that may or may not be present */
  172. for (;; avio_skip(pb, size)) {
  173. tag = avio_rl32(pb);
  174. size = avio_rb32(pb);
  175. if (tag == MKTAG('C', 'N', 'T', 'I'))
  176. continue;
  177. if (tag == MKTAG('O', 'P', 'D', 'A'))
  178. continue;
  179. break;
  180. }
  181. /* Tag = "ATRx", where "x" = track number */
  182. if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
  183. av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
  184. return -1;
  185. }
  186. if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
  187. av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
  188. return -1;
  189. }
  190. avio_r8(pb); /* format type */
  191. avio_r8(pb); /* sequence type */
  192. params = avio_r8(pb); /* (channel << 7) | (format << 4) | rate */
  193. rate = mmf_rate(params & 0x0f);
  194. if (rate < 0) {
  195. av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
  196. return -1;
  197. }
  198. avio_r8(pb); /* wave base bit */
  199. avio_r8(pb); /* time base d */
  200. avio_r8(pb); /* time base g */
  201. /* Skip some unused chunks that may or may not be present */
  202. for (;; avio_skip(pb, size)) {
  203. tag = avio_rl32(pb);
  204. size = avio_rb32(pb);
  205. if (tag == MKTAG('A', 't', 's', 'q'))
  206. continue;
  207. if (tag == MKTAG('A', 's', 'p', 'I'))
  208. continue;
  209. break;
  210. }
  211. /* Make sure it's followed by an Awa chunk, aka wave data */
  212. if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
  213. av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
  214. return -1;
  215. }
  216. mmf->data_size = size;
  217. st = avformat_new_stream(s, NULL);
  218. if (!st)
  219. return AVERROR(ENOMEM);
  220. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  221. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_YAMAHA;
  222. st->codecpar->sample_rate = rate;
  223. st->codecpar->channels = 1;
  224. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  225. st->codecpar->bits_per_coded_sample = 4;
  226. st->codecpar->bit_rate = st->codecpar->sample_rate *
  227. st->codecpar->bits_per_coded_sample;
  228. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  229. return 0;
  230. }
  231. #define MAX_SIZE 4096
  232. static int mmf_read_packet(AVFormatContext *s, AVPacket *pkt)
  233. {
  234. MMFContext *mmf = s->priv_data;
  235. int ret, size;
  236. if (s->pb->eof_reached)
  237. return AVERROR(EIO);
  238. size = MAX_SIZE;
  239. if (size > mmf->data_size)
  240. size = mmf->data_size;
  241. if (!size)
  242. return AVERROR(EIO);
  243. if (av_new_packet(pkt, size))
  244. return AVERROR(EIO);
  245. pkt->stream_index = 0;
  246. ret = avio_read(s->pb, pkt->data, pkt->size);
  247. if (ret < 0)
  248. av_packet_unref(pkt);
  249. mmf->data_size -= ret;
  250. pkt->size = ret;
  251. return ret;
  252. }
  253. #if CONFIG_MMF_DEMUXER
  254. AVInputFormat ff_mmf_demuxer = {
  255. .name = "mmf",
  256. .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
  257. .priv_data_size = sizeof(MMFContext),
  258. .read_probe = mmf_probe,
  259. .read_header = mmf_read_header,
  260. .read_packet = mmf_read_packet,
  261. .read_seek = ff_pcm_read_seek,
  262. };
  263. #endif
  264. #if CONFIG_MMF_MUXER
  265. AVOutputFormat ff_mmf_muxer = {
  266. .name = "mmf",
  267. .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
  268. .mime_type = "application/vnd.smaf",
  269. .extensions = "mmf",
  270. .priv_data_size = sizeof(MMFContext),
  271. .audio_codec = AV_CODEC_ID_ADPCM_YAMAHA,
  272. .video_codec = AV_CODEC_ID_NONE,
  273. .write_header = mmf_write_header,
  274. .write_packet = mmf_write_packet,
  275. .write_trailer = mmf_write_trailer,
  276. };
  277. #endif