]> git.sesse.net Git - ffmpeg/blob - libavcodec/kgv1dec.c
Move av_fast_{m,re}alloc from lavc to lavu.
[ffmpeg] / libavcodec / kgv1dec.c
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 /**
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 "avcodec.h"
31 #include "internal.h"
32
33 typedef struct {
34     AVCodecContext *avctx;
35     AVFrame prev;
36 } KgvContext;
37
38 static void decode_flush(AVCodecContext *avctx)
39 {
40     KgvContext * const c = avctx->priv_data;
41
42     av_frame_unref(&c->prev);
43 }
44
45 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
46                         AVPacket *avpkt)
47 {
48     AVFrame *frame = data;
49     const uint8_t *buf = avpkt->data;
50     const uint8_t *buf_end = buf + avpkt->size;
51     KgvContext * const c = avctx->priv_data;
52     int offsets[8];
53     uint16_t *out, *prev;
54     int outcnt = 0, maxcnt;
55     int w, h, i, res;
56
57     if (avpkt->size < 2)
58         return AVERROR_INVALIDDATA;
59
60     w = (buf[0] + 1) * 8;
61     h = (buf[1] + 1) * 8;
62     buf += 2;
63
64     if (w != avctx->width || h != avctx->height) {
65         av_frame_unref(&c->prev);
66         if ((res = ff_set_dimensions(avctx, w, h)) < 0)
67             return res;
68     }
69
70     maxcnt = w * h;
71
72     if ((res = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
73         return res;
74     out  = (uint16_t *) frame->data[0];
75     if (c->prev.data[0]) {
76         prev = (uint16_t *) c->prev.data[0];
77     } else {
78         prev = NULL;
79     }
80
81     for (i = 0; i < 8; i++)
82         offsets[i] = -1;
83
84     while (outcnt < maxcnt && buf_end - 2 > buf) {
85         int code = AV_RL16(buf);
86         buf += 2;
87
88         if (!(code & 0x8000)) {
89             out[outcnt++] = code; // rgb555 pixel coded directly
90         } else {
91             int count;
92             int inp_off;
93             uint16_t *inp;
94
95             if ((code & 0x6000) == 0x6000) {
96                 // copy from previous frame
97                 int oidx = (code >> 10) & 7;
98                 int start;
99
100                 count = (code & 0x3FF) + 3;
101
102                 if (offsets[oidx] < 0) {
103                     if (buf_end - 3 < buf)
104                         break;
105                     offsets[oidx] = AV_RL24(buf);
106                     buf += 3;
107                 }
108
109                 start = (outcnt + offsets[oidx]) % maxcnt;
110
111                 if (maxcnt - start < count)
112                     break;
113
114                 if (!prev) {
115                     av_log(avctx, AV_LOG_ERROR,
116                            "Frame reference does not exist\n");
117                     break;
118                 }
119
120                 inp = prev;
121                 inp_off = start;
122             } else {
123                 // copy from earlier in this frame
124                 int offset = (code & 0x1FFF) + 1;
125
126                 if (!(code & 0x6000)) {
127                     count = 2;
128                 } else if ((code & 0x6000) == 0x2000) {
129                     count = 3;
130                 } else {
131                     if (buf_end - 1 < buf)
132                         break;
133                     count = 4 + *buf++;
134                 }
135
136                 if (outcnt < offset)
137                     break;
138
139                 inp = out;
140                 inp_off = outcnt - offset;
141             }
142
143             if (maxcnt - outcnt < count)
144                 break;
145
146             for (i = inp_off; i < count + inp_off; i++) {
147                 out[outcnt++] = inp[i];
148             }
149         }
150     }
151
152     if (outcnt - maxcnt)
153         av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
154
155     av_frame_unref(&c->prev);
156     if ((res = av_frame_ref(&c->prev, frame)) < 0)
157         return res;
158
159     *got_frame = 1;
160
161     return avpkt->size;
162 }
163
164 static av_cold int decode_init(AVCodecContext *avctx)
165 {
166     KgvContext * const c = avctx->priv_data;
167
168     c->avctx = avctx;
169     avctx->pix_fmt = AV_PIX_FMT_RGB555;
170     avctx->flags  |= CODEC_FLAG_EMU_EDGE;
171
172     return 0;
173 }
174
175 static av_cold int decode_end(AVCodecContext *avctx)
176 {
177     decode_flush(avctx);
178     return 0;
179 }
180
181 AVCodec ff_kgv1_decoder = {
182     .name           = "kgv1",
183     .long_name      = NULL_IF_CONFIG_SMALL("Kega Game Video"),
184     .type           = AVMEDIA_TYPE_VIDEO,
185     .id             = AV_CODEC_ID_KGV1,
186     .priv_data_size = sizeof(KgvContext),
187     .init           = decode_init,
188     .close          = decode_end,
189     .decode         = decode_frame,
190     .flush          = decode_flush,
191     .capabilities   = CODEC_CAP_DR1,
192 };