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.

215 lines
5.0KB

  1. /*
  2. * AU encoder and decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * First version by Francois Revol revol@free.fr
  21. *
  22. * Reference documents:
  23. * http://www.opengroup.org/public/pubs/external/auformat.html
  24. * http://www.goice.co.jp/member/mo/formats/au.html
  25. */
  26. #include "avformat.h"
  27. #include "avi.h"
  28. /* if we don't know the size in advance */
  29. #define AU_UNKOWN_SIZE ((uint32_t)(~0))
  30. /* The ffmpeg codecs we support, and the IDs they have in the file */
  31. static const CodecTag codec_au_tags[] = {
  32. { CODEC_ID_PCM_MULAW, 1 },
  33. { CODEC_ID_PCM_S16BE, 3 },
  34. { CODEC_ID_PCM_ALAW, 27 },
  35. { 0, 0 },
  36. };
  37. #ifdef CONFIG_ENCODERS
  38. /* AUDIO_FILE header */
  39. static int put_au_header(ByteIOContext *pb, AVCodecContext *enc)
  40. {
  41. if(!enc->codec_tag)
  42. enc->codec_tag = codec_get_tag(codec_au_tags, enc->codec_id);
  43. if(!enc->codec_tag)
  44. return -1;
  45. put_tag(pb, ".snd"); /* magic number */
  46. put_be32(pb, 24); /* header size */
  47. put_be32(pb, AU_UNKOWN_SIZE); /* data size */
  48. put_be32(pb, (uint32_t)enc->codec_tag); /* codec ID */
  49. put_be32(pb, enc->sample_rate);
  50. put_be32(pb, (uint32_t)enc->channels);
  51. return 0;
  52. }
  53. static int au_write_header(AVFormatContext *s)
  54. {
  55. ByteIOContext *pb = &s->pb;
  56. s->priv_data = NULL;
  57. /* format header */
  58. if (put_au_header(pb, &s->streams[0]->codec) < 0) {
  59. return -1;
  60. }
  61. put_flush_packet(pb);
  62. return 0;
  63. }
  64. static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
  65. {
  66. ByteIOContext *pb = &s->pb;
  67. put_buffer(pb, pkt->data, pkt->size);
  68. return 0;
  69. }
  70. static int au_write_trailer(AVFormatContext *s)
  71. {
  72. ByteIOContext *pb = &s->pb;
  73. offset_t file_size;
  74. if (!url_is_streamed(&s->pb)) {
  75. /* update file size */
  76. file_size = url_ftell(pb);
  77. url_fseek(pb, 8, SEEK_SET);
  78. put_be32(pb, (uint32_t)(file_size - 24));
  79. url_fseek(pb, file_size, SEEK_SET);
  80. put_flush_packet(pb);
  81. }
  82. return 0;
  83. }
  84. #endif //CONFIG_ENCODERS
  85. static int au_probe(AVProbeData *p)
  86. {
  87. /* check file header */
  88. if (p->buf_size <= 24)
  89. return 0;
  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. 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 -EIO;
  137. if (av_new_packet(pkt, MAX_SIZE))
  138. return -EIO;
  139. pkt->stream_index = 0;
  140. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  141. if (ret < 0)
  142. av_free_packet(pkt);
  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. static AVInputFormat au_iformat = {
  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. };
  162. #ifdef CONFIG_ENCODERS
  163. static AVOutputFormat au_oformat = {
  164. "au",
  165. "SUN AU Format",
  166. "audio/basic",
  167. "au",
  168. 0,
  169. CODEC_ID_PCM_S16BE,
  170. CODEC_ID_NONE,
  171. au_write_header,
  172. au_write_packet,
  173. au_write_trailer,
  174. };
  175. #endif //CONFIG_ENCODERS
  176. int au_init(void)
  177. {
  178. av_register_input_format(&au_iformat);
  179. #ifdef CONFIG_ENCODERS
  180. av_register_output_format(&au_oformat);
  181. #endif //CONFIG_ENCODERS
  182. return 0;
  183. }