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.

730 lines
22KB

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