vocdec.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. AVIOContext *pb = s->pb;
  39. int header_size;
  40. AVStream *st;
  41. avio_skip(pb, 20);
  42. header_size = avio_rl16(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. avio_skip(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. AVIOContext *pb = s->pb;
  61. VocType type;
  62. int size, tmp_codec=-1;
  63. int sample_rate = 0;
  64. int channels = 1;
  65. while (!voc->remaining_size) {
  66. type = avio_r8(pb);
  67. if (type == VOC_TYPE_EOF)
  68. return AVERROR(EIO);
  69. voc->remaining_size = avio_rl24(pb);
  70. if (!voc->remaining_size) {
  71. if (!s->pb->seekable)
  72. return AVERROR(EIO);
  73. voc->remaining_size = avio_size(pb) - avio_tell(pb);
  74. }
  75. max_size -= 4;
  76. switch (type) {
  77. case VOC_TYPE_VOICE_DATA:
  78. dec->sample_rate = 1000000 / (256 - avio_r8(pb));
  79. if (sample_rate)
  80. dec->sample_rate = sample_rate;
  81. dec->channels = channels;
  82. tmp_codec = avio_r8(pb);
  83. dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
  84. voc->remaining_size -= 2;
  85. max_size -= 2;
  86. channels = 1;
  87. break;
  88. case VOC_TYPE_VOICE_DATA_CONT:
  89. break;
  90. case VOC_TYPE_EXTENDED:
  91. sample_rate = avio_rl16(pb);
  92. avio_r8(pb);
  93. channels = avio_r8(pb) + 1;
  94. sample_rate = 256000000 / (channels * (65536 - sample_rate));
  95. voc->remaining_size = 0;
  96. max_size -= 4;
  97. break;
  98. case VOC_TYPE_NEW_VOICE_DATA:
  99. dec->sample_rate = avio_rl32(pb);
  100. dec->bits_per_coded_sample = avio_r8(pb);
  101. dec->channels = avio_r8(pb);
  102. tmp_codec = avio_rl16(pb);
  103. avio_skip(pb, 4);
  104. voc->remaining_size -= 12;
  105. max_size -= 12;
  106. break;
  107. default:
  108. avio_skip(pb, voc->remaining_size);
  109. max_size -= voc->remaining_size;
  110. voc->remaining_size = 0;
  111. break;
  112. }
  113. }
  114. if (tmp_codec >= 0) {
  115. tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
  116. if (dec->codec_id == CODEC_ID_NONE)
  117. dec->codec_id = tmp_codec;
  118. else if (dec->codec_id != tmp_codec)
  119. av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
  120. if (dec->codec_id == CODEC_ID_NONE) {
  121. if (s->audio_codec_id == CODEC_ID_NONE) {
  122. av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
  123. return AVERROR(EINVAL);
  124. }
  125. av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
  126. }
  127. }
  128. dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
  129. if (max_size <= 0)
  130. max_size = 2048;
  131. size = FFMIN(voc->remaining_size, max_size);
  132. voc->remaining_size -= size;
  133. return av_get_packet(pb, pkt, size);
  134. }
  135. static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
  136. {
  137. return voc_get_packet(s, pkt, s->streams[0], 0);
  138. }
  139. AVInputFormat ff_voc_demuxer = {
  140. "voc",
  141. NULL_IF_CONFIG_SMALL("Creative Voice file format"),
  142. sizeof(VocDecContext),
  143. voc_probe,
  144. voc_read_header,
  145. voc_read_packet,
  146. .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
  147. };