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
47
51
52 // Workaround for GCC bug 102513
53 #if AV_GCC_VERSION_AT_LEAST(10, 0) && AV_GCC_VERSION_AT_MOST(12, 0) \
54 && !defined(__clang__) && !defined(__INTEL_COMPILER)
55 #pragma GCC optimize ("no-ipa-cp-clone")
56 #endif
57
59 /* FIXME: Needed for motion estimation, should not be used for anything
60 * else, the idea is to make the motion estimation eventually independent
61 * of MpegEncContext, so this will be removed then. */
68
69 /* Some compression statistics */
72
73 /* why ooh why this sick breadth first order,
74 * everything is slower and more complex */
76
79
80 /* Y plane block dimensions */
83
84 /* U & V plane (C planes) block dimensions */
87
89
94
96
98
100
103
105 {
107
108 /* frame code */
110
111 /* temporal reference (sure hope this is a "don't care") */
113
114 /* frame type */
116
118 /* no checksum since frame code is 0x20 */
119 /* no embedded string either */
120 /* output 5 unknown bits (2 + 2 + 1) */
121 put_bits(pb, 5, 2);
/* 2 needed by quicktime decoder */
122
125 s->frame_width,
s->frame_height);
127
131 }
132 }
133
134 /* no checksum or extra data (next 2 bits get 0) */
136 }
137
138 #define QUALITY_THRESHOLD 100
139 #define THRESHOLD_MULTIPLIER 0.6
140
143 int threshold, int lambda, int intra)
144 {
145 int count, y, x,
i, j,
split, best_mean, best_score, best_count;
146 int best_vector[6];
148 int w = 2 << (
level + 2 >> 1);
149 int h = 2 << (
level + 1 >> 1);
151 int16_t (*
block)[256] =
s->encoded_block_levels[
level];
152 const int8_t *codebook_sum, *
codebook;
153 const uint16_t(*mean_vlc)[2];
154 const uint8_t(*multistage_vlc)[2];
155
156 best_score = 0;
157 // FIXME: Optimize, this does not need to be done multiple times.
158 if (intra) {
159 // level is 5 when encode_block is called from svq1_encode_plane
160 // and always < 4 when called recursively from this function.
165 for (y = 0; y <
h; y++) {
166 for (x = 0; x <
w; x++) {
169 best_score += v * v;
171 }
172 }
173 } else {
174 // level is 5 or < 4, see above for details.
179 for (y = 0; y <
h; y++) {
180 for (x = 0; x <
w; x++) {
183 best_score += v * v;
185 }
186 }
187 }
188
189 best_count = 0;
192
194 for (count = 1; count < 7; count++) {
195 int best_vector_score = INT_MAX;
196 int best_vector_sum = -999, best_vector_mean = -999;
197 const int stage = count - 1;
198 const int8_t *vector;
199
200 for (
i = 0;
i < 16;
i++) {
201 int sum = codebook_sum[stage * 16 +
i];
203
205 sqr =
s->svq1encdsp.ssd_int8_vs_int16(vector,
block[stage],
size);
208 if (score < best_vector_score) {
212 best_vector_score = score;
213 best_vector[stage] =
i;
214 best_vector_sum = sum;
215 best_vector_mean =
mean;
216 }
217 }
220 for (j = 0; j <
size; j++)
221 block[stage + 1][j] =
block[stage][j] - vector[j];
223 best_vector_score += lambda *
224 (+1 + 4 * count +
225 multistage_vlc[1 + count][1]
226 + mean_vlc[best_vector_mean][1]);
227
228 if (best_vector_score < best_score) {
229 best_score = best_vector_score;
230 best_count = count;
231 best_mean = best_vector_mean;
232 }
233 }
234 }
235
236 if (best_mean == -128)
237 best_mean = -127;
238 else if (best_mean == 128)
239 best_mean = 127;
240
242 if (best_score > threshold &&
level) {
243 int score = 0;
246
248 backup[
i] =
s->reorder_pb[
i];
250 threshold >> 1, lambda, intra);
253 score += lambda;
254
255 if (score < best_score) {
256 best_score = score;
258 } else {
260 s->reorder_pb[
i] = backup[
i];
261 }
262 }
265
267 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
268 av_assert1(best_mean >= -256 && best_mean < 256);
269 av_assert1(best_count >= 0 && best_count < 7);
271
272 /* output the encoding */
274 multistage_vlc[1 + best_count][1],
275 multistage_vlc[1 + best_count][0]);
277 mean_vlc[best_mean][0]);
278
279 for (
i = 0;
i < best_count;
i++) {
282 }
283
284 for (y = 0; y <
h; y++)
285 for (x = 0; x <
w; x++)
287 block[best_count][x +
w * y] +
288 best_mean;
289 }
290
291 return best_score;
292 }
293
295 s->block_index[0]=
s->b8_stride*(
s->mb_y*2 ) +
s->mb_x*2;
296 s->block_index[1]=
s->b8_stride*(
s->mb_y*2 ) + 1 +
s->mb_x*2;
297 s->block_index[2]=
s->b8_stride*(
s->mb_y*2 + 1) +
s->mb_x*2;
298 s->block_index[3]=
s->b8_stride*(
s->mb_y*2 + 1) + 1 +
s->mb_x*2;
299 s->block_index[4]=
s->mb_stride*(
s->mb_y + 1) +
s->b8_stride*
s->mb_height*2 +
s->mb_x;
300 s->block_index[5]=
s->mb_stride*(
s->mb_y +
s->mb_height + 2) +
s->b8_stride*
s->mb_height*2 +
s->mb_x;
301 }
302
305 const unsigned char *src_plane,
306 unsigned char *ref_plane,
307 unsigned char *decoded_plane,
309 {
310 int x, y;
312 int block_width, block_height;
314 int threshold[6];
316 const int lambda = (
s->quality *
s->quality) >>
318
319 /* figure out the acceptable level thresholds in advance */
323
324 block_width = (
width + 15) / 16;
325 block_height = (
height + 15) / 16;
326
328 s->m.avctx =
s->avctx;
329 s->m.last_pic.data[0] = ref_plane;
331 s->m.last_pic.linesize[0] =
332 s->m.new_pic->linesize[0] =
333 s->m.cur_pic.linesize[0] =
stride;
336 s->m.mb_width = block_width;
337 s->m.mb_height = block_height;
338 s->m.mb_stride =
s->m.mb_width + 1;
339 s->m.b8_stride = 2 *
s->m.mb_width + 1;
341 s->m.pict_type =
s->pict_type;
342 s->m.motion_est =
s->motion_est;
343 s->m.me.scene_change_score = 0;
344 // s->m.out_format = FMT_H263;
345 // s->m.unrestricted_mv = 1;
346 s->m.lambda =
s->quality;
347 s->m.qscale =
s->m.lambda * 139 +
350 s->m.lambda2 =
s->m.lambda *
s->m.lambda +
353
354 if (!
s->motion_val8[plane]) {
356 block_height * 2 + 2) *
357 2 * sizeof(int16_t));
359 (block_height + 2) + 1) *
360 2 * sizeof(int16_t));
361 if (!
s->motion_val8[plane] || !
s->motion_val16[plane])
363 }
364
365 s->m.mb_type =
s->mb_type;
366
367 // dummies, to avoid segfaults
368 s->m.mb_mean = (uint8_t *)
s->dummy;
369 s->m.mb_var = (uint16_t *)
s->dummy;
370 s->m.mc_mb_var = (uint16_t *)
s->dummy;
371 s->m.cur_pic.mb_type =
s->dummy;
372
373 s->m.cur_pic.motion_val[0] =
s->motion_val8[plane] + 2;
374 s->m.p_mv_table =
s->motion_val16[plane] +
377
378 s->m.me.dia_size =
s->avctx->dia_size;
379 s->m.first_slice_line = 1;
380 for (y = 0; y < block_height; y++) {
381 s->m.new_pic->data[0] =
src - y * 16 *
stride;
// ugly
383
384 for (
i = 0;
i < 16 &&
i + 16 * y <
height;
i++) {
385 memcpy(&
src[
i *
stride], &src_plane[(
i + 16 * y) * src_stride],
387 for (x =
width; x < 16 * block_width; x++)
389 }
390 for (;
i < 16 &&
i + 16 * y < 16 * block_height;
i++)
392 16 * block_width);
393
394 for (x = 0; x < block_width; x++) {
397
399 }
400 s->m.first_slice_line = 0;
401 }
402
406 }
407
408 s->m.first_slice_line = 1;
409 for (y = 0; y < block_height; y++) {
410 for (
i = 0;
i < 16 &&
i + 16 * y <
height;
i++) {
411 memcpy(&
src[
i *
stride], &src_plane[(
i + 16 * y) * src_stride],
413 for (x =
width; x < 16 * block_width; x++)
415 }
416 for (;
i < 16 &&
i + 16 * y < 16 * block_height;
i++)
418
420 for (x = 0; x < block_width; x++) {
421 uint8_t reorder_buffer[2][6][7 * 32];
422 int count[2][6];
424 uint8_t *decoded = decoded_plane +
offset;
426 int score[4] = { 0, 0, 0, 0 }, best;
427 uint8_t *
temp =
s->scratchbuf;
428
431 return -1;
432 }
433
436
438 (
s->m.mb_type[x + y *
s->m.mb_stride] &
440 for (
i = 0;
i < 6;
i++)
442 7 * 32);
446 }
448 5, 64, lambda, 1);
449 for (
i = 0;
i < 6;
i++) {
452 }
453 } else
454 score[0] = INT_MAX;
455
456 best = 0;
457
459 int mx,
my, pred_x, pred_y, dxy;
460 int16_t *motion_ptr;
461
463 if (
s->m.mb_type[x + y *
s->m.mb_stride] &
465 for (
i = 0;
i < 6;
i++)
467 7 * 32);
468
470
480
481 dxy = (
mx & 1) + 2 * (
my & 1);
482
487
489 decoded,
stride, 5, 64, lambda, 0);
490 best = score[1] <= score[0];
491
495 if (score[2] < score[best] &&
mx == 0 &&
my == 0) {
496 best = 2;
497 s->hdsp.put_pixels_tab[0][0](decoded,
ref,
stride, 16);
499 }
500 }
501
502 if (best == 1) {
503 for (
i = 0;
i < 6;
i++) {
506 }
507 } else {
508 motion_ptr[0] =
509 motion_ptr[1] =
510 motion_ptr[2] =
511 motion_ptr[3] =
512 motion_ptr[0 + 2 *
s->m.b8_stride] =
513 motion_ptr[1 + 2 *
s->m.b8_stride] =
514 motion_ptr[2 + 2 *
s->m.b8_stride] =
515 motion_ptr[3 + 2 *
s->m.b8_stride] = 0;
516 }
517 }
518
519 s->rd_total += score[best];
520
521 if (best != 2)
522 for (
i = 5;
i >= 0;
i--)
525 if (best == 0)
526 s->hdsp.put_pixels_tab[0][0](decoded,
temp,
stride, 16);
527 }
528 s->m.first_slice_line = 0;
529 }
530 return 0;
531 }
532
534 {
537
542
548
551
552 for (
i = 0;
i < 3;
i++) {
555 }
556
560
561 return 0;
562 }
563
565 {
566 int size = strlen(ident);
574 return 0;
575 }
576
578 {
581
582 if (avctx->
width >= 4096 || avctx->
height >= 4096) {
585 }
586
593
596 if (!
s->current_picture || !
s->last_picture) {
598 }
599
600 s->frame_width = avctx->
width;
601 s->frame_height = avctx->
height;
602
603 s->y_block_width = (
s->frame_width + 15) / 16;
604 s->y_block_height = (
s->frame_height + 15) / 16;
605
606 s->c_block_width = (
s->frame_width / 4 + 15) / 16;
607 s->c_block_height = (
s->frame_height / 4 + 15) / 16;
608
611
614 }
615
619 2 * 16 * 2 * sizeof(uint8_t));
621 s->y_block_height *
sizeof(int16_t));
623 s->y_block_height *
sizeof(
int32_t));
626
627 if (!
s->m.me.scratchpad || !
s->m.me.map ||
628 !
s->mb_type || !
s->dummy || !
s->m.new_pic)
631
633
635
637 }
638
640 const AVFrame *pict,
int *got_packet)
641 {
645
650
653 return -1;
654 }
655
656 if (!
s->current_picture->data[0]) {
659 }
660 }
661 if (!
s->last_picture->data[0]) {
665 }
666 if (!
s->scratchbuf) {
670 }
671
673
676 else
679
681
684 for (
i = 0;
i < 3;
i++) {
687 s->last_picture->data[
i],
688 s->current_picture->data[
i],
689 s->frame_width / (
i ? 4 : 1),
690 s->frame_height / (
i ? 4 : 1),
692 s->current_picture->linesize[
i]);
695 int j;
696 for (j = 0; j <
i; j++) {
699 }
701 return -1;
702 }
703 }
704
705 // align_put_bits(&pb);
708
710
714 *got_packet = 1;
715
716 return 0;
717 }
718
719 #define OFFSET(x) offsetof(struct SVQ1EncContext, x)
720 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
726
728 };
729
735 };
736
739 CODEC_LONG_NAME(
"Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
752 };