Commit 8a3d9ca6 authored by Martin Storsjö's avatar Martin Storsjö

libavcodec: Add an OpenH264 encoder wrapper

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.
Signed-off-by: 's avatarMartin Storsjö <martin@martin.st>
parent 2a1500fb
...@@ -10,6 +10,7 @@ version <next>: ...@@ -10,6 +10,7 @@ version <next>:
- H.261 RTP payload format (RFC 4587) depacketizer and experimental packetizer - H.261 RTP payload format (RFC 4587) depacketizer and experimental packetizer
- RTP/mpegts muxer - RTP/mpegts muxer
- VP8 in Ogg demuxing - VP8 in Ogg demuxing
- OpenH264 encoder wrapper
version 11: version 11:
......
...@@ -193,6 +193,7 @@ External library support: ...@@ -193,6 +193,7 @@ External library support:
--enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no] --enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no]
--enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no] --enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no]
--enable-libopencv enable video filtering via libopencv [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-libopenjpeg enable JPEG 2000 de/encoding via OpenJPEG [no]
--enable-libopus enable Opus de/encoding via libopus [no] --enable-libopus enable Opus de/encoding via libopus [no]
--enable-libpulse enable Pulseaudio input via libpulse [no] --enable-libpulse enable Pulseaudio input via libpulse [no]
...@@ -1156,6 +1157,7 @@ EXTERNAL_LIBRARY_LIST=" ...@@ -1156,6 +1157,7 @@ EXTERNAL_LIBRARY_LIST="
libopencore_amrnb libopencore_amrnb
libopencore_amrwb libopencore_amrwb
libopencv libopencv
libopenh264
libopenjpeg libopenjpeg
libopus libopus
libpulse libpulse
...@@ -2002,6 +2004,7 @@ libopencore_amrnb_decoder_deps="libopencore_amrnb" ...@@ -2002,6 +2004,7 @@ libopencore_amrnb_decoder_deps="libopencore_amrnb"
libopencore_amrnb_encoder_deps="libopencore_amrnb" libopencore_amrnb_encoder_deps="libopencore_amrnb"
libopencore_amrnb_encoder_select="audio_frame_queue" libopencore_amrnb_encoder_select="audio_frame_queue"
libopencore_amrwb_decoder_deps="libopencore_amrwb" libopencore_amrwb_decoder_deps="libopencore_amrwb"
libopenh264_encoder_deps="libopenh264"
libopenjpeg_decoder_deps="libopenjpeg" libopenjpeg_decoder_deps="libopenjpeg"
libopenjpeg_encoder_deps="libopenjpeg" libopenjpeg_encoder_deps="libopenjpeg"
libopus_decoder_deps="libopus" libopus_decoder_deps="libopus"
...@@ -4189,6 +4192,7 @@ enabled libmp3lame && require "libmp3lame >= 3.98.3" lame/lame.h lame_set ...@@ -4189,6 +4192,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_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 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 libopencv && require_pkg_config opencv opencv/cv.h cvCreateImageHeader
enabled libopenh264 && require_pkg_config openh264 wels/codec_api.h WelsGetCodecVersion
enabled libopenjpeg && { { check_header openjpeg.h && check_lib2 openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC; } || 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; } } { require_pkg_config libopenjpeg1 openjpeg.h opj_version -DOPJ_STATIC; } }
enabled libopus && require_pkg_config opus opus_multistream.h opus_multistream_decoder_create enabled libopus && require_pkg_config opus opus_multistream.h opus_multistream_decoder_create
......
...@@ -611,6 +611,7 @@ OBJS-$(CONFIG_LIBMP3LAME_ENCODER) += libmp3lame.o mpegaudiodecheader.o ...@@ -611,6 +611,7 @@ OBJS-$(CONFIG_LIBMP3LAME_ENCODER) += libmp3lame.o mpegaudiodecheader.o
OBJS-$(CONFIG_LIBOPENCORE_AMRNB_DECODER) += libopencore-amr.o OBJS-$(CONFIG_LIBOPENCORE_AMRNB_DECODER) += libopencore-amr.o
OBJS-$(CONFIG_LIBOPENCORE_AMRNB_ENCODER) += libopencore-amr.o OBJS-$(CONFIG_LIBOPENCORE_AMRNB_ENCODER) += libopencore-amr.o
OBJS-$(CONFIG_LIBOPENCORE_AMRWB_DECODER) += 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_DECODER) += libopenjpegdec.o
OBJS-$(CONFIG_LIBOPENJPEG_ENCODER) += libopenjpegenc.o OBJS-$(CONFIG_LIBOPENJPEG_ENCODER) += libopenjpegenc.o
OBJS-$(CONFIG_LIBOPUS_DECODER) += libopusdec.o libopus.o \ OBJS-$(CONFIG_LIBOPUS_DECODER) += libopusdec.o libopus.o \
......
...@@ -442,6 +442,7 @@ void avcodec_register_all(void) ...@@ -442,6 +442,7 @@ void avcodec_register_all(void)
REGISTER_ENCODER(LIBMP3LAME, libmp3lame); REGISTER_ENCODER(LIBMP3LAME, libmp3lame);
REGISTER_ENCDEC (LIBOPENCORE_AMRNB, libopencore_amrnb); REGISTER_ENCDEC (LIBOPENCORE_AMRNB, libopencore_amrnb);
REGISTER_DECODER(LIBOPENCORE_AMRWB, libopencore_amrwb); REGISTER_DECODER(LIBOPENCORE_AMRWB, libopencore_amrwb);
REGISTER_ENCODER(LIBOPENH264, libopenh264);
REGISTER_ENCDEC (LIBOPENJPEG, libopenjpeg); REGISTER_ENCDEC (LIBOPENJPEG, libopenjpeg);
REGISTER_ENCDEC (LIBOPUS, libopus); REGISTER_ENCDEC (LIBOPUS, libopus);
REGISTER_ENCDEC (LIBSCHROEDINGER, libschroedinger); REGISTER_ENCDEC (LIBSCHROEDINGER, libschroedinger);
......
/*
* 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/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, &param);
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, &param) != 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,
};
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include "libavutil/version.h" #include "libavutil/version.h"
#define LIBAVCODEC_VERSION_MAJOR 56 #define LIBAVCODEC_VERSION_MAJOR 56
#define LIBAVCODEC_VERSION_MINOR 10 #define LIBAVCODEC_VERSION_MINOR 11
#define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_MICRO 0
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment