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.

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