111 s->frame_width,
s->frame_height);
124#define QUALITY_THRESHOLD 100
125#define THRESHOLD_MULTIPLIER 0.6
129 int threshold,
int lambda,
int intra)
133 int count, y, x,
i, j,
split, best_mean, best_score, best_count;
136 int w = 2 << (
level + 2 >> 1);
137 int h = 2 << (
level + 1 >> 1);
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];
153 for (y = 0; y <
h; y++) {
154 for (x = 0; x <
w; x++) {
167 for (y = 0; y <
h; y++) {
168 for (x = 0; x <
w; x++) {
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;
188 for (
i = 0;
i < 16;
i++) {
189 int sum = codebook_sum[stage * 16 +
i];
193 sqr =
s->svq1encdsp.ssd_int8_vs_int16(vector,
block[stage],
size);
196 if (score < best_vector_score) {
200 best_vector_score = score;
201 best_vector[stage] =
i;
202 best_vector_sum = sum;
203 best_vector_mean =
mean;
208 for (j = 0; j <
size; j++)
209 block[stage + 1][j] =
block[stage][j] - vector[j];
211 best_vector_score += lambda *
213 multistage_vlc[1 + count][1]
214 + mean_vlc[best_vector_mean][1]);
216 if (best_vector_score < best_score) {
217 best_score = best_vector_score;
219 best_mean = best_vector_mean;
224 if (best_mean == -128)
226 else if (best_mean == 128)
230 if (best_score > threshold &&
level) {
236 backup[
i] =
s->reorder_pb[
i];
238 threshold >> 1, lambda, intra);
243 if (score < best_score) {
248 s->reorder_pb[
i] = backup[
i];
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);
262 multistage_vlc[1 + best_count][1],
263 multistage_vlc[1 + best_count][0]);
265 mean_vlc[best_mean][0]);
267 for (
i = 0;
i < best_count;
i++) {
272 for (y = 0; y <
h; y++)
273 for (x = 0; x <
w; x++)
275 block[best_count][x +
w * y] +
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;
292 const unsigned char *src_plane,
293 unsigned char *ref_plane,
294 unsigned char *decoded_plane,
300 int block_width, block_height;
304 const int lambda = (
s->quality *
s->quality) >>
312 block_width = (
width + 15) / 16;
313 block_height = (
height + 15) / 16;
319 s->m.new_pic->linesize[0] =
329 s->m.me.scene_change_score = 0;
332 s->m.lambda =
s->quality;
333 s2->
qscale =
s->m.lambda * 139 +
336 s->m.lambda2 =
s->m.lambda *
s->m.lambda +
340 s->m.mb_type =
s->mb_type;
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;
349 s->m.p_mv_table =
s->motion_val16[plane] +
353 s->m.me.dia_size =
s->avctx->dia_size;
355 for (y = 0; y < block_height; y++) {
356 s->m.new_pic->data[0] =
src - y * 16 *
stride;
359 for (
i = 0;
i < 16 &&
i + 16 * y <
height;
i++) {
360 memcpy(&
src[
i *
stride], &src_plane[(
i + 16 * y) * src_stride],
362 for (x =
width; x < 16 * block_width; x++)
365 for (;
i < 16 &&
i + 16 * y < 16 * block_height;
i++)
369 for (x = 0; x < block_width; x++) {
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],
388 for (x =
width; x < 16 * block_width; x++)
391 for (;
i < 16 &&
i + 16 * y < 16 * block_height;
i++)
395 for (x = 0; x < block_width; x++) {
396 uint8_t reorder_buffer[2][6][7 * 32];
399 uint8_t *decoded = decoded_plane +
offset;
401 int score[4] = { 0, 0, 0, 0 }, best;
402 uint8_t *
temp =
s->scratchbuf;
415 for (
i = 0;
i < 6;
i++)
424 for (
i = 0;
i < 6;
i++) {
434 int mx,
my, pred_x, pred_y, dxy;
440 for (
i = 0;
i < 6;
i++)
456 dxy = (
mx & 1) + 2 * (
my & 1);
464 decoded,
stride, 5, 64, lambda, 0);
465 best = score[1] <= score[0];
470 if (score[2] < score[best] &&
mx == 0 &&
my == 0) {
478 for (
i = 0;
i < 6;
i++) {
494 s->rd_total += score[best];
497 for (
i = 5;
i >= 0;
i--)
523 for (
i = 0;
i < 3;
i++) {
537 int size = strlen(ident);
553 if (avctx->
width >= 4096 || avctx->
height >= 4096) {
567 if (!
s->current_picture || !
s->last_picture) {
580 s->frame_width = avctx->
width;
581 s->frame_height = avctx->
height;
583 s->y_block_width = (
s->frame_width + 15) / 16;
584 s->y_block_height = (
s->frame_height + 15) / 16;
587 s->m.c.avctx = avctx;
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;
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])
605 2 * 16 * 2 *
sizeof(uint8_t));
607 s->y_block_height *
sizeof(int16_t));
609 s->y_block_height *
sizeof(
int32_t));
612 if (!
s->m.me.scratchpad ||
613 !
s->mb_type || !
s->dummy || !
s->m.new_pic)
624 const AVFrame *pict,
int *got_packet)
647 for (
i = 0;
i < 3;
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),
655 s->current_picture->linesize[
i]);
675#define OFFSET(x) offsetof(struct SVQ1EncContext, x)
676#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
687 .class_name =
"svq1enc",
695 CODEC_LONG_NAME(
"Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
static double sqr(double in)
static char * split(char *message, char delim)
const FFCodec ff_svq1_encoder
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.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
#define i(width, name, range_min, range_max)
static const unsigned codebook[256][2]
#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...
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
reference-counted frame API
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
#define LIBAVUTIL_VERSION_INT
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)
const uint8_t(* ff_h263_get_mv_penalty(void))[MAX_DMV *2+1]
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static int shift(int a, int b)
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
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.
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
#define QUALITY_THRESHOLD
static void init_block_index(MpegEncContext *const s)
static av_cold int svq1_encode_end(AVCodecContext *avctx)
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)
static const AVClass svq1enc_class
static av_cold int svq1_encode_init(AVCodecContext *avctx)
static av_cold int write_ident(AVCodecContext *avctx, const char *ident)
#define THRESHOLD_MULTIPLIER
static void svq1_write_header(SVQ1EncContext *s, PutBitContext *pb, int frame_type)
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)
static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Libavcodec version macros.
#define FFSWAP(type, a, b)
#define MKTAG(a, b, c, d)
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
void * av_calloc(size_t nmemb, size_t size)
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)
void ff_estimate_p_frame_motion(MPVEncContext *const s, int mb_x, int mb_y)
void ff_me_init_pic(MPVEncContext *const s)
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)
#define CANDIDATE_MB_TYPE_INTRA
#define CANDIDATE_MB_TYPE_INTER
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int put_bits_count(PutBitContext *s)
static int put_bytes_left(const PutBitContext *s, int round_up)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int put_bytes_output(const PutBitContext *s)
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
main external API structure.
int width
picture width / height.
int64_t frame_num
Frame counter, set by libavcodec.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
This structure stores compressed data.
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
uint32_t * mb_type
types and macros are defined in mpegutils.h
int16_t(*[2] motion_val)[2]
ptrdiff_t linesize[MPV_MAX_PLANES]
uint8_t * data[MPV_MAX_PLANES]
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11
int first_slice_line
used in MPEG-4 too to handle resync markers
int mb_height
number of MBs horizontally & vertically
MPVWorkPicture last_pic
copy of the previous picture structure.
int height
picture size. must be a multiple of 16
ptrdiff_t linesize
line size, in bytes, may be different from width
enum AVPictureType pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
MPVWorkPicture cur_pic
copy of the current picture structure.
int16_t(*[3] motion_val16)[2]
enum AVPictureType pict_type
PutBitContext reorder_pb[6]
SVQ1EncDSPContext svq1encdsp
AVFrame * current_picture
int16_t(*[3] motion_val8)[2]
int16_t encoded_block_levels[6][7][256]
const uint16_t ff_svq1_frame_size_table[7][2]
Sorenson Vector Quantizer #1 (SVQ1) video codec.
#define SVQ1_BLOCK_SKIP_CODE
const uint8_t ff_svq1_intra_multistage_vlc[6][8][2]
const uint8_t ff_svq1_inter_multistage_vlc[6][8][2]
#define SVQ1_BLOCK_INTRA_CODE
#define SVQ1_BLOCK_INTRA_LEN
#define SVQ1_BLOCK_INTER_LEN
const int8_t *const ff_svq1_intra_codebooks[6]
#define SVQ1_BLOCK_INTER_CODE
const uint16_t ff_svq1_inter_mean_vlc[512][2]
#define SVQ1_BLOCK_SKIP_LEN
const uint16_t ff_svq1_intra_mean_vlc[256][2]
FF_VISIBILITY_PUSH_HIDDEN const int8_t *const ff_svq1_inter_codebooks[6]
static const int8_t svq1_intra_codebook_sum[4][16 *6]
static const int8_t svq1_inter_codebook_sum[4][16 *6]
static void ff_svq1enc_init(SVQ1EncDSPContext *c)
#define av_malloc_array(a, b)
static int ref[MAX_W *MAX_W]
static float mean(const float *input, int size)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)