au.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "avio_internal.h"
  30. #include "pcm.h"
  31. #include "riff.h"
  32. /* if we don't know the size in advance */
  33. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  34. /* The ffmpeg codecs we support, and the IDs they have in the file */
  35. static const AVCodecTag codec_au_tags[] = {
  36. { CODEC_ID_PCM_MULAW, 1 },
  37. { CODEC_ID_PCM_S8, 2 },
  38. { CODEC_ID_PCM_S16BE, 3 },
  39. { CODEC_ID_PCM_S24BE, 4 },
  40. { CODEC_ID_PCM_S32BE, 5 },
  41. { CODEC_ID_PCM_F32BE, 6 },
  42. { CODEC_ID_PCM_F64BE, 7 },
  43. { CODEC_ID_PCM_ALAW, 27 },
  44. { CODEC_ID_NONE, 0 },
  45. };
  46. #if CONFIG_AU_MUXER
  47. /* AUDIO_FILE header */
  48. static int put_au_header(AVIOContext *pb, AVCodecContext *enc)
  49. {
  50. if(!enc->codec_tag)
  51. return -1;
  52. ffio_wfourcc(pb, ".snd"); /* magic number */
  53. avio_wb32(pb, 24); /* header size */
  54. avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
  55. avio_wb32(pb, (uint32_t)enc->codec_tag); /* codec ID */
  56. avio_wb32(pb, enc->sample_rate);
  57. avio_wb32(pb, (uint32_t)enc->channels);
  58. return 0;
  59. }
  60. static int au_write_header(AVFormatContext *s)
  61. {
  62. AVIOContext *pb = s->pb;
  63. s->priv_data = NULL;
  64. /* format header */
  65. if (put_au_header(pb, s->streams[0]->codec) < 0) {
  66. return -1;
  67. }
  68. avio_flush(pb);
  69. return 0;
  70. }
  71. static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
  72. {
  73. AVIOContext *pb = s->pb;
  74. avio_write(pb, pkt->data, pkt->size);
  75. return 0;
  76. }
  77. static int au_write_trailer(AVFormatContext *s)
  78. {
  79. AVIOContext *pb = s->pb;
  80. int64_t file_size;
  81. if (s->pb->seekable) {
  82. /* update file size */
  83. file_size = avio_tell(pb);
  84. avio_seek(pb, 8, SEEK_SET);
  85. avio_wb32(pb, (uint32_t)(file_size - 24));
  86. avio_seek(pb, file_size, SEEK_SET);
  87. avio_flush(pb);
  88. }
  89. return 0;
  90. }
  91. #endif /* CONFIG_AU_MUXER */
  92. static int au_probe(AVProbeData *p)
  93. {
  94. /* check file header */
  95. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  96. p->buf[2] == 'n' && p->buf[3] == 'd')
  97. return AVPROBE_SCORE_MAX;
  98. else
  99. return 0;
  100. }
  101. /* au input */
  102. static int au_read_header(AVFormatContext *s,
  103. AVFormatParameters *ap)
  104. {
  105. int size, bps, data_size = 0;
  106. unsigned int tag;
  107. AVIOContext *pb = s->pb;
  108. unsigned int id, channels, rate;
  109. enum CodecID codec;
  110. AVStream *st;
  111. /* check ".snd" header */
  112. tag = avio_rl32(pb);
  113. if (tag != MKTAG('.', 's', 'n', 'd'))
  114. return -1;
  115. size = avio_rb32(pb); /* header size */
  116. data_size = avio_rb32(pb); /* data size in bytes */
  117. if (data_size < 0) {
  118. av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
  119. return AVERROR_INVALIDDATA;
  120. }
  121. id = avio_rb32(pb);
  122. rate = avio_rb32(pb);
  123. channels = avio_rb32(pb);
  124. codec = ff_codec_get_id(codec_au_tags, id);
  125. if (!(bps = av_get_bits_per_sample(codec))) {
  126. av_log_ask_for_sample(s, "could not determine bits per sample\n");
  127. return AVERROR_INVALIDDATA;
  128. }
  129. if (size >= 24) {
  130. /* skip unused data */
  131. avio_skip(pb, size - 24);
  132. }
  133. /* now we are ready: build format streams */
  134. st = av_new_stream(s, 0);
  135. if (!st)
  136. return -1;
  137. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  138. st->codec->codec_tag = id;
  139. st->codec->codec_id = codec;
  140. st->codec->channels = channels;
  141. st->codec->sample_rate = rate;
  142. st->duration = (((int64_t)data_size)<<3) / (st->codec->channels * bps);
  143. av_set_pts_info(st, 64, 1, rate);
  144. return 0;
  145. }
  146. #define BLOCK_SIZE 1024
  147. static int au_read_packet(AVFormatContext *s,
  148. AVPacket *pkt)
  149. {
  150. int ret;
  151. ret= av_get_packet(s->pb, pkt, BLOCK_SIZE *
  152. s->streams[0]->codec->channels *
  153. av_get_bits_per_sample(s->streams[0]->codec->codec_id) >> 3);
  154. if (ret < 0)
  155. return ret;
  156. pkt->stream_index = 0;
  157. /* note: we need to modify the packet size here to handle the last
  158. packet */
  159. pkt->size = ret;
  160. return 0;
  161. }
  162. #if CONFIG_AU_DEMUXER
  163. AVInputFormat ff_au_demuxer = {
  164. .name = "au",
  165. .long_name = NULL_IF_CONFIG_SMALL("SUN AU format"),
  166. .read_probe = au_probe,
  167. .read_header = au_read_header,
  168. .read_packet = au_read_packet,
  169. .read_seek = pcm_read_seek,
  170. .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
  171. };
  172. #endif
  173. #if CONFIG_AU_MUXER
  174. AVOutputFormat ff_au_muxer = {
  175. .name = "au",
  176. .long_name = NULL_IF_CONFIG_SMALL("SUN AU format"),
  177. .mime_type = "audio/basic",
  178. .extensions = "au",
  179. .audio_codec = CODEC_ID_PCM_S16BE,
  180. .video_codec = CODEC_ID_NONE,
  181. .write_header = au_write_header,
  182. .write_packet = au_write_packet,
  183. .write_trailer = au_write_trailer,
  184. .codec_tag= (const AVCodecTag* const []){codec_au_tags, 0},
  185. };
  186. #endif //CONFIG_AU_MUXER