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.

180 lines
5.5KB

  1. /*
  2. * Creative Voice File demuxer.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. int
  25. ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
  26. {
  27. VocDecContext *voc = s->priv_data;
  28. AVCodecContext *dec = st->codec;
  29. AVIOContext *pb = s->pb;
  30. VocType type;
  31. int size, tmp_codec=-1;
  32. int sample_rate = 0;
  33. int channels = 1;
  34. while (!voc->remaining_size) {
  35. type = avio_r8(pb);
  36. if (type == VOC_TYPE_EOF)
  37. return AVERROR(EIO);
  38. voc->remaining_size = avio_rl24(pb);
  39. if (!voc->remaining_size) {
  40. if (!s->pb->seekable)
  41. return AVERROR(EIO);
  42. voc->remaining_size = avio_size(pb) - avio_tell(pb);
  43. }
  44. max_size -= 4;
  45. switch (type) {
  46. case VOC_TYPE_VOICE_DATA:
  47. if (!dec->sample_rate) {
  48. dec->sample_rate = 1000000 / (256 - avio_r8(pb));
  49. if (sample_rate)
  50. dec->sample_rate = sample_rate;
  51. avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
  52. dec->channels = channels;
  53. dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
  54. } else
  55. avio_skip(pb, 1);
  56. tmp_codec = avio_r8(pb);
  57. voc->remaining_size -= 2;
  58. max_size -= 2;
  59. channels = 1;
  60. break;
  61. case VOC_TYPE_VOICE_DATA_CONT:
  62. break;
  63. case VOC_TYPE_EXTENDED:
  64. sample_rate = avio_rl16(pb);
  65. avio_r8(pb);
  66. channels = avio_r8(pb) + 1;
  67. sample_rate = 256000000 / (channels * (65536 - sample_rate));
  68. voc->remaining_size = 0;
  69. max_size -= 4;
  70. break;
  71. case VOC_TYPE_NEW_VOICE_DATA:
  72. if (!dec->sample_rate) {
  73. dec->sample_rate = avio_rl32(pb);
  74. avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
  75. dec->bits_per_coded_sample = avio_r8(pb);
  76. dec->channels = avio_r8(pb);
  77. } else
  78. avio_skip(pb, 6);
  79. tmp_codec = avio_rl16(pb);
  80. avio_skip(pb, 4);
  81. voc->remaining_size -= 12;
  82. max_size -= 12;
  83. break;
  84. default:
  85. avio_skip(pb, voc->remaining_size);
  86. max_size -= voc->remaining_size;
  87. voc->remaining_size = 0;
  88. break;
  89. }
  90. }
  91. if (tmp_codec >= 0) {
  92. tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
  93. if (dec->codec_id == AV_CODEC_ID_NONE)
  94. dec->codec_id = tmp_codec;
  95. else if (dec->codec_id != tmp_codec)
  96. av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
  97. if (dec->codec_id == AV_CODEC_ID_NONE) {
  98. if (s->audio_codec_id == AV_CODEC_ID_NONE) {
  99. av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
  100. return AVERROR(EINVAL);
  101. }
  102. av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
  103. }
  104. }
  105. dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
  106. if (max_size <= 0)
  107. max_size = 2048;
  108. size = FFMIN(voc->remaining_size, max_size);
  109. voc->remaining_size -= size;
  110. return av_get_packet(pb, pkt, size);
  111. }
  112. #if CONFIG_VOC_DEMUXER
  113. static int voc_probe(AVProbeData *p)
  114. {
  115. int version, check;
  116. if (memcmp(p->buf, ff_voc_magic, sizeof(ff_voc_magic) - 1))
  117. return 0;
  118. version = AV_RL16(p->buf + 22);
  119. check = AV_RL16(p->buf + 24);
  120. if (~version + 0x1234 != check)
  121. return 10;
  122. return AVPROBE_SCORE_MAX;
  123. }
  124. static int voc_read_header(AVFormatContext *s)
  125. {
  126. VocDecContext *voc = s->priv_data;
  127. AVIOContext *pb = s->pb;
  128. int header_size;
  129. avio_skip(pb, 20);
  130. header_size = avio_rl16(pb) - 22;
  131. if (header_size != 4) {
  132. av_log(s, AV_LOG_ERROR, "unknown header size: %d\n", header_size);
  133. return AVERROR(ENOSYS);
  134. }
  135. avio_skip(pb, header_size);
  136. s->ctx_flags |= AVFMTCTX_NOHEADER;
  137. voc->remaining_size = 0;
  138. return 0;
  139. }
  140. static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
  141. {
  142. if (!s->nb_streams) {
  143. AVStream *st = avformat_new_stream(s, NULL);
  144. if (!st)
  145. return AVERROR(ENOMEM);
  146. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  147. }
  148. return ff_voc_get_packet(s, pkt, s->streams[0], 0);
  149. }
  150. AVInputFormat ff_voc_demuxer = {
  151. .name = "voc",
  152. .long_name = NULL_IF_CONFIG_SMALL("Creative Voice"),
  153. .priv_data_size = sizeof(VocDecContext),
  154. .read_probe = voc_probe,
  155. .read_header = voc_read_header,
  156. .read_packet = voc_read_packet,
  157. .codec_tag = (const AVCodecTag* const []){ ff_voc_codec_tags, 0 },
  158. };
  159. #endif /* CONFIG_VOC_DEMUXER */