wav.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. ff_read_riff_info(s, size - 4);
  455. }
  456. break;
  457. }
  458. /* seek to next tag unless we know that we'll run into EOF */
  459. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  460. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  461. break;
  462. }
  463. }
  464. break_loop:
  465. if (data_ofs < 0) {
  466. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  467. return AVERROR_INVALIDDATA;
  468. }
  469. avio_seek(pb, data_ofs, SEEK_SET);
  470. if (!sample_count || av_get_exact_bits_per_sample(st->codec->codec_id) > 0)
  471. if ( st->codec->channels
  472. && data_size
  473. && av_get_bits_per_sample(st->codec->codec_id)
  474. && wav->data_end <= avio_size(pb))
  475. sample_count = (data_size << 3)
  476. /
  477. (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  478. if (sample_count)
  479. st->duration = sample_count;
  480. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  481. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  482. return 0;
  483. }
  484. /** Find chunk with w64 GUID by skipping over other chunks
  485. * @return the size of the found chunk
  486. */
  487. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  488. {
  489. uint8_t guid[16];
  490. int64_t size;
  491. while (!url_feof(pb)) {
  492. avio_read(pb, guid, 16);
  493. size = avio_rl64(pb);
  494. if (size <= 24)
  495. return -1;
  496. if (!memcmp(guid, guid1, 16))
  497. return size;
  498. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  499. }
  500. return -1;
  501. }
  502. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  503. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  504. #define MAX_SIZE 4096
  505. static int wav_read_packet(AVFormatContext *s,
  506. AVPacket *pkt)
  507. {
  508. int ret, size;
  509. int64_t left;
  510. AVStream *st;
  511. WAVContext *wav = s->priv_data;
  512. if (wav->smv_data_ofs > 0) {
  513. int64_t audio_dts, video_dts;
  514. smv_retry:
  515. audio_dts = s->streams[0]->cur_dts;
  516. video_dts = s->streams[1]->cur_dts;
  517. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  518. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  519. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  520. wav->smv_last_stream = video_dts >= audio_dts;
  521. }
  522. wav->smv_last_stream = !wav->smv_last_stream;
  523. wav->smv_last_stream |= wav->audio_eof;
  524. wav->smv_last_stream &= !wav->smv_eof;
  525. if (wav->smv_last_stream) {
  526. uint64_t old_pos = avio_tell(s->pb);
  527. uint64_t new_pos = wav->smv_data_ofs +
  528. wav->smv_block * wav->smv_block_size;
  529. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  530. ret = AVERROR_EOF;
  531. goto smv_out;
  532. }
  533. size = avio_rl24(s->pb);
  534. ret = av_get_packet(s->pb, pkt, size);
  535. if (ret < 0)
  536. goto smv_out;
  537. pkt->pos -= 3;
  538. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  539. wav->smv_block++;
  540. pkt->stream_index = 1;
  541. smv_out:
  542. avio_seek(s->pb, old_pos, SEEK_SET);
  543. if (ret == AVERROR_EOF) {
  544. wav->smv_eof = 1;
  545. goto smv_retry;
  546. }
  547. return ret;
  548. }
  549. }
  550. st = s->streams[0];
  551. left = wav->data_end - avio_tell(s->pb);
  552. if (wav->ignore_length)
  553. left= INT_MAX;
  554. if (left <= 0){
  555. if (CONFIG_W64_DEMUXER && wav->w64)
  556. left = find_guid(s->pb, guid_data) - 24;
  557. else
  558. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  559. if (left < 0) {
  560. wav->audio_eof = 1;
  561. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  562. goto smv_retry;
  563. return AVERROR_EOF;
  564. }
  565. wav->data_end= avio_tell(s->pb) + left;
  566. }
  567. size = MAX_SIZE;
  568. if (st->codec->block_align > 1) {
  569. if (size < st->codec->block_align)
  570. size = st->codec->block_align;
  571. size = (size / st->codec->block_align) * st->codec->block_align;
  572. }
  573. size = FFMIN(size, left);
  574. ret = av_get_packet(s->pb, pkt, size);
  575. if (ret < 0)
  576. return ret;
  577. pkt->stream_index = 0;
  578. return ret;
  579. }
  580. static int wav_read_seek(AVFormatContext *s,
  581. int stream_index, int64_t timestamp, int flags)
  582. {
  583. WAVContext *wav = s->priv_data;
  584. AVStream *st;
  585. wav->smv_eof = 0;
  586. wav->audio_eof = 0;
  587. if (wav->smv_data_ofs > 0) {
  588. int64_t smv_timestamp = timestamp;
  589. if (stream_index == 0)
  590. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  591. else
  592. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  593. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  594. }
  595. st = s->streams[0];
  596. switch (st->codec->codec_id) {
  597. case AV_CODEC_ID_MP2:
  598. case AV_CODEC_ID_MP3:
  599. case AV_CODEC_ID_AC3:
  600. case AV_CODEC_ID_DTS:
  601. /* use generic seeking with dynamically generated indexes */
  602. return -1;
  603. default:
  604. break;
  605. }
  606. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  607. }
  608. #define OFFSET(x) offsetof(WAVContext, x)
  609. #define DEC AV_OPT_FLAG_DECODING_PARAM
  610. static const AVOption demux_options[] = {
  611. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  612. { NULL },
  613. };
  614. static const AVClass wav_demuxer_class = {
  615. .class_name = "WAV demuxer",
  616. .item_name = av_default_item_name,
  617. .option = demux_options,
  618. .version = LIBAVUTIL_VERSION_INT,
  619. };
  620. AVInputFormat ff_wav_demuxer = {
  621. .name = "wav",
  622. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  623. .priv_data_size = sizeof(WAVContext),
  624. .read_probe = wav_probe,
  625. .read_header = wav_read_header,
  626. .read_packet = wav_read_packet,
  627. .read_seek = wav_read_seek,
  628. .flags = AVFMT_GENERIC_INDEX,
  629. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  630. .priv_class = &wav_demuxer_class,
  631. };
  632. #endif /* CONFIG_WAV_DEMUXER */
  633. #if CONFIG_W64_DEMUXER
  634. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  635. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  636. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  637. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  638. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  639. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  640. static int w64_probe(AVProbeData *p)
  641. {
  642. if (p->buf_size <= 40)
  643. return 0;
  644. if (!memcmp(p->buf, guid_riff, 16) &&
  645. !memcmp(p->buf + 24, guid_wave, 16))
  646. return AVPROBE_SCORE_MAX;
  647. else
  648. return 0;
  649. }
  650. static int w64_read_header(AVFormatContext *s)
  651. {
  652. int64_t size;
  653. AVIOContext *pb = s->pb;
  654. WAVContext *wav = s->priv_data;
  655. AVStream *st;
  656. uint8_t guid[16];
  657. int ret;
  658. avio_read(pb, guid, 16);
  659. if (memcmp(guid, guid_riff, 16))
  660. return -1;
  661. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  662. return -1;
  663. avio_read(pb, guid, 16);
  664. if (memcmp(guid, guid_wave, 16)) {
  665. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  666. return -1;
  667. }
  668. size = find_guid(pb, guid_fmt);
  669. if (size < 0) {
  670. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  671. return -1;
  672. }
  673. st = avformat_new_stream(s, NULL);
  674. if (!st)
  675. return AVERROR(ENOMEM);
  676. /* subtract chunk header size - normal wav file doesn't count it */
  677. ret = ff_get_wav_header(pb, st->codec, size - 24);
  678. if (ret < 0)
  679. return ret;
  680. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  681. handle_stream_probing(st);
  682. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  683. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  684. size = find_guid(pb, guid_data);
  685. if (size < 0) {
  686. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  687. return -1;
  688. }
  689. wav->data_end = avio_tell(pb) + size - 24;
  690. wav->w64 = 1;
  691. return 0;
  692. }
  693. AVInputFormat ff_w64_demuxer = {
  694. .name = "w64",
  695. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  696. .priv_data_size = sizeof(WAVContext),
  697. .read_probe = w64_probe,
  698. .read_header = w64_read_header,
  699. .read_packet = wav_read_packet,
  700. .read_seek = wav_read_seek,
  701. .flags = AVFMT_GENERIC_INDEX,
  702. .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
  703. };
  704. #endif /* CONFIG_W64_DEMUXER */