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.

262 lines
6.1KB

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