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.

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