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.

294 lines
7.5KB

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