mpegvideo_enc.c 149 KB
Newer Older
Benoit Fouet's avatar
Benoit Fouet committed
1 2
/*
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Fabrice Bellard
Benoit Fouet's avatar
Benoit Fouet committed
4 5
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
 *
6 7
 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
 *
8
 * This file is part of Libav.
Benoit Fouet's avatar
Benoit Fouet committed
9
 *
10
 * Libav is free software; you can redistribute it and/or
Benoit Fouet's avatar
Benoit Fouet committed
11 12 13 14
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
15
 * Libav is distributed in the hope that it will be useful,
Benoit Fouet's avatar
Benoit Fouet committed
16 17 18 19 20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with Libav; if not, write to the Free Software
Benoit Fouet's avatar
Benoit Fouet committed
22 23 24 25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
26
 * @file
Benoit Fouet's avatar
Benoit Fouet committed
27 28 29
 * The simplest mpeg encoder (well, it was the simplest!).
 */

30
#include "libavutil/internal.h"
31
#include "libavutil/intmath.h"
32
#include "libavutil/mathematics.h"
33
#include "libavutil/pixdesc.h"
34
#include "libavutil/opt.h"
Benoit Fouet's avatar
Benoit Fouet committed
35
#include "avcodec.h"
36
#include "dct.h"
Benoit Fouet's avatar
Benoit Fouet committed
37
#include "dsputil.h"
38
#include "mpeg12.h"
Benoit Fouet's avatar
Benoit Fouet committed
39
#include "mpegvideo.h"
40
#include "h261.h"
41
#include "h263.h"
42
#include "mathops.h"
Benoit Fouet's avatar
Benoit Fouet committed
43 44 45
#include "mjpegenc.h"
#include "msmpeg4.h"
#include "faandct.h"
46
#include "thread.h"
47
#include "aandcttab.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
48
#include "flv.h"
49
#include "mpeg4video.h"
50
#include "internal.h"
51
#include "bytestream.h"
Benoit Fouet's avatar
Benoit Fouet committed
52 53 54
#include <limits.h>

static int encode_picture(MpegEncContext *s, int picture_number);
Diego Biurrun's avatar
Diego Biurrun committed
55
static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
Benoit Fouet's avatar
Benoit Fouet committed
56
static int sse_mb(MpegEncContext *s);
Diego Biurrun's avatar
Diego Biurrun committed
57 58
static void denoise_dct_c(MpegEncContext *s, int16_t *block);
static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
Benoit Fouet's avatar
Benoit Fouet committed
59

Aneesh Dogra's avatar
Aneesh Dogra committed
60 61
static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
Benoit Fouet's avatar
Benoit Fouet committed
62

63 64 65 66 67
const AVOption ff_mpv_generic_options[] = {
    FF_MPV_COMMON_OPTS
    { NULL },
};

Aneesh Dogra's avatar
Aneesh Dogra committed
68 69 70 71
void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
                       uint16_t (*qmat16)[2][64],
                       const uint16_t *quant_matrix,
                       int bias, int qmin, int qmax, int intra)
Benoit Fouet's avatar
Benoit Fouet committed
72 73
{
    int qscale;
Aneesh Dogra's avatar
Aneesh Dogra committed
74
    int shift = 0;
Benoit Fouet's avatar
Benoit Fouet committed
75

Aneesh Dogra's avatar
Aneesh Dogra committed
76
    for (qscale = qmin; qscale <= qmax; qscale++) {
Benoit Fouet's avatar
Benoit Fouet committed
77
        int i;
Mans Rullgard's avatar
Mans Rullgard committed
78
        if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
79 80
            dsp->fdct == ff_jpeg_fdct_islow_10 ||
            dsp->fdct == ff_faandct) {
Aneesh Dogra's avatar
Aneesh Dogra committed
81 82 83 84 85 86 87
            for (i = 0; i < 64; i++) {
                const int j = dsp->idct_permutation[i];
                /* 16 <= qscale * quant_matrix[i] <= 7905
                 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
                 *             19952 <=              x  <= 249205026
                 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
                 *           3444240 >= (1 << 36) / (x) >= 275 */
Benoit Fouet's avatar
Benoit Fouet committed
88 89

                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
Aneesh Dogra's avatar
Aneesh Dogra committed
90
                                        (qscale * quant_matrix[j]));
Benoit Fouet's avatar
Benoit Fouet committed
91
            }
92
        } else if (dsp->fdct == ff_fdct_ifast) {
Aneesh Dogra's avatar
Aneesh Dogra committed
93 94 95 96 97 98 99
            for (i = 0; i < 64; i++) {
                const int j = dsp->idct_permutation[i];
                /* 16 <= qscale * quant_matrix[i] <= 7905
                 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
                 *             19952 <=              x  <= 249205026
                 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
                 *           3444240 >= (1 << 36) / (x) >= 275 */
Benoit Fouet's avatar
Benoit Fouet committed
100 101

                qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
Aneesh Dogra's avatar
Aneesh Dogra committed
102 103
                                        (ff_aanscales[i] * qscale *
                                         quant_matrix[j]));
Benoit Fouet's avatar
Benoit Fouet committed
104 105
            }
        } else {
Aneesh Dogra's avatar
Aneesh Dogra committed
106 107
            for (i = 0; i < 64; i++) {
                const int j = dsp->idct_permutation[i];
Benoit Fouet's avatar
Benoit Fouet committed
108
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
Aneesh Dogra's avatar
Aneesh Dogra committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                 * Assume x = qscale * quant_matrix[i]
                 * So             16 <=              x  <= 7905
                 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
                 * so          32768 >= (1 << 19) / (x) >= 67 */
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
                                        (qscale * quant_matrix[j]));
                //qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) /
                //                    (qscale * quant_matrix[i]);
                qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
                                       (qscale * quant_matrix[j]);

                if (qmat16[qscale][0][i] == 0 ||
                    qmat16[qscale][0][i] == 128 * 256)
                    qmat16[qscale][0][i] = 128 * 256 - 1;
                qmat16[qscale][1][i] =
                    ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
                                qmat16[qscale][0][i]);
