annotate kgv1dec.c @ 11965:3dbbf18d4905 libavcodec

ffv1: remove VLAs
author mru
date Sat, 26 Jun 2010 15:53:25 +0000
parents 7dd2a45249a9
children c35d7bc64882
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11425
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
1 /*
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
2 * Kega Game Video (KGV1) decoder
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
3 * Copyright (c) 2010 Daniel Verkamp
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
4 *
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
5 * This file is part of FFmpeg.
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
6 *
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
11 *
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
15 * Lesser General Public License for more details.
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
16 *
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
20 */
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
21
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
22 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
23 * @file
11425
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
24 * Kega Game Video decoder
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
25 */
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
26
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
27 #include "libavutil/intreadwrite.h"
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
28 #include "avcodec.h"
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
29
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
30 typedef struct {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
31 AVCodecContext *avctx;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
32 AVFrame pic;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
33 uint16_t *prev, *cur;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
34 } KgvContext;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
35
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
36 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
37 {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
38 const uint8_t *buf = avpkt->data;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
39 const uint8_t *buf_end = buf + avpkt->size;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
40 KgvContext * const c = avctx->priv_data;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
41 int offsets[7];
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
42 uint16_t *out, *prev;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
43 int outcnt = 0, maxcnt;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
44 int w, h, i;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
45
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
46 if (avpkt->size < 2)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
47 return -1;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
48
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
49 w = (buf[0] + 1) * 8;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
50 h = (buf[1] + 1) * 8;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
51 buf += 2;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
52
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
53 if (avcodec_check_dimensions(avctx, w, h))
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
54 return -1;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
55
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
56 if (w != avctx->width || h != avctx->height)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
57 avcodec_set_dimensions(avctx, w, h);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
58
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
59 maxcnt = w * h;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
60
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
61 out = av_realloc(c->cur, w * h * 2);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
62 if (!out)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
63 return -1;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
64 c->cur = out;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
65
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
66 prev = av_realloc(c->prev, w * h * 2);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
67 if (!prev)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
68 return -1;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
69 c->prev = prev;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
70
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
71 for (i = 0; i < 7; i++)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
72 offsets[i] = -1;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
73
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
74 while (outcnt < maxcnt && buf_end - 2 > buf) {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
75 int code = AV_RL16(buf);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
76 buf += 2;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
77
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
78 if (!(code & 0x8000)) {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
79 out[outcnt++] = code; // rgb555 pixel coded directly
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
80 } else {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
81 int count;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
82 uint16_t *inp;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
83
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
84 if ((code & 0x6000) == 0x6000) {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
85 // copy from previous frame
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
86 int oidx = (code >> 10) & 7;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
87 int start;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
88
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
89 count = (code & 0x3FF) + 3;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
90
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
91 if (offsets[oidx] < 0) {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
92 if (buf_end - 3 < buf)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
93 break;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
94 offsets[oidx] = AV_RL24(buf);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
95 buf += 3;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
96 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
97
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
98 start = (outcnt + offsets[oidx]) % maxcnt;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
99
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
100 if (maxcnt - start < count)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
101 break;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
102
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
103 inp = prev + start;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
104 } else {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
105 // copy from earlier in this frame
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
106 int offset = (code & 0x1FFF) + 1;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
107
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
108 if (!(code & 0x6000)) {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
109 count = 2;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
110 } else if ((code & 0x6000) == 0x2000) {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
111 count = 3;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
112 } else {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
113 if (buf_end - 1 < buf)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
114 break;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
115 count = 4 + *buf++;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
116 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
117
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
118 if (outcnt < offset)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
119 break;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
120
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
121 inp = out + outcnt - offset;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
122 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
123
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
124 if (maxcnt - outcnt < count)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
125 break;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
126
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
127 for (i = 0; i < count; i++)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
128 out[outcnt++] = inp[i];
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
129 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
130 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
131
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
132 if (outcnt - maxcnt)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
133 av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
134
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
135 c->pic.data[0] = (uint8_t *)c->cur;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
136 c->pic.linesize[0] = w * 2;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
137
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
138 *data_size = sizeof(AVFrame);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
139 *(AVFrame*)data = c->pic;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
140
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
141 FFSWAP(uint16_t *, c->cur, c->prev);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
142
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
143 return avpkt->size;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
144 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
145
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
146 static av_cold int decode_init(AVCodecContext *avctx)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
147 {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
148 KgvContext * const c = avctx->priv_data;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
149
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
150 c->avctx = avctx;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
151 avctx->pix_fmt = PIX_FMT_RGB555;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
152
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
153 return 0;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
154 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
155
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
156 static av_cold int decode_end(AVCodecContext *avctx)
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
157 {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
158 KgvContext * const c = avctx->priv_data;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
159
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
160 av_freep(&c->cur);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
161 av_freep(&c->prev);
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
162
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
163 return 0;
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
164 }
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
165
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
166 AVCodec kgv1_decoder = {
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
167 "kgv1",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 11425
diff changeset
168 AVMEDIA_TYPE_VIDEO,
11425
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
169 CODEC_ID_KGV1,
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
170 sizeof(KgvContext),
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
171 decode_init,
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
172 NULL,
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
173 decode_end,
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
174 decode_frame,
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
175 .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
599e717b0549 Kega Game Video (KGV1) decoder
daniel
parents:
diff changeset
176 };