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
40
41
43 {
44 int i;
45
46 /* frame code */
48
49 /* temporal reference (sure hope this is a "don't care") */
51
52 /* frame type */
54
56 /* no checksum since frame code is 0x20 */
57 /* no embedded string either */
58 /* output 5 unknown bits (2 + 2 + 1) */
59 put_bits(&s->
pb, 5, 2);
/* 2 needed by quicktime decoder */
60
65
66 if (i == 7) {
69 }
70 }
71
72 /* no checksum or extra data (next 2 bits get 0) */
74 }
75
76 #define QUALITY_THRESHOLD 100
77 #define THRESHOLD_MULTIPLIER 0.6
78
81 {
82 int score = 0, i;
83
84 for (i = 0; i <
size; i++)
85 score += (pix1[i] - pix2[i]) * (pix1[i] - pix2[i]);
86 return score;
87 }
88
91 int threshold, int lambda, int intra)
92 {
93 int count,
y, x, i, j,
split, best_mean, best_score, best_count;
94 int best_vector[6];
95 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
96 int w = 2 << (level + 2 >> 1);
97 int h = 2 << (level + 1 >> 1);
100 const int8_t *codebook_sum, *codebook;
101 const uint16_t(*mean_vlc)[2];
102 const uint8_t(*multistage_vlc)[2];
103
104 best_score = 0;
105 // FIXME: Optimize, this does not need to be done multiple times.
106 if (intra) {
111 for (y = 0; y < h; y++) {
112 for (x = 0; x < w; x++) {
113 int v = src[x + y * stride];
117 }
118 }
119 } else {
124 for (y = 0; y < h; y++) {
125 for (x = 0; x < w; x++) {
126 int v = src[x + y * stride] - ref[x + y * stride];
130 }
131 }
132 }
133
134 best_count = 0;
135 best_score -= (int)((unsigned)block_sum[0] * block_sum[0] >> (level + 3));
136 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
137
138 if (level < 4) {
139 for (count = 1; count < 7; count++) {
140 int best_vector_score = INT_MAX;
141 int best_vector_sum = -999, best_vector_mean = -999;
142 const int stage = count - 1;
143 const int8_t *vector;
144
145 for (i = 0; i < 16; i++) {
146 int sum = codebook_sum[stage * 16 + i];
147 int sqr,
diff, score;
148
149 vector = codebook + stage * size * 16 + i *
size;
151 diff = block_sum[stage] - sum;
152 score = sqr - (diff * (int64_t)diff >> (level + 3)); // FIXME: 64bit slooow
153 if (score < best_vector_score) {
154 int mean = diff + (size >> 1) >> (level + 3);
156 mean = av_clip(mean, intra ? 0 : -256, 255);
157 best_vector_score = score;
158 best_vector[stage] = i;
159 best_vector_sum = sum;
160 best_vector_mean = mean;
161 }
162 }
164 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
165 for (j = 0; j <
size; j++)
166 block[stage + 1][j] =
block[stage][j] - vector[j];
167 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
168 best_vector_score += lambda *
169 (+1 + 4 * count +
170 multistage_vlc[1 +
count][1]
171 + mean_vlc[best_vector_mean][1]);
172
173 if (best_vector_score < best_score) {
174 best_score = best_vector_score;
176 best_mean = best_vector_mean;
177 }
178 }
179 }
180
181 split = 0;
182 if (best_score > threshold && level) {
183 int score = 0;
184 int offset = level & 1 ? stride * h / 2 : w / 2;
186
187 for (i = level - 1; i >= 0; i--)
189 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
190 threshold >> 1, lambda, intra);
191 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
192 stride, level - 1, threshold >> 1, lambda, intra);
193 score += lambda;
194
195 if (score < best_score) {
196 best_score = score;
197 split = 1;
198 } else {
199 for (i = level - 1; i >= 0; i--)
201 }
202 }
203 if (level > 0)
205
206 if (!split) {
207 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
208 av_assert1(best_mean >= -256 && best_mean < 256);
209 av_assert1(best_count >= 0 && best_count < 7);
211
212 /* output the encoding */
214 multistage_vlc[1 + best_count][1],
215 multistage_vlc[1 + best_count][0]);
217 mean_vlc[best_mean][0]);
218
219 for (i = 0; i < best_count; i++) {
220 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
222 }
223
224 for (y = 0; y < h; y++)
225 for (x = 0; x < w; x++)
226 decoded[x + y * stride] = src[x + y * stride] -
227 block[best_count][x + w * y] +
228 best_mean;
229 }
230
231 return best_score;
232 }
233
241 }
242
244 unsigned char *src_plane,
245 unsigned char *ref_plane,
246 unsigned char *decoded_plane,
248 {
251 int i;
252 int block_width, block_height;
254 int threshold[6];
258
259 /* figure out the acceptable level thresholds in advance */
261 for (level = 4; level >= 0; level--)
263
264 block_width = (width + 15) / 16;
265 block_height = (height + 15) / 16;
266
287 // s->m.out_format = FMT_H263;
288 // s->m.unrestricted_mv = 1;
296
299 block_height * 2 + 2) *
300 2 * sizeof(int16_t));
302 (block_height + 2) + 1) *
303 2 * sizeof(int16_t));
306 }
307
309
310 // dummies, to avoid segfaults
315
321
324 for (y = 0; y < block_height; y++) {
327
328 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
329 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
330 width);
331 for (x = width; x < 16 * block_width; x++)
332 src[i * stride + x] = src[i * stride + x - 1];
333 }
334 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
335 memcpy(&src[i * stride], &src[(i - 1) * stride],
336 16 * block_width);
337
338 for (x = 0; x < block_width; x++) {
341
343 }
345 }
346
350 }
351
353 for (y = 0; y < block_height; y++) {
354 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
355 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
356 width);
357 for (x = width; x < 16 * block_width; x++)
358 src[i * stride + x] = src[i * stride + x - 1];
359 }
360 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
361 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
362
364 for (x = 0; x < block_width; x++) {
365 uint8_t reorder_buffer[2][6][7 * 32];
367 int offset = y * 16 * stride + x * 16;
370 int score[4] = { 0, 0, 0, 0 }, best;
372
376 return -1;
377 }
378
381
385 for (i = 0; i < 6; i++)
387 7 * 32);
391 score[0] = vlc[1] * lambda;
392 }
394 5, 64, lambda, 1);
395 for (i = 0; i < 6; i++) {
398 }
399 } else
400 score[0] = INT_MAX;
401
402 best = 0;
403
406 int mx, my, pred_x, pred_y, dxy;
407 int16_t *motion_ptr;
408
412 for (i = 0; i < 6; i++)
414 7 * 32);
415
417
419 mx = motion_ptr[0];
420 my = motion_ptr[1];
429
430 dxy = (mx & 1) + 2 * (my & 1);
431
433 ref + (mx >> 1) +
434 stride * (my >> 1),
435 stride, 16);
436
437 score[1] +=
encode_block(s, src + 16 * x, temp + 16*stride,
438 decoded, stride, 5, 64, lambda, 0);
439 best = score[1] <= score[0];
440
443 stride, 16);
444 score[2] += vlc[1] * lambda;
445 if (score[2] < score[best] && mx == 0 && my == 0) {
446 best = 2;
449 }
450 }
451
452 if (best == 1) {
453 for (i = 0; i < 6; i++) {
456 }
457 } else {
458 motion_ptr[0] =
459 motion_ptr[1] =
460 motion_ptr[2] =
461 motion_ptr[3] =
466 }
467 }
468
470
471 if (best != 2)
472 for (i = 5; i >= 0; i--)
474 count[best][i]);
475 if (best == 0)
477 }
479 }
480 return 0;
481 }
482
484 {
486 int i;
487
491
494
501
502 for (i = 0; i < 3; i++) {
505 }
506
510
511 return 0;
512 }
513
515 {
518
522
529 }
530
533
536
539
542
546 }
547
559
564 }
565
566 if (ARCH_PPC)
568 if (ARCH_X86)
570
572
573 return 0;
574 }
575
577 const AVFrame *pict,
int *got_packet)
578 {
582
586
589 return -1;
590 }
591
595 }
596 }
599 if (ret < 0)
601 }
606 }
607
609
611
616
618 for (i = 0; i < 3; i++)
627 return -1;
628
629 // avpriv_align_put_bits(&s->pb);
632
634
638 *got_packet = 1;
639
640 return 0;
641 }
642
654 };