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.

352 lines
10KB

  1. /*
  2. * AU muxer and demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * first version by Francois Revol <revol@free.fr>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * Reference documents:
  25. * http://www.opengroup.org/public/pubs/external/auformat.html
  26. * http://www.goice.co.jp/member/mo/formats/au.html
  27. */
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "avio_internal.h"
  31. #include "pcm.h"
  32. #include "libavutil/avassert.h"
  33. /* if we don't know the size in advance */
  34. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  35. /* the specification requires an annotation field of at least eight bytes */
  36. #define AU_DEFAULT_HEADER_SIZE (24+8)
  37. static const AVCodecTag codec_au_tags[] = {
  38. { AV_CODEC_ID_PCM_MULAW, 1 },
  39. { AV_CODEC_ID_PCM_S8, 2 },
  40. { AV_CODEC_ID_PCM_S16BE, 3 },
  41. { AV_CODEC_ID_PCM_S24BE, 4 },
  42. { AV_CODEC_ID_PCM_S32BE, 5 },
  43. { AV_CODEC_ID_PCM_F32BE, 6 },
  44. { AV_CODEC_ID_PCM_F64BE, 7 },
  45. { AV_CODEC_ID_ADPCM_G726LE, 23 },
  46. { AV_CODEC_ID_ADPCM_G722,24 },
  47. { AV_CODEC_ID_ADPCM_G726LE, 25 },
  48. { AV_CODEC_ID_ADPCM_G726LE, 26 },
  49. { AV_CODEC_ID_PCM_ALAW, 27 },
  50. { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
  51. { AV_CODEC_ID_NONE, 0 },
  52. };
  53. #if CONFIG_AU_DEMUXER
  54. static int au_probe(const AVProbeData *p)
  55. {
  56. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  57. p->buf[2] == 'n' && p->buf[3] == 'd')
  58. return AVPROBE_SCORE_MAX;
  59. else
  60. return 0;
  61. }
  62. static int au_read_annotation(AVFormatContext *s, int size)
  63. {
  64. static const char keys[][7] = {
  65. "title",
  66. "artist",
  67. "album",
  68. "track",
  69. "genre",
  70. };
  71. AVIOContext *pb = s->pb;
  72. enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
  73. char c;
  74. AVBPrint bprint;
  75. char * key = NULL;
  76. char * value = NULL;
  77. int ret, i;
  78. av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
  79. while (size-- > 0) {
  80. c = avio_r8(pb);
  81. switch(state) {
  82. case PARSE_KEY:
  83. if (c == '\0') {
  84. state = PARSE_FINISHED;
  85. } else if (c == '=') {
  86. ret = av_bprint_finalize(&bprint, &key);
  87. if (ret < 0)
  88. return ret;
  89. av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
  90. state = PARSE_VALUE;
  91. } else {
  92. av_bprint_chars(&bprint, c, 1);
  93. }
  94. break;
  95. case PARSE_VALUE:
  96. if (c == '\0' || c == '\n') {
  97. if (av_bprint_finalize(&bprint, &value) != 0) {
  98. av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
  99. } else {
  100. av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
  101. for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
  102. if (av_strcasecmp(keys[i], key) == 0) {
  103. av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
  104. value = NULL;
  105. break;
  106. }
  107. }
  108. }
  109. av_freep(&key);
  110. av_freep(&value);
  111. state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
  112. } else {
  113. av_bprint_chars(&bprint, c, 1);
  114. }
  115. break;
  116. case PARSE_FINISHED:
  117. break;
  118. default:
  119. /* should never happen */
  120. av_assert0(0);
  121. }
  122. }
  123. av_bprint_finalize(&bprint, NULL);
  124. av_freep(&key);
  125. return 0;
  126. }
  127. #define BLOCK_SIZE 1024
  128. static int au_read_header(AVFormatContext *s)
  129. {
  130. int size, data_size = 0;
  131. unsigned int tag;
  132. AVIOContext *pb = s->pb;
  133. unsigned int id, channels, rate;
  134. int bps, ba = 0;
  135. enum AVCodecID codec;
  136. AVStream *st;
  137. int ret;
  138. tag = avio_rl32(pb);
  139. if (tag != MKTAG('.', 's', 'n', 'd'))
  140. return AVERROR_INVALIDDATA;
  141. size = avio_rb32(pb); /* header size */
  142. data_size = avio_rb32(pb); /* data size in bytes */
  143. if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
  144. av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
  145. return AVERROR_INVALIDDATA;
  146. }
  147. id = avio_rb32(pb);
  148. rate = avio_rb32(pb);
  149. channels = avio_rb32(pb);
  150. if (size > 24) {
  151. /* parse annotation field to get metadata */
  152. ret = au_read_annotation(s, size - 24);
  153. if (ret < 0)
  154. return ret;
  155. }
  156. codec = ff_codec_get_id(codec_au_tags, id);
  157. if (codec == AV_CODEC_ID_NONE) {
  158. avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
  159. return AVERROR_PATCHWELCOME;
  160. }
  161. bps = av_get_bits_per_sample(codec);
  162. if (codec == AV_CODEC_ID_ADPCM_G726LE) {
  163. if (id == MKBETAG('7','2','6','2')) {
  164. bps = 2;
  165. } else {
  166. const uint8_t bpcss[] = {4, 0, 3, 5};
  167. av_assert0(id >= 23 && id < 23 + 4);
  168. ba = bpcss[id - 23];
  169. bps = bpcss[id - 23];
  170. }
  171. } else if (!bps) {
  172. avpriv_request_sample(s, "Unknown bits per sample");
  173. return AVERROR_PATCHWELCOME;
  174. }
  175. if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
  176. av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. if (rate == 0 || rate > INT_MAX) {
  180. av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
  181. return AVERROR_INVALIDDATA;
  182. }
  183. st = avformat_new_stream(s, NULL);
  184. if (!st)
  185. return AVERROR(ENOMEM);
  186. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  187. st->codecpar->codec_tag = id;
  188. st->codecpar->codec_id = codec;
  189. st->codecpar->channels = channels;
  190. st->codecpar->sample_rate = rate;
  191. st->codecpar->bits_per_coded_sample = bps;
  192. st->codecpar->bit_rate = channels * rate * bps;
  193. st->codecpar->block_align = ba ? ba : FFMAX(bps * st->codecpar->channels / 8, 1);
  194. if (data_size != AU_UNKNOWN_SIZE)
  195. st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
  196. st->start_time = 0;
  197. avpriv_set_pts_info(st, 64, 1, rate);
  198. return 0;
  199. }
  200. AVInputFormat ff_au_demuxer = {
  201. .name = "au",
  202. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  203. .read_probe = au_probe,
  204. .read_header = au_read_header,
  205. .read_packet = ff_pcm_read_packet,
  206. .read_seek = ff_pcm_read_seek,
  207. .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
  208. };
  209. #endif /* CONFIG_AU_DEMUXER */
  210. #if CONFIG_AU_MUXER
  211. typedef struct AUContext {
  212. uint32_t header_size;
  213. } AUContext;
  214. #include "rawenc.h"
  215. static int au_get_annotations(AVFormatContext *s, char **buffer)
  216. {
  217. static const char keys[][7] = {
  218. "Title",
  219. "Artist",
  220. "Album",
  221. "Track",
  222. "Genre",
  223. };
  224. int cnt = 0;
  225. AVDictionary *m = s->metadata;
  226. AVDictionaryEntry *t = NULL;
  227. AVBPrint bprint;
  228. av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
  229. for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
  230. t = av_dict_get(m, keys[i], NULL, 0);
  231. if (t != NULL) {
  232. if (cnt++)
  233. av_bprint_chars(&bprint, '\n', 1);
  234. av_bprintf(&bprint, "%s=%s", keys[i], t->value);
  235. }
  236. }
  237. /* pad with 0's */
  238. av_bprint_chars(&bprint, '\0', 8);
  239. return av_bprint_finalize(&bprint, buffer);
  240. }
  241. static int au_write_header(AVFormatContext *s)
  242. {
  243. int ret;
  244. AUContext *au = s->priv_data;
  245. AVIOContext *pb = s->pb;
  246. AVCodecParameters *par = s->streams[0]->codecpar;
  247. char *annotations = NULL;
  248. au->header_size = AU_DEFAULT_HEADER_SIZE;
  249. if (s->nb_streams != 1) {
  250. av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
  251. return AVERROR(EINVAL);
  252. }
  253. par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
  254. if (!par->codec_tag) {
  255. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  256. return AVERROR(EINVAL);
  257. }
  258. if (av_dict_count(s->metadata) > 0) {
  259. ret = au_get_annotations(s, &annotations);
  260. if (ret < 0)
  261. return ret;
  262. if (annotations != NULL) {
  263. au->header_size = (24 + strlen(annotations) + 8) & ~7;
  264. if (au->header_size < AU_DEFAULT_HEADER_SIZE)
  265. au->header_size = AU_DEFAULT_HEADER_SIZE;
  266. }
  267. }
  268. ffio_wfourcc(pb, ".snd"); /* magic number */
  269. avio_wb32(pb, au->header_size); /* header size */
  270. avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
  271. avio_wb32(pb, par->codec_tag); /* codec ID */
  272. avio_wb32(pb, par->sample_rate);
  273. avio_wb32(pb, par->channels);
  274. if (annotations != NULL) {
  275. avio_write(pb, annotations, au->header_size - 24);
  276. av_freep(&annotations);
  277. } else {
  278. avio_wb64(pb, 0); /* annotation field */
  279. }
  280. return 0;
  281. }
  282. static int au_write_trailer(AVFormatContext *s)
  283. {
  284. AVIOContext *pb = s->pb;
  285. AUContext *au = s->priv_data;
  286. int64_t file_size = avio_tell(pb);
  287. if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
  288. /* update file size */
  289. avio_seek(pb, 8, SEEK_SET);
  290. avio_wb32(pb, (uint32_t)(file_size - au->header_size));
  291. avio_seek(pb, file_size, SEEK_SET);
  292. }
  293. return 0;
  294. }
  295. AVOutputFormat ff_au_muxer = {
  296. .name = "au",
  297. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  298. .mime_type = "audio/basic",
  299. .extensions = "au",
  300. .priv_data_size = sizeof(AUContext),
  301. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  302. .video_codec = AV_CODEC_ID_NONE,
  303. .write_header = au_write_header,
  304. .write_packet = ff_raw_write_packet,
  305. .write_trailer = au_write_trailer,
  306. .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
  307. .flags = AVFMT_NOTIMESTAMPS,
  308. };
  309. #endif /* CONFIG_AU_MUXER */