wav.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * WAV encoder and decoder
  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 "allformats.h"
  23. #include "riff.h"
  24. typedef struct {
  25. offset_t data;
  26. offset_t data_end;
  27. } WAVContext;
  28. #ifdef CONFIG_MUXERS
  29. static int wav_write_header(AVFormatContext *s)
  30. {
  31. WAVContext *wav = s->priv_data;
  32. ByteIOContext *pb = &s->pb;
  33. offset_t fmt;
  34. put_tag(pb, "RIFF");
  35. put_le32(pb, 0); /* file length */
  36. put_tag(pb, "WAVE");
  37. /* format header */
  38. fmt = start_tag(pb, "fmt ");
  39. if (put_wav_header(pb, s->streams[0]->codec) < 0) {
  40. av_free(wav);
  41. return -1;
  42. }
  43. end_tag(pb, fmt);
  44. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  45. /* data header */
  46. wav->data = start_tag(pb, "data");
  47. put_flush_packet(pb);
  48. return 0;
  49. }
  50. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  51. {
  52. ByteIOContext *pb = &s->pb;
  53. put_buffer(pb, pkt->data, pkt->size);
  54. return 0;
  55. }
  56. static int wav_write_trailer(AVFormatContext *s)
  57. {
  58. ByteIOContext *pb = &s->pb;
  59. WAVContext *wav = s->priv_data;
  60. offset_t file_size;
  61. if (!url_is_streamed(&s->pb)) {
  62. end_tag(pb, wav->data);
  63. /* update file size */
  64. file_size = url_ftell(pb);
  65. url_fseek(pb, 4, SEEK_SET);
  66. put_le32(pb, (uint32_t)(file_size - 8));
  67. url_fseek(pb, file_size, SEEK_SET);
  68. put_flush_packet(pb);
  69. }
  70. return 0;
  71. }
  72. #endif //CONFIG_MUXERS
  73. /* return the size of the found tag */
  74. /* XXX: > 2GB ? */
  75. static int find_tag(ByteIOContext *pb, uint32_t tag1)
  76. {
  77. unsigned int tag;
  78. int size;
  79. for(;;) {
  80. if (url_feof(pb))
  81. return -1;
  82. tag = get_le32(pb);
  83. size = get_le32(pb);
  84. if (tag == tag1)
  85. break;
  86. url_fseek(pb, size, SEEK_CUR);
  87. }
  88. if (size < 0)
  89. size = 0x7fffffff;
  90. return size;
  91. }
  92. static int wav_probe(AVProbeData *p)
  93. {
  94. /* check file header */
  95. if (p->buf_size <= 32)
  96. return 0;
  97. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  98. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  99. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  100. p->buf[10] == 'V' && p->buf[11] == 'E')
  101. return AVPROBE_SCORE_MAX;
  102. else
  103. return 0;
  104. }
  105. /* wav input */
  106. static int wav_read_header(AVFormatContext *s,
  107. AVFormatParameters *ap)
  108. {
  109. int size;
  110. unsigned int tag;
  111. ByteIOContext *pb = &s->pb;
  112. AVStream *st;
  113. WAVContext *wav = s->priv_data;
  114. /* check RIFF header */
  115. tag = get_le32(pb);
  116. if (tag != MKTAG('R', 'I', 'F', 'F'))
  117. return -1;
  118. get_le32(pb); /* file size */
  119. tag = get_le32(pb);
  120. if (tag != MKTAG('W', 'A', 'V', 'E'))
  121. return -1;
  122. /* parse fmt header */
  123. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  124. if (size < 0)
  125. return -1;
  126. st = av_new_stream(s, 0);
  127. if (!st)
  128. return AVERROR_NOMEM;
  129. get_wav_header(pb, st->codec, size);
  130. st->need_parsing = 1;
  131. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  132. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  133. if (size < 0)
  134. return -1;
  135. wav->data_end= url_ftell(pb) + size;
  136. return 0;
  137. }
  138. #define MAX_SIZE 4096
  139. static int wav_read_packet(AVFormatContext *s,
  140. AVPacket *pkt)
  141. {
  142. int ret, size, left;
  143. AVStream *st;
  144. WAVContext *wav = s->priv_data;
  145. if (url_feof(&s->pb))
  146. return AVERROR_IO;
  147. st = s->streams[0];
  148. left= wav->data_end - url_ftell(&s->pb);
  149. if(left <= 0){
  150. left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
  151. if (left < 0) {
  152. return AVERROR_IO;
  153. }
  154. wav->data_end= url_ftell(&s->pb) + left;
  155. }
  156. size = MAX_SIZE;
  157. if (st->codec->block_align > 1) {
  158. if (size < st->codec->block_align)
  159. size = st->codec->block_align;
  160. size = (size / st->codec->block_align) * st->codec->block_align;
  161. }
  162. size= FFMIN(size, left);
  163. if (av_new_packet(pkt, size))
  164. return AVERROR_IO;
  165. pkt->stream_index = 0;
  166. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  167. if (ret < 0)
  168. av_free_packet(pkt);
  169. /* note: we need to modify the packet size here to handle the last
  170. packet */
  171. pkt->size = ret;
  172. return ret;
  173. }
  174. static int wav_read_close(AVFormatContext *s)
  175. {
  176. return 0;
  177. }
  178. static int wav_read_seek(AVFormatContext *s,
  179. int stream_index, int64_t timestamp, int flags)
  180. {
  181. AVStream *st;
  182. st = s->streams[0];
  183. switch(st->codec->codec_id) {
  184. case CODEC_ID_MP2:
  185. case CODEC_ID_MP3:
  186. case CODEC_ID_AC3:
  187. case CODEC_ID_DTS:
  188. /* use generic seeking with dynamically generated indexes */
  189. return -1;
  190. default:
  191. break;
  192. }
  193. return pcm_read_seek(s, stream_index, timestamp, flags);
  194. }
  195. #ifdef CONFIG_WAV_DEMUXER
  196. AVInputFormat wav_demuxer = {
  197. "wav",
  198. "wav format",
  199. sizeof(WAVContext),
  200. wav_probe,
  201. wav_read_header,
  202. wav_read_packet,
  203. wav_read_close,
  204. wav_read_seek,
  205. };
  206. #endif
  207. #ifdef CONFIG_WAV_MUXER
  208. AVOutputFormat wav_muxer = {
  209. "wav",
  210. "wav format",
  211. "audio/x-wav",
  212. "wav",
  213. sizeof(WAVContext),
  214. CODEC_ID_PCM_S16LE,
  215. CODEC_ID_NONE,
  216. wav_write_header,
  217. wav_write_packet,
  218. wav_write_trailer,
  219. };
  220. #endif