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.

797 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, { 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 format"),
  187. .mime_type = "audio/x-wav",
  188. .extensions = "wav",
  189. .priv_data_size = sizeof(WAVContext),
  190. .audio_codec = CODEC_ID_PCM_S16LE,
  191. .video_codec = 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 int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  241. {
  242. AVIOContext *pb = s->pb;
  243. int ret;
  244. /* parse fmt header */
  245. *st = avformat_new_stream(s, NULL);
  246. if (!*st)
  247. return AVERROR(ENOMEM);
  248. ret = ff_get_wav_header(pb, (*st)->codec, size);
  249. if (ret < 0)
  250. return ret;
  251. (*st)->need_parsing = AVSTREAM_PARSE_FULL;
  252. avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  253. return 0;
  254. }
  255. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  256. int length)
  257. {
  258. char temp[257];
  259. int ret;
  260. av_assert0(length <= sizeof(temp));
  261. if ((ret = avio_read(s->pb, temp, length)) < 0)
  262. return ret;
  263. temp[length] = 0;
  264. if (strlen(temp))
  265. return av_dict_set(&s->metadata, key, temp, 0);
  266. return 0;
  267. }
  268. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  269. {
  270. char temp[131], *coding_history;
  271. int ret, x;
  272. uint64_t time_reference;
  273. int64_t umid_parts[8], umid_mask = 0;
  274. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  275. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  276. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  277. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  278. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  279. return ret;
  280. time_reference = avio_rl64(s->pb);
  281. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  282. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  283. return ret;
  284. /* check if version is >= 1, in which case an UMID may be present */
  285. if (avio_rl16(s->pb) >= 1) {
  286. for (x = 0; x < 8; x++)
  287. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  288. if (umid_mask) {
  289. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  290. if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
  291. /* basic UMID */
  292. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  293. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
  294. } else {
  295. /* extended UMID */
  296. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  297. "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  298. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
  299. umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
  300. }
  301. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  302. return ret;
  303. }
  304. avio_skip(s->pb, 190);
  305. } else
  306. avio_skip(s->pb, 254);
  307. if (size > 602) {
  308. /* CodingHistory present */
  309. size -= 602;
  310. if (!(coding_history = av_malloc(size+1)))
  311. return AVERROR(ENOMEM);
  312. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  313. return ret;
  314. coding_history[size] = 0;
  315. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  316. AV_DICT_DONT_STRDUP_VAL)) < 0)
  317. return ret;
  318. }
  319. return 0;
  320. }
  321. static const AVMetadataConv wav_metadata_conv[] = {
  322. {"description", "comment" },
  323. {"originator", "encoded_by" },
  324. {"origination_date", "date" },
  325. {"origination_time", "creation_time"},
  326. {0},
  327. };
  328. /* wav input */
  329. static int wav_read_header(AVFormatContext *s)
  330. {
  331. int64_t size, av_uninit(data_size);
  332. int64_t sample_count=0;
  333. int rf64;
  334. uint32_t tag, list_type;
  335. AVIOContext *pb = s->pb;
  336. AVStream *st = NULL;
  337. WAVContext *wav = s->priv_data;
  338. int ret, got_fmt = 0;
  339. int64_t next_tag_ofs, data_ofs = -1;
  340. wav->smv_data_ofs = -1;
  341. /* check RIFF header */
  342. tag = avio_rl32(pb);
  343. rf64 = tag == MKTAG('R', 'F', '6', '4');
  344. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  345. return -1;
  346. avio_rl32(pb); /* file size */
  347. tag = avio_rl32(pb);
  348. if (tag != MKTAG('W', 'A', 'V', 'E'))
  349. return -1;
  350. if (rf64) {
  351. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  352. return -1;
  353. size = avio_rl32(pb);
  354. if (size < 24)
  355. return -1;
  356. avio_rl64(pb); /* RIFF size */
  357. data_size = avio_rl64(pb);
  358. sample_count = avio_rl64(pb);
  359. if (data_size < 0 || sample_count < 0) {
  360. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  361. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  362. data_size, sample_count);
  363. return AVERROR_INVALIDDATA;
  364. }
  365. avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
  366. }
  367. for (;;) {
  368. AVStream *vst;
  369. size = next_tag(pb, &tag);
  370. next_tag_ofs = avio_tell(pb) + size;
  371. if (url_feof(pb))
  372. break;
  373. switch (tag) {
  374. case MKTAG('f', 'm', 't', ' '):
  375. /* only parse the first 'fmt ' tag found */
  376. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
  377. return ret;
  378. } else if (got_fmt)
  379. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  380. got_fmt = 1;
  381. break;
  382. case MKTAG('d', 'a', 't', 'a'):
  383. if (!got_fmt) {
  384. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
  385. return AVERROR_INVALIDDATA;
  386. }
  387. if (rf64) {
  388. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  389. } else {
  390. data_size = size;
  391. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  392. }
  393. data_ofs = avio_tell(pb);
  394. /* don't look for footer metadata if we can't seek or if we don't
  395. * know where the data tag ends
  396. */
  397. if (!pb->seekable || (!rf64 && !size))
  398. goto break_loop;
  399. break;
  400. case MKTAG('f','a','c','t'):
  401. if (!sample_count)
  402. sample_count = avio_rl32(pb);
  403. break;
  404. case MKTAG('b','e','x','t'):
  405. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  406. return ret;
  407. break;
  408. case MKTAG('S','M','V','0'):
  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. vst = avformat_new_stream(s, NULL);
  416. if (!vst)
  417. return AVERROR(ENOMEM);
  418. avio_r8(pb);
  419. vst->id = 1;
  420. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  421. vst->codec->codec_id = CODEC_ID_MJPEG;
  422. vst->codec->width = avio_rl24(pb);
  423. vst->codec->height = avio_rl24(pb);
  424. size = avio_rl24(pb);
  425. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  426. avio_rl24(pb);
  427. wav->smv_block_size = avio_rl24(pb);
  428. avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
  429. vst->duration = avio_rl24(pb);
  430. avio_rl24(pb);
  431. avio_rl24(pb);
  432. wav->smv_frames_per_jpeg = avio_rl24(pb);
  433. goto break_loop;
  434. case MKTAG('L', 'I', 'S', 'T'):
  435. list_type = avio_rl32(pb);
  436. if (size < 4) {
  437. av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
  438. return AVERROR_INVALIDDATA;
  439. }
  440. switch (list_type) {
  441. case MKTAG('I', 'N', 'F', 'O'):
  442. if ((ret = ff_read_riff_info(s, size - 4)) < 0)
  443. return ret;
  444. }
  445. break;
  446. }
  447. /* seek to next tag unless we know that we'll run into EOF */
  448. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  449. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  450. break;
  451. }
  452. }
  453. break_loop:
  454. if (data_ofs < 0) {
  455. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  456. return AVERROR_INVALIDDATA;
  457. }
  458. avio_seek(pb, data_ofs, SEEK_SET);
  459. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
  460. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  461. if (sample_count)
  462. st->duration = sample_count;
  463. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  464. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  465. return 0;
  466. }
  467. /** Find chunk with w64 GUID by skipping over other chunks
  468. * @return the size of the found chunk
  469. */
  470. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  471. {
  472. uint8_t guid[16];
  473. int64_t size;
  474. while (!url_feof(pb)) {
  475. avio_read(pb, guid, 16);
  476. size = avio_rl64(pb);
  477. if (size <= 24)
  478. return -1;
  479. if (!memcmp(guid, guid1, 16))
  480. return size;
  481. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  482. }
  483. return -1;
  484. }
  485. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  486. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  487. #define MAX_SIZE 4096
  488. static int wav_read_packet(AVFormatContext *s,
  489. AVPacket *pkt)
  490. {
  491. int ret, size;
  492. int64_t left;
  493. AVStream *st;
  494. WAVContext *wav = s->priv_data;
  495. if (wav->smv_data_ofs > 0) {
  496. int64_t audio_dts, video_dts;
  497. smv_retry:
  498. audio_dts = s->streams[0]->cur_dts;
  499. video_dts = s->streams[1]->cur_dts;
  500. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  501. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  502. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  503. wav->smv_last_stream = video_dts >= audio_dts;
  504. }
  505. wav->smv_last_stream = !wav->smv_last_stream;
  506. wav->smv_last_stream |= wav->audio_eof;
  507. wav->smv_last_stream &= !wav->smv_eof;
  508. if (wav->smv_last_stream) {
  509. uint64_t old_pos = avio_tell(s->pb);
  510. uint64_t new_pos = wav->smv_data_ofs +
  511. wav->smv_block * wav->smv_block_size;
  512. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  513. ret = AVERROR_EOF;
  514. goto smv_out;
  515. }
  516. size = avio_rl24(s->pb);
  517. ret = av_get_packet(s->pb, pkt, size);
  518. if (ret < 0)
  519. goto smv_out;
  520. pkt->pos -= 3;
  521. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  522. wav->smv_block++;
  523. pkt->stream_index = 1;
  524. smv_out:
  525. avio_seek(s->pb, old_pos, SEEK_SET);
  526. if (ret == AVERROR_EOF) {
  527. wav->smv_eof = 1;
  528. goto smv_retry;
  529. }
  530. return ret;
  531. }
  532. }
  533. st = s->streams[0];
  534. left = wav->data_end - avio_tell(s->pb);
  535. if (wav->ignore_length)
  536. left= INT_MAX;
  537. if (left <= 0){
  538. if (CONFIG_W64_DEMUXER && wav->w64)
  539. left = find_guid(s->pb, guid_data) - 24;
  540. else
  541. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  542. if (left < 0) {
  543. wav->audio_eof = 1;
  544. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  545. goto smv_retry;
  546. return AVERROR_EOF;
  547. }
  548. wav->data_end= avio_tell(s->pb) + left;
  549. }
  550. size = MAX_SIZE;
  551. if (st->codec->block_align > 1) {
  552. if (size < st->codec->block_align)
  553. size = st->codec->block_align;
  554. size = (size / st->codec->block_align) * st->codec->block_align;
  555. }
  556. size = FFMIN(size, left);
  557. ret = av_get_packet(s->pb, pkt, size);
  558. if (ret < 0)
  559. return ret;
  560. pkt->stream_index = 0;
  561. return ret;
  562. }
  563. static int wav_read_seek(AVFormatContext *s,
  564. int stream_index, int64_t timestamp, int flags)
  565. {
  566. WAVContext *wav = s->priv_data;
  567. AVStream *st;
  568. wav->smv_eof = 0;
  569. wav->audio_eof = 0;
  570. if (wav->smv_data_ofs > 0) {
  571. int64_t smv_timestamp = timestamp;
  572. if (stream_index == 0)
  573. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  574. else
  575. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  576. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  577. }
  578. st = s->streams[0];
  579. switch (st->codec->codec_id) {
  580. case CODEC_ID_MP2:
  581. case CODEC_ID_MP3:
  582. case CODEC_ID_AC3:
  583. case CODEC_ID_DTS:
  584. /* use generic seeking with dynamically generated indexes */
  585. return -1;
  586. default:
  587. break;
  588. }
  589. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  590. }
  591. #define OFFSET(x) offsetof(WAVContext, x)
  592. #define DEC AV_OPT_FLAG_DECODING_PARAM
  593. static const AVOption demux_options[] = {
  594. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { 0 }, 0, 1, DEC },
  595. { NULL },
  596. };
  597. static const AVClass wav_demuxer_class = {
  598. .class_name = "WAV demuxer",
  599. .item_name = av_default_item_name,
  600. .option = demux_options,
  601. .version = LIBAVUTIL_VERSION_INT,
  602. };
  603. AVInputFormat ff_wav_demuxer = {
  604. .name = "wav",
  605. .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
  606. .priv_data_size = sizeof(WAVContext),
  607. .read_probe = wav_probe,
  608. .read_header = wav_read_header,
  609. .read_packet = wav_read_packet,
  610. .read_seek = wav_read_seek,
  611. .flags = AVFMT_GENERIC_INDEX,
  612. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  613. .priv_class = &wav_demuxer_class,
  614. };
  615. #endif /* CONFIG_WAV_DEMUXER */
  616. #if CONFIG_W64_DEMUXER
  617. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  618. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  619. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  620. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  621. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  622. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  623. static int w64_probe(AVProbeData *p)
  624. {
  625. if (p->buf_size <= 40)
  626. return 0;
  627. if (!memcmp(p->buf, guid_riff, 16) &&
  628. !memcmp(p->buf + 24, guid_wave, 16))
  629. return AVPROBE_SCORE_MAX;
  630. else
  631. return 0;
  632. }
  633. static int w64_read_header(AVFormatContext *s)
  634. {
  635. int64_t size;
  636. AVIOContext *pb = s->pb;
  637. WAVContext *wav = s->priv_data;
  638. AVStream *st;
  639. uint8_t guid[16];
  640. int ret;
  641. avio_read(pb, guid, 16);
  642. if (memcmp(guid, guid_riff, 16))
  643. return -1;
  644. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  645. return -1;
  646. avio_read(pb, guid, 16);
  647. if (memcmp(guid, guid_wave, 16)) {
  648. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  649. return -1;
  650. }
  651. size = find_guid(pb, guid_fmt);
  652. if (size < 0) {
  653. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  654. return -1;
  655. }
  656. st = avformat_new_stream(s, NULL);
  657. if (!st)
  658. return AVERROR(ENOMEM);
  659. /* subtract chunk header size - normal wav file doesn't count it */
  660. ret = ff_get_wav_header(pb, st->codec, size - 24);
  661. if (ret < 0)
  662. return ret;
  663. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  664. st->need_parsing = AVSTREAM_PARSE_FULL;
  665. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  666. size = find_guid(pb, guid_data);
  667. if (size < 0) {
  668. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  669. return -1;
  670. }
  671. wav->data_end = avio_tell(pb) + size - 24;
  672. wav->w64 = 1;
  673. return 0;
  674. }
  675. AVInputFormat ff_w64_demuxer = {
  676. .name = "w64",
  677. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  678. .priv_data_size = sizeof(WAVContext),
  679. .read_probe = w64_probe,
  680. .read_header = w64_read_header,
  681. .read_packet = wav_read_packet,
  682. .read_seek = wav_read_seek,
  683. .flags = AVFMT_GENERIC_INDEX,
  684. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  685. };
  686. #endif /* CONFIG_W64_DEMUXER */