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.

413 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. wav->data_end= url_ftell(pb) + size;
  203. return 0;
  204. }
  205. /** Find chunk with w64 GUID by skipping over other chunks
  206. * @return the size of the found chunk
  207. */
  208. static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
  209. {
  210. uint8_t guid[16];
  211. int64_t size;
  212. while (!url_feof(pb)) {
  213. get_buffer(pb, guid, 16);
  214. size = get_le64(pb);
  215. if (size <= 24)
  216. return -1;
  217. if (!memcmp(guid, guid1, 16))
  218. return size;
  219. url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
  220. }
  221. return -1;
  222. }
  223. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  224. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  225. #define MAX_SIZE 4096
  226. static int wav_read_packet(AVFormatContext *s,
  227. AVPacket *pkt)
  228. {
  229. int ret, size;
  230. int64_t left;
  231. AVStream *st;
  232. WAVContext *wav = s->priv_data;
  233. st = s->streams[0];
  234. left = wav->data_end - url_ftell(s->pb);
  235. if (left <= 0){
  236. if (CONFIG_W64_DEMUXER && wav->w64)
  237. left = find_guid(s->pb, guid_data) - 24;
  238. else
  239. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  240. if (left < 0)
  241. return AVERROR_EOF;
  242. wav->data_end= url_ftell(s->pb) + left;
  243. }
  244. size = MAX_SIZE;
  245. if (st->codec->block_align > 1) {
  246. if (size < st->codec->block_align)
  247. size = st->codec->block_align;
  248. size = (size / st->codec->block_align) * st->codec->block_align;
  249. }
  250. size = FFMIN(size, left);
  251. ret = av_get_packet(s->pb, pkt, size);
  252. if (ret < 0)
  253. return ret;
  254. pkt->stream_index = 0;
  255. return ret;
  256. }
  257. static int wav_read_seek(AVFormatContext *s,
  258. int stream_index, int64_t timestamp, int flags)
  259. {
  260. AVStream *st;
  261. st = s->streams[0];
  262. switch (st->codec->codec_id) {
  263. case CODEC_ID_MP2:
  264. case CODEC_ID_MP3:
  265. case CODEC_ID_AC3:
  266. case CODEC_ID_DTS:
  267. /* use generic seeking with dynamically generated indexes */
  268. return -1;
  269. default:
  270. break;
  271. }
  272. return pcm_read_seek(s, stream_index, timestamp, flags);
  273. }
  274. AVInputFormat wav_demuxer = {
  275. "wav",
  276. NULL_IF_CONFIG_SMALL("WAV format"),
  277. sizeof(WAVContext),
  278. wav_probe,
  279. wav_read_header,
  280. wav_read_packet,
  281. NULL,
  282. wav_read_seek,
  283. .flags= AVFMT_GENERIC_INDEX,
  284. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  285. };
  286. #endif /* CONFIG_WAV_DEMUXER */
  287. #if CONFIG_W64_DEMUXER
  288. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  289. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  290. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  291. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  292. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  293. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  294. static int w64_probe(AVProbeData *p)
  295. {
  296. if (p->buf_size <= 40)
  297. return 0;
  298. if (!memcmp(p->buf, guid_riff, 16) &&
  299. !memcmp(p->buf + 24, guid_wave, 16))
  300. return AVPROBE_SCORE_MAX;
  301. else
  302. return 0;
  303. }
  304. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  305. {
  306. int64_t size;
  307. ByteIOContext *pb = s->pb;
  308. WAVContext *wav = s->priv_data;
  309. AVStream *st;
  310. uint8_t guid[16];
  311. get_buffer(pb, guid, 16);
  312. if (memcmp(guid, guid_riff, 16))
  313. return -1;
  314. if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  315. return -1;
  316. get_buffer(pb, guid, 16);
  317. if (memcmp(guid, guid_wave, 16)) {
  318. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  319. return -1;
  320. }
  321. size = find_guid(pb, guid_fmt);
  322. if (size < 0) {
  323. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  324. return -1;
  325. }
  326. st = av_new_stream(s, 0);
  327. if (!st)
  328. return AVERROR(ENOMEM);
  329. /* subtract chunk header size - normal wav file doesn't count it */
  330. ff_get_wav_header(pb, st->codec, size - 24);
  331. url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
  332. st->need_parsing = AVSTREAM_PARSE_FULL;
  333. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  334. size = find_guid(pb, guid_data);
  335. if (size < 0) {
  336. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  337. return -1;
  338. }
  339. wav->data_end = url_ftell(pb) + size - 24;
  340. wav->w64 = 1;
  341. return 0;
  342. }
  343. AVInputFormat w64_demuxer = {
  344. "w64",
  345. NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  346. sizeof(WAVContext),
  347. w64_probe,
  348. w64_read_header,
  349. wav_read_packet,
  350. NULL,
  351. wav_read_seek,
  352. .flags = AVFMT_GENERIC_INDEX,
  353. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  354. };
  355. #endif /* CONFIG_W64_DEMUXER */