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.

210 lines
4.9KB

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