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.

186 lines
4.9KB

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