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.

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