wav.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /* XXX: > 2GB ? */
  106. static int find_tag(ByteIOContext *pb, uint32_t tag1)
  107. {
  108. unsigned int tag;
  109. int size;
  110. for(;;) {
  111. if (url_feof(pb))
  112. return -1;
  113. tag = get_le32(pb);
  114. size = get_le32(pb);
  115. if (tag == tag1)
  116. break;
  117. url_fseek(pb, size, SEEK_CUR);
  118. }
  119. if (size < 0)
  120. size = 0x7fffffff;
  121. return size;
  122. }
  123. static int wav_probe(AVProbeData *p)
  124. {
  125. /* check file header */
  126. if (p->buf_size <= 32)
  127. return 0;
  128. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  129. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  130. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  131. p->buf[10] == 'V' && p->buf[11] == 'E')
  132. /*
  133. Since ACT demuxer has standard WAV header at top of it's own,
  134. returning score is decreased to avoid probe conflict
  135. between ACT and WAV.
  136. */
  137. return AVPROBE_SCORE_MAX - 1;
  138. else
  139. return 0;
  140. }
  141. /* wav input */
  142. static int wav_read_header(AVFormatContext *s,
  143. AVFormatParameters *ap)
  144. {
  145. int size;
  146. unsigned int tag;
  147. ByteIOContext *pb = s->pb;
  148. AVStream *st;
  149. WAVContext *wav = s->priv_data;
  150. /* check RIFF header */
  151. tag = get_le32(pb);
  152. if (tag != MKTAG('R', 'I', 'F', 'F'))
  153. return -1;
  154. get_le32(pb); /* file size */
  155. tag = get_le32(pb);
  156. if (tag != MKTAG('W', 'A', 'V', 'E'))
  157. return -1;
  158. /* parse fmt header */
  159. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  160. if (size < 0)
  161. return -1;
  162. st = av_new_stream(s, 0);
  163. if (!st)
  164. return AVERROR(ENOMEM);
  165. get_wav_header(pb, st->codec, size);
  166. st->need_parsing = AVSTREAM_PARSE_FULL;
  167. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  168. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  169. if (size < 0)
  170. return -1;
  171. wav->data_end= url_ftell(pb) + size;
  172. return 0;
  173. }
  174. #define MAX_SIZE 4096
  175. static int wav_read_packet(AVFormatContext *s,
  176. AVPacket *pkt)
  177. {
  178. int ret, size, left;
  179. AVStream *st;
  180. WAVContext *wav = s->priv_data;
  181. if (url_feof(s->pb))
  182. return AVERROR(EIO);
  183. st = s->streams[0];
  184. left= wav->data_end - url_ftell(s->pb);
  185. if(left <= 0){
  186. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  187. if (left < 0) {
  188. return AVERROR(EIO);
  189. }
  190. wav->data_end= url_ftell(s->pb) + left;
  191. }
  192. size = MAX_SIZE;
  193. if (st->codec->block_align > 1) {
  194. if (size < st->codec->block_align)
  195. size = st->codec->block_align;
  196. size = (size / st->codec->block_align) * st->codec->block_align;
  197. }
  198. size= FFMIN(size, left);
  199. ret= av_get_packet(s->pb, pkt, size);
  200. if (ret <= 0)
  201. return AVERROR(EIO);
  202. pkt->stream_index = 0;
  203. /* note: we need to modify the packet size here to handle the last
  204. packet */
  205. pkt->size = ret;
  206. return ret;
  207. }
  208. static int wav_read_seek(AVFormatContext *s,
  209. int stream_index, int64_t timestamp, int flags)
  210. {
  211. AVStream *st;
  212. st = s->streams[0];
  213. switch(st->codec->codec_id) {
  214. case CODEC_ID_MP2:
  215. case CODEC_ID_MP3:
  216. case CODEC_ID_AC3:
  217. case CODEC_ID_DTS:
  218. /* use generic seeking with dynamically generated indexes */
  219. return -1;
  220. default:
  221. break;
  222. }
  223. return pcm_read_seek(s, stream_index, timestamp, flags);
  224. }
  225. #if CONFIG_WAV_DEMUXER
  226. AVInputFormat wav_demuxer = {
  227. "wav",
  228. NULL_IF_CONFIG_SMALL("WAV format"),
  229. sizeof(WAVContext),
  230. wav_probe,
  231. wav_read_header,
  232. wav_read_packet,
  233. NULL,
  234. wav_read_seek,
  235. .flags= AVFMT_GENERIC_INDEX,
  236. .codec_tag= (const AVCodecTag* const []){codec_wav_tags, 0},
  237. };
  238. #endif
  239. #if CONFIG_WAV_MUXER
  240. AVOutputFormat wav_muxer = {
  241. "wav",
  242. NULL_IF_CONFIG_SMALL("WAV format"),
  243. "audio/x-wav",
  244. "wav",
  245. sizeof(WAVContext),
  246. CODEC_ID_PCM_S16LE,
  247. CODEC_ID_NONE,
  248. wav_write_header,
  249. wav_write_packet,
  250. wav_write_trailer,
  251. .codec_tag= (const AVCodecTag* const []){codec_wav_tags, 0},
  252. };
  253. #endif