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.

390 lines
12KB

  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. * WAV muxer RF64 support
  9. * Copyright (c) 2013 Daniel Verkamp <daniel@drv.nu>
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "libavutil/dict.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/mathematics.h"
  32. #include "libavutil/opt.h"
  33. #include "avformat.h"
  34. #include "avio.h"
  35. #include "avio_internal.h"
  36. #include "internal.h"
  37. #include "riff.h"
  38. #define RF64_AUTO (-1)
  39. #define RF64_NEVER 0
  40. #define RF64_ALWAYS 1
  41. typedef struct WAVMuxContext {
  42. const AVClass *class;
  43. int64_t data;
  44. int64_t fact_pos;
  45. int64_t ds64;
  46. int64_t minpts;
  47. int64_t maxpts;
  48. int last_duration;
  49. int write_bext;
  50. int rf64;
  51. } WAVMuxContext;
  52. #if CONFIG_WAV_MUXER
  53. static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
  54. {
  55. AVDictionaryEntry *tag;
  56. int len = 0;
  57. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  58. len = strlen(tag->value);
  59. len = FFMIN(len, maxlen);
  60. avio_write(s->pb, tag->value, len);
  61. }
  62. ffio_fill(s->pb, 0, maxlen - len);
  63. }
  64. static void bwf_write_bext_chunk(AVFormatContext *s)
  65. {
  66. AVDictionaryEntry *tmp_tag;
  67. uint64_t time_reference = 0;
  68. int64_t bext = ff_start_tag(s->pb, "bext");
  69. bwf_write_bext_string(s, "description", 256);
  70. bwf_write_bext_string(s, "originator", 32);
  71. bwf_write_bext_string(s, "originator_reference", 32);
  72. bwf_write_bext_string(s, "origination_date", 10);
  73. bwf_write_bext_string(s, "origination_time", 8);
  74. if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
  75. time_reference = strtoll(tmp_tag->value, NULL, 10);
  76. avio_wl64(s->pb, time_reference);
  77. avio_wl16(s->pb, 1); // set version to 1
  78. if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
  79. unsigned char umidpart_str[17] = {0};
  80. int i;
  81. uint64_t umidpart;
  82. int len = strlen(tmp_tag->value+2);
  83. for (i = 0; i < len/16; i++) {
  84. memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
  85. umidpart = strtoll(umidpart_str, NULL, 16);
  86. avio_wb64(s->pb, umidpart);
  87. }
  88. ffio_fill(s->pb, 0, 64 - i*8);
  89. } else
  90. ffio_fill(s->pb, 0, 64); // zero UMID
  91. ffio_fill(s->pb, 0, 190); // Reserved
  92. if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
  93. avio_put_str(s->pb, tmp_tag->value);
  94. ff_end_tag(s->pb, bext);
  95. }
  96. static int wav_write_header(AVFormatContext *s)
  97. {
  98. WAVMuxContext *wav = s->priv_data;
  99. AVIOContext *pb = s->pb;
  100. int64_t fmt;
  101. if (s->nb_streams != 1) {
  102. av_log(s, AV_LOG_ERROR, "WAVE files have exactly one stream\n");
  103. return AVERROR(EINVAL);
  104. }
  105. if (wav->rf64 == RF64_ALWAYS) {
  106. ffio_wfourcc(pb, "RF64");
  107. avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
  108. } else {
  109. ffio_wfourcc(pb, "RIFF");
  110. avio_wl32(pb, -1); /* file length */
  111. }
  112. ffio_wfourcc(pb, "WAVE");
  113. if (wav->rf64 != RF64_NEVER) {
  114. /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
  115. ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
  116. avio_wl32(pb, 28); /* chunk size */
  117. wav->ds64 = avio_tell(pb);
  118. ffio_fill(pb, 0, 28);
  119. }
  120. /* format header */
  121. fmt = ff_start_tag(pb, "fmt ");
  122. if (ff_put_wav_header(pb, s->streams[0]->codec, 0) < 0) {
  123. const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codec->codec_id);
  124. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  125. desc ? desc->name : "unknown");
  126. return AVERROR(ENOSYS);
  127. }
  128. ff_end_tag(pb, fmt);
  129. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  130. && s->pb->seekable) {
  131. wav->fact_pos = ff_start_tag(pb, "fact");
  132. avio_wl32(pb, 0);
  133. ff_end_tag(pb, wav->fact_pos);
  134. }
  135. if (wav->write_bext)
  136. bwf_write_bext_chunk(s);
  137. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  138. wav->maxpts = wav->last_duration = 0;
  139. wav->minpts = INT64_MAX;
  140. /* info header */
  141. ff_riff_write_info(s);
  142. /* data header */
  143. wav->data = ff_start_tag(pb, "data");
  144. avio_flush(pb);
  145. return 0;
  146. }
  147. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  148. {
  149. AVIOContext *pb = s->pb;
  150. WAVMuxContext *wav = s->priv_data;
  151. avio_write(pb, pkt->data, pkt->size);
  152. if(pkt->pts != AV_NOPTS_VALUE) {
  153. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  154. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  155. wav->last_duration = pkt->duration;
  156. } else
  157. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  158. return 0;
  159. }
  160. static int wav_write_trailer(AVFormatContext *s)
  161. {
  162. AVIOContext *pb = s->pb;
  163. WAVMuxContext *wav = s->priv_data;
  164. int64_t file_size, data_size;
  165. int64_t number_of_samples = 0;
  166. int rf64 = 0;
  167. avio_flush(pb);
  168. if (s->pb->seekable) {
  169. /* update file size */
  170. file_size = avio_tell(pb);
  171. data_size = file_size - wav->data;
  172. if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
  173. rf64 = 1;
  174. } else {
  175. avio_seek(pb, 4, SEEK_SET);
  176. avio_wl32(pb, (uint32_t)(file_size - 8));
  177. avio_seek(pb, file_size, SEEK_SET);
  178. ff_end_tag(pb, wav->data);
  179. avio_flush(pb);
  180. }
  181. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  182. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  183. s->streams[0]->time_base.den);
  184. if(s->streams[0]->codec->codec_tag != 0x01) {
  185. /* Update num_samps in fact chunk */
  186. avio_seek(pb, wav->fact_pos, SEEK_SET);
  187. if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
  188. rf64 = 1;
  189. avio_wl32(pb, -1);
  190. } else {
  191. avio_wl32(pb, number_of_samples);
  192. avio_seek(pb, file_size, SEEK_SET);
  193. avio_flush(pb);
  194. }
  195. }
  196. if (rf64) {
  197. /* overwrite RIFF with RF64 */
  198. avio_seek(pb, 0, SEEK_SET);
  199. ffio_wfourcc(pb, "RF64");
  200. avio_wl32(pb, -1);
  201. /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
  202. avio_seek(pb, wav->ds64 - 8, SEEK_SET);
  203. ffio_wfourcc(pb, "ds64");
  204. avio_wl32(pb, 28); /* ds64 chunk size */
  205. avio_wl64(pb, file_size - 8); /* RF64 chunk size */
  206. avio_wl64(pb, data_size); /* data chunk size */
  207. avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
  208. avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
  209. /* write -1 in data chunk size */
  210. avio_seek(pb, wav->data - 4, SEEK_SET);
  211. avio_wl32(pb, -1);
  212. avio_seek(pb, file_size, SEEK_SET);
  213. avio_flush(pb);
  214. }
  215. }
  216. return 0;
  217. }
  218. #define OFFSET(x) offsetof(WAVMuxContext, x)
  219. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  220. static const AVOption options[] = {
  221. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  222. { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, "rf64" },
  223. { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, "rf64" },
  224. { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
  225. { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, "rf64" },
  226. { NULL },
  227. };
  228. static const AVClass wav_muxer_class = {
  229. .class_name = "WAV muxer",
  230. .item_name = av_default_item_name,
  231. .option = options,
  232. .version = LIBAVUTIL_VERSION_INT,
  233. };
  234. AVOutputFormat ff_wav_muxer = {
  235. .name = "wav",
  236. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  237. .mime_type = "audio/x-wav",
  238. .extensions = "wav",
  239. .priv_data_size = sizeof(WAVMuxContext),
  240. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  241. .video_codec = AV_CODEC_ID_NONE,
  242. .write_header = wav_write_header,
  243. .write_packet = wav_write_packet,
  244. .write_trailer = wav_write_trailer,
  245. .flags = AVFMT_TS_NONSTRICT,
  246. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  247. .priv_class = &wav_muxer_class,
  248. };
  249. #endif /* CONFIG_WAV_MUXER */
  250. #if CONFIG_W64_MUXER
  251. #include "w64.h"
  252. static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
  253. {
  254. *pos = avio_tell(pb);
  255. avio_write(pb, guid, 16);
  256. avio_wl64(pb, INT64_MAX);
  257. }
  258. static void end_guid(AVIOContext *pb, int64_t start)
  259. {
  260. int64_t end, pos = avio_tell(pb);
  261. end = FFALIGN(pos, 8);
  262. ffio_fill(pb, 0, end - pos);
  263. avio_seek(pb, start + 16, SEEK_SET);
  264. avio_wl64(pb, end - start);
  265. avio_seek(pb, end, SEEK_SET);
  266. }
  267. static int w64_write_header(AVFormatContext *s)
  268. {
  269. WAVMuxContext *wav = s->priv_data;
  270. AVIOContext *pb = s->pb;
  271. int64_t start;
  272. int ret;
  273. avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
  274. avio_wl64(pb, -1);
  275. avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
  276. start_guid(pb, ff_w64_guid_fmt, &start);
  277. if ((ret = ff_put_wav_header(pb, s->streams[0]->codec, 0)) < 0) {
  278. av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
  279. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  280. return ret;
  281. }
  282. end_guid(pb, start);
  283. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  284. && s->pb->seekable) {
  285. start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
  286. avio_wl64(pb, 0);
  287. end_guid(pb, wav->fact_pos);
  288. }
  289. start_guid(pb, ff_w64_guid_data, &wav->data);
  290. return 0;
  291. }
  292. static int w64_write_trailer(AVFormatContext *s)
  293. {
  294. AVIOContext *pb = s->pb;
  295. WAVMuxContext *wav = s->priv_data;
  296. int64_t file_size;
  297. if (pb->seekable) {
  298. end_guid(pb, wav->data);
  299. file_size = avio_tell(pb);
  300. avio_seek(pb, 16, SEEK_SET);
  301. avio_wl64(pb, file_size);
  302. if (s->streams[0]->codec->codec_tag != 0x01) {
  303. int64_t number_of_samples;
  304. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  305. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  306. s->streams[0]->time_base.den);
  307. avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
  308. avio_wl64(pb, number_of_samples);
  309. }
  310. avio_seek(pb, file_size, SEEK_SET);
  311. avio_flush(pb);
  312. }
  313. return 0;
  314. }
  315. AVOutputFormat ff_w64_muxer = {
  316. .name = "w64",
  317. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  318. .extensions = "w64",
  319. .priv_data_size = sizeof(WAVMuxContext),
  320. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  321. .video_codec = AV_CODEC_ID_NONE,
  322. .write_header = w64_write_header,
  323. .write_packet = wav_write_packet,
  324. .write_trailer = w64_write_trailer,
  325. .flags = AVFMT_TS_NONSTRICT,
  326. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  327. };
  328. #endif /* CONFIG_W64_MUXER */