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
46
50
51 // Workaround for GCC bug 102513
52 #if AV_GCC_VERSION_AT_LEAST(10, 0) && AV_GCC_VERSION_AT_MOST(12, 0) \
53 && !defined(__clang__) && !defined(__INTEL_COMPILER)
54 #pragma GCC optimize ("no-ipa-cp-clone")
55 #endif
56
58 /* FIXME: Needed for motion estimation, should not be used for anything
59 * else, the idea is to make the motion estimation eventually independent
60 * of MpegEncContext, so this will be removed then. */
67
68 /* Some compression statistics */
71
72 /* why ooh why this sick breadth first order,
73 * everything is slower and more complex */
75
78
79 /* Y plane block dimensions */
82
83 /* U & V plane (C planes) block dimensions */
86
88
93
95
97
99
102
104 {
106
107 /* frame code */
109
110 /* temporal reference (sure hope this is a "don't care") */
112
113 /* frame type */
115
117 /* no checksum since frame code is 0x20 */
118 /* no embedded string either */
119 /* output 5 unknown bits (2 + 2 + 1) */
120 put_bits(pb, 5, 2);
/* 2 needed by quicktime decoder */
121
124 s->frame_width,
s->frame_height);
126
130 }
131 }
132
133 /* no checksum or extra data (next 2 bits get 0) */
135 }
136
137 #define QUALITY_THRESHOLD 100
138 #define THRESHOLD_MULTIPLIER 0.6
139
142 int threshold, int lambda, int intra)
143 {
144 int count, y, x,
i, j,
split, best_mean, best_score, best_count;
145 int best_vector[6];
147 int w = 2 << (
level + 2 >> 1);
148 int h = 2 << (
level + 1 >> 1);
150 int16_t (*
block)[256] =
s->encoded_block_levels[
level];
151 const int8_t *codebook_sum, *
codebook;
152 const uint16_t(*mean_vlc)[2];
153 const uint8_t(*multistage_vlc)[2];
154
155 best_score = 0;
156 // FIXME: Optimize, this does not need to be done multiple times.
157 if (intra) {
158 // level is 5 when encode_block is called from svq1_encode_plane
159 // and always < 4 when called recursively from this function.
164 for (y = 0; y <
h; y++) {
165 for (x = 0; x <
w; x++) {
168 best_score += v * v;
170 }
171 }
172 } else {
173 // level is 5 or < 4, see above for details.
178 for (y = 0; y <
h; y++) {
179 for (x = 0; x <
w; x++) {
182 best_score += v * v;
184 }
185 }
186 }
187
188 best_count = 0;
191
193 for (count = 1; count < 7; count++) {
194 int best_vector_score = INT_MAX;
195 int best_vector_sum = -999, best_vector_mean = -999;
196 const int stage = count - 1;
197 const int8_t *vector;
198
199 for (
i = 0;
i < 16;
i++) {
200 int sum = codebook_sum[stage * 16 +
i];
202
204 sqr =
s->svq1encdsp.ssd_int8_vs_int16(vector,
block[stage],
size);
207 if (score < best_vector_score) {
211 best_vector_score = score;
212 best_vector[stage] =
i;
213 best_vector_sum = sum;
214 best_vector_mean =
mean;
215 }
216 }
219 for (j = 0; j <
size; j++)
220 block[stage + 1][j] =
block[stage][j] - vector[j];
222 best_vector_score += lambda *
223 (+1 + 4 * count +
224 multistage_vlc[1 + count][1]
225 + mean_vlc[best_vector_mean][1]);
226
227 if (best_vector_score < best_score) {
228 best_score = best_vector_score;
229 best_count = count;
230 best_mean = best_vector_mean;
231 }
232 }
233 }
234
235 if (best_mean == -128)
236 best_mean = -127;
237 else if (best_mean == 128)
238 best_mean = 127;
239
241 if (best_score > threshold &&
level) {
242 int score = 0;
245
247 backup[
i] =
s->reorder_pb[
i];
249 threshold >> 1, lambda, intra);
252 score += lambda;
253
254 if (score < best_score) {
255 best_score = score;
257 } else {
259 s->reorder_pb[
i] = backup[
i];
260 }
261 }
264
266 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
267 av_assert1(best_mean >= -256 && best_mean < 256);
268 av_assert1(best_count >= 0 && best_count < 7);
270
271 /* output the encoding */
273 multistage_vlc[1 + best_count][1],
274 multistage_vlc[1 + best_count][0]);
276 mean_vlc[best_mean][0]);
277
278 for (
i = 0;
i < best_count;
i++) {
281 }
282
283 for (y = 0; y <
h; y++)
284 for (x = 0; x <
w; x++)
286 block[best_count][x +
w * y] +
287 best_mean;
288 }
289
290 return best_score;
291 }
292
294 s->block_index[0]=
s->b8_stride*(
s->mb_y*2 ) +
s->mb_x*2;
295 s->block_index[1]=
s->b8_stride*(
s->mb_y*2 ) + 1 +
s->mb_x*2;
296 s->block_index[2]=
s->b8_stride*(
s->mb_y*2 + 1) +
s->mb_x*2;
297 s->block_index[3]=
s->b8_stride*(
s->mb_y*2 + 1) + 1 +
s->mb_x*2;
298 s->block_index[4]=
s->mb_stride*(
s->mb_y + 1) +
s->b8_stride*
s->mb_height*2 +
s->mb_x;
299 s->block_index[5]=
s->mb_stride*(
s->mb_y +
s->mb_height + 2) +
s->b8_stride*
s->mb_height*2 +
s->mb_x;
300 }
301
304 const unsigned char *src_plane,
305 unsigned char *ref_plane,
306 unsigned char *decoded_plane,
308 {
309 int x, y;
311 int block_width, block_height;
313 int threshold[6];
315 const int lambda = (
s->quality *
s->quality) >>
317
318 /* figure out the acceptable level thresholds in advance */
322
323 block_width = (
width + 15) / 16;
324 block_height = (
height + 15) / 16;
325
327 s->m.avctx =
s->avctx;
328 s->m.current_picture_ptr = &
s->m.current_picture;
329 s->m.last_picture_ptr = &
s->m.last_picture;
330 s->m.last_picture.f->data[0] = ref_plane;
332 s->m.last_picture.f->linesize[0] =
333 s->m.new_picture->linesize[0] =
334 s->m.current_picture.f->linesize[0] =
stride;
337 s->m.mb_width = block_width;
338 s->m.mb_height = block_height;
339 s->m.mb_stride =
s->m.mb_width + 1;
340 s->m.b8_stride = 2 *
s->m.mb_width + 1;
342 s->m.pict_type =
s->pict_type;
343 s->m.motion_est =
s->motion_est;
344 s->m.me.scene_change_score = 0;
345 // s->m.out_format = FMT_H263;
346 // s->m.unrestricted_mv = 1;
347 s->m.lambda =
s->quality;
348 s->m.qscale =
s->m.lambda * 139 +
351 s->m.lambda2 =
s->m.lambda *
s->m.lambda +
354
355 if (!
s->motion_val8[plane]) {
357 block_height * 2 + 2) *
358 2 * sizeof(int16_t));
360 (block_height + 2) + 1) *
361 2 * sizeof(int16_t));
362 if (!
s->motion_val8[plane] || !
s->motion_val16[plane])
364 }
365
366 s->m.mb_type =
s->mb_type;
367
368 // dummies, to avoid segfaults
369 s->m.mb_mean = (uint8_t *)
s->dummy;
370 s->m.mb_var = (uint16_t *)
s->dummy;
371 s->m.mc_mb_var = (uint16_t *)
s->dummy;
372 s->m.current_picture.mb_type =
s->dummy;
373
374 s->m.current_picture.motion_val[0] =
s->motion_val8[plane] + 2;
375 s->m.p_mv_table =
s->motion_val16[plane] +
377 s->m.mecc =
s->mecc;
// move
379
380 s->m.me.dia_size =
s->avctx->dia_size;
381 s->m.first_slice_line = 1;
382 for (y = 0; y < block_height; y++) {
383 s->m.new_picture->data[0] =
src - y * 16 *
stride;
// ugly
385
386 for (
i = 0;
i < 16 &&
i + 16 * y <
height;
i++) {
387 memcpy(&
src[
i *
stride], &src_plane[(
i + 16 * y) * src_stride],
389 for (x =
width; x < 16 * block_width; x++)
391 }
392 for (;
i < 16 &&
i + 16 * y < 16 * block_height;
i++)
394 16 * block_width);
395
396 for (x = 0; x < block_width; x++) {
399
401 }
402 s->m.first_slice_line = 0;
403 }
404
408 }
409
410 s->m.first_slice_line = 1;
411 for (y = 0; y < block_height; y++) {
412 for (
i = 0;
i < 16 &&
i + 16 * y <
height;
i++) {
413 memcpy(&
src[
i *
stride], &src_plane[(
i + 16 * y) * src_stride],
415 for (x =
width; x < 16 * block_width; x++)
417 }
418 for (;
i < 16 &&
i + 16 * y < 16 * block_height;
i++)
420
422 for (x = 0; x < block_width; x++) {
423 uint8_t reorder_buffer[2][6][7 * 32];
424 int count[2][6];
426 uint8_t *decoded = decoded_plane +
offset;
428 int score[4] = { 0, 0, 0, 0 }, best;
429 uint8_t *
temp =
s->scratchbuf;
430
433 return -1;
434 }
435
438
440 (
s->m.mb_type[x + y *
s->m.mb_stride] &
442 for (
i = 0;
i < 6;
i++)
444 7 * 32);
448 }
450 5, 64, lambda, 1);
451 for (
i = 0;
i < 6;
i++) {
454 }
455 } else
456 score[0] = INT_MAX;
457
458 best = 0;
459
461 int mx, my, pred_x, pred_y, dxy;
462 int16_t *motion_ptr;
463
465 if (
s->m.mb_type[x + y *
s->m.mb_stride] &
467 for (
i = 0;
i < 6;
i++)
469 7 * 32);
470
472
473 s->m.pb =
s->reorder_pb[5];
474 mx = motion_ptr[0];
475 my = motion_ptr[1];
482 s->reorder_pb[5] =
s->m.pb;
484
485 dxy = (mx & 1) + 2 * (my & 1);
486
491
493 decoded,
stride, 5, 64, lambda, 0);
494 best = score[1] <= score[0];
495
499 if (score[2] < score[best] && mx == 0 && my == 0) {
500 best = 2;
501 s->hdsp.put_pixels_tab[0][0](decoded,
ref,
stride, 16);
503 }
504 }
505
506 if (best == 1) {
507 for (
i = 0;
i < 6;
i++) {
510 }
511 } else {
512 motion_ptr[0] =
513 motion_ptr[1] =
514 motion_ptr[2] =
515 motion_ptr[3] =
516 motion_ptr[0 + 2 *
s->m.b8_stride] =
517 motion_ptr[1 + 2 *
s->m.b8_stride] =
518 motion_ptr[2 + 2 *
s->m.b8_stride] =
519 motion_ptr[3 + 2 *
s->m.b8_stride] = 0;
520 }
521 }
522
523 s->rd_total += score[best];
524
525 if (best != 2)
526 for (
i = 5;
i >= 0;
i--)
529 if (best == 0)
530 s->hdsp.put_pixels_tab[0][0](decoded,
temp,
stride, 16);
531 }
532 s->m.first_slice_line = 0;
533 }
534 return 0;
535 }
536
538 {
541
546
549
555
556 for (
i = 0;
i < 3;
i++) {
559 }
560
564
565 return 0;
566 }
567
569 {
570 int size = strlen(ident);
578 return 0;
579 }
580
582 {
585
586 if (avctx->
width >= 4096 || avctx->
height >= 4096) {
589 }
590
594
597 if (!
s->current_picture || !
s->last_picture) {
599 }
600
601 s->frame_width = avctx->
width;
602 s->frame_height = avctx->
height;
603
604 s->y_block_width = (
s->frame_width + 15) / 16;
605 s->y_block_height = (
s->frame_height + 15) / 16;
606
607 s->c_block_width = (
s->frame_width / 4 + 15) / 16;
608 s->c_block_height = (
s->frame_height / 4 + 15) / 16;
609
612
615 }
616
620 2 * 16 * 2 * sizeof(uint8_t));
622 s->y_block_height *
sizeof(int16_t));
624 s->y_block_height *
sizeof(
int32_t));
627
628 if (!
s->m.me.scratchpad || !
s->m.me.map ||
629 !
s->mb_type || !
s->dummy || !
s->m.new_picture)
632
634
636
638 }
639
641 const AVFrame *pict,
int *got_packet)
642 {
646
651
654 return -1;
655 }
656
657 if (!
s->current_picture->data[0]) {
660 }
661 }
662 if (!
s->last_picture->data[0]) {
666 }
667 if (!
s->scratchbuf) {
671 }
672
674
677 else
680
682
685 for (
i = 0;
i < 3;
i++) {
688 s->last_picture->data[
i],
689 s->current_picture->data[
i],
690 s->frame_width / (
i ? 4 : 1),
691 s->frame_height / (
i ? 4 : 1),
693 s->current_picture->linesize[
i]);
696 int j;
697 for (j = 0; j <
i; j++) {
700 }
702 return -1;
703 }
704 }
705
706 // align_put_bits(&pb);
709
711
715 *got_packet = 1;
716
717 return 0;
718 }
719
720 #define OFFSET(x) offsetof(struct SVQ1EncContext, x)
721 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
727
729 };
730
736 };
737
740 CODEC_LONG_NAME(
"Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
752 };