diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2015-01-07 00:48:23 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2015-01-07 00:48:23 +0100 |
commit | c09eeca5468ca03cc5733f08dc6303b4aef2c2c1 (patch) | |
tree | 4cb9bc74bdd24febb33f737eb4d8206f0c6f803a | |
parent | cb03d29dec649efb03b895810f610e61d70800ca (diff) | |
parent | 8a3d9ca603f4d15ecaa9ca379cbaab4ecaec8ce4 (diff) | |
download | ffmpeg-c09eeca5468ca03cc5733f08dc6303b4aef2c2c1.tar.gz |
Merge commit '8a3d9ca603f4d15ecaa9ca379cbaab4ecaec8ce4'
* commit '8a3d9ca603f4d15ecaa9ca379cbaab4ecaec8ce4':
libavcodec: Add an OpenH264 encoder wrapper
Conflicts:
Changelog
configure
libavcodec/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
-rw-r--r-- | Changelog | 1 | ||||
-rwxr-xr-x | configure | 4 | ||||
-rw-r--r-- | libavcodec/Makefile | 1 | ||||
-rw-r--r-- | libavcodec/allcodecs.c | 1 | ||||
-rw-r--r-- | libavcodec/libopenh264enc.c | 228 | ||||
-rw-r--r-- | libavcodec/version.h | 2 |
6 files changed, 236 insertions, 1 deletions
@@ -11,6 +11,7 @@ version <next>: - tblend filter - cropdetect support for non 8bpp, absolute (if limit >= 1) and relative (if limit < 1.0) threshold - Camellia symmetric block cipher +- OpenH264 encoder wrapper version 2.5: @@ -226,6 +226,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] @@ -1367,6 +1368,7 @@ EXTERNAL_LIBRARY_LIST=" libopencore_amrnb libopencore_amrwb libopencv + libopenh264 libopenjpeg libopus libpulse @@ -2363,6 +2365,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" @@ -4886,6 +4889,7 @@ enabled libnut && require libnut libnut.h nut_demuxer_init -lnut 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/cxcore.h cvCreateImageHeader +enabled libopenh264 && require_pkg_config openh264 wels/codec_api.h WelsGetCodecVersion enabled libopenjpeg && { check_lib openjpeg.h opj_version -lopenmj2 -DOPJ_STATIC || check_lib openjpeg-1.5/openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC || check_lib openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC || diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 107661bc38..6b85420931 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -734,6 +734,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 8ceee2f1e4..053bca4f3b 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -511,6 +511,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 0000000000..ed32b41b93 --- /dev/null +++ b/libavcodec/libopenh264enc.c @@ -0,0 +1,228 @@ +/* + * OpenH264 video encoder + * Copyright (C) 2014 Martin Storsjo + * + * This file is part of FFmpeg. + * + * 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. + * + * 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 FFmpeg; 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/attributes.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", "A fixed number of slices", 0, AV_OPT_TYPE_CONST, { .i64 = SM_FIXEDSLCNUM_SLICE }, 0, 0, VE, "slice_mode" }, + { "rowmb", "One slice per row of macroblocks", 0, AV_OPT_TYPE_CONST, { .i64 = SM_ROWMB_SLICE }, 0, 0, VE, "slice_mode" }, + { "auto", "Automatic number of slices according to number of threads", 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; + av_unused OpenH264Version libver; + (void)g_strCodecVer; // Avoid warnings due to unused static members from codec_ver.h + + // Mingw GCC < 4.7 on x86_32 uses an incorrect/buggy ABI for the WelsGetCodecVersion + // function (for functions returning larger structs), thus skip the check in those + // configurations. +#if !defined(_WIN32) || !defined(__GNUC__) || !ARCH_X86_32 || AV_GCC_VERSION_AT_LEAST(4, 7) + libver = WelsGetCodecVersion(); + if (memcmp(&libver, &g_stCodecVersion, sizeof(libver))) { + av_log(avctx, AV_LOG_ERROR, "Incorrect library version loaded\n"); + return AVERROR(EINVAL); + } +#endif + + 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; + param.iMultipleThreadIdc = avctx->thread_count; + 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->slices > 1) + s->slice_mode = SM_FIXEDSLCNUM_SLICE; + param.sSpatialLayers[0].sSliceCfg.uiSliceMode = s->slice_mode; + param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->slices; + + 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; + // Normal frames are returned with one single layers, while IDR + // frames have two layers, where the first layer contains the SPS/PPS. + // If using global headers, don't include the SPS/PPS in the returned + // packet - thus, only return one layer. + 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, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index d8c891de22..7e51f3b39e 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,7 +29,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 56 -#define LIBAVCODEC_VERSION_MINOR 19 +#define LIBAVCODEC_VERSION_MINOR 20 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ |