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.

395 lines
11KB

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