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.

212 lines
4.8KB

  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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <netinet/in.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "avformat.h"
  25. #include "avi.h"
  26. typedef struct {
  27. offset_t data;
  28. } WAVContext;
  29. static int wav_write_header(AVFormatContext *s)
  30. {
  31. WAVContext *wav;
  32. ByteIOContext *pb = &s->pb;
  33. offset_t fmt;
  34. wav = malloc(sizeof(WAVContext));
  35. if (!wav)
  36. return -1;
  37. memset(wav, 0, sizeof(WAVContext));
  38. s->priv_data = wav;
  39. put_tag(pb, "RIFF");
  40. put_le32(pb, 0); /* file length */
  41. put_tag(pb, "WAVE");
  42. /* format header */
  43. fmt = start_tag(pb, "fmt ");
  44. put_wav_header(pb, &s->streams[0]->codec);
  45. end_tag(pb, fmt);
  46. /* data header */
  47. wav->data = start_tag(pb, "data");
  48. put_flush_packet(pb);
  49. return 0;
  50. }
  51. static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
  52. UINT8 *buf, int size)
  53. {
  54. ByteIOContext *pb = &s->pb;
  55. put_buffer(pb, buf, size);
  56. return 0;
  57. }
  58. static int wav_write_trailer(AVFormatContext *s)
  59. {
  60. ByteIOContext *pb = &s->pb;
  61. WAVContext *wav = s->priv_data;
  62. offset_t file_size;
  63. if (!url_is_streamed(&s->pb)) {
  64. end_tag(pb, wav->data);
  65. /* update file size */
  66. file_size = url_ftell(pb);
  67. url_fseek(pb, 4, SEEK_SET);
  68. put_le32(pb, file_size);
  69. url_fseek(pb, file_size, SEEK_SET);
  70. put_flush_packet(pb);
  71. }
  72. free(wav);
  73. return 0;
  74. }
  75. /* return the size of the found tag */
  76. /* XXX: > 2GB ? */
  77. static int find_tag(ByteIOContext *pb, int tag1)
  78. {
  79. unsigned int tag;
  80. int size;
  81. for(;;) {
  82. if (url_feof(pb))
  83. return -1;
  84. tag = get_le32(pb);
  85. size = get_le32(pb);
  86. if (tag == tag1)
  87. break;
  88. url_fseek(pb, size, SEEK_CUR);
  89. }
  90. if (size < 0)
  91. size = 0x7fffffff;
  92. return size;
  93. }
  94. /* wav input */
  95. static int wav_read_header(AVFormatContext *s,
  96. AVFormatParameters *ap)
  97. {
  98. int size;
  99. unsigned int tag;
  100. ByteIOContext *pb = &s->pb;
  101. unsigned int id, channels, rate, bit_rate, extra_size;
  102. AVStream *st;
  103. /* check RIFF header */
  104. tag = get_le32(pb);
  105. if (tag != MKTAG('R', 'I', 'F', 'F'))
  106. return -1;
  107. get_le32(pb); /* file size */
  108. tag = get_le32(pb);
  109. if (tag != MKTAG('W', 'A', 'V', 'E'))
  110. return -1;
  111. /* parse fmt header */
  112. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  113. if (size < 0)
  114. return -1;
  115. id = get_le16(pb);
  116. channels = get_le16(pb);
  117. rate = get_le32(pb);
  118. bit_rate = get_le32(pb) * 8;
  119. get_le16(pb); /* block align */
  120. get_le16(pb); /* bits per sample */
  121. if (size >= 18) {
  122. /* wav_extra_size */
  123. extra_size = get_le16(pb);
  124. /* skip unused data */
  125. url_fseek(pb, size - 18, SEEK_CUR);
  126. }
  127. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  128. if (size < 0)
  129. return -1;
  130. /* now we are ready: build format streams */
  131. st = malloc(sizeof(AVStream));
  132. if (!st)
  133. return -1;
  134. s->nb_streams = 1;
  135. s->streams[0] = st;
  136. st->id = 0;
  137. st->codec.codec_type = CODEC_TYPE_AUDIO;
  138. st->codec.codec_tag = id;
  139. st->codec.codec_id = codec_get_id(codec_wav_tags, id);
  140. st->codec.channels = channels;
  141. st->codec.sample_rate = rate;
  142. return 0;
  143. }
  144. #define MAX_SIZE 4096
  145. static int wav_read_packet(AVFormatContext *s,
  146. AVPacket *pkt)
  147. {
  148. int packet_size, n, ret;
  149. if (url_feof(&s->pb))
  150. return -EIO;
  151. packet_size = url_get_packet_size(&s->pb);
  152. n = MAX_SIZE / packet_size;
  153. if (n <= 0)
  154. return n = 1;
  155. if (av_new_packet(pkt, n * packet_size))
  156. return -EIO;
  157. pkt->stream_index = 0;
  158. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  159. if (ret < 0)
  160. av_free_packet(pkt);
  161. return ret;
  162. }
  163. static int wav_read_close(AVFormatContext *s)
  164. {
  165. return 0;
  166. }
  167. AVFormat wav_format = {
  168. "wav",
  169. "wav format",
  170. "audio/x-wav",
  171. "wav",
  172. CODEC_ID_PCM,
  173. CODEC_ID_NONE,
  174. wav_write_header,
  175. wav_write_packet,
  176. wav_write_trailer,
  177. wav_read_header,
  178. wav_read_packet,
  179. wav_read_close,
  180. };