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.

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