sol.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 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. unsigned int magic,tag;
  80. AVIOContext *pb = s->pb;
  81. unsigned int id, channels, rate, type;
  82. enum CodecID 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 == 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->sample_rate = rate;
  108. avpriv_set_pts_info(st, 64, 1, rate);
  109. return 0;
  110. }
  111. #define MAX_SIZE 4096
  112. static int sol_read_packet(AVFormatContext *s,
  113. AVPacket *pkt)
  114. {
  115. int ret;
  116. if (url_feof(s->pb))
  117. return AVERROR(EIO);
  118. ret= av_get_packet(s->pb, pkt, MAX_SIZE);
  119. if (ret < 0)
  120. return ret;
  121. pkt->stream_index = 0;
  122. /* note: we need to modify the packet size here to handle the last
  123. packet */
  124. pkt->size = ret;
  125. return 0;
  126. }
  127. AVInputFormat ff_sol_demuxer = {
  128. .name = "sol",
  129. .long_name = NULL_IF_CONFIG_SMALL("Sierra SOL format"),
  130. .read_probe = sol_probe,
  131. .read_header = sol_read_header,
  132. .read_packet = sol_read_packet,
  133. .read_seek = pcm_read_seek,
  134. };