soxenc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SoX native format muxer
  3. * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
  4. *
  5. * Based on libSoX sox-fmt.c
  6. * Copyright (c) 2008 robs@users.sourceforge.net
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * SoX native format muxer
  26. * @file
  27. * @author Daniel Verkamp
  28. * @sa http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format
  29. */
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/dict.h"
  32. #include "avformat.h"
  33. #include "avio_internal.h"
  34. #include "sox.h"
  35. typedef struct {
  36. int64_t header_size;
  37. } SoXContext;
  38. static int sox_write_header(AVFormatContext *s)
  39. {
  40. SoXContext *sox = s->priv_data;
  41. AVIOContext *pb = s->pb;
  42. AVCodecContext *enc = s->streams[0]->codec;
  43. AVDictionaryEntry *comment;
  44. size_t comment_len = 0, comment_size;
  45. comment = av_dict_get(s->metadata, "comment", NULL, 0);
  46. if (comment)
  47. comment_len = strlen(comment->value);
  48. comment_size = (comment_len + 7) & ~7;
  49. sox->header_size = SOX_FIXED_HDR + comment_size;
  50. if (enc->codec_id == CODEC_ID_PCM_S32LE) {
  51. ffio_wfourcc(pb, ".SoX");
  52. avio_wl32(pb, sox->header_size);
  53. avio_wl64(pb, 0); /* number of samples */
  54. avio_wl64(pb, av_dbl2int(enc->sample_rate));
  55. avio_wl32(pb, enc->channels);
  56. avio_wl32(pb, comment_size);
  57. } else if (enc->codec_id == CODEC_ID_PCM_S32BE) {
  58. ffio_wfourcc(pb, "XoS.");
  59. avio_wb32(pb, sox->header_size);
  60. avio_wb64(pb, 0); /* number of samples */
  61. avio_wb64(pb, av_dbl2int(enc->sample_rate));
  62. avio_wb32(pb, enc->channels);
  63. avio_wb32(pb, comment_size);
  64. } else {
  65. av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
  66. return -1;
  67. }
  68. if (comment_len)
  69. avio_write(pb, comment->value, comment_len);
  70. for ( ; comment_size > comment_len; comment_len++)
  71. avio_w8(pb, 0);
  72. avio_flush(pb);
  73. return 0;
  74. }
  75. static int sox_write_packet(AVFormatContext *s, AVPacket *pkt)
  76. {
  77. AVIOContext *pb = s->pb;
  78. avio_write(pb, pkt->data, pkt->size);
  79. return 0;
  80. }
  81. static int sox_write_trailer(AVFormatContext *s)
  82. {
  83. SoXContext *sox = s->priv_data;
  84. AVIOContext *pb = s->pb;
  85. AVCodecContext *enc = s->streams[0]->codec;
  86. if (s->pb->seekable) {
  87. /* update number of samples */
  88. int64_t file_size = avio_tell(pb);
  89. int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
  90. avio_seek(pb, 8, SEEK_SET);
  91. if (enc->codec_id == CODEC_ID_PCM_S32LE) {
  92. avio_wl64(pb, num_samples);
  93. } else
  94. avio_wb64(pb, num_samples);
  95. avio_seek(pb, file_size, SEEK_SET);
  96. avio_flush(pb);
  97. }
  98. return 0;
  99. }
  100. AVOutputFormat ff_sox_muxer = {
  101. "sox",
  102. NULL_IF_CONFIG_SMALL("SoX native format"),
  103. NULL,
  104. "sox",
  105. sizeof(SoXContext),
  106. CODEC_ID_PCM_S32LE,
  107. CODEC_ID_NONE,
  108. sox_write_header,
  109. sox_write_packet,
  110. sox_write_trailer,
  111. };