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.

706 lines
21KB

  1. /*
  2. * WAV 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 FFmpeg.
  10. *
  11. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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.h"
  32. #include "avio_internal.h"
  33. #include "internal.h"
  34. #include "metadata.h"
  35. #include "pcm.h"
  36. #include "riff.h"
  37. #include "w64.h"
  38. #include "spdif.h"
  39. typedef struct WAVDemuxContext {
  40. const AVClass *class;
  41. int64_t data_end;
  42. int w64;
  43. int64_t smv_data_ofs;
  44. int smv_block_size;
  45. int smv_frames_per_jpeg;
  46. int smv_block;
  47. int smv_last_stream;
  48. int smv_eof;
  49. int audio_eof;
  50. int ignore_length;
  51. int spdif;
  52. } WAVDemuxContext;
  53. #if CONFIG_WAV_DEMUXER
  54. static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
  55. {
  56. *tag = avio_rl32(pb);
  57. return avio_rl32(pb);
  58. }
  59. /* RIFF chunks are always on a even offset. */
  60. static int64_t wav_seek_tag(AVIOContext *s, int64_t offset, int whence)
  61. {
  62. offset += offset < INT64_MAX && offset & 1;
  63. return avio_seek(s, offset, whence);
  64. }
  65. /* return the size of the found tag */
  66. static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
  67. {
  68. unsigned int tag;
  69. int64_t size;
  70. for (;;) {
  71. if (url_feof(pb))
  72. return AVERROR_EOF;
  73. size = next_tag(pb, &tag);
  74. if (tag == tag1)
  75. break;
  76. wav_seek_tag(pb, size, SEEK_CUR);
  77. }
  78. return size;
  79. }
  80. static int wav_probe(AVProbeData *p)
  81. {
  82. /* check file header */
  83. if (p->buf_size <= 32)
  84. return 0;
  85. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  86. if (!memcmp(p->buf, "RIFF", 4))
  87. /* Since the ACT demuxer has a standard WAV header at the top of
  88. * its own, the returned score is decreased to avoid a probe
  89. * conflict between ACT and WAV. */
  90. return AVPROBE_SCORE_MAX - 1;
  91. else if (!memcmp(p->buf, "RF64", 4) &&
  92. !memcmp(p->buf + 12, "ds64", 4))
  93. return AVPROBE_SCORE_MAX;
  94. }
  95. return 0;
  96. }
  97. static void handle_stream_probing(AVStream *st)
  98. {
  99. if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
  100. st->request_probe = AVPROBE_SCORE_EXTENSION;
  101. st->probe_packets = FFMIN(st->probe_packets, 4);
  102. }
  103. }
  104. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  105. {
  106. AVIOContext *pb = s->pb;
  107. int ret;
  108. /* parse fmt header */
  109. *st = avformat_new_stream(s, NULL);
  110. if (!*st)
  111. return AVERROR(ENOMEM);
  112. ret = ff_get_wav_header(pb, (*st)->codec, size);
  113. if (ret < 0)
  114. return ret;
  115. handle_stream_probing(*st);
  116. (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  117. avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  118. return 0;
  119. }
  120. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  121. int length)
  122. {
  123. char temp[257];
  124. int ret;
  125. av_assert0(length <= sizeof(temp));
  126. if ((ret = avio_read(s->pb, temp, length)) < 0)
  127. return ret;
  128. temp[length] = 0;
  129. if (strlen(temp))
  130. return av_dict_set(&s->metadata, key, temp, 0);
  131. return 0;
  132. }
  133. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  134. {
  135. char temp[131], *coding_history;
  136. int ret, x;
  137. uint64_t time_reference;
  138. int64_t umid_parts[8], umid_mask = 0;
  139. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  140. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  141. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  142. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  143. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  144. return ret;
  145. time_reference = avio_rl64(s->pb);
  146. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  147. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  148. return ret;
  149. /* check if version is >= 1, in which case an UMID may be present */
  150. if (avio_rl16(s->pb) >= 1) {
  151. for (x = 0; x < 8; x++)
  152. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  153. if (umid_mask) {
  154. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  155. if (umid_parts[4] == 0 && umid_parts[5] == 0 &&
  156. umid_parts[6] == 0 && umid_parts[7] == 0) {
  157. /* basic UMID */
  158. snprintf(temp, sizeof(temp),
  159. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  160. umid_parts[0], umid_parts[1],
  161. umid_parts[2], umid_parts[3]);
  162. } else {
  163. /* extended UMID */
  164. snprintf(temp, sizeof(temp),
  165. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  166. "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  167. umid_parts[0], umid_parts[1],
  168. umid_parts[2], umid_parts[3],
  169. umid_parts[4], umid_parts[5],
  170. umid_parts[6], umid_parts[7]);
  171. }
  172. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  173. return ret;
  174. }
  175. avio_skip(s->pb, 190);
  176. } else
  177. avio_skip(s->pb, 254);
  178. if (size > 602) {
  179. /* CodingHistory present */
  180. size -= 602;
  181. if (!(coding_history = av_malloc(size + 1)))
  182. return AVERROR(ENOMEM);
  183. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  184. return ret;
  185. coding_history[size] = 0;
  186. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  187. AV_DICT_DONT_STRDUP_VAL)) < 0)
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static const AVMetadataConv wav_metadata_conv[] = {
  193. { "description", "comment" },
  194. { "originator", "encoded_by" },
  195. { "origination_date", "date" },
  196. { "origination_time", "creation_time" },
  197. { 0 },
  198. };
  199. /* wav input */
  200. static int wav_read_header(AVFormatContext *s)
  201. {
  202. int64_t size, av_uninit(data_size);
  203. int64_t sample_count = 0;
  204. int rf64;
  205. uint32_t tag;
  206. AVIOContext *pb = s->pb;
  207. AVStream *st = NULL;
  208. WAVDemuxContext *wav = s->priv_data;
  209. int ret, got_fmt = 0;
  210. int64_t next_tag_ofs, data_ofs = -1;
  211. wav->smv_data_ofs = -1;
  212. /* check RIFF header */
  213. tag = avio_rl32(pb);
  214. rf64 = tag == MKTAG('R', 'F', '6', '4');
  215. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  216. return AVERROR_INVALIDDATA;
  217. avio_rl32(pb); /* file size */
  218. tag = avio_rl32(pb);
  219. if (tag != MKTAG('W', 'A', 'V', 'E'))
  220. return AVERROR_INVALIDDATA;
  221. if (rf64) {
  222. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  223. return AVERROR_INVALIDDATA;
  224. size = avio_rl32(pb);
  225. if (size < 24)
  226. return AVERROR_INVALIDDATA;
  227. avio_rl64(pb); /* RIFF size */
  228. data_size = avio_rl64(pb);
  229. sample_count = avio_rl64(pb);
  230. if (data_size < 0 || sample_count < 0) {
  231. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  232. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  233. data_size, sample_count);
  234. return AVERROR_INVALIDDATA;
  235. }
  236. avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
  237. }
  238. for (;;) {
  239. AVStream *vst;
  240. size = next_tag(pb, &tag);
  241. next_tag_ofs = avio_tell(pb) + size;
  242. if (url_feof(pb))
  243. break;
  244. switch (tag) {
  245. case MKTAG('f', 'm', 't', ' '):
  246. /* only parse the first 'fmt ' tag found */
  247. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
  248. return ret;
  249. } else if (got_fmt)
  250. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  251. got_fmt = 1;
  252. break;
  253. case MKTAG('d', 'a', 't', 'a'):
  254. if (!got_fmt) {
  255. av_log(s, AV_LOG_ERROR,
  256. "found no 'fmt ' tag before the 'data' tag\n");
  257. return AVERROR_INVALIDDATA;
  258. }
  259. if (rf64) {
  260. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  261. } else {
  262. data_size = size;
  263. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  264. }
  265. data_ofs = avio_tell(pb);
  266. /* don't look for footer metadata if we can't seek or if we don't
  267. * know where the data tag ends
  268. */
  269. if (!pb->seekable || (!rf64 && !size))
  270. goto break_loop;
  271. break;
  272. case MKTAG('f', 'a', 'c', 't'):
  273. if (!sample_count)
  274. sample_count = avio_rl32(pb);
  275. break;
  276. case MKTAG('b', 'e', 'x', 't'):
  277. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  278. return ret;
  279. break;
  280. case MKTAG('S','M','V','0'):
  281. if (!got_fmt) {
  282. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
  283. return AVERROR_INVALIDDATA;
  284. }
  285. // SMV file, a wav file with video appended.
  286. if (size != MKTAG('0','2','0','0')) {
  287. av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
  288. goto break_loop;
  289. }
  290. av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
  291. vst = avformat_new_stream(s, NULL);
  292. if (!vst)
  293. return AVERROR(ENOMEM);
  294. avio_r8(pb);
  295. vst->id = 1;
  296. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  297. vst->codec->codec_id = AV_CODEC_ID_MJPEG;
  298. vst->codec->width = avio_rl24(pb);
  299. vst->codec->height = avio_rl24(pb);
  300. size = avio_rl24(pb);
  301. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  302. avio_rl24(pb);
  303. wav->smv_block_size = avio_rl24(pb);
  304. avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
  305. vst->duration = avio_rl24(pb);
  306. avio_rl24(pb);
  307. avio_rl24(pb);
  308. wav->smv_frames_per_jpeg = avio_rl24(pb);
  309. goto break_loop;
  310. case MKTAG('L', 'I', 'S', 'T'):
  311. if (size < 4) {
  312. av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
  313. return AVERROR_INVALIDDATA;
  314. }
  315. switch (avio_rl32(pb)) {
  316. case MKTAG('I', 'N', 'F', 'O'):
  317. ff_read_riff_info(s, size - 4);
  318. }
  319. break;
  320. }
  321. /* seek to next tag unless we know that we'll run into EOF */
  322. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  323. wav_seek_tag(pb, next_tag_ofs, SEEK_SET) < 0) {
  324. break;
  325. }
  326. }
  327. break_loop:
  328. if (data_ofs < 0) {
  329. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  330. return AVERROR_INVALIDDATA;
  331. }
  332. avio_seek(pb, data_ofs, SEEK_SET);
  333. if (!sample_count && st->codec->channels &&
  334. av_get_bits_per_sample(st->codec->codec_id) && wav->data_end <= avio_size(pb))
  335. sample_count = (data_size << 3) /
  336. (st->codec->channels *
  337. (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  338. if (sample_count)
  339. st->duration = sample_count;
  340. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  341. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  342. return 0;
  343. }
  344. /**
  345. * Find chunk with w64 GUID by skipping over other chunks.
  346. * @return the size of the found chunk
  347. */
  348. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  349. {
  350. uint8_t guid[16];
  351. int64_t size;
  352. while (!url_feof(pb)) {
  353. avio_read(pb, guid, 16);
  354. size = avio_rl64(pb);
  355. if (size <= 24)
  356. return AVERROR_INVALIDDATA;
  357. if (!memcmp(guid, guid1, 16))
  358. return size;
  359. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  360. }
  361. return AVERROR_EOF;
  362. }
  363. #define MAX_SIZE 4096
  364. static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
  365. {
  366. int ret, size;
  367. int64_t left;
  368. AVStream *st;
  369. WAVDemuxContext *wav = s->priv_data;
  370. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 0 &&
  371. s->streams[0]->codec->codec_tag == 1) {
  372. enum AVCodecID codec;
  373. ret = ff_spdif_probe(s->pb->buffer, s->pb->buf_end - s->pb->buffer,
  374. &codec);
  375. if (ret > AVPROBE_SCORE_EXTENSION) {
  376. s->streams[0]->codec->codec_id = codec;
  377. wav->spdif = 1;
  378. } else {
  379. wav->spdif = -1;
  380. }
  381. }
  382. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
  383. return ff_spdif_read_packet(s, pkt);
  384. if (wav->smv_data_ofs > 0) {
  385. int64_t audio_dts, video_dts;
  386. smv_retry:
  387. audio_dts = s->streams[0]->cur_dts;
  388. video_dts = s->streams[1]->cur_dts;
  389. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  390. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  391. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  392. wav->smv_last_stream = video_dts >= audio_dts;
  393. }
  394. wav->smv_last_stream = !wav->smv_last_stream;
  395. wav->smv_last_stream |= wav->audio_eof;
  396. wav->smv_last_stream &= !wav->smv_eof;
  397. if (wav->smv_last_stream) {
  398. uint64_t old_pos = avio_tell(s->pb);
  399. uint64_t new_pos = wav->smv_data_ofs +
  400. wav->smv_block * wav->smv_block_size;
  401. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  402. ret = AVERROR_EOF;
  403. goto smv_out;
  404. }
  405. size = avio_rl24(s->pb);
  406. ret = av_get_packet(s->pb, pkt, size);
  407. if (ret < 0)
  408. goto smv_out;
  409. pkt->pos -= 3;
  410. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  411. wav->smv_block++;
  412. pkt->stream_index = 1;
  413. smv_out:
  414. avio_seek(s->pb, old_pos, SEEK_SET);
  415. if (ret == AVERROR_EOF) {
  416. wav->smv_eof = 1;
  417. goto smv_retry;
  418. }
  419. return ret;
  420. }
  421. }
  422. st = s->streams[0];
  423. left = wav->data_end - avio_tell(s->pb);
  424. if (wav->ignore_length)
  425. left = INT_MAX;
  426. if (left <= 0) {
  427. if (CONFIG_W64_DEMUXER && wav->w64)
  428. left = find_guid(s->pb, ff_w64_guid_data) - 24;
  429. else
  430. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  431. if (left < 0) {
  432. wav->audio_eof = 1;
  433. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  434. goto smv_retry;
  435. return AVERROR_EOF;
  436. }
  437. wav->data_end = avio_tell(s->pb) + left;
  438. }
  439. size = MAX_SIZE;
  440. if (st->codec->block_align > 1) {
  441. if (size < st->codec->block_align)
  442. size = st->codec->block_align;
  443. size = (size / st->codec->block_align) * st->codec->block_align;
  444. }
  445. size = FFMIN(size, left);
  446. ret = av_get_packet(s->pb, pkt, size);
  447. if (ret < 0)
  448. return ret;
  449. pkt->stream_index = 0;
  450. return ret;
  451. }
  452. static int wav_read_seek(AVFormatContext *s,
  453. int stream_index, int64_t timestamp, int flags)
  454. {
  455. WAVDemuxContext *wav = s->priv_data;
  456. AVStream *st;
  457. wav->smv_eof = 0;
  458. wav->audio_eof = 0;
  459. if (wav->smv_data_ofs > 0) {
  460. int64_t smv_timestamp = timestamp;
  461. if (stream_index == 0)
  462. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  463. else
  464. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  465. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  466. }
  467. st = s->streams[0];
  468. switch (st->codec->codec_id) {
  469. case AV_CODEC_ID_MP2:
  470. case AV_CODEC_ID_MP3:
  471. case AV_CODEC_ID_AC3:
  472. case AV_CODEC_ID_DTS:
  473. /* use generic seeking with dynamically generated indexes */
  474. return -1;
  475. default:
  476. break;
  477. }
  478. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  479. }
  480. #define OFFSET(x) offsetof(WAVDemuxContext, x)
  481. #define DEC AV_OPT_FLAG_DECODING_PARAM
  482. static const AVOption demux_options[] = {
  483. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  484. { NULL },
  485. };
  486. static const AVClass wav_demuxer_class = {
  487. .class_name = "WAV demuxer",
  488. .item_name = av_default_item_name,
  489. .option = demux_options,
  490. .version = LIBAVUTIL_VERSION_INT,
  491. };
  492. AVInputFormat ff_wav_demuxer = {
  493. .name = "wav",
  494. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  495. .priv_data_size = sizeof(WAVDemuxContext),
  496. .read_probe = wav_probe,
  497. .read_header = wav_read_header,
  498. .read_packet = wav_read_packet,
  499. .read_seek = wav_read_seek,
  500. .flags = AVFMT_GENERIC_INDEX,
  501. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  502. .priv_class = &wav_demuxer_class,
  503. };
  504. #endif /* CONFIG_WAV_DEMUXER */
  505. #if CONFIG_W64_DEMUXER
  506. static int w64_probe(AVProbeData *p)
  507. {
  508. if (p->buf_size <= 40)
  509. return 0;
  510. if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
  511. !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
  512. return AVPROBE_SCORE_MAX;
  513. else
  514. return 0;
  515. }
  516. static int w64_read_header(AVFormatContext *s)
  517. {
  518. int64_t size, data_ofs = 0;
  519. AVIOContext *pb = s->pb;
  520. WAVDemuxContext *wav = s->priv_data;
  521. AVStream *st;
  522. uint8_t guid[16];
  523. int ret;
  524. avio_read(pb, guid, 16);
  525. if (memcmp(guid, ff_w64_guid_riff, 16))
  526. return AVERROR_INVALIDDATA;
  527. /* riff + wave + fmt + sizes */
  528. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
  529. return AVERROR_INVALIDDATA;
  530. avio_read(pb, guid, 16);
  531. if (memcmp(guid, ff_w64_guid_wave, 16)) {
  532. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  533. return AVERROR_INVALIDDATA;
  534. }
  535. wav->w64 = 1;
  536. st = avformat_new_stream(s, NULL);
  537. if (!st)
  538. return AVERROR(ENOMEM);
  539. while (!url_feof(pb)) {
  540. if (avio_read(pb, guid, 16) != 16)
  541. break;
  542. size = avio_rl64(pb);
  543. if (size <= 24 || INT64_MAX - size < avio_tell(pb))
  544. return AVERROR_INVALIDDATA;
  545. if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
  546. /* subtract chunk header size - normal wav file doesn't count it */
  547. ret = ff_get_wav_header(pb, st->codec, size - 24);
  548. if (ret < 0)
  549. return ret;
  550. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  551. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  552. } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
  553. int64_t samples;
  554. samples = avio_rl64(pb);
  555. if (samples > 0)
  556. st->duration = samples;
  557. } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
  558. wav->data_end = avio_tell(pb) + size - 24;
  559. data_ofs = avio_tell(pb);
  560. if (!pb->seekable)
  561. break;
  562. avio_skip(pb, size - 24);
  563. } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
  564. int64_t start, end, cur;
  565. uint32_t count, chunk_size, i;
  566. start = avio_tell(pb);
  567. end = start + size;
  568. count = avio_rl32(pb);
  569. for (i = 0; i < count; i++) {
  570. char chunk_key[5], *value;
  571. if (url_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
  572. break;
  573. chunk_key[4] = 0;
  574. avio_read(pb, chunk_key, 4);
  575. chunk_size = avio_rl32(pb);
  576. value = av_mallocz(chunk_size + 1);
  577. if (!value)
  578. return AVERROR(ENOMEM);
  579. ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
  580. avio_skip(pb, chunk_size - ret);
  581. av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
  582. }
  583. avio_skip(pb, end - avio_tell(pb));
  584. } else {
  585. av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
  586. avio_skip(pb, size - 24);
  587. }
  588. }
  589. if (!data_ofs)
  590. return AVERROR_EOF;
  591. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  592. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  593. handle_stream_probing(st);
  594. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  595. avio_seek(pb, data_ofs, SEEK_SET);
  596. return 0;
  597. }
  598. AVInputFormat ff_w64_demuxer = {
  599. .name = "w64",
  600. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  601. .priv_data_size = sizeof(WAVDemuxContext),
  602. .read_probe = w64_probe,
  603. .read_header = w64_read_header,
  604. .read_packet = wav_read_packet,
  605. .read_seek = wav_read_seek,
  606. .flags = AVFMT_GENERIC_INDEX,
  607. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  608. };
  609. #endif /* CONFIG_W64_DEMUXER */