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.

128 lines
3.5KB

  1. /*
  2. * AST demuxer
  3. * Copyright (c) 2012 Paul B Mahol
  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/channel_layout.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. static int ast_probe(AVProbeData *p)
  26. {
  27. if (AV_RL32(p->buf) == MKTAG('S','T','R','M') &&
  28. AV_RB16(p->buf + 10) &&
  29. AV_RB16(p->buf + 12) &&
  30. AV_RB32(p->buf + 16))
  31. return AVPROBE_SCORE_MAX / 3 * 2;
  32. return 0;
  33. }
  34. static int ast_read_header(AVFormatContext *s)
  35. {
  36. int codec, depth;
  37. AVStream *st;
  38. st = avformat_new_stream(s, NULL);
  39. if (!st)
  40. return AVERROR(ENOMEM);
  41. avio_skip(s->pb, 8);
  42. codec = avio_rb16(s->pb);
  43. switch (codec) {
  44. case 0:
  45. st->codec->codec_id = AV_CODEC_ID_ADPCM_AFC;
  46. break;
  47. case 1:
  48. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
  49. break;
  50. default:
  51. av_log(s, AV_LOG_ERROR, "unsupported codec %d\n", codec);
  52. }
  53. depth = avio_rb16(s->pb);
  54. if (depth != 16) {
  55. av_log_ask_for_sample(s, "unsupported depth %d\n", depth);
  56. return AVERROR_INVALIDDATA;
  57. }
  58. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  59. st->codec->channels = avio_rb16(s->pb);
  60. if (!st->codec->channels)
  61. return AVERROR_INVALIDDATA;
  62. if (st->codec->channels == 2)
  63. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  64. else if (st->codec->channels == 4)
  65. st->codec->channel_layout = AV_CH_LAYOUT_4POINT0;
  66. avio_skip(s->pb, 2);
  67. st->codec->sample_rate = avio_rb32(s->pb);
  68. if (st->codec->sample_rate <= 0)
  69. return AVERROR_INVALIDDATA;
  70. st->start_time = 0;
  71. st->duration = avio_rb32(s->pb);
  72. avio_skip(s->pb, 40);
  73. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  74. return 0;
  75. }
  76. static int ast_read_packet(AVFormatContext *s, AVPacket *pkt)
  77. {
  78. uint32_t type, size;
  79. int64_t pos;
  80. int ret;
  81. if (url_feof(s->pb))
  82. return AVERROR_EOF;
  83. pos = avio_tell(s->pb);
  84. type = avio_rl32(s->pb);
  85. size = avio_rb32(s->pb);
  86. if (size > INT_MAX / s->streams[0]->codec->channels)
  87. return AVERROR_INVALIDDATA;
  88. size *= s->streams[0]->codec->channels;
  89. if ((ret = avio_skip(s->pb, 24)) < 0) // padding
  90. return ret;
  91. if (type == MKTAG('B','L','C','K')) {
  92. ret = av_get_packet(s->pb, pkt, size);
  93. pkt->stream_index = 0;
  94. pkt->pos = pos;
  95. } else {
  96. av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", type);
  97. avio_skip(s->pb, size);
  98. ret = AVERROR_INVALIDDATA;
  99. }
  100. return ret;
  101. }
  102. AVInputFormat ff_ast_demuxer = {
  103. .name = "ast",
  104. .long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"),
  105. .read_probe = ast_probe,
  106. .read_header = ast_read_header,
  107. .read_packet = ast_read_packet,
  108. .extensions = "ast",
  109. .flags = AVFMT_GENERIC_INDEX,
  110. };