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.

273 lines
7.3KB

  1. /*
  2. * Creative Voice File demuxer.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "riff.h" /* for CodecTag */
  21. #include "voc.h"
  22. typedef enum voc_type {
  23. VOC_TYPE_EOF = 0x00,
  24. VOC_TYPE_VOICE_DATA = 0x01,
  25. VOC_TYPE_VOICE_DATA_CONT = 0x02,
  26. VOC_TYPE_SILENCE = 0x03,
  27. VOC_TYPE_MARKER = 0x04,
  28. VOC_TYPE_ASCII = 0x05,
  29. VOC_TYPE_REPETITION_START = 0x06,
  30. VOC_TYPE_REPETITION_END = 0x07,
  31. VOC_TYPE_EXTENDED = 0x08,
  32. VOC_TYPE_NEW_VOICE_DATA = 0x09,
  33. } voc_type_t;
  34. static const int voc_max_pkt_size = 2048;
  35. static const unsigned char voc_magic[] = "Creative Voice File\x1A";
  36. static const CodecTag voc_codec_tags[] = {
  37. {CODEC_ID_PCM_U8, 0x00},
  38. {CODEC_ID_ADPCM_SBPRO_4, 0x01},
  39. {CODEC_ID_ADPCM_SBPRO_3, 0x02},
  40. {CODEC_ID_ADPCM_SBPRO_2, 0x03},
  41. {CODEC_ID_PCM_S16LE, 0x04},
  42. {CODEC_ID_PCM_ALAW, 0x06},
  43. {CODEC_ID_PCM_MULAW, 0x07},
  44. {CODEC_ID_ADPCM_CT, 0x0200},
  45. {0, 0},
  46. };
  47. #ifdef CONFIG_DEMUXERS
  48. static int voc_probe(AVProbeData *p)
  49. {
  50. int version, check;
  51. if (p->buf_size < 26)
  52. return 0;
  53. if (memcmp(p->buf, voc_magic, sizeof(voc_magic) - 1))
  54. return 0;
  55. version = p->buf[22] | (p->buf[23] << 8);
  56. check = p->buf[24] | (p->buf[25] << 8);
  57. if (~version + 0x1234 != check)
  58. return 10;
  59. return AVPROBE_SCORE_MAX;
  60. }
  61. static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
  62. {
  63. voc_dec_context_t *voc = s->priv_data;
  64. ByteIOContext *pb = &s->pb;
  65. int header_size;
  66. AVStream *st;
  67. url_fskip(pb, 20);
  68. header_size = get_le16(pb) - 22;
  69. if (header_size != 4) {
  70. av_log(s, AV_LOG_ERROR, "unkown header size: %d\n", header_size);
  71. return AVERROR_NOTSUPP;
  72. }
  73. url_fskip(pb, header_size);
  74. st = av_new_stream(s, 0);
  75. if (!st)
  76. return AVERROR_NOMEM;
  77. st->codec->codec_type = CODEC_TYPE_AUDIO;
  78. voc->remaining_size = 0;
  79. return 0;
  80. }
  81. int
  82. voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
  83. {
  84. voc_dec_context_t *voc = s->priv_data;
  85. AVCodecContext *dec = st->codec;
  86. ByteIOContext *pb = &s->pb;
  87. voc_type_t type;
  88. int size;
  89. int sample_rate = 0;
  90. int channels = 1;
  91. while (!voc->remaining_size) {
  92. type = get_byte(pb);
  93. if (type == VOC_TYPE_EOF)
  94. return AVERROR_IO;
  95. voc->remaining_size = get_le24(pb);
  96. max_size -= 4;
  97. switch (type) {
  98. case VOC_TYPE_VOICE_DATA:
  99. dec->sample_rate = 1000000 / (256 - get_byte(pb));
  100. if (sample_rate)
  101. dec->sample_rate = sample_rate;
  102. dec->channels = channels;
  103. dec->codec_id = codec_get_id(voc_codec_tags, get_byte(pb));
  104. dec->bits_per_sample = av_get_bits_per_sample(dec->codec_id);
  105. voc->remaining_size -= 2;
  106. max_size -= 2;
  107. channels = 1;
  108. break;
  109. case VOC_TYPE_VOICE_DATA_CONT:
  110. break;
  111. case VOC_TYPE_EXTENDED:
  112. sample_rate = get_le16(pb);
  113. get_byte(pb);
  114. channels = get_byte(pb) + 1;
  115. sample_rate = 256000000 / (channels * (65536 - sample_rate));
  116. voc->remaining_size = 0;
  117. max_size -= 4;
  118. break;
  119. case VOC_TYPE_NEW_VOICE_DATA:
  120. dec->sample_rate = get_le32(pb);
  121. dec->bits_per_sample = get_byte(pb);
  122. dec->channels = get_byte(pb);
  123. dec->codec_id = codec_get_id(voc_codec_tags, get_le16(pb));
  124. url_fskip(pb, 4);
  125. voc->remaining_size -= 12;
  126. max_size -= 12;
  127. break;
  128. default:
  129. url_fskip(pb, voc->remaining_size);
  130. max_size -= voc->remaining_size;
  131. voc->remaining_size = 0;
  132. break;
  133. }
  134. }
  135. dec->bit_rate = dec->sample_rate * dec->bits_per_sample;
  136. if (max_size <= 0)
  137. max_size = voc_max_pkt_size;
  138. size = FFMIN(voc->remaining_size, max_size);
  139. voc->remaining_size -= size;
  140. return av_get_packet(pb, pkt, size);
  141. }
  142. static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
  143. {
  144. return voc_get_packet(s, pkt, s->streams[0], 0);
  145. }
  146. static int voc_read_close(AVFormatContext *s)
  147. {
  148. return 0;
  149. }
  150. AVInputFormat voc_demuxer = {
  151. "voc",
  152. "Creative Voice File format",
  153. sizeof(voc_dec_context_t),
  154. voc_probe,
  155. voc_read_header,
  156. voc_read_packet,
  157. voc_read_close,
  158. };
  159. #endif /* CONFIG_DEMUXERS */
  160. #ifdef CONFIG_MUXERS
  161. typedef struct voc_enc_context {
  162. int param_written;
  163. } voc_enc_context_t;
  164. static int voc_write_header(AVFormatContext *s)
  165. {
  166. ByteIOContext *pb = &s->pb;
  167. const int header_size = 26;
  168. const int version = 0x0114;
  169. if (s->nb_streams != 1
  170. || s->streams[0]->codec->codec_type != CODEC_TYPE_AUDIO)
  171. return AVERROR_NOTSUPP;
  172. put_buffer(pb, voc_magic, sizeof(voc_magic) - 1);
  173. put_le16(pb, header_size);
  174. put_le16(pb, version);
  175. put_le16(pb, ~version + 0x1234);
  176. return 0;
  177. }
  178. static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
  179. {
  180. voc_enc_context_t *voc = s->priv_data;
  181. AVCodecContext *enc = s->streams[0]->codec;
  182. ByteIOContext *pb = &s->pb;
  183. if (!voc->param_written) {
  184. int format = codec_get_tag(voc_codec_tags, enc->codec_id);
  185. if (format > 0xFF) {
  186. put_byte(pb, VOC_TYPE_NEW_VOICE_DATA);
  187. put_le24(pb, pkt->size + 12);
  188. put_le32(pb, enc->sample_rate);
  189. put_byte(pb, enc->bits_per_sample);
  190. put_byte(pb, enc->channels);
  191. put_le16(pb, format);
  192. put_le32(pb, 0);
  193. } else {
  194. if (s->streams[0]->codec->channels > 1) {
  195. put_byte(pb, VOC_TYPE_EXTENDED);
  196. put_le24(pb, 4);
  197. put_le16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
  198. put_byte(pb, format);
  199. put_byte(pb, enc->channels - 1);
  200. }
  201. put_byte(pb, VOC_TYPE_VOICE_DATA);
  202. put_le24(pb, pkt->size + 2);
  203. put_byte(pb, 256 - 1000000 / enc->sample_rate);
  204. put_byte(pb, format);
  205. }
  206. voc->param_written = 1;
  207. } else {
  208. put_byte(pb, VOC_TYPE_VOICE_DATA_CONT);
  209. put_le24(pb, pkt->size);
  210. }
  211. put_buffer(pb, pkt->data, pkt->size);
  212. return 0;
  213. }
  214. static int voc_write_trailer(AVFormatContext *s)
  215. {
  216. put_byte(&s->pb, 0);
  217. return 0;
  218. }
  219. AVOutputFormat voc_muxer = {
  220. "voc",
  221. "Creative Voice File format",
  222. "audio/x-voc",
  223. "voc",
  224. sizeof(voc_enc_context_t),
  225. CODEC_ID_PCM_U8,
  226. CODEC_ID_NONE,
  227. voc_write_header,
  228. voc_write_packet,
  229. voc_write_trailer,
  230. };
  231. #endif /* CONFIG_MUXERS */