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 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_UNKOWN_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_size <= 24)
  90. return 0;
  91. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  92. p->buf[2] == 'n' && p->buf[3] == 'd')
  93. return AVPROBE_SCORE_MAX;
  94. else
  95. return 0;
  96. }
  97. /* au input */
  98. static int au_read_header(AVFormatContext *s,
  99. AVFormatParameters *ap)
  100. {
  101. int size;
  102. unsigned int tag;
  103. ByteIOContext *pb = &s->pb;
  104. unsigned int id, codec, channels, rate;
  105. AVStream *st;
  106. /* check ".snd" header */
  107. tag = get_le32(pb);
  108. if (tag != MKTAG('.', 's', 'n', 'd'))
  109. return -1;
  110. size = get_be32(pb); /* header size */
  111. get_be32(pb); /* data size */
  112. id = get_be32(pb);
  113. rate = get_be32(pb);
  114. channels = get_be32(pb);
  115. codec = codec_get_id(codec_au_tags, id);
  116. if (size >= 24) {
  117. /* skip unused data */
  118. url_fseek(pb, size - 24, SEEK_CUR);
  119. }
  120. /* now we are ready: build format streams */
  121. st = av_new_stream(s, 0);
  122. if (!st)
  123. return -1;
  124. st->codec->codec_type = CODEC_TYPE_AUDIO;
  125. st->codec->codec_tag = id;
  126. st->codec->codec_id = codec;
  127. st->codec->channels = channels;
  128. st->codec->sample_rate = rate;
  129. av_set_pts_info(st, 64, 1, rate);
  130. return 0;
  131. }
  132. #define MAX_SIZE 4096
  133. static int au_read_packet(AVFormatContext *s,
  134. AVPacket *pkt)
  135. {
  136. int ret;
  137. if (url_feof(&s->pb))
  138. return AVERROR_IO;
  139. ret= av_get_packet(&s->pb, pkt, MAX_SIZE);
  140. if (ret < 0)
  141. return AVERROR_IO;
  142. pkt->stream_index = 0;
  143. /* note: we need to modify the packet size here to handle the last
  144. packet */
  145. pkt->size = ret;
  146. return 0;
  147. }
  148. static int au_read_close(AVFormatContext *s)
  149. {
  150. return 0;
  151. }
  152. #ifdef CONFIG_AU_DEMUXER
  153. AVInputFormat au_demuxer = {
  154. "au",
  155. "SUN AU Format",
  156. 0,
  157. au_probe,
  158. au_read_header,
  159. au_read_packet,
  160. au_read_close,
  161. pcm_read_seek,
  162. .codec_tag= (const AVCodecTag*[]){codec_au_tags, 0},
  163. };
  164. #endif
  165. #ifdef CONFIG_AU_MUXER
  166. AVOutputFormat au_muxer = {
  167. "au",
  168. "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*[]){codec_au_tags, 0},
  178. };
  179. #endif //CONFIG_AU_MUXER