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.

177 lines
4.3KB

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