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