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.

298 lines
7.6KB

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