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.

404 lines
11KB

  1. /*
  2. * WAV muxer and demuxer
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * Sony Wave64 demuxer
  6. * Copyright (c) 2009 Daniel Verkamp
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "avformat.h"
  25. #include "raw.h"
  26. #include "riff.h"
  27. typedef struct {
  28. int64_t data;
  29. int64_t data_end;
  30. int64_t minpts;
  31. int64_t maxpts;
  32. int last_duration;
  33. int w64;
  34. } WAVContext;
  35. #if CONFIG_WAV_MUXER
  36. static int wav_write_header(AVFormatContext *s)
  37. {
  38. WAVContext *wav = s->priv_data;
  39. ByteIOContext *pb = s->pb;
  40. int64_t fmt, fact;
  41. put_tag(pb, "RIFF");
  42. put_le32(pb, 0); /* file length */
  43. put_tag(pb, "WAVE");
  44. /* format header */
  45. fmt = ff_start_tag(pb, "fmt ");
  46. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  47. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  48. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  49. av_free(wav);
  50. return -1;
  51. }
  52. ff_end_tag(pb, fmt);
  53. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  54. && !url_is_streamed(s->pb)) {
  55. fact = ff_start_tag(pb, "fact");
  56. put_le32(pb, 0);
  57. ff_end_tag(pb, fact);
  58. }
  59. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  60. wav->maxpts = wav->last_duration = 0;
  61. wav->minpts = INT64_MAX;
  62. /* data header */
  63. wav->data = ff_start_tag(pb, "data");
  64. put_flush_packet(pb);
  65. return 0;
  66. }
  67. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  68. {
  69. ByteIOContext *pb = s->pb;
  70. WAVContext *wav = s->priv_data;
  71. put_buffer(pb, pkt->data, pkt->size);
  72. if(pkt->pts != AV_NOPTS_VALUE) {
  73. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  74. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  75. wav->last_duration = pkt->duration;
  76. } else
  77. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  78. return 0;
  79. }
  80. static int wav_write_trailer(AVFormatContext *s)
  81. {
  82. ByteIOContext *pb = s->pb;
  83. WAVContext *wav = s->priv_data;
  84. int64_t file_size;
  85. if (!url_is_streamed(s->pb)) {
  86. ff_end_tag(pb, wav->data);
  87. /* update file size */
  88. file_size = url_ftell(pb);
  89. url_fseek(pb, 4, SEEK_SET);
  90. put_le32(pb, (uint32_t)(file_size - 8));
  91. url_fseek(pb, file_size, SEEK_SET);
  92. put_flush_packet(pb);
  93. if(s->streams[0]->codec->codec_tag != 0x01) {
  94. /* Update num_samps in fact chunk */
  95. int number_of_samples;
  96. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  97. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  98. s->streams[0]->time_base.den);
  99. url_fseek(pb, wav->data-12, SEEK_SET);
  100. put_le32(pb, number_of_samples);
  101. url_fseek(pb, file_size, SEEK_SET);
  102. put_flush_packet(pb);
  103. }
  104. }
  105. return 0;
  106. }
  107. #endif /* CONFIG_WAV_MUXER */
  108. /* return the size of the found tag */
  109. static int64_t find_tag(ByteIOContext *pb, uint32_t tag1)
  110. {
  111. unsigned int tag;
  112. int64_t size;
  113. for (;;) {
  114. if (url_feof(pb))
  115. return -1;
  116. tag = get_le32(pb);
  117. size = get_le32(pb);
  118. if (tag == tag1)
  119. break;
  120. url_fseek(pb, size, SEEK_CUR);
  121. }
  122. return size;
  123. }
  124. static int wav_probe(AVProbeData *p)
  125. {
  126. /* check file header */
  127. if (p->buf_size <= 32)
  128. return 0;
  129. if (p->buf[ 0] == 'R' && p->buf[ 1] == 'I' &&
  130. p->buf[ 2] == 'F' && p->buf[ 3] == 'F' &&
  131. p->buf[ 8] == 'W' && p->buf[ 9] == 'A' &&
  132. p->buf[10] == 'V' && p->buf[11] == 'E')
  133. /*
  134. Since ACT demuxer has standard WAV header at top of it's own,
  135. returning score is decreased to avoid probe conflict
  136. between ACT and WAV.
  137. */
  138. return AVPROBE_SCORE_MAX - 1;
  139. else
  140. return 0;
  141. }
  142. /* wav input */
  143. static int wav_read_header(AVFormatContext *s,
  144. AVFormatParameters *ap)
  145. {
  146. int64_t size;
  147. unsigned int tag;
  148. ByteIOContext *pb = s->pb;
  149. AVStream *st;
  150. WAVContext *wav = s->priv_data;
  151. /* check RIFF header */
  152. tag = get_le32(pb);
  153. if (tag != MKTAG('R', 'I', 'F', 'F'))
  154. return -1;
  155. get_le32(pb); /* file size */
  156. tag = get_le32(pb);
  157. if (tag != MKTAG('W', 'A', 'V', 'E'))
  158. return -1;
  159. /* parse fmt header */
  160. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  161. if (size < 0)
  162. return -1;
  163. st = av_new_stream(s, 0);
  164. if (!st)
  165. return AVERROR(ENOMEM);
  166. ff_get_wav_header(pb, st->codec, size);
  167. st->need_parsing = AVSTREAM_PARSE_FULL;
  168. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  169. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  170. if (size < 0)
  171. return -1;
  172. wav->data_end= url_ftell(pb) + size;
  173. return 0;
  174. }
  175. #if CONFIG_W64_DEMUXER
  176. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  177. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  178. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  179. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  180. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  181. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  182. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  183. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  184. static int w64_probe(AVProbeData *p)
  185. {
  186. if (p->buf_size <= 40)
  187. return 0;
  188. if (!memcmp(p->buf, guid_riff, 16) &&
  189. !memcmp(p->buf + 24, guid_wave, 16))
  190. return AVPROBE_SCORE_MAX;
  191. else
  192. return 0;
  193. }
  194. /** Find chunk with w64 GUID by skipping over other chunks
  195. * @return the size of the found chunk
  196. */
  197. static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
  198. {
  199. uint8_t guid[16];
  200. int64_t size;
  201. while (!url_feof(pb)) {
  202. get_buffer(pb, guid, 16);
  203. size = get_le64(pb);
  204. if (size <= 24)
  205. return -1;
  206. if (!memcmp(guid, guid1, 16))
  207. return size;
  208. url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
  209. }
  210. return -1;
  211. }
  212. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  213. {
  214. int64_t size;
  215. ByteIOContext *pb = s->pb;
  216. WAVContext *wav = s->priv_data;
  217. AVStream *st;
  218. uint8_t guid[16];
  219. get_buffer(pb, guid, 16);
  220. if (memcmp(guid, guid_riff, 16))
  221. return -1;
  222. if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  223. return -1;
  224. get_buffer(pb, guid, 16);
  225. if (memcmp(guid, guid_wave, 16)) {
  226. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  227. return -1;
  228. }
  229. size = find_guid(pb, guid_fmt);
  230. if (size < 0) {
  231. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  232. return -1;
  233. }
  234. st = av_new_stream(s, 0);
  235. if (!st)
  236. return AVERROR(ENOMEM);
  237. /* subtract chunk header size - normal wav file doesn't count it */
  238. ff_get_wav_header(pb, st->codec, size - 24);
  239. url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
  240. st->need_parsing = AVSTREAM_PARSE_FULL;
  241. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  242. size = find_guid(pb, guid_data);
  243. if (size < 0) {
  244. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  245. return -1;
  246. }
  247. wav->data_end = url_ftell(pb) + size - 24;
  248. wav->w64 = 1;
  249. return 0;
  250. }
  251. #endif /* CONFIG_W64_DEMUXER */
  252. #define MAX_SIZE 4096
  253. static int wav_read_packet(AVFormatContext *s,
  254. AVPacket *pkt)
  255. {
  256. int ret, size;
  257. int64_t left;
  258. AVStream *st;
  259. WAVContext *wav = s->priv_data;
  260. if (url_feof(s->pb))
  261. return AVERROR(EIO);
  262. st = s->streams[0];
  263. left = wav->data_end - url_ftell(s->pb);
  264. if (left <= 0){
  265. if (CONFIG_W64_DEMUXER && wav->w64)
  266. left = find_guid(s->pb, guid_data) - 24;
  267. else
  268. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  269. if (left < 0)
  270. return AVERROR(EIO);
  271. wav->data_end= url_ftell(s->pb) + left;
  272. }
  273. size = MAX_SIZE;
  274. if (st->codec->block_align > 1) {
  275. if (size < st->codec->block_align)
  276. size = st->codec->block_align;
  277. size = (size / st->codec->block_align) * st->codec->block_align;
  278. }
  279. size = FFMIN(size, left);
  280. ret = av_get_packet(s->pb, pkt, size);
  281. if (ret <= 0)
  282. return AVERROR(EIO);
  283. pkt->stream_index = 0;
  284. /* note: we need to modify the packet size here to handle the last
  285. packet */
  286. pkt->size = ret;
  287. return ret;
  288. }
  289. static int wav_read_seek(AVFormatContext *s,
  290. int stream_index, int64_t timestamp, int flags)
  291. {
  292. AVStream *st;
  293. st = s->streams[0];
  294. switch (st->codec->codec_id) {
  295. case CODEC_ID_MP2:
  296. case CODEC_ID_MP3:
  297. case CODEC_ID_AC3:
  298. case CODEC_ID_DTS:
  299. /* use generic seeking with dynamically generated indexes */
  300. return -1;
  301. default:
  302. break;
  303. }
  304. return pcm_read_seek(s, stream_index, timestamp, flags);
  305. }
  306. #if CONFIG_WAV_DEMUXER
  307. AVInputFormat wav_demuxer = {
  308. "wav",
  309. NULL_IF_CONFIG_SMALL("WAV format"),
  310. sizeof(WAVContext),
  311. wav_probe,
  312. wav_read_header,
  313. wav_read_packet,
  314. NULL,
  315. wav_read_seek,
  316. .flags= AVFMT_GENERIC_INDEX,
  317. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  318. };
  319. #endif
  320. #if CONFIG_WAV_MUXER
  321. AVOutputFormat wav_muxer = {
  322. "wav",
  323. NULL_IF_CONFIG_SMALL("WAV format"),
  324. "audio/x-wav",
  325. "wav",
  326. sizeof(WAVContext),
  327. CODEC_ID_PCM_S16LE,
  328. CODEC_ID_NONE,
  329. wav_write_header,
  330. wav_write_packet,
  331. wav_write_trailer,
  332. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  333. };
  334. #endif
  335. #if CONFIG_W64_DEMUXER
  336. AVInputFormat w64_demuxer = {
  337. "w64",
  338. NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  339. sizeof(WAVContext),
  340. w64_probe,
  341. w64_read_header,
  342. wav_read_packet,
  343. NULL,
  344. wav_read_seek,
  345. .flags = AVFMT_GENERIC_INDEX,
  346. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  347. };
  348. #endif