iss.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  23. * Funcom ISS file demuxer
  24. * @author Jaikrishnan Menon
  25. * @see http://wiki.multimedia.cx/index.php?title=FunCom_ISS
  26. */
  27. #include "avformat.h"
  28. #include "internal.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(AVIOContext *s, char *buf, int maxlen)
  38. {
  39. int i = 0;
  40. char c;
  41. while ((c = avio_r8(s))) {
  42. if(c == ' ')
  43. break;
  44. if (i < maxlen-1)
  45. buf[i++] = c;
  46. }
  47. if(!c)
  48. avio_r8(s);
  49. buf[i] = 0; /* Ensure null terminated, but may be truncated */
  50. }
  51. static int iss_probe(AVProbeData *p)
  52. {
  53. if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
  54. return 0;
  55. return AVPROBE_SCORE_MAX;
  56. }
  57. static av_cold int iss_read_header(AVFormatContext *s, AVFormatParameters *ap)
  58. {
  59. IssDemuxContext *iss = s->priv_data;
  60. AVIOContext *pb = s->pb;
  61. AVStream *st;
  62. char token[MAX_TOKEN_SIZE];
  63. int stereo, rate_divisor;
  64. get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
  65. get_token(pb, token, sizeof(token)); //packet size
  66. sscanf(token, "%d", &iss->packet_size);
  67. get_token(pb, token, sizeof(token)); //File ID
  68. get_token(pb, token, sizeof(token)); //out size
  69. get_token(pb, token, sizeof(token)); //stereo
  70. sscanf(token, "%d", &stereo);
  71. get_token(pb, token, sizeof(token)); //Unknown1
  72. get_token(pb, token, sizeof(token)); //RateDivisor
  73. sscanf(token, "%d", &rate_divisor);
  74. get_token(pb, token, sizeof(token)); //Unknown2
  75. get_token(pb, token, sizeof(token)); //Version ID
  76. get_token(pb, token, sizeof(token)); //Size
  77. if (iss->packet_size <= 0) {
  78. av_log(s, AV_LOG_ERROR, "packet_size %d is invalid\n", iss->packet_size);
  79. return AVERROR_INVALIDDATA;
  80. }
  81. iss->sample_start_pos = avio_tell(pb);
  82. st = avformat_new_stream(s, NULL);
  83. if (!st)
  84. return AVERROR(ENOMEM);
  85. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  86. st->codec->codec_id = CODEC_ID_ADPCM_IMA_ISS;
  87. st->codec->channels = stereo ? 2 : 1;
  88. st->codec->sample_rate = 44100;
  89. if(rate_divisor > 0)
  90. st->codec->sample_rate /= rate_divisor;
  91. st->codec->bits_per_coded_sample = 4;
  92. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate
  93. * st->codec->bits_per_coded_sample;
  94. st->codec->block_align = iss->packet_size;
  95. avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
  96. return 0;
  97. }
  98. static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
  99. {
  100. IssDemuxContext *iss = s->priv_data;
  101. int ret = av_get_packet(s->pb, pkt, iss->packet_size);
  102. if(ret != iss->packet_size)
  103. return AVERROR(EIO);
  104. pkt->stream_index = 0;
  105. pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
  106. if(s->streams[0]->codec->channels > 0)
  107. pkt->pts /= s->streams[0]->codec->channels*2;
  108. return 0;
  109. }
  110. AVInputFormat ff_iss_demuxer = {
  111. .name = "ISS",
  112. .long_name = NULL_IF_CONFIG_SMALL("Funcom ISS format"),
  113. .priv_data_size = sizeof(IssDemuxContext),
  114. .read_probe = iss_probe,
  115. .read_header = iss_read_header,
  116. .read_packet = iss_read_packet,
  117. };