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.0KB

  1. /*
  2. * Sierra SOL demuxer
  3. * Copyright Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 (type & SOL_DPCM)
  48. return AV_CODEC_ID_SOL_DPCM;
  49. if (magic == 0x0B8D)
  50. return AV_CODEC_ID_PCM_U8;
  51. if (type & SOL_16BIT)
  52. return AV_CODEC_ID_PCM_S16LE;
  53. return AV_CODEC_ID_PCM_U8;
  54. }
  55. static int sol_codec_type(int magic, int type)
  56. {
  57. if (magic == 0x0B8D) return 1;//SOL_DPCM_OLD;
  58. if (type & SOL_DPCM)
  59. {
  60. if (type & SOL_16BIT) return 3;//SOL_DPCM_NEW16;
  61. else if (magic == 0x0C8D) return 1;//SOL_DPCM_OLD;
  62. else return 2;//SOL_DPCM_NEW8;
  63. }
  64. return -1;
  65. }
  66. static int sol_channels(int magic, int type)
  67. {
  68. if (magic == 0x0B8D || !(type & SOL_STEREO)) return 1;
  69. return 2;
  70. }
  71. static int sol_read_header(AVFormatContext *s)
  72. {
  73. unsigned int magic,tag;
  74. AVIOContext *pb = s->pb;
  75. unsigned int id, channels, rate, type;
  76. enum AVCodecID codec;
  77. AVStream *st;
  78. /* check ".snd" header */
  79. magic = avio_rl16(pb);
  80. tag = avio_rl32(pb);
  81. if (tag != MKTAG('S', 'O', 'L', 0))
  82. return -1;
  83. rate = avio_rl16(pb);
  84. type = avio_r8(pb);
  85. avio_skip(pb, 4); /* size */
  86. if (magic != 0x0B8D)
  87. avio_r8(pb); /* newer SOLs contain padding byte */
  88. codec = sol_codec_id(magic, type);
  89. channels = sol_channels(magic, type);
  90. if (codec == AV_CODEC_ID_SOL_DPCM)
  91. id = sol_codec_type(magic, type);
  92. else id = 0;
  93. /* now we are ready: build format streams */
  94. st = avformat_new_stream(s, NULL);
  95. if (!st)
  96. return -1;
  97. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  98. st->codecpar->codec_tag = id;
  99. st->codecpar->codec_id = codec;
  100. st->codecpar->channels = channels;
  101. st->codecpar->channel_layout = channels == 1 ? AV_CH_LAYOUT_MONO :
  102. AV_CH_LAYOUT_STEREO;
  103. st->codecpar->sample_rate = rate;
  104. avpriv_set_pts_info(st, 64, 1, rate);
  105. return 0;
  106. }
  107. #define MAX_SIZE 4096
  108. static int sol_read_packet(AVFormatContext *s,
  109. AVPacket *pkt)
  110. {
  111. int ret;
  112. if (s->pb->eof_reached)
  113. return AVERROR(EIO);
  114. ret= av_get_packet(s->pb, pkt, MAX_SIZE);
  115. if (ret < 0)
  116. return ret;
  117. pkt->stream_index = 0;
  118. /* note: we need to modify the packet size here to handle the last
  119. packet */
  120. pkt->size = ret;
  121. return 0;
  122. }
  123. AVInputFormat ff_sol_demuxer = {
  124. .name = "sol",
  125. .long_name = NULL_IF_CONFIG_SMALL("Sierra SOL"),
  126. .read_probe = sol_probe,
  127. .read_header = sol_read_header,
  128. .read_packet = sol_read_packet,
  129. .read_seek = ff_pcm_read_seek,
  130. };