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.

165 lines
5.0KB

  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 Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "voc.h"
  23. #include "internal.h"
  24. static int voc_probe(AVProbeData *p)
  25. {
  26. int version, check;
  27. if (memcmp(p->buf, ff_voc_magic, sizeof(ff_voc_magic) - 1))
  28. return 0;
  29. version = AV_RL16(p->buf + 22);
  30. check = AV_RL16(p->buf + 24);
  31. if (~version + 0x1234 != check)
  32. return 10;
  33. return AVPROBE_SCORE_MAX;
  34. }
  35. static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
  36. {
  37. VocDecContext *voc = s->priv_data;
  38. ByteIOContext *pb = s->pb;
  39. int header_size;
  40. AVStream *st;
  41. url_fskip(pb, 20);
  42. header_size = get_le16(pb) - 22;
  43. if (header_size != 4) {
  44. av_log(s, AV_LOG_ERROR, "unknown header size: %d\n", header_size);
  45. return AVERROR(ENOSYS);
  46. }
  47. url_fskip(pb, header_size);
  48. st = av_new_stream(s, 0);
  49. if (!st)
  50. return AVERROR(ENOMEM);
  51. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  52. voc->remaining_size = 0;
  53. return 0;
  54. }
  55. int
  56. voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
  57. {
  58. VocDecContext *voc = s->priv_data;
  59. AVCodecContext *dec = st->codec;
  60. ByteIOContext *pb = s->pb;
  61. VocType type;
  62. int size, tmp_codec;
  63. int sample_rate = 0;
  64. int channels = 1;
  65. while (!voc->remaining_size) {
  66. type = get_byte(pb);
  67. if (type == VOC_TYPE_EOF)
  68. return AVERROR(EIO);
  69. voc->remaining_size = get_le24(pb);
  70. if (!voc->remaining_size) {
  71. if (url_is_streamed(s->pb))
  72. return AVERROR(EIO);
  73. voc->remaining_size = url_fsize(pb) - url_ftell(pb);
  74. }
  75. max_size -= 4;
  76. switch (type) {
  77. case VOC_TYPE_VOICE_DATA:
  78. dec->sample_rate = 1000000 / (256 - get_byte(pb));
  79. if (sample_rate)
  80. dec->sample_rate = sample_rate;
  81. dec->channels = channels;
  82. tmp_codec = ff_codec_get_id(ff_voc_codec_tags, get_byte(pb));
  83. if (dec->codec_id == CODEC_ID_NONE)
  84. dec->codec_id = tmp_codec;
  85. else if (dec->codec_id != tmp_codec)
  86. av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
  87. dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
  88. voc->remaining_size -= 2;
  89. max_size -= 2;
  90. channels = 1;
  91. break;
  92. case VOC_TYPE_VOICE_DATA_CONT:
  93. break;
  94. case VOC_TYPE_EXTENDED:
  95. sample_rate = get_le16(pb);
  96. get_byte(pb);
  97. channels = get_byte(pb) + 1;
  98. sample_rate = 256000000 / (channels * (65536 - sample_rate));
  99. voc->remaining_size = 0;
  100. max_size -= 4;
  101. break;
  102. case VOC_TYPE_NEW_VOICE_DATA:
  103. dec->sample_rate = get_le32(pb);
  104. dec->bits_per_coded_sample = get_byte(pb);
  105. dec->channels = get_byte(pb);
  106. tmp_codec = ff_codec_get_id(ff_voc_codec_tags, get_byte(pb));
  107. if (dec->codec_id == CODEC_ID_NONE)
  108. dec->codec_id = tmp_codec;
  109. else if (dec->codec_id != tmp_codec)
  110. av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
  111. url_fskip(pb, 4);
  112. voc->remaining_size -= 12;
  113. max_size -= 12;
  114. break;
  115. default:
  116. url_fskip(pb, voc->remaining_size);
  117. max_size -= voc->remaining_size;
  118. voc->remaining_size = 0;
  119. break;
  120. }
  121. if (dec->codec_id == CODEC_ID_NONE) {
  122. av_log(s, AV_LOG_ERROR, "Invalid codec_id\n");
  123. if (s->audio_codec_id == CODEC_ID_NONE) return AVERROR(EINVAL);
  124. }
  125. }
  126. dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
  127. if (max_size <= 0)
  128. max_size = 2048;
  129. size = FFMIN(voc->remaining_size, max_size);
  130. voc->remaining_size -= size;
  131. return av_get_packet(pb, pkt, size);
  132. }
  133. static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
  134. {
  135. return voc_get_packet(s, pkt, s->streams[0], 0);
  136. }
  137. AVInputFormat voc_demuxer = {
  138. "voc",
  139. NULL_IF_CONFIG_SMALL("Creative Voice file format"),
  140. sizeof(VocDecContext),
  141. voc_probe,
  142. voc_read_header,
  143. voc_read_packet,
  144. .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
  145. };