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 #endif //CONFIG_ENCODERS
55 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
58 /* enable all paranoid tests for rounding, overflows, etc... */
64 /* for jpeg fast DCT */
67 static const uint16_t aanscales[64] = {
68 /* precomputed values scaled up by 14 bits */
69 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
70 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
71 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
72 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
73 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
74 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
75 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
76 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
79 static const uint8_t h263_chroma_roundtab[16] = {
80 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
81 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
84 #ifdef CONFIG_ENCODERS
85 static uint16_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
86 static uint8_t default_fcode_tab[MAX_MV*2+1];
88 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
90 static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
91 const uint16_t *quant_matrix, int bias, int qmin, int qmax)
95 for(qscale=qmin; qscale<=qmax; qscale++){
97 if (s->dsp.fdct == ff_jpeg_fdct_islow) {
99 const int j= s->dsp.idct_permutation[i];
100 /* 16 <= qscale * quant_matrix[i] <= 7905 */
101 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
102 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
103 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
105 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
106 (qscale * quant_matrix[j]));
108 } else if (s->dsp.fdct == fdct_ifast) {
110 const int j= s->dsp.idct_permutation[i];
111 /* 16 <= qscale * quant_matrix[i] <= 7905 */
112 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
113 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
114 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
116 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
117 (aanscales[i] * qscale * quant_matrix[j]));
121 const int j= s->dsp.idct_permutation[i];
122 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
123 So 16 <= qscale * quant_matrix[i] <= 7905
124 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
125 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
127 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
128 // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
129 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
131 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
132 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
137 #endif //CONFIG_ENCODERS
139 // move into common.c perhaps
140 #define CHECKED_ALLOCZ(p, size)\
142 p= av_mallocz(size);\
149 void ff_init_scantable(MpegEncContext *s, ScanTable *st, const uint8_t *src_scantable){
153 st->scantable= src_scantable;
157 j = src_scantable[i];
158 st->permutated[i] = s->dsp.idct_permutation[j];
167 j = st->permutated[i];
169 st->raster_end[i]= end;
173 /* init common dct for both encoder and decoder */
174 int DCT_common_init(MpegEncContext *s)
176 s->dct_unquantize_h263 = dct_unquantize_h263_c;
177 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
178 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
180 #ifdef CONFIG_ENCODERS
181 s->dct_quantize= dct_quantize_c;
185 MPV_common_init_mmx(s);
188 MPV_common_init_axp(s);
191 MPV_common_init_mlib(s);
194 MPV_common_init_mmi(s);
197 MPV_common_init_armv4l(s);
200 MPV_common_init_ppc(s);
203 #ifdef CONFIG_ENCODERS
204 s->fast_dct_quantize= s->dct_quantize;
206 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
207 s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
210 #endif //CONFIG_ENCODERS
212 /* load & permutate scantables
213 note: only wmv uses differnt ones
215 ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
216 ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
217 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
218 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
220 s->picture_structure= PICT_FRAME;
226 * allocates a Picture
227 * The pixels are allocated/set by calling get_buffer() if shared=0
229 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
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 , s->mb_num * sizeof(int16_t))
264 CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(int16_t))
265 CHECKED_ALLOCZ(pic->mb_mean , s->mb_num * sizeof(int8_t))
266 CHECKED_ALLOCZ(pic->mb_cmp_score, s->mb_num * sizeof(int32_t))
269 CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(uint8_t)+1) //the +1 is for the slice end check
270 CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(uint8_t))
271 pic->qstride= s->mb_width;
274 //it might be nicer if the application would keep track of these but it would require a API change
275 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
276 s->prev_pict_types[0]= s->pict_type;
277 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
278 pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
281 fail: //for the CHECKED_ALLOCZ macro
286 * deallocates a picture
288 static void free_picture(MpegEncContext *s, Picture *pic){
291 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
292 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
295 av_freep(&pic->mb_var);
296 av_freep(&pic->mc_mb_var);
297 av_freep(&pic->mb_mean);
298 av_freep(&pic->mb_cmp_score);
299 av_freep(&pic->mbskip_table);
300 av_freep(&pic->qscale_table);
302 if(pic->type == FF_BUFFER_TYPE_INTERNAL){
304 av_freep(&pic->base[i]);
307 av_freep(&pic->opaque);
309 }else if(pic->type == FF_BUFFER_TYPE_SHARED){
318 /* init common structure for both encoder and decoder */
319 int MPV_common_init(MpegEncContext *s)
321 int y_size, c_size, yc_size, i;
323 dsputil_init(&s->dsp, s->avctx);
326 s->flags= s->avctx->flags;
328 s->mb_width = (s->width + 15) / 16;
329 s->mb_height = (s->height + 15) / 16;
331 /* set default edge pos, will be overriden in decode_header if needed */
332 s->h_edge_pos= s->mb_width*16;
333 s->v_edge_pos= s->mb_height*16;
335 s->mb_num = s->mb_width * s->mb_height;
337 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
338 c_size = (s->mb_width + 2) * (s->mb_height + 2);
339 yc_size = y_size + 2 * c_size;
341 /* convert fourcc to upper case */
342 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
343 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
344 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
345 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
347 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
348 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
350 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
353 int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
355 /* Allocate MV tables */
356 CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(int16_t))
357 CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
358 CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
359 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
360 CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
361 CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(int16_t))
363 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
364 CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
366 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
367 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
369 if(s->codec_id==CODEC_ID_MPEG4){
370 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
371 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
374 if(s->msmpeg4_version){
375 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
377 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
380 CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(uint8_t))
382 if (s->out_format == FMT_H263 || s->encoding) {
384 /* Allocate MB type table */
385 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(uint8_t))
388 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
389 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
392 if(s->codec_id==CODEC_ID_MPEG4){
393 /* interlaced direct mode decoding tables */
394 CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(int16_t))
395 CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(int8_t))
397 /* 4mv b frame decoding table */
398 //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
399 CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(uint8_t))
400 if (s->out_format == FMT_H263) {
402 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
403 s->ac_val[1] = s->ac_val[0] + y_size;
404 s->ac_val[2] = s->ac_val[1] + c_size;
407 CHECKED_ALLOCZ(s->coded_block, y_size);
409 /* divx501 bitstream reorder buffer */
410 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
412 /* cbp, ac_pred, pred_dir */
413 CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(uint8_t))
414 CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(uint8_t))
417 if (s->h263_pred || s->h263_plus || !s->encoding) {
419 //MN: we need these for error resilience of intra-frames
420 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
421 s->dc_val[1] = s->dc_val[0] + y_size;
422 s->dc_val[2] = s->dc_val[1] + c_size;
423 for(i=0;i<yc_size;i++)
424 s->dc_val[0][i] = 1024;
427 /* which mb is a intra block */
428 CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
429 memset(s->mbintra_table, 1, s->mb_num);
431 /* default structure is frame */
432 s->picture_structure = PICT_FRAME;
434 /* init macroblock skip table */
435 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
436 //Note the +1 is for a quicker mpeg4 slice_end detection
437 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
439 s->block= s->blocks[0];
441 s->parse_context.state= -1;
443 s->context_initialized = 1;
453 /* init common structure for both encoder and decoder */
454 void MPV_common_end(MpegEncContext *s)
458 av_freep(&s->mb_type);
459 av_freep(&s->p_mv_table);
460 av_freep(&s->b_forw_mv_table);
461 av_freep(&s->b_back_mv_table);
462 av_freep(&s->b_bidir_forw_mv_table);
463 av_freep(&s->b_bidir_back_mv_table);
464 av_freep(&s->b_direct_mv_table);
465 av_freep(&s->motion_val);
466 av_freep(&s->dc_val[0]);
467 av_freep(&s->ac_val[0]);
468 av_freep(&s->coded_block);
469 av_freep(&s->mbintra_table);
470 av_freep(&s->cbp_table);
471 av_freep(&s->pred_dir_table);
472 av_freep(&s->me.scratchpad);
473 av_freep(&s->me.map);
474 av_freep(&s->me.score_map);
476 av_freep(&s->mbskip_table);
477 av_freep(&s->prev_pict_types);
478 av_freep(&s->bitstream_buffer);
479 av_freep(&s->tex_pb_buffer);
480 av_freep(&s->pb2_buffer);
481 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
482 av_freep(&s->co_located_type_table);
483 av_freep(&s->field_mv_table);
484 av_freep(&s->field_select_table);
485 av_freep(&s->avctx->stats_out);
486 av_freep(&s->ac_stats);
487 av_freep(&s->error_status_table);
489 for(i=0; i<MAX_PICTURE_COUNT; i++){
490 free_picture(s, &s->picture[i]);
492 s->context_initialized = 0;
495 #ifdef CONFIG_ENCODERS
497 /* init video encoder */
498 int MPV_encode_init(AVCodecContext *avctx)
500 MpegEncContext *s = avctx->priv_data;
503 avctx->pix_fmt = PIX_FMT_YUV420P;
505 s->bit_rate = avctx->bit_rate;
506 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
507 s->frame_rate = avctx->frame_rate;
508 s->width = avctx->width;
509 s->height = avctx->height;
510 if(avctx->gop_size > 600){
511 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
514 s->gop_size = avctx->gop_size;
515 s->rtp_mode = avctx->rtp_mode;
516 s->rtp_payload_size = avctx->rtp_payload_size;
517 if (avctx->rtp_callback)
518 s->rtp_callback = avctx->rtp_callback;
519 s->qmin= avctx->qmin;
520 s->qmax= avctx->qmax;
521 s->max_qdiff= avctx->max_qdiff;
522 s->qcompress= avctx->qcompress;
523 s->qblur= avctx->qblur;
525 s->flags= avctx->flags;
526 s->max_b_frames= avctx->max_b_frames;
527 s->b_frame_strategy= avctx->b_frame_strategy;
528 s->codec_id= avctx->codec->id;
529 s->luma_elim_threshold = avctx->luma_elim_threshold;
530 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
531 s->strict_std_compliance= avctx->strict_std_compliance;
532 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
533 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
534 s->mpeg_quant= avctx->mpeg_quant;
536 if (s->gop_size <= 1) {
543 s->me_method = avctx->me_method;
546 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
548 s->adaptive_quant= ( s->avctx->lumi_masking
549 || s->avctx->dark_masking
550 || s->avctx->temporal_cplx_masking
551 || s->avctx->spatial_cplx_masking
552 || s->avctx->p_masking)
555 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
557 switch(avctx->codec->id) {
558 case CODEC_ID_MPEG1VIDEO:
559 s->out_format = FMT_MPEG1;
560 avctx->delay=0; //FIXME not sure, should check the spec
563 s->out_format = FMT_MJPEG;
564 s->intra_only = 1; /* force intra only for jpeg */
565 s->mjpeg_write_tables = 1; /* write all tables */
566 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
567 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
568 s->mjpeg_vsample[1] = 1; /* the only currently supported values */
569 s->mjpeg_vsample[2] = 1;
570 s->mjpeg_hsample[0] = 2;
571 s->mjpeg_hsample[1] = 1;
572 s->mjpeg_hsample[2] = 1;
573 if (mjpeg_init(s) < 0)
580 if (h263_get_picture_format(s->width, s->height) == 7) {
581 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
584 s->out_format = FMT_H263;
589 s->out_format = FMT_H263;
592 s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
593 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
595 /* These are just to be sure */
601 s->out_format = FMT_H263;
607 s->out_format = FMT_H263;
609 s->unrestricted_mv = 1;
610 s->low_delay= s->max_b_frames ? 0 : 1;
611 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
613 case CODEC_ID_MSMPEG4V1:
614 s->out_format = FMT_H263;
617 s->unrestricted_mv = 1;
618 s->msmpeg4_version= 1;
622 case CODEC_ID_MSMPEG4V2:
623 s->out_format = FMT_H263;
626 s->unrestricted_mv = 1;
627 s->msmpeg4_version= 2;
631 case CODEC_ID_MSMPEG4V3:
632 s->out_format = FMT_H263;
635 s->unrestricted_mv = 1;
636 s->msmpeg4_version= 3;
641 s->out_format = FMT_H263;
644 s->unrestricted_mv = 1;
645 s->msmpeg4_version= 4;
650 s->out_format = FMT_H263;
653 s->unrestricted_mv = 1;
654 s->msmpeg4_version= 5;
663 { /* set up some save defaults, some codecs might override them later */
669 default_mv_penalty= av_mallocz( sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
670 memset(default_mv_penalty, 0, sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
671 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
673 for(i=-16; i<16; i++){
674 default_fcode_tab[i + MAX_MV]= 1;
678 s->me.mv_penalty= default_mv_penalty;
679 s->fcode_tab= default_fcode_tab;
681 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
683 /* dont use mv_penalty table for crap MV as it would be confused */
684 //FIXME remove after fixing / removing old ME
685 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
690 if (MPV_common_init(s) < 0)
695 #ifdef CONFIG_ENCODERS
697 if (s->out_format == FMT_H263)
699 if(s->msmpeg4_version)
700 ff_msmpeg4_encode_init(s);
702 if (s->out_format == FMT_MPEG1)
703 ff_mpeg1_encode_init(s);
706 /* init default q matrix */
708 int j= s->dsp.idct_permutation[i];
710 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
711 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
712 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
713 }else if(s->out_format == FMT_H263){
715 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
719 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
720 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
724 /* precompute matrix */
725 /* for mjpeg, we do include qscale in the matrix */
726 if (s->out_format != FMT_MJPEG) {
727 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
728 s->intra_matrix, s->intra_quant_bias, 1, 31);
729 convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
730 s->inter_matrix, s->inter_quant_bias, 1, 31);
733 if(ff_rate_control_init(s) < 0)
736 s->picture_number = 0;
737 s->picture_in_gop_number = 0;
738 s->fake_picture_number = 0;
739 /* motion detector init */
746 int MPV_encode_end(AVCodecContext *avctx)
748 MpegEncContext *s = avctx->priv_data;
754 ff_rate_control_uninit(s);
757 if (s->out_format == FMT_MJPEG)
763 #endif //CONFIG_ENCODERS
765 void init_rl(RLTable *rl)
767 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
768 uint8_t index_run[MAX_RUN+1];
769 int last, run, level, start, end, i;
771 /* compute max_level[], max_run[] and index_run[] */
772 for(last=0;last<2;last++) {
781 memset(max_level, 0, MAX_RUN + 1);
782 memset(max_run, 0, MAX_LEVEL + 1);
783 memset(index_run, rl->n, MAX_RUN + 1);
784 for(i=start;i<end;i++) {
785 run = rl->table_run[i];
786 level = rl->table_level[i];
787 if (index_run[run] == rl->n)
789 if (level > max_level[run])
790 max_level[run] = level;
791 if (run > max_run[level])
792 max_run[level] = run;
794 rl->max_level[last] = av_malloc(MAX_RUN + 1);
795 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
796 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
797 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
798 rl->index_run[last] = av_malloc(MAX_RUN + 1);
799 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
803 /* draw the edges of width 'w' of an image of size width, height */
804 //FIXME check that this is ok for mpeg4 interlaced
805 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
807 uint8_t *ptr, *last_line;
810 last_line = buf + (height - 1) * wrap;
813 memcpy(buf - (i + 1) * wrap, buf, width);
814 memcpy(last_line + (i + 1) * wrap, last_line, width);
818 for(i=0;i<height;i++) {
819 memset(ptr - w, ptr[0], w);
820 memset(ptr + width, ptr[width-1], w);
825 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
826 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
827 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
828 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
832 static int find_unused_picture(MpegEncContext *s, int shared){
836 for(i=0; i<MAX_PICTURE_COUNT; i++){
837 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
840 for(i=0; i<MAX_PICTURE_COUNT; i++){
841 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
843 for(i=0; i<MAX_PICTURE_COUNT; i++){
844 if(s->picture[i].data[0]==NULL) break;
848 assert(i<MAX_PICTURE_COUNT);
852 /* generic function for encode/decode called before a frame is coded/decoded */
853 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
860 /* mark&release old frames */
861 if (s->pict_type != B_TYPE && s->last_picture.data[0]) {
862 for(i=0; i<MAX_PICTURE_COUNT; i++){
863 //printf("%8X %d %d %X %X\n", s->picture[i].data[0], s->picture[i].type, i, s->next_picture.data[0], s->last_picture.data[0]);
864 if(s->picture[i].data[0] == s->last_picture.data[0]){
865 // s->picture[i].reference=0;
866 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
870 assert(i<MAX_PICTURE_COUNT);
872 /* release forgotten pictures */
873 /* if(mpeg124/h263) */
875 for(i=0; i<MAX_PICTURE_COUNT; i++){
876 if(s->picture[i].data[0] && s->picture[i].data[0] != s->next_picture.data[0] && s->picture[i].reference){
877 fprintf(stderr, "releasing zombie picture\n");
878 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
885 i= find_unused_picture(s, 0);
887 pic= (AVFrame*)&s->picture[i];
888 pic->reference= s->pict_type != B_TYPE;
889 pic->coded_picture_number= s->current_picture.coded_picture_number+1;
891 alloc_picture(s, (Picture*)pic, 0);
893 s->current_picture= s->picture[i];
896 if (s->pict_type != B_TYPE) {
897 s->last_picture= s->next_picture;
898 s->next_picture= s->current_picture;
901 if(s->pict_type != I_TYPE && s->last_picture.data[0]==NULL){
902 fprintf(stderr, "warning: first frame is no keyframe\n");
903 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
907 s->hurry_up= s->avctx->hurry_up;
908 s->error_resilience= avctx->error_resilience;
910 /* set dequantizer, we cant do it during init as it might change for mpeg4
911 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
912 if(s->out_format == FMT_H263){
914 s->dct_unquantize = s->dct_unquantize_mpeg2;
916 s->dct_unquantize = s->dct_unquantize_h263;
918 s->dct_unquantize = s->dct_unquantize_mpeg1;
923 /* generic function for encode/decode called after a frame has been coded/decoded */
924 void MPV_frame_end(MpegEncContext *s)
927 /* draw edge for correct motion prediction if outside */
928 if(s->codec_id!=CODEC_ID_SVQ1){
929 if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
930 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
931 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
932 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
937 s->last_pict_type = s->pict_type;
938 if(s->pict_type!=B_TYPE){
939 s->last_non_b_pict_type= s->pict_type;
942 s->current_picture.quality= s->qscale; //FIXME get average of qscale_table
943 s->current_picture.pict_type= s->pict_type;
944 s->current_picture.key_frame= s->pict_type == I_TYPE;
946 /* copy back current_picture variables */
947 for(i=0; i<MAX_PICTURE_COUNT; i++){
948 if(s->picture[i].data[0] == s->current_picture.data[0]){
949 s->picture[i]= s->current_picture;
953 assert(i<MAX_PICTURE_COUNT);
955 /* release non refernce frames */
956 for(i=0; i<MAX_PICTURE_COUNT; i++){
957 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
958 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
960 if(s->avctx->debug&FF_DEBUG_SKIP){
962 for(y=0; y<s->mb_height; y++){
963 for(x=0; x<s->mb_width; x++){
964 int count= s->mbskip_table[x + y*s->mb_width];
966 printf(" %1d", count);
970 printf("pict type: %d\n", s->pict_type);
974 #ifdef CONFIG_ENCODERS
976 static int get_sae(uint8_t *src, int ref, int stride){
982 acc+= ABS(src[x+y*stride] - ref);
989 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
996 for(y=0; y<h; y+=16){
997 for(x=0; x<w; x+=16){
998 int offset= x + y*stride;
999 int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1000 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1001 int sae = get_sae(src + offset, mean, stride);
1003 acc+= sae + 500 < sad;
1010 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1013 const int encoding_delay= s->max_b_frames;
1016 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1017 if(pic_arg->linesize[0] != s->linesize) direct=0;
1018 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1019 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1021 // printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1024 i= find_unused_picture(s, 1);
1026 pic= (AVFrame*)&s->picture[i];
1030 pic->data[i]= pic_arg->data[i];
1031 pic->linesize[i]= pic_arg->linesize[i];
1033 alloc_picture(s, (Picture*)pic, 1);
1035 i= find_unused_picture(s, 0);
1037 pic= (AVFrame*)&s->picture[i];
1040 alloc_picture(s, (Picture*)pic, 0);
1042 if( pic->data[0] == pic_arg->data[0]
1043 && pic->data[1] == pic_arg->data[1]
1044 && pic->data[2] == pic_arg->data[2]){
1047 int h_chroma_shift, v_chroma_shift;
1049 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1052 int src_stride= pic_arg->linesize[i];
1053 int dst_stride= i ? s->uvlinesize : s->linesize;
1054 int h_shift= i ? h_chroma_shift : 0;
1055 int v_shift= i ? v_chroma_shift : 0;
1056 int w= s->width >>h_shift;
1057 int h= s->height>>v_shift;
1058 uint8_t *src= pic_arg->data[i];
1059 uint8_t *dst= pic->data[i];
1061 if(src_stride==dst_stride)
1062 memcpy(dst, src, src_stride*h);
1065 memcpy(dst, src, w);
1073 pic->quality= pic_arg->quality;
1074 pic->pict_type= pic_arg->pict_type;
1075 pic->pts = pic_arg->pts;
1077 if(s->input_picture[encoding_delay])
1078 pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1080 /* shift buffer entries */
1081 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1082 s->input_picture[i-1]= s->input_picture[i];
1084 s->input_picture[encoding_delay]= (Picture*)pic;
1089 static void select_input_picture(MpegEncContext *s){
1091 const int encoding_delay= s->max_b_frames;
1092 int coded_pic_num=0;
1094 if(s->reordered_input_picture[0])
1095 coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1097 for(i=1; i<MAX_PICTURE_COUNT; i++)
1098 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1099 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1101 /* set next picture types & ordering */
1102 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1103 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){
1104 s->reordered_input_picture[0]= s->input_picture[0];
1105 s->reordered_input_picture[0]->pict_type= I_TYPE;
1106 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1110 if(s->flags&CODEC_FLAG_PASS2){
1111 for(i=0; i<s->max_b_frames+1; i++){
1112 int pict_num= s->input_picture[0]->display_picture_number + i;
1113 int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1114 s->input_picture[i]->pict_type= pict_type;
1116 if(i + 1 >= s->rc_context.num_entries) break;
1120 if(s->input_picture[0]->pict_type){
1121 /* user selected pict_type */
1122 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1123 if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1126 if(b_frames > s->max_b_frames){
1127 fprintf(stderr, "warning, too many bframes in a row\n");
1128 b_frames = s->max_b_frames;
1130 }else if(s->b_frame_strategy==0){
1131 b_frames= s->max_b_frames;
1132 }else if(s->b_frame_strategy==1){
1133 for(i=1; i<s->max_b_frames+1; i++){
1134 if(s->input_picture[i]->b_frame_score==0){
1135 s->input_picture[i]->b_frame_score=
1136 get_intra_count(s, s->input_picture[i ]->data[0],
1137 s->input_picture[i-1]->data[0], s->linesize) + 1;
1140 for(i=0; i<s->max_b_frames; i++){
1141 if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1144 b_frames= FFMAX(0, i-1);
1147 for(i=0; i<b_frames+1; i++){
1148 s->input_picture[i]->b_frame_score=0;
1151 fprintf(stderr, "illegal b frame strategy\n");
1156 //static int b_count=0;
1157 //b_count+= b_frames;
1158 //printf("b_frames: %d\n", b_count);
1160 s->reordered_input_picture[0]= s->input_picture[b_frames];
1161 if( s->picture_in_gop_number + b_frames >= s->gop_size
1162 || s->reordered_input_picture[0]->pict_type== I_TYPE)
1163 s->reordered_input_picture[0]->pict_type= I_TYPE;
1165 s->reordered_input_picture[0]->pict_type= P_TYPE;
1166 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1167 for(i=0; i<b_frames; i++){
1169 s->reordered_input_picture[i+1]= s->input_picture[i];
1170 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1171 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1176 if(s->reordered_input_picture[0]){
1177 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;
1179 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1180 int i= find_unused_picture(s, 0);
1181 Picture *pic= &s->picture[i];
1183 s->new_picture= *s->reordered_input_picture[0];
1185 /* mark us unused / free shared pic */
1187 s->reordered_input_picture[0]->data[i]= NULL;
1188 s->reordered_input_picture[0]->type= 0;
1190 pic->pict_type = s->reordered_input_picture[0]->pict_type;
1191 pic->quality = s->reordered_input_picture[0]->quality;
1192 pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1193 pic->reference = s->reordered_input_picture[0]->reference;
1195 alloc_picture(s, pic, 0);
1197 s->current_picture= *pic;
1199 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1200 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1202 s->new_picture= *s->reordered_input_picture[0];
1205 s->reordered_input_picture[0]->data[i]-=16; //FIXME dirty
1207 s->current_picture= *s->reordered_input_picture[0];
1210 s->picture_number= s->new_picture.display_picture_number;
1211 //printf("dpn:%d\n", s->picture_number);
1213 memset(&s->new_picture, 0, sizeof(Picture));
1217 int MPV_encode_picture(AVCodecContext *avctx,
1218 unsigned char *buf, int buf_size, void *data)
1220 MpegEncContext *s = avctx->priv_data;
1221 AVFrame *pic_arg = data;
1224 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1226 s->picture_in_gop_number++;
1228 load_input_picture(s, pic_arg);
1230 select_input_picture(s);
1233 if(s->new_picture.data[0]){
1235 s->pict_type= s->new_picture.pict_type;
1236 if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1237 s->qscale= (int)(s->new_picture.quality+0.5);
1241 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1242 MPV_frame_start(s, avctx);
1244 encode_picture(s, s->picture_number);
1246 avctx->real_pict_num = s->picture_number;
1247 avctx->header_bits = s->header_bits;
1248 avctx->mv_bits = s->mv_bits;
1249 avctx->misc_bits = s->misc_bits;
1250 avctx->i_tex_bits = s->i_tex_bits;
1251 avctx->p_tex_bits = s->p_tex_bits;
1252 avctx->i_count = s->i_count;
1253 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1254 avctx->skip_count = s->skip_count;
1258 if (s->out_format == FMT_MJPEG)
1259 mjpeg_picture_trailer(s);
1261 if(s->flags&CODEC_FLAG_PASS1)
1262 ff_write_pass1_stats(s);
1265 s->input_picture_number++;
1267 flush_put_bits(&s->pb);
1268 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1270 s->total_bits += s->frame_bits;
1271 avctx->frame_bits = s->frame_bits;
1274 avctx->error[i] += s->current_picture.error[i];
1277 return pbBufPtr(&s->pb) - s->pb.buf;
1280 #endif //CONFIG_ENCODERS
1282 static inline void gmc1_motion(MpegEncContext *s,
1283 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1285 uint8_t **ref_picture, int src_offset)
1288 int offset, src_x, src_y, linesize, uvlinesize;
1289 int motion_x, motion_y;
1292 motion_x= s->sprite_offset[0][0];
1293 motion_y= s->sprite_offset[0][1];
1294 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1295 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1296 motion_x<<=(3-s->sprite_warping_accuracy);
1297 motion_y<<=(3-s->sprite_warping_accuracy);
1298 src_x = clip(src_x, -16, s->width);
1299 if (src_x == s->width)
1301 src_y = clip(src_y, -16, s->height);
1302 if (src_y == s->height)
1305 linesize = s->linesize;
1306 uvlinesize = s->uvlinesize;
1308 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1310 dest_y+=dest_offset;
1311 if(s->flags&CODEC_FLAG_EMU_EDGE){
1312 if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
1313 || src_y + 17 >= s->v_edge_pos){
1314 ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1315 ptr= s->edge_emu_buffer;
1319 if((motion_x|motion_y)&7){
1320 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1321 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1325 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1326 if (s->no_rounding){
1327 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1329 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
1333 if(s->flags&CODEC_FLAG_GRAY) return;
1335 motion_x= s->sprite_offset[1][0];
1336 motion_y= s->sprite_offset[1][1];
1337 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1338 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1339 motion_x<<=(3-s->sprite_warping_accuracy);
1340 motion_y<<=(3-s->sprite_warping_accuracy);
1341 src_x = clip(src_x, -8, s->width>>1);
1342 if (src_x == s->width>>1)
1344 src_y = clip(src_y, -8, s->height>>1);
1345 if (src_y == s->height>>1)
1348 offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1349 ptr = ref_picture[1] + offset;
1350 if(s->flags&CODEC_FLAG_EMU_EDGE){
1351 if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
1352 || src_y + 9 >= s->v_edge_pos>>1){
1353 ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1354 ptr= s->edge_emu_buffer;
1358 s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1360 ptr = ref_picture[2] + offset;
1362 ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1363 ptr= s->edge_emu_buffer;
1365 s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1370 static inline void gmc_motion(MpegEncContext *s,
1371 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1373 uint8_t **ref_picture, int src_offset)
1376 int linesize, uvlinesize;
1377 const int a= s->sprite_warping_accuracy;
1380 linesize = s->linesize;
1381 uvlinesize = s->uvlinesize;
1383 ptr = ref_picture[0] + src_offset;
1385 dest_y+=dest_offset;
1387 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1388 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1390 s->dsp.gmc(dest_y, ptr, linesize, 16,
1393 s->sprite_delta[0][0], s->sprite_delta[0][1],
1394 s->sprite_delta[1][0], s->sprite_delta[1][1],
1395 a+1, (1<<(2*a+1)) - s->no_rounding,
1396 s->h_edge_pos, s->v_edge_pos);
1397 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1398 ox + s->sprite_delta[0][0]*8,
1399 oy + s->sprite_delta[1][0]*8,
1400 s->sprite_delta[0][0], s->sprite_delta[0][1],
1401 s->sprite_delta[1][0], s->sprite_delta[1][1],
1402 a+1, (1<<(2*a+1)) - s->no_rounding,
1403 s->h_edge_pos, s->v_edge_pos);
1405 if(s->flags&CODEC_FLAG_GRAY) return;
1408 dest_cb+=dest_offset>>1;
1409 dest_cr+=dest_offset>>1;
1411 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1412 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1414 ptr = ref_picture[1] + (src_offset>>1);
1415 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1418 s->sprite_delta[0][0], s->sprite_delta[0][1],
1419 s->sprite_delta[1][0], s->sprite_delta[1][1],
1420 a+1, (1<<(2*a+1)) - s->no_rounding,
1421 s->h_edge_pos>>1, s->v_edge_pos>>1);
1423 ptr = ref_picture[2] + (src_offset>>1);
1424 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1427 s->sprite_delta[0][0], s->sprite_delta[0][1],
1428 s->sprite_delta[1][0], s->sprite_delta[1][1],
1429 a+1, (1<<(2*a+1)) - s->no_rounding,
1430 s->h_edge_pos>>1, s->v_edge_pos>>1);
1434 void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h,
1435 int src_x, int src_y, int w, int h){
1437 int start_y, start_x, end_y, end_x;
1438 uint8_t *buf= s->edge_emu_buffer;
1441 src+= (h-1-src_y)*linesize;
1443 }else if(src_y<=-block_h){
1444 src+= (1-block_h-src_y)*linesize;
1450 }else if(src_x<=-block_w){
1451 src+= (1-block_w-src_x);
1455 start_y= FFMAX(0, -src_y);
1456 start_x= FFMAX(0, -src_x);
1457 end_y= FFMIN(block_h, h-src_y);
1458 end_x= FFMIN(block_w, w-src_x);
1460 // copy existing part
1461 for(y=start_y; y<end_y; y++){
1462 for(x=start_x; x<end_x; x++){
1463 buf[x + y*linesize]= src[x + y*linesize];
1468 for(y=0; y<start_y; y++){
1469 for(x=start_x; x<end_x; x++){
1470 buf[x + y*linesize]= buf[x + start_y*linesize];
1475 for(y=end_y; y<block_h; y++){
1476 for(x=start_x; x<end_x; x++){
1477 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1481 for(y=0; y<block_h; y++){
1483 for(x=0; x<start_x; x++){
1484 buf[x + y*linesize]= buf[start_x + y*linesize];
1488 for(x=end_x; x<block_w; x++){
1489 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1495 /* apply one mpeg motion vector to the three components */
1496 static inline void mpeg_motion(MpegEncContext *s,
1497 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1499 uint8_t **ref_picture, int src_offset,
1500 int field_based, op_pixels_func (*pix_op)[4],
1501 int motion_x, int motion_y, int h)
1504 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1507 if(s->quarter_sample)
1513 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1514 src_x = s->mb_x * 16 + (motion_x >> 1);
1515 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1517 /* WARNING: do no forget half pels */
1518 height = s->height >> field_based;
1519 v_edge_pos = s->v_edge_pos >> field_based;
1520 src_x = clip(src_x, -16, s->width);
1521 if (src_x == s->width)
1523 src_y = clip(src_y, -16, height);
1524 if (src_y == height)
1526 linesize = s->linesize << field_based;
1527 uvlinesize = s->uvlinesize << field_based;
1528 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1529 dest_y += dest_offset;
1531 if(s->flags&CODEC_FLAG_EMU_EDGE){
1532 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1533 || src_y + (motion_y&1) + h > v_edge_pos){
1534 ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
1535 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1536 ptr= s->edge_emu_buffer + src_offset;
1540 pix_op[0][dxy](dest_y, ptr, linesize, h);
1542 if(s->flags&CODEC_FLAG_GRAY) return;
1544 if (s->out_format == FMT_H263) {
1546 if ((motion_x & 3) != 0)
1548 if ((motion_y & 3) != 0)
1555 dxy = ((my & 1) << 1) | (mx & 1);
1560 src_x = s->mb_x * 8 + mx;
1561 src_y = s->mb_y * (8 >> field_based) + my;
1562 src_x = clip(src_x, -8, s->width >> 1);
1563 if (src_x == (s->width >> 1))
1565 src_y = clip(src_y, -8, height >> 1);
1566 if (src_y == (height >> 1))
1568 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1569 ptr = ref_picture[1] + offset;
1571 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
1572 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1573 ptr= s->edge_emu_buffer + (src_offset >> 1);
1575 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1577 ptr = ref_picture[2] + offset;
1579 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
1580 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1581 ptr= s->edge_emu_buffer + (src_offset >> 1);
1583 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1586 static inline void qpel_motion(MpegEncContext *s,
1587 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1589 uint8_t **ref_picture, int src_offset,
1590 int field_based, op_pixels_func (*pix_op)[4],
1591 qpel_mc_func (*qpix_op)[16],
1592 int motion_x, int motion_y, int h)
1595 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1598 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1599 src_x = s->mb_x * 16 + (motion_x >> 2);
1600 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1602 height = s->height >> field_based;
1603 v_edge_pos = s->v_edge_pos >> field_based;
1604 src_x = clip(src_x, -16, s->width);
1605 if (src_x == s->width)
1607 src_y = clip(src_y, -16, height);
1608 if (src_y == height)
1610 linesize = s->linesize << field_based;
1611 uvlinesize = s->uvlinesize << field_based;
1612 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1613 dest_y += dest_offset;
1614 //printf("%d %d %d\n", src_x, src_y, dxy);
1616 if(s->flags&CODEC_FLAG_EMU_EDGE){
1617 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1618 || src_y + (motion_y&3) + h > v_edge_pos){
1619 ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
1620 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1621 ptr= s->edge_emu_buffer + src_offset;
1626 qpix_op[0][dxy](dest_y, ptr, linesize);
1628 //damn interlaced mode
1629 //FIXME boundary mirroring is not exactly correct here
1630 qpix_op[1][dxy](dest_y , ptr , linesize);
1631 qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1634 if(s->flags&CODEC_FLAG_GRAY) return;
1639 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
1640 static const int rtab[8]= {0,0,1,1,0,0,0,1};
1641 mx= (motion_x>>1) + rtab[motion_x&7];
1642 my= (motion_y>>1) + rtab[motion_y&7];
1643 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1644 mx= (motion_x>>1)|(motion_x&1);
1645 my= (motion_y>>1)|(motion_y&1);
1653 dxy= (mx&1) | ((my&1)<<1);
1657 src_x = s->mb_x * 8 + mx;
1658 src_y = s->mb_y * (8 >> field_based) + my;
1659 src_x = clip(src_x, -8, s->width >> 1);
1660 if (src_x == (s->width >> 1))
1662 src_y = clip(src_y, -8, height >> 1);
1663 if (src_y == (height >> 1))
1666 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1667 ptr = ref_picture[1] + offset;
1669 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
1670 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1671 ptr= s->edge_emu_buffer + (src_offset >> 1);
1673 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1675 ptr = ref_picture[2] + offset;
1677 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
1678 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1679 ptr= s->edge_emu_buffer + (src_offset >> 1);
1681 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1684 inline int ff_h263_round_chroma(int x){
1686 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1689 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1693 static inline void MPV_motion(MpegEncContext *s,
1694 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1695 int dir, uint8_t **ref_picture,
1696 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
1698 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1700 uint8_t *ptr, *dest;
1706 switch(s->mv_type) {
1710 if(s->real_sprite_warping_points==1){
1711 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1714 gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
1717 }else if(s->quarter_sample){
1718 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1721 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1723 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
1724 ref_picture, pix_op,
1725 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1729 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1732 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1738 if(s->quarter_sample){
1740 motion_x = s->mv[dir][i][0];
1741 motion_y = s->mv[dir][i][1];
1743 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1744 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
1745 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
1747 /* WARNING: do no forget half pels */
1748 src_x = clip(src_x, -16, s->width);
1749 if (src_x == s->width)
1751 src_y = clip(src_y, -16, s->height);
1752 if (src_y == s->height)
1755 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1756 if(s->flags&CODEC_FLAG_EMU_EDGE){
1757 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
1758 || src_y + (motion_y&3) + 8 > s->v_edge_pos){
1759 ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1760 ptr= s->edge_emu_buffer;
1763 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1764 qpix_op[1][dxy](dest, ptr, s->linesize);
1766 mx += s->mv[dir][i][0]/2;
1767 my += s->mv[dir][i][1]/2;
1771 motion_x = s->mv[dir][i][0];
1772 motion_y = s->mv[dir][i][1];
1774 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1775 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1776 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1778 /* WARNING: do no forget half pels */
1779 src_x = clip(src_x, -16, s->width);
1780 if (src_x == s->width)
1782 src_y = clip(src_y, -16, s->height);
1783 if (src_y == s->height)
1786 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1787 if(s->flags&CODEC_FLAG_EMU_EDGE){
1788 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
1789 || src_y + (motion_y&1) + 8 > s->v_edge_pos){
1790 ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1791 ptr= s->edge_emu_buffer;
1794 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1795 pix_op[1][dxy](dest, ptr, s->linesize, 8);
1797 mx += s->mv[dir][i][0];
1798 my += s->mv[dir][i][1];
1802 if(s->flags&CODEC_FLAG_GRAY) break;
1803 /* In case of 8X8, we construct a single chroma motion vector
1804 with a special rounding */
1805 mx= ff_h263_round_chroma(mx);
1806 my= ff_h263_round_chroma(my);
1807 dxy = ((my & 1) << 1) | (mx & 1);
1811 src_x = mb_x * 8 + mx;
1812 src_y = mb_y * 8 + my;
1813 src_x = clip(src_x, -8, s->width/2);
1814 if (src_x == s->width/2)
1816 src_y = clip(src_y, -8, s->height/2);
1817 if (src_y == s->height/2)
1820 offset = (src_y * (s->uvlinesize)) + src_x;
1821 ptr = ref_picture[1] + offset;
1822 if(s->flags&CODEC_FLAG_EMU_EDGE){
1823 if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
1824 || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
1825 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1826 ptr= s->edge_emu_buffer;
1830 pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
1832 ptr = ref_picture[2] + offset;
1834 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1835 ptr= s->edge_emu_buffer;
1837 pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
1840 if (s->picture_structure == PICT_FRAME) {
1841 if(s->quarter_sample){
1843 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1844 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1846 s->mv[dir][0][0], s->mv[dir][0][1], 8);
1848 qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1849 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1851 s->mv[dir][1][0], s->mv[dir][1][1], 8);
1854 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1855 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1857 s->mv[dir][0][0], s->mv[dir][0][1], 8);
1859 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1860 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1862 s->mv[dir][1][0], s->mv[dir][1][1], 8);
1866 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
1867 offset= s->field_select[dir][0] ? s->linesize/2 : 0;
1869 ref_picture= s->current_picture.data;
1870 offset= s->field_select[dir][0] ? s->linesize/2 : -s->linesize/2;
1873 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1874 ref_picture, offset,
1876 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1883 /* put block[] to dest[] */
1884 static inline void put_dct(MpegEncContext *s,
1885 DCTELEM *block, int i, uint8_t *dest, int line_size)
1887 s->dct_unquantize(s, block, i, s->qscale);
1888 s->dsp.idct_put (dest, line_size, block);
1891 /* add block[] to dest[] */
1892 static inline void add_dct(MpegEncContext *s,
1893 DCTELEM *block, int i, uint8_t *dest, int line_size)
1895 if (s->block_last_index[i] >= 0) {
1896 s->dsp.idct_add (dest, line_size, block);
1900 static inline void add_dequant_dct(MpegEncContext *s,
1901 DCTELEM *block, int i, uint8_t *dest, int line_size)
1903 if (s->block_last_index[i] >= 0) {
1904 s->dct_unquantize(s, block, i, s->qscale);
1906 s->dsp.idct_add (dest, line_size, block);
1911 * cleans dc, ac, coded_block for the current non intra MB
1913 void ff_clean_intra_table_entries(MpegEncContext *s)
1915 int wrap = s->block_wrap[0];
1916 int xy = s->block_index[0];
1919 s->dc_val[0][xy + 1 ] =
1920 s->dc_val[0][xy + wrap] =
1921 s->dc_val[0][xy + 1 + wrap] = 1024;
1923 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1924 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1925 if (s->msmpeg4_version>=3) {
1926 s->coded_block[xy ] =
1927 s->coded_block[xy + 1 ] =
1928 s->coded_block[xy + wrap] =
1929 s->coded_block[xy + 1 + wrap] = 0;
1932 wrap = s->block_wrap[4];
1933 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1935 s->dc_val[2][xy] = 1024;
1937 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1938 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1940 s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1943 /* generic function called after a macroblock has been parsed by the
1944 decoder or after it has been encoded by the encoder.
1946 Important variables used:
1947 s->mb_intra : true if intra macroblock
1948 s->mv_dir : motion vector direction
1949 s->mv_type : motion vector type
1950 s->mv : motion vector
1951 s->interlaced_dct : true if interlaced dct used (mpeg2)
1953 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1956 const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
1961 s->current_picture.qscale_table[mb_xy]= s->qscale;
1963 /* update DC predictors for P macroblocks */
1965 if (s->h263_pred || s->h263_aic) {
1966 if(s->mbintra_table[mb_xy])
1967 ff_clean_intra_table_entries(s);
1971 s->last_dc[2] = 128 << s->intra_dc_precision;
1974 else if (s->h263_pred || s->h263_aic)
1975 s->mbintra_table[mb_xy]=1;
1977 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1978 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1979 //FIXME a lot of thet is only needed for !low_delay
1980 const int wrap = s->block_wrap[0];
1981 const int xy = s->block_index[0];
1982 const int mb_index= s->mb_x + s->mb_y*s->mb_width;
1983 if(s->mv_type == MV_TYPE_8X8){
1984 s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
1986 int motion_x, motion_y;
1990 if(s->co_located_type_table)
1991 s->co_located_type_table[mb_index]= 0;
1992 } else if (s->mv_type == MV_TYPE_16X16) {
1993 motion_x = s->mv[0][0][0];
1994 motion_y = s->mv[0][0][1];
1995 if(s->co_located_type_table)
1996 s->co_located_type_table[mb_index]= 0;
1997 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
1999 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
2000 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
2001 motion_x = (motion_x>>1) | (motion_x&1);
2003 s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
2004 s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
2005 s->field_select_table[mb_index][i]= s->field_select[0][i];
2007 s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
2009 /* no update if 8X8 because it has been done during parsing */
2010 s->motion_val[xy][0] = motion_x;
2011 s->motion_val[xy][1] = motion_y;
2012 s->motion_val[xy + 1][0] = motion_x;
2013 s->motion_val[xy + 1][1] = motion_y;
2014 s->motion_val[xy + wrap][0] = motion_x;
2015 s->motion_val[xy + wrap][1] = motion_y;
2016 s->motion_val[xy + 1 + wrap][0] = motion_x;
2017 s->motion_val[xy + 1 + wrap][1] = motion_y;
2021 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2022 uint8_t *dest_y, *dest_cb, *dest_cr;
2023 int dct_linesize, dct_offset;
2024 op_pixels_func (*op_pix)[4];
2025 qpel_mc_func (*op_qpix)[16];
2027 /* avoid copy if macroblock skipped in last frame too */
2028 if (s->pict_type != B_TYPE) {
2029 s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
2032 /* skip only during decoding as we might trash the buffers during encoding a bit */
2034 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2035 const int age= s->current_picture.age;
2041 assert(s->pict_type!=I_TYPE);
2043 (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2044 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2046 /* if previous was skipped too, then nothing to do ! */
2047 if (*mbskip_ptr >= age && s->current_picture.reference){
2050 } else if(!s->current_picture.reference){
2051 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2052 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2054 *mbskip_ptr = 0; /* not skipped */
2059 if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
2060 dest_y = s->current_picture.data[0] + mb_x * 16;
2061 dest_cb = s->current_picture.data[1] + mb_x * 8;
2062 dest_cr = s->current_picture.data[2] + mb_x * 8;
2064 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
2065 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2066 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2069 if (s->interlaced_dct) {
2070 dct_linesize = s->linesize * 2;
2071 dct_offset = s->linesize;
2073 dct_linesize = s->linesize;
2074 dct_offset = s->linesize * 8;
2078 /* motion handling */
2079 /* decoding or more than one mb_type (MC was allready done otherwise) */
2080 if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
2081 if ((!s->no_rounding) || s->pict_type==B_TYPE){
2082 op_pix = s->dsp.put_pixels_tab;
2083 op_qpix= s->dsp.put_qpel_pixels_tab;
2085 op_pix = s->dsp.put_no_rnd_pixels_tab;
2086 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2089 if (s->mv_dir & MV_DIR_FORWARD) {
2090 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2091 op_pix = s->dsp.avg_pixels_tab;
2092 op_qpix= s->dsp.avg_qpel_pixels_tab;
2094 if (s->mv_dir & MV_DIR_BACKWARD) {
2095 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2099 /* skip dequant / idct if we are really late ;) */
2100 if(s->hurry_up>1) return;
2102 /* add dct residue */
2103 if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
2104 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
2105 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2106 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2107 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2108 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2110 if(!(s->flags&CODEC_FLAG_GRAY)){
2111 add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2112 add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
2114 } else if(s->codec_id != CODEC_ID_WMV2){
2115 add_dct(s, block[0], 0, dest_y, dct_linesize);
2116 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2117 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2118 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2120 if(!(s->flags&CODEC_FLAG_GRAY)){
2121 add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2122 add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
2127 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2131 /* dct only in intra block */
2132 if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
2133 put_dct(s, block[0], 0, dest_y, dct_linesize);
2134 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2135 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2136 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2138 if(!(s->flags&CODEC_FLAG_GRAY)){
2139 put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2140 put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
2143 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2144 s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
2145 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2146 s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
2148 if(!(s->flags&CODEC_FLAG_GRAY)){
2149 s->dsp.idct_put(dest_cb, s->uvlinesize, block[4]);
2150 s->dsp.idct_put(dest_cr, s->uvlinesize, block[5]);
2157 #ifdef CONFIG_ENCODERS
2159 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2161 static const char tab[64]=
2173 DCTELEM *block= s->block[n];
2174 const int last_index= s->block_last_index[n];
2179 threshold= -threshold;
2183 /* are all which we could set to zero are allready zero? */
2184 if(last_index<=skip_dc - 1) return;
2186 for(i=0; i<=last_index; i++){
2187 const int j = s->intra_scantable.permutated[i];
2188 const int level = ABS(block[j]);
2190 if(skip_dc && i==0) continue;
2199 if(score >= threshold) return;
2200 for(i=skip_dc; i<=last_index; i++){
2201 const int j = s->intra_scantable.permutated[i];
2204 if(block[0]) s->block_last_index[n]= 0;
2205 else s->block_last_index[n]= -1;
2208 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2211 const int maxlevel= s->max_qcoeff;
2212 const int minlevel= s->min_qcoeff;
2215 i=1; //skip clipping of intra dc
2219 for(;i<=last_index; i++){
2220 const int j= s->intra_scantable.permutated[i];
2221 int level = block[j];
2223 if (level>maxlevel) level=maxlevel;
2224 else if(level<minlevel) level=minlevel;
2231 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2236 for(x=0; x<16; x+=4){
2237 score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
2238 +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2246 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2251 for(x=0; x<16; x++){
2252 score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2261 #define SQ(a) ((a)*(a))
2263 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2268 for(x=0; x<16; x+=4){
2269 score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
2270 +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2278 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2283 for(x=0; x<16; x++){
2284 score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2295 #endif //CONFIG_ENCODERS
2299 * @param h is the normal height, this will be reduced automatically if needed for the last row
2301 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2302 if ( s->avctx->draw_horiz_band
2303 && (s->last_picture.data[0] || s->low_delay) ) {
2304 uint8_t *src_ptr[3];
2306 h= FFMIN(h, s->height - y);
2308 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME)
2311 offset = y * s->linesize;
2313 if(s->pict_type==B_TYPE || s->low_delay){
2314 src_ptr[0] = s->current_picture.data[0] + offset;
2315 src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
2316 src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
2318 src_ptr[0] = s->last_picture.data[0] + offset;
2319 src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
2320 src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
2324 s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2329 #ifdef CONFIG_ENCODERS
2331 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2333 const int mb_x= s->mb_x;
2334 const int mb_y= s->mb_y;
2337 int dct_offset = s->linesize*8; //default for progressive frames
2339 for(i=0; i<6; i++) skip_dct[i]=0;
2341 if(s->adaptive_quant){
2342 s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
2344 if(s->out_format==FMT_H263){
2345 if (s->dquant> 2) s->dquant= 2;
2346 else if(s->dquant<-2) s->dquant=-2;
2349 if(s->codec_id==CODEC_ID_MPEG4){
2351 assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2353 if(s->mv_dir&MV_DIRECT)
2357 s->qscale+= s->dquant;
2358 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2359 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2367 wrap_y = s->linesize;
2368 ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2370 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2371 ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2372 ptr= s->edge_emu_buffer;
2376 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2377 int progressive_score, interlaced_score;
2379 progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2380 interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
2382 if(progressive_score > interlaced_score + 100){
2383 s->interlaced_dct=1;
2388 s->interlaced_dct=0;
2391 s->dsp.get_pixels(s->block[0], ptr , wrap_y);
2392 s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
2393 s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
2394 s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2396 if(s->flags&CODEC_FLAG_GRAY){
2400 int wrap_c = s->uvlinesize;
2401 ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2403 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2404 ptr= s->edge_emu_buffer;
2406 s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2408 ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2410 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2411 ptr= s->edge_emu_buffer;
2413 s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2416 op_pixels_func (*op_pix)[4];
2417 qpel_mc_func (*op_qpix)[16];
2418 uint8_t *dest_y, *dest_cb, *dest_cr;
2419 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2423 dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
2424 dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
2425 dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
2426 wrap_y = s->linesize;
2427 wrap_c = s->uvlinesize;
2428 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2429 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2430 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2432 if ((!s->no_rounding) || s->pict_type==B_TYPE){
2433 op_pix = s->dsp.put_pixels_tab;
2434 op_qpix= s->dsp.put_qpel_pixels_tab;
2436 op_pix = s->dsp.put_no_rnd_pixels_tab;
2437 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2440 if (s->mv_dir & MV_DIR_FORWARD) {
2441 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2442 op_pix = s->dsp.avg_pixels_tab;
2443 op_qpix= s->dsp.avg_qpel_pixels_tab;
2445 if (s->mv_dir & MV_DIR_BACKWARD) {
2446 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2449 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2450 ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2451 ptr_y= s->edge_emu_buffer;
2455 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2456 int progressive_score, interlaced_score;
2458 progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
2459 + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
2460 interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
2461 + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
2463 if(progressive_score > interlaced_score + 600){
2464 s->interlaced_dct=1;
2469 s->interlaced_dct=0;
2472 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
2473 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2474 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
2475 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2477 if(s->flags&CODEC_FLAG_GRAY){
2482 ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2483 ptr_cb= s->edge_emu_buffer;
2485 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2487 ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2488 ptr_cr= s->edge_emu_buffer;
2490 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2492 /* pre quantization */
2493 if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
2495 if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2496 if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2497 if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2498 if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2499 if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
2500 if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
2506 if(skip_dct[i]) num++;
2509 if(s->mb_x==0 && s->mb_y==0){
2511 printf("%6d %1d\n", stat[i], i);
2524 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
2525 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2527 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
2528 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
2529 s->qscale, adap_parm, s->qscale*adap_parm,
2530 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
2533 /* DCT & quantize */
2534 if(s->out_format==FMT_MJPEG){
2537 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2538 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2544 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2545 // FIXME we could decide to change to quantizer instead of clipping
2546 // JS: I don't think that would be a good idea it could lower quality instead
2547 // of improve it. Just INTRADC clipping deserves changes in quantizer
2548 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2550 s->block_last_index[i]= -1;
2552 if(s->luma_elim_threshold && !s->mb_intra)
2554 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2555 if(s->chroma_elim_threshold && !s->mb_intra)
2557 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2560 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
2561 s->block_last_index[4]=
2562 s->block_last_index[5]= 0;
2564 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
2567 /* huffman encode */
2568 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2569 case CODEC_ID_MPEG1VIDEO:
2570 mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
2572 case CODEC_ID_MPEG4:
2573 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2574 case CODEC_ID_MSMPEG4V2:
2575 case CODEC_ID_MSMPEG4V3:
2577 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2579 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
2581 case CODEC_ID_H263P:
2583 h263_encode_mb(s, s->block, motion_x, motion_y); break;
2585 case CODEC_ID_MJPEG:
2586 mjpeg_encode_mb(s, s->block); break;
2592 #endif //CONFIG_ENCODERS
2595 * combines the (truncated) bitstream to a complete frame
2596 * @returns -1 if no complete frame could be created
2598 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
2599 ParseContext *pc= &s->parse_context;
2601 pc->last_index= pc->index;
2604 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2606 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
2607 pc->index += *buf_size;
2612 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2614 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
2617 *buf_size= pc->last_index + next;
2623 #ifdef CONFIG_ENCODERS
2624 void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
2626 int bytes= length>>4;
2627 int bits= length&15;
2630 if(length==0) return;
2632 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
2633 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
2636 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2639 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2642 d->mb_incr= s->mb_incr;
2644 d->last_dc[i]= s->last_dc[i];
2647 d->mv_bits= s->mv_bits;
2648 d->i_tex_bits= s->i_tex_bits;
2649 d->p_tex_bits= s->p_tex_bits;
2650 d->i_count= s->i_count;
2651 d->f_count= s->f_count;
2652 d->b_count= s->b_count;
2653 d->skip_count= s->skip_count;
2654 d->misc_bits= s->misc_bits;
2657 d->mb_skiped= s->mb_skiped;
2658 d->qscale= s->qscale;
2661 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2664 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2665 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2668 d->mb_incr= s->mb_incr;
2670 d->last_dc[i]= s->last_dc[i];
2673 d->mv_bits= s->mv_bits;
2674 d->i_tex_bits= s->i_tex_bits;
2675 d->p_tex_bits= s->p_tex_bits;
2676 d->i_count= s->i_count;
2677 d->f_count= s->f_count;
2678 d->b_count= s->b_count;
2679 d->skip_count= s->skip_count;
2680 d->misc_bits= s->misc_bits;
2682 d->mb_intra= s->mb_intra;
2683 d->mb_skiped= s->mb_skiped;
2684 d->mv_type= s->mv_type;
2685 d->mv_dir= s->mv_dir;
2687 if(s->data_partitioning){
2689 d->tex_pb= s->tex_pb;
2693 d->block_last_index[i]= s->block_last_index[i];
2694 d->interlaced_dct= s->interlaced_dct;
2695 d->qscale= s->qscale;
2698 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2699 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2700 int *dmin, int *next_block, int motion_x, int motion_y)
2704 copy_context_before_encode(s, backup, type);
2706 s->block= s->blocks[*next_block];
2707 s->pb= pb[*next_block];
2708 if(s->data_partitioning){
2709 s->pb2 = pb2 [*next_block];
2710 s->tex_pb= tex_pb[*next_block];
2713 encode_mb(s, motion_x, motion_y);
2715 bits_count= get_bit_count(&s->pb);
2716 if(s->data_partitioning){
2717 bits_count+= get_bit_count(&s->pb2);
2718 bits_count+= get_bit_count(&s->tex_pb);
2721 if(bits_count<*dmin){
2725 copy_context_after_encode(best, s, type);
2729 static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2730 uint32_t *sq = squareTbl + 256;
2735 return s->dsp.sse[0](NULL, src1, src2, stride);
2736 else if(w==8 && h==8)
2737 return s->dsp.sse[1](NULL, src1, src2, stride);
2741 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2750 static void encode_picture(MpegEncContext *s, int picture_number)
2752 int mb_x, mb_y, pdif = 0;
2755 MpegEncContext best_s, backup_s;
2756 uint8_t bit_buf[2][3000];
2757 uint8_t bit_buf2[2][3000];
2758 uint8_t bit_buf_tex[2][3000];
2759 PutBitContext pb[2], pb2[2], tex_pb[2];
2762 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
2763 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
2764 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
2767 s->picture_number = picture_number;
2772 s->block_wrap[3]= s->mb_width*2 + 2;
2774 s->block_wrap[5]= s->mb_width + 2;
2776 /* Reset the average MB variance */
2777 s->current_picture.mb_var_sum = 0;
2778 s->current_picture.mc_mb_var_sum = 0;
2781 /* we need to initialize some time vars before we can encode b-frames */
2782 // RAL: Condition added for MPEG1VIDEO
2783 if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
2784 ff_set_mpeg4_time(s, s->picture_number);
2787 s->scene_change_score=0;
2789 s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
2791 if(s->pict_type==I_TYPE){
2792 if(s->msmpeg4_version) s->no_rounding=1;
2793 else s->no_rounding=0;
2794 }else if(s->pict_type!=B_TYPE){
2795 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
2796 s->no_rounding ^= 1;
2799 /* Estimate motion for every MB */
2800 s->mb_intra=0; //for the rate distoration & bit compare functions
2801 if(s->pict_type != I_TYPE){
2802 if(s->pict_type != B_TYPE){
2803 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
2805 s->me.dia_size= s->avctx->pre_dia_size;
2807 for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
2808 for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
2811 ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
2818 s->me.dia_size= s->avctx->dia_size;
2819 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2820 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2821 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2822 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2823 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2824 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2827 s->block_index[0]+=2;
2828 s->block_index[1]+=2;
2829 s->block_index[2]+=2;
2830 s->block_index[3]+=2;
2832 /* compute motion vector & mb_type and store in context */
2833 if(s->pict_type==B_TYPE)
2834 ff_estimate_b_frame_motion(s, mb_x, mb_y);
2836 ff_estimate_p_frame_motion(s, mb_x, mb_y);
2839 }else /* if(s->pict_type == I_TYPE) */{
2841 //FIXME do we need to zero them?
2842 memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2843 memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_width+2)*(s->mb_height+2)*2);
2844 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
2846 if(!s->fixed_qscale){
2847 /* finding spatial complexity for I-frame rate control */
2848 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2849 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2852 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
2854 int sum = s->dsp.pix_sum(pix, s->linesize);
2856 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
2858 s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
2859 s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
2860 s->current_picture.mb_var_sum += varc;
2867 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
2868 s->pict_type= I_TYPE;
2869 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
2870 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
2874 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
2875 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
2877 ff_fix_long_p_mvs(s);
2880 if(s->pict_type==B_TYPE){
2883 a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
2884 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
2885 s->f_code = FFMAX(a, b);
2887 a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
2888 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
2889 s->b_code = FFMAX(a, b);
2891 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
2892 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
2893 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
2894 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
2898 if (s->fixed_qscale)
2899 s->frame_qscale = s->current_picture.quality;
2901 s->frame_qscale = ff_rate_estimate_qscale(s);
2903 if(s->adaptive_quant){
2905 switch(s->codec_id){
2906 case CODEC_ID_MPEG4:
2907 ff_clean_mpeg4_qscales(s);
2910 case CODEC_ID_H263P:
2911 ff_clean_h263_qscales(s);
2916 s->qscale= s->current_picture.qscale_table[0];
2918 s->qscale= (int)(s->frame_qscale + 0.5);
2920 if (s->out_format == FMT_MJPEG) {
2921 /* for mjpeg, we do include qscale in the matrix */
2922 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2924 int j= s->dsp.idct_permutation[i];
2926 s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2928 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
2929 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
2932 //FIXME var duplication
2933 s->current_picture.key_frame= s->pict_type == I_TYPE;
2934 s->current_picture.pict_type= s->pict_type;
2936 if(s->current_picture.key_frame)
2937 s->picture_in_gop_number=0;
2939 s->last_bits= get_bit_count(&s->pb);
2940 switch(s->out_format) {
2942 mjpeg_picture_header(s);
2946 if (s->codec_id == CODEC_ID_WMV2)
2947 ff_wmv2_encode_picture_header(s, picture_number);
2948 else if (s->h263_msmpeg4)
2949 msmpeg4_encode_picture_header(s, picture_number);
2950 else if (s->h263_pred)
2951 mpeg4_encode_picture_header(s, picture_number);
2952 else if (s->h263_rv10)
2953 rv10_encode_picture_header(s, picture_number);
2955 h263_encode_picture_header(s, picture_number);
2959 mpeg1_encode_picture_header(s, picture_number);
2962 bits= get_bit_count(&s->pb);
2963 s->header_bits= bits - s->last_bits;
2975 /* init last dc values */
2976 /* note: quant matrix value (8) is implied here */
2977 s->last_dc[i] = 128;
2979 s->current_picture.error[i] = 0;
2982 s->last_mv[0][0][0] = 0;
2983 s->last_mv[0][0][1] = 0;
2984 s->last_mv[1][0][0] = 0;
2985 s->last_mv[1][0][1] = 0;
2990 if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
2991 s->gob_index = ff_h263_get_gob_height(s);
2993 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2994 ff_mpeg4_init_partitions(s);
2999 s->first_slice_line = 1;
3000 s->ptr_lastgob = s->pb.buf;
3001 s->ptr_last_mb_line = s->pb.buf;
3002 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3003 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3004 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
3006 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3007 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3008 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3009 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3010 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
3011 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
3012 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3013 int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
3014 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
3020 s->block_index[0]+=2;
3021 s->block_index[1]+=2;
3022 s->block_index[2]+=2;
3023 s->block_index[3]+=2;
3024 s->block_index[4]++;
3025 s->block_index[5]++;
3027 /* write gob / video packet header */
3030 int current_packet_size, is_gob_start;
3032 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
3035 if(s->codec_id==CODEC_ID_MPEG4){
3036 if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
3037 && s->mb_y + s->mb_x>0){
3039 if(s->partitioned_frame){
3040 ff_mpeg4_merge_partitions(s);
3041 ff_mpeg4_init_partitions(s);
3043 ff_mpeg4_encode_video_packet_header(s);
3045 if(s->flags&CODEC_FLAG_PASS1){
3046 int bits= get_bit_count(&s->pb);
3047 s->misc_bits+= bits - s->last_bits;
3050 ff_mpeg4_clean_buffers(s);
3054 if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
3055 && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
3057 h263_encode_gob_header(s, mb_y);
3063 s->ptr_lastgob = pbBufPtr(&s->pb);
3064 s->first_slice_line=1;
3065 s->resync_mb_x=mb_x;
3066 s->resync_mb_y=mb_y;
3071 if( (s->resync_mb_x == s->mb_x)
3072 && s->resync_mb_y+1 == s->mb_y){
3073 s->first_slice_line=0;
3076 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
3078 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3080 copy_context_before_encode(&backup_s, s, -1);
3082 best_s.data_partitioning= s->data_partitioning;
3083 best_s.partitioned_frame= s->partitioned_frame;
3084 if(s->data_partitioning){
3085 backup_s.pb2= s->pb2;
3086 backup_s.tex_pb= s->tex_pb;
3089 if(mb_type&MB_TYPE_INTER){
3090 s->mv_dir = MV_DIR_FORWARD;
3091 s->mv_type = MV_TYPE_16X16;
3093 s->mv[0][0][0] = s->p_mv_table[xy][0];
3094 s->mv[0][0][1] = s->p_mv_table[xy][1];
3095 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
3096 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3098 if(mb_type&MB_TYPE_INTER4V){
3099 s->mv_dir = MV_DIR_FORWARD;
3100 s->mv_type = MV_TYPE_8X8;
3103 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3104 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3106 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
3107 &dmin, &next_block, 0, 0);
3109 if(mb_type&MB_TYPE_FORWARD){
3110 s->mv_dir = MV_DIR_FORWARD;
3111 s->mv_type = MV_TYPE_16X16;
3113 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3114 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3115 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
3116 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3118 if(mb_type&MB_TYPE_BACKWARD){
3119 s->mv_dir = MV_DIR_BACKWARD;
3120 s->mv_type = MV_TYPE_16X16;
3122 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3123 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3124 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3125 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3127 if(mb_type&MB_TYPE_BIDIR){
3128 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3129 s->mv_type = MV_TYPE_16X16;
3131 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3132 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3133 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3134 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3135 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
3136 &dmin, &next_block, 0, 0);
3138 if(mb_type&MB_TYPE_DIRECT){
3139 int mx= s->b_direct_mv_table[xy][0];
3140 int my= s->b_direct_mv_table[xy][1];
3142 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3145 ff_mpeg4_set_direct_mv(s, mx, my);
3147 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
3148 &dmin, &next_block, mx, my);
3150 if(mb_type&MB_TYPE_INTRA){
3152 s->mv_type = MV_TYPE_16X16;
3156 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
3157 &dmin, &next_block, 0, 0);
3158 /* force cleaning of ac/dc pred stuff if needed ... */
3159 if(s->h263_pred || s->h263_aic)
3160 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
3162 copy_context_after_encode(s, &best_s, -1);
3164 pb_bits_count= get_bit_count(&s->pb);
3165 flush_put_bits(&s->pb);
3166 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3169 if(s->data_partitioning){
3170 pb2_bits_count= get_bit_count(&s->pb2);
3171 flush_put_bits(&s->pb2);
3172 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3173 s->pb2= backup_s.pb2;
3175 tex_pb_bits_count= get_bit_count(&s->tex_pb);
3176 flush_put_bits(&s->tex_pb);
3177 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3178 s->tex_pb= backup_s.tex_pb;
3180 s->last_bits= get_bit_count(&s->pb);
3182 int motion_x, motion_y;
3184 int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_width];
3186 if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){
3187 /* get luma score */
3188 if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
3189 intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_width]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
3193 int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_width]; //FIXME
3196 dest_y = s->new_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
3198 for(i=0; i<16; i++){
3199 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
3200 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
3201 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
3202 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
3206 intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
3208 /* printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8,
3209 s->current_picture.mb_var[mb_x + mb_y*s->mb_width],
3210 s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_width]);*/
3213 /* get chroma score */
3214 if(s->avctx->mb_cmp&FF_CMP_CHROMA){
3222 if(s->out_format == FMT_H263){
3223 mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
3225 mean= (s->last_dc[i] + 4)>>3;
3227 dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
3231 *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
3232 *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
3235 intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
3240 switch(s->avctx->mb_cmp&0xFF){
3243 intra_score+= 32*s->qscale;
3246 intra_score+= 24*s->qscale*s->qscale;
3249 intra_score+= 96*s->qscale;
3252 intra_score+= 48*s->qscale;
3259 intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
3263 if(intra_score < inter_score)
3264 mb_type= MB_TYPE_INTRA;
3267 s->mv_type=MV_TYPE_16X16;
3268 // only one MB-Type possible
3274 motion_x= s->mv[0][0][0] = 0;
3275 motion_y= s->mv[0][0][1] = 0;
3278 s->mv_dir = MV_DIR_FORWARD;
3280 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3281 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3283 case MB_TYPE_INTER4V:
3284 s->mv_dir = MV_DIR_FORWARD;
3285 s->mv_type = MV_TYPE_8X8;
3288 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3289 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3291 motion_x= motion_y= 0;
3293 case MB_TYPE_DIRECT:
3294 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3296 motion_x=s->b_direct_mv_table[xy][0];
3297 motion_y=s->b_direct_mv_table[xy][1];
3299 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3303 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3307 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3308 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3309 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3310 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3312 case MB_TYPE_BACKWARD:
3313 s->mv_dir = MV_DIR_BACKWARD;
3315 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3316 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3318 case MB_TYPE_FORWARD:
3319 s->mv_dir = MV_DIR_FORWARD;
3321 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3322 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3323 // printf(" %d %d ", motion_x, motion_y);
3326 motion_x=motion_y=0; //gcc warning fix
3327 printf("illegal MB type\n");
3330 encode_mb(s, motion_x, motion_y);
3332 // RAL: Update last macrobloc type
3333 s->last_mv_dir = s->mv_dir;
3336 /* clean the MV table in IPS frames for direct mode in B frames */
3337 if(s->mb_intra /* && I,P,S_TYPE */){
3338 s->p_mv_table[xy][0]=0;
3339 s->p_mv_table[xy][1]=0;
3342 MPV_decode_mb(s, s->block);
3344 if(s->flags&CODEC_FLAG_PSNR){
3348 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3349 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3351 s->current_picture.error[0] += sse(
3353 s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3354 s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3356 s->current_picture.error[1] += sse(
3358 s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3359 s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3360 w>>1, h>>1, s->uvlinesize);
3361 s->current_picture.error[2] += sse(
3363 s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3364 s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3365 w>>1, h>>1, s->uvlinesize);
3367 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
3371 /* Obtain average mb_row size for RTP */
3374 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
3376 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
3378 s->ptr_last_mb_line = pbBufPtr(&s->pb);
3384 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
3385 ff_mpeg4_merge_partitions(s);
3387 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
3388 msmpeg4_encode_ext_header(s);
3390 if(s->codec_id==CODEC_ID_MPEG4)
3391 ff_mpeg4_stuffing(&s->pb);
3394 //if (s->gob_number)
3395 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
3397 /* Send the last GOB if RTP */
3399 flush_put_bits(&s->pb);
3400 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
3401 /* Call the RTP callback to send the last GOB */
3402 if (s->rtp_callback)
3403 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
3404 s->ptr_lastgob = pbBufPtr(&s->pb);
3405 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
3409 static int dct_quantize_trellis_c(MpegEncContext *s,
3410 DCTELEM *block, int n,
3411 int qscale, int *overflow){
3413 const uint8_t *scantable= s->intra_scantable.scantable;
3415 unsigned int threshold1, threshold2;
3425 int coeff_count[64];
3426 int lambda, qmul, qadd, start_i, last_non_zero, i;
3427 const int esc_length= s->ac_esc_length;
3429 uint8_t * last_length;
3433 s->dsp.fdct (block);
3436 qadd= ((qscale-1)|1)*8;
3447 /* For AIC we skip quant/dequant of INTRADC */
3452 /* note: block[0] is assumed to be positive */
3453 block[0] = (block[0] + (q >> 1)) / q;
3456 qmat = s->q_intra_matrix[qscale];
3457 if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
3458 bias= 1<<(QMAT_SHIFT-1);
3459 length = s->intra_ac_vlc_length;
3460 last_length= s->intra_ac_vlc_last_length;
3464 qmat = s->q_inter_matrix[qscale];
3465 length = s->inter_ac_vlc_length;
3466 last_length= s->inter_ac_vlc_last_length;
3469 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3470 threshold2= (threshold1<<1);
3472 for(i=start_i; i<64; i++) {
3473 const int j = scantable[i];
3474 const int k= i-start_i;
3475 int level = block[j];
3476 level = level * qmat[j];
3478 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3479 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3480 if(((unsigned)(level+threshold1))>threshold2){
3482 level= (bias + level)>>QMAT_SHIFT;
3484 coeff[1][k]= level-1;
3485 // coeff[2][k]= level-2;
3487 level= (bias - level)>>QMAT_SHIFT;
3488 coeff[0][k]= -level;
3489 coeff[1][k]= -level+1;
3490 // coeff[2][k]= -level+2;
3492 coeff_count[k]= FFMIN(level, 2);
3496 coeff[0][k]= (level>>31)|1;
3501 *overflow= s->max_qcoeff < max; //overflow might have happend
3503 if(last_non_zero < start_i){
3504 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3505 return last_non_zero;
3508 lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
3511 for(i=0; i<=last_non_zero - start_i; i++){
3512 int level_index, run, j;
3513 const int dct_coeff= block[ scantable[i + start_i] ];
3514 const int zero_distoration= dct_coeff*dct_coeff;
3515 int best_score=256*256*256*120;
3517 last_score += zero_distoration;
3518 for(level_index=0; level_index < coeff_count[i]; level_index++){
3520 int level= coeff[level_index][i];
3525 if(s->out_format == FMT_H263){
3527 unquant_coeff= level*qmul + qadd;
3529 unquant_coeff= level*qmul - qadd;
3532 j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
3535 unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
3536 unquant_coeff = -((unquant_coeff - 1) | 1);
3538 unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
3539 unquant_coeff = (unquant_coeff - 1) | 1;
3543 unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3544 unquant_coeff = -((unquant_coeff - 1) | 1);
3546 unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3547 unquant_coeff = (unquant_coeff - 1) | 1;
3553 distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
3555 if((level&(~127)) == 0){
3556 for(run=0; run<=i - left_limit; run++){
3557 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3558 score += score_tab[i-run];
3560 if(score < best_score){
3562 score_tab[i+1]= score;
3564 level_tab[i+1]= level-64;
3568 if(s->out_format == FMT_H263){
3569 for(run=0; run<=i - left_limit; run++){
3570 int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3571 score += score_tab[i-run];
3572 if(score < last_score){
3575 last_level= level-64;
3581 distoration += esc_length*lambda;
3582 for(run=0; run<=i - left_limit; run++){
3583 int score= distoration + score_tab[i-run];
3585 if(score < best_score){
3587 score_tab[i+1]= score;
3589 level_tab[i+1]= level-64;
3593 if(s->out_format == FMT_H263){
3594 for(run=0; run<=i - left_limit; run++){
3595 int score= distoration + score_tab[i-run];
3596 if(score < last_score){
3599 last_level= level-64;
3607 for(j=left_limit; j<=i; j++){
3608 score_tab[j] += zero_distoration;
3610 score_limit+= zero_distoration;
3611 if(score_tab[i+1] < score_limit)
3612 score_limit= score_tab[i+1];
3614 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3615 while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
3618 //FIXME add some cbp penalty
3620 if(s->out_format != FMT_H263){
3621 last_score= 256*256*256*120;
3622 for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
3623 int score= score_tab[i];
3624 if(i) score += lambda*2; //FIXME exacter?
3626 if(score < last_score){
3629 last_level= level_tab[i];
3630 last_run= run_tab[i];
3635 last_non_zero= last_i - 1 + start_i;
3636 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3638 if(last_non_zero < start_i)
3639 return last_non_zero;
3643 //FIXME use permutated scantable
3644 block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
3647 for(;i>0 ; i -= run_tab[i] + 1){
3648 const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
3650 block[j]= level_tab[i];
3654 return last_non_zero;
3657 static int dct_quantize_c(MpegEncContext *s,
3658 DCTELEM *block, int n,
3659 int qscale, int *overflow)
3661 int i, j, level, last_non_zero, q;
3663 const uint8_t *scantable= s->intra_scantable.scantable;
3666 unsigned int threshold1, threshold2;
3668 s->dsp.fdct (block);
3678 /* For AIC we skip quant/dequant of INTRADC */
3681 /* note: block[0] is assumed to be positive */
3682 block[0] = (block[0] + (q >> 1)) / q;
3685 qmat = s->q_intra_matrix[qscale];
3686 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3690 qmat = s->q_inter_matrix[qscale];
3691 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3693 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3694 threshold2= (threshold1<<1);
3699 level = level * qmat[j];
3701 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3702 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3703 if(((unsigned)(level+threshold1))>threshold2){
3705 level= (bias + level)>>QMAT_SHIFT;
3708 level= (bias - level)>>QMAT_SHIFT;
3717 *overflow= s->max_qcoeff < max; //overflow might have happend
3719 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3720 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
3721 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
3723 return last_non_zero;
3726 #endif //CONFIG_ENCODERS
3728 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
3729 DCTELEM *block, int n, int qscale)
3731 int i, level, nCoeffs;
3732 const uint16_t *quant_matrix;
3734 nCoeffs= s->block_last_index[n];
3738 block[0] = block[0] * s->y_dc_scale;
3740 block[0] = block[0] * s->c_dc_scale;
3741 /* XXX: only mpeg1 */
3742 quant_matrix = s->intra_matrix;
3743 for(i=1;i<=nCoeffs;i++) {
3744 int j= s->intra_scantable.permutated[i];
3749 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3750 level = (level - 1) | 1;
3753 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3754 level = (level - 1) | 1;
3757 if (level < -2048 || level > 2047)
3758 fprintf(stderr, "unquant error %d %d\n", i, level);
3765 quant_matrix = s->inter_matrix;
3766 for(;i<=nCoeffs;i++) {
3767 int j= s->intra_scantable.permutated[i];
3772 level = (((level << 1) + 1) * qscale *
3773 ((int) (quant_matrix[j]))) >> 4;
3774 level = (level - 1) | 1;
3777 level = (((level << 1) + 1) * qscale *
3778 ((int) (quant_matrix[j]))) >> 4;
3779 level = (level - 1) | 1;
3782 if (level < -2048 || level > 2047)
3783 fprintf(stderr, "unquant error %d %d\n", i, level);
3791 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
3792 DCTELEM *block, int n, int qscale)
3794 int i, level, nCoeffs;
3795 const uint16_t *quant_matrix;
3797 if(s->alternate_scan) nCoeffs= 63;
3798 else nCoeffs= s->block_last_index[n];
3802 block[0] = block[0] * s->y_dc_scale;
3804 block[0] = block[0] * s->c_dc_scale;
3805 quant_matrix = s->intra_matrix;
3806 for(i=1;i<=nCoeffs;i++) {
3807 int j= s->intra_scantable.permutated[i];
3812 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3815 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3818 if (level < -2048 || level > 2047)
3819 fprintf(stderr, "unquant error %d %d\n", i, level);
3827 quant_matrix = s->inter_matrix;
3828 for(;i<=nCoeffs;i++) {
3829 int j= s->intra_scantable.permutated[i];
3834 level = (((level << 1) + 1) * qscale *
3835 ((int) (quant_matrix[j]))) >> 4;
3838 level = (((level << 1) + 1) * qscale *
3839 ((int) (quant_matrix[j]))) >> 4;
3842 if (level < -2048 || level > 2047)
3843 fprintf(stderr, "unquant error %d %d\n", i, level);
3854 static void dct_unquantize_h263_c(MpegEncContext *s,
3855 DCTELEM *block, int n, int qscale)
3857 int i, level, qmul, qadd;
3860 assert(s->block_last_index[n]>=0);
3862 qadd = (qscale - 1) | 1;
3868 block[0] = block[0] * s->y_dc_scale;
3870 block[0] = block[0] * s->c_dc_scale;
3874 nCoeffs= 63; //does not allways use zigzag table
3877 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
3880 for(;i<=nCoeffs;i++) {
3884 level = level * qmul - qadd;
3886 level = level * qmul + qadd;
3889 if (level < -2048 || level > 2047)
3890 fprintf(stderr, "unquant error %d %d\n", i, level);
3898 char ff_get_pict_type_char(int pict_type){
3900 case I_TYPE: return 'I';
3901 case P_TYPE: return 'P';
3902 case B_TYPE: return 'B';
3903 case S_TYPE: return 'S';
3904 default: return '?';
3908 extern const AVOption common_options[2];
3909 static const AVOption mpeg4_options[] =
3911 AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
3912 AVOPTION_CODEC_FLAG("vhq", "very high quality", flags, CODEC_FLAG_HQ, 0),
3913 AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
3914 "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
3915 bit_rate_tolerance, 4, 240000000, 8000),
3916 AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
3917 AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
3918 AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
3919 rc_eq, "tex^qComp,option1,options2", 0),
3920 AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
3921 rc_min_rate, 4, 24000000, 0),
3922 AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
3923 rc_max_rate, 4, 24000000, 0),
3924 AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
3925 flags, CODEC_FLAG_PSNR, 0),
3926 AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
3928 AVOPTION_SUB(common_options),
3932 #ifdef CONFIG_ENCODERS
3934 AVCodec mpeg1video_encoder = {
3937 CODEC_ID_MPEG1VIDEO,
3938 sizeof(MpegEncContext),
3946 AVCodec h263_encoder = {
3950 sizeof(MpegEncContext),
3956 AVCodec h263p_encoder = {
3960 sizeof(MpegEncContext),
3966 AVCodec rv10_encoder = {
3970 sizeof(MpegEncContext),
3976 AVCodec mpeg4_encoder = {
3980 sizeof(MpegEncContext),
3984 .options = mpeg4_options,
3987 AVCodec msmpeg4v1_encoder = {
3991 sizeof(MpegEncContext),
3997 AVCodec msmpeg4v2_encoder = {
4001 sizeof(MpegEncContext),
4007 AVCodec msmpeg4v3_encoder = {
4011 sizeof(MpegEncContext),
4017 AVCodec wmv1_encoder = {
4021 sizeof(MpegEncContext),
4029 AVCodec mjpeg_encoder = {
4033 sizeof(MpegEncContext),
4039 #endif //CONFIG_ENCODERS