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.

691 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 + (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. /*
  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. /* skip padding byte */
  311. next_tag_ofs += (next_tag_ofs < INT64_MAX && next_tag_ofs & 1);
  312. /* seek to next tag unless we know that we'll run into EOF */
  313. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  314. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  315. break;
  316. }
  317. }
  318. break_loop:
  319. if (data_ofs < 0) {
  320. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  321. return AVERROR_INVALIDDATA;
  322. }
  323. avio_seek(pb, data_ofs, SEEK_SET);
  324. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id) && wav->data_end <= avio_size(pb))
  325. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  326. if (sample_count)
  327. st->duration = sample_count;
  328. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  329. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  330. return 0;
  331. }
  332. /** Find chunk with w64 GUID by skipping over other chunks
  333. * @return the size of the found chunk
  334. */
  335. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  336. {
  337. uint8_t guid[16];
  338. int64_t size;
  339. while (!url_feof(pb)) {
  340. avio_read(pb, guid, 16);
  341. size = avio_rl64(pb);
  342. if (size <= 24)
  343. return -1;
  344. if (!memcmp(guid, guid1, 16))
  345. return size;
  346. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  347. }
  348. return -1;
  349. }
  350. #define MAX_SIZE 4096
  351. static int wav_read_packet(AVFormatContext *s,
  352. AVPacket *pkt)
  353. {
  354. int ret, size;
  355. int64_t left;
  356. AVStream *st;
  357. WAVDemuxContext *wav = s->priv_data;
  358. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 0 &&
  359. s->streams[0]->codec->codec_tag == 1) {
  360. enum AVCodecID codec;
  361. ret = ff_spdif_probe(s->pb->buffer, s->pb->buf_end - s->pb->buffer,
  362. &codec);
  363. if (ret > AVPROBE_SCORE_MAX / 2) {
  364. s->streams[0]->codec->codec_id = codec;
  365. wav->spdif = 1;
  366. } else {
  367. wav->spdif = -1;
  368. }
  369. }
  370. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
  371. return ff_spdif_read_packet(s, pkt);
  372. if (wav->smv_data_ofs > 0) {
  373. int64_t audio_dts, video_dts;
  374. smv_retry:
  375. audio_dts = s->streams[0]->cur_dts;
  376. video_dts = s->streams[1]->cur_dts;
  377. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  378. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  379. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  380. wav->smv_last_stream = video_dts >= audio_dts;
  381. }
  382. wav->smv_last_stream = !wav->smv_last_stream;
  383. wav->smv_last_stream |= wav->audio_eof;
  384. wav->smv_last_stream &= !wav->smv_eof;
  385. if (wav->smv_last_stream) {
  386. uint64_t old_pos = avio_tell(s->pb);
  387. uint64_t new_pos = wav->smv_data_ofs +
  388. wav->smv_block * wav->smv_block_size;
  389. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  390. ret = AVERROR_EOF;
  391. goto smv_out;
  392. }
  393. size = avio_rl24(s->pb);
  394. ret = av_get_packet(s->pb, pkt, size);
  395. if (ret < 0)
  396. goto smv_out;
  397. pkt->pos -= 3;
  398. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  399. wav->smv_block++;
  400. pkt->stream_index = 1;
  401. smv_out:
  402. avio_seek(s->pb, old_pos, SEEK_SET);
  403. if (ret == AVERROR_EOF) {
  404. wav->smv_eof = 1;
  405. goto smv_retry;
  406. }
  407. return ret;
  408. }
  409. }
  410. st = s->streams[0];
  411. left = wav->data_end - avio_tell(s->pb);
  412. if (wav->ignore_length)
  413. left= INT_MAX;
  414. if (left <= 0){
  415. if (CONFIG_W64_DEMUXER && wav->w64)
  416. left = find_guid(s->pb, ff_w64_guid_data) - 24;
  417. else
  418. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  419. if (left < 0) {
  420. wav->audio_eof = 1;
  421. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  422. goto smv_retry;
  423. return AVERROR_EOF;
  424. }
  425. wav->data_end= avio_tell(s->pb) + left;
  426. }
  427. size = MAX_SIZE;
  428. if (st->codec->block_align > 1) {
  429. if (size < st->codec->block_align)
  430. size = st->codec->block_align;
  431. size = (size / st->codec->block_align) * st->codec->block_align;
  432. }
  433. size = FFMIN(size, left);
  434. ret = av_get_packet(s->pb, pkt, size);
  435. if (ret < 0)
  436. return ret;
  437. pkt->stream_index = 0;
  438. return ret;
  439. }
  440. static int wav_read_seek(AVFormatContext *s,
  441. int stream_index, int64_t timestamp, int flags)
  442. {
  443. WAVDemuxContext *wav = s->priv_data;
  444. AVStream *st;
  445. wav->smv_eof = 0;
  446. wav->audio_eof = 0;
  447. if (wav->smv_data_ofs > 0) {
  448. int64_t smv_timestamp = timestamp;
  449. if (stream_index == 0)
  450. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  451. else
  452. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  453. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  454. }
  455. st = s->streams[0];
  456. switch (st->codec->codec_id) {
  457. case AV_CODEC_ID_MP2:
  458. case AV_CODEC_ID_MP3:
  459. case AV_CODEC_ID_AC3:
  460. case AV_CODEC_ID_DTS:
  461. /* use generic seeking with dynamically generated indexes */
  462. return -1;
  463. default:
  464. break;
  465. }
  466. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  467. }
  468. #define OFFSET(x) offsetof(WAVDemuxContext, x)
  469. #define DEC AV_OPT_FLAG_DECODING_PARAM
  470. static const AVOption demux_options[] = {
  471. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  472. { NULL },
  473. };
  474. static const AVClass wav_demuxer_class = {
  475. .class_name = "WAV demuxer",
  476. .item_name = av_default_item_name,
  477. .option = demux_options,
  478. .version = LIBAVUTIL_VERSION_INT,
  479. };
  480. AVInputFormat ff_wav_demuxer = {
  481. .name = "wav",
  482. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  483. .priv_data_size = sizeof(WAVDemuxContext),
  484. .read_probe = wav_probe,
  485. .read_header = wav_read_header,
  486. .read_packet = wav_read_packet,
  487. .read_seek = wav_read_seek,
  488. .flags = AVFMT_GENERIC_INDEX,
  489. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  490. .priv_class = &wav_demuxer_class,
  491. };
  492. #endif /* CONFIG_WAV_DEMUXER */
  493. #if CONFIG_W64_DEMUXER
  494. static int w64_probe(AVProbeData *p)
  495. {
  496. if (p->buf_size <= 40)
  497. return 0;
  498. if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
  499. !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
  500. return AVPROBE_SCORE_MAX;
  501. else
  502. return 0;
  503. }
  504. static int w64_read_header(AVFormatContext *s)
  505. {
  506. int64_t size, data_ofs = 0;
  507. AVIOContext *pb = s->pb;
  508. WAVDemuxContext *wav = s->priv_data;
  509. AVStream *st;
  510. uint8_t guid[16];
  511. int ret;
  512. avio_read(pb, guid, 16);
  513. if (memcmp(guid, ff_w64_guid_riff, 16))
  514. return -1;
  515. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  516. return -1;
  517. avio_read(pb, guid, 16);
  518. if (memcmp(guid, ff_w64_guid_wave, 16)) {
  519. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  520. return -1;
  521. }
  522. wav->w64 = 1;
  523. st = avformat_new_stream(s, NULL);
  524. if (!st)
  525. return AVERROR(ENOMEM);
  526. while (!url_feof(pb)) {
  527. if (avio_read(pb, guid, 16) != 16)
  528. break;
  529. size = avio_rl64(pb);
  530. if (size <= 24 || INT64_MAX - size < avio_tell(pb))
  531. return AVERROR_INVALIDDATA;
  532. if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
  533. /* subtract chunk header size - normal wav file doesn't count it */
  534. ret = ff_get_wav_header(pb, st->codec, size - 24);
  535. if (ret < 0)
  536. return ret;
  537. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  538. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  539. } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
  540. int64_t samples;
  541. samples = avio_rl64(pb);
  542. if (samples > 0)
  543. st->duration = samples;
  544. } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
  545. wav->data_end = avio_tell(pb) + size - 24;
  546. data_ofs = avio_tell(pb);
  547. if (!pb->seekable)
  548. break;
  549. avio_skip(pb, size - 24);
  550. } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
  551. int64_t start, end, cur;
  552. uint32_t count, chunk_size, i;
  553. start = avio_tell(pb);
  554. end = start + size;
  555. count = avio_rl32(pb);
  556. for (i = 0; i < count; i++) {
  557. char chunk_key[5], *value;
  558. if (url_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
  559. break;
  560. chunk_key[4] = 0;
  561. avio_read(pb, chunk_key, 4);
  562. chunk_size = avio_rl32(pb);
  563. value = av_mallocz(chunk_size + 1);
  564. if (!value)
  565. return AVERROR(ENOMEM);
  566. ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
  567. avio_skip(pb, chunk_size - ret);
  568. av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
  569. }
  570. avio_skip(pb, end - avio_tell(pb));
  571. } else {
  572. av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
  573. avio_skip(pb, size - 24);
  574. }
  575. }
  576. if (!data_ofs)
  577. return AVERROR_EOF;
  578. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  579. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  580. handle_stream_probing(st);
  581. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  582. avio_seek(pb, data_ofs, SEEK_SET);
  583. return 0;
  584. }
  585. AVInputFormat ff_w64_demuxer = {
  586. .name = "w64",
  587. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  588. .priv_data_size = sizeof(WAVDemuxContext),
  589. .read_probe = w64_probe,
  590. .read_header = w64_read_header,
  591. .read_packet = wav_read_packet,
  592. .read_seek = wav_read_seek,
  593. .flags = AVFMT_GENERIC_INDEX,
  594. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  595. };
  596. #endif /* CONFIG_W64_DEMUXER */