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.

284 lines
7.0KB

  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, 0x55 },
  23. { CODEC_ID_MP3LAME, 0x55 },
  24. { CODEC_ID_MP2, 0x50 },
  25. { CODEC_ID_AC3, 0x2000 },
  26. { CODEC_ID_PCM_S16LE, 0x01 },
  27. { CODEC_ID_PCM_U8, 0x01 }, /* must come after s16le in this list */
  28. { CODEC_ID_PCM_ALAW, 0x06 },
  29. { CODEC_ID_PCM_MULAW, 0x07 },
  30. { 0, 0 },
  31. };
  32. /* WAVEFORMATEX header */
  33. int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
  34. {
  35. int tag, bps, blkalign, bytespersec;
  36. tag = codec_get_tag(codec_wav_tags, enc->codec_id);
  37. if (tag == 0)
  38. return -1;
  39. put_le16(pb, tag);
  40. put_le16(pb, enc->channels);
  41. put_le32(pb, enc->sample_rate);
  42. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  43. enc->codec_id == CODEC_ID_PCM_ALAW ||
  44. enc->codec_id == CODEC_ID_PCM_MULAW) {
  45. bps = 8;
  46. } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
  47. bps = 0;
  48. } else {
  49. bps = 16;
  50. }
  51. if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME)
  52. blkalign = 1;
  53. else
  54. blkalign = enc->channels*bps >> 3;
  55. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  56. enc->codec_id == CODEC_ID_PCM_S16LE) {
  57. bytespersec = enc->sample_rate * blkalign;
  58. } else {
  59. bytespersec = enc->bit_rate / 8;
  60. }
  61. put_le32(pb, bytespersec); /* bytes per second */
  62. put_le16(pb, blkalign); /* block align */
  63. put_le16(pb, bps); /* bits per sample */
  64. if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
  65. put_le16(pb, 12); /* wav_extra_size */
  66. put_le16(pb, 1); /* wID */
  67. put_le32(pb, 2); /* fdwFlags */
  68. put_le16(pb, 1152); /* nBlockSize */
  69. put_le16(pb, 1); /* nFramesPerBlock */
  70. put_le16(pb, 1393); /* nCodecDelay */
  71. } else
  72. put_le16(pb, 0); /* wav_extra_size */
  73. return 0;
  74. }
  75. int wav_codec_get_id(unsigned int tag, int bps)
  76. {
  77. int id;
  78. id = codec_get_id(codec_wav_tags, tag);
  79. if (id <= 0)
  80. return id;
  81. /* handle specific u8 codec */
  82. if (id == CODEC_ID_PCM_S16LE && bps == 8)
  83. id = CODEC_ID_PCM_U8;
  84. return id;
  85. }
  86. typedef struct {
  87. offset_t data;
  88. } WAVContext;
  89. static int wav_write_header(AVFormatContext *s)
  90. {
  91. WAVContext *wav;
  92. ByteIOContext *pb = &s->pb;
  93. offset_t fmt;
  94. wav = malloc(sizeof(WAVContext));
  95. if (!wav)
  96. return -1;
  97. memset(wav, 0, sizeof(WAVContext));
  98. s->priv_data = wav;
  99. put_tag(pb, "RIFF");
  100. put_le32(pb, 0); /* file length */
  101. put_tag(pb, "WAVE");
  102. /* format header */
  103. fmt = start_tag(pb, "fmt ");
  104. if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
  105. free(wav);
  106. return -1;
  107. }
  108. end_tag(pb, fmt);
  109. /* data header */
  110. wav->data = start_tag(pb, "data");
  111. put_flush_packet(pb);
  112. return 0;
  113. }
  114. static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
  115. UINT8 *buf, int size)
  116. {
  117. ByteIOContext *pb = &s->pb;
  118. put_buffer(pb, buf, size);
  119. return 0;
  120. }
  121. static int wav_write_trailer(AVFormatContext *s)
  122. {
  123. ByteIOContext *pb = &s->pb;
  124. WAVContext *wav = s->priv_data;
  125. offset_t file_size;
  126. if (!url_is_streamed(&s->pb)) {
  127. end_tag(pb, wav->data);
  128. /* update file size */
  129. file_size = url_ftell(pb);
  130. url_fseek(pb, 4, SEEK_SET);
  131. put_le32(pb, (UINT32)(file_size - 8));
  132. url_fseek(pb, file_size, SEEK_SET);
  133. put_flush_packet(pb);
  134. }
  135. free(wav);
  136. return 0;
  137. }
  138. /* return the size of the found tag */
  139. /* XXX: > 2GB ? */
  140. static int find_tag(ByteIOContext *pb, UINT32 tag1)
  141. {
  142. unsigned int tag;
  143. int size;
  144. for(;;) {
  145. if (url_feof(pb))
  146. return -1;
  147. tag = get_le32(pb);
  148. size = get_le32(pb);
  149. if (tag == tag1)
  150. break;
  151. url_fseek(pb, size, SEEK_CUR);
  152. }
  153. if (size < 0)
  154. size = 0x7fffffff;
  155. return size;
  156. }
  157. /* wav input */
  158. static int wav_read_header(AVFormatContext *s,
  159. AVFormatParameters *ap)
  160. {
  161. int size;
  162. unsigned int tag;
  163. ByteIOContext *pb = &s->pb;
  164. unsigned int id, channels, rate, bit_rate, extra_size, bps;
  165. AVStream *st;
  166. /* check RIFF header */
  167. tag = get_le32(pb);
  168. if (tag != MKTAG('R', 'I', 'F', 'F'))
  169. return -1;
  170. get_le32(pb); /* file size */
  171. tag = get_le32(pb);
  172. if (tag != MKTAG('W', 'A', 'V', 'E'))
  173. return -1;
  174. /* parse fmt header */
  175. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  176. if (size < 0)
  177. return -1;
  178. id = get_le16(pb);
  179. channels = get_le16(pb);
  180. rate = get_le32(pb);
  181. bit_rate = get_le32(pb) * 8;
  182. get_le16(pb); /* block align */
  183. bps = get_le16(pb); /* bits per sample */
  184. if (size >= 18) {
  185. /* wav_extra_size */
  186. extra_size = get_le16(pb);
  187. /* skip unused data */
  188. url_fseek(pb, size - 18, SEEK_CUR);
  189. }
  190. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  191. if (size < 0)
  192. return -1;
  193. /* now we are ready: build format streams */
  194. st = malloc(sizeof(AVStream));
  195. if (!st)
  196. return -1;
  197. s->nb_streams = 1;
  198. s->streams[0] = st;
  199. st->id = 0;
  200. st->codec.codec_type = CODEC_TYPE_AUDIO;
  201. st->codec.codec_tag = id;
  202. st->codec.codec_id = wav_codec_get_id(id, bps);
  203. st->codec.channels = channels;
  204. st->codec.sample_rate = rate;
  205. return 0;
  206. }
  207. #define MAX_SIZE 4096
  208. static int wav_read_packet(AVFormatContext *s,
  209. AVPacket *pkt)
  210. {
  211. int packet_size, n, ret;
  212. if (url_feof(&s->pb))
  213. return -EIO;
  214. packet_size = url_get_packet_size(&s->pb);
  215. n = MAX_SIZE / packet_size;
  216. if (n <= 0)
  217. return n = 1;
  218. if (av_new_packet(pkt, n * packet_size))
  219. return -EIO;
  220. pkt->stream_index = 0;
  221. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  222. if (ret < 0)
  223. av_free_packet(pkt);
  224. /* note: we need to modify the packet size here to handle the last
  225. packet */
  226. pkt->size = ret;
  227. return ret;
  228. }
  229. static int wav_read_close(AVFormatContext *s)
  230. {
  231. return 0;
  232. }
  233. AVFormat wav_format = {
  234. "wav",
  235. "wav format",
  236. "audio/x-wav",
  237. "wav",
  238. CODEC_ID_PCM_S16LE,
  239. CODEC_ID_NONE,
  240. wav_write_header,
  241. wav_write_packet,
  242. wav_write_trailer,
  243. wav_read_header,
  244. wav_read_packet,
  245. wav_read_close,
  246. };