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.

222 lines
5.8KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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. static const AVCodecTag codec_au_tags[] = {
  33. { AV_CODEC_ID_PCM_MULAW, 1 },
  34. { AV_CODEC_ID_PCM_S8, 2 },
  35. { AV_CODEC_ID_PCM_S16BE, 3 },
  36. { AV_CODEC_ID_PCM_S24BE, 4 },
  37. { AV_CODEC_ID_PCM_S32BE, 5 },
  38. { AV_CODEC_ID_PCM_F32BE, 6 },
  39. { AV_CODEC_ID_PCM_F64BE, 7 },
  40. { AV_CODEC_ID_PCM_ALAW, 27 },
  41. { AV_CODEC_ID_NONE, 0 },
  42. };
  43. #if CONFIG_AU_DEMUXER
  44. static int au_probe(AVProbeData *p)
  45. {
  46. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  47. p->buf[2] == 'n' && p->buf[3] == 'd')
  48. return AVPROBE_SCORE_MAX;
  49. else
  50. return 0;
  51. }
  52. #define BLOCK_SIZE 1024
  53. static int au_read_header(AVFormatContext *s)
  54. {
  55. int size;
  56. unsigned int tag;
  57. AVIOContext *pb = s->pb;
  58. unsigned int id, channels, rate;
  59. int bps;
  60. enum AVCodecID codec;
  61. AVStream *st;
  62. tag = avio_rl32(pb);
  63. if (tag != MKTAG('.', 's', 'n', 'd'))
  64. return AVERROR_INVALIDDATA;
  65. size = avio_rb32(pb); /* header size */
  66. avio_rb32(pb); /* data size */
  67. id = avio_rb32(pb);
  68. rate = avio_rb32(pb);
  69. channels = avio_rb32(pb);
  70. if (size > 24) {
  71. /* skip unused data */
  72. avio_skip(pb, size - 24);
  73. }
  74. codec = ff_codec_get_id(codec_au_tags, id);
  75. if (codec == AV_CODEC_ID_NONE) {
  76. avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
  77. return AVERROR_PATCHWELCOME;
  78. }
  79. bps = av_get_bits_per_sample(codec);
  80. if (!bps) {
  81. avpriv_request_sample(s, "Unknown bits per sample");
  82. return AVERROR_PATCHWELCOME;
  83. }
  84. if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
  85. av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
  86. return AVERROR_INVALIDDATA;
  87. }
  88. if (rate == 0 || rate > INT_MAX) {
  89. av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
  90. return AVERROR_INVALIDDATA;
  91. }
  92. st = avformat_new_stream(s, NULL);
  93. if (!st)
  94. return AVERROR(ENOMEM);
  95. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  96. st->codecpar->codec_tag = id;
  97. st->codecpar->codec_id = codec;
  98. st->codecpar->channels = channels;
  99. st->codecpar->sample_rate = rate;
  100. st->codecpar->bit_rate = channels * rate * bps;
  101. st->codecpar->block_align = channels * bps >> 3;
  102. st->start_time = 0;
  103. avpriv_set_pts_info(st, 64, 1, rate);
  104. return 0;
  105. }
  106. static int au_read_packet(AVFormatContext *s, AVPacket *pkt)
  107. {
  108. int ret;
  109. ret = av_get_packet(s->pb, pkt, BLOCK_SIZE *
  110. s->streams[0]->codecpar->block_align);
  111. if (ret < 0)
  112. return ret;
  113. pkt->stream_index = 0;
  114. pkt->duration = ret / s->streams[0]->codecpar->block_align;
  115. return 0;
  116. }
  117. AVInputFormat ff_au_demuxer = {
  118. .name = "au",
  119. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  120. .read_probe = au_probe,
  121. .read_header = au_read_header,
  122. .read_packet = au_read_packet,
  123. .read_seek = ff_pcm_read_seek,
  124. .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
  125. };
  126. #endif /* CONFIG_AU_DEMUXER */
  127. #if CONFIG_AU_MUXER
  128. #include "rawenc.h"
  129. /* if we don't know the size in advance */
  130. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  131. /* AUDIO_FILE header */
  132. static int put_au_header(AVIOContext *pb, AVCodecParameters *par)
  133. {
  134. if (!par->codec_tag)
  135. return AVERROR(EINVAL);
  136. ffio_wfourcc(pb, ".snd"); /* magic number */
  137. avio_wb32(pb, 24); /* header size */
  138. avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
  139. avio_wb32(pb, par->codec_tag); /* codec ID */
  140. avio_wb32(pb, par->sample_rate);
  141. avio_wb32(pb, par->channels);
  142. return 0;
  143. }
  144. static int au_write_header(AVFormatContext *s)
  145. {
  146. AVIOContext *pb = s->pb;
  147. int ret;
  148. s->priv_data = NULL;
  149. if ((ret = put_au_header(pb, s->streams[0]->codecpar)) < 0)
  150. return ret;
  151. avio_flush(pb);
  152. return 0;
  153. }
  154. static int au_write_trailer(AVFormatContext *s)
  155. {
  156. AVIOContext *pb = s->pb;
  157. int64_t file_size;
  158. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  159. /* update file size */
  160. file_size = avio_tell(pb);
  161. avio_seek(pb, 8, SEEK_SET);
  162. avio_wb32(pb, (uint32_t)(file_size - 24));
  163. avio_seek(pb, file_size, SEEK_SET);
  164. avio_flush(pb);
  165. }
  166. return 0;
  167. }
  168. AVOutputFormat ff_au_muxer = {
  169. .name = "au",
  170. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  171. .mime_type = "audio/basic",
  172. .extensions = "au",
  173. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  174. .video_codec = AV_CODEC_ID_NONE,
  175. .write_header = au_write_header,
  176. .write_packet = ff_raw_write_packet,
  177. .write_trailer = au_write_trailer,
  178. .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
  179. .flags = AVFMT_NOTIMESTAMPS,
  180. };
  181. #endif /* CONFIG_AU_MUXER */