Benoit Fouet's avatar
Benoit Fouet committed
126 127 128
            }
        }

Aneesh Dogra's avatar
Aneesh Dogra committed
129 130
        for (i = intra; i < 64; i++) {
            int64_t max = 8191;
131
            if (dsp->fdct == ff_fdct_ifast) {
Aneesh Dogra's avatar
Aneesh Dogra committed
132
                max = (8191LL * ff_aanscales[i]) >> 14;
Benoit Fouet's avatar
Benoit Fouet committed
133
            }
Aneesh Dogra's avatar
Aneesh Dogra committed
134
            while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
Benoit Fouet's avatar
Benoit Fouet committed
135 136 137 138
                shift++;
            }
        }
    }
Aneesh Dogra's avatar
Aneesh Dogra committed
139 140 141 142
    if (shift) {
        av_log(NULL, AV_LOG_INFO,
               "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
               QMAT_SHIFT - shift);
Benoit Fouet's avatar
Benoit Fouet committed
143 144 145
    }
}

Aneesh Dogra's avatar
Aneesh Dogra committed
146 147 148 149 150
static inline void update_qscale(MpegEncContext *s)
{
    s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
                (FF_LAMBDA_SHIFT + 7);
    s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
Benoit Fouet's avatar
Benoit Fouet committed
151

Aneesh Dogra's avatar
Aneesh Dogra committed
152 153
    s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
                 FF_LAMBDA_SHIFT;
Benoit Fouet's avatar
Benoit Fouet committed
154 155
}

Aneesh Dogra's avatar
Aneesh Dogra committed
156 157
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
{
Benoit Fouet's avatar
Benoit Fouet committed
158 159
    int i;

Aneesh Dogra's avatar
Aneesh Dogra committed
160
    if (matrix) {
Benoit Fouet's avatar
Benoit Fouet committed
161
        put_bits(pb, 1, 1);
Aneesh Dogra's avatar
Aneesh Dogra committed
162 163
        for (i = 0; i < 64; i++) {
            put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
Benoit Fouet's avatar
Benoit Fouet committed
164
        }
Aneesh Dogra's avatar
Aneesh Dogra committed
165
    } else
Benoit Fouet's avatar
Benoit Fouet committed
166 167 168
        put_bits(pb, 1, 0);
}

169 170 171
/**
 * init s->current_picture.qscale_table from s->lambda_table
 */
Aneesh Dogra's avatar
Aneesh Dogra committed
172 173
void ff_init_qscale_tab(MpegEncContext *s)
{
174
    int8_t * const qscale_table = s->current_picture.qscale_table;
175 176
    int i;

Aneesh Dogra's avatar
Aneesh Dogra committed
177 178 179 180 181
    for (i = 0; i < s->mb_num; i++) {
        unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
        int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
        qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
                                                  s->avctx->qmax);
182 183 184
    }
}

Aneesh Dogra's avatar
Aneesh Dogra committed
185 186 187
static void update_duplicate_context_after_me(MpegEncContext *dst,
                                              MpegEncContext *src)
{
Benoit Fouet's avatar
Benoit Fouet committed
188 189 190 191 192 193 194 195 196 197
#define COPY(a) dst->a= src->a
    COPY(pict_type);
    COPY(current_picture);
    COPY(f_code);
    COPY(b_code);
    COPY(qscale);
    COPY(lambda);
    COPY(lambda2);
    COPY(picture_in_gop_number);
    COPY(gop_picture_number);
Aneesh Dogra's avatar
Aneesh Dogra committed
198 199 200
    COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
    COPY(progressive_frame);    // FIXME don't set in encode_header
    COPY(partitioned_frame);    // FIXME don't set in encode_header
Benoit Fouet's avatar
Benoit Fouet committed
201 202 203 204
#undef COPY
}

/**
205
 * Set the given MpegEncContext to defaults for encoding.
Benoit Fouet's avatar
Benoit Fouet committed
206 207
 * the changed fields will not depend upon the prior state of the MpegEncContext.
 */
