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.

178 lines
4.5KB

  1. /*
  2. * Kega Game Video (KGV1) decoder
  3. * Copyright (c) 2010 Daniel Verkamp
  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. * Kega Game Video decoder
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/imgutils.h"
  27. #include "avcodec.h"
  28. typedef struct {
  29. AVCodecContext *avctx;
  30. AVFrame pic;
  31. uint16_t *prev, *cur;
  32. } KgvContext;
  33. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  34. {
  35. const uint8_t *buf = avpkt->data;
  36. const uint8_t *buf_end = buf + avpkt->size;
  37. KgvContext * const c = avctx->priv_data;
  38. int offsets[7];
  39. uint16_t *out, *prev;
  40. int outcnt = 0, maxcnt;
  41. int w, h, i;
  42. if (avpkt->size < 2)
  43. return -1;
  44. w = (buf[0] + 1) * 8;
  45. h = (buf[1] + 1) * 8;
  46. buf += 2;
  47. if (av_image_check_size(w, h, 0, avctx))
  48. return -1;
  49. if (w != avctx->width || h != avctx->height)
  50. avcodec_set_dimensions(avctx, w, h);
  51. maxcnt = w * h;
  52. out = av_realloc(c->cur, w * h * 2);
  53. if (!out)
  54. return -1;
  55. c->cur = out;
  56. prev = av_realloc(c->prev, w * h * 2);
  57. if (!prev)
  58. return -1;
  59. c->prev = prev;
  60. for (i = 0; i < 7; i++)
  61. offsets[i] = -1;
  62. while (outcnt < maxcnt && buf_end - 2 > buf) {
  63. int code = AV_RL16(buf);
  64. buf += 2;
  65. if (!(code & 0x8000)) {
  66. out[outcnt++] = code; // rgb555 pixel coded directly
  67. } else {
  68. int count;
  69. uint16_t *inp;
  70. if ((code & 0x6000) == 0x6000) {
  71. // copy from previous frame
  72. int oidx = (code >> 10) & 7;
  73. int start;
  74. count = (code & 0x3FF) + 3;
  75. if (offsets[oidx] < 0) {
  76. if (buf_end - 3 < buf)
  77. break;
  78. offsets[oidx] = AV_RL24(buf);
  79. buf += 3;
  80. }
  81. start = (outcnt + offsets[oidx]) % maxcnt;
  82. if (maxcnt - start < count)
  83. break;
  84. inp = prev + start;
  85. } else {
  86. // copy from earlier in this frame
  87. int offset = (code & 0x1FFF) + 1;
  88. if (!(code & 0x6000)) {
  89. count = 2;
  90. } else if ((code & 0x6000) == 0x2000) {
  91. count = 3;
  92. } else {
  93. if (buf_end - 1 < buf)
  94. break;
  95. count = 4 + *buf++;
  96. }
  97. if (outcnt < offset)
  98. break;
  99. inp = out + outcnt - offset;
  100. }
  101. if (maxcnt - outcnt < count)
  102. break;
  103. for (i = 0; i < count; i++)
  104. out[outcnt++] = inp[i];
  105. }
  106. }
  107. if (outcnt - maxcnt)
  108. av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
  109. c->pic.data[0] = (uint8_t *)c->cur;
  110. c->pic.linesize[0] = w * 2;
  111. *data_size = sizeof(AVFrame);
  112. *(AVFrame*)data = c->pic;
  113. FFSWAP(uint16_t *, c->cur, c->prev);
  114. return avpkt->size;
  115. }
  116. static av_cold int decode_init(AVCodecContext *avctx)
  117. {
  118. KgvContext * const c = avctx->priv_data;
  119. c->avctx = avctx;
  120. avctx->pix_fmt = PIX_FMT_RGB555;
  121. avcodec_get_frame_defaults(&c->pic);
  122. return 0;
  123. }
  124. static av_cold int decode_end(AVCodecContext *avctx)
  125. {
  126. KgvContext * const c = avctx->priv_data;
  127. av_freep(&c->cur);
  128. av_freep(&c->prev);
  129. return 0;
  130. }
  131. AVCodec ff_kgv1_decoder = {
  132. .name = "kgv1",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .id = CODEC_ID_KGV1,
  135. .priv_data_size = sizeof(KgvContext),
  136. .init = decode_init,
  137. .close = decode_end,
  138. .decode = decode_frame,
  139. .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
  140. };