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.

416 lines
11KB

  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 "raw.h"
  27. #include "riff.h"
  28. typedef struct {
  29. int64_t data;
  30. int64_t data_end;
  31. int64_t minpts;
  32. int64_t maxpts;
  33. int last_duration;
  34. int w64;
  35. } WAVContext;
  36. #if CONFIG_WAV_MUXER
  37. static int wav_write_header(AVFormatContext *s)
  38. {
  39. WAVContext *wav = s->priv_data;
  40. ByteIOContext *pb = s->pb;
  41. int64_t fmt, fact;
  42. put_tag(pb, "RIFF");
  43. put_le32(pb, 0); /* file length */
  44. put_tag(pb, "WAVE");
  45. /* format header */
  46. fmt = ff_start_tag(pb, "fmt ");
  47. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  48. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  49. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  50. av_free(wav);
  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. && !url_is_streamed(s->pb)) {
  56. fact = ff_start_tag(pb, "fact");
  57. put_le32(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. put_flush_packet(pb);
  66. return 0;
  67. }
  68. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  69. {
  70. ByteIOContext *pb = s->pb;
  71. WAVContext *wav = s->priv_data;
  72. put_buffer(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. ByteIOContext *pb = s->pb;
  84. WAVContext *wav = s->priv_data;
  85. int64_t file_size;
  86. if (!url_is_streamed(s->pb)) {
  87. ff_end_tag(pb, wav->data);
  88. /* update file size */
  89. file_size = url_ftell(pb);
  90. url_fseek(pb, 4, SEEK_SET);
  91. put_le32(pb, (uint32_t)(file_size - 8));
  92. url_fseek(pb, file_size, SEEK_SET);
  93. put_flush_packet(pb);
  94. if(s->streams[0]->codec->codec_tag != 0x01) {
  95. /* Update num_samps in fact chunk */
  96. int number_of_samples;
  97. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  98. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  99. s->streams[0]->time_base.den);
  100. url_fseek(pb, wav->data-12, SEEK_SET);
  101. put_le32(pb, number_of_samples);
  102. url_fseek(pb, file_size, SEEK_SET);
  103. put_flush_packet(pb);
  104. }
  105. }
  106. return 0;
  107. }
  108. AVOutputFormat wav_muxer = {
  109. "wav",
  110. NULL_IF_CONFIG_SMALL("WAV format"),
  111. "audio/x-wav",
  112. "wav",
  113. sizeof(WAVContext),
  114. CODEC_ID_PCM_S16LE,
  115. CODEC_ID_NONE,
  116. wav_write_header,
  117. wav_write_packet,
  118. wav_write_trailer,
  119. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  120. };
  121. #endif /* CONFIG_WAV_MUXER */
  122. #if CONFIG_WAV_DEMUXER
  123. /* return the size of the found tag */
  124. static int64_t find_tag(ByteIOContext *pb, uint32_t tag1)
  125. {
  126. unsigned int tag;
  127. int64_t size;
  128. for (;;) {
  129. if (url_feof(pb))
  130. return -1;
  131. tag = get_le32(pb);
  132. size = get_le32(pb);
  133. if (tag == tag1)
  134. break;
  135. url_fseek(pb, size, SEEK_CUR);
  136. }
  137. return size;
  138. }
  139. static int wav_probe(AVProbeData *p)
  140. {
  141. /* check file header */
  142. if (p->buf_size <= 32)
  143. return 0;
  144. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  145. if (!memcmp(p->buf, "RIFF", 4))
  146. /*
  147. Since ACT demuxer has standard WAV header at top of it's own,
  148. returning score is decreased to avoid probe conflict
  149. between ACT and WAV.
  150. */
  151. return AVPROBE_SCORE_MAX - 1;
  152. else if (!memcmp(p->buf, "RF64", 4) &&
  153. !memcmp(p->buf + 12, "ds64", 4))
  154. return AVPROBE_SCORE_MAX;
  155. }
  156. return 0;
  157. }
  158. /* wav input */
  159. static int wav_read_header(AVFormatContext *s,
  160. AVFormatParameters *ap)
  161. {
  162. int64_t size, av_uninit(data_size);
  163. int rf64;
  164. unsigned int tag;
  165. ByteIOContext *pb = s->pb;
  166. AVStream *st;
  167. WAVContext *wav = s->priv_data;
  168. /* check RIFF header */
  169. tag = get_le32(pb);
  170. rf64 = tag == MKTAG('R', 'F', '6', '4');
  171. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  172. return -1;
  173. get_le32(pb); /* file size */
  174. tag = get_le32(pb);
  175. if (tag != MKTAG('W', 'A', 'V', 'E'))
  176. return -1;
  177. if (rf64) {
  178. if (get_le32(pb) != MKTAG('d', 's', '6', '4'))
  179. return -1;
  180. size = get_le32(pb);
  181. if (size < 16)
  182. return -1;
  183. get_le64(pb); /* RIFF size */
  184. data_size = get_le64(pb);
  185. url_fskip(pb, size - 16); /* skip rest of ds64 chunk */
  186. }
  187. /* parse fmt header */
  188. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  189. if (size < 0)
  190. return -1;
  191. st = av_new_stream(s, 0);
  192. if (!st)
  193. return AVERROR(ENOMEM);
  194. ff_get_wav_header(pb, st->codec, size);
  195. st->need_parsing = AVSTREAM_PARSE_FULL;
  196. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  197. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  198. if (rf64)
  199. size = data_size;
  200. if (size < 0)
  201. return -1;
  202. if (!size) {
  203. wav->data_end = INT64_MAX;
  204. } else
  205. wav->data_end= url_ftell(pb) + size;
  206. return 0;
  207. }
  208. /** Find chunk with w64 GUID by skipping over other chunks
  209. * @return the size of the found chunk
  210. */
  211. static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
  212. {
  213. uint8_t guid[16];
  214. int64_t size;
  215. while (!url_feof(pb)) {
  216. get_buffer(pb, guid, 16);
  217. size = get_le64(pb);
  218. if (size <= 24)
  219. return -1;
  220. if (!memcmp(guid, guid1, 16))
  221. return size;
  222. url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
  223. }
  224. return -1;
  225. }
  226. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  227. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  228. #define MAX_SIZE 4096
  229. static int wav_read_packet(AVFormatContext *s,
  230. AVPacket *pkt)
  231. {
  232. int ret, size;
  233. int64_t left;
  234. AVStream *st;
  235. WAVContext *wav = s->priv_data;
  236. st = s->streams[0];
  237. left = wav->data_end - url_ftell(s->pb);
  238. if (left <= 0){
  239. if (CONFIG_W64_DEMUXER && wav->w64)
  240. left = find_guid(s->pb, guid_data) - 24;
  241. else
  242. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  243. if (left < 0)
  244. return AVERROR_EOF;
  245. wav->data_end= url_ftell(s->pb) + left;
  246. }
  247. size = MAX_SIZE;
  248. if (st->codec->block_align > 1) {
  249. if (size < st->codec->block_align)
  250. size = st->codec->block_align;
  251. size = (size / st->codec->block_align) * st->codec->block_align;
  252. }
  253. size = FFMIN(size, left);
  254. ret = av_get_packet(s->pb, pkt, size);
  255. if (ret < 0)
  256. return ret;
  257. pkt->stream_index = 0;
  258. return ret;
  259. }
  260. static int wav_read_seek(AVFormatContext *s,
  261. int stream_index, int64_t timestamp, int flags)
  262. {
  263. AVStream *st;
  264. st = s->streams[0];
  265. switch (st->codec->codec_id) {
  266. case CODEC_ID_MP2:
  267. case CODEC_ID_MP3:
  268. case CODEC_ID_AC3:
  269. case CODEC_ID_DTS:
  270. /* use generic seeking with dynamically generated indexes */
  271. return -1;
  272. default:
  273. break;
  274. }
  275. return pcm_read_seek(s, stream_index, timestamp, flags);
  276. }
  277. AVInputFormat wav_demuxer = {
  278. "wav",
  279. NULL_IF_CONFIG_SMALL("WAV format"),
  280. sizeof(WAVContext),
  281. wav_probe,
  282. wav_read_header,
  283. wav_read_packet,
  284. NULL,
  285. wav_read_seek,
  286. .flags= AVFMT_GENERIC_INDEX,
  287. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  288. };
  289. #endif /* CONFIG_WAV_DEMUXER */
  290. #if CONFIG_W64_DEMUXER
  291. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  292. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  293. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  294. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  295. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  296. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  297. static int w64_probe(AVProbeData *p)
  298. {
  299. if (p->buf_size <= 40)
  300. return 0;
  301. if (!memcmp(p->buf, guid_riff, 16) &&
  302. !memcmp(p->buf + 24, guid_wave, 16))
  303. return AVPROBE_SCORE_MAX;
  304. else
  305. return 0;
  306. }
  307. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  308. {
  309. int64_t size;
  310. ByteIOContext *pb = s->pb;
  311. WAVContext *wav = s->priv_data;
  312. AVStream *st;
  313. uint8_t guid[16];
  314. get_buffer(pb, guid, 16);
  315. if (memcmp(guid, guid_riff, 16))
  316. return -1;
  317. if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  318. return -1;
  319. get_buffer(pb, guid, 16);
  320. if (memcmp(guid, guid_wave, 16)) {
  321. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  322. return -1;
  323. }
  324. size = find_guid(pb, guid_fmt);
  325. if (size < 0) {
  326. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  327. return -1;
  328. }
  329. st = av_new_stream(s, 0);
  330. if (!st)
  331. return AVERROR(ENOMEM);
  332. /* subtract chunk header size - normal wav file doesn't count it */
  333. ff_get_wav_header(pb, st->codec, size - 24);
  334. url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
  335. st->need_parsing = AVSTREAM_PARSE_FULL;
  336. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  337. size = find_guid(pb, guid_data);
  338. if (size < 0) {
  339. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  340. return -1;
  341. }
  342. wav->data_end = url_ftell(pb) + size - 24;
  343. wav->w64 = 1;
  344. return 0;
  345. }
  346. AVInputFormat w64_demuxer = {
  347. "w64",
  348. NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  349. sizeof(WAVContext),
  350. w64_probe,
  351. w64_read_header,
  352. wav_read_packet,
  353. NULL,
  354. wav_read_seek,
  355. .flags = AVFMT_GENERIC_INDEX,
  356. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  357. };
  358. #endif /* CONFIG_W64_DEMUXER */