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.

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