Compared to existing, common opensource H264 encoders, this can be useful since it has got a different license (BSD instead of GPL).
Performance- and qualitywise it is comparable to x264 in ultrafast mode. Hooking it up as an encoder in libavcodec also simplifies comparing it against other common encoders. This requires OpenH264 1.3 or newer. Since the OpenH264 API and ABI changes frequently, only releases are supported. To take advantage of the OpenH264 patent offer, the OpenH264 library must not be redistributed, but downloaded at runtime at the end-user's system. --- configure | 4 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/libopenh264enc.c | 219 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 225 insertions(+) create mode 100644 libavcodec/libopenh264enc.c diff --git a/configure b/configure index 48669a0..5e163f9 100755 --- a/configure +++ b/configure @@ -193,6 +193,7 @@ External library support: --enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no] --enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no] --enable-libopencv enable video filtering via libopencv [no] + --enable-libopenh264 enable H264 encoding via OpenH264 [no] --enable-libopenjpeg enable JPEG 2000 de/encoding via OpenJPEG [no] --enable-libopus enable Opus de/encoding via libopus [no] --enable-libpulse enable Pulseaudio input via libpulse [no] @@ -1156,6 +1157,7 @@ EXTERNAL_LIBRARY_LIST=" libopencore_amrnb libopencore_amrwb libopencv + libopenh264 libopenjpeg libopus libpulse @@ -2002,6 +2004,7 @@ libopencore_amrnb_decoder_deps="libopencore_amrnb" libopencore_amrnb_encoder_deps="libopencore_amrnb" libopencore_amrnb_encoder_select="audio_frame_queue" libopencore_amrwb_decoder_deps="libopencore_amrwb" +libopenh264_encoder_deps="libopenh264" libopenjpeg_decoder_deps="libopenjpeg" libopenjpeg_encoder_deps="libopenjpeg" libopus_decoder_deps="libopus" @@ -4188,6 +4191,7 @@ enabled libmp3lame && require "libmp3lame >= 3.98.3" lame/lame.h lame_set enabled libopencore_amrnb && require libopencore_amrnb opencore-amrnb/interf_dec.h Decoder_Interface_init -lopencore-amrnb enabled libopencore_amrwb && require libopencore_amrwb opencore-amrwb/dec_if.h D_IF_init -lopencore-amrwb enabled libopencv && require_pkg_config opencv opencv/cv.h cvCreateImageHeader +enabled libopenh264 && require libopenh264 wels/codec_api.h WelsGetCodecVersion -lopenh264 -lstdc++ enabled libopenjpeg && { { check_header openjpeg.h && check_lib2 openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC; } || { require_pkg_config libopenjpeg1 openjpeg.h opj_version -DOPJ_STATIC; } } enabled libopus && require_pkg_config opus opus_multistream.h opus_multistream_decoder_create diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 092e2fd..419dcb6 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -611,6 +611,7 @@ OBJS-$(CONFIG_LIBMP3LAME_ENCODER) += libmp3lame.o mpegaudiodecheader.o OBJS-$(CONFIG_LIBOPENCORE_AMRNB_DECODER) += libopencore-amr.o OBJS-$(CONFIG_LIBOPENCORE_AMRNB_ENCODER) += libopencore-amr.o OBJS-$(CONFIG_LIBOPENCORE_AMRWB_DECODER) += libopencore-amr.o +OBJS-$(CONFIG_LIBOPENH264_ENCODER) += libopenh264enc.o OBJS-$(CONFIG_LIBOPENJPEG_DECODER) += libopenjpegdec.o OBJS-$(CONFIG_LIBOPENJPEG_ENCODER) += libopenjpegenc.o OBJS-$(CONFIG_LIBOPUS_DECODER) += libopusdec.o libopus.o \ diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 62735e2..0d46afe 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -442,6 +442,7 @@ void avcodec_register_all(void) REGISTER_ENCODER(LIBMP3LAME, libmp3lame); REGISTER_ENCDEC (LIBOPENCORE_AMRNB, libopencore_amrnb); REGISTER_DECODER(LIBOPENCORE_AMRWB, libopencore_amrwb); + REGISTER_ENCODER(LIBOPENH264, libopenh264); REGISTER_ENCDEC (LIBOPENJPEG, libopenjpeg); REGISTER_ENCDEC (LIBOPUS, libopus); REGISTER_ENCDEC (LIBSCHROEDINGER, libschroedinger); diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c new file mode 100644 index 0000000..17aa75d --- /dev/null +++ b/libavcodec/libopenh264enc.c @@ -0,0 +1,219 @@ +/* + * OpenH264 video encoder + * Copyright (C) 2014 Martin Storsjo + * + * This file is part of Libav. + * + * Libav 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, + * 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <wels/codec_api.h> +#include <wels/codec_ver.h> + +#include "libavutil/common.h" +#include "libavutil/opt.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/mathematics.h" + +#include "avcodec.h" +#include "internal.h" + +typedef struct SVCContext { + const AVClass *av_class; + ISVCEncoder *encoder; + int slice_mode; + int loopfilter; + char *profile; +} SVCContext; + +#define OFFSET(x) offsetof(SVCContext, x) +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM +static const AVOption options[] = { + { "slice_mode", "Slice mode", OFFSET(slice_mode), AV_OPT_TYPE_INT, { .i64 = SM_AUTO_SLICE }, SM_SINGLE_SLICE, SM_RESERVED, VE, "slice_mode" }, + { "fixed", "", 0, AV_OPT_TYPE_CONST, { .i64 = SM_FIXEDSLCNUM_SLICE }, 0, 0, VE, "slice_mode" }, + { "rowmb", "", 0, AV_OPT_TYPE_CONST, { .i64 = SM_ROWMB_SLICE }, 0, 0, VE, "slice_mode" }, + { "auto", "", 0, AV_OPT_TYPE_CONST, { .i64 = SM_AUTO_SLICE }, 0, 0, VE, "slice_mode" }, + { "loopfilter", "Enable loop filter", OFFSET(loopfilter), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE }, + { "profile", "Set profile restrictions", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE }, + { NULL } +}; + +static const AVClass class = { + "libopenh264enc", av_default_item_name, options, LIBAVUTIL_VERSION_INT +}; + +static av_cold int svc_encode_close(AVCodecContext *avctx) +{ + SVCContext *s = avctx->priv_data; + + if (s->encoder) + WelsDestroySVCEncoder(s->encoder); + return 0; +} + +static av_cold int svc_encode_init(AVCodecContext *avctx) +{ + SVCContext *s = avctx->priv_data; + SEncParamExt param = { 0 }; + int err = AVERROR_UNKNOWN; + OpenH264Version libver = WelsGetCodecVersion(); + + if (memcmp(&libver, &g_stCodecVersion, sizeof(libver))) { + av_log(avctx, AV_LOG_ERROR, "Incorrect library version loaded\n"); + return AVERROR(EINVAL); + } + + if (WelsCreateSVCEncoder(&s->encoder)) { + av_log(avctx, AV_LOG_ERROR, "Unable to create encoder\n"); + return AVERROR_UNKNOWN; + } + + (*s->encoder)->GetDefaultParams(s->encoder, ¶m); + + param.fMaxFrameRate = avctx->time_base.den / avctx->time_base.num; + param.iPicWidth = avctx->width; + param.iPicHeight = avctx->height; + param.iTargetBitrate = avctx->bit_rate; + param.iMaxBitrate = FFMAX(avctx->rc_max_rate, avctx->bit_rate); + param.iRCMode = RC_QUALITY_MODE; + param.iTemporalLayerNum = 1; + param.iSpatialLayerNum = 1; + param.bEnableDenoise = 0; + param.bEnableBackgroundDetection = 1; + param.bEnableAdaptiveQuant = 1; + param.bEnableFrameSkip = 0; + param.bEnableLongTermReference = 0; + param.iLtrMarkPeriod = 30; + param.uiIntraPeriod = avctx->gop_size; + param.bEnableSpsPpsIdAddition = 0; + param.bPrefixNalAddingCtrl = 0; + param.iLoopFilterDisableIdc = !s->loopfilter; + param.iEntropyCodingModeFlag = 0; + if (s->profile && !strcmp(s->profile, "main")) + param.iEntropyCodingModeFlag = 1; + else if (!s->profile && avctx->coder_type == FF_CODER_TYPE_AC) + param.iEntropyCodingModeFlag = 1; + + param.sSpatialLayers[0].iVideoWidth = param.iPicWidth; + param.sSpatialLayers[0].iVideoHeight = param.iPicHeight; + param.sSpatialLayers[0].fFrameRate = param.fMaxFrameRate; + param.sSpatialLayers[0].iSpatialBitrate = param.iTargetBitrate; + param.sSpatialLayers[0].iMaxSpatialBitrate = param.iMaxBitrate; + + if (avctx->thread_count != 1) { + SSliceConfig *cfg = ¶m.sSpatialLayers[0].sSliceCfg; + + param.iMultipleThreadIdc = avctx->thread_count; + + cfg->uiSliceMode = s->slice_mode; + cfg->sSliceArgument.uiSliceNum = avctx->slice_count; + } + + if ((*s->encoder)->InitializeExt(s->encoder, ¶m) != cmResultSuccess) { + av_log(avctx, AV_LOG_ERROR, "Initialize failed\n"); + goto fail; + } + + if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { + SFrameBSInfo fbi = { 0 }; + int i, size = 0; + (*s->encoder)->EncodeParameterSets(s->encoder, &fbi); + for (i = 0; i < fbi.sLayerInfo[0].iNalCount; i++) + size += fbi.sLayerInfo[0].pNalLengthInByte[i]; + avctx->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE); + if (!avctx->extradata) { + err = AVERROR(ENOMEM); + goto fail; + } + avctx->extradata_size = size; + memcpy(avctx->extradata, fbi.sLayerInfo[0].pBsBuf, size); + } + + return 0; + +fail: + svc_encode_close(avctx); + return err; +} + +static int svc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, + const AVFrame *frame, int *got_packet) +{ + SVCContext *s = avctx->priv_data; + SFrameBSInfo fbi = { 0 }; + int i, ret; + int encoded; + SSourcePicture sp = { 0 }; + int size = 0, layer, first_layer = 0; + int layer_size[MAX_LAYER_NUM_OF_FRAME] = { 0 }; + + sp.iColorFormat = videoFormatI420; + for (i = 0; i < 3; i++) { + sp.iStride[i] = frame->linesize[i]; + sp.pData[i] = frame->data[i]; + } + sp.iPicWidth = avctx->width; + sp.iPicHeight = avctx->height; + + encoded = (*s->encoder)->EncodeFrame(s->encoder, &sp, &fbi); + if (encoded != cmResultSuccess) { + av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed\n"); + return AVERROR_UNKNOWN; + } + if (fbi.eFrameType == videoFrameTypeSkip) { + av_log(avctx, AV_LOG_DEBUG, "frame skipped\n"); + return 0; + } + first_layer = 0; + if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) + first_layer = fbi.iLayerNum - 1; + + for (layer = first_layer; layer < fbi.iLayerNum; layer++) { + for (i = 0; i < fbi.sLayerInfo[layer].iNalCount; i++) + layer_size[layer] += fbi.sLayerInfo[layer].pNalLengthInByte[i]; + size += layer_size[layer]; + } + av_log(NULL, AV_LOG_DEBUG, "%d slices\n", fbi.sLayerInfo[fbi.iLayerNum - 1].iNalCount); + + if ((ret = ff_alloc_packet(avpkt, size))) { + av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n"); + return ret; + } + size = 0; + for (layer = first_layer; layer < fbi.iLayerNum; layer++) { + memcpy(avpkt->data + size, fbi.sLayerInfo[layer].pBsBuf, layer_size[layer]); + size += layer_size[layer]; + } + avpkt->pts = frame->pts; + if (fbi.eFrameType == videoFrameTypeIDR) + avpkt->flags |= AV_PKT_FLAG_KEY; + *got_packet = 1; + return 0; +} + +AVCodec ff_libopenh264_encoder = { + .name = "libopenh264", + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_H264, + .priv_data_size = sizeof(SVCContext), + .init = svc_encode_init, + .encode2 = svc_encode_frame, + .close = svc_encode_close, + .capabilities = CODEC_CAP_AUTO_THREADS, + .pix_fmts = (const enum PixelFormat[]){ AV_PIX_FMT_YUV420P, + AV_PIX_FMT_NONE }, + .long_name = NULL_IF_CONFIG_SMALL("OpenH264"), + .priv_class = &class, +}; -- 1.9.3 (Apple Git-50) _______________________________________________ libav-devel mailing list libav-devel@libav.org https://lists.libav.org/mailman/listinfo/libav-devel