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.

891 lines
28KB

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