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.

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