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.

122 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. 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, depth;
  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. depth = avio_rb16(s->pb);
  48. if (depth != 16) {
  49. av_log_ask_for_sample(s, "unsupported depth %d\n", depth);
  50. return AVERROR_INVALIDDATA;
  51. }
  52. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  53. st->codec->channels = avio_rb16(s->pb);
  54. if (!st->codec->channels)
  55. return AVERROR_INVALIDDATA;
  56. if (st->codec->channels == 2)
  57. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  58. else if (st->codec->channels == 4)
  59. st->codec->channel_layout = AV_CH_LAYOUT_4POINT0;
  60. avio_skip(s->pb, 2);
  61. st->codec->sample_rate = avio_rb32(s->pb);
  62. if (st->codec->sample_rate <= 0)
  63. return AVERROR_INVALIDDATA;
  64. st->start_time = 0;
  65. st->duration = avio_rb32(s->pb);
  66. avio_skip(s->pb, 40);
  67. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  68. return 0;
  69. }
  70. static int ast_read_packet(AVFormatContext *s, AVPacket *pkt)
  71. {
  72. uint32_t type, size;
  73. int64_t pos;
  74. int ret;
  75. if (url_feof(s->pb))
  76. return AVERROR_EOF;
  77. pos = avio_tell(s->pb);
  78. type = avio_rl32(s->pb);
  79. size = avio_rb32(s->pb);
  80. if (size > INT_MAX / s->streams[0]->codec->channels)
  81. return AVERROR_INVALIDDATA;
  82. size *= s->streams[0]->codec->channels;
  83. if ((ret = avio_skip(s->pb, 24)) < 0) // padding
  84. return ret;
  85. if (type == MKTAG('B','L','C','K')) {
  86. ret = av_get_packet(s->pb, pkt, size);
  87. pkt->stream_index = 0;
  88. pkt->pos = pos;
  89. } else {
  90. av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", type);
  91. avio_skip(s->pb, size);
  92. ret = AVERROR_INVALIDDATA;
  93. }
  94. return ret;
  95. }
  96. AVInputFormat ff_ast_demuxer = {
  97. .name = "ast",
  98. .long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"),
  99. .read_probe = ast_probe,
  100. .read_header = ast_read_header,
  101. .read_packet = ast_read_packet,
  102. .extensions = "ast",
  103. .flags = AVFMT_GENERIC_INDEX,
  104. };