2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
24 * The simplest mpeg encoder (well, it was the simplest!).
31 #include "mpegvideo.h"
34 #include "fastmemcpy.h"
40 #ifdef CONFIG_ENCODERS
41 static void encode_picture(MpegEncContext *s, int picture_number);
42 #endif //CONFIG_ENCODERS
43 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
44 DCTELEM *block, int n, int qscale);
45 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
46 DCTELEM *block, int n, int qscale);
47 static void dct_unquantize_h263_c(MpegEncContext *s,
48 DCTELEM *block, int n, int qscale);
49 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
50 #ifdef CONFIG_ENCODERS
51 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
52 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
53 static int sse_mb(MpegEncContext *s);
54 #endif //CONFIG_ENCODERS
57 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
58 extern void XVMC_field_end(MpegEncContext *s);
59 extern void XVMC_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
62 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
65 /* enable all paranoid tests for rounding, overflows, etc... */
71 /* for jpeg fast DCT */
74 static const uint16_t aanscales[64] = {
75 /* precomputed values scaled up by 14 bits */
76 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
77 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
78 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
79 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
80 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
81 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
82 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
83 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
86 static const uint8_t h263_chroma_roundtab[16] = {
87 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
88 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
91 #ifdef CONFIG_ENCODERS
92 static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
93 static uint8_t default_fcode_tab[MAX_MV*2+1];
95 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
97 static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
98 const uint16_t *quant_matrix, int bias, int qmin, int qmax)
102 for(qscale=qmin; qscale<=qmax; qscale++){
104 if (s->dsp.fdct == ff_jpeg_fdct_islow) {
106 const int j= s->dsp.idct_permutation[i];
107 /* 16 <= qscale * quant_matrix[i] <= 7905 */
108 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
109 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
110 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
112 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
113 (qscale * quant_matrix[j]));
115 } else if (s->dsp.fdct == fdct_ifast) {
117 const int j= s->dsp.idct_permutation[i];
118 /* 16 <= qscale * quant_matrix[i] <= 7905 */
119 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
120 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
121 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
123 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
124 (aanscales[i] * qscale * quant_matrix[j]));
128 const int j= s->dsp.idct_permutation[i];
129 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
130 So 16 <= qscale * quant_matrix[i] <= 7905
131 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
132 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
134 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
135 // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
136 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
138 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
139 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
144 #endif //CONFIG_ENCODERS
146 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
150 st->scantable= src_scantable;
154 j = src_scantable[i];
155 st->permutated[i] = permutation[j];
164 j = st->permutated[i];
166 st->raster_end[i]= end;
170 /* init common dct for both encoder and decoder */
171 int DCT_common_init(MpegEncContext *s)
173 s->dct_unquantize_h263 = dct_unquantize_h263_c;
174 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
175 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
177 #ifdef CONFIG_ENCODERS
178 s->dct_quantize= dct_quantize_c;
182 MPV_common_init_mmx(s);
185 MPV_common_init_axp(s);
188 MPV_common_init_mlib(s);
191 MPV_common_init_mmi(s);
194 MPV_common_init_armv4l(s);
197 MPV_common_init_ppc(s);
200 #ifdef CONFIG_ENCODERS
201 s->fast_dct_quantize= s->dct_quantize;
203 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
204 s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
207 #endif //CONFIG_ENCODERS
209 /* load & permutate scantables
210 note: only wmv uses differnt ones
212 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
213 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
214 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
215 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
217 s->picture_structure= PICT_FRAME;
223 * allocates a Picture
224 * The pixels are allocated/set by calling get_buffer() if shared=0
226 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
227 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
228 const int mb_array_size= s->mb_stride*s->mb_height;
232 assert(pic->data[0]);
233 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
234 pic->type= FF_BUFFER_TYPE_SHARED;
238 assert(!pic->data[0]);
240 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
242 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
243 fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
247 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
248 fprintf(stderr, "get_buffer() failed (stride changed)\n");
252 if(pic->linesize[1] != pic->linesize[2]){
253 fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
257 s->linesize = pic->linesize[0];
258 s->uvlinesize= pic->linesize[1];
261 if(pic->qscale_table==NULL){
263 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
264 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
265 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
266 CHECKED_ALLOCZ(pic->mb_cmp_score, mb_array_size * sizeof(int32_t))
269 CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
270 CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
271 CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(int))
272 pic->mb_type= pic->mb_type_base + s->mb_stride+1;
273 if(s->out_format == FMT_H264){
275 CHECKED_ALLOCZ(pic->motion_val[i], 2 * 16 * s->mb_num * sizeof(uint16_t))
276 CHECKED_ALLOCZ(pic->ref_index[i] , 4 * s->mb_num * sizeof(uint8_t))
279 pic->qstride= s->mb_stride;
282 //it might be nicer if the application would keep track of these but it would require a API change
283 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
284 s->prev_pict_types[0]= s->pict_type;
285 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
286 pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
289 fail: //for the CHECKED_ALLOCZ macro
294 * deallocates a picture
296 static void free_picture(MpegEncContext *s, Picture *pic){
299 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
300 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
303 av_freep(&pic->mb_var);
304 av_freep(&pic->mc_mb_var);
305 av_freep(&pic->mb_mean);
306 av_freep(&pic->mb_cmp_score);
307 av_freep(&pic->mbskip_table);
308 av_freep(&pic->qscale_table);
309 av_freep(&pic->mb_type_base);
312 av_freep(&pic->motion_val[i]);
313 av_freep(&pic->ref_index[i]);
316 if(pic->type == FF_BUFFER_TYPE_SHARED){
325 /* init common structure for both encoder and decoder */
326 int MPV_common_init(MpegEncContext *s)
328 int y_size, c_size, yc_size, i, mb_array_size, x, y;
330 dsputil_init(&s->dsp, s->avctx);
333 s->flags= s->avctx->flags;
335 s->mb_width = (s->width + 15) / 16;
336 s->mb_height = (s->height + 15) / 16;
337 s->mb_stride = s->mb_width + 1;
338 mb_array_size= s->mb_height * s->mb_stride;
340 /* set default edge pos, will be overriden in decode_header if needed */
341 s->h_edge_pos= s->mb_width*16;
342 s->v_edge_pos= s->mb_height*16;
344 s->mb_num = s->mb_width * s->mb_height;
349 s->block_wrap[3]= s->mb_width*2 + 2;
351 s->block_wrap[5]= s->mb_width + 2;
353 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
354 c_size = (s->mb_width + 2) * (s->mb_height + 2);
355 yc_size = y_size + 2 * c_size;
357 /* convert fourcc to upper case */
358 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
359 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
360 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
361 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
363 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
364 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
366 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
368 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
369 for(y=0; y<s->mb_height; y++){
370 for(x=0; x<s->mb_width; x++){
371 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
374 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
377 int mv_table_size= s->mb_stride * (s->mb_height+2) + 1;
379 /* Allocate MV tables */
380 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
381 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
382 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
383 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
384 CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
385 CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
386 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
387 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
388 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
389 s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
390 s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
391 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
393 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
394 CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
396 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
397 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
399 if(s->codec_id==CODEC_ID_MPEG4){
400 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
401 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
404 if(s->msmpeg4_version){
405 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
407 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
409 /* Allocate MB type table */
410 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint8_t)) //needed for encoding
413 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
415 if (s->out_format == FMT_H263 || s->encoding) {
419 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
420 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
423 if(s->codec_id==CODEC_ID_MPEG4){
424 /* interlaced direct mode decoding tables */
425 CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
426 CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
428 if (s->out_format == FMT_H263) {
430 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
431 s->ac_val[1] = s->ac_val[0] + y_size;
432 s->ac_val[2] = s->ac_val[1] + c_size;
435 CHECKED_ALLOCZ(s->coded_block, y_size);
437 /* divx501 bitstream reorder buffer */
438 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
440 /* cbp, ac_pred, pred_dir */
441 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
442 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
445 if (s->h263_pred || s->h263_plus || !s->encoding) {
447 //MN: we need these for error resilience of intra-frames
448 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
449 s->dc_val[1] = s->dc_val[0] + y_size;
450 s->dc_val[2] = s->dc_val[1] + c_size;
451 for(i=0;i<yc_size;i++)
452 s->dc_val[0][i] = 1024;
455 /* which mb is a intra block */
456 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
457 memset(s->mbintra_table, 1, mb_array_size);
459 /* default structure is frame */
460 s->picture_structure = PICT_FRAME;
462 /* init macroblock skip table */
463 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
464 //Note the +1 is for a quicker mpeg4 slice_end detection
465 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
467 s->block= s->blocks[0];
469 s->parse_context.state= -1;
471 s->context_initialized = 1;
481 /* init common structure for both encoder and decoder */
482 void MPV_common_end(MpegEncContext *s)
486 av_freep(&s->parse_context.buffer);
487 s->parse_context.buffer_size=0;
489 av_freep(&s->mb_type);
490 av_freep(&s->p_mv_table_base);
491 av_freep(&s->b_forw_mv_table_base);
492 av_freep(&s->b_back_mv_table_base);
493 av_freep(&s->b_bidir_forw_mv_table_base);
494 av_freep(&s->b_bidir_back_mv_table_base);
495 av_freep(&s->b_direct_mv_table_base);
497 s->b_forw_mv_table= NULL;
498 s->b_back_mv_table= NULL;
499 s->b_bidir_forw_mv_table= NULL;
500 s->b_bidir_back_mv_table= NULL;
501 s->b_direct_mv_table= NULL;
503 av_freep(&s->motion_val);
504 av_freep(&s->dc_val[0]);
505 av_freep(&s->ac_val[0]);
506 av_freep(&s->coded_block);
507 av_freep(&s->mbintra_table);
508 av_freep(&s->cbp_table);
509 av_freep(&s->pred_dir_table);
510 av_freep(&s->me.scratchpad);
511 av_freep(&s->me.map);
512 av_freep(&s->me.score_map);
514 av_freep(&s->mbskip_table);
515 av_freep(&s->prev_pict_types);
516 av_freep(&s->bitstream_buffer);
517 av_freep(&s->tex_pb_buffer);
518 av_freep(&s->pb2_buffer);
519 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
520 av_freep(&s->field_mv_table);
521 av_freep(&s->field_select_table);
522 av_freep(&s->avctx->stats_out);
523 av_freep(&s->ac_stats);
524 av_freep(&s->error_status_table);
525 av_freep(&s->mb_index2xy);
527 for(i=0; i<MAX_PICTURE_COUNT; i++){
528 free_picture(s, &s->picture[i]);
530 avcodec_default_free_buffers(s->avctx);
531 s->context_initialized = 0;
534 #ifdef CONFIG_ENCODERS
536 /* init video encoder */
537 int MPV_encode_init(AVCodecContext *avctx)
539 MpegEncContext *s = avctx->priv_data;
541 int chroma_h_shift, chroma_v_shift;
543 avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
545 s->bit_rate = avctx->bit_rate;
546 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
547 s->width = avctx->width;
548 s->height = avctx->height;
549 if(avctx->gop_size > 600){
550 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
553 s->gop_size = avctx->gop_size;
554 s->rtp_mode = avctx->rtp_mode;
555 s->rtp_payload_size = avctx->rtp_payload_size;
556 if (avctx->rtp_callback)
557 s->rtp_callback = avctx->rtp_callback;
558 s->max_qdiff= avctx->max_qdiff;
559 s->qcompress= avctx->qcompress;
560 s->qblur= avctx->qblur;
562 s->flags= avctx->flags;
563 s->max_b_frames= avctx->max_b_frames;
564 s->b_frame_strategy= avctx->b_frame_strategy;
565 s->codec_id= avctx->codec->id;
566 s->luma_elim_threshold = avctx->luma_elim_threshold;
567 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
568 s->strict_std_compliance= avctx->strict_std_compliance;
569 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
570 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
571 s->mpeg_quant= avctx->mpeg_quant;
573 if (s->gop_size <= 1) {
580 s->me_method = avctx->me_method;
583 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
585 s->adaptive_quant= ( s->avctx->lumi_masking
586 || s->avctx->dark_masking
587 || s->avctx->temporal_cplx_masking
588 || s->avctx->spatial_cplx_masking
589 || s->avctx->p_masking)
592 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
594 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4){
595 fprintf(stderr, "4MV not supporetd by codec\n");
599 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
600 fprintf(stderr, "qpel not supporetd by codec\n");
604 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
605 fprintf(stderr, "data partitioning not supporetd by codec\n");
609 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO){
610 fprintf(stderr, "b frames not supporetd by codec\n");
614 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
615 fprintf(stderr, "mpeg2 style quantization not supporetd by codec\n");
619 if(s->codec_id==CODEC_ID_MJPEG){
620 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
621 s->inter_quant_bias= 0;
622 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO){
623 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
624 s->inter_quant_bias= 0;
626 s->intra_quant_bias=0;
627 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
630 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
631 s->intra_quant_bias= avctx->intra_quant_bias;
632 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
633 s->inter_quant_bias= avctx->inter_quant_bias;
635 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
637 switch(avctx->codec->id) {
638 case CODEC_ID_MPEG1VIDEO:
639 s->out_format = FMT_MPEG1;
640 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
641 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
645 s->out_format = FMT_MJPEG;
646 s->intra_only = 1; /* force intra only for jpeg */
647 s->mjpeg_write_tables = 1; /* write all tables */
648 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
649 s->mjpeg_vsample[0] = 1<<chroma_v_shift;
650 s->mjpeg_vsample[1] = 1;
651 s->mjpeg_vsample[2] = 1;
652 s->mjpeg_hsample[0] = 1<<chroma_h_shift;
653 s->mjpeg_hsample[1] = 1;
654 s->mjpeg_hsample[2] = 1;
655 if (mjpeg_init(s) < 0)
662 if (h263_get_picture_format(s->width, s->height) == 7) {
663 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
666 s->out_format = FMT_H263;
671 s->out_format = FMT_H263;
674 s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
675 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
677 /* These are just to be sure */
683 s->out_format = FMT_H263;
684 s->h263_flv = 2; /* format = 1; 11-bit codes */
685 s->unrestricted_mv = 1;
686 s->rtp_mode=0; /* don't allow GOB */
691 s->out_format = FMT_H263;
697 s->out_format = FMT_H263;
699 s->unrestricted_mv = 1;
700 s->low_delay= s->max_b_frames ? 0 : 1;
701 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
703 case CODEC_ID_MSMPEG4V1:
704 s->out_format = FMT_H263;
707 s->unrestricted_mv = 1;
708 s->msmpeg4_version= 1;
712 case CODEC_ID_MSMPEG4V2:
713 s->out_format = FMT_H263;
716 s->unrestricted_mv = 1;
717 s->msmpeg4_version= 2;
721 case CODEC_ID_MSMPEG4V3:
722 s->out_format = FMT_H263;
725 s->unrestricted_mv = 1;
726 s->msmpeg4_version= 3;
727 s->flipflop_rounding=1;
732 s->out_format = FMT_H263;
735 s->unrestricted_mv = 1;
736 s->msmpeg4_version= 4;
737 s->flipflop_rounding=1;
742 s->out_format = FMT_H263;
745 s->unrestricted_mv = 1;
746 s->msmpeg4_version= 5;
747 s->flipflop_rounding=1;
756 { /* set up some save defaults, some codecs might override them later */
762 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
763 memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
764 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
766 for(i=-16; i<16; i++){
767 default_fcode_tab[i + MAX_MV]= 1;
771 s->me.mv_penalty= default_mv_penalty;
772 s->fcode_tab= default_fcode_tab;
774 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
776 /* dont use mv_penalty table for crap MV as it would be confused */
777 //FIXME remove after fixing / removing old ME
778 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
783 if (MPV_common_init(s) < 0)
788 #ifdef CONFIG_ENCODERS
790 if (s->out_format == FMT_H263)
792 if(s->msmpeg4_version)
793 ff_msmpeg4_encode_init(s);
795 if (s->out_format == FMT_MPEG1)
796 ff_mpeg1_encode_init(s);
799 /* init default q matrix */
801 int j= s->dsp.idct_permutation[i];
803 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
804 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
805 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
806 }else if(s->out_format == FMT_H263){
808 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
812 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
813 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
817 /* precompute matrix */
818 /* for mjpeg, we do include qscale in the matrix */
819 if (s->out_format != FMT_MJPEG) {
820 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
821 s->intra_matrix, s->intra_quant_bias, 1, 31);
822 convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
823 s->inter_matrix, s->inter_quant_bias, 1, 31);
826 if(ff_rate_control_init(s) < 0)
829 s->picture_number = 0;
830 s->picture_in_gop_number = 0;
831 s->fake_picture_number = 0;
832 /* motion detector init */
839 int MPV_encode_end(AVCodecContext *avctx)
841 MpegEncContext *s = avctx->priv_data;
847 ff_rate_control_uninit(s);
850 if (s->out_format == FMT_MJPEG)
856 #endif //CONFIG_ENCODERS
858 void init_rl(RLTable *rl)
860 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
861 uint8_t index_run[MAX_RUN+1];
862 int last, run, level, start, end, i;
864 /* compute max_level[], max_run[] and index_run[] */
865 for(last=0;last<2;last++) {
874 memset(max_level, 0, MAX_RUN + 1);
875 memset(max_run, 0, MAX_LEVEL + 1);
876 memset(index_run, rl->n, MAX_RUN + 1);
877 for(i=start;i<end;i++) {
878 run = rl->table_run[i];
879 level = rl->table_level[i];
880 if (index_run[run] == rl->n)
882 if (level > max_level[run])
883 max_level[run] = level;
884 if (run > max_run[level])
885 max_run[level] = run;
887 rl->max_level[last] = av_malloc(MAX_RUN + 1);
888 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
889 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
890 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
891 rl->index_run[last] = av_malloc(MAX_RUN + 1);
892 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
896 /* draw the edges of width 'w' of an image of size width, height */
897 //FIXME check that this is ok for mpeg4 interlaced
898 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
900 uint8_t *ptr, *last_line;
903 last_line = buf + (height - 1) * wrap;
906 memcpy(buf - (i + 1) * wrap, buf, width);
907 memcpy(last_line + (i + 1) * wrap, last_line, width);
911 for(i=0;i<height;i++) {
912 memset(ptr - w, ptr[0], w);
913 memset(ptr + width, ptr[width-1], w);
918 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
919 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
920 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
921 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
925 static int find_unused_picture(MpegEncContext *s, int shared){
929 for(i=0; i<MAX_PICTURE_COUNT; i++){
930 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
933 for(i=0; i<MAX_PICTURE_COUNT; i++){
934 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break; //FIXME
936 for(i=0; i<MAX_PICTURE_COUNT; i++){
937 if(s->picture[i].data[0]==NULL) break;
941 assert(i<MAX_PICTURE_COUNT);
945 /* generic function for encode/decode called before a frame is coded/decoded */
946 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
953 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
955 /* mark&release old frames */
956 if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
957 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
959 /* release forgotten pictures */
960 /* if(mpeg124/h263) */
962 for(i=0; i<MAX_PICTURE_COUNT; i++){
963 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
964 fprintf(stderr, "releasing zombie picture\n");
965 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
972 /* release non refernce frames */
973 for(i=0; i<MAX_PICTURE_COUNT; i++){
974 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
975 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
979 i= find_unused_picture(s, 0);
981 pic= (AVFrame*)&s->picture[i];
982 pic->reference= s->pict_type != B_TYPE ? 3 : 0;
984 if(s->current_picture_ptr)
985 pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
987 if( alloc_picture(s, (Picture*)pic, 0) < 0)
990 s->current_picture_ptr= &s->picture[i];
993 s->current_picture_ptr->pict_type= s->pict_type;
994 s->current_picture_ptr->quality= s->qscale;
995 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
997 s->current_picture= *s->current_picture_ptr;
999 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1000 if (s->pict_type != B_TYPE) {
1001 s->last_picture_ptr= s->next_picture_ptr;
1002 s->next_picture_ptr= s->current_picture_ptr;
1005 if(s->last_picture_ptr) s->last_picture= *s->last_picture_ptr;
1006 if(s->next_picture_ptr) s->next_picture= *s->next_picture_ptr;
1007 if(s->new_picture_ptr ) s->new_picture = *s->new_picture_ptr;
1009 if(s->pict_type != I_TYPE && s->last_picture_ptr==NULL){
1010 fprintf(stderr, "warning: first frame is no keyframe\n");
1011 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1015 assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
1017 if(s->picture_structure!=PICT_FRAME){
1020 if(s->picture_structure == PICT_BOTTOM_FIELD){
1021 s->current_picture.data[i] += s->current_picture.linesize[i];
1023 s->current_picture.linesize[i] *= 2;
1024 s->last_picture.linesize[i] *=2;
1025 s->next_picture.linesize[i] *=2;
1030 s->hurry_up= s->avctx->hurry_up;
1031 s->error_resilience= avctx->error_resilience;
1033 /* set dequantizer, we cant do it during init as it might change for mpeg4
1034 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1035 if(s->out_format == FMT_H263){
1037 s->dct_unquantize = s->dct_unquantize_mpeg2;
1039 s->dct_unquantize = s->dct_unquantize_h263;
1041 s->dct_unquantize = s->dct_unquantize_mpeg1;
1044 if(s->avctx->xvmc_acceleration)
1045 return XVMC_field_start(s, avctx);
1050 /* generic function for encode/decode called after a frame has been coded/decoded */
1051 void MPV_frame_end(MpegEncContext *s)
1054 /* draw edge for correct motion prediction if outside */
1056 //just to make sure that all data is rendered.
1057 if(s->avctx->xvmc_acceleration){
1061 if(s->codec_id!=CODEC_ID_SVQ1 && s->codec_id != CODEC_ID_MPEG1VIDEO){
1062 if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1063 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
1064 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1065 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1070 s->last_pict_type = s->pict_type;
1071 if(s->pict_type!=B_TYPE){
1072 s->last_non_b_pict_type= s->pict_type;
1075 /* copy back current_picture variables */
1076 for(i=0; i<MAX_PICTURE_COUNT; i++){
1077 if(s->picture[i].data[0] == s->current_picture.data[0]){
1078 s->picture[i]= s->current_picture;
1082 assert(i<MAX_PICTURE_COUNT);
1086 /* release non refernce frames */
1087 for(i=0; i<MAX_PICTURE_COUNT; i++){
1088 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1089 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1093 // clear copies, to avoid confusion
1095 memset(&s->last_picture, 0, sizeof(Picture));
1096 memset(&s->next_picture, 0, sizeof(Picture));
1097 memset(&s->current_picture, 0, sizeof(Picture));
1102 * draws an line from (ex, ey) -> (sx, sy).
1103 * @param w width of the image
1104 * @param h height of the image
1105 * @param stride stride/linesize of the image
1106 * @param color color of the arrow
1108 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1111 sx= clip(sx, 0, w-1);
1112 sy= clip(sy, 0, h-1);
1113 ex= clip(ex, 0, w-1);
1114 ey= clip(ey, 0, h-1);
1116 buf[sy*stride + sx]+= color;
1118 if(ABS(ex - sx) > ABS(ey - sy)){
1123 buf+= sx + sy*stride;
1125 f= ((ey-sy)<<16)/ex;
1126 for(x= 0; x <= ex; x++){
1127 y= ((x*f) + (1<<15))>>16;
1128 buf[y*stride + x]+= color;
1135 buf+= sx + sy*stride;
1137 if(ey) f= ((ex-sx)<<16)/ey;
1139 for(y= 0; y <= ey; y++){
1140 x= ((y*f) + (1<<15))>>16;
1141 buf[y*stride + x]+= color;
1147 * draws an arrow from (ex, ey) -> (sx, sy).
1148 * @param w width of the image
1149 * @param h height of the image
1150 * @param stride stride/linesize of the image
1151 * @param color color of the arrow
1153 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1156 sx= clip(sx, -100, w+100);
1157 sy= clip(sy, -100, h+100);
1158 ex= clip(ex, -100, w+100);
1159 ey= clip(ey, -100, h+100);
1164 if(dx*dx + dy*dy > 3*3){
1167 int length= ff_sqrt((rx*rx + ry*ry)<<8);
1169 //FIXME subpixel accuracy
1170 rx= ROUNDED_DIV(rx*3<<4, length);
1171 ry= ROUNDED_DIV(ry*3<<4, length);
1173 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1174 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1176 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1180 * prints debuging info for the given picture.
1182 void ff_print_debug_info(MpegEncContext *s, Picture *pict){
1184 if(!pict || !pict->mb_type) return;
1186 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1189 for(y=0; y<s->mb_height; y++){
1190 for(x=0; x<s->mb_width; x++){
1191 if(s->avctx->debug&FF_DEBUG_SKIP){
1192 int count= s->mbskip_table[x + y*s->mb_stride];
1193 if(count>9) count=9;
1194 printf("%1d", count);
1196 if(s->avctx->debug&FF_DEBUG_QP){
1197 printf("%2d", pict->qscale_table[x + y*s->mb_stride]);
1199 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1200 int mb_type= pict->mb_type[x + y*s->mb_stride];
1202 //Type & MV direction
1205 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1207 else if(IS_INTRA4x4(mb_type))
1209 else if(IS_INTRA16x16(mb_type))
1211 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1213 else if(IS_DIRECT(mb_type))
1215 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1217 else if(IS_GMC(mb_type))
1219 else if(IS_SKIP(mb_type))
1221 else if(!USES_LIST(mb_type, 1))
1223 else if(!USES_LIST(mb_type, 0))
1226 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1233 else if(IS_16X8(mb_type))
1235 else if(IS_8X16(mb_type))
1237 else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
1243 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
1254 if((s->avctx->debug&FF_DEBUG_VIS_MV) && s->motion_val){
1255 const int shift= 1 + s->quarter_sample;
1257 uint8_t *ptr= pict->data[0];
1258 s->low_delay=0; //needed to see the vectors without trashing the buffers
1260 for(mb_y=0; mb_y<s->mb_height; mb_y++){
1262 for(mb_x=0; mb_x<s->mb_width; mb_x++){
1263 const int mb_index= mb_x + mb_y*s->mb_stride;
1264 if(IS_8X8(s->current_picture.mb_type[mb_index])){
1267 int sx= mb_x*16 + 4 + 8*(i&1);
1268 int sy= mb_y*16 + 4 + 8*(i>>1);
1269 int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
1270 int mx= (s->motion_val[xy][0]>>shift) + sx;
1271 int my= (s->motion_val[xy][1]>>shift) + sy;
1272 draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1275 int sx= mb_x*16 + 8;
1276 int sy= mb_y*16 + 8;
1277 int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
1278 int mx= (s->motion_val[xy][0]>>shift) + sx;
1279 int my= (s->motion_val[xy][1]>>shift) + sy;
1280 draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1282 s->mbskip_table[mb_index]=0;
1288 #ifdef CONFIG_ENCODERS
1290 static int get_sae(uint8_t *src, int ref, int stride){
1294 for(y=0; y<16; y++){
1295 for(x=0; x<16; x++){
1296 acc+= ABS(src[x+y*stride] - ref);
1303 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1310 for(y=0; y<h; y+=16){
1311 for(x=0; x<w; x+=16){
1312 int offset= x + y*stride;
1313 int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1314 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1315 int sae = get_sae(src + offset, mean, stride);
1317 acc+= sae + 500 < sad;
1324 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1327 const int encoding_delay= s->max_b_frames;
1331 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1332 if(pic_arg->linesize[0] != s->linesize) direct=0;
1333 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1334 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1336 // printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1339 i= find_unused_picture(s, 1);
1341 pic= (AVFrame*)&s->picture[i];
1345 pic->data[i]= pic_arg->data[i];
1346 pic->linesize[i]= pic_arg->linesize[i];
1348 alloc_picture(s, (Picture*)pic, 1);
1350 i= find_unused_picture(s, 0);
1352 pic= (AVFrame*)&s->picture[i];
1355 alloc_picture(s, (Picture*)pic, 0);
1357 /* the input will be 16 pixels to the right relative to the actual buffer start
1358 * and the current_pic, so the buffer can be reused, yes its not beatifull
1363 if( pic->data[0] == pic_arg->data[0]
1364 && pic->data[1] == pic_arg->data[1]
1365 && pic->data[2] == pic_arg->data[2]){
1368 int h_chroma_shift, v_chroma_shift;
1369 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1372 int src_stride= pic_arg->linesize[i];
1373 int dst_stride= i ? s->uvlinesize : s->linesize;
1374 int h_shift= i ? h_chroma_shift : 0;
1375 int v_shift= i ? v_chroma_shift : 0;
1376 int w= s->width >>h_shift;
1377 int h= s->height>>v_shift;
1378 uint8_t *src= pic_arg->data[i];
1379 uint8_t *dst= pic->data[i];
1381 if(src_stride==dst_stride)
1382 memcpy(dst, src, src_stride*h);
1385 memcpy(dst, src, w);
1393 pic->quality= pic_arg->quality;
1394 pic->pict_type= pic_arg->pict_type;
1395 pic->pts = pic_arg->pts;
1397 if(s->input_picture[encoding_delay])
1398 pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1402 /* shift buffer entries */
1403 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1404 s->input_picture[i-1]= s->input_picture[i];
1406 s->input_picture[encoding_delay]= (Picture*)pic;
1411 static void select_input_picture(MpegEncContext *s){
1413 const int encoding_delay= s->max_b_frames;
1414 int coded_pic_num=0;
1416 if(s->reordered_input_picture[0])
1417 coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1419 for(i=1; i<MAX_PICTURE_COUNT; i++)
1420 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1421 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1423 /* set next picture types & ordering */
1424 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1425 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1426 s->reordered_input_picture[0]= s->input_picture[0];
1427 s->reordered_input_picture[0]->pict_type= I_TYPE;
1428 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1432 if(s->flags&CODEC_FLAG_PASS2){
1433 for(i=0; i<s->max_b_frames+1; i++){
1434 int pict_num= s->input_picture[0]->display_picture_number + i;
1435 int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1436 s->input_picture[i]->pict_type= pict_type;
1438 if(i + 1 >= s->rc_context.num_entries) break;
1442 if(s->input_picture[0]->pict_type){
1443 /* user selected pict_type */
1444 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1445 if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1448 if(b_frames > s->max_b_frames){
1449 fprintf(stderr, "warning, too many bframes in a row\n");
1450 b_frames = s->max_b_frames;
1452 }else if(s->b_frame_strategy==0){
1453 b_frames= s->max_b_frames;
1454 while(b_frames && !s->input_picture[b_frames]) b_frames--;
1455 }else if(s->b_frame_strategy==1){
1456 for(i=1; i<s->max_b_frames+1; i++){
1457 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1458 s->input_picture[i]->b_frame_score=
1459 get_intra_count(s, s->input_picture[i ]->data[0],
1460 s->input_picture[i-1]->data[0], s->linesize) + 1;
1463 for(i=0; i<s->max_b_frames; i++){
1464 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1467 b_frames= FFMAX(0, i-1);
1470 for(i=0; i<b_frames+1; i++){
1471 s->input_picture[i]->b_frame_score=0;
1474 fprintf(stderr, "illegal b frame strategy\n");
1479 //static int b_count=0;
1480 //b_count+= b_frames;
1481 //printf("b_frames: %d\n", b_count);
1483 s->reordered_input_picture[0]= s->input_picture[b_frames];
1484 if( s->picture_in_gop_number + b_frames >= s->gop_size
1485 || s->reordered_input_picture[0]->pict_type== I_TYPE)
1486 s->reordered_input_picture[0]->pict_type= I_TYPE;
1488 s->reordered_input_picture[0]->pict_type= P_TYPE;
1489 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1490 for(i=0; i<b_frames; i++){
1492 s->reordered_input_picture[i+1]= s->input_picture[i];
1493 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1494 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1499 if(s->reordered_input_picture[0]){
1500 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1502 s->new_picture= *s->reordered_input_picture[0];
1504 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1505 // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1507 int i= find_unused_picture(s, 0);
1508 Picture *pic= &s->picture[i];
1510 /* mark us unused / free shared pic */
1512 s->reordered_input_picture[0]->data[i]= NULL;
1513 s->reordered_input_picture[0]->type= 0;
1515 //FIXME bad, copy * except
1516 pic->pict_type = s->reordered_input_picture[0]->pict_type;
1517 pic->quality = s->reordered_input_picture[0]->quality;
1518 pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1519 pic->reference = s->reordered_input_picture[0]->reference;
1520 pic->pts = s->reordered_input_picture[0]->pts;
1522 alloc_picture(s, pic, 0);
1524 s->current_picture_ptr= pic;
1526 // input is not a shared pix -> reuse buffer for current_pix
1528 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1529 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1531 s->current_picture_ptr= s->reordered_input_picture[0];
1533 //reverse the +16 we did before storing the input
1534 s->current_picture_ptr->data[i]-=16;
1537 s->current_picture= *s->current_picture_ptr;
1539 s->picture_number= s->new_picture.display_picture_number;
1540 //printf("dpn:%d\n", s->picture_number);
1542 memset(&s->new_picture, 0, sizeof(Picture));
1546 int MPV_encode_picture(AVCodecContext *avctx,
1547 unsigned char *buf, int buf_size, void *data)
1549 MpegEncContext *s = avctx->priv_data;
1550 AVFrame *pic_arg = data;
1553 if(avctx->pix_fmt != PIX_FMT_YUV420P){
1554 fprintf(stderr, "this codec supports only YUV420P\n");
1558 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1560 s->picture_in_gop_number++;
1562 load_input_picture(s, pic_arg);
1564 select_input_picture(s);
1567 if(s->new_picture.data[0]){
1569 s->pict_type= s->new_picture.pict_type;
1570 if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1571 s->qscale= (int)(s->new_picture.quality+0.5);
1575 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1576 MPV_frame_start(s, avctx);
1578 encode_picture(s, s->picture_number);
1580 avctx->real_pict_num = s->picture_number;
1581 avctx->header_bits = s->header_bits;
1582 avctx->mv_bits = s->mv_bits;
1583 avctx->misc_bits = s->misc_bits;
1584 avctx->i_tex_bits = s->i_tex_bits;
1585 avctx->p_tex_bits = s->p_tex_bits;
1586 avctx->i_count = s->i_count;
1587 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1588 avctx->skip_count = s->skip_count;
1592 if (s->out_format == FMT_MJPEG)
1593 mjpeg_picture_trailer(s);
1595 if(s->flags&CODEC_FLAG_PASS1)
1596 ff_write_pass1_stats(s);
1599 avctx->error[i] += s->current_picture_ptr->error[i];
1603 s->input_picture_number++;
1605 flush_put_bits(&s->pb);
1606 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1608 s->total_bits += s->frame_bits;
1609 avctx->frame_bits = s->frame_bits;
1611 return pbBufPtr(&s->pb) - s->pb.buf;
1614 #endif //CONFIG_ENCODERS
1616 static inline void gmc1_motion(MpegEncContext *s,
1617 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1619 uint8_t **ref_picture, int src_offset)
1622 int offset, src_x, src_y, linesize, uvlinesize;
1623 int motion_x, motion_y;
1626 motion_x= s->sprite_offset[0][0];
1627 motion_y= s->sprite_offset[0][1];
1628 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1629 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1630 motion_x<<=(3-s->sprite_warping_accuracy);
1631 motion_y<<=(3-s->sprite_warping_accuracy);
1632 src_x = clip(src_x, -16, s->width);
1633 if (src_x == s->width)
1635 src_y = clip(src_y, -16, s->height);
1636 if (src_y == s->height)
1639 linesize = s->linesize;
1640 uvlinesize = s->uvlinesize;
1642 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1644 dest_y+=dest_offset;
1645 if(s->flags&CODEC_FLAG_EMU_EDGE){
1646 if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
1647 || src_y + 17 >= s->v_edge_pos){
1648 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1649 ptr= s->edge_emu_buffer;
1653 if((motion_x|motion_y)&7){
1654 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1655 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1659 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1660 if (s->no_rounding){
1661 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1663 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
1667 if(s->flags&CODEC_FLAG_GRAY) return;
1669 motion_x= s->sprite_offset[1][0];
1670 motion_y= s->sprite_offset[1][1];
1671 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1672 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1673 motion_x<<=(3-s->sprite_warping_accuracy);
1674 motion_y<<=(3-s->sprite_warping_accuracy);
1675 src_x = clip(src_x, -8, s->width>>1);
1676 if (src_x == s->width>>1)
1678 src_y = clip(src_y, -8, s->height>>1);
1679 if (src_y == s->height>>1)
1682 offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1683 ptr = ref_picture[1] + offset;
1684 if(s->flags&CODEC_FLAG_EMU_EDGE){
1685 if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
1686 || src_y + 9 >= s->v_edge_pos>>1){
1687 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1688 ptr= s->edge_emu_buffer;
1692 s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1694 ptr = ref_picture[2] + offset;
1696 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1697 ptr= s->edge_emu_buffer;
1699 s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1704 static inline void gmc_motion(MpegEncContext *s,
1705 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1707 uint8_t **ref_picture, int src_offset)
1710 int linesize, uvlinesize;
1711 const int a= s->sprite_warping_accuracy;
1714 linesize = s->linesize;
1715 uvlinesize = s->uvlinesize;
1717 ptr = ref_picture[0] + src_offset;
1719 dest_y+=dest_offset;
1721 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1722 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1724 s->dsp.gmc(dest_y, ptr, linesize, 16,
1727 s->sprite_delta[0][0], s->sprite_delta[0][1],
1728 s->sprite_delta[1][0], s->sprite_delta[1][1],
1729 a+1, (1<<(2*a+1)) - s->no_rounding,
1730 s->h_edge_pos, s->v_edge_pos);
1731 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1732 ox + s->sprite_delta[0][0]*8,
1733 oy + s->sprite_delta[1][0]*8,
1734 s->sprite_delta[0][0], s->sprite_delta[0][1],
1735 s->sprite_delta[1][0], s->sprite_delta[1][1],
1736 a+1, (1<<(2*a+1)) - s->no_rounding,
1737 s->h_edge_pos, s->v_edge_pos);
1739 if(s->flags&CODEC_FLAG_GRAY) return;
1742 dest_cb+=dest_offset>>1;
1743 dest_cr+=dest_offset>>1;
1745 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1746 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1748 ptr = ref_picture[1] + (src_offset>>1);
1749 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1752 s->sprite_delta[0][0], s->sprite_delta[0][1],
1753 s->sprite_delta[1][0], s->sprite_delta[1][1],
1754 a+1, (1<<(2*a+1)) - s->no_rounding,
1755 s->h_edge_pos>>1, s->v_edge_pos>>1);
1757 ptr = ref_picture[2] + (src_offset>>1);
1758 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1761 s->sprite_delta[0][0], s->sprite_delta[0][1],
1762 s->sprite_delta[1][0], s->sprite_delta[1][1],
1763 a+1, (1<<(2*a+1)) - s->no_rounding,
1764 s->h_edge_pos>>1, s->v_edge_pos>>1);
1768 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
1769 * @param buf destination buffer
1770 * @param src source buffer
1771 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
1772 * @param block_w width of block
1773 * @param block_h height of block
1774 * @param src_x x coordinate of the top left sample of the block in the source buffer
1775 * @param src_y y coordinate of the top left sample of the block in the source buffer
1776 * @param w width of the source buffer
1777 * @param h height of the source buffer
1779 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
1780 int src_x, int src_y, int w, int h){
1782 int start_y, start_x, end_y, end_x;
1785 src+= (h-1-src_y)*linesize;
1787 }else if(src_y<=-block_h){
1788 src+= (1-block_h-src_y)*linesize;
1794 }else if(src_x<=-block_w){
1795 src+= (1-block_w-src_x);
1799 start_y= FFMAX(0, -src_y);
1800 start_x= FFMAX(0, -src_x);
1801 end_y= FFMIN(block_h, h-src_y);
1802 end_x= FFMIN(block_w, w-src_x);
1804 // copy existing part
1805 for(y=start_y; y<end_y; y++){
1806 for(x=start_x; x<end_x; x++){
1807 buf[x + y*linesize]= src[x + y*linesize];
1812 for(y=0; y<start_y; y++){
1813 for(x=start_x; x<end_x; x++){
1814 buf[x + y*linesize]= buf[x + start_y*linesize];
1819 for(y=end_y; y<block_h; y++){
1820 for(x=start_x; x<end_x; x++){
1821 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1825 for(y=0; y<block_h; y++){
1827 for(x=0; x<start_x; x++){
1828 buf[x + y*linesize]= buf[start_x + y*linesize];
1832 for(x=end_x; x<block_w; x++){
1833 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1839 /* apply one mpeg motion vector to the three components */
1840 static inline void mpeg_motion(MpegEncContext *s,
1841 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1843 uint8_t **ref_picture, int src_offset,
1844 int field_based, op_pixels_func (*pix_op)[4],
1845 int motion_x, int motion_y, int h)
1848 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1851 if(s->quarter_sample)
1857 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1858 src_x = s->mb_x * 16 + (motion_x >> 1);
1859 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1861 /* WARNING: do no forget half pels */
1862 height = s->height >> field_based;
1863 v_edge_pos = s->v_edge_pos >> field_based;
1864 src_x = clip(src_x, -16, s->width);
1865 if (src_x == s->width)
1867 src_y = clip(src_y, -16, height);
1868 if (src_y == height)
1870 linesize = s->current_picture.linesize[0] << field_based;
1871 uvlinesize = s->current_picture.linesize[1] << field_based;
1872 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1873 dest_y += dest_offset;
1875 if(s->flags&CODEC_FLAG_EMU_EDGE){
1876 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1877 || src_y + (motion_y&1) + h > v_edge_pos){
1878 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based, //FIXME linesize? and uv below
1879 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1880 ptr= s->edge_emu_buffer + src_offset;
1884 pix_op[0][dxy](dest_y, ptr, linesize, h);
1886 if(s->flags&CODEC_FLAG_GRAY) return;
1888 if (s->out_format == FMT_H263) {
1890 if ((motion_x & 3) != 0)
1892 if ((motion_y & 3) != 0)
1899 dxy = ((my & 1) << 1) | (mx & 1);
1904 src_x = s->mb_x * 8 + mx;
1905 src_y = s->mb_y * (8 >> field_based) + my;
1906 src_x = clip(src_x, -8, s->width >> 1);
1907 if (src_x == (s->width >> 1))
1909 src_y = clip(src_y, -8, height >> 1);
1910 if (src_y == (height >> 1))
1912 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1913 ptr = ref_picture[1] + offset;
1915 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
1916 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1917 ptr= s->edge_emu_buffer + (src_offset >> 1);
1919 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1921 ptr = ref_picture[2] + offset;
1923 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
1924 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1925 ptr= s->edge_emu_buffer + (src_offset >> 1);
1927 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1930 static inline void qpel_motion(MpegEncContext *s,
1931 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1933 uint8_t **ref_picture, int src_offset,
1934 int field_based, op_pixels_func (*pix_op)[4],
1935 qpel_mc_func (*qpix_op)[16],
1936 int motion_x, int motion_y, int h)
1939 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1942 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1943 src_x = s->mb_x * 16 + (motion_x >> 2);
1944 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1946 height = s->height >> field_based;
1947 v_edge_pos = s->v_edge_pos >> field_based;
1948 src_x = clip(src_x, -16, s->width);
1949 if (src_x == s->width)
1951 src_y = clip(src_y, -16, height);
1952 if (src_y == height)
1954 linesize = s->linesize << field_based;
1955 uvlinesize = s->uvlinesize << field_based;
1956 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1957 dest_y += dest_offset;
1958 //printf("%d %d %d\n", src_x, src_y, dxy);
1960 if(s->flags&CODEC_FLAG_EMU_EDGE){
1961 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1962 || src_y + (motion_y&3) + h > v_edge_pos){
1963 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,
1964 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1965 ptr= s->edge_emu_buffer + src_offset;
1970 qpix_op[0][dxy](dest_y, ptr, linesize);
1972 //damn interlaced mode
1973 //FIXME boundary mirroring is not exactly correct here
1974 qpix_op[1][dxy](dest_y , ptr , linesize);
1975 qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1978 if(s->flags&CODEC_FLAG_GRAY) return;
1983 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
1984 static const int rtab[8]= {0,0,1,1,0,0,0,1};
1985 mx= (motion_x>>1) + rtab[motion_x&7];
1986 my= (motion_y>>1) + rtab[motion_y&7];
1987 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1988 mx= (motion_x>>1)|(motion_x&1);
1989 my= (motion_y>>1)|(motion_y&1);
1997 dxy= (mx&1) | ((my&1)<<1);
2001 src_x = s->mb_x * 8 + mx;
2002 src_y = s->mb_y * (8 >> field_based) + my;
2003 src_x = clip(src_x, -8, s->width >> 1);
2004 if (src_x == (s->width >> 1))
2006 src_y = clip(src_y, -8, height >> 1);
2007 if (src_y == (height >> 1))
2010 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2011 ptr = ref_picture[1] + offset;
2013 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
2014 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2015 ptr= s->edge_emu_buffer + (src_offset >> 1);
2017 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2019 ptr = ref_picture[2] + offset;
2021 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
2022 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2023 ptr= s->edge_emu_buffer + (src_offset >> 1);
2025 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2028 inline int ff_h263_round_chroma(int x){
2030 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2033 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2038 * motion compesation of a single macroblock
2040 * @param dest_y luma destination pointer
2041 * @param dest_cb chroma cb/u destination pointer
2042 * @param dest_cr chroma cr/v destination pointer
2043 * @param dir direction (0->forward, 1->backward)
2044 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2045 * @param pic_op halfpel motion compensation function (average or put normally)
2046 * @param pic_op qpel motion compensation function (average or put normally)
2047 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2049 static inline void MPV_motion(MpegEncContext *s,
2050 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2051 int dir, uint8_t **ref_picture,
2052 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
2054 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
2056 uint8_t *ptr, *dest;
2062 switch(s->mv_type) {
2066 if(s->real_sprite_warping_points==1){
2067 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
2070 gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
2073 }else if(s->quarter_sample){
2074 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2077 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2079 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
2080 ref_picture, pix_op,
2081 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2085 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2088 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2094 if(s->quarter_sample){
2096 motion_x = s->mv[dir][i][0];
2097 motion_y = s->mv[dir][i][1];
2099 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2100 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
2101 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
2103 /* WARNING: do no forget half pels */
2104 src_x = clip(src_x, -16, s->width);
2105 if (src_x == s->width)
2107 src_y = clip(src_y, -16, s->height);
2108 if (src_y == s->height)
2111 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2112 if(s->flags&CODEC_FLAG_EMU_EDGE){
2113 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
2114 || src_y + (motion_y&3) + 8 > s->v_edge_pos){
2115 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
2116 ptr= s->edge_emu_buffer;
2119 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2120 qpix_op[1][dxy](dest, ptr, s->linesize);
2122 mx += s->mv[dir][i][0]/2;
2123 my += s->mv[dir][i][1]/2;
2127 motion_x = s->mv[dir][i][0];
2128 motion_y = s->mv[dir][i][1];
2130 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
2131 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
2132 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
2134 /* WARNING: do no forget half pels */
2135 src_x = clip(src_x, -16, s->width);
2136 if (src_x == s->width)
2138 src_y = clip(src_y, -16, s->height);
2139 if (src_y == s->height)
2142 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2143 if(s->flags&CODEC_FLAG_EMU_EDGE){
2144 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
2145 || src_y + (motion_y&1) + 8 > s->v_edge_pos){
2146 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
2147 ptr= s->edge_emu_buffer;
2150 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2151 pix_op[1][dxy](dest, ptr, s->linesize, 8);
2153 mx += s->mv[dir][i][0];
2154 my += s->mv[dir][i][1];
2158 if(s->flags&CODEC_FLAG_GRAY) break;
2159 /* In case of 8X8, we construct a single chroma motion vector
2160 with a special rounding */
2161 mx= ff_h263_round_chroma(mx);
2162 my= ff_h263_round_chroma(my);
2163 dxy = ((my & 1) << 1) | (mx & 1);
2167 src_x = mb_x * 8 + mx;
2168 src_y = mb_y * 8 + my;
2169 src_x = clip(src_x, -8, s->width/2);
2170 if (src_x == s->width/2)
2172 src_y = clip(src_y, -8, s->height/2);
2173 if (src_y == s->height/2)
2176 offset = (src_y * (s->uvlinesize)) + src_x;
2177 ptr = ref_picture[1] + offset;
2178 if(s->flags&CODEC_FLAG_EMU_EDGE){
2179 if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
2180 || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
2181 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2182 ptr= s->edge_emu_buffer;
2186 pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
2188 ptr = ref_picture[2] + offset;
2190 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2191 ptr= s->edge_emu_buffer;
2193 pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
2196 if (s->picture_structure == PICT_FRAME) {
2197 if(s->quarter_sample){
2199 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2200 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2202 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2204 qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2205 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2207 s->mv[dir][1][0], s->mv[dir][1][1], 8);
2210 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2211 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2213 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2215 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2216 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2218 s->mv[dir][1][0], s->mv[dir][1][1], 8);
2222 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2223 offset= s->field_select[dir][0] ? s->linesize : 0;
2225 ref_picture= s->current_picture.data;
2226 offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
2229 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2230 ref_picture, offset,
2232 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2237 uint8_t ** ref2picture;
2239 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2240 ref2picture= ref_picture;
2241 offset= s->field_select[dir][0] ? s->linesize : 0;
2243 ref2picture= s->current_picture.data;
2244 offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
2247 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2248 ref2picture, offset,
2250 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2253 if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
2254 ref2picture= ref_picture;
2255 offset= s->field_select[dir][1] ? s->linesize : 0;
2257 ref2picture= s->current_picture.data;
2258 offset= s->field_select[dir][1] ? s->linesize : -s->linesize;
2260 // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
2261 mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
2263 ref2picture, offset,
2265 s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
2271 op_pixels_func (*dmv_pix_op)[4];
2274 dmv_pix_op = s->dsp.put_pixels_tab;
2276 if(s->picture_structure == PICT_FRAME){
2277 //put top field from top field
2278 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2281 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2282 //put bottom field from bottom field
2283 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2284 ref_picture, s->linesize,
2286 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2288 dmv_pix_op = s->dsp.avg_pixels_tab;
2290 //avg top field from bottom field
2291 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2292 ref_picture, s->linesize,
2294 s->mv[dir][2][0], s->mv[dir][2][1], 8);
2295 //avg bottom field from top field
2296 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2299 s->mv[dir][3][0], s->mv[dir][3][1], 8);
2302 offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
2305 //put field from the same parity
2306 //same parity is never in the same frame
2307 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2310 s->mv[dir][0][0],s->mv[dir][0][1],16);
2312 // after put we make avg of the same block
2313 dmv_pix_op=s->dsp.avg_pixels_tab;
2315 //opposite parity is always in the same frame if this is second field
2316 if(!s->first_field){
2317 ref_picture = s->current_picture.data;
2318 //top field is one linesize from frame beginig
2319 offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
2320 -s->linesize : s->linesize;
2322 offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
2325 //avg field from the opposite parity
2326 mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
2327 ref_picture, offset,
2329 s->mv[dir][2][0],s->mv[dir][2][1],16);
2338 /* put block[] to dest[] */
2339 static inline void put_dct(MpegEncContext *s,
2340 DCTELEM *block, int i, uint8_t *dest, int line_size)
2342 s->dct_unquantize(s, block, i, s->qscale);
2343 s->dsp.idct_put (dest, line_size, block);
2346 /* add block[] to dest[] */
2347 static inline void add_dct(MpegEncContext *s,
2348 DCTELEM *block, int i, uint8_t *dest, int line_size)
2350 if (s->block_last_index[i] >= 0) {
2351 s->dsp.idct_add (dest, line_size, block);
2355 static inline void add_dequant_dct(MpegEncContext *s,
2356 DCTELEM *block, int i, uint8_t *dest, int line_size)
2358 if (s->block_last_index[i] >= 0) {
2359 s->dct_unquantize(s, block, i, s->qscale);
2361 s->dsp.idct_add (dest, line_size, block);
2366 * cleans dc, ac, coded_block for the current non intra MB
2368 void ff_clean_intra_table_entries(MpegEncContext *s)
2370 int wrap = s->block_wrap[0];
2371 int xy = s->block_index[0];
2374 s->dc_val[0][xy + 1 ] =
2375 s->dc_val[0][xy + wrap] =
2376 s->dc_val[0][xy + 1 + wrap] = 1024;
2378 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2379 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2380 if (s->msmpeg4_version>=3) {
2381 s->coded_block[xy ] =
2382 s->coded_block[xy + 1 ] =
2383 s->coded_block[xy + wrap] =
2384 s->coded_block[xy + 1 + wrap] = 0;
2387 wrap = s->block_wrap[4];
2388 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
2390 s->dc_val[2][xy] = 1024;
2392 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2393 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2395 s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
2398 /* generic function called after a macroblock has been parsed by the
2399 decoder or after it has been encoded by the encoder.
2401 Important variables used:
2402 s->mb_intra : true if intra macroblock
2403 s->mv_dir : motion vector direction
2404 s->mv_type : motion vector type
2405 s->mv : motion vector
2406 s->interlaced_dct : true if interlaced dct used (mpeg2)
2408 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
2411 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2413 if(s->avctx->xvmc_acceleration){
2414 XVMC_decode_mb(s,block);
2422 s->current_picture.qscale_table[mb_xy]= s->qscale;
2424 /* update DC predictors for P macroblocks */
2426 if (s->h263_pred || s->h263_aic) {
2427 if(s->mbintra_table[mb_xy])
2428 ff_clean_intra_table_entries(s);
2432 s->last_dc[2] = 128 << s->intra_dc_precision;
2435 else if (s->h263_pred || s->h263_aic)
2436 s->mbintra_table[mb_xy]=1;
2438 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2439 uint8_t *dest_y, *dest_cb, *dest_cr;
2440 int dct_linesize, dct_offset;
2441 op_pixels_func (*op_pix)[4];
2442 qpel_mc_func (*op_qpix)[16];
2443 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2444 const int uvlinesize= s->current_picture.linesize[1];
2446 /* avoid copy if macroblock skipped in last frame too */
2447 /* skip only during decoding as we might trash the buffers during encoding a bit */
2449 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2450 const int age= s->current_picture.age;
2456 assert(s->pict_type!=I_TYPE);
2458 (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2459 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2461 /* if previous was skipped too, then nothing to do ! */
2462 if (*mbskip_ptr >= age && s->current_picture.reference){
2465 } else if(!s->current_picture.reference){
2466 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2467 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2469 *mbskip_ptr = 0; /* not skipped */
2473 if (s->interlaced_dct) {
2474 dct_linesize = linesize * 2;
2475 dct_offset = linesize;
2477 dct_linesize = linesize;
2478 dct_offset = linesize * 8;
2482 dest_cb= s->dest[1];
2483 dest_cr= s->dest[2];
2486 /* motion handling */
2487 /* decoding or more than one mb_type (MC was allready done otherwise) */
2489 if ((!s->no_rounding) || s->pict_type==B_TYPE){
2490 op_pix = s->dsp.put_pixels_tab;
2491 op_qpix= s->dsp.put_qpel_pixels_tab;
2493 op_pix = s->dsp.put_no_rnd_pixels_tab;
2494 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2497 if (s->mv_dir & MV_DIR_FORWARD) {
2498 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2499 op_pix = s->dsp.avg_pixels_tab;
2500 op_qpix= s->dsp.avg_qpel_pixels_tab;
2502 if (s->mv_dir & MV_DIR_BACKWARD) {
2503 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2507 /* skip dequant / idct if we are really late ;) */
2508 if(s->hurry_up>1) return;
2510 /* add dct residue */
2511 if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
2512 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
2513 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2514 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2515 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2516 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2518 if(!(s->flags&CODEC_FLAG_GRAY)){
2519 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
2520 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
2522 } else if(s->codec_id != CODEC_ID_WMV2){
2523 add_dct(s, block[0], 0, dest_y, dct_linesize);
2524 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2525 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2526 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2528 if(!(s->flags&CODEC_FLAG_GRAY)){
2529 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2530 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2535 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2539 /* dct only in intra block */
2540 if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
2541 put_dct(s, block[0], 0, dest_y, dct_linesize);
2542 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2543 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2544 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2546 if(!(s->flags&CODEC_FLAG_GRAY)){
2547 put_dct(s, block[4], 4, dest_cb, uvlinesize);
2548 put_dct(s, block[5], 5, dest_cr, uvlinesize);
2551 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2552 s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
2553 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2554 s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
2556 if(!(s->flags&CODEC_FLAG_GRAY)){
2557 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2558 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2565 #ifdef CONFIG_ENCODERS
2567 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2569 static const char tab[64]=
2581 DCTELEM *block= s->block[n];
2582 const int last_index= s->block_last_index[n];
2587 threshold= -threshold;
2591 /* are all which we could set to zero are allready zero? */
2592 if(last_index<=skip_dc - 1) return;
2594 for(i=0; i<=last_index; i++){
2595 const int j = s->intra_scantable.permutated[i];
2596 const int level = ABS(block[j]);
2598 if(skip_dc && i==0) continue;
2607 if(score >= threshold) return;
2608 for(i=skip_dc; i<=last_index; i++){
2609 const int j = s->intra_scantable.permutated[i];
2612 if(block[0]) s->block_last_index[n]= 0;
2613 else s->block_last_index[n]= -1;
2616 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2619 const int maxlevel= s->max_qcoeff;
2620 const int minlevel= s->min_qcoeff;
2623 i=1; //skip clipping of intra dc
2627 for(;i<=last_index; i++){
2628 const int j= s->intra_scantable.permutated[i];
2629 int level = block[j];
2631 if (level>maxlevel) level=maxlevel;
2632 else if(level<minlevel) level=minlevel;
2639 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2644 for(x=0; x<16; x+=4){
2645 score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
2646 +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2654 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2659 for(x=0; x<16; x++){
2660 score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2669 #define SQ(a) ((a)*(a))
2671 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2676 for(x=0; x<16; x+=4){
2677 score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
2678 +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2686 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2691 for(x=0; x<16; x++){
2692 score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2703 #endif //CONFIG_ENCODERS
2707 * @param h is the normal height, this will be reduced automatically if needed for the last row
2709 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2710 if (s->avctx->draw_horiz_band) {
2712 uint8_t *src_ptr[3];
2715 if(s->picture_structure != PICT_FRAME){
2718 if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
2721 h= FFMIN(h, s->height - y);
2723 if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
2724 src= (AVFrame*)s->current_picture_ptr;
2725 else if(s->last_picture_ptr)
2726 src= (AVFrame*)s->last_picture_ptr;
2730 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
2736 offset[0]= y * s->linesize;;
2738 offset[2]= (y>>1) * s->uvlinesize;;
2744 s->avctx->draw_horiz_band(s->avctx, src, offset,
2745 y, s->picture_structure, h);
2749 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2750 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2751 const int uvlinesize= s->current_picture.linesize[1];
2753 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2754 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
2755 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
2756 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
2757 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2758 s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2760 if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
2761 s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
2762 s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
2763 s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
2765 s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16 - 16;
2766 s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
2767 s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
2771 #ifdef CONFIG_ENCODERS
2773 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2775 const int mb_x= s->mb_x;
2776 const int mb_y= s->mb_y;
2779 int dct_offset = s->linesize*8; //default for progressive frames
2781 for(i=0; i<6; i++) skip_dct[i]=0;
2783 if(s->adaptive_quant){
2784 s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_stride] - s->qscale;
2786 if(s->out_format==FMT_H263){
2787 if (s->dquant> 2) s->dquant= 2;
2788 else if(s->dquant<-2) s->dquant=-2;
2791 if(s->codec_id==CODEC_ID_MPEG4){
2793 if(s->mv_dir&MV_DIRECT)
2796 assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2799 s->qscale+= s->dquant;
2800 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2801 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2809 wrap_y = s->linesize;
2810 ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2812 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2813 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2814 ptr= s->edge_emu_buffer;
2818 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2819 int progressive_score, interlaced_score;
2821 progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2822 interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
2824 if(progressive_score > interlaced_score + 100){
2825 s->interlaced_dct=1;
2830 s->interlaced_dct=0;
2833 s->dsp.get_pixels(s->block[0], ptr , wrap_y);
2834 s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
2835 s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
2836 s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2838 if(s->flags&CODEC_FLAG_GRAY){
2842 int wrap_c = s->uvlinesize;
2843 ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2845 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2846 ptr= s->edge_emu_buffer;
2848 s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2850 ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2852 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2853 ptr= s->edge_emu_buffer;
2855 s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2858 op_pixels_func (*op_pix)[4];
2859 qpel_mc_func (*op_qpix)[16];
2860 uint8_t *dest_y, *dest_cb, *dest_cr;
2861 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2865 dest_y = s->dest[0];
2866 dest_cb = s->dest[1];
2867 dest_cr = s->dest[2];
2868 wrap_y = s->linesize;
2869 wrap_c = s->uvlinesize;
2870 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2871 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2872 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2874 if ((!s->no_rounding) || s->pict_type==B_TYPE){
2875 op_pix = s->dsp.put_pixels_tab;
2876 op_qpix= s->dsp.put_qpel_pixels_tab;
2878 op_pix = s->dsp.put_no_rnd_pixels_tab;
2879 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2882 if (s->mv_dir & MV_DIR_FORWARD) {
2883 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2884 op_pix = s->dsp.avg_pixels_tab;
2885 op_qpix= s->dsp.avg_qpel_pixels_tab;
2887 if (s->mv_dir & MV_DIR_BACKWARD) {
2888 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2891 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2892 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2893 ptr_y= s->edge_emu_buffer;
2897 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2898 int progressive_score, interlaced_score;
2900 progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
2901 + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
2902 interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
2903 + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
2905 if(progressive_score > interlaced_score + 600){
2906 s->interlaced_dct=1;
2911 s->interlaced_dct=0;
2914 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
2915 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2916 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
2917 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2919 if(s->flags&CODEC_FLAG_GRAY){
2924 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2925 ptr_cb= s->edge_emu_buffer;
2927 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2929 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2930 ptr_cr= s->edge_emu_buffer;
2932 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2934 /* pre quantization */
2935 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
2937 if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2938 if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2939 if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2940 if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2941 if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
2942 if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
2948 if(skip_dct[i]) num++;
2951 if(s->mb_x==0 && s->mb_y==0){
2953 printf("%6d %1d\n", stat[i], i);
2966 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_stride*mb_y+mb_x] + 1.0) /
2967 ((s->mb_var[s->mb_stride*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2969 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
2970 (s->mb_type[s->mb_stride*mb_y+mb_x] > 0) ? 'I' : 'P',
2971 s->qscale, adap_parm, s->qscale*adap_parm,
2972 s->mb_var[s->mb_stride*mb_y+mb_x], s->avg_mb_var);
2975 /* DCT & quantize */
2976 if(s->out_format==FMT_MJPEG){
2979 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2980 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2986 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2987 // FIXME we could decide to change to quantizer instead of clipping
2988 // JS: I don't think that would be a good idea it could lower quality instead
2989 // of improve it. Just INTRADC clipping deserves changes in quantizer
2990 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2992 s->block_last_index[i]= -1;
2994 if(s->luma_elim_threshold && !s->mb_intra)
2996 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2997 if(s->chroma_elim_threshold && !s->mb_intra)
2999 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
3002 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
3003 s->block_last_index[4]=
3004 s->block_last_index[5]= 0;
3006 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
3009 /* huffman encode */
3010 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
3011 case CODEC_ID_MPEG1VIDEO:
3012 mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
3014 case CODEC_ID_MPEG4:
3015 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3016 case CODEC_ID_MSMPEG4V2:
3017 case CODEC_ID_MSMPEG4V3:
3019 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3021 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
3023 case CODEC_ID_H263P:
3026 h263_encode_mb(s, s->block, motion_x, motion_y); break;
3028 case CODEC_ID_MJPEG:
3029 mjpeg_encode_mb(s, s->block); break;
3035 #endif //CONFIG_ENCODERS
3038 * combines the (truncated) bitstream to a complete frame
3039 * @returns -1 if no complete frame could be created
3041 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
3042 ParseContext *pc= &s->parse_context;
3046 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3047 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3051 /* copy overreaded byes from last frame into buffer */
3052 for(; pc->overread>0; pc->overread--){
3053 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
3056 pc->last_index= pc->index;
3058 /* copy into buffer end return */
3059 if(next == END_NOT_FOUND){
3060 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3062 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
3063 pc->index += *buf_size;
3068 pc->overread_index= pc->index + next;
3070 /* append to buffer */
3072 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3074 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
3079 /* store overread bytes */
3080 for(;next < 0; next++){
3081 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
3087 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3088 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3095 void ff_mpeg_flush(AVCodecContext *avctx){
3097 MpegEncContext *s = avctx->priv_data;
3099 for(i=0; i<MAX_PICTURE_COUNT; i++){
3100 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
3101 || s->picture[i].type == FF_BUFFER_TYPE_USER))
3102 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
3104 s->last_picture_ptr = s->next_picture_ptr = NULL;
3106 s->parse_context.state= -1;
3107 s->parse_context.frame_start_found= 0;
3108 s->parse_context.overread= 0;
3109 s->parse_context.overread_index= 0;
3110 s->parse_context.index= 0;
3111 s->parse_context.last_index= 0;
3114 #ifdef CONFIG_ENCODERS
3115 void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
3117 int bytes= length>>4;
3118 int bits= length&15;
3121 if(length==0) return;
3123 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
3124 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
3127 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
3130 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
3133 d->mb_skip_run= s->mb_skip_run;
3135 d->last_dc[i]= s->last_dc[i];
3138 d->mv_bits= s->mv_bits;
3139 d->i_tex_bits= s->i_tex_bits;
3140 d->p_tex_bits= s->p_tex_bits;
3141 d->i_count= s->i_count;
3142 d->f_count= s->f_count;
3143 d->b_count= s->b_count;
3144 d->skip_count= s->skip_count;
3145 d->misc_bits= s->misc_bits;
3149 d->qscale= s->qscale;
3152 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
3155 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
3156 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
3159 d->mb_skip_run= s->mb_skip_run;
3161 d->last_dc[i]= s->last_dc[i];
3164 d->mv_bits= s->mv_bits;
3165 d->i_tex_bits= s->i_tex_bits;
3166 d->p_tex_bits= s->p_tex_bits;
3167 d->i_count= s->i_count;
3168 d->f_count= s->f_count;
3169 d->b_count= s->b_count;
3170 d->skip_count= s->skip_count;
3171 d->misc_bits= s->misc_bits;
3173 d->mb_intra= s->mb_intra;
3174 d->mb_skiped= s->mb_skiped;
3175 d->mv_type= s->mv_type;
3176 d->mv_dir= s->mv_dir;
3178 if(s->data_partitioning){
3180 d->tex_pb= s->tex_pb;
3184 d->block_last_index[i]= s->block_last_index[i];
3185 d->interlaced_dct= s->interlaced_dct;
3186 d->qscale= s->qscale;
3189 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
3190 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
3191 int *dmin, int *next_block, int motion_x, int motion_y)
3194 uint8_t *dest_backup[3];
3196 copy_context_before_encode(s, backup, type);
3198 s->block= s->blocks[*next_block];
3199 s->pb= pb[*next_block];
3200 if(s->data_partitioning){
3201 s->pb2 = pb2 [*next_block];
3202 s->tex_pb= tex_pb[*next_block];
3206 memcpy(dest_backup, s->dest, sizeof(s->dest));
3207 s->dest[0] = s->me.scratchpad;
3208 s->dest[1] = s->me.scratchpad + 16;
3209 s->dest[2] = s->me.scratchpad + 16 + 8;
3210 assert(2*s->uvlinesize == s->linesize); //should be no prob for encoding
3211 assert(s->linesize >= 64); //FIXME
3214 encode_mb(s, motion_x, motion_y);
3216 score= get_bit_count(&s->pb);
3217 if(s->data_partitioning){
3218 score+= get_bit_count(&s->pb2);
3219 score+= get_bit_count(&s->tex_pb);
3222 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
3223 MPV_decode_mb(s, s->block);
3225 score *= s->qscale * s->qscale * 109;
3226 score += sse_mb(s) << 7;
3230 memcpy(s->dest, dest_backup, sizeof(s->dest));
3237 copy_context_after_encode(best, s, type);
3241 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
3242 uint32_t *sq = squareTbl + 256;
3247 return s->dsp.sse[0](NULL, src1, src2, stride);
3248 else if(w==8 && h==8)
3249 return s->dsp.sse[1](NULL, src1, src2, stride);
3253 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
3262 static int sse_mb(MpegEncContext *s){
3266 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3267 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3270 return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize)
3271 +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize)
3272 +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize);
3274 return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
3275 +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
3276 +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
3279 static void encode_picture(MpegEncContext *s, int picture_number)
3281 int mb_x, mb_y, pdif = 0;
3284 MpegEncContext best_s, backup_s;
3285 uint8_t bit_buf[2][3000];
3286 uint8_t bit_buf2[2][3000];
3287 uint8_t bit_buf_tex[2][3000];
3288 PutBitContext pb[2], pb2[2], tex_pb[2];
3291 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
3292 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
3293 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
3296 s->picture_number = picture_number;
3298 /* Reset the average MB variance */
3299 s->current_picture.mb_var_sum = 0;
3300 s->current_picture.mc_mb_var_sum = 0;
3303 /* we need to initialize some time vars before we can encode b-frames */
3304 // RAL: Condition added for MPEG1VIDEO
3305 if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
3306 ff_set_mpeg4_time(s, s->picture_number);
3309 s->scene_change_score=0;
3311 s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
3313 if(s->pict_type==I_TYPE){
3314 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3315 else s->no_rounding=0;
3316 }else if(s->pict_type!=B_TYPE){
3317 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
3318 s->no_rounding ^= 1;
3321 /* Estimate motion for every MB */
3322 s->mb_intra=0; //for the rate distoration & bit compare functions
3323 if(s->pict_type != I_TYPE){
3324 if(s->pict_type != B_TYPE){
3325 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
3327 s->me.dia_size= s->avctx->pre_dia_size;
3329 for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
3330 for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
3333 ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
3340 s->me.dia_size= s->avctx->dia_size;
3341 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3342 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3343 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3344 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3345 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3346 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3349 s->block_index[0]+=2;
3350 s->block_index[1]+=2;
3351 s->block_index[2]+=2;
3352 s->block_index[3]+=2;
3354 /* compute motion vector & mb_type and store in context */
3355 if(s->pict_type==B_TYPE)
3356 ff_estimate_b_frame_motion(s, mb_x, mb_y);
3358 ff_estimate_p_frame_motion(s, mb_x, mb_y);
3361 }else /* if(s->pict_type == I_TYPE) */{
3363 //FIXME do we need to zero them?
3364 memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
3365 memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_stride)*s->mb_height*2);
3366 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
3368 if(!s->fixed_qscale){
3369 /* finding spatial complexity for I-frame rate control */
3370 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3371 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3374 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
3376 int sum = s->dsp.pix_sum(pix, s->linesize);
3378 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
3380 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
3381 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
3382 s->current_picture.mb_var_sum += varc;
3389 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
3390 s->pict_type= I_TYPE;
3391 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
3392 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3396 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
3397 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
3399 ff_fix_long_p_mvs(s);
3402 if(s->pict_type==B_TYPE){
3405 a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
3406 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
3407 s->f_code = FFMAX(a, b);
3409 a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
3410 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
3411 s->b_code = FFMAX(a, b);
3413 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
3414 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
3415 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
3416 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
3420 if (s->fixed_qscale)
3421 s->frame_qscale = s->current_picture.quality;
3423 s->frame_qscale = ff_rate_estimate_qscale(s);
3425 if(s->adaptive_quant){
3427 switch(s->codec_id){
3428 case CODEC_ID_MPEG4:
3429 ff_clean_mpeg4_qscales(s);
3432 case CODEC_ID_H263P:
3434 ff_clean_h263_qscales(s);
3439 s->qscale= s->current_picture.qscale_table[0];
3441 s->qscale= (int)(s->frame_qscale + 0.5);
3443 if (s->out_format == FMT_MJPEG) {
3444 /* for mjpeg, we do include qscale in the matrix */
3445 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
3447 int j= s->dsp.idct_permutation[i];
3449 s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3451 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3452 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
3455 //FIXME var duplication
3456 s->current_picture.key_frame= s->pict_type == I_TYPE;
3457 s->current_picture.pict_type= s->pict_type;
3459 if(s->current_picture.key_frame)
3460 s->picture_in_gop_number=0;
3462 s->last_bits= get_bit_count(&s->pb);
3463 switch(s->out_format) {
3465 mjpeg_picture_header(s);
3469 if (s->codec_id == CODEC_ID_WMV2)
3470 ff_wmv2_encode_picture_header(s, picture_number);
3471 else if (s->h263_msmpeg4)
3472 msmpeg4_encode_picture_header(s, picture_number);
3473 else if (s->h263_pred)
3474 mpeg4_encode_picture_header(s, picture_number);
3475 else if (s->h263_rv10)
3476 rv10_encode_picture_header(s, picture_number);
3477 else if (s->codec_id == CODEC_ID_FLV1)
3478 ff_flv_encode_picture_header(s, picture_number);
3480 h263_encode_picture_header(s, picture_number);
3484 mpeg1_encode_picture_header(s, picture_number);
3487 bits= get_bit_count(&s->pb);
3488 s->header_bits= bits - s->last_bits;
3500 /* init last dc values */
3501 /* note: quant matrix value (8) is implied here */
3502 s->last_dc[i] = 128;
3504 s->current_picture_ptr->error[i] = 0;
3507 s->last_mv[0][0][0] = 0;
3508 s->last_mv[0][0][1] = 0;
3509 s->last_mv[1][0][0] = 0;
3510 s->last_mv[1][0][1] = 0;
3515 switch(s->codec_id){
3517 case CODEC_ID_H263P:
3519 s->gob_index = ff_h263_get_gob_height(s);
3521 case CODEC_ID_MPEG4:
3522 if(s->partitioned_frame)
3523 ff_mpeg4_init_partitions(s);
3530 s->first_slice_line = 1;
3531 s->ptr_lastgob = s->pb.buf;
3532 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3536 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3537 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
3538 ff_init_block_index(s);
3540 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3541 const int xy= mb_y*s->mb_stride + mb_x;
3542 int mb_type= s->mb_type[xy];
3547 ff_update_block_index(s);
3549 /* write gob / video packet header */
3552 int current_packet_size, is_gob_start;
3554 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
3557 if(s->codec_id==CODEC_ID_MPEG4){
3558 if(current_packet_size >= s->rtp_payload_size
3559 && s->mb_y + s->mb_x>0){
3561 if(s->partitioned_frame){
3562 ff_mpeg4_merge_partitions(s);
3563 ff_mpeg4_init_partitions(s);
3565 ff_mpeg4_encode_video_packet_header(s);
3567 if(s->flags&CODEC_FLAG_PASS1){
3568 int bits= get_bit_count(&s->pb);
3569 s->misc_bits+= bits - s->last_bits;
3572 ff_mpeg4_clean_buffers(s);
3575 }else if(s->codec_id==CODEC_ID_MPEG1VIDEO){
3576 if( current_packet_size >= s->rtp_payload_size
3577 && s->mb_y + s->mb_x>0 && s->mb_skip_run==0){
3578 ff_mpeg1_encode_slice_header(s);
3579 ff_mpeg1_clean_buffers(s);
3583 if(current_packet_size >= s->rtp_payload_size
3584 && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
3586 h263_encode_gob_header(s, mb_y);
3592 s->ptr_lastgob = pbBufPtr(&s->pb);
3593 s->first_slice_line=1;
3594 s->resync_mb_x=mb_x;
3595 s->resync_mb_y=mb_y;
3600 if( (s->resync_mb_x == s->mb_x)
3601 && s->resync_mb_y+1 == s->mb_y){
3602 s->first_slice_line=0;
3607 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
3609 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3611 copy_context_before_encode(&backup_s, s, -1);
3613 best_s.data_partitioning= s->data_partitioning;
3614 best_s.partitioned_frame= s->partitioned_frame;
3615 if(s->data_partitioning){
3616 backup_s.pb2= s->pb2;
3617 backup_s.tex_pb= s->tex_pb;
3620 if(mb_type&MB_TYPE_INTER){
3621 s->mv_dir = MV_DIR_FORWARD;
3622 s->mv_type = MV_TYPE_16X16;
3624 s->mv[0][0][0] = s->p_mv_table[xy][0];
3625 s->mv[0][0][1] = s->p_mv_table[xy][1];
3626 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
3627 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3629 if(mb_type&MB_TYPE_INTER4V){
3630 s->mv_dir = MV_DIR_FORWARD;
3631 s->mv_type = MV_TYPE_8X8;
3634 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3635 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3637 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
3638 &dmin, &next_block, 0, 0);
3640 if(mb_type&MB_TYPE_FORWARD){
3641 s->mv_dir = MV_DIR_FORWARD;
3642 s->mv_type = MV_TYPE_16X16;
3644 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3645 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3646 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
3647 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3649 if(mb_type&MB_TYPE_BACKWARD){
3650 s->mv_dir = MV_DIR_BACKWARD;
3651 s->mv_type = MV_TYPE_16X16;
3653 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3654 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3655 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3656 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3658 if(mb_type&MB_TYPE_BIDIR){
3659 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3660 s->mv_type = MV_TYPE_16X16;
3662 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3663 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3664 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3665 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3666 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
3667 &dmin, &next_block, 0, 0);
3669 if(mb_type&MB_TYPE_DIRECT){
3670 int mx= s->b_direct_mv_table[xy][0];
3671 int my= s->b_direct_mv_table[xy][1];
3673 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3676 ff_mpeg4_set_direct_mv(s, mx, my);
3678 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
3679 &dmin, &next_block, mx, my);
3681 if(mb_type&MB_TYPE_INTRA){
3683 s->mv_type = MV_TYPE_16X16;
3687 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
3688 &dmin, &next_block, 0, 0);
3689 if(s->h263_pred || s->h263_aic){
3691 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3693 ff_clean_intra_table_entries(s); //old mode?
3696 copy_context_after_encode(s, &best_s, -1);
3698 pb_bits_count= get_bit_count(&s->pb);
3699 flush_put_bits(&s->pb);
3700 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3703 if(s->data_partitioning){
3704 pb2_bits_count= get_bit_count(&s->pb2);
3705 flush_put_bits(&s->pb2);
3706 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3707 s->pb2= backup_s.pb2;
3709 tex_pb_bits_count= get_bit_count(&s->tex_pb);
3710 flush_put_bits(&s->tex_pb);
3711 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3712 s->tex_pb= backup_s.tex_pb;
3714 s->last_bits= get_bit_count(&s->pb);
3716 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
3717 ff_h263_update_motion_val(s);
3720 s->dsp.put_pixels_tab[0][0](s->dest[0], s->me.scratchpad , s->linesize ,16);
3721 s->dsp.put_pixels_tab[1][0](s->dest[1], s->me.scratchpad + 16, s->uvlinesize, 8);
3722 s->dsp.put_pixels_tab[1][0](s->dest[2], s->me.scratchpad + 24, s->uvlinesize, 8);
3725 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3726 MPV_decode_mb(s, s->block);
3728 int motion_x, motion_y;
3730 int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_stride];
3732 if(s->avctx->mb_decision==FF_MB_DECISION_SIMPLE && s->pict_type==P_TYPE){ //FIXME check if the mess is usefull at all
3733 /* get luma score */
3734 if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
3735 intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_stride]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
3739 int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_stride]; //FIXME
3742 dest_y = s->new_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
3744 for(i=0; i<16; i++){
3745 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
3746 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
3747 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
3748 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
3752 intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
3754 /* printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8,
3755 s->current_picture.mb_var[mb_x + mb_y*s->mb_stride],
3756 s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_stride]);*/
3759 /* get chroma score */
3760 if(s->avctx->mb_cmp&FF_CMP_CHROMA){
3768 if(s->out_format == FMT_H263){
3769 mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
3771 mean= (s->last_dc[i] + 4)>>3;
3773 dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
3777 *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
3778 *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
3781 intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
3786 switch(s->avctx->mb_cmp&0xFF){
3789 intra_score+= 32*s->qscale;
3792 intra_score+= 24*s->qscale*s->qscale;
3795 intra_score+= 96*s->qscale;
3798 intra_score+= 48*s->qscale;
3805 intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
3809 if(intra_score < inter_score)
3810 mb_type= MB_TYPE_INTRA;
3813 s->mv_type=MV_TYPE_16X16;
3814 // only one MB-Type possible
3820 motion_x= s->mv[0][0][0] = 0;
3821 motion_y= s->mv[0][0][1] = 0;
3824 s->mv_dir = MV_DIR_FORWARD;
3826 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3827 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3829 case MB_TYPE_INTER4V:
3830 s->mv_dir = MV_DIR_FORWARD;
3831 s->mv_type = MV_TYPE_8X8;
3834 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3835 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3837 motion_x= motion_y= 0;
3839 case MB_TYPE_DIRECT:
3840 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3842 motion_x=s->b_direct_mv_table[xy][0];
3843 motion_y=s->b_direct_mv_table[xy][1];
3845 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3849 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3853 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3854 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3855 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3856 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3858 case MB_TYPE_BACKWARD:
3859 s->mv_dir = MV_DIR_BACKWARD;
3861 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3862 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3864 case MB_TYPE_FORWARD:
3865 s->mv_dir = MV_DIR_FORWARD;
3867 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3868 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3869 // printf(" %d %d ", motion_x, motion_y);
3872 motion_x=motion_y=0; //gcc warning fix
3873 printf("illegal MB type\n");
3876 encode_mb(s, motion_x, motion_y);
3878 // RAL: Update last macrobloc type
3879 s->last_mv_dir = s->mv_dir;
3881 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
3882 ff_h263_update_motion_val(s);
3884 MPV_decode_mb(s, s->block);
3887 /* clean the MV table in IPS frames for direct mode in B frames */
3888 if(s->mb_intra /* && I,P,S_TYPE */){
3889 s->p_mv_table[xy][0]=0;
3890 s->p_mv_table[xy][1]=0;
3893 if(s->flags&CODEC_FLAG_PSNR){
3897 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3898 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3900 s->current_picture_ptr->error[0] += sse(
3901 s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3902 s->dest[0], w, h, s->linesize);
3903 s->current_picture_ptr->error[1] += sse(
3904 s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3905 s->dest[1], w>>1, h>>1, s->uvlinesize);
3906 s->current_picture_ptr->error[2] += sse(
3907 s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3908 s->dest[2], w>>1, h>>1, s->uvlinesize);
3910 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
3916 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
3917 ff_mpeg4_merge_partitions(s);
3919 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
3920 msmpeg4_encode_ext_header(s);
3922 if(s->codec_id==CODEC_ID_MPEG4)
3923 ff_mpeg4_stuffing(&s->pb);
3926 //if (s->gob_number)
3927 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
3929 /* Send the last GOB if RTP */
3931 flush_put_bits(&s->pb);
3932 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
3933 /* Call the RTP callback to send the last GOB */
3934 if (s->rtp_callback)
3935 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
3936 s->ptr_lastgob = pbBufPtr(&s->pb);
3937 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
3941 static int dct_quantize_trellis_c(MpegEncContext *s,
3942 DCTELEM *block, int n,
3943 int qscale, int *overflow){
3945 const uint8_t *scantable= s->intra_scantable.scantable;
3947 unsigned int threshold1, threshold2;
3957 int coeff_count[64];
3958 int lambda, qmul, qadd, start_i, last_non_zero, i;
3959 const int esc_length= s->ac_esc_length;
3961 uint8_t * last_length;
3965 s->dsp.fdct (block);
3968 qadd= ((qscale-1)|1)*8;
3979 /* For AIC we skip quant/dequant of INTRADC */
3984 /* note: block[0] is assumed to be positive */
3985 block[0] = (block[0] + (q >> 1)) / q;
3988 qmat = s->q_intra_matrix[qscale];
3989 if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
3990 bias= 1<<(QMAT_SHIFT-1);
3991 length = s->intra_ac_vlc_length;
3992 last_length= s->intra_ac_vlc_last_length;
3996 qmat = s->q_inter_matrix[qscale];
3997 length = s->inter_ac_vlc_length;
3998 last_length= s->inter_ac_vlc_last_length;
4001 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4002 threshold2= (threshold1<<1);
4004 for(i=start_i; i<64; i++) {
4005 const int j = scantable[i];
4006 const int k= i-start_i;
4007 int level = block[j];
4008 level = level * qmat[j];
4010 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
4011 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
4012 if(((unsigned)(level+threshold1))>threshold2){
4014 level= (bias + level)>>QMAT_SHIFT;
4016 coeff[1][k]= level-1;
4017 // coeff[2][k]= level-2;
4019 level= (bias - level)>>QMAT_SHIFT;
4020 coeff[0][k]= -level;
4021 coeff[1][k]= -level+1;
4022 // coeff[2][k]= -level+2;
4024 coeff_count[k]= FFMIN(level, 2);
4028 coeff[0][k]= (level>>31)|1;
4033 *overflow= s->max_qcoeff < max; //overflow might have happend
4035 if(last_non_zero < start_i){
4036 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
4037 return last_non_zero;
4040 lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
4043 for(i=0; i<=last_non_zero - start_i; i++){
4044 int level_index, run, j;
4045 const int dct_coeff= block[ scantable[i + start_i] ];
4046 const int zero_distoration= dct_coeff*dct_coeff;
4047 int best_score=256*256*256*120;
4049 last_score += zero_distoration;
4050 for(level_index=0; level_index < coeff_count[i]; level_index++){
4052 int level= coeff[level_index][i];
4057 if(s->out_format == FMT_H263){
4059 unquant_coeff= level*qmul + qadd;
4061 unquant_coeff= level*qmul - qadd;
4064 j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
4067 unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
4068 unquant_coeff = -((unquant_coeff - 1) | 1);
4070 unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
4071 unquant_coeff = (unquant_coeff - 1) | 1;
4075 unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
4076 unquant_coeff = -((unquant_coeff - 1) | 1);
4078 unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
4079 unquant_coeff = (unquant_coeff - 1) | 1;
4085 distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
4087 if((level&(~127)) == 0){
4088 for(run=0; run<=i - left_limit; run++){
4089 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4090 score += score_tab[i-run];
4092 if(score < best_score){
4094 score_tab[i+1]= score;
4096 level_tab[i+1]= level-64;
4100 if(s->out_format == FMT_H263){
4101 for(run=0; run<=i - left_limit; run++){
4102 int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4103 score += score_tab[i-run];
4104 if(score < last_score){
4107 last_level= level-64;
4113 distoration += esc_length*lambda;
4114 for(run=0; run<=i - left_limit; run++){
4115 int score= distoration + score_tab[i-run];
4117 if(score < best_score){
4119 score_tab[i+1]= score;
4121 level_tab[i+1]= level-64;
4125 if(s->out_format == FMT_H263){
4126 for(run=0; run<=i - left_limit; run++){
4127 int score= distoration + score_tab[i-run];
4128 if(score < last_score){
4131 last_level= level-64;
4139 for(j=left_limit; j<=i; j++){
4140 score_tab[j] += zero_distoration;
4142 score_limit+= zero_distoration;
4143 if(score_tab[i+1] < score_limit)
4144 score_limit= score_tab[i+1];
4146 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
4147 while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
4150 //FIXME add some cbp penalty
4152 if(s->out_format != FMT_H263){
4153 last_score= 256*256*256*120;
4154 for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
4155 int score= score_tab[i];
4156 if(i) score += lambda*2; //FIXME exacter?
4158 if(score < last_score){
4161 last_level= level_tab[i];
4162 last_run= run_tab[i];
4167 last_non_zero= last_i - 1 + start_i;
4168 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
4170 if(last_non_zero < start_i)
4171 return last_non_zero;
4175 //FIXME use permutated scantable
4176 block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
4179 for(;i>0 ; i -= run_tab[i] + 1){
4180 const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
4182 block[j]= level_tab[i];
4186 return last_non_zero;
4189 static int dct_quantize_c(MpegEncContext *s,
4190 DCTELEM *block, int n,
4191 int qscale, int *overflow)
4193 int i, j, level, last_non_zero, q;
4195 const uint8_t *scantable= s->intra_scantable.scantable;
4198 unsigned int threshold1, threshold2;
4200 s->dsp.fdct (block);
4210 /* For AIC we skip quant/dequant of INTRADC */
4213 /* note: block[0] is assumed to be positive */
4214 block[0] = (block[0] + (q >> 1)) / q;
4217 qmat = s->q_intra_matrix[qscale];
4218 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4222 qmat = s->q_inter_matrix[qscale];
4223 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4225 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4226 threshold2= (threshold1<<1);
4231 level = level * qmat[j];
4233 // if( bias+level >= (1<<QMAT_SHIFT)
4234 // || bias-level >= (1<<QMAT_SHIFT)){
4235 if(((unsigned)(level+threshold1))>threshold2){
4237 level= (bias + level)>>QMAT_SHIFT;
4240 level= (bias - level)>>QMAT_SHIFT;
4249 *overflow= s->max_qcoeff < max; //overflow might have happend
4251 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4252 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
4253 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
4255 return last_non_zero;
4258 #endif //CONFIG_ENCODERS
4260 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
4261 DCTELEM *block, int n, int qscale)
4263 int i, level, nCoeffs;
4264 const uint16_t *quant_matrix;
4266 nCoeffs= s->block_last_index[n];
4270 block[0] = block[0] * s->y_dc_scale;
4272 block[0] = block[0] * s->c_dc_scale;
4273 /* XXX: only mpeg1 */
4274 quant_matrix = s->intra_matrix;
4275 for(i=1;i<=nCoeffs;i++) {
4276 int j= s->intra_scantable.permutated[i];
4281 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4282 level = (level - 1) | 1;
4285 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4286 level = (level - 1) | 1;
4289 if (level < -2048 || level > 2047)
4290 fprintf(stderr, "unquant error %d %d\n", i, level);
4297 quant_matrix = s->inter_matrix;
4298 for(;i<=nCoeffs;i++) {
4299 int j= s->intra_scantable.permutated[i];
4304 level = (((level << 1) + 1) * qscale *
4305 ((int) (quant_matrix[j]))) >> 4;
4306 level = (level - 1) | 1;
4309 level = (((level << 1) + 1) * qscale *
4310 ((int) (quant_matrix[j]))) >> 4;
4311 level = (level - 1) | 1;
4314 if (level < -2048 || level > 2047)
4315 fprintf(stderr, "unquant error %d %d\n", i, level);
4323 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
4324 DCTELEM *block, int n, int qscale)
4326 int i, level, nCoeffs;
4327 const uint16_t *quant_matrix;
4329 if(s->alternate_scan) nCoeffs= 63;
4330 else nCoeffs= s->block_last_index[n];
4334 block[0] = block[0] * s->y_dc_scale;
4336 block[0] = block[0] * s->c_dc_scale;
4337 quant_matrix = s->intra_matrix;
4338 for(i=1;i<=nCoeffs;i++) {
4339 int j= s->intra_scantable.permutated[i];
4344 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4347 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4350 if (level < -2048 || level > 2047)
4351 fprintf(stderr, "unquant error %d %d\n", i, level);
4359 quant_matrix = s->inter_matrix;
4360 for(;i<=nCoeffs;i++) {
4361 int j= s->intra_scantable.permutated[i];
4366 level = (((level << 1) + 1) * qscale *
4367 ((int) (quant_matrix[j]))) >> 4;
4370 level = (((level << 1) + 1) * qscale *
4371 ((int) (quant_matrix[j]))) >> 4;
4374 if (level < -2048 || level > 2047)
4375 fprintf(stderr, "unquant error %d %d\n", i, level);
4386 static void dct_unquantize_h263_c(MpegEncContext *s,
4387 DCTELEM *block, int n, int qscale)
4389 int i, level, qmul, qadd;
4392 assert(s->block_last_index[n]>=0);
4394 qadd = (qscale - 1) | 1;
4400 block[0] = block[0] * s->y_dc_scale;
4402 block[0] = block[0] * s->c_dc_scale;
4406 nCoeffs= 63; //does not allways use zigzag table
4409 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
4412 for(;i<=nCoeffs;i++) {
4416 level = level * qmul - qadd;
4418 level = level * qmul + qadd;
4421 if (level < -2048 || level > 2047)
4422 fprintf(stderr, "unquant error %d %d\n", i, level);
4430 static const AVOption mpeg4_options[] =
4432 AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
4433 AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
4434 "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
4435 bit_rate_tolerance, 4, 240000000, 8000),
4436 AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
4437 AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
4438 AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
4439 rc_eq, "tex^qComp,option1,options2", 0),
4440 AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
4441 rc_min_rate, 4, 24000000, 0),
4442 AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
4443 rc_max_rate, 4, 24000000, 0),
4444 AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
4445 rc_buffer_aggressivity, 4, 24000000, 0),
4446 AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
4447 rc_initial_cplx, 0., 9999999., 0),
4448 AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
4449 i_quant_factor, 0., 0., 0),
4450 AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
4451 i_quant_factor, -999999., 999999., 0),
4452 AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
4453 dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
4454 AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
4455 lumi_masking, 0., 999999., 0),
4456 AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
4457 temporal_cplx_masking, 0., 999999., 0),
4458 AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
4459 spatial_cplx_masking, 0., 999999., 0),
4460 AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
4461 p_masking, 0., 999999., 0),
4462 AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
4463 dark_masking, 0., 999999., 0),
4464 AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
4465 idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
4467 AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
4469 AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
4472 AVOPTION_CODEC_INT("me_cmp", "ME compare function",
4473 me_cmp, 0, 24000000, 0),
4474 AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
4475 me_sub_cmp, 0, 24000000, 0),
4478 AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
4479 dia_size, 0, 24000000, 0),
4480 AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
4481 last_predictor_count, 0, 24000000, 0),
4483 AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
4484 pre_me, 0, 24000000, 0),
4485 AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
4486 me_pre_cmp, 0, 24000000, 0),
4488 AVOPTION_CODEC_INT("me_range", "maximum ME search range",
4489 me_range, 0, 24000000, 0),
4490 AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
4491 pre_dia_size, 0, 24000000, 0),
4492 AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
4493 me_subpel_quality, 0, 24000000, 0),
4494 AVOPTION_CODEC_INT("me_range", "maximum ME search range",
4495 me_range, 0, 24000000, 0),
4496 AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
4497 flags, CODEC_FLAG_PSNR, 0),
4498 AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
4500 AVOPTION_SUB(avoptions_common),
4504 #ifdef CONFIG_ENCODERS
4506 AVCodec mpeg1video_encoder = {
4509 CODEC_ID_MPEG1VIDEO,
4510 sizeof(MpegEncContext),
4518 AVCodec h263_encoder = {
4522 sizeof(MpegEncContext),
4528 AVCodec h263p_encoder = {
4532 sizeof(MpegEncContext),
4538 AVCodec flv_encoder = {
4542 sizeof(MpegEncContext),
4548 AVCodec rv10_encoder = {
4552 sizeof(MpegEncContext),
4558 AVCodec mpeg4_encoder = {
4562 sizeof(MpegEncContext),
4566 .options = mpeg4_options,
4569 AVCodec msmpeg4v1_encoder = {
4573 sizeof(MpegEncContext),
4577 .options = mpeg4_options,
4580 AVCodec msmpeg4v2_encoder = {
4584 sizeof(MpegEncContext),
4588 .options = mpeg4_options,
4591 AVCodec msmpeg4v3_encoder = {
4595 sizeof(MpegEncContext),
4599 .options = mpeg4_options,
4602 AVCodec wmv1_encoder = {
4606 sizeof(MpegEncContext),
4610 .options = mpeg4_options,
4615 AVCodec mjpeg_encoder = {
4619 sizeof(MpegEncContext),
4625 #endif //CONFIG_ENCODERS