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.

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