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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. uint16_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 = (uint16_t *) frame->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. av_frame_unref(&c->prev);
  129. if ((res = av_frame_ref(&c->prev, frame)) < 0)
  130. return res;
  131. *got_frame = 1;
  132. return avpkt->size;
  133. }
  134. static av_cold int decode_init(AVCodecContext *avctx)
  135. {
  136. KgvContext * const c = avctx->priv_data;
  137. c->avctx = avctx;
  138. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  139. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  140. return 0;
  141. }
  142. static av_cold int decode_end(AVCodecContext *avctx)
  143. {
  144. decode_flush(avctx);
  145. return 0;
  146. }
  147. AVCodec ff_kgv1_decoder = {
  148. .name = "kgv1",
  149. .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
  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. .capabilities = CODEC_CAP_DR1,
  158. };