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.

219 lines
5.0KB

  1. /*
  2. * AU encoder and decoder
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. 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. 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 packet_size, n, ret;
  137. if (url_feof(&s->pb))
  138. return -EIO;
  139. packet_size = url_get_packet_size(&s->pb);
  140. n = MAX_SIZE / packet_size;
  141. if (n <= 0)
  142. return n = 1;
  143. if (av_new_packet(pkt, n * packet_size))
  144. return -EIO;
  145. pkt->stream_index = 0;
  146. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  147. if (ret < 0)
  148. av_free_packet(pkt);
  149. /* note: we need to modify the packet size here to handle the last
  150. packet */
  151. pkt->size = ret;
  152. return ret;
  153. }
  154. static int au_read_close(AVFormatContext *s)
  155. {
  156. return 0;
  157. }
  158. static AVInputFormat au_iformat = {
  159. "au",
  160. "SUN AU Format",
  161. 0,
  162. au_probe,
  163. au_read_header,
  164. au_read_packet,
  165. au_read_close,
  166. };
  167. static AVOutputFormat au_oformat = {
  168. "au",
  169. "SUN AU Format",
  170. "audio/basic",
  171. "au",
  172. 0,
  173. CODEC_ID_PCM_S16BE,
  174. CODEC_ID_NONE,
  175. au_write_header,
  176. au_write_packet,
  177. au_write_trailer,
  178. };
  179. int au_init(void)
  180. {
  181. av_register_input_format(&au_iformat);
  182. av_register_output_format(&au_oformat);
  183. return 0;
  184. }