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.

181 lines
4.4KB

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