Aneesh Dogra's avatar
Aneesh Dogra committed
208 209
static void MPV_encode_defaults(MpegEncContext *s)
{
Benoit Fouet's avatar
Benoit Fouet committed
210
    int i;
211
    ff_MPV_common_defaults(s);
Benoit Fouet's avatar
Benoit Fouet committed
212

Aneesh Dogra's avatar
Aneesh Dogra committed
213 214
    for (i = -16; i < 16; i++) {
        default_fcode_tab[i + MAX_MV] = 1;
Benoit Fouet's avatar
Benoit Fouet committed
215
    }
Aneesh Dogra's avatar
Aneesh Dogra committed
216 217
    s->me.mv_penalty = default_mv_penalty;
    s->fcode_tab     = default_fcode_tab;
Benoit Fouet's avatar
Benoit Fouet committed
218 219 220
}

/* init video encoder */
221
av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
Benoit Fouet's avatar
Benoit Fouet committed
222 223
{
    MpegEncContext *s = avctx->priv_data;
224
    int i, ret;
Benoit Fouet's avatar
Benoit Fouet committed
225 226 227 228 229
    int chroma_h_shift, chroma_v_shift;

    MPV_encode_defaults(s);

    switch (avctx->codec_id) {
230
    case AV_CODEC_ID_MPEG2VIDEO:
231 232
        if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
            avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
Aneesh Dogra's avatar
Aneesh Dogra committed
233 234
            av_log(avctx, AV_LOG_ERROR,
                   "only YUV420 and YUV422 are supported\n");
Benoit Fouet's avatar
Benoit Fouet committed
235 236 237
            return -1;
        }
        break;
238
    case AV_CODEC_ID_LJPEG:
239 240 241 242 243 244 245
        if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
            avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
            avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
            avctx->pix_fmt != AV_PIX_FMT_BGRA     &&
            ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
              avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
              avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
Aneesh Dogra's avatar
Aneesh Dogra committed
246
             avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
247 248 249 250
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
            return -1;
        }
        break;
251
    case AV_CODEC_ID_MJPEG:
252 253 254 255
        if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
            avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
            ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
              avctx->pix_fmt != AV_PIX_FMT_YUV422P) ||
Aneesh Dogra's avatar
Aneesh Dogra committed
256
             avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
Benoit Fouet's avatar
Benoit Fouet committed
257 258 259 260 261
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
            return -1;
        }
        break;
    default:
262
        if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
Benoit Fouet's avatar
Benoit Fouet committed
263 264 265 266 267 268
            av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
            return -1;
        }
    }

    switch (avctx->pix_fmt) {
269 270
    case AV_PIX_FMT_YUVJ422P:
    case AV_PIX_FMT_YUV422P:
Benoit Fouet's avatar
Benoit Fouet committed
271 272
        s->chroma_format = CHROMA_422;
        break;
273 274
    case AV_PIX_FMT_YUVJ420P:
    case AV_PIX_FMT_YUV420P:
Benoit Fouet's avatar
Benoit Fouet committed
275 276 277 278 279 280
    default:
        s->chroma_format = CHROMA_420;
        break;
    }

    s->bit_rate = avctx->bit_rate;
Aneesh Dogra's avatar
Aneesh Dogra committed
281 282 283 284 285 286 287 288 289 290 291 292
    s->width    = avctx->width;
    s->height   = avctx->height;
    if (avctx->gop_size > 600 &&
        avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
        av_log(avctx, AV_LOG_ERROR,
               "Warning keyframe interval too large! reducing it ...\n");
        avctx->gop_size = 600;
    }
    s->gop_size     = avctx->gop_size;
    s->avctx        = avctx;
    s->flags        = avctx->flags;
    s->flags2       = avctx->flags2;
293 294 295 296
    if (avctx->max_b_frames > MAX_B_FRAMES) {
        av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
               "is %d.\n", MAX_B_FRAMES);
    }
Aneesh Dogra's avatar
Aneesh Dogra committed
297 298 299 300 301 302 303
    s->max_b_frames = avctx->max_b_frames;
    s->codec_id     = avctx->codec->id;
    s->strict_std_compliance = avctx->strict_std_compliance;
    s->quarter_sample     = (avctx->flags & CODEC_FLAG_QPEL) != 0;
    s->mpeg_quant         = avctx->mpeg_quant;
    s->rtp_mode           = !!avctx->rtp_payload_size;
    s->intra_dc_precision = avctx->intra_dc_precision;
Benoit Fouet's avatar
Benoit Fouet committed
304 305 306 307
    s->user_specified_pts = AV_NOPTS_VALUE;

    if (s->gop_size <= 1) {
        s->intra_only = 1;
Aneesh Dogra's avatar
Aneesh Dogra committed
308
        s->gop_size   = 12;
Benoit Fouet's avatar
Benoit Fouet committed
309 310 311 312 313 314 315 316 317
    } else {
        s->intra_only = 0;
    }

    s->me_method = avctx->me_method;

    /* Fixed QSCALE */
    s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);

