au.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * AU muxer and demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "raw.h"
  30. #include "riff.h"
  31. /* if we don't know the size in advance */
  32. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  33. /* The ffmpeg codecs we support, and the IDs they have in the file */
  34. static const AVCodecTag codec_au_tags[] = {
  35. { CODEC_ID_PCM_MULAW, 1 },
  36. { CODEC_ID_PCM_S8, 2 },
  37. { CODEC_ID_PCM_S16BE, 3 },
  38. { CODEC_ID_PCM_S24BE, 4 },
  39. { CODEC_ID_PCM_S32BE, 5 },
  40. { CODEC_ID_PCM_F32BE, 6 },
  41. { CODEC_ID_PCM_F64BE, 7 },
  42. { CODEC_ID_PCM_ALAW, 27 },
  43. { 0, 0 },
  44. };
  45. #if CONFIG_AU_MUXER
  46. /* AUDIO_FILE header */
  47. static int put_au_header(ByteIOContext *pb, AVCodecContext *enc)
  48. {
  49. if(!enc->codec_tag)
  50. return -1;
  51. put_tag(pb, ".snd"); /* magic number */
  52. put_be32(pb, 24); /* header size */
  53. put_be32(pb, AU_UNKNOWN_SIZE); /* data size */
  54. put_be32(pb, (uint32_t)enc->codec_tag); /* codec ID */
  55. put_be32(pb, enc->sample_rate);
  56. put_be32(pb, (uint32_t)enc->channels);
  57. return 0;
  58. }
  59. static int au_write_header(AVFormatContext *s)
  60. {
  61. ByteIOContext *pb = s->pb;
  62. s->priv_data = NULL;
  63. /* format header */
  64. if (put_au_header(pb, s->streams[0]->codec) < 0) {
  65. return -1;
  66. }
  67. put_flush_packet(pb);
  68. return 0;
  69. }
  70. static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
  71. {
  72. ByteIOContext *pb = s->pb;
  73. put_buffer(pb, pkt->data, pkt->size);
  74. return 0;
  75. }
  76. static int au_write_trailer(AVFormatContext *s)
  77. {
  78. ByteIOContext *pb = s->pb;
  79. int64_t file_size;
  80. if (!url_is_streamed(s->pb)) {
  81. /* update file size */
  82. file_size = url_ftell(pb);
  83. url_fseek(pb, 8, SEEK_SET);
  84. put_be32(pb, (uint32_t)(file_size - 24));
  85. url_fseek(pb, file_size, SEEK_SET);
  86. put_flush_packet(pb);
  87. }
  88. return 0;
  89. }
  90. #endif /* CONFIG_AU_MUXER */
  91. static int au_probe(AVProbeData *p)
  92. {
  93. /* check file header */
  94. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  95. p->buf[2] == 'n' && p->buf[3] == 'd')
  96. return AVPROBE_SCORE_MAX;
  97. else
  98. return 0;
  99. }
  100. /* au input */
  101. static int au_read_header(AVFormatContext *s,
  102. AVFormatParameters *ap)
  103. {
  104. int size;
  105. unsigned int tag;
  106. ByteIOContext *pb = s->pb;
  107. unsigned int id, channels, rate;
  108. enum CodecID codec;
  109. AVStream *st;
  110. /* check ".snd" header */
  111. tag = get_le32(pb);
  112. if (tag != MKTAG('.', 's', 'n', 'd'))
  113. return -1;
  114. size = get_be32(pb); /* header size */
  115. get_be32(pb); /* data size */
  116. id = get_be32(pb);
  117. rate = get_be32(pb);
  118. channels = get_be32(pb);
  119. codec = codec_get_id(codec_au_tags, id);
  120. if (size >= 24) {
  121. /* skip unused data */
  122. url_fseek(pb, size - 24, SEEK_CUR);
  123. }
  124. /* now we are ready: build format streams */
  125. st = av_new_stream(s, 0);
  126. if (!st)
  127. return -1;
  128. st->codec->codec_type = CODEC_TYPE_AUDIO;
  129. st->codec->codec_tag = id;
  130. st->codec->codec_id = codec;
  131. st->codec->channels = channels;
  132. st->codec->sample_rate = rate;
  133. av_set_pts_info(st, 64, 1, rate);
  134. return 0;
  135. }
  136. #define MAX_SIZE 4096
  137. static int au_read_packet(AVFormatContext *s,
  138. AVPacket *pkt)
  139. {
  140. int ret;
  141. if (url_feof(s->pb))
  142. return AVERROR(EIO);
  143. ret= av_get_packet(s->pb, pkt, MAX_SIZE);
  144. if (ret < 0)
  145. return AVERROR(EIO);
  146. pkt->stream_index = 0;
  147. /* note: we need to modify the packet size here to handle the last
  148. packet */
  149. pkt->size = ret;
  150. return 0;
  151. }
  152. #if CONFIG_AU_DEMUXER
  153. AVInputFormat au_demuxer = {
  154. "au",
  155. NULL_IF_CONFIG_SMALL("SUN AU format"),
  156. 0,
  157. au_probe,
  158. au_read_header,
  159. au_read_packet,
  160. NULL,
  161. pcm_read_seek,
  162. .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
  163. };
  164. #endif
  165. #if CONFIG_AU_MUXER
  166. AVOutputFormat au_muxer = {
  167. "au",
  168. NULL_IF_CONFIG_SMALL("SUN AU format"),
  169. "audio/basic",
  170. "au",
  171. 0,
  172. CODEC_ID_PCM_S16BE,
  173. CODEC_ID_NONE,
  174. au_write_header,
  175. au_write_packet,
  176. au_write_trailer,
  177. .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
  178. };
  179. #endif //CONFIG_AU_MUXER