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.

217 lines
5.7KB

  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. #include "riff.h"
  33. /* if we don't know the size in advance */
  34. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  35. /* The libavcodec codecs we support, and the IDs they have in the file */
  36. static const AVCodecTag codec_au_tags[] = {
  37. { CODEC_ID_PCM_MULAW, 1 },
  38. { CODEC_ID_PCM_S8, 2 },
  39. { CODEC_ID_PCM_S16BE, 3 },
  40. { CODEC_ID_PCM_S24BE, 4 },
  41. { CODEC_ID_PCM_S32BE, 5 },
  42. { CODEC_ID_PCM_F32BE, 6 },
  43. { CODEC_ID_PCM_F64BE, 7 },
  44. { CODEC_ID_PCM_ALAW, 27 },
  45. { CODEC_ID_NONE, 0 },
  46. };
  47. #if CONFIG_AU_MUXER
  48. /* AUDIO_FILE header */
  49. static int put_au_header(AVIOContext *pb, AVCodecContext *enc)
  50. {
  51. if(!enc->codec_tag)
  52. return -1;
  53. ffio_wfourcc(pb, ".snd"); /* magic number */
  54. avio_wb32(pb, 24); /* header size */
  55. avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
  56. avio_wb32(pb, (uint32_t)enc->codec_tag); /* codec ID */
  57. avio_wb32(pb, enc->sample_rate);
  58. avio_wb32(pb, (uint32_t)enc->channels);
  59. return 0;
  60. }
  61. static int au_write_header(AVFormatContext *s)
  62. {
  63. AVIOContext *pb = s->pb;
  64. s->priv_data = NULL;
  65. /* format header */
  66. if (put_au_header(pb, s->streams[0]->codec) < 0) {
  67. return -1;
  68. }
  69. avio_flush(pb);
  70. return 0;
  71. }
  72. static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
  73. {
  74. AVIOContext *pb = s->pb;
  75. avio_write(pb, pkt->data, pkt->size);
  76. return 0;
  77. }
  78. static int au_write_trailer(AVFormatContext *s)
  79. {
  80. AVIOContext *pb = s->pb;
  81. int64_t file_size;
  82. if (s->pb->seekable) {
  83. /* update file size */
  84. file_size = avio_tell(pb);
  85. avio_seek(pb, 8, SEEK_SET);
  86. avio_wb32(pb, (uint32_t)(file_size - 24));
  87. avio_seek(pb, file_size, SEEK_SET);
  88. avio_flush(pb);
  89. }
  90. return 0;
  91. }
  92. #endif /* CONFIG_AU_MUXER */
  93. static int au_probe(AVProbeData *p)
  94. {
  95. /* check file header */
  96. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  97. p->buf[2] == 'n' && p->buf[3] == 'd')
  98. return AVPROBE_SCORE_MAX;
  99. else
  100. return 0;
  101. }
  102. /* au input */
  103. static int au_read_header(AVFormatContext *s)
  104. {
  105. int size;
  106. unsigned int tag;
  107. AVIOContext *pb = s->pb;
  108. unsigned int id, channels, rate;
  109. enum CodecID codec;
  110. AVStream *st;
  111. /* check ".snd" header */
  112. tag = avio_rl32(pb);
  113. if (tag != MKTAG('.', 's', 'n', 'd'))
  114. return -1;
  115. size = avio_rb32(pb); /* header size */
  116. avio_rb32(pb); /* data size */
  117. id = avio_rb32(pb);
  118. rate = avio_rb32(pb);
  119. channels = avio_rb32(pb);
  120. codec = ff_codec_get_id(codec_au_tags, id);
  121. if (!av_get_bits_per_sample(codec)) {
  122. av_log_ask_for_sample(s, "could not determine bits per sample\n");
  123. return AVERROR_INVALIDDATA;
  124. }
  125. if (channels == 0 || channels > 64) {
  126. av_log(s, AV_LOG_ERROR, "Invalid number of channels %d\n", channels);
  127. return AVERROR_INVALIDDATA;
  128. }
  129. if (size >= 24) {
  130. /* skip unused data */
  131. avio_skip(pb, size - 24);
  132. }
  133. /* now we are ready: build format streams */
  134. st = avformat_new_stream(s, NULL);
  135. if (!st)
  136. return -1;
  137. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  138. st->codec->codec_tag = id;
  139. st->codec->codec_id = codec;
  140. st->codec->channels = channels;
  141. st->codec->sample_rate = rate;
  142. avpriv_set_pts_info(st, 64, 1, rate);
  143. return 0;
  144. }
  145. #define BLOCK_SIZE 1024
  146. static int au_read_packet(AVFormatContext *s,
  147. AVPacket *pkt)
  148. {
  149. int ret;
  150. ret= av_get_packet(s->pb, pkt, BLOCK_SIZE *
  151. s->streams[0]->codec->channels *
  152. av_get_bits_per_sample(s->streams[0]->codec->codec_id) >> 3);
  153. if (ret < 0)
  154. return ret;
  155. pkt->stream_index = 0;
  156. /* note: we need to modify the packet size here to handle the last
  157. packet */
  158. pkt->size = ret;
  159. return 0;
  160. }
  161. #if CONFIG_AU_DEMUXER
  162. AVInputFormat ff_au_demuxer = {
  163. .name = "au",
  164. .long_name = NULL_IF_CONFIG_SMALL("SUN AU format"),
  165. .read_probe = au_probe,
  166. .read_header = au_read_header,
  167. .read_packet = au_read_packet,
  168. .read_seek = ff_pcm_read_seek,
  169. .codec_tag = (const AVCodecTag* const []){ codec_au_tags, 0 },
  170. };
  171. #endif
  172. #if CONFIG_AU_MUXER
  173. AVOutputFormat ff_au_muxer = {
  174. .name = "au",
  175. .long_name = NULL_IF_CONFIG_SMALL("SUN AU format"),
  176. .mime_type = "audio/basic",
  177. .extensions = "au",
  178. .audio_codec = CODEC_ID_PCM_S16BE,
  179. .video_codec = CODEC_ID_NONE,
  180. .write_header = au_write_header,
  181. .write_packet = au_write_packet,
  182. .write_trailer = au_write_trailer,
  183. .codec_tag = (const AVCodecTag* const []){ codec_au_tags, 0 },
  184. };
  185. #endif //CONFIG_AU_MUXER