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.

191 lines
5.4KB

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