sol.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/bswap.h"
  25. #include "avformat.h"
  26. #include "raw.h"
  27. /* if we don't know the size in advance */
  28. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  29. static int sol_probe(AVProbeData *p)
  30. {
  31. /* check file header */
  32. uint16_t magic;
  33. magic=le2me_16(*((uint16_t*)p->buf));
  34. if ((magic == 0x0B8D || magic == 0x0C0D || magic == 0x0C8D) &&
  35. p->buf[2] == 'S' && p->buf[3] == 'O' &&
  36. p->buf[4] == 'L' && p->buf[5] == 0)
  37. return AVPROBE_SCORE_MAX;
  38. else
  39. return 0;
  40. }
  41. #define SOL_DPCM 1
  42. #define SOL_16BIT 4
  43. #define SOL_STEREO 16
  44. static enum CodecID sol_codec_id(int magic, int type)
  45. {
  46. if (magic == 0x0B8D)
  47. {
  48. if (type & SOL_DPCM) return CODEC_ID_SOL_DPCM;
  49. else return CODEC_ID_PCM_U8;
  50. }
  51. if (type & SOL_DPCM)
  52. {
  53. if (type & SOL_16BIT) return CODEC_ID_SOL_DPCM;
  54. else if (magic == 0x0C8D) return CODEC_ID_SOL_DPCM;
  55. else return CODEC_ID_SOL_DPCM;
  56. }
  57. if (type & SOL_16BIT) return CODEC_ID_PCM_S16LE;
  58. return CODEC_ID_PCM_U8;
  59. }
  60. static int sol_codec_type(int magic, int type)
  61. {
  62. if (magic == 0x0B8D) return 1;//SOL_DPCM_OLD;
  63. if (type & SOL_DPCM)
  64. {
  65. if (type & SOL_16BIT) return 3;//SOL_DPCM_NEW16;
  66. else if (magic == 0x0C8D) return 1;//SOL_DPCM_OLD;
  67. else return 2;//SOL_DPCM_NEW8;
  68. }
  69. return -1;
  70. }
  71. static int sol_channels(int magic, int type)
  72. {
  73. if (magic == 0x0B8D || !(type & SOL_STEREO)) return 1;
  74. return 2;
  75. }
  76. static int sol_read_header(AVFormatContext *s,
  77. AVFormatParameters *ap)
  78. {
  79. int size;
  80. unsigned int magic,tag;
  81. ByteIOContext *pb = s->pb;
  82. unsigned int id, channels, rate, type;
  83. enum CodecID codec;
  84. AVStream *st;
  85. /* check ".snd" header */
  86. magic = get_le16(pb);
  87. tag = get_le32(pb);
  88. if (tag != MKTAG('S', 'O', 'L', 0))
  89. return -1;
  90. rate = get_le16(pb);
  91. type = get_byte(pb);
  92. size = get_le32(pb);
  93. if (magic != 0x0B8D)
  94. get_byte(pb); /* newer SOLs contain padding byte */
  95. codec = sol_codec_id(magic, type);
  96. channels = sol_channels(magic, type);
  97. if (codec == CODEC_ID_SOL_DPCM)
  98. id = sol_codec_type(magic, type);
  99. else id = 0;
  100. /* now we are ready: build format streams */
  101. st = av_new_stream(s, 0);
  102. if (!st)
  103. return -1;
  104. st->codec->codec_type = CODEC_TYPE_AUDIO;
  105. st->codec->codec_tag = id;
  106. st->codec->codec_id = codec;
  107. st->codec->channels = channels;
  108. st->codec->sample_rate = rate;
  109. av_set_pts_info(st, 64, 1, rate);
  110. return 0;
  111. }
  112. #define MAX_SIZE 4096
  113. static int sol_read_packet(AVFormatContext *s,
  114. AVPacket *pkt)
  115. {
  116. int ret;
  117. if (url_feof(s->pb))
  118. return AVERROR(EIO);
  119. ret= av_get_packet(s->pb, pkt, MAX_SIZE);
  120. pkt->stream_index = 0;
  121. /* note: we need to modify the packet size here to handle the last
  122. packet */
  123. pkt->size = ret;
  124. return 0;
  125. }
  126. AVInputFormat sol_demuxer = {
  127. "sol",
  128. NULL_IF_CONFIG_SMALL("Sierra SOL format"),
  129. 0,
  130. sol_probe,
  131. sol_read_header,
  132. sol_read_packet,
  133. NULL,
  134. pcm_read_seek,
  135. };