| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | /** | ||
| 23 | * @file | ||
| 24 | * Kega Game Video decoder | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/common.h" | ||
| 28 | #include "libavutil/intreadwrite.h" | ||
| 29 | #include "libavutil/imgutils.h" | ||
| 30 | #include "libavutil/mem.h" | ||
| 31 | #include "avcodec.h" | ||
| 32 | #include "codec_internal.h" | ||
| 33 | #include "decode.h" | ||
| 34 | |||
| 35 | typedef struct KgvContext { | ||
| 36 | uint16_t *frame_buffer; | ||
| 37 | uint16_t *last_frame_buffer; | ||
| 38 | } KgvContext; | ||
| 39 | |||
| 40 | 2 | static void decode_flush(AVCodecContext *avctx) | |
| 41 | { | ||
| 42 | 2 | KgvContext * const c = avctx->priv_data; | |
| 43 | |||
| 44 | 2 | av_freep(&c->frame_buffer); | |
| 45 | 2 | av_freep(&c->last_frame_buffer); | |
| 46 | 2 | } | |
| 47 | |||
| 48 | 313 | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 49 | int *got_frame, AVPacket *avpkt) | ||
| 50 | { | ||
| 51 | 313 | const uint8_t *buf = avpkt->data; | |
| 52 | 313 | const uint8_t *buf_end = buf + avpkt->size; | |
| 53 | 313 | KgvContext * const c = avctx->priv_data; | |
| 54 | int offsets[8]; | ||
| 55 | uint8_t *out, *prev; | ||
| 56 | 313 | int outcnt = 0, maxcnt; | |
| 57 | int w, h, i, res; | ||
| 58 | |||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313 times.
|
313 | if (avpkt->size < 2) |
| 60 | ✗ | return AVERROR_INVALIDDATA; | |
| 61 | |||
| 62 | 313 | w = (buf[0] + 1) * 8; | |
| 63 | 313 | h = (buf[1] + 1) * 8; | |
| 64 | 313 | buf += 2; | |
| 65 | |||
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313 times.
|
313 | if (avpkt->size < 2 + w*h / 513) |
| 67 | ✗ | return AVERROR_INVALIDDATA; | |
| 68 | |||
| 69 |
2/4✓ Branch 0 taken 313 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 313 times.
|
313 | if (w != avctx->width || h != avctx->height) { |
| 70 | ✗ | av_freep(&c->frame_buffer); | |
| 71 | ✗ | av_freep(&c->last_frame_buffer); | |
| 72 | ✗ | if ((res = ff_set_dimensions(avctx, w, h)) < 0) | |
| 73 | ✗ | return res; | |
| 74 | } | ||
| 75 | |||
| 76 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 312 times.
|
313 | if (!c->frame_buffer) { |
| 77 | 1 | c->frame_buffer = av_mallocz(avctx->width * avctx->height * 2); | |
| 78 | 1 | c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2); | |
| 79 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!c->frame_buffer || !c->last_frame_buffer) { |
| 80 | ✗ | decode_flush(avctx); | |
| 81 | ✗ | return AVERROR(ENOMEM); | |
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | 313 | maxcnt = w * h; | |
| 86 | |||
| 87 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 313 times.
|
313 | if ((res = ff_get_buffer(avctx, frame, 0)) < 0) |
| 88 | ✗ | return res; | |
| 89 | 313 | out = (uint8_t*)c->frame_buffer; | |
| 90 | 313 | prev = (uint8_t*)c->last_frame_buffer; | |
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 2504 times.
✓ Branch 1 taken 313 times.
|
2817 | for (i = 0; i < 8; i++) |
| 93 | 2504 | offsets[i] = -1; | |
| 94 | |||
| 95 |
3/4✓ Branch 0 taken 50791 times.
✓ Branch 1 taken 313 times.
✓ Branch 2 taken 50791 times.
✗ Branch 3 not taken.
|
51104 | while (outcnt < maxcnt && buf_end - 2 >= buf) { |
| 96 | 50791 | int code = AV_RL16(buf); | |
| 97 | 50791 | buf += 2; | |
| 98 | |||
| 99 |
2/2✓ Branch 0 taken 756 times.
✓ Branch 1 taken 50035 times.
|
50791 | if (!(code & 0x8000)) { |
| 100 | 756 | AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly | |
| 101 | 756 | outcnt++; | |
| 102 | } else { | ||
| 103 | int count; | ||
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 22112 times.
✓ Branch 1 taken 27923 times.
|
50035 | if ((code & 0x6000) == 0x6000) { |
| 106 | // copy from previous frame | ||
| 107 | 22112 | int oidx = (code >> 10) & 7; | |
| 108 | int start; | ||
| 109 | |||
| 110 | 22112 | count = (code & 0x3FF) + 3; | |
| 111 | |||
| 112 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 21808 times.
|
22112 | if (offsets[oidx] < 0) { |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
|
304 | if (buf_end - 3 < buf) |
| 114 | ✗ | break; | |
| 115 | 304 | offsets[oidx] = AV_RL24(buf); | |
| 116 | 304 | buf += 3; | |
| 117 | } | ||
| 118 | |||
| 119 | 22112 | start = (outcnt + offsets[oidx]) % maxcnt; | |
| 120 | |||
| 121 |
2/4✓ Branch 0 taken 22112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22112 times.
✗ Branch 3 not taken.
|
22112 | if (maxcnt - start < count || maxcnt - outcnt < count) |
| 122 | break; | ||
| 123 | |||
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22112 times.
|
22112 | if (!prev) { |
| 125 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 126 | "Frame reference does not exist\n"); | ||
| 127 | ✗ | break; | |
| 128 | } | ||
| 129 | |||
| 130 | 22112 | memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count); | |
| 131 | } else { | ||
| 132 | // copy from earlier in this frame | ||
| 133 | 27923 | int offset = (code & 0x1FFF) + 1; | |
| 134 | |||
| 135 |
2/2✓ Branch 0 taken 1091 times.
✓ Branch 1 taken 26832 times.
|
27923 | if (!(code & 0x6000)) { |
| 136 | 1091 | count = 2; | |
| 137 |
2/2✓ Branch 0 taken 776 times.
✓ Branch 1 taken 26056 times.
|
26832 | } else if ((code & 0x6000) == 0x2000) { |
| 138 | 776 | count = 3; | |
| 139 | } else { | ||
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26056 times.
|
26056 | if (buf_end - 1 < buf) |
| 141 | ✗ | break; | |
| 142 | 26056 | count = 4 + *buf++; | |
| 143 | } | ||
| 144 | |||
| 145 |
2/4✓ Branch 0 taken 27923 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27923 times.
✗ Branch 3 not taken.
|
27923 | if (outcnt < offset || maxcnt - outcnt < count) |
| 146 | break; | ||
| 147 | |||
| 148 | 27923 | av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count); | |
| 149 | } | ||
| 150 | 50035 | outcnt += count; | |
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313 times.
|
313 | if (outcnt - maxcnt) |
| 155 | ✗ | av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt); | |
| 156 | |||
| 157 | 313 | av_image_copy_plane(frame->data[0], frame->linesize[0], | |
| 158 | 313 | (const uint8_t*)c->frame_buffer, avctx->width * 2, | |
| 159 | 313 | avctx->width * 2, avctx->height); | |
| 160 | 313 | FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer); | |
| 161 | |||
| 162 | 313 | *got_frame = 1; | |
| 163 | |||
| 164 | 313 | return avpkt->size; | |
| 165 | } | ||
| 166 | |||
| 167 | 2 | static av_cold int decode_init(AVCodecContext *avctx) | |
| 168 | { | ||
| 169 | 2 | avctx->pix_fmt = AV_PIX_FMT_RGB555; | |
| 170 | |||
| 171 | 2 | return 0; | |
| 172 | } | ||
| 173 | |||
| 174 | 2 | static av_cold int decode_end(AVCodecContext *avctx) | |
| 175 | { | ||
| 176 | 2 | decode_flush(avctx); | |
| 177 | 2 | return 0; | |
| 178 | } | ||
| 179 | |||
| 180 | const FFCodec ff_kgv1_decoder = { | ||
| 181 | .p.name = "kgv1", | ||
| 182 | CODEC_LONG_NAME("Kega Game Video"), | ||
| 183 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 184 | .p.id = AV_CODEC_ID_KGV1, | ||
| 185 | .priv_data_size = sizeof(KgvContext), | ||
| 186 | .init = decode_init, | ||
| 187 | .close = decode_end, | ||
| 188 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 189 | .flush = decode_flush, | ||
| 190 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 191 | }; | ||
| 192 |