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.

120 lines
4.0KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "avformat.h"
  19. #include "internal.h"
  20. #include "voc.h"
  21. int
  22. ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
  23. {
  24. VocDecContext *voc = s->priv_data;
  25. AVCodecParameters *par = st->codecpar;
  26. AVIOContext *pb = s->pb;
  27. VocType type;
  28. int size, tmp_codec=-1;
  29. int sample_rate = 0;
  30. int channels = 1;
  31. while (!voc->remaining_size) {
  32. type = avio_r8(pb);
  33. if (type == VOC_TYPE_EOF)
  34. return AVERROR(EIO);
  35. voc->remaining_size = avio_rl24(pb);
  36. if (!voc->remaining_size) {
  37. if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
  38. return AVERROR(EIO);
  39. voc->remaining_size = avio_size(pb) - avio_tell(pb);
  40. }
  41. max_size -= 4;
  42. switch (type) {
  43. case VOC_TYPE_VOICE_DATA:
  44. if (!par->sample_rate) {
  45. par->sample_rate = 1000000 / (256 - avio_r8(pb));
  46. if (sample_rate)
  47. par->sample_rate = sample_rate;
  48. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  49. par->channels = channels;
  50. par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
  51. } else
  52. avio_skip(pb, 1);
  53. tmp_codec = avio_r8(pb);
  54. voc->remaining_size -= 2;
  55. max_size -= 2;
  56. channels = 1;
  57. break;
  58. case VOC_TYPE_VOICE_DATA_CONT:
  59. break;
  60. case VOC_TYPE_EXTENDED:
  61. sample_rate = avio_rl16(pb);
  62. avio_r8(pb);
  63. channels = avio_r8(pb) + 1;
  64. sample_rate = 256000000 / (channels * (65536 - sample_rate));
  65. voc->remaining_size = 0;
  66. max_size -= 4;
  67. break;
  68. case VOC_TYPE_NEW_VOICE_DATA:
  69. if (!par->sample_rate) {
  70. par->sample_rate = avio_rl32(pb);
  71. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  72. par->bits_per_coded_sample = avio_r8(pb);
  73. par->channels = avio_r8(pb);
  74. } else
  75. avio_skip(pb, 6);
  76. tmp_codec = avio_rl16(pb);
  77. avio_skip(pb, 4);
  78. voc->remaining_size -= 12;
  79. max_size -= 12;
  80. break;
  81. default:
  82. avio_skip(pb, voc->remaining_size);
  83. max_size -= voc->remaining_size;
  84. voc->remaining_size = 0;
  85. break;
  86. }
  87. }
  88. if (tmp_codec >= 0) {
  89. tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
  90. if (par->codec_id == AV_CODEC_ID_NONE)
  91. par->codec_id = tmp_codec;
  92. else if (par->codec_id != tmp_codec)
  93. av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
  94. if (par->codec_id == AV_CODEC_ID_NONE) {
  95. if (s->audio_codec_id == AV_CODEC_ID_NONE) {
  96. av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
  97. return AVERROR(EINVAL);
  98. }
  99. av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
  100. }
  101. }
  102. par->bit_rate = par->sample_rate * par->bits_per_coded_sample;
  103. if (max_size <= 0)
  104. max_size = 2048;
  105. size = FFMIN(voc->remaining_size, max_size);
  106. voc->remaining_size -= size;
  107. return av_get_packet(pb, pkt, size);
  108. }