soxenc.c 3.8 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. * @file
  26. * SoX native format muxer
  27. * @author Daniel Verkamp
  28. * @see http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format
  29. */
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/intfloat.h"
  32. #include "libavutil/dict.h"
  33. #include "avformat.h"
  34. #include "avio_internal.h"
  35. #include "sox.h"
  36. typedef struct {
  37. int64_t header_size;
  38. } SoXContext;
  39. static int sox_write_header(AVFormatContext *s)
  40. {
  41. SoXContext *sox = s->priv_data;
  42. AVIOContext *pb = s->pb;
  43. AVCodecContext *enc = s->streams[0]->codec;
  44. AVDictionaryEntry *comment;
  45. size_t comment_len = 0, comment_size;
  46. comment = av_dict_get(s->metadata, "comment", NULL, 0);
  47. if (comment)
  48. comment_len = strlen(comment->value);
  49. comment_size = (comment_len + 7) & ~7;
  50. sox->header_size = SOX_FIXED_HDR + comment_size;
  51. if (enc->codec_id == CODEC_ID_PCM_S32LE) {
  52. ffio_wfourcc(pb, ".SoX");
  53. avio_wl32(pb, sox->header_size);
  54. avio_wl64(pb, 0); /* number of samples */
  55. avio_wl64(pb, av_double2int(enc->sample_rate));
  56. avio_wl32(pb, enc->channels);
  57. avio_wl32(pb, comment_size);
  58. } else if (enc->codec_id == CODEC_ID_PCM_S32BE) {
  59. ffio_wfourcc(pb, "XoS.");
  60. avio_wb32(pb, sox->header_size);
  61. avio_wb64(pb, 0); /* number of samples */
  62. avio_wb64(pb, av_double2int(enc->sample_rate));
  63. avio_wb32(pb, enc->channels);
  64. avio_wb32(pb, comment_size);
  65. } else {
  66. av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
  67. return -1;
  68. }
  69. if (comment_len)
  70. avio_write(pb, comment->value, comment_len);
  71. for ( ; comment_size > comment_len; comment_len++)
  72. avio_w8(pb, 0);
  73. avio_flush(pb);
  74. return 0;
  75. }
  76. static int sox_write_packet(AVFormatContext *s, AVPacket *pkt)
  77. {
  78. AVIOContext *pb = s->pb;
  79. avio_write(pb, pkt->data, pkt->size);
  80. return 0;
  81. }
  82. static int sox_write_trailer(AVFormatContext *s)
  83. {
  84. SoXContext *sox = s->priv_data;
  85. AVIOContext *pb = s->pb;
  86. AVCodecContext *enc = s->streams[0]->codec;
  87. if (s->pb->seekable) {
  88. /* update number of samples */
  89. int64_t file_size = avio_tell(pb);
  90. int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
  91. avio_seek(pb, 8, SEEK_SET);
  92. if (enc->codec_id == CODEC_ID_PCM_S32LE) {
  93. avio_wl64(pb, num_samples);
  94. } else
  95. avio_wb64(pb, num_samples);
  96. avio_seek(pb, file_size, SEEK_SET);
  97. avio_flush(pb);
  98. }
  99. return 0;
  100. }
  101. AVOutputFormat ff_sox_muxer = {
  102. .name = "sox",
  103. .long_name = NULL_IF_CONFIG_SMALL("SoX native format"),
  104. .extensions = "sox",
  105. .priv_data_size = sizeof(SoXContext),
  106. .audio_codec = CODEC_ID_PCM_S32LE,
  107. .video_codec = CODEC_ID_NONE,
  108. .write_header = sox_write_header,
  109. .write_packet = sox_write_packet,
  110. .write_trailer = sox_write_trailer,
  111. };