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.

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