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.

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