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.

627 lines
21KB

  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. * EBU Tech 3285 - Supplement 3 - Peak Envelope Chunk encoder
  12. * Copyright (c) 2014 Georg Lippitsch <georg.lippitsch@gmx.at>
  13. *
  14. * This file is part of FFmpeg.
  15. *
  16. * FFmpeg is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Lesser General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 2.1 of the License, or (at your option) any later version.
  20. *
  21. * FFmpeg is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public
  27. * License along with FFmpeg; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include "libavutil/avstring.h"
  33. #include "libavutil/dict.h"
  34. #include "libavutil/common.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/time.h"
  39. #include "avformat.h"
  40. #include "avio.h"
  41. #include "avio_internal.h"
  42. #include "internal.h"
  43. #include "riff.h"
  44. #define RF64_AUTO (-1)
  45. #define RF64_NEVER 0
  46. #define RF64_ALWAYS 1
  47. #define PEAK_BUFFER_SIZE 1024
  48. typedef enum {
  49. PEAK_OFF = 0,
  50. PEAK_ON,
  51. PEAK_ONLY
  52. } PeakType;
  53. typedef enum {
  54. PEAK_FORMAT_UINT8 = 1,
  55. PEAK_FORMAT_UINT16
  56. } PeakFormat;
  57. typedef struct WAVMuxContext {
  58. const AVClass *class;
  59. int64_t data;
  60. int64_t fact_pos;
  61. int64_t ds64;
  62. int64_t minpts;
  63. int64_t maxpts;
  64. int16_t *peak_maxpos, *peak_maxneg;
  65. uint32_t peak_num_frames;
  66. uint32_t peak_outbuf_size;
  67. uint32_t peak_outbuf_bytes;
  68. uint32_t peak_pos_pop;
  69. uint16_t peak_pop;
  70. uint8_t *peak_output;
  71. int last_duration;
  72. int write_bext;
  73. int write_peak;
  74. int rf64;
  75. int peak_block_size;
  76. PeakFormat peak_format;
  77. int peak_block_pos;
  78. int peak_ppv;
  79. int peak_bps;
  80. } WAVMuxContext;
  81. #if CONFIG_WAV_MUXER
  82. static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
  83. {
  84. AVDictionaryEntry *tag;
  85. int len = 0;
  86. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  87. len = strlen(tag->value);
  88. len = FFMIN(len, maxlen);
  89. avio_write(s->pb, tag->value, len);
  90. }
  91. ffio_fill(s->pb, 0, maxlen - len);
  92. }
  93. static void bwf_write_bext_chunk(AVFormatContext *s)
  94. {
  95. AVDictionaryEntry *tmp_tag;
  96. uint64_t time_reference = 0;
  97. int64_t bext = ff_start_tag(s->pb, "bext");
  98. bwf_write_bext_string(s, "description", 256);
  99. bwf_write_bext_string(s, "originator", 32);
  100. bwf_write_bext_string(s, "originator_reference", 32);
  101. bwf_write_bext_string(s, "origination_date", 10);
  102. bwf_write_bext_string(s, "origination_time", 8);
  103. if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
  104. time_reference = strtoll(tmp_tag->value, NULL, 10);
  105. avio_wl64(s->pb, time_reference);
  106. avio_wl16(s->pb, 1); // set version to 1
  107. if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
  108. unsigned char umidpart_str[17] = {0};
  109. int i;
  110. uint64_t umidpart;
  111. int len = strlen(tmp_tag->value+2);
  112. for (i = 0; i < len/16; i++) {
  113. memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
  114. umidpart = strtoll(umidpart_str, NULL, 16);
  115. avio_wb64(s->pb, umidpart);
  116. }
  117. ffio_fill(s->pb, 0, 64 - i*8);
  118. } else
  119. ffio_fill(s->pb, 0, 64); // zero UMID
  120. ffio_fill(s->pb, 0, 190); // Reserved
  121. if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
  122. avio_put_str(s->pb, tmp_tag->value);
  123. ff_end_tag(s->pb, bext);
  124. }
  125. static av_cold void peak_free_buffers(AVFormatContext *s)
  126. {
  127. WAVMuxContext *wav = s->priv_data;
  128. av_freep(&wav->peak_maxpos);
  129. av_freep(&wav->peak_maxneg);
  130. av_freep(&wav->peak_output);
  131. }
  132. static av_cold int peak_init_writer(AVFormatContext *s)
  133. {
  134. WAVMuxContext *wav = s->priv_data;
  135. AVCodecContext *enc = s->streams[0]->codec;
  136. if (enc->codec_id != AV_CODEC_ID_PCM_S8 &&
  137. enc->codec_id != AV_CODEC_ID_PCM_S16LE &&
  138. enc->codec_id != AV_CODEC_ID_PCM_U8 &&
  139. enc->codec_id != AV_CODEC_ID_PCM_U16LE) {
  140. av_log(s, AV_LOG_ERROR, "%s codec not supported for Peak Chunk\n",
  141. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  142. return -1;
  143. }
  144. wav->peak_bps = av_get_bits_per_sample(enc->codec_id) / 8;
  145. if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
  146. av_log(s, AV_LOG_ERROR,
  147. "Writing 16 bit peak for 8 bit audio does not make sense\n");
  148. return AVERROR(EINVAL);
  149. }
  150. wav->peak_maxpos = av_mallocz(enc->channels * sizeof(*wav->peak_maxpos));
  151. wav->peak_maxneg = av_mallocz(enc->channels * sizeof(*wav->peak_maxneg));
  152. wav->peak_output = av_malloc(PEAK_BUFFER_SIZE);
  153. if (!wav->peak_maxpos || !wav->peak_maxneg || !wav->peak_output)
  154. goto nomem;
  155. wav->peak_outbuf_size = PEAK_BUFFER_SIZE;
  156. return 0;
  157. nomem:
  158. av_log(s, AV_LOG_ERROR, "Out of memory\n");
  159. peak_free_buffers(s);
  160. return AVERROR(ENOMEM);
  161. }
  162. static void peak_write_frame(AVFormatContext *s)
  163. {
  164. WAVMuxContext *wav = s->priv_data;
  165. AVCodecContext *enc = s->streams[0]->codec;
  166. int peak_of_peaks;
  167. int c;
  168. if (!wav->peak_output)
  169. return;
  170. for (c = 0; c < enc->channels; c++) {
  171. wav->peak_maxneg[c] = -wav->peak_maxneg[c];
  172. if (wav->peak_bps == 2 && wav->peak_format == PEAK_FORMAT_UINT8) {
  173. wav->peak_maxpos[c] = wav->peak_maxpos[c] / 256;
  174. wav->peak_maxneg[c] = wav->peak_maxneg[c] / 256;
  175. }
  176. if (wav->peak_ppv == 1)
  177. wav->peak_maxpos[c] =
  178. FFMAX(wav->peak_maxpos[c], wav->peak_maxneg[c]);
  179. peak_of_peaks = FFMAX3(wav->peak_maxpos[c], wav->peak_maxneg[c],
  180. wav->peak_pop);
  181. if (peak_of_peaks > wav->peak_pop)
  182. wav->peak_pos_pop = wav->peak_num_frames;
  183. wav->peak_pop = peak_of_peaks;
  184. if (wav->peak_outbuf_size - wav->peak_outbuf_bytes <
  185. wav->peak_format * wav->peak_ppv) {
  186. wav->peak_outbuf_size += PEAK_BUFFER_SIZE;
  187. wav->peak_output = av_realloc(wav->peak_output,
  188. wav->peak_outbuf_size);
  189. if (!wav->peak_output) {
  190. av_log(s, AV_LOG_ERROR, "No memory for peak data\n");
  191. return;
  192. }
  193. }
  194. if (wav->peak_format == PEAK_FORMAT_UINT8) {
  195. wav->peak_output[wav->peak_outbuf_bytes++] =
  196. wav->peak_maxpos[c];
  197. if (wav->peak_ppv == 2) {
  198. wav->peak_output[wav->peak_outbuf_bytes++] =
  199. wav->peak_maxneg[c];
  200. }
  201. } else {
  202. AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
  203. wav->peak_maxpos[c]);
  204. wav->peak_outbuf_bytes += 2;
  205. if (wav->peak_ppv == 2) {
  206. AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
  207. wav->peak_maxneg[c]);
  208. wav->peak_outbuf_bytes += 2;
  209. }
  210. }
  211. wav->peak_maxpos[c] = 0;
  212. wav->peak_maxneg[c] = 0;
  213. }
  214. wav->peak_num_frames++;
  215. }
  216. static void peak_write_chunk(AVFormatContext *s)
  217. {
  218. WAVMuxContext *wav = s->priv_data;
  219. AVIOContext *pb = s->pb;
  220. AVCodecContext *enc = s->streams[0]->codec;
  221. int64_t peak = ff_start_tag(s->pb, "levl");
  222. int64_t now0;
  223. time_t now_secs;
  224. char timestamp[28];
  225. /* Peak frame of incomplete block at end */
  226. if (wav->peak_block_pos)
  227. peak_write_frame(s);
  228. memset(timestamp, 0, sizeof(timestamp));
  229. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  230. av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
  231. now0 = av_gettime();
  232. now_secs = now0 / 1000000;
  233. strftime(timestamp, sizeof(timestamp), "%Y:%m:%d:%H:%M:%S:", localtime(&now_secs));
  234. av_strlcatf(timestamp, sizeof(timestamp), "%03d", (int)((now0 / 1000) % 1000));
  235. }
  236. avio_wl32(pb, 1); /* version */
  237. avio_wl32(pb, wav->peak_format); /* 8 or 16 bit */
  238. avio_wl32(pb, wav->peak_ppv); /* positive and negative */
  239. avio_wl32(pb, wav->peak_block_size); /* frames per value */
  240. avio_wl32(pb, enc->channels); /* number of channels */
  241. avio_wl32(pb, wav->peak_num_frames); /* number of peak frames */
  242. avio_wl32(pb, wav->peak_pos_pop); /* audio sample frame index */
  243. avio_wl32(pb, 128); /* equal to size of header */
  244. avio_write(pb, timestamp, 28); /* ASCII time stamp */
  245. ffio_fill(pb, 0, 60);
  246. avio_write(pb, wav->peak_output, wav->peak_outbuf_bytes);
  247. ff_end_tag(pb, peak);
  248. if (!wav->data)
  249. wav->data = peak;
  250. }
  251. static int wav_write_header(AVFormatContext *s)
  252. {
  253. WAVMuxContext *wav = s->priv_data;
  254. AVIOContext *pb = s->pb;
  255. int64_t fmt;
  256. if (s->nb_streams != 1) {
  257. av_log(s, AV_LOG_ERROR, "WAVE files have exactly one stream\n");
  258. return AVERROR(EINVAL);
  259. }
  260. if (wav->rf64 == RF64_ALWAYS) {
  261. ffio_wfourcc(pb, "RF64");
  262. avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
  263. } else {
  264. ffio_wfourcc(pb, "RIFF");
  265. avio_wl32(pb, -1); /* file length */
  266. }
  267. ffio_wfourcc(pb, "WAVE");
  268. if (wav->rf64 != RF64_NEVER) {
  269. /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
  270. ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
  271. avio_wl32(pb, 28); /* chunk size */
  272. wav->ds64 = avio_tell(pb);
  273. ffio_fill(pb, 0, 28);
  274. }
  275. if (wav->write_peak != 2) {
  276. /* format header */
  277. fmt = ff_start_tag(pb, "fmt ");
  278. if (ff_put_wav_header(pb, s->streams[0]->codec, 0) < 0) {
  279. const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codec->codec_id);
  280. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  281. desc ? desc->name : "unknown");
  282. return AVERROR(ENOSYS);
  283. }
  284. ff_end_tag(pb, fmt);
  285. }
  286. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  287. && s->pb->seekable) {
  288. wav->fact_pos = ff_start_tag(pb, "fact");
  289. avio_wl32(pb, 0);
  290. ff_end_tag(pb, wav->fact_pos);
  291. }
  292. if (wav->write_bext)
  293. bwf_write_bext_chunk(s);
  294. if (wav->write_peak) {
  295. int ret;
  296. if ((ret = peak_init_writer(s)) < 0)
  297. return ret;
  298. }
  299. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  300. wav->maxpts = wav->last_duration = 0;
  301. wav->minpts = INT64_MAX;
  302. if (wav->write_peak != 2) {
  303. /* info header */
  304. ff_riff_write_info(s);
  305. /* data header */
  306. wav->data = ff_start_tag(pb, "data");
  307. }
  308. avio_flush(pb);
  309. return 0;
  310. }
  311. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  312. {
  313. AVIOContext *pb = s->pb;
  314. WAVMuxContext *wav = s->priv_data;
  315. if (wav->write_peak != 2)
  316. avio_write(pb, pkt->data, pkt->size);
  317. if (wav->write_peak) {
  318. int c = 0;
  319. int i;
  320. for (i = 0; i < pkt->size; i += wav->peak_bps) {
  321. if (wav->peak_bps == 1) {
  322. wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], *(int8_t*)(pkt->data + i));
  323. wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], *(int8_t*)(pkt->data + i));
  324. } else {
  325. wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], (int16_t)AV_RL16(pkt->data + i));
  326. wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], (int16_t)AV_RL16(pkt->data + i));
  327. }
  328. if (++c == s->streams[0]->codec->channels) {
  329. c = 0;
  330. if (++wav->peak_block_pos == wav->peak_block_size) {
  331. peak_write_frame(s);
  332. wav->peak_block_pos = 0;
  333. }
  334. }
  335. }
  336. }
  337. if(pkt->pts != AV_NOPTS_VALUE) {
  338. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  339. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  340. wav->last_duration = pkt->duration;
  341. } else
  342. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  343. return 0;
  344. }
  345. static int wav_write_trailer(AVFormatContext *s)
  346. {
  347. AVIOContext *pb = s->pb;
  348. WAVMuxContext *wav = s->priv_data;
  349. int64_t file_size, data_size;
  350. int64_t number_of_samples = 0;
  351. int rf64 = 0;
  352. avio_flush(pb);
  353. if (s->pb->seekable) {
  354. if (wav->write_peak != 2) {
  355. ff_end_tag(pb, wav->data);
  356. avio_flush(pb);
  357. }
  358. if (wav->write_peak && wav->peak_output) {
  359. peak_write_chunk(s);
  360. avio_flush(pb);
  361. }
  362. /* update file size */
  363. file_size = avio_tell(pb);
  364. data_size = file_size - wav->data;
  365. if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
  366. rf64 = 1;
  367. } else {
  368. avio_seek(pb, 4, SEEK_SET);
  369. avio_wl32(pb, (uint32_t)(file_size - 8));
  370. avio_seek(pb, file_size, SEEK_SET);
  371. avio_flush(pb);
  372. }
  373. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  374. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  375. s->streams[0]->time_base.den);
  376. if(s->streams[0]->codec->codec_tag != 0x01) {
  377. /* Update num_samps in fact chunk */
  378. avio_seek(pb, wav->fact_pos, SEEK_SET);
  379. if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
  380. rf64 = 1;
  381. avio_wl32(pb, -1);
  382. } else {
  383. avio_wl32(pb, number_of_samples);
  384. avio_seek(pb, file_size, SEEK_SET);
  385. avio_flush(pb);
  386. }
  387. }
  388. if (rf64) {
  389. /* overwrite RIFF with RF64 */
  390. avio_seek(pb, 0, SEEK_SET);
  391. ffio_wfourcc(pb, "RF64");
  392. avio_wl32(pb, -1);
  393. /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
  394. avio_seek(pb, wav->ds64 - 8, SEEK_SET);
  395. ffio_wfourcc(pb, "ds64");
  396. avio_wl32(pb, 28); /* ds64 chunk size */
  397. avio_wl64(pb, file_size - 8); /* RF64 chunk size */
  398. avio_wl64(pb, data_size); /* data chunk size */
  399. avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
  400. avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
  401. /* write -1 in data chunk size */
  402. avio_seek(pb, wav->data - 4, SEEK_SET);
  403. avio_wl32(pb, -1);
  404. avio_seek(pb, file_size, SEEK_SET);
  405. avio_flush(pb);
  406. }
  407. }
  408. if (wav->write_peak)
  409. peak_free_buffers(s);
  410. return 0;
  411. }
  412. #define OFFSET(x) offsetof(WAVMuxContext, x)
  413. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  414. static const AVOption options[] = {
  415. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  416. { "write_peak", "Write Peak Envelope chunk.", OFFSET(write_peak), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, ENC, "peak" },
  417. { "off", "Do not write peak chunk.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_OFF }, 0, 0, ENC, "peak" },
  418. { "on", "Append peak chunk after wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ON }, 0, 0, ENC, "peak" },
  419. { "only", "Write only peak chunk, omit wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ONLY }, 0, 0, ENC, "peak" },
  420. { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, "rf64" },
  421. { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, "rf64" },
  422. { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
  423. { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, "rf64" },
  424. { "peak_block_size", "Number of audio samples used to generate each peak frame.", OFFSET(peak_block_size), AV_OPT_TYPE_INT, { .i64 = 256 }, 0, 65536, ENC },
  425. { "peak_format", "The format of the peak envelope data (1: uint8, 2: uint16).", OFFSET(peak_format), AV_OPT_TYPE_INT, { .i64 = PEAK_FORMAT_UINT16 }, PEAK_FORMAT_UINT8, PEAK_FORMAT_UINT16, ENC },
  426. { "peak_ppv", "Number of peak points per peak value (1 or 2).", OFFSET(peak_ppv), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, ENC },
  427. { NULL },
  428. };
  429. static const AVClass wav_muxer_class = {
  430. .class_name = "WAV muxer",
  431. .item_name = av_default_item_name,
  432. .option = options,
  433. .version = LIBAVUTIL_VERSION_INT,
  434. };
  435. AVOutputFormat ff_wav_muxer = {
  436. .name = "wav",
  437. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  438. .mime_type = "audio/x-wav",
  439. .extensions = "wav",
  440. .priv_data_size = sizeof(WAVMuxContext),
  441. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  442. .video_codec = AV_CODEC_ID_NONE,
  443. .write_header = wav_write_header,
  444. .write_packet = wav_write_packet,
  445. .write_trailer = wav_write_trailer,
  446. .flags = AVFMT_TS_NONSTRICT,
  447. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  448. .priv_class = &wav_muxer_class,
  449. };
  450. #endif /* CONFIG_WAV_MUXER */
  451. #if CONFIG_W64_MUXER
  452. #include "w64.h"
  453. static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
  454. {
  455. *pos = avio_tell(pb);
  456. avio_write(pb, guid, 16);
  457. avio_wl64(pb, INT64_MAX);
  458. }
  459. static void end_guid(AVIOContext *pb, int64_t start)
  460. {
  461. int64_t end, pos = avio_tell(pb);
  462. end = FFALIGN(pos, 8);
  463. ffio_fill(pb, 0, end - pos);
  464. avio_seek(pb, start + 16, SEEK_SET);
  465. avio_wl64(pb, end - start);
  466. avio_seek(pb, end, SEEK_SET);
  467. }
  468. static int w64_write_header(AVFormatContext *s)
  469. {
  470. WAVMuxContext *wav = s->priv_data;
  471. AVIOContext *pb = s->pb;
  472. int64_t start;
  473. int ret;
  474. avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
  475. avio_wl64(pb, -1);
  476. avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
  477. start_guid(pb, ff_w64_guid_fmt, &start);
  478. if ((ret = ff_put_wav_header(pb, s->streams[0]->codec, 0)) < 0) {
  479. av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
  480. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  481. return ret;
  482. }
  483. end_guid(pb, start);
  484. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  485. && s->pb->seekable) {
  486. start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
  487. avio_wl64(pb, 0);
  488. end_guid(pb, wav->fact_pos);
  489. }
  490. start_guid(pb, ff_w64_guid_data, &wav->data);
  491. return 0;
  492. }
  493. static int w64_write_trailer(AVFormatContext *s)
  494. {
  495. AVIOContext *pb = s->pb;
  496. WAVMuxContext *wav = s->priv_data;
  497. int64_t file_size;
  498. if (pb->seekable) {
  499. end_guid(pb, wav->data);
  500. file_size = avio_tell(pb);
  501. avio_seek(pb, 16, SEEK_SET);
  502. avio_wl64(pb, file_size);
  503. if (s->streams[0]->codec->codec_tag != 0x01) {
  504. int64_t number_of_samples;
  505. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  506. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  507. s->streams[0]->time_base.den);
  508. avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
  509. avio_wl64(pb, number_of_samples);
  510. }
  511. avio_seek(pb, file_size, SEEK_SET);
  512. avio_flush(pb);
  513. }
  514. return 0;
  515. }
  516. AVOutputFormat ff_w64_muxer = {
  517. .name = "w64",
  518. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  519. .extensions = "w64",
  520. .priv_data_size = sizeof(WAVMuxContext),
  521. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  522. .video_codec = AV_CODEC_ID_NONE,
  523. .write_header = w64_write_header,
  524. .write_packet = wav_write_packet,
  525. .write_trailer = w64_write_trailer,
  526. .flags = AVFMT_TS_NONSTRICT,
  527. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  528. };
  529. #endif /* CONFIG_W64_MUXER */