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.

123 lines
3.7KB

  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 "rawenc.h"
  36. #include "sox.h"
  37. typedef struct {
  38. int64_t header_size;
  39. } SoXContext;
  40. static int sox_write_header(AVFormatContext *s)
  41. {
  42. SoXContext *sox = s->priv_data;
  43. AVIOContext *pb = s->pb;
  44. AVCodecContext *enc = s->streams[0]->codec;
  45. AVDictionaryEntry *comment;
  46. size_t comment_len = 0, comment_size;
  47. comment = av_dict_get(s->metadata, "comment", NULL, 0);
  48. if (comment)
  49. comment_len = strlen(comment->value);
  50. comment_size = FFALIGN(comment_len, 8);
  51. sox->header_size = SOX_FIXED_HDR + comment_size;
  52. if (enc->codec_id == AV_CODEC_ID_PCM_S32LE) {
  53. ffio_wfourcc(pb, ".SoX");
  54. avio_wl32(pb, sox->header_size);
  55. avio_wl64(pb, 0); /* number of samples */
  56. avio_wl64(pb, av_double2int(enc->sample_rate));
  57. avio_wl32(pb, enc->channels);
  58. avio_wl32(pb, comment_size);
  59. } else if (enc->codec_id == AV_CODEC_ID_PCM_S32BE) {
  60. ffio_wfourcc(pb, "XoS.");
  61. avio_wb32(pb, sox->header_size);
  62. avio_wb64(pb, 0); /* number of samples */
  63. avio_wb64(pb, av_double2int(enc->sample_rate));
  64. avio_wb32(pb, enc->channels);
  65. avio_wb32(pb, comment_size);
  66. } else {
  67. av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
  68. return -1;
  69. }
  70. if (comment_len)
  71. avio_write(pb, comment->value, comment_len);
  72. ffio_fill(pb, 0, comment_size - comment_len);
  73. avio_flush(pb);
  74. return 0;
  75. }
  76. static int sox_write_trailer(AVFormatContext *s)
  77. {
  78. SoXContext *sox = s->priv_data;
  79. AVIOContext *pb = s->pb;
  80. AVCodecContext *enc = s->streams[0]->codec;
  81. if (s->pb->seekable) {
  82. /* update number of samples */
  83. int64_t file_size = avio_tell(pb);
  84. int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
  85. avio_seek(pb, 8, SEEK_SET);
  86. if (enc->codec_id == AV_CODEC_ID_PCM_S32LE) {
  87. avio_wl64(pb, num_samples);
  88. } else
  89. avio_wb64(pb, num_samples);
  90. avio_seek(pb, file_size, SEEK_SET);
  91. avio_flush(pb);
  92. }
  93. return 0;
  94. }
  95. AVOutputFormat ff_sox_muxer = {
  96. .name = "sox",
  97. .long_name = NULL_IF_CONFIG_SMALL("SoX native"),
  98. .extensions = "sox",
  99. .priv_data_size = sizeof(SoXContext),
  100. .audio_codec = AV_CODEC_ID_PCM_S32LE,
  101. .video_codec = AV_CODEC_ID_NONE,
  102. .write_header = sox_write_header,
  103. .write_packet = ff_raw_write_packet,
  104. .write_trailer = sox_write_trailer,
  105. .flags = AVFMT_NOTIMESTAMPS,
  106. };