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.

98 lines
2.7KB

  1. /*
  2. * SVS demuxer
  3. * Copyright (c) 2020 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 svs_probe(const AVProbeData *p)
  26. {
  27. if (p->buf_size < 32)
  28. return 0;
  29. if (memcmp(p->buf, "SVS\00", 4))
  30. return 0;
  31. if (AV_RL32(p->buf + 16) == 0)
  32. return 0;
  33. return AVPROBE_SCORE_MAX / 3;
  34. }
  35. static int svs_read_header(AVFormatContext *s)
  36. {
  37. AVStream *st;
  38. uint32_t pitch;
  39. st = avformat_new_stream(s, NULL);
  40. if (!st)
  41. return AVERROR(ENOMEM);
  42. avio_skip(s->pb, 16);
  43. pitch = avio_rl32(s->pb);
  44. avio_skip(s->pb, 12);
  45. s->internal->data_offset = avio_tell(s->pb);
  46. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  47. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX;
  48. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  49. st->codecpar->channels = 2;
  50. st->codecpar->sample_rate = av_rescale_rnd(pitch, 48000, 4096, AV_ROUND_INF);
  51. st->codecpar->block_align = 32;
  52. st->start_time = 0;
  53. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
  54. st->duration = av_get_audio_frame_duration2(st->codecpar,
  55. avio_size(s->pb) - 32);
  56. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  57. return 0;
  58. }
  59. static int svs_read_packet(AVFormatContext *s, AVPacket *pkt)
  60. {
  61. int ret;
  62. if (avio_feof(s->pb))
  63. return AVERROR_EOF;
  64. ret = av_get_packet(s->pb, pkt, 32 * 256);
  65. if (ret != 32 * 256) {
  66. if (ret < 0)
  67. return ret;
  68. av_shrink_packet(pkt, ret);
  69. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  70. }
  71. pkt->stream_index = 0;
  72. return ret;
  73. }
  74. AVInputFormat ff_svs_demuxer = {
  75. .name = "svs",
  76. .long_name = NULL_IF_CONFIG_SMALL("Square SVS"),
  77. .read_probe = svs_probe,
  78. .read_header = svs_read_header,
  79. .read_packet = svs_read_packet,
  80. .extensions = "svs",
  81. };