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.

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