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.5KB

  1. /*
  2. * Copyright (c) 2021 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/common.h"
  21. #include "avcodec.h"
  22. #include "bytestream.h"
  23. #include "internal.h"
  24. typedef struct SimbiosisIMXContext {
  25. uint32_t pal[256];
  26. uint8_t history[32768];
  27. int pos;
  28. } SimbiosisIMXContext;
  29. static av_cold int imx_decode_init(AVCodecContext *avctx)
  30. {
  31. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  32. avctx->width = 320;
  33. avctx->height = 160;
  34. return 0;
  35. }
  36. static int imx_decode_frame(AVCodecContext *avctx, void *data,
  37. int *got_frame, AVPacket *avpkt)
  38. {
  39. SimbiosisIMXContext *imx = avctx->priv_data;
  40. int ret, x, y, pal_size;
  41. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
  42. AVFrame *frame = data;
  43. GetByteContext gb;
  44. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  45. return ret;
  46. if (pal && pal_size == AVPALETTE_SIZE) {
  47. memcpy(imx->pal, pal, pal_size);
  48. frame->palette_has_changed = 1;
  49. }
  50. bytestream2_init(&gb, avpkt->data, avpkt->size);
  51. memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
  52. x = 0, y = 0;
  53. while (bytestream2_get_bytes_left(&gb) > 0 &&
  54. x < 320 && y < 160) {
  55. int b = bytestream2_get_byte(&gb);
  56. int len = b & 0x3f;
  57. int op = b >> 6;
  58. int fill;
  59. switch (op) {
  60. case 3:
  61. len = len * 64 + bytestream2_get_byte(&gb);
  62. case 0:
  63. while (len > 0) {
  64. x++;
  65. len--;
  66. if (x >= 320) {
  67. x = 0;
  68. y++;
  69. }
  70. if (y >= 160)
  71. break;
  72. }
  73. break;
  74. case 1:
  75. if (len == 0) {
  76. int offset = bytestream2_get_le16(&gb);
  77. if (offset < 0 || offset >= 32768)
  78. return AVERROR_INVALIDDATA;
  79. len = bytestream2_get_byte(&gb);
  80. while (len > 0 && offset < 32768) {
  81. frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
  82. x++;
  83. len--;
  84. if (x >= 320) {
  85. x = 0;
  86. y++;
  87. }
  88. if (y >= 160)
  89. break;
  90. }
  91. } else {
  92. while (len > 0) {
  93. fill = bytestream2_get_byte(&gb);
  94. frame->data[0][x + y * frame->linesize[0]] = fill;
  95. if (imx->pos < 32768)
  96. imx->history[imx->pos++] = fill;
  97. x++;
  98. len--;
  99. if (x >= 320) {
  100. x = 0;
  101. y++;
  102. }
  103. if (y >= 160)
  104. break;
  105. }
  106. }
  107. break;
  108. case 2:
  109. fill = bytestream2_get_byte(&gb);
  110. while (len > 0) {
  111. frame->data[0][x + y * frame->linesize[0]] = fill;
  112. x++;
  113. len--;
  114. if (x >= 320) {
  115. x = 0;
  116. y++;
  117. }
  118. if (y >= 160)
  119. break;
  120. }
  121. break;
  122. }
  123. }
  124. *got_frame = 1;
  125. return avpkt->size;
  126. }
  127. AVCodec ff_simbiosis_imx_decoder = {
  128. .name = "simbiosis_imx",
  129. .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX Video"),
  130. .type = AVMEDIA_TYPE_VIDEO,
  131. .id = AV_CODEC_ID_SIMBIOSIS_IMX,
  132. .priv_data_size = sizeof(SimbiosisIMXContext),
  133. .init = imx_decode_init,
  134. .decode = imx_decode_frame,
  135. .capabilities = AV_CODEC_CAP_DR1,
  136. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  137. };