Revision 0140d3f0 libavcodec/libx264.c

View differences:

libavcodec/libx264.c
19 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
 */
21 21

  
22
#include "libavutil/opt.h"
22 23
#include "avcodec.h"
23 24
#include <x264.h>
24 25
#include <math.h>
......
27 28
#include <string.h>
28 29

  
29 30
typedef struct X264Context {
31
    AVClass        *class;
30 32
    x264_param_t    params;
31 33
    x264_t         *enc;
32 34
    x264_picture_t  pic;
33 35
    uint8_t        *sei;
34 36
    int             sei_size;
35 37
    AVFrame         out_pic;
38
    const char *preset;
39
    const char *tune;
40
    const char *profile;
41
    int fastfirstpass;
36 42
} X264Context;
37 43

  
38 44
static void X264_log(void *p, int level, const char *fmt, va_list args)
......
163 169
    x4->sei_size = 0;
164 170
    x264_param_default(&x4->params);
165 171

  
166
    x4->params.pf_log               = X264_log;
167
    x4->params.p_log_private        = avctx;
168

  
169 172
    x4->params.i_keyint_max         = avctx->gop_size;
170
    x4->params.b_intra_refresh      = avctx->flags2 & CODEC_FLAG2_INTRA_REFRESH;
171
    x4->params.rc.i_bitrate         = avctx->bit_rate       / 1000;
172
    x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
173
    x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate    / 1000;
174
    x4->params.rc.b_stat_write      = avctx->flags & CODEC_FLAG_PASS1;
175
    if (avctx->flags & CODEC_FLAG_PASS2) {
176
        x4->params.rc.b_stat_read = 1;
177
    } else {
178
        if (avctx->crf) {
179
            x4->params.rc.i_rc_method   = X264_RC_CRF;
180
            x4->params.rc.f_rf_constant = avctx->crf;
181
            x4->params.rc.f_rf_constant_max = avctx->crf_max;
182
        } else if (avctx->cqp > -1) {
183
            x4->params.rc.i_rc_method   = X264_RC_CQP;
184
            x4->params.rc.i_qp_constant = avctx->cqp;
185
        }
186
    }
187

  
188
    // if neither crf nor cqp modes are selected we have to enable the RC
189
    // we do it this way because we cannot check if the bitrate has been set
190
    if (!(avctx->crf || (avctx->cqp > -1)))
191
        x4->params.rc.i_rc_method = X264_RC_ABR;
192 173

  
193 174
    x4->params.i_bframe          = avctx->max_b_frames;
194 175
    x4->params.b_cabac           = avctx->coder_type == FF_CODER_TYPE_AC;
......
217 198

  
218 199
    x4->params.i_frame_reference    = avctx->refs;
219 200

  
220
    x4->params.i_width              = avctx->width;
221
    x4->params.i_height             = avctx->height;
222
    x4->params.vui.i_sar_width      = avctx->sample_aspect_ratio.num;
223
    x4->params.vui.i_sar_height     = avctx->sample_aspect_ratio.den;
224
    x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
225
    x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
226

  
227 201
    x4->params.analyse.inter    = 0;
228 202
    if (avctx->partitions) {
229 203
        if (avctx->partitions & X264_PART_I4X4)
......
277 251
    if (avctx->level > 0)
278 252
        x4->params.i_level_idc = avctx->level;
279 253

  
254
    x4->params.rc.b_mb_tree               = !!(avctx->flags2 & CODEC_FLAG2_MBTREE);
255
    x4->params.rc.f_ip_factor             = 1 / fabs(avctx->i_quant_factor);
256
    x4->params.rc.f_pb_factor             = avctx->b_quant_factor;
257
    x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
258

  
259
    if (x4->preset || x4->tune) {
260
        if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0)
261
            return -1;
262
    }
263

  
264
    if (x4->fastfirstpass)
265
        x264_param_apply_fastfirstpass(&x4->params);
266

  
267
    if (x4->profile)
268
        if (x264_param_apply_profile(&x4->params, x4->profile) < 0)
269
            return -1;
270

  
271
    x4->params.pf_log               = X264_log;
272
    x4->params.p_log_private        = avctx;
273
    x4->params.i_log_level          = X264_LOG_DEBUG;
