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.

218 lines
6.7KB

  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. int64_t duration;
  66. int ret;
  67. av_add_index_entry(st,
  68. avio_tell(pb),
  69. voc->pts,
  70. voc->remaining_size,
  71. 0,
  72. AVINDEX_KEYFRAME);
  73. while (!voc->remaining_size) {
  74. type = avio_r8(pb);
  75. if (type == VOC_TYPE_EOF)
  76. return AVERROR_EOF;
  77. voc->remaining_size = avio_rl24(pb);
  78. if (!voc->remaining_size) {
  79. if (!s->pb->seekable)
  80. return AVERROR(EIO);
  81. voc->remaining_size = avio_size(pb) - avio_tell(pb);
  82. }
  83. max_size -= 4;
  84. switch (type) {
  85. case VOC_TYPE_VOICE_DATA:
  86. if (!dec->sample_rate) {
  87. dec->sample_rate = 1000000 / (256 - avio_r8(pb));
  88. if (sample_rate)
  89. dec->sample_rate = sample_rate;
  90. avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
  91. dec->channels = channels;
  92. dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
  93. } else
  94. avio_skip(pb, 1);
  95. tmp_codec = avio_r8(pb);
  96. voc->remaining_size -= 2;
  97. max_size -= 2;
  98. channels = 1;
  99. break;
  100. case VOC_TYPE_VOICE_DATA_CONT:
  101. break;
  102. case VOC_TYPE_EXTENDED:
  103. sample_rate = avio_rl16(pb);
  104. avio_r8(pb);
  105. channels = avio_r8(pb) + 1;
  106. sample_rate = 256000000 / (channels * (65536 - sample_rate));
  107. voc->remaining_size = 0;
  108. max_size -= 4;
  109. break;
  110. case VOC_TYPE_NEW_VOICE_DATA:
  111. if (!dec->sample_rate) {
  112. dec->sample_rate = avio_rl32(pb);
  113. avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
  114. dec->bits_per_coded_sample = avio_r8(pb);
  115. dec->channels = avio_r8(pb);
  116. } else
  117. avio_skip(pb, 6);
  118. tmp_codec = avio_rl16(pb);
  119. avio_skip(pb, 4);
  120. voc->remaining_size -= 12;
  121. max_size -= 12;
  122. break;
  123. default:
  124. avio_skip(pb, voc->remaining_size);
  125. max_size -= voc->remaining_size;
  126. voc->remaining_size = 0;
  127. break;
  128. }
  129. }
  130. if (tmp_codec >= 0) {
  131. tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
  132. if (dec->codec_id == AV_CODEC_ID_NONE)
  133. dec->codec_id = tmp_codec;
  134. else if (dec->codec_id != tmp_codec)
  135. av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
  136. if (dec->codec_id == AV_CODEC_ID_NONE) {
  137. if (s->audio_codec_id == AV_CODEC_ID_NONE) {
  138. av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
  139. return AVERROR(EINVAL);
  140. }
  141. av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
  142. }
  143. }
  144. dec->bit_rate = dec->sample_rate * dec->channels * dec->bits_per_coded_sample;
  145. if (max_size <= 0)
  146. max_size = 2048;
  147. size = FFMIN(voc->remaining_size, max_size);
  148. voc->remaining_size -= size;
  149. ret = av_get_packet(pb, pkt, size);
  150. pkt->dts = pkt->pts = voc->pts;
  151. duration = av_get_audio_frame_duration(st->codec, size);
  152. if (duration > 0 && voc->pts != AV_NOPTS_VALUE)
  153. voc->pts += duration;
  154. else
  155. voc->pts = AV_NOPTS_VALUE;
  156. return ret;
  157. }
  158. static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
  159. {
  160. return ff_voc_get_packet(s, pkt, s->streams[0], 0);
  161. }
  162. static int voc_read_seek(AVFormatContext *s, int stream_index,
  163. int64_t timestamp, int flags)
  164. {
  165. VocDecContext *voc = s->priv_data;
  166. AVStream *st = s->streams[stream_index];
  167. int index = av_index_search_timestamp(st, timestamp, flags);
  168. if (index >= 0 && index < st->nb_index_entries - 1) {
  169. AVIndexEntry *e = &st->index_entries[index];
  170. avio_seek(s->pb, e->pos, SEEK_SET);
  171. voc->pts = e->timestamp;
  172. voc->remaining_size = e->size;
  173. return 0;
  174. } else if (st->nb_index_entries && st->index_entries[0].timestamp <= timestamp) {
  175. AVIndexEntry *e = &st->index_entries[st->nb_index_entries - 1];
  176. // prepare context for seek_frame_generic()
  177. voc->pts = e->timestamp;
  178. voc->remaining_size = e->size;
  179. }
  180. return -1;
  181. }
  182. AVInputFormat ff_voc_demuxer = {
  183. .name = "voc",
  184. .long_name = NULL_IF_CONFIG_SMALL("Creative Voice"),
  185. .priv_data_size = sizeof(VocDecContext),
  186. .read_probe = voc_probe,
  187. .read_header = voc_read_header,
  188. .read_packet = voc_read_packet,
  189. .read_seek = voc_read_seek,
  190. .codec_tag = (const AVCodecTag* const []){ ff_voc_codec_tags, 0 },
  191. };