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