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.

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