Aneesh Dogra's avatar
Aneesh Dogra committed
318 319 320 321 322 323
    s->adaptive_quant = (s->avctx->lumi_masking ||
                         s->avctx->dark_masking ||
                         s->avctx->temporal_cplx_masking ||
                         s->avctx->spatial_cplx_masking  ||
                         s->avctx->p_masking      ||
                         s->avctx->border_masking ||
324
                         (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
Aneesh Dogra's avatar
Aneesh Dogra committed
325
                        !s->fixed_qscale;
Benoit Fouet's avatar
Benoit Fouet committed
326

Aneesh Dogra's avatar
Aneesh Dogra committed
327
    s->loop_filter      = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
Benoit Fouet's avatar
Benoit Fouet committed
328

Aneesh Dogra's avatar
Aneesh Dogra committed
329 330 331 332
    if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
        av_log(avctx, AV_LOG_ERROR,
               "a vbv buffer size is needed, "
               "for encoding with a maximum bitrate\n");
Benoit Fouet's avatar
Benoit Fouet committed
333 334 335
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
336 337 338
    if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
        av_log(avctx, AV_LOG_INFO,
               "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
Benoit Fouet's avatar
Benoit Fouet committed
339 340
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
341
    if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
Benoit Fouet's avatar
Benoit Fouet committed
342 343 344 345
        av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
346
    if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
Benoit Fouet's avatar
Benoit Fouet committed
347 348 349 350
        av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
351 352 353 354 355
    if (avctx->rc_max_rate &&
        avctx->rc_max_rate == avctx->bit_rate &&
        avctx->rc_max_rate != avctx->rc_min_rate) {
        av_log(avctx, AV_LOG_INFO,
               "impossible bitrate constraints, this will fail\n");
356 357
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
358 359 360
    if (avctx->rc_buffer_size &&
        avctx->bit_rate * (int64_t)avctx->time_base.num >
            avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
Benoit Fouet's avatar
Benoit Fouet committed
361 362 363 364
        av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
365 366 367 368 369
    if (!s->fixed_qscale &&
        avctx->bit_rate * av_q2d(avctx->time_base) >
            avctx->bit_rate_tolerance) {
        av_log(avctx, AV_LOG_ERROR,
               "bitrate tolerance too small for bitrate\n");
Benoit Fouet's avatar
Benoit Fouet committed
370 371 372
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
373 374
    if (s->avctx->rc_max_rate &&
        s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
375 376
        (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
         s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
Aneesh Dogra's avatar
Aneesh Dogra committed
377 378 379 380 381
        90000LL * (avctx->rc_buffer_size - 1) >
            s->avctx->rc_max_rate * 0xFFFFLL) {
        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");
Benoit Fouet's avatar
Benoit Fouet committed
382 383
    }

384 385 386
    if ((s->flags & CODEC_FLAG_4MV)  && s->codec_id != AV_CODEC_ID_MPEG4 &&
        s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
        s->codec_id != AV_CODEC_ID_FLV1) {
Benoit Fouet's avatar
Benoit Fouet committed
387 388 389 390
        av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
391 392 393
    if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
        av_log(avctx, AV_LOG_ERROR,
               "OBMC is only supported with simple mb decision\n");
Benoit Fouet's avatar
Benoit Fouet committed
394 395 396
        return -1;
    }

397
    if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
Benoit Fouet's avatar
Benoit Fouet committed
398 399 400 401
        av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
402
    if (s->max_b_frames                    &&
403 404 405
        s->codec_id != AV_CODEC_ID_MPEG4      &&
        s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
        s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
Benoit Fouet's avatar
Benoit Fouet committed
406 407 408 409
        av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
        return -1;
    }

410 411 412
    if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
         s->codec_id == AV_CODEC_ID_H263  ||
         s->codec_id == AV_CODEC_ID_H263P) &&
Aneesh Dogra's avatar
Aneesh Dogra committed
413 414 415 416
        (avctx->sample_aspect_ratio.num > 255 ||
         avctx->sample_aspect_ratio.den > 255)) {
        av_log(avctx, AV_LOG_ERROR,
               "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
417 418 419 420
               avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
        return -1;
    }

421
    if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
422
        s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
Benoit Fouet's avatar
Benoit Fouet committed
423 424 425 426
        av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
427
    // FIXME mpeg2 uses that too
428
    if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
Aneesh Dogra's avatar
Aneesh Dogra committed
429 430
        av_log(avctx, AV_LOG_ERROR,
               "mpeg2 style quantization not supported by codec\n");
Benoit Fouet's avatar
Benoit Fouet committed
431 432 433
        return -1;
    }

434
    if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
Benoit Fouet's avatar
Benoit Fouet committed
435 436 437 438
        av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
        return -1;
    }

439
    if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
Aneesh Dogra's avatar
Aneesh Dogra committed
440
        s->avctx->mb_decision != FF_MB_DECISION_RD) {
Benoit Fouet's avatar
Benoit Fouet committed
441 442 443 444
        av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
445 446 447 448 449
    if (s->avctx->scenechange_threshold < 1000000000 &&
        (s->flags & CODEC_FLAG_CLOSED_GOP)) {
        av_log(avctx, AV_LOG_ERROR,
               "closed gop with scene change detection are not supported yet, "
               "set threshold to 1000000000\n");
Benoit Fouet's avatar
Benoit Fouet committed
450 451 452
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
453
    if (s->flags & CODEC_FLAG_LOW_DELAY) {
454
        if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
Aneesh Dogra's avatar
Aneesh Dogra committed
455 456
            av_log(avctx, AV_LOG_ERROR,
                  "low delay forcing is only available for mpeg2\n");
Benoit Fouet's avatar
Benoit Fouet committed
457 458
            return -1;
        }
Aneesh Dogra's avatar
Aneesh Dogra committed
459 460 461
        if (s->max_b_frames != 0) {
            av_log(avctx, AV_LOG_ERROR,
                   "b frames cannot be used with low delay\n");
Benoit Fouet's avatar
Benoit Fouet committed
462 463 464 465
            return -1;
        }
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
466 467 468 469
    if (s->q_scale_type == 1) {
        if (avctx->qmax > 12) {
            av_log(avctx, AV_LOG_ERROR,
                   "non linear quant only supports qmax <= 12 currently\n");
Benoit Fouet's avatar
Benoit Fouet committed
470 471 472 473
            return -1;
        }
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
474
    if (s->avctx->thread_count > 1         &&
475 476 477 478
        s->codec_id != AV_CODEC_ID_MPEG4      &&
        s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
        s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
        (s->codec_id != AV_CODEC_ID_H263P)) {
Aneesh Dogra's avatar
Aneesh Dogra committed
479 480
        av_log(avctx, AV_LOG_ERROR,
               "multi threaded encoding not supported by codec\n");
Benoit Fouet's avatar
Benoit Fouet committed
481 482 483
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
484 485 486 487
    if (s->avctx->thread_count < 1) {
        av_log(avctx, AV_LOG_ERROR,
               "automatic thread number detection not supported by codec,"
               "patch welcome\n");
488 489 490
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
491 492
    if (s->avctx->thread_count > 1)
        s->rtp_mode = 1;
Benoit Fouet's avatar
Benoit Fouet committed
493

Aneesh Dogra's avatar
Aneesh Dogra committed
494
    if (!avctx->time_base.den || !avctx->time_base.num) {
Benoit Fouet's avatar
Benoit Fouet committed
495 496 497 498
        av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
499 500 501 502
    i = (INT_MAX / 2 + 128) >> 8;
    if (avctx->mb_threshold >= i) {
        av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
               i - 1);
Benoit Fouet's avatar
Benoit Fouet committed
503 504 505
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
506 507 508
    if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
        av_log(avctx, AV_LOG_INFO,
               "notice: b_frame_strategy only affects the first pass\n");
Benoit Fouet's avatar
Benoit Fouet committed
509 510 511
        avctx->b_frame_strategy = 0;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
512 513
    i = av_gcd(avctx->time_base.den, avctx->time_base.num);
    if (i > 1) {
Benoit Fouet's avatar
Benoit Fouet committed
514 515 516
        av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
        avctx->time_base.den /= i;
        avctx->time_base.num /= i;
Aneesh Dogra's avatar
Aneesh Dogra committed
517
        //return -1;
Benoit Fouet's avatar
Benoit Fouet committed
518 519
    }

520 521
    if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
        s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG) {
Aneesh Dogra's avatar
Aneesh Dogra committed
522 523 524 525 526 527 528
        // (a + x * 3 / 8) / x
        s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
        s->inter_quant_bias = 0;
    } else {
        s->intra_quant_bias = 0;
        // (a - x / 4) / x
        s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
Benoit Fouet's avatar
Benoit Fouet committed
529 530
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
531 532 533 534 535
    if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
        s->intra_quant_bias = avctx->intra_quant_bias;
    if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
        s->inter_quant_bias = avctx->inter_quant_bias;

536 537
    av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
                                     &chroma_v_shift);
Aneesh Dogra's avatar
Aneesh Dogra committed
538

539
    if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
Aneesh Dogra's avatar
Aneesh Dogra committed
540 541 542 543 544 545
        s->avctx->time_base.den > (1 << 16) - 1) {
        av_log(avctx, AV_LOG_ERROR,
               "timebase %d/%d not supported by MPEG 4 standard, "
               "the maximum admitted value for the timebase denominator "
               "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
               (1 << 16) - 1);
Benoit Fouet's avatar
Benoit Fouet committed
546 547 548 549
        return -1;
    }
    s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;

Aneesh Dogra's avatar
Aneesh Dogra committed
550
    switch (avctx->codec->id) {
551
    case AV_CODEC_ID_MPEG1VIDEO:
Benoit Fouet's avatar
Benoit Fouet committed
552
        s->out_format = FMT_MPEG1;
Aneesh Dogra's avatar
Aneesh Dogra committed
553 554
        s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
        avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
Benoit Fouet's avatar
Benoit Fouet committed
555
        break;
556
    case AV_CODEC_ID_MPEG2VIDEO:
Benoit Fouet's avatar
Benoit Fouet committed
557
        s->out_format = FMT_MPEG1;
Aneesh Dogra's avatar
Aneesh Dogra committed
558 559 560
        s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
        avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
        s->rtp_mode   = 1;
Benoit Fouet's avatar
Benoit Fouet committed
561
        break;
562 563
    case AV_CODEC_ID_LJPEG:
    case AV_CODEC_ID_MJPEG:
Benoit Fouet's avatar
Benoit Fouet committed
564 565
        s->out_format = FMT_MJPEG;
        s->intra_only = 1; /* force intra only for jpeg */
566
        if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
567
            avctx->pix_fmt   == AV_PIX_FMT_BGRA) {
Michael Niedermayer's avatar
Michael Niedermayer committed
568 569 570
            s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
            s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
            s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
Aneesh Dogra's avatar
Aneesh Dogra committed
571
        } else {
572
            s->mjpeg_vsample[0] = 2;
Aneesh Dogra's avatar
Aneesh Dogra committed
573 574
            s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
            s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
575
            s->mjpeg_hsample[0] = 2;
Aneesh Dogra's avatar
Aneesh Dogra committed
576 577
            s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
            s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
Michael Niedermayer's avatar
Michael Niedermayer committed
578
        }
Aneesh Dogra's avatar
Aneesh Dogra committed
579 580
        if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
            ff_mjpeg_encode_init(s) < 0)
Benoit Fouet's avatar
Benoit Fouet committed
581
            return -1;
Aneesh Dogra's avatar
Aneesh Dogra committed
582 583
        avctx->delay = 0;
        s->low_delay = 1;
Benoit Fouet's avatar
Benoit Fouet committed
584
        break;
585
    case AV_CODEC_ID_H261:
Aneesh Dogra's avatar
Aneesh Dogra committed
586 587
        if (!CONFIG_H261_ENCODER)
            return -1;
Benoit Fouet's avatar
Benoit Fouet committed
588
        if (ff_h261_get_picture_format(s->width, s->height) < 0) {
Aneesh Dogra's avatar
Aneesh Dogra committed
589 590 591 592
            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);
Benoit Fouet's avatar
Benoit Fouet committed
593 594 595
            return -1;
        }
        s->out_format = FMT_H261;
Aneesh Dogra's avatar
Aneesh Dogra committed
596 597
        avctx->delay  = 0;
        s->low_delay  = 1;
Benoit Fouet's avatar
Benoit Fouet committed
598
        break;
599
    case AV_CODEC_ID_H263:
Aneesh Dogra's avatar
Aneesh Dogra committed
600 601
        if (!CONFIG_H263_ENCODER)
        return -1;
602
        if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
Aneesh Dogra's avatar
Aneesh Dogra committed
603 604 605 606 607 608
                             s->width, s->height) == 8) {
            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);
Benoit Fouet's avatar
Benoit Fouet committed
609 610 611
            return -1;
        }
        s->out_format = FMT_H263;
Aneesh Dogra's avatar
Aneesh Dogra committed
612 613
        avctx->delay  = 0;
        s->low_delay  = 1;
Benoit Fouet's avatar
Benoit Fouet committed
614
        break;
615
    case AV_CODEC_ID_H263P:
Benoit Fouet's avatar
Benoit Fouet committed
616
        s->out_format = FMT_H263;
Aneesh Dogra's avatar
Aneesh Dogra committed
617
        s->h263_plus  = 1;
Benoit Fouet's avatar
Benoit Fouet committed
618
        /* Fx */
Aneesh Dogra's avatar
Aneesh Dogra committed
619 620 621 622
        s->h263_aic        = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
        s->modified_quant  = s->h263_aic;
        s->loop_filter     = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
        s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
Benoit Fouet's avatar
Benoit Fouet committed
623 624 625

        /* /Fx */
        /* These are just to be sure */
Aneesh Dogra's avatar
Aneesh Dogra committed
626 627
        avctx->delay = 0;
        s->low_delay = 1;
Benoit Fouet's avatar
Benoit Fouet committed
628
        break;
629
    case AV_CODEC_ID_FLV1:
Aneesh Dogra's avatar
Aneesh Dogra committed
630 631
        s->out_format      = FMT_H263;
        s->h263_flv        = 2; /* format = 1; 11-bit codes */
Benoit Fouet's avatar
Benoit Fouet committed
632
        s->unrestricted_mv = 1;
Aneesh Dogra's avatar
Aneesh Dogra committed
633 634 635
        s->rtp_mode  = 0; /* don't allow GOB */
        avctx->delay = 0;
        s->low_delay = 1;
Benoit Fouet's avatar
Benoit Fouet committed
636
        break;
637
    case AV_CODEC_ID_RV10:
Benoit Fouet's avatar
Benoit Fouet committed
638
        s->out_format = FMT_H263;
Aneesh Dogra's avatar
Aneesh Dogra committed
639 640
        avctx->delay  = 0;
        s->low_delay  = 1;
Benoit Fouet's avatar
Benoit Fouet committed
641
        break;
642
    case AV_CODEC_ID_RV20:
Aneesh Dogra's avatar
Aneesh Dogra committed
643 644 645 646 647 648 649 650
        s->out_format      = FMT_H263;
        avctx->delay       = 0;
        s->low_delay       = 1;
        s->modified_quant  = 1;
        s->h263_aic        = 1;
        s->h263_plus       = 1;
        s->loop_filter     = 1;
        s->unrestricted_mv = 0;
Benoit Fouet's avatar
Benoit Fouet committed
651
        break;
652
    case AV_CODEC_ID_MPEG4:
Aneesh Dogra's avatar
Aneesh Dogra committed
653 654
        s->out_format      = FMT_H263;
        s->h263_pred       = 1;
Benoit Fouet's avatar
Benoit Fouet committed
655
        s->unrestricted_mv = 1;
Aneesh Dogra's avatar
Aneesh Dogra committed
656 657
        s->low_delay       = s->max_b_frames ? 0 : 1;
        avctx->delay       = s->low_delay ? 0 : (s->max_b_frames + 1);
Benoit Fouet's avatar
Benoit Fouet committed
658
        break;
659
    case AV_CODEC_ID_MSMPEG4V2:
Aneesh Dogra's avatar
Aneesh Dogra committed
660 661
        s->out_format      = FMT_H263;
        s->h263_pred       = 1;
Benoit Fouet's avatar
Benoit Fouet committed
662
        s->unrestricted_mv = 1;
Aneesh Dogra's avatar
Aneesh Dogra committed
663 664 665
        s->msmpeg4_version = 2;
        avctx->delay       = 0;
        s->low_delay       = 1;
Benoit Fouet's avatar
Benoit Fouet committed
666
        break;
667
    case AV_CODEC_ID_MSMPEG4V3:
Aneesh Dogra's avatar
Aneesh Dogra committed
668 669 670 671 672 673 674
        s->out_format        = FMT_H263;
        s->h263_pred         = 1;
        s->unrestricted_mv   = 1;
        s->msmpeg4_version   = 3;
        s->flipflop_rounding = 1;
        avctx->delay         = 0;
        s->low_delay         = 1;
Benoit Fouet's avatar
Benoit Fouet committed
675
        break;
676
    case AV_CODEC_ID_WMV1:
Aneesh Dogra's avatar
Aneesh Dogra committed
677 678 679 680 681 682 683
        s->out_format        = FMT_H263;
        s->h263_pred         = 1;
        s->unrestricted_mv   = 1;
        s->msmpeg4_version   = 4;
        s->flipflop_rounding = 1;
        avctx->delay         = 0;
        s->low_delay         = 1;
Benoit Fouet's avatar
Benoit Fouet committed
684
        break;
685
    case AV_CODEC_ID_WMV2:
Aneesh Dogra's avatar
Aneesh Dogra committed
686 687 688 689 690 691 692
        s->out_format        = FMT_H263;
        s->h263_pred         = 1;
        s->unrestricted_mv   = 1;
        s->msmpeg4_version   = 5;
        s->flipflop_rounding = 1;
        avctx->delay         = 0;
        s->low_delay         = 1;
Benoit Fouet's avatar
Benoit Fouet committed
693 694 695 696 697
        break;
    default:
        return -1;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
698
    avctx->has_b_frames = !s->low_delay;
Benoit Fouet's avatar
Benoit Fouet committed
699 700 701

    s->encoding = 1;

Aneesh Dogra's avatar
Aneesh Dogra committed
702 703
    s->progressive_frame    =
    s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
704 705
                                                CODEC_FLAG_INTERLACED_ME) ||
                                s->alternate_scan);
