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