113 #define QUALITY_THRESHOLD 100
114 #define THRESHOLD_MULTIPLIER 0.6
117 int count, y, x, i, j, split, best_mean, best_score, best_count;
119 int block_sum[7]= {0, 0, 0, 0, 0, 0};
120 int w= 2<<((level+2)>>1);
121 int h= 2<<((level+1)>>1);
123 int16_t
block[7][256];
124 const int8_t *codebook_sum, *codebook;
125 const uint16_t (*mean_vlc)[2];
126 const uint8_t (*multistage_vlc)[2];
138 block[0][x + w*y]=
v;
151 block[0][x + w*y]=
v;
159 best_score -= (int)(((
unsigned)block_sum[0]*block_sum[0])>>(level+3));
160 best_mean= (block_sum[0] + (size>>1)) >> (level+3);
163 for(count=1; count<7; count++){
164 int best_vector_score= INT_MAX;
165 int best_vector_sum=-999, best_vector_mean=-999;
166 const int stage= count-1;
167 const int8_t *vector;
170 int sum= codebook_sum[stage*16 + i];
171 int sqr, diff, score;
173 vector = codebook + stage*size*16 + i*
size;
175 diff= block_sum[stage] - sum;
176 score= sqr - ((diff*(int64_t)diff)>>(level+3));
177 if(score < best_vector_score){
178 int mean= (diff + (size>>1)) >> (level+3);
179 assert(mean >-300 && mean<300);
180 mean= av_clip(mean, intra?0:-256, 255);
181 best_vector_score= score;
182 best_vector[stage]= i;
183 best_vector_sum= sum;
184 best_vector_mean= mean;
187 assert(best_vector_mean != -999);
188 vector= codebook + stage*size*16 + best_vector[stage]*
size;
189 for(j=0; j<
size; j++){
190 block[stage+1][j] = block[stage][j] - vector[j];
192 block_sum[stage+1]= block_sum[stage] - best_vector_sum;
194 lambda*(+ 1 + 4*count
195 + multistage_vlc[1+count][1]
196 + mean_vlc[best_vector_mean][1]);
198 if(best_vector_score < best_score){
199 best_score= best_vector_score;
201 best_mean= best_vector_mean;
207 if(best_score > threshold && level){
209 int offset= (level&1) ? stride*h/2 : w/2;
212 for(i=level-1; i>=0; i--){
215 score +=
encode_block(s, src , ref , decoded , stride, level-1, threshold>>1, lambda, intra);
216 score +=
encode_block(s, src + offset, ref + offset, decoded + offset, stride, level-1, threshold>>1, lambda, intra);
219 if(score < best_score){
223 for(i=level-1; i>=0; i--){
232 assert((best_mean >= 0 && best_mean<256) || !intra);
233 assert(best_mean >= -256 && best_mean<256);
234 assert(best_count >=0 && best_count<7);
235 assert(level<4 || best_count==0);
239 multistage_vlc[1 + best_count][1],
240 multistage_vlc[1 + best_count][0]);
242 mean_vlc[best_mean][0]);
244 for (i = 0; i < best_count; i++){
245 assert(best_vector[i]>=0 && best_vector[i]<16);
251 decoded[x + y*
stride]= src[x + y*
stride] - block[best_count][x + w*y] + best_mean;
265 int block_width, block_height;
273 for (level = 4; level >= 0; level--)
276 block_width = (width + 15) / 16;
277 block_height = (height + 15) / 16;
326 for (y = 0; y < block_height; y++) {
330 for(i=0; i<16 && i + 16*y<
height; i++){
331 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
332 for(x=width; x<16*block_width; x++)
333 src[i*stride+x]= src[i*stride+x-1];
335 for(; i<16 && i + 16*y<16*block_height; i++)
336 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
338 for (x = 0; x < block_width; x++) {
353 for (y = 0; y < block_height; y++) {
354 for(i=0; i<16 && i + 16*y<
height; i++){
355 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
356 for(x=width; x<16*block_width; x++)
357 src[i*stride+x]= src[i*stride+x-1];
359 for(; i<16 && i + 16*y<16*block_height; i++)
360 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
363 for (x = 0; x < block_width; x++) {
364 uint8_t reorder_buffer[3][6][7*32];
366 int offset = y * 16 * stride + x * 16;
367 uint8_t *decoded= decoded_plane + offset;
368 uint8_t *ref= ref_plane + offset;
369 int score[4]={0,0,0,0}, best;
388 score[0]= vlc[1]*lambda;
402 int mx, my, pred_x, pred_y, dxy;
415 assert(mx>=-32 && mx<=31);
416 assert(my>=-32 && my<=31);
417 assert(pred_x>=-32 && pred_x<=31);
418 assert(pred_y>=-32 && pred_y<=31);
424 dxy= (mx&1) + 2*(my&1);
428 score[1]+=
encode_block(s, src+16*x, temp+16, decoded, stride, 5, 64, lambda, 0);
429 best= score[1] <= score[0];
433 score[2]+= vlc[1]*lambda;
434 if(score[2] < score[best] && mx==0 && my==0){
450 motion_ptr[0 ] = motion_ptr[1 ]=
451 motion_ptr[2 ] = motion_ptr[3 ]=
502 int buf_size,
void *
data)
uint8_t * scratchpad
data area for the ME algo, so that the ME does not need to malloc/free
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
void ff_init_block_index(MpegEncContext *s)
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
int16_t(* p_mv_table)[2]
MV table (1MV per MB) p-frame encoding.
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
void ff_h263_encode_init(MpegEncContext *s)
AVFrame * coded_frame
the picture in the bitstream
uint16_t * mb_var
Table for MB variances.
static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, int level, int threshold, int lambda, int intra)
#define CANDIDATE_MB_TYPE_INTRA
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
uint32_t * score_map
map to store the scores
#define FF_ARRAY_ELEMS(a)
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
static const int8_t svq1_intra_codebook_sum[4][16 *6]
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static av_cold int svq1_encode_init(AVCodecContext *avctx)
static int svq1_encode_plane(SVQ1Context *s, int plane, unsigned char *src_plane, unsigned char *ref_plane, unsigned char *decoded_plane, int width, int height, int src_stride, int stride)
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
const int8_t *const ff_svq1_inter_codebooks[6]
const struct svq1_frame_size ff_svq1_frame_size_table[7]
Picture current_picture
copy of the current picture structure.
int mb_height
number of MBs horizontally & vertically
struct SVQ1Context SVQ1Context
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
static void ff_update_block_index(MpegEncContext *s)
static int init(AVCodecParserContext *s)
static void svq1_write_header(SVQ1Context *s, int frame_type)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define CANDIDATE_MB_TYPE_INTER
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
uint16_t * mb_type
Table for candidate MB types for encoding.
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Sorenson Vector Quantizer #1 (SVQ1) video codec.
static int put_bits_count(PutBitContext *s)
void ff_h263_encode_motion(MpegEncContext *s, int val, int f_code)
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
int me_method
ME algorithm.
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.
Picture * current_picture_ptr
pointer to the current picture
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
const uint8_t ff_svq1_inter_multistage_vlc[6][8][2]
static av_cold int svq1_encode_end(AVCodecContext *avctx)
uint32_t * mb_type
macroblock type table mb_type_base + mb_width + 2
int first_slice_line
used in mpeg4 too to handle resync markers
uint16_t * mc_mb_var
Table for motion compensated MB variances.
static int svq1_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
unsigned int lambda2
(lambda*lambda) >> FF_LAMBDA_SHIFT
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
main external API structure.
static void close(AVCodecParserContext *s)
int height
picture size. must be a multiple of 16
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int16_t(*[2] motion_val)[2]
motion vector table
void ff_fix_long_p_mvs(MpegEncContext *s)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
DSPContext dsp
pointers for accelerated dsp functions
int16_t(*[3] motion_val16)[2]
int f_code
forward MV resolution
int(* ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2, int size)
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
int ff_init_me(MpegEncContext *s)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int16_t(*[3] motion_val8)[2]
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
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 ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
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]
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
int dia_size
ME diamond size & shape.
int key_frame
1 -> keyframe, 0-> not
int linesize
line size, in bytes, may be different from width
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)
PutBitContext reorder_pb[6]
int frame_number
audio or video frame number
int flags
AVCodecContext.flags (HQ, MV4, ...)
#define QUALITY_THRESHOLD
int me_method
Motion estimation algorithm used for video coding.
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 multipler used in rate distortion