Avoid linking with h263.c functions when the relevant codecs
[libav.git] / libavcodec / mpegvideo_enc.c
CommitLineData
3ada94ba
BF
1/*
2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
7b94177e
DB
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
3ada94ba
BF
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3ada94ba
BF
23 */
24
25/**
26 * @file mpegvideo_enc.c
27 * The simplest mpeg encoder (well, it was the simplest!).
28 */
29
30#include "avcodec.h"
31#include "dsputil.h"
32#include "mpegvideo.h"
33#include "mpegvideo_common.h"
34#include "mjpegenc.h"
35#include "msmpeg4.h"
eb75a698 36#include "h263.h"
3ada94ba
BF
37#include "faandct.h"
38#include <limits.h>
39
40//#undef NDEBUG
41//#include <assert.h>
42
43static int encode_picture(MpegEncContext *s, int picture_number);
44static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
45static int sse_mb(MpegEncContext *s);
46
47/* enable all paranoid tests for rounding, overflows, etc... */
48//#define PARANOID
49
50//#define DEBUG
51
52static const uint16_t aanscales[64] = {
53 /* precomputed values scaled up by 14 bits */
54 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
55 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
56 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
57 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
58 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
59 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
60 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
61 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
62};
63
64static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
65static uint8_t default_fcode_tab[MAX_MV*2+1];
66
67enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
68
69static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
70 const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
71{
72 int qscale;
73 int shift=0;
74
75 for(qscale=qmin; qscale<=qmax; qscale++){
76 int i;
77 if (dsp->fdct == ff_jpeg_fdct_islow
78#ifdef FAAN_POSTSCALE
79 || dsp->fdct == ff_faandct
80#endif
81 ) {
82 for(i=0;i<64;i++) {
83 const int j= dsp->idct_permutation[i];
84 /* 16 <= qscale * quant_matrix[i] <= 7905 */
85 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
86 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
87 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
88
89 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
90 (qscale * quant_matrix[j]));
91 }
92 } else if (dsp->fdct == fdct_ifast
93#ifndef FAAN_POSTSCALE
94 || dsp->fdct == ff_faandct
95#endif
96 ) {
97 for(i=0;i<64;i++) {
98 const int j= dsp->idct_permutation[i];
99 /* 16 <= qscale * quant_matrix[i] <= 7905 */
100 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
101 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
102 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
103
104 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
105 (aanscales[i] * qscale * quant_matrix[j]));
106 }
107 } else {
108 for(i=0;i<64;i++) {
109 const int j= dsp->idct_permutation[i];
110 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
111 So 16 <= qscale * quant_matrix[i] <= 7905
112 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
113 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
114 */
115 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
116// qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
117 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
118
119 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
120 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
121 }
122 }
123
124 for(i=intra; i<64; i++){
125 int64_t max= 8191;
126 if (dsp->fdct == fdct_ifast
127#ifndef FAAN_POSTSCALE
128 || dsp->fdct == ff_faandct
129#endif
130 ) {
131 max= (8191LL*aanscales[i]) >> 14;
132 }
133 while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
134 shift++;
135 }
136 }
137 }
138 if(shift){
139 av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger than %d, overflows possible\n", QMAT_SHIFT - shift);
140 }
141}
142
143static inline void update_qscale(MpegEncContext *s){
144 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
145 s->qscale= av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
146
147 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
148}
149
150void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix){
151 int i;
152
153 if(matrix){
154 put_bits(pb, 1, 1);
155 for(i=0;i<64;i++) {
156 put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
157 }
158 }else
159 put_bits(pb, 1, 0);
160}
161
162static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
163 int i;
164
165 dst->pict_type = src->pict_type;
166 dst->quality = src->quality;
167 dst->coded_picture_number = src->coded_picture_number;
168 dst->display_picture_number = src->display_picture_number;
169// dst->reference = src->reference;
170 dst->pts = src->pts;
171 dst->interlaced_frame = src->interlaced_frame;
172 dst->top_field_first = src->top_field_first;
173
174 if(s->avctx->me_threshold){
175 if(!src->motion_val[0])
176 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
177 if(!src->mb_type)
178 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
179 if(!src->ref_index[0])
180 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
181 if(src->motion_subsample_log2 != dst->motion_subsample_log2)
182 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
183 src->motion_subsample_log2, dst->motion_subsample_log2);
184
185 memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
186
187 for(i=0; i<2; i++){
188 int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
189 int height= ((16*s->mb_height)>>src->motion_subsample_log2);
190
191 if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
192 memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
193 }
194 if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
195 memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t));
196 }
197 }
198 }
199}
200
201static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
202#define COPY(a) dst->a= src->a
203 COPY(pict_type);
204 COPY(current_picture);
205 COPY(f_code);
206 COPY(b_code);
207 COPY(qscale);
208 COPY(lambda);
209 COPY(lambda2);
210 COPY(picture_in_gop_number);
211 COPY(gop_picture_number);
212 COPY(frame_pred_frame_dct); //FIXME don't set in encode_header
213 COPY(progressive_frame); //FIXME don't set in encode_header
214 COPY(partitioned_frame); //FIXME don't set in encode_header
215#undef COPY
216}
217
218/**
219 * sets the given MpegEncContext to defaults for encoding.
220 * the changed fields will not depend upon the prior state of the MpegEncContext.
221 */
222static void MPV_encode_defaults(MpegEncContext *s){
223 int i;
224 MPV_common_defaults(s);
225
226 for(i=-16; i<16; i++){
227 default_fcode_tab[i + MAX_MV]= 1;
228 }
229 s->me.mv_penalty= default_mv_penalty;
230 s->fcode_tab= default_fcode_tab;
231}
232
233/* init video encoder */
234int MPV_encode_init(AVCodecContext *avctx)
235{
236 MpegEncContext *s = avctx->priv_data;
237 int i;
238 int chroma_h_shift, chroma_v_shift;
239
240 MPV_encode_defaults(s);
241
242 switch (avctx->codec_id) {
243 case CODEC_ID_MPEG2VIDEO:
244 if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P){
245 av_log(avctx, AV_LOG_ERROR, "only YUV420 and YUV422 are supported\n");
246 return -1;
247 }
248 break;
249 case CODEC_ID_LJPEG:
250 case CODEC_ID_MJPEG:
251 if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P &&
252 ((avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P) || avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL)){
253 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
254 return -1;
255 }
256 break;
257 default:
258 if(avctx->pix_fmt != PIX_FMT_YUV420P){
259 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
260 return -1;
261 }
262 }
263
264 switch (avctx->pix_fmt) {
265 case PIX_FMT_YUVJ422P:
266 case PIX_FMT_YUV422P:
267 s->chroma_format = CHROMA_422;
268 break;
269 case PIX_FMT_YUVJ420P:
270 case PIX_FMT_YUV420P:
271 default:
272 s->chroma_format = CHROMA_420;
273 break;
274 }
275
276 s->bit_rate = avctx->bit_rate;
277 s->width = avctx->width;
278 s->height = avctx->height;
279 if(avctx->gop_size > 600 && avctx->strict_std_compliance>FF_COMPLIANCE_EXPERIMENTAL){
280 av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
281 avctx->gop_size=600;
282 }
283 s->gop_size = avctx->gop_size;
284 s->avctx = avctx;
285 s->flags= avctx->flags;
286 s->flags2= avctx->flags2;
287 s->max_b_frames= avctx->max_b_frames;
288 s->codec_id= avctx->codec->id;
289 s->luma_elim_threshold = avctx->luma_elim_threshold;
290 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
291 s->strict_std_compliance= avctx->strict_std_compliance;
292 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
293 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
294 s->mpeg_quant= avctx->mpeg_quant;
295 s->rtp_mode= !!avctx->rtp_payload_size;
296 s->intra_dc_precision= avctx->intra_dc_precision;
297 s->user_specified_pts = AV_NOPTS_VALUE;
298
299 if (s->gop_size <= 1) {
300 s->intra_only = 1;
301 s->gop_size = 12;
302 } else {
303 s->intra_only = 0;
304 }
305
306 s->me_method = avctx->me_method;
307
308 /* Fixed QSCALE */
309 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
310
311 s->adaptive_quant= ( s->avctx->lumi_masking
312 || s->avctx->dark_masking
313 || s->avctx->temporal_cplx_masking
314 || s->avctx->spatial_cplx_masking
315 || s->avctx->p_masking
316 || s->avctx->border_masking
317 || (s->flags&CODEC_FLAG_QP_RD))
318 && !s->fixed_qscale;
319
320 s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
321 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
322 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
323 s->intra_vlc_format= !!(s->flags2 & CODEC_FLAG2_INTRA_VLC);
324 s->q_scale_type= !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT);
325
326 if(avctx->rc_max_rate && !avctx->rc_buffer_size){
327 av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
328 return -1;
329 }
330
331 if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
332 av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
333 }
334
335 if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
336 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
337 return -1;
338 }
339
340 if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
341 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
342 return -1;
343 }
344
345 if(avctx->rc_buffer_size && avctx->bit_rate*av_q2d(avctx->time_base) > avctx->rc_buffer_size){
346 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
347 return -1;
348 }
349
350 if(avctx->bit_rate*av_q2d(avctx->time_base) > avctx->bit_rate_tolerance){
351 av_log(avctx, AV_LOG_ERROR, "bitrate tolerance too small for bitrate\n");
352 return -1;
353 }
354
355 if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
356 && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
357 && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
358
359 av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n");
360 }
361
362 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
363 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
364 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
365 return -1;
366 }
367
368 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
369 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n");
370 return -1;
371 }
372
373 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
374 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
375 return -1;
376 }
377
378 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
379 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
380 return -1;
381 }
382
383 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
384 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
385 return -1;
386 }
387
388 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
389 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
390 return -1;
391 }
392
393 if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
394 && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
395 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
396 return -1;
397 }
398
399 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
400 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n");
401 return -1;
402 }
403
404 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
405 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
406 return -1;
407 }
408
409 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
410 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
411 return -1;
412 }
413
414 if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
415 av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet, set threshold to 1000000000\n");
416 return -1;
417 }
418
419 if((s->flags2 & CODEC_FLAG2_INTRA_VLC) && s->codec_id != CODEC_ID_MPEG2VIDEO){
420 av_log(avctx, AV_LOG_ERROR, "intra vlc table not supported by codec\n");
421 return -1;
422 }
423
424 if(s->flags & CODEC_FLAG_LOW_DELAY){
425 if (s->codec_id != CODEC_ID_MPEG2VIDEO && s->codec_id != CODEC_ID_MPEG1VIDEO){
426 av_log(avctx, AV_LOG_ERROR, "low delay forcing is only available for mpeg1/2\n");
427 return -1;
428 }
429 if (s->max_b_frames != 0){
430 av_log(avctx, AV_LOG_ERROR, "b frames cannot be used with low delay\n");
431 return -1;
432 }
433 }
434
435 if(s->q_scale_type == 1){
436 if(s->codec_id != CODEC_ID_MPEG2VIDEO){
437 av_log(avctx, AV_LOG_ERROR, "non linear quant is only available for mpeg2\n");
438 return -1;
439 }
440 if(avctx->qmax > 12){
441 av_log(avctx, AV_LOG_ERROR, "non linear quant only supports qmax <= 12 currently\n");
442 return -1;
443 }
444 }
445
446 if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
447 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
448 && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
449 av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
450 return -1;
451 }
452
453 if(s->avctx->thread_count > 1)
454 s->rtp_mode= 1;
455
456 if(!avctx->time_base.den || !avctx->time_base.num){
457 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
458 return -1;
459 }
460
461 i= (INT_MAX/2+128)>>8;
462 if(avctx->me_threshold >= i){
463 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1);
464 return -1;
465 }
466 if(avctx->mb_threshold >= i){
467 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1);
468 return -1;
469 }
470
471 if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){
472 av_log(avctx, AV_LOG_INFO, "notice: b_frame_strategy only affects the first pass\n");
473 avctx->b_frame_strategy = 0;
474 }
475
476 i= ff_gcd(avctx->time_base.den, avctx->time_base.num);
477 if(i > 1){
478 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
479 avctx->time_base.den /= i;
480 avctx->time_base.num /= i;
481// return -1;
482 }
483
484 if(s->codec_id==CODEC_ID_MJPEG){
485 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
486 s->inter_quant_bias= 0;
487 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
488 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
489 s->inter_quant_bias= 0;
490 }else{
491 s->intra_quant_bias=0;
492 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
493 }
494
495 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
496 s->intra_quant_bias= avctx->intra_quant_bias;
497 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
498 s->inter_quant_bias= avctx->inter_quant_bias;
499
500 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
501
502 if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){
503 av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n");
504 return -1;
505 }
506 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
507
508 switch(avctx->codec->id) {
509 case CODEC_ID_MPEG1VIDEO:
510 s->out_format = FMT_MPEG1;
511 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
512 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
513 break;
514 case CODEC_ID_MPEG2VIDEO:
515 s->out_format = FMT_MPEG1;
516 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
517 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
518 s->rtp_mode= 1;
519 break;
520 case CODEC_ID_LJPEG:
521 case CODEC_ID_MJPEG:
522 s->out_format = FMT_MJPEG;
523 s->intra_only = 1; /* force intra only for jpeg */
524 s->mjpeg_vsample[0] = 2;
525 s->mjpeg_vsample[1] = 2>>chroma_v_shift;
526 s->mjpeg_vsample[2] = 2>>chroma_v_shift;
527 s->mjpeg_hsample[0] = 2;
528 s->mjpeg_hsample[1] = 2>>chroma_h_shift;
529 s->mjpeg_hsample[2] = 2>>chroma_h_shift;
530 if (!(ENABLE_MJPEG_ENCODER || ENABLE_LJPEG_ENCODER)
531 || ff_mjpeg_encode_init(s) < 0)
532 return -1;
533 avctx->delay=0;
534 s->low_delay=1;
535 break;
536 case CODEC_ID_H261:
537 if (!ENABLE_H261_ENCODER) return -1;
538 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
539 av_log(avctx, AV_LOG_ERROR, "The specified picture size of %dx%d is not valid for the H.261 codec.\nValid sizes are 176x144, 352x288\n", s->width, s->height);
540 return -1;
541 }
542 s->out_format = FMT_H261;
543 avctx->delay=0;
544 s->low_delay=1;
545 break;
546 case CODEC_ID_H263:
eb75a698 547 if (!ENABLE_H263_ENCODER) return -1;
3ada94ba
BF
548 if (h263_get_picture_format(s->width, s->height) == 7) {
549 av_log(avctx, AV_LOG_INFO, "The specified picture size of %dx%d is not valid for the H.263 codec.\nValid sizes are 128x96, 176x144, 352x288, 704x576, and 1408x1152. Try H.263+.\n", s->width, s->height);
550 return -1;
551 }
552 s->out_format = FMT_H263;
553 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
554 avctx->delay=0;
555 s->low_delay=1;
556 break;
557 case CODEC_ID_H263P:
558 s->out_format = FMT_H263;
559 s->h263_plus = 1;
560 /* Fx */
561 s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
562 s->h263_aic= (avctx->flags & CODEC_FLAG_AC_PRED) ? 1:0;
563 s->modified_quant= s->h263_aic;
564 s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
565 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
566 s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
567 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
568 s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
569
570 /* /Fx */
571 /* These are just to be sure */
572 avctx->delay=0;
573 s->low_delay=1;
574 break;
575 case CODEC_ID_FLV1:
576 s->out_format = FMT_H263;
577 s->h263_flv = 2; /* format = 1; 11-bit codes */
578 s->unrestricted_mv = 1;
579 s->rtp_mode=0; /* don't allow GOB */
580 avctx->delay=0;
581 s->low_delay=1;
582 break;
583 case CODEC_ID_RV10:
584 s->out_format = FMT_H263;
585 avctx->delay=0;
586 s->low_delay=1;
587 break;
588 case CODEC_ID_RV20:
589 s->out_format = FMT_H263;
590 avctx->delay=0;
591 s->low_delay=1;
592 s->modified_quant=1;
593 s->h263_aic=1;
594 s->h263_plus=1;
595 s->loop_filter=1;
596 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
597 break;
598 case CODEC_ID_MPEG4:
599 s->out_format = FMT_H263;
600 s->h263_pred = 1;
601 s->unrestricted_mv = 1;
602 s->low_delay= s->max_b_frames ? 0 : 1;
603 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
604 break;
605 case CODEC_ID_MSMPEG4V1:
606 s->out_format = FMT_H263;
607 s->h263_msmpeg4 = 1;
608 s->h263_pred = 1;
609 s->unrestricted_mv = 1;
610 s->msmpeg4_version= 1;
611 avctx->delay=0;
612 s->low_delay=1;
613 break;
614 case CODEC_ID_MSMPEG4V2:
615 s->out_format = FMT_H263;
616 s->h263_msmpeg4 = 1;
617 s->h263_pred = 1;
618 s->unrestricted_mv = 1;
619 s->msmpeg4_version= 2;
620 avctx->delay=0;
621 s->low_delay=1;
622 break;
623 case CODEC_ID_MSMPEG4V3:
624 s->out_format = FMT_H263;
625 s->h263_msmpeg4 = 1;
626 s->h263_pred = 1;
627 s->unrestricted_mv = 1;
628 s->msmpeg4_version= 3;
629 s->flipflop_rounding=1;
630 avctx->delay=0;
631 s->low_delay=1;
632 break;
633 case CODEC_ID_WMV1:
634 s->out_format = FMT_H263;
635 s->h263_msmpeg4 = 1;
636 s->h263_pred = 1;
637 s->unrestricted_mv = 1;
638 s->msmpeg4_version= 4;
639 s->flipflop_rounding=1;
640 avctx->delay=0;
641 s->low_delay=1;
642 break;
643 case CODEC_ID_WMV2:
644 s->out_format = FMT_H263;
645 s->h263_msmpeg4 = 1;
646 s->h263_pred = 1;
647 s->unrestricted_mv = 1;
648 s->msmpeg4_version= 5;
649 s->flipflop_rounding=1;
650 avctx->delay=0;
651 s->low_delay=1;
652 break;
653 default:
654 return -1;
655 }
656
657 avctx->has_b_frames= !s->low_delay;
658
659 s->encoding = 1;
660
661 /* init */
662 if (MPV_common_init(s) < 0)
663 return -1;
664
53727262
AJ
665 if(!s->dct_quantize)
666 s->dct_quantize = dct_quantize_c;
667 if(!s->denoise_dct)
668 s->denoise_dct = denoise_dct_c;
669 s->fast_dct_quantize = s->dct_quantize;
670 if(s->flags & CODEC_FLAG_TRELLIS_QUANT)
671 s->dct_quantize = dct_quantize_trellis_c;
672
eb75a698 673 if((ENABLE_H263P_ENCODER || ENABLE_RV20_ENCODER) && s->modified_quant)
3ada94ba
BF
674 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
675 s->progressive_frame=
676 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN));
677 s->quant_precision=5;
678
679 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
680 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
681
682 if (ENABLE_H261_ENCODER && s->out_format == FMT_H261)
683 ff_h261_encode_init(s);
eb75a698 684 if (ENABLE_ANY_H263_ENCODER && s->out_format == FMT_H263)
3ada94ba
BF
685 h263_encode_init(s);
686 if (ENABLE_MSMPEG4_ENCODER && s->msmpeg4_version)
687 ff_msmpeg4_encode_init(s);
a6bc5731
AJ
688 if ((ENABLE_MPEG1VIDEO_ENCODER || ENABLE_MPEG2VIDEO_ENCODER)
689 && s->out_format == FMT_MPEG1)
3ada94ba
BF
690 ff_mpeg1_encode_init(s);
691
692 /* init q matrix */
693 for(i=0;i<64;i++) {
694 int j= s->dsp.idct_permutation[i];
eb75a698 695 if(ENABLE_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
3ada94ba
BF
696 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
697 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
698 }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
699 s->intra_matrix[j] =
700 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
701 }else
702 { /* mpeg1/2 */
703 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
704 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
705 }
706 if(s->avctx->intra_matrix)
707 s->intra_matrix[j] = s->avctx->intra_matrix[i];
708 if(s->avctx->inter_matrix)
709 s->inter_matrix[j] = s->avctx->inter_matrix[i];
710 }
711
712 /* precompute matrix */
713 /* for mjpeg, we do include qscale in the matrix */
714 if (s->out_format != FMT_MJPEG) {
715 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
716 s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
717 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
718 s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
719 }
720
721 if(ff_rate_control_init(s) < 0)
722 return -1;
723
724 return 0;
725}
726
727int MPV_encode_end(AVCodecContext *avctx)
728{
729 MpegEncContext *s = avctx->priv_data;
730
731 ff_rate_control_uninit(s);
732
733 MPV_common_end(s);
734 if ((ENABLE_MJPEG_ENCODER || ENABLE_LJPEG_ENCODER) && s->out_format == FMT_MJPEG)
735 ff_mjpeg_encode_close(s);
736
737 av_freep(&avctx->extradata);
738
739 return 0;
740}
741
742static int get_sae(uint8_t *src, int ref, int stride){
743 int x,y;
744 int acc=0;
745
746 for(y=0; y<16; y++){
747 for(x=0; x<16; x++){
748 acc+= FFABS(src[x+y*stride] - ref);
749 }
750 }
751
752 return acc;
753}
754
755static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
756 int x, y, w, h;
757 int acc=0;
758
759 w= s->width &~15;
760 h= s->height&~15;
761
762 for(y=0; y<h; y+=16){
763 for(x=0; x<w; x+=16){
764 int offset= x + y*stride;
765 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
766 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
767 int sae = get_sae(src + offset, mean, stride);
768
769 acc+= sae + 500 < sad;
770 }
771 }
772 return acc;
773}
774
775
776static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
777 AVFrame *pic=NULL;
778 int64_t pts;
779 int i;
780 const int encoding_delay= s->max_b_frames;
781 int direct=1;
782
783 if(pic_arg){
784 pts= pic_arg->pts;
785 pic_arg->display_picture_number= s->input_picture_number++;
786
787 if(pts != AV_NOPTS_VALUE){
788 if(s->user_specified_pts != AV_NOPTS_VALUE){
789 int64_t time= pts;
790 int64_t last= s->user_specified_pts;
791
792 if(time <= last){
793 av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts);
794 return -1;
795 }
796 }
797 s->user_specified_pts= pts;
798 }else{
799 if(s->user_specified_pts != AV_NOPTS_VALUE){
800 s->user_specified_pts=
801 pts= s->user_specified_pts + 1;
802 av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts);
803 }else{
804 pts= pic_arg->display_picture_number;
805 }
806 }
807 }
808
809 if(pic_arg){
810 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
811 if(pic_arg->linesize[0] != s->linesize) direct=0;
812 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
813 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
814
815// av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
816
817 if(direct){
818 i= ff_find_unused_picture(s, 1);
819
820 pic= (AVFrame*)&s->picture[i];
821 pic->reference= 3;
822
823 for(i=0; i<4; i++){
824 pic->data[i]= pic_arg->data[i];
825 pic->linesize[i]= pic_arg->linesize[i];
826 }
827 alloc_picture(s, (Picture*)pic, 1);
828 }else{
829 i= ff_find_unused_picture(s, 0);
830
831 pic= (AVFrame*)&s->picture[i];
832 pic->reference= 3;
833
834 alloc_picture(s, (Picture*)pic, 0);
835
836 if( pic->data[0] + INPLACE_OFFSET == pic_arg->data[0]
837 && pic->data[1] + INPLACE_OFFSET == pic_arg->data[1]
838 && pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]){
839 // empty
840 }else{
841 int h_chroma_shift, v_chroma_shift;
842 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
843
844 for(i=0; i<3; i++){
845 int src_stride= pic_arg->linesize[i];
846 int dst_stride= i ? s->uvlinesize : s->linesize;
847 int h_shift= i ? h_chroma_shift : 0;
848 int v_shift= i ? v_chroma_shift : 0;
849 int w= s->width >>h_shift;
850 int h= s->height>>v_shift;
851 uint8_t *src= pic_arg->data[i];
852 uint8_t *dst= pic->data[i];
853
854 if(!s->avctx->rc_buffer_size)
855 dst +=INPLACE_OFFSET;
856
857 if(src_stride==dst_stride)
858 memcpy(dst, src, src_stride*h);
859 else{
860 while(h--){
861 memcpy(dst, src, w);
862 dst += dst_stride;
863 src += src_stride;
864 }
865 }
866 }
867 }
868 }
869 copy_picture_attributes(s, pic, pic_arg);
870 pic->pts= pts; //we set this here to avoid modifiying pic_arg
871 }
872
873 /* shift buffer entries */
874 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
875 s->input_picture[i-1]= s->input_picture[i];
876
877 s->input_picture[encoding_delay]= (Picture*)pic;
878
879 return 0;
880}
881
882static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){
883 int x, y, plane;
884 int score=0;
885 int64_t score64=0;
886
887 for(plane=0; plane<3; plane++){
888 const int stride= p->linesize[plane];
889 const int bw= plane ? 1 : 2;
890 for(y=0; y<s->mb_height*bw; y++){
891 for(x=0; x<s->mb_width*bw; x++){
892 int off= p->type == FF_BUFFER_TYPE_SHARED ? 0: 16;
893 int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride)+off, ref->data[plane] + 8*(x + y*stride), stride, 8);
894
895 switch(s->avctx->frame_skip_exp){
896 case 0: score= FFMAX(score, v); break;
897 case 1: score+= FFABS(v);break;
898 case 2: score+= v*v;break;
899 case 3: score64+= FFABS(v*v*(int64_t)v);break;
900 case 4: score64+= v*v*(int64_t)(v*v);break;
901 }
902 }
903 }
904 }
905
906 if(score) score64= score;
907
908 if(score64 < s->avctx->frame_skip_threshold)
909 return 1;
910 if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
911 return 1;
912 return 0;
913}
914
915static int estimate_best_b_count(MpegEncContext *s){
916 AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id);
917 AVCodecContext *c= avcodec_alloc_context();
918 AVFrame input[FF_MAX_B_FRAMES+2];
919 const int scale= s->avctx->brd_scale;
920 int i, j, out_size, p_lambda, b_lambda, lambda2;
921 int outbuf_size= s->width * s->height; //FIXME
922 uint8_t *outbuf= av_malloc(outbuf_size);
923 int64_t best_rd= INT64_MAX;
924 int best_b_count= -1;
925
926 assert(scale>=0 && scale <=3);
927
928// emms_c();
929 p_lambda= s->last_lambda_for[P_TYPE]; //s->next_picture_ptr->quality;
930 b_lambda= s->last_lambda_for[B_TYPE]; //p_lambda *FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
931 if(!b_lambda) b_lambda= p_lambda; //FIXME we should do this somewhere else
932 lambda2= (b_lambda*b_lambda + (1<<FF_LAMBDA_SHIFT)/2 ) >> FF_LAMBDA_SHIFT;
933
934 c->width = s->width >> scale;
935 c->height= s->height>> scale;
936 c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
937 c->flags|= s->avctx->flags & CODEC_FLAG_QPEL;
938 c->mb_decision= s->avctx->mb_decision;
939 c->me_cmp= s->avctx->me_cmp;
940 c->mb_cmp= s->avctx->mb_cmp;
941 c->me_sub_cmp= s->avctx->me_sub_cmp;
942 c->pix_fmt = PIX_FMT_YUV420P;
943 c->time_base= s->avctx->time_base;
944 c->max_b_frames= s->max_b_frames;
945
946 if (avcodec_open(c, codec) < 0)
947 return -1;
948
949 for(i=0; i<s->max_b_frames+2; i++){
950 int ysize= c->width*c->height;
951 int csize= (c->width/2)*(c->height/2);
952 Picture pre_input, *pre_input_ptr= i ? s->input_picture[i-1] : s->next_picture_ptr;
953
954 avcodec_get_frame_defaults(&input[i]);
955 input[i].data[0]= av_malloc(ysize + 2*csize);
956 input[i].data[1]= input[i].data[0] + ysize;
957 input[i].data[2]= input[i].data[1] + csize;
958 input[i].linesize[0]= c->width;
959 input[i].linesize[1]=
960 input[i].linesize[2]= c->width/2;
961
962 if(pre_input_ptr && (!i || s->input_picture[i-1])) {
963 pre_input= *pre_input_ptr;
964
965 if(pre_input.type != FF_BUFFER_TYPE_SHARED && i) {
966 pre_input.data[0]+=INPLACE_OFFSET;
967 pre_input.data[1]+=INPLACE_OFFSET;
968 pre_input.data[2]+=INPLACE_OFFSET;
969 }
970
971 s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0], pre_input.data[0], pre_input.linesize[0], c->width, c->height);
972 s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1], pre_input.data[1], pre_input.linesize[1], c->width>>1, c->height>>1);
973 s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2], pre_input.data[2], pre_input.linesize[2], c->width>>1, c->height>>1);
974 }
975 }
976
977 for(j=0; j<s->max_b_frames+1; j++){
978 int64_t rd=0;
979
980 if(!s->input_picture[j])
981 break;
982
983 c->error[0]= c->error[1]= c->error[2]= 0;
984
985 input[0].pict_type= I_TYPE;
986 input[0].quality= 1 * FF_QP2LAMBDA;
987 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]);
988// rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
989
990 for(i=0; i<s->max_b_frames+1; i++){
991 int is_p= i % (j+1) == j || i==s->max_b_frames;
992
993 input[i+1].pict_type= is_p ? P_TYPE : B_TYPE;
994 input[i+1].quality= is_p ? p_lambda : b_lambda;
995 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]);
996 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
997 }
998
999 /* get the delayed frames */
1000 while(out_size){
1001 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
1002 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1003 }
1004
1005 rd += c->error[0] + c->error[1] + c->error[2];
1006
1007 if(rd < best_rd){
1008 best_rd= rd;
1009 best_b_count= j;
1010 }
1011 }
1012
1013 av_freep(&outbuf);
1014 avcodec_close(c);
1015 av_freep(&c);
1016
1017 for(i=0; i<s->max_b_frames+2; i++){
1018 av_freep(&input[i].data[0]);
1019 }
1020
1021 return best_b_count;
1022}
1023
1024static void select_input_picture(MpegEncContext *s){
1025 int i;
1026
1027 for(i=1; i<MAX_PICTURE_COUNT; i++)
1028 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1029 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1030
1031 /* set next picture type & ordering */
1032 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1033 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1034 s->reordered_input_picture[0]= s->input_picture[0];
1035 s->reordered_input_picture[0]->pict_type= I_TYPE;
1036 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1037 }else{
1038 int b_frames;
1039
1040 if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
1041 if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){
1042 //FIXME check that te gop check above is +-1 correct
1043//av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n", s->input_picture[0]->data[0], s->input_picture[0]->pts);
1044
1045 if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1046 for(i=0; i<4; i++)
1047 s->input_picture[0]->data[i]= NULL;
1048 s->input_picture[0]->type= 0;
1049 }else{
1050 assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER
1051 || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1052
1053 s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
1054 }
1055
1056 emms_c();
1057 ff_vbv_update(s, 0);
1058
1059 goto no_output_pic;
1060 }
1061 }
1062
1063 if(s->flags&CODEC_FLAG_PASS2){
1064 for(i=0; i<s->max_b_frames+1; i++){
1065 int pict_num= s->input_picture[0]->display_picture_number + i;
1066
1067 if(pict_num >= s->rc_context.num_entries)
1068 break;
1069 if(!s->input_picture[i]){
1070 s->rc_context.entry[pict_num-1].new_pict_type = P_TYPE;
1071 break;
1072 }
1073
1074 s->input_picture[i]->pict_type=
1075 s->rc_context.entry[pict_num].new_pict_type;
1076 }
1077 }
1078
1079 if(s->avctx->b_frame_strategy==0){
1080 b_frames= s->max_b_frames;
1081 while(b_frames && !s->input_picture[b_frames]) b_frames--;
1082 }else if(s->avctx->b_frame_strategy==1){
1083 for(i=1; i<s->max_b_frames+1; i++){
1084 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1085 s->input_picture[i]->b_frame_score=
1086 get_intra_count(s, s->input_picture[i ]->data[0],
1087 s->input_picture[i-1]->data[0], s->linesize) + 1;
1088 }
1089 }
1090 for(i=0; i<s->max_b_frames+1; i++){
1091 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/s->avctx->b_sensitivity) break;
1092 }
1093
1094 b_frames= FFMAX(0, i-1);
1095
1096 /* reset scores */
1097 for(i=0; i<b_frames+1; i++){
1098 s->input_picture[i]->b_frame_score=0;
1099 }
1100 }else if(s->avctx->b_frame_strategy==2){
1101 b_frames= estimate_best_b_count(s);
1102 }else{
1103 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1104 b_frames=0;
1105 }
1106
1107 emms_c();
1108//static int b_count=0;
1109//b_count+= b_frames;
1110//av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1111
1112 for(i= b_frames - 1; i>=0; i--){
1113 int type= s->input_picture[i]->pict_type;
1114 if(type && type != B_TYPE)
1115 b_frames= i;
1116 }
1117 if(s->input_picture[b_frames]->pict_type == B_TYPE && b_frames == s->max_b_frames){
1118 av_log(s->avctx, AV_LOG_ERROR, "warning, too many b frames in a row\n");
1119 }
1120
1121 if(s->picture_in_gop_number + b_frames >= s->gop_size){
1122 if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){
1123 b_frames= s->gop_size - s->picture_in_gop_number - 1;
1124 }else{
1125 if(s->flags & CODEC_FLAG_CLOSED_GOP)
1126 b_frames=0;
1127 s->input_picture[b_frames]->pict_type= I_TYPE;
1128 }
1129 }
1130
1131 if( (s->flags & CODEC_FLAG_CLOSED_GOP)
1132 && b_frames
1133 && s->input_picture[b_frames]->pict_type== I_TYPE)
1134 b_frames--;
1135
1136 s->reordered_input_picture[0]= s->input_picture[b_frames];
1137 if(s->reordered_input_picture[0]->pict_type != I_TYPE)
1138 s->reordered_input_picture[0]->pict_type= P_TYPE;
1139 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1140 for(i=0; i<b_frames; i++){
1141 s->reordered_input_picture[i+1]= s->input_picture[i];
1142 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1143 s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
1144 }
1145 }
1146 }
1147no_output_pic:
1148 if(s->reordered_input_picture[0]){
1149 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1150
1151 copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1152
1153 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED || s->avctx->rc_buffer_size){
1154 // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable
1155
1156 int i= ff_find_unused_picture(s, 0);
1157 Picture *pic= &s->picture[i];
1158
1159 pic->reference = s->reordered_input_picture[0]->reference;
1160 alloc_picture(s, pic, 0);
1161
1162 /* mark us unused / free shared pic */
1163 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_INTERNAL)
1164 s->avctx->release_buffer(s->avctx, (AVFrame*)s->reordered_input_picture[0]);
1165 for(i=0; i<4; i++)
1166 s->reordered_input_picture[0]->data[i]= NULL;
1167 s->reordered_input_picture[0]->type= 0;
1168
1169 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
1170
1171 s->current_picture_ptr= pic;
1172 }else{
1173 // input is not a shared pix -> reuse buffer for current_pix
1174
1175 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1176 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1177
1178 s->current_picture_ptr= s->reordered_input_picture[0];
1179 for(i=0; i<4; i++){
1180 s->new_picture.data[i]+= INPLACE_OFFSET;
1181 }
1182 }
1183 copy_picture(&s->current_picture, s->current_picture_ptr);
1184
1185 s->picture_number= s->new_picture.display_picture_number;
1186//printf("dpn:%d\n", s->picture_number);
1187 }else{
1188 memset(&s->new_picture, 0, sizeof(Picture));
1189 }
1190}
1191
1192int MPV_encode_picture(AVCodecContext *avctx,
1193 unsigned char *buf, int buf_size, void *data)
1194{
1195 MpegEncContext *s = avctx->priv_data;
1196 AVFrame *pic_arg = data;
1197 int i, stuffing_count;
1198
1199 for(i=0; i<avctx->thread_count; i++){
1200 int start_y= s->thread_context[i]->start_mb_y;
1201 int end_y= s->thread_context[i]-> end_mb_y;
1202 int h= s->mb_height;
1203 uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h);
1204 uint8_t *end = buf + (size_t)(((int64_t) buf_size)* end_y/h);
1205
1206 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1207 }
1208
1209 s->picture_in_gop_number++;
1210
1211 if(load_input_picture(s, pic_arg) < 0)
1212 return -1;
1213
1214 select_input_picture(s);
1215
1216 /* output? */
1217 if(s->new_picture.data[0]){
1218 s->pict_type= s->new_picture.pict_type;
1219//emms_c();
1220//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1221 MPV_frame_start(s, avctx);
1222vbv_retry:
1223 if (encode_picture(s, s->picture_number) < 0)
1224 return -1;
1225
1226 avctx->real_pict_num = s->picture_number;
1227 avctx->header_bits = s->header_bits;
1228 avctx->mv_bits = s->mv_bits;
1229 avctx->misc_bits = s->misc_bits;
1230 avctx->i_tex_bits = s->i_tex_bits;
1231 avctx->p_tex_bits = s->p_tex_bits;
1232 avctx->i_count = s->i_count;
1233 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1234 avctx->skip_count = s->skip_count;
1235
1236 MPV_frame_end(s);
1237
1238 if (ENABLE_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1239 ff_mjpeg_encode_picture_trailer(s);
1240
1241 if(avctx->rc_buffer_size){
1242 RateControlContext *rcc= &s->rc_context;
1243 int max_size= rcc->buffer_index/3;
1244
1245 if(put_bits_count(&s->pb) > max_size && s->lambda < s->avctx->lmax){
1246 s->next_lambda= FFMAX(s->lambda+1, s->lambda*(s->qscale+1) / s->qscale);
1247 if(s->adaptive_quant){
1248 int i;
1249 for(i=0; i<s->mb_height*s->mb_stride; i++)
1250 s->lambda_table[i]= FFMAX(s->lambda_table[i]+1, s->lambda_table[i]*(s->qscale+1) / s->qscale);
1251 }
1252 s->mb_skipped = 0; //done in MPV_frame_start()
1253 if(s->pict_type==P_TYPE){ //done in encode_picture() so we must undo it
1254 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
1255 s->no_rounding ^= 1;
1256 }
1257 if(s->pict_type!=B_TYPE){
1258 s->time_base= s->last_time_base;
1259 s->last_non_b_time= s->time - s->pp_time;
1260 }
1261// av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda);
1262 for(i=0; i<avctx->thread_count; i++){
1263 PutBitContext *pb= &s->thread_context[i]->pb;
1264 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1265 }
1266 goto vbv_retry;
1267 }
1268
1269 assert(s->avctx->rc_max_rate);
1270 }
1271
1272 if(s->flags&CODEC_FLAG_PASS1)
1273 ff_write_pass1_stats(s);
1274
1275 for(i=0; i<4; i++){
1276 s->current_picture_ptr->error[i]= s->current_picture.error[i];
1277 avctx->error[i] += s->current_picture_ptr->error[i];
1278 }
1279
1280 if(s->flags&CODEC_FLAG_PASS1)
1281 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb));
1282 flush_put_bits(&s->pb);
1283 s->frame_bits = put_bits_count(&s->pb);
1284
1285 stuffing_count= ff_vbv_update(s, s->frame_bits);
1286 if(stuffing_count){
1287 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){
1288 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1289 return -1;
1290 }
1291
1292 switch(s->codec_id){
1293 case CODEC_ID_MPEG1VIDEO:
1294 case CODEC_ID_MPEG2VIDEO:
1295 while(stuffing_count--){
1296 put_bits(&s->pb, 8, 0);
1297 }
1298 break;
1299 case CODEC_ID_MPEG4:
1300 put_bits(&s->pb, 16, 0);
1301 put_bits(&s->pb, 16, 0x1C3);
1302 stuffing_count -= 4;
1303 while(stuffing_count--){
1304 put_bits(&s->pb, 8, 0xFF);
1305 }
1306 break;
1307 default:
1308 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1309 }
1310 flush_put_bits(&s->pb);
1311 s->frame_bits = put_bits_count(&s->pb);
1312 }
1313
1314 /* update mpeg1/2 vbv_delay for CBR */
1315 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
1316 && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
1317 int vbv_delay;
1318
1319 assert(s->repeat_first_field==0);
1320
1321 vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
1322 assert(vbv_delay < 0xFFFF);
1323
1324 s->vbv_delay_ptr[0] &= 0xF8;
1325 s->vbv_delay_ptr[0] |= vbv_delay>>13;
1326 s->vbv_delay_ptr[1] = vbv_delay>>5;
1327 s->vbv_delay_ptr[2] &= 0x07;
1328 s->vbv_delay_ptr[2] |= vbv_delay<<3;
1329 }
1330 s->total_bits += s->frame_bits;
1331 avctx->frame_bits = s->frame_bits;
1332 }else{
1333 assert((pbBufPtr(&s->pb) == s->pb.buf));
1334 s->frame_bits=0;
1335 }
1336 assert((s->frame_bits&7)==0);
1337
1338 return s->frame_bits/8;
1339}
1340
1341static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
1342{
1343 static const char tab[64]=
1344 {3,2,2,1,1,1,1,1,
1345 1,1,1,1,1,1,1,1,
1346 1,1,1,1,1,1,1,1,
1347 0,0,0,0,0,0,0,0,
1348 0,0,0,0,0,0,0,0,
1349 0,0,0,0,0,0,0,0,
1350 0,0,0,0,0,0,0,0,
1351 0,0,0,0,0,0,0,0};
1352 int score=0;
1353 int run=0;
1354 int i;
1355 DCTELEM *block= s->block[n];
1356 const int last_index= s->block_last_index[n];
1357 int skip_dc;
1358
1359 if(threshold<0){
1360 skip_dc=0;
1361 threshold= -threshold;
1362 }else
1363 skip_dc=1;
1364
1365 /* are all which we could set to zero are allready zero? */
1366 if(last_index<=skip_dc - 1) return;
1367
1368 for(i=0; i<=last_index; i++){
1369 const int j = s->intra_scantable.permutated[i];
1370 const int level = FFABS(block[j]);
1371 if(level==1){
1372 if(skip_dc && i==0) continue;
1373 score+= tab[run];
1374 run=0;
1375 }else if(level>1){
1376 return;
1377 }else{
1378 run++;
1379 }
1380 }
1381 if(score >= threshold) return;
1382 for(i=skip_dc; i<=last_index; i++){
1383 const int j = s->intra_scantable.permutated[i];
1384 block[j]=0;
1385 }
1386 if(block[0]) s->block_last_index[n]= 0;
1387 else s->block_last_index[n]= -1;
1388}
1389
1390static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1391{
1392 int i;
1393 const int maxlevel= s->max_qcoeff;
1394 const int minlevel= s->min_qcoeff;
1395 int overflow=0;
1396
1397 if(s->mb_intra){
1398 i=1; //skip clipping of intra dc
1399 }else
1400 i=0;
1401
1402 for(;i<=last_index; i++){
1403 const int j= s->intra_scantable.permutated[i];
1404 int level = block[j];
1405
1406 if (level>maxlevel){
1407 level=maxlevel;
1408 overflow++;
1409 }else if(level<minlevel){
1410 level=minlevel;
1411 overflow++;
1412 }
1413
1414 block[j]= level;
1415 }
1416
1417 if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1418 av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel);
1419}
1420
1421static void get_vissual_weight(int16_t *weight, uint8_t *ptr, int stride){
1422 int x, y;
1423//FIXME optimize
1424 for(y=0; y<8; y++){
1425 for(x=0; x<8; x++){
1426 int x2, y2;
1427 int sum=0;
1428 int sqr=0;
1429 int count=0;
1430
1431 for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
1432 for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
1433 int v= ptr[x2 + y2*stride];
1434 sum += v;
1435 sqr += v*v;
1436 count++;
1437 }
1438 }
1439 weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
1440 }
1441 }
1442}
1443
1444static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count)
1445{
1446 int16_t weight[8][64];
1447 DCTELEM orig[8][64];
1448 const int mb_x= s->mb_x;
1449 const int mb_y= s->mb_y;
1450 int i;
1451 int skip_dct[8];
1452 int dct_offset = s->linesize*8; //default for progressive frames
1453 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1454 int wrap_y, wrap_c;
1455
1456 for(i=0; i<mb_block_count; i++) skip_dct[i]=s->skipdct;
1457
1458 if(s->adaptive_quant){
1459 const int last_qp= s->qscale;
1460 const int mb_xy= mb_x + mb_y*s->mb_stride;
1461
1462 s->lambda= s->lambda_table[mb_xy];
1463 update_qscale(s);
1464
1465 if(!(s->flags&CODEC_FLAG_QP_RD)){
1466 s->qscale= s->current_picture_ptr->qscale_table[mb_xy];
1467 s->dquant= s->qscale - last_qp;
1468
1469 if(s->out_format==FMT_H263){
1470 s->dquant= av_clip(s->dquant, -2, 2);
1471
1472 if(s->codec_id==CODEC_ID_MPEG4){
1473 if(!s->mb_intra){
1474 if(s->pict_type == B_TYPE){
1475 if(s->dquant&1 || s->mv_dir&MV_DIRECT)
1476 s->dquant= 0;
1477 }
1478 if(s->mv_type==MV_TYPE_8X8)
1479 s->dquant=0;
1480 }
1481 }
1482 }
1483 }
1484 ff_set_qscale(s, last_qp + s->dquant);
1485 }else if(s->flags&CODEC_FLAG_QP_RD)
1486 ff_set_qscale(s, s->qscale + s->dquant);
1487
1488 wrap_y = s->linesize;
1489 wrap_c = s->uvlinesize;
1490 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
1491 ptr_cb = s->new_picture.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1492 ptr_cr = s->new_picture.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1493
1494 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
1495 uint8_t *ebuf= s->edge_emu_buffer + 32;
1496 ff_emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height);
1497 ptr_y= ebuf;
1498 ff_emulated_edge_mc(ebuf+18*wrap_y , ptr_cb, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
1499 ptr_cb= ebuf+18*wrap_y;
1500 ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
1501 ptr_cr= ebuf+18*wrap_y+8;
1502 }
1503
1504 if (s->mb_intra) {
1505 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
1506 int progressive_score, interlaced_score;
1507
1508 s->interlaced_dct=0;
1509 progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
1510 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
1511
1512 if(progressive_score > 0){
1513 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
1514 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
1515 if(progressive_score > interlaced_score){
1516 s->interlaced_dct=1;
1517
1518 dct_offset= wrap_y;
1519 wrap_y<<=1;
1520 if (s->chroma_format == CHROMA_422)
1521 wrap_c<<=1;
1522 }
1523 }
1524 }
1525
1526 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
1527 s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
1528 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
1529 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
1530
1531 if(s->flags&CODEC_FLAG_GRAY){
1532 skip_dct[4]= 1;
1533 skip_dct[5]= 1;
1534 }else{
1535 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1536 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1537 if(!s->chroma_y_shift){ /* 422 */
1538 s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset>>1), wrap_c);
1539 s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset>>1), wrap_c);
1540 }
1541 }
1542 }else{
1543 op_pixels_func (*op_pix)[4];
1544 qpel_mc_func (*op_qpix)[16];
1545 uint8_t *dest_y, *dest_cb, *dest_cr;
1546
1547 dest_y = s->dest[0];
1548 dest_cb = s->dest[1];
1549 dest_cr = s->dest[2];
1550
1551 if ((!s->no_rounding) || s->pict_type==B_TYPE){
1552 op_pix = s->dsp.put_pixels_tab;
1553 op_qpix= s->dsp.put_qpel_pixels_tab;
1554 }else{
1555 op_pix = s->dsp.put_no_rnd_pixels_tab;
1556 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
1557 }
1558
1559 if (s->mv_dir & MV_DIR_FORWARD) {
1560 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
1561 op_pix = s->dsp.avg_pixels_tab;
1562 op_qpix= s->dsp.avg_qpel_pixels_tab;
1563 }
1564 if (s->mv_dir & MV_DIR_BACKWARD) {
1565 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
1566 }
1567
1568 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
1569 int progressive_score, interlaced_score;
1570
1571 s->interlaced_dct=0;
1572 progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
1573 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
1574
1575 if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
1576
1577 if(progressive_score>0){
1578 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
1579 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
1580
1581 if(progressive_score > interlaced_score){
1582 s->interlaced_dct=1;
1583
1584 dct_offset= wrap_y;
1585 wrap_y<<=1;
1586 if (s->chroma_format == CHROMA_422)
1587 wrap_c<<=1;
1588 }
1589 }
1590 }
1591
1592 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
1593 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
1594 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
1595 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
1596
1597 if(s->flags&CODEC_FLAG_GRAY){
1598 skip_dct[4]= 1;
1599 skip_dct[5]= 1;
1600 }else{
1601 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1602 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1603 if(!s->chroma_y_shift){ /* 422 */
1604 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset>>1), dest_cb + (dct_offset>>1), wrap_c);
1605 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset>>1), dest_cr + (dct_offset>>1), wrap_c);
1606 }
1607 }
1608 /* pre quantization */
1609 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
1610 //FIXME optimize
1611 if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
1612 if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
1613 if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
1614 if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
1615 if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
1616 if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
1617 if(!s->chroma_y_shift){ /* 422 */
1618 if(s->dsp.sad[1](NULL, ptr_cb +(dct_offset>>1), dest_cb +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[6]= 1;
1619 if(s->dsp.sad[1](NULL, ptr_cr +(dct_offset>>1), dest_cr +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[7]= 1;
1620 }
1621 }
1622 }
1623
1624 if(s->avctx->quantizer_noise_shaping){
1625 if(!skip_dct[0]) get_vissual_weight(weight[0], ptr_y , wrap_y);
1626 if(!skip_dct[1]) get_vissual_weight(weight[1], ptr_y + 8, wrap_y);
1627 if(!skip_dct[2]) get_vissual_weight(weight[2], ptr_y + dct_offset , wrap_y);
1628 if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
1629 if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb , wrap_c);
1630 if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr , wrap_c);
1631 if(!s->chroma_y_shift){ /* 422 */
1632 if(!skip_dct[6]) get_vissual_weight(weight[6], ptr_cb + (dct_offset>>1), wrap_c);
1633 if(!skip_dct[7]) get_vissual_weight(weight[7], ptr_cr + (dct_offset>>1), wrap_c);
1634 }
1635 memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*mb_block_count);
1636 }
1637
1638 /* DCT & quantize */
1639 assert(s->out_format!=FMT_MJPEG || s->qscale==8);
1640 {
1641 for(i=0;i<mb_block_count;i++) {
1642 if(!skip_dct[i]){
1643 int overflow;
1644 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1645 // FIXME we could decide to change to quantizer instead of clipping
1646 // JS: I don't think that would be a good idea it could lower quality instead
1647 // of improve it. Just INTRADC clipping deserves changes in quantizer
1648 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1649 }else
1650 s->block_last_index[i]= -1;
1651 }
1652 if(s->avctx->quantizer_noise_shaping){
1653 for(i=0;i<mb_block_count;i++) {
1654 if(!skip_dct[i]){
1655 s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
1656 }
1657 }
1658 }
1659
1660 if(s->luma_elim_threshold && !s->mb_intra)
1661 for(i=0; i<4; i++)
1662 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
1663 if(s->chroma_elim_threshold && !s->mb_intra)
1664 for(i=4; i<mb_block_count; i++)
1665 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
1666
1667 if(s->flags & CODEC_FLAG_CBP_RD){
1668 for(i=0;i<mb_block_count;i++) {
1669 if(s->block_last_index[i] == -1)
1670 s->coded_score[i]= INT_MAX/256;
1671 }
1672 }
1673 }
1674
1675 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
1676 s->block_last_index[4]=
1677 s->block_last_index[5]= 0;
1678 s->block[4][0]=
1679 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
1680 }
1681
1682 //non c quantize code returns incorrect block_last_index FIXME
1683 if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
1684 for(i=0; i<mb_block_count; i++){
1685 int j;
1686 if(s->block_last_index[i]>0){
1687 for(j=63; j>0; j--){
1688 if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
1689 }
1690 s->block_last_index[i]= j;
1691 }
1692 }
1693 }
1694
1695 /* huffman encode */
1696 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
1697 case CODEC_ID_MPEG1VIDEO:
1698 case CODEC_ID_MPEG2VIDEO:
a6bc5731 1699 if (ENABLE_MPEG1VIDEO_ENCODER || ENABLE_MPEG2VIDEO_ENCODER)
56be950a
AJ
1700 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1701 break;
3ada94ba 1702 case CODEC_ID_MPEG4:
eb75a698 1703 if (ENABLE_MPEG4_ENCODER)
3ada94ba
BF
1704 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
1705 case CODEC_ID_MSMPEG4V2:
1706 case CODEC_ID_MSMPEG4V3:
1707 case CODEC_ID_WMV1:
1708 if (ENABLE_MSMPEG4_ENCODER)
1709 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1710 break;
1711 case CODEC_ID_WMV2:
1712 if (ENABLE_WMV2_ENCODER)
1713 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
1714 break;
1715 case CODEC_ID_H261:
1716 if (ENABLE_H261_ENCODER)
1717 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
1718 break;
1719 case CODEC_ID_H263:
1720 case CODEC_ID_H263P:
1721 case CODEC_ID_FLV1:
1722 case CODEC_ID_RV10:
1723 case CODEC_ID_RV20:
eb75a698
AJ
1724 if (ENABLE_H263_ENCODER || ENABLE_H263P_ENCODER ||
1725 ENABLE_FLV_ENCODER || ENABLE_RV10_ENCODER || ENABLE_RV20_ENCODER)
3ada94ba
BF
1726 h263_encode_mb(s, s->block, motion_x, motion_y); break;
1727 case CODEC_ID_MJPEG:
1728 if (ENABLE_MJPEG_ENCODER)
1729 ff_mjpeg_encode_mb(s, s->block);
1730 break;
1731 default:
1732 assert(0);
1733 }
1734}
1735
1736static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1737{
1738 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
1739 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
1740}
1741
3ada94ba
BF
1742static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1743 int i;
1744
1745 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1746
1747 /* mpeg1 */
1748 d->mb_skip_run= s->mb_skip_run;
1749 for(i=0; i<3; i++)
1750 d->last_dc[i]= s->last_dc[i];
1751
1752 /* statistics */
1753 d->mv_bits= s->mv_bits;
1754 d->i_tex_bits= s->i_tex_bits;
1755 d->p_tex_bits= s->p_tex_bits;
1756 d->i_count= s->i_count;
1757 d->f_count= s->f_count;
1758 d->b_count= s->b_count;
1759 d->skip_count= s->skip_count;
1760 d->misc_bits= s->misc_bits;
1761 d->last_bits= 0;
1762
1763 d->mb_skipped= 0;
1764 d->qscale= s->qscale;
1765 d->dquant= s->dquant;
1766}
1767
1768static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1769 int i;
1770
1771 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
1772 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1773
1774 /* mpeg1 */
1775 d->mb_skip_run= s->mb_skip_run;
1776 for(i=0; i<3; i++)
1777 d->last_dc[i]= s->last_dc[i];
1778
1779 /* statistics */
1780 d->mv_bits= s->mv_bits;
1781 d->i_tex_bits= s->i_tex_bits;
1782 d->p_tex_bits= s->p_tex_bits;
1783 d->i_count= s->i_count;
1784 d->f_count= s->f_count;
1785 d->b_count= s->b_count;
1786 d->skip_count= s->skip_count;
1787 d->misc_bits= s->misc_bits;
1788
1789 d->mb_intra= s->mb_intra;
1790 d->mb_skipped= s->mb_skipped;
1791 d->mv_type= s->mv_type;
1792 d->mv_dir= s->mv_dir;
1793 d->pb= s->pb;
1794 if(s->data_partitioning){
1795 d->pb2= s->pb2;
1796 d->tex_pb= s->tex_pb;
1797 }
1798 d->block= s->block;
1799 for(i=0; i<8; i++)
1800 d->block_last_index[i]= s->block_last_index[i];
1801 d->interlaced_dct= s->interlaced_dct;
1802 d->qscale= s->qscale;
1803}
1804
1805static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
1806 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
1807 int *dmin, int *next_block, int motion_x, int motion_y)
1808{
1809 int score;
1810 uint8_t *dest_backup[3];
1811
1812 copy_context_before_encode(s, backup, type);
1813
1814 s->block= s->blocks[*next_block];
1815 s->pb= pb[*next_block];
1816 if(s->data_partitioning){
1817 s->pb2 = pb2 [*next_block];
1818 s->tex_pb= tex_pb[*next_block];
1819 }
1820
1821 if(*next_block){
1822 memcpy(dest_backup, s->dest, sizeof(s->dest));
1823 s->dest[0] = s->rd_scratchpad;
1824 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
1825 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
1826 assert(s->linesize >= 32); //FIXME
1827 }
1828
1829 encode_mb(s, motion_x, motion_y);
1830
1831 score= put_bits_count(&s->pb);
1832 if(s->data_partitioning){
1833 score+= put_bits_count(&s->pb2);
1834 score+= put_bits_count(&s->tex_pb);
1835 }
1836
1837 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
1838 MPV_decode_mb(s, s->block);
1839
1840 score *= s->lambda2;
1841 score += sse_mb(s) << FF_LAMBDA_SHIFT;
1842 }
1843
1844 if(*next_block){
1845 memcpy(s->dest, dest_backup, sizeof(s->dest));
1846 }
1847
1848 if(score<*dmin){
1849 *dmin= score;
1850 *next_block^=1;
1851
1852 copy_context_after_encode(best, s, type);
1853 }
1854}
1855
1856static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
1857 uint32_t *sq = ff_squareTbl + 256;
1858 int acc=0;
1859 int x,y;
1860
1861 if(w==16 && h==16)
1862 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
1863 else if(w==8 && h==8)
1864 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
1865
1866 for(y=0; y<h; y++){
1867 for(x=0; x<w; x++){
1868 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
1869 }
1870 }
1871
1872 assert(acc>=0);
1873
1874 return acc;
1875}
1876
1877static int sse_mb(MpegEncContext *s){
1878 int w= 16;
1879 int h= 16;
1880
1881 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
1882 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
1883
1884 if(w==16 && h==16)
1885 if(s->avctx->mb_cmp == FF_CMP_NSSE){
1886 return s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
1887 +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
1888 +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
1889 }else{
1890 return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
1891 +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
1892 +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
1893 }
1894 else
1895 return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
1896 +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
1897 +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
1898}
1899
1900static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
1901 MpegEncContext *s= arg;
1902
1903
1904 s->me.pre_pass=1;
1905 s->me.dia_size= s->avctx->pre_dia_size;
1906 s->first_slice_line=1;
1907 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
1908 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
1909 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
1910 }
1911 s->first_slice_line=0;
1912 }
1913
1914 s->me.pre_pass=0;
1915
1916 return 0;
1917}
1918
1919static int estimate_motion_thread(AVCodecContext *c, void *arg){
1920 MpegEncContext *s= arg;
1921
1922 ff_check_alignment();
1923
1924 s->me.dia_size= s->avctx->dia_size;
1925 s->first_slice_line=1;
1926 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
1927 s->mb_x=0; //for block init below
1928 ff_init_block_index(s);
1929 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
1930 s->block_index[0]+=2;
1931 s->block_index[1]+=2;
1932 s->block_index[2]+=2;
1933 s->block_index[3]+=2;
1934
1935 /* compute motion vector & mb_type and store in context */
1936 if(s->pict_type==B_TYPE)
1937 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
1938 else
1939 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
1940 }
1941 s->first_slice_line=0;
1942 }
1943 return 0;
1944}
1945
1946static int mb_var_thread(AVCodecContext *c, void *arg){
1947 MpegEncContext *s= arg;
1948 int mb_x, mb_y;
1949
1950 ff_check_alignment();
1951
1952 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
1953 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1954 int xx = mb_x * 16;
1955 int yy = mb_y * 16;
1956 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
1957 int varc;
1958 int sum = s->dsp.pix_sum(pix, s->linesize);
1959
1960 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
1961
1962 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
1963 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1964 s->me.mb_var_sum_temp += varc;
1965 }
1966 }
1967 return 0;
1968}
1969
1970static void write_slice_end(MpegEncContext *s){
eb75a698 1971 if(ENABLE_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
3ada94ba
BF
1972 if(s->partitioned_frame){
1973 ff_mpeg4_merge_partitions(s);
1974 }
1975
1976 ff_mpeg4_stuffing(&s->pb);
1977 }else if(ENABLE_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
1978 ff_mjpeg_encode_stuffing(&s->pb);
1979 }
1980
1981 align_put_bits(&s->pb);
1982 flush_put_bits(&s->pb);
1983
1984 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
1985 s->misc_bits+= get_bits_diff(s);
1986}
1987
1988static int encode_thread(AVCodecContext *c, void *arg){
1989 MpegEncContext *s= arg;
1990 int mb_x, mb_y, pdif = 0;
1991 int i, j;
1992 MpegEncContext best_s, backup_s;
1993 uint8_t bit_buf[2][MAX_MB_BYTES];
1994 uint8_t bit_buf2[2][MAX_MB_BYTES];
1995 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
1996 PutBitContext pb[2], pb2[2], tex_pb[2];
1997//printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
1998
1999 ff_check_alignment();
2000
2001 for(i=0; i<2; i++){
2002 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2003 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2004 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2005 }
2006
2007 s->last_bits= put_bits_count(&s->pb);
2008 s->mv_bits=0;
2009 s->misc_bits=0;
2010 s->i_tex_bits=0;
2011 s->p_tex_bits=0;
2012 s->i_count=0;
2013 s->f_count=0;
2014 s->b_count=0;
2015 s->skip_count=0;
2016
2017 for(i=0; i<3; i++){
2018 /* init last dc values */
2019 /* note: quant matrix value (8) is implied here */
2020 s->last_dc[i] = 128 << s->intra_dc_precision;
2021
2022 s->current_picture.error[i] = 0;
2023 }
2024 s->mb_skip_run = 0;
2025 memset(s->last_mv, 0, sizeof(s->last_mv));
2026
2027 s->last_mv_dir = 0;
2028
2029 switch(s->codec_id){
2030 case CODEC_ID_H263:
2031 case CODEC_ID_H263P:
2032 case CODEC_ID_FLV1:
eb75a698 2033 if (ENABLE_H263_ENCODER || ENABLE_H263P_ENCODER || ENABLE_FLV_ENCODER)
3ada94ba
BF
2034 s->gob_index = ff_h263_get_gob_height(s);
2035 break;
2036 case CODEC_ID_MPEG4:
eb75a698 2037 if(ENABLE_MPEG4_ENCODER && s->partitioned_frame)
3ada94ba
BF
2038 ff_mpeg4_init_partitions(s);
2039 break;
2040 }
2041
2042 s->resync_mb_x=0;
2043 s->resync_mb_y=0;
2044 s->first_slice_line = 1;
2045 s->ptr_lastgob = s->pb.buf;
2046 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2047// printf("row %d at %X\n", s->mb_y, (int)s);
2048 s->mb_x=0;
2049 s->mb_y= mb_y;
2050
2051 ff_set_qscale(s, s->qscale);
2052 ff_init_block_index(s);
2053
2054 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2055 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2056 int mb_type= s->mb_type[xy];
2057// int d;
2058 int dmin= INT_MAX;
2059 int dir;
2060
2061 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2062 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2063 return -1;
2064 }
2065 if(s->data_partitioning){
2066 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2067 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2068 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2069 return -1;
2070 }
2071 }
2072
2073 s->mb_x = mb_x;
2074 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2075 ff_update_block_index(s);
2076
2077 if(ENABLE_H261_ENCODER && s->codec_id == CODEC_ID_H261){
2078 ff_h261_reorder_mb_index(s);
2079 xy= s->mb_y*s->mb_stride + s->mb_x;
2080 mb_type= s->mb_type[xy];
2081 }
2082
2083 /* write gob / video packet header */
2084 if(s->rtp_mode){
2085 int current_packet_size, is_gob_start;
2086
2087 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2088
2089 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2090
2091 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2092
2093 switch(s->codec_id){
2094 case CODEC_ID_H263:
2095 case CODEC_ID_H263P:
2096 if(!s->h263_slice_structured)
2097 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2098 break;
2099 case CODEC_ID_MPEG2VIDEO:
2100 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2101 case CODEC_ID_MPEG1VIDEO:
2102 if(s->mb_skip_run) is_gob_start=0;
2103 break;
2104 }
2105
2106 if(is_gob_start){
2107 if(s->start_mb_y != mb_y || mb_x!=0){
2108 write_slice_end(s);
2109
eb75a698 2110 if(ENABLE_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
3ada94ba
BF
2111 ff_mpeg4_init_partitions(s);
2112 }
2113 }
2114
2115 assert((put_bits_count(&s->pb)&7) == 0);
2116 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
2117
2118 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2119 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2120 int d= 100 / s->avctx->error_rate;
2121 if(r % d == 0){
2122 current_packet_size=0;
2123#ifndef ALT_BITSTREAM_WRITER
2124 s->pb.buf_ptr= s->ptr_lastgob;
2125#endif
2126 assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
2127 }
2128 }
2129
2130 if (s->avctx->rtp_callback){
2131 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2132 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2133 }
2134
2135 switch(s->codec_id){
2136 case CODEC_ID_MPEG4:
eb75a698 2137 if (ENABLE_MPEG4_ENCODER) {
3ada94ba
BF
2138 ff_mpeg4_encode_video_packet_header(s);
2139 ff_mpeg4_clean_buffers(s);
eb75a698 2140 }
3ada94ba
BF
2141 break;
2142 case CODEC_ID_MPEG1VIDEO:
2143 case CODEC_ID_MPEG2VIDEO:
a6bc5731 2144 if (ENABLE_MPEG1VIDEO_ENCODER || ENABLE_MPEG2VIDEO_ENCODER) {
56be950a
AJ
2145 ff_mpeg1_encode_slice_header(s);
2146 ff_mpeg1_clean_buffers(s);
a6bc5731 2147 }
3ada94ba
BF
2148 break;
2149 case CODEC_ID_H263:
2150 case CODEC_ID_H263P:
eb75a698 2151 if (ENABLE_H263_ENCODER || ENABLE_H263P_ENCODER)
3ada94ba
BF
2152 h263_encode_gob_header(s, mb_y);
2153 break;
2154 }
2155
2156 if(s->flags&CODEC_FLAG_PASS1){
2157 int bits= put_bits_count(&s->pb);
2158 s->misc_bits+= bits - s->last_bits;
2159 s->last_bits= bits;
2160 }
2161
2162 s->ptr_lastgob += current_packet_size;
2163 s->first_slice_line=1;
2164 s->resync_mb_x=mb_x;
2165 s->resync_mb_y=mb_y;
2166 }
2167 }
2168
2169 if( (s->resync_mb_x == s->mb_x)
2170 && s->resync_mb_y+1 == s->mb_y){
2171 s->first_slice_line=0;
2172 }
2173
2174 s->mb_skipped=0;
2175 s->dquant=0; //only for QP_RD
2176
2177 if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD
2178 int next_block=0;
2179 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2180
2181 copy_context_before_encode(&backup_s, s, -1);
2182 backup_s.pb= s->pb;
2183 best_s.data_partitioning= s->data_partitioning;
2184 best_s.partitioned_frame= s->partitioned_frame;
2185 if(s->data_partitioning){
2186 backup_s.pb2= s->pb2;
2187 backup_s.tex_pb= s->tex_pb;
2188 }
2189
2190 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2191 s->mv_dir = MV_DIR_FORWARD;
2192 s->mv_type = MV_TYPE_16X16;
2193 s->mb_intra= 0;
2194 s->mv[0][0][0] = s->p_mv_table[xy][0];
2195 s->mv[0][0][1] = s->p_mv_table[xy][1];
2196 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2197 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2198 }
2199 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2200 s->mv_dir = MV_DIR_FORWARD;
2201 s->mv_type = MV_TYPE_FIELD;
2202 s->mb_intra= 0;
2203 for(i=0; i<2; i++){
2204 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2205 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2206 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2207 }
2208 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2209 &dmin, &next_block, 0, 0);
2210 }
2211 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2212 s->mv_dir = MV_DIR_FORWARD;
2213 s->mv_type = MV_TYPE_16X16;
2214 s->mb_intra= 0;
2215 s->mv[0][0][0] = 0;
2216 s->mv[0][0][1] = 0;
2217 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2218 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2219 }
2220 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2221 s->mv_dir = MV_DIR_FORWARD;
2222 s->mv_type = MV_TYPE_8X8;
2223 s->mb_intra= 0;
2224 for(i=0; i<4; i++){
2225 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2226 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2227 }
2228 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2229 &dmin, &next_block, 0, 0);
2230 }
2231 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2232 s->mv_dir = MV_DIR_FORWARD;
2233 s->mv_type = MV_TYPE_16X16;
2234 s->mb_intra= 0;
2235 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2236 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2237 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2238 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2239 }
2240 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2241 s->mv_dir = MV_DIR_BACKWARD;
2242 s->mv_type = MV_TYPE_16X16;
2243 s->mb_intra= 0;
2244 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2245 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2246 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2247 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2248 }
2249 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2250 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2251 s->mv_type = MV_TYPE_16X16;
2252 s->mb_intra= 0;
2253 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2254 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2255 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2256 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2257 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2258 &dmin, &next_block, 0, 0);
2259 }
2260 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2261 s->mv_dir = MV_DIR_FORWARD;
2262 s->mv_type = MV_TYPE_FIELD;
2263 s->mb_intra= 0;
2264 for(i=0; i<2; i++){
2265 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2266 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2267 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2268 }
2269 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2270 &dmin, &next_block, 0, 0);
2271 }
2272 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2273 s->mv_dir = MV_DIR_BACKWARD;
2274 s->mv_type = MV_TYPE_FIELD;
2275 s->mb_intra= 0;
2276 for(i=0; i<2; i++){
2277 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2278 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2279 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2280 }
2281 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2282 &dmin, &next_block, 0, 0);
2283 }
2284 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2285 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2286 s->mv_type = MV_TYPE_FIELD;
2287 s->mb_intra= 0;
2288 for(dir=0; dir<2; dir++){
2289 for(i=0; i<2; i++){
2290 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2291 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2292 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2293 }
2294 }
2295 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2296 &dmin, &next_block, 0, 0);
2297 }
2298 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2299 s->mv_dir = 0;
2300 s->mv_type = MV_TYPE_16X16;
2301 s->mb_intra= 1;
2302 s->mv[0][0][0] = 0;
2303 s->mv[0][0][1] = 0;
2304 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2305 &dmin, &next_block, 0, 0);
2306 if(s->h263_pred || s->h263_aic){
2307 if(best_s.mb_intra)
2308 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2309 else
2310 ff_clean_intra_table_entries(s); //old mode?
2311 }
2312 }
2313
2314 if((s->flags & CODEC_FLAG_QP_RD) && dmin < INT_MAX){
2315 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2316 const int last_qp= backup_s.qscale;
2317 int qpi, qp, dc[6];
2318 DCTELEM ac[6][16];
2319 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2320 static const int dquant_tab[4]={-1,1,-2,2};
2321
2322 assert(backup_s.dquant == 0);
2323
2324 //FIXME intra
2325 s->mv_dir= best_s.mv_dir;
2326 s->mv_type = MV_TYPE_16X16;
2327 s->mb_intra= best_s.mb_intra;
2328 s->mv[0][0][0] = best_s.mv[0][0][0];
2329 s->mv[0][0][1] = best_s.mv[0][0][1];
2330 s->mv[1][0][0] = best_s.mv[1][0][0];
2331 s->mv[1][0][1] = best_s.mv[1][0][1];
2332
2333 qpi = s->pict_type == B_TYPE ? 2 : 0;
2334 for(; qpi<4; qpi++){
2335 int dquant= dquant_tab[qpi];
2336 qp= last_qp + dquant;
2337 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2338 continue;
2339 backup_s.dquant= dquant;
2340 if(s->mb_intra && s->dc_val[0]){
2341 for(i=0; i<6; i++){
2342 dc[i]= s->dc_val[0][ s->block_index[i] ];
2343 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
2344 }
2345 }
2346
2347 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2348 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2349 if(best_s.qscale != qp){
2350 if(s->mb_intra && s->dc_val[0]){
2351 for(i=0; i<6; i++){
2352 s->dc_val[0][ s->block_index[i] ]= dc[i];
2353 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
2354 }
2355 }
2356 }
2357 }
2358 }
2359 }
eb75a698 2360 if(ENABLE_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3ada94ba
BF
2361 int mx= s->b_direct_mv_table[xy][0];
2362 int my= s->b_direct_mv_table[xy][1];
2363
2364 backup_s.dquant = 0;
2365 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2366 s->mb_intra= 0;
2367 ff_mpeg4_set_direct_mv(s, mx, my);
2368 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2369 &dmin, &next_block, mx, my);
2370 }
eb75a698 2371 if(ENABLE_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3ada94ba
BF
2372 backup_s.dquant = 0;
2373 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2374 s->mb_intra= 0;
2375 ff_mpeg4_set_direct_mv(s, 0, 0);
2376 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2377 &dmin, &next_block, 0, 0);
2378 }
2379 if(!best_s.mb_intra && s->flags2&CODEC_FLAG2_SKIP_RD){
2380 int coded=0;
2381 for(i=0; i<6; i++)
2382 coded |= s->block_last_index[i];
2383 if(coded){
2384 int mx,my;
2385 memcpy(s->mv, best_s.mv, sizeof(s->mv));
eb75a698 2386 if(ENABLE_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3ada94ba
BF
2387 mx=my=0; //FIXME find the one we actually used
2388 ff_mpeg4_set_direct_mv(s, mx, my);
2389 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2390 mx= s->mv[1][0][0];
2391 my= s->mv[1][0][1];
2392 }else{
2393 mx= s->mv[0][0][0];
2394 my= s->mv[0][0][1];
2395 }
2396
2397 s->mv_dir= best_s.mv_dir;
2398 s->mv_type = best_s.mv_type;
2399 s->mb_intra= 0;
2400/* s->mv[0][0][0] = best_s.mv[0][0][0];
2401 s->mv[0][0][1] = best_s.mv[0][0][1];
2402 s->mv[1][0][0] = best_s.mv[1][0][0];
2403 s->mv[1][0][1] = best_s.mv[1][0][1];*/
2404 backup_s.dquant= 0;
2405 s->skipdct=1;
2406 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2407 &dmin, &next_block, mx, my);
2408 s->skipdct=0;
2409 }
2410 }
2411
2412 s->current_picture.qscale_table[xy]= best_s.qscale;
2413
2414 copy_context_after_encode(s, &best_s, -1);
2415
2416 pb_bits_count= put_bits_count(&s->pb);
2417 flush_put_bits(&s->pb);
2418 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2419 s->pb= backup_s.pb;
2420
2421 if(s->data_partitioning){
2422 pb2_bits_count= put_bits_count(&s->pb2);
2423 flush_put_bits(&s->pb2);
2424 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2425 s->pb2= backup_s.pb2;
2426
2427 tex_pb_bits_count= put_bits_count(&s->tex_pb);
2428 flush_put_bits(&s->tex_pb);
2429 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2430 s->tex_pb= backup_s.tex_pb;
2431 }
2432 s->last_bits= put_bits_count(&s->pb);
2433
eb75a698
AJ
2434 if (ENABLE_ANY_H263_ENCODER &&
2435 s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
3ada94ba
BF
2436 ff_h263_update_motion_val(s);
2437
2438 if(next_block==0){ //FIXME 16 vs linesize16
2439 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
2440 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
2441 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2442 }
2443
2444 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2445 MPV_decode_mb(s, s->block);
2446 } else {
2447 int motion_x = 0, motion_y = 0;
2448 s->mv_type=MV_TYPE_16X16;
2449 // only one MB-Type possible
2450
2451 switch(mb_type){
2452 case CANDIDATE_MB_TYPE_INTRA:
2453 s->mv_dir = 0;
2454 s->mb_intra= 1;
2455 motion_x= s->mv[0][0][0] = 0;
2456 motion_y= s->mv[0][0][1] = 0;
2457 break;
2458 case CANDIDATE_MB_TYPE_INTER:
2459 s->mv_dir = MV_DIR_FORWARD;
2460 s->mb_intra= 0;
2461 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2462 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2463 break;
2464 case CANDIDATE_MB_TYPE_INTER_I:
2465 s->mv_dir = MV_DIR_FORWARD;
2466 s->mv_type = MV_TYPE_FIELD;
2467 s->mb_intra= 0;
2468 for(i=0; i<2; i++){
2469 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2470 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2471 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2472 }
2473 break;
2474 case CANDIDATE_MB_TYPE_INTER4V:
2475 s->mv_dir = MV_DIR_FORWARD;
2476 s->mv_type = MV_TYPE_8X8;
2477 s->mb_intra= 0;
2478 for(i=0; i<4; i++){
2479 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2480 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2481 }
2482 break;
2483 case CANDIDATE_MB_TYPE_DIRECT:
eb75a698 2484 if (ENABLE_MPEG4_ENCODER) {
3ada94ba
BF
2485 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2486 s->mb_intra= 0;
2487 motion_x=s->b_direct_mv_table[xy][0];
2488 motion_y=s->b_direct_mv_table[xy][1];
2489 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
eb75a698 2490 }
3ada94ba
BF
2491 break;
2492 case CANDIDATE_MB_TYPE_DIRECT0:
eb75a698 2493 if (ENABLE_MPEG4_ENCODER) {
3ada94ba
BF
2494 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2495 s->mb_intra= 0;
2496 ff_mpeg4_set_direct_mv(s, 0, 0);
eb75a698 2497 }
3ada94ba
BF
2498 break;
2499 case CANDIDATE_MB_TYPE_BIDIR:
2500 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2501 s->mb_intra= 0;
2502 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2503 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2504 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2505 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2506 break;
2507 case CANDIDATE_MB_TYPE_BACKWARD:
2508 s->mv_dir = MV_DIR_BACKWARD;
2509 s->mb_intra= 0;
2510 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2511 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2512 break;
2513 case CANDIDATE_MB_TYPE_FORWARD:
2514 s->mv_dir = MV_DIR_FORWARD;
2515 s->mb_intra= 0;
2516 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2517 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2518// printf(" %d %d ", motion_x, motion_y);
2519 break;
2520 case CANDIDATE_MB_TYPE_FORWARD_I:
2521 s->mv_dir = MV_DIR_FORWARD;
2522 s->mv_type = MV_TYPE_FIELD;
2523 s->mb_intra= 0;
2524 for(i=0; i<2; i++){
2525 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2526 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2527 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2528 }
2529 break;
2530 case CANDIDATE_MB_TYPE_BACKWARD_I:
2531 s->mv_dir = MV_DIR_BACKWARD;
2532 s->mv_type = MV_TYPE_FIELD;
2533 s->mb_intra= 0;
2534 for(i=0; i<2; i++){
2535 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2536 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2537 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2538 }
2539 break;
2540 case CANDIDATE_MB_TYPE_BIDIR_I:
2541 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2542 s->mv_type = MV_TYPE_FIELD;
2543 s->mb_intra= 0;
2544 for(dir=0; dir<2; dir++){
2545 for(i=0; i<2; i++){
2546 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2547 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2548 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2549 }
2550 }
2551 break;
2552 default:
2553 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
2554 }
2555
2556 encode_mb(s, motion_x, motion_y);
2557
2558 // RAL: Update last macroblock type
2559 s->last_mv_dir = s->mv_dir;
2560
eb75a698
AJ
2561 if (ENABLE_ANY_H263_ENCODER &&
2562 s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
3ada94ba
BF
2563 ff_h263_update_motion_val(s);
2564
2565 MPV_decode_mb(s, s->block);
2566 }
2567
2568 /* clean the MV table in IPS frames for direct mode in B frames */
2569 if(s->mb_intra /* && I,P,S_TYPE */){
2570 s->p_mv_table[xy][0]=0;
2571 s->p_mv_table[xy][1]=0;
2572 }
2573
2574 if(s->flags&CODEC_FLAG_PSNR){
2575 int w= 16;
2576 int h= 16;
2577
2578 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2579 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2580
2581 s->current_picture.error[0] += sse(
2582 s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
2583 s->dest[0], w, h, s->linesize);
2584 s->current_picture.error[1] += sse(
2585 s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
2586 s->dest[1], w>>1, h>>1, s->uvlinesize);
2587 s->current_picture.error[2] += sse(
2588 s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
2589 s->dest[2], w>>1, h>>1, s->uvlinesize);
2590 }
2591 if(s->loop_filter){
eb75a698 2592 if(ENABLE_ANY_H263_ENCODER && s->out_format == FMT_H263)
3ada94ba
BF
2593 ff_h263_loop_filter(s);
2594 }
2595//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
2596 }
2597 }
2598
2599 //not beautiful here but we must write it before flushing so it has to be here
2600 if (ENABLE_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
2601 msmpeg4_encode_ext_header(s);
2602
2603 write_slice_end(s);
2604
2605 /* Send the last GOB if RTP */
2606 if (s->avctx->rtp_callback) {
2607 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
2608 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
2609 /* Call the RTP callback to send the last GOB */
2610 emms_c();
2611 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
2612 }
2613
2614 return 0;
2615}
2616
2617#define MERGE(field) dst->field += src->field; src->field=0
2618static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
2619 MERGE(me.scene_change_score);
2620 MERGE(me.mc_mb_var_sum_temp);
2621 MERGE(me.mb_var_sum_temp);
2622}
2623
2624static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
2625 int i;
2626
2627 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
2628 MERGE(dct_count[1]);
2629 MERGE(mv_bits);
2630 MERGE(i_tex_bits);
2631 MERGE(p_tex_bits);
2632 MERGE(i_count);
2633 MERGE(f_count);
2634 MERGE(b_count);
2635 MERGE(skip_count);
2636 MERGE(misc_bits);
2637 MERGE(error_count);
2638 MERGE(padding_bug_score);
2639 MERGE(current_picture.error[0]);
2640 MERGE(current_picture.error[1]);
2641 MERGE(current_picture.error[2]);
2642
2643 if(dst->avctx->noise_reduction){
2644 for(i=0; i<64; i++){
2645 MERGE(dct_error_sum[0][i]);
2646 MERGE(dct_error_sum[1][i]);
2647 }
2648 }
2649
2650 assert(put_bits_count(&src->pb) % 8 ==0);
2651 assert(put_bits_count(&dst->pb) % 8 ==0);
2652 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
2653 flush_put_bits(&dst->pb);
2654}
2655
2656static int estimate_qp(MpegEncContext *s, int dry_run){
2657 if (s->next_lambda){
2658 s->current_picture_ptr->quality=
2659 s->current_picture.quality = s->next_lambda;
2660 if(!dry_run) s->next_lambda= 0;
2661 } else if (!s->fixed_qscale) {
2662 s->current_picture_ptr->quality=
2663 s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run);
2664 if (s->current_picture.quality < 0)
2665 return -1;
2666 }
2667
2668 if(s->adaptive_quant){
2669 switch(s->codec_id){
2670 case CODEC_ID_MPEG4:
eb75a698 2671 if (ENABLE_MPEG4_ENCODER)
3ada94ba
BF
2672 ff_clean_mpeg4_qscales(s);
2673 break;
2674 case CODEC_ID_H263:
2675 case CODEC_ID_H263P:
2676 case CODEC_ID_FLV1:
eb75a698 2677 if (ENABLE_H263_ENCODER||ENABLE_H263P_ENCODER||ENABLE_FLV_ENCODER)
3ada94ba
BF
2678 ff_clean_h263_qscales(s);
2679 break;
2680 }
2681
2682 s->lambda= s->lambda_table[0];
2683 //FIXME broken
2684 }else
2685 s->lambda= s->current_picture.quality;
2686//printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
2687 update_qscale(s);
2688 return 0;
2689}
2690
7da31a80
AJ
2691/* must be called before writing the header */
2692static void set_frame_distances(MpegEncContext * s){
2693 assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE);
2694 s->time= s->current_picture_ptr->pts*s->avctx->time_base.num;
2695
2696 if(s->pict_type==B_TYPE){
2697 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
2698 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
2699 }else{
2700 s->pp_time= s->time - s->last_non_b_time;
2701 s->last_non_b_time= s->time;
2702 assert(s->picture_number==0 || s->pp_time > 0);
2703 }
2704}
2705
3ada94ba
BF
2706static int encode_picture(MpegEncContext *s, int picture_number)
2707{
2708 int i;
2709 int bits;
2710
2711 s->picture_number = picture_number;
2712
2713 /* Reset the average MB variance */
2714 s->me.mb_var_sum_temp =
2715 s->me.mc_mb_var_sum_temp = 0;
2716
2717 /* we need to initialize some time vars before we can encode b-frames */
2718 // RAL: Condition added for MPEG1VIDEO
2719 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
7da31a80
AJ
2720 set_frame_distances(s);
2721 if(ENABLE_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
2722 ff_set_mpeg4_time(s);
3ada94ba
BF
2723
2724 s->me.scene_change_score=0;
2725
2726// s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
2727
2728 if(s->pict_type==I_TYPE){
2729 if(s->msmpeg4_version >= 3) s->no_rounding=1;
2730 else s->no_rounding=0;
2731 }else if(s->pict_type!=B_TYPE){
2732 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
2733 s->no_rounding ^= 1;
2734 }
2735
2736 if(s->flags & CODEC_FLAG_PASS2){
2737 if (estimate_qp(s,1) < 0)
2738 return -1;
2739 ff_get_2pass_fcode(s);
2740 }else if(!(s->flags & CODEC_FLAG_QSCALE)){
2741 if(s->pict_type==B_TYPE)
2742 s->lambda= s->last_lambda_for[s->pict_type];
2743 else
2744 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
2745 update_qscale(s);
2746 }
2747
2748 s->mb_intra=0; //for the rate distortion & bit compare functions
2749 for(i=1; i<s->avctx->thread_count; i++){
2750 ff_update_duplicate_context(s->thread_context[i], s);
2751 }
2752
2753 ff_init_me(s);
2754
2755 /* Estimate motion for every MB */
2756 if(s->pict_type != I_TYPE){
2757 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
2758 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
2759 if(s->pict_type != B_TYPE && s->avctx->me_threshold==0){
2760 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
2761 s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
2762 }
2763 }
2764
2765 s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
2766 }else /* if(s->pict_type == I_TYPE) */{
2767 /* I-Frame */
2768 for(i=0; i<s->mb_stride*s->mb_height; i++)
2769 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
2770
2771 if(!s->fixed_qscale){
2772 /* finding spatial complexity for I-frame rate control */
2773 s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
2774 }
2775 }
2776 for(i=1; i<s->avctx->thread_count; i++){
2777 merge_context_after_me(s, s->thread_context[i]);
2778 }
2779 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
2780 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
2781 emms_c();
2782
2783 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
2784 s->pict_type= I_TYPE;
2785 for(i=0; i<s->mb_stride*s->mb_height; i++)
2786 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
2787//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
2788 }
2789
2790 if(!s->umvplus){
2791 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
2792 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
2793
2794 if(s->flags & CODEC_FLAG_INTERLACED_ME){
2795 int a,b;
2796 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
2797 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
2798 s->f_code= FFMAX(s->f_code, FFMAX(a,b));
2799 }
2800
2801 ff_fix_long_p_mvs(s);
2802 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
2803 if(s->flags & CODEC_FLAG_INTERLACED_ME){
2804 int j;
2805 for(i=0; i<2; i++){
2806 for(j=0; j<2; j++)
2807 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
2808 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
2809 }
2810 }
2811 }
2812
2813 if(s->pict_type==B_TYPE){
2814 int a, b;
2815
2816 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
2817 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
2818 s->f_code = FFMAX(a, b);
2819
2820 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
2821 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
2822 s->b_code = FFMAX(a, b);
2823
2824 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
2825 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
2826 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
2827 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
2828 if(s->flags & CODEC_FLAG_INTERLACED_ME){
2829 int dir, j;
2830 for(dir=0; dir<2; dir++){
2831 for(i=0; i<2; i++){
2832 for(j=0; j<2; j++){
2833 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
2834 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
2835 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
2836 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
2837 }
2838 }
2839 }
2840 }
2841 }
2842 }
2843
2844 if (estimate_qp(s, 0) < 0)
2845 return -1;
2846
2847 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
2848 s->qscale= 3; //reduce clipping problems
2849
2850 if (s->out_format == FMT_MJPEG) {
2851 /* for mjpeg, we do include qscale in the matrix */
2852 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2853 for(i=1;i<64;i++){
2854 int j= s->dsp.idct_permutation[i];
2855
2856 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2857 }
2858 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
2859 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
2860 s->qscale= 8;
2861 }
2862
2863 //FIXME var duplication
2864 s->current_picture_ptr->key_frame=
2865 s->current_picture.key_frame= s->pict_type == I_TYPE; //FIXME pic_ptr
2866 s->current_picture_ptr->pict_type=
2867 s->current_picture.pict_type= s->pict_type;
2868
2869 if(s->current_picture.key_frame)
2870 s->picture_in_gop_number=0;
2871
2872 s->last_bits= put_bits_count(&s->pb);
2873 switch(s->out_format) {
2874 case FMT_MJPEG:
2875 if (ENABLE_MJPEG_ENCODER)
2876 ff_mjpeg_encode_picture_header(s);
2877 break;
2878 case FMT_H261:
2879 if (ENABLE_H261_ENCODER)
2880 ff_h261_encode_picture_header(s, picture_number);
2881 break;
2882 case FMT_H263:
2883 if (ENABLE_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
2884 ff_wmv2_encode_picture_header(s, picture_number);
2885 else if (ENABLE_MSMPEG4_ENCODER && s->h263_msmpeg4)
2886 msmpeg4_encode_picture_header(s, picture_number);
eb75a698 2887 else if (ENABLE_MPEG4_ENCODER && s->h263_pred)
3ada94ba
BF
2888 mpeg4_encode_picture_header(s, picture_number);
2889 else if (ENABLE_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
2890 rv10_encode_picture_header(s, picture_number);
2891 else if (ENABLE_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
2892 rv20_encode_picture_header(s, picture_number);
eb75a698 2893 else if (ENABLE_FLV_ENCODER && s->codec_id == CODEC_ID_FLV1)
3ada94ba 2894 ff_flv_encode_picture_header(s, picture_number);
eb75a698 2895 else if (ENABLE_ANY_H263_ENCODER)
3ada94ba
BF
2896 h263_encode_picture_header(s, picture_number);
2897 break;
2898 case FMT_MPEG1:
a6bc5731 2899 if (ENABLE_MPEG1VIDEO_ENCODER || ENABLE_MPEG2VIDEO_ENCODER)
56be950a 2900 mpeg1_encode_picture_header(s, picture_number);
3ada94ba
BF
2901 break;
2902 case FMT_H264:
2903 break;
2904 default:
2905 assert(0);
2906 }
2907 bits= put_bits_count(&s->pb);
2908 s->header_bits= bits - s->last_bits;
2909
2910 for(i=1; i<s->avctx->thread_count; i++){
2911 update_duplicate_context_after_me(s->thread_context[i], s);
2912 }
2913 s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
2914 for(i=1; i<s->avctx->thread_count; i++){
2915 merge_context_after_encode(s, s->thread_context[i]);
2916 }
2917 emms_c();
2918 return 0;
2919}
2920
2921void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
2922 const int intra= s->mb_intra;
2923 int i;
2924
2925 s->dct_count[intra]++;
2926
2927 for(i=0; i<64; i++){
2928 int level= block[i];
2929
2930 if(level){
2931 if(level>0){
2932 s->dct_error_sum[intra][i] += level;
2933 level -= s->dct_offset[intra][i];
2934 if(level<0) level=0;
2935 }else{
2936 s->dct_error_sum[intra][i] -= level;
2937 level += s->dct_offset[intra][i];
2938 if(level>0) level=0;
2939 }
2940 block[i]= level;
2941 }
2942 }
2943}
2944
2945int dct_quantize_trellis_c(MpegEncContext *s,
2946 DCTELEM *block, int n,
2947 int qscale, int *overflow){
2948 const int *qmat;
2949 const uint8_t *scantable= s->intra_scantable.scantable;
2950 const uint8_t *perm_scantable= s->intra_scantable.permutated;
2951 int max=0;
2952 unsigned int threshold1, threshold2;
2953 int bias=0;
2954 int run_tab[65];
2955 int level_tab[65];
2956 int score_tab[65];
2957 int survivor[65];
2958 int survivor_count;
2959 int last_run=0;
2960 int last_level=0;
2961 int last_score= 0;
2962 int last_i;
2963 int coeff[2][64];
2964 int coeff_count[64];
2965 int qmul, qadd, start_i, last_non_zero, i, dc;
2966 const int esc_length= s->ac_esc_length;
2967 uint8_t * length;
2968 uint8_t * last_length;
2969 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
2970
2971 s->dsp.fdct (block);
2972
2973 if(s->dct_error_sum)
2974 s->denoise_dct(s, block);
2975 qmul= qscale*16;
2976 qadd= ((qscale-1)|1)*8;
2977
2978 if (s->mb_intra) {
2979 int q;
2980 if (!s->h263_aic) {
2981 if (n < 4)
2982 q = s->y_dc_scale;
2983 else
2984 q = s->c_dc_scale;
2985 q = q << 3;
2986 } else{
2987 /* For AIC we skip quant/dequant of INTRADC */
2988 q = 1 << 3;
2989 qadd=0;
2990 }
2991
2992 /* note: block[0] is assumed to be positive */
2993 block[0] = (block[0] + (q >> 1)) / q;
2994 start_i = 1;
2995 last_non_zero = 0;
2996 qmat = s->q_intra_matrix[qscale];
2997 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
2998 bias= 1<<(QMAT_SHIFT-1);
2999 length = s->intra_ac_vlc_length;
3000 last_length= s->intra_ac_vlc_last_length;
3001 } else {
3002 start_i = 0;
3003 last_non_zero = -1;
3004 qmat = s->q_inter_matrix[qscale];
3005 length = s->inter_ac_vlc_length;
3006 last_length= s->inter_ac_vlc_last_length;
3007 }
3008 last_i= start_i;
3009
3010 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3011 threshold2= (threshold1<<1);
3012
3013 for(i=63; i>=start_i; i--) {
3014 const int j = scantable[i];
3015 int level = block[j] * qmat[j];
3016
3017 if(((unsigned)(level+threshold1))>threshold2){
3018 last_non_zero = i;
3019 break;
3020 }
3021 }
3022
3023 for(i=start_i; i<=last_non_zero; i++) {
3024 const int j = scantable[i];
3025 int level = block[j] * qmat[j];
3026
3027// if( bias+level >= (1<<(QMAT_SHIFT - 3))
3028// || bias-level >= (1<<(QMAT_SHIFT - 3))){
3029 if(((unsigned)(level+threshold1))>threshold2){
3030 if(level>0){
3031 level= (bias + level)>>QMAT_SHIFT;
3032 coeff[0][i]= level;
3033 coeff[1][i]= level-1;
3034// coeff[2][k]= level-2;
3035 }else{
3036 level= (bias - level)>>QMAT_SHIFT;
3037 coeff[0][i]= -level;
3038 coeff[1][i]= -level+1;
3039// coeff[2][k]= -level+2;
3040 }
3041 coeff_count[i]= FFMIN(level, 2);
3042 assert(coeff_count[i]);
3043 max |=level;
3044 }else{
3045 coeff[0][i]= (level>>31)|1;
3046 coeff_count[i]= 1;
3047 }
3048 }
3049
3050 *overflow= s->max_qcoeff < max; //overflow might have happened
3051
3052 if(last_non_zero < start_i){
3053 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3054 return last_non_zero;
3055 }
3056
3057 score_tab[start_i]= 0;
3058 survivor[0]= start_i;
3059 survivor_count= 1;
3060
3061 for(i=start_i; i<=last_non_zero; i++){
3062 int level_index, j;
3063 const int dct_coeff= FFABS(block[ scantable[i] ]);
3064 const int zero_distoration= dct_coeff*dct_coeff;
3065 int best_score=256*256*256*120;
3066 for(level_index=0; level_index < coeff_count[i]; level_index++){
3067 int distoration;
3068 int level= coeff[level_index][i];
3069 const int alevel= FFABS(level);
3070 int unquant_coeff;
3071
3072 assert(level);
3073
3074 if(s->out_format == FMT_H263){
3075 unquant_coeff= alevel*qmul + qadd;
3076 }else{ //MPEG1
3077 j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
3078 if(s->mb_intra){
3079 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
3080 unquant_coeff = (unquant_coeff - 1) | 1;
3081 }else{
3082 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3083 unquant_coeff = (unquant_coeff - 1) | 1;
3084 }
3085 unquant_coeff<<= 3;
3086 }
3087
3088 distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration;
3089 level+=64;
3090 if((level&(~127)) == 0){
3091 for(j=survivor_count-1; j>=0; j--){
3092 int run= i - survivor[j];
3093 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3094 score += score_tab[i-run];
3095
3096 if(score < best_score){
3097 best_score= score;
3098 run_tab[i+1]= run;
3099 level_tab[i+1]= level-64;
3100 }
3101 }
3102
3103 if(s->out_format == FMT_H263){
3104 for(j=survivor_count-1; j>=0; j--){
3105 int run= i - survivor[j];
3106 int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3107 score += score_tab[i-run];
3108 if(score < last_score){
3109 last_score= score;
3110 last_run= run;
3111 last_level= level-64;
3112 last_i= i+1;
3113 }
3114 }
3115 }
3116 }else{
3117 distoration += esc_length*lambda;
3118 for(j=survivor_count-1; j>=0; j--){
3119 int run= i - survivor[j];
3120 int score= distoration + score_tab[i-run];
3121
3122 if(score < best_score){
3123 best_score= score;
3124 run_tab[i+1]= run;
3125 level_tab[i+1]= level-64;
3126 }
3127 }
3128
3129 if(s->out_format == FMT_H263){
3130 for(j=survivor_count-1; j>=0; j--){
3131 int run= i - survivor[j];
3132 int score= distoration + score_tab[i-run];
3133 if(score < last_score){
3134 last_score= score;
3135 last_run= run;
3136 last_level= level-64;
3137 last_i= i+1;
3138 }
3139 }
3140 }
3141 }
3142 }
3143
3144 score_tab[i+1]= best_score;
3145
3146 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3147 if(last_non_zero <= 27){
3148 for(; survivor_count; survivor_count--){
3149 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3150 break;
3151 }
3152 }else{
3153 for(; survivor_count; survivor_count--){
3154 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3155 break;
3156 }
3157 }
3158
3159 survivor[ survivor_count++ ]= i+1;
3160 }
3161
3162 if(s->out_format != FMT_H263){
3163 last_score= 256*256*256*120;
3164 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3165 int score= score_tab[i];
3166 if(i) score += lambda*2; //FIXME exacter?
3167
3168 if(score < last_score){
3169 last_score= score;
3170 last_i= i;
3171 last_level= level_tab[i];
3172 last_run= run_tab[i];
3173 }
3174 }
3175 }
3176
3177 s->coded_score[n] = last_score;
3178
3179 dc= FFABS(block[0]);
3180 last_non_zero= last_i - 1;
3181 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3182
3183 if(last_non_zero < start_i)
3184 return last_non_zero;
3185
3186 if(last_non_zero == 0 && start_i == 0){
3187 int best_level= 0;
3188 int best_score= dc * dc;
3189
3190 for(i=0; i<coeff_count[0]; i++){
3191 int level= coeff[i][0];
3192 int alevel= FFABS(level);
3193 int unquant_coeff, score, distortion;
3194
3195 if(s->out_format == FMT_H263){
3196 unquant_coeff= (alevel*qmul + qadd)>>3;
3197 }else{ //MPEG1
3198 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3199 unquant_coeff = (unquant_coeff - 1) | 1;
3200 }
3201 unquant_coeff = (unquant_coeff + 4) >> 3;
3202 unquant_coeff<<= 3 + 3;
3203
3204 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3205 level+=64;
3206 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3207 else score= distortion + esc_length*lambda;
3208
3209 if(score < best_score){
3210 best_score= score;
3211 best_level= level - 64;
3212 }
3213 }
3214 block[0]= best_level;
3215 s->coded_score[n] = best_score - dc*dc;
3216 if(best_level == 0) return -1;
3217 else return last_non_zero;
3218 }
3219
3220 i= last_i;
3221 assert(last_level);
3222
3223 block[ perm_scantable[last_non_zero] ]= last_level;
3224 i -= last_run + 1;
3225
3226 for(; i>start_i; i -= run_tab[i] + 1){
3227 block[ perm_scantable[i-1] ]= level_tab[i];
3228 }
3229
3230 return last_non_zero;
3231}
3232
3233//#define REFINE_STATS 1
3234static int16_t basis[64][64];
3235
3236static void build_basis(uint8_t *perm){
3237 int i, j, x, y;
3238 emms_c();
3239 for(i=0; i<8; i++){
3240 for(j=0; j<8; j++){
3241 for(y=0; y<8; y++){
3242 for(x=0; x<8; x++){
3243 double s= 0.25*(1<<BASIS_SHIFT);
3244 int index= 8*i + j;
3245 int perm_index= perm[index];
3246 if(i==0) s*= sqrt(0.5);
3247 if(j==0) s*= sqrt(0.5);
3248 basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
3249 }
3250 }
3251 }
3252 }
3253}
3254
3255static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3256 DCTELEM *block, int16_t *weight, DCTELEM *orig,
3257 int n, int qscale){
3258 int16_t rem[64];
3259 DECLARE_ALIGNED_16(DCTELEM, d1[64]);
3260 const int *qmat;
3261 const uint8_t *scantable= s->intra_scantable.scantable;
3262 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3263// unsigned int threshold1, threshold2;
3264// int bias=0;
3265 int run_tab[65];
3266 int prev_run=0;
3267 int prev_level=0;
3268 int qmul, qadd, start_i, last_non_zero, i, dc;
3269 uint8_t * length;
3270 uint8_t * last_length;
3271 int lambda;
3272 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3273#ifdef REFINE_STATS
3274static int count=0;
3275static int after_last=0;
3276static int to_zero=0;
3277static int from_zero=0;
3278static int raise=0;
3279static int lower=0;
3280static int messed_sign=0;
3281#endif
3282
3283 if(basis[0][0] == 0)
3284 build_basis(s->dsp.idct_permutation);
3285
3286 qmul= qscale*2;
3287 qadd= (qscale-1)|1;
3288 if (s->mb_intra) {
3289 if (!s->h263_aic) {
3290 if (n < 4)
3291 q = s->y_dc_scale;
3292 else
3293 q = s->c_dc_scale;
3294 } else{
3295 /* For AIC we skip quant/dequant of INTRADC */
3296 q = 1;
3297 qadd=0;
3298 }
3299 q <<= RECON_SHIFT-3;
3300 /* note: block[0] is assumed to be positive */
3301 dc= block[0]*q;
3302// block[0] = (block[0] + (q >> 1)) / q;
3303 start_i = 1;
3304 qmat = s->q_intra_matrix[qscale];
3305// if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3306// bias= 1<<(QMAT_SHIFT-1);
3307 length = s->intra_ac_vlc_length;
3308 last_length= s->intra_ac_vlc_last_length;
3309 } else {
3310 dc= 0;
3311 start_i = 0;
3312 qmat = s->q_inter_matrix[qscale];
3313 length = s->inter_ac_vlc_length;
3314 last_length= s->inter_ac_vlc_last_length;
3315 }
3316 last_non_zero = s->block_last_index[n];
3317
3318#ifdef REFINE_STATS
3319{START_TIMER
3320#endif
3321 dc += (1<<(RECON_SHIFT-1));
3322 for(i=0; i<64; i++){
3323 rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
3324 }
3325#ifdef REFINE_STATS
3326STOP_TIMER("memset rem[]")}
3327#endif
3328 sum=0;
3329 for(i=0; i<64; i++){
3330 int one= 36;
3331 int qns=4;
3332 int w;
3333
3334 w= FFABS(weight[i]) + qns*one;
3335 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3336
3337 weight[i] = w;
3338// w=weight[i] = (63*qns + (w/2)) / w;
3339
3340 assert(w>0);
3341 assert(w<(1<<6));
3342 sum += w*w;
3343 }
3344 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3345#ifdef REFINE_STATS
3346{START_TIMER
3347#endif
3348 run=0;
3349 rle_index=0;
3350 for(i=start_i; i<=last_non_zero; i++){
3351 int j= perm_scantable[i];
3352 const int level= block[j];
3353 int coeff;
3354
3355 if(level){
3356 if(level<0) coeff= qmul*level - qadd;
3357 else coeff= qmul*level + qadd;
3358 run_tab[rle_index++]=run;
3359 run=0;
3360
3361 s->dsp.add_8x8basis(rem, basis[j], coeff);
3362 }else{
3363 run++;
3364 }
3365 }
3366#ifdef REFINE_STATS
3367if(last_non_zero>0){
3368STOP_TIMER("init rem[]")
3369}
3370}
3371
3372{START_TIMER
3373#endif
3374 for(;;){
3375 int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
3376 int best_coeff=0;
3377 int best_change=0;
3378 int run2, best_unquant_change=0, analyze_gradient;
3379#ifdef REFINE_STATS
3380{START_TIMER
3381#endif
3382 analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
3383
3384 if(analyze_gradient){
3385#ifdef REFINE_STATS
3386{START_TIMER
3387#endif
3388 for(i=0; i<64; i++){
3389 int w= weight[i];
3390
3391 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3392 }
3393#ifdef REFINE_STATS
3394STOP_TIMER("rem*w*w")}
3395{START_TIMER
3396#endif
3397 s->dsp.fdct(d1);
3398#ifdef REFINE_STATS
3399STOP_TIMER("dct")}
3400#endif
3401 }
3402
3403 if(start_i){
3404 const int level= block[0];
3405 int change, old_coeff;
3406
3407 assert(s->mb_intra);
3408
3409 old_coeff= q*level;
3410
3411 for(change=-1; change<=1; change+=2){
3412 int new_level= level + change;
3413 int score, new_coeff;
3414
3415 new_coeff= q*new_level;
3416 if(new_coeff >= 2048 || new_coeff < 0)
3417 continue;
3418
3419 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3420 if(score<best_score){
3421 best_score= score;
3422 best_coeff= 0;
3423 best_change= change;
3424 best_unquant_change= new_coeff - old_coeff;
3425 }
3426 }
3427 }
3428
3429 run=0;
3430 rle_index=0;
3431 run2= run_tab[rle_index++];
3432 prev_level=0;
3433 prev_run=0;
3434
3435 for(i=start_i; i<64; i++){
3436 int j= perm_scantable[i];
3437 const int level= block[j];
3438 int change, old_coeff;
3439
3440 if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
3441 break;
3442
3443 if(level){
3444 if(level<0) old_coeff= qmul*level - qadd;
3445 else old_coeff= qmul*level + qadd;
3446 run2= run_tab[rle_index++]; //FIXME ! maybe after last
3447 }else{
3448 old_coeff=0;
3449 run2--;
3450 assert(run2>=0 || i >= last_non_zero );
3451 }
3452
3453 for(change=-1; change<=1; change+=2){
3454 int new_level= level + change;
3455 int score, new_coeff, unquant_change;
3456
3457 score=0;
3458 if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
3459 continue;
3460
3461 if(new_level){
3462 if(new_level<0) new_coeff= qmul*new_level - qadd;
3463 else new_coeff= qmul*new_level + qadd;
3464 if(new_coeff >= 2048 || new_coeff <= -2048)
3465 continue;
3466 //FIXME check for overflow
3467
3468 if(level){
3469 if(level < 63 && level > -63){
3470 if(i < last_non_zero)
3471 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
3472 - length[UNI_AC_ENC_INDEX(run, level+64)];
3473 else
3474 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
3475 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
3476 }
3477 }else{
3478 assert(FFABS(new_level)==1);
3479
3480 if(analyze_gradient){
3481 int g= d1[ scantable[i] ];
3482 if(g && (g^new_level) >= 0)
3483 continue;
3484 }
3485
3486 if(i < last_non_zero){
3487 int next_i= i + run2 + 1;
3488 int next_level= block[ perm_scantable[next_i] ] + 64;
3489
3490 if(next_level&(~127))
3491 next_level= 0;
3492
3493 if(next_i < last_non_zero)
3494 score += length[UNI_AC_ENC_INDEX(run, 65)]
3495 + length[UNI_AC_ENC_INDEX(run2, next_level)]
3496 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3497 else
3498 score += length[UNI_AC_ENC_INDEX(run, 65)]
3499 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
3500 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3501 }else{
3502 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
3503 if(prev_level){
3504 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
3505 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
3506 }
3507 }
3508 }
3509 }else{
3510 new_coeff=0;
3511 assert(FFABS(level)==1);
3512
3513 if(i < last_non_zero){
3514 int next_i= i + run2 + 1;
3515 int next_level= block[ perm_scantable[next_i] ] + 64;
3516
3517 if(next_level&(~127))
3518 next_level= 0;
3519
3520 if(next_i < last_non_zero)
3521 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
3522 - length[UNI_AC_ENC_INDEX(run2, next_level)]
3523 - length[UNI_AC_ENC_INDEX(run, 65)];
3524 else
3525 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
3526 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
3527 - length[UNI_AC_ENC_INDEX(run, 65)];
3528 }else{
3529 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
3530 if(prev_level){
3531 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
3532 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
3533 }
3534 }
3535 }
3536
3537 score *= lambda;
3538
3539 unquant_change= new_coeff - old_coeff;
3540 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
3541
3542 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
3543 if(score<best_score){
3544 best_score= score;
3545 best_coeff= i;
3546 best_change= change;
3547 best_unquant_change= unquant_change;
3548 }
3549 }
3550 if(level){
3551 prev_level= level + 64;
3552 if(prev_level&(~127))
3553 prev_level= 0;
3554 prev_run= run;
3555 run=0;
3556 }else{
3557 run++;
3558 }
3559 }
3560#ifdef REFINE_STATS
3561STOP_TIMER("iterative step")}
3562#endif
3563
3564 if(best_change){
3565 int j= perm_scantable[ best_coeff ];
3566
3567 block[j] += best_change;
3568
3569 if(best_coeff > last_non_zero){
3570 last_non_zero= best_coeff;
3571 assert(block[j]);
3572#ifdef REFINE_STATS
3573after_last++;
3574#endif
3575 }else{
3576#ifdef REFINE_STATS
3577if(block[j]){
3578 if(block[j] - best_change){
3579 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
3580 raise++;
3581 }else{
3582 lower++;
3583 }
3584 }else{
3585 from_zero++;
3586 }
3587}else{
3588 to_zero++;
3589}
3590#endif
3591 for(; last_non_zero>=start_i; last_non_zero--){
3592 if(block[perm_scantable[last_non_zero]])
3593 break;
3594 }
3595 }
3596#ifdef REFINE_STATS
3597count++;
3598if(256*256*256*64 % count == 0){
3599 printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
3600}
3601#endif
3602 run=0;
3603 rle_index=0;
3604 for(i=start_i; i<=last_non_zero; i++){
3605 int j= perm_scantable[i];
3606 const int level= block[j];
3607
3608 if(level){
3609 run_tab[rle_index++]=run;
3610 run=0;
3611 }else{
3612 run++;
3613 }
3614 }
3615
3616 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
3617 }else{
3618 break;
3619 }
3620 }
3621#ifdef REFINE_STATS
3622if(last_non_zero>0){
3623STOP_TIMER("iterative search")
3624}
3625}
3626#endif
3627
3628 return last_non_zero;
3629}
3630
3631int dct_quantize_c(MpegEncContext *s,
3632 DCTELEM *block, int n,
3633 int qscale, int *overflow)
3634{
3635 int i, j, level, last_non_zero, q, start_i;
3636 const int *qmat;
3637 const uint8_t *scantable= s->intra_scantable.scantable;
3638 int bias;
3639 int max=0;
3640 unsigned int threshold1, threshold2;
3641
3642 s->dsp.fdct (block);
3643
3644 if(s->dct_error_sum)
3645 s->denoise_dct(s, block);
3646
3647 if (s->mb_intra) {
3648 if (!s->h263_aic) {
3649 if (n < 4)
3650 q = s->y_dc_scale;
3651 else
3652 q = s->c_dc_scale;
3653 q = q << 3;
3654 } else
3655 /* For AIC we skip quant/dequant of INTRADC */
3656 q = 1 << 3;
3657
3658 /* note: block[0] is assumed to be positive */
3659 block[0] = (block[0] + (q >> 1)) / q;
3660 start_i = 1;
3661 last_non_zero = 0;
3662 qmat = s->q_intra_matrix[qscale];
3663 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3664 } else {
3665 start_i = 0;
3666 last_non_zero = -1;
3667 qmat = s->q_inter_matrix[qscale];
3668 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3669 }
3670 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3671 threshold2= (threshold1<<1);
3672 for(i=63;i>=start_i;i--) {
3673 j = scantable[i];
3674 level = block[j] * qmat[j];
3675
3676 if(((unsigned)(level+threshold1))>threshold2){
3677 last_non_zero = i;
3678 break;
3679 }else{
3680 block[j]=0;
3681 }
3682 }
3683 for(i=start_i; i<=last_non_zero; i++) {
3684 j = scantable[i];
3685 level = block[j] * qmat[j];
3686
3687// if( bias+level >= (1<<QMAT_SHIFT)
3688// || bias-level >= (1<<QMAT_SHIFT)){
3689 if(((unsigned)(level+threshold1))>threshold2){
3690 if(level>0){
3691 level= (bias + level)>>QMAT_SHIFT;
3692 block[j]= level;
3693 }else{
3694 level= (bias - level)>>QMAT_SHIFT;
3695 block[j]= -level;
3696 }
3697 max |=level;
3698 }else{
3699 block[j]=0;
3700 }
3701 }
3702 *overflow= s->max_qcoeff < max; //overflow might have happened
3703
3704 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3705 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
3706 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
3707
3708 return last_non_zero;
3709}
3710
3711AVCodec h263_encoder = {
3712 "h263",
3713 CODEC_TYPE_VIDEO,
3714 CODEC_ID_H263,
3715 sizeof(MpegEncContext),
3716 MPV_encode_init,
3717 MPV_encode_picture,
3718 MPV_encode_end,
3719 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3720};
3721
3722AVCodec h263p_encoder = {
3723 "h263p",
3724 CODEC_TYPE_VIDEO,
3725 CODEC_ID_H263P,
3726 sizeof(MpegEncContext),
3727 MPV_encode_init,
3728 MPV_encode_picture,
3729 MPV_encode_end,
3730 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3731};
3732
3733AVCodec flv_encoder = {
3734 "flv",
3735 CODEC_TYPE_VIDEO,
3736 CODEC_ID_FLV1,
3737 sizeof(MpegEncContext),
3738 MPV_encode_init,
3739 MPV_encode_picture,
3740 MPV_encode_end,
3741 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3742};
3743
3744AVCodec rv10_encoder = {
3745 "rv10",
3746 CODEC_TYPE_VIDEO,
3747 CODEC_ID_RV10,
3748 sizeof(MpegEncContext),
3749 MPV_encode_init,
3750 MPV_encode_picture,
3751 MPV_encode_end,
3752 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3753};
3754
3755AVCodec rv20_encoder = {
3756 "rv20",
3757 CODEC_TYPE_VIDEO,
3758 CODEC_ID_RV20,
3759 sizeof(MpegEncContext),
3760 MPV_encode_init,
3761 MPV_encode_picture,
3762 MPV_encode_end,
3763 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3764};
3765
3766AVCodec mpeg4_encoder = {
3767 "mpeg4",
3768 CODEC_TYPE_VIDEO,
3769 CODEC_ID_MPEG4,
3770 sizeof(MpegEncContext),
3771 MPV_encode_init,
3772 MPV_encode_picture,
3773 MPV_encode_end,
3774 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3775 .capabilities= CODEC_CAP_DELAY,
3776};
3777
3778AVCodec msmpeg4v1_encoder = {
3779 "msmpeg4v1",
3780 CODEC_TYPE_VIDEO,
3781 CODEC_ID_MSMPEG4V1,
3782 sizeof(MpegEncContext),
3783 MPV_encode_init,
3784 MPV_encode_picture,
3785 MPV_encode_end,
3786 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3787};
3788
3789AVCodec msmpeg4v2_encoder = {
3790 "msmpeg4v2",
3791 CODEC_TYPE_VIDEO,
3792 CODEC_ID_MSMPEG4V2,
3793 sizeof(MpegEncContext),
3794 MPV_encode_init,
3795 MPV_encode_picture,
3796 MPV_encode_end,
3797 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3798};
3799
3800AVCodec msmpeg4v3_encoder = {
3801 "msmpeg4",
3802 CODEC_TYPE_VIDEO,
3803 CODEC_ID_MSMPEG4V3,
3804 sizeof(MpegEncContext),
3805 MPV_encode_init,
3806 MPV_encode_picture,
3807 MPV_encode_end,
3808 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3809};
3810
3811AVCodec wmv1_encoder = {
3812 "wmv1",
3813 CODEC_TYPE_VIDEO,
3814 CODEC_ID_WMV1,
3815 sizeof(MpegEncContext),
3816 MPV_encode_init,
3817 MPV_encode_picture,
3818 MPV_encode_end,
3819 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3820};