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.

169 lines
4.6KB

  1. /*
  2. * Simbiosis game demuxer
  3. *
  4. * Copyright (C) 2021 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/internal.h"
  27. #define IMX_TAG MKTAG('I', 'M', 'A', 'X')
  28. typedef struct SimbiosisIMXDemuxContext {
  29. uint8_t pal[AVPALETTE_SIZE];
  30. int pal_changed;
  31. int64_t first_video_packet_pos;
  32. } SimbiosisIMXDemuxContext;
  33. static int simbiosis_imx_probe(const AVProbeData *p)
  34. {
  35. if (AV_RL32(p->buf) != IMX_TAG)
  36. return 0;
  37. if (AV_RN32(p->buf+4) == 0)
  38. return 0;
  39. if (AV_RN16(p->buf+8) == 0)
  40. return 0;
  41. if (AV_RL16(p->buf+10) != 0x102)
  42. return 0;
  43. return AVPROBE_SCORE_EXTENSION + 10;
  44. }
  45. static int simbiosis_imx_read_header(AVFormatContext *s)
  46. {
  47. AVIOContext *pb = s->pb;
  48. AVStream *vst, *ast;
  49. int rate;
  50. vst = avformat_new_stream(s, NULL);
  51. ast = avformat_new_stream(s, NULL);
  52. if (!vst || !ast)
  53. return AVERROR(ENOMEM);
  54. avio_skip(pb, 4);
  55. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  56. vst->codecpar->codec_tag = 0;
  57. vst->codecpar->format = AV_PIX_FMT_PAL8;
  58. vst->codecpar->codec_id = AV_CODEC_ID_SIMBIOSIS_IMX;
  59. vst->start_time = 0;
  60. vst->duration =
  61. vst->nb_frames = avio_rl32(pb);
  62. rate = avio_rl16(pb);
  63. avio_skip(pb, 12);
  64. avpriv_set_pts_info(vst, 64, 1, rate);
  65. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  66. ast->codecpar->codec_tag = 0;
  67. ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  68. ast->codecpar->channels = 1;
  69. ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  70. ast->codecpar->sample_rate = 22050;
  71. ast->start_time = 0;
  72. avpriv_set_pts_info(ast, 64, 1, 22050);
  73. return 0;
  74. }
  75. static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)
  76. {
  77. AVIOContext *pb = s->pb;
  78. SimbiosisIMXDemuxContext *imx = s->priv_data;
  79. uint32_t chunk_size, chunk_type;
  80. int64_t pos = avio_tell(pb);
  81. int ret, idx = -1;
  82. retry:
  83. if (avio_feof(pb))
  84. return AVERROR_EOF;
  85. chunk_size = avio_rl32(pb);
  86. chunk_type = avio_rl32(pb);
  87. switch (chunk_type) {
  88. case 0xAAFF:
  89. return AVERROR_EOF;
  90. case 0xAA99:
  91. idx = 1;
  92. break;
  93. case 0xAA97:
  94. idx = 0;
  95. if (!imx->first_video_packet_pos)
  96. imx->first_video_packet_pos = pos;
  97. break;
  98. case 0xAA98:
  99. if (chunk_size > 256 * 3)
  100. return AVERROR_INVALIDDATA;
  101. for (int i = 0; i < chunk_size / 3; i++) {
  102. unsigned r = avio_r8(pb) << 18;
  103. unsigned g = avio_r8(pb) << 10;
  104. unsigned b = avio_r8(pb) << 2;
  105. AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b);
  106. }
  107. imx->pal_changed = 1;
  108. idx = -1;
  109. break;
  110. default:
  111. return AVERROR_INVALIDDATA;
  112. }
  113. if (idx == -1)
  114. goto retry;
  115. ret = av_get_packet(pb, pkt, chunk_size);
  116. if (ret < 0)
  117. return ret;
  118. if (imx->pal_changed && idx == 0) {
  119. uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  120. AVPALETTE_SIZE);
  121. if (!pal)
  122. return AVERROR(ENOMEM);
  123. memcpy(pal, imx->pal, AVPALETTE_SIZE);
  124. imx->pal_changed = 0;
  125. if (pos <= imx->first_video_packet_pos)
  126. pkt->flags |= AV_PKT_FLAG_KEY;
  127. } else if (idx == 1) {
  128. pkt->flags |= AV_PKT_FLAG_KEY;
  129. }
  130. pkt->pos = pos;
  131. pkt->stream_index = idx;
  132. pkt->duration = idx ? chunk_size : 1;
  133. return ret;
  134. }
  135. AVInputFormat ff_simbiosis_imx_demuxer = {
  136. .name = "simbiosis_imx",
  137. .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"),
  138. .priv_data_size = sizeof(SimbiosisIMXDemuxContext),
  139. .read_probe = simbiosis_imx_probe,
  140. .read_header = simbiosis_imx_read_header,
  141. .read_packet = simbiosis_imx_read_packet,
  142. .extensions = "imx",
  143. .flags = AVFMT_GENERIC_INDEX,
  144. };