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.

688 lines
20KB

  1. /*
  2. * WAV muxer and demuxer
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * Sony Wave64 demuxer
  6. * RF64 demuxer
  7. * Copyright (c) 2009 Daniel Verkamp
  8. *
  9. * This file is part of Libav.
  10. *
  11. * Libav is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Libav is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Libav; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "avformat.h"
  31. #include "avio_internal.h"
  32. #include "pcm.h"
  33. #include "riff.h"
  34. #include "avio.h"
  35. #include "metadata.h"
  36. typedef struct {
  37. const AVClass *class;
  38. int64_t data;
  39. int64_t data_end;
  40. int64_t minpts;
  41. int64_t maxpts;
  42. int last_duration;
  43. int w64;
  44. int write_bext;
  45. } WAVContext;
  46. #if CONFIG_WAV_MUXER
  47. static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
  48. {
  49. AVDictionaryEntry *tag;
  50. int len = 0;
  51. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  52. len = strlen(tag->value);
  53. len = FFMIN(len, maxlen);
  54. avio_write(s->pb, tag->value, len);
  55. }
  56. ffio_fill(s->pb, 0, maxlen - len);
  57. }
  58. static void bwf_write_bext_chunk(AVFormatContext *s)
  59. {
  60. AVDictionaryEntry *tmp_tag;
  61. uint64_t time_reference = 0;
  62. int64_t bext = ff_start_tag(s->pb, "bext");
  63. bwf_write_bext_string(s, "description", 256);
  64. bwf_write_bext_string(s, "originator", 32);
  65. bwf_write_bext_string(s, "originator_reference", 32);
  66. bwf_write_bext_string(s, "origination_date", 10);
  67. bwf_write_bext_string(s, "origination_time", 8);
  68. if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
  69. time_reference = strtoll(tmp_tag->value, NULL, 10);
  70. avio_wl64(s->pb, time_reference);
  71. avio_wl16(s->pb, 1); // set version to 1
  72. if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
  73. unsigned char umidpart_str[17] = {0};
  74. int i;
  75. uint64_t umidpart;
  76. int len = strlen(tmp_tag->value+2);
  77. for (i = 0; i < len/16; i++) {
  78. memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
  79. umidpart = strtoll(umidpart_str, NULL, 16);
  80. avio_wb64(s->pb, umidpart);
  81. }
  82. ffio_fill(s->pb, 0, 64 - i*8);
  83. } else
  84. ffio_fill(s->pb, 0, 64); // zero UMID
  85. ffio_fill(s->pb, 0, 190); // Reserved
  86. if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
  87. avio_put_str(s->pb, tmp_tag->value);
  88. ff_end_tag(s->pb, bext);
  89. }
  90. static int wav_write_header(AVFormatContext *s)
  91. {
  92. WAVContext *wav = s->priv_data;
  93. AVIOContext *pb = s->pb;
  94. int64_t fmt, fact;
  95. ffio_wfourcc(pb, "RIFF");
  96. avio_wl32(pb, 0); /* file length */
  97. ffio_wfourcc(pb, "WAVE");
  98. /* format header */
  99. fmt = ff_start_tag(pb, "fmt ");
  100. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  101. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  102. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  103. return -1;
  104. }
  105. ff_end_tag(pb, fmt);
  106. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  107. && s->pb->seekable) {
  108. fact = ff_start_tag(pb, "fact");
  109. avio_wl32(pb, 0);
  110. ff_end_tag(pb, fact);
  111. }
  112. if (wav->write_bext)
  113. bwf_write_bext_chunk(s);
  114. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  115. wav->maxpts = wav->last_duration = 0;
  116. wav->minpts = INT64_MAX;
  117. /* data header */
  118. wav->data = ff_start_tag(pb, "data");
  119. avio_flush(pb);
  120. return 0;
  121. }
  122. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  123. {
  124. AVIOContext *pb = s->pb;
  125. WAVContext *wav = s->priv_data;
  126. avio_write(pb, pkt->data, pkt->size);
  127. if(pkt->pts != AV_NOPTS_VALUE) {
  128. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  129. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  130. wav->last_duration = pkt->duration;
  131. } else
  132. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  133. return 0;
  134. }
  135. static int wav_write_trailer(AVFormatContext *s)
  136. {
  137. AVIOContext *pb = s->pb;
  138. WAVContext *wav = s->priv_data;
  139. int64_t file_size;
  140. avio_flush(pb);
  141. if (s->pb->seekable) {
  142. ff_end_tag(pb, wav->data);
  143. /* update file size */
  144. file_size = avio_tell(pb);
  145. avio_seek(pb, 4, SEEK_SET);
  146. avio_wl32(pb, (uint32_t)(file_size - 8));
  147. avio_seek(pb, file_size, SEEK_SET);
  148. avio_flush(pb);
  149. if(s->streams[0]->codec->codec_tag != 0x01) {
  150. /* Update num_samps in fact chunk */
  151. int number_of_samples;
  152. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  153. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  154. s->streams[0]->time_base.den);
  155. avio_seek(pb, wav->data-12, SEEK_SET);
  156. avio_wl32(pb, number_of_samples);
  157. avio_seek(pb, file_size, SEEK_SET);
  158. avio_flush(pb);
  159. }
  160. }
  161. return 0;
  162. }
  163. #define OFFSET(x) offsetof(WAVContext, x)
  164. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  165. static const AVOption options[] = {
  166. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
  167. { NULL },
  168. };
  169. static const AVClass wav_muxer_class = {
  170. .class_name = "WAV muxer",
  171. .item_name = av_default_item_name,
  172. .option = options,
  173. .version = LIBAVUTIL_VERSION_INT,
  174. };
  175. AVOutputFormat ff_wav_muxer = {
  176. .name = "wav",
  177. .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
  178. .mime_type = "audio/x-wav",
  179. .extensions = "wav",
  180. .priv_data_size = sizeof(WAVContext),
  181. .audio_codec = CODEC_ID_PCM_S16LE,
  182. .video_codec = CODEC_ID_NONE,
  183. .write_header = wav_write_header,
  184. .write_packet = wav_write_packet,
  185. .write_trailer = wav_write_trailer,
  186. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  187. .priv_class = &wav_muxer_class,
  188. };
  189. #endif /* CONFIG_WAV_MUXER */
  190. #if CONFIG_WAV_DEMUXER
  191. static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
  192. {
  193. *tag = avio_rl32(pb);
  194. return avio_rl32(pb);
  195. }
  196. /* return the size of the found tag */
  197. static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
  198. {
  199. unsigned int tag;
  200. int64_t size;
  201. for (;;) {
  202. if (pb->eof_reached)
  203. return -1;
  204. size = next_tag(pb, &tag);
  205. if (tag == tag1)
  206. break;
  207. avio_skip(pb, size);
  208. }
  209. return size;
  210. }
  211. static int wav_probe(AVProbeData *p)
  212. {
  213. /* check file header */
  214. if (p->buf_size <= 32)
  215. return 0;
  216. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  217. if (!memcmp(p->buf, "RIFF", 4))
  218. /*
  219. Since ACT demuxer has standard WAV header at top of it's own,
  220. returning score is decreased to avoid probe conflict
  221. between ACT and WAV.
  222. */
  223. return AVPROBE_SCORE_MAX - 1;
  224. else if (!memcmp(p->buf, "RF64", 4) &&
  225. !memcmp(p->buf + 12, "ds64", 4))
  226. return AVPROBE_SCORE_MAX;
  227. }
  228. return 0;
  229. }
  230. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  231. {
  232. AVIOContext *pb = s->pb;
  233. int ret;
  234. /* parse fmt header */
  235. *st = avformat_new_stream(s, NULL);
  236. if (!*st)
  237. return AVERROR(ENOMEM);
  238. ret = ff_get_wav_header(pb, (*st)->codec, size);
  239. if (ret < 0)
  240. return ret;
  241. (*st)->need_parsing = AVSTREAM_PARSE_FULL;
  242. av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  243. return 0;
  244. }
  245. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  246. int length)
  247. {
  248. char temp[257];
  249. int ret;
  250. av_assert0(length <= sizeof(temp));
  251. if ((ret = avio_read(s->pb, temp, length)) < 0)
  252. return ret;
  253. temp[length] = 0;
  254. if (strlen(temp))
  255. return av_dict_set(&s->metadata, key, temp, 0);
  256. return 0;
  257. }
  258. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  259. {
  260. char temp[131], *coding_history;
  261. int ret, x;
  262. uint64_t time_reference;
  263. int64_t umid_parts[8], umid_mask = 0;
  264. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  265. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  266. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  267. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  268. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  269. return ret;
  270. time_reference = avio_rl64(s->pb);
  271. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  272. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  273. return ret;
  274. /* check if version is >= 1, in which case an UMID may be present */
  275. if (avio_rl16(s->pb) >= 1) {
  276. for (x = 0; x < 8; x++)
  277. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  278. if (umid_mask) {
  279. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  280. if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
  281. /* basic UMID */
  282. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  283. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
  284. } else {
  285. /* extended UMID */
  286. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  287. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  288. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
  289. umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
  290. }
  291. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  292. return ret;
  293. }
  294. avio_skip(s->pb, 190);
  295. } else
  296. avio_skip(s->pb, 254);
  297. if (size > 602) {
  298. /* CodingHistory present */
  299. size -= 602;
  300. if (!(coding_history = av_malloc(size+1)))
  301. return AVERROR(ENOMEM);
  302. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  303. return ret;
  304. coding_history[size] = 0;
  305. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  306. AV_DICT_DONT_STRDUP_VAL)) < 0)
  307. return ret;
  308. }
  309. return 0;
  310. }
  311. static const AVMetadataConv wav_metadata_conv[] = {
  312. {"description", "comment" },
  313. {"originator", "encoded_by" },
  314. {"origination_date", "date" },
  315. {"origination_time", "creation_time"},
  316. {0},
  317. };
  318. /* wav input */
  319. static int wav_read_header(AVFormatContext *s,
  320. AVFormatParameters *ap)
  321. {
  322. int64_t size, av_uninit(data_size);
  323. int64_t sample_count=0;
  324. int rf64;
  325. uint32_t tag, list_type;
  326. AVIOContext *pb = s->pb;
  327. AVStream *st;
  328. WAVContext *wav = s->priv_data;
  329. int ret, got_fmt = 0;
  330. int64_t next_tag_ofs, data_ofs = -1;
  331. /* check RIFF header */
  332. tag = avio_rl32(pb);
  333. rf64 = tag == MKTAG('R', 'F', '6', '4');
  334. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  335. return -1;
  336. avio_rl32(pb); /* file size */
  337. tag = avio_rl32(pb);
  338. if (tag != MKTAG('W', 'A', 'V', 'E'))
  339. return -1;
  340. if (rf64) {
  341. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  342. return -1;
  343. size = avio_rl32(pb);
  344. if (size < 16)
  345. return -1;
  346. avio_rl64(pb); /* RIFF size */
  347. data_size = avio_rl64(pb);
  348. sample_count = avio_rl64(pb);
  349. if (data_size < 0 || sample_count < 0) {
  350. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  351. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  352. data_size, sample_count);
  353. return AVERROR_INVALIDDATA;
  354. }
  355. avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
  356. }
  357. for (;;) {
  358. size = next_tag(pb, &tag);
  359. next_tag_ofs = avio_tell(pb) + size;
  360. if (pb->eof_reached)
  361. break;
  362. switch (tag) {
  363. case MKTAG('f', 'm', 't', ' '):
  364. /* only parse the first 'fmt ' tag found */
  365. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
  366. return ret;
  367. } else if (got_fmt)
  368. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  369. got_fmt = 1;
  370. break;
  371. case MKTAG('d', 'a', 't', 'a'):
  372. if (!got_fmt) {
  373. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
  374. return AVERROR_INVALIDDATA;
  375. }
  376. if (rf64) {
  377. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  378. } else {
  379. data_size = size;
  380. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  381. }
  382. data_ofs = avio_tell(pb);
  383. /* don't look for footer metadata if we can't seek or if we don't
  384. * know where the data tag ends
  385. */
  386. if (!pb->seekable || (!rf64 && !size))
  387. goto break_loop;
  388. break;
  389. case MKTAG('f','a','c','t'):
  390. if (!sample_count)
  391. sample_count = avio_rl32(pb);
  392. break;
  393. case MKTAG('b','e','x','t'):
  394. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  395. return ret;
  396. break;
  397. case MKTAG('L', 'I', 'S', 'T'):
  398. list_type = avio_rl32(pb);
  399. if (size <= 4) {
  400. av_log(s, AV_LOG_ERROR, "too short LIST");
  401. return AVERROR_INVALIDDATA;
  402. }
  403. switch (list_type) {
  404. case MKTAG('I', 'N', 'F', 'O'):
  405. if ((ret = ff_read_riff_info(s, size - 4)) < 0)
  406. return ret;
  407. }
  408. break;
  409. }
  410. /* seek to next tag unless we know that we'll run into EOF */
  411. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  412. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  413. break;
  414. }
  415. }
  416. break_loop:
  417. if (data_ofs < 0) {
  418. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  419. return AVERROR_INVALIDDATA;
  420. }
  421. avio_seek(pb, data_ofs, SEEK_SET);
  422. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
  423. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  424. if (sample_count)
  425. st->duration = sample_count;
  426. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  427. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  428. return 0;
  429. }
  430. /** Find chunk with w64 GUID by skipping over other chunks
  431. * @return the size of the found chunk
  432. */
  433. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  434. {
  435. uint8_t guid[16];
  436. int64_t size;
  437. while (!pb->eof_reached) {
  438. avio_read(pb, guid, 16);
  439. size = avio_rl64(pb);
  440. if (size <= 24)
  441. return -1;
  442. if (!memcmp(guid, guid1, 16))
  443. return size;
  444. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  445. }
  446. return -1;
  447. }
  448. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  449. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  450. #define MAX_SIZE 4096
  451. static int wav_read_packet(AVFormatContext *s,
  452. AVPacket *pkt)
  453. {
  454. int ret, size;
  455. int64_t left;
  456. AVStream *st;
  457. WAVContext *wav = s->priv_data;
  458. st = s->streams[0];
  459. left = wav->data_end - avio_tell(s->pb);
  460. if (left <= 0){
  461. if (CONFIG_W64_DEMUXER && wav->w64)
  462. left = find_guid(s->pb, guid_data) - 24;
  463. else
  464. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  465. if (left < 0)
  466. return AVERROR_EOF;
  467. wav->data_end= avio_tell(s->pb) + left;
  468. }
  469. size = MAX_SIZE;
  470. if (st->codec->block_align > 1) {
  471. if (size < st->codec->block_align)
  472. size = st->codec->block_align;
  473. size = (size / st->codec->block_align) * st->codec->block_align;
  474. }
  475. size = FFMIN(size, left);
  476. ret = av_get_packet(s->pb, pkt, size);
  477. if (ret < 0)
  478. return ret;
  479. pkt->stream_index = 0;
  480. return ret;
  481. }
  482. static int wav_read_seek(AVFormatContext *s,
  483. int stream_index, int64_t timestamp, int flags)
  484. {
  485. AVStream *st;
  486. st = s->streams[0];
  487. switch (st->codec->codec_id) {
  488. case CODEC_ID_MP2:
  489. case CODEC_ID_MP3:
  490. case CODEC_ID_AC3:
  491. case CODEC_ID_DTS:
  492. /* use generic seeking with dynamically generated indexes */
  493. return -1;
  494. default:
  495. break;
  496. }
  497. return pcm_read_seek(s, stream_index, timestamp, flags);
  498. }
  499. AVInputFormat ff_wav_demuxer = {
  500. .name = "wav",
  501. .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
  502. .priv_data_size = sizeof(WAVContext),
  503. .read_probe = wav_probe,
  504. .read_header = wav_read_header,
  505. .read_packet = wav_read_packet,
  506. .read_seek = wav_read_seek,
  507. .flags= AVFMT_GENERIC_INDEX,
  508. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  509. };
  510. #endif /* CONFIG_WAV_DEMUXER */
  511. #if CONFIG_W64_DEMUXER
  512. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  513. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  514. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  515. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  516. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  517. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  518. static int w64_probe(AVProbeData *p)
  519. {
  520. if (p->buf_size <= 40)
  521. return 0;
  522. if (!memcmp(p->buf, guid_riff, 16) &&
  523. !memcmp(p->buf + 24, guid_wave, 16))
  524. return AVPROBE_SCORE_MAX;
  525. else
  526. return 0;
  527. }
  528. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  529. {
  530. int64_t size;
  531. AVIOContext *pb = s->pb;
  532. WAVContext *wav = s->priv_data;
  533. AVStream *st;
  534. uint8_t guid[16];
  535. int ret;
  536. avio_read(pb, guid, 16);
  537. if (memcmp(guid, guid_riff, 16))
  538. return -1;
  539. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  540. return -1;
  541. avio_read(pb, guid, 16);
  542. if (memcmp(guid, guid_wave, 16)) {
  543. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  544. return -1;
  545. }
  546. size = find_guid(pb, guid_fmt);
  547. if (size < 0) {
  548. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  549. return -1;
  550. }
  551. st = avformat_new_stream(s, NULL);
  552. if (!st)
  553. return AVERROR(ENOMEM);
  554. /* subtract chunk header size - normal wav file doesn't count it */
  555. ret = ff_get_wav_header(pb, st->codec, size - 24);
  556. if (ret < 0)
  557. return ret;
  558. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  559. st->need_parsing = AVSTREAM_PARSE_FULL;
  560. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  561. size = find_guid(pb, guid_data);
  562. if (size < 0) {
  563. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  564. return -1;
  565. }
  566. wav->data_end = avio_tell(pb) + size - 24;
  567. wav->w64 = 1;
  568. return 0;
  569. }
  570. AVInputFormat ff_w64_demuxer = {
  571. .name = "w64",
  572. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  573. .priv_data_size = sizeof(WAVContext),
  574. .read_probe = w64_probe,
  575. .read_header = w64_read_header,
  576. .read_packet = wav_read_packet,
  577. .read_seek = wav_read_seek,
  578. .flags = AVFMT_GENERIC_INDEX,
  579. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  580. };
  581. #endif /* CONFIG_W64_DEMUXER */