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.

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