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.

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