1 /*
2 * SVQ1 Encoder
3 * Copyright (C) 2004 Mike Melanson <melanson@pcisys.net>
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 * Sorenson Vector Quantizer #1 (SVQ1) video codec.
25 * For more information of the SVQ1 algorithm, visit:
26 * http://www.pcisys.net/~melanson/codecs/
27 */
28
38
39
41 /* FIXME: Needed for motion estimation, should not be used for anything
42 * else, the idea is to make the motion estimation eventually independent
43 * of MpegEncContext, so this will be removed then. */
53
54 /* why ooh why this sick breadth first order,
55 * everything is slower and more complex */
57
60
61 /* Y plane block dimensions */
64
65 /* U & V plane (C planes) block dimensions */
68
73
75
78
80 {
81 int i;
82
83 /* frame code */
85
86 /* temporal reference (sure hope this is a "don't care") */
88
89 /* frame type */
91
93 /* no checksum since frame code is 0x20 */
94 /* no embedded string either */
95 /* output 5 unknown bits (2 + 2 + 1) */
96 put_bits(&s->
pb, 5, 2);
/* 2 needed by quicktime decoder */
97
102
103 if (i == 7) {
106 }
107 }
108
109 /* no checksum or extra data (next 2 bits get 0) */
111 }
112
113 #define QUALITY_THRESHOLD 100
114 #define THRESHOLD_MULTIPLIER 0.6
115
118 int threshold, int lambda, int intra)
119 {
120 int count,
y, x, i, j,
split, best_mean, best_score, best_count;
121 int best_vector[6];
122 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
123 int w = 2 << (level + 2 >> 1);
124 int h = 2 << (level + 1 >> 1);
126 int16_t
block[7][256];
127 const int8_t *codebook_sum, *codebook;
128 const uint16_t(*mean_vlc)[2];
129 const uint8_t(*multistage_vlc)[2];
130
131 best_score = 0;
132 // FIXME: Optimize, this does not need to be done multiple times.
133 if (intra) {
138 for (y = 0; y < h; y++) {
139 for (x = 0; x < w; x++) {
141 block[0][x + w *
y] =
v;
144 }
145 }
146 } else {
151 for (y = 0; y < h; y++) {
152 for (x = 0; x < w; x++) {
154 block[0][x + w *
y] =
v;
157 }
158 }
159 }
160
161 best_count = 0;
162 best_score -= (int)((unsigned)block_sum[0] * block_sum[0] >> (level + 3));
163 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
164
165 if (level < 4) {
166 for (count = 1; count < 7; count++) {
167 int best_vector_score = INT_MAX;
168 int best_vector_sum = -999, best_vector_mean = -999;
169 const int stage = count - 1;
170 const int8_t *vector;
171
172 for (i = 0; i < 16; i++) {
173 int sum = codebook_sum[stage * 16 + i];
174 int sqr,
diff, score;
175
176 vector = codebook + stage * size * 16 + i *
size;
178 diff = block_sum[stage] - sum;
179 score = sqr - (diff * (int64_t)diff >> (level + 3)); // FIXME: 64bit slooow
180 if (score < best_vector_score) {
181 int mean = diff + (size >> 1) >> (level + 3);
183 mean = av_clip(mean, intra ? 0 : -256, 255);
184 best_vector_score = score;
185 best_vector[stage] = i;
186 best_vector_sum = sum;
187 best_vector_mean = mean;
188 }
189 }
191 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
192 for (j = 0; j <
size; j++)
193 block[stage + 1][j] = block[stage][j] - vector[j];
194 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
195 best_vector_score += lambda *
196 (+1 + 4 * count +
197 multistage_vlc[1 +
count][1]
198 + mean_vlc[best_vector_mean][1]);
199
200 if (best_vector_score < best_score) {
201 best_score = best_vector_score;
203 best_mean = best_vector_mean;
204 }
205 }
206 }
207
208 split = 0;
209 if (best_score > threshold && level) {
210 int score = 0;
211 int offset = level & 1 ? stride * h / 2 : w / 2;
213
214 for (i = level - 1; i >= 0; i--)
216 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
217 threshold >> 1, lambda, intra);
218 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
219 stride, level - 1, threshold >> 1, lambda, intra);
220 score += lambda;
221
222 if (score < best_score) {
223 best_score = score;
224 split = 1;
225 } else {
226 for (i = level - 1; i >= 0; i--)
228 }
229 }
230 if (level > 0)
232
233 if (!split) {
234 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
235 av_assert1(best_mean >= -256 && best_mean < 256);
236 av_assert1(best_count >= 0 && best_count < 7);
238
239 /* output the encoding */
241 multistage_vlc[1 + best_count][1],
242 multistage_vlc[1 + best_count][0]);
244 mean_vlc[best_mean][0]);
245
246 for (i = 0; i < best_count; i++) {
247 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
249 }
250
251 for (y = 0; y < h; y++)
252 for (x = 0; x < w; x++)
253 decoded[x + y * stride] = src[x + y * stride] -
254 block[best_count][x + w * y] +
255 best_mean;
256 }
257
258 return best_score;
259 }
260
262 unsigned char *src_plane,
263 unsigned char *ref_plane,
264 unsigned char *decoded_plane,
266 {
268 int i;
269 int block_width, block_height;
271 int threshold[6];
275
276 /* figure out the acceptable level thresholds in advance */
278 for (level = 4; level >= 0; level--)
280
281 block_width = (width + 15) / 16;
282 block_height = (height + 15) / 16;
283
304 // s->m.out_format = FMT_H263;
305 // s->m.unrestricted_mv = 1;
313
316 block_height * 2 + 2) *
317 2 * sizeof(int16_t));
319 (block_height + 2) + 1) *
320 2 * sizeof(int16_t));
321 }
322
324
325 // dummies, to avoid segfaults
330
336
339 for (y = 0; y < block_height; y++) {
342
343 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
344 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
345 width);
346 for (x = width; x < 16 * block_width; x++)
347 src[i * stride + x] = src[i * stride + x - 1];
348 }
349 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
350 memcpy(&src[i * stride], &src[(i - 1) * stride],
351 16 * block_width);
352
353 for (x = 0; x < block_width; x++) {
357
359 }
361 }
362
366 }
367
369 for (y = 0; y < block_height; y++) {
370 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
371 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
372 width);
373 for (x = width; x < 16 * block_width; x++)
374 src[i * stride + x] = src[i * stride + x - 1];
375 }
376 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
377 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
378
380 for (x = 0; x < block_width; x++) {
381 uint8_t reorder_buffer[3][6][7 * 32];
383 int offset = y * 16 * stride + x * 16;
386 int score[4] = { 0, 0, 0, 0 }, best;
388
392 return -1;
393 }
394
398
402 for (i = 0; i < 6; i++)
404 7 * 32);
408 score[0] = vlc[1] * lambda;
409 }
410 score[0] +=
encode_block(s, src + 16 * x, NULL, temp, stride,
411 5, 64, lambda, 1);
412 for (i = 0; i < 6; i++) {
415 }
416 } else
417 score[0] = INT_MAX;
418
419 best = 0;
420
423 int mx, my, pred_x, pred_y, dxy;
424 int16_t *motion_ptr;
425
429 for (i = 0; i < 6; i++)
431 7 * 32);
432
434
436 mx = motion_ptr[0];
437 my = motion_ptr[1];
446
447 dxy = (mx & 1) + 2 * (my & 1);
448
450 ref + (mx >> 1) +
451 stride * (my >> 1),
453
455 decoded, stride, 5, 64, lambda, 0);
456 best = score[1] <= score[0];
457
459 score[2] = s->
dsp.
sse[0](NULL, src + 16 * x, ref,
461 score[2] += vlc[1] * lambda;
462 if (score[2] < score[best] && mx == 0 && my == 0) {
463 best = 2;
465 for (i = 0; i < 6; i++)
466 count[2][i] = 0;
468 }
469 }
470
471 if (best == 1) {
472 for (i = 0; i < 6; i++) {
475 }
476 } else {
477 motion_ptr[0] =
478 motion_ptr[1] =
479 motion_ptr[2] =
480 motion_ptr[3] =
485 }
486 }
487
489
490 for (i = 5; i >= 0; i--)
492 count[best][i]);
493 if (best == 0)
495 }
497 }
498 return 0;
499 }
500
502 {
504
508
511
514
517
531
532 return 0;
533 }
534
536 const AVFrame *pict,
int *got_packet)
537 {
542
546
549 return -1;
550 }
551
556 }
558 }
559
563
565
566 *p = *pict;
570
572 for (i = 0; i < 3; i++)
581 return -1;
582
583 // avpriv_align_put_bits(&s->pb);
586
588
592 *got_packet = 1;
593
594 return 0;
595 }
596
598 {
600 int i;
601
605
612
613 for (i = 0; i < 3; i++) {
616 }
617
620
621 return 0;
622 }
623
635 };