sol.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/intreadwrite.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "pcm.h"
  28. /* if we don't know the size in advance */
  29. #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
  30. static int sol_probe(AVProbeData *p)
  31. {
  32. /* check file header */
  33. uint16_t magic = AV_RL32(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 AVCodecID sol_codec_id(int magic, int type)
  45. {
  46. if (magic == 0x0B8D)
  47. {
  48. if (type & SOL_DPCM) return AV_CODEC_ID_SOL_DPCM;
  49. else return AV_CODEC_ID_PCM_U8;
  50. }
  51. if (type & SOL_DPCM)
  52. {
  53. if (type & SOL_16BIT) return AV_CODEC_ID_SOL_DPCM;
  54. else if (magic == 0x0C8D) return AV_CODEC_ID_SOL_DPCM;
  55. else return AV_CODEC_ID_SOL_DPCM;
  56. }
  57. if (type & SOL_16BIT) return AV_CODEC_ID_PCM_S16LE;
  58. return AV_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. {
  78. unsigned int magic,tag;
  79. AVIOContext *pb = s->pb;
  80. unsigned int id, channels, rate, type;
  81. enum AVCodecID codec;
  82. AVStream *st;
  83. /* check ".snd" header */
  84. magic = avio_rl16(pb);
  85. tag = avio_rl32(pb);
  86. if (tag != MKTAG('S', 'O', 'L', 0))
  87. return -1;
  88. rate = avio_rl16(pb);
  89. type = avio_r8(pb);
  90. avio_skip(pb, 4); /* size */
  91. if (magic != 0x0B8D)
  92. avio_r8(pb); /* newer SOLs contain padding byte */
  93. codec = sol_codec_id(magic, type);
  94. channels = sol_channels(magic, type);
  95. if (codec == AV_CODEC_ID_SOL_DPCM)
  96. id = sol_codec_type(magic, type);
  97. else id = 0;
  98. /* now we are ready: build format streams */
  99. st = avformat_new_stream(s, NULL);
  100. if (!st)
  101. return -1;
  102. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  103. st->codec->codec_tag = id;
  104. st->codec->codec_id = codec;
  105. st->codec->channels = channels;
  106. st->codec->sample_rate = rate;
  107. avpriv_set_pts_info(st, 64, 1, rate);
  108. return 0;
  109. }
  110. #define MAX_SIZE 4096
  111. static int sol_read_packet(AVFormatContext *s,
  112. AVPacket *pkt)
  113. {
  114. int ret;
  115. if (url_feof(s->pb))
  116. return AVERROR(EIO);
  117. ret= av_get_packet(s->pb, pkt, MAX_SIZE);
  118. if (ret < 0)
  119. return ret;
  120. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  121. pkt->stream_index = 0;
  122. return 0;
  123. }
  124. AVInputFormat ff_sol_demuxer = {
  125. .name = "sol",
  126. .long_name = NULL_IF_CONFIG_SMALL("Sierra SOL"),
  127. .read_probe = sol_probe,
  128. .read_header = sol_read_header,
  129. .read_packet = sol_read_packet,
  130. .read_seek = ff_pcm_read_seek,
  131. };