au.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "internal.h"
  30. #include "avio_internal.h"
  31. #include "pcm.h"
  32. #include "riff.h"
  33. /* if we don't know the size in advance */
  34. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  35. /* The libavcodec codecs we support, and the IDs they have in the file */
  36. static const AVCodecTag codec_au_tags[] = {
  37. { AV_CODEC_ID_PCM_MULAW, 1 },
  38. { AV_CODEC_ID_PCM_S8, 2 },
  39. { AV_CODEC_ID_PCM_S16BE, 3 },
  40. { AV_CODEC_ID_PCM_S24BE, 4 },
  41. { AV_CODEC_ID_PCM_S32BE, 5 },
  42. { AV_CODEC_ID_PCM_F32BE, 6 },
  43. { AV_CODEC_ID_PCM_F64BE, 7 },
  44. { AV_CODEC_ID_PCM_ALAW, 27 },
  45. { AV_CODEC_ID_NONE, 0 },
  46. };
  47. #if CONFIG_AU_MUXER
  48. /* AUDIO_FILE header */
  49. static int put_au_header(AVIOContext *pb, AVCodecContext *enc)
  50. {
  51. if(!enc->codec_tag)
  52. return -1;
  53. ffio_wfourcc(pb, ".snd"); /* magic number */
  54. avio_wb32(pb, 24); /* header size */
  55. avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
  56. avio_wb32(pb, (uint32_t)enc->codec_tag); /* codec ID */
  57. avio_wb32(pb, enc->sample_rate);
  58. avio_wb32(pb, (uint32_t)enc->channels);
  59. return 0;
  60. }
  61. static int au_write_header(AVFormatContext *s)
  62. {
  63. AVIOContext *pb = s->pb;
  64. s->priv_data = NULL;
  65. /* format header */
  66. if (put_au_header(pb, s->streams[0]->codec) < 0) {
  67. return -1;
  68. }
  69. avio_flush(pb);
  70. return 0;
  71. }
  72. static int au_write_packet(AVFormatContext *s, AVPacket *pkt)
  73. {
  74. AVIOContext *pb = s->pb;
  75. avio_write(pb, pkt->data, pkt->size);
  76. return 0;
  77. }
  78. static int au_write_trailer(AVFormatContext *s)
  79. {
  80. AVIOContext *pb = s->pb;
  81. int64_t file_size;
  82. if (s->pb->seekable) {
  83. /* update file size */
  84. file_size = avio_tell(pb);
  85. avio_seek(pb, 8, SEEK_SET);
  86. avio_wb32(pb, (uint32_t)(file_size - 24));
  87. avio_seek(pb, file_size, SEEK_SET);
  88. avio_flush(pb);
  89. }
  90. return 0;
  91. }
  92. #endif /* CONFIG_AU_MUXER */
  93. static int au_probe(AVProbeData *p)
  94. {
  95. /* check file header */
  96. if (p->buf[0] == '.' && p->buf[1] == 's' &&
  97. p->buf[2] == 'n' && p->buf[3] == 'd')
  98. return AVPROBE_SCORE_MAX;
  99. else
  100. return 0;
  101. }
  102. /* au input */
  103. static int au_read_header(AVFormatContext *s)
  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 AVCodecID 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 && data_size != AU_UNKNOWN_SIZE) {
  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 (channels == 0 || channels > 64) {
  130. av_log(s, AV_LOG_ERROR, "Invalid number of channels %d\n", channels);
  131. return AVERROR_INVALIDDATA;
  132. }
  133. if (size >= 24) {
  134. /* skip unused data */
  135. avio_skip(pb, size - 24);
  136. }
  137. /* now we are ready: build format streams */
  138. st = avformat_new_stream(s, NULL);
  139. if (!st)
  140. return -1;
  141. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  142. st->codec->codec_tag = id;
  143. st->codec->codec_id = codec;
  144. st->codec->channels = channels;
  145. st->codec->sample_rate = rate;
  146. if (data_size != AU_UNKNOWN_SIZE)
  147. st->duration = (((int64_t)data_size)<<3) / (st->codec->channels * (int64_t)bps);
  148. avpriv_set_pts_info(st, 64, 1, rate);
  149. return 0;
  150. }
  151. #define BLOCK_SIZE 1024
  152. static int au_read_packet(AVFormatContext *s,
  153. AVPacket *pkt)
  154. {
  155. int ret;
  156. int bpcs = av_get_bits_per_sample(s->streams[0]->codec->codec_id);
  157. if (!bpcs)
  158. return AVERROR(EINVAL);
  159. ret= av_get_packet(s->pb, pkt, BLOCK_SIZE *
  160. s->streams[0]->codec->channels *
  161. bpcs >> 3);
  162. if (ret < 0)
  163. return ret;
  164. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  165. pkt->stream_index = 0;
  166. return 0;
  167. }
  168. #if CONFIG_AU_DEMUXER
  169. AVInputFormat ff_au_demuxer = {
  170. .name = "au",
  171. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  172. .read_probe = au_probe,
  173. .read_header = au_read_header,
  174. .read_packet = au_read_packet,
  175. .read_seek = ff_pcm_read_seek,
  176. .codec_tag = (const AVCodecTag* const []){ codec_au_tags, 0 },
  177. };
  178. #endif
  179. #if CONFIG_AU_MUXER
  180. AVOutputFormat ff_au_muxer = {
  181. .name = "au",
  182. .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
  183. .mime_type = "audio/basic",
  184. .extensions = "au",
  185. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  186. .video_codec = AV_CODEC_ID_NONE,
  187. .write_header = au_write_header,
  188. .write_packet = au_write_packet,
  189. .write_trailer = au_write_trailer,
  190. .codec_tag = (const AVCodecTag* const []){ codec_au_tags, 0 },
  191. };
  192. #endif //CONFIG_AU_MUXER