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.

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