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.

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