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.

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