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.

198 lines
5.1KB

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