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.

733 lines
23KB

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