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.

323 lines
9.6KB

  1. /*
  2. * WAV muxer
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * Sony Wave64 muxer
  6. * Copyright (c) 2012 Paul B Mahol
  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. #include <stdint.h>
  25. #include <string.h>
  26. #include "libavutil/dict.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "avformat.h"
  31. #include "avio.h"
  32. #include "avio_internal.h"
  33. #include "internal.h"
  34. #include "riff.h"
  35. typedef struct WAVMuxContext {
  36. const AVClass *class;
  37. int64_t data;
  38. int64_t fact_pos;
  39. int64_t minpts;
  40. int64_t maxpts;
  41. int last_duration;
  42. int write_bext;
  43. } WAVMuxContext;
  44. #if CONFIG_WAV_MUXER
  45. static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
  46. {
  47. AVDictionaryEntry *tag;
  48. int len = 0;
  49. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  50. len = strlen(tag->value);
  51. len = FFMIN(len, maxlen);
  52. avio_write(s->pb, tag->value, len);
  53. }
  54. ffio_fill(s->pb, 0, maxlen - len);
  55. }
  56. static void bwf_write_bext_chunk(AVFormatContext *s)
  57. {
  58. AVDictionaryEntry *tmp_tag;
  59. uint64_t time_reference = 0;
  60. int64_t bext = ff_start_tag(s->pb, "bext");
  61. bwf_write_bext_string(s, "description", 256);
  62. bwf_write_bext_string(s, "originator", 32);
  63. bwf_write_bext_string(s, "originator_reference", 32);
  64. bwf_write_bext_string(s, "origination_date", 10);
  65. bwf_write_bext_string(s, "origination_time", 8);
  66. if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
  67. time_reference = strtoll(tmp_tag->value, NULL, 10);
  68. avio_wl64(s->pb, time_reference);
  69. avio_wl16(s->pb, 1); // set version to 1
  70. if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
  71. unsigned char umidpart_str[17] = {0};
  72. int i;
  73. uint64_t umidpart;
  74. int len = strlen(tmp_tag->value+2);
  75. for (i = 0; i < len/16; i++) {
  76. memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
  77. umidpart = strtoll(umidpart_str, NULL, 16);
  78. avio_wb64(s->pb, umidpart);
  79. }
  80. ffio_fill(s->pb, 0, 64 - i*8);
  81. } else
  82. ffio_fill(s->pb, 0, 64); // zero UMID
  83. ffio_fill(s->pb, 0, 190); // Reserved
  84. if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
  85. avio_put_str(s->pb, tmp_tag->value);
  86. ff_end_tag(s->pb, bext);
  87. }
  88. static int wav_write_header(AVFormatContext *s)
  89. {
  90. WAVMuxContext *wav = s->priv_data;
  91. AVIOContext *pb = s->pb;
  92. int64_t fmt;
  93. ffio_wfourcc(pb, "RIFF");
  94. avio_wl32(pb, 0); /* file length */
  95. ffio_wfourcc(pb, "WAVE");
  96. /* format header */
  97. fmt = ff_start_tag(pb, "fmt ");
  98. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  99. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  100. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  101. return -1;
  102. }
  103. ff_end_tag(pb, fmt);
  104. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  105. && s->pb->seekable) {
  106. wav->fact_pos = ff_start_tag(pb, "fact");
  107. avio_wl32(pb, 0);
  108. ff_end_tag(pb, wav->fact_pos);
  109. }
  110. if (wav->write_bext)
  111. bwf_write_bext_chunk(s);
  112. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  113. wav->maxpts = wav->last_duration = 0;
  114. wav->minpts = INT64_MAX;
  115. /* info header */
  116. ff_riff_write_info(s);
  117. /* data header */
  118. wav->data = ff_start_tag(pb, "data");
  119. avio_flush(pb);
  120. return 0;
  121. }
  122. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  123. {
  124. AVIOContext *pb = s->pb;
  125. WAVMuxContext *wav = s->priv_data;
  126. avio_write(pb, pkt->data, pkt->size);
  127. if(pkt->pts != AV_NOPTS_VALUE) {
  128. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  129. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  130. wav->last_duration = pkt->duration;
  131. } else
  132. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  133. return 0;
  134. }
  135. static int wav_write_trailer(AVFormatContext *s)
  136. {
  137. AVIOContext *pb = s->pb;
  138. WAVMuxContext *wav = s->priv_data;
  139. int64_t file_size;
  140. avio_flush(pb);
  141. if (s->pb->seekable) {
  142. ff_end_tag(pb, wav->data);
  143. /* update file size */
  144. file_size = avio_tell(pb);
  145. avio_seek(pb, 4, SEEK_SET);
  146. avio_wl32(pb, (uint32_t)(file_size - 8));
  147. avio_seek(pb, file_size, SEEK_SET);
  148. avio_flush(pb);
  149. if(s->streams[0]->codec->codec_tag != 0x01) {
  150. /* Update num_samps in fact chunk */
  151. int number_of_samples;
  152. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  153. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  154. s->streams[0]->time_base.den);
  155. avio_seek(pb, wav->fact_pos, SEEK_SET);
  156. avio_wl32(pb, number_of_samples);
  157. avio_seek(pb, file_size, SEEK_SET);
  158. avio_flush(pb);
  159. }
  160. }
  161. return 0;
  162. }
  163. #define OFFSET(x) offsetof(WAVMuxContext, x)
  164. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  165. static const AVOption options[] = {
  166. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  167. { NULL },
  168. };
  169. static const AVClass wav_muxer_class = {
  170. .class_name = "WAV muxer",
  171. .item_name = av_default_item_name,
  172. .option = options,
  173. .version = LIBAVUTIL_VERSION_INT,
  174. };
  175. AVOutputFormat ff_wav_muxer = {
  176. .name = "wav",
  177. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  178. .mime_type = "audio/x-wav",
  179. .extensions = "wav",
  180. .priv_data_size = sizeof(WAVMuxContext),
  181. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  182. .video_codec = AV_CODEC_ID_NONE,
  183. .write_header = wav_write_header,
  184. .write_packet = wav_write_packet,
  185. .write_trailer = wav_write_trailer,
  186. .flags = AVFMT_TS_NONSTRICT,
  187. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  188. .priv_class = &wav_muxer_class,
  189. };
  190. #endif /* CONFIG_WAV_MUXER */
  191. #if CONFIG_W64_MUXER
  192. #include "w64.h"
  193. static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
  194. {
  195. *pos = avio_tell(pb);
  196. avio_write(pb, guid, 16);
  197. avio_wl64(pb, INT64_MAX);
  198. }
  199. static void end_guid(AVIOContext *pb, int64_t start)
  200. {
  201. int64_t end, pos = avio_tell(pb);
  202. end = FFALIGN(pos, 8);
  203. ffio_fill(pb, 0, end - pos);
  204. avio_seek(pb, start + 16, SEEK_SET);
  205. avio_wl64(pb, end - start);
  206. avio_seek(pb, end, SEEK_SET);
  207. }
  208. static int w64_write_header(AVFormatContext *s)
  209. {
  210. WAVMuxContext *wav = s->priv_data;
  211. AVIOContext *pb = s->pb;
  212. int64_t start;
  213. int ret;
  214. avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
  215. avio_wl64(pb, -1);
  216. avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
  217. start_guid(pb, ff_w64_guid_fmt, &start);
  218. if ((ret = ff_put_wav_header(pb, s->streams[0]->codec)) < 0) {
  219. av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
  220. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  221. return ret;
  222. }
  223. end_guid(pb, start);
  224. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  225. && s->pb->seekable) {
  226. start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
  227. avio_wl64(pb, 0);
  228. end_guid(pb, wav->fact_pos);
  229. }
  230. start_guid(pb, ff_w64_guid_data, &wav->data);
  231. return 0;
  232. }
  233. static int w64_write_trailer(AVFormatContext *s)
  234. {
  235. AVIOContext *pb = s->pb;
  236. WAVMuxContext *wav = s->priv_data;
  237. int64_t file_size;
  238. if (pb->seekable) {
  239. end_guid(pb, wav->data);
  240. file_size = avio_tell(pb);
  241. avio_seek(pb, 16, SEEK_SET);
  242. avio_wl64(pb, file_size);
  243. if (s->streams[0]->codec->codec_tag != 0x01) {
  244. int64_t number_of_samples;
  245. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  246. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  247. s->streams[0]->time_base.den);
  248. avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
  249. avio_wl64(pb, number_of_samples);
  250. }
  251. avio_seek(pb, file_size, SEEK_SET);
  252. avio_flush(pb);
  253. }
  254. return 0;
  255. }
  256. AVOutputFormat ff_w64_muxer = {
  257. .name = "w64",
  258. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  259. .extensions = "w64",
  260. .priv_data_size = sizeof(WAVMuxContext),
  261. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  262. .video_codec = AV_CODEC_ID_NONE,
  263. .write_header = w64_write_header,
  264. .write_packet = wav_write_packet,
  265. .write_trailer = w64_write_trailer,
  266. .flags = AVFMT_TS_NONSTRICT,
  267. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  268. };
  269. #endif /* CONFIG_W64_MUXER */