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.

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