wav.c 24 KB

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