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.

403 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. /* data header */
  188. wav->data = start_tag(pb, "data");
  189. put_flush_packet(pb);
  190. return 0;
  191. }
  192. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  193. {
  194. ByteIOContext *pb = &s->pb;
  195. put_buffer(pb, pkt->data, pkt->size);
  196. return 0;
  197. }
  198. static int wav_write_trailer(AVFormatContext *s)
  199. {
  200. ByteIOContext *pb = &s->pb;
  201. WAVContext *wav = s->priv_data;
  202. offset_t file_size;
  203. if (!url_is_streamed(&s->pb)) {
  204. end_tag(pb, wav->data);
  205. /* update file size */
  206. file_size = url_ftell(pb);
  207. url_fseek(pb, 4, SEEK_SET);
  208. put_le32(pb, (uint32_t)(file_size - 8));
  209. url_fseek(pb, file_size, SEEK_SET);
  210. put_flush_packet(pb);
  211. }
  212. return 0;
  213. }
  214. #endif //CONFIG_ENCODERS
  215. /* return the size of the found tag */
  216. /* XXX: > 2GB ? */
  217. static int find_tag(ByteIOContext *pb, uint32_t tag1)
  218. {
  219. unsigned int tag;
  220. int size;
  221. for(;;) {
  222. if (url_feof(pb))
  223. return -1;
  224. tag = get_le32(pb);
  225. size = get_le32(pb);
  226. if (tag == tag1)
  227. break;
  228. url_fseek(pb, size, SEEK_CUR);
  229. }
  230. if (size < 0)
  231. size = 0x7fffffff;
  232. return size;
  233. }
  234. static int wav_probe(AVProbeData *p)
  235. {
  236. /* check file header */
  237. if (p->buf_size <= 32)
  238. return 0;
  239. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  240. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  241. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  242. p->buf[10] == 'V' && p->buf[11] == 'E')
  243. return AVPROBE_SCORE_MAX;
  244. else
  245. return 0;
  246. }
  247. /* wav input */
  248. static int wav_read_header(AVFormatContext *s,
  249. AVFormatParameters *ap)
  250. {
  251. int size;
  252. unsigned int tag;
  253. ByteIOContext *pb = &s->pb;
  254. AVStream *st;
  255. /* check RIFF header */
  256. tag = get_le32(pb);
  257. if (tag != MKTAG('R', 'I', 'F', 'F'))
  258. return -1;
  259. get_le32(pb); /* file size */
  260. tag = get_le32(pb);
  261. if (tag != MKTAG('W', 'A', 'V', 'E'))
  262. return -1;
  263. /* parse fmt header */
  264. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  265. if (size < 0)
  266. return -1;
  267. st = av_new_stream(s, 0);
  268. if (!st)
  269. return AVERROR_NOMEM;
  270. get_wav_header(pb, &st->codec, size);
  271. st->need_parsing = 1;
  272. av_set_pts_info(st, 64, 1, st->codec.sample_rate);
  273. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  274. if (size < 0)
  275. return -1;
  276. return 0;
  277. }
  278. #define MAX_SIZE 4096
  279. static int wav_read_packet(AVFormatContext *s,
  280. AVPacket *pkt)
  281. {
  282. int ret, size;
  283. AVStream *st;
  284. if (url_feof(&s->pb))
  285. return AVERROR_IO;
  286. st = s->streams[0];
  287. size = MAX_SIZE;
  288. if (st->codec.block_align > 1) {
  289. if (size < st->codec.block_align)
  290. size = st->codec.block_align;
  291. size = (size / st->codec.block_align) * st->codec.block_align;
  292. }
  293. if (av_new_packet(pkt, size))
  294. return AVERROR_IO;
  295. pkt->stream_index = 0;
  296. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  297. if (ret < 0)
  298. av_free_packet(pkt);
  299. /* note: we need to modify the packet size here to handle the last
  300. packet */
  301. pkt->size = ret;
  302. return ret;
  303. }
  304. static int wav_read_close(AVFormatContext *s)
  305. {
  306. return 0;
  307. }
  308. static int wav_read_seek(AVFormatContext *s,
  309. int stream_index, int64_t timestamp, int flags)
  310. {
  311. AVStream *st;
  312. st = s->streams[0];
  313. switch(st->codec.codec_id) {
  314. case CODEC_ID_MP2:
  315. case CODEC_ID_MP3:
  316. case CODEC_ID_AC3:
  317. case CODEC_ID_DTS:
  318. /* use generic seeking with dynamically generated indexes */
  319. return -1;
  320. default:
  321. break;
  322. }
  323. return pcm_read_seek(s, stream_index, timestamp, flags);
  324. }
  325. static AVInputFormat wav_iformat = {
  326. "wav",
  327. "wav format",
  328. 0,
  329. wav_probe,
  330. wav_read_header,
  331. wav_read_packet,
  332. wav_read_close,
  333. wav_read_seek,
  334. };
  335. #ifdef CONFIG_ENCODERS
  336. static AVOutputFormat wav_oformat = {
  337. "wav",
  338. "wav format",
  339. "audio/x-wav",
  340. "wav",
  341. sizeof(WAVContext),
  342. CODEC_ID_PCM_S16LE,
  343. CODEC_ID_NONE,
  344. wav_write_header,
  345. wav_write_packet,
  346. wav_write_trailer,
  347. };
  348. #endif //CONFIG_ENCODERS
  349. int ff_wav_init(void)
  350. {
  351. av_register_input_format(&wav_iformat);
  352. #ifdef CONFIG_ENCODERS
  353. av_register_output_format(&wav_oformat);
  354. #endif //CONFIG_ENCODERS
  355. return 0;
  356. }