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.

335 lines
8.4KB

  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 "avi.h"
  21. typedef struct {
  22. offset_t atrpos, atsqpos, awapos;
  23. offset_t data_size;
  24. } MMFContext;
  25. static int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
  26. static int mmf_rate_code(int rate)
  27. {
  28. int i;
  29. for(i = 0; i < 5; i++)
  30. if(mmf_rates[i] == rate)
  31. return i;
  32. return -1;
  33. }
  34. static int mmf_rate(int code)
  35. {
  36. if((code < 0) || (code > 4))
  37. return -1;
  38. return mmf_rates[code];
  39. }
  40. #ifdef CONFIG_MUXERS
  41. /* Copy of end_tag() from avienc.c, but for big-endian chunk size */
  42. static void end_tag_be(ByteIOContext *pb, offset_t start)
  43. {
  44. offset_t pos;
  45. pos = url_ftell(pb);
  46. url_fseek(pb, start - 4, SEEK_SET);
  47. put_be32(pb, (uint32_t)(pos - start));
  48. url_fseek(pb, pos, SEEK_SET);
  49. }
  50. static int mmf_write_header(AVFormatContext *s)
  51. {
  52. MMFContext *mmf = s->priv_data;
  53. ByteIOContext *pb = &s->pb;
  54. offset_t pos;
  55. int rate;
  56. rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
  57. if(rate < 0) {
  58. av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n", s->streams[0]->codec->sample_rate);
  59. return -1;
  60. }
  61. put_tag(pb, "MMMD");
  62. put_be32(pb, 0);
  63. pos = start_tag(pb, "CNTI");
  64. put_byte(pb, 0); /* class */
  65. put_byte(pb, 0); /* type */
  66. put_byte(pb, 0); /* code type */
  67. put_byte(pb, 0); /* status */
  68. put_byte(pb, 0); /* counts */
  69. put_tag(pb, "VN:libavcodec,"); /* metadata ("ST:songtitle,VN:version,...") */
  70. end_tag_be(pb, pos);
  71. put_buffer(pb, "ATR\x00", 4);
  72. put_be32(pb, 0);
  73. mmf->atrpos = url_ftell(pb);
  74. put_byte(pb, 0); /* format type */
  75. put_byte(pb, 0); /* sequence type */
  76. put_byte(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
  77. put_byte(pb, 0); /* wave base bit */
  78. put_byte(pb, 2); /* time base d */
  79. put_byte(pb, 2); /* time base g */
  80. put_tag(pb, "Atsq");
  81. put_be32(pb, 16);
  82. mmf->atsqpos = url_ftell(pb);
  83. /* Will be filled on close */
  84. put_buffer(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  85. mmf->awapos = start_tag(pb, "Awa\x01");
  86. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  87. put_flush_packet(pb);
  88. return 0;
  89. }
  90. static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
  91. {
  92. ByteIOContext *pb = &s->pb;
  93. put_buffer(pb, pkt->data, pkt->size);
  94. return 0;
  95. }
  96. /* Write a variable-length symbol */
  97. static void put_varlength(ByteIOContext *pb, int val)
  98. {
  99. if(val < 128)
  100. put_byte(pb, val);
  101. else {
  102. val -= 128;
  103. put_byte(pb, 0x80 | val >> 7);
  104. put_byte(pb, 0x7f & val);
  105. }
  106. }
  107. static int mmf_write_trailer(AVFormatContext *s)
  108. {
  109. ByteIOContext *pb = &s->pb;
  110. MMFContext *mmf = s->priv_data;
  111. offset_t pos, size;
  112. int gatetime;
  113. if (!url_is_streamed(&s->pb)) {
  114. /* Fill in length fields */
  115. end_tag_be(pb, mmf->awapos);
  116. end_tag_be(pb, mmf->atrpos);
  117. end_tag_be(pb, 8);
  118. pos = url_ftell(pb);
  119. size = pos - mmf->awapos;
  120. /* Fill Atsq chunk */
  121. url_fseek(pb, mmf->atsqpos, SEEK_SET);
  122. /* "play wav" */
  123. put_byte(pb, 0); /* start time */
  124. put_byte(pb, 1); /* (channel << 6) | wavenum */
  125. gatetime = size * 500 / s->streams[0]->codec->sample_rate;
  126. put_varlength(pb, gatetime); /* duration */
  127. /* "nop" */
  128. put_varlength(pb, gatetime); /* start time */
  129. put_buffer(pb, "\xff\x00", 2); /* nop */
  130. /* "end of sequence" */
  131. put_buffer(pb, "\x00\x00\x00\x00", 4);
  132. url_fseek(pb, pos, SEEK_SET);
  133. put_flush_packet(pb);
  134. }
  135. return 0;
  136. }
  137. #endif //CONFIG_MUXERS
  138. static int mmf_probe(AVProbeData *p)
  139. {
  140. /* check file header */
  141. if (p->buf_size <= 32)
  142. return 0;
  143. if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
  144. p->buf[2] == 'M' && p->buf[3] == 'D' &&
  145. p->buf[8] == 'C' && p->buf[9] == 'N' &&
  146. p->buf[10] == 'T' && p->buf[11] == 'I')
  147. return AVPROBE_SCORE_MAX;
  148. else
  149. return 0;
  150. }
  151. /* mmf input */
  152. static int mmf_read_header(AVFormatContext *s,
  153. AVFormatParameters *ap)
  154. {
  155. MMFContext *mmf = s->priv_data;
  156. unsigned int tag;
  157. ByteIOContext *pb = &s->pb;
  158. AVStream *st;
  159. offset_t file_size, size;
  160. int rate, params;
  161. tag = get_le32(pb);
  162. if (tag != MKTAG('M', 'M', 'M', 'D'))
  163. return -1;
  164. file_size = get_be32(pb);
  165. /* Skip some unused chunks that may or may not be present */
  166. for(;; url_fseek(pb, size, SEEK_CUR)) {
  167. tag = get_le32(pb);
  168. size = get_be32(pb);
  169. if(tag == MKTAG('C','N','T','I')) continue;
  170. if(tag == MKTAG('O','P','D','A')) continue;
  171. break;
  172. }
  173. /* Tag = "ATRx", where "x" = track number */
  174. if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
  175. av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
  176. return -1;
  177. }
  178. get_byte(pb); /* format type */
  179. get_byte(pb); /* sequence type */
  180. params = get_byte(pb); /* (channel << 7) | (format << 4) | rate */
  181. rate = mmf_rate(params & 0x0f);
  182. if(rate < 0) {
  183. av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
  184. return -1;
  185. }
  186. get_byte(pb); /* wave base bit */
  187. get_byte(pb); /* time base d */
  188. get_byte(pb); /* time base g */
  189. /* Skip some unused chunks that may or may not be present */
  190. for(;; url_fseek(pb, size, SEEK_CUR)) {
  191. tag = get_le32(pb);
  192. size = get_be32(pb);
  193. if(tag == MKTAG('A','t','s','q')) continue;
  194. if(tag == MKTAG('A','s','p','I')) continue;
  195. break;
  196. }
  197. /* Make sure it's followed by an Awa chunk, aka wave data */
  198. if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
  199. av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
  200. return -1;
  201. }
  202. mmf->data_size = size;
  203. st = av_new_stream(s, 0);
  204. if (!st)
  205. return AVERROR_NOMEM;
  206. st->codec->codec_type = CODEC_TYPE_AUDIO;
  207. st->codec->codec_id = CODEC_ID_ADPCM_YAMAHA;
  208. st->codec->sample_rate = rate;
  209. st->codec->channels = 1;
  210. st->codec->bits_per_sample = 4;
  211. st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample;
  212. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  213. return 0;
  214. }
  215. #define MAX_SIZE 4096
  216. static int mmf_read_packet(AVFormatContext *s,
  217. AVPacket *pkt)
  218. {
  219. MMFContext *mmf = s->priv_data;
  220. AVStream *st;
  221. int ret, size;
  222. if (url_feof(&s->pb))
  223. return AVERROR_IO;
  224. st = s->streams[0];
  225. size = MAX_SIZE;
  226. if(size > mmf->data_size)
  227. size = mmf->data_size;
  228. if(!size)
  229. return AVERROR_IO;
  230. if (av_new_packet(pkt, size))
  231. return AVERROR_IO;
  232. pkt->stream_index = 0;
  233. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  234. if (ret < 0)
  235. av_free_packet(pkt);
  236. mmf->data_size -= ret;
  237. pkt->size = ret;
  238. return ret;
  239. }
  240. static int mmf_read_close(AVFormatContext *s)
  241. {
  242. return 0;
  243. }
  244. static int mmf_read_seek(AVFormatContext *s,
  245. int stream_index, int64_t timestamp, int flags)
  246. {
  247. return pcm_read_seek(s, stream_index, timestamp, flags);
  248. }
  249. static AVInputFormat mmf_iformat = {
  250. "mmf",
  251. "mmf format",
  252. sizeof(MMFContext),
  253. mmf_probe,
  254. mmf_read_header,
  255. mmf_read_packet,
  256. mmf_read_close,
  257. mmf_read_seek,
  258. };
  259. #ifdef CONFIG_MUXERS
  260. static AVOutputFormat mmf_oformat = {
  261. "mmf",
  262. "mmf format",
  263. "application/vnd.smaf",
  264. "mmf",
  265. sizeof(MMFContext),
  266. CODEC_ID_ADPCM_YAMAHA,
  267. CODEC_ID_NONE,
  268. mmf_write_header,
  269. mmf_write_packet,
  270. mmf_write_trailer,
  271. };
  272. #endif //CONFIG_MUXERS
  273. int ff_mmf_init(void)
  274. {
  275. av_register_input_format(&mmf_iformat);
  276. #ifdef CONFIG_MUXERS
  277. av_register_output_format(&mmf_oformat);
  278. #endif //CONFIG_MUXERS
  279. return 0;
  280. }