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.

320 lines
8.3KB

  1. /*
  2. * WAV encoder and decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. CodecTag codec_wav_tags[] = {
  22. { CODEC_ID_MP2, 0x50 },
  23. { CODEC_ID_MP3LAME, 0x55 },
  24. { CODEC_ID_AC3, 0x2000 },
  25. { CODEC_ID_PCM_S16LE, 0x01 },
  26. { CODEC_ID_PCM_U8, 0x01 }, /* must come after s16le in this list */
  27. { CODEC_ID_PCM_ALAW, 0x06 },
  28. { CODEC_ID_PCM_MULAW, 0x07 },
  29. { CODEC_ID_ADPCM_MS, 0x02 },
  30. { CODEC_ID_ADPCM_IMA_WAV, 0x11 },
  31. { 0, 0 },
  32. };
  33. /* WAVEFORMATEX header */
  34. /* returns the size or -1 on error */
  35. int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
  36. {
  37. int tag, bps, blkalign, bytespersec;
  38. int hdrsize = 18;
  39. tag = codec_get_tag(codec_wav_tags, enc->codec_id);
  40. if (tag == 0)
  41. return -1;
  42. put_le16(pb, tag);
  43. put_le16(pb, enc->channels);
  44. put_le32(pb, enc->sample_rate);
  45. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  46. enc->codec_id == CODEC_ID_PCM_ALAW ||
  47. enc->codec_id == CODEC_ID_PCM_MULAW) {
  48. bps = 8;
  49. } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
  50. bps = 0;
  51. } else {
  52. bps = 16;
  53. }
  54. if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
  55. blkalign = 1;
  56. //blkalign = 144 * enc->bit_rate/enc->sample_rate;
  57. } else if (enc->block_align != 0) { /* specified by the codec */
  58. blkalign = enc->block_align;
  59. } else
  60. blkalign = enc->channels*bps >> 3;
  61. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  62. enc->codec_id == CODEC_ID_PCM_S16LE) {
  63. bytespersec = enc->sample_rate * blkalign;
  64. } else {
  65. bytespersec = enc->bit_rate / 8;
  66. }
  67. put_le32(pb, bytespersec); /* bytes per second */
  68. put_le16(pb, blkalign); /* block align */
  69. put_le16(pb, bps); /* bits per sample */
  70. if (enc->codec_id == CODEC_ID_MP3LAME) {
  71. put_le16(pb, 12); /* wav_extra_size */
  72. hdrsize += 12;
  73. put_le16(pb, 1); /* wID */
  74. put_le32(pb, 2); /* fdwFlags */
  75. put_le16(pb, 1152); /* nBlockSize */
  76. put_le16(pb, 1); /* nFramesPerBlock */
  77. put_le16(pb, 1393); /* nCodecDelay */
  78. } else if (enc->codec_id == CODEC_ID_MP2) {
  79. put_le16(pb, 22); /* wav_extra_size */
  80. hdrsize += 22;
  81. put_le16(pb, 2); /* fwHeadLayer */
  82. put_le32(pb, enc->bit_rate); /* dwHeadBitrate */
  83. put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
  84. put_le16(pb, 0); /* fwHeadModeExt */
  85. put_le16(pb, 1); /* wHeadEmphasis */
  86. put_le16(pb, 16); /* fwHeadFlags */
  87. put_le32(pb, 0); /* dwPTSLow */
  88. put_le32(pb, 0); /* dwPTSHigh */
  89. } else
  90. put_le16(pb, 0); /* wav_extra_size */
  91. return hdrsize;
  92. }
  93. int wav_codec_get_id(unsigned int tag, int bps)
  94. {
  95. int id;
  96. id = codec_get_id(codec_wav_tags, tag);
  97. if (id <= 0)
  98. return id;
  99. /* handle specific u8 codec */
  100. if (id == CODEC_ID_PCM_S16LE && bps == 8)
  101. id = CODEC_ID_PCM_U8;
  102. return id;
  103. }
  104. typedef struct {
  105. offset_t data;
  106. } WAVContext;
  107. static int wav_write_header(AVFormatContext *s)
  108. {
  109. WAVContext *wav = s->priv_data;
  110. ByteIOContext *pb = &s->pb;
  111. offset_t fmt;
  112. put_tag(pb, "RIFF");
  113. put_le32(pb, 0); /* file length */
  114. put_tag(pb, "WAVE");
  115. /* format header */
  116. fmt = start_tag(pb, "fmt ");
  117. if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
  118. av_free(wav);
  119. return -1;
  120. }
  121. end_tag(pb, fmt);
  122. /* data header */
  123. wav->data = start_tag(pb, "data");
  124. put_flush_packet(pb);
  125. return 0;
  126. }
  127. static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
  128. UINT8 *buf, int size, int force_pts)
  129. {
  130. ByteIOContext *pb = &s->pb;
  131. put_buffer(pb, buf, size);
  132. return 0;
  133. }
  134. static int wav_write_trailer(AVFormatContext *s)
  135. {
  136. ByteIOContext *pb = &s->pb;
  137. WAVContext *wav = s->priv_data;
  138. offset_t file_size;
  139. if (!url_is_streamed(&s->pb)) {
  140. end_tag(pb, wav->data);
  141. /* update file size */
  142. file_size = url_ftell(pb);
  143. url_fseek(pb, 4, SEEK_SET);
  144. put_le32(pb, (UINT32)(file_size - 8));
  145. url_fseek(pb, file_size, SEEK_SET);
  146. put_flush_packet(pb);
  147. }
  148. return 0;
  149. }
  150. /* return the size of the found tag */
  151. /* XXX: > 2GB ? */
  152. static int find_tag(ByteIOContext *pb, UINT32 tag1)
  153. {
  154. unsigned int tag;
  155. int size;
  156. for(;;) {
  157. if (url_feof(pb))
  158. return -1;
  159. tag = get_le32(pb);
  160. size = get_le32(pb);
  161. if (tag == tag1)
  162. break;
  163. url_fseek(pb, size, SEEK_CUR);
  164. }
  165. if (size < 0)
  166. size = 0x7fffffff;
  167. return size;
  168. }
  169. static int wav_probe(AVProbeData *p)
  170. {
  171. /* check file header */
  172. if (p->buf_size <= 32)
  173. return 0;
  174. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  175. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  176. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  177. p->buf[10] == 'V' && p->buf[11] == 'E')
  178. return AVPROBE_SCORE_MAX;
  179. else
  180. return 0;
  181. }
  182. /* wav input */
  183. static int wav_read_header(AVFormatContext *s,
  184. AVFormatParameters *ap)
  185. {
  186. int size;
  187. unsigned int tag;
  188. ByteIOContext *pb = &s->pb;
  189. unsigned int id, channels, rate, bit_rate, extra_size, bps;
  190. unsigned int blkalign;
  191. AVStream *st;
  192. /* check RIFF header */
  193. tag = get_le32(pb);
  194. if (tag != MKTAG('R', 'I', 'F', 'F'))
  195. return -1;
  196. get_le32(pb); /* file size */
  197. tag = get_le32(pb);
  198. if (tag != MKTAG('W', 'A', 'V', 'E'))
  199. return -1;
  200. /* parse fmt header */
  201. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  202. if (size < 0)
  203. return -1;
  204. id = get_le16(pb);
  205. channels = get_le16(pb);
  206. rate = get_le32(pb);
  207. bit_rate = get_le32(pb) * 8;
  208. blkalign = get_le16(pb); /* block align */
  209. bps = get_le16(pb); /* bits per sample */
  210. if (size >= 18) {
  211. /* wav_extra_size */
  212. extra_size = get_le16(pb);
  213. /* skip unused data */
  214. url_fseek(pb, size - 18, SEEK_CUR);
  215. }
  216. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  217. if (size < 0)
  218. return -1;
  219. /* now we are ready: build format streams */
  220. st = av_new_stream(s, 0);
  221. if (!st)
  222. return AVERROR_NOMEM;
  223. st->codec.codec_type = CODEC_TYPE_AUDIO;
  224. st->codec.codec_tag = id;
  225. st->codec.codec_id = wav_codec_get_id(id, bps);
  226. st->codec.channels = channels;
  227. st->codec.sample_rate = rate;
  228. st->codec.block_align = blkalign;
  229. return 0;
  230. }
  231. #define MAX_SIZE 4096
  232. static int wav_read_packet(AVFormatContext *s,
  233. AVPacket *pkt)
  234. {
  235. int packet_size, n, ret;
  236. if (url_feof(&s->pb))
  237. return -EIO;
  238. packet_size = url_get_packet_size(&s->pb);
  239. n = MAX_SIZE / packet_size;
  240. if (n <= 0)
  241. return n = 1;
  242. if (av_new_packet(pkt, n * packet_size))
  243. return -EIO;
  244. pkt->stream_index = 0;
  245. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  246. if (ret < 0)
  247. av_free_packet(pkt);
  248. /* note: we need to modify the packet size here to handle the last
  249. packet */
  250. pkt->size = ret;
  251. return ret;
  252. }
  253. static int wav_read_close(AVFormatContext *s)
  254. {
  255. return 0;
  256. }
  257. static AVInputFormat wav_iformat = {
  258. "wav",
  259. "wav format",
  260. 0,
  261. wav_probe,
  262. wav_read_header,
  263. wav_read_packet,
  264. wav_read_close,
  265. };
  266. static AVOutputFormat wav_oformat = {
  267. "wav",
  268. "wav format",
  269. "audio/x-wav",
  270. "wav",
  271. sizeof(WAVContext),
  272. CODEC_ID_PCM_S16LE,
  273. CODEC_ID_NONE,
  274. wav_write_header,
  275. wav_write_packet,
  276. wav_write_trailer,
  277. };
  278. int wav_init(void)
  279. {
  280. av_register_input_format(&wav_iformat);
  281. av_register_output_format(&wav_oformat);
  282. return 0;
  283. }