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.

785 lines
25KB

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