annotate a64multienc.c @ 12408:ae72506d4c2a libavcodec

acenc: LAME-inspired window decision This performs quite a bit better than the current 3GPP-inspired window decision on all the samples I have tested. On the castanets.wav sample it performs very similar to iTunes window selection, and seems to perform better than Nero. On fatboy.wav, it seems to perform at least as good as iTunes, if not better. Nero performs horribly on this sample. Patch by: Nathan Caldwell <saintdev@gmail.com>
author alexc
date Mon, 23 Aug 2010 20:00:03 +0000
parents f25a00f68cfa
children 91db982aaaad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12401
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
1 /*
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
2 * a64 video encoder - multicolor modes
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
3 * Copyright (c) 2009 Tobias Bindhammer
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
4 *
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
5 * This file is part of FFmpeg.
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
6 *
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
11 *
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
15 * Lesser General Public License for more details.
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
16 *
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
20 */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
21
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
22 /**
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
23 * @file
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
24 * a64 video encoder - multicolor modes
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
25 */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
26
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
27 #include "a64enc.h"
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
28 #include "a64tables.h"
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
29 #include "elbg.h"
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
30 #include "libavutil/intreadwrite.h"
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
31
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
32 #define DITHERSTEPS 8
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
33
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
34 static void to_meta_with_crop(AVCodecContext *avctx, AVFrame *p, int *dest)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
35 {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
36 int blockx, blocky, x, y;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
37 int luma = 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
38 int height = FFMIN(avctx->height,C64YRES);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
39 int width = FFMIN(avctx->width ,C64XRES);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
40 uint8_t *src = p->data[0];
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
41
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
42 for (blocky = 0; blocky < height; blocky += 8) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
43 for (blockx = 0; blockx < C64XRES; blockx += 8) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
44 for (y = blocky; y < blocky+8 && y < height; y++) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
45 for (x = blockx; x < blockx+8 && x < C64XRES; x += 2) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
46 if(x < width) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
47 /* build average over 2 pixels */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
48 luma = (src[(x + 0 + y * p->linesize[0])] +
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
49 src[(x + 1 + y * p->linesize[0])]) / 2;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
50 /* write blocks as linear data now so they are suitable for elbg */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
51 dest[0] = luma;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
52 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
53 dest++;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
54 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
55 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
56 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
57 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
58 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
59
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
60 static void render_charset(AVCodecContext *avctx, uint8_t *charset,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
61 uint8_t *colrammap)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
62 {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
63 A64Context *c = avctx->priv_data;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
64 uint8_t row1;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
65 int charpos, x, y;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
66 int pix;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
67 int dither;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
68 int index1, index2;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
69 int lowdiff, highdiff;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
70 int maxindex = c->mc_use_5col + 3;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
71 int maxsteps = DITHERSTEPS * maxindex + 1;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
72 int *best_cb = c->mc_best_cb;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
73
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
74 /* now reduce colors first */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
75 for (x = 0; x < 256 * 32; x++) best_cb[x] = best_cb[x] * maxsteps / 255;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
76
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
77 /* and render charset */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
78 for (charpos = 0; charpos < 256; charpos++) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
79 lowdiff = 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
80 highdiff = 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
81 for (y = 0; y < 8; y++) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
82 row1 = 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
83 for (x = 0; x < 4; x++) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
84 pix = best_cb[y * 4 + x];
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
85 dither = pix % DITHERSTEPS;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
86 index1 = pix / DITHERSTEPS;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
87 index2 = FFMIN(index1 + 1, maxindex);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
88
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
89 if (pix > 3 * DITHERSTEPS)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
90 highdiff += pix - 3 * DITHERSTEPS;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
91 if (pix < DITHERSTEPS)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
92 lowdiff += DITHERSTEPS - pix;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
93
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
94 row1 <<= 2;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
95 if (prep_dither_patterns[dither][y & 3][x & 3]) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
96 row1 |= 3-(index2 & 3);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
97 } else {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
98 row1 |= 3-(index1 & 3);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
99 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
100 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
101 charset[y] = row1;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
102 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
103
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
104 /* are we in 5col mode and need to adjust pixels? */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
105 if (c->mc_use_5col && highdiff > 0 && lowdiff > 0) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
106 if (lowdiff > highdiff) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
107 for (x = 0; x < 32; x++)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
108 best_cb[x] = FFMIN(3 * DITHERSTEPS, best_cb[x]);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
109 } else {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
110 for (x = 0; x < 32; x++)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
111 best_cb[x] = FFMAX(DITHERSTEPS, best_cb[x]);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
112 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
113 charpos--; /* redo char */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
114 } else {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
115 /* advance pointers */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
116 best_cb += 32;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
117 charset += 8;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
118
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
119 if (highdiff > 0) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
120 colrammap[charpos] = 0x9;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
121 } else {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
122 colrammap[charpos] = 0x8;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
123 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
124 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
125 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
126 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
127
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
128 static av_cold int a64multi_close_encoder(AVCodecContext *avctx)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
129 {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
130 A64Context *c = avctx->priv_data;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
131 av_free(c->mc_meta_charset);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
132 av_free(c->mc_best_cb);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
133 av_free(c->mc_charmap);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
134 return 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
135 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
136
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
137 static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
138 {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
139 A64Context *c = avctx->priv_data;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
140 av_lfg_init(&c->randctx, 1);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
141
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
142 if (avctx->global_quality < 1) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
143 c->mc_lifetime = 4;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
144 } else {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
145 c->mc_lifetime = avctx->global_quality /= FF_QP2LAMBDA;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
146 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
147
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
148 av_log(avctx, AV_LOG_INFO, "charset lifetime set to %d frame(s)\n", c->mc_lifetime);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
149
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
150 c->mc_frame_counter = 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
151 c->mc_use_5col = avctx->codec->id == CODEC_ID_A64_MULTI5;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
152 c->mc_meta_charset = av_malloc(32000 * c->mc_lifetime * sizeof(int));
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
153 c->mc_best_cb = av_malloc(256 * 32 * sizeof(int));
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
154 c->mc_charmap = av_malloc(1000 * c->mc_lifetime * sizeof(int));
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
155
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
156 avcodec_get_frame_defaults(&c->picture);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
157 avctx->coded_frame = &c->picture;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
158 avctx->coded_frame->pict_type = FF_I_TYPE;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
159 avctx->coded_frame->key_frame = 1;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
160 if (!avctx->codec_tag)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
161 avctx->codec_tag = AV_RL32("a64m");
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
162
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
163 return 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
164 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
165
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
166 static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
167 int buf_size, void *data)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
168 {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
169 A64Context *c = avctx->priv_data;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
170 AVFrame *pict = data;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
171 AVFrame *const p = (AVFrame *) & c->picture;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
172
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
173 int frame;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
174 int a;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
175
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
176 uint8_t colrammap[256];
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
177 int *charmap = c->mc_charmap;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
178 int *meta = c->mc_meta_charset;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
179 int *best_cb = c->mc_best_cb;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
180 int frm_size = 0x400 + 0x400 * c->mc_use_5col;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
181 int req_size;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
182
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
183 /* it is the last frame so prepare to flush */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
184 if (!data)
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
185 c->mc_lifetime = c->mc_frame_counter;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
186
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
187 req_size = 0x800 + frm_size * c->mc_lifetime;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
188
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
189 if (req_size > buf_size) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
190 av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
191 return -1;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
192 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
193 /* fill up mc_meta_charset with framedata until lifetime exceeds */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
194 if (c->mc_frame_counter < c->mc_lifetime) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
195 *p = *pict;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
196 p->pict_type = FF_I_TYPE;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
197 p->key_frame = 1;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
198 to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
199 c->mc_frame_counter++;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
200 /* lifetime is not reached */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
201 return 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
202 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
203 /* lifetime exceeded so now convert X frames at once */
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
204 if (c->mc_frame_counter == c->mc_lifetime && c->mc_lifetime > 0) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
205 c->mc_frame_counter = 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
206 ff_init_elbg(meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
207 ff_do_elbg (meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
208
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
209 render_charset(avctx, buf, colrammap);
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
210
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
211 for (frame = 0; frame < c->mc_lifetime; frame++) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
212 for (a = 0; a < 1000; a++) {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
213 buf[0x800 + a] = charmap[a];
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
214 if (c->mc_use_5col) buf[0xc00 + a] = colrammap[charmap[a]];
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
215 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
216 buf += frm_size;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
217 charmap += 1000;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
218 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
219 return req_size;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
220 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
221 return 0;
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
222 }
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
223
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
224 AVCodec a64multi_encoder = {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
225 .name = "a64multi",
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
226 .type = CODEC_TYPE_VIDEO,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
227 .id = CODEC_ID_A64_MULTI,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
228 .priv_data_size = sizeof(A64Context),
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
229 .init = a64multi_init_encoder,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
230 .encode = a64multi_encode_frame,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
231 .close = a64multi_close_encoder,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
232 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
233 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
234 .capabilities = CODEC_CAP_DELAY,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
235 };
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
236
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
237 AVCodec a64multi5_encoder = {
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
238 .name = "a64multi5",
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
239 .type = CODEC_TYPE_VIDEO,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
240 .id = CODEC_ID_A64_MULTI5,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
241 .priv_data_size = sizeof(A64Context),
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
242 .init = a64multi_init_encoder,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
243 .encode = a64multi_encode_frame,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
244 .close = a64multi_close_encoder,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
245 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
246 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
247 .capabilities = CODEC_CAP_DELAY,
f25a00f68cfa Initial version of the a64 (multicolor charset) codec
bindhammer
parents:
diff changeset
248 };