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.

227 lines
6.6KB

  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_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(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. #define BLOCK_SIZE 1024
  63. static int au_read_header(AVFormatContext *s)
  64. {
  65. int size, data_size = 0;
  66. unsigned int tag;
  67. AVIOContext *pb = s->pb;
  68. unsigned int id, channels, rate;
  69. int bps;
  70. enum AVCodecID codec;
  71. AVStream *st;
  72. tag = avio_rl32(pb);
  73. if (tag != MKTAG('.', 's', 'n', 'd'))
  74. return AVERROR_INVALIDDATA;
  75. size = avio_rb32(pb); /* header size */
  76. data_size = avio_rb32(pb); /* data size in bytes */
  77. if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
  78. av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
  79. return AVERROR_INVALIDDATA;
  80. }
  81. id = avio_rb32(pb);
  82. rate = avio_rb32(pb);
  83. channels = avio_rb32(pb);
  84. if (size > 24) {
  85. /* skip unused data */
  86. avio_skip(pb, size - 24);
  87. }
  88. codec = ff_codec_get_id(codec_au_tags, id);
  89. if (codec == AV_CODEC_ID_NONE) {
  90. avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
  91. return AVERROR_PATCHWELCOME;
  92. }
  93. bps = av_get_bits_per_sample(codec);
  94. if (codec == AV_CODEC_ID_ADPCM_G726LE) {
  95. if (id == MKBETAG('7','2','6','2')) {
  96. bps = 2;
  97. } else {
  98. const uint8_t bpcss[] = {4, 0, 3, 5};
  99. av_assert0(id >= 23 && id < 23 + 4);
  100. bps = bpcss[id - 23];
  101. }
  102. } else if (!bps) {
  103. avpriv_request_sample(s, "Unknown bits per sample");
  104. return AVERROR_PATCHWELCOME;
  105. }
  106. if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
  107. av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
  108. return AVERROR_INVALIDDATA;
  109. }
  110. if (rate == 0 || rate > INT_MAX) {
  111. av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
  112. return AVERROR_INVALIDDATA;
  113. }
  114. st = avformat_new_stream(s, NULL);
  115. if (!st)
  116. return AVERROR(ENOMEM);
  117. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  118. st->codec->codec_tag = id;
  119. st->codec->codec_id = codec;
  120. st->codec->channels = channels;
  121. st->codec->sample_rate = rate;
  122. st->codec->bits_per_coded_sample = bps;
  123. st->codec->bit_rate = channels * rate * bps;
  124. st->codec->block_align = FFMAX(bps * st->codec->channels / 8, 1);
  125. if (data_size != AU_UNKNOWN_SIZE)
  126. st->duration = (((int64_t)data_size)<<3) / (st->codec->channels * (int64_t)bps);
  127. st->start_time = 0;
  128. avpriv_set_pts_info(st, 64, 1, rate);
  129. return 0;
  130. }
  131. AVInputFormat ff_au_demuxer = {
  132. .name = "au",
  133. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  134. .read_probe = au_probe,
  135. .read_header = au_read_header,
  136. .read_packet = ff_pcm_read_packet,
  137. .read_seek = ff_pcm_read_seek,
  138. .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
  139. };
  140. #endif /* CONFIG_AU_DEMUXER */
  141. #if CONFIG_AU_MUXER
  142. #include "rawenc.h"
  143. static int au_write_header(AVFormatContext *s)
  144. {
  145. AVIOContext *pb = s->pb;
  146. AVCodecContext *enc = s->streams[0]->codec;
  147. if (s->nb_streams != 1) {
  148. av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
  149. return AVERROR(EINVAL);
  150. }
  151. enc->codec_tag = ff_codec_get_tag(codec_au_tags, enc->codec_id);
  152. if (!enc->codec_tag) {
  153. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  154. return AVERROR(EINVAL);
  155. }
  156. ffio_wfourcc(pb, ".snd"); /* magic number */
  157. avio_wb32(pb, AU_HEADER_SIZE); /* header size */
  158. avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
  159. avio_wb32(pb, enc->codec_tag); /* codec ID */
  160. avio_wb32(pb, enc->sample_rate);
  161. avio_wb32(pb, enc->channels);
  162. avio_wb64(pb, 0); /* annotation field */
  163. avio_flush(pb);
  164. return 0;
  165. }
  166. static int au_write_trailer(AVFormatContext *s)
  167. {
  168. AVIOContext *pb = s->pb;
  169. int64_t file_size = avio_tell(pb);
  170. if (s->pb->seekable && file_size < INT32_MAX) {
  171. /* update file size */
  172. avio_seek(pb, 8, SEEK_SET);
  173. avio_wb32(pb, (uint32_t)(file_size - AU_HEADER_SIZE));
  174. avio_seek(pb, file_size, SEEK_SET);
  175. avio_flush(pb);
  176. }
  177. return 0;
  178. }
  179. AVOutputFormat ff_au_muxer = {
  180. .name = "au",
  181. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  182. .mime_type = "audio/basic",
  183. .extensions = "au",
  184. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  185. .video_codec = AV_CODEC_ID_NONE,
  186. .write_header = au_write_header,
  187. .write_packet = ff_raw_write_packet,
  188. .write_trailer = au_write_trailer,
  189. .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
  190. .flags = AVFMT_NOTIMESTAMPS,
  191. };
  192. #endif /* CONFIG_AU_MUXER */