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.

118 lines
3.2KB

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