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.

982 lines
32KB

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