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.

195 lines
5.0KB

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