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.

140 lines
4.1KB

  1. /*
  2. * ISS (.iss) file demuxer
  3. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavutil/channel_layout.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "libavutil/avstring.h"
  31. #define ISS_SIG "IMA_ADPCM_Sound"
  32. #define ISS_SIG_LEN 15
  33. #define MAX_TOKEN_SIZE 20
  34. typedef struct IssDemuxContext {
  35. int packet_size;
  36. int sample_start_pos;
  37. } IssDemuxContext;
  38. static void get_token(AVIOContext *s, char *buf, int maxlen)
  39. {
  40. int i = 0;
  41. char c;
  42. while ((c = avio_r8(s))) {
  43. if(c == ' ')
  44. break;
  45. if (i < maxlen-1)
  46. buf[i++] = c;
  47. }
  48. if(!c)
  49. avio_r8(s);
  50. buf[i] = 0; /* Ensure null terminated, but may be truncated */
  51. }
  52. static int iss_probe(AVProbeData *p)
  53. {
  54. if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
  55. return 0;
  56. return AVPROBE_SCORE_MAX;
  57. }
  58. static av_cold int iss_read_header(AVFormatContext *s)
  59. {
  60. IssDemuxContext *iss = s->priv_data;
  61. AVIOContext *pb = s->pb;
  62. AVStream *st;
  63. char token[MAX_TOKEN_SIZE];
  64. int stereo, rate_divisor;
  65. get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
  66. get_token(pb, token, sizeof(token)); //packet size
  67. sscanf(token, "%d", &iss->packet_size);
  68. get_token(pb, token, sizeof(token)); //File ID
  69. get_token(pb, token, sizeof(token)); //out size
  70. get_token(pb, token, sizeof(token)); //stereo
  71. sscanf(token, "%d", &stereo);
  72. get_token(pb, token, sizeof(token)); //Unknown1
  73. get_token(pb, token, sizeof(token)); //RateDivisor
  74. sscanf(token, "%d", &rate_divisor);
  75. get_token(pb, token, sizeof(token)); //Unknown2
  76. get_token(pb, token, sizeof(token)); //Version ID
  77. get_token(pb, token, sizeof(token)); //Size
  78. iss->sample_start_pos = avio_tell(pb);
  79. st = avformat_new_stream(s, NULL);
  80. if (!st)
  81. return AVERROR(ENOMEM);
  82. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  83. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ISS;
  84. if (stereo) {
  85. st->codecpar->channels = 2;
  86. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  87. } else {
  88. st->codecpar->channels = 1;
  89. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  90. }
  91. st->codecpar->sample_rate = 44100;
  92. if(rate_divisor > 0)
  93. st->codecpar->sample_rate /= rate_divisor;
  94. st->codecpar->bits_per_coded_sample = 4;
  95. st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate
  96. * st->codecpar->bits_per_coded_sample;
  97. st->codecpar->block_align = iss->packet_size;
  98. avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
  99. return 0;
  100. }
  101. static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
  102. {
  103. IssDemuxContext *iss = s->priv_data;
  104. int ret = av_get_packet(s->pb, pkt, iss->packet_size);
  105. if(ret != iss->packet_size)
  106. return AVERROR(EIO);
  107. pkt->stream_index = 0;
  108. pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
  109. if(s->streams[0]->codecpar->channels > 0)
  110. pkt->pts /= s->streams[0]->codecpar->channels*2;
  111. return 0;
  112. }
  113. AVInputFormat ff_iss_demuxer = {
  114. .name = "iss",
  115. .long_name = NULL_IF_CONFIG_SMALL("Funcom ISS"),
  116. .priv_data_size = sizeof(IssDemuxContext),
  117. .read_probe = iss_probe,
  118. .read_header = iss_read_header,
  119. .read_packet = iss_read_packet,
  120. };