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.

386 lines
10KB

  1. /*
  2. * WAV encoder and decoder
  3. * Copyright (c) 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. const CodecTag codec_wav_tags[] = {
  22. { CODEC_ID_MP2, 0x50 },
  23. { CODEC_ID_MP3, 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. { CODEC_ID_ADPCM_MS, 0x02 },
  30. { CODEC_ID_ADPCM_IMA_WAV, 0x11 },
  31. { CODEC_ID_ADPCM_G726, 0x45 },
  32. { CODEC_ID_ADPCM_IMA_DK4, 0x61 }, /* rogue format number */
  33. { CODEC_ID_ADPCM_IMA_DK3, 0x62 }, /* rogue format number */
  34. { CODEC_ID_WMAV1, 0x160 },
  35. { CODEC_ID_WMAV2, 0x161 },
  36. { 0, 0 },
  37. };
  38. #ifdef CONFIG_ENCODERS
  39. /* WAVEFORMATEX header */
  40. /* returns the size or -1 on error */
  41. int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
  42. {
  43. int bps, blkalign, bytespersec;
  44. int hdrsize = 18;
  45. if(!enc->codec_tag)
  46. enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
  47. if(!enc->codec_tag)
  48. return -1;
  49. put_le16(pb, enc->codec_tag);
  50. put_le16(pb, enc->channels);
  51. put_le32(pb, enc->sample_rate);
  52. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  53. enc->codec_id == CODEC_ID_PCM_ALAW ||
  54. enc->codec_id == CODEC_ID_PCM_MULAW) {
  55. bps = 8;
  56. } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
  57. bps = 0;
  58. } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) {
  59. bps = 4;
  60. } else {
  61. bps = 16;
  62. }
  63. if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
  64. blkalign = 1;
  65. //blkalign = 144 * enc->bit_rate/enc->sample_rate;
  66. } else if (enc->block_align != 0) { /* specified by the codec */
  67. blkalign = enc->block_align;
  68. } else
  69. blkalign = enc->channels*bps >> 3;
  70. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  71. enc->codec_id == CODEC_ID_PCM_S16LE) {
  72. bytespersec = enc->sample_rate * blkalign;
  73. } else {
  74. bytespersec = enc->bit_rate / 8;
  75. }
  76. put_le32(pb, bytespersec); /* bytes per second */
  77. put_le16(pb, blkalign); /* block align */
  78. put_le16(pb, bps); /* bits per sample */
  79. if (enc->codec_id == CODEC_ID_MP3) {
  80. put_le16(pb, 12); /* wav_extra_size */
  81. hdrsize += 12;
  82. put_le16(pb, 1); /* wID */
  83. put_le32(pb, 2); /* fdwFlags */
  84. put_le16(pb, 1152); /* nBlockSize */
  85. put_le16(pb, 1); /* nFramesPerBlock */
  86. put_le16(pb, 1393); /* nCodecDelay */
  87. } else if (enc->codec_id == CODEC_ID_MP2) {
  88. put_le16(pb, 22); /* wav_extra_size */
  89. hdrsize += 22;
  90. put_le16(pb, 2); /* fwHeadLayer */
  91. put_le32(pb, enc->bit_rate); /* dwHeadBitrate */
  92. put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
  93. put_le16(pb, 0); /* fwHeadModeExt */
  94. put_le16(pb, 1); /* wHeadEmphasis */
  95. put_le16(pb, 16); /* fwHeadFlags */
  96. put_le32(pb, 0); /* dwPTSLow */
  97. put_le32(pb, 0); /* dwPTSHigh */
  98. } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
  99. put_le16(pb, 2); /* wav_extra_size */
  100. put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
  101. } else
  102. put_le16(pb, 0); /* wav_extra_size */
  103. return hdrsize;
  104. }
  105. #endif //CONFIG_ENCODERS
  106. /* We could be given one of the three possible structures here:
  107. * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
  108. * is an expansion of the previous one with the fields added
  109. * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
  110. * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
  111. * an openended structure.
  112. */
  113. void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size)
  114. {
  115. int id;
  116. id = get_le16(pb);
  117. codec->codec_type = CODEC_TYPE_AUDIO;
  118. codec->codec_tag = id;
  119. codec->channels = get_le16(pb);
  120. codec->sample_rate = get_le32(pb);
  121. codec->bit_rate = get_le32(pb) * 8;
  122. codec->block_align = get_le16(pb);
  123. if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
  124. codec->bits_per_sample = 8;
  125. }else
  126. codec->bits_per_sample = get_le16(pb);
  127. codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
  128. if (size > 16) { /* We're obviously dealing with WAVEFORMATEX */
  129. codec->extradata_size = get_le16(pb);
  130. if (codec->extradata_size > 0) {
  131. if (codec->extradata_size > size - 18)
  132. codec->extradata_size = size - 18;
  133. codec->extradata = av_mallocz(codec->extradata_size);
  134. get_buffer(pb, codec->extradata, codec->extradata_size);
  135. } else
  136. codec->extradata_size = 0;
  137. /* It is possible for the chunk to contain garbage at the end */
  138. if (size - codec->extradata_size - 18 > 0)
  139. url_fskip(pb, size - codec->extradata_size - 18);
  140. }
  141. }
  142. int wav_codec_get_id(unsigned int tag, int bps)
  143. {
  144. int id;
  145. id = codec_get_id(codec_wav_tags, tag);
  146. if (id <= 0)
  147. return id;
  148. /* handle specific u8 codec */
  149. if (id == CODEC_ID_PCM_S16LE && bps == 8)
  150. id = CODEC_ID_PCM_U8;
  151. return id;
  152. }
  153. #ifdef CONFIG_ENCODERS
  154. typedef struct {
  155. offset_t data;
  156. } WAVContext;
  157. static int wav_write_header(AVFormatContext *s)
  158. {
  159. WAVContext *wav = s->priv_data;
  160. ByteIOContext *pb = &s->pb;
  161. offset_t fmt;
  162. put_tag(pb, "RIFF");
  163. put_le32(pb, 0); /* file length */
  164. put_tag(pb, "WAVE");
  165. /* format header */
  166. fmt = start_tag(pb, "fmt ");
  167. if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
  168. av_free(wav);
  169. return -1;
  170. }
  171. end_tag(pb, fmt);
  172. /* data header */
  173. wav->data = start_tag(pb, "data");
  174. put_flush_packet(pb);
  175. return 0;
  176. }
  177. static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
  178. const uint8_t *buf, int size, int64_t pts)
  179. {
  180. ByteIOContext *pb = &s->pb;
  181. put_buffer(pb, buf, size);
  182. return 0;
  183. }
  184. static int wav_write_trailer(AVFormatContext *s)
  185. {
  186. ByteIOContext *pb = &s->pb;
  187. WAVContext *wav = s->priv_data;
  188. offset_t file_size;
  189. if (!url_is_streamed(&s->pb)) {
  190. end_tag(pb, wav->data);
  191. /* update file size */
  192. file_size = url_ftell(pb);
  193. url_fseek(pb, 4, SEEK_SET);
  194. put_le32(pb, (uint32_t)(file_size - 8));
  195. url_fseek(pb, file_size, SEEK_SET);
  196. put_flush_packet(pb);
  197. }
  198. return 0;
  199. }
  200. #endif //CONFIG_ENCODERS
  201. /* return the size of the found tag */
  202. /* XXX: > 2GB ? */
  203. static int find_tag(ByteIOContext *pb, uint32_t tag1)
  204. {
  205. unsigned int tag;
  206. int size;
  207. for(;;) {
  208. if (url_feof(pb))
  209. return -1;
  210. tag = get_le32(pb);
  211. size = get_le32(pb);
  212. if (tag == tag1)
  213. break;
  214. url_fseek(pb, size, SEEK_CUR);
  215. }
  216. if (size < 0)
  217. size = 0x7fffffff;
  218. return size;
  219. }
  220. static int wav_probe(AVProbeData *p)
  221. {
  222. /* check file header */
  223. if (p->buf_size <= 32)
  224. return 0;
  225. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  226. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  227. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  228. p->buf[10] == 'V' && p->buf[11] == 'E')
  229. return AVPROBE_SCORE_MAX;
  230. else
  231. return 0;
  232. }
  233. /* wav input */
  234. static int wav_read_header(AVFormatContext *s,
  235. AVFormatParameters *ap)
  236. {
  237. int size;
  238. unsigned int tag;
  239. ByteIOContext *pb = &s->pb;
  240. AVStream *st;
  241. /* check RIFF header */
  242. tag = get_le32(pb);
  243. if (tag != MKTAG('R', 'I', 'F', 'F'))
  244. return -1;
  245. get_le32(pb); /* file size */
  246. tag = get_le32(pb);
  247. if (tag != MKTAG('W', 'A', 'V', 'E'))
  248. return -1;
  249. /* parse fmt header */
  250. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  251. if (size < 0)
  252. return -1;
  253. st = av_new_stream(s, 0);
  254. if (!st)
  255. return AVERROR_NOMEM;
  256. get_wav_header(pb, &st->codec, size);
  257. st->need_parsing = 1;
  258. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  259. if (size < 0)
  260. return -1;
  261. return 0;
  262. }
  263. #define MAX_SIZE 4096
  264. static int wav_read_packet(AVFormatContext *s,
  265. AVPacket *pkt)
  266. {
  267. int ret, size;
  268. AVStream *st;
  269. if (url_feof(&s->pb))
  270. return -EIO;
  271. st = s->streams[0];
  272. size = MAX_SIZE;
  273. if (st->codec.block_align > 1) {
  274. if (size < st->codec.block_align)
  275. size = st->codec.block_align;
  276. size = (size / st->codec.block_align) * st->codec.block_align;
  277. }
  278. if (av_new_packet(pkt, size))
  279. return -EIO;
  280. pkt->stream_index = 0;
  281. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  282. if (ret < 0)
  283. av_free_packet(pkt);
  284. /* note: we need to modify the packet size here to handle the last
  285. packet */
  286. pkt->size = ret;
  287. return ret;
  288. }
  289. static int wav_read_close(AVFormatContext *s)
  290. {
  291. return 0;
  292. }
  293. static int wav_read_seek(AVFormatContext *s,
  294. int stream_index, int64_t timestamp)
  295. {
  296. AVStream *st;
  297. st = s->streams[0];
  298. switch(st->codec.codec_id) {
  299. case CODEC_ID_MP2:
  300. case CODEC_ID_MP3:
  301. case CODEC_ID_AC3:
  302. /* use generic seeking with dynamically generated indexes */
  303. return -1;
  304. default:
  305. break;
  306. }
  307. return pcm_read_seek(s, stream_index, timestamp);
  308. }
  309. static AVInputFormat wav_iformat = {
  310. "wav",
  311. "wav format",
  312. 0,
  313. wav_probe,
  314. wav_read_header,
  315. wav_read_packet,
  316. wav_read_close,
  317. wav_read_seek,
  318. };
  319. #ifdef CONFIG_ENCODERS
  320. static AVOutputFormat wav_oformat = {
  321. "wav",
  322. "wav format",
  323. "audio/x-wav",
  324. "wav",
  325. sizeof(WAVContext),
  326. CODEC_ID_PCM_S16LE,
  327. CODEC_ID_NONE,
  328. wav_write_header,
  329. wav_write_packet,
  330. wav_write_trailer,
  331. };
  332. #endif //CONFIG_ENCODERS
  333. int wav_init(void)
  334. {
  335. av_register_input_format(&wav_iformat);
  336. #ifdef CONFIG_ENCODERS
  337. av_register_output_format(&wav_oformat);
  338. #endif //CONFIG_ENCODERS
  339. return 0;
  340. }