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.

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