706

Benoit Fouet's avatar
Benoit Fouet committed
707
    /* init */
708
    if (ff_MPV_common_init(s) < 0)
Benoit Fouet's avatar
Benoit Fouet committed
709 710
        return -1;

711 712 713
    if (ARCH_X86)
        ff_MPV_encode_init_x86(s);

714
    ff_h263dsp_init(&s->h263dsp);
Aneesh Dogra's avatar
Aneesh Dogra committed
715
    if (!s->dct_quantize)
716
        s->dct_quantize = ff_dct_quantize_c;
Aneesh Dogra's avatar
Aneesh Dogra committed
717 718
    if (!s->denoise_dct)
        s->denoise_dct  = denoise_dct_c;
719
    s->fast_dct_quantize = s->dct_quantize;
Aneesh Dogra's avatar
Aneesh Dogra committed
720 721
    if (avctx->trellis)
        s->dct_quantize  = dct_quantize_trellis_c;
722

Aneesh Dogra's avatar
Aneesh Dogra committed
723 724
    if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
        s->chroma_qscale_table = ff_h263_chroma_qscale_table;
725

Aneesh Dogra's avatar
Aneesh Dogra committed
726
    s->quant_precision = 5;
Benoit Fouet's avatar
Benoit Fouet committed
727 728 729 730

    ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
    ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);

