diff options
Diffstat (limited to 'libavcodec/libx264.c')
-rw-r--r-- | libavcodec/libx264.c | 134 |
1 files changed, 122 insertions, 12 deletions
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index 3934627537..6279612c82 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -2,20 +2,20 @@ * H.264 encoding using the x264 library * Copyright (C) 2005 Mans Rullgard <mans@mansr.com> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * 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. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * 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 - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -41,7 +41,11 @@ typedef struct X264Context { char *preset; char *tune; char *profile; + char *level; int fastfirstpass; + char *stats; + char *wpredp; + char *x264opts; float crf; float crf_max; int cqp; @@ -104,9 +108,14 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, /* Write the SEI as part of the first frame. */ if (x4->sei_size > 0 && nnal > 0) { + if (x4->sei_size > size) { + av_log(ctx, AV_LOG_ERROR, "Error: nal buffer is too small\n"); + return -1; + } memcpy(p, x4->sei, x4->sei_size); p += x4->sei_size; x4->sei_size = 0; + av_freep(&x4->sei); } for (i = 0; i < nnal; i++){ @@ -117,6 +126,25 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, return 1; } +static int avfmt2_num_planes(int avfmt) +{ + switch (avfmt) { + case PIX_FMT_YUV420P: + case PIX_FMT_YUVJ420P: + case PIX_FMT_YUV420P9: + case PIX_FMT_YUV420P10: + case PIX_FMT_YUV444P: + return 3; + + case PIX_FMT_BGR24: + case PIX_FMT_RGB24: + return 1; + + default: + return 3; + } +} + static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { @@ -129,10 +157,10 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, x4->pic.img.i_csp = x4->params.i_csp; if (x264_bit_depth > 8) x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH; - x4->pic.img.i_plane = 3; + x4->pic.img.i_plane = avfmt2_num_planes(ctx->pix_fmt); if (frame) { - for (i = 0; i < 3; i++) { + for (i = 0; i < x4->pic.img.i_plane; i++) { x4->pic.img.plane[i] = frame->data[i]; x4->pic.img.i_stride[i] = frame->linesize[i]; } @@ -147,6 +175,12 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, x4->params.b_tff = frame->top_field_first; x264_encoder_reconfig(x4->enc, &x4->params); } + if (x4->params.vui.i_sar_height != ctx->sample_aspect_ratio.den + || x4->params.vui.i_sar_width != ctx->sample_aspect_ratio.num) { + x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den; + x4->params.vui.i_sar_width = ctx->sample_aspect_ratio.num; + x264_encoder_reconfig(x4->enc, &x4->params); + } } do { @@ -196,6 +230,20 @@ static av_cold int X264_close(AVCodecContext *avctx) return 0; } +#define OPT_STR(opt, param) \ + do { \ + int ret; \ + if (param && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \ + if(ret == X264_PARAM_BAD_NAME) \ + av_log(avctx, AV_LOG_ERROR, \ + "bad option '%s': '%s'\n", opt, param); \ + else \ + av_log(avctx, AV_LOG_ERROR, \ + "bad value for '%s': '%s'\n", opt, param); \ + return -1; \ + } \ + } while (0) + static int convert_pix_fmt(enum PixelFormat pix_fmt) { switch (pix_fmt) { @@ -208,6 +256,13 @@ static int convert_pix_fmt(enum PixelFormat pix_fmt) case PIX_FMT_YUV444P: case PIX_FMT_YUV444P9: case PIX_FMT_YUV444P10: return X264_CSP_I444; +#ifdef X264_CSP_BGR + case PIX_FMT_BGR24: + return X264_CSP_BGR; + + case PIX_FMT_RGB24: + return X264_CSP_RGB; +#endif }; return 0; } @@ -221,11 +276,15 @@ static int convert_pix_fmt(enum PixelFormat pix_fmt) static av_cold int X264_init(AVCodecContext *avctx) { X264Context *x4 = avctx->priv_data; + int sw,sh; x264_param_default(&x4->params); x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER; + x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor); + x4->params.rc.f_pb_factor = avctx->b_quant_factor; + x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset; if (x4->preset || x4->tune) if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) { av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune); @@ -240,6 +299,8 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.i_log_level = X264_LOG_DEBUG; x4->params.i_csp = convert_pix_fmt(avctx->pix_fmt); + OPT_STR("weightp", x4->wpredp); + if (avctx->bit_rate) { x4->params.rc.i_bitrate = avctx->bit_rate / 1000; x4->params.rc.i_rc_method = X264_RC_ABR; @@ -262,15 +323,28 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.rc.f_rf_constant_max = x4->crf_max; } + OPT_STR("stats", x4->stats); + if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy && (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) { x4->params.rc.f_vbv_buffer_init = (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size; } - x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor); - x4->params.rc.f_pb_factor = avctx->b_quant_factor; - x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset; + OPT_STR("level", x4->level); + + if(x4->x264opts){ + const char *p= x4->x264opts; + while(p){ + char param[256]={0}, val[256]={0}; + if(sscanf(p, "%255[^:=]=%255[^:]", param, val) == 1){ + OPT_STR(param, "1"); + }else + OPT_STR(param, val); + p= strchr(p, ':'); + p+=!!p; + } + } if (avctx->me_method == ME_EPZS) x4->params.analyse.i_me_method = X264_ME_DIA; @@ -371,8 +445,9 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.i_width = avctx->width; x4->params.i_height = avctx->height; - x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num; - x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den; + av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096); + x4->params.vui.i_sar_width = sw; + x4->params.vui.i_sar_height = sh; x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den; x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num; @@ -382,7 +457,7 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT; - x4->params.b_open_gop = !(avctx->flags & CODEC_FLAG_CLOSED_GOP); +// x4->params.b_open_gop = !(avctx->flags & CODEC_FLAG_CLOSED_GOP); x4->params.i_slice_count = avctx->slices; @@ -449,6 +524,13 @@ static const enum PixelFormat pix_fmts_10bit[] = { PIX_FMT_YUV444P10, PIX_FMT_NONE }; +static const enum PixelFormat pix_fmts_8bit_rgb[] = { +#ifdef X264_CSP_BGR + PIX_FMT_BGR24, + PIX_FMT_RGB24, +#endif + PIX_FMT_NONE +}; static av_cold void X264_init_static(AVCodec *codec) { @@ -467,6 +549,10 @@ static const AVOption options[] = { { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE}, { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE}, { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_INT, { 1 }, 0, 1, VE}, + {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE}, + {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE}, + {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE}, + {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE}, { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE }, { "crf_max", "In CRF mode, prevents VBV from lowering quality beyond this point.",OFFSET(crf_max), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE }, { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE }, @@ -515,15 +601,24 @@ static const AVClass class = { .version = LIBAVUTIL_VERSION_INT, }; +static const AVClass rgbclass = { + .class_name = "libx264rgb", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + static const AVCodecDefault x264_defaults[] = { { "b", "0" }, { "bf", "-1" }, + { "flags2", "0" }, { "g", "-1" }, { "qmin", "-1" }, { "qmax", "-1" }, { "qdiff", "-1" }, { "qblur", "-1" }, { "qcomp", "-1" }, +// { "rc_lookahead", "-1" }, { "refs", "-1" }, { "sc_threshold", "-1" }, { "trellis", "-1" }, @@ -553,3 +648,18 @@ AVCodec ff_libx264_encoder = { .defaults = x264_defaults, .init_static_data = X264_init_static, }; + +AVCodec ff_libx264rgb_encoder = { + .name = "libx264rgb", + .type = AVMEDIA_TYPE_VIDEO, + .id = CODEC_ID_H264, + .priv_data_size = sizeof(X264Context), + .init = X264_init, + .encode = X264_frame, + .close = X264_close, + .capabilities = CODEC_CAP_DELAY, + .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"), + .priv_class = &rgbclass, + .defaults = x264_defaults, + .pix_fmts = pix_fmts_8bit_rgb, +}; |