274

  
275
    x4->params.b_intra_refresh      = avctx->flags2 & CODEC_FLAG2_INTRA_REFRESH;
276
    x4->params.rc.i_bitrate         = avctx->bit_rate       / 1000;
277
    x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
278
    x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate    / 1000;
279
    x4->params.rc.b_stat_write      = avctx->flags & CODEC_FLAG_PASS1;
280
    if (avctx->flags & CODEC_FLAG_PASS2) {
281
        x4->params.rc.b_stat_read = 1;
282
    } else {
283
        if (avctx->crf) {
284
            x4->params.rc.i_rc_method   = X264_RC_CRF;
285
            x4->params.rc.f_rf_constant = avctx->crf;
286
            x4->params.rc.f_rf_constant_max = avctx->crf_max;
287
        } else if (avctx->cqp > -1) {
288
            x4->params.rc.i_rc_method   = X264_RC_CQP;
289
            x4->params.rc.i_qp_constant = avctx->cqp;
290
        }
291
    }
292

  
293
    // if neither crf nor cqp modes are selected we have to enable the RC
294
    // we do it this way because we cannot check if the bitrate has been set
295
    if (!(avctx->crf || (avctx->cqp > -1)))
296
        x4->params.rc.i_rc_method = X264_RC_ABR;
297

  
280 298
    if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy &&
281 299
        (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
282 300
        x4->params.rc.f_vbv_buffer_init =
283 301
            (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
284 302
    }
285 303

  
286
    x4->params.rc.b_mb_tree               = !!(avctx->flags2 & CODEC_FLAG2_MBTREE);
287
    x4->params.rc.f_ip_factor             = 1 / fabs(avctx->i_quant_factor);
288
    x4->params.rc.f_pb_factor             = avctx->b_quant_factor;
289
    x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
304
    x4->params.i_width          = avctx->width;
305
    x4->params.i_height         = avctx->height;
306
    x4->params.vui.i_sar_width  = avctx->sample_aspect_ratio.num;
307
    x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
308
    x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
309
    x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
290 310

  
291 311
    x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
292 312
    x4->params.analyse.b_ssim = avctx->flags2 & CODEC_FLAG2_SSIM;
293
    x4->params.i_log_level    = X264_LOG_DEBUG;
294 313

  
295 314
    x4->params.b_aud          = avctx->flags2 & CODEC_FLAG2_AUD;
296 315

  
......
305 324
    if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
306 325
        x4->params.b_repeat_headers = 0;
307 326

  
327
    // update AVCodecContext with x264 parameters
328
    avctx->has_b_frames = x4->params.i_bframe_pyramid ? 2 : !!x4->params.i_bframe;
329
    avctx->bit_rate = x4->params.rc.i_bitrate*1000;
330
    avctx->crf = x4->params.rc.f_rf_constant;
331

  
308 332
    x4->enc = x264_encoder_open(&x4->params);
309 333
    if (!x4->enc)
310 334
        return -1;
......
328 352
    return 0;
329 353
}
330 354

  
355
#define OFFSET(x) offsetof(X264Context,x)
356
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
357

  
358
static const AVOption options[] = {
359
    {"preset", "Set the encoding preset", OFFSET(preset), FF_OPT_TYPE_STRING, 0, 0, 0, VE},
360
    {"tune", "Tune the encoding params", OFFSET(tune), FF_OPT_TYPE_STRING, 0, 0, 0, VE},
361
    {"fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), FF_OPT_TYPE_INT, 1, 0, 1, VE},
362
    {"profile", "Set profile restrictions", OFFSET(profile), FF_OPT_TYPE_STRING, 0, 0, 0, VE},
363
    { NULL },
364
};
365

  
366
static const AVClass class = { "libx264", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
367

  
331 368
AVCodec ff_libx264_encoder = {
332 369
    .name           = "libx264",
333 370
    .type           = AVMEDIA_TYPE_VIDEO,
......
339 376
    .capabilities   = CODEC_CAP_DELAY,
340 377
    .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_YUVJ420P, PIX_FMT_NONE },
341 378
    .long_name      = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
379
    .priv_class     = &class,
342 380
};

Also available in: Unified diff