FFmpeg
Loading...
Searching...
No Matches
svq1enc.c
Go to the documentation of this file.
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
29#include "libavutil/emms.h"
30#include "libavutil/mem.h"
31#include "avcodec.h"
32#include "codec_internal.h"
33#include "encode.h"
34#include "hpeldsp.h"
35#include "me_cmp.h"
36#include "mpegvideo.h"
37#include "h263.h"
38#include "h263enc.h"
39#include "internal.h"
40#include "mpegutils.h"
41#include "put_bits.h"
42#include "svq1.h"
43#include "svq1encdsp.h"
44#include "svq1enc_cb.h"
45#include "version.h"
46
47#include "libavutil/avassert.h"
48#include "libavutil/frame.h"
50
51typedef struct SVQ1EncContext {
52 /* FIXME: Needed for motion estimation, should not be used for anything
53 * else, the idea is to make the motion estimation eventually independent
54 * of MPVEncContext, so this will be removed then. */
60
61 /* Some compression statistics */
64
65 /* why ooh why this sick breadth first order,
66 * everything is slower and more complex */
68
71
72 /* Y plane block dimensions */
75
76 DECLARE_ALIGNED(16, int16_t, encoded_block_levels)[6][7][256];
77
78 uint16_t *mb_type;
79 uint32_t *dummy;
80 int16_t (*motion_val8[3])[2];
81 int16_t (*motion_val16[3])[2];
82
84
85 uint8_t *scratchbuf;
86
89
91{
92 int i;
93
94 /* frame code */
95 put_bits(pb, 22, 0x20);
96
97 /* temporal reference (sure hope this is a "don't care") */
98 put_bits(pb, 8, 0x00);
99
100 /* frame type */
101 put_bits(pb, 2, frame_type - 1);
102
104 /* no checksum since frame code is 0x20 */
105 /* no embedded string either */
106 /* output 5 unknown bits (2 + 2 + 1) */
107 put_bits(pb, 5, 2); /* 2 needed by quicktime decoder */
108
111 s->frame_width, s->frame_height);
112 put_bits(pb, 3, i);
113
114 if (i == 7) {
115 put_bits(pb, 12, s->frame_width);
116 put_bits(pb, 12, s->frame_height);
117 }
118 }
119
120 /* no checksum or extra data (next 2 bits get 0) */
121 put_bits(pb, 2, 0);
122}
123
124#define QUALITY_THRESHOLD 100
125#define THRESHOLD_MULTIPLIER 0.6
126
127static int encode_block(SVQ1EncContext *s, uint8_t *src, uint8_t *ref,
128 uint8_t *decoded, int stride, unsigned level,
129 int threshold, int lambda, int intra)
130{
131 av_assume(level <= 5U); // Workaround for GCC bug 102513
132
133 int count, y, x, i, j, split, best_mean, best_score, best_count;
134 int best_vector[6];
135 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
136 int w = 2 << (level + 2 >> 1);
137 int h = 2 << (level + 1 >> 1);
138 int size = w * h;
139 int16_t (*block)[256] = s->encoded_block_levels[level];
140 const int8_t *codebook_sum, *codebook;
141 const uint16_t(*mean_vlc)[2];
142 const uint8_t(*multistage_vlc)[2];
143
144 best_score = 0;
145 // FIXME: Optimize, this does not need to be done multiple times.
146 if (intra) {
147 // level is 5 when encode_block is called from svq1_encode_plane
148 // and always < 4 when called recursively from this function.
149 codebook_sum = level < 4 ? svq1_intra_codebook_sum[level] : NULL;
151 mean_vlc = ff_svq1_intra_mean_vlc;
152 multistage_vlc = ff_svq1_intra_multistage_vlc[level];
153 for (y = 0; y < h; y++) {
154 for (x = 0; x < w; x++) {
155 int v = src[x + y * stride];
156 block[0][x + w * y] = v;
157 best_score += v * v;
158 block_sum[0] += v;
159 }
160 }
161 } else {
162 // level is 5 or < 4, see above for details.
163 codebook_sum = level < 4 ? svq1_inter_codebook_sum[level] : NULL;
165 mean_vlc = ff_svq1_inter_mean_vlc + 256;
166 multistage_vlc = ff_svq1_inter_multistage_vlc[level];
167 for (y = 0; y < h; y++) {
168 for (x = 0; x < w; x++) {
169 int v = src[x + y * stride] - ref[x + y * stride];
170 block[0][x + w * y] = v;
171 best_score += v * v;
172 block_sum[0] += v;
173 }
174 }
175 }
176
177 best_count = 0;
178 best_score -= (int)((unsigned)block_sum[0] * block_sum[0] >> (level + 3));
179 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
180
181 if (level < 4) {
182 for (count = 1; count < 7; count++) {
183 int best_vector_score = INT_MAX;
184 int best_vector_sum = -999, best_vector_mean = -999;
185 const int stage = count - 1;
186 const int8_t *vector;
187
188 for (i = 0; i < 16; i++) {
189 int sum = codebook_sum[stage * 16 + i];
190 int sqr, diff, score;
191
192 vector = codebook + stage * size * 16 + i * size;
193 sqr = s->svq1encdsp.ssd_int8_vs_int16(vector, block[stage], size);
194 diff = block_sum[stage] - sum;
195 score = sqr - (diff * (int64_t)diff >> (level + 3)); // FIXME: 64 bits slooow
196 if (score < best_vector_score) {
197 int mean = diff + (size >> 1) >> (level + 3);
198 av_assert2(mean > -300 && mean < 300);
199 mean = av_clip(mean, intra ? 0 : -256, 255);
200 best_vector_score = score;
201 best_vector[stage] = i;
202 best_vector_sum = sum;
203 best_vector_mean = mean;
204 }
205 }
206 av_assert0(best_vector_mean != -999);
207 vector = codebook + stage * size * 16 + best_vector[stage] * size;
208 for (j = 0; j < size; j++)
209 block[stage + 1][j] = block[stage][j] - vector[j];
210 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
211 best_vector_score += lambda *
212 (+1 + 4 * count +
213 multistage_vlc[1 + count][1]
214 + mean_vlc[best_vector_mean][1]);
215
216 if (best_vector_score < best_score) {
217 best_score = best_vector_score;
218 best_count = count;
219 best_mean = best_vector_mean;
220 }
221 }
222 }
223
224 if (best_mean == -128)
225 best_mean = -127;
226 else if (best_mean == 128)
227 best_mean = 127;
228
229 split = 0;
230 if (best_score > threshold && level) {
231 int score = 0;
232 int offset = level & 1 ? stride * h / 2 : w / 2;
233 PutBitContext backup[6];
234
235 for (i = level - 1; i >= 0; i--)
236 backup[i] = s->reorder_pb[i];
237 score += encode_block(s, src, ref, decoded, stride, level - 1,
238 threshold >> 1, lambda, intra);
239 score += encode_block(s, src + offset, ref + offset, decoded + offset,
240 stride, level - 1, threshold >> 1, lambda, intra);
241 score += lambda;
242
243 if (score < best_score) {
244 best_score = score;
245 split = 1;
246 } else {
247 for (i = level - 1; i >= 0; i--)
248 s->reorder_pb[i] = backup[i];
249 }
250 }
251 if (level > 0)
252 put_bits(&s->reorder_pb[level], 1, split);
253
254 if (!split) {
255 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
256 av_assert1(best_mean >= -256 && best_mean < 256);
257 av_assert1(best_count >= 0 && best_count < 7);
258 av_assert1(level < 4 || best_count == 0);
259
260 /* output the encoding */
261 put_bits(&s->reorder_pb[level],
262 multistage_vlc[1 + best_count][1],
263 multistage_vlc[1 + best_count][0]);
264 put_bits(&s->reorder_pb[level], mean_vlc[best_mean][1],
265 mean_vlc[best_mean][0]);
266
267 for (i = 0; i < best_count; i++) {
268 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
269 put_bits(&s->reorder_pb[level], 4, best_vector[i]);
270 }
271
272 for (y = 0; y < h; y++)
273 for (x = 0; x < w; x++)
274 decoded[x + y * stride] = src[x + y * stride] -
275 block[best_count][x + w * y] +
276 best_mean;
277 }
278
279 return best_score;
280}
281
283{
284 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) + s->mb_x*2;
285 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) + 1 + s->mb_x*2;
286 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) + s->mb_x*2;
287 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) + 1 + s->mb_x*2;
288}
289
290static int svq1_encode_plane(SVQ1EncContext *s, int plane,
291 PutBitContext *pb,
292 const unsigned char *src_plane,
293 unsigned char *ref_plane,
294 unsigned char *decoded_plane,
295 int width, int height, int src_stride, int stride)
296{
297 MpegEncContext *const s2 = &s->m.c;
298 int x, y;
299 int i;
300 int block_width, block_height;
301 int level;
302 int threshold[6];
303 uint8_t *src = s->scratchbuf + stride * 32;
304 const int lambda = (s->quality * s->quality) >>
305 (2 * FF_LAMBDA_SHIFT);
306
307 /* figure out the acceptable level thresholds in advance */
308 threshold[5] = QUALITY_THRESHOLD;
309 for (level = 4; level >= 0; level--)
310 threshold[level] = threshold[level + 1] * THRESHOLD_MULTIPLIER;
311
312 block_width = (width + 15) / 16;
313 block_height = (height + 15) / 16;
314
315 if (s->pict_type == AV_PICTURE_TYPE_P) {
316 s2->last_pic.data[0] = ref_plane;
317 s2->linesize =
318 s2->last_pic.linesize[0] =
319 s->m.new_pic->linesize[0] =
320 s2->cur_pic.linesize[0] = stride;
321 s2->width = width;
322 s2->height = height;
323 s2->mb_width = block_width;
324 s2->mb_height = block_height;
325 s2->mb_stride = s2->mb_width + 1;
326 s2->b8_stride = 2 * s2->mb_width + 1;
327 s->m.f_code = 1;
328 s2->pict_type = s->pict_type;
329 s->m.me.scene_change_score = 0;
330 // s2->out_format = FMT_H263;
331 // s->m.me.unrestricted_mv = 1;
332 s->m.lambda = s->quality;
333 s2->qscale = s->m.lambda * 139 +
334 FF_LAMBDA_SCALE * 64 >>
335 FF_LAMBDA_SHIFT + 7;
336 s->m.lambda2 = s->m.lambda * s->m.lambda +
337 FF_LAMBDA_SCALE / 2 >>
339
340 s->m.mb_type = s->mb_type;
341
342 // dummies, to avoid segfaults
343 s->m.mb_mean = (uint8_t *)s->dummy;
344 s->m.mb_var = (uint16_t *)s->dummy;
345 s->m.mc_mb_var = (uint16_t *)s->dummy;
346 s2->cur_pic.mb_type = s->dummy;
347
348 s2->cur_pic.motion_val[0] = s->motion_val8[plane] + 2;
349 s->m.p_mv_table = s->motion_val16[plane] +
350 s2->mb_stride + 1;
351 ff_me_init_pic(&s->m);
352
353 s->m.me.dia_size = s->avctx->dia_size;
354 s2->first_slice_line = 1;
355 for (y = 0; y < block_height; y++) {
356 s->m.new_pic->data[0] = src - y * 16 * stride; // ugly
357 s2->mb_y = y;
358
359 for (i = 0; i < 16 && i + 16 * y < height; i++) {
360 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
361 width);
362 for (x = width; x < 16 * block_width; x++)
363 src[i * stride + x] = src[i * stride + x - 1];
364 }
365 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
366 memcpy(&src[i * stride], &src[(i - 1) * stride],
367 16 * block_width);
368
369 for (x = 0; x < block_width; x++) {
370 s2->mb_x = x;
372
373 ff_estimate_p_frame_motion(&s->m, x, y);
374 }
375 s2->first_slice_line = 0;
376 }
377
379 ff_fix_long_mvs(&s->m, NULL, 0, s->m.p_mv_table, s->m.f_code,
381 }
382
383 s2->first_slice_line = 1;
384 for (y = 0; y < block_height; y++) {
385 for (i = 0; i < 16 && i + 16 * y < height; i++) {
386 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
387 width);
388 for (x = width; x < 16 * block_width; x++)
389 src[i * stride + x] = src[i * stride + x - 1];
390 }
391 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
392 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
393
394 s2->mb_y = y;
395 for (x = 0; x < block_width; x++) {
396 uint8_t reorder_buffer[2][6][7 * 32];
397 int count[2][6];
398 int offset = y * 16 * stride + x * 16;
399 uint8_t *decoded = decoded_plane + offset;
400 const uint8_t *ref = ref_plane + offset;
401 int score[4] = { 0, 0, 0, 0 }, best;
402 uint8_t *temp = s->scratchbuf;
403
404 if (put_bytes_left(pb, 0) < 3000) { // FIXME: check size
405 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
406 return -1;
407 }
408
409 s2->mb_x = x;
411
412 if (s->pict_type == AV_PICTURE_TYPE_I ||
413 (s->m.mb_type[x + y * s2->mb_stride] &
415 for (i = 0; i < 6; i++)
416 init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i],
417 7 * 32);
418 if (s->pict_type == AV_PICTURE_TYPE_P) {
420 score[0] = SVQ1_BLOCK_INTRA_LEN * lambda;
421 }
422 score[0] += encode_block(s, src + 16 * x, src + 16 * x /* unused */,
423 temp, stride, 5, 64, lambda, 1);
424 for (i = 0; i < 6; i++) {
425 count[0][i] = put_bits_count(&s->reorder_pb[i]);
426 flush_put_bits(&s->reorder_pb[i]);
427 }
428 } else
429 score[0] = INT_MAX;
430
431 best = 0;
432
433 if (s->pict_type == AV_PICTURE_TYPE_P) {
434 int mx, my, pred_x, pred_y, dxy;
435 int16_t *motion_ptr;
436
437 motion_ptr = ff_h263_pred_motion(s2, 0, 0, &pred_x, &pred_y);
438 if (s->m.mb_type[x + y * s2->mb_stride] &
440 for (i = 0; i < 6; i++)
441 init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i],
442 7 * 32);
443
445
446 mx = motion_ptr[0];
447 my = motion_ptr[1];
448 av_assert1(mx >= -32 && mx <= 31);
449 av_assert1(my >= -32 && my <= 31);
450 av_assert1(pred_x >= -32 && pred_x <= 31);
451 av_assert1(pred_y >= -32 && pred_y <= 31);
452 ff_h263_encode_motion(&s->reorder_pb[5], mx - pred_x, 1);
453 ff_h263_encode_motion(&s->reorder_pb[5], my - pred_y, 1);
454 score[1] += lambda * put_bits_count(&s->reorder_pb[5]);
455
456 dxy = (mx & 1) + 2 * (my & 1);
457
458 s2->hdsp.put_pixels_tab[0][dxy](temp + 16*stride,
459 ref + (mx >> 1) +
460 stride * (my >> 1),
461 stride, 16);
462
463 score[1] += encode_block(s, src + 16 * x, temp + 16*stride,
464 decoded, stride, 5, 64, lambda, 0);
465 best = score[1] <= score[0];
466
467 score[2] = s->mecc.sse[0](NULL, src + 16 * x, ref,
468 stride, 16);
469 score[2] += SVQ1_BLOCK_SKIP_LEN * lambda;
470 if (score[2] < score[best] && mx == 0 && my == 0) {
471 best = 2;
472 s2->hdsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
474 }
475 }
476
477 if (best == 1) {
478 for (i = 0; i < 6; i++) {
479 count[1][i] = put_bits_count(&s->reorder_pb[i]);
480 flush_put_bits(&s->reorder_pb[i]);
481 }
482 } else {
483 motion_ptr[0] =
484 motion_ptr[1] =
485 motion_ptr[2] =
486 motion_ptr[3] =
487 motion_ptr[0 + 2 * s2->b8_stride] =
488 motion_ptr[1 + 2 * s2->b8_stride] =
489 motion_ptr[2 + 2 * s2->b8_stride] =
490 motion_ptr[3 + 2 * s2->b8_stride] = 0;
491 }
492 }
493
494 s->rd_total += score[best];
495
496 if (best != 2)
497 for (i = 5; i >= 0; i--)
498 ff_copy_bits(pb, reorder_buffer[best][i],
499 count[best][i]);
500 if (best == 0)
501 s2->hdsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
502 }
503 s2->first_slice_line = 0;
504 }
505 return 0;
506}
507
509{
510 SVQ1EncContext *const s = avctx->priv_data;
511 int i;
512
513 if (avctx->frame_num)
514 av_log(avctx, AV_LOG_DEBUG, "RD: %f\n",
515 s->rd_total / (double)(avctx->width * avctx->height *
516 avctx->frame_num));
517
518 av_freep(&s->m.me.scratchpad);
519 av_freep(&s->mb_type);
520 av_freep(&s->dummy);
521 av_freep(&s->scratchbuf);
522
523 for (i = 0; i < 3; i++) {
524 av_freep(&s->motion_val8[i]);
525 av_freep(&s->motion_val16[i]);
526 }
527
528 av_frame_free(&s->current_picture);
529 av_frame_free(&s->last_picture);
530 av_frame_free(&s->m.new_pic);
531
532 return 0;
533}
534
535static av_cold int write_ident(AVCodecContext *avctx, const char *ident)
536{
537 int size = strlen(ident);
538 avctx->extradata = av_malloc(size + 8);
539 if (!avctx->extradata)
540 return AVERROR(ENOMEM);
541 AV_WB32(avctx->extradata, size + 8);
542 AV_WL32(avctx->extradata + 4, MKTAG('S', 'V', 'Q', '1'));
543 memcpy(avctx->extradata + 8, ident, size);
544 avctx->extradata_size = size + 8;
545 return 0;
546}
547
549{
550 SVQ1EncContext *const s = avctx->priv_data;
551 int ret;
552
553 if (avctx->width >= 4096 || avctx->height >= 4096) {
554 av_log(avctx, AV_LOG_ERROR, "Dimensions too large, maximum is 4095x4095\n");
555 return AVERROR(EINVAL);
556 }
557
558 ff_hpeldsp_init(&s->m.c.hdsp, avctx->flags);
559 ff_me_cmp_init(&s->mecc, avctx);
560 ret = ff_me_init(&s->m.me, avctx, &s->mecc, 0);
561 if (ret < 0)
562 return ret;
563 ff_mpegvideoencdsp_init(&s->m.mpvencdsp, avctx);
564
565 s->current_picture = av_frame_alloc();
566 s->last_picture = av_frame_alloc();
567 if (!s->current_picture || !s->last_picture) {
568 return AVERROR(ENOMEM);
569 }
570 ret = ff_encode_alloc_frame(avctx, s->current_picture);
571 if (ret < 0)
572 return ret;
573 ret = ff_encode_alloc_frame(avctx, s->last_picture);
574 if (ret < 0)
575 return ret;
576 s->scratchbuf = av_malloc_array(s->current_picture->linesize[0], 16 * 3);
577 if (!s->scratchbuf)
578 return AVERROR(ENOMEM);
579
580 s->frame_width = avctx->width;
581 s->frame_height = avctx->height;
582
583 s->y_block_width = (s->frame_width + 15) / 16;
584 s->y_block_height = (s->frame_height + 15) / 16;
585
586 s->avctx = avctx;
587 s->m.c.avctx = avctx;
588
589 for (size_t plane = 0; plane < FF_ARRAY_ELEMS(s->motion_val16); ++plane) {
590 const int shift = plane ? 2 : 0;
591 unsigned block_height = ((s->frame_height >> shift) + 15U) / 16;
592 unsigned block_width = ((s->frame_width >> shift) + 15U) / 16;
593
594 s->motion_val8[plane] = av_calloc((2 * block_width + 1) * block_height * 2 + 2,
595 2 * sizeof(int16_t));
596 s->motion_val16[plane] = av_calloc((block_width + 1) * (block_height + 2) + 1,
597 2 * sizeof(int16_t));
598 if (!s->motion_val8[plane] || !s->motion_val16[plane])
599 return AVERROR(ENOMEM);
600 }
601
602 s->m.c.picture_structure = PICT_FRAME;
603 s->m.me.temp =
604 s->m.me.scratchpad = av_mallocz((avctx->width + 64) *
605 2 * 16 * 2 * sizeof(uint8_t));
606 s->mb_type = av_mallocz((s->y_block_width + 1) *
607 s->y_block_height * sizeof(int16_t));
608 s->dummy = av_mallocz((s->y_block_width + 1) *
609 s->y_block_height * sizeof(int32_t));
610 s->m.new_pic = av_frame_alloc();
611
612 if (!s->m.me.scratchpad ||
613 !s->mb_type || !s->dummy || !s->m.new_pic)
614 return AVERROR(ENOMEM);
615
616 ff_svq1enc_init(&s->svq1encdsp);
617
618 s->m.me.mv_penalty = ff_h263_get_mv_penalty();
619
620 return write_ident(avctx, s->avctx->flags & AV_CODEC_FLAG_BITEXACT ? "Lavc" : LIBAVCODEC_IDENT);
621}
622
624 const AVFrame *pict, int *got_packet)
625{
626 SVQ1EncContext *const s = avctx->priv_data;
627 PutBitContext pb;
628 int i, ret;
629
630 ret = ff_alloc_packet(avctx, pkt, s->y_block_width * s->y_block_height *
632 if (ret < 0)
633 return ret;
634
635 FFSWAP(AVFrame*, s->current_picture, s->last_picture);
636
637 if (avctx->gop_size && (avctx->frame_num % avctx->gop_size))
638 s->pict_type = AV_PICTURE_TYPE_P;
639 else
640 s->pict_type = AV_PICTURE_TYPE_I;
641 s->quality = pict->quality;
642
643 ff_encode_add_stats_side_data(pkt, pict->quality, NULL, 0, s->pict_type);
644
645 init_put_bits(&pb, pkt->data, pkt->size);
646 svq1_write_header(s, &pb, s->pict_type);
647 for (i = 0; i < 3; i++) {
648 ret = svq1_encode_plane(s, i, &pb,
649 pict->data[i],
650 s->last_picture->data[i],
651 s->current_picture->data[i],
652 s->frame_width / (i ? 4 : 1),
653 s->frame_height / (i ? 4 : 1),
654 pict->linesize[i],
655 s->current_picture->linesize[i]);
656 emms_c();
657 if (ret < 0)
658 return ret;
659 }
660
661 // align_put_bits(&pb);
662 while (put_bits_count(&pb) & 31)
663 put_bits(&pb, 1, 0);
664
665 flush_put_bits(&pb);
666
667 pkt->size = put_bytes_output(&pb);
668 if (s->pict_type == AV_PICTURE_TYPE_I)
669 pkt->flags |= AV_PKT_FLAG_KEY;
670 *got_packet = 1;
671
672 return 0;
673}
674
675#define OFFSET(x) offsetof(struct SVQ1EncContext, x)
676#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
677static const AVOption options[] = {
678 { "motion-est", "Motion estimation algorithm", OFFSET(m.me.motion_est), AV_OPT_TYPE_INT, { .i64 = FF_ME_EPZS }, FF_ME_ZERO, FF_ME_XONE, VE, .unit = "motion-est"},
679 { "zero", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ZERO }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "motion-est" },
680 { "epzs", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_EPZS }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "motion-est" },
681 { "xone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_XONE }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "motion-est" },
682
683 { NULL },
684};
685
686static const AVClass svq1enc_class = {
687 .class_name = "svq1enc",
688 .item_name = av_default_item_name,
689 .option = options,
690 .version = LIBAVUTIL_VERSION_INT,
691};
692
694 .p.name = "svq1",
695 CODEC_LONG_NAME("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
696 .p.type = AVMEDIA_TYPE_VIDEO,
697 .p.id = AV_CODEC_ID_SVQ1,
699 .priv_data_size = sizeof(SVQ1EncContext),
700 .p.priv_class = &svq1enc_class,
701 .init = svq1_encode_init,
703 .close = svq1_encode_end,
705 .color_ranges = AVCOL_RANGE_MPEG,
706 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
707};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:57
static double sqr(double in)
Definition af_afwtdn.c:872
static char * split(char *message, char delim)
const FFCodec ff_svq1_encoder
Definition svq1enc.c:693
#define VE
Definition amfenc_av1.c:30
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define av_assume(cond)
Definition avassert.h:119
Libavcodec external API header.
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition bitstream.c:49
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static const unsigned codebook[256][2]
Definition cfhdenc.c:41
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static int16_t block[64]
Definition dct.c:125
static AVPacket * pkt
#define emms_c()
Definition emms.h:88
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
Definition encode.c:1070
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
Definition encode.c:989
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition encode.h:34
reference-counted frame API
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SVQ1
Definition codec_id.h:72
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
#define FF_LAMBDA_SCALE
Definition avutil.h:225
#define FF_LAMBDA_SHIFT
Definition avutil.h:224
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
AVPictureType
Definition avutil.h:276
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition h263.c:182
void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
Definition ituh263enc.c:157
const uint8_t(* ff_h263_get_mv_penalty(void))[MAX_DMV *2+1]
Definition ituh263enc.c:148
Half-pel DSP functions.
#define AV_WB32(p, v)
#define AV_WL32(p, v)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
frame_type
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition hpeldsp.c:337
common internal api header.
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
Definition utils.c:850
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
#define QUALITY_THRESHOLD
Definition svq1enc.c:124
static void init_block_index(MpegEncContext *const s)
Definition svq1enc.c:282
static av_cold int svq1_encode_end(AVCodecContext *avctx)
Definition svq1enc.c:508
static int svq1_encode_plane(SVQ1EncContext *s, int plane, PutBitContext *pb, const unsigned char *src_plane, unsigned char *ref_plane, unsigned char *decoded_plane, int width, int height, int src_stride, int stride)
Definition svq1enc.c:290
static const AVClass svq1enc_class
Definition svq1enc.c:686
static av_cold int svq1_encode_init(AVCodecContext *avctx)
Definition svq1enc.c:548
static av_cold int write_ident(AVCodecContext *avctx, const char *ident)
Definition svq1enc.c:535
#define THRESHOLD_MULTIPLIER
Definition svq1enc.c:125
static void svq1_write_header(SVQ1EncContext *s, PutBitContext *pb, int frame_type)
Definition svq1enc.c:90
static int encode_block(SVQ1EncContext *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, unsigned level, int threshold, int lambda, int intra)
Definition svq1enc.c:127
static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition svq1enc.c:623
#define OFFSET(x)
Definition svq1enc.c:675
Libavcodec version macros.
#define LIBAVCODEC_IDENT
Definition version.h:43
#define av_cold
Definition attributes.h:117
uint8_t w
Definition llvidencdsp.c:39
#define FFSWAP(type, a, b)
Definition macros.h:52
#define MKTAG(a, b, c, d)
Definition macros.h:55
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
Definition me_cmp.c:961
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
static int block_sum(const uint8_t *block, int w, int h, int linesize)
Definition mobiclip.c:814
void ff_estimate_p_frame_motion(MPVEncContext *const s, int mb_x, int mb_y)
Definition motion_est.c:888
void ff_me_init_pic(MPVEncContext *const s)
Definition motion_est.c:371
void ff_fix_long_p_mvs(MPVEncContext *const s, int type)
void ff_fix_long_mvs(MPVEncContext *const s, uint8_t *field_select_table, int field_select, int16_t(*mv_table)[2], int f_code, int type, int truncate)
av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, const MECmpContext *mecc, int mpvenc)
Definition motion_est.c:309
#define FF_ME_EPZS
Definition motion_est.h:43
#define FF_ME_XONE
Definition motion_est.h:44
#define FF_ME_ZERO
Definition motion_est.h:42
#define MAX_MB_BYTES
Definition mpegutils.h:35
#define PICT_FRAME
Definition mpegutils.h:33
mpegvideo header.
#define CANDIDATE_MB_TYPE_INTRA
#define FF_MPV_OPT_FLAGS
#define CANDIDATE_MB_TYPE_INTER
#define av_malloc(s)
Definition ops_static.c:52
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static int put_bits_count(PutBitContext *s)
Definition put_bits.h:90
static int put_bytes_left(const PutBitContext *s, int round_up)
Definition put_bits.h:145
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition avcodec.h:1021
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition frame.h:594
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition hpeldsp.h:57
uint32_t * mb_type
types and macros are defined in mpegutils.h
int16_t(*[2] motion_val)[2]
ptrdiff_t linesize[MPV_MAX_PLANES]
Definition mpegpicture.h:97
uint8_t * data[MPV_MAX_PLANES]
Definition mpegpicture.h:96
MpegEncContext.
Definition mpegvideo.h:67
HpelDSPContext hdsp
Definition mpegvideo.h:159
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11
Definition mpegvideo.h:97
int first_slice_line
used in MPEG-4 too to handle resync markers
Definition mpegvideo.h:234
int mb_height
number of MBs horizontally & vertically
Definition mpegvideo.h:96
MPVWorkPicture last_pic
copy of the previous picture structure.
Definition mpegvideo.h:120
int height
picture size. must be a multiple of 16
Definition mpegvideo.h:84
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition mpegvideo.h:101
enum AVPictureType pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition mpegvideo.h:154
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition mpegvideo.h:98
MPVWorkPicture cur_pic
copy of the current picture structure.
Definition mpegvideo.h:132
int y_block_height
Definition svq1enc.c:74
MECmpContext mecc
Definition svq1enc.c:57
int16_t(*[3] motion_val16)[2]
Definition svq1enc.c:81
uint8_t * scratchbuf
Definition svq1enc.c:85
int frame_width
Definition svq1enc.c:69
enum AVPictureType pict_type
Definition svq1enc.c:62
PutBitContext reorder_pb[6]
Definition svq1enc.c:67
uint32_t * dummy
Definition svq1enc.c:79
AVCodecContext * avctx
Definition svq1enc.c:56
SVQ1EncDSPContext svq1encdsp
Definition svq1enc.c:87
uint16_t * mb_type
Definition svq1enc.c:78
AVFrame * current_picture
Definition svq1enc.c:58
MPVEncContext m
Definition svq1enc.c:55
int16_t(*[3] motion_val8)[2]
Definition svq1enc.c:80
int y_block_width
Definition svq1enc.c:73
AVFrame * last_picture
Definition svq1enc.c:59
int frame_height
Definition svq1enc.c:70
int64_t rd_total
Definition svq1enc.c:83
int16_t encoded_block_levels[6][7][256]
Definition svq1enc.c:76
const uint16_t ff_svq1_frame_size_table[7][2]
Definition svq1.c:40
Sorenson Vector Quantizer #1 (SVQ1) video codec.
#define SVQ1_BLOCK_SKIP_CODE
Definition svq1.h:47
const uint8_t ff_svq1_intra_multistage_vlc[6][8][2]
Definition svq1_vlc.h:33
const uint8_t ff_svq1_inter_multistage_vlc[6][8][2]
Definition svq1_vlc.h:50
#define SVQ1_BLOCK_INTRA_CODE
Definition svq1.h:51
#define SVQ1_BLOCK_INTRA_LEN
Definition svq1.h:52
#define SVQ1_BLOCK_INTER_LEN
Definition svq1.h:50
const int8_t *const ff_svq1_intra_codebooks[6]
Definition svq1_cb.h:1519
#define SVQ1_BLOCK_INTER_CODE
Definition svq1.h:49
const uint16_t ff_svq1_inter_mean_vlc[512][2]
Definition svq1_vlc.h:136
#define SVQ1_BLOCK_SKIP_LEN
Definition svq1.h:48
const uint16_t ff_svq1_intra_mean_vlc[256][2]
Definition svq1_vlc.h:67
FF_VISIBILITY_PUSH_HIDDEN const int8_t *const ff_svq1_inter_codebooks[6]
Definition svq1_cb.h:776
svq1 code books.
static const int8_t svq1_intra_codebook_sum[4][16 *6]
Definition svq1enc_cb.h:59
static const int8_t svq1_inter_codebook_sum[4][16 *6]
Definition svq1enc_cb.h:32
static void ff_svq1enc_init(SVQ1EncDSPContext *c)
Definition svq1encdsp.h:47
uint8_t level
Definition svq3.c:208
#define stride
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size
else temp
Definition vf_mcdeint.c:275
static float mean(const float *input, int size)
Definition vf_nnedi.c:861
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)