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.

632 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 -1;
  149. }
  150. wav->peak_maxpos = av_mallocz(enc->channels * sizeof(*wav->peak_maxpos));
  151. if (!wav->peak_maxpos)
  152. goto nomem;
  153. wav->peak_maxneg = av_mallocz(enc->channels * sizeof(*wav->peak_maxneg));
  154. if (!wav->peak_maxneg)
  155. goto nomem;
  156. wav->peak_output = av_malloc(PEAK_BUFFER_SIZE);
  157. if (!wav->peak_output)
  158. goto nomem;
  159. wav->peak_outbuf_size = PEAK_BUFFER_SIZE;
  160. return 0;
  161. nomem:
  162. av_log(s, AV_LOG_ERROR, "Out of memory\n");
  163. peak_free_buffers(s);
  164. return AVERROR(ENOMEM);
  165. }
  166. static void peak_write_frame(AVFormatContext *s)
  167. {
  168. WAVMuxContext *wav = s->priv_data;
  169. AVCodecContext *enc = s->streams[0]->codec;
  170. int peak_of_peaks;
  171. int c;
  172. if (!wav->peak_output)
  173. return;
  174. for (c = 0; c < enc->channels; c++) {
  175. wav->peak_maxneg[c] = -wav->peak_maxneg[c];
  176. if (wav->peak_bps == 2 && wav->peak_format == PEAK_FORMAT_UINT8) {
  177. wav->peak_maxpos[c] = wav->peak_maxpos[c] / 256;
  178. wav->peak_maxneg[c] = wav->peak_maxneg[c] / 256;
  179. }
  180. if (wav->peak_ppv == 1)
  181. wav->peak_maxpos[c] =
  182. FFMAX(wav->peak_maxpos[c], wav->peak_maxneg[c]);
  183. peak_of_peaks = FFMAX3(wav->peak_maxpos[c], wav->peak_maxneg[c],
  184. wav->peak_pop);
  185. if (peak_of_peaks > wav->peak_pop)
  186. wav->peak_pos_pop = wav->peak_num_frames;
  187. wav->peak_pop = peak_of_peaks;
  188. if (wav->peak_outbuf_size - wav->peak_outbuf_bytes <
  189. wav->peak_format * wav->peak_ppv) {
  190. wav->peak_outbuf_size += PEAK_BUFFER_SIZE;
  191. wav->peak_output = av_realloc(wav->peak_output,
  192. wav->peak_outbuf_size);
  193. if (!wav->peak_output) {
  194. av_log(s, AV_LOG_ERROR, "No memory for peak data\n");
  195. return;
  196. }
  197. }
  198. if (wav->peak_format == PEAK_FORMAT_UINT8) {
  199. wav->peak_output[wav->peak_outbuf_bytes++] =
  200. wav->peak_maxpos[c];
  201. if (wav->peak_ppv == 2) {
  202. wav->peak_output[wav->peak_outbuf_bytes++] =
  203. wav->peak_maxneg[c];
  204. }
  205. } else {
  206. AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
  207. wav->peak_maxpos[c]);
  208. wav->peak_outbuf_bytes += 2;
  209. if (wav->peak_ppv == 2) {
  210. AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
  211. wav->peak_maxneg[c]);
  212. wav->peak_outbuf_bytes += 2;
  213. }
  214. }
  215. wav->peak_maxpos[c] = 0;
  216. wav->peak_maxneg[c] = 0;
  217. }
  218. wav->peak_num_frames++;
  219. }
  220. static void peak_write_chunk(AVFormatContext *s)
  221. {
  222. WAVMuxContext *wav = s->priv_data;
  223. AVIOContext *pb = s->pb;
  224. AVCodecContext *enc = s->streams[0]->codec;
  225. int64_t peak = ff_start_tag(s->pb, "levl");
  226. int64_t now0;
  227. time_t now_secs;
  228. char timestamp[28];
  229. /* Peak frame of incomplete block at end */
  230. if (wav->peak_block_pos)
  231. peak_write_frame(s);
  232. memset(timestamp, 0, 28);
  233. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  234. av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
  235. now0 = av_gettime();
  236. now_secs = now0 / 1000000;
  237. strftime(timestamp, 28, "%Y:%m:%d:%H:%M:%S:", localtime(&now_secs));
  238. av_strlcatf(timestamp, 28, "%03d", (int)((now0 / 1000) % 1000));
  239. }
  240. avio_wl32(pb, 1); /* version */
  241. avio_wl32(pb, wav->peak_format); /* 8 or 16 bit */
  242. avio_wl32(pb, wav->peak_ppv); /* positive and negative */
  243. avio_wl32(pb, wav->peak_block_size); /* frames per value */
  244. avio_wl32(pb, enc->channels); /* number of channels */
  245. avio_wl32(pb, wav->peak_num_frames); /* number of peak frames */
  246. avio_wl32(pb, wav->peak_pos_pop); /* audio sample frame index */
  247. avio_wl32(pb, 128); /* equal to size of header */
  248. avio_write(pb, timestamp, 28); /* ASCII time stamp */
  249. ffio_fill(pb, 0, 60);
  250. avio_write(pb, wav->peak_output, wav->peak_outbuf_bytes);
  251. ff_end_tag(pb, peak);
  252. if (!wav->data)
  253. wav->data = peak;
  254. }
  255. static int wav_write_header(AVFormatContext *s)
  256. {
  257. WAVMuxContext *wav = s->priv_data;
  258. AVIOContext *pb = s->pb;
  259. int64_t fmt;
  260. if (s->nb_streams != 1) {
  261. av_log(s, AV_LOG_ERROR, "WAVE files have exactly one stream\n");
  262. return AVERROR(EINVAL);
  263. }
  264. if (wav->rf64 == RF64_ALWAYS) {
  265. ffio_wfourcc(pb, "RF64");
  266. avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
  267. } else {
  268. ffio_wfourcc(pb, "RIFF");
  269. avio_wl32(pb, -1); /* file length */
  270. }
  271. ffio_wfourcc(pb, "WAVE");
  272. if (wav->rf64 != RF64_NEVER) {
  273. /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
  274. ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
  275. avio_wl32(pb, 28); /* chunk size */
  276. wav->ds64 = avio_tell(pb);
  277. ffio_fill(pb, 0, 28);
  278. }
  279. if (wav->write_peak != 2) {
  280. /* format header */
  281. fmt = ff_start_tag(pb, "fmt ");
  282. if (ff_put_wav_header(pb, s->streams[0]->codec, 0) < 0) {
  283. const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codec->codec_id);
  284. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  285. desc ? desc->name : "unknown");
  286. return AVERROR(ENOSYS);
  287. }
  288. ff_end_tag(pb, fmt);
  289. }
  290. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  291. && s->pb->seekable) {
  292. wav->fact_pos = ff_start_tag(pb, "fact");
  293. avio_wl32(pb, 0);
  294. ff_end_tag(pb, wav->fact_pos);
  295. }
  296. if (wav->write_bext)
  297. bwf_write_bext_chunk(s);
  298. if (wav->write_peak) {
  299. int ret;
  300. if ((ret = peak_init_writer(s)) < 0)
  301. return ret;
  302. }
  303. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  304. wav->maxpts = wav->last_duration = 0;
  305. wav->minpts = INT64_MAX;
  306. if (wav->write_peak != 2) {
  307. /* info header */
  308. ff_riff_write_info(s);
  309. /* data header */
  310. wav->data = ff_start_tag(pb, "data");
  311. }
  312. avio_flush(pb);
  313. return 0;
  314. }
  315. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  316. {
  317. AVIOContext *pb = s->pb;
  318. WAVMuxContext *wav = s->priv_data;
  319. if (wav->write_peak != 2)
  320. avio_write(pb, pkt->data, pkt->size);
  321. if (wav->write_peak) {
  322. int c = 0;
  323. int i;
  324. for (i = 0; i < pkt->size; i += wav->peak_bps) {
  325. if (wav->peak_bps == 1) {
  326. wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], *(int8_t*)(pkt->data + i));
  327. wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], *(int8_t*)(pkt->data + i));
  328. } else {
  329. wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], (int16_t)AV_RL16(pkt->data + i));
  330. wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], (int16_t)AV_RL16(pkt->data + i));
  331. }
  332. if (++c == s->streams[0]->codec->channels) {
  333. c = 0;
  334. if (++wav->peak_block_pos == wav->peak_block_size) {
  335. peak_write_frame(s);
  336. wav->peak_block_pos = 0;
  337. }
  338. }
  339. }
  340. }
  341. if(pkt->pts != AV_NOPTS_VALUE) {
  342. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  343. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  344. wav->last_duration = pkt->duration;
  345. } else
  346. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  347. return 0;
  348. }
  349. static int wav_write_trailer(AVFormatContext *s)
  350. {
  351. AVIOContext *pb = s->pb;
  352. WAVMuxContext *wav = s->priv_data;
  353. int64_t file_size, data_size;
  354. int64_t number_of_samples = 0;
  355. int rf64 = 0;
  356. avio_flush(pb);
  357. if (s->pb->seekable) {
  358. if (wav->write_peak != 2) {
  359. ff_end_tag(pb, wav->data);
  360. avio_flush(pb);
  361. }
  362. if (wav->write_peak && wav->peak_output) {
  363. peak_write_chunk(s);
  364. avio_flush(pb);
  365. }
  366. /* update file size */
  367. file_size = avio_tell(pb);
  368. data_size = file_size - wav->data;
  369. if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
  370. rf64 = 1;
  371. } else {
  372. avio_seek(pb, 4, SEEK_SET);
  373. avio_wl32(pb, (uint32_t)(file_size - 8));
  374. avio_seek(pb, file_size, SEEK_SET);
  375. avio_flush(pb);
  376. }
  377. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  378. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  379. s->streams[0]->time_base.den);
  380. if(s->streams[0]->codec->codec_tag != 0x01) {
  381. /* Update num_samps in fact chunk */
  382. avio_seek(pb, wav->fact_pos, SEEK_SET);
  383. if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
  384. rf64 = 1;
  385. avio_wl32(pb, -1);
  386. } else {
  387. avio_wl32(pb, number_of_samples);
  388. avio_seek(pb, file_size, SEEK_SET);
  389. avio_flush(pb);
  390. }
  391. }
  392. if (rf64) {
  393. /* overwrite RIFF with RF64 */
  394. avio_seek(pb, 0, SEEK_SET);
  395. ffio_wfourcc(pb, "RF64");
  396. avio_wl32(pb, -1);
  397. /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
  398. avio_seek(pb, wav->ds64 - 8, SEEK_SET);
  399. ffio_wfourcc(pb, "ds64");
  400. avio_wl32(pb, 28); /* ds64 chunk size */
  401. avio_wl64(pb, file_size - 8); /* RF64 chunk size */
  402. avio_wl64(pb, data_size); /* data chunk size */
  403. avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
  404. avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
  405. /* write -1 in data chunk size */
  406. avio_seek(pb, wav->data - 4, SEEK_SET);
  407. avio_wl32(pb, -1);
  408. avio_seek(pb, file_size, SEEK_SET);
  409. avio_flush(pb);
  410. }
  411. }
  412. if (wav->write_peak)
  413. peak_free_buffers(s);
  414. return 0;
  415. }
  416. #define OFFSET(x) offsetof(WAVMuxContext, x)
  417. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  418. static const AVOption options[] = {
  419. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  420. { "write_peak", "Write Peak Envelope chunk.", OFFSET(write_peak), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, ENC, "peak" },
  421. { "off", "Do not write peak chunk.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_OFF }, 0, 0, ENC, "peak" },
  422. { "on", "Append peak chunk after wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ON }, 0, 0, ENC, "peak" },
  423. { "only", "Write only peak chunk, omit wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ONLY }, 0, 0, ENC, "peak" },
  424. { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, "rf64" },
  425. { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, "rf64" },
  426. { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
  427. { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, "rf64" },
  428. { "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 },
  429. { "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 },
  430. { "peak_ppv", "Number of peak points per peak value (1 or 2).", OFFSET(peak_ppv), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, ENC },
  431. { NULL },
  432. };
  433. static const AVClass wav_muxer_class = {
  434. .class_name = "WAV muxer",
  435. .item_name = av_default_item_name,
  436. .option = options,
  437. .version = LIBAVUTIL_VERSION_INT,
  438. };
  439. AVOutputFormat ff_wav_muxer = {
  440. .name = "wav",
  441. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  442. .mime_type = "audio/x-wav",
  443. .extensions = "wav",
  444. .priv_data_size = sizeof(WAVMuxContext),
  445. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  446. .video_codec = AV_CODEC_ID_NONE,
  447. .write_header = wav_write_header,
  448. .write_packet = wav_write_packet,
  449. .write_trailer = wav_write_trailer,
  450. .flags = AVFMT_TS_NONSTRICT,
  451. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  452. .priv_class = &wav_muxer_class,
  453. };
  454. #endif /* CONFIG_WAV_MUXER */
  455. #if CONFIG_W64_MUXER
  456. #include "w64.h"
  457. static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
  458. {
  459. *pos = avio_tell(pb);
  460. avio_write(pb, guid, 16);
  461. avio_wl64(pb, INT64_MAX);
  462. }
  463. static void end_guid(AVIOContext *pb, int64_t start)
  464. {
  465. int64_t end, pos = avio_tell(pb);
  466. end = FFALIGN(pos, 8);
  467. ffio_fill(pb, 0, end - pos);
  468. avio_seek(pb, start + 16, SEEK_SET);
  469. avio_wl64(pb, end - start);
  470. avio_seek(pb, end, SEEK_SET);
  471. }
  472. static int w64_write_header(AVFormatContext *s)
  473. {
  474. WAVMuxContext *wav = s->priv_data;
  475. AVIOContext *pb = s->pb;
  476. int64_t start;
  477. int ret;
  478. avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
  479. avio_wl64(pb, -1);
  480. avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
  481. start_guid(pb, ff_w64_guid_fmt, &start);
  482. if ((ret = ff_put_wav_header(pb, s->streams[0]->codec, 0)) < 0) {
  483. av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
  484. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  485. return ret;
  486. }
  487. end_guid(pb, start);
  488. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  489. && s->pb->seekable) {
  490. start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
  491. avio_wl64(pb, 0);
  492. end_guid(pb, wav->fact_pos);
  493. }
  494. start_guid(pb, ff_w64_guid_data, &wav->data);
  495. return 0;
  496. }
  497. static int w64_write_trailer(AVFormatContext *s)
  498. {
  499. AVIOContext *pb = s->pb;
  500. WAVMuxContext *wav = s->priv_data;
  501. int64_t file_size;
  502. if (pb->seekable) {
  503. end_guid(pb, wav->data);
  504. file_size = avio_tell(pb);
  505. avio_seek(pb, 16, SEEK_SET);
  506. avio_wl64(pb, file_size);
  507. if (s->streams[0]->codec->codec_tag != 0x01) {
  508. int64_t number_of_samples;
  509. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  510. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  511. s->streams[0]->time_base.den);
  512. avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
  513. avio_wl64(pb, number_of_samples);
  514. }
  515. avio_seek(pb, file_size, SEEK_SET);
  516. avio_flush(pb);
  517. }
  518. return 0;
  519. }
  520. AVOutputFormat ff_w64_muxer = {
  521. .name = "w64",
  522. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  523. .extensions = "w64",
  524. .priv_data_size = sizeof(WAVMuxContext),
  525. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  526. .video_codec = AV_CODEC_ID_NONE,
  527. .write_header = w64_write_header,
  528. .write_packet = wav_write_packet,
  529. .write_trailer = w64_write_trailer,
  530. .flags = AVFMT_TS_NONSTRICT,
  531. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  532. };
  533. #endif /* CONFIG_W64_MUXER */