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
56 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
57 extern void XVMC_field_end(MpegEncContext *s);
58 extern void XVMC_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
61 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
64 /* enable all paranoid tests for rounding, overflows, etc... */
70 /* for jpeg fast DCT */
73 static const uint16_t aanscales[64] = {
74 /* precomputed values scaled up by 14 bits */
75 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
76 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
77 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
78 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
79 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
80 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
81 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
82 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
85 static const uint8_t h263_chroma_roundtab[16] = {
86 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
87 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
90 #ifdef CONFIG_ENCODERS
91 static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
92 static uint8_t default_fcode_tab[MAX_MV*2+1];
94 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
96 static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
97 const uint16_t *quant_matrix, int bias, int qmin, int qmax)
101 for(qscale=qmin; qscale<=qmax; qscale++){
103 if (s->dsp.fdct == ff_jpeg_fdct_islow) {
105 const int j= s->dsp.idct_permutation[i];
106 /* 16 <= qscale * quant_matrix[i] <= 7905 */
107 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
108 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
109 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
111 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
112 (qscale * quant_matrix[j]));
114 } else if (s->dsp.fdct == fdct_ifast) {
116 const int j= s->dsp.idct_permutation[i];
117 /* 16 <= qscale * quant_matrix[i] <= 7905 */
118 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
119 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
120 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
122 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
123 (aanscales[i] * qscale * quant_matrix[j]));
127 const int j= s->dsp.idct_permutation[i];
128 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
129 So 16 <= qscale * quant_matrix[i] <= 7905
130 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
131 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
133 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
134 // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
135 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
137 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
138 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
143 #endif //CONFIG_ENCODERS
145 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
149 st->scantable= src_scantable;
153 j = src_scantable[i];
154 st->permutated[i] = permutation[j];
163 j = st->permutated[i];
165 st->raster_end[i]= end;
169 /* init common dct for both encoder and decoder */
170 int DCT_common_init(MpegEncContext *s)
172 s->dct_unquantize_h263 = dct_unquantize_h263_c;
173 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
174 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
176 #ifdef CONFIG_ENCODERS
177 s->dct_quantize= dct_quantize_c;
181 MPV_common_init_mmx(s);
184 MPV_common_init_axp(s);
187 MPV_common_init_mlib(s);
190 MPV_common_init_mmi(s);
193 MPV_common_init_armv4l(s);
196 MPV_common_init_ppc(s);
199 #ifdef CONFIG_ENCODERS
200 s->fast_dct_quantize= s->dct_quantize;
202 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
203 s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
206 #endif //CONFIG_ENCODERS
208 /* load & permutate scantables
209 note: only wmv uses differnt ones
211 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
212 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
213 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
214 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
216 s->picture_structure= PICT_FRAME;
222 * allocates a Picture
223 * The pixels are allocated/set by calling get_buffer() if shared=0
225 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
226 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
227 const int mb_array_size= s->mb_stride*s->mb_height;
231 assert(pic->data[0]);
232 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
233 pic->type= FF_BUFFER_TYPE_SHARED;
237 assert(!pic->data[0]);
239 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
241 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
242 fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
246 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
247 fprintf(stderr, "get_buffer() failed (stride changed)\n");
251 if(pic->linesize[1] != pic->linesize[2]){
252 fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
256 s->linesize = pic->linesize[0];
257 s->uvlinesize= pic->linesize[1];
260 if(pic->qscale_table==NULL){
262 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
263 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
264 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
265 CHECKED_ALLOCZ(pic->mb_cmp_score, mb_array_size * sizeof(int32_t))
268 CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
269 CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
270 CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(int))
271 pic->mb_type= pic->mb_type_base + s->mb_stride+1;
272 if(s->out_format == FMT_H264){
274 CHECKED_ALLOCZ(pic->motion_val[i], 2 * 16 * s->mb_num * sizeof(uint16_t))
275 CHECKED_ALLOCZ(pic->ref_index[i] , 4 * s->mb_num * sizeof(uint8_t))
278 pic->qstride= s->mb_stride;
281 //it might be nicer if the application would keep track of these but it would require a API change
282 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
283 s->prev_pict_types[0]= s->pict_type;
284 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
285 pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
288 fail: //for the CHECKED_ALLOCZ macro
293 * deallocates a picture
295 static void free_picture(MpegEncContext *s, Picture *pic){
298 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
299 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
302 av_freep(&pic->mb_var);
303 av_freep(&pic->mc_mb_var);
304 av_freep(&pic->mb_mean);
305 av_freep(&pic->mb_cmp_score);
306 av_freep(&pic->mbskip_table);
307 av_freep(&pic->qscale_table);
308 av_freep(&pic->mb_type_base);
311 av_freep(&pic->motion_val[i]);
312 av_freep(&pic->ref_index[i]);
315 if(pic->type == FF_BUFFER_TYPE_SHARED){
324 /* init common structure for both encoder and decoder */
325 int MPV_common_init(MpegEncContext *s)
327 int y_size, c_size, yc_size, i, mb_array_size, x, y;
329 dsputil_init(&s->dsp, s->avctx);
332 s->flags= s->avctx->flags;
334 s->mb_width = (s->width + 15) / 16;
335 s->mb_height = (s->height + 15) / 16;
336 s->mb_stride = s->mb_width + 1;
337 mb_array_size= s->mb_height * s->mb_stride;
339 /* set default edge pos, will be overriden in decode_header if needed */
340 s->h_edge_pos= s->mb_width*16;
341 s->v_edge_pos= s->mb_height*16;
343 s->mb_num = s->mb_width * s->mb_height;
348 s->block_wrap[3]= s->mb_width*2 + 2;
350 s->block_wrap[5]= s->mb_width + 2;
352 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
353 c_size = (s->mb_width + 2) * (s->mb_height + 2);
354 yc_size = y_size + 2 * c_size;
356 /* convert fourcc to upper case */
357 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
358 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
359 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
360 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
362 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
363 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
365 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
367 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
368 for(y=0; y<s->mb_height; y++){
369 for(x=0; x<s->mb_width; x++){
370 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
373 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
376 int mv_table_size= s->mb_stride * (s->mb_height+2) + 1;
378 /* Allocate MV tables */
379 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
380 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
381 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
382 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
383 CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
384 CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
385 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
386 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
387 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
388 s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
389 s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
390 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
392 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
393 CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
395 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
396 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
398 if(s->codec_id==CODEC_ID_MPEG4){
399 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
400 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
403 if(s->msmpeg4_version){
404 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
406 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
408 /* Allocate MB type table */
409 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint8_t)) //needed for encoding
412 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
414 if (s->out_format == FMT_H263 || s->encoding) {
418 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
419 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
422 if(s->codec_id==CODEC_ID_MPEG4){
423 /* interlaced direct mode decoding tables */
424 CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
425 CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
427 if (s->out_format == FMT_H263) {
429 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
430 s->ac_val[1] = s->ac_val[0] + y_size;
431 s->ac_val[2] = s->ac_val[1] + c_size;
434 CHECKED_ALLOCZ(s->coded_block, y_size);
436 /* divx501 bitstream reorder buffer */
437 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
439 /* cbp, ac_pred, pred_dir */
440 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
441 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
444 if (s->h263_pred || s->h263_plus || !s->encoding) {
446 //MN: we need these for error resilience of intra-frames
447 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
448 s->dc_val[1] = s->dc_val[0] + y_size;
449 s->dc_val[2] = s->dc_val[1] + c_size;
450 for(i=0;i<yc_size;i++)
451 s->dc_val[0][i] = 1024;
454 /* which mb is a intra block */
455 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
456 memset(s->mbintra_table, 1, mb_array_size);
458 /* default structure is frame */
459 s->picture_structure = PICT_FRAME;
461 /* init macroblock skip table */
462 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
463 //Note the +1 is for a quicker mpeg4 slice_end detection
464 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
466 s->block= s->blocks[0];
468 s->parse_context.state= -1;
470 s->context_initialized = 1;
480 /* init common structure for both encoder and decoder */
481 void MPV_common_end(MpegEncContext *s)
485 av_freep(&s->parse_context.buffer);
486 s->parse_context.buffer_size=0;
488 av_freep(&s->mb_type);
489 av_freep(&s->p_mv_table_base);
490 av_freep(&s->b_forw_mv_table_base);
491 av_freep(&s->b_back_mv_table_base);
492 av_freep(&s->b_bidir_forw_mv_table_base);
493 av_freep(&s->b_bidir_back_mv_table_base);
494 av_freep(&s->b_direct_mv_table_base);
496 s->b_forw_mv_table= NULL;
497 s->b_back_mv_table= NULL;
498 s->b_bidir_forw_mv_table= NULL;
499 s->b_bidir_back_mv_table= NULL;
500 s->b_direct_mv_table= NULL;
502 av_freep(&s->motion_val);
503 av_freep(&s->dc_val[0]);
504 av_freep(&s->ac_val[0]);
505 av_freep(&s->coded_block);
506 av_freep(&s->mbintra_table);
507 av_freep(&s->cbp_table);
508 av_freep(&s->pred_dir_table);
509 av_freep(&s->me.scratchpad);
510 av_freep(&s->me.map);
511 av_freep(&s->me.score_map);
513 av_freep(&s->mbskip_table);
514 av_freep(&s->prev_pict_types);
515 av_freep(&s->bitstream_buffer);
516 av_freep(&s->tex_pb_buffer);
517 av_freep(&s->pb2_buffer);
518 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
519 av_freep(&s->field_mv_table);
520 av_freep(&s->field_select_table);
521 av_freep(&s->avctx->stats_out);
522 av_freep(&s->ac_stats);
523 av_freep(&s->error_status_table);
524 av_freep(&s->mb_index2xy);
526 for(i=0; i<MAX_PICTURE_COUNT; i++){
527 free_picture(s, &s->picture[i]);
529 avcodec_default_free_buffers(s->avctx);
530 s->context_initialized = 0;
533 #ifdef CONFIG_ENCODERS
535 /* init video encoder */
536 int MPV_encode_init(AVCodecContext *avctx)
538 MpegEncContext *s = avctx->priv_data;
540 int chroma_h_shift, chroma_v_shift;
542 s->bit_rate = avctx->bit_rate;
543 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
544 s->width = avctx->width;
545 s->height = avctx->height;
546 if(avctx->gop_size > 600){
547 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
550 s->gop_size = avctx->gop_size;
551 s->rtp_mode = avctx->rtp_mode;
552 s->rtp_payload_size = avctx->rtp_payload_size;
553 if (avctx->rtp_callback)
554 s->rtp_callback = avctx->rtp_callback;
555 s->max_qdiff= avctx->max_qdiff;
556 s->qcompress= avctx->qcompress;
557 s->qblur= avctx->qblur;
559 s->flags= avctx->flags;
560 s->max_b_frames= avctx->max_b_frames;
561 s->b_frame_strategy= avctx->b_frame_strategy;
562 s->codec_id= avctx->codec->id;
563 s->luma_elim_threshold = avctx->luma_elim_threshold;
564 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
565 s->strict_std_compliance= avctx->strict_std_compliance;
566 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
567 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
568 s->mpeg_quant= avctx->mpeg_quant;
570 if (s->gop_size <= 1) {
577 s->me_method = avctx->me_method;
580 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
582 s->adaptive_quant= ( s->avctx->lumi_masking
583 || s->avctx->dark_masking
584 || s->avctx->temporal_cplx_masking
585 || s->avctx->spatial_cplx_masking
586 || s->avctx->p_masking)
589 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
591 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4){
592 fprintf(stderr, "4MV not supporetd by codec\n");
596 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
597 fprintf(stderr, "qpel not supporetd by codec\n");
601 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
602 fprintf(stderr, "data partitioning not supporetd by codec\n");
606 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO){
607 fprintf(stderr, "b frames not supporetd by codec\n");
611 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
612 fprintf(stderr, "mpeg2 style quantization not supporetd by codec\n");
616 if(s->codec_id==CODEC_ID_MJPEG){
617 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
618 s->inter_quant_bias= 0;
619 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO){
620 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
621 s->inter_quant_bias= 0;
623 s->intra_quant_bias=0;
624 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
627 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
628 s->intra_quant_bias= avctx->intra_quant_bias;
629 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
630 s->inter_quant_bias= avctx->inter_quant_bias;
632 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
634 switch(avctx->codec->id) {
635 case CODEC_ID_MPEG1VIDEO:
636 s->out_format = FMT_MPEG1;
637 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
638 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
642 s->out_format = FMT_MJPEG;
643 s->intra_only = 1; /* force intra only for jpeg */
644 s->mjpeg_write_tables = 1; /* write all tables */
645 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
646 s->mjpeg_vsample[0] = 1<<chroma_v_shift;
647 s->mjpeg_vsample[1] = 1;
648 s->mjpeg_vsample[2] = 1;
649 s->mjpeg_hsample[0] = 1<<chroma_h_shift;
650 s->mjpeg_hsample[1] = 1;
651 s->mjpeg_hsample[2] = 1;
652 if (mjpeg_init(s) < 0)
659 if (h263_get_picture_format(s->width, s->height) == 7) {
660 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
663 s->out_format = FMT_H263;
668 s->out_format = FMT_H263;
671 s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
672 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
674 /* These are just to be sure */
680 s->out_format = FMT_H263;
681 s->h263_flv = 2; /* format = 1; 11-bit codes */
682 s->unrestricted_mv = 1;
683 s->rtp_mode=0; /* don't allow GOB */
688 s->out_format = FMT_H263;
694 s->out_format = FMT_H263;
696 s->unrestricted_mv = 1;
697 s->low_delay= s->max_b_frames ? 0 : 1;
698 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
700 case CODEC_ID_MSMPEG4V1:
701 s->out_format = FMT_H263;
704 s->unrestricted_mv = 1;
705 s->msmpeg4_version= 1;
709 case CODEC_ID_MSMPEG4V2:
710 s->out_format = FMT_H263;
713 s->unrestricted_mv = 1;
714 s->msmpeg4_version= 2;
718 case CODEC_ID_MSMPEG4V3:
719 s->out_format = FMT_H263;
722 s->unrestricted_mv = 1;
723 s->msmpeg4_version= 3;
724 s->flipflop_rounding=1;
729 s->out_format = FMT_H263;
732 s->unrestricted_mv = 1;
733 s->msmpeg4_version= 4;
734 s->flipflop_rounding=1;
739 s->out_format = FMT_H263;
742 s->unrestricted_mv = 1;
743 s->msmpeg4_version= 5;
744 s->flipflop_rounding=1;
753 { /* set up some save defaults, some codecs might override them later */
759 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
760 memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
761 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
763 for(i=-16; i<16; i++){
764 default_fcode_tab[i + MAX_MV]= 1;
768 s->me.mv_penalty= default_mv_penalty;
769 s->fcode_tab= default_fcode_tab;
771 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
773 /* dont use mv_penalty table for crap MV as it would be confused */
774 //FIXME remove after fixing / removing old ME
775 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
780 if (MPV_common_init(s) < 0)
785 #ifdef CONFIG_ENCODERS
787 if (s->out_format == FMT_H263)
789 if(s->msmpeg4_version)
790 ff_msmpeg4_encode_init(s);
792 if (s->out_format == FMT_MPEG1)
793 ff_mpeg1_encode_init(s);
796 /* init default q matrix */
798 int j= s->dsp.idct_permutation[i];
800 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
801 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
802 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
803 }else if(s->out_format == FMT_H263){
805 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
809 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
810 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
814 /* precompute matrix */
815 /* for mjpeg, we do include qscale in the matrix */
816 if (s->out_format != FMT_MJPEG) {
817 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
818 s->intra_matrix, s->intra_quant_bias, 1, 31);
819 convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
820 s->inter_matrix, s->inter_quant_bias, 1, 31);
823 if(ff_rate_control_init(s) < 0)
826 s->picture_number = 0;
827 s->picture_in_gop_number = 0;
828 s->fake_picture_number = 0;
829 /* motion detector init */
836 int MPV_encode_end(AVCodecContext *avctx)
838 MpegEncContext *s = avctx->priv_data;
844 ff_rate_control_uninit(s);
847 if (s->out_format == FMT_MJPEG)
853 #endif //CONFIG_ENCODERS
855 void init_rl(RLTable *rl)
857 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
858 uint8_t index_run[MAX_RUN+1];
859 int last, run, level, start, end, i;
861 /* compute max_level[], max_run[] and index_run[] */
862 for(last=0;last<2;last++) {
871 memset(max_level, 0, MAX_RUN + 1);
872 memset(max_run, 0, MAX_LEVEL + 1);
873 memset(index_run, rl->n, MAX_RUN + 1);
874 for(i=start;i<end;i++) {
875 run = rl->table_run[i];
876 level = rl->table_level[i];
877 if (index_run[run] == rl->n)
879 if (level > max_level[run])
880 max_level[run] = level;
881 if (run > max_run[level])
882 max_run[level] = run;
884 rl->max_level[last] = av_malloc(MAX_RUN + 1);
885 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
886 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
887 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
888 rl->index_run[last] = av_malloc(MAX_RUN + 1);
889 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
893 /* draw the edges of width 'w' of an image of size width, height */
894 //FIXME check that this is ok for mpeg4 interlaced
895 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
897 uint8_t *ptr, *last_line;
900 last_line = buf + (height - 1) * wrap;
903 memcpy(buf - (i + 1) * wrap, buf, width);
904 memcpy(last_line + (i + 1) * wrap, last_line, width);
908 for(i=0;i<height;i++) {
909 memset(ptr - w, ptr[0], w);
910 memset(ptr + width, ptr[width-1], w);
915 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
916 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
917 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
918 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
922 static int find_unused_picture(MpegEncContext *s, int shared){
926 for(i=0; i<MAX_PICTURE_COUNT; i++){
927 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
930 for(i=0; i<MAX_PICTURE_COUNT; i++){
931 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break; //FIXME
933 for(i=0; i<MAX_PICTURE_COUNT; i++){
934 if(s->picture[i].data[0]==NULL) break;
938 assert(i<MAX_PICTURE_COUNT);
942 /* generic function for encode/decode called before a frame is coded/decoded */
943 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
950 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
952 /* mark&release old frames */
953 if (s->pict_type != B_TYPE && s->last_picture_ptr) {
954 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
956 /* release forgotten pictures */
957 /* if(mpeg124/h263) */
959 for(i=0; i<MAX_PICTURE_COUNT; i++){
960 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
961 fprintf(stderr, "releasing zombie picture\n");
962 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
969 /* release non refernce frames */
970 for(i=0; i<MAX_PICTURE_COUNT; i++){
971 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
972 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
976 i= find_unused_picture(s, 0);
978 pic= (AVFrame*)&s->picture[i];
979 pic->reference= s->pict_type != B_TYPE ? 3 : 0;
981 if(s->current_picture_ptr)
982 pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
984 if( alloc_picture(s, (Picture*)pic, 0) < 0)
987 s->current_picture_ptr= &s->picture[i];
990 s->current_picture_ptr->pict_type= s->pict_type;
991 s->current_picture_ptr->quality= s->qscale;
992 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
994 s->current_picture= *s->current_picture_ptr;
996 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
997 if (s->pict_type != B_TYPE) {
998 s->last_picture_ptr= s->next_picture_ptr;
999 s->next_picture_ptr= s->current_picture_ptr;
1002 if(s->last_picture_ptr) s->last_picture= *s->last_picture_ptr;
1003 if(s->next_picture_ptr) s->next_picture= *s->next_picture_ptr;
1004 if(s->new_picture_ptr ) s->new_picture = *s->new_picture_ptr;
1006 if(s->picture_structure!=PICT_FRAME){
1009 if(s->picture_structure == PICT_BOTTOM_FIELD){
1010 s->current_picture.data[i] += s->current_picture.linesize[i];
1012 s->current_picture.linesize[i] *= 2;
1013 s->last_picture.linesize[i] *=2;
1014 s->next_picture.linesize[i] *=2;
1018 if(s->pict_type != I_TYPE && s->last_picture_ptr==NULL){
1019 fprintf(stderr, "warning: first frame is no keyframe\n");
1020 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1025 s->hurry_up= s->avctx->hurry_up;
1026 s->error_resilience= avctx->error_resilience;
1028 /* set dequantizer, we cant do it during init as it might change for mpeg4
1029 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1030 if(s->out_format == FMT_H263){
1032 s->dct_unquantize = s->dct_unquantize_mpeg2;
1034 s->dct_unquantize = s->dct_unquantize_h263;
1036 s->dct_unquantize = s->dct_unquantize_mpeg1;
1039 if(s->avctx->xvmc_acceleration)
1040 return XVMC_field_start(s, avctx);
1045 /* generic function for encode/decode called after a frame has been coded/decoded */
1046 void MPV_frame_end(MpegEncContext *s)
1049 /* draw edge for correct motion prediction if outside */
1051 //just to make sure that all data is rendered.
1052 if(s->avctx->xvmc_acceleration){
1056 if(s->codec_id!=CODEC_ID_SVQ1 && s->codec_id != CODEC_ID_MPEG1VIDEO){
1057 if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1058 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
1059 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1060 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1065 s->last_pict_type = s->pict_type;
1066 if(s->pict_type!=B_TYPE){
1067 s->last_non_b_pict_type= s->pict_type;
1070 /* copy back current_picture variables */
1071 for(i=0; i<MAX_PICTURE_COUNT; i++){
1072 if(s->picture[i].data[0] == s->current_picture.data[0]){
1073 s->picture[i]= s->current_picture;
1077 assert(i<MAX_PICTURE_COUNT);
1081 /* release non refernce frames */
1082 for(i=0; i<MAX_PICTURE_COUNT; i++){
1083 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1084 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1088 // clear copies, to avoid confusion
1090 memset(&s->last_picture, 0, sizeof(Picture));
1091 memset(&s->next_picture, 0, sizeof(Picture));
1092 memset(&s->current_picture, 0, sizeof(Picture));
1097 * draws an line from (ex, ey) -> (sx, sy).
1098 * @param w width of the image
1099 * @param h height of the image
1100 * @param stride stride/linesize of the image
1101 * @param color color of the arrow
1103 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1106 sx= clip(sx, 0, w-1);
1107 sy= clip(sy, 0, h-1);
1108 ex= clip(ex, 0, w-1);
1109 ey= clip(ey, 0, h-1);
1111 buf[sy*stride + sx]+= color;
1113 if(ABS(ex - sx) > ABS(ey - sy)){
1118 buf+= sx + sy*stride;
1120 f= ((ey-sy)<<16)/ex;
1121 for(x= 0; x <= ex; x++){
1122 y= ((x*f) + (1<<15))>>16;
1123 buf[y*stride + x]+= color;
1130 buf+= sx + sy*stride;
1132 if(ey) f= ((ex-sx)<<16)/ey;
1134 for(y= 0; y <= ey; y++){
1135 x= ((y*f) + (1<<15))>>16;
1136 buf[y*stride + x]+= color;
1142 * draws an arrow from (ex, ey) -> (sx, sy).
1143 * @param w width of the image
1144 * @param h height of the image
1145 * @param stride stride/linesize of the image
1146 * @param color color of the arrow
1148 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1151 sx= clip(sx, -100, w+100);
1152 sy= clip(sy, -100, h+100);
1153 ex= clip(ex, -100, w+100);
1154 ey= clip(ey, -100, h+100);
1159 if(dx*dx + dy*dy > 3*3){
1162 int length= ff_sqrt((rx*rx + ry*ry)<<8);
1164 //FIXME subpixel accuracy
1165 rx= ROUNDED_DIV(rx*3<<4, length);
1166 ry= ROUNDED_DIV(ry*3<<4, length);
1168 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1169 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1171 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1175 * prints debuging info for the given picture.
1177 void ff_print_debug_info(MpegEncContext *s, Picture *pict){
1179 if(!pict || !pict->mb_type) return;
1181 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1184 for(y=0; y<s->mb_height; y++){
1185 for(x=0; x<s->mb_width; x++){
1186 if(s->avctx->debug&FF_DEBUG_SKIP){
1187 int count= s->mbskip_table[x + y*s->mb_stride];
1188 if(count>9) count=9;
1189 printf("%1d", count);
1191 if(s->avctx->debug&FF_DEBUG_QP){
1192 printf("%2d", pict->qscale_table[x + y*s->mb_stride]);
1194 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1195 int mb_type= pict->mb_type[x + y*s->mb_stride];
1197 //Type & MV direction
1200 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1202 else if(IS_INTRA4x4(mb_type))
1204 else if(IS_INTRA16x16(mb_type))
1206 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1208 else if(IS_DIRECT(mb_type))
1210 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1212 else if(IS_GMC(mb_type))
1214 else if(IS_SKIP(mb_type))
1216 else if(!USES_LIST(mb_type, 1))
1218 else if(!USES_LIST(mb_type, 0))
1221 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1228 else if(IS_16X8(mb_type))
1230 else if(IS_8X16(mb_type))
1232 else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
1238 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
1249 if((s->avctx->debug&FF_DEBUG_VIS_MV) && s->motion_val){
1250 const int shift= 1 + s->quarter_sample;
1252 uint8_t *ptr= pict->data[0];
1253 s->low_delay=0; //needed to see the vectors without trashing the buffers
1255 for(mb_y=0; mb_y<s->mb_height; mb_y++){
1257 for(mb_x=0; mb_x<s->mb_width; mb_x++){
1258 const int mb_index= mb_x + mb_y*s->mb_stride;
1259 if(IS_8X8(s->current_picture.mb_type[mb_index])){
1262 int sx= mb_x*16 + 4 + 8*(i&1);
1263 int sy= mb_y*16 + 4 + 8*(i>>1);
1264 int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
1265 int mx= (s->motion_val[xy][0]>>shift) + sx;
1266 int my= (s->motion_val[xy][1]>>shift) + sy;
1267 draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1270 int sx= mb_x*16 + 8;
1271 int sy= mb_y*16 + 8;
1272 int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
1273 int mx= (s->motion_val[xy][0]>>shift) + sx;
1274 int my= (s->motion_val[xy][1]>>shift) + sy;
1275 draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1277 s->mbskip_table[mb_index]=0;
1283 #ifdef CONFIG_ENCODERS
1285 static int get_sae(uint8_t *src, int ref, int stride){
1289 for(y=0; y<16; y++){
1290 for(x=0; x<16; x++){
1291 acc+= ABS(src[x+y*stride] - ref);
1298 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1305 for(y=0; y<h; y+=16){
1306 for(x=0; x<w; x+=16){
1307 int offset= x + y*stride;
1308 int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1309 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1310 int sae = get_sae(src + offset, mean, stride);
1312 acc+= sae + 500 < sad;
1319 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1322 const int encoding_delay= s->max_b_frames;
1326 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1327 if(pic_arg->linesize[0] != s->linesize) direct=0;
1328 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1329 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1331 // printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1334 i= find_unused_picture(s, 1);
1336 pic= (AVFrame*)&s->picture[i];
1340 pic->data[i]= pic_arg->data[i];
1341 pic->linesize[i]= pic_arg->linesize[i];
1343 alloc_picture(s, (Picture*)pic, 1);
1345 i= find_unused_picture(s, 0);
1347 pic= (AVFrame*)&s->picture[i];
1350 alloc_picture(s, (Picture*)pic, 0);
1352 /* the input will be 16 pixels to the right relative to the actual buffer start
1353 * and the current_pic, so the buffer can be reused, yes its not beatifull
1358 if( pic->data[0] == pic_arg->data[0]
1359 && pic->data[1] == pic_arg->data[1]
1360 && pic->data[2] == pic_arg->data[2]){
1363 int h_chroma_shift, v_chroma_shift;
1365 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1368 int src_stride= pic_arg->linesize[i];
1369 int dst_stride= i ? s->uvlinesize : s->linesize;
1370 int h_shift= i ? h_chroma_shift : 0;
1371 int v_shift= i ? v_chroma_shift : 0;
1372 int w= s->width >>h_shift;
1373 int h= s->height>>v_shift;
1374 uint8_t *src= pic_arg->data[i];
1375 uint8_t *dst= pic->data[i];
1377 if(src_stride==dst_stride)
1378 memcpy(dst, src, src_stride*h);
1381 memcpy(dst, src, w);
1389 pic->quality= pic_arg->quality;
1390 pic->pict_type= pic_arg->pict_type;
1391 pic->pts = pic_arg->pts;
1393 if(s->input_picture[encoding_delay])
1394 pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1398 /* shift buffer entries */
1399 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1400 s->input_picture[i-1]= s->input_picture[i];
1402 s->input_picture[encoding_delay]= (Picture*)pic;
1407 static void select_input_picture(MpegEncContext *s){
1409 const int encoding_delay= s->max_b_frames;
1410 int coded_pic_num=0;
1412 if(s->reordered_input_picture[0])
1413 coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1415 for(i=1; i<MAX_PICTURE_COUNT; i++)
1416 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1417 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1419 /* set next picture types & ordering */
1420 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1421 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1422 s->reordered_input_picture[0]= s->input_picture[0];
1423 s->reordered_input_picture[0]->pict_type= I_TYPE;
1424 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1428 if(s->flags&CODEC_FLAG_PASS2){
1429 for(i=0; i<s->max_b_frames+1; i++){
1430 int pict_num= s->input_picture[0]->display_picture_number + i;
1431 int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1432 s->input_picture[i]->pict_type= pict_type;
1434 if(i + 1 >= s->rc_context.num_entries) break;
1438 if(s->input_picture[0]->pict_type){
1439 /* user selected pict_type */
1440 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1441 if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1444 if(b_frames > s->max_b_frames){
1445 fprintf(stderr, "warning, too many bframes in a row\n");
1446 b_frames = s->max_b_frames;
1448 }else if(s->b_frame_strategy==0){
1449 b_frames= s->max_b_frames;
1450 while(b_frames && !s->input_picture[b_frames]) b_frames--;
1451 }else if(s->b_frame_strategy==1){
1452 for(i=1; i<s->max_b_frames+1; i++){
1453 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1454 s->input_picture[i]->b_frame_score=
1455 get_intra_count(s, s->input_picture[i ]->data[0],
1456 s->input_picture[i-1]->data[0], s->linesize) + 1;
1459 for(i=0; i<s->max_b_frames; i++){
1460 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1463 b_frames= FFMAX(0, i-1);
1466 for(i=0; i<b_frames+1; i++){
1467 s->input_picture[i]->b_frame_score=0;
1470 fprintf(stderr, "illegal b frame strategy\n");
1475 //static int b_count=0;
1476 //b_count+= b_frames;
1477 //printf("b_frames: %d\n", b_count);
1479 s->reordered_input_picture[0]= s->input_picture[b_frames];
1480 if( s->picture_in_gop_number + b_frames >= s->gop_size
1481 || s->reordered_input_picture[0]->pict_type== I_TYPE)
1482 s->reordered_input_picture[0]->pict_type= I_TYPE;
1484 s->reordered_input_picture[0]->pict_type= P_TYPE;
1485 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1486 for(i=0; i<b_frames; i++){
1488 s->reordered_input_picture[i+1]= s->input_picture[i];
1489 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1490 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1495 if(s->reordered_input_picture[0]){
1496 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1498 s->new_picture= *s->reordered_input_picture[0];
1500 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1501 // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1503 int i= find_unused_picture(s, 0);
1504 Picture *pic= &s->picture[i];
1506 /* mark us unused / free shared pic */
1508 s->reordered_input_picture[0]->data[i]= NULL;
1509 s->reordered_input_picture[0]->type= 0;
1511 //FIXME bad, copy * except
1512 pic->pict_type = s->reordered_input_picture[0]->pict_type;
1513 pic->quality = s->reordered_input_picture[0]->quality;
1514 pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1515 pic->reference = s->reordered_input_picture[0]->reference;
1517 alloc_picture(s, pic, 0);
1519 s->current_picture_ptr= pic;
1521 // input is not a shared pix -> reuse buffer for current_pix
1523 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1524 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1526 s->current_picture_ptr= s->reordered_input_picture[0];
1528 //reverse the +16 we did before storing the input
1529 s->current_picture_ptr->data[i]-=16;
1532 s->current_picture= *s->current_picture_ptr;
1534 s->picture_number= s->new_picture.display_picture_number;
1535 //printf("dpn:%d\n", s->picture_number);
1537 memset(&s->new_picture, 0, sizeof(Picture));
1541 int MPV_encode_picture(AVCodecContext *avctx,
1542 unsigned char *buf, int buf_size, void *data)
1544 MpegEncContext *s = avctx->priv_data;
1545 AVFrame *pic_arg = data;
1548 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1550 s->picture_in_gop_number++;
1552 load_input_picture(s, pic_arg);
1554 select_input_picture(s);
1557 if(s->new_picture.data[0]){
1559 s->pict_type= s->new_picture.pict_type;
1560 if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1561 s->qscale= (int)(s->new_picture.quality+0.5);
1565 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1566 MPV_frame_start(s, avctx);
1568 encode_picture(s, s->picture_number);
1570 avctx->real_pict_num = s->picture_number;
1571 avctx->header_bits = s->header_bits;
1572 avctx->mv_bits = s->mv_bits;
1573 avctx->misc_bits = s->misc_bits;
1574 avctx->i_tex_bits = s->i_tex_bits;
1575 avctx->p_tex_bits = s->p_tex_bits;
1576 avctx->i_count = s->i_count;
1577 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1578 avctx->skip_count = s->skip_count;
1582 if (s->out_format == FMT_MJPEG)
1583 mjpeg_picture_trailer(s);
1585 if(s->flags&CODEC_FLAG_PASS1)
1586 ff_write_pass1_stats(s);
1589 avctx->error[i] += s->current_picture_ptr->error[i];
1593 s->input_picture_number++;
1595 flush_put_bits(&s->pb);
1596 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1598 s->total_bits += s->frame_bits;
1599 avctx->frame_bits = s->frame_bits;
1601 return pbBufPtr(&s->pb) - s->pb.buf;
1604 #endif //CONFIG_ENCODERS
1606 static inline void gmc1_motion(MpegEncContext *s,
1607 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1609 uint8_t **ref_picture, int src_offset)
1612 int offset, src_x, src_y, linesize, uvlinesize;
1613 int motion_x, motion_y;
1616 motion_x= s->sprite_offset[0][0];
1617 motion_y= s->sprite_offset[0][1];
1618 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1619 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1620 motion_x<<=(3-s->sprite_warping_accuracy);
1621 motion_y<<=(3-s->sprite_warping_accuracy);
1622 src_x = clip(src_x, -16, s->width);
1623 if (src_x == s->width)
1625 src_y = clip(src_y, -16, s->height);
1626 if (src_y == s->height)
1629 linesize = s->linesize;
1630 uvlinesize = s->uvlinesize;
1632 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1634 dest_y+=dest_offset;
1635 if(s->flags&CODEC_FLAG_EMU_EDGE){
1636 if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
1637 || src_y + 17 >= s->v_edge_pos){
1638 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1639 ptr= s->edge_emu_buffer;
1643 if((motion_x|motion_y)&7){
1644 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1645 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1649 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1650 if (s->no_rounding){
1651 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1653 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
1657 if(s->flags&CODEC_FLAG_GRAY) return;
1659 motion_x= s->sprite_offset[1][0];
1660 motion_y= s->sprite_offset[1][1];
1661 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1662 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1663 motion_x<<=(3-s->sprite_warping_accuracy);
1664 motion_y<<=(3-s->sprite_warping_accuracy);
1665 src_x = clip(src_x, -8, s->width>>1);
1666 if (src_x == s->width>>1)
1668 src_y = clip(src_y, -8, s->height>>1);
1669 if (src_y == s->height>>1)
1672 offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1673 ptr = ref_picture[1] + offset;
1674 if(s->flags&CODEC_FLAG_EMU_EDGE){
1675 if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
1676 || src_y + 9 >= s->v_edge_pos>>1){
1677 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1678 ptr= s->edge_emu_buffer;
1682 s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1684 ptr = ref_picture[2] + offset;
1686 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1687 ptr= s->edge_emu_buffer;
1689 s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1694 static inline void gmc_motion(MpegEncContext *s,
1695 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1697 uint8_t **ref_picture, int src_offset)
1700 int linesize, uvlinesize;
1701 const int a= s->sprite_warping_accuracy;
1704 linesize = s->linesize;
1705 uvlinesize = s->uvlinesize;
1707 ptr = ref_picture[0] + src_offset;
1709 dest_y+=dest_offset;
1711 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1712 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1714 s->dsp.gmc(dest_y, ptr, linesize, 16,
1717 s->sprite_delta[0][0], s->sprite_delta[0][1],
1718 s->sprite_delta[1][0], s->sprite_delta[1][1],
1719 a+1, (1<<(2*a+1)) - s->no_rounding,
1720 s->h_edge_pos, s->v_edge_pos);
1721 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1722 ox + s->sprite_delta[0][0]*8,
1723 oy + s->sprite_delta[1][0]*8,
1724 s->sprite_delta[0][0], s->sprite_delta[0][1],
1725 s->sprite_delta[1][0], s->sprite_delta[1][1],
1726 a+1, (1<<(2*a+1)) - s->no_rounding,
1727 s->h_edge_pos, s->v_edge_pos);
1729 if(s->flags&CODEC_FLAG_GRAY) return;
1732 dest_cb+=dest_offset>>1;
1733 dest_cr+=dest_offset>>1;
1735 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1736 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1738 ptr = ref_picture[1] + (src_offset>>1);
1739 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1742 s->sprite_delta[0][0], s->sprite_delta[0][1],
1743 s->sprite_delta[1][0], s->sprite_delta[1][1],
1744 a+1, (1<<(2*a+1)) - s->no_rounding,
1745 s->h_edge_pos>>1, s->v_edge_pos>>1);
1747 ptr = ref_picture[2] + (src_offset>>1);
1748 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1751 s->sprite_delta[0][0], s->sprite_delta[0][1],
1752 s->sprite_delta[1][0], s->sprite_delta[1][1],
1753 a+1, (1<<(2*a+1)) - s->no_rounding,
1754 s->h_edge_pos>>1, s->v_edge_pos>>1);
1758 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
1759 * @param buf destination buffer
1760 * @param src source buffer
1761 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
1762 * @param block_w width of block
1763 * @param block_h height of block
1764 * @param src_x x coordinate of the top left sample of the block in the source buffer
1765 * @param src_y y coordinate of the top left sample of the block in the source buffer
1766 * @param w width of the source buffer
1767 * @param h height of the source buffer
1769 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
1770 int src_x, int src_y, int w, int h){
1772 int start_y, start_x, end_y, end_x;
1775 src+= (h-1-src_y)*linesize;
1777 }else if(src_y<=-block_h){
1778 src+= (1-block_h-src_y)*linesize;
1784 }else if(src_x<=-block_w){
1785 src+= (1-block_w-src_x);
1789 start_y= FFMAX(0, -src_y);
1790 start_x= FFMAX(0, -src_x);
1791 end_y= FFMIN(block_h, h-src_y);
1792 end_x= FFMIN(block_w, w-src_x);
1794 // copy existing part
1795 for(y=start_y; y<end_y; y++){
1796 for(x=start_x; x<end_x; x++){
1797 buf[x + y*linesize]= src[x + y*linesize];
1802 for(y=0; y<start_y; y++){
1803 for(x=start_x; x<end_x; x++){
1804 buf[x + y*linesize]= buf[x + start_y*linesize];
1809 for(y=end_y; y<block_h; y++){
1810 for(x=start_x; x<end_x; x++){
1811 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1815 for(y=0; y<block_h; y++){
1817 for(x=0; x<start_x; x++){
1818 buf[x + y*linesize]= buf[start_x + y*linesize];
1822 for(x=end_x; x<block_w; x++){
1823 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1829 /* apply one mpeg motion vector to the three components */
1830 static inline void mpeg_motion(MpegEncContext *s,
1831 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1833 uint8_t **ref_picture, int src_offset,
1834 int field_based, op_pixels_func (*pix_op)[4],
1835 int motion_x, int motion_y, int h)
1838 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1841 if(s->quarter_sample)
1847 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1848 src_x = s->mb_x * 16 + (motion_x >> 1);
1849 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1851 /* WARNING: do no forget half pels */
1852 height = s->height >> field_based;
1853 v_edge_pos = s->v_edge_pos >> field_based;
1854 src_x = clip(src_x, -16, s->width);
1855 if (src_x == s->width)
1857 src_y = clip(src_y, -16, height);
1858 if (src_y == height)
1860 linesize = s->current_picture.linesize[0] << field_based;
1861 uvlinesize = s->current_picture.linesize[1] << field_based;
1862 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1863 dest_y += dest_offset;
1865 if(s->flags&CODEC_FLAG_EMU_EDGE){
1866 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1867 || src_y + (motion_y&1) + h > v_edge_pos){
1868 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based, //FIXME linesize? and uv below
1869 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1870 ptr= s->edge_emu_buffer + src_offset;
1874 pix_op[0][dxy](dest_y, ptr, linesize, h);
1876 if(s->flags&CODEC_FLAG_GRAY) return;
1878 if (s->out_format == FMT_H263) {
1880 if ((motion_x & 3) != 0)
1882 if ((motion_y & 3) != 0)
1889 dxy = ((my & 1) << 1) | (mx & 1);
1894 src_x = s->mb_x * 8 + mx;
1895 src_y = s->mb_y * (8 >> field_based) + my;
1896 src_x = clip(src_x, -8, s->width >> 1);
1897 if (src_x == (s->width >> 1))
1899 src_y = clip(src_y, -8, height >> 1);
1900 if (src_y == (height >> 1))
1902 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1903 ptr = ref_picture[1] + offset;
1905 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
1906 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1907 ptr= s->edge_emu_buffer + (src_offset >> 1);
1909 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1911 ptr = ref_picture[2] + offset;
1913 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
1914 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1915 ptr= s->edge_emu_buffer + (src_offset >> 1);
1917 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1920 static inline void qpel_motion(MpegEncContext *s,
1921 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1923 uint8_t **ref_picture, int src_offset,
1924 int field_based, op_pixels_func (*pix_op)[4],
1925 qpel_mc_func (*qpix_op)[16],
1926 int motion_x, int motion_y, int h)
1929 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1932 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1933 src_x = s->mb_x * 16 + (motion_x >> 2);
1934 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1936 height = s->height >> field_based;
1937 v_edge_pos = s->v_edge_pos >> field_based;
1938 src_x = clip(src_x, -16, s->width);
1939 if (src_x == s->width)
1941 src_y = clip(src_y, -16, height);
1942 if (src_y == height)
1944 linesize = s->linesize << field_based;
1945 uvlinesize = s->uvlinesize << field_based;
1946 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1947 dest_y += dest_offset;
1948 //printf("%d %d %d\n", src_x, src_y, dxy);
1950 if(s->flags&CODEC_FLAG_EMU_EDGE){
1951 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1952 || src_y + (motion_y&3) + h > v_edge_pos){
1953 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,
1954 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1955 ptr= s->edge_emu_buffer + src_offset;
1960 qpix_op[0][dxy](dest_y, ptr, linesize);
1962 //damn interlaced mode
1963 //FIXME boundary mirroring is not exactly correct here
1964 qpix_op[1][dxy](dest_y , ptr , linesize);
1965 qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1968 if(s->flags&CODEC_FLAG_GRAY) return;
1973 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
1974 static const int rtab[8]= {0,0,1,1,0,0,0,1};
1975 mx= (motion_x>>1) + rtab[motion_x&7];
1976 my= (motion_y>>1) + rtab[motion_y&7];
1977 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1978 mx= (motion_x>>1)|(motion_x&1);
1979 my= (motion_y>>1)|(motion_y&1);
1987 dxy= (mx&1) | ((my&1)<<1);
1991 src_x = s->mb_x * 8 + mx;
1992 src_y = s->mb_y * (8 >> field_based) + my;
1993 src_x = clip(src_x, -8, s->width >> 1);
1994 if (src_x == (s->width >> 1))
1996 src_y = clip(src_y, -8, height >> 1);
1997 if (src_y == (height >> 1))
2000 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2001 ptr = ref_picture[1] + offset;
2003 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
2004 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2005 ptr= s->edge_emu_buffer + (src_offset >> 1);
2007 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2009 ptr = ref_picture[2] + offset;
2011 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
2012 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2013 ptr= s->edge_emu_buffer + (src_offset >> 1);
2015 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2018 inline int ff_h263_round_chroma(int x){
2020 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2023 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2028 * motion compesation of a single macroblock
2030 * @param dest_y luma destination pointer
2031 * @param dest_cb chroma cb/u destination pointer
2032 * @param dest_cr chroma cr/v destination pointer
2033 * @param dir direction (0->forward, 1->backward)
2034 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2035 * @param pic_op halfpel motion compensation function (average or put normally)
2036 * @param pic_op qpel motion compensation function (average or put normally)
2037 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2039 static inline void MPV_motion(MpegEncContext *s,
2040 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2041 int dir, uint8_t **ref_picture,
2042 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
2044 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
2046 uint8_t *ptr, *dest;
2052 switch(s->mv_type) {
2056 if(s->real_sprite_warping_points==1){
2057 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
2060 gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
2063 }else if(s->quarter_sample){
2064 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2067 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2069 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
2070 ref_picture, pix_op,
2071 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2075 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2078 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2084 if(s->quarter_sample){
2086 motion_x = s->mv[dir][i][0];
2087 motion_y = s->mv[dir][i][1];
2089 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2090 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
2091 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
2093 /* WARNING: do no forget half pels */
2094 src_x = clip(src_x, -16, s->width);
2095 if (src_x == s->width)
2097 src_y = clip(src_y, -16, s->height);
2098 if (src_y == s->height)
2101 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2102 if(s->flags&CODEC_FLAG_EMU_EDGE){
2103 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
2104 || src_y + (motion_y&3) + 8 > s->v_edge_pos){
2105 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
2106 ptr= s->edge_emu_buffer;
2109 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2110 qpix_op[1][dxy](dest, ptr, s->linesize);
2112 mx += s->mv[dir][i][0]/2;
2113 my += s->mv[dir][i][1]/2;
2117 motion_x = s->mv[dir][i][0];
2118 motion_y = s->mv[dir][i][1];
2120 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
2121 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
2122 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
2124 /* WARNING: do no forget half pels */
2125 src_x = clip(src_x, -16, s->width);
2126 if (src_x == s->width)
2128 src_y = clip(src_y, -16, s->height);
2129 if (src_y == s->height)
2132 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2133 if(s->flags&CODEC_FLAG_EMU_EDGE){
2134 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
2135 || src_y + (motion_y&1) + 8 > s->v_edge_pos){
2136 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
2137 ptr= s->edge_emu_buffer;
2140 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2141 pix_op[1][dxy](dest, ptr, s->linesize, 8);
2143 mx += s->mv[dir][i][0];
2144 my += s->mv[dir][i][1];
2148 if(s->flags&CODEC_FLAG_GRAY) break;
2149 /* In case of 8X8, we construct a single chroma motion vector
2150 with a special rounding */
2151 mx= ff_h263_round_chroma(mx);
2152 my= ff_h263_round_chroma(my);
2153 dxy = ((my & 1) << 1) | (mx & 1);
2157 src_x = mb_x * 8 + mx;
2158 src_y = mb_y * 8 + my;
2159 src_x = clip(src_x, -8, s->width/2);
2160 if (src_x == s->width/2)
2162 src_y = clip(src_y, -8, s->height/2);
2163 if (src_y == s->height/2)
2166 offset = (src_y * (s->uvlinesize)) + src_x;
2167 ptr = ref_picture[1] + offset;
2168 if(s->flags&CODEC_FLAG_EMU_EDGE){
2169 if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
2170 || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
2171 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2172 ptr= s->edge_emu_buffer;
2176 pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
2178 ptr = ref_picture[2] + offset;
2180 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2181 ptr= s->edge_emu_buffer;
2183 pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
2186 if (s->picture_structure == PICT_FRAME) {
2187 if(s->quarter_sample){
2189 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2190 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2192 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2194 qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2195 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2197 s->mv[dir][1][0], s->mv[dir][1][1], 8);
2200 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2201 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2203 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2205 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2206 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2208 s->mv[dir][1][0], s->mv[dir][1][1], 8);
2212 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2213 offset= s->field_select[dir][0] ? s->linesize : 0;
2215 ref_picture= s->current_picture.data;
2216 offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
2219 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2220 ref_picture, offset,
2222 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2227 uint8_t ** ref2picture;
2229 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2230 ref2picture= ref_picture;
2231 offset= s->field_select[dir][0] ? s->linesize : 0;
2233 ref2picture= s->current_picture.data;
2234 offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
2237 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2238 ref2picture, offset,
2240 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2243 if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
2244 ref2picture= ref_picture;
2245 offset= s->field_select[dir][1] ? s->linesize : 0;
2247 ref2picture= s->current_picture.data;
2248 offset= s->field_select[dir][1] ? s->linesize : -s->linesize;
2250 // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
2251 mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
2253 ref2picture, offset,
2255 s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
2261 op_pixels_func (*dmv_pix_op)[4];
2264 dmv_pix_op = s->dsp.put_pixels_tab;
2266 if(s->picture_structure == PICT_FRAME){
2267 //put top field from top field
2268 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2271 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2272 //put bottom field from bottom field
2273 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2274 ref_picture, s->linesize,
2276 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2278 dmv_pix_op = s->dsp.avg_pixels_tab;
2280 //avg top field from bottom field
2281 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2282 ref_picture, s->linesize,
2284 s->mv[dir][2][0], s->mv[dir][2][1], 8);
2285 //avg bottom field from top field
2286 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2289 s->mv[dir][3][0], s->mv[dir][3][1], 8);
2292 offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
2295 //put field from the same parity
2296 //same parity is never in the same frame
2297 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2300 s->mv[dir][0][0],s->mv[dir][0][1],16);
2302 // after put we make avg of the same block
2303 dmv_pix_op=s->dsp.avg_pixels_tab;
2305 //opposite parity is always in the same frame if this is second field
2306 if(!s->first_field){
2307 ref_picture = s->current_picture.data;
2308 //top field is one linesize from frame beginig
2309 offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
2310 -s->linesize : s->linesize;
2312 offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
2315 //avg field from the opposite parity
2316 mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
2317 ref_picture, offset,
2319 s->mv[dir][2][0],s->mv[dir][2][1],16);
2328 /* put block[] to dest[] */
2329 static inline void put_dct(MpegEncContext *s,
2330 DCTELEM *block, int i, uint8_t *dest, int line_size)
2332 s->dct_unquantize(s, block, i, s->qscale);
2333 s->dsp.idct_put (dest, line_size, block);
2336 /* add block[] to dest[] */
2337 static inline void add_dct(MpegEncContext *s,
2338 DCTELEM *block, int i, uint8_t *dest, int line_size)
2340 if (s->block_last_index[i] >= 0) {
2341 s->dsp.idct_add (dest, line_size, block);
2345 static inline void add_dequant_dct(MpegEncContext *s,
2346 DCTELEM *block, int i, uint8_t *dest, int line_size)
2348 if (s->block_last_index[i] >= 0) {
2349 s->dct_unquantize(s, block, i, s->qscale);
2351 s->dsp.idct_add (dest, line_size, block);
2356 * cleans dc, ac, coded_block for the current non intra MB
2358 void ff_clean_intra_table_entries(MpegEncContext *s)
2360 int wrap = s->block_wrap[0];
2361 int xy = s->block_index[0];
2364 s->dc_val[0][xy + 1 ] =
2365 s->dc_val[0][xy + wrap] =
2366 s->dc_val[0][xy + 1 + wrap] = 1024;
2368 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2369 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2370 if (s->msmpeg4_version>=3) {
2371 s->coded_block[xy ] =
2372 s->coded_block[xy + 1 ] =
2373 s->coded_block[xy + wrap] =
2374 s->coded_block[xy + 1 + wrap] = 0;
2377 wrap = s->block_wrap[4];
2378 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
2380 s->dc_val[2][xy] = 1024;
2382 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2383 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2385 s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
2388 /* generic function called after a macroblock has been parsed by the
2389 decoder or after it has been encoded by the encoder.
2391 Important variables used:
2392 s->mb_intra : true if intra macroblock
2393 s->mv_dir : motion vector direction
2394 s->mv_type : motion vector type
2395 s->mv : motion vector
2396 s->interlaced_dct : true if interlaced dct used (mpeg2)
2398 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
2401 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2403 if(s->avctx->xvmc_acceleration){
2404 XVMC_decode_mb(s,block);
2412 s->current_picture.qscale_table[mb_xy]= s->qscale;
2414 /* update DC predictors for P macroblocks */
2416 if (s->h263_pred || s->h263_aic) {
2417 if(s->mbintra_table[mb_xy])
2418 ff_clean_intra_table_entries(s);
2422 s->last_dc[2] = 128 << s->intra_dc_precision;
2425 else if (s->h263_pred || s->h263_aic)
2426 s->mbintra_table[mb_xy]=1;
2428 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
2429 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
2430 //FIXME a lot of thet is only needed for !low_delay
2431 const int wrap = s->block_wrap[0];
2432 const int xy = s->block_index[0];
2433 if(s->mv_type != MV_TYPE_8X8){
2434 int motion_x, motion_y;
2438 } else if (s->mv_type == MV_TYPE_16X16) {
2439 motion_x = s->mv[0][0][0];
2440 motion_y = s->mv[0][0][1];
2441 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
2443 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
2444 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
2445 motion_x = (motion_x>>1) | (motion_x&1);
2447 s->field_mv_table[mb_xy][i][0]= s->mv[0][i][0];
2448 s->field_mv_table[mb_xy][i][1]= s->mv[0][i][1];
2449 s->field_select_table[mb_xy][i]= s->field_select[0][i];
2453 /* no update if 8X8 because it has been done during parsing */
2454 s->motion_val[xy][0] = motion_x;
2455 s->motion_val[xy][1] = motion_y;
2456 s->motion_val[xy + 1][0] = motion_x;
2457 s->motion_val[xy + 1][1] = motion_y;
2458 s->motion_val[xy + wrap][0] = motion_x;
2459 s->motion_val[xy + wrap][1] = motion_y;
2460 s->motion_val[xy + 1 + wrap][0] = motion_x;
2461 s->motion_val[xy + 1 + wrap][1] = motion_y;
2464 if(s->encoding){ //FIXME encoding MUST be cleaned up
2465 if (s->mv_type == MV_TYPE_8X8)
2466 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_8x8;
2468 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;
2472 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2473 uint8_t *dest_y, *dest_cb, *dest_cr;
2474 int dct_linesize, dct_offset;
2475 op_pixels_func (*op_pix)[4];
2476 qpel_mc_func (*op_qpix)[16];
2477 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2478 const int uvlinesize= s->current_picture.linesize[1];
2480 /* avoid copy if macroblock skipped in last frame too */
2481 if (s->pict_type != B_TYPE) {
2482 s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
2485 /* skip only during decoding as we might trash the buffers during encoding a bit */
2487 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2488 const int age= s->current_picture.age;
2494 assert(s->pict_type!=I_TYPE);
2496 (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2497 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2499 /* if previous was skipped too, then nothing to do ! */
2500 if (*mbskip_ptr >= age && s->current_picture.reference){
2503 } else if(!s->current_picture.reference){
2504 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2505 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2507 *mbskip_ptr = 0; /* not skipped */
2512 if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
2513 dest_y = s->current_picture.data[0] + mb_x * 16;
2514 dest_cb = s->current_picture.data[1] + mb_x * 8;
2515 dest_cr = s->current_picture.data[2] + mb_x * 8;
2517 dest_y = s->current_picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
2518 dest_cb = s->current_picture.data[1] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2519 dest_cr = s->current_picture.data[2] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2522 if (s->interlaced_dct) {
2523 dct_linesize = linesize * 2;
2524 dct_offset = linesize;
2526 dct_linesize = linesize;
2527 dct_offset = linesize * 8;
2531 /* motion handling */
2532 /* decoding or more than one mb_type (MC was allready done otherwise) */
2533 if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
2534 if ((!s->no_rounding) || s->pict_type==B_TYPE){
2535 op_pix = s->dsp.put_pixels_tab;
2536 op_qpix= s->dsp.put_qpel_pixels_tab;
2538 op_pix = s->dsp.put_no_rnd_pixels_tab;
2539 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2542 if (s->mv_dir & MV_DIR_FORWARD) {
2543 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2544 op_pix = s->dsp.avg_pixels_tab;
2545 op_qpix= s->dsp.avg_qpel_pixels_tab;
2547 if (s->mv_dir & MV_DIR_BACKWARD) {
2548 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2552 /* skip dequant / idct if we are really late ;) */
2553 if(s->hurry_up>1) return;
2555 /* add dct residue */
2556 if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
2557 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
2558 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2559 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2560 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2561 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2563 if(!(s->flags&CODEC_FLAG_GRAY)){
2564 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
2565 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
2567 } else if(s->codec_id != CODEC_ID_WMV2){
2568 add_dct(s, block[0], 0, dest_y, dct_linesize);
2569 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2570 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2571 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2573 if(!(s->flags&CODEC_FLAG_GRAY)){
2574 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2575 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2580 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2584 /* dct only in intra block */
2585 if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
2586 put_dct(s, block[0], 0, dest_y, dct_linesize);
2587 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2588 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2589 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2591 if(!(s->flags&CODEC_FLAG_GRAY)){
2592 put_dct(s, block[4], 4, dest_cb, uvlinesize);
2593 put_dct(s, block[5], 5, dest_cr, uvlinesize);
2596 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2597 s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
2598 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2599 s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
2601 if(!(s->flags&CODEC_FLAG_GRAY)){
2602 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2603 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2610 #ifdef CONFIG_ENCODERS
2612 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2614 static const char tab[64]=
2626 DCTELEM *block= s->block[n];
2627 const int last_index= s->block_last_index[n];
2632 threshold= -threshold;
2636 /* are all which we could set to zero are allready zero? */
2637 if(last_index<=skip_dc - 1) return;
2639 for(i=0; i<=last_index; i++){
2640 const int j = s->intra_scantable.permutated[i];
2641 const int level = ABS(block[j]);
2643 if(skip_dc && i==0) continue;
2652 if(score >= threshold) return;
2653 for(i=skip_dc; i<=last_index; i++){
2654 const int j = s->intra_scantable.permutated[i];
2657 if(block[0]) s->block_last_index[n]= 0;
2658 else s->block_last_index[n]= -1;
2661 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2664 const int maxlevel= s->max_qcoeff;
2665 const int minlevel= s->min_qcoeff;
2668 i=1; //skip clipping of intra dc
2672 for(;i<=last_index; i++){
2673 const int j= s->intra_scantable.permutated[i];
2674 int level = block[j];
2676 if (level>maxlevel) level=maxlevel;
2677 else if(level<minlevel) level=minlevel;
2684 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2689 for(x=0; x<16; x+=4){
2690 score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
2691 +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2699 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2704 for(x=0; x<16; x++){
2705 score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2714 #define SQ(a) ((a)*(a))
2716 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2721 for(x=0; x<16; x+=4){
2722 score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
2723 +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2731 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2736 for(x=0; x<16; x++){
2737 score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2748 #endif //CONFIG_ENCODERS
2752 * @param h is the normal height, this will be reduced automatically if needed for the last row
2754 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2755 if (s->avctx->draw_horiz_band) {
2757 uint8_t *src_ptr[3];
2760 if(s->picture_structure != PICT_FRAME){
2763 if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
2766 h= FFMIN(h, s->height - y);
2768 if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
2769 src= (AVFrame*)s->current_picture_ptr;
2770 else if(s->last_picture_ptr)
2771 src= (AVFrame*)s->last_picture_ptr;
2775 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
2781 offset[0]= y * s->linesize;;
2783 offset[2]= (y>>1) * s->uvlinesize;;
2789 s->avctx->draw_horiz_band(s->avctx, src, offset,
2790 y, s->picture_structure, h);
2794 #ifdef CONFIG_ENCODERS
2796 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2798 const int mb_x= s->mb_x;
2799 const int mb_y= s->mb_y;
2802 int dct_offset = s->linesize*8; //default for progressive frames
2804 for(i=0; i<6; i++) skip_dct[i]=0;
2806 if(s->adaptive_quant){
2807 s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_stride] - s->qscale;
2809 if(s->out_format==FMT_H263){
2810 if (s->dquant> 2) s->dquant= 2;
2811 else if(s->dquant<-2) s->dquant=-2;
2814 if(s->codec_id==CODEC_ID_MPEG4){
2816 if(s->mv_dir&MV_DIRECT)
2819 assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2822 s->qscale+= s->dquant;
2823 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2824 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2832 wrap_y = s->linesize;
2833 ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2835 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2836 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2837 ptr= s->edge_emu_buffer;
2841 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2842 int progressive_score, interlaced_score;
2844 progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2845 interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
2847 if(progressive_score > interlaced_score + 100){
2848 s->interlaced_dct=1;
2853 s->interlaced_dct=0;
2856 s->dsp.get_pixels(s->block[0], ptr , wrap_y);
2857 s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
2858 s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
2859 s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2861 if(s->flags&CODEC_FLAG_GRAY){
2865 int wrap_c = s->uvlinesize;
2866 ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2868 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2869 ptr= s->edge_emu_buffer;
2871 s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2873 ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2875 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2876 ptr= s->edge_emu_buffer;
2878 s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2881 op_pixels_func (*op_pix)[4];
2882 qpel_mc_func (*op_qpix)[16];
2883 uint8_t *dest_y, *dest_cb, *dest_cr;
2884 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2888 dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
2889 dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
2890 dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
2891 wrap_y = s->linesize;
2892 wrap_c = s->uvlinesize;
2893 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2894 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2895 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2897 if ((!s->no_rounding) || s->pict_type==B_TYPE){
2898 op_pix = s->dsp.put_pixels_tab;
2899 op_qpix= s->dsp.put_qpel_pixels_tab;
2901 op_pix = s->dsp.put_no_rnd_pixels_tab;
2902 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2905 if (s->mv_dir & MV_DIR_FORWARD) {
2906 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2907 op_pix = s->dsp.avg_pixels_tab;
2908 op_qpix= s->dsp.avg_qpel_pixels_tab;
2910 if (s->mv_dir & MV_DIR_BACKWARD) {
2911 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2914 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2915 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2916 ptr_y= s->edge_emu_buffer;
2920 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2921 int progressive_score, interlaced_score;
2923 progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
2924 + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
2925 interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
2926 + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
2928 if(progressive_score > interlaced_score + 600){
2929 s->interlaced_dct=1;
2934 s->interlaced_dct=0;
2937 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
2938 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2939 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
2940 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2942 if(s->flags&CODEC_FLAG_GRAY){
2947 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2948 ptr_cb= s->edge_emu_buffer;
2950 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2952 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2953 ptr_cr= s->edge_emu_buffer;
2955 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2957 /* pre quantization */
2958 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
2960 if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2961 if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2962 if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2963 if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2964 if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
2965 if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
2971 if(skip_dct[i]) num++;
2974 if(s->mb_x==0 && s->mb_y==0){
2976 printf("%6d %1d\n", stat[i], i);
2989 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_stride*mb_y+mb_x] + 1.0) /
2990 ((s->mb_var[s->mb_stride*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2992 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
2993 (s->mb_type[s->mb_stride*mb_y+mb_x] > 0) ? 'I' : 'P',
2994 s->qscale, adap_parm, s->qscale*adap_parm,
2995 s->mb_var[s->mb_stride*mb_y+mb_x], s->avg_mb_var);
2998 /* DCT & quantize */
2999 if(s->out_format==FMT_MJPEG){
3002 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
3003 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
3009 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
3010 // FIXME we could decide to change to quantizer instead of clipping
3011 // JS: I don't think that would be a good idea it could lower quality instead
3012 // of improve it. Just INTRADC clipping deserves changes in quantizer
3013 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
3015 s->block_last_index[i]= -1;
3017 if(s->luma_elim_threshold && !s->mb_intra)
3019 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
3020 if(s->chroma_elim_threshold && !s->mb_intra)
3022 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
3025 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
3026 s->block_last_index[4]=
3027 s->block_last_index[5]= 0;
3029 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
3032 /* huffman encode */
3033 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
3034 case CODEC_ID_MPEG1VIDEO:
3035 mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
3037 case CODEC_ID_MPEG4:
3038 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3039 case CODEC_ID_MSMPEG4V2:
3040 case CODEC_ID_MSMPEG4V3:
3042 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3044 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
3046 case CODEC_ID_H263P:
3049 h263_encode_mb(s, s->block, motion_x, motion_y); break;
3051 case CODEC_ID_MJPEG:
3052 mjpeg_encode_mb(s, s->block); break;
3058 #endif //CONFIG_ENCODERS
3061 * combines the (truncated) bitstream to a complete frame
3062 * @returns -1 if no complete frame could be created
3064 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
3065 ParseContext *pc= &s->parse_context;
3069 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3070 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3074 /* copy overreaded byes from last frame into buffer */
3075 for(; pc->overread>0; pc->overread--){
3076 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
3079 pc->last_index= pc->index;
3081 /* copy into buffer end return */
3082 if(next == END_NOT_FOUND){
3083 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3085 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
3086 pc->index += *buf_size;
3091 pc->overread_index= pc->index + next;
3093 /* append to buffer */
3095 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3097 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
3102 /* store overread bytes */
3103 for(;next < 0; next++){
3104 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
3110 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3111 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3118 void ff_mpeg_flush(AVCodecContext *avctx){
3120 MpegEncContext *s = avctx->priv_data;
3122 for(i=0; i<MAX_PICTURE_COUNT; i++){
3123 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
3124 || s->picture[i].type == FF_BUFFER_TYPE_USER))
3125 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
3127 s->last_picture_ptr = s->next_picture_ptr = NULL;
3130 #ifdef CONFIG_ENCODERS
3131 void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
3133 int bytes= length>>4;
3134 int bits= length&15;
3137 if(length==0) return;
3139 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
3140 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
3143 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
3146 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
3149 d->mb_skip_run= s->mb_skip_run;
3151 d->last_dc[i]= s->last_dc[i];
3154 d->mv_bits= s->mv_bits;
3155 d->i_tex_bits= s->i_tex_bits;
3156 d->p_tex_bits= s->p_tex_bits;
3157 d->i_count= s->i_count;
3158 d->f_count= s->f_count;
3159 d->b_count= s->b_count;
3160 d->skip_count= s->skip_count;
3161 d->misc_bits= s->misc_bits;
3164 d->mb_skiped= s->mb_skiped;
3165 d->qscale= s->qscale;
3168 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
3171 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
3172 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
3175 d->mb_skip_run= s->mb_skip_run;
3177 d->last_dc[i]= s->last_dc[i];
3180 d->mv_bits= s->mv_bits;
3181 d->i_tex_bits= s->i_tex_bits;
3182 d->p_tex_bits= s->p_tex_bits;
3183 d->i_count= s->i_count;
3184 d->f_count= s->f_count;
3185 d->b_count= s->b_count;
3186 d->skip_count= s->skip_count;
3187 d->misc_bits= s->misc_bits;
3189 d->mb_intra= s->mb_intra;
3190 d->mb_skiped= s->mb_skiped;
3191 d->mv_type= s->mv_type;
3192 d->mv_dir= s->mv_dir;
3194 if(s->data_partitioning){
3196 d->tex_pb= s->tex_pb;
3200 d->block_last_index[i]= s->block_last_index[i];
3201 d->interlaced_dct= s->interlaced_dct;
3202 d->qscale= s->qscale;
3205 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
3206 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
3207 int *dmin, int *next_block, int motion_x, int motion_y)
3211 copy_context_before_encode(s, backup, type);
3213 s->block= s->blocks[*next_block];
3214 s->pb= pb[*next_block];
3215 if(s->data_partitioning){
3216 s->pb2 = pb2 [*next_block];
3217 s->tex_pb= tex_pb[*next_block];
3220 encode_mb(s, motion_x, motion_y);
3222 bits_count= get_bit_count(&s->pb);
3223 if(s->data_partitioning){
3224 bits_count+= get_bit_count(&s->pb2);
3225 bits_count+= get_bit_count(&s->tex_pb);
3228 if(bits_count<*dmin){
3232 copy_context_after_encode(best, s, type);
3236 static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
3237 uint32_t *sq = squareTbl + 256;
3242 return s->dsp.sse[0](NULL, src1, src2, stride);
3243 else if(w==8 && h==8)
3244 return s->dsp.sse[1](NULL, src1, src2, stride);
3248 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
3257 static void encode_picture(MpegEncContext *s, int picture_number)
3259 int mb_x, mb_y, pdif = 0;
3262 MpegEncContext best_s, backup_s;
3263 uint8_t bit_buf[2][3000];
3264 uint8_t bit_buf2[2][3000];
3265 uint8_t bit_buf_tex[2][3000];
3266 PutBitContext pb[2], pb2[2], tex_pb[2];
3269 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
3270 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
3271 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
3274 s->picture_number = picture_number;
3276 /* Reset the average MB variance */
3277 s->current_picture.mb_var_sum = 0;
3278 s->current_picture.mc_mb_var_sum = 0;
3281 /* we need to initialize some time vars before we can encode b-frames */
3282 // RAL: Condition added for MPEG1VIDEO
3283 if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
3284 ff_set_mpeg4_time(s, s->picture_number);
3287 s->scene_change_score=0;
3289 s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
3291 if(s->pict_type==I_TYPE){
3292 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3293 else s->no_rounding=0;
3294 }else if(s->pict_type!=B_TYPE){
3295 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
3296 s->no_rounding ^= 1;
3299 /* Estimate motion for every MB */
3300 s->mb_intra=0; //for the rate distoration & bit compare functions
3301 if(s->pict_type != I_TYPE){
3302 if(s->pict_type != B_TYPE){
3303 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
3305 s->me.dia_size= s->avctx->pre_dia_size;
3307 for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
3308 for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
3311 ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
3318 s->me.dia_size= s->avctx->dia_size;
3319 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3320 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3321 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3322 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3323 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3324 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3327 s->block_index[0]+=2;
3328 s->block_index[1]+=2;
3329 s->block_index[2]+=2;
3330 s->block_index[3]+=2;
3332 /* compute motion vector & mb_type and store in context */
3333 if(s->pict_type==B_TYPE)
3334 ff_estimate_b_frame_motion(s, mb_x, mb_y);
3336 ff_estimate_p_frame_motion(s, mb_x, mb_y);
3339 }else /* if(s->pict_type == I_TYPE) */{
3341 //FIXME do we need to zero them?
3342 memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
3343 memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_stride)*s->mb_height*2);
3344 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
3346 if(!s->fixed_qscale){
3347 /* finding spatial complexity for I-frame rate control */
3348 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3349 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3352 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
3354 int sum = s->dsp.pix_sum(pix, s->linesize);
3356 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
3358 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
3359 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
3360 s->current_picture.mb_var_sum += varc;
3367 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
3368 s->pict_type= I_TYPE;
3369 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
3370 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3374 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
3375 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
3377 ff_fix_long_p_mvs(s);
3380 if(s->pict_type==B_TYPE){
3383 a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
3384 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
3385 s->f_code = FFMAX(a, b);
3387 a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
3388 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
3389 s->b_code = FFMAX(a, b);
3391 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
3392 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
3393 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
3394 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
3398 if (s->fixed_qscale)
3399 s->frame_qscale = s->current_picture.quality;
3401 s->frame_qscale = ff_rate_estimate_qscale(s);
3403 if(s->adaptive_quant){
3405 switch(s->codec_id){
3406 case CODEC_ID_MPEG4:
3407 ff_clean_mpeg4_qscales(s);
3410 case CODEC_ID_H263P:
3412 ff_clean_h263_qscales(s);
3417 s->qscale= s->current_picture.qscale_table[0];
3419 s->qscale= (int)(s->frame_qscale + 0.5);
3421 if (s->out_format == FMT_MJPEG) {
3422 /* for mjpeg, we do include qscale in the matrix */
3423 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
3425 int j= s->dsp.idct_permutation[i];
3427 s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3429 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3430 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
3433 //FIXME var duplication
3434 s->current_picture.key_frame= s->pict_type == I_TYPE;
3435 s->current_picture.pict_type= s->pict_type;
3437 if(s->current_picture.key_frame)
3438 s->picture_in_gop_number=0;
3440 s->last_bits= get_bit_count(&s->pb);
3441 switch(s->out_format) {
3443 mjpeg_picture_header(s);
3447 if (s->codec_id == CODEC_ID_WMV2)
3448 ff_wmv2_encode_picture_header(s, picture_number);
3449 else if (s->h263_msmpeg4)
3450 msmpeg4_encode_picture_header(s, picture_number);
3451 else if (s->h263_pred)
3452 mpeg4_encode_picture_header(s, picture_number);
3453 else if (s->h263_rv10)
3454 rv10_encode_picture_header(s, picture_number);
3455 else if (s->codec_id == CODEC_ID_FLV1)
3456 ff_flv_encode_picture_header(s, picture_number);
3458 h263_encode_picture_header(s, picture_number);
3462 mpeg1_encode_picture_header(s, picture_number);
3465 bits= get_bit_count(&s->pb);
3466 s->header_bits= bits - s->last_bits;
3478 /* init last dc values */
3479 /* note: quant matrix value (8) is implied here */
3480 s->last_dc[i] = 128;
3482 s->current_picture_ptr->error[i] = 0;
3485 s->last_mv[0][0][0] = 0;
3486 s->last_mv[0][0][1] = 0;
3487 s->last_mv[1][0][0] = 0;
3488 s->last_mv[1][0][1] = 0;
3493 switch(s->codec_id){
3495 case CODEC_ID_H263P:
3497 s->gob_index = ff_h263_get_gob_height(s);
3499 case CODEC_ID_MPEG4:
3500 if(s->partitioned_frame)
3501 ff_mpeg4_init_partitions(s);
3508 s->first_slice_line = 1;
3509 s->ptr_lastgob = s->pb.buf;
3510 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3511 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3512 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
3514 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3515 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3516 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3517 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3518 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
3519 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
3520 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3521 const int xy= mb_y*s->mb_stride + mb_x;
3522 int mb_type= s->mb_type[xy];
3528 s->block_index[0]+=2;
3529 s->block_index[1]+=2;
3530 s->block_index[2]+=2;
3531 s->block_index[3]+=2;
3532 s->block_index[4]++;
3533 s->block_index[5]++;
3535 /* write gob / video packet header */
3538 int current_packet_size, is_gob_start;
3540 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
3543 if(s->codec_id==CODEC_ID_MPEG4){
3544 if(current_packet_size >= s->rtp_payload_size
3545 && s->mb_y + s->mb_x>0){
3547 if(s->partitioned_frame){
3548 ff_mpeg4_merge_partitions(s);
3549 ff_mpeg4_init_partitions(s);
3551 ff_mpeg4_encode_video_packet_header(s);
3553 if(s->flags&CODEC_FLAG_PASS1){
3554 int bits= get_bit_count(&s->pb);
3555 s->misc_bits+= bits - s->last_bits;
3558 ff_mpeg4_clean_buffers(s);
3561 }else if(s->codec_id==CODEC_ID_MPEG1VIDEO){
3562 if( current_packet_size >= s->rtp_payload_size
3563 && s->mb_y + s->mb_x>0 && s->mb_skip_run==0){
3564 ff_mpeg1_encode_slice_header(s);
3565 ff_mpeg1_clean_buffers(s);
3569 if(current_packet_size >= s->rtp_payload_size
3570 && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
3572 h263_encode_gob_header(s, mb_y);
3578 s->ptr_lastgob = pbBufPtr(&s->pb);
3579 s->first_slice_line=1;
3580 s->resync_mb_x=mb_x;
3581 s->resync_mb_y=mb_y;
3586 if( (s->resync_mb_x == s->mb_x)
3587 && s->resync_mb_y+1 == s->mb_y){
3588 s->first_slice_line=0;
3591 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
3593 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3595 copy_context_before_encode(&backup_s, s, -1);
3597 best_s.data_partitioning= s->data_partitioning;
3598 best_s.partitioned_frame= s->partitioned_frame;
3599 if(s->data_partitioning){
3600 backup_s.pb2= s->pb2;
3601 backup_s.tex_pb= s->tex_pb;
3604 if(mb_type&MB_TYPE_INTER){
3605 s->mv_dir = MV_DIR_FORWARD;
3606 s->mv_type = MV_TYPE_16X16;
3608 s->mv[0][0][0] = s->p_mv_table[xy][0];
3609 s->mv[0][0][1] = s->p_mv_table[xy][1];
3610 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
3611 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3613 if(mb_type&MB_TYPE_INTER4V){
3614 s->mv_dir = MV_DIR_FORWARD;
3615 s->mv_type = MV_TYPE_8X8;
3618 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3619 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3621 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
3622 &dmin, &next_block, 0, 0);
3624 if(mb_type&MB_TYPE_FORWARD){
3625 s->mv_dir = MV_DIR_FORWARD;
3626 s->mv_type = MV_TYPE_16X16;
3628 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3629 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3630 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
3631 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3633 if(mb_type&MB_TYPE_BACKWARD){
3634 s->mv_dir = MV_DIR_BACKWARD;
3635 s->mv_type = MV_TYPE_16X16;
3637 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3638 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3639 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3640 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3642 if(mb_type&MB_TYPE_BIDIR){
3643 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3644 s->mv_type = MV_TYPE_16X16;
3646 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3647 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3648 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3649 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3650 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
3651 &dmin, &next_block, 0, 0);
3653 if(mb_type&MB_TYPE_DIRECT){
3654 int mx= s->b_direct_mv_table[xy][0];
3655 int my= s->b_direct_mv_table[xy][1];
3657 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3660 ff_mpeg4_set_direct_mv(s, mx, my);
3662 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
3663 &dmin, &next_block, mx, my);
3665 if(mb_type&MB_TYPE_INTRA){
3667 s->mv_type = MV_TYPE_16X16;
3671 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
3672 &dmin, &next_block, 0, 0);
3673 /* force cleaning of ac/dc pred stuff if needed ... */
3674 if(s->h263_pred || s->h263_aic)
3675 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3677 copy_context_after_encode(s, &best_s, -1);
3679 pb_bits_count= get_bit_count(&s->pb);
3680 flush_put_bits(&s->pb);
3681 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3684 if(s->data_partitioning){
3685 pb2_bits_count= get_bit_count(&s->pb2);
3686 flush_put_bits(&s->pb2);
3687 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3688 s->pb2= backup_s.pb2;
3690 tex_pb_bits_count= get_bit_count(&s->tex_pb);
3691 flush_put_bits(&s->tex_pb);
3692 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3693 s->tex_pb= backup_s.tex_pb;
3695 s->last_bits= get_bit_count(&s->pb);
3697 int motion_x, motion_y;
3699 int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_stride];
3701 if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){
3702 /* get luma score */
3703 if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
3704 intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_stride]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
3708 int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_stride]; //FIXME
3711 dest_y = s->new_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
3713 for(i=0; i<16; i++){
3714 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
3715 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
3716 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
3717 *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
3721 intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
3723 /* printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8,
3724 s->current_picture.mb_var[mb_x + mb_y*s->mb_stride],
3725 s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_stride]);*/
3728 /* get chroma score */
3729 if(s->avctx->mb_cmp&FF_CMP_CHROMA){
3737 if(s->out_format == FMT_H263){
3738 mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
3740 mean= (s->last_dc[i] + 4)>>3;
3742 dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
3746 *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
3747 *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
3750 intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
3755 switch(s->avctx->mb_cmp&0xFF){
3758 intra_score+= 32*s->qscale;
3761 intra_score+= 24*s->qscale*s->qscale;
3764 intra_score+= 96*s->qscale;
3767 intra_score+= 48*s->qscale;
3774 intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
3778 if(intra_score < inter_score)
3779 mb_type= MB_TYPE_INTRA;
3782 s->mv_type=MV_TYPE_16X16;
3783 // only one MB-Type possible
3789 motion_x= s->mv[0][0][0] = 0;
3790 motion_y= s->mv[0][0][1] = 0;
3793 s->mv_dir = MV_DIR_FORWARD;
3795 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3796 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3798 case MB_TYPE_INTER4V:
3799 s->mv_dir = MV_DIR_FORWARD;
3800 s->mv_type = MV_TYPE_8X8;
3803 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3804 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3806 motion_x= motion_y= 0;
3808 case MB_TYPE_DIRECT:
3809 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3811 motion_x=s->b_direct_mv_table[xy][0];
3812 motion_y=s->b_direct_mv_table[xy][1];
3814 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3818 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3822 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3823 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3824 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3825 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3827 case MB_TYPE_BACKWARD:
3828 s->mv_dir = MV_DIR_BACKWARD;
3830 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3831 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3833 case MB_TYPE_FORWARD:
3834 s->mv_dir = MV_DIR_FORWARD;
3836 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3837 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3838 // printf(" %d %d ", motion_x, motion_y);
3841 motion_x=motion_y=0; //gcc warning fix
3842 printf("illegal MB type\n");
3845 encode_mb(s, motion_x, motion_y);
3847 // RAL: Update last macrobloc type
3848 s->last_mv_dir = s->mv_dir;
3851 /* clean the MV table in IPS frames for direct mode in B frames */
3852 if(s->mb_intra /* && I,P,S_TYPE */){
3853 s->p_mv_table[xy][0]=0;
3854 s->p_mv_table[xy][1]=0;
3857 MPV_decode_mb(s, s->block);
3859 if(s->flags&CODEC_FLAG_PSNR){
3863 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3864 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3866 s->current_picture_ptr->error[0] += sse(
3868 s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3869 s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3871 s->current_picture_ptr->error[1] += sse(
3873 s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3874 s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3875 w>>1, h>>1, s->uvlinesize);
3876 s->current_picture_ptr->error[2] += sse(
3878 s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3879 s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3880 w>>1, h>>1, s->uvlinesize);
3882 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
3888 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
3889 ff_mpeg4_merge_partitions(s);
3891 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
3892 msmpeg4_encode_ext_header(s);
3894 if(s->codec_id==CODEC_ID_MPEG4)
3895 ff_mpeg4_stuffing(&s->pb);
3898 //if (s->gob_number)
3899 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
3901 /* Send the last GOB if RTP */
3903 flush_put_bits(&s->pb);
3904 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
3905 /* Call the RTP callback to send the last GOB */
3906 if (s->rtp_callback)
3907 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
3908 s->ptr_lastgob = pbBufPtr(&s->pb);
3909 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
3913 static int dct_quantize_trellis_c(MpegEncContext *s,
3914 DCTELEM *block, int n,
3915 int qscale, int *overflow){
3917 const uint8_t *scantable= s->intra_scantable.scantable;
3919 unsigned int threshold1, threshold2;
3929 int coeff_count[64];
3930 int lambda, qmul, qadd, start_i, last_non_zero, i;
3931 const int esc_length= s->ac_esc_length;
3933 uint8_t * last_length;
3937 s->dsp.fdct (block);
3940 qadd= ((qscale-1)|1)*8;
3951 /* For AIC we skip quant/dequant of INTRADC */
3956 /* note: block[0] is assumed to be positive */
3957 block[0] = (block[0] + (q >> 1)) / q;
3960 qmat = s->q_intra_matrix[qscale];
3961 if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
3962 bias= 1<<(QMAT_SHIFT-1);
3963 length = s->intra_ac_vlc_length;
3964 last_length= s->intra_ac_vlc_last_length;
3968 qmat = s->q_inter_matrix[qscale];
3969 length = s->inter_ac_vlc_length;
3970 last_length= s->inter_ac_vlc_last_length;
3973 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3974 threshold2= (threshold1<<1);
3976 for(i=start_i; i<64; i++) {
3977 const int j = scantable[i];
3978 const int k= i-start_i;
3979 int level = block[j];
3980 level = level * qmat[j];
3982 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3983 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3984 if(((unsigned)(level+threshold1))>threshold2){
3986 level= (bias + level)>>QMAT_SHIFT;
3988 coeff[1][k]= level-1;
3989 // coeff[2][k]= level-2;
3991 level= (bias - level)>>QMAT_SHIFT;
3992 coeff[0][k]= -level;
3993 coeff[1][k]= -level+1;
3994 // coeff[2][k]= -level+2;
3996 coeff_count[k]= FFMIN(level, 2);
4000 coeff[0][k]= (level>>31)|1;
4005 *overflow= s->max_qcoeff < max; //overflow might have happend
4007 if(last_non_zero < start_i){
4008 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
4009 return last_non_zero;
4012 lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
4015 for(i=0; i<=last_non_zero - start_i; i++){
4016 int level_index, run, j;
4017 const int dct_coeff= block[ scantable[i + start_i] ];
4018 const int zero_distoration= dct_coeff*dct_coeff;
4019 int best_score=256*256*256*120;
4021 last_score += zero_distoration;
4022 for(level_index=0; level_index < coeff_count[i]; level_index++){
4024 int level= coeff[level_index][i];
4029 if(s->out_format == FMT_H263){
4031 unquant_coeff= level*qmul + qadd;
4033 unquant_coeff= level*qmul - qadd;
4036 j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
4039 unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
4040 unquant_coeff = -((unquant_coeff - 1) | 1);
4042 unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
4043 unquant_coeff = (unquant_coeff - 1) | 1;
4047 unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
4048 unquant_coeff = -((unquant_coeff - 1) | 1);
4050 unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
4051 unquant_coeff = (unquant_coeff - 1) | 1;
4057 distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
4059 if((level&(~127)) == 0){
4060 for(run=0; run<=i - left_limit; run++){
4061 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4062 score += score_tab[i-run];
4064 if(score < best_score){
4066 score_tab[i+1]= score;
4068 level_tab[i+1]= level-64;
4072 if(s->out_format == FMT_H263){
4073 for(run=0; run<=i - left_limit; run++){
4074 int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4075 score += score_tab[i-run];
4076 if(score < last_score){
4079 last_level= level-64;
4085 distoration += esc_length*lambda;
4086 for(run=0; run<=i - left_limit; run++){
4087 int score= distoration + score_tab[i-run];
4089 if(score < best_score){
4091 score_tab[i+1]= score;
4093 level_tab[i+1]= level-64;
4097 if(s->out_format == FMT_H263){
4098 for(run=0; run<=i - left_limit; run++){
4099 int score= distoration + score_tab[i-run];
4100 if(score < last_score){
4103 last_level= level-64;
4111 for(j=left_limit; j<=i; j++){
4112 score_tab[j] += zero_distoration;
4114 score_limit+= zero_distoration;
4115 if(score_tab[i+1] < score_limit)
4116 score_limit= score_tab[i+1];
4118 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
4119 while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
4122 //FIXME add some cbp penalty
4124 if(s->out_format != FMT_H263){
4125 last_score= 256*256*256*120;
4126 for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
4127 int score= score_tab[i];
4128 if(i) score += lambda*2; //FIXME exacter?
4130 if(score < last_score){
4133 last_level= level_tab[i];
4134 last_run= run_tab[i];
4139 last_non_zero= last_i - 1 + start_i;
4140 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
4142 if(last_non_zero < start_i)
4143 return last_non_zero;
4147 //FIXME use permutated scantable
4148 block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
4151 for(;i>0 ; i -= run_tab[i] + 1){
4152 const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
4154 block[j]= level_tab[i];
4158 return last_non_zero;
4161 static int dct_quantize_c(MpegEncContext *s,
4162 DCTELEM *block, int n,
4163 int qscale, int *overflow)
4165 int i, j, level, last_non_zero, q;
4167 const uint8_t *scantable= s->intra_scantable.scantable;
4170 unsigned int threshold1, threshold2;
4172 s->dsp.fdct (block);
4182 /* For AIC we skip quant/dequant of INTRADC */
4185 /* note: block[0] is assumed to be positive */
4186 block[0] = (block[0] + (q >> 1)) / q;
4189 qmat = s->q_intra_matrix[qscale];
4190 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4194 qmat = s->q_inter_matrix[qscale];
4195 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4197 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4198 threshold2= (threshold1<<1);
4203 level = level * qmat[j];
4205 // if( bias+level >= (1<<QMAT_SHIFT)
4206 // || bias-level >= (1<<QMAT_SHIFT)){
4207 if(((unsigned)(level+threshold1))>threshold2){
4209 level= (bias + level)>>QMAT_SHIFT;
4212 level= (bias - level)>>QMAT_SHIFT;
4221 *overflow= s->max_qcoeff < max; //overflow might have happend
4223 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4224 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
4225 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
4227 return last_non_zero;
4230 #endif //CONFIG_ENCODERS
4232 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
4233 DCTELEM *block, int n, int qscale)
4235 int i, level, nCoeffs;
4236 const uint16_t *quant_matrix;
4238 nCoeffs= s->block_last_index[n];
4242 block[0] = block[0] * s->y_dc_scale;
4244 block[0] = block[0] * s->c_dc_scale;
4245 /* XXX: only mpeg1 */
4246 quant_matrix = s->intra_matrix;
4247 for(i=1;i<=nCoeffs;i++) {
4248 int j= s->intra_scantable.permutated[i];
4253 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4254 level = (level - 1) | 1;
4257 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4258 level = (level - 1) | 1;
4261 if (level < -2048 || level > 2047)
4262 fprintf(stderr, "unquant error %d %d\n", i, level);
4269 quant_matrix = s->inter_matrix;
4270 for(;i<=nCoeffs;i++) {
4271 int j= s->intra_scantable.permutated[i];
4276 level = (((level << 1) + 1) * qscale *
4277 ((int) (quant_matrix[j]))) >> 4;
4278 level = (level - 1) | 1;
4281 level = (((level << 1) + 1) * qscale *
4282 ((int) (quant_matrix[j]))) >> 4;
4283 level = (level - 1) | 1;
4286 if (level < -2048 || level > 2047)
4287 fprintf(stderr, "unquant error %d %d\n", i, level);
4295 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
4296 DCTELEM *block, int n, int qscale)
4298 int i, level, nCoeffs;
4299 const uint16_t *quant_matrix;
4301 if(s->alternate_scan) nCoeffs= 63;
4302 else nCoeffs= s->block_last_index[n];
4306 block[0] = block[0] * s->y_dc_scale;
4308 block[0] = block[0] * s->c_dc_scale;
4309 quant_matrix = s->intra_matrix;
4310 for(i=1;i<=nCoeffs;i++) {
4311 int j= s->intra_scantable.permutated[i];
4316 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4319 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4322 if (level < -2048 || level > 2047)
4323 fprintf(stderr, "unquant error %d %d\n", i, level);
4331 quant_matrix = s->inter_matrix;
4332 for(;i<=nCoeffs;i++) {
4333 int j= s->intra_scantable.permutated[i];
4338 level = (((level << 1) + 1) * qscale *
4339 ((int) (quant_matrix[j]))) >> 4;
4342 level = (((level << 1) + 1) * qscale *
4343 ((int) (quant_matrix[j]))) >> 4;
4346 if (level < -2048 || level > 2047)
4347 fprintf(stderr, "unquant error %d %d\n", i, level);
4358 static void dct_unquantize_h263_c(MpegEncContext *s,
4359 DCTELEM *block, int n, int qscale)
4361 int i, level, qmul, qadd;
4364 assert(s->block_last_index[n]>=0);
4366 qadd = (qscale - 1) | 1;
4372 block[0] = block[0] * s->y_dc_scale;
4374 block[0] = block[0] * s->c_dc_scale;
4378 nCoeffs= 63; //does not allways use zigzag table
4381 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
4384 for(;i<=nCoeffs;i++) {
4388 level = level * qmul - qadd;
4390 level = level * qmul + qadd;
4393 if (level < -2048 || level > 2047)
4394 fprintf(stderr, "unquant error %d %d\n", i, level);
4402 static const AVOption mpeg4_options[] =
4404 AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
4405 AVOPTION_CODEC_FLAG("vhq", "very high quality", flags, CODEC_FLAG_HQ, 0),
4406 AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
4407 "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
4408 bit_rate_tolerance, 4, 240000000, 8000),
4409 AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
4410 AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
4411 AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
4412 rc_eq, "tex^qComp,option1,options2", 0),
4413 AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
4414 rc_min_rate, 4, 24000000, 0),
4415 AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
4416 rc_max_rate, 4, 24000000, 0),
4417 AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
4418 rc_buffer_aggressivity, 4, 24000000, 0),
4419 AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
4420 rc_initial_cplx, 0., 9999999., 0),
4421 AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
4422 i_quant_factor, 0., 0., 0),
4423 AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
4424 i_quant_factor, -999999., 999999., 0),
4425 AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
4426 dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
4427 AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
4428 lumi_masking, 0., 999999., 0),
4429 AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
4430 temporal_cplx_masking, 0., 999999., 0),
4431 AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
4432 spatial_cplx_masking, 0., 999999., 0),
4433 AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
4434 p_masking, 0., 999999., 0),
4435 AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
4436 dark_masking, 0., 999999., 0),
4437 AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
4438 idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
4440 AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
4442 AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
4445 AVOPTION_CODEC_INT("me_cmp", "ME compare function",
4446 me_cmp, 0, 24000000, 0),
4447 AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
4448 me_sub_cmp, 0, 24000000, 0),
4451 AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
4452 dia_size, 0, 24000000, 0),
4453 AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
4454 last_predictor_count, 0, 24000000, 0),
4456 AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
4457 pre_me, 0, 24000000, 0),
4458 AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
4459 me_pre_cmp, 0, 24000000, 0),
4461 AVOPTION_CODEC_INT("me_range", "maximum ME search range",
4462 me_range, 0, 24000000, 0),
4463 AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
4464 pre_dia_size, 0, 24000000, 0),
4465 AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
4466 me_subpel_quality, 0, 24000000, 0),
4467 AVOPTION_CODEC_INT("me_range", "maximum ME search range",
4468 me_range, 0, 24000000, 0),
4469 AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
4470 flags, CODEC_FLAG_PSNR, 0),
4471 AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
4473 AVOPTION_SUB(avoptions_common),
4477 #ifdef CONFIG_ENCODERS
4479 AVCodec mpeg1video_encoder = {
4482 CODEC_ID_MPEG1VIDEO,
4483 sizeof(MpegEncContext),
4491 AVCodec h263_encoder = {
4495 sizeof(MpegEncContext),
4501 AVCodec h263p_encoder = {
4505 sizeof(MpegEncContext),
4511 AVCodec flv_encoder = {
4515 sizeof(MpegEncContext),
4521 AVCodec rv10_encoder = {
4525 sizeof(MpegEncContext),
4531 AVCodec mpeg4_encoder = {
4535 sizeof(MpegEncContext),
4539 .options = mpeg4_options,
4542 AVCodec msmpeg4v1_encoder = {
4546 sizeof(MpegEncContext),
4550 .options = mpeg4_options,
4553 AVCodec msmpeg4v2_encoder = {
4557 sizeof(MpegEncContext),
4561 .options = mpeg4_options,
4564 AVCodec msmpeg4v3_encoder = {
4568 sizeof(MpegEncContext),
4572 .options = mpeg4_options,
4575 AVCodec wmv1_encoder = {
4579 sizeof(MpegEncContext),
4583 .options = mpeg4_options,
4588 AVCodec mjpeg_encoder = {
4592 sizeof(MpegEncContext),
4598 #endif //CONFIG_ENCODERS