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.

207 lines
4.7KB

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