731
    if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
Benoit Fouet's avatar
Benoit Fouet committed
732
        ff_h261_encode_init(s);
733
    if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
734
        ff_h263_encode_init(s);
735
    if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
Benoit Fouet's avatar
Benoit Fouet committed
736
        ff_msmpeg4_encode_init(s);
737
    if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
738
        && s->out_format == FMT_MPEG1)
Benoit Fouet's avatar
Benoit Fouet committed
739 740 741
        ff_mpeg1_encode_init(s);

    /* init q matrix */
Aneesh Dogra's avatar
Aneesh Dogra committed
742 743
    for (i = 0; i < 64; i++) {
        int j = s->dsp.idct_permutation[i];
744
        if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
Aneesh Dogra's avatar
Aneesh Dogra committed
745
            s->mpeg_quant) {
Benoit Fouet's avatar
Benoit Fouet committed
746 747
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
Aneesh Dogra's avatar
Aneesh Dogra committed
748
        } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
Benoit Fouet's avatar
Benoit Fouet committed
749 750
            s->intra_matrix[j] =
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
Aneesh Dogra's avatar
Aneesh Dogra committed
751 752
        } else {
            /* mpeg1/2 */
Benoit Fouet's avatar
Benoit Fouet committed
753 754 755
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
        }
Aneesh Dogra's avatar
Aneesh Dogra committed
756
        if (s->avctx->intra_matrix)
