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.

154 lines
4.6KB

  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 "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 {
  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. if (sscanf(token, "%d", &iss->packet_size) != 1) {
  68. av_log(s, AV_LOG_ERROR, "Failed parsing packet size\n");
  69. return AVERROR_INVALIDDATA;
  70. }
  71. get_token(pb, token, sizeof(token)); //File ID
  72. get_token(pb, token, sizeof(token)); //out size
  73. get_token(pb, token, sizeof(token)); //stereo
  74. if (sscanf(token, "%d", &stereo) != 1) {
  75. av_log(s, AV_LOG_ERROR, "Failed parsing stereo flag\n");
  76. return AVERROR_INVALIDDATA;
  77. }
  78. get_token(pb, token, sizeof(token)); //Unknown1
  79. get_token(pb, token, sizeof(token)); //RateDivisor
  80. if (sscanf(token, "%d", &rate_divisor) != 1) {
  81. av_log(s, AV_LOG_ERROR, "Failed parsing rate_divisor\n");
  82. return AVERROR_INVALIDDATA;
  83. }
  84. get_token(pb, token, sizeof(token)); //Unknown2
  85. get_token(pb, token, sizeof(token)); //Version ID
  86. get_token(pb, token, sizeof(token)); //Size
  87. if (iss->packet_size <= 0) {
  88. av_log(s, AV_LOG_ERROR, "packet_size %d is invalid\n", iss->packet_size);
  89. return AVERROR_INVALIDDATA;
  90. }
  91. iss->sample_start_pos = avio_tell(pb);
  92. st = avformat_new_stream(s, NULL);
  93. if (!st)
  94. return AVERROR(ENOMEM);
  95. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  96. st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_ISS;
  97. if (stereo) {
  98. st->codec->channels = 2;
  99. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  100. } else {
  101. st->codec->channels = 1;
  102. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  103. }
  104. st->codec->sample_rate = 44100;
  105. if(rate_divisor > 0)
  106. st->codec->sample_rate /= rate_divisor;
  107. st->codec->bits_per_coded_sample = 4;
  108. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate
  109. * st->codec->bits_per_coded_sample;
  110. st->codec->block_align = iss->packet_size;
  111. avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
  112. return 0;
  113. }
  114. static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
  115. {
  116. IssDemuxContext *iss = s->priv_data;
  117. int ret = av_get_packet(s->pb, pkt, iss->packet_size);
  118. if(ret != iss->packet_size)
  119. return AVERROR(EIO);
  120. pkt->stream_index = 0;
  121. pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
  122. if(s->streams[0]->codec->channels > 0)
  123. pkt->pts /= s->streams[0]->codec->channels*2;
  124. return 0;
  125. }
  126. AVInputFormat ff_iss_demuxer = {
  127. .name = "iss",
  128. .long_name = NULL_IF_CONFIG_SMALL("Funcom ISS"),
  129. .priv_data_size = sizeof(IssDemuxContext),
  130. .read_probe = iss_probe,
  131. .read_header = iss_read_header,
  132. .read_packet = iss_read_packet,
  133. };