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.

220 lines
6.6KB

  1. /*
  2. * WAV muxer
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include "libavutil/dict.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavutil/opt.h"
  27. #include "avformat.h"
  28. #include "avio.h"
  29. #include "avio_internal.h"
  30. #include "internal.h"
  31. #include "riff.h"
  32. typedef struct WAVMuxContext {
  33. const AVClass *class;
  34. int64_t data;
  35. int64_t minpts;
  36. int64_t maxpts;
  37. int last_duration;
  38. int write_bext;
  39. } WAVMuxContext;
  40. static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
  41. {
  42. AVDictionaryEntry *tag;
  43. int len = 0;
  44. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  45. len = strlen(tag->value);
  46. len = FFMIN(len, maxlen);
  47. avio_write(s->pb, tag->value, len);
  48. }
  49. ffio_fill(s->pb, 0, maxlen - len);
  50. }
  51. static void bwf_write_bext_chunk(AVFormatContext *s)
  52. {
  53. AVDictionaryEntry *tmp_tag;
  54. uint64_t time_reference = 0;
  55. int64_t bext = ff_start_tag(s->pb, "bext");
  56. bwf_write_bext_string(s, "description", 256);
  57. bwf_write_bext_string(s, "originator", 32);
  58. bwf_write_bext_string(s, "originator_reference", 32);
  59. bwf_write_bext_string(s, "origination_date", 10);
  60. bwf_write_bext_string(s, "origination_time", 8);
  61. if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
  62. time_reference = strtoll(tmp_tag->value, NULL, 10);
  63. avio_wl64(s->pb, time_reference);
  64. avio_wl16(s->pb, 1); // set version to 1
  65. if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
  66. unsigned char umidpart_str[17] = {0};
  67. int i;
  68. uint64_t umidpart;
  69. int len = strlen(tmp_tag->value+2);
  70. for (i = 0; i < len/16; i++) {
  71. memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
  72. umidpart = strtoll(umidpart_str, NULL, 16);
  73. avio_wb64(s->pb, umidpart);
  74. }
  75. ffio_fill(s->pb, 0, 64 - i*8);
  76. } else
  77. ffio_fill(s->pb, 0, 64); // zero UMID
  78. ffio_fill(s->pb, 0, 190); // Reserved
  79. if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
  80. avio_put_str(s->pb, tmp_tag->value);
  81. ff_end_tag(s->pb, bext);
  82. }
  83. static int wav_write_header(AVFormatContext *s)
  84. {
  85. WAVMuxContext *wav = s->priv_data;
  86. AVIOContext *pb = s->pb;
  87. int64_t fmt, fact;
  88. ffio_wfourcc(pb, "RIFF");
  89. avio_wl32(pb, 0); /* file length */
  90. ffio_wfourcc(pb, "WAVE");
  91. /* format header */
  92. fmt = ff_start_tag(pb, "fmt ");
  93. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  94. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  95. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  96. return -1;
  97. }
  98. ff_end_tag(pb, fmt);
  99. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  100. && s->pb->seekable) {
  101. fact = ff_start_tag(pb, "fact");
  102. avio_wl32(pb, 0);
  103. ff_end_tag(pb, fact);
  104. }
  105. if (wav->write_bext)
  106. bwf_write_bext_chunk(s);
  107. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  108. wav->maxpts = wav->last_duration = 0;
  109. wav->minpts = INT64_MAX;
  110. /* info header */
  111. ff_riff_write_info(s);
  112. /* data header */
  113. wav->data = ff_start_tag(pb, "data");
  114. avio_flush(pb);
  115. return 0;
  116. }
  117. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  118. {
  119. AVIOContext *pb = s->pb;
  120. WAVMuxContext *wav = s->priv_data;
  121. avio_write(pb, pkt->data, pkt->size);
  122. if(pkt->pts != AV_NOPTS_VALUE) {
  123. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  124. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  125. wav->last_duration = pkt->duration;
  126. } else
  127. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  128. return 0;
  129. }
  130. static int wav_write_trailer(AVFormatContext *s)
  131. {
  132. AVIOContext *pb = s->pb;
  133. WAVMuxContext *wav = s->priv_data;
  134. int64_t file_size;
  135. avio_flush(pb);
  136. if (s->pb->seekable) {
  137. ff_end_tag(pb, wav->data);
  138. /* update file size */
  139. file_size = avio_tell(pb);
  140. avio_seek(pb, 4, SEEK_SET);
  141. avio_wl32(pb, (uint32_t)(file_size - 8));
  142. avio_seek(pb, file_size, SEEK_SET);
  143. avio_flush(pb);
  144. if(s->streams[0]->codec->codec_tag != 0x01) {
  145. /* Update num_samps in fact chunk */
  146. int number_of_samples;
  147. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  148. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  149. s->streams[0]->time_base.den);
  150. avio_seek(pb, wav->data-12, SEEK_SET);
  151. avio_wl32(pb, number_of_samples);
  152. avio_seek(pb, file_size, SEEK_SET);
  153. avio_flush(pb);
  154. }
  155. }
  156. return 0;
  157. }
  158. #define OFFSET(x) offsetof(WAVMuxContext, x)
  159. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  160. static const AVOption options[] = {
  161. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  162. { NULL },
  163. };
  164. static const AVClass wav_muxer_class = {
  165. .class_name = "WAV muxer",
  166. .item_name = av_default_item_name,
  167. .option = options,
  168. .version = LIBAVUTIL_VERSION_INT,
  169. };
  170. AVOutputFormat ff_wav_muxer = {
  171. .name = "wav",
  172. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  173. .mime_type = "audio/x-wav",
  174. .extensions = "wav",
  175. .priv_data_size = sizeof(WAVMuxContext),
  176. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  177. .video_codec = AV_CODEC_ID_NONE,
  178. .write_header = wav_write_header,
  179. .write_packet = wav_write_packet,
  180. .write_trailer = wav_write_trailer,
  181. .flags = AVFMT_TS_NONSTRICT,
  182. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  183. .priv_class = &wav_muxer_class,
  184. };