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.

596 lines
17KB

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