vocdec.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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)
  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 = avformat_new_stream(s, NULL);
  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. ff_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. if (!dec->sample_rate) {
  79. dec->sample_rate = 1000000 / (256 - avio_r8(pb));
  80. if (sample_rate)
  81. dec->sample_rate = sample_rate;
  82. avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
  83. } else
  84. avio_skip(pb, 1);
  85. dec->channels = channels;
  86. tmp_codec = avio_r8(pb);
  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 = avio_rl16(pb);
  96. avio_r8(pb);
  97. channels = avio_r8(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. if (!dec->sample_rate) {
  104. dec->sample_rate = avio_rl32(pb);
  105. avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
  106. } else
  107. avio_skip(pb, 4);
  108. dec->bits_per_coded_sample = avio_r8(pb);
  109. dec->channels = avio_r8(pb);
  110. tmp_codec = avio_rl16(pb);
  111. avio_skip(pb, 4);
  112. voc->remaining_size -= 12;
  113. max_size -= 12;
  114. break;
  115. default:
  116. avio_skip(pb, voc->remaining_size);
  117. max_size -= voc->remaining_size;
  118. voc->remaining_size = 0;
  119. break;
  120. }
  121. }
  122. if (tmp_codec >= 0) {
  123. tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
  124. if (dec->codec_id == AV_CODEC_ID_NONE)
  125. dec->codec_id = tmp_codec;
  126. else if (dec->codec_id != tmp_codec)
  127. av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
  128. if (dec->codec_id == AV_CODEC_ID_NONE) {
  129. if (s->audio_codec_id == AV_CODEC_ID_NONE) {
  130. av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
  131. return AVERROR(EINVAL);
  132. }
  133. av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
  134. }
  135. }
  136. dec->bit_rate = dec->sample_rate * dec->channels * dec->bits_per_coded_sample;
  137. if (max_size <= 0)
  138. max_size = 2048;
  139. size = FFMIN(voc->remaining_size, max_size);
  140. voc->remaining_size -= size;
  141. return av_get_packet(pb, pkt, size);
  142. }
  143. static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
  144. {
  145. return ff_voc_get_packet(s, pkt, s->streams[0], 0);
  146. }
  147. AVInputFormat ff_voc_demuxer = {
  148. .name = "voc",
  149. .long_name = NULL_IF_CONFIG_SMALL("Creative Voice"),
  150. .priv_data_size = sizeof(VocDecContext),
  151. .read_probe = voc_probe,
  152. .read_header = voc_read_header,
  153. .read_packet = voc_read_packet,
  154. .codec_tag = (const AVCodecTag* const []){ ff_voc_codec_tags, 0 },
  155. };