1 /*
2 * Microsoft Video-1 Encoder
3 * Copyright (c) 2009 Konstantin Shishkov
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 * Microsoft Video-1 encoder
25 */
26
33 /**
34 * Encoder context
35 */
41
52
58 };
59
60 #define SKIP_PREFIX 0x8400
61 #define SKIPS_MAX 0x0FFF
62 #define MKRGB555(in, off) ((in[off] << 10) | (in[off + 1] << 5) | (in[off + 2]))
63
64 static const int remap[16] = { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15 };
65
67 const AVFrame *pict,
int *got_packet)
68 {
74 int keyframe = 0;
75 int no_skips = 1;
76 int i, j, k, x,
y,
ret;
77 int skips = 0;
78
82
83 *p = *pict;
89 keyframe = 1;
90
92
93 for(y = 0; y < avctx->
height; y += 4){
94 for(x = 0; x < avctx->
width; x += 4){
96 int bestscore = INT_MAX;
98 int score;
99
100 for(j = 0; j < 4; j++){
101 for(i = 0; i < 4; i++){
103 for(k = 0; k < 3; k++){
104 c->
block[(i + j*4)*3 + k] =
105 c->
block2[
remap[i + j*4]*3 + k] = (val >> (10-k*5)) & 0x1F;
106 }
107 }
108 }
109 if(!keyframe){
110 bestscore = 0;
111 for(j = 0; j < 4; j++){
112 for(i = 0; i < 4*3; i++){
113 int t = prevptr[x*3 + i - j*3*avctx->
width] - c->
block[i + j*4*3];
115 }
116 }
118 }
119 // try to find optimal value to fill whole 4x4 block
120 score = 0;
123 if(c->
avg[0] == 1)
// red component = 1 will be written as skip code
125 for(j = 0; j < 4; j++){
126 for(i = 0; i < 4; i++){
127 for(k = 0; k < 3; k++){
128 int t = c->
avg[k] - c->
block[(i+j*4)*3+k];
130 }
131 }
132 }
134 score += 2;
135 if(score < bestscore){
136 bestscore = score;
138 }
139 // search for optimal filling of 2-color block
140 score = 0;
143 // last output value should be always 1, swap codebooks if needed
145 for(i = 0; i < 3; i++)
147 for(i = 0; i < 16; i++)
149 }
150 for(j = 0; j < 4; j++){
151 for(i = 0; i < 4; i++){
152 for(k = 0; k < 3; k++){
155 }
156 }
157 }
159 score += 6;
160 if(score < bestscore){
161 bestscore = score;
163 }
164 // search for optimal filling of 2-color 2x2 subblocks
165 score = 0;
166 for(i = 0; i < 4; i++){
169 }
170 // last value should be always 1, swap codebooks if needed
172 for(i = 0; i < 3; i++)
174 for(i = 12; i < 16; i++)
176 }
177 for(j = 0; j < 4; j++){
178 for(i = 0; i < 4; i++){
179 for(k = 0; k < 3; k++){
181 score += t*t;
182 }
183 }
184 }
186 score += 18;
187 if(score < bestscore){
188 bestscore = score;
190 }
191
193 skips++;
194 no_skips = 0;
195 }
198 skips = 0;
199 }
200
201 switch(bestmode){
203 bytestream_put_le16(&dst,
MKRGB555(c->
avg,0) | 0x8000);
204 for(j = 0; j < 4; j++)
205 for(i = 0; i < 4; i++)
206 for(k = 0; k < 3; k++)
207 prevptr[x*3 + i*3 + k - j*3*avctx->
width] = c->
avg[k];
208 break;
210 for(j = 0; j < 4; j++){
211 for(i = 0; i < 4; i++){
212 flags |= (c->
output[i + j*4]^1) << (i + j*4);
213 for(k = 0; k < 3; k++)
215 }
216 }
217 bytestream_put_le16(&dst, flags);
220 break;
222 for(j = 0; j < 4; j++){
223 for(i = 0; i < 4; i++){
225 for(k = 0; k < 3; k++)
227 }
228 }
229 bytestream_put_le16(&dst, flags);
231 for(i = 3; i < 24; i += 3)
233 break;
234 }
235 }
237 prevptr -= avctx->
width * 3 * 4;
238 }
239 if(skips)
241 //EOF
242 bytestream_put_byte(&dst, 0);
243 bytestream_put_byte(&dst, 0);
244
245 if(no_skips)
246 keyframe = 1;
247 if(keyframe)
249 else
255 *got_packet = 1;
256
257 return 0;
258 }
259
260
261 /**
262 * init encoder
263 */
265 {
267
270 return -1;
271 }
274 return -1;
275 }
276
280
283
284 return 0;
285 }
286
287
288
289 /**
290 * Uninit encoder
291 */
293 {
295
297
298 return 0;
299 }
300
311 };