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.

901 lines
29KB

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