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.

221 lines
5.8KB

  1. /*
  2. * AU muxer and demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  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. /*
  22. * First version by Francois Revol revol@free.fr
  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. /* The libavcodec codecs we support, and the IDs they have in the file */
  33. static const AVCodecTag codec_au_tags[] = {
  34. { AV_CODEC_ID_PCM_MULAW, 1 },
  35. { AV_CODEC_ID_PCM_S8, 2 },
  36. { AV_CODEC_ID_PCM_S16BE, 3 },
  37. { AV_CODEC_ID_PCM_S24BE, 4 },
  38. { AV_CODEC_ID_PCM_S32BE, 5 },
  39. { AV_CODEC_ID_PCM_F32BE, 6 },
  40. { AV_CODEC_ID_PCM_F64BE, 7 },
  41. { AV_CODEC_ID_PCM_ALAW, 27 },
  42. { AV_CODEC_ID_NONE, 0 },
  43. };
  44. #if CONFIG_AU_DEMUXER
  45. static int au_probe(AVProbeData *p)
  46. {
  47. /* check file header */
  48. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  49. p->buf[2] == 'n' && p->buf[3] == 'd')
  50. return AVPROBE_SCORE_MAX;
  51. else
  52. return 0;
  53. }
  54. #define BLOCK_SIZE 1024
  55. /* au input */
  56. static int au_read_header(AVFormatContext *s)
  57. {
  58. int size;
  59. unsigned int tag;
  60. AVIOContext *pb = s->pb;
  61. unsigned int id, channels, rate;
  62. int bps;
  63. enum AVCodecID codec;
  64. AVStream *st;
  65. /* check ".snd" header */
  66. tag = avio_rl32(pb);
  67. if (tag != MKTAG('.', 's', 'n', 'd'))
  68. return -1;
  69. size = avio_rb32(pb); /* header size */
  70. avio_rb32(pb); /* data size */
  71. id = avio_rb32(pb);
  72. rate = avio_rb32(pb);
  73. channels = avio_rb32(pb);
  74. if (size > 24) {
  75. /* skip unused data */
  76. avio_skip(pb, size - 24);
  77. }
  78. codec = ff_codec_get_id(codec_au_tags, id);
  79. if (codec == AV_CODEC_ID_NONE) {
  80. av_log_ask_for_sample(s, "unknown or unsupported codec tag: %d\n", id);
  81. return AVERROR_PATCHWELCOME;
  82. }
  83. bps = av_get_bits_per_sample(codec);
  84. if (!bps) {
  85. av_log_ask_for_sample(s, "could not determine bits per sample\n");
  86. return AVERROR_PATCHWELCOME;
  87. }
  88. if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
  89. av_log(s, AV_LOG_ERROR, "Invalid number of channels %d\n", channels);
  90. return AVERROR_INVALIDDATA;
  91. }
  92. /* now we are ready: build format streams */
  93. st = avformat_new_stream(s, NULL);
  94. if (!st)
  95. return -1;
  96. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  97. st->codec->codec_tag = id;
  98. st->codec->codec_id = codec;
  99. st->codec->channels = channels;
  100. st->codec->sample_rate = rate;
  101. st->codec->bit_rate = channels * rate * bps;
  102. st->codec->block_align = channels * bps >> 3;
  103. avpriv_set_pts_info(st, 64, 1, rate);
  104. return 0;
  105. }
  106. static int au_read_packet(AVFormatContext *s,
  107. AVPacket *pkt)
  108. {
  109. int ret;
  110. ret = av_get_packet(s->pb, pkt, BLOCK_SIZE *
  111. s->streams[0]->codec->block_align);
  112. if (ret < 0)
  113. return ret;
  114. pkt->stream_index = 0;
  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. /* if we don't know the size in advance */
  129. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  130. /* AUDIO_FILE header */
  131. static int put_au_header(AVIOContext *pb, AVCodecContext *enc)
  132. {
  133. if(!enc->codec_tag)
  134. return -1;
  135. ffio_wfourcc(pb, ".snd"); /* magic number */
  136. avio_wb32(pb, 24); /* header size */
  137. avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
  138. avio_wb32(pb, (uint32_t)enc->codec_tag); /* codec ID */
  139. avio_wb32(pb, enc->sample_rate);
  140. avio_wb32(pb, (uint32_t)enc->channels);
  141. return 0;
  142. }
  143. static int au_write_header(AVFormatContext *s)
  144. {
  145. AVIOContext *pb = s->pb;
  146. s->priv_data = NULL;
  147. /* format header */
  148. if (put_au_header(pb, s->streams[0]->codec) < 0) {
  149. return -1;
  150. }
  151. avio_flush(pb);
  152. return 0;
  153. }
  154. static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
  155. {
  156. AVIOContext *pb = s->pb;
  157. avio_write(pb, pkt->data, pkt->size);
  158. return 0;
  159. }
  160. static int au_write_trailer(AVFormatContext *s)
  161. {
  162. AVIOContext *pb = s->pb;
  163. int64_t file_size;
  164. if (s->pb->seekable) {
  165. /* update file size */
  166. file_size = avio_tell(pb);
  167. avio_seek(pb, 8, SEEK_SET);
  168. avio_wb32(pb, (uint32_t)(file_size - 24));
  169. avio_seek(pb, file_size, SEEK_SET);
  170. avio_flush(pb);
  171. }
  172. return 0;
  173. }
  174. AVOutputFormat ff_au_muxer = {
  175. .name = "au",
  176. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  177. .mime_type = "audio/basic",
  178. .extensions = "au",
  179. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  180. .video_codec = AV_CODEC_ID_NONE,
  181. .write_header = au_write_header,
  182. .write_packet = au_write_packet,
  183. .write_trailer = au_write_trailer,
  184. .codec_tag = (const AVCodecTag* const []){ codec_au_tags, 0 },
  185. };
  186. #endif /* CONFIG_AU_MUXER */