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.

131 lines
3.7KB

  1. /*
  2. * ISS (.iss) file demuxer
  3. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  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. * @file iss.c
  23. * Funcom ISS file demuxer
  24. * @author Jaikrishnan Menon
  25. * for more information on the .iss file format, visit:
  26. * http://wiki.multimedia.cx/index.php?title=FunCom_ISS
  27. */
  28. #include "avformat.h"
  29. #include "libavutil/avstring.h"
  30. #define ISS_SIG "IMA_ADPCM_Sound"
  31. #define ISS_SIG_LEN 15
  32. #define MAX_TOKEN_SIZE 20
  33. typedef struct {
  34. int packet_size;
  35. int sample_start_pos;
  36. } IssDemuxContext;
  37. static void get_token(ByteIOContext *s, char *buf, int maxlen)
  38. {
  39. int i = 0;
  40. char c;
  41. while ((c = get_byte(s))) {
  42. if(c == ' ')
  43. break;
  44. if (i < maxlen-1)
  45. buf[i++] = c;
  46. }
  47. buf[i] = 0; /* Ensure null terminated, but may be truncated */
  48. }
  49. static int iss_probe(AVProbeData *p)
  50. {
  51. if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
  52. return 0;
  53. return AVPROBE_SCORE_MAX;
  54. }
  55. static av_cold int iss_read_header(AVFormatContext *s, AVFormatParameters *ap)
  56. {
  57. IssDemuxContext *iss = s->priv_data;
  58. ByteIOContext *pb = s->pb;
  59. AVStream *st;
  60. char token[MAX_TOKEN_SIZE];
  61. int stereo, rate_divisor;
  62. get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
  63. get_token(pb, token, sizeof(token)); //packet size
  64. sscanf(token, "%d", &iss->packet_size);
  65. get_token(pb, token, sizeof(token)); //File ID
  66. get_token(pb, token, sizeof(token)); //out size
  67. get_token(pb, token, sizeof(token)); //stereo
  68. sscanf(token, "%d", &stereo);
  69. get_token(pb, token, sizeof(token)); //Unknown1
  70. get_token(pb, token, sizeof(token)); //RateDivisor
  71. sscanf(token, "%d", &rate_divisor);
  72. get_token(pb, token, sizeof(token)); //Unknown2
  73. get_token(pb, token, sizeof(token)); //Version ID
  74. get_token(pb, token, sizeof(token)); //Size
  75. iss->sample_start_pos = url_ftell(pb);
  76. st = av_new_stream(s, 0);
  77. if (!st)
  78. return AVERROR(ENOMEM);
  79. st->codec->codec_type = CODEC_TYPE_AUDIO;
  80. st->codec->codec_id = CODEC_ID_ADPCM_IMA_ISS;
  81. st->codec->channels = stereo ? 2 : 1;
  82. st->codec->sample_rate = 44100;
  83. if(rate_divisor > 0)
  84. st->codec->sample_rate /= rate_divisor;
  85. st->codec->bits_per_coded_sample = 4;
  86. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate
  87. * st->codec->bits_per_coded_sample;
  88. st->codec->block_align = iss->packet_size;
  89. av_set_pts_info(st, 32, 1, st->codec->sample_rate);
  90. return 0;
  91. }
  92. static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
  93. {
  94. IssDemuxContext *iss = s->priv_data;
  95. int ret = av_get_packet(s->pb, pkt, iss->packet_size);
  96. if(ret < 0)
  97. return ret;
  98. pkt->stream_index = 0;
  99. pkt->pts = url_ftell(s->pb) - iss->sample_start_pos;
  100. if(s->streams[0]->codec->channels > 0)
  101. pkt->pts /= s->streams[0]->codec->channels*2;
  102. return 0;
  103. }
  104. AVInputFormat iss_demuxer = {
  105. "ISS",
  106. NULL_IF_CONFIG_SMALL("Funcom ISS format"),
  107. sizeof(IssDemuxContext),
  108. iss_probe,
  109. iss_read_header,
  110. iss_read_packet,
  111. };