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
3.4KB

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