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.

619 lines
19KB

  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 "libavutil/avassert.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "avio_internal.h"
  33. #include "pcm.h"
  34. #include "riff.h"
  35. #include "w64.h"
  36. #include "avio.h"
  37. #include "metadata.h"
  38. typedef struct WAVDemuxContext {
  39. const AVClass *class;
  40. int64_t data_end;
  41. int w64;
  42. int64_t smv_data_ofs;
  43. int smv_block_size;
  44. int smv_frames_per_jpeg;
  45. int smv_block;
  46. int smv_last_stream;
  47. int smv_eof;
  48. int audio_eof;
  49. int ignore_length;
  50. } WAVDemuxContext;
  51. #if CONFIG_WAV_DEMUXER
  52. static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
  53. {
  54. *tag = avio_rl32(pb);
  55. return avio_rl32(pb);
  56. }
  57. /* return the size of the found tag */
  58. static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
  59. {
  60. unsigned int tag;
  61. int64_t size;
  62. for (;;) {
  63. if (url_feof(pb))
  64. return -1;
  65. size = next_tag(pb, &tag);
  66. if (tag == tag1)
  67. break;
  68. avio_skip(pb, size);
  69. }
  70. return size;
  71. }
  72. static int wav_probe(AVProbeData *p)
  73. {
  74. /* check file header */
  75. if (p->buf_size <= 32)
  76. return 0;
  77. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  78. if (!memcmp(p->buf, "RIFF", 4))
  79. /*
  80. Since ACT demuxer has standard WAV header at top of it's own,
  81. returning score is decreased to avoid probe conflict
  82. between ACT and WAV.
  83. */
  84. return AVPROBE_SCORE_MAX - 1;
  85. else if (!memcmp(p->buf, "RF64", 4) &&
  86. !memcmp(p->buf + 12, "ds64", 4))
  87. return AVPROBE_SCORE_MAX;
  88. }
  89. return 0;
  90. }
  91. static void handle_stream_probing(AVStream *st)
  92. {
  93. if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
  94. st->request_probe = AVPROBE_SCORE_MAX/2;
  95. st->probe_packets = FFMIN(st->probe_packets, 4);
  96. }
  97. }
  98. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  99. {
  100. AVIOContext *pb = s->pb;
  101. int ret;
  102. /* parse fmt header */
  103. *st = avformat_new_stream(s, NULL);
  104. if (!*st)
  105. return AVERROR(ENOMEM);
  106. ret = ff_get_wav_header(pb, (*st)->codec, size);
  107. if (ret < 0)
  108. return ret;
  109. handle_stream_probing(*st);
  110. (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  111. avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  112. return 0;
  113. }
  114. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  115. int length)
  116. {
  117. char temp[257];
  118. int ret;
  119. av_assert0(length <= sizeof(temp));
  120. if ((ret = avio_read(s->pb, temp, length)) < 0)
  121. return ret;
  122. temp[length] = 0;
  123. if (strlen(temp))
  124. return av_dict_set(&s->metadata, key, temp, 0);
  125. return 0;
  126. }
  127. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  128. {
  129. char temp[131], *coding_history;
  130. int ret, x;
  131. uint64_t time_reference;
  132. int64_t umid_parts[8], umid_mask = 0;
  133. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  134. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  135. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  136. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  137. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  138. return ret;
  139. time_reference = avio_rl64(s->pb);
  140. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  141. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  142. return ret;
  143. /* check if version is >= 1, in which case an UMID may be present */
  144. if (avio_rl16(s->pb) >= 1) {
  145. for (x = 0; x < 8; x++)
  146. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  147. if (umid_mask) {
  148. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  149. if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
  150. /* basic UMID */
  151. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  152. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
  153. } else {
  154. /* extended UMID */
  155. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  156. "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  157. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
  158. umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
  159. }
  160. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  161. return ret;
  162. }
  163. avio_skip(s->pb, 190);
  164. } else
  165. avio_skip(s->pb, 254);
  166. if (size > 602) {
  167. /* CodingHistory present */
  168. size -= 602;
  169. if (!(coding_history = av_malloc(size+1)))
  170. return AVERROR(ENOMEM);
  171. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  172. return ret;
  173. coding_history[size] = 0;
  174. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  175. AV_DICT_DONT_STRDUP_VAL)) < 0)
  176. return ret;
  177. }
  178. return 0;
  179. }
  180. static const AVMetadataConv wav_metadata_conv[] = {
  181. {"description", "comment" },
  182. {"originator", "encoded_by" },
  183. {"origination_date", "date" },
  184. {"origination_time", "creation_time"},
  185. {0},
  186. };
  187. /* wav input */
  188. static int wav_read_header(AVFormatContext *s)
  189. {
  190. int64_t size, av_uninit(data_size);
  191. int64_t sample_count=0;
  192. int rf64;
  193. uint32_t tag;
  194. AVIOContext *pb = s->pb;
  195. AVStream *st = NULL;
  196. WAVDemuxContext *wav = s->priv_data;
  197. int ret, got_fmt = 0;
  198. int64_t next_tag_ofs, data_ofs = -1;
  199. wav->smv_data_ofs = -1;
  200. /* check RIFF header */
  201. tag = avio_rl32(pb);
  202. rf64 = tag == MKTAG('R', 'F', '6', '4');
  203. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  204. return -1;
  205. avio_rl32(pb); /* file size */
  206. tag = avio_rl32(pb);
  207. if (tag != MKTAG('W', 'A', 'V', 'E'))
  208. return -1;
  209. if (rf64) {
  210. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  211. return -1;
  212. size = avio_rl32(pb);
  213. if (size < 24)
  214. return -1;
  215. avio_rl64(pb); /* RIFF size */
  216. data_size = avio_rl64(pb);
  217. sample_count = avio_rl64(pb);
  218. if (data_size < 0 || sample_count < 0) {
  219. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  220. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  221. data_size, sample_count);
  222. return AVERROR_INVALIDDATA;
  223. }
  224. avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
  225. }
  226. for (;;) {
  227. AVStream *vst;
  228. size = next_tag(pb, &tag);
  229. next_tag_ofs = avio_tell(pb) + size;
  230. if (url_feof(pb))
  231. break;
  232. switch (tag) {
  233. case MKTAG('f', 'm', 't', ' '):
  234. /* only parse the first 'fmt ' tag found */
  235. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
  236. return ret;
  237. } else if (got_fmt)
  238. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  239. got_fmt = 1;
  240. break;
  241. case MKTAG('d', 'a', 't', 'a'):
  242. if (!got_fmt) {
  243. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
  244. return AVERROR_INVALIDDATA;
  245. }
  246. if (rf64) {
  247. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  248. } else {
  249. data_size = size;
  250. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  251. }
  252. data_ofs = avio_tell(pb);
  253. /* don't look for footer metadata if we can't seek or if we don't
  254. * know where the data tag ends
  255. */
  256. if (!pb->seekable || (!rf64 && !size))
  257. goto break_loop;
  258. break;
  259. case MKTAG('f','a','c','t'):
  260. if (!sample_count)
  261. sample_count = avio_rl32(pb);
  262. break;
  263. case MKTAG('b','e','x','t'):
  264. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  265. return ret;
  266. break;
  267. case MKTAG('S','M','V','0'):
  268. if (!got_fmt) {
  269. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
  270. return AVERROR_INVALIDDATA;
  271. }
  272. // SMV file, a wav file with video appended.
  273. if (size != MKTAG('0','2','0','0')) {
  274. av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
  275. goto break_loop;
  276. }
  277. av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
  278. vst = avformat_new_stream(s, NULL);
  279. if (!vst)
  280. return AVERROR(ENOMEM);
  281. avio_r8(pb);
  282. vst->id = 1;
  283. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  284. vst->codec->codec_id = AV_CODEC_ID_MJPEG;
  285. vst->codec->width = avio_rl24(pb);
  286. vst->codec->height = avio_rl24(pb);
  287. size = avio_rl24(pb);
  288. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  289. avio_rl24(pb);
  290. wav->smv_block_size = avio_rl24(pb);
  291. avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
  292. vst->duration = avio_rl24(pb);
  293. avio_rl24(pb);
  294. avio_rl24(pb);
  295. wav->smv_frames_per_jpeg = avio_rl24(pb);
  296. goto break_loop;
  297. case MKTAG('L', 'I', 'S', 'T'):
  298. if (size < 4) {
  299. av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
  300. return AVERROR_INVALIDDATA;
  301. }
  302. switch (avio_rl32(pb)) {
  303. case MKTAG('I', 'N', 'F', 'O'):
  304. ff_read_riff_info(s, size - 4);
  305. }
  306. break;
  307. }
  308. /* seek to next tag unless we know that we'll run into EOF */
  309. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  310. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  311. break;
  312. }
  313. }
  314. break_loop:
  315. if (data_ofs < 0) {
  316. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  317. return AVERROR_INVALIDDATA;
  318. }
  319. avio_seek(pb, data_ofs, SEEK_SET);
  320. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id) && wav->data_end <= avio_size(pb))
  321. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  322. if (sample_count)
  323. st->duration = sample_count;
  324. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  325. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  326. return 0;
  327. }
  328. /** Find chunk with w64 GUID by skipping over other chunks
  329. * @return the size of the found chunk
  330. */
  331. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  332. {
  333. uint8_t guid[16];
  334. int64_t size;
  335. while (!url_feof(pb)) {
  336. avio_read(pb, guid, 16);
  337. size = avio_rl64(pb);
  338. if (size <= 24)
  339. return -1;
  340. if (!memcmp(guid, guid1, 16))
  341. return size;
  342. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  343. }
  344. return -1;
  345. }
  346. #define MAX_SIZE 4096
  347. static int wav_read_packet(AVFormatContext *s,
  348. AVPacket *pkt)
  349. {
  350. int ret, size;
  351. int64_t left;
  352. AVStream *st;
  353. WAVDemuxContext *wav = s->priv_data;
  354. if (wav->smv_data_ofs > 0) {
  355. int64_t audio_dts, video_dts;
  356. smv_retry:
  357. audio_dts = s->streams[0]->cur_dts;
  358. video_dts = s->streams[1]->cur_dts;
  359. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  360. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  361. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  362. wav->smv_last_stream = video_dts >= audio_dts;
  363. }
  364. wav->smv_last_stream = !wav->smv_last_stream;
  365. wav->smv_last_stream |= wav->audio_eof;
  366. wav->smv_last_stream &= !wav->smv_eof;
  367. if (wav->smv_last_stream) {
  368. uint64_t old_pos = avio_tell(s->pb);
  369. uint64_t new_pos = wav->smv_data_ofs +
  370. wav->smv_block * wav->smv_block_size;
  371. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  372. ret = AVERROR_EOF;
  373. goto smv_out;
  374. }
  375. size = avio_rl24(s->pb);
  376. ret = av_get_packet(s->pb, pkt, size);
  377. if (ret < 0)
  378. goto smv_out;
  379. pkt->pos -= 3;
  380. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  381. wav->smv_block++;
  382. pkt->stream_index = 1;
  383. smv_out:
  384. avio_seek(s->pb, old_pos, SEEK_SET);
  385. if (ret == AVERROR_EOF) {
  386. wav->smv_eof = 1;
  387. goto smv_retry;
  388. }
  389. return ret;
  390. }
  391. }
  392. st = s->streams[0];
  393. left = wav->data_end - avio_tell(s->pb);
  394. if (wav->ignore_length)
  395. left= INT_MAX;
  396. if (left <= 0){
  397. if (CONFIG_W64_DEMUXER && wav->w64)
  398. left = find_guid(s->pb, ff_w64_guid_data) - 24;
  399. else
  400. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  401. if (left < 0) {
  402. wav->audio_eof = 1;
  403. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  404. goto smv_retry;
  405. return AVERROR_EOF;
  406. }
  407. wav->data_end= avio_tell(s->pb) + left;
  408. }
  409. size = MAX_SIZE;
  410. if (st->codec->block_align > 1) {
  411. if (size < st->codec->block_align)
  412. size = st->codec->block_align;
  413. size = (size / st->codec->block_align) * st->codec->block_align;
  414. }
  415. size = FFMIN(size, left);
  416. ret = av_get_packet(s->pb, pkt, size);
  417. if (ret < 0)
  418. return ret;
  419. pkt->stream_index = 0;
  420. return ret;
  421. }
  422. static int wav_read_seek(AVFormatContext *s,
  423. int stream_index, int64_t timestamp, int flags)
  424. {
  425. WAVDemuxContext *wav = s->priv_data;
  426. AVStream *st;
  427. wav->smv_eof = 0;
  428. wav->audio_eof = 0;
  429. if (wav->smv_data_ofs > 0) {
  430. int64_t smv_timestamp = timestamp;
  431. if (stream_index == 0)
  432. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  433. else
  434. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  435. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  436. }
  437. st = s->streams[0];
  438. switch (st->codec->codec_id) {
  439. case AV_CODEC_ID_MP2:
  440. case AV_CODEC_ID_MP3:
  441. case AV_CODEC_ID_AC3:
  442. case AV_CODEC_ID_DTS:
  443. /* use generic seeking with dynamically generated indexes */
  444. return -1;
  445. default:
  446. break;
  447. }
  448. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  449. }
  450. #define OFFSET(x) offsetof(WAVDemuxContext, x)
  451. #define DEC AV_OPT_FLAG_DECODING_PARAM
  452. static const AVOption demux_options[] = {
  453. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  454. { NULL },
  455. };
  456. static const AVClass wav_demuxer_class = {
  457. .class_name = "WAV demuxer",
  458. .item_name = av_default_item_name,
  459. .option = demux_options,
  460. .version = LIBAVUTIL_VERSION_INT,
  461. };
  462. AVInputFormat ff_wav_demuxer = {
  463. .name = "wav",
  464. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  465. .priv_data_size = sizeof(WAVDemuxContext),
  466. .read_probe = wav_probe,
  467. .read_header = wav_read_header,
  468. .read_packet = wav_read_packet,
  469. .read_seek = wav_read_seek,
  470. .flags = AVFMT_GENERIC_INDEX,
  471. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  472. .priv_class = &wav_demuxer_class,
  473. };
  474. #endif /* CONFIG_WAV_DEMUXER */
  475. #if CONFIG_W64_DEMUXER
  476. static int w64_probe(AVProbeData *p)
  477. {
  478. if (p->buf_size <= 40)
  479. return 0;
  480. if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
  481. !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
  482. return AVPROBE_SCORE_MAX;
  483. else
  484. return 0;
  485. }
  486. static int w64_read_header(AVFormatContext *s)
  487. {
  488. int64_t size;
  489. AVIOContext *pb = s->pb;
  490. WAVDemuxContext *wav = s->priv_data;
  491. AVStream *st;
  492. uint8_t guid[16];
  493. int ret;
  494. avio_read(pb, guid, 16);
  495. if (memcmp(guid, ff_w64_guid_riff, 16))
  496. return -1;
  497. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  498. return -1;
  499. avio_read(pb, guid, 16);
  500. if (memcmp(guid, ff_w64_guid_wave, 16)) {
  501. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  502. return -1;
  503. }
  504. size = find_guid(pb, ff_w64_guid_fmt);
  505. if (size < 0) {
  506. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  507. return -1;
  508. }
  509. st = avformat_new_stream(s, NULL);
  510. if (!st)
  511. return AVERROR(ENOMEM);
  512. /* subtract chunk header size - normal wav file doesn't count it */
  513. ret = ff_get_wav_header(pb, st->codec, size - 24);
  514. if (ret < 0)
  515. return ret;
  516. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  517. handle_stream_probing(st);
  518. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  519. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  520. size = find_guid(pb, ff_w64_guid_data);
  521. if (size < 0) {
  522. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  523. return -1;
  524. }
  525. wav->data_end = avio_tell(pb) + size - 24;
  526. wav->w64 = 1;
  527. return 0;
  528. }
  529. AVInputFormat ff_w64_demuxer = {
  530. .name = "w64",
  531. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  532. .priv_data_size = sizeof(WAVDemuxContext),
  533. .read_probe = w64_probe,
  534. .read_header = w64_read_header,
  535. .read_packet = wav_read_packet,
  536. .read_seek = wav_read_seek,
  537. .flags = AVFMT_GENERIC_INDEX,
  538. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  539. };
  540. #endif /* CONFIG_W64_DEMUXER */