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.

275 lines
7.4KB

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