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.

579 lines
16KB

  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 "avformat.h"
  26. #include "avio_internal.h"
  27. #include "pcm.h"
  28. #include "riff.h"
  29. typedef struct {
  30. int64_t data;
  31. int64_t data_end;
  32. int64_t minpts;
  33. int64_t maxpts;
  34. int last_duration;
  35. int w64;
  36. } WAVContext;
  37. #if CONFIG_WAV_MUXER
  38. static int wav_write_header(AVFormatContext *s)
  39. {
  40. WAVContext *wav = s->priv_data;
  41. AVIOContext *pb = s->pb;
  42. int64_t fmt, fact;
  43. ffio_wfourcc(pb, "RIFF");
  44. avio_wl32(pb, 0); /* file length */
  45. ffio_wfourcc(pb, "WAVE");
  46. /* format header */
  47. fmt = ff_start_tag(pb, "fmt ");
  48. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  49. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  50. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  51. return -1;
  52. }
  53. ff_end_tag(pb, fmt);
  54. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  55. && s->pb->seekable) {
  56. fact = ff_start_tag(pb, "fact");
  57. avio_wl32(pb, 0);
  58. ff_end_tag(pb, fact);
  59. }
  60. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  61. wav->maxpts = wav->last_duration = 0;
  62. wav->minpts = INT64_MAX;
  63. /* data header */
  64. wav->data = ff_start_tag(pb, "data");
  65. avio_flush(pb);
  66. return 0;
  67. }
  68. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  69. {
  70. AVIOContext *pb = s->pb;
  71. WAVContext *wav = s->priv_data;
  72. avio_write(pb, pkt->data, pkt->size);
  73. if(pkt->pts != AV_NOPTS_VALUE) {
  74. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  75. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  76. wav->last_duration = pkt->duration;
  77. } else
  78. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  79. return 0;
  80. }
  81. static int wav_write_trailer(AVFormatContext *s)
  82. {
  83. AVIOContext *pb = s->pb;
  84. WAVContext *wav = s->priv_data;
  85. int64_t file_size;
  86. avio_flush(pb);
  87. if (s->pb->seekable) {
  88. ff_end_tag(pb, wav->data);
  89. /* update file size */
  90. file_size = avio_tell(pb);
  91. avio_seek(pb, 4, SEEK_SET);
  92. avio_wl32(pb, (uint32_t)(file_size - 8));
  93. avio_seek(pb, file_size, SEEK_SET);
  94. avio_flush(pb);
  95. if(s->streams[0]->codec->codec_tag != 0x01) {
  96. /* Update num_samps in fact chunk */
  97. int number_of_samples;
  98. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  99. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  100. s->streams[0]->time_base.den);
  101. avio_seek(pb, wav->data-12, SEEK_SET);
  102. avio_wl32(pb, number_of_samples);
  103. avio_seek(pb, file_size, SEEK_SET);
  104. avio_flush(pb);
  105. }
  106. }
  107. return 0;
  108. }
  109. AVOutputFormat ff_wav_muxer = {
  110. "wav",
  111. NULL_IF_CONFIG_SMALL("WAV format"),
  112. "audio/x-wav",
  113. "wav",
  114. sizeof(WAVContext),
  115. CODEC_ID_PCM_S16LE,
  116. CODEC_ID_NONE,
  117. wav_write_header,
  118. wav_write_packet,
  119. wav_write_trailer,
  120. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  121. };
  122. #endif /* CONFIG_WAV_MUXER */
  123. #if CONFIG_WAV_DEMUXER
  124. static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
  125. {
  126. *tag = avio_rl32(pb);
  127. return avio_rl32(pb);
  128. }
  129. /* return the size of the found tag */
  130. static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
  131. {
  132. unsigned int tag;
  133. int64_t size;
  134. for (;;) {
  135. if (url_feof(pb))
  136. return -1;
  137. size = next_tag(pb, &tag);
  138. if (tag == tag1)
  139. break;
  140. avio_skip(pb, size);
  141. }
  142. return size;
  143. }
  144. static int wav_probe(AVProbeData *p)
  145. {
  146. /* check file header */
  147. if (p->buf_size <= 32)
  148. return 0;
  149. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  150. if (!memcmp(p->buf, "RIFF", 4))
  151. /*
  152. Since ACT demuxer has standard WAV header at top of it's own,
  153. returning score is decreased to avoid probe conflict
  154. between ACT and WAV.
  155. */
  156. return AVPROBE_SCORE_MAX - 1;
  157. else if (!memcmp(p->buf, "RF64", 4) &&
  158. !memcmp(p->buf + 12, "ds64", 4))
  159. return AVPROBE_SCORE_MAX;
  160. }
  161. return 0;
  162. }
  163. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  164. {
  165. AVIOContext *pb = s->pb;
  166. int ret;
  167. /* parse fmt header */
  168. *st = av_new_stream(s, 0);
  169. if (!*st)
  170. return AVERROR(ENOMEM);
  171. ret = ff_get_wav_header(pb, (*st)->codec, size);
  172. if (ret < 0)
  173. return ret;
  174. (*st)->need_parsing = AVSTREAM_PARSE_FULL;
  175. av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  176. return 0;
  177. }
  178. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key, int length)
  179. {
  180. char temp[257];
  181. int ret;
  182. if ((ret = avio_read(s->pb, temp, length)) < 0)
  183. return ret;
  184. temp[length] = 0;
  185. if (strlen(temp))
  186. return av_metadata_set2(&s->metadata, key, temp, 0);
  187. return 0;
  188. }
  189. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  190. {
  191. char temp[131], *coding_history;
  192. int ret, x;
  193. uint64_t time_reference;
  194. int64_t umid_parts[8], umid_mask = 0;
  195. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  196. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  197. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  198. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  199. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  200. return ret;
  201. time_reference = avio_rl64(s->pb);
  202. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  203. if ((ret = av_metadata_set2(&s->metadata, "time_reference", temp, 0)) < 0)
  204. return ret;
  205. /* check if version is >= 1, in which case an UMID may be present */
  206. if (avio_rl16(s->pb) >= 1) {
  207. for (x = 0; x < 8; x++)
  208. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  209. if (umid_mask) {
  210. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  211. if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
  212. /* basic UMID */
  213. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  214. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
  215. } else {
  216. /* extended UMID */
  217. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  218. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  219. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
  220. umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
  221. }
  222. if ((ret = av_metadata_set2(&s->metadata, "umid", temp, 0)) < 0)
  223. return ret;
  224. }
  225. avio_skip(s->pb, 190);
  226. } else
  227. avio_skip(s->pb, 254);
  228. if (size > 602) {
  229. /* CodingHistory present */
  230. size -= 602;
  231. if (!(coding_history = av_malloc(size+1)))
  232. return AVERROR(ENOMEM);
  233. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  234. return ret;
  235. coding_history[size] = 0;
  236. if ((ret = av_metadata_set2(&s->metadata, "coding_history", coding_history,
  237. AV_METADATA_DONT_STRDUP_VAL)) < 0)
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. /* wav input */
  243. static int wav_read_header(AVFormatContext *s,
  244. AVFormatParameters *ap)
  245. {
  246. int64_t size, av_uninit(data_size);
  247. int64_t sample_count=0;
  248. int rf64;
  249. unsigned int tag;
  250. AVIOContext *pb = s->pb;
  251. AVStream *st;
  252. WAVContext *wav = s->priv_data;
  253. int ret, got_fmt = 0;
  254. int64_t next_tag_ofs, data_ofs = -1;
  255. /* check RIFF header */
  256. tag = avio_rl32(pb);
  257. rf64 = tag == MKTAG('R', 'F', '6', '4');
  258. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  259. return -1;
  260. avio_rl32(pb); /* file size */
  261. tag = avio_rl32(pb);
  262. if (tag != MKTAG('W', 'A', 'V', 'E'))
  263. return -1;
  264. if (rf64) {
  265. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  266. return -1;
  267. size = avio_rl32(pb);
  268. if (size < 16)
  269. return -1;
  270. avio_rl64(pb); /* RIFF size */
  271. data_size = avio_rl64(pb);
  272. sample_count = avio_rl64(pb);
  273. if (data_size < 0 || sample_count < 0) {
  274. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  275. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  276. data_size, sample_count);
  277. return AVERROR_INVALIDDATA;
  278. }
  279. avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
  280. }
  281. for (;;) {
  282. size = next_tag(pb, &tag);
  283. next_tag_ofs = avio_tell(pb) + size;
  284. if (url_feof(pb)) {
  285. if (data_ofs < 0) {
  286. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  287. return AVERROR_INVALIDDATA;
  288. }
  289. break;
  290. }
  291. switch (tag) {
  292. case MKTAG('f', 'm', 't', ' '):
  293. /* only parse the first 'fmt ' tag found */
  294. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
  295. return ret;
  296. } else if (got_fmt)
  297. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  298. got_fmt = 1;
  299. break;
  300. case MKTAG('d', 'a', 't', 'a'):
  301. if (!got_fmt) {
  302. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
  303. return AVERROR_INVALIDDATA;
  304. }
  305. if (rf64) {
  306. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  307. } else {
  308. data_size = size;
  309. wav->data_end = size ? next_tag_ofs : INT64_MAX;
  310. }
  311. /* don't look for footer metadata if we can't seek or if we don't
  312. * know where the data tag ends
  313. */
  314. if (!pb->seekable || (!rf64 && !size))
  315. goto break_loop;
  316. data_ofs = avio_tell(pb);
  317. break;
  318. case MKTAG('f','a','c','t'):
  319. if(!sample_count)
  320. sample_count = avio_rl32(pb);
  321. break;
  322. case MKTAG('b','e','x','t'):
  323. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  324. return ret;
  325. break;
  326. }
  327. avio_seek(pb, next_tag_ofs, SEEK_SET);
  328. }
  329. break_loop:
  330. if (data_ofs >= 0)
  331. avio_seek(pb, data_ofs, SEEK_SET);
  332. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
  333. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  334. if (sample_count)
  335. st->duration = sample_count;
  336. return 0;
  337. }
  338. /** Find chunk with w64 GUID by skipping over other chunks
  339. * @return the size of the found chunk
  340. */
  341. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  342. {
  343. uint8_t guid[16];
  344. int64_t size;
  345. while (!url_feof(pb)) {
  346. avio_read(pb, guid, 16);
  347. size = avio_rl64(pb);
  348. if (size <= 24)
  349. return -1;
  350. if (!memcmp(guid, guid1, 16))
  351. return size;
  352. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  353. }
  354. return -1;
  355. }
  356. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  357. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  358. #define MAX_SIZE 4096
  359. static int wav_read_packet(AVFormatContext *s,
  360. AVPacket *pkt)
  361. {
  362. int ret, size;
  363. int64_t left;
  364. AVStream *st;
  365. WAVContext *wav = s->priv_data;
  366. st = s->streams[0];
  367. left = wav->data_end - avio_tell(s->pb);
  368. if (left <= 0){
  369. if (CONFIG_W64_DEMUXER && wav->w64)
  370. left = find_guid(s->pb, guid_data) - 24;
  371. else
  372. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  373. if (left < 0)
  374. return AVERROR_EOF;
  375. wav->data_end= avio_tell(s->pb) + left;
  376. }
  377. size = MAX_SIZE;
  378. if (st->codec->block_align > 1) {
  379. if (size < st->codec->block_align)
  380. size = st->codec->block_align;
  381. size = (size / st->codec->block_align) * st->codec->block_align;
  382. }
  383. size = FFMIN(size, left);
  384. ret = av_get_packet(s->pb, pkt, size);
  385. if (ret < 0)
  386. return ret;
  387. pkt->stream_index = 0;
  388. return ret;
  389. }
  390. static int wav_read_seek(AVFormatContext *s,
  391. int stream_index, int64_t timestamp, int flags)
  392. {
  393. AVStream *st;
  394. st = s->streams[0];
  395. switch (st->codec->codec_id) {
  396. case CODEC_ID_MP2:
  397. case CODEC_ID_MP3:
  398. case CODEC_ID_AC3:
  399. case CODEC_ID_DTS:
  400. /* use generic seeking with dynamically generated indexes */
  401. return -1;
  402. default:
  403. break;
  404. }
  405. return pcm_read_seek(s, stream_index, timestamp, flags);
  406. }
  407. AVInputFormat ff_wav_demuxer = {
  408. "wav",
  409. NULL_IF_CONFIG_SMALL("WAV format"),
  410. sizeof(WAVContext),
  411. wav_probe,
  412. wav_read_header,
  413. wav_read_packet,
  414. NULL,
  415. wav_read_seek,
  416. .flags= AVFMT_GENERIC_INDEX,
  417. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  418. };
  419. #endif /* CONFIG_WAV_DEMUXER */
  420. #if CONFIG_W64_DEMUXER
  421. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  422. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  423. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  424. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  425. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  426. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  427. static int w64_probe(AVProbeData *p)
  428. {
  429. if (p->buf_size <= 40)
  430. return 0;
  431. if (!memcmp(p->buf, guid_riff, 16) &&
  432. !memcmp(p->buf + 24, guid_wave, 16))
  433. return AVPROBE_SCORE_MAX;
  434. else
  435. return 0;
  436. }
  437. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  438. {
  439. int64_t size;
  440. AVIOContext *pb = s->pb;
  441. WAVContext *wav = s->priv_data;
  442. AVStream *st;
  443. uint8_t guid[16];
  444. int ret;
  445. avio_read(pb, guid, 16);
  446. if (memcmp(guid, guid_riff, 16))
  447. return -1;
  448. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  449. return -1;
  450. avio_read(pb, guid, 16);
  451. if (memcmp(guid, guid_wave, 16)) {
  452. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  453. return -1;
  454. }
  455. size = find_guid(pb, guid_fmt);
  456. if (size < 0) {
  457. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  458. return -1;
  459. }
  460. st = av_new_stream(s, 0);
  461. if (!st)
  462. return AVERROR(ENOMEM);
  463. /* subtract chunk header size - normal wav file doesn't count it */
  464. ret = ff_get_wav_header(pb, st->codec, size - 24);
  465. if (ret < 0)
  466. return ret;
  467. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  468. st->need_parsing = AVSTREAM_PARSE_FULL;
  469. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  470. size = find_guid(pb, guid_data);
  471. if (size < 0) {
  472. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  473. return -1;
  474. }
  475. wav->data_end = avio_tell(pb) + size - 24;
  476. wav->w64 = 1;
  477. return 0;
  478. }
  479. AVInputFormat ff_w64_demuxer = {
  480. "w64",
  481. NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  482. sizeof(WAVContext),
  483. w64_probe,
  484. w64_read_header,
  485. wav_read_packet,
  486. NULL,
  487. wav_read_seek,
  488. .flags = AVFMT_GENERIC_INDEX,
  489. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  490. };
  491. #endif /* CONFIG_W64_DEMUXER */