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.

152 lines
4.1KB

  1. /*
  2. * Sierra SOL demuxer
  3. * Copyright Konstantin Shishkov
  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. /*
  22. * Based on documents from Game Audio Player and own research
  23. */
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "pcm.h"
  29. /* if we don't know the size in advance */
  30. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  31. static int sol_probe(AVProbeData *p)
  32. {
  33. /* check file header */
  34. uint16_t magic = AV_RL32(p->buf);
  35. if ((magic == 0x0B8D || magic == 0x0C0D || magic == 0x0C8D) &&
  36. p->buf[2] == 'S' && p->buf[3] == 'O' &&
  37. p->buf[4] == 'L' && p->buf[5] == 0)
  38. return AVPROBE_SCORE_MAX;
  39. else
  40. return 0;
  41. }
  42. #define SOL_DPCM 1
  43. #define SOL_16BIT 4
  44. #define SOL_STEREO 16
  45. static enum AVCodecID sol_codec_id(int magic, int type)
  46. {
  47. if (magic == 0x0B8D)
  48. {
  49. if (type & SOL_DPCM) return AV_CODEC_ID_SOL_DPCM;
  50. else return AV_CODEC_ID_PCM_U8;
  51. }
  52. if (type & SOL_DPCM)
  53. {
  54. if (type & SOL_16BIT) return AV_CODEC_ID_SOL_DPCM;
  55. else if (magic == 0x0C8D) return AV_CODEC_ID_SOL_DPCM;
  56. else return AV_CODEC_ID_SOL_DPCM;
  57. }
  58. if (type & SOL_16BIT) return AV_CODEC_ID_PCM_S16LE;
  59. return AV_CODEC_ID_PCM_U8;
  60. }
  61. static int sol_codec_type(int magic, int type)
  62. {
  63. if (magic == 0x0B8D) return 1;//SOL_DPCM_OLD;
  64. if (type & SOL_DPCM)
  65. {
  66. if (type & SOL_16BIT) return 3;//SOL_DPCM_NEW16;
  67. else if (magic == 0x0C8D) return 1;//SOL_DPCM_OLD;
  68. else return 2;//SOL_DPCM_NEW8;
  69. }
  70. return -1;
  71. }
  72. static int sol_channels(int magic, int type)
  73. {
  74. if (magic == 0x0B8D || !(type & SOL_STEREO)) return 1;
  75. return 2;
  76. }
  77. static int sol_read_header(AVFormatContext *s)
  78. {
  79. unsigned int magic,tag;
  80. AVIOContext *pb = s->pb;
  81. unsigned int id, channels, rate, type;
  82. enum AVCodecID codec;
  83. AVStream *st;
  84. /* check ".snd" header */
  85. magic = avio_rl16(pb);
  86. tag = avio_rl32(pb);
  87. if (tag != MKTAG('S', 'O', 'L', 0))
  88. return -1;
  89. rate = avio_rl16(pb);
  90. type = avio_r8(pb);
  91. avio_skip(pb, 4); /* size */
  92. if (magic != 0x0B8D)
  93. avio_r8(pb); /* newer SOLs contain padding byte */
  94. codec = sol_codec_id(magic, type);
  95. channels = sol_channels(magic, type);
  96. if (codec == AV_CODEC_ID_SOL_DPCM)
  97. id = sol_codec_type(magic, type);
  98. else id = 0;
  99. /* now we are ready: build format streams */
  100. st = avformat_new_stream(s, NULL);
  101. if (!st)
  102. return -1;
  103. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  104. st->codec->codec_tag = id;
  105. st->codec->codec_id = codec;
  106. st->codec->channels = channels;
  107. st->codec->channel_layout = channels == 1 ? AV_CH_LAYOUT_MONO :
  108. AV_CH_LAYOUT_STEREO;
  109. st->codec->sample_rate = rate;
  110. avpriv_set_pts_info(st, 64, 1, rate);
  111. return 0;
  112. }
  113. #define MAX_SIZE 4096
  114. static int sol_read_packet(AVFormatContext *s,
  115. AVPacket *pkt)
  116. {
  117. int ret;
  118. if (url_feof(s->pb))
  119. return AVERROR(EIO);
  120. ret= av_get_packet(s->pb, pkt, MAX_SIZE);
  121. if (ret < 0)
  122. return ret;
  123. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  124. pkt->stream_index = 0;
  125. return 0;
  126. }
  127. AVInputFormat ff_sol_demuxer = {
  128. .name = "sol",
  129. .long_name = NULL_IF_CONFIG_SMALL("Sierra SOL"),
  130. .read_probe = sol_probe,
  131. .read_header = sol_read_header,
  132. .read_packet = sol_read_packet,
  133. .read_seek = ff_pcm_read_seek,
  134. };