Mercurial > libavcodec.hg
comparison a64multienc.c @ 12401:f25a00f68cfa libavcodec
Initial version of the a64 (multicolor charset) codec
author | bindhammer |
---|---|
date | Mon, 23 Aug 2010 11:46:32 +0000 |
parents | |
children | 91db982aaaad |
comparison
equal
deleted
inserted
replaced
12400:4f13b2ded34d | 12401:f25a00f68cfa |
---|---|
1 /* | |
2 * a64 video encoder - multicolor modes | |
3 * Copyright (c) 2009 Tobias Bindhammer | |
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 * a64 video encoder - multicolor modes | |
25 */ | |
26 | |
27 #include "a64enc.h" | |
28 #include "a64tables.h" | |
29 #include "elbg.h" | |
30 #include "libavutil/intreadwrite.h" | |
31 | |
32 #define DITHERSTEPS 8 | |
33 | |
34 static void to_meta_with_crop(AVCodecContext *avctx, AVFrame *p, int *dest) | |
35 { | |
36 int blockx, blocky, x, y; | |
37 int luma = 0; | |
38 int height = FFMIN(avctx->height,C64YRES); | |
39 int width = FFMIN(avctx->width ,C64XRES); | |
40 uint8_t *src = p->data[0]; | |
41 | |
42 for (blocky = 0; blocky < height; blocky += 8) { | |
43 for (blockx = 0; blockx < C64XRES; blockx += 8) { | |
44 for (y = blocky; y < blocky+8 && y < height; y++) { | |
45 for (x = blockx; x < blockx+8 && x < C64XRES; x += 2) { | |
46 if(x < width) { | |
47 /* build average over 2 pixels */ | |
48 luma = (src[(x + 0 + y * p->linesize[0])] + | |
49 src[(x + 1 + y * p->linesize[0])]) / 2; | |
50 /* write blocks as linear data now so they are suitable for elbg */ | |
51 dest[0] = luma; | |
52 } | |
53 dest++; | |
54 } | |
55 } | |
56 } | |
57 } | |
58 } | |
59 | |
60 static void render_charset(AVCodecContext *avctx, uint8_t *charset, | |
61 uint8_t *colrammap) | |
62 { | |
63 A64Context *c = avctx->priv_data; | |
64 uint8_t row1; | |
65 int charpos, x, y; | |
66 int pix; | |
67 int dither; | |
68 int index1, index2; | |
69 int lowdiff, highdiff; | |
70 int maxindex = c->mc_use_5col + 3; | |
71 int maxsteps = DITHERSTEPS * maxindex + 1; | |
72 int *best_cb = c->mc_best_cb; | |
73 | |
74 /* now reduce colors first */ | |
75 for (x = 0; x < 256 * 32; x++) best_cb[x] = best_cb[x] * maxsteps / 255; | |
76 | |
77 /* and render charset */ | |
78 for (charpos = 0; charpos < 256; charpos++) { | |
79 lowdiff = 0; | |
80 highdiff = 0; | |
81 for (y = 0; y < 8; y++) { | |
82 row1 = 0; | |
83 for (x = 0; x < 4; x++) { | |
84 pix = best_cb[y * 4 + x]; | |
85 dither = pix % DITHERSTEPS; | |
86 index1 = pix / DITHERSTEPS; | |
87 index2 = FFMIN(index1 + 1, maxindex); | |
88 | |
89 if (pix > 3 * DITHERSTEPS) | |
90 highdiff += pix - 3 * DITHERSTEPS; | |
91 if (pix < DITHERSTEPS) | |
92 lowdiff += DITHERSTEPS - pix; | |
93 | |
94 row1 <<= 2; | |
95 if (prep_dither_patterns[dither][y & 3][x & 3]) { | |
96 row1 |= 3-(index2 & 3); | |
97 } else { | |
98 row1 |= 3-(index1 & 3); | |
99 } | |
100 } | |
101 charset[y] = row1; | |
102 } | |
103 | |
104 /* are we in 5col mode and need to adjust pixels? */ | |
105 if (c->mc_use_5col && highdiff > 0 && lowdiff > 0) { | |
106 if (lowdiff > highdiff) { | |
107 for (x = 0; x < 32; x++) | |
108 best_cb[x] = FFMIN(3 * DITHERSTEPS, best_cb[x]); | |
109 } else { | |
110 for (x = 0; x < 32; x++) | |
111 best_cb[x] = FFMAX(DITHERSTEPS, best_cb[x]); | |
112 } | |
113 charpos--; /* redo char */ | |
114 } else { | |
115 /* advance pointers */ | |
116 best_cb += 32; | |
117 charset += 8; | |
118 | |
119 if (highdiff > 0) { | |
120 colrammap[charpos] = 0x9; | |
121 } else { | |
122 colrammap[charpos] = 0x8; | |
123 } | |
124 } | |
125 } | |
126 } | |
127 | |
128 static av_cold int a64multi_close_encoder(AVCodecContext *avctx) | |
129 { | |
130 A64Context *c = avctx->priv_data; | |
131 av_free(c->mc_meta_charset); | |
132 av_free(c->mc_best_cb); | |
133 av_free(c->mc_charmap); | |
134 return 0; | |
135 } | |
136 | |
137 static av_cold int a64multi_init_encoder(AVCodecContext *avctx) | |
138 { | |
139 A64Context *c = avctx->priv_data; | |
140 av_lfg_init(&c->randctx, 1); | |
141 | |
142 if (avctx->global_quality < 1) { | |
143 c->mc_lifetime = 4; | |
144 } else { | |
145 c->mc_lifetime = avctx->global_quality /= FF_QP2LAMBDA; | |
146 } | |
147 | |
148 av_log(avctx, AV_LOG_INFO, "charset lifetime set to %d frame(s)\n", c->mc_lifetime); | |
149 | |
150 c->mc_frame_counter = 0; | |
151 c->mc_use_5col = avctx->codec->id == CODEC_ID_A64_MULTI5; | |
152 c->mc_meta_charset = av_malloc(32000 * c->mc_lifetime * sizeof(int)); | |
153 c->mc_best_cb = av_malloc(256 * 32 * sizeof(int)); | |
154 c->mc_charmap = av_malloc(1000 * c->mc_lifetime * sizeof(int)); | |
155 | |
156 avcodec_get_frame_defaults(&c->picture); | |
157 avctx->coded_frame = &c->picture; | |
158 avctx->coded_frame->pict_type = FF_I_TYPE; | |
159 avctx->coded_frame->key_frame = 1; | |
160 if (!avctx->codec_tag) | |
161 avctx->codec_tag = AV_RL32("a64m"); | |
162 | |
163 return 0; | |
164 } | |
165 | |
166 static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf, | |
167 int buf_size, void *data) | |
168 { | |
169 A64Context *c = avctx->priv_data; | |
170 AVFrame *pict = data; | |
171 AVFrame *const p = (AVFrame *) & c->picture; | |
172 | |
173 int frame; | |
174 int a; | |
175 | |
176 uint8_t colrammap[256]; | |
177 int *charmap = c->mc_charmap; | |
178 int *meta = c->mc_meta_charset; | |
179 int *best_cb = c->mc_best_cb; | |
180 int frm_size = 0x400 + 0x400 * c->mc_use_5col; | |
181 int req_size; | |
182 | |
183 /* it is the last frame so prepare to flush */ | |
184 if (!data) | |
185 c->mc_lifetime = c->mc_frame_counter; | |
186 | |
187 req_size = 0x800 + frm_size * c->mc_lifetime; | |
188 | |
189 if (req_size > buf_size) { | |
190 av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size); | |
191 return -1; | |
192 } | |
193 /* fill up mc_meta_charset with framedata until lifetime exceeds */ | |
194 if (c->mc_frame_counter < c->mc_lifetime) { | |
195 *p = *pict; | |
196 p->pict_type = FF_I_TYPE; | |
197 p->key_frame = 1; | |
198 to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter); | |
199 c->mc_frame_counter++; | |
200 /* lifetime is not reached */ | |
201 return 0; | |
202 } | |
203 /* lifetime exceeded so now convert X frames at once */ | |
204 if (c->mc_frame_counter == c->mc_lifetime && c->mc_lifetime > 0) { | |
205 c->mc_frame_counter = 0; | |
206 ff_init_elbg(meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx); | |
207 ff_do_elbg (meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx); | |
208 | |
209 render_charset(avctx, buf, colrammap); | |
210 | |
211 for (frame = 0; frame < c->mc_lifetime; frame++) { | |
212 for (a = 0; a < 1000; a++) { | |
213 buf[0x800 + a] = charmap[a]; | |
214 if (c->mc_use_5col) buf[0xc00 + a] = colrammap[charmap[a]]; | |
215 } | |
216 buf += frm_size; | |
217 charmap += 1000; | |
218 } | |
219 return req_size; | |
220 } | |
221 return 0; | |
222 } | |
223 | |
224 AVCodec a64multi_encoder = { | |
225 .name = "a64multi", | |
226 .type = CODEC_TYPE_VIDEO, | |
227 .id = CODEC_ID_A64_MULTI, | |
228 .priv_data_size = sizeof(A64Context), | |
229 .init = a64multi_init_encoder, | |
230 .encode = a64multi_encode_frame, | |
231 .close = a64multi_close_encoder, | |
232 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE}, | |
233 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"), | |
234 .capabilities = CODEC_CAP_DELAY, | |
235 }; | |
236 | |
237 AVCodec a64multi5_encoder = { | |
238 .name = "a64multi5", | |
239 .type = CODEC_TYPE_VIDEO, | |
240 .id = CODEC_ID_A64_MULTI5, | |
241 .priv_data_size = sizeof(A64Context), | |
242 .init = a64multi_init_encoder, | |
243 .encode = a64multi_encode_frame, | |
244 .close = a64multi_close_encoder, | |
245 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE}, | |
246 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"), | |
247 .capabilities = CODEC_CAP_DELAY, | |
248 }; |