Benoit Fouet's avatar
Benoit Fouet committed
757
            s->intra_matrix[j] = s->avctx->intra_matrix[i];
Aneesh Dogra's avatar
Aneesh Dogra committed
758
        if (s->avctx->inter_matrix)
Benoit Fouet's avatar
Benoit Fouet committed
759 760 761 762 763 764
            s->inter_matrix[j] = s->avctx->inter_matrix[i];
    }

    /* precompute matrix */
    /* for mjpeg, we do include qscale in the matrix */
    if (s->out_format != FMT_MJPEG) {
765
        ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
Aneesh Dogra's avatar
Aneesh Dogra committed
766 767
                          s->intra_matrix, s->intra_quant_bias, avctx->qmin,
                          31, 1);
768
        ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
Aneesh Dogra's avatar
Aneesh Dogra committed
769 770
                          s->inter_matrix, s->inter_quant_bias, avctx->qmin,
                          31, 0);
Benoit Fouet's avatar
Benoit Fouet committed
771 772
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
773
    if (ff_rate_control_init(s) < 0)
Benoit Fouet's avatar
Benoit Fouet committed
774 775
        return -1;

776 777 778 779 780 781 782
#if FF_API_ERROR_RATE
    FF_DISABLE_DEPRECATION_WARNINGS
    if (avctx->error_rate)
        s->error_rate = avctx->error_rate;
    FF_ENABLE_DEPRECATION_WARNINGS;
