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.

222 lines
6.8KB

  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(s, pb, s->streams[0]->codecpar) < 0) {
  95. const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codecpar->codec_id);
  96. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  97. desc ? desc->name : "unknown");
  98. return AVERROR(ENOSYS);
  99. }
  100. ff_end_tag(pb, fmt);
  101. if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
  102. && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  103. wav->fact_pos = ff_start_tag(pb, "fact");
  104. avio_wl32(pb, 0);
  105. ff_end_tag(pb, wav->fact_pos);
  106. }
  107. if (wav->write_bext)
  108. bwf_write_bext_chunk(s);
  109. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
  110. wav->maxpts = wav->last_duration = 0;
  111. wav->minpts = INT64_MAX;
  112. /* info header */
  113. ff_riff_write_info(s);
  114. /* data header */
  115. wav->data = ff_start_tag(pb, "data");
  116. avio_flush(pb);
  117. return 0;
  118. }
  119. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  120. {
  121. AVIOContext *pb = s->pb;
  122. WAVMuxContext *wav = s->priv_data;
  123. avio_write(pb, pkt->data, pkt->size);
  124. if(pkt->pts != AV_NOPTS_VALUE) {
  125. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  126. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  127. wav->last_duration = pkt->duration;
  128. } else
  129. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  130. return 0;
  131. }
  132. static int wav_write_trailer(AVFormatContext *s)
  133. {
  134. AVIOContext *pb = s->pb;
  135. WAVMuxContext *wav = s->priv_data;
  136. int64_t file_size;
  137. avio_flush(pb);
  138. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  139. ff_end_tag(pb, wav->data);
  140. /* update file size */
  141. file_size = avio_tell(pb);
  142. avio_seek(pb, 4, SEEK_SET);
  143. avio_wl32(pb, (uint32_t)(file_size - 8));
  144. avio_seek(pb, file_size, SEEK_SET);
  145. avio_flush(pb);
  146. if(s->streams[0]->codecpar->codec_tag != 0x01) {
  147. /* Update num_samps in fact chunk */
  148. int number_of_samples;
  149. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  150. s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
  151. s->streams[0]->time_base.den);
  152. avio_seek(pb, wav->fact_pos, SEEK_SET);
  153. avio_wl32(pb, number_of_samples);
  154. avio_seek(pb, file_size, SEEK_SET);
  155. avio_flush(pb);
  156. }
  157. }
  158. return 0;
  159. }
  160. #define OFFSET(x) offsetof(WAVMuxContext, x)
  161. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  162. static const AVOption options[] = {
  163. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  164. { NULL },
  165. };
  166. static const AVClass wav_muxer_class = {
  167. .class_name = "WAV muxer",
  168. .item_name = av_default_item_name,
  169. .option = options,
  170. .version = LIBAVUTIL_VERSION_INT,
  171. };
  172. AVOutputFormat ff_wav_muxer = {
  173. .name = "wav",
  174. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  175. .mime_type = "audio/x-wav",
  176. .extensions = "wav",
  177. .priv_data_size = sizeof(WAVMuxContext),
  178. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  179. .video_codec = AV_CODEC_ID_NONE,
  180. .write_header = wav_write_header,
  181. .write_packet = wav_write_packet,
  182. .write_trailer = wav_write_trailer,
  183. .flags = AVFMT_TS_NONSTRICT,
  184. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  185. .priv_class = &wav_muxer_class,
  186. };