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.

193 lines
4.5KB

  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. /* au input */
  85. static int au_read_header(AVFormatContext *s,
  86. AVFormatParameters *ap)
  87. {
  88. int size;
  89. unsigned int tag;
  90. ByteIOContext *pb = &s->pb;
  91. unsigned int id, codec, channels, rate;
  92. AVStream *st;
  93. /* check ".snd" header */
  94. tag = get_le32(pb);
  95. if (tag != MKTAG('.', 's', 'n', 'd'))
  96. return -1;
  97. size = get_be32(pb); /* header size */
  98. get_be32(pb); /* data size */
  99. id = get_be32(pb);
  100. rate = get_be32(pb);
  101. channels = get_be32(pb);
  102. codec = codec_get_id(codec_au_tags, id);
  103. if (size >= 24) {
  104. /* skip unused data */
  105. url_fseek(pb, size - 24, SEEK_CUR);
  106. }
  107. /* now we are ready: build format streams */
  108. st = malloc(sizeof(AVStream));
  109. if (!st)
  110. return -1;
  111. s->nb_streams = 1;
  112. s->streams[0] = st;
  113. st->id = 0;
  114. st->codec.codec_type = CODEC_TYPE_AUDIO;
  115. st->codec.codec_tag = id;
  116. st->codec.codec_id = codec;
  117. st->codec.channels = channels;
  118. st->codec.sample_rate = rate;
  119. return 0;
  120. }
  121. #define MAX_SIZE 4096
  122. static int au_read_packet(AVFormatContext *s,
  123. AVPacket *pkt)
  124. {
  125. int packet_size, n, ret;
  126. if (url_feof(&s->pb))
  127. return -EIO;
  128. packet_size = url_get_packet_size(&s->pb);
  129. n = MAX_SIZE / packet_size;
  130. if (n <= 0)
  131. return n = 1;
  132. if (av_new_packet(pkt, n * packet_size))
  133. return -EIO;
  134. pkt->stream_index = 0;
  135. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  136. if (ret < 0)
  137. av_free_packet(pkt);
  138. /* note: we need to modify the packet size here to handle the last
  139. packet */
  140. pkt->size = ret;
  141. return ret;
  142. }
  143. static int au_read_close(AVFormatContext *s)
  144. {
  145. return 0;
  146. }
  147. AVFormat au_format = {
  148. "au",
  149. "SUN AU Format",
  150. "audio/basic",
  151. "au",
  152. CODEC_ID_PCM_S16BE,
  153. CODEC_ID_NONE,
  154. au_write_header,
  155. au_write_packet,
  156. au_write_trailer,
  157. au_read_header,
  158. au_read_packet,
  159. au_read_close,
  160. };