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) {
107 // level is 5 when encode_block is called from svq1_encode_plane
108 // and always < 4 when called recursively from this function.
113 for (y = 0; y <
h; y++) {
114 for (x = 0; x <
w; x++) {
115 int v = src[x + y *
stride];
116 block[0][x + w * y] = v;
117 best_score += v * v;
118 block_sum[0] += v;
119 }
120 }
121 } else {
122 // level is 5 or < 4, see above for details.
127 for (y = 0; y <
h; y++) {
128 for (x = 0; x <
w; x++) {
130 block[0][x + w * y] = v;
131 best_score += v * v;
132 block_sum[0] += v;
133 }
134 }
135 }
136
137 best_count = 0;
138 best_score -= (
int)((
unsigned)block_sum[0] * block_sum[0] >> (level + 3));
139 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
140
141 if (level < 4) {
142 for (count = 1; count < 7; count++) {
143 int best_vector_score = INT_MAX;
144 int best_vector_sum = -999, best_vector_mean = -999;
145 const int stage = count - 1;
146 const int8_t *vector;
147
148 for (i = 0; i < 16; i++) {
149 int sum = codebook_sum[stage * 16 + i];
150 int sqr,
diff, score;
151
152 vector = codebook + stage * size * 16 + i *
size;
154 diff = block_sum[stage] - sum;
155 score = sqr - (diff * (int64_t)diff >> (level + 3)); // FIXME: 64 bits slooow
156 if (score < best_vector_score) {
157 int mean = diff + (size >> 1) >> (level + 3);
159 mean = av_clip(mean, intra ? 0 : -256, 255);
160 best_vector_score = score;
161 best_vector[stage] = i;
162 best_vector_sum = sum;
163 best_vector_mean = mean;
164 }
165 }
167 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
168 for (j = 0; j <
size; j++)
169 block[stage + 1][j] =
block[stage][j] - vector[j];
170 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
171 best_vector_score += lambda *
172 (+1 + 4 * count +
173 multistage_vlc[1 +
count][1]
174 + mean_vlc[best_vector_mean][1]);
175
176 if (best_vector_score < best_score) {
177 best_score = best_vector_score;
179 best_mean = best_vector_mean;
180 }
181 }
182 }
183
184 split = 0;
185 if (best_score > threshold && level) {
186 int score = 0;
187 int offset = level & 1 ? stride * h / 2 : w / 2;
189
190 for (i = level - 1; i >= 0; i--)
192 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
193 threshold >> 1, lambda, intra);
194 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
195 stride, level - 1, threshold >> 1, lambda, intra);
196 score += lambda;
197
198 if (score < best_score) {
199 best_score = score;
200 split = 1;
201 } else {
202 for (i = level - 1; i >= 0; i--)
204 }
205 }
206 if (level > 0)
208
209 if (!split) {
210 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
211 av_assert1(best_mean >= -256 && best_mean < 256);
212 av_assert1(best_count >= 0 && best_count < 7);
214
215 /* output the encoding */
217 multistage_vlc[1 + best_count][1],
218 multistage_vlc[1 + best_count][0]);
220 mean_vlc[best_mean][0]);
221
222 for (i = 0; i < best_count; i++) {
223 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
225 }
226
227 for (y = 0; y <
h; y++)
228 for (x = 0; x <
w; x++)
229 decoded[x + y * stride] = src[x + y * stride] -
230 block[best_count][x + w * y] +
231 best_mean;
232 }
233
234 return best_score;
235 }
236
244 }
245
247 unsigned char *src_plane,
248 unsigned char *ref_plane,
249 unsigned char *decoded_plane,
251 {
252 int x, y;
253 int i;
254 int block_width, block_height;
256 int threshold[6];
260
261 /* figure out the acceptable level thresholds in advance */
263 for (level = 4; level >= 0; level--)
265
266 block_width = (width + 15) / 16;
267 block_height = (height + 15) / 16;
268
288 // s->m.out_format = FMT_H263;
289 // s->m.unrestricted_mv = 1;
297
300 block_height * 2 + 2) *
301 2 * sizeof(int16_t));
303 (block_height + 2) + 1) *
304 2 * sizeof(int16_t));
307 }
308
310
311 // dummies, to avoid segfaults
316
322
325 for (y = 0; y < block_height; y++) {
328
329 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
330 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
331 width);
332 for (x = width; x < 16 * block_width; x++)
333 src[i * stride + x] = src[i * stride + x - 1];
334 }
335 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
336 memcpy(&src[i * stride], &src[(i - 1) * stride],
337 16 * block_width);
338
339 for (x = 0; x < block_width; x++) {
342
344 }
346 }
347
351 }
352
354 for (y = 0; y < block_height; y++) {
355 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
356 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
357 width);
358 for (x = width; x < 16 * block_width; x++)
359 src[i * stride + x] = src[i * stride + x - 1];
360 }
361 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
362 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
363
365 for (x = 0; x < block_width; x++) {
366 uint8_t reorder_buffer[2][6][7 * 32];
368 int offset = y * 16 * stride + x * 16;
371 int score[4] = { 0, 0, 0, 0 }, best;
373
377 return -1;
378 }
379
382
386 for (i = 0; i < 6; i++)
388 7 * 32);
392 score[0] = vlc[1] * lambda;
393 }
395 5, 64, lambda, 1);
396 for (i = 0; i < 6; i++) {
399 }
400 } else
401 score[0] = INT_MAX;
402
403 best = 0;
404
407 int mx, my, pred_x, pred_y, dxy;
408 int16_t *motion_ptr;
409
413 for (i = 0; i < 6; i++)
415 7 * 32);
416
418
420 mx = motion_ptr[0];
421 my = motion_ptr[1];
430
431 dxy = (mx & 1) + 2 * (my & 1);
432
434 ref + (mx >> 1) +
435 stride * (my >> 1),
437
438 score[1] +=
encode_block(s, src + 16 * x, temp + 16*stride,
439 decoded, stride, 5, 64, lambda, 0);
440 best = score[1] <= score[0];
441
445 score[2] += vlc[1] * lambda;
446 if (score[2] < score[best] && mx == 0 && my == 0) {
447 best = 2;
450 }
451 }
452
453 if (best == 1) {
454 for (i = 0; i < 6; i++) {
457 }
458 } else {
459 motion_ptr[0] =
460 motion_ptr[1] =
461 motion_ptr[2] =
462 motion_ptr[3] =
467 }
468 }
469
471
472 if (best != 2)
473 for (i = 5; i >= 0; i--)
475 count[best][i]);
476 if (best == 0)
478 }
480 }
481 return 0;
482 }
483
485 {
487 int i;
488
492
495
502
503 for (i = 0; i < 3; i++) {
506 }
507
510
511 return 0;
512 }
513
515 {
517 int ret;
518
519 if (avctx->
width >= 4096 || avctx->
height >= 4096) {
522 }
523
527
533 }
534
537
540
543
546
549 return ret;
550 }
551
563
568 }
569
570 if (ARCH_PPC)
572 if (ARCH_X86)
574
576
577 return 0;
578 }
579
581 const AVFrame *pict,
int *got_packet)
582 {
584 int i, ret;
585
588 return ret;
589
592 return -1;
593 }
594
597 return ret;
598 }
599 }
602 if (ret < 0)
603 return ret;
604 }
609 }
610
612
614
617 else
620
621 #if FF_API_CODED_FRAME
626 #endif
627
629
631 for (i = 0; i < 3; i++) {
640 emms_c();
641 if (ret < 0) {
642 int j;
643 for (j = 0; j < i; j++) {
646 }
648 return -1;
649 }
650 }
651
652 // avpriv_align_put_bits(&s->pb);
655
657
661 *got_packet = 1;
662
663 return 0;
664 }
665
666 #define OFFSET(x) offsetof(struct SVQ1EncContext, x)
667 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
673
675 };
676
682 };
683
690 .priv_class = &svq1enc_class,
696 };
uint8_t * scratchpad
data area for the ME algo, so that the ME does not need to malloc/free.
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
static const AVClass svq1enc_class
This structure describes decoded (raw) audio or video data.
int16_t(* p_mv_table)[2]
MV table (1MV per MB) P-frame encoding.
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
void ff_estimate_p_frame_motion(MpegEncContext *s, int mb_x, int mb_y)
uint8_t * mb_mean
Table for MB luminance.
#define THRESHOLD_MULTIPLIER
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define LIBAVUTIL_VERSION_INT
void ff_h263_encode_init(MpegEncContext *s)
static av_cold int init(AVCodecContext *avctx)
uint16_t * mb_var
Table for MB variances.
AVFrame * current_picture
const char * av_default_item_name(void *ptr)
Return the context name.
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
static int svq1_encode_plane(SVQ1EncContext *s, int plane, unsigned char *src_plane, unsigned char *ref_plane, unsigned char *decoded_plane, int width, int height, int src_stride, int stride)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
uint32_t * score_map
map to store the scores
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
static const int8_t svq1_intra_codebook_sum[4][16 *6]
static void init_block_index(MpegEncContext *s)
const uint16_t ff_svq1_frame_size_table[7][2]
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define CANDIDATE_MB_TYPE_INTER
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static av_cold int svq1_encode_init(AVCodecContext *avctx)
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
const int8_t *const ff_svq1_inter_codebooks[6]
int16_t encoded_block_levels[6][7][256]
enum AVPictureType pict_type
Picture current_picture
copy of the current picture structure.
int mb_height
number of MBs horizontally & vertically
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static int ssd_int8_vs_int16_c(const int8_t *pix1, const int16_t *pix2, intptr_t size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int flags
AV_CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
MpegvideoEncDSPContext mpvencdsp
const char * name
Name of the codec implementation.
int16_t(*[3] motion_val16)[2]
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
static const uint8_t offset[127][2]
void ff_mpv_common_end(MpegEncContext *s)
static int encode_block(SVQ1EncContext *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, int level, int threshold, int lambda, int intra)
int flags
A combination of AV_PKT_FLAG values.
Sorenson Vector Quantizer #1 (SVQ1) video codec.
static int put_bits_count(PutBitContext *s)
static char * split(char *message, char delim)
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
PutBitContext reorder_pb[6]
enum AVPictureType pict_type
Picture type of the frame.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Picture new_picture
copy of the source picture structure for encoding.
const uint8_t ff_svq1_block_type_vlc[4][2]
int width
picture width / height.
int16_t(*[2] motion_val)[2]
Picture * current_picture_ptr
pointer to the current picture
int(* ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2, intptr_t size)
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
#define FF_ARRAY_ELEMS(a)
const uint8_t ff_svq1_inter_multistage_vlc[6][8][2]
int block_index[6]
index to current MB in block based arrays with edges
static av_cold int svq1_encode_end(AVCodecContext *avctx)
int first_slice_line
used in MPEG-4 too to handle resync markers
uint16_t * mc_mb_var
Table for motion compensated MB variances.
int16_t(*[3] motion_val8)[2]
unsigned int lambda2
(lambda*lambda) >> FF_LAMBDA_SHIFT
Libavcodec external API header.
ptrdiff_t linesize
line size, in bytes, may be different from width
static void svq1_write_header(SVQ1EncContext *s, int frame_type)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
av_cold void ff_svq1enc_init_ppc(SVQ1EncContext *c)
main external API structure.
int height
picture size. must be a multiple of 16
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
void ff_fix_long_p_mvs(MpegEncContext *s)
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Describe the class of an AVClass context structure.
int f_code
forward MV resolution
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
static enum AVPixelFormat pix_fmts[]
int motion_est
ME algorithm.
void ff_svq1enc_init_x86(SVQ1EncContext *c)
int ff_init_me(MpegEncContext *s)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
struct AVCodecContext * avctx
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
PutBitContext pb
bit output
GLint GLenum GLboolean GLsizei stride
#define FF_DISABLE_DEPRECATION_WARNINGS
const int8_t *const ff_svq1_intra_codebooks[6]
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int ref[MAX_W *MAX_W]
const uint8_t ff_svq1_intra_multistage_vlc[6][8][2]
Picture last_picture
copy of the previous picture structure.
Picture * last_picture_ptr
pointer to the previous picture.
static const int8_t svq1_inter_codebook_sum[4][16 *6]
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
const uint16_t ff_svq1_inter_mean_vlc[512][2]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
uint32_t * map
map to avoid duplicate evaluations
static const AVOption options[]
#define CANDIDATE_MB_TYPE_INTRA
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int dia_size
ME diamond size & shape.
#define FF_ENABLE_DEPRECATION_WARNINGS
int key_frame
1 -> keyframe, 0-> not
void ff_fix_long_mvs(MpegEncContext *s, uint8_t *field_select_table, int field_select, int16_t(*mv_table)[2], int f_code, int type, int truncate)
static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
int frame_number
Frame counter, set by libavcodec.
uint32_t * mb_type
types and macros are defined in mpegutils.h
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
#define QUALITY_THRESHOLD
AVPixelFormat
Pixel format.
This structure stores compressed data.
const uint16_t ff_svq1_intra_mean_vlc[256][2]
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.
unsigned int lambda
Lagrange multiplier used in rate distortion.