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.

384 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 (wav->rf64 == RF64_ALWAYS) {
  102. ffio_wfourcc(pb, "RF64");
  103. avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
  104. } else {
  105. ffio_wfourcc(pb, "RIFF");
  106. avio_wl32(pb, 0); /* file length */
  107. }
  108. ffio_wfourcc(pb, "WAVE");
  109. if (wav->rf64 != RF64_NEVER) {
  110. /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
  111. ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
  112. avio_wl32(pb, 28); /* chunk size */
  113. wav->ds64 = avio_tell(pb);
  114. ffio_fill(pb, 0, 28);
  115. }
  116. /* format header */
  117. fmt = ff_start_tag(pb, "fmt ");
  118. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  119. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  120. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  121. return -1;
  122. }
  123. ff_end_tag(pb, fmt);
  124. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  125. && s->pb->seekable) {
  126. wav->fact_pos = ff_start_tag(pb, "fact");
  127. avio_wl32(pb, 0);
  128. ff_end_tag(pb, wav->fact_pos);
  129. }
  130. if (wav->write_bext)
  131. bwf_write_bext_chunk(s);
  132. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  133. wav->maxpts = wav->last_duration = 0;
  134. wav->minpts = INT64_MAX;
  135. /* info header */
  136. ff_riff_write_info(s);
  137. /* data header */
  138. wav->data = ff_start_tag(pb, "data");
  139. avio_flush(pb);
  140. return 0;
  141. }
  142. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  143. {
  144. AVIOContext *pb = s->pb;
  145. WAVMuxContext *wav = s->priv_data;
  146. avio_write(pb, pkt->data, pkt->size);
  147. if(pkt->pts != AV_NOPTS_VALUE) {
  148. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  149. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  150. wav->last_duration = pkt->duration;
  151. } else
  152. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  153. return 0;
  154. }
  155. static int wav_write_trailer(AVFormatContext *s)
  156. {
  157. AVIOContext *pb = s->pb;
  158. WAVMuxContext *wav = s->priv_data;
  159. int64_t file_size, data_size;
  160. int64_t number_of_samples = 0;
  161. int rf64 = 0;
  162. avio_flush(pb);
  163. if (s->pb->seekable) {
  164. /* update file size */
  165. file_size = avio_tell(pb);
  166. data_size = file_size - wav->data;
  167. if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
  168. rf64 = 1;
  169. } else {
  170. avio_seek(pb, 4, SEEK_SET);
  171. avio_wl32(pb, (uint32_t)(file_size - 8));
  172. avio_seek(pb, file_size, SEEK_SET);
  173. ff_end_tag(pb, wav->data);
  174. avio_flush(pb);
  175. }
  176. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  177. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  178. s->streams[0]->time_base.den);
  179. if(s->streams[0]->codec->codec_tag != 0x01) {
  180. /* Update num_samps in fact chunk */
  181. avio_seek(pb, wav->fact_pos, SEEK_SET);
  182. if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
  183. rf64 = 1;
  184. avio_wl32(pb, -1);
  185. } else {
  186. avio_wl32(pb, number_of_samples);
  187. avio_seek(pb, file_size, SEEK_SET);
  188. avio_flush(pb);
  189. }
  190. }
  191. if (rf64) {
  192. /* overwrite RIFF with RF64 */
  193. avio_seek(pb, 0, SEEK_SET);
  194. ffio_wfourcc(pb, "RF64");
  195. avio_wl32(pb, -1);
  196. /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
  197. avio_seek(pb, wav->ds64 - 8, SEEK_SET);
  198. ffio_wfourcc(pb, "ds64");
  199. avio_wl32(pb, 28); /* ds64 chunk size */
  200. avio_wl64(pb, file_size - 8); /* RF64 chunk size */
  201. avio_wl64(pb, data_size); /* data chunk size */
  202. avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
  203. avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
  204. /* write -1 in data chunk size */
  205. avio_seek(pb, wav->data - 4, SEEK_SET);
  206. avio_wl32(pb, -1);
  207. avio_seek(pb, file_size, SEEK_SET);
  208. avio_flush(pb);
  209. }
  210. }
  211. return 0;
  212. }
  213. #define OFFSET(x) offsetof(WAVMuxContext, x)
  214. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  215. static const AVOption options[] = {
  216. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  217. { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, "rf64" },
  218. { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, "rf64" },
  219. { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
  220. { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, "rf64" },
  221. { NULL },
  222. };
  223. static const AVClass wav_muxer_class = {
  224. .class_name = "WAV muxer",
  225. .item_name = av_default_item_name,
  226. .option = options,
  227. .version = LIBAVUTIL_VERSION_INT,
  228. };
  229. AVOutputFormat ff_wav_muxer = {
  230. .name = "wav",
  231. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  232. .mime_type = "audio/x-wav",
  233. .extensions = "wav",
  234. .priv_data_size = sizeof(WAVMuxContext),
  235. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  236. .video_codec = AV_CODEC_ID_NONE,
  237. .write_header = wav_write_header,
  238. .write_packet = wav_write_packet,
  239. .write_trailer = wav_write_trailer,
  240. .flags = AVFMT_TS_NONSTRICT,
  241. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  242. .priv_class = &wav_muxer_class,
  243. };
  244. #endif /* CONFIG_WAV_MUXER */
  245. #if CONFIG_W64_MUXER
  246. #include "w64.h"
  247. static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
  248. {
  249. *pos = avio_tell(pb);
  250. avio_write(pb, guid, 16);
  251. avio_wl64(pb, INT64_MAX);
  252. }
  253. static void end_guid(AVIOContext *pb, int64_t start)
  254. {
  255. int64_t end, pos = avio_tell(pb);
  256. end = FFALIGN(pos, 8);
  257. ffio_fill(pb, 0, end - pos);
  258. avio_seek(pb, start + 16, SEEK_SET);
  259. avio_wl64(pb, end - start);
  260. avio_seek(pb, end, SEEK_SET);
  261. }
  262. static int w64_write_header(AVFormatContext *s)
  263. {
  264. WAVMuxContext *wav = s->priv_data;
  265. AVIOContext *pb = s->pb;
  266. int64_t start;
  267. int ret;
  268. avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
  269. avio_wl64(pb, -1);
  270. avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
  271. start_guid(pb, ff_w64_guid_fmt, &start);
  272. if ((ret = ff_put_wav_header(pb, s->streams[0]->codec)) < 0) {
  273. av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
  274. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  275. return ret;
  276. }
  277. end_guid(pb, start);
  278. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  279. && s->pb->seekable) {
  280. start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
  281. avio_wl64(pb, 0);
  282. end_guid(pb, wav->fact_pos);
  283. }
  284. start_guid(pb, ff_w64_guid_data, &wav->data);
  285. return 0;
  286. }
  287. static int w64_write_trailer(AVFormatContext *s)
  288. {
  289. AVIOContext *pb = s->pb;
  290. WAVMuxContext *wav = s->priv_data;
  291. int64_t file_size;
  292. if (pb->seekable) {
  293. end_guid(pb, wav->data);
  294. file_size = avio_tell(pb);
  295. avio_seek(pb, 16, SEEK_SET);
  296. avio_wl64(pb, file_size);
  297. if (s->streams[0]->codec->codec_tag != 0x01) {
  298. int64_t number_of_samples;
  299. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  300. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  301. s->streams[0]->time_base.den);
  302. avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
  303. avio_wl64(pb, number_of_samples);
  304. }
  305. avio_seek(pb, file_size, SEEK_SET);
  306. avio_flush(pb);
  307. }
  308. return 0;
  309. }
  310. AVOutputFormat ff_w64_muxer = {
  311. .name = "w64",
  312. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  313. .extensions = "w64",
  314. .priv_data_size = sizeof(WAVMuxContext),
  315. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  316. .video_codec = AV_CODEC_ID_NONE,
  317. .write_header = w64_write_header,
  318. .write_packet = wav_write_packet,
  319. .write_trailer = w64_write_trailer,
  320. .flags = AVFMT_TS_NONSTRICT,
  321. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  322. };
  323. #endif /* CONFIG_W64_MUXER */