#endif

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
    if (avctx->b_frame_strategy == 2) {
        for (i = 0; i < s->max_b_frames + 2; i++) {
            s->tmp_frames[i] = av_frame_alloc();
            if (!s->tmp_frames[i])
                return AVERROR(ENOMEM);

            s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
            s->tmp_frames[i]->width  = s->width  >> avctx->brd_scale;
            s->tmp_frames[i]->height = s->height >> avctx->brd_scale;

            ret = av_frame_get_buffer(s->tmp_frames[i], 32);
            if (ret < 0)
                return ret;
        }
    }

Benoit Fouet's avatar
Benoit Fouet committed
799 800 801
    return 0;
}

802
av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
Benoit Fouet's avatar
Benoit Fouet committed
803 804
{
    MpegEncContext *s = avctx->priv_data;
805
    int i;
Benoit Fouet's avatar
Benoit Fouet committed
806 807 808

    ff_rate_control_uninit(s);

809
    ff_MPV_common_end(s);
Aneesh Dogra's avatar
Aneesh Dogra committed
810 811
    if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
        s->out_format == FMT_MJPEG)
Benoit Fouet's avatar
Benoit Fouet committed
812 813 814 815
        ff_mjpeg_encode_close(s);

    av_freep(&avctx->extradata);

816 817 818
    for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
        av_frame_free(&s->tmp_frames[i]);

Benoit Fouet's avatar
Benoit Fouet committed
819 820 821
    return 0;
}

Aneesh Dogra's avatar
Aneesh Dogra committed
822 823
static int get_sae(uint8_t *src, int ref, int stride)
{
Benoit Fouet's avatar
Benoit Fouet committed
824
    int x,y;
Aneesh Dogra's avatar
Aneesh Dogra committed
825
    int acc = 0;
Benoit Fouet's avatar
Benoit Fouet committed
826

Aneesh Dogra's avatar
Aneesh Dogra committed
827 828 829
    for (y = 0; y < 16; y++) {
        for (x = 0; x < 16; x++) {
            acc += FFABS(src[x + y * stride] - ref);
Benoit Fouet's avatar
Benoit Fouet committed
830 831 832 833 834 835
        }
    }

    return acc;
}

Aneesh Dogra's avatar
Aneesh Dogra committed
836 837 838
static int get_intra_count(MpegEncContext *s, uint8_t *src,
                           uint8_t *ref, int stride)
{
Benoit Fouet's avatar
Benoit Fouet committed
839
    int x, y, w, h;
Aneesh Dogra's avatar
Aneesh Dogra committed
840
    int acc = 0;
Benoit Fouet's avatar
Benoit Fouet committed
841

Aneesh Dogra's avatar
Aneesh Dogra committed
842 843
    w = s->width  & ~15;
    h = s->height & ~15;
Benoit Fouet's avatar
Benoit Fouet committed
844

Aneesh Dogra's avatar
Aneesh Dogra committed
845 846 847 848 849 850 851
    for (y = 0; y < h; y += 16) {
        for (x = 0; x < w; x += 16) {
            int offset = x + y * stride;
            int sad  = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
                                     16);
            int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
            int sae  = get_sae(src + offset, mean, stride);
Benoit Fouet's avatar
Benoit Fouet committed
852

Aneesh Dogra's avatar
Aneesh Dogra committed
853
            acc += sae + 500 < sad;
Benoit Fouet's avatar
Benoit Fouet committed
854 855 856 857 858 859
        }
    }
    return acc;
}