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.

285 lines
7.5KB

  1. /*
  2. * WAV muxer and demuxer
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "raw.h"
  23. #include "riff.h"
  24. typedef struct {
  25. int64_t data;
  26. int64_t data_end;
  27. int64_t minpts;
  28. int64_t maxpts;
  29. int last_duration;
  30. } WAVContext;
  31. #if CONFIG_WAV_MUXER
  32. static int wav_write_header(AVFormatContext *s)
  33. {
  34. WAVContext *wav = s->priv_data;
  35. ByteIOContext *pb = s->pb;
  36. int64_t fmt, fact;
  37. put_tag(pb, "RIFF");
  38. put_le32(pb, 0); /* file length */
  39. put_tag(pb, "WAVE");
  40. /* format header */
  41. fmt = start_tag(pb, "fmt ");
  42. if (put_wav_header(pb, s->streams[0]->codec) < 0) {
  43. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  44. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  45. av_free(wav);
  46. return -1;
  47. }
  48. end_tag(pb, fmt);
  49. if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  50. && !url_is_streamed(s->pb)) {
  51. fact = start_tag(pb, "fact");
  52. put_le32(pb, 0);
  53. end_tag(pb, fact);
  54. }
  55. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  56. wav->maxpts = wav->last_duration = 0;
  57. wav->minpts = INT64_MAX;
  58. /* data header */
  59. wav->data = start_tag(pb, "data");
  60. put_flush_packet(pb);
  61. return 0;
  62. }
  63. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  64. {
  65. ByteIOContext *pb = s->pb;
  66. WAVContext *wav = s->priv_data;
  67. put_buffer(pb, pkt->data, pkt->size);
  68. if(pkt->pts != AV_NOPTS_VALUE) {
  69. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  70. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  71. wav->last_duration = pkt->duration;
  72. } else
  73. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  74. return 0;
  75. }
  76. static int wav_write_trailer(AVFormatContext *s)
  77. {
  78. ByteIOContext *pb = s->pb;
  79. WAVContext *wav = s->priv_data;
  80. int64_t file_size;
  81. if (!url_is_streamed(s->pb)) {
  82. end_tag(pb, wav->data);
  83. /* update file size */
  84. file_size = url_ftell(pb);
  85. url_fseek(pb, 4, SEEK_SET);
  86. put_le32(pb, (uint32_t)(file_size - 8));
  87. url_fseek(pb, file_size, SEEK_SET);
  88. put_flush_packet(pb);
  89. if(s->streams[0]->codec->codec_tag != 0x01) {
  90. /* Update num_samps in fact chunk */
  91. int number_of_samples;
  92. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  93. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  94. s->streams[0]->time_base.den);
  95. url_fseek(pb, wav->data-12, SEEK_SET);
  96. put_le32(pb, number_of_samples);
  97. url_fseek(pb, file_size, SEEK_SET);
  98. put_flush_packet(pb);
  99. }
  100. }
  101. return 0;
  102. }
  103. #endif /* CONFIG_WAV_MUXER */
  104. /* return the size of the found tag */
  105. static int64_t find_tag(ByteIOContext *pb, uint32_t tag1)
  106. {
  107. unsigned int tag;
  108. int64_t size;
  109. for(;;) {
  110. if (url_feof(pb))
  111. return -1;
  112. tag = get_le32(pb);
  113. size = get_le32(pb);
  114. if (tag == tag1)
  115. break;
  116. url_fseek(pb, size, SEEK_CUR);
  117. }
  118. return size;
  119. }
  120. static int wav_probe(AVProbeData *p)
  121. {
  122. /* check file header */
  123. if (p->buf_size <= 32)
  124. return 0;
  125. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  126. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  127. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  128. p->buf[10] == 'V' && p->buf[11] == 'E')
  129. /*
  130. Since ACT demuxer has standard WAV header at top of it's own,
  131. returning score is decreased to avoid probe conflict
  132. between ACT and WAV.
  133. */
  134. return AVPROBE_SCORE_MAX - 1;
  135. else
  136. return 0;
  137. }
  138. /* wav input */
  139. static int wav_read_header(AVFormatContext *s,
  140. AVFormatParameters *ap)
  141. {
  142. int64_t size;
  143. unsigned int tag;
  144. ByteIOContext *pb = s->pb;
  145. AVStream *st;
  146. WAVContext *wav = s->priv_data;
  147. /* check RIFF header */
  148. tag = get_le32(pb);
  149. if (tag != MKTAG('R', 'I', 'F', 'F'))
  150. return -1;
  151. get_le32(pb); /* file size */
  152. tag = get_le32(pb);
  153. if (tag != MKTAG('W', 'A', 'V', 'E'))
  154. return -1;
  155. /* parse fmt header */
  156. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  157. if (size < 0)
  158. return -1;
  159. st = av_new_stream(s, 0);
  160. if (!st)
  161. return AVERROR(ENOMEM);
  162. get_wav_header(pb, st->codec, size);
  163. st->need_parsing = AVSTREAM_PARSE_FULL;
  164. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  165. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  166. if (size < 0)
  167. return -1;
  168. wav->data_end= url_ftell(pb) + size;
  169. return 0;
  170. }
  171. #define MAX_SIZE 4096
  172. static int wav_read_packet(AVFormatContext *s,
  173. AVPacket *pkt)
  174. {
  175. int ret, size, left;
  176. AVStream *st;
  177. WAVContext *wav = s->priv_data;
  178. if (url_feof(s->pb))
  179. return AVERROR(EIO);
  180. st = s->streams[0];
  181. left= wav->data_end - url_ftell(s->pb);
  182. if(left <= 0){
  183. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  184. if (left < 0) {
  185. return AVERROR(EIO);
  186. }
  187. wav->data_end= url_ftell(s->pb) + left;
  188. }
  189. size = MAX_SIZE;
  190. if (st->codec->block_align > 1) {
  191. if (size < st->codec->block_align)
  192. size = st->codec->block_align;
  193. size = (size / st->codec->block_align) * st->codec->block_align;
  194. }
  195. size= FFMIN(size, left);
  196. ret= av_get_packet(s->pb, pkt, size);
  197. if (ret <= 0)
  198. return AVERROR(EIO);
  199. pkt->stream_index = 0;
  200. /* note: we need to modify the packet size here to handle the last
  201. packet */
  202. pkt->size = ret;
  203. return ret;
  204. }
  205. static int wav_read_seek(AVFormatContext *s,
  206. int stream_index, int64_t timestamp, int flags)
  207. {
  208. AVStream *st;
  209. st = s->streams[0];
  210. switch(st->codec->codec_id) {
  211. case CODEC_ID_MP2:
  212. case CODEC_ID_MP3:
  213. case CODEC_ID_AC3:
  214. case CODEC_ID_DTS:
  215. /* use generic seeking with dynamically generated indexes */
  216. return -1;
  217. default:
  218. break;
  219. }
  220. return pcm_read_seek(s, stream_index, timestamp, flags);
  221. }
  222. #if CONFIG_WAV_DEMUXER
  223. AVInputFormat wav_demuxer = {
  224. "wav",
  225. NULL_IF_CONFIG_SMALL("WAV format"),
  226. sizeof(WAVContext),
  227. wav_probe,
  228. wav_read_header,
  229. wav_read_packet,
  230. NULL,
  231. wav_read_seek,
  232. .flags= AVFMT_GENERIC_INDEX,
  233. .codec_tag= (const AVCodecTag* const []){codec_wav_tags, 0},
  234. };
  235. #endif
  236. #if CONFIG_WAV_MUXER
  237. AVOutputFormat wav_muxer = {
  238. "wav",
  239. NULL_IF_CONFIG_SMALL("WAV format"),
  240. "audio/x-wav",
  241. "wav",
  242. sizeof(WAVContext),
  243. CODEC_ID_PCM_S16LE,
  244. CODEC_ID_NONE,
  245. wav_write_header,
  246. wav_write_packet,
  247. wav_write_trailer,
  248. .codec_tag= (const AVCodecTag* const []){codec_wav_tags, 0},
  249. };
  250. #endif