diff options
Diffstat (limited to 'libavcodec/libx264.c')
-rw-r--r-- | libavcodec/libx264.c | 203 |
1 files changed, 186 insertions, 17 deletions
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index 6233f1e..d9130b2 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 */ @@ -48,7 +48,10 @@ typedef struct X264Context { char *preset; char *tune; char *profile; + char *level; int fastfirstpass; + char *wpredp; + char *x264opts; float crf; float crf_max; int cqp; @@ -108,16 +111,21 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, for (i = 0; i < nnal; i++) size += nals[i].i_payload; - if ((ret = ff_alloc_packet(pkt, size)) < 0) + if ((ret = ff_alloc_packet2(ctx, pkt, size)) < 0) return ret; p = pkt->data; /* 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++){ @@ -128,23 +136,42 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, return 1; } +static int avfmt2_num_planes(int avfmt) +{ + switch (avfmt) { + case AV_PIX_FMT_YUV420P: + case AV_PIX_FMT_YUVJ420P: + case AV_PIX_FMT_YUV420P9: + case AV_PIX_FMT_YUV420P10: + case AV_PIX_FMT_YUV444P: + return 3; + + case AV_PIX_FMT_BGR24: + case AV_PIX_FMT_RGB24: + return 1; + + default: + return 3; + } +} + static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { X264Context *x4 = ctx->priv_data; x264_nal_t *nal; int nnal, i, ret; - x264_picture_t pic_out; + x264_picture_t pic_out = {0}; AVFrameSideData *side_data; x264_picture_init( &x4->pic ); 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]; } @@ -155,7 +182,7 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, frame->pict_type == AV_PICTURE_TYPE_P ? X264_TYPE_P : frame->pict_type == AV_PICTURE_TYPE_B ? X264_TYPE_B : X264_TYPE_AUTO; - if (x4->params.b_tff != frame->top_field_first) { + if (x4->params.b_interlaced && x4->params.b_tff != frame->top_field_first) { x4->params.b_tff = frame->top_field_first; x264_encoder_reconfig(x4->enc, &x4->params); } @@ -250,6 +277,20 @@ static av_cold int X264_close(AVCodecContext *avctx) return 0; } +#define OPT_STR(opt, param) \ + do { \ + int ret; \ + if (param!=NULL && (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 AVPixelFormat pix_fmt) { switch (pix_fmt) { @@ -258,10 +299,19 @@ static int convert_pix_fmt(enum AVPixelFormat pix_fmt) case AV_PIX_FMT_YUV420P9: case AV_PIX_FMT_YUV420P10: return X264_CSP_I420; case AV_PIX_FMT_YUV422P: + case AV_PIX_FMT_YUVJ422P: case AV_PIX_FMT_YUV422P10: return X264_CSP_I422; case AV_PIX_FMT_YUV444P: + case AV_PIX_FMT_YUVJ444P: case AV_PIX_FMT_YUV444P9: case AV_PIX_FMT_YUV444P10: return X264_CSP_I444; +#ifdef X264_CSP_BGR + case AV_PIX_FMT_BGR24: + return X264_CSP_BGR; + + case AV_PIX_FMT_RGB24: + return X264_CSP_RGB; +#endif case AV_PIX_FMT_NV12: return X264_CSP_NV12; case AV_PIX_FMT_NV16: case AV_PIX_FMT_NV20: return X264_CSP_NV16; @@ -278,14 +328,29 @@ static int convert_pix_fmt(enum AVPixelFormat pix_fmt) static av_cold int X264_init(AVCodecContext *avctx) { X264Context *x4 = avctx->priv_data; + int sw,sh; + + if (avctx->global_quality > 0) + av_log(avctx, AV_LOG_WARNING, "-qscale is ignored, -crf is recommended.\n"); x264_param_default(&x4->params); x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER; + 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) { + int i; av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune); + av_log(avctx, AV_LOG_INFO, "Possible presets:"); + for (i = 0; x264_preset_names[i]; i++) + av_log(avctx, AV_LOG_INFO, " %s", x264_preset_names[i]); + av_log(avctx, AV_LOG_INFO, "\n"); + av_log(avctx, AV_LOG_INFO, "Possible tunes:"); + for (i = 0; x264_tune_names[i]; i++) + av_log(avctx, AV_LOG_INFO, " %s", x264_tune_names[i]); + av_log(avctx, AV_LOG_INFO, "\n"); return AVERROR(EINVAL); } @@ -297,6 +362,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; @@ -325,10 +392,10 @@ static av_cold int X264_init(AVCodecContext *avctx) (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size; } + OPT_STR("level", x4->level); + if (avctx->i_quant_factor > 0) 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 (avctx->me_method == ME_EPZS) x4->params.analyse.i_me_method = X264_ME_DIA; @@ -422,25 +489,68 @@ static av_cold int X264_init(AVCodecContext *avctx) if (x4->slice_max_size >= 0) x4->params.i_slice_max_size = x4->slice_max_size; + else { + /* + * Allow x264 to be instructed through AVCodecContext about the maximum + * size of the RTP payload. For example, this enables the production of + * payload suitable for the H.264 RTP packetization-mode 0 i.e. single + * NAL unit per RTP packet. + */ + if (avctx->rtp_payload_size) + x4->params.i_slice_max_size = avctx->rtp_payload_size; + } if (x4->fastfirstpass) x264_param_apply_fastfirstpass(&x4->params); + /* Allow specifying the x264 profile through AVCodecContext. */ + if (!x4->profile) + switch (avctx->profile) { + case FF_PROFILE_H264_BASELINE: + x4->profile = av_strdup("baseline"); + break; + case FF_PROFILE_H264_HIGH: + x4->profile = av_strdup("high"); + break; + case FF_PROFILE_H264_HIGH_10: + x4->profile = av_strdup("high10"); + break; + case FF_PROFILE_H264_HIGH_422: + x4->profile = av_strdup("high422"); + break; + case FF_PROFILE_H264_HIGH_444: + x4->profile = av_strdup("high444"); + break; + case FF_PROFILE_H264_MAIN: + x4->profile = av_strdup("main"); + break; + default: + break; + } + if (x4->nal_hrd >= 0) x4->params.i_nal_hrd = x4->nal_hrd; if (x4->profile) if (x264_param_apply_profile(&x4->params, x4->profile) < 0) { + int i; av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile); + av_log(avctx, AV_LOG_INFO, "Possible profiles:"); + for (i = 0; x264_profile_names[i]; i++) + av_log(avctx, AV_LOG_INFO, " %s", x264_profile_names[i]); + av_log(avctx, AV_LOG_INFO, "\n"); return AVERROR(EINVAL); } 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; - 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; + 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_timebase_den = avctx->time_base.den; + x4->params.i_timebase_num = avctx->time_base.num; + x4->params.i_fps_num = avctx->time_base.den; + x4->params.i_fps_den = avctx->time_base.num * avctx->ticks_per_frame; x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR; @@ -455,11 +565,33 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.i_slice_count = avctx->slices; x4->params.vui.b_fullrange = avctx->pix_fmt == AV_PIX_FMT_YUVJ420P || + avctx->pix_fmt == AV_PIX_FMT_YUVJ422P || + avctx->pix_fmt == AV_PIX_FMT_YUVJ444P || avctx->color_range == AVCOL_RANGE_JPEG; + if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED) + x4->params.vui.i_colmatrix = avctx->colorspace; + if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED) + x4->params.vui.i_colorprim = avctx->color_primaries; + if (avctx->color_trc != AVCOL_TRC_UNSPECIFIED) + x4->params.vui.i_transfer = avctx->color_trc; + if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) x4->params.b_repeat_headers = 0; + 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 (x4->x264_params) { AVDictionary *dict = NULL; AVDictionaryEntry *en = NULL; @@ -522,7 +654,9 @@ static const enum AVPixelFormat pix_fmts_8bit[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUV422P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NV12, AV_PIX_FMT_NV16, AV_PIX_FMT_NONE @@ -539,6 +673,13 @@ static const enum AVPixelFormat pix_fmts_10bit[] = { AV_PIX_FMT_NV20, AV_PIX_FMT_NONE }; +static const enum AVPixelFormat pix_fmts_8bit_rgb[] = { +#ifdef X264_CSP_BGR + AV_PIX_FMT_BGR24, + AV_PIX_FMT_RGB24, +#endif + AV_PIX_FMT_NONE +}; static av_cold void X264_init_static(AVCodec *codec) { @@ -557,6 +698,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, { .i64 = 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, {.dbl = -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, {.dbl = -1 }, -1, FLT_MAX, VE }, { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE }, @@ -606,16 +751,24 @@ static const AVOption options[] = { { NULL }, }; -static const AVClass class = { +static const AVClass x264_class = { .class_name = "libx264", .item_name = av_default_item_name, .option = options, .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" }, { "i_qfactor", "-1" }, { "qmin", "-1" }, @@ -623,6 +776,7 @@ static const AVCodecDefault x264_defaults[] = { { "qdiff", "-1" }, { "qblur", "-1" }, { "qcomp", "-1" }, +// { "rc_lookahead", "-1" }, { "refs", "-1" }, { "sc_threshold", "-1" }, { "trellis", "-1" }, @@ -651,7 +805,22 @@ AVCodec ff_libx264_encoder = { .encode2 = X264_frame, .close = X264_close, .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS, - .priv_class = &class, + .priv_class = &x264_class, .defaults = x264_defaults, .init_static_data = X264_init_static, }; + +AVCodec ff_libx264rgb_encoder = { + .name = "libx264rgb", + .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_H264, + .priv_data_size = sizeof(X264Context), + .init = X264_init, + .encode2 = X264_frame, + .close = X264_close, + .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS, + .priv_class = &rgbclass, + .defaults = x264_defaults, + .pix_fmts = pix_fmts_8bit_rgb, +}; |