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)(~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. /* AUDIO_FILE header */
  38. static int put_au_header(ByteIOContext *pb, AVCodecContext *enc)
  39. {
  40. int tag;
  41. tag = codec_get_tag(codec_au_tags, enc->codec_id);
  42. if (tag == 0)
  43. return -1;
  44. put_tag(pb, ".snd"); /* magic number */
  45. put_be32(pb, 24); /* header size */
  46. put_be32(pb, AU_UNKOWN_SIZE); /* data size */
  47. put_be32(pb, (UINT32)tag); /* codec ID */
  48. put_be32(pb, enc->sample_rate);
  49. put_be32(pb, (UINT32)enc->channels);
  50. return 0;
  51. }
  52. static int au_write_header(AVFormatContext *s)
  53. {
  54. ByteIOContext *pb = &s->pb;
  55. s->priv_data = NULL;
  56. /* format header */
  57. if (put_au_header(pb, &s->streams[0]->codec) < 0) {
  58. return -1;
  59. }
  60. put_flush_packet(pb);
  61. return 0;
  62. }
  63. static int au_write_packet(AVFormatContext *s, int stream_index_ptr,
  64. UINT8 *buf, int size, int force_pts)
  65. {
  66. ByteIOContext *pb = &s->pb;
  67. put_buffer(pb, buf, 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)(file_size - 24));
  79. url_fseek(pb, file_size, SEEK_SET);
  80. put_flush_packet(pb);
  81. }
  82. return 0;
  83. }
  84. static int au_probe(AVProbeData *p)
  85. {
  86. /* check file header */
  87. if (p->buf_size <= 24)
  88. return 0;
  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_malloc(sizeof(AVStream));
  120. if (!st)
  121. return -1;
  122. s->nb_streams = 1;
  123. s->streams[0] = st;
  124. st->id = 0;
  125. st->codec.codec_type = CODEC_TYPE_AUDIO;
  126. st->codec.codec_tag = id;
  127. st->codec.codec_id = codec;
  128. st->codec.channels = channels;
  129. st->codec.sample_rate = 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 -EIO;
  139. if (av_new_packet(pkt, MAX_SIZE))
  140. return -EIO;
  141. pkt->stream_index = 0;
  142. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  143. if (ret < 0)
  144. av_free_packet(pkt);
  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. static AVInputFormat au_iformat = {
  155. "au",
  156. "SUN AU Format",
  157. 0,
  158. au_probe,
  159. au_read_header,
  160. au_read_packet,
  161. au_read_close,
  162. };
  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. int au_init(void)
  176. {
  177. av_register_input_format(&au_iformat);
  178. av_register_output_format(&au_oformat);
  179. return 0;
  180. }