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.

794 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 ( st->codec->bit_rate > 0 && data_size > 0
  381. && st->codec->sample_rate > 0
  382. && sample_count > 0 && st->codec->channels > 1
  383. && sample_count % st->codec->channels == 0) {
  384. if (fabs(8.0 * data_size * st->codec->channels * st->codec->sample_rate /
  385. sample_count /st->codec->bit_rate - 1.0) < 0.3)
  386. sample_count /= st->codec->channels;
  387. }
  388. if ( data_size > 0 && sample_count && st->codec->channels
  389. && (data_size << 3) / sample_count / st->codec->channels > st->codec->bits_per_coded_sample + 1) {
  390. av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
  391. sample_count = 0;
  392. }
  393. /* G.729 hack (for Ticket4577)
  394. * FIXME: Come up with cleaner, more general solution */
  395. if (st->codec->codec_id == AV_CODEC_ID_G729 && sample_count && (data_size << 3) > sample_count) {
  396. av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
  397. sample_count = 0;
  398. }
  399. if (!sample_count || av_get_exact_bits_per_sample(st->codec->codec_id) > 0)
  400. if ( st->codec->channels
  401. && data_size
  402. && av_get_bits_per_sample(st->codec->codec_id)
  403. && wav->data_end <= avio_size(pb))
  404. sample_count = (data_size << 3)
  405. /
  406. (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  407. if (sample_count)
  408. st->duration = sample_count;
  409. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  410. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  411. return 0;
  412. }
  413. /**
  414. * Find chunk with w64 GUID by skipping over other chunks.
  415. * @return the size of the found chunk
  416. */
  417. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  418. {
  419. uint8_t guid[16];
  420. int64_t size;
  421. while (!avio_feof(pb)) {
  422. avio_read(pb, guid, 16);
  423. size = avio_rl64(pb);
  424. if (size <= 24)
  425. return AVERROR_INVALIDDATA;
  426. if (!memcmp(guid, guid1, 16))
  427. return size;
  428. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  429. }
  430. return AVERROR_EOF;
  431. }
  432. #define MAX_SIZE 4096
  433. static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
  434. {
  435. int ret, size;
  436. int64_t left;
  437. AVStream *st;
  438. WAVDemuxContext *wav = s->priv_data;
  439. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 0 &&
  440. s->streams[0]->codec->codec_tag == 1) {
  441. enum AVCodecID codec;
  442. ret = ff_spdif_probe(s->pb->buffer, s->pb->buf_end - s->pb->buffer,
  443. &codec);
  444. if (ret > AVPROBE_SCORE_EXTENSION) {
  445. s->streams[0]->codec->codec_id = codec;
  446. wav->spdif = 1;
  447. } else {
  448. wav->spdif = -1;
  449. }
  450. }
  451. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
  452. return ff_spdif_read_packet(s, pkt);
  453. if (wav->smv_data_ofs > 0) {
  454. int64_t audio_dts, video_dts;
  455. smv_retry:
  456. audio_dts = (int32_t)s->streams[0]->cur_dts;
  457. video_dts = (int32_t)s->streams[1]->cur_dts;
  458. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  459. /*We always return a video frame first to get the pixel format first*/
  460. wav->smv_last_stream = wav->smv_given_first ?
  461. av_compare_ts(video_dts, s->streams[1]->time_base,
  462. audio_dts, s->streams[0]->time_base) > 0 : 0;
  463. wav->smv_given_first = 1;
  464. }
  465. wav->smv_last_stream = !wav->smv_last_stream;
  466. wav->smv_last_stream |= wav->audio_eof;
  467. wav->smv_last_stream &= !wav->smv_eof;
  468. if (wav->smv_last_stream) {
  469. uint64_t old_pos = avio_tell(s->pb);
  470. uint64_t new_pos = wav->smv_data_ofs +
  471. wav->smv_block * wav->smv_block_size;
  472. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  473. ret = AVERROR_EOF;
  474. goto smv_out;
  475. }
  476. size = avio_rl24(s->pb);
  477. ret = av_get_packet(s->pb, pkt, size);
  478. if (ret < 0)
  479. goto smv_out;
  480. pkt->pos -= 3;
  481. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt;
  482. wav->smv_cur_pt++;
  483. if (wav->smv_frames_per_jpeg > 0)
  484. wav->smv_cur_pt %= wav->smv_frames_per_jpeg;
  485. if (!wav->smv_cur_pt)
  486. wav->smv_block++;
  487. pkt->stream_index = 1;
  488. smv_out:
  489. avio_seek(s->pb, old_pos, SEEK_SET);
  490. if (ret == AVERROR_EOF) {
  491. wav->smv_eof = 1;
  492. goto smv_retry;
  493. }
  494. return ret;
  495. }
  496. }
  497. st = s->streams[0];
  498. left = wav->data_end - avio_tell(s->pb);
  499. if (wav->ignore_length)
  500. left = INT_MAX;
  501. if (left <= 0) {
  502. if (CONFIG_W64_DEMUXER && wav->w64)
  503. left = find_guid(s->pb, ff_w64_guid_data) - 24;
  504. else
  505. left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
  506. if (left < 0) {
  507. wav->audio_eof = 1;
  508. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  509. goto smv_retry;
  510. return AVERROR_EOF;
  511. }
  512. wav->data_end = avio_tell(s->pb) + left;
  513. }
  514. size = MAX_SIZE;
  515. if (st->codec->block_align > 1) {
  516. if (size < st->codec->block_align)
  517. size = st->codec->block_align;
  518. size = (size / st->codec->block_align) * st->codec->block_align;
  519. }
  520. size = FFMIN(size, left);
  521. ret = av_get_packet(s->pb, pkt, size);
  522. if (ret < 0)
  523. return ret;
  524. pkt->stream_index = 0;
  525. return ret;
  526. }
  527. static int wav_read_seek(AVFormatContext *s,
  528. int stream_index, int64_t timestamp, int flags)
  529. {
  530. WAVDemuxContext *wav = s->priv_data;
  531. AVStream *st;
  532. wav->smv_eof = 0;
  533. wav->audio_eof = 0;
  534. if (wav->smv_data_ofs > 0) {
  535. int64_t smv_timestamp = timestamp;
  536. if (stream_index == 0)
  537. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  538. else
  539. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  540. if (wav->smv_frames_per_jpeg > 0) {
  541. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  542. wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg;
  543. }
  544. }
  545. st = s->streams[0];
  546. switch (st->codec->codec_id) {
  547. case AV_CODEC_ID_MP2:
  548. case AV_CODEC_ID_MP3:
  549. case AV_CODEC_ID_AC3:
  550. case AV_CODEC_ID_DTS:
  551. /* use generic seeking with dynamically generated indexes */
  552. return -1;
  553. default:
  554. break;
  555. }
  556. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  557. }
  558. #define OFFSET(x) offsetof(WAVDemuxContext, x)
  559. #define DEC AV_OPT_FLAG_DECODING_PARAM
  560. static const AVOption demux_options[] = {
  561. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  562. { NULL },
  563. };
  564. static const AVClass wav_demuxer_class = {
  565. .class_name = "WAV demuxer",
  566. .item_name = av_default_item_name,
  567. .option = demux_options,
  568. .version = LIBAVUTIL_VERSION_INT,
  569. };
  570. AVInputFormat ff_wav_demuxer = {
  571. .name = "wav",
  572. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  573. .priv_data_size = sizeof(WAVDemuxContext),
  574. .read_probe = wav_probe,
  575. .read_header = wav_read_header,
  576. .read_packet = wav_read_packet,
  577. .read_seek = wav_read_seek,
  578. .flags = AVFMT_GENERIC_INDEX,
  579. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  580. .priv_class = &wav_demuxer_class,
  581. };
  582. #endif /* CONFIG_WAV_DEMUXER */
  583. #if CONFIG_W64_DEMUXER
  584. static int w64_probe(AVProbeData *p)
  585. {
  586. if (p->buf_size <= 40)
  587. return 0;
  588. if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
  589. !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
  590. return AVPROBE_SCORE_MAX;
  591. else
  592. return 0;
  593. }
  594. static int w64_read_header(AVFormatContext *s)
  595. {
  596. int64_t size, data_ofs = 0;
  597. AVIOContext *pb = s->pb;
  598. WAVDemuxContext *wav = s->priv_data;
  599. AVStream *st;
  600. uint8_t guid[16];
  601. int ret;
  602. avio_read(pb, guid, 16);
  603. if (memcmp(guid, ff_w64_guid_riff, 16))
  604. return AVERROR_INVALIDDATA;
  605. /* riff + wave + fmt + sizes */
  606. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
  607. return AVERROR_INVALIDDATA;
  608. avio_read(pb, guid, 16);
  609. if (memcmp(guid, ff_w64_guid_wave, 16)) {
  610. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  611. return AVERROR_INVALIDDATA;
  612. }
  613. wav->w64 = 1;
  614. st = avformat_new_stream(s, NULL);
  615. if (!st)
  616. return AVERROR(ENOMEM);
  617. while (!avio_feof(pb)) {
  618. if (avio_read(pb, guid, 16) != 16)
  619. break;
  620. size = avio_rl64(pb);
  621. if (size <= 24 || INT64_MAX - size < avio_tell(pb))
  622. return AVERROR_INVALIDDATA;
  623. if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
  624. /* subtract chunk header size - normal wav file doesn't count it */
  625. ret = ff_get_wav_header(s, pb, st->codec, size - 24, 0);
  626. if (ret < 0)
  627. return ret;
  628. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  629. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  630. } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
  631. int64_t samples;
  632. samples = avio_rl64(pb);
  633. if (samples > 0)
  634. st->duration = samples;
  635. } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
  636. wav->data_end = avio_tell(pb) + size - 24;
  637. data_ofs = avio_tell(pb);
  638. if (!pb->seekable)
  639. break;
  640. avio_skip(pb, size - 24);
  641. } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
  642. int64_t start, end, cur;
  643. uint32_t count, chunk_size, i;
  644. start = avio_tell(pb);
  645. end = start + FFALIGN(size, INT64_C(8)) - 24;
  646. count = avio_rl32(pb);
  647. for (i = 0; i < count; i++) {
  648. char chunk_key[5], *value;
  649. if (avio_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
  650. break;
  651. chunk_key[4] = 0;
  652. avio_read(pb, chunk_key, 4);
  653. chunk_size = avio_rl32(pb);
  654. value = av_mallocz(chunk_size + 1);
  655. if (!value)
  656. return AVERROR(ENOMEM);
  657. ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
  658. avio_skip(pb, chunk_size - ret);
  659. av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
  660. }
  661. avio_skip(pb, end - avio_tell(pb));
  662. } else {
  663. av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
  664. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  665. }
  666. }
  667. if (!data_ofs)
  668. return AVERROR_EOF;
  669. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  670. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  671. handle_stream_probing(st);
  672. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  673. avio_seek(pb, data_ofs, SEEK_SET);
  674. return 0;
  675. }
  676. AVInputFormat ff_w64_demuxer = {
  677. .name = "w64",
  678. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  679. .priv_data_size = sizeof(WAVDemuxContext),
  680. .read_probe = w64_probe,
  681. .read_header = w64_read_header,
  682. .read_packet = wav_read_packet,
  683. .read_seek = wav_read_seek,
  684. .flags = AVFMT_GENERIC_INDEX,
  685. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  686. };
  687. #endif /* CONFIG_W64_DEMUXER */