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.

812 lines
24KB

  1. /*
  2. * WAV muxer and 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 "avio.h"
  36. #include "metadata.h"
  37. typedef struct {
  38. const AVClass *class;
  39. int64_t data;
  40. int64_t data_end;
  41. int64_t minpts;
  42. int64_t maxpts;
  43. int last_duration;
  44. int w64;
  45. int write_bext;
  46. int64_t smv_data_ofs;
  47. int smv_block_size;
  48. int smv_frames_per_jpeg;
  49. int smv_block;
  50. int smv_last_stream;
  51. int smv_eof;
  52. int audio_eof;
  53. int ignore_length;
  54. } WAVContext;
  55. #if CONFIG_WAV_MUXER
  56. static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
  57. {
  58. AVDictionaryEntry *tag;
  59. int len = 0;
  60. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  61. len = strlen(tag->value);
  62. len = FFMIN(len, maxlen);
  63. avio_write(s->pb, tag->value, len);
  64. }
  65. ffio_fill(s->pb, 0, maxlen - len);
  66. }
  67. static void bwf_write_bext_chunk(AVFormatContext *s)
  68. {
  69. AVDictionaryEntry *tmp_tag;
  70. uint64_t time_reference = 0;
  71. int64_t bext = ff_start_tag(s->pb, "bext");
  72. bwf_write_bext_string(s, "description", 256);
  73. bwf_write_bext_string(s, "originator", 32);
  74. bwf_write_bext_string(s, "originator_reference", 32);
  75. bwf_write_bext_string(s, "origination_date", 10);
  76. bwf_write_bext_string(s, "origination_time", 8);
  77. if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
  78. time_reference = strtoll(tmp_tag->value, NULL, 10);
  79. avio_wl64(s->pb, time_reference);
  80. avio_wl16(s->pb, 1); // set version to 1
  81. if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
  82. unsigned char umidpart_str[17] = {0};
  83. int i;
  84. uint64_t umidpart;
  85. int len = strlen(tmp_tag->value+2);
  86. for (i = 0; i < len/16; i++) {
  87. memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
  88. umidpart = strtoll(umidpart_str, NULL, 16);
  89. avio_wb64(s->pb, umidpart);
  90. }
  91. ffio_fill(s->pb, 0, 64 - i*8);
  92. } else
  93. ffio_fill(s->pb, 0, 64); // zero UMID
  94. ffio_fill(s->pb, 0, 190); // Reserved
  95. if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
  96. avio_put_str(s->pb, tmp_tag->value);
  97. ff_end_tag(s->pb, bext);
  98. }
  99. static int wav_write_header(AVFormatContext *s)
  100. {
  101. WAVContext *wav = s->priv_data;
  102. AVIOContext *pb = s->pb;
  103. int64_t fmt, fact;
  104. ffio_wfourcc(pb, "RIFF");
  105. avio_wl32(pb, 0); /* file length */
  106. ffio_wfourcc(pb, "WAVE");
  107. /* format header */
  108. fmt = ff_start_tag(pb, "fmt ");
  109. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  110. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  111. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  112. return -1;
  113. }
  114. ff_end_tag(pb, fmt);
  115. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  116. && s->pb->seekable) {
  117. fact = ff_start_tag(pb, "fact");
  118. avio_wl32(pb, 0);
  119. ff_end_tag(pb, fact);
  120. }
  121. if (wav->write_bext)
  122. bwf_write_bext_chunk(s);
  123. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  124. wav->maxpts = wav->last_duration = 0;
  125. wav->minpts = INT64_MAX;
  126. /* data header */
  127. wav->data = ff_start_tag(pb, "data");
  128. avio_flush(pb);
  129. return 0;
  130. }
  131. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  132. {
  133. AVIOContext *pb = s->pb;
  134. WAVContext *wav = s->priv_data;
  135. avio_write(pb, pkt->data, pkt->size);
  136. if(pkt->pts != AV_NOPTS_VALUE) {
  137. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  138. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  139. wav->last_duration = pkt->duration;
  140. } else
  141. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  142. return 0;
  143. }
  144. static int wav_write_trailer(AVFormatContext *s)
  145. {
  146. AVIOContext *pb = s->pb;
  147. WAVContext *wav = s->priv_data;
  148. int64_t file_size;
  149. avio_flush(pb);
  150. if (s->pb->seekable) {
  151. ff_end_tag(pb, wav->data);
  152. /* update file size */
  153. file_size = avio_tell(pb);
  154. avio_seek(pb, 4, SEEK_SET);
  155. avio_wl32(pb, (uint32_t)(file_size - 8));
  156. avio_seek(pb, file_size, SEEK_SET);
  157. avio_flush(pb);
  158. if(s->streams[0]->codec->codec_tag != 0x01) {
  159. /* Update num_samps in fact chunk */
  160. int number_of_samples;
  161. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  162. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  163. s->streams[0]->time_base.den);
  164. avio_seek(pb, wav->data-12, SEEK_SET);
  165. avio_wl32(pb, number_of_samples);
  166. avio_seek(pb, file_size, SEEK_SET);
  167. avio_flush(pb);
  168. }
  169. }
  170. return 0;
  171. }
  172. #define OFFSET(x) offsetof(WAVContext, x)
  173. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  174. static const AVOption options[] = {
  175. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  176. { NULL },
  177. };
  178. static const AVClass wav_muxer_class = {
  179. .class_name = "WAV muxer",
  180. .item_name = av_default_item_name,
  181. .option = options,
  182. .version = LIBAVUTIL_VERSION_INT,
  183. };
  184. AVOutputFormat ff_wav_muxer = {
  185. .name = "wav",
  186. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  187. .mime_type = "audio/x-wav",
  188. .extensions = "wav",
  189. .priv_data_size = sizeof(WAVContext),
  190. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  191. .video_codec = AV_CODEC_ID_NONE,
  192. .write_header = wav_write_header,
  193. .write_packet = wav_write_packet,
  194. .write_trailer = wav_write_trailer,
  195. .flags = AVFMT_TS_NONSTRICT,
  196. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  197. .priv_class = &wav_muxer_class,
  198. };
  199. #endif /* CONFIG_WAV_MUXER */
  200. #if CONFIG_WAV_DEMUXER
  201. static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
  202. {
  203. *tag = avio_rl32(pb);
  204. return avio_rl32(pb);
  205. }
  206. /* return the size of the found tag */
  207. static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
  208. {
  209. unsigned int tag;
  210. int64_t size;
  211. for (;;) {
  212. if (url_feof(pb))
  213. return -1;
  214. size = next_tag(pb, &tag);
  215. if (tag == tag1)
  216. break;
  217. avio_skip(pb, size);
  218. }
  219. return size;
  220. }
  221. static int wav_probe(AVProbeData *p)
  222. {
  223. /* check file header */
  224. if (p->buf_size <= 32)
  225. return 0;
  226. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  227. if (!memcmp(p->buf, "RIFF", 4))
  228. /*
  229. Since ACT demuxer has standard WAV header at top of it's own,
  230. returning score is decreased to avoid probe conflict
  231. between ACT and WAV.
  232. */
  233. return AVPROBE_SCORE_MAX - 1;
  234. else if (!memcmp(p->buf, "RF64", 4) &&
  235. !memcmp(p->buf + 12, "ds64", 4))
  236. return AVPROBE_SCORE_MAX;
  237. }
  238. return 0;
  239. }
  240. static void handle_stream_probing(AVStream *st)
  241. {
  242. if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
  243. st->request_probe = AVPROBE_SCORE_MAX/2;
  244. st->probe_packets = FFMIN(st->probe_packets, 4);
  245. }
  246. }
  247. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  248. {
  249. AVIOContext *pb = s->pb;
  250. int ret;
  251. /* parse fmt header */
  252. *st = avformat_new_stream(s, NULL);
  253. if (!*st)
  254. return AVERROR(ENOMEM);
  255. ret = ff_get_wav_header(pb, (*st)->codec, size);
  256. if (ret < 0)
  257. return ret;
  258. handle_stream_probing(*st);
  259. (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  260. avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  261. return 0;
  262. }
  263. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  264. int length)
  265. {
  266. char temp[257];
  267. int ret;
  268. av_assert0(length <= sizeof(temp));
  269. if ((ret = avio_read(s->pb, temp, length)) < 0)
  270. return ret;
  271. temp[length] = 0;
  272. if (strlen(temp))
  273. return av_dict_set(&s->metadata, key, temp, 0);
  274. return 0;
  275. }
  276. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  277. {
  278. char temp[131], *coding_history;
  279. int ret, x;
  280. uint64_t time_reference;
  281. int64_t umid_parts[8], umid_mask = 0;
  282. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  283. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  284. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  285. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  286. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  287. return ret;
  288. time_reference = avio_rl64(s->pb);
  289. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  290. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  291. return ret;
  292. /* check if version is >= 1, in which case an UMID may be present */
  293. if (avio_rl16(s->pb) >= 1) {
  294. for (x = 0; x < 8; x++)
  295. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  296. if (umid_mask) {
  297. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  298. if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
  299. /* basic UMID */
  300. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  301. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
  302. } else {
  303. /* extended UMID */
  304. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  305. "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  306. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
  307. umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
  308. }
  309. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  310. return ret;
  311. }
  312. avio_skip(s->pb, 190);
  313. } else
  314. avio_skip(s->pb, 254);
  315. if (size > 602) {
  316. /* CodingHistory present */
  317. size -= 602;
  318. if (!(coding_history = av_malloc(size+1)))
  319. return AVERROR(ENOMEM);
  320. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  321. return ret;
  322. coding_history[size] = 0;
  323. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  324. AV_DICT_DONT_STRDUP_VAL)) < 0)
  325. return ret;
  326. }
  327. return 0;
  328. }
  329. static const AVMetadataConv wav_metadata_conv[] = {
  330. {"description", "comment" },
  331. {"originator", "encoded_by" },
  332. {"origination_date", "date" },
  333. {"origination_time", "creation_time"},
  334. {0},
  335. };
  336. /* wav input */
  337. static int wav_read_header(AVFormatContext *s)
  338. {
  339. int64_t size, av_uninit(data_size);
  340. int64_t sample_count=0;
  341. int rf64;
  342. uint32_t tag, list_type;
  343. AVIOContext *pb = s->pb;
  344. AVStream *st = NULL;
  345. WAVContext *wav = s->priv_data;
  346. int ret, got_fmt = 0;
  347. int64_t next_tag_ofs, data_ofs = -1;
  348. wav->smv_data_ofs = -1;
  349. /* check RIFF header */
  350. tag = avio_rl32(pb);
  351. rf64 = tag == MKTAG('R', 'F', '6', '4');
  352. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  353. return -1;
  354. avio_rl32(pb); /* file size */
  355. tag = avio_rl32(pb);
  356. if (tag != MKTAG('W', 'A', 'V', 'E'))
  357. return -1;
  358. if (rf64) {
  359. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  360. return -1;
  361. size = avio_rl32(pb);
  362. if (size < 24)
  363. return -1;
  364. avio_rl64(pb); /* RIFF size */
  365. data_size = avio_rl64(pb);
  366. sample_count = avio_rl64(pb);
  367. if (data_size < 0 || sample_count < 0) {
  368. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  369. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  370. data_size, sample_count);
  371. return AVERROR_INVALIDDATA;
  372. }
  373. avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
  374. }
  375. for (;;) {
  376. AVStream *vst;
  377. size = next_tag(pb, &tag);
  378. next_tag_ofs = avio_tell(pb) + size;
  379. if (url_feof(pb))
  380. break;
  381. switch (tag) {
  382. case MKTAG('f', 'm', 't', ' '):
  383. /* only parse the first 'fmt ' tag found */
  384. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
  385. return ret;
  386. } else if (got_fmt)
  387. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  388. got_fmt = 1;
  389. break;
  390. case MKTAG('d', 'a', 't', 'a'):
  391. if (!got_fmt) {
  392. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
  393. return AVERROR_INVALIDDATA;
  394. }
  395. if (rf64) {
  396. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  397. } else {
  398. data_size = size;
  399. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  400. }
  401. data_ofs = avio_tell(pb);
  402. /* don't look for footer metadata if we can't seek or if we don't
  403. * know where the data tag ends
  404. */
  405. if (!pb->seekable || (!rf64 && !size))
  406. goto break_loop;
  407. break;
  408. case MKTAG('f','a','c','t'):
  409. if (!sample_count)
  410. sample_count = avio_rl32(pb);
  411. break;
  412. case MKTAG('b','e','x','t'):
  413. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  414. return ret;
  415. break;
  416. case MKTAG('S','M','V','0'):
  417. if (!got_fmt) {
  418. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
  419. return AVERROR_INVALIDDATA;
  420. }
  421. // SMV file, a wav file with video appended.
  422. if (size != MKTAG('0','2','0','0')) {
  423. av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
  424. goto break_loop;
  425. }
  426. av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
  427. vst = avformat_new_stream(s, NULL);
  428. if (!vst)
  429. return AVERROR(ENOMEM);
  430. avio_r8(pb);
  431. vst->id = 1;
  432. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  433. vst->codec->codec_id = AV_CODEC_ID_MJPEG;
  434. vst->codec->width = avio_rl24(pb);
  435. vst->codec->height = avio_rl24(pb);
  436. size = avio_rl24(pb);
  437. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  438. avio_rl24(pb);
  439. wav->smv_block_size = avio_rl24(pb);
  440. avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
  441. vst->duration = avio_rl24(pb);
  442. avio_rl24(pb);
  443. avio_rl24(pb);
  444. wav->smv_frames_per_jpeg = avio_rl24(pb);
  445. goto break_loop;
  446. case MKTAG('L', 'I', 'S', 'T'):
  447. list_type = avio_rl32(pb);
  448. if (size < 4) {
  449. av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
  450. return AVERROR_INVALIDDATA;
  451. }
  452. switch (list_type) {
  453. case MKTAG('I', 'N', 'F', 'O'):
  454. if ((ret = ff_read_riff_info(s, size - 4)) < 0)
  455. return ret;
  456. }
  457. break;
  458. }
  459. /* seek to next tag unless we know that we'll run into EOF */
  460. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  461. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  462. break;
  463. }
  464. }
  465. break_loop:
  466. if (data_ofs < 0) {
  467. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  468. return AVERROR_INVALIDDATA;
  469. }
  470. avio_seek(pb, data_ofs, SEEK_SET);
  471. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
  472. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  473. if (sample_count)
  474. st->duration = sample_count;
  475. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  476. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  477. return 0;
  478. }
  479. /** Find chunk with w64 GUID by skipping over other chunks
  480. * @return the size of the found chunk
  481. */
  482. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  483. {
  484. uint8_t guid[16];
  485. int64_t size;
  486. while (!url_feof(pb)) {
  487. avio_read(pb, guid, 16);
  488. size = avio_rl64(pb);
  489. if (size <= 24)
  490. return -1;
  491. if (!memcmp(guid, guid1, 16))
  492. return size;
  493. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  494. }
  495. return -1;
  496. }
  497. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  498. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  499. #define MAX_SIZE 4096
  500. static int wav_read_packet(AVFormatContext *s,
  501. AVPacket *pkt)
  502. {
  503. int ret, size;
  504. int64_t left;
  505. AVStream *st;
  506. WAVContext *wav = s->priv_data;
  507. if (wav->smv_data_ofs > 0) {
  508. int64_t audio_dts, video_dts;
  509. smv_retry:
  510. audio_dts = s->streams[0]->cur_dts;
  511. video_dts = s->streams[1]->cur_dts;
  512. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  513. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  514. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  515. wav->smv_last_stream = video_dts >= audio_dts;
  516. }
  517. wav->smv_last_stream = !wav->smv_last_stream;
  518. wav->smv_last_stream |= wav->audio_eof;
  519. wav->smv_last_stream &= !wav->smv_eof;
  520. if (wav->smv_last_stream) {
  521. uint64_t old_pos = avio_tell(s->pb);
  522. uint64_t new_pos = wav->smv_data_ofs +
  523. wav->smv_block * wav->smv_block_size;
  524. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  525. ret = AVERROR_EOF;
  526. goto smv_out;
  527. }
  528. size = avio_rl24(s->pb);
  529. ret = av_get_packet(s->pb, pkt, size);
  530. if (ret < 0)
  531. goto smv_out;
  532. pkt->pos -= 3;
  533. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  534. wav->smv_block++;
  535. pkt->stream_index = 1;
  536. smv_out:
  537. avio_seek(s->pb, old_pos, SEEK_SET);
  538. if (ret == AVERROR_EOF) {
  539. wav->smv_eof = 1;
  540. goto smv_retry;
  541. }
  542. return ret;
  543. }
  544. }
  545. st = s->streams[0];
  546. left = wav->data_end - avio_tell(s->pb);
  547. if (wav->ignore_length)
  548. left= INT_MAX;
  549. if (left <= 0){
  550. if (CONFIG_W64_DEMUXER && wav->w64)
  551. left = find_guid(s->pb, guid_data) - 24;
  552. else
  553. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  554. if (left < 0) {
  555. wav->audio_eof = 1;
  556. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  557. goto smv_retry;
  558. return AVERROR_EOF;
  559. }
  560. wav->data_end= avio_tell(s->pb) + left;
  561. }
  562. size = MAX_SIZE;
  563. if (st->codec->block_align > 1) {
  564. if (size < st->codec->block_align)
  565. size = st->codec->block_align;
  566. size = (size / st->codec->block_align) * st->codec->block_align;
  567. }
  568. size = FFMIN(size, left);
  569. ret = av_get_packet(s->pb, pkt, size);
  570. if (ret < 0)
  571. return ret;
  572. pkt->stream_index = 0;
  573. return ret;
  574. }
  575. static int wav_read_seek(AVFormatContext *s,
  576. int stream_index, int64_t timestamp, int flags)
  577. {
  578. WAVContext *wav = s->priv_data;
  579. AVStream *st;
  580. wav->smv_eof = 0;
  581. wav->audio_eof = 0;
  582. if (wav->smv_data_ofs > 0) {
  583. int64_t smv_timestamp = timestamp;
  584. if (stream_index == 0)
  585. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  586. else
  587. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  588. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  589. }
  590. st = s->streams[0];
  591. switch (st->codec->codec_id) {
  592. case AV_CODEC_ID_MP2:
  593. case AV_CODEC_ID_MP3:
  594. case AV_CODEC_ID_AC3:
  595. case AV_CODEC_ID_DTS:
  596. /* use generic seeking with dynamically generated indexes */
  597. return -1;
  598. default:
  599. break;
  600. }
  601. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  602. }
  603. #define OFFSET(x) offsetof(WAVContext, x)
  604. #define DEC AV_OPT_FLAG_DECODING_PARAM
  605. static const AVOption demux_options[] = {
  606. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  607. { NULL },
  608. };
  609. static const AVClass wav_demuxer_class = {
  610. .class_name = "WAV demuxer",
  611. .item_name = av_default_item_name,
  612. .option = demux_options,
  613. .version = LIBAVUTIL_VERSION_INT,
  614. };
  615. AVInputFormat ff_wav_demuxer = {
  616. .name = "wav",
  617. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  618. .priv_data_size = sizeof(WAVContext),
  619. .read_probe = wav_probe,
  620. .read_header = wav_read_header,
  621. .read_packet = wav_read_packet,
  622. .read_seek = wav_read_seek,
  623. .flags = AVFMT_GENERIC_INDEX,
  624. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  625. .priv_class = &wav_demuxer_class,
  626. };
  627. #endif /* CONFIG_WAV_DEMUXER */
  628. #if CONFIG_W64_DEMUXER
  629. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  630. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  631. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  632. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  633. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  634. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  635. static int w64_probe(AVProbeData *p)
  636. {
  637. if (p->buf_size <= 40)
  638. return 0;
  639. if (!memcmp(p->buf, guid_riff, 16) &&
  640. !memcmp(p->buf + 24, guid_wave, 16))
  641. return AVPROBE_SCORE_MAX;
  642. else
  643. return 0;
  644. }
  645. static int w64_read_header(AVFormatContext *s)
  646. {
  647. int64_t size;
  648. AVIOContext *pb = s->pb;
  649. WAVContext *wav = s->priv_data;
  650. AVStream *st;
  651. uint8_t guid[16];
  652. int ret;
  653. avio_read(pb, guid, 16);
  654. if (memcmp(guid, guid_riff, 16))
  655. return -1;
  656. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  657. return -1;
  658. avio_read(pb, guid, 16);
  659. if (memcmp(guid, guid_wave, 16)) {
  660. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  661. return -1;
  662. }
  663. size = find_guid(pb, guid_fmt);
  664. if (size < 0) {
  665. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  666. return -1;
  667. }
  668. st = avformat_new_stream(s, NULL);
  669. if (!st)
  670. return AVERROR(ENOMEM);
  671. /* subtract chunk header size - normal wav file doesn't count it */
  672. ret = ff_get_wav_header(pb, st->codec, size - 24);
  673. if (ret < 0)
  674. return ret;
  675. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  676. handle_stream_probing(st);
  677. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  678. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  679. size = find_guid(pb, guid_data);
  680. if (size < 0) {
  681. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  682. return -1;
  683. }
  684. wav->data_end = avio_tell(pb) + size - 24;
  685. wav->w64 = 1;
  686. return 0;
  687. }
  688. AVInputFormat ff_w64_demuxer = {
  689. .name = "w64",
  690. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  691. .priv_data_size = sizeof(WAVContext),
  692. .read_probe = w64_probe,
  693. .read_header = w64_read_header,
  694. .read_packet = wav_read_packet,
  695. .read_seek = wav_read_seek,
  696. .flags = AVFMT_GENERIC_INDEX,
  697. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  698. };
  699. #endif /* CONFIG_W64_DEMUXER */