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.

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