Re: [FFmpeg-devel] [PATCH] MAINTAINERS: add myself as a mediacodec and videotoolbox maintainer

2023-11-06 Thread Rick Kern
On Mon, Nov 6, 2023 at 10:20 PM Zhao Zhili  wrote:

> From: Zhao Zhili 
>
> Signed-off-by: Zhao Zhili 
> ---
>  MAINTAINERS | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index b66c3d09a6..3430e1722b 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -268,11 +268,11 @@ Codecs:
>  Hardware acceleration:
>dxva2*Hendrik Leppkes, Laurent Aimar,
> Steve Lhomme
>d3d11va*  Steve Lhomme
> -  mediacodec*   Matthieu Bouron, Aman Gupta
> +  mediacodec*   Matthieu Bouron, Aman Gupta, Zhao
> Zhili
>vaapi*Haihao Xiang
>vaapi_encode* Mark Thompson, Haihao Xiang
>vdpau*Philip Langdale, Carl Eugen Hoyos
> -  videotoolbox* Rick Kern, Aman Gupta
> +  videotoolbox* Rick Kern, Aman Gupta, Zhao Zhili

Sounds good on the videotoolbox files.


>
>
>  libavdevice
> --
> 2.25.1
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: add CBP/CHP profile

2023-06-03 Thread Rick Kern
On Fri, May 26, 2023 at 7:37 AM 徐福隆 <839789...@qq.com> wrote:

> Kern, please help to review this patch, which has added CBP profile with
> profile options.
> As for the VT_H264Profile replacement, let you take over, follow up this
> patch.
>
Thanks. Pushed with minor modifications.


>
> Thanks
>
> -- 原始邮件 --
> *发件人:* "xufuji456" <839789...@qq.com>;
> *发送时间:* 2023年5月23日(星期二) 上午10:27
> *收件人:* "ffmpeg-devel";
> *抄送:* "徐福隆"<839789...@qq.com>;
> *主题:* [PATCH] avcodec/videotoolboxenc: add CBP/CHP profile
>
> The Constrained_Baseline and Constrained_High profile of H264 has
> available in iOS 15.0
>
> Signed-off-by: xufuji456 <839789...@qq.com>
> ---
>  libavcodec/videotoolboxenc.c | 25 +
>  1 file changed, 21 insertions(+), 4 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index b017c90c36..028095cd04 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -108,6 +108,8 @@ static struct{
>  CFStringRef kVTProfileLevel_H264_High_AutoLevel;
>  CFStringRef kVTProfileLevel_H264_Extended_5_0;
>  CFStringRef kVTProfileLevel_H264_Extended_AutoLevel;
> +CFStringRef kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel;
> +CFStringRef kVTProfileLevel_H264_ConstrainedHigh_AutoLevel;
>
>  CFStringRef kVTProfileLevel_HEVC_Main_AutoLevel;
>  CFStringRef kVTProfileLevel_HEVC_Main10_AutoLevel;
> @@ -171,6 +173,8 @@ static void loadVTEncSymbols(void){
>  GET_SYM(kVTProfileLevel_H264_High_AutoLevel,
> "H264_High_AutoLevel");
>  GET_SYM(kVTProfileLevel_H264_Extended_5_0,   "H264_Extended_5_0");
>  GET_SYM(kVTProfileLevel_H264_Extended_AutoLevel,
> "H264_Extended_AutoLevel");
> +GET_SYM(kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel,
> "H264_ConstrainedBaseline_AutoLevel");
> +GET_SYM(kVTProfileLevel_H264_ConstrainedHigh_AutoLevel,
> "H264_ConstrainedHigh_AutoLevel");
>
>  GET_SYM(kVTProfileLevel_HEVC_Main_AutoLevel,
> "HEVC_Main_AutoLevel");
>  GET_SYM(kVTProfileLevel_HEVC_Main10_AutoLevel,
> "HEVC_Main10_AutoLevel");
> @@ -193,8 +197,10 @@ static void loadVTEncSymbols(void){
>  typedef enum VT_H264Profile {
>  H264_PROF_AUTO,
>  H264_PROF_BASELINE,
> +H264_PROF_CONSTRAINED_BASELINE,
>  H264_PROF_MAIN,
>  H264_PROF_HIGH,
> +H264_PROF_CONSTRAINED_HIGH,
>  H264_PROF_EXTENDED,
>  H264_PROF_COUNT
>  } VT_H264Profile;
> @@ -763,6 +769,10 @@ static bool get_vt_h264_profile_level(AVCodecContext
> *avctx,
>  }
>  break;
>
> +case H264_PROF_CONSTRAINED_BASELINE:
> +*profile_level_val =
> compat_keys.kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel;
> +break;
> +
>  case H264_PROF_MAIN:
>  switch (vtctx->level) {
>  case  0: *profile_level_val =
> @@ -805,6 +815,11 @@ static bool get_vt_h264_profile_level(AVCodecContext
> *avctx,
>
> compat_keys.kVTProfileLevel_H264_High_5_2;   break;
>  }
>  break;
> +
> +case H264_PROF_CONSTRAINED_HIGH:
> +*profile_level_val =
> compat_keys.kVTProfileLevel_H264_ConstrainedHigh_AutoLevel;
> +break;
> +
>  case H264_PROF_EXTENDED:
>  switch (vtctx->level) {
>  case  0: *profile_level_val =
> @@ -2757,10 +2772,12 @@ static const enum AVPixelFormat prores_pix_fmts[]
> = {
>  #define OFFSET(x) offsetof(VTEncContext, x)
>  static const AVOption h264_options[] = {
>  { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT64, { .i64 =
> H264_PROF_AUTO }, H264_PROF_AUTO, H264_PROF_COUNT, VE, "profile" },
> -{ "baseline", "Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
> -{ "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
> -{ "high", "High Profile", 0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
> -{ "extended", "Extend Profile",   0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_EXTENDED }, INT_MIN, INT_MAX, VE, "profile" },
> +{ "baseline", "Baseline Profile", 0,
> AV_OPT_TYPE_CONST, { .i64 = H264_PROF_BASELINE }, INT_MIN,
> INT_MAX, VE, "profile" },
> +{ "constrained_baseline", "Constrained_Baseline Profile", 0,
> AV_OPT_TYPE_CONST, { .i64 = H264_PROF_CONSTRAINED_BASELINE }, INT_MIN,
> INT_MAX, VE, "profile" },
> +{ "main", "Main Profile", 0,
> AV_OPT_TYPE_CONST, { .i64 = H264_PROF_MAIN }, INT_MIN,
> INT_MAX, VE, "profile" },
> +{ "high", "High Profile", 0,
> AV_OPT_TYPE_CONST, { .i64 = H264_PROF_HIGH }, INT_MIN,
> INT_MAX, VE, "profile" },
> +{ "constrained_high", "Constrained_High Profile", 0,
> AV_OPT_TYPE_CONST, { .i64 = H264_PROF_CONSTRAINED_HI

Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: replace VT_H264Profile with avctx profile

2023-05-24 Thread Rick Kern
On Mon, May 22, 2023 at 12:17 AM 徐福隆 <839789...@qq.com> wrote:

> It's my mistake that forget to remove H264_PROF_AUTO. I will fix that.
> Any other suggestions about the profile_options? Thanks.
>
Nothing else from me - just the default profile selection behavior
zhilizhao mentioned.


>
> -- 原始邮件 --
> *发件人:* ""zhilizhao(赵志立)"" ;
> *发送时间:* 2023年5月22日(星期一) 中午11:11
> *收件人:* "FFmpeg development discussions and patches"<
> ffmpeg-devel@ffmpeg.org>;
> *抄送:* "徐福隆"<839789...@qq.com>;"Rick Kern";
> *主题:* Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: replace
> VT_H264Profile with avctx profile
>
>
>
> > On May 22, 2023, at 11:05, zhilizhao(赵志立) 
> wrote:
> >
> >> On May 21, 2023, at 22:41, xufuji456 <839789...@qq.com> wrote:
> >>
> >> For compatibility with constrained_baseline in the future,
> >> replace VT_H264Profile/VT_HEVCProfile with avctx->profile.
> >>
> >> Signed-off-by: xufuji456 <839789...@qq.com>
> >> ---
> >> libavcodec/videotoolboxenc.c | 55 +++-
> >> 1 file changed, 16 insertions(+), 39 deletions(-)
> >>
> >> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> >> index b017c90c36..4966ab36ae 100644
> >> --- a/libavcodec/videotoolboxenc.c
> >> +++ b/libavcodec/videotoolboxenc.c
> >> @@ -190,28 +190,12 @@ static void loadVTEncSymbols(void){
> >>"EnableLowLatencyRateControl");
> >> }
> >>
> >> -typedef enum VT_H264Profile {
> >> -H264_PROF_AUTO,
> >> -H264_PROF_BASELINE,
> >> -H264_PROF_MAIN,
> >> -H264_PROF_HIGH,
> >> -H264_PROF_EXTENDED,
> >> -H264_PROF_COUNT
> >> -} VT_H264Profile;
> >> -
> >> typedef enum VTH264Entropy{
> >>VT_ENTROPY_NOT_SET,
> >>VT_CAVLC,
> >>VT_CABAC
> >> } VTH264Entropy;
> >>
> >> -typedef enum VT_HEVCProfile {
> >> -HEVC_PROF_AUTO,
> >> -HEVC_PROF_MAIN,
> >> -HEVC_PROF_MAIN10,
> >> -HEVC_PROF_COUNT
> >> -} VT_HEVCProfile;
> >> -
> >> static const uint8_t start_code[] = { 0, 0, 0, 1 };
> >>
> >> typedef struct ExtraSEI {
> >> @@ -730,18 +714,13 @@ static bool
> get_vt_h264_profile_level(AVCodecContext *avctx,
> >>VTEncContext *vtctx = avctx->priv_data;
> >>int64_t profile = vtctx->profile;
> >>
> >> -if (profile == H264_PROF_AUTO && vtctx->level) {
> >> -//Need to pick a profile if level is not auto-selected.
> >> -profile = vtctx->has_b_frames ? H264_PROF_MAIN :
> H264_PROF_BASELINE;
> >> -}
> >> -
> >>*profile_level_val = NULL;
> >>
> >>switch (profile) {
> >>case H264_PROF_AUTO:
> >>return true;
>
> Isn’t it failed to build since H264_PROF_AUTO isn’t defined?
>
Please be sure to compile and test before submitting a patch.


>
> >>
> >> -case H264_PROF_BASELINE:
> >> +case FF_PROFILE_H264_BASELINE:
> >>switch (vtctx->level) {
> >>case  0: *profile_level_val =
> >>
> compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel; break;
> >> @@ -763,7 +742,7 @@ static bool
> get_vt_h264_profile_level(AVCodecContext *avctx,
> >>}
> >>break;
> >>
> >> -case H264_PROF_MAIN:
> >> +case FF_PROFILE_H264_MAIN:
> >>switch (vtctx->level) {
> >>case  0: *profile_level_val =
> >>
> compat_keys.kVTProfileLevel_H264_Main_AutoLevel; break;
> >> @@ -782,7 +761,7 @@ static bool
> get_vt_h264_profile_level(AVCodecContext *avctx,
> >>}
> >>break;
> >>
> >> -case H264_PROF_HIGH:
> >> +case FF_PROFILE_H264_HIGH:
> >>switch (vtctx->level) {
> >>case  0: *profile_level_val =
> >>
> compat_keys.kVTProfileLevel_H264_High_AutoLevel; break;
> >> @@ -805,7 +784,7 @@ static bool
> get_vt_h264_profile_level(AVCodecContext *avctx,
> >>
> compat_keys.kVTProfileLevel_H264_High_5_2;   break;
> >>}
> >>break;
> >> -case H264_PROF_EXTENDED:
> >> +case FF_PROFILE_H264_EXTENDED:
> >>  

Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: add CBP/CHP profile

2023-05-21 Thread Rick Kern
On Sun, May 21, 2023 at 7:22 AM xufuji456 <839789...@qq.com> wrote:

> The CBP/CHP profile has available with H264 in iOS 15.0.
> Official Doc:
> https://developer.apple.com/documentation/videotoolbox/kvtprofilelevel_h264_constrainedbaseline_autolevel
>
> Signed-off-by: xufuji456 <839789...@qq.com>
> ---
>  libavcodec/videotoolboxenc.c | 24 ++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index b017c90c36..6a9b19831b 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -108,6 +108,8 @@ static struct{
>  CFStringRef kVTProfileLevel_H264_High_AutoLevel;
>  CFStringRef kVTProfileLevel_H264_Extended_5_0;
>  CFStringRef kVTProfileLevel_H264_Extended_AutoLevel;
> +CFStringRef kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel;
> +CFStringRef kVTProfileLevel_H264_ConstrainedHigh_AutoLevel;
>
>  CFStringRef kVTProfileLevel_HEVC_Main_AutoLevel;
>  CFStringRef kVTProfileLevel_HEVC_Main10_AutoLevel;
> @@ -171,6 +173,8 @@ static void loadVTEncSymbols(void){
>  GET_SYM(kVTProfileLevel_H264_High_AutoLevel,
>  "H264_High_AutoLevel");
>  GET_SYM(kVTProfileLevel_H264_Extended_5_0,   "H264_Extended_5_0");
>  GET_SYM(kVTProfileLevel_H264_Extended_AutoLevel,
> "H264_Extended_AutoLevel");
> +GET_SYM(kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel,
> "H264_ConstrainedBaseline_AutoLevel");
> +GET_SYM(kVTProfileLevel_H264_ConstrainedHigh_AutoLevel,
>  "H264_ConstrainedHigh_AutoLevel");
>
>  GET_SYM(kVTProfileLevel_HEVC_Main_AutoLevel,
>  "HEVC_Main_AutoLevel");
>  GET_SYM(kVTProfileLevel_HEVC_Main10_AutoLevel,
>  "HEVC_Main10_AutoLevel");
> @@ -743,8 +747,15 @@ static bool get_vt_h264_profile_level(AVCodecContext
> *avctx,
>
>  case H264_PROF_BASELINE:
>  switch (vtctx->level) {
> -case  0: *profile_level_val =
> -
> compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel; break;
> +case  0: {
> +// check avctx->profile has been set
> constrained_baseline
> +if (avctx->profile ==
> FF_PROFILE_H264_CONSTRAINED_BASELINE) {
>
The profiles need to be added to the list of usable profiles in
h264_options so they can be used on the command line.

Using the FF_PROFILE_ constants doesn't follow the pattern of using the
profile constants defined in this file, but I like your approach better. To
keep things consistent, either remove the constants in VT_H264Profile and
VT_HEVCProfile and replace them with global constants, or I can follow up
with a patch.


> +*profile_level_val =
> compat_keys.kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel;
> +} else {
> +*profile_level_val =
> compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel;
> +}
> +break;
> +}
>  case 13: *profile_level_val =
> kVTProfileLevel_H264_Baseline_1_3;   break;
>  case 30: *profile_level_val =
> kVTProfileLevel_H264_Baseline_3_0;   break;
>  case 31: *profile_level_val =
> kVTProfileLevel_H264_Baseline_3_1;   break;
> @@ -784,6 +795,15 @@ static bool get_vt_h264_profile_level(AVCodecContext
> *avctx,
>
>  case H264_PROF_HIGH:
>  switch (vtctx->level) {
> +case  0: {
> +// check avctx->profile has been set constrained_high
> +if (avctx->profile == (FF_PROFILE_H264_HIGH |
> FF_PROFILE_H264_CONSTRAINED)) {
> +*profile_level_val =
> compat_keys.kVTProfileLevel_H264_ConstrainedHigh_AutoLevel;
> +} else {
> +*profile_level_val =
> compat_keys.kVTProfileLevel_H264_High_AutoLevel;
> +}
> +break;
> +}
>  case  0: *profile_level_val =
>
>  compat_keys.kVTProfileLevel_H264_High_AutoLevel; break;
>
This duplicate case 0 can be removed.

 case 30: *profile_level_val =
> --
> 2.32.0 (Apple Git-132)
>
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH] lavc/videotoolboxenc: support additional options

2023-05-20 Thread Rick Kern
Added support for more VideoToolbox encoder options:
- qmin and qmax options are now used
- max_slice_bytes: Max number of bytes per H.264 slice
- max_ref_frames: Limit the number of reference frames
- Disable open GOP when the cgop flag is set
- power_efficient: Enable power-efficient mode
---
 libavcodec/videotoolboxenc.c | 177 ++-
 1 file changed, 176 insertions(+), 1 deletion(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index b017c90c36..be29d13629 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -120,6 +120,11 @@ static struct{
 CFStringRef 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
 CFStringRef 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
 CFStringRef kVTVideoEncoderSpecification_EnableLowLatencyRateControl;
+CFStringRef kVTCompressionPropertyKey_AllowOpenGOP;
+CFStringRef kVTCompressionPropertyKey_MaximizePowerEfficiency;
+CFStringRef kVTCompressionPropertyKey_ReferenceBufferCount;
+CFStringRef kVTCompressionPropertyKey_MaxAllowedFrameQP;
+CFStringRef kVTCompressionPropertyKey_MinAllowedFrameQP;
 
 getParameterSetAtIndex CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
 } compat_keys;
@@ -188,6 +193,13 @@ static void loadVTEncSymbols(void){
 "RequireHardwareAcceleratedVideoEncoder");
 GET_SYM(kVTVideoEncoderSpecification_EnableLowLatencyRateControl,
 "EnableLowLatencyRateControl");
+GET_SYM(kVTCompressionPropertyKey_AllowOpenGOP, "AllowOpenGOP");
+GET_SYM(kVTCompressionPropertyKey_MaximizePowerEfficiency,
+"MaximizePowerEfficiency");
+GET_SYM(kVTCompressionPropertyKey_ReferenceBufferCount,
+"ReferenceBufferCount");
+GET_SYM(kVTCompressionPropertyKey_MaxAllowedFrameQP, "MaxAllowedFrameQP");
+GET_SYM(kVTCompressionPropertyKey_MinAllowedFrameQP, "MinAllowedFrameQP");
 }
 
 typedef enum VT_H264Profile {
@@ -268,6 +280,10 @@ typedef struct VTEncContext {
 
 /* can't be bool type since AVOption will access it as int */
 int a53_cc;
+
+int max_slice_bytes;
+int power_efficient;
+int max_ref_frames;
 } VTEncContext;
 
 static int vtenc_populate_extradata(AVCodecContext   *avctx,
@@ -1106,6 +1122,78 @@ static bool vtenc_qscale_enabled(void)
 return !TARGET_OS_IPHONE && TARGET_CPU_ARM64;
 }
 
+static int set_encoder_int_property_or_log(
+AVCodecContext* avctx,
+CFStringRef key,
+const char* print_option_name,
+int value) {
+int status;
+VTEncContext *vtctx = avctx->priv_data;
+CFNumberRef value_cfnum = CFNumberCreate(
+kCFAllocatorDefault,
+kCFNumberIntType,
+&value);
+
+if (value_cfnum == NULL) {
+return AVERROR(ENOMEM);
+}
+
+status = VTSessionSetProperty(vtctx->session, key, value_cfnum);
+if (status == kVTPropertyNotSupportedErr) {
+av_log(
+avctx,
+AV_LOG_INFO,
+"This device does not support the %s option. Value %d ignored.\n",
+print_option_name,
+value);
+} else if (status != 0) {
+av_log(
+avctx,
+AV_LOG_ERROR,
+"Error setting %s=%d: Error %d\n",
+print_option_name,
+value,
+status);
+}
+
+CFRelease (value_cfnum);
+
+return 0;
+}
+
+static int set_encoder_bool_property_or_log(
+AVCodecContext* avctx,
+CFStringRef key,
+const char* print_option_name,
+int value) {
+int status;
+VTEncContext *vtctx = avctx->priv_data;
+
+status = VTSessionSetProperty(
+vtctx->session,
+key,
+value ? kCFBooleanTrue : kCFBooleanFalse);
+
+if (status == kVTPropertyNotSupportedErr) {
+av_log(
+avctx,
+AV_LOG_INFO,
+"This device does not support the %s option. Value %d ignored.\n",
+print_option_name,
+value);
+} else if (status != 0) {
+av_log(
+avctx,
+AV_LOG_ERROR,
+"Error setting %s=%d: Error %d\n",
+print_option_name,
+value,
+status);
+}
+
+return 0;
+}
+
 static int vtenc_create_encoder(AVCodecContext   *avctx,
 CMVideoCodecType codec_type,
 CFStringRef  profile_level,
@@ -1476,6 +1564,81 @@ static int vtenc_create_encoder(AVCodecContext   *avctx,
 }
 }
 
+if ((avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) != 0) {
+set_encoder_bool_property_or_log(
+avctx,
+compat_keys.kVTCompressionPropertyKey_AllowOpenGOP,
+"AllowOpenGop",
+0);
+
+if (status) {
+return status;
+}
+}
+
+if (avctx->qmin >= 0) {
+status = set_encoder_int_property_or_log(
+avctx,
+compat_keys.kVTCompressionPropertyKey_MinAllowe

Re: [FFmpeg-devel] 回复: [PATCH] avcodec/videotoolboxenc: add low-latency encoding

2023-05-20 Thread Rick Kern
On Fri, May 19, 2023 at 2:06 PM Zhao Zhili  wrote:

>
> > From: ffmpeg-devel  On Behalf Of Rick
> Kern
> > Sent: 2023年5月19日 21:37
> > To: FFmpeg development discussions and patches 
> > Cc: zhilizhao(赵志立) 
> > Subject: Re: [FFmpeg-devel] 回复: [PATCH] avcodec/videotoolboxenc: add
> low-latency encoding
> >
> > On Fri, May 19, 2023 at 1:56 AM 徐福隆 <839789...@qq.com> wrote:
> >
> > > Thank you for your review, and point out the details.
> > > Actually, I have also considered this issue.
> > > I see that some keys are used directly, some are used by compatibility.
> > >
> >
> > The compat_keys are used for symbols that might not exist. For constants
> > available in the earliest version of VideoToolbox, compat_keys doesn't
> need
> > to be used.
>
> I curious to know what's the minimum supported version of macOS which
> doesn't need dynamic loading symbols. I can't find such information. The
> question
> applies to other OS too. It's not clear from configure, and I can't find
> the relevant
> documentation.
>
VideoToolbox was released in MacOS 10.8 and iOS 8.0. I'll update the
compat_keys docs with this info.


> >
> > Your patch looks good - I'll push it shortly.
> >
> >
> > > I will use compat_key instead, and submit a new patch.
> > >
> > >
> > > -- 原始邮件 --
> > > 发件人:
> > >   ""zhilizhao(赵志立)""
> > >
> > >  > > 发送时间: 2023年5月19日(星期五) 中午11:34
> > > 收件人: "FFmpeg development discussions and patches"<
> > > ffmpeg-devel@ffmpeg.org>;
> > > 抄送: "徐福隆"<839789...@qq.com>;
> > > 主题: Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: add
> > > low-latency encoding
> > >
> > >
> > >
> > >
> > >
> > > > On May 19, 2023, at 11:17, xufuji456 <839789...@qq.com> wrote:
> > > >
> > > > When using low-latency mode, it eliminates frame reordering
> > > > and follows a one-in-one-out encoding mode
> > > >
> > > > Signed-off-by: xufuji456 <839789...@qq.com>
> > > > ---
> > > > libavcodec/videotoolboxenc.c | 11 +++
> > > > 1 file changed, 11 insertions(+)
> > > >
> > > > diff --git a/libavcodec/videotoolboxenc.c
> > > b/libavcodec/videotoolboxenc.c
> > > > index c6f22723d6..ae1ef32b2d 100644
> > > > --- a/libavcodec/videotoolboxenc.c
> > > > +++ b/libavcodec/videotoolboxenc.c
> > > > @@ -1441,6 +1441,17 @@ static int
> > > vtenc_create_encoder(AVCodecContext   *avctx,
> > > > }
> > > > }
> > > >
> > > > +    // low-latency mode: eliminate frame
> reordering,
> > > follow a one-in-one-out encoding mode
> > > > +    if ((avctx->flags &
> > > AV_CODEC_FLAG_LOW_DELAY) && avctx->codec_id ==
> AV_CODEC_ID_H264)
> > > {
> > > > +    status =
> > > VTSessionSetProperty(vtctx->session,
> > > >
> > >
> >
> +   
> >
> ;  
> > > kVTVideoEncoderSpecification_EnableLowLatencyRateControl,
> > > >
> > >
> >
> +   
> >
> ;  
> > > kCFBooleanTrue);
> > >
> > > Please use compat_keys in case of
> > > kVTVideoEncoderSpecification_EnableLowLatencyRateControl
> > > isn’t available.
> > >
> > > > +
> > > > +    if (status) {
> > > >
> +   
> > > av_log(avctx, AV_LOG_ERROR, "Error setting low latency property: %d\n",
> > > status);
> > > > +    }
> > > > +    }
> > > > +
> > > > status =
> > > VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
> > > > if (status) {
> > > > av_log(avctx,
> > > AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);
> > > > --
> > > > 2.32.0 (Apple Git-132)
> > > >
> > > > ___
> > > > ffmpeg-devel mailing list
> > > > ffmpeg-devel@ffmpeg.org
> > > > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > > >
> > > > To unsubscribe, visit link above, or email
> > > > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> > > ___
> > > ffmpeg-devel mailing list
> > > ffmpeg-devel@ffmpeg.org
> > > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > >
> > > To unsubscribe, visit link above, or email
> > > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> > >
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] 回复: [PATCH] avcodec/videotoolboxenc: add low-latency encoding

2023-05-19 Thread Rick Kern
On Fri, May 19, 2023 at 1:56 AM 徐福隆 <839789...@qq.com> wrote:

> Thank you for your review, and point out the details.
> Actually, I have also considered this issue.
> I see that some keys are used directly, some are used by compatibility.
>

The compat_keys are used for symbols that might not exist. For constants
available in the earliest version of VideoToolbox, compat_keys doesn't need
to be used.

Your patch looks good - I'll push it shortly.


> I will use compat_key instead, and submit a new patch.
>
>
> -- 原始邮件 --
> 发件人:
>   ""zhilizhao(赵志立)""
>
>  发送时间: 2023年5月19日(星期五) 中午11:34
> 收件人: "FFmpeg development discussions and patches"<
> ffmpeg-devel@ffmpeg.org>;
> 抄送: "徐福隆"<839789...@qq.com>;
> 主题: Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: add
> low-latency encoding
>
>
>
>
>
> > On May 19, 2023, at 11:17, xufuji456 <839789...@qq.com> wrote:
> >
> > When using low-latency mode, it eliminates frame reordering
> > and follows a one-in-one-out encoding mode
> >
> > Signed-off-by: xufuji456 <839789...@qq.com>
> > ---
> > libavcodec/videotoolboxenc.c | 11 +++
> > 1 file changed, 11 insertions(+)
> >
> > diff --git a/libavcodec/videotoolboxenc.c
> b/libavcodec/videotoolboxenc.c
> > index c6f22723d6..ae1ef32b2d 100644
> > --- a/libavcodec/videotoolboxenc.c
> > +++ b/libavcodec/videotoolboxenc.c
> > @@ -1441,6 +1441,17 @@ static int
> vtenc_create_encoder(AVCodecContext   *avctx,
> > }
> > }
> >
> > +    // low-latency mode: eliminate frame reordering,
> follow a one-in-one-out encoding mode
> > +    if ((avctx->flags &
> AV_CODEC_FLAG_LOW_DELAY) && avctx->codec_id == AV_CODEC_ID_H264)
> {
> > +    status =
> VTSessionSetProperty(vtctx->session,
> >
> + 
> kVTVideoEncoderSpecification_EnableLowLatencyRateControl,
> >
> + 
> kCFBooleanTrue);
>
> Please use compat_keys in case of
> kVTVideoEncoderSpecification_EnableLowLatencyRateControl
> isn’t available.
>
> > +
> > +    if (status) {
> > +   
> av_log(avctx, AV_LOG_ERROR, "Error setting low latency property: %d\n",
> status);
> > +    }
> > +    }
> > +
> > status =
> VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
> > if (status) {
> > av_log(avctx,
> AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);
> > --
> > 2.32.0 (Apple Git-132)
> >
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavc/videotoolbox: validate vt context in the decoder callback

2022-12-05 Thread Rick Kern
On Sun, Dec 4, 2022 at 12:51 PM Alessandro Di Nepi <
alessandro.din...@gmail.com> wrote:

> On 4 Dec 2022, 17:01 +0200, FFmpeg development discussions and patches <
> ffmpeg-devel@ffmpeg.org>, wrote:
> > When this happens, does it continue happening, or is it transient? My
> main
> > concern is log spamming.
> Good question: this is just a transient state, so that it won't continue
> happening.
> To give you some context: when the decoding start, the value of `vtctx` is
> captured "too" early so that the first time the callback is called, it's
> still NULL.
> The next time it will have a proper value.
>
If the code isn't setting a variable in time, that issue should be fixed.
Otherwise the decoder will drop frames.

___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavc/videotoolbox: validate vt context in the decoder callback

2022-12-04 Thread Rick Kern
On Tue, Nov 29, 2022 at 11:47 AM Alessandro Di Nepi <
alessandro.din...@gmail.com> wrote:

> Just to add that this fix, once approved, should be cherry-picked to all
> the release branches where d7f4ad88a0df3c1339e142957bf2c40cd056b8ce has
> been cherry-picked.
> Basically, 4.4, 5.0, and 5.1.
>
> Thanks
> On 27 Nov 2022, 19:34 +0200, Alessandro Di Nepi <
> alessandro.din...@gmail.com>, wrote:
> > The commit d7f4ad88a0df3c1339e142957bf2c40cd056b8ce introduced a race
> > condition where the passed opaque pointer reference might be NULL,
> > when the decoding process starts.
> > This patch checks that vtctx has a value before accessing it.
> >
> > This patch fixes #10079.
> >
> > Signed-off-by: Alessandro Di Nepi 
> > ---
> > libavcodec/videotoolbox.c | 5 +
> > 1 file changed, 5 insertions(+)
> >
> > diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c
> > index 1b1be8ddb4..615e2b087a 100644
> > --- a/libavcodec/videotoolbox.c
> > +++ b/libavcodec/videotoolbox.c
> > @@ -692,6 +692,11 @@ static void videotoolbox_decoder_callback(void
> *opaque,
> > {
> > VTContext *vtctx = opaque;
> >
> > + if (!vtctx) {
>

When this happens, does it continue happening, or is it transient? My main
concern is log spamming.


> > + av_log(NULL, AV_LOG_WARNING, "vt decoder cb: vt context is null");
> > + return;
> > + }
> > +
> > if (vtctx->frame) {
> > CVPixelBufferRelease(vtctx->frame);
> > vtctx->frame = NULL;
> > --
> > 2.37.1 (Apple Git-137.1)
> >
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 2/2] lavc/videotoolboxenc: use objectpool for encoder output information

2022-12-03 Thread Rick Kern
Instead of allocating/freeing, use objpool for data passed from an
input frame to an output packet.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 80 
 1 file changed, 71 insertions(+), 9 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index dc9e321d3d..184feb029b 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -24,6 +24,7 @@
 #include 
 #include 
 #include "avcodec.h"
+#include "libavutil/objpool.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -251,8 +252,49 @@ typedef struct VTEncContext {
 
 /* can't be bool type since AVOption will access it as int */
 int a53_cc;
+
+AVObjPool *output_node_pool;
 } VTEncContext;
 
+static BufNode *output_node_alloc(void) { return av_mallocz(sizeof(BufNode)); }
+static void output_node_free(BufNode **node) { av_freep(node); }
+static void output_node_reset(BufNode *node) {
+if (node->cm_buffer != NULL) {
+CFRelease(node->cm_buffer);
+}
+
+if (node->sei != NULL) {
+if (node->sei->data != NULL) {
+av_free(node->sei->data);
+}
+
+av_free(node->sei);
+}
+
+memset(node, 0, sizeof(*node));
+}
+
+static int alloc_output_node_pool(AVObjPool **pool_out) {
+AVObjPool *pool = NULL;
+
+if (pool_out == NULL) {
+return AVERROR(EINVAL);
+}
+
+pool = av_objpool_alloc(
+(AVObjPoolCBAlloc)output_node_alloc,
+(AVObjPoolCBReset)output_node_reset,
+(AVObjPoolCBFree)output_node_free);
+
+if (pool == NULL) {
+return AVERROR(ENOMEM);
+}
+
+*pool_out = pool;
+
+return 0;
+}
+
 static int vtenc_populate_extradata(AVCodecContext   *avctx,
 CMVideoCodecType codec_type,
 CFStringRef  profile_level,
@@ -285,8 +327,7 @@ static void set_async_error(VTEncContext *vtctx, int err)
 
 while (info) {
 BufNode *next = info->next;
-CFRelease(info->cm_buffer);
-av_free(info);
+av_objpool_release(vtctx->output_node_pool, (void**)&info);
 info = next;
 }
 
@@ -336,22 +377,29 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait, 
CMSampleBufferRef *buf, E
 pthread_mutex_unlock(&vtctx->lock);
 
 *buf = info->cm_buffer;
+if (*buf != NULL) {
+CFRetain(*buf);
+}
+
 if (sei && *buf) {
 *sei = info->sei;
-} else if (info->sei) {
-if (info->sei->data) av_free(info->sei->data);
-av_free(info->sei);
+info->sei = NULL;
 }
-av_free(info);
 
+av_objpool_release(vtctx->output_node_pool, (void**)&info);
 
 return 0;
 }
 
 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, 
ExtraSEI *sei)
 {
-BufNode *info = av_malloc(sizeof(BufNode));
-if (!info) {
+BufNode *info = NULL;
+int status = av_objpool_get(vtctx->output_node_pool, (void**)&info);
+
+if (status < 0) {
+set_async_error(vtctx, status);
+return;
+} else if (!info) {
 set_async_error(vtctx, AVERROR(ENOMEM));
 return;
 }
@@ -1569,6 +1617,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 pthread_mutex_init(&vtctx->lock, NULL);
 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
 
+status = alloc_output_node_pool(&vtctx->output_node_pool);
+
+if (status < 0) {
+av_log(
+avctx,
+AV_LOG_ERROR,
+"Failed to allocate output pool (%d)\n",
+status);
+}
+
 vtctx->session = NULL;
 status = vtenc_configure_encoder(avctx);
 if (status) return status;
@@ -1589,7 +1647,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 avctx->has_b_frames = vtctx->has_b_frames;
 
-return 0;
+return status;
 }
 
 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
@@ -2647,6 +2705,10 @@ static av_cold int vtenc_close(AVCodecContext *avctx)
 vtctx->ycbcr_matrix = NULL;
 }
 
+if (vtctx->output_node_pool != NULL) {
+av_objpool_free(&vtctx->output_node_pool);
+}
+
 return 0;
 }
 
-- 
2.38.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 1/2] fftools/objpool: move objpool to libavutil

2022-12-03 Thread Rick Kern
Moved objpool to libavutil to make it available outside of fftools.

Signed-off-by: Rick Kern 
---
 fftools/Makefile |  1 -
 fftools/ffmpeg_mux.c |  8 
 fftools/sync_queue.c | 18 +-
 fftools/thread_queue.c   | 14 +++---
 fftools/thread_queue.h   |  4 ++--
 libavutil/Makefile   |  2 ++
 {fftools => libavutil}/objpool.c | 32 +---
 {fftools => libavutil}/objpool.h | 30 --
 8 files changed, 57 insertions(+), 52 deletions(-)
 rename {fftools => libavutil}/objpool.c (77%)
 rename {fftools => libavutil}/objpool.h (56%)

diff --git a/fftools/Makefile b/fftools/Makefile
index 8ac38e75d2..25b11deaff 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -16,7 +16,6 @@ OBJS-ffmpeg +=  \
 fftools/ffmpeg_mux.o\
 fftools/ffmpeg_mux_init.o   \
 fftools/ffmpeg_opt.o\
-fftools/objpool.o   \
 fftools/sync_queue.o\
 fftools/thread_queue.o  \
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 20524e5a28..8e4dd0facf 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -22,7 +22,6 @@
 
 #include "ffmpeg.h"
 #include "ffmpeg_mux.h"
-#include "objpool.h"
 #include "sync_queue.h"
 #include "thread_queue.h"
 
@@ -30,6 +29,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/mem.h"
+#include "libavutil/objpool.h"
 #include "libavutil/timestamp.h"
 #include "libavutil/thread.h"
 
@@ -398,16 +398,16 @@ static void pkt_move(void *dst, void *src)
 static int thread_start(Muxer *mux)
 {
 AVFormatContext *fc = mux->fc;
-ObjPool *op;
+AVObjPool *op;
 int ret;
 
-op = objpool_alloc_packets();
+op = av_objpool_alloc_packets();
 if (!op)
 return AVERROR(ENOMEM);
 
 mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move);
 if (!mux->tq) {
-objpool_free(&op);
+av_objpool_free(&op);
 return AVERROR(ENOMEM);
 }
 
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index c2b23ee4f5..bfa91bc129 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -24,8 +24,8 @@
 #include "libavutil/fifo.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
+#include "libavutil/objpool.h"
 
-#include "objpool.h"
 #include "sync_queue.h"
 
 typedef struct SyncQueueStream {
@@ -60,7 +60,7 @@ struct SyncQueue {
 unsigned int  nb_streams;
 
 // pool of preallocated frames to avoid constant allocations
-ObjPool *pool;
+AVObjPool *pool;
 };
 
 static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
@@ -250,7 +250,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, 
SyncQueueFrame frame)
 if (st->finished)
 return AVERROR_EOF;
 
-ret = objpool_get(sq->pool, (void**)&dst);
+ret = av_objpool_get(sq->pool, (void**)&dst);
 if (ret < 0)
 return ret;
 
@@ -261,7 +261,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, 
SyncQueueFrame frame)
 ret = av_fifo_write(st->fifo, &dst, 1);
 if (ret < 0) {
 frame_move(sq, frame, dst);
-objpool_release(sq->pool, (void**)&dst);
+av_objpool_release(sq->pool, (void**)&dst);
 return ret;
 }
 
@@ -302,7 +302,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int 
stream_idx,
  */
 if (cmp <= 0 || ts == AV_NOPTS_VALUE) {
 frame_move(sq, frame, peek);
-objpool_release(sq->pool, (void**)&peek);
+av_objpool_release(sq->pool, (void**)&peek);
 av_fifo_drain2(st->fifo, 1);
 return 0;
 }
@@ -415,8 +415,8 @@ SyncQueue *sq_alloc(enum SyncQueueType type, int64_t 
buf_size_us)
 sq->head_stream  = -1;
 sq->head_finished_stream = -1;
 
-sq->pool = (type == SYNC_QUEUE_PACKETS) ? objpool_alloc_packets() :
-  objpool_alloc_frames();
+sq->pool = (type == SYNC_QUEUE_PACKETS) ? av_objpool_alloc_packets() :
+  av_objpool_alloc_frames();
 if (!sq->pool) {
 av_freep(&sq);
 return NULL;
@@ -435,14 +435,14 @@ void sq_free(SyncQueue **psq)
 for (unsigned int i = 0; i < sq->nb_streams; i++) {
 SyncQueueFrame frame;
 while (av_fifo_read(sq->streams[i].fifo, &frame, 1) >= 0)
-objpool_release(sq->pool, (void**)&frame);
+av_objpool_release(sq->pool, (void**)&frame);
 
 av_fifo_freep2(&sq->streams[i].fifo);
 }
 
 av_freep(&sq->streams);
 
-objpool_fr

[FFmpeg-devel] [PATCH v3 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame

2022-12-03 Thread Rick Kern
Update inclusion guards in objpool.h start with AVUTIL_ instead of FFTOOLS_.

Rick Kern (2):
  fftools/objpool: move objpool to libavutil
  lavc/videotoolboxenc: use objectpool for encoder output information

 fftools/Makefile |  1 -
 fftools/ffmpeg_mux.c |  8 ++--
 fftools/sync_queue.c | 18 +++
 fftools/thread_queue.c   | 14 +++---
 fftools/thread_queue.h   |  4 +-
 libavcodec/videotoolboxenc.c | 80 
 libavutil/Makefile   |  2 +
 {fftools => libavutil}/objpool.c | 32 +++--
 {fftools => libavutil}/objpool.h | 30 ++--
 9 files changed, 128 insertions(+), 61 deletions(-)
 rename {fftools => libavutil}/objpool.c (77%)
 rename {fftools => libavutil}/objpool.h (56%)

-- 
2.38.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 2/2] lavc/videotoolboxenc: use objectpool for encoder output information

2022-12-03 Thread Rick Kern
Instead of allocating/freeing, use objpool for data passed from an
input frame to an output packet.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 80 
 1 file changed, 71 insertions(+), 9 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index dc9e321d3d..184feb029b 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -24,6 +24,7 @@
 #include 
 #include 
 #include "avcodec.h"
+#include "libavutil/objpool.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -251,8 +252,49 @@ typedef struct VTEncContext {
 
 /* can't be bool type since AVOption will access it as int */
 int a53_cc;
+
+AVObjPool *output_node_pool;
 } VTEncContext;
 
+static BufNode *output_node_alloc(void) { return av_mallocz(sizeof(BufNode)); }
+static void output_node_free(BufNode **node) { av_freep(node); }
+static void output_node_reset(BufNode *node) {
+if (node->cm_buffer != NULL) {
+CFRelease(node->cm_buffer);
+}
+
+if (node->sei != NULL) {
+if (node->sei->data != NULL) {
+av_free(node->sei->data);
+}
+
+av_free(node->sei);
+}
+
+memset(node, 0, sizeof(*node));
+}
+
+static int alloc_output_node_pool(AVObjPool **pool_out) {
+AVObjPool *pool = NULL;
+
+if (pool_out == NULL) {
+return AVERROR(EINVAL);
+}
+
+pool = av_objpool_alloc(
+(AVObjPoolCBAlloc)output_node_alloc,
+(AVObjPoolCBReset)output_node_reset,
+(AVObjPoolCBFree)output_node_free);
+
+if (pool == NULL) {
+return AVERROR(ENOMEM);
+}
+
+*pool_out = pool;
+
+return 0;
+}
+
 static int vtenc_populate_extradata(AVCodecContext   *avctx,
 CMVideoCodecType codec_type,
 CFStringRef  profile_level,
@@ -285,8 +327,7 @@ static void set_async_error(VTEncContext *vtctx, int err)
 
 while (info) {
 BufNode *next = info->next;
-CFRelease(info->cm_buffer);
-av_free(info);
+av_objpool_release(vtctx->output_node_pool, (void**)&info);
 info = next;
 }
 
@@ -336,22 +377,29 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait, 
CMSampleBufferRef *buf, E
 pthread_mutex_unlock(&vtctx->lock);
 
 *buf = info->cm_buffer;
+if (*buf != NULL) {
+CFRetain(*buf);
+}
+
 if (sei && *buf) {
 *sei = info->sei;
-} else if (info->sei) {
-if (info->sei->data) av_free(info->sei->data);
-av_free(info->sei);
+info->sei = NULL;
 }
-av_free(info);
 
+av_objpool_release(vtctx->output_node_pool, (void**)&info);
 
 return 0;
 }
 
 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, 
ExtraSEI *sei)
 {
-BufNode *info = av_malloc(sizeof(BufNode));
-if (!info) {
+BufNode *info = NULL;
+int status = av_objpool_get(vtctx->output_node_pool, (void**)&info);
+
+if (status < 0) {
+set_async_error(vtctx, status);
+return;
+} else if (!info) {
 set_async_error(vtctx, AVERROR(ENOMEM));
 return;
 }
@@ -1569,6 +1617,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 pthread_mutex_init(&vtctx->lock, NULL);
 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
 
+status = alloc_output_node_pool(&vtctx->output_node_pool);
+
+if (status < 0) {
+av_log(
+avctx,
+AV_LOG_ERROR,
+"Failed to allocate output pool (%d)\n",
+status);
+}
+
 vtctx->session = NULL;
 status = vtenc_configure_encoder(avctx);
 if (status) return status;
@@ -1589,7 +1647,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 avctx->has_b_frames = vtctx->has_b_frames;
 
-return 0;
+return status;
 }
 
 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
@@ -2647,6 +2705,10 @@ static av_cold int vtenc_close(AVCodecContext *avctx)
 vtctx->ycbcr_matrix = NULL;
 }
 
+if (vtctx->output_node_pool != NULL) {
+av_objpool_free(&vtctx->output_node_pool);
+}
+
 return 0;
 }
 
-- 
2.38.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 1/2] fftools/objpool: move objpool to libavutil

2022-12-03 Thread Rick Kern
Moved objpool to libavutil to make it available outside of fftools.

Signed-off-by: Rick Kern 
---
 fftools/Makefile |  1 -
 fftools/ffmpeg_mux.c |  8 
 fftools/sync_queue.c | 18 +-
 fftools/thread_queue.c   | 14 +++---
 fftools/thread_queue.h   |  4 ++--
 libavutil/Makefile   |  2 ++
 {fftools => libavutil}/objpool.c | 32 +---
 {fftools => libavutil}/objpool.h | 24 +---
 8 files changed, 54 insertions(+), 49 deletions(-)
 rename {fftools => libavutil}/objpool.c (77%)
 rename {fftools => libavutil}/objpool.h (62%)

diff --git a/fftools/Makefile b/fftools/Makefile
index 8ac38e75d2..25b11deaff 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -16,7 +16,6 @@ OBJS-ffmpeg +=  \
 fftools/ffmpeg_mux.o\
 fftools/ffmpeg_mux_init.o   \
 fftools/ffmpeg_opt.o\
-fftools/objpool.o   \
 fftools/sync_queue.o\
 fftools/thread_queue.o  \
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 20524e5a28..8e4dd0facf 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -22,7 +22,6 @@
 
 #include "ffmpeg.h"
 #include "ffmpeg_mux.h"
-#include "objpool.h"
 #include "sync_queue.h"
 #include "thread_queue.h"
 
@@ -30,6 +29,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/mem.h"
+#include "libavutil/objpool.h"
 #include "libavutil/timestamp.h"
 #include "libavutil/thread.h"
 
@@ -398,16 +398,16 @@ static void pkt_move(void *dst, void *src)
 static int thread_start(Muxer *mux)
 {
 AVFormatContext *fc = mux->fc;
-ObjPool *op;
+AVObjPool *op;
 int ret;
 
-op = objpool_alloc_packets();
+op = av_objpool_alloc_packets();
 if (!op)
 return AVERROR(ENOMEM);
 
 mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move);
 if (!mux->tq) {
-objpool_free(&op);
+av_objpool_free(&op);
 return AVERROR(ENOMEM);
 }
 
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index c2b23ee4f5..bfa91bc129 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -24,8 +24,8 @@
 #include "libavutil/fifo.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
+#include "libavutil/objpool.h"
 
-#include "objpool.h"
 #include "sync_queue.h"
 
 typedef struct SyncQueueStream {
@@ -60,7 +60,7 @@ struct SyncQueue {
 unsigned int  nb_streams;
 
 // pool of preallocated frames to avoid constant allocations
-ObjPool *pool;
+AVObjPool *pool;
 };
 
 static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
@@ -250,7 +250,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, 
SyncQueueFrame frame)
 if (st->finished)
 return AVERROR_EOF;
 
-ret = objpool_get(sq->pool, (void**)&dst);
+ret = av_objpool_get(sq->pool, (void**)&dst);
 if (ret < 0)
 return ret;
 
@@ -261,7 +261,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, 
SyncQueueFrame frame)
 ret = av_fifo_write(st->fifo, &dst, 1);
 if (ret < 0) {
 frame_move(sq, frame, dst);
-objpool_release(sq->pool, (void**)&dst);
+av_objpool_release(sq->pool, (void**)&dst);
 return ret;
 }
 
@@ -302,7 +302,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int 
stream_idx,
  */
 if (cmp <= 0 || ts == AV_NOPTS_VALUE) {
 frame_move(sq, frame, peek);
-objpool_release(sq->pool, (void**)&peek);
+av_objpool_release(sq->pool, (void**)&peek);
 av_fifo_drain2(st->fifo, 1);
 return 0;
 }
@@ -415,8 +415,8 @@ SyncQueue *sq_alloc(enum SyncQueueType type, int64_t 
buf_size_us)
 sq->head_stream  = -1;
 sq->head_finished_stream = -1;
 
-sq->pool = (type == SYNC_QUEUE_PACKETS) ? objpool_alloc_packets() :
-  objpool_alloc_frames();
+sq->pool = (type == SYNC_QUEUE_PACKETS) ? av_objpool_alloc_packets() :
+  av_objpool_alloc_frames();
 if (!sq->pool) {
 av_freep(&sq);
 return NULL;
@@ -435,14 +435,14 @@ void sq_free(SyncQueue **psq)
 for (unsigned int i = 0; i < sq->nb_streams; i++) {
 SyncQueueFrame frame;
 while (av_fifo_read(sq->streams[i].fifo, &frame, 1) >= 0)
-objpool_release(sq->pool, (void**)&frame);
+av_objpool_release(sq->pool, (void**)&frame);
 
 av_fifo_freep2(&sq->streams[i].fifo);
 }
 
 av_freep(&sq->streams);
 
-objpool_free(&am

[FFmpeg-devel] [PATCH v2 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame

2022-12-03 Thread Rick Kern
Added 'av' prefix to ObjPool and its functions.

Rick Kern (2):
  fftools/objpool: move objpool to libavutil
  lavc/videotoolboxenc: use objectpool for encoder output information

 fftools/Makefile |  1 -
 fftools/ffmpeg_mux.c |  8 ++--
 fftools/sync_queue.c | 18 +++
 fftools/thread_queue.c   | 14 +++---
 fftools/thread_queue.h   |  4 +-
 libavcodec/videotoolboxenc.c | 80 
 libavutil/Makefile   |  2 +
 {fftools => libavutil}/objpool.c | 32 +++--
 {fftools => libavutil}/objpool.h | 24 +-
 9 files changed, 125 insertions(+), 58 deletions(-)
 rename {fftools => libavutil}/objpool.c (77%)
 rename {fftools => libavutil}/objpool.h (62%)

-- 
2.38.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 2/2] lavc/videotoolboxenc: use objectpool for encoder output information

2022-11-19 Thread Rick Kern
Instead of allocating/freeing, use objpool for data passed from an
input frame to an output packet.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 80 
 1 file changed, 71 insertions(+), 9 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index dc9e321d3d..205cbfbc68 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -24,6 +24,7 @@
 #include 
 #include 
 #include "avcodec.h"
+#include "libavutil/objpool.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -251,8 +252,49 @@ typedef struct VTEncContext {
 
 /* can't be bool type since AVOption will access it as int */
 int a53_cc;
+
+ObjPool *output_node_pool;
 } VTEncContext;
 
+static BufNode *output_node_alloc(void) { return av_mallocz(sizeof(BufNode)); }
+static void output_node_free(BufNode **node) { av_freep(node); }
+static void output_node_reset(BufNode *node) {
+  if (node->cm_buffer != NULL) {
+CFRelease(node->cm_buffer);
+  }
+
+  if (node->sei != NULL) {
+if (node->sei->data != NULL) {
+  av_free(node->sei->data);
+}
+
+av_free(node->sei);
+  }
+
+  memset(node, 0, sizeof(*node));
+}
+
+static int alloc_output_node_pool(ObjPool **pool_out) {
+  ObjPool *pool = NULL;
+
+  if (pool_out == NULL) {
+return AVERROR(EINVAL);
+  }
+
+  pool = objpool_alloc(
+  output_node_alloc,
+  output_node_reset,
+  output_node_free);
+
+  if (pool == NULL) {
+return AVERROR(ENOMEM);
+  }
+
+  *pool_out = pool;
+
+  return 0;
+}
+
 static int vtenc_populate_extradata(AVCodecContext   *avctx,
 CMVideoCodecType codec_type,
 CFStringRef  profile_level,
@@ -285,8 +327,7 @@ static void set_async_error(VTEncContext *vtctx, int err)
 
 while (info) {
 BufNode *next = info->next;
-CFRelease(info->cm_buffer);
-av_free(info);
+objpool_release(vtctx->output_node_pool, &info);
 info = next;
 }
 
@@ -336,22 +377,29 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait, 
CMSampleBufferRef *buf, E
 pthread_mutex_unlock(&vtctx->lock);
 
 *buf = info->cm_buffer;
+if (*buf != NULL) {
+  CFRetain(*buf);
+}
+
 if (sei && *buf) {
 *sei = info->sei;
-} else if (info->sei) {
-if (info->sei->data) av_free(info->sei->data);
-av_free(info->sei);
+info->sei = NULL;
 }
-av_free(info);
 
+objpool_release(vtctx->output_node_pool, &info);
 
 return 0;
 }
 
 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, 
ExtraSEI *sei)
 {
-BufNode *info = av_malloc(sizeof(BufNode));
-if (!info) {
+BufNode *info = NULL;
+int status = objpool_get(vtctx->output_node_pool, &info);
+
+if (status < 0) {
+  set_async_error(vtctx, status);
+  return;
+} else if (!info) {
 set_async_error(vtctx, AVERROR(ENOMEM));
 return;
 }
@@ -1589,7 +1637,17 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 avctx->has_b_frames = vtctx->has_b_frames;
 
-return 0;
+status = alloc_output_node_pool(&vtctx->output_node_pool);
+
+if (status < 0) {
+av_log(
+  avctx,
+  AV_LOG_ERROR,
+  "Failed to allocate output pool (%d)\n",
+  status);
+}
+
+return status;
 }
 
 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
@@ -2647,6 +2705,10 @@ static av_cold int vtenc_close(AVCodecContext *avctx)
 vtctx->ycbcr_matrix = NULL;
 }
 
+if (vtctx->output_node_pool != NULL) {
+  objpool_free(&vtctx->output_node_pool);
+}
+
 return 0;
 }
 
-- 
2.38.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 1/2] fftools/objpool: move objpool to libavutil

2022-11-19 Thread Rick Kern
Moved objpool to libavutil to make it available outside of fftools.

Signed-off-by: Rick Kern 
---
 fftools/Makefile | 1 -
 fftools/ffmpeg_mux.c | 2 +-
 fftools/sync_queue.c | 2 +-
 fftools/thread_queue.c   | 2 +-
 fftools/thread_queue.h   | 2 +-
 libavutil/Makefile   | 2 ++
 {fftools => libavutil}/objpool.c | 0
 {fftools => libavutil}/objpool.h | 0
 8 files changed, 6 insertions(+), 5 deletions(-)
 rename {fftools => libavutil}/objpool.c (100%)
 rename {fftools => libavutil}/objpool.h (100%)

diff --git a/fftools/Makefile b/fftools/Makefile
index 8ac38e75d2..25b11deaff 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -16,7 +16,6 @@ OBJS-ffmpeg +=  \
 fftools/ffmpeg_mux.o\
 fftools/ffmpeg_mux_init.o   \
 fftools/ffmpeg_opt.o\
-fftools/objpool.o   \
 fftools/sync_queue.o\
 fftools/thread_queue.o  \
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index ad04f5049d..bd1a1ed403 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -22,7 +22,6 @@
 
 #include "ffmpeg.h"
 #include "ffmpeg_mux.h"
-#include "objpool.h"
 #include "sync_queue.h"
 #include "thread_queue.h"
 
@@ -30,6 +29,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/mem.h"
+#include "libavutil/objpool.h"
 #include "libavutil/timestamp.h"
 #include "libavutil/thread.h"
 
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index c2b23ee4f5..75b8dc72fc 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -24,8 +24,8 @@
 #include "libavutil/fifo.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
+#include "libavutil/objpool.h"
 
-#include "objpool.h"
 #include "sync_queue.h"
 
 typedef struct SyncQueueStream {
diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index a1ab4ce92e..ced7b02fba 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -24,9 +24,9 @@
 #include "libavutil/fifo.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
+#include "libavutil/objpool.h"
 #include "libavutil/thread.h"
 
-#include "objpool.h"
 #include "thread_queue.h"
 
 enum {
diff --git a/fftools/thread_queue.h b/fftools/thread_queue.h
index 0cc8c71ebd..6d5862bd1f 100644
--- a/fftools/thread_queue.h
+++ b/fftools/thread_queue.h
@@ -21,7 +21,7 @@
 
 #include 
 
-#include "objpool.h"
+#include "libavutil/objpool.h"
 
 typedef struct ThreadQueue ThreadQueue;
 
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 3d9c07aea8..bd9ac28335 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -62,6 +62,7 @@ HEADERS = adler32.h   
  \
   mem.h \
   motion_vector.h   \
   murmur3.h \
+  objpool.h \
   opt.h \
   parseutils.h  \
   pixdesc.h \
@@ -149,6 +150,7 @@ OBJS = adler32.o
\
md5.o\
mem.o\
murmur3.o\
+   objpool.o\
opt.o\
parseutils.o \
pixdesc.o\
diff --git a/fftools/objpool.c b/libavutil/objpool.c
similarity index 100%
rename from fftools/objpool.c
rename to libavutil/objpool.c
diff --git a/fftools/objpool.h b/libavutil/objpool.h
similarity index 100%
rename from fftools/objpool.h
rename to libavutil/objpool.h
-- 
2.38.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame

2022-11-19 Thread Rick Kern
objpool was only being used in fftools, but it's useful in other components to 
reduce alloation time and memory fragmentation.

This patch set moves objpool to libavutil, so it can be used by 
lavc/videotoolboxenc for data being passed from input frames to output packets.

Rick Kern (2):
  fftools/objpool: move objpool to libavutil
  lavc/videotoolboxenc: use objectpool for encoder output information

 fftools/Makefile |  1 -
 fftools/ffmpeg_mux.c |  2 +-
 fftools/sync_queue.c |  2 +-
 fftools/thread_queue.c   |  2 +-
 fftools/thread_queue.h   |  2 +-
 libavcodec/videotoolboxenc.c | 80 
 libavutil/Makefile   |  2 +
 {fftools => libavutil}/objpool.c |  0
 {fftools => libavutil}/objpool.h |  0
 9 files changed, 77 insertions(+), 14 deletions(-)
 rename {fftools => libavutil}/objpool.c (100%)
 rename {fftools => libavutil}/objpool.h (100%)

-- 
2.38.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v3] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder

2022-08-29 Thread Rick Kern
On Mon, Aug 29, 2022 at 9:05 AM Sebastian Beckmann <
beckmann.sebast...@outlook.de> wrote:

>
> > The patch doesn't apply. Can you create a .patch file with git
> format-patch
> > and send the file as an attachment? Sometimes email clients/providers
> > corrupt inline patches.
>

Thanks, pushed.


>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v3] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder

2022-08-29 Thread Rick Kern
On Sun, Aug 28, 2022 at 4:27 PM Sebastian Beckmann <
beckmann.sebast...@outlook.de> wrote:

> Adds an option to use constant bitrate instead of average bitrate to the
> videotoolbox encoders. This is enabled via -constant_bit_rate true.
> macOS 13 is required for this option to work.
>
> Signed-off-by: Sebastian Beckmann 
> ---
> Removed the hard-coded check for Apple Silicon CPUs.
>
> libavcodec/videotoolboxenc.c | 27 ---
> 1 file changed, 24 insertions(+), 3 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 823e5ad94e..9de86cc4d9 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -101,6 +101,7 @@ static struct{
> CFStringRef kVTCompressionPropertyKey_RealTime;
> CFStringRef kVTCompressionPropertyKey_TargetQualityForAlpha;
> CFStringRef
> kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality;
> +CFStringRef kVTCompressionPropertyKey_ConstantBitRate;
>
> CFStringRef
> kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
> CFStringRef
> kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
> @@ -164,6 +165,7 @@ static void loadVTEncSymbols(){
> "TargetQualityForAlpha");
> GET_SYM(kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
> "PrioritizeEncodingSpeedOverQuality");
> +GET_SYM(kVTCompressionPropertyKey_ConstantBitRate, "ConstantBitRate");
>
>
> GET_SYM(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
> "EnableHardwareAcceleratedVideoEncoder");
> @@ -236,6 +238,7 @@ typedef struct VTEncContext {
> int realtime;
> int frames_before;
> int frames_after;
> +bool constant_bit_rate;
>
> int allow_sw;
> int require_sw;
> @@ -1079,6 +1082,7 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
> CFNumberRef  gamma_level,
> CFDictionaryRef  enc_info,
> CFDictionaryRef  pixel_buffer_info,
> +bool constant_bit_rate,
> VTCompressionSessionRef *session)
> {
> VTEncContext *vtctx = avctx->priv_data;
> @@ -1139,9 +1143,20 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
>   &bit_rate);
> if (!bit_rate_num) return AVERROR(ENOMEM);
>
> -status = VTSessionSetProperty(vtctx->session,
> -
> kVTCompressionPropertyKey_AverageBitRate,
> -  bit_rate_num);
> +if (constant_bit_rate) {
> +status = VTSessionSetProperty(vtctx->session,
> +
> compat_keys.kVTCompressionPropertyKey_ConstantBitRate,
> +  bit_rate_num);
> +if (status == kVTPropertyNotSupportedErr) {
> +av_log(avctx, AV_LOG_ERROR, "Error: -constant_bit_rate
> true is not supported by the encoder.\n");
> +return AVERROR_EXTERNAL;
> +}
> +} else {
> +status = VTSessionSetProperty(vtctx->session,
> +
> kVTCompressionPropertyKey_AverageBitRate,
> +  bit_rate_num);
> +}
> +
> CFRelease(bit_rate_num);
> }
>
> @@ -1530,6 +1545,7 @@ static int vtenc_configure_encoder(AVCodecContext
> *avctx)
>   gamma_level,
>   enc_info,
>   pixel_buffer_info,
> +  vtctx->constant_bit_rate,
>   &vtctx->session);
>
> init_cleanup:
> @@ -2532,6 +2548,7 @@ static int vtenc_populate_extradata(AVCodecContext
>  *avctx,
>   gamma_level,
>   enc_info,
>   pixel_buffer_info,
> +  vtctx->constant_bit_rate,
>   &vtctx->session);
> if (status)
> goto pe_cleanup;
> @@ -2727,6 +2744,8 @@ static const AVOption h264_options[] = {
>
> { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc),
> AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE },
>
> +{ "constant_bit_rate", "Require constant bit rate (macOS 13 or
> newer)", OFFSET(constant_bit_rate), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
> VE },
> +
> COMMON_OPTIONS
> { NULL },
> };
> @@ -2760,6 +2779,8 @@ static const AVOption hevc_options[] = {
>
> { "alpha_quality", "Compression quality for the alpha channel",
> OFFSET(alpha_quality), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0, VE },
>
> +{ "constant_bit_rate", "Require constant bit rate (macOS 13 or
> newer)", OFFSET(constant_bit_rate), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
> VE },
> +
> COMMON_OPTIONS
> { NULL },
> };
> --
> 2.37.0 (Apple Git-136)
>

The patch doesn't apply. Can you create a .patch file with git format-patch

Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder

2022-08-28 Thread Rick Kern
On Fri, Aug 26, 2022 at 7:42 AM Sebastian Beckmann <
beckmann.sebast...@outlook.de> wrote:

> Adds an option to use constant bitrate instead of average bitrate to the
> videotoolbox encoders. This is enabled via -constant_bit_rate true.
> macOS 13 is required for this option to work.
>
> Signed-off-by: Sebastian Beckmann 
> ---
> libavcodec/videotoolboxenc.c | 37 +---
> 1 file changed, 34 insertions(+), 3 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 823e5ad94e..9eb6fe09a2 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -101,6 +101,7 @@ static struct{
> CFStringRef kVTCompressionPropertyKey_RealTime;
> CFStringRef kVTCompressionPropertyKey_TargetQualityForAlpha;
> CFStringRef
> kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality;
> +CFStringRef kVTCompressionPropertyKey_ConstantBitRate;
>
> CFStringRef
> kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
> CFStringRef
> kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
> @@ -164,6 +165,7 @@ static void loadVTEncSymbols(){
> "TargetQualityForAlpha");
> GET_SYM(kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
> "PrioritizeEncodingSpeedOverQuality");
> +GET_SYM(kVTCompressionPropertyKey_ConstantBitRate, "ConstantBitRate");
>
>
> GET_SYM(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
> "EnableHardwareAcceleratedVideoEncoder");
> @@ -236,6 +238,7 @@ typedef struct VTEncContext {
> int realtime;
> int frames_before;
> int frames_after;
> +bool constant_bit_rate;
>
> int allow_sw;
> int require_sw;
> @@ -1073,12 +1076,22 @@ static bool vtenc_qscale_enabled(void)
> return !TARGET_OS_IPHONE && TARGET_CPU_ARM64;
> }
>
> +// constant bit rate only on Macs with Apple Silicon running macOS 13
> (Ventura) or newer
> +static bool vtenc_constant_bit_rate_enabled(void)
>

It's possible that Apple adds support for CBR on iOS or Intel-based Macs in
the future, so it would be more future-proof to avoid hard-coding here.
Then you could always try to set the property, and print an error message
if it returns an error.  See PrioritizeEncodingSpeedOverQuality for an
example.

I see the same hard-coded logic is used in vtenc_qscale_enabled(). Thanks
for following the patterns in this file, because that's usually the right
thing to do. However, the qscale issue slipped through the cracks and also
needs to be changed.


> +{
> +if (__builtin_available(macOS 13, *))

+return !TARGET_OS_IPHONE && TARGET_CPU_ARM64;
> +else
> +return false;
> +}
> +
> static int vtenc_create_encoder(AVCodecContext   *avctx,
> CMVideoCodecType codec_type,
> CFStringRef  profile_level,
> CFNumberRef  gamma_level,
> CFDictionaryRef  enc_info,
> CFDictionaryRef  pixel_buffer_info,
> +bool constant_bit_rate,
> VTCompressionSessionRef *session)
> {
> VTEncContext *vtctx = avctx->priv_data;
> @@ -1122,6 +1135,11 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
> return AVERROR_EXTERNAL;
> }
>
> +if (constant_bit_rate && !vtenc_constant_bit_rate_enabled()) {
> +av_log(avctx, AV_LOG_ERROR, "Error: -constant_bit_rate true not
> available for encoder.\n");
> +return AVERROR_EXTERNAL;
> +}
> +
> if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
> quality = quality >= 100 ? 1.0 : quality / 100;
> quality_num = CFNumberCreate(kCFAllocatorDefault,
> @@ -1139,9 +1157,16 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
>   &bit_rate);
> if (!bit_rate_num) return AVERROR(ENOMEM);
>
> -status = VTSessionSetProperty(vtctx->session,
> -
> kVTCompressionPropertyKey_AverageBitRate,
> -  bit_rate_num);
> +if (constant_bit_rate) {
> +status = VTSessionSetProperty(vtctx->session,
> +
> compat_keys.kVTCompressionPropertyKey_ConstantBitRate,
> +  bit_rate_num);
>

For example, you could check status here, and output a message about no CBR
support.


> +} else {
> +status = VTSessionSetProperty(vtctx->session,
> +
> kVTCompressionPropertyKey_AverageBitRate,
> +  bit_rate_num);
> +}
> +
> CFRelease(bit_rate_num);
> }
>
> @@ -1530,6 +1555,7 @@ static int vtenc_configure_encoder(AVCodecContext
> *avctx)
>   gamma_level,
>   enc_info,
>   pixel_buffer_info,
> +  vtctx-

Re: [FFmpeg-devel] [PATCH] libavcodec/videotoolboxenc.c: add option to hevc encoder to prioritize speed.

2022-05-02 Thread Rick Kern
On Sun, May 1, 2022 at 12:12 PM Simone Karin Lehmann 
wrote:

> Hi,
>
> … I’m so sorry, the patch I recently sent didn’t test the prio_speed bool.
> This corrects it.
>
> I really sorry. :-/
>
> Regards
> Simone
>
Pushed, with some commit message changes. The first line of the commit
message should start with a short topic - in this case
lavc/videotoolboxenc, followed by a brief description. Commit messages
should also be limited to an 80-character width when practical.


>
>
> > Am 01.05.2022 um 20:25 schrieb Richard Kern :
> >
> >
> >
> >> On May 1, 2022, at 1:31 PM, Simone Karin Lehmann 
> wrote:
> >>
> >> Hi,
> >>
> >> may I kindly ask you to check, if this patch could now be applied?
> > I’ll look at it today.
> >
> >>
> >> Regards
> >> Simone
> >>
> >> Von meinem iPad gesendet
> >>
> >>> Am 25.04.2022 um 22:33 schrieb Simone Karin Lehmann  >:
> >>>
> >>> 
> >>>
> >>>>>> Am 25.04.2022 um 21:14 schrieb Rick Kern :
> >>>>>>
> >>>>>>
> >>>>>> { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc),
> >>>>>> AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE },
> >>>>>> +{ "prio_speed", "prioritize encoding speed",
> OFFSET(prio_speed),
> >>>>>> AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
> >>>>>>
> >>>> It might be simpler to make the default value of prio_speed -1, and
> set
> >>>> this property on the encoder session only when it's non-negative.
> Then we
> >>>> won't need to worry about the default value changing for different
> codecs
> >>>> or different OS versions. It could also be moved into COMMON_OPTIONS
> in
> >>>> this case.
> >>>
> >>> oh yes, that’s way better. I didn’t think of that in the first place.
> Thanks for the hint.
> >>>
> >>> Here’s the modified patch
> >>>
> >>> Simone
> >>>
> > <0001-add-options-to-h264-hevc-and-prores-encoders-to-prio.patch>
> >>
> >>>
> >>> ___
> >>> ffmpeg-devel mailing list
> >>> ffmpeg-devel@ffmpeg.org
> >>> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >>>
> >>> To unsubscribe, visit link above, or email
> >>> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> >> ___
> >> ffmpeg-devel mailing list
> >> ffmpeg-devel@ffmpeg.org
> >> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >>
> >> To unsubscribe, visit link above, or email
> >> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] libavcodec/videotoolboxenc.c: add option to hevc encoder to prioritize speed.

2022-04-25 Thread Rick Kern
On Sat, Apr 23, 2022 at 8:31 AM Simone Karin Lehmann 
wrote:

>
>
> > Am 22.04.2022 um 18:52 schrieb Thilo Borgmann :
> >
> >
>
> > For that version I get:
> >
> > libavcodec/videotoolboxenc.c:1153:39: error: use of undeclared
> identifier 'kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality'
> >
> kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
> >  ^
> >
> > Should require some OSX version dependency via #if'ery somewhere sane.
> Like
> > #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
> >
> > or similar.
>
> If I understand the code correctly, it seems to me to be bettter to add
> kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality to the
> compat_keys struct.
>
> And digging through the SDK headers I’ve found that this property is
> available for h264 and ProRes encoders too. But since the SDK states that
> this property defaults to FALSE for h264 and hevc and to TRUE for ProRes I
> didn’t add it to the COMMON_OPTIONS but to each encoder options, because I
> didn’t want to make a regression to the ProRes or enabling it by default
> for h264 and hevc. Hope that’s ok.
>
> Here’s the resulting patch
>
> Regards
> Simone
>
>
> Signed-off-by: Simone Karin Lehmann  sim...@lisanet.de>>
> ---
> libavcodec/videotoolboxenc.c | 18 ++
> 1 file changed, 18 insertions(+)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 270496b7a7..462d2a8fb6 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -100,6 +100,7 @@ static struct{
>
> CFStringRef kVTCompressionPropertyKey_RealTime;
> CFStringRef kVTCompressionPropertyKey_TargetQualityForAlpha;
> +CFStringRef
> kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality;
>
> CFStringRef
> kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
> CFStringRef
> kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
> @@ -161,6 +162,8 @@ static void loadVTEncSymbols(){
> GET_SYM(kVTCompressionPropertyKey_RealTime, "RealTime");
> GET_SYM(kVTCompressionPropertyKey_TargetQualityForAlpha,
> "TargetQualityForAlpha");
> +GET_SYM(kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
> +"PrioritizeEncodingSpeedOverQuality");
>
>
> GET_SYM(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
> "EnableHardwareAcceleratedVideoEncoder");
> @@ -237,6 +240,7 @@ typedef struct VTEncContext {
> int allow_sw;
> int require_sw;
> double alpha_quality;
> +int prio_speed;
>
> bool flushing;
> int has_b_frames;
> @@ -1146,6 +1150,16 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
> return AVERROR_EXTERNAL;
> }
>
> +// prioritize speed over quality
> +if (vtctx->prio_speed) {

+status = VTSessionSetProperty(vtctx->session,
> +
> compat_keys.kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
> +  kCFBooleanTrue);
> +if (status) {
> +av_log(avctx, AV_LOG_WARNING,
> "PrioritizeEncodingSpeedOverQuality property is not supported on this
> device. Ignoring.\n");
> +}
> +}
> +
> if ((vtctx->codec_id == AV_CODEC_ID_H264 || vtctx->codec_id ==
> AV_CODEC_ID_HEVC)
> && max_rate > 0) {
> bytes_per_second_value = max_rate >> 3;
> @@ -2711,6 +2725,7 @@ static const AVOption h264_options[] = {
> { "ac","CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 =
> VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
>
> { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc),
> AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE },
> +{ "prio_speed", "prioritize encoding speed", OFFSET(prio_speed),
> AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
>
It might be simpler to make the default value of prio_speed -1, and set
this property on the encoder session only when it's non-negative. Then we
won't need to worry about the default value changing for different codecs
or different OS versions. It could also be moved into COMMON_OPTIONS in
this case.


>
> COMMON_OPTIONS
> { NULL },
> @@ -2745,6 +2760,7 @@ static const AVOption hevc_options[] = {
> { "main10",   "Main10 Profile",   0, AV_OPT_TYPE_CONST, { .i64 =
> HEVC_PROF_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
>
> { "alpha_quality", "Compression quality for the alpha channel",
> OFFSET(alpha_quality), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0, VE },
> +{ "prio_speed", "prioritize encoding speed", OFFSET(prio_speed),
> AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
>
> COMMON_OPTIONS
> { NULL },
> @@ -2785,6 +2801,8 @@ static const AVOption prores_options[] = {
> { "", "ProRes ",   0,
> AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_PRORES_ },INT_MIN,
> INT_MAX, VE, "profile" },
> { "xq",   "ProRes  XQ", 

Re: [FFmpeg-devel] lavc/videotoolboxenc: add ProRes support

2021-12-19 Thread Rick Kern
On Thu, Dec 16, 2021 at 7:12 PM rcombs  wrote:

> This patchset includes several infrastructural improvements to
> videotoolboxenc,
> an added group of output formats for swscale, and a ProRes encoder wrapper.
>
> Also available as a GitHub PR here:
> https://github.com/FFmpeg/FFmpeg/pull/378
>
> Review comments and followup changes will be applied there to reduce ML
> noise.
>
I noticed this a little late. I'll add my comments on github too. Is that
the preferred way now? Seems better to me.


>
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 07/10] lavc/videotoolboxenc: add handling for non-NAL-based codecs

2021-12-19 Thread Rick Kern
On Thu, Dec 16, 2021 at 7:13 PM rcombs  wrote:

> ---
>  libavcodec/videotoolboxenc.c | 149 +--
>  1 file changed, 92 insertions(+), 57 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 82e01fbe29..67bb563639 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -549,6 +549,7 @@ static int copy_param_sets(
>
>  static int set_extradata(AVCodecContext *avctx, CMSampleBufferRef
> sample_buffer)
>  {
> +VTEncContext *vtctx = avctx->priv_data;
>  CMVideoFormatDescriptionRef vid_fmt;
>  size_t total_size;
>  int status;
> @@ -559,23 +560,37 @@ static int set_extradata(AVCodecContext *avctx,
> CMSampleBufferRef sample_buffer)
>  return AVERROR_EXTERNAL;
>  }
>
> -status = get_params_size(avctx, vid_fmt, &total_size);
> -if (status) {
> -av_log(avctx, AV_LOG_ERROR, "Could not get parameter sets.\n");
> -return status;
> -}
> +if (vtctx->get_param_set_func) {
> +status = get_params_size(avctx, vid_fmt, &total_size);
> +if (status) {
> +av_log(avctx, AV_LOG_ERROR, "Could not get parameter
> sets.\n");
> +return status;
> +}
>
> -avctx->extradata = av_mallocz(total_size +
> AV_INPUT_BUFFER_PADDING_SIZE);
> -if (!avctx->extradata) {
> -return AVERROR(ENOMEM);
> -}
> -avctx->extradata_size = total_size;
> +avctx->extradata = av_mallocz(total_size +
> AV_INPUT_BUFFER_PADDING_SIZE);
> +if (!avctx->extradata) {
> +return AVERROR(ENOMEM);
> +}
> +avctx->extradata_size = total_size;
>
> -status = copy_param_sets(avctx, vid_fmt, avctx->extradata,
> total_size);
> +status = copy_param_sets(avctx, vid_fmt, avctx->extradata,
> total_size);
>
> -if (status) {
> -av_log(avctx, AV_LOG_ERROR, "Could not copy param sets.\n");
> -return status;
> +if (status) {
> +av_log(avctx, AV_LOG_ERROR, "Could not copy param sets.\n");
> +return status;
> +}
> +} else {
> +CFDataRef data = CMFormatDescriptionGetExtension(vid_fmt,
> kCMFormatDescriptionExtension_VerbatimSampleDescription);
> +if (data && CFGetTypeID(data) == CFDataGetTypeID()) {
> +CFIndex size = CFDataGetLength(data);
> +
> +avctx->extradata = av_mallocz(size +
> AV_INPUT_BUFFER_PADDING_SIZE);
> +if (!avctx->extradata)
> +return AVERROR(ENOMEM);
> +avctx->extradata_size = size;
> +
> +CFDataGetBytes(data, CFRangeMake(0, size), avctx->extradata);
> +}
>  }
>
>  return 0;
> @@ -1938,62 +1953,82 @@ static int vtenc_cm_to_avpacket(
>  CMTime  dts;
>  CMVideoFormatDescriptionRef vid_fmt;
>
> -
>  vtenc_get_frame_info(sample_buffer, &is_key_frame);
> -status = get_length_code_size(avctx, sample_buffer,
> &length_code_size);
> -if (status) return status;
>
> -add_header = is_key_frame && !(avctx->flags &
> AV_CODEC_FLAG_GLOBAL_HEADER);
> +if (vtctx->get_param_set_func) {
> +status = get_length_code_size(avctx, sample_buffer,
> &length_code_size);
> +if (status) return status;
>
> -if (add_header) {
> -vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
> -if (!vid_fmt) {
> -av_log(avctx, AV_LOG_ERROR, "Cannot get format
> description.\n");
> -return AVERROR_EXTERNAL;
> +add_header = is_key_frame && !(avctx->flags &
> AV_CODEC_FLAG_GLOBAL_HEADER);
> +
> +if (add_header) {
> +vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
> +if (!vid_fmt) {
> +av_log(avctx, AV_LOG_ERROR, "Cannot get format
> description.\n");
> +return AVERROR_EXTERNAL;
> +}
> +
> +int status = get_params_size(avctx, vid_fmt, &header_size);
> +if (status) return status;
>  }
>
> -int status = get_params_size(avctx, vid_fmt, &header_size);
> -if (status) return status;
> -}
> +status = count_nalus(length_code_size, sample_buffer,
> &nalu_count);
> +if(status)
> +return status;
>
> -status = count_nalus(length_code_size, sample_buffer, &nalu_count);
> -if(status)
> -return status;
> +if (sei) {
> +size_t msg_size = get_sei_msg_bytes(sei,
> +
> SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35);
>
> -if (sei) {
> -size_t msg_size = get_sei_msg_bytes(sei,
> -
> SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35);
> +sei_nalu_size = sizeof(start_code) + 1 + msg_size + 1;
> +}
>
> -sei_nalu_size = sizeof(start_code) + 1 + msg_size + 1;
> -}
> +in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
> +out_buf_size = header_size +
> +   in_buf_size +
> +   sei_nalu_size +
> 

Re: [FFmpeg-devel] [PATCH 06/10] lavc/videotoolboxenc: vastly simplify get_cv_pixel_info

2021-12-19 Thread Rick Kern
On Thu, Dec 16, 2021 at 7:13 PM rcombs  wrote:

> No longer requires per-format switch cases.
>
> The frame==0 path was unused (and would've crashed anyway).
> ---
>  libavcodec/videotoolboxenc.c | 69 +++-
>  1 file changed, 13 insertions(+), 56 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index b66d44f6b7..82e01fbe29 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -2034,6 +2034,7 @@ static int get_cv_pixel_info(
>  size_t *strides,
>  size_t *contiguous_buf_size)
>  {
> +const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
>  VTEncContext *vtctx = avctx->priv_data;
>  int av_format   = frame->format;
>  int av_color_range  = frame->color_range;
> @@ -2041,6 +2042,9 @@ static int get_cv_pixel_info(
>  int range_guessed;
>  int status;
>
> +if (!desc)
> +return AVERROR(EINVAL);
> +
>  status = get_cv_pixel_format(avctx, av_format, av_color_range, color,
> &range_guessed);
>  if (status) {
>  av_log(avctx,
> @@ -2065,63 +2069,16 @@ static int get_cv_pixel_info(
>  }
>  }
>
> -switch (av_format) {
> -case AV_PIX_FMT_NV12:
> -*plane_count = 2;
> -
> -widths [0] = avctx->width;
> -heights[0] = avctx->height;
> -strides[0] = frame ? frame->linesize[0] : avctx->width;
> -
> -widths [1] = (avctx->width  + 1) / 2;
> -heights[1] = (avctx->height + 1) / 2;
> -strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) & -2;
> -break;
> -
> -case AV_PIX_FMT_YUV420P:
> -*plane_count = 3;
> -
> -widths [0] = avctx->width;
> -heights[0] = avctx->height;
> -strides[0] = frame ? frame->linesize[0] : avctx->width;
> -
> -widths [1] = (avctx->width  + 1) / 2;
> -heights[1] = (avctx->height + 1) / 2;
> -strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) / 2;
> -
> -widths [2] = (avctx->width  + 1) / 2;
> -heights[2] = (avctx->height + 1) / 2;
> -strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2;
> -break;
> +*plane_count = av_pix_fmt_count_planes(avctx->pix_fmt);
>
> -case AV_PIX_FMT_BGRA:
> -*plane_count = 1;
> -
> -widths [0] = avctx->width;
> -heights[0] = avctx->height;
> -strides[0] = frame ? frame->linesize[0] : avctx->width * 4;
> -break;
> -
> -case AV_PIX_FMT_P010LE:
> -*plane_count = 2;
> -widths[0] = avctx->width;
> -heights[0] = avctx->height;
> -strides[0] = frame ? frame->linesize[0] : (avctx->width * 2 + 63)
> & -64;
> -
> -widths[1] = (avctx->width + 1) / 2;
> -heights[1] = (avctx->height + 1) / 2;
> -strides[1] = frame ? frame->linesize[1] : ((avctx->width + 1) / 2
> + 63) & -64;
> -break;
> -
> -default:
> -av_log(
> -   avctx,
> -   AV_LOG_ERROR,
> -   "Could not get frame format info for color %d range %d.\n",
> -   av_format,
> -   av_color_range);
> -
> -return AVERROR(EINVAL);
> +for (i = 0; i < desc->nb_components; i++) {
> +int p = desc->comp[i].plane;
> +int chroma = (p != 0) && (!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)
> || p + 1 != *plane_count);
>
This could be broken out into more variables to make the logic clearer. It
took me a while to parse this.


> +int shiftw = chroma ? desc->log2_chroma_w : 0;
> +int shifth = chroma ? desc->log2_chroma_h : 0;
> +widths[p]  = (avctx->width  + ((1 << shiftw) >> 1)) >> shiftw;
> +heights[p] = (avctx->height + ((1 << shifth) >> 1)) >> shifth;
> +strides[p] = frame->linesize[p];
>  }
>
>  *contiguous_buf_size = 0;
> --
> 2.33.1
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 04/10] lavc/videotoolboxenc: fix RGB support

2021-12-19 Thread Rick Kern
On Thu, Dec 16, 2021 at 7:13 PM rcombs  wrote:

> ---
>  libavcodec/videotoolboxenc.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index fa8f717a6c..e10373dded 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -1012,6 +1012,9 @@ static int get_cv_ycbcr_matrix(AVCodecContext
> *avctx, CFStringRef *matrix) {
>  *matrix = compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
>  break;
>
> +case AVCOL_SPC_RGB:
>
It would be clearer to set the matrix to NULL here (like the
AVCOL_SPC_UNSPECIFIED case).

+break;
> +
>  default:
>  av_log(avctx, AV_LOG_ERROR, "Color space %s is not
> supported.\n", av_color_space_name(avctx->colorspace));
>  return -1;
> --
> 2.33.1
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] libavcodec/videotoolboxenc: fix pixel buffer memory leak

2021-11-09 Thread Rick Kern
On Tue, Nov 9, 2021 at 5:57 AM Steven Liu  wrote:

> <13102179...@163.com> 于2021年11月9日周二 下午6:52写道:
> >
> > From: songyutong 
> >
> > In function vtenc_populate_extradata(), there is a manually created
> > pixel buffer that has not been released. So we should use
> CVPixelBufferRelease
> > to release this pixel buffer at the end, otherwise will cause a memory
> leak.
>
Good catch. Pushed.

In the future, add your signoff to the commit message once you have
self-reviewed and tested the code. (--signoff with the git CLI).

> ---
> >  libavcodec/videotoolboxenc.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> > index 8a02d3d1a0..c06b506cc9 100644
> > --- a/libavcodec/videotoolboxenc.c
> > +++ b/libavcodec/videotoolboxenc.c
> > @@ -2548,6 +2548,7 @@ static int
> vtenc_populate_extradata(AVCodecContext   *avctx,
> >
> >
> >  pe_cleanup:
> > +CVPixelBufferRelease(pix_buf);
> >  if(vtctx->session)
> >  CFRelease(vtctx->session);
> >
> > --
> > 2.30.0
> >
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
>
> Test ok here, but I'm not the maintainer of this module. :D
>
>
> Thanks
> Steven
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Fixes non-B-Frame encoding

2021-09-27 Thread Rick Kern
On Sun, Sep 26, 2021 at 8:48 AM NoHalfBits 
wrote:

> Sets vtctx->has_b_frames to 0 if the VideoToolbox compression
> session will not emit B-frames (and, in consequence, no valid
> DTSs). Required for the handling of invalid DTSs in
> 'vtenc_cm_to_avpacket' (line 2018ff) to work correctly and not
> abort encoding with "DTS is invalid." when no B-frames are
> generated.
>
> Signed-off-by: NoHalfBits 
> ---
>  libavcodec/videotoolboxenc.c | 5 -
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 8dfd6e3d0c..93c3898fb5 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -1516,7 +1516,10 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
>  if (!status && has_b_frames_cfbool) {
>  //Some devices don't output B-frames for main profile, even if
> requested.
>  // HEVC has b-pyramid
> -vtctx->has_b_frames = (CFBooleanGetValue(has_b_frames_cfbool) &&
> avctx->codec_id == AV_CODEC_ID_HEVC) ? 2 : 1;
> +if (CFBooleanGetValue(has_b_frames_cfbool))
> +vtctx->has_b_frames = avctx->codec_id == AV_CODEC_ID_HEVC ? 2
> : 1;
> +else
> +vtctx->has_b_frames = 0;
>
Pushed, thanks.

 CFRelease(has_b_frames_cfbool);
>  }
>  avctx->has_b_frames = vtctx->has_b_frames;
> --
> 2.30.1 (Apple Git-130)
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] Fix for bug #9231: B-frames parameter is ignored in videotoolboxenc

2021-08-13 Thread Rick Kern
On Fri, Aug 13, 2021 at 5:39 AM "zhilizhao(赵志立)" 
wrote:

>
>
> > On Aug 13, 2021, at 3:00 PM, Anselm Busse 
> wrote:
> >
> > Hey everyone,
> >
> > I posted this patch almost a month ago and got no reaction. Is there
> anything else I have to/can do to get this fix merged? I’m a little bit
> worried that it gets lost.
>
Sorry for the delay. The patch is applied, thanks.


>
> LGTM.
>
> >
> > - Anselm
> >
> >> Am 21.07.2021 um 20:00 schrieb Anselm Busse :
> >>
> >> This commit fixes the bug as report in
> https://trac.ffmpeg.org/ticket/9231 by removing the line that overwrites
> the user settings for max_b_frames.
> >>
> >> Signed-off-by: Anselm Busse 
> >> ---
> >> libavcodec/videotoolboxenc.c | 1 -
> >> 1 file changed, 1 deletion(-)
> >>
> >> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> >> index 4eaabed5d8..8dfd6e3d0c 100644
> >> --- a/libavcodec/videotoolboxenc.c
> >> +++ b/libavcodec/videotoolboxenc.c
> >> @@ -1398,7 +1398,6 @@ static int vtenc_configure_encoder(AVCodecContext
> *avctx)
> >>}
> >>
> >>vtctx->codec_id = avctx->codec_id;
> >> -avctx->max_b_frames = 16;
> >>
> >>if (vtctx->codec_id == AV_CODEC_ID_H264) {
> >>vtctx->get_param_set_func =
> CMVideoFormatDescriptionGetH264ParameterSetAtIndex;
> >> --
> >> 2.32.0
> >>
> >
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc.c: Add trc for iec61966-2-1 when available

2021-08-13 Thread Rick Kern
On Fri, Jul 23, 2021 at 4:31 AM Hao Guan  wrote:

> Signed-off-by: Hao Guan 
> ---
>
> Notes:
> I have checked out the code about other functions introduced after
> macOS 10.13 and noticed that the availability is checked during configure.
> Therefore I add the check for sRGB function too. It should compile now.
>
>  configure| 2 ++
>  libavcodec/videotoolboxenc.c | 5 +
>  2 files changed, 7 insertions(+)
>
> diff --git a/configure b/configure
> index bb6b096414..644d9f3537 100755
> --- a/configure
> +++ b/configure
> @@ -2325,6 +2325,7 @@ TYPES_LIST="
>  kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ
>  kCVImageBufferTransferFunction_ITU_R_2100_HLG
>  kCVImageBufferTransferFunction_Linear
> +kCVImageBufferTransferFunction_sRGB
>  socklen_t
>  struct_addrinfo
>  struct_group_source_req
> @@ -6278,6 +6279,7 @@ enabled videotoolbox && {
>  check_func_headers CoreVideo/CVImageBuffer.h
> kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ "-framework CoreVideo"
>  check_func_headers CoreVideo/CVImageBuffer.h
> kCVImageBufferTransferFunction_ITU_R_2100_HLG "-framework CoreVideo"
>  check_func_headers CoreVideo/CVImageBuffer.h
> kCVImageBufferTransferFunction_Linear "-framework CoreVideo"
> +check_func_headers CoreVideo/CVImageBuffer.h
> kCVImageBufferTransferFunction_sRGB "-framework CoreVideo"
>
Instead of being checked at build time, this should be checked at runtime
by adding it to compat_keys in videotoolboxenc.c. The reason is newer MacOS
versions will compile with this symbol, but if the build is targeting an
older MacOS version, it will crash on the older version because it can't
find the symbol.

 }
>
>  check_struct "sys/time.h sys/resource.h" "struct rusage" ru_maxrss
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 4eaabed5d8..4cba781e9b 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -977,6 +977,11 @@ static int get_cv_transfer_function(AVCodecContext
> *avctx,
>  *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
>  break;
>  #endif
> +#if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SRGB
> +case AVCOL_TRC_IEC61966_2_1:
> +*transfer_fnc = kCVImageBufferTransferFunction_sRGB;
> +break;
> +#endif
>
>  case AVCOL_TRC_GAMMA22:
>  gamma = 2.2;
> --
> 2.30.1 (Apple Git-130)
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc.c: Fix the trc support for iec61966-2-1 (sRGB)

2021-07-19 Thread Rick Kern
On Thu, Jul 15, 2021 at 12:47 AM Hao Guan  wrote:

> ---
>  libavcodec/videotoolboxenc.c | 4 
>  1 file changed, 4 insertions(+)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 4eaabed5d8..61eb6e1921 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -990,6 +990,10 @@ static int get_cv_transfer_function(AVCodecContext
> *avctx,
>  *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type,
> &gamma);
>  break;
>
> +case AVCOL_TRC_IEC61966_2_1:
> +*transfer_fnc = kCVImageBufferTransferFunction_sRGB;
> +break;
> +
>
kCVImageBufferTransferFunction_sRGB is only available starting in iOS 11
and MacOS 10.13, so this will cause a build error for old OS's. This should
be added to compat_keys and accessed from there.

 case AVCOL_TRC_BT2020_10:
>  case AVCOL_TRC_BT2020_12:
>  *transfer_fnc =
> compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
> --
> 2.30.1 (Apple Git-130)
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 42/46] avcodec/videotoolboxenc: Avoid copying data, allow user-supplied buffers

2021-05-05 Thread Rick Kern
On Thu, Apr 29, 2021 at 8:04 PM Andreas Rheinhardt <
andreas.rheinha...@outlook.com> wrote:

> Here the packet size is known before allocating the packet because
> the encoder provides said information (and works with internal buffers
> itself), so one use this information to avoid the implicit use of another
> intermediate buffer for the packet data; and by switching to
> ff_get_encode_buffer() one can also allow user-supplied buffers.
>
> Signed-off-by: Andreas Rheinhardt 
> ---
> This is completely untested (but actually also trivial). I hope someone
> can actually test it.
>
This works fine - applied.


>
>  libavcodec/videotoolboxenc.c | 9 +
>  1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index e576ff7d7e..f3e66c9e06 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -32,6 +32,7 @@
>  #include "internal.h"
>  #include 
>  #include "atsc_a53.h"
> +#include "encode.h"
>  #include "h264.h"
>  #include "h264_sei.h"
>  #include 
> @@ -1974,7 +1975,7 @@ static int vtenc_cm_to_avpacket(
> sei_nalu_size +
> nalu_count * ((int)sizeof(start_code) -
> (int)length_code_size);
>
> -status = ff_alloc_packet2(avctx, pkt, out_buf_size, out_buf_size);
> +status = ff_get_encode_buffer(avctx, pkt, out_buf_size, 0);
>  if (status < 0)
>  return status;
>
> @@ -2017,7 +2018,6 @@ static int vtenc_cm_to_avpacket(
>  time_base_num = avctx->time_base.num;
>  pkt->pts = pts.value / time_base_num;
>  pkt->dts = dts.value / time_base_num - dts_delta;
> -pkt->size = out_buf_size;
>
>  return 0;
>  }
> @@ -2664,12 +2664,12 @@ const AVCodec ff_h264_videotoolbox_encoder = {
>  .long_name= NULL_IF_CONFIG_SMALL("VideoToolbox H.264
> Encoder"),
>  .type = AVMEDIA_TYPE_VIDEO,
>  .id   = AV_CODEC_ID_H264,
> +.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
>  .priv_data_size   = sizeof(VTEncContext),
>  .pix_fmts = avc_pix_fmts,
>  .init = vtenc_init,
>  .encode2  = vtenc_frame,
>  .close= vtenc_close,
> -.capabilities = AV_CODEC_CAP_DELAY,
>  .priv_class   = &h264_videotoolbox_class,
>  .caps_internal= FF_CODEC_CAP_INIT_THREADSAFE |
>  FF_CODEC_CAP_INIT_CLEANUP,
> @@ -2698,12 +2698,13 @@ const AVCodec ff_hevc_videotoolbox_encoder = {
>  .long_name= NULL_IF_CONFIG_SMALL("VideoToolbox H.265
> Encoder"),
>  .type = AVMEDIA_TYPE_VIDEO,
>  .id   = AV_CODEC_ID_HEVC,
> +.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
> +AV_CODEC_CAP_HARDWARE,
>  .priv_data_size   = sizeof(VTEncContext),
>  .pix_fmts = hevc_pix_fmts,
>  .init = vtenc_init,
>  .encode2  = vtenc_frame,
>  .close= vtenc_close,
> -.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
>  .priv_class   = &hevc_videotoolbox_class,
>  .caps_internal= FF_CODEC_CAP_INIT_THREADSAFE |
>  FF_CODEC_CAP_INIT_CLEANUP,
> --
> 2.27.0
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: set DataRateLimits for hevc

2021-05-05 Thread Rick Kern
On Mon, Apr 26, 2021 at 3:24 AM "zhilizhao(赵志立)" 
wrote:

>
>
> > On Apr 25, 2021, at 11:31 PM, Rick Kern  wrote:
> >
> > On Sun, Apr 25, 2021 at 4:06 AM Zhao Zhili 
> wrote:
> >
> >> From the comment it's not available on old version. It works now
> >> by testing on macOS 11.2.1. There is no document about since when.
> >> So trying to set the configuration and ignore the error for hevc.
> >> ---
> >> libavcodec/videotoolboxenc.c | 10 +++---
> >> 1 file changed, 7 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> >> index 9b7ee6720c..cefd70fa88 100644
> >> --- a/libavcodec/videotoolboxenc.c
> >> +++ b/libavcodec/videotoolboxenc.c
> >> @@ -1113,8 +1113,8 @@ static int vtenc_create_encoder(AVCodecContext
> >> *avctx,
> >> return AVERROR_EXTERNAL;
> >> }
> >>
> >> -if (vtctx->codec_id == AV_CODEC_ID_H264 && max_rate > 0) {
> >> -// kVTCompressionPropertyKey_DataRateLimits is not available
> for
> >> HEVC
> >> +if ((vtctx->codec_id == AV_CODEC_ID_H264 || vtctx->codec_id ==
> >> AV_CODEC_ID_HEVC)
> >> +&& max_rate > 0) {
> >> bytes_per_second_value = max_rate >> 3;
> >> bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
> >>   kCFNumberSInt64Type,
> >> @@ -1152,7 +1152,11 @@ static int vtenc_create_encoder(AVCodecContext
> >> *avctx,
> >>
> >> if (status) {
> >> av_log(avctx, AV_LOG_ERROR, "Error setting max bitrate
> >> property: %d\n", status);
> >> -return AVERROR_EXTERNAL;
> >> +// kVTCompressionPropertyKey_DataRateLimits is available
> for
> >> HEVC
> >> +// now but not on old release. There is no document about
> >> since
> >> +// when. So ignore the error if it failed for hevc.
> >> +if (vtctx->codec_id != AV_CODEC_ID_HEVC)
> >> +return AVERROR_EXTERNAL;
> >>
> > The failure should be logged. Looks good otherwise.
>
> Yes and the failure is already logged just above the comments after `if
> (status)`.
>
Thanks - applied.


>
> >
> > }
> >> }
> >>
> >> --
> >> 2.31.1
> >>
> >>
> >> ___
> >> ffmpeg-devel mailing list
> >> ffmpeg-devel@ffmpeg.org
> >> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >>
> >> To unsubscribe, visit link above, or email
> >> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> >>
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> >
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: set DataRateLimits for hevc

2021-04-25 Thread Rick Kern
On Sun, Apr 25, 2021 at 4:06 AM Zhao Zhili  wrote:

> From the comment it's not available on old version. It works now
> by testing on macOS 11.2.1. There is no document about since when.
> So trying to set the configuration and ignore the error for hevc.
> ---
>  libavcodec/videotoolboxenc.c | 10 +++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 9b7ee6720c..cefd70fa88 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -1113,8 +1113,8 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
>  return AVERROR_EXTERNAL;
>  }
>
> -if (vtctx->codec_id == AV_CODEC_ID_H264 && max_rate > 0) {
> -// kVTCompressionPropertyKey_DataRateLimits is not available for
> HEVC
> +if ((vtctx->codec_id == AV_CODEC_ID_H264 || vtctx->codec_id ==
> AV_CODEC_ID_HEVC)
> +&& max_rate > 0) {
>  bytes_per_second_value = max_rate >> 3;
>  bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
>kCFNumberSInt64Type,
> @@ -1152,7 +1152,11 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
>
>  if (status) {
>  av_log(avctx, AV_LOG_ERROR, "Error setting max bitrate
> property: %d\n", status);
> -return AVERROR_EXTERNAL;
> +// kVTCompressionPropertyKey_DataRateLimits is available for
> HEVC
> +// now but not on old release. There is no document about
> since
> +// when. So ignore the error if it failed for hevc.
> +if (vtctx->codec_id != AV_CODEC_ID_HEVC)
> +return AVERROR_EXTERNAL;
>
The failure should be logged. Looks good otherwise.

 }
>  }
>
> --
> 2.31.1
>
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] videotoolboxenc: enable constant quality with -q:v on Apple Silicon Macs and use b-frames für HEVC and H264 and b-pyramid for HEVC.

2021-03-19 Thread Rick Kern
On Fri, Jan 22, 2021 at 3:28 PM  wrote:

> From: Simone Karin Lehmann 
>
> Signed-off-by: Simone Karin Lehmann 
>

Applied

> ---
>  libavcodec/videotoolboxenc.c | 54 
>  1 file changed, 42 insertions(+), 12 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 400401550a..e8cbc9dd4d 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -224,7 +224,7 @@ typedef struct VTEncContext {
>  int64_t require_sw;
>
>  bool flushing;
> -bool has_b_frames;
> +int has_b_frames;
>  bool warned_color_range;
>
>  /* can't be bool type since AVOption will access it as int */
> @@ -1014,6 +1014,12 @@ static int get_cv_ycbcr_matrix(AVCodecContext
> *avctx, CFStringRef *matrix) {
>  return 0;
>  }
>
> +// constant quality only on Macs with Apple Silicon
> +static bool vtenc_qscale_enabled(void)
> +{
> +return TARGET_OS_OSX && TARGET_CPU_ARM64;
> +}
> +
>  static int vtenc_create_encoder(AVCodecContext   *avctx,
>  CMVideoCodecType codec_type,
>  CFStringRef  profile_level,
> @@ -1025,7 +1031,9 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
>  VTEncContext *vtctx = avctx->priv_data;
>  SInt32   bit_rate = avctx->bit_rate;
>  SInt32   max_rate = avctx->rc_max_rate;
> +Float32  quality = avctx->global_quality / FF_QP2LAMBDA;
>  CFNumberRef  bit_rate_num;
> +CFNumberRef  quality_num;
>  CFNumberRef  bytes_per_second;
>  CFNumberRef  one_second;
>  CFArrayRef   data_rate_limits;
> @@ -1056,15 +1064,33 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
>  return AVERROR_EXTERNAL;
>  }
>
> -bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
> -  kCFNumberSInt32Type,
> -  &bit_rate);
> -if (!bit_rate_num) return AVERROR(ENOMEM);
> +if (avctx->flags & AV_CODEC_FLAG_QSCALE && !vtenc_qscale_enabled()) {
> +av_log(avctx, AV_LOG_ERROR, "Error: -q:v qscale not available for
> encoder. Use -b:v bitrate instead.\n");
> +return AVERROR_EXTERNAL;
> +}
> +
> +if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
> +quality = quality >= 100 ? 1.0 : quality / 100;
> +quality_num = CFNumberCreate(kCFAllocatorDefault,
> + kCFNumberFloat32Type,
> + &quality);
> +if (!quality_num) return AVERROR(ENOMEM);
>
> -status = VTSessionSetProperty(vtctx->session,
> -
> kVTCompressionPropertyKey_AverageBitRate,
> -  bit_rate_num);
> -CFRelease(bit_rate_num);
> +status = VTSessionSetProperty(vtctx->session,
> +  kVTCompressionPropertyKey_Quality,
> +  quality_num);
> +CFRelease(quality_num);
> +} else {
> +bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
> +  kCFNumberSInt32Type,
> +  &bit_rate);
> +if (!bit_rate_num) return AVERROR(ENOMEM);
> +
> +status = VTSessionSetProperty(vtctx->session,
> +
> kVTCompressionPropertyKey_AverageBitRate,
> +  bit_rate_num);
> +CFRelease(bit_rate_num);
> +}
>
>  if (status) {
>  av_log(avctx, AV_LOG_ERROR, "Error setting bitrate property:
> %d\n", status);
> @@ -1333,6 +1359,7 @@ static int vtenc_configure_encoder(AVCodecContext
> *avctx)
>  }
>
>  vtctx->codec_id = avctx->codec_id;
> +avctx->max_b_frames = 16;
>
>  if (vtctx->codec_id == AV_CODEC_ID_H264) {
>  vtctx->get_param_set_func =
> CMVideoFormatDescriptionGetH264ParameterSetAtIndex;
> @@ -1340,7 +1367,7 @@ static int vtenc_configure_encoder(AVCodecContext
> *avctx)
>  vtctx->has_b_frames = avctx->max_b_frames > 0;
>  if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
>  av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with
> baseline profile. Output will not contain B-frames.\n");
> -vtctx->has_b_frames = false;
> +vtctx->has_b_frames = 0;
>  }
>
>  if (vtctx->entropy == VT_CABAC && vtctx->profile ==
> H264_PROF_BASELINE) {
> @@ -1353,6 +1380,8 @@ static int vtenc_configure_encoder(AVCodecContext
> *avctx)
>  vtctx->get_param_set_func =
> compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
>  if (!vtctx->get_param_set_func) return AVERROR(EINVAL);
>  if (!get_vt_hevc_profile_level(avctx, &profile_level)) return
> AVERROR(EINVAL);
> +// HEVC has b-byramid
> +vtctx->has_b_frames = avctx->max_b_frames > 0 ? 2 : 0;
>  }
>
>  enc_info = CFDictionaryCreateMutable(
> @@ -1448,7 +1477,8 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
>
>  i

Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: Add support for HEVC with Alpha.

2021-03-19 Thread Rick Kern
On Tue, Mar 16, 2021 at 5:59 AM  wrote:

> Greetings,
>
> Thank you for your help in advance.
> Unfortunately, my patch, which add support the "HEVC with alpha" profile
> to the videotoolbox encoder, has not been reviewed for nearly three weeks.
> Would it be possible to give me why nobody has reviewed my patch?
>
> Regards,
>
> Hironori Bono
> E-mail: bo...@rouge.plala.or.jp
>
> -Original Message-
> From: ffmpeg-devel  On Behalf Of
> bo...@rouge.plala.or.jp
> Sent: Tuesday, February 23, 2021 2:02 PM
> To: ffmpeg-devel@ffmpeg.org
> Cc: Hironori Bono 
> Subject: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: Add support for HEVC
> with Alpha.
>
> From: Hironori Bono 
>
> This change supports the "HEVC Video with Alpha" profile introduced in
> WWDC 2019 . (This
> change is a partial fix for Ticket #7965.)
>
> For example, the following command converts an animation PNG file to an
> HEVC with Alpha video:
> ./ffmpeg -i fate-suite/apng/clock.png -c:v hevc_videotoolbox -allow_sw 1
> -alpha_quality 0.75 -vtag hvc1 clock.mov
>
> (This change uses the "HEVC Video with Alpha" profile only when the
> '-alpha_quality' value is not 0 for backward compatibility.)
>
> Signed-off-by: Hironori Bono 
>

Applied

> ---
>  configure|  2 ++
>  libavcodec/videotoolboxenc.c | 47 +---
>  2 files changed, 46 insertions(+), 3 deletions(-)
>
> diff --git a/configure b/configure
> index 336301cb40..63adf131b9 100755
> --- a/configure
> +++ b/configure
> @@ -2288,6 +2288,7 @@ TOOLCHAIN_FEATURES="
>
>  TYPES_LIST="
>  kCMVideoCodecType_HEVC
> +kCMVideoCodecType_HEVCWithAlpha
>  kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange
>  kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ
>  kCVImageBufferTransferFunction_ITU_R_2100_HLG
> @@ -6211,6 +6212,7 @@ enabled avfoundation && {  enabled videotoolbox && {
>  check_lib coreservices CoreServices/CoreServices.h
> UTGetOSTypeFromString "-framework CoreServices"
>  check_func_headers CoreMedia/CMFormatDescription.h
> kCMVideoCodecType_HEVC "-framework CoreMedia"
> +check_func_headers CoreMedia/CMFormatDescription.h
> kCMVideoCodecType_HEVCWithAlpha "-framework CoreMedia"
>  check_func_headers CoreVideo/CVPixelBuffer.h
> kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange "-framework CoreVideo"
>  check_func_headers CoreVideo/CVImageBuffer.h
> kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ "-framework CoreVideo"
>  check_func_headers CoreVideo/CVImageBuffer.h
> kCVImageBufferTransferFunction_ITU_R_2100_HLG "-framework CoreVideo"
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index c487d2dc60..9d3c7e29dc 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -40,6 +40,10 @@
>  enum { kCMVideoCodecType_HEVC = 'hvc1' };  #endif
>
> +#if !HAVE_KCMVIDEOCODECTYPE_HEVCWITHALPHA
> +enum { kCMVideoCodecType_HEVCWithAlpha = 'muxa' }; #endif
> +
>  #if !HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
>  enum { kCVPixelFormatType_420YpCbCr10BiPlanarFullRange = 'xf20' };  enum
> { kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange = 'x420' }; @@ -88,6
> +92,7 @@ static struct{
>  CFStringRef kVTProfileLevel_HEVC_Main10_AutoLevel;
>
>  CFStringRef kVTCompressionPropertyKey_RealTime;
> +CFStringRef kVTCompressionPropertyKey_TargetQualityForAlpha;
>
>  CFStringRef
> kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
>  CFStringRef
> kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
> @@ -147,6 +152,8 @@ static void loadVTEncSymbols(){
>  GET_SYM(kVTProfileLevel_HEVC_Main10_AutoLevel,
>  "HEVC_Main10_AutoLevel");
>
>  GET_SYM(kVTCompressionPropertyKey_RealTime, "RealTime");
> +GET_SYM(kVTCompressionPropertyKey_TargetQualityForAlpha,
> +"TargetQualityForAlpha");
>
>
>  GET_SYM(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
>  "EnableHardwareAcceleratedVideoEncoder");
> @@ -222,6 +229,7 @@ typedef struct VTEncContext {
>
>  int64_t allow_sw;
>  int64_t require_sw;
> +double alpha_quality;
>
>  bool flushing;
>  bool has_b_frames;
> @@ -392,11 +400,17 @@ static int count_nalus(size_t length_code_size,
>  return 0;
>  }
>
> -static CMVideoCodecType get_cm_codec_type(enum AVCodecID id)
> +static CMVideoCodecType get_cm_codec_type(enum AVCodecID id,
> +  enum AVPixelFormat fmt,
> +  double alpha_quality)
>  {
>  switch (id) {
>  case AV_CODEC_ID_H264: return kCMVideoCodecType_H264;
> -case AV_CODEC_ID_HEVC: return kCMVideoCodecType_HEVC;
> +case AV_CODEC_ID_HEVC:
> +if (fmt == AV_PIX_FMT_BGRA && alpha_quality > 0.0) {
> +return kCMVideoCodecType_HEVCWithAlpha;
> +}
> +return kCMVideoCodecType_HEVC;
>  default:   return 0;
>

Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: ignore encoded h264 SEI nalu by default

2021-02-15 Thread Rick Kern
On Mon, Feb 15, 2021 at 7:51 AM Andreas Rheinhardt <
andreas.rheinha...@gmail.com> wrote:

> sharpbai:
> > Before macOS 11, the encoded h264 sample contains fixed SEI before
> > IDR frame, which the content is below
> >
> > 06 05 10 b9 ed b9 30 5d 21 4b 71 83 71 2c 10 a3
> > 14 bb 29 80
> >
> > The length of the encoded SEI nalu is 20 byte. According to
> > the ITU-T H.264 Recommendation section 7.3.2.3.1, the last byte
> > 0x80 means type. Then there is no length byte, which make the
> > SEI nonstandard.
> >
> Wrong:
> 06: forbidden_zero_bit + nal_ref_idc + nal_unit_type (6 for SEI)
> 05: payload type 5 (user data unregistered) SEI message
> 10: length of this SEI message is 16
> b9-29: the payload of this SEI message (the first 12 byte are an uuid,
> the last are user_data_payload_byte)
> 80: rbsp_trailing_bits: the first bit is rbsp_stop_one_bit (equal to
> one), the remaining bits are padding (with value zero)
>
> You can use trace_headers to inspect this.
>
> (Notice that one needs to know the length of the NAL unit in advance
> before one can start parsing it; how one gets it depends upon the
> framing. There are two common methods for this: Annex B and mp4. The
> former works by using start codes 0x00 00 01 to mark the beginning of a
> new NAL, the latter prefixes the length of the NAL units to the units.)
>
> > On macOS 11, the encoded h264 sample still contains fixed SEI before
> > IDR frame, which length is 5 bytes longer than before.
> >
> > 06 05 10 b9 ed b9 30 5d 21 4b 71 83 71 2c 10 a3
> > 14 bb 29 80 00 00 03 00 01
> > ^^ ^^ ^^ ^^ ^^
>
> That looks as if there were a 00 00 00 01 startcode after the SEI that
> some tool has tried to 0x03 escape. That is a bug. Can you share samples
> containing these NAL units?
>
This file is inserting the emulation prevention byte 0x03. I can take a
look later today.


> >
> > This time type the content of the SEI has type 0 payload but
> > invalid length payload data, which violates recommendation
> > appendix D.
> >
> > Whether it is a bug or not, these SEI nalus should be erased to keep
> > the output bytestream not violating the standard. But we may
> > not come across the situration we do need these encoded SEI
> > nalu, so we just reserve a switch and set not to output by default.
> >
> > Signed-off-by: sharpbai 
> > ---
> >  libavcodec/videotoolboxenc.c | 51 +---
> >  1 file changed, 30 insertions(+), 21 deletions(-)
> >
> > diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> > index cc08cf6a50..c240090f10 100644
> > --- a/libavcodec/videotoolboxenc.c
> > +++ b/libavcodec/videotoolboxenc.c
> > @@ -221,6 +221,7 @@ typedef struct VTEncContext {
> >
> >  int64_t allow_sw;
> >  int64_t require_sw;
> > +int64_t ignore_internal_sei;
> >
> >  bool flushing;
> >  bool has_b_frames;
> > @@ -1690,7 +1691,8 @@ static int copy_replace_length_codes(
> >  CMSampleBufferRef sample_buffer,
> >  ExtraSEI  *sei,
> >  uint8_t   *dst_data,
> > -size_tdst_size)
> > +size_tdst_size,
> > +int   ignore_internal_sei)
> >  {
> >  size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
> >  size_t remaining_src_size = src_size;
> > @@ -1707,8 +1709,8 @@ static int copy_replace_length_codes(
> >  }
> >
> >  while (remaining_src_size > 0) {
> > -size_t curr_src_len;
> > -size_t curr_dst_len;
> > +size_t curr_src_len = 0;
> > +size_t curr_dst_len = 0;
> >  size_t box_len = 0;
> >  size_t i;
> >
> > @@ -1740,7 +1742,8 @@ static int copy_replace_length_codes(
> >  box_len |= size_buf[i];
> >  }
> >
> > -if (sei && !wrote_sei && is_post_sei_nal_type(nal_type)) {
> > +if (sei && !wrote_sei &&
> > +(ignore_internal_sei ||
> is_post_sei_nal_type(nal_type))) {
> >  //No SEI NAL unit - insert.
> >  int wrote_bytes;
> >
> > @@ -1775,27 +1778,30 @@ static int copy_replace_length_codes(
> >  }
> >
> >  curr_src_len = box_len + length_code_size;
> > -curr_dst_len = box_len + sizeof(start_code);
> >
> > -if (remaining_src_size < curr_src_len) {
> > -return AVERROR_BUFFER_TOO_SMALL;
> > -}
> > +if (!(ignore_internal_sei && nal_type == H264_NAL_SEI)) {
> > +curr_dst_len = box_len + sizeof(start_code);
> >
> > -if (remaining_dst_size < curr_dst_len) {
> > -return AVERROR_BUFFER_TOO_SMALL;
> > -}
> > +if (remaining_src_size < curr_src_len) {
> > +return AVERROR_BUFFER_TOO_SMALL;
> > +}
> > +
> > +if (remaining_dst_size < curr_dst_len) {
> > +return AVERROR_BUFFER_TOO_SMALL;
> > +}
> >
> > -dst_box = dst_data + sizeof(start_code);
> > +dst_box = dst_data + sizeof(start_code);
> >
> > -memcpy(dst_data, star

Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: don't wait when flushing data

2020-08-28 Thread Rick Kern
On Wed, Aug 26, 2020 at 10:53 PM Steven Liu  wrote:

> From: Tian Qi 
>
> because there is run in thread mode, few times will block
> the workflow at the wait, so check the status is flushing data,
> don't wait when flushing data.
>
I'd like to reproduce the issue and test the fix before the patch is
applied, so if you can provide a media file that causes the issue or code
snippet, it would be helpful.

It looks like this could happen after an encoding error. Do you see any
"Error encoding frame ..." logs?

>
> Signed-off-by: Tian Qi 
> Signed-off-by: Steven Liu 
> ---
>  libavcodec/videotoolboxenc.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index e89cfaeed8..f9626be18f 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -292,7 +292,7 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait,
> CMSampleBufferRef *buf, E
>  return 0;
>  }
>
> -while (!vtctx->q_head && !vtctx->async_error && wait) {
> +while (!vtctx->q_head && !vtctx->async_error && wait &&
> !vtctx->flushing) {
>  pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
>  }
>
> @@ -308,6 +308,7 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait,
> CMSampleBufferRef *buf, E
>  vtctx->q_tail = NULL;
>  }
>
> +vtctx->frame_ct_out++;
>  pthread_mutex_unlock(&vtctx->lock);
>
>  *buf = info->cm_buffer;
> @@ -319,7 +320,6 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait,
> CMSampleBufferRef *buf, E
>  }
>  av_free(info);
>
> -vtctx->frame_ct_out++;
>
>  return 0;
>  }
> --
> 2.25.0
>
>
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH] [PATCH] avcodec: Fix h264_videotoolbox writing b-frames (fix ticket #8353)

2020-03-21 Thread Rick Kern
On Wed, Mar 18, 2020 at 12:32 PM Nomis101  wrote:

> Am 29.02.20 um 02:39 schrieb Nomis101:
> > From: Damiano Galassi 
> >
> > Signed-off-by: Nomis101 
> > ---
> >  libavcodec/videotoolboxenc.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> > index cc08cf6a50..32d893e485 100644
> > --- a/libavcodec/videotoolboxenc.c
> > +++ b/libavcodec/videotoolboxenc.c
> > @@ -1355,6 +1355,7 @@ static int vtenc_configure_encoder(AVCodecContext
> *avctx)
> >  vtctx->get_param_set_func =
> compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
> >  if (!vtctx->get_param_set_func) return AVERROR(EINVAL);
> >  if (!get_vt_hevc_profile_level(avctx, &profile_level)) return
> AVERROR(EINVAL);
> > +vtctx->has_b_frames = avctx->max_b_frames > 0;
>
This is in the HEVC block. The H.264 encoder will output B-frames if the
hardware supports it and the -bf parameter is used with a value of 1 or
more.

>  }
> >
> >  enc_info = CFDictionaryCreateMutable(
> > --
> > 2.21.1 (Apple Git-122.3)
> >
>
>
> What do I need to do to get this reviewed? Its just a one-liner.
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH v2] configure: Work around a start-up crash when building with clang.

2019-11-21 Thread Rick Kern
On Wed, Nov 20, 2019 at 11:29 AM Hendrik Leppkes 
wrote:

> On Wed, Nov 20, 2019 at 5:00 PM Timo Rothenpieler 
> wrote:
> >
> > On 20.11.2019 13:51, Rick Kern wrote:
> > > The current version of clang enables stack checking by default, causing
> > > a crash when binaries are run.
> >
> > Why does it trigger a crash? Doesn't it indicate something is wrong that
> > should be fixed instead?
>

Here's the discussion about the compiler bug -
https://forums.developer.apple.com/thread/121887

Maybe this is too heavy-handed. Disabling avx, avx2, and avx512 fixes the
issue too, so maybe this is the better than disabling a security feature.
Detecting the compiler bug in configure and disabling only when present
would have less impact on performance.

I'm torn between automatically disabling it when the bug is detected, or
bailing from configure with an error message to disable it.  What's the
best option?


>
> Its a bug in the Apple compiler (not in Clang in general afaik) that
> causes stack alignment to be thrown off.
>
> The commit should probably indicate that this is a Apple compiler bug.
>
> -  Hendrik
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2] configure: Work around a start-up crash when building with clang.

2019-11-20 Thread Rick Kern
The current version of clang enables stack checking by default, causing
a crash when binaries are run.

Fixes #8073 using the -fno-stack-check work-around mentioned in the ticket.
---
 configure | 1 +
 1 file changed, 1 insertion(+)

diff --git a/configure b/configure
index 8f4f2884cf..97e614f18a 100755
--- a/configure
+++ b/configure
@@ -6880,6 +6880,7 @@ elif enabled clang; then
 check_cflags -Werror=implicit-function-declaration
 check_cflags -Werror=missing-prototypes
 check_cflags -Werror=return-type
+check_cflags -fno-stack-check
 elif enabled cparser; then
 add_cflags -Wno-missing-variable-declarations
 add_cflags -Wno-empty-statement
-- 
2.20.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] configure: Fix build on MacOS 10.15

2019-11-17 Thread Rick Kern
The default ./configure produces binaries that segfault on MacOS 10.15.
Fixes #8073 using the work-around mentioned in the ticket.
---
 configure | 1 +
 1 file changed, 1 insertion(+)

diff --git a/configure b/configure
index 31a3744dc6..6e371ae433 100755
--- a/configure
+++ b/configure
@@ -6878,6 +6878,7 @@ elif enabled clang; then
 check_cflags -Werror=implicit-function-declaration
 check_cflags -Werror=missing-prototypes
 check_cflags -Werror=return-type
+check_cflags -fno-stack-check
 elif enabled cparser; then
 add_cflags -Wno-missing-variable-declarations
 add_cflags -Wno-empty-statement
-- 
2.20.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH] [PATCH] avcodec: Add more kCVImageBufferColorPrimaries to videotoolboxenc

2019-11-17 Thread Rick Kern
On Sun, Nov 17, 2019 at 10:25 AM Nomis101  wrote:

> Am 17.11.19 um 01:05 schrieb Richard Kern:
> >
> >> On Nov 16, 2019, at 7:03 PM, Nomis101  wrote:
> >>
> >> Thanks for reviewing. Would be nice, if somebody could push to master
> then. I can't.
> >>
> >>
> > I’ll test it out tomorrow and push.
>
> Cool, thank you very much. :-)
>
Thanks, pushed.


>
> >
> >>> Am 15.11.19 um 15:58 schrieb Limin Wang:
>  On Sun, Nov 03, 2019 at 01:20:38AM +0100, Nomis101 wrote:
>  ---
>  libavcodec/videotoolboxenc.c | 8 
>  1 file changed, 8 insertions(+)
> 
>  diff --git a/libavcodec/videotoolboxenc.c
> b/libavcodec/videotoolboxenc.c
>  index 40a7f643e0..cc08cf6a50 100644
>  --- a/libavcodec/videotoolboxenc.c
>  +++ b/libavcodec/videotoolboxenc.c
>  @@ -894,6 +894,14 @@ static int get_cv_color_primaries(AVCodecContext
> *avctx,
>  *primaries = NULL;
>  break;
> 
>  +case AVCOL_PRI_BT470BG:
>  +*primaries = kCVImageBufferColorPrimaries_EBU_3213;
>  +break;
>  +
>  +case AVCOL_PRI_SMPTE170M:
>  +*primaries = kCVImageBufferColorPrimaries_SMPTE_C;
>  +break;
>  +
> >>> lgtm
> >>>
>  case AVCOL_PRI_BT709:
>  *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
>  break;
>  --
>  2.21.0 (Apple Git-122)
> 
>  ___
>  ffmpeg-devel mailing list
>  ffmpeg-devel@ffmpeg.org
>  https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
>  To unsubscribe, visit link above, or email
>  ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> >>> ___
> >>> ffmpeg-devel mailing list
> >>> ffmpeg-devel@ffmpeg.org
> >>> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >>>
> >>> To unsubscribe, visit link above, or email
> >>> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> >> ___
> >> ffmpeg-devel mailing list
> >> ffmpeg-devel@ffmpeg.org
> >> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >>
> >> To unsubscribe, visit link above, or email
> >> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
>
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH v1 1/2] avcodec/videotoolboxenc: add H264 Extended profile and level

2019-09-02 Thread Rick Kern
On Mon, Sep 2, 2019 at 9:53 AM Limin Wang  wrote:

> On Tue, Aug 27, 2019 at 10:56:42AM -0400, Richard Kern wrote:
>
> > I’ll look at it this weekend.
> ping
>
>
It shows error kVTParameterErr when using the software encoder:

Error setting profile/level property: -12902

I used the following command for testing: ffmpeg -i "$mediaFile" -c:v
h264_videotoolbox -profile:v extended -y -require_sw 1 h264.ts


It defaults to High profile in the output.



The hardware encoder also fails to encode using Extended profile, but the
OS complains the parameter isn't supported. It also defaults to High
profile in the output, which seems like a good default.



Can you try both hardware and software encodes on your end? If you're
seeing Extended profile using ffprobe on either output file, I'll push the
changes.



>
> > > On Aug 27, 2019, at 10:40 AM, Limin Wang 
> wrote:
> > >
> > >
> > > ping the patchset.
> > >
> > >> On Tue, Aug 20, 2019 at 07:04:29PM +0800, lance.lmw...@gmail.com
> wrote:
> > >> From: Limin Wang 
> > >>
> > >> Signed-off-by: Limin Wang 
> > >> ---
> > >> libavcodec/videotoolboxenc.c | 14 ++
> > >> 1 file changed, 14 insertions(+)
> > >>
> > >> diff --git a/libavcodec/videotoolboxenc.c
> b/libavcodec/videotoolboxenc.c
> > >> index d76bb7f646..b16b056f6c 100644
> > >> --- a/libavcodec/videotoolboxenc.c
> > >> +++ b/libavcodec/videotoolboxenc.c
> > >> @@ -80,6 +80,8 @@ static struct{
> > >> CFStringRef kVTProfileLevel_H264_High_5_1;
> > >> CFStringRef kVTProfileLevel_H264_High_5_2;
> > >> CFStringRef kVTProfileLevel_H264_High_AutoLevel;
> > >> +CFStringRef kVTProfileLevel_H264_Extended_5_0;
> > >> +CFStringRef kVTProfileLevel_H264_Extended_AutoLevel;
> > >>
> > >> CFStringRef kVTProfileLevel_HEVC_Main_AutoLevel;
> > >> CFStringRef kVTProfileLevel_HEVC_Main10_AutoLevel;
> > >> @@ -137,6 +139,8 @@ static void loadVTEncSymbols(){
> > >> GET_SYM(kVTProfileLevel_H264_High_5_1,   "H264_High_5_1");
> > >> GET_SYM(kVTProfileLevel_H264_High_5_2, "H264_High_5_2");
> > >> GET_SYM(kVTProfileLevel_H264_High_AutoLevel,
>  "H264_High_AutoLevel");
> > >> +GET_SYM(kVTProfileLevel_H264_Extended_5_0,
>  "H264_Extended_5_0");
> > >> +GET_SYM(kVTProfileLevel_H264_Extended_AutoLevel,
> "H264_Extended_AutoLevel");
> > >>
> > >> GET_SYM(kVTProfileLevel_HEVC_Main_AutoLevel,
>  "HEVC_Main_AutoLevel");
> > >> GET_SYM(kVTProfileLevel_HEVC_Main10_AutoLevel,
>  "HEVC_Main10_AutoLevel");
> > >> @@ -154,6 +158,7 @@ typedef enum VT_H264Profile {
> > >> H264_PROF_BASELINE,
> > >> H264_PROF_MAIN,
> > >> H264_PROF_HIGH,
> > >> +H264_PROF_EXTENDED,
> > >> H264_PROF_COUNT
> > >> } VT_H264Profile;
> > >>
> > >> @@ -704,6 +709,14 @@ static bool
> get_vt_h264_profile_level(AVCodecContext *avctx,
> > >>
>  compat_keys.kVTProfileLevel_H264_High_5_2;   break;
> > >> }
> > >> break;
> > >> +case H264_PROF_EXTENDED:
> > >> +switch (vtctx->level) {
> > >> +case  0: *profile_level_val =
> > >> +
> compat_keys.kVTProfileLevel_H264_Extended_AutoLevel; break;
> > >> +case 50: *profile_level_val =
> > >> +
> compat_keys.kVTProfileLevel_H264_Extended_5_0;   break;
> > >> +}
> > >> +break;
> > >> }
> > >>
> > >> if (!*profile_level_val) {
> > >> @@ -2531,6 +2544,7 @@ static const AVOption h264_options[] = {
> > >> { "baseline", "Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
> > >> { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
> > >> { "high", "High Profile", 0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
> > >> +{ "extended", "Extend Profile",   0, AV_OPT_TYPE_CONST, { .i64 =
> H264_PROF_EXTENDED }, INT_MIN, INT_MAX, VE, "profile" },
> > >>
> > >> { "level", "Level", OFFSET(level), AV_OPT_TYPE_INT, { .i64 = 0 },
> 0, 52, VE, "level" },
> > >> { "1.3", "Level 1.3, only available with Baseline Profile", 0,
> AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX, VE, "level" },
> > >> --
> > >> 2.21.0
> > >>
> > > ___
> > > ffmpeg-devel mailing list
> > > ffmpeg-devel@ffmpeg.org
> > > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > >
> > > To unsubscribe, visit link above, or email
> > > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> > ___
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, 

Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: make transfer_fnc initialized for unsupport function

2019-08-11 Thread Rick Kern
On Sat, Aug 10, 2019 at 11:02 AM Limin Wang  wrote:

>
>
> ping the change!
>
Tests look good. Pushed.


>
> On Thu, Jul 18, 2019 at 06:59:35AM +0800, lance.lmw...@gmail.com wrote:
> > From: Limin Wang 
> >
> > The current function will report one error message, but the caller func
> > haven't check it, so change the default to process as
> AVCOL_TRC_UNSPECIFIED.
> >
> > Signed-off-by: Limin Wang 
> > ---
> >  libavcodec/videotoolboxenc.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> > index ece9d6e..d76bb7f 100644
> > --- a/libavcodec/videotoolboxenc.c
> > +++ b/libavcodec/videotoolboxenc.c
> > @@ -949,6 +949,7 @@ static int get_cv_transfer_function(AVCodecContext
> *avctx,
> >  break;
> >
> >  default:
> > +*transfer_fnc = NULL;
> >  av_log(avctx, AV_LOG_ERROR, "Transfer function %s is not
> supported.\n", av_color_transfer_name(trc));
> >  return -1;
> >  }
> > --
> > 2.6.4
> >
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: add hdr10, linear, hlg color transfer function for videotoolboxenc

2019-07-16 Thread Rick Kern
On Wed, Jul 10, 2019 at 5:29 AM Dennis Mungai  wrote:

> On Wed, Jul 10, 2019, 11:06 Hendrik Leppkes  wrote:
>
> > On Wed, Jul 10, 2019 at 4:23 AM Dennis Mungai  wrote:
> > >
> > > On Wed, Jul 10, 2019, 03:05 Aman Gupta  wrote:
> > >
> > > > On Wed, Jun 26, 2019 at 4:25 AM  wrote:
> > > >
> > > > > From: Limin Wang 
> > > > >
> > > > > Below is the testing ffmpeg command for the setting:
> > > > > ./ffmpeg -i input.ts -c:v hevc_videotoolbox -color_primaries bt2020
> > > > > -colorspace bt2020_ncl -color_trc smpte2084 smpte2048.ts
> > > > > ./ffmpeg -i input.ts -c:v hevc_videotoolbox -color_primaries bt2020
> > > > > -colorspace bt2020_ncl -color_trc linear linear.ts
> > > > > ./ffmpeg -i input.ts -c:v hevc_videotoolbox -color_primaries bt2020
> > > > > -colorspace bt2020_ncl -color_trc arib-std-b67 hlg.ts
> > > > >
> > > >
> > > > Patch looks reasonable to me. Will commit in a few days if no one
> else
> > > > comments.
> > > >
> > > > Aman
> > > >
> > > >
> > > > >
> > > > > Signed-off-by: Limin Wang 
> > > > > ---
> > > > >  configure|  6 ++
> > > > >  libavcodec/videotoolboxenc.c | 16 
> > > > >  2 files changed, 22 insertions(+)
> > > > >
> > > > > diff --git a/configure b/configure
> > > > > index 7cea9d4d73..0a5e940c0c 100755
> > > > > --- a/configure
> > > > > +++ b/configure
> > > > > @@ -2260,6 +2260,9 @@ TOOLCHAIN_FEATURES="
> > > > >  TYPES_LIST="
> > > > >  kCMVideoCodecType_HEVC
> > > > >  kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange
> > > > > +kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ
> > > > > +kCVImageBufferTransferFunction_ITU_R_2100_HLG
> > > > > +kCVImageBufferTransferFunction_Linear
> > > > >  socklen_t
> > > > >  struct_addrinfo
> > > > >  struct_group_source_req
> > > > > @@ -6044,6 +6047,9 @@ enabled videotoolbox && {
> > > > >  check_lib coreservices CoreServices/CoreServices.h
> > > > > UTGetOSTypeFromString "-framework CoreServices"
> > > > >  check_func_headers CoreMedia/CMFormatDescription.h
> > > > > kCMVideoCodecType_HEVC "-framework CoreMedia"
> > > > >  check_func_headers CoreVideo/CVPixelBuffer.h
> > > > > kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange "-framework
> > CoreVideo"
> > > > > +check_func_headers CoreVideo/CVImageBuffer.h
> > > > > kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ "-framework
> > CoreVideo"
> > > > > +check_func_headers CoreVideo/CVImageBuffer.h
> > > > > kCVImageBufferTransferFunction_ITU_R_2100_HLG "-framework
> CoreVideo"
> > > > > +check_func_headers CoreVideo/CVImageBuffer.h
> > > > > kCVImageBufferTransferFunction_Linear "-framework CoreVideo"
> > > > >  }
> > > > >
> > > > >  check_struct "sys/time.h sys/resource.h" "struct rusage" ru_maxrss
> > > > > diff --git a/libavcodec/videotoolboxenc.c
> > b/libavcodec/videotoolboxenc.c
> > > > > index f8ccdea52d..0dc6eb4cf4 100644
> > > > > --- a/libavcodec/videotoolboxenc.c
> > > > > +++ b/libavcodec/videotoolboxenc.c
> > > > > @@ -915,6 +915,22 @@ static int
> > get_cv_transfer_function(AVCodecContext
> > > > > *avctx,
> > > > >  *transfer_fnc =
> > > > > kCVImageBufferTransferFunction_SMPTE_240M_1995;
> > > > >  break;
> > > > >
> > > > > +#if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
> > > > > +case AVCOL_TRC_SMPTE2084:
> > > > > +*transfer_fnc =
> > > > > kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
> > > > > +break;
> > > > > +#endif
> > > > > +#if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR
> > > > > +case AVCOL_TRC_LINEAR:
> > > > > +*transfer_fnc = kCVImageBufferTransferFunction_Linear;
> > > > > +break;
> > > > > +#endif
> > > > > +#if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
> > > > > +case AVCOL_TRC_ARIB_STD_B67:
> > > > > +*transfer_fnc =
> > > > kCVImageBufferTransferFunction_ITU_R_2100_HLG;
> > > > > +break;
> > > > > +#endif
> > > > > +
> > > > >  case AVCOL_TRC_GAMMA22:
> > > > >  gamma = 2.2;
> > > > >  *transfer_fnc =
> kCVImageBufferTransferFunction_UseGamma;
> > > > > --
> > > > > 2.21.0
> > > > >
> > > > > ___
> > > > > ffmpeg-devel mailing list
> > > > > ffmpeg-devel@ffmpeg.org
> > > > > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > > > >
> > > > > To unsubscribe, visit link above, or email
> > > > > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> > > >
> > >
> > > A while back, a similar patch was rejected for NVENC (from the same
> > author)
> > > on the basis of such functionality not being suitably placed as an
> > encoder
> > > feature.
> > >
> > > Looking back at other implementations, eg hevc_vaapi, the same is
> indeed
> > > allowed, where SEI metadata retention for HDR is present.
> > >
> > > To the OP: If the patch for NVENC were refactored in a manner similar
> to
> > > this, abstracting details such as master display info 

Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: Fix compilation for IOS < 11.0 and OSX, < 10.13

2019-05-30 Thread Rick Kern
On Fri, May 24, 2019 at 6:34 PM Thilo Borgmann 
wrote:

> Am 22.05.19 um 14:27 schrieb Thilo Borgmann:
> > Am 22.05.19 um 01:41 schrieb Jan Ekström:
> >> Hi,
> >>
> >> On Tue, May 14, 2019 at 4:16 PM Thilo Borgmann 
> wrote:
> >>>
> >>> $Subject
> >>>
> >>> Tested compilation only, sanity test actually using it appreciated.
> >>>
> >>
> >> Thanks for the patch. To be completely fair, this is not to fix
> >> compilation for specific target systems, but rather to fix compilation
> >> on older SDKs (building with newer SDKs you can still build aiming for
> >> macOS starting from 10.9, for example).
> >>
> >> I didn't notice a patch landed on the encoder side that utilized the
> >> defines without further checking/ifdefs. Too bad. I think I
> >> specifically didn't yet merge the full/limited range patch on the
> >> decoder side due to related reasons.
> >>
> >> I did notice that VLC just re-defined these enum values themselves to
> >> stop needing to have ifdefs depending on which SDK is being utilized
> >> (
> https://github.com/videolan/vlc/commit/1b7e1c4bfcda375e2d4e657135aeaf3732e44af2#diff-a11cdb805d111956af60619d7dfa022bR735
> ).
> >> I wonder if we should have a helper header that would re-define these
> >> enum values with their name. That way the code would look correct, and
> >> the resulting binary has the same features independent of the SDK it
> >> had been built under.
> >>
> >> What would be the opinion of people on a solution like this?
> >
> > Tested with a local definition of the symbols (like a would be header
> would do).
> > Seems to work for building with -macosx-version-min=XXX.
> >
> > Also checked with VLC, they do these checks via thinks like:
> >
> > #ifndef MAC_OS_X_VERSION_10_13
> >   ...
> > #endif
> >
> > Wich might be a better alternative to what I suggested.
> >
> > Thus I would be fine with a helping header.
>
> If nobody else cares, should I try to come up with something like this? Or
> do you want to?
>

If you're willing to maintain the header as new values are added, it will
make it easier to read the code.


>
> -Thilo
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH] MAINTAINERS: add myself as videotoolbox* maintainer

2017-11-13 Thread Rick Kern



On 11/12/2017 05:16 PM, Aman Gupta wrote:

From: Aman Gupta 

---
  MAINTAINERS | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 4df6c6533d..6a92b5190d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -245,7 +245,7 @@ Codecs:
txd.c Ivo van Poorten
vc2*  Rostislav Pehlivanov
vcr1.cMichael Niedermayer
-  videotoolboxenc.c Rick Kern
+  videotoolboxenc.c Rick Kern, Aman Gupta
vima.cPaul B Mahol
vorbisdec.c   Denes Balatoni, David Conrad
vorbisenc.c   Oded Shimon
@@ -272,7 +272,7 @@ Hardware acceleration:
vaapi*Gwenole Beauchesne
vaapi_encode* Mark Thompson
vdpau*Philip Langdale, Carl Eugen Hoyos
-  videotoolbox* Rick Kern
+  videotoolbox* Rick Kern, Aman Gupta
  
  
  libavdevice

LGTM
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3] lavd/decklink_common: Fix error caused by -Werror=missing-prototypes

2016-06-26 Thread Rick Kern
decklink_common.cpp includes a .cpp file from the DeckLink API which fails
to build because there are non-static functions in the included .cpp file.
This disables the missing-prototypes error so the file can be included.

Signed-off-by: Rick Kern 
---
 libavdevice/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libavdevice/Makefile b/libavdevice/Makefile
index 585827b..e281825 100644
--- a/libavdevice/Makefile
+++ b/libavdevice/Makefile
@@ -19,6 +19,8 @@ OBJS-$(CONFIG_BKTR_INDEV)+= bktr.o
 OBJS-$(CONFIG_CACA_OUTDEV)   += caca.o
 OBJS-$(CONFIG_DECKLINK_OUTDEV)   += decklink_enc.o decklink_enc_c.o 
decklink_common.o
 OBJS-$(CONFIG_DECKLINK_INDEV)+= decklink_dec.o decklink_dec_c.o 
decklink_common.o
+$(SUBDIR)decklink_common.o: CXXFLAGS += -Wno-error=missing-prototypes
+
 OBJS-$(CONFIG_DSHOW_INDEV)   += dshow_crossbar.o dshow.o 
dshow_enummediatypes.o \
 dshow_enumpins.o dshow_filter.o \
 dshow_pin.o dshow_common.o
-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH] configure: remove -std= from c++ compilation

2016-06-26 Thread Rick Kern
Pulling -std=c99 into CXXFLAGS from CFLAGS causes a compile error with c++
files using clang on OS X.
Adding -std=c++98 unconditionally to CXXFLAGS will generate a compile error
on compilers that don't support the -std option.
It works but is less readable if -std=c++98 is appended after -std=c99 when
the -std option is supported.

Signed-off-by: Rick Kern 
---
 common.mak | 2 +-
 configure  | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/common.mak b/common.mak
index 3f2096d..c4ab049 100644
--- a/common.mak
+++ b/common.mak
@@ -39,7 +39,7 @@ CCFLAGS = $(CPPFLAGS) $(CFLAGS)
 OBJCFLAGS  += $(EOBJCFLAGS)
 OBJCCFLAGS  = $(CPPFLAGS) $(CFLAGS) $(OBJCFLAGS)
 ASFLAGS:= $(CPPFLAGS) $(ASFLAGS)
-CXXFLAGS   := $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS)
+CXXFLAGS   := $(CPPFLAGS) $(patsubst -std=%,,$(CFLAGS)) $(CXXFLAGS)
 YASMFLAGS  += $(IFLAGS:%=%/) -Pconfig.asm
 
 HOSTCCFLAGS = $(IFLAGS) $(HOSTCPPFLAGS) $(HOSTCFLAGS)
diff --git a/configure b/configure
index 76d2d27..901df6c 100755
--- a/configure
+++ b/configure
@@ -4529,7 +4529,6 @@ fi
 
 add_cppflags -D_ISOC99_SOURCE
 add_cxxflags -D__STDC_CONSTANT_MACROS
-add_cxxflags -std=c++98
 check_cflags -std=c99
 check_cc -D_FILE_OFFSET_BITS=64 <
-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH] configure: use c++98 for c++ files

2016-06-26 Thread Rick Kern
Use c++98 standard instead of c++11.

Signed-off-by: Rick Kern 
---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/configure b/configure
index 19aea61..76d2d27 100755
--- a/configure
+++ b/configure
@@ -4529,7 +4529,7 @@ fi
 
 add_cppflags -D_ISOC99_SOURCE
 add_cxxflags -D__STDC_CONSTANT_MACROS
-add_cxxflags -std=c++11
+add_cxxflags -std=c++98
 check_cflags -std=c99
 check_cc -D_FILE_OFFSET_BITS=64 <
-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2] lavd/decklink_common: Fix error caused by -Werror=missing-prototypes

2016-06-24 Thread Rick Kern
decklink_common.cpp includes a .cpp file from the DeckLink API which fails
to build because there are non-static functions in the included .cpp file.
This disables the missing-prototypes error so the file can be included.

Signed-off-by: Rick Kern 
---
 configure | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index 3787894..9068d6c 100755
--- a/configure
+++ b/configure
@@ -5615,7 +5615,8 @@ enabled cuvid && { check_lib cuviddec.h 
cuvidCreateDecoder -lnvcuvid
 enabled chromaprint   && require chromaprint chromaprint.h 
chromaprint_get_version -lchromaprint
 enabled coreimage_filter  && { check_header_objcc QuartzCore/CoreImage.h || 
disable coreimage_filter; }
 enabled coreimagesrc_filter && { check_header_objcc QuartzCore/CoreImage.h || 
disable coreimagesrc_filter; }
-enabled decklink  && { check_header DeckLinkAPI.h || die "ERROR: 
DeckLinkAPI.h header not found"; }
+enabled decklink  && { { check_header DeckLinkAPI.h || die "ERROR: 
DeckLinkAPI.h header not found"; } &&
+ add_cxxflags -Wno-error=missing-prototypes; }
 enabled frei0r&& { check_header frei0r.h || die "ERROR: frei0r.h 
header not found"; }
 enabled gmp   && require2 gmp gmp.h mpz_export -lgmp
 enabled gnutls&& require_pkg_config gnutls gnutls/gnutls.h 
gnutls_global_init
-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH] lavd/avfoundation: Fix skewed video output

2016-06-24 Thread Rick Kern
Fixes #5654.
The linesize can be greater than the minimum required. This copies the
frame taking linesize into account.

Signed-off-by: Rick Kern 
---
 libavdevice/avfoundation.m | 56 --
 1 file changed, 49 insertions(+), 7 deletions(-)

diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index e2ddf47..8b92308 100644
--- a/libavdevice/avfoundation.m
+++ b/libavdevice/avfoundation.m
@@ -35,6 +35,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/time.h"
+#include "libavutil/imgutils.h"
 #include "avdevice.h"
 
 static const int avf_time_base = 100;
@@ -892,6 +893,49 @@ fail:
 return AVERROR(EIO);
 }
 
+static int copy_cvpixelbuffer(AVFormatContext *s,
+   CVPixelBufferRef image_buffer,
+   AVPacket *pkt)
+{
+AVFContext *ctx = s->priv_data;
+int src_linesize[4];
+const uint8_t *src_data[4];
+int width  = CVPixelBufferGetWidth(image_buffer);
+int height = CVPixelBufferGetHeight(image_buffer);
+int status;
+
+memset(src_linesize, 0, sizeof(src_linesize));
+memset(src_data, 0, sizeof(src_data));
+
+status = CVPixelBufferLockBaseAddress(image_buffer, 0);
+if (status != kCVReturnSuccess) {
+av_log(s, AV_LOG_ERROR, "Could not lock base address: %d\n", status);
+return AVERROR_EXTERNAL;
+}
+
+if (CVPixelBufferIsPlanar(image_buffer)) {
+size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
+int i;
+for(i = 0; i < plane_count; i++){
+src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer, 
i);
+src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer, i);
+}
+} else {
+src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
+src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
+}
+
+status = av_image_copy_to_buffer(pkt->data, pkt->size,
+ src_data, src_linesize,
+ ctx->pixel_format, width, height, 1);
+
+
+
+CVPixelBufferUnlockBaseAddress(image_buffer, 0);
+
+return status;
+}
+
 static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
 AVFContext* ctx = (AVFContext*)s->priv_data;
@@ -903,7 +947,7 @@ static int avf_read_packet(AVFormatContext *s, AVPacket 
*pkt)
 image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
 
 if (ctx->current_frame != nil) {
-void *data;
+int status;
 if (av_new_packet(pkt, 
(int)CVPixelBufferGetDataSize(image_buffer)) < 0) {
 return AVERROR(EIO);
 }
@@ -919,14 +963,12 @@ static int avf_read_packet(AVFormatContext *s, AVPacket 
*pkt)
 pkt->stream_index  = ctx->video_stream_index;
 pkt->flags|= AV_PKT_FLAG_KEY;
 
-CVPixelBufferLockBaseAddress(image_buffer, 0);
-
-data = CVPixelBufferGetBaseAddress(image_buffer);
-memcpy(pkt->data, data, pkt->size);
-
-CVPixelBufferUnlockBaseAddress(image_buffer, 0);
+status = copy_cvpixelbuffer(s, image_buffer, pkt);
 CFRelease(ctx->current_frame);
 ctx->current_frame = nil;
+
+if (status < 0)
+return status;
 } else if (ctx->current_audio_frame != nil) {
 CMBlockBufferRef block_buffer = 
CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
 int block_buffer_size = 
CMBlockBufferGetDataLength(block_buffer);
-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 1/2] lavd/decklink: Fix compile issue on OS X

2016-06-23 Thread Rick Kern
Fixes #4124: Invalid argument '-std=c99' not allowed with 'C++/ObjC++'
C++ files fail to compile. This adds '-std=c++11' to CXX_FLAGS to fix.

Signed-off-by: Rick Kern 
---
 common.mak | 2 +-
 configure  | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/common.mak b/common.mak
index 59b039f..3f2096d 100644
--- a/common.mak
+++ b/common.mak
@@ -39,7 +39,7 @@ CCFLAGS = $(CPPFLAGS) $(CFLAGS)
 OBJCFLAGS  += $(EOBJCFLAGS)
 OBJCCFLAGS  = $(CPPFLAGS) $(CFLAGS) $(OBJCFLAGS)
 ASFLAGS:= $(CPPFLAGS) $(ASFLAGS)
-CXXFLAGS   += $(CPPFLAGS) $(CFLAGS)
+CXXFLAGS   := $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS)
 YASMFLAGS  += $(IFLAGS:%=%/) -Pconfig.asm
 
 HOSTCCFLAGS = $(IFLAGS) $(HOSTCPPFLAGS) $(HOSTCFLAGS)
diff --git a/configure b/configure
index 94a0a6c..3787894 100755
--- a/configure
+++ b/configure
@@ -4519,6 +4519,7 @@ fi
 
 add_cppflags -D_ISOC99_SOURCE
 add_cxxflags -D__STDC_CONSTANT_MACROS
+add_cxxflags -std=c++11
 check_cflags -std=c99
 check_cc -D_FILE_OFFSET_BITS=64 <
-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 2/2] lavd/decklink_common: Fix error caused by -Werror=missing-prototypes

2016-06-23 Thread Rick Kern
decklink_common.cpp includes a .cpp file from the DeckLink API which fails
to build because there are non-static functions in the included .cpp file.
This temporarily disables the missing-prototypes error so the file can
be included.

Signed-off-by: Rick Kern 
---
 libavdevice/decklink_common.cpp | 18 ++
 1 file changed, 18 insertions(+)

diff --git a/libavdevice/decklink_common.cpp b/libavdevice/decklink_common.cpp
index ac7964c..f4d4275 100644
--- a/libavdevice/decklink_common.cpp
+++ b/libavdevice/decklink_common.cpp
@@ -23,7 +23,25 @@
 #ifdef _WIN32
 #include 
 #else
+#include "libavutil/attributes.h"
+
+#if AV_GCC_VERSION_AT_LEAST(4, 6)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
+#warning GCC
+#elif defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmissing-prototypes"
+#endif
+
 #include 
+
+#if AV_GCC_VERSION_AT_LEAST(4, 6)
+#pragma GCC diagnostic pop
+#elif defined(__clang__)
+#pragma clang diagnostic pop
+#warning clang
+#endif
 #endif
 
 #include 
-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 0/2] DeckLink build fixes

2016-06-23 Thread Rick Kern
This fixes two build issues on OS X when --enable-decklink.

Rick Kern (2):
  lavd/decklink: Fix compile issue on OS X
  lavd/decklink_common: Fix error caused by -Werror=missing-prototypes

 common.mak  |  2 +-
 configure   |  1 +
 libavdevice/decklink_common.cpp | 18 ++
 3 files changed, 20 insertions(+), 1 deletion(-)

-- 
2.9.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH] MAINTAINERS: Add myself for videotoolbox.c, remove vda* maintainer

2016-06-16 Thread Rick Kern
Person in MAINTAINERS hasn't responded to a patch on the ML or private
email, and doesn't maintain the files according to git.

Signed-off-by: Rick Kern 
---
 MAINTAINERS | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 4fe999d..ef23967 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -306,9 +306,8 @@ Hardware acceleration:
   mediacodec*   Matthieu Bouron
   vaapi*Gwenole Beauchesne
   vaapi_encode* Mark Thompson
-  vda*  Sebastien Zwickert
   vdpau*Philip Langdale, Carl Eugen Hoyos
-  videotoolbox* Sebastien Zwickert
+  videotoolbox*     Rick Kern
 
 
 libavdevice
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 2/2] lavc/videotoolboxenc: set extradata when opening codec

2016-06-01 Thread Rick Kern
VideoToolbox doesn't supply parameter sets until the first frame is done
encoding. This spins up a temporary encoder and encodes a single frame to
get this data.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 326 ---
 1 file changed, 241 insertions(+), 85 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index f4f0d8e..895924a 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -96,6 +96,13 @@ typedef struct VTEncContext {
 bool warned_color_range;
 } VTEncContext;
 
+static int vtenc_populate_extradata(AVCodecContext   *avctx,
+CMVideoCodecType codec_type,
+CFStringRef  profile_level,
+CFNumberRef  gamma_level,
+CFDictionaryRef  enc_info,
+CFDictionaryRef  pixel_buffer_info);
+
 /**
  * NULL-safe release of *refPtr, and sets value to NULL.
  */
@@ -388,7 +395,7 @@ static int set_extradata(AVCodecContext *avctx, 
CMSampleBufferRef sample_buffer)
 return status;
 }
 
-avctx->extradata = av_malloc(total_size);
+avctx->extradata = av_mallocz(total_size + AV_INPUT_BUFFER_PADDING_SIZE);
 if (!avctx->extradata) {
 return AVERROR(ENOMEM);
 }
@@ -761,83 +768,28 @@ static int get_cv_ycbcr_matrix(AVCodecContext *avctx, 
CFStringRef *matrix) {
 return 0;
 }
 
-
-static av_cold int vtenc_init(AVCodecContext *avctx)
+static int vtenc_create_encoder(AVCodecContext   *avctx,
+CMVideoCodecType codec_type,
+CFStringRef  profile_level,
+CFNumberRef  gamma_level,
+CFDictionaryRef  enc_info,
+CFDictionaryRef  pixel_buffer_info,
+VTCompressionSessionRef *session)
 {
-CFMutableDictionaryRef enc_info;
-CFMutableDictionaryRef pixel_buffer_info;
-CMVideoCodecType   codec_type;
-VTEncContext   *vtctx = avctx->priv_data;
-CFStringRefprofile_level;
-SInt32 bit_rate = avctx->bit_rate;
-CFNumberRefbit_rate_num;
-CFBooleanRef   has_b_frames_cfbool;
-CFNumberRefgamma_level;
-intstatus;
-
-codec_type = get_cm_codec_type(avctx->codec_id);
-if (!codec_type) {
-av_log(avctx, AV_LOG_ERROR, "Error: no mapping for AVCodecID %d\n", 
avctx->codec_id);
-return AVERROR(EINVAL);
-}
-
-vtctx->has_b_frames = avctx->max_b_frames > 0;
-if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
-av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline 
profile. Output will not contain B-frames.\n");
-vtctx->has_b_frames = false;
-}
-
-if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
-av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' 
profile, but baseline was requested. Encode will not use CABAC entropy.\n");
-vtctx->entropy = VT_ENTROPY_NOT_SET;
-}
-
-if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
-
-vtctx->session = NULL;
-
-enc_info = CFDictionaryCreateMutable(
-kCFAllocatorDefault,
-20,
-&kCFCopyStringDictionaryKeyCallBacks,
-&kCFTypeDictionaryValueCallBacks
-);
-
-if (!enc_info) return AVERROR(ENOMEM);
-
-#if !TARGET_OS_IPHONE
-if (!vtctx->allow_sw) {
-CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
-} else {
-CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,  
kCFBooleanTrue);
-}
-#endif
-
-if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
-status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
-if (status) {
-CFRelease(enc_info);
-return status;
-}
-} else {
-pixel_buffer_info = NULL;
-}
-
-status = VTCompressionSessionCreate(
-kCFAllocatorDefault,
-avctx->width,
-avctx->height,
-codec_type,
-enc_info,
-pixel_buffer_info,
-kCFAllocatorDefault,
-vtenc_output_callback,
-avctx,
-&vtctx->session
-);
-
-if (pixel_buffer_info) CFRelease(pixel_buffer_info);
-CFRelease(enc_info);
+VTEncContext *vtctx = avctx->priv_data;
+SInt32   bit_rate = avctx->bit_rate;
+CFNumberRef  bit_rate_num;
+
+int status = VTCompressionSessionCreate(kCFAllocatorDefault,
+  

[FFmpeg-devel] [PATCH 1/2] lavc/videotoolboxenc: fix allocated buffer size

2016-06-01 Thread Rick Kern
Allocates a buffer with padding, and with enough room for start codes when
length codes are smaller.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 223 ++-
 1 file changed, 94 insertions(+), 129 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 57ea2b3..f4f0d8e 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -199,6 +199,45 @@ static void vtenc_q_push(VTEncContext *vtctx, 
CMSampleBufferRef buffer)
 pthread_mutex_unlock(&vtctx->lock);
 }
 
+static int count_nalus(size_t length_code_size,
+   CMSampleBufferRef sample_buffer,
+   int *count)
+{
+size_t offset = 0;
+int status;
+int nalu_ct = 0;
+uint8_t size_buf[4];
+size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
+CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
+
+if (length_code_size > 4)
+return AVERROR_INVALIDDATA;
+
+while (offset < src_size) {
+size_t curr_src_len;
+size_t box_len = 0;
+size_t i;
+
+status = CMBlockBufferCopyDataBytes(block,
+offset,
+length_code_size,
+size_buf);
+
+for (i = 0; i < length_code_size; i++) {
+box_len <<= 8;
+box_len |= size_buf[i];
+}
+
+curr_src_len = box_len + length_code_size;
+offset += curr_src_len;
+
+nalu_ct++;
+}
+
+*count = nalu_ct;
+return 0;
+}
+
 static CMVideoCodecType get_cm_codec_type(enum AVCodecID id)
 {
 switch (id) {
@@ -207,12 +246,6 @@ static CMVideoCodecType get_cm_codec_type(enum AVCodecID 
id)
 }
 }
 
-static void vtenc_free_block(void *opaque, uint8_t *data)
-{
-CMBlockBufferRef block = opaque;
-CFRelease(block);
-}
-
 /**
  * Get the parameter sets from a CMSampleBufferRef.
  * @param dst If *dst isn't NULL, the parameters are copied into existing
@@ -1081,54 +1114,6 @@ static void vtenc_get_frame_info(CMSampleBufferRef 
buffer, bool *is_key_frame)
 }
 
 /**
- * Replaces length codes with H.264 Annex B start codes.
- * length_code_size must equal sizeof(start_code).
- * On failure, the contents of data may have been modified.
- *
- * @param length_code_size Byte length of each length code
- * @param data Call with NAL units prefixed with length codes.
- * On success, the length codes are replace with
- * start codes.
- * @param size Length of data, excluding any padding.
- * @return 0 on success
- * AVERROR_BUFFER_TOO_SMALL if length code size is smaller
- * than a start code or if a length_code in data specifies
- * data beyond the end of its buffer.
- */
-static int replace_length_codes(size_t  length_code_size,
-uint8_t *data,
-size_t  size)
-{
-size_t remaining_size = size;
-
-if (length_code_size != sizeof(start_code)) {
-av_log(NULL, AV_LOG_ERROR, "Start code size and length code size not 
equal.\n");
-return AVERROR_BUFFER_TOO_SMALL;
-}
-
-while (remaining_size > 0) {
-size_t box_len = 0;
-size_t i;
-
-for (i = 0; i < length_code_size; i++) {
-box_len <<= 8;
-box_len |= data[i];
-}
-
-if (remaining_size < box_len + sizeof(start_code)) {
-av_log(NULL, AV_LOG_ERROR, "Length is out of range.\n");
-AVERROR_BUFFER_TOO_SMALL;
-}
-
-memcpy(data, start_code, sizeof(start_code));
-data += box_len + sizeof(start_code);
-remaining_size -= box_len + sizeof(start_code);
-}
-
-return 0;
-}
-
-/**
  * Copies NAL units and replaces length codes with
  * H.264 Annex B start codes. On failure, the contents of
  * dst_data may have been modified.
@@ -1148,14 +1133,19 @@ static int replace_length_codes(size_t  
length_code_size,
  * the end of its buffer.
  */
 static int copy_replace_length_codes(
+AVCodecContext *avctx,
 size_tlength_code_size,
-const uint8_t *src_data,
-size_tsrc_size,
+CMSampleBufferRef sample_buffer,
 uint8_t   *dst_data,
 size_tdst_size)
 {
+size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
 size_t remaining_src_size = src_size;
 size_t remaining_dst_size = dst_size;
+size_t src_offset = 0;
+int status;
+uint8_t size_buf[4];
+CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
 
 if (length_code_size > 4) {
 return AVERROR_INVALIDDATA;
@@ -1168,11 +1158,19 @@ static int copy_replace_length_codes(
 size_t i;
 
 uint8_t   *dst_box;
-const uint8

[FFmpeg-devel] [PATCH] lavc/audiotoolboxenc: fix dropped frames on iOS

2016-06-01 Thread Rick Kern
AudioConverterFillComplexBuffer() doesn't always call its callback. A frame
queue is used to prevent skipped audio samples.

Signed-off-by: Rick Kern 
---
 libavcodec/audiotoolboxenc.c | 78 +---
 1 file changed, 52 insertions(+), 26 deletions(-)

diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c
index 855df0c..262308a 100644
--- a/libavcodec/audiotoolboxenc.c
+++ b/libavcodec/audiotoolboxenc.c
@@ -22,6 +22,9 @@
 
 #include 
 
+#define FF_BUFQUEUE_SIZE 256
+#include "libavfilter/bufferqueue.h"
+
 #include "config.h"
 #include "audio_frame_queue.h"
 #include "avcodec.h"
@@ -38,8 +41,8 @@ typedef struct ATDecodeContext {
 int quality;
 
 AudioConverterRef converter;
-AVFrame in_frame;
-AVFrame new_in_frame;
+struct FFBufQueue frame_queue;
+struct FFBufQueue used_frame_queue;
 
 unsigned pkt_size;
 AudioFrameQueue afq;
@@ -449,28 +452,30 @@ static OSStatus ffat_encode_callback(AudioConverterRef 
converter, UInt32 *nb_pac
 {
 AVCodecContext *avctx = inctx;
 ATDecodeContext *at = avctx->priv_data;
+AVFrame *frame;
 
-if (at->eof) {
-*nb_packets = 0;
-return 0;
+if (!at->frame_queue.available) {
+if (at->eof) {
+*nb_packets = 0;
+return 0;
+} else {
+*nb_packets = 0;
+return 1;
+}
 }
 
-av_frame_unref(&at->in_frame);
-av_frame_move_ref(&at->in_frame, &at->new_in_frame);
-
-if (!at->in_frame.data[0]) {
-*nb_packets = 0;
-return 1;
-}
+frame = ff_bufqueue_get(&at->frame_queue);
 
 data->mNumberBuffers  = 1;
 data->mBuffers[0].mNumberChannels = avctx->channels;
-data->mBuffers[0].mDataByteSize   = at->in_frame.nb_samples *
+data->mBuffers[0].mDataByteSize   = frame->nb_samples *
 
av_get_bytes_per_sample(avctx->sample_fmt) *
 avctx->channels;
-data->mBuffers[0].mData   = at->in_frame.data[0];
-if (*nb_packets > at->in_frame.nb_samples)
-*nb_packets = at->in_frame.nb_samples;
+data->mBuffers[0].mData   = frame->data[0];
+if (*nb_packets > frame->nb_samples)
+*nb_packets = frame->nb_samples;
+
+ff_bufqueue_add(avctx, &at->used_frame_queue, frame);
 
 return 0;
 }
@@ -492,20 +497,38 @@ static int ffat_encode(AVCodecContext *avctx, AVPacket 
*avpkt,
 };
 AudioStreamPacketDescription out_pkt_desc = {0};
 
-if ((ret = ff_alloc_packet2(avctx, avpkt, at->pkt_size, 0)) < 0)
-return ret;
-
-av_frame_unref(&at->new_in_frame);
-
 if (frame) {
+AVFrame *in_frame;
+
+if (ff_bufqueue_is_full(&at->frame_queue)) {
+/*
+ * The frame queue is significantly larger than needed in practice,
+ * but no clear way to determine the minimum number of samples to
+ * get output from AudioConverterFillComplexBuffer().
+ */
+av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
+return AVERROR_BUG;
+}
+
 if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
 return ret;
-if ((ret = av_frame_ref(&at->new_in_frame, frame)) < 0)
+
+in_frame = av_frame_alloc();
+if (!in_frame)
+return AVERROR(ENOMEM);
+
+if ((ret = av_frame_ref(in_frame, frame)) < 0)
 return ret;
+
+ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
 } else {
 at->eof = 1;
 }
 
+if ((ret = ff_alloc_packet2(avctx, avpkt, at->pkt_size, 0)) < 0)
+return ret;
+
+
 out_buffers.mBuffers[0].mData = avpkt->data;
 
 *got_packet_ptr = avctx->frame_size / at->frame_size;
@@ -513,6 +536,9 @@ static int ffat_encode(AVCodecContext *avctx, AVPacket 
*avpkt,
 ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, 
avctx,
   got_packet_ptr, &out_buffers,
   (avctx->frame_size > at->frame_size) 
? NULL : &out_pkt_desc);
+
+ff_bufqueue_discard_all(&at->used_frame_queue);
+
 if ((!ret || ret == 1) && *got_packet_ptr) {
 avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
 ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
@@ -531,16 +557,16 @@ static av_cold void ffat_encode_flush(AVCodecContext 
*avctx)
 {
 ATDecodeContext *at = avctx->priv_data;
 AudioConverterReset(at->converter);
-av_frame_unref(&at->new_in_frame);
-av_frame_unref(&at->in_frame);
+ff_bufqueue_discard_all(&at-

[FFmpeg-devel] [PATCH] lavc/videotoolbox: fix H.264 hwaccel init issue

2016-06-01 Thread Rick Kern
Fixes VTDecompressionSessionCreate() error.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolbox.c | 59 ---
 1 file changed, 45 insertions(+), 14 deletions(-)

diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c
index 2f4d531..cadfb23 100644
--- a/libavcodec/videotoolbox.c
+++ b/libavcodec/videotoolbox.c
@@ -487,23 +487,53 @@ static CFDictionaryRef 
videotoolbox_buffer_attributes_create(int width,
 return buffer_attributes;
 }
 
-static CMVideoFormatDescriptionRef 
videotoolbox_format_desc_create(CMVideoCodecType codec_type,
+static CMVideoFormatDescriptionRef 
videotoolbox_format_desc_create(AVCodecContext *avctx,
+   
CMVideoCodecType codec_type,

CFDictionaryRef decoder_spec,
int width,
int height)
 {
-CMFormatDescriptionRef cm_fmt_desc;
-OSStatus status;
-
-status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
-codec_type,
-width,
-height,
-decoder_spec, // Dictionary of 
extension
-&cm_fmt_desc);
-
-if (status)
-return NULL;
+CMFormatDescriptionRef cm_fmt_desc = NULL;
+int status;
+H264Context *h = codec_type == kCMVideoCodecType_H264 ? avctx->priv_data : 
NULL;
+
+if (h && h->sps.data_size && h->pps.data_size) {
+int ps_count = 2;
+const uint8_t **ps_data = av_malloc(sizeof(uint8_t*) * ps_count);
+size_t *ps_sizes = av_malloc(sizeof(size_t)  * ps_count);
+
+ps_data[0]  = h->sps.data;
+ps_sizes[0] = h->sps.data_size;
+
+ps_data[1]  = h->pps.data;
+ps_sizes[1] = h->pps.data_size;
+
+status = CMVideoFormatDescriptionCreateFromH264ParameterSets(NULL,
+ ps_count,
+ ps_data,
+ ps_sizes,
+ 4,
+ 
&cm_fmt_desc);
+av_freep(&ps_sizes);
+av_freep(&ps_data);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error creating H.264 format 
description: %d\n", status);
+return NULL;
+}
+} else {
+status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
+codec_type,
+width,
+height,
+decoder_spec, // Dictionary of 
extension
+&cm_fmt_desc);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error creating format description: 
%d\n", status);
+return NULL;
+}
+}
 
 return cm_fmt_desc;
 }
@@ -543,7 +573,8 @@ static int videotoolbox_default_init(AVCodecContext *avctx)
 
 decoder_spec = 
videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
 
-videotoolbox->cm_fmt_desc = 
videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
+videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(avctx,
+
videotoolbox->cm_codec_type,
 decoder_spec,
 avctx->width,
 avctx->height);
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH] lavd/avfoundation: use AVCodecParameters

2016-05-05 Thread Rick Kern
Fixes "Could not find codec parameters for stream" error (#5494)

Signed-off-by: Rick Kern 
---
 libavdevice/avfoundation.m | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index 763e675..8132278 100644
--- a/libavdevice/avfoundation.m
+++ b/libavdevice/avfoundation.m
@@ -560,11 +560,11 @@ static int get_video_config(AVFormatContext *s)
 image_buffer  = CMSampleBufferGetImageBuffer(ctx->current_frame);
 image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
 
-stream->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
-stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-stream->codec->width  = (int)image_buffer_size.width;
-stream->codec->height = (int)image_buffer_size.height;
-stream->codec->pix_fmt= ctx->pixel_format;
+stream->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
+stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
+stream->codecpar->width  = (int)image_buffer_size.width;
+stream->codecpar->height = (int)image_buffer_size.height;
+stream->codecpar->format = ctx->pixel_format;
 
 CFRelease(ctx->current_frame);
 ctx->current_frame = nil;
@@ -603,10 +603,10 @@ static int get_audio_config(AVFormatContext *s)
 return 1;
 }
 
-stream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
-stream->codec->sample_rate= basic_desc->mSampleRate;
-stream->codec->channels   = basic_desc->mChannelsPerFrame;
-stream->codec->channel_layout = 
av_get_default_channel_layout(stream->codec->channels);
+stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+stream->codecpar->sample_rate= basic_desc->mSampleRate;
+stream->codecpar->channels   = basic_desc->mChannelsPerFrame;
+stream->codecpar->channel_layout = 
av_get_default_channel_layout(stream->codecpar->channels);
 
 ctx->audio_channels= basic_desc->mChannelsPerFrame;
 ctx->audio_bits_per_sample = basic_desc->mBitsPerChannel;
@@ -620,22 +620,22 @@ static int get_audio_config(AVFormatContext *s)
 ctx->audio_float &&
 ctx->audio_bits_per_sample == 32 &&
 ctx->audio_packed) {
-stream->codec->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_F32BE : 
AV_CODEC_ID_PCM_F32LE;
+stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_F32BE : 
AV_CODEC_ID_PCM_F32LE;
 } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
 ctx->audio_signed_integer &&
 ctx->audio_bits_per_sample == 16 &&
 ctx->audio_packed) {
-stream->codec->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S16BE : 
AV_CODEC_ID_PCM_S16LE;
+stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S16BE : 
AV_CODEC_ID_PCM_S16LE;
 } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
 ctx->audio_signed_integer &&
 ctx->audio_bits_per_sample == 24 &&
 ctx->audio_packed) {
-stream->codec->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S24BE : 
AV_CODEC_ID_PCM_S24LE;
+stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S24BE : 
AV_CODEC_ID_PCM_S24LE;
 } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
 ctx->audio_signed_integer &&
 ctx->audio_bits_per_sample == 32 &&
 ctx->audio_packed) {
-stream->codec->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S32BE : 
AV_CODEC_ID_PCM_S32LE;
+stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S32BE : 
AV_CODEC_ID_PCM_S32LE;
 } else {
 av_log(s, AV_LOG_ERROR, "audio format is not supported\n");
 return 1;
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 06/12] lavc/videotoolboxenc: Fix DTS

2016-04-27 Thread Rick Kern
Some devices output an invalid DTS when B-frames aren't used. Using PTS for
the DTS in this case.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 25c9648..72c7ba8 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -1034,6 +1034,15 @@ static int vtenc_cm_to_avpacket(
 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
 dts = CMSampleBufferGetDecodeTimeStamp  (sample_buffer);
 
+if (CMTIME_IS_INVALID(dts)) {
+if (!vtctx->has_b_frames) {
+dts = pts;
+} else {
+av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
+return AVERROR_EXTERNAL;
+}
+}
+
 dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
 time_base_num = avctx->time_base.num;
 pkt->pts = pts.value / time_base_num;
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 05/12] lavc/videotoolboxenc: Fix AVCodecContext.has_b_frames usage.

2016-04-27 Thread Rick Kern
Now set by the encoder, not used as an input parameter.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index bd153e7..25c9648 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -598,6 +598,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFStringRefprofile_level;
 SInt32 bit_rate = avctx->bit_rate;
 CFNumberRefbit_rate_num;
+CFBooleanRef   has_b_frames_cfbool;
 intstatus;
 
 codec_type = get_cm_codec_type(avctx->codec_id);
@@ -606,7 +607,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 return AVERROR(EINVAL);
 }
 
-vtctx->has_b_frames = avctx->has_b_frames || avctx->max_b_frames > 0;
+vtctx->has_b_frames = avctx->max_b_frames > 0;
 if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
 av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline 
profile. Output will not contain B-frames.\n");
 vtctx->has_b_frames = false;
@@ -751,6 +752,18 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
 vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
 
+status = VTSessionCopyProperty(vtctx->session,
+   
kVTCompressionPropertyKey_AllowFrameReordering,
+   kCFAllocatorDefault,
+   &has_b_frames_cfbool);
+
+if (!status) {
+//Some devices don't output B-frames for main profile, even if 
requested.
+vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
+CFRelease(has_b_frames_cfbool);
+}
+avctx->has_b_frames = vtctx->has_b_frames;
+
 return 0;
 }
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 08/12] lavc/videotoolboxenc: Add realtime encoding property

2016-04-27 Thread Rick Kern
Hint to the encoder that encoding should be done in real-time, even at the
expense of quality.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index f53015a..45a6a33 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -76,6 +76,7 @@ typedef struct VTEncContext {
 int64_t profile;
 int64_t level;
 int64_t entropy;
+int64_t realtime;
 
 int64_t allow_sw;
 
@@ -747,6 +748,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->realtime) {
+status = VTSessionSetProperty(vtctx->session,
+  kVTCompressionPropertyKey_RealTime,
+  kCFBooleanTrue);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting realtime property: 
%d\n", status);
+}
+}
+
 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", 
status);
@@ -1520,6 +1531,9 @@ static const AVOption options[] = {
 { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "coder" },
 { "ac","CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "coder" },
 
+{ "realtime", "Hint that encoding should happen in real-time if not faster 
(e.g. capturing from camera).",
+OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 02/12] lavc/videotoolboxenc: Handle hwaccel format as input

2016-04-27 Thread Rick Kern
Handle AV_PIX_FMT_VIDEOTOOLBOX.
This results in better energy usage and faster encoding, especially on iOS.
When the buffer comes from the media server, no memcpy's are needed.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 26 +-
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index ea8ff70..381bc12 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -621,10 +621,14 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,  
kCFBooleanTrue);
 #endif
 
-status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
-if (status) {
-CFRelease(enc_info);
-return status;
+if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
+status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
+if (status) {
+CFRelease(enc_info);
+return status;
+}
+} else {
+pixel_buffer_info = NULL;
 }
 
 status = VTCompressionSessionCreate(
@@ -659,7 +663,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 #endif
 
-CFRelease(pixel_buffer_info);
+if (pixel_buffer_info) CFRelease(pixel_buffer_info);
 CFRelease(enc_info);
 
 if (status || !vtctx->session) {
@@ -1227,6 +1231,17 @@ static int create_cv_pixel_buffer(AVCodecContext   
*avctx,
 CVPixelBufferPoolRef pix_buf_pool;
 VTEncContext* vtctx = avctx->priv_data;
 
+
+if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) {
+av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX);
+
+*cv_img = (CVPixelBufferRef)frame->data[3];
+av_assert0(*cv_img);
+
+CFRetain(*cv_img);
+return 0;
+}
+
 memset(widths,  0, sizeof(widths));
 memset(heights, 0, sizeof(heights));
 memset(strides, 0, sizeof(strides));
@@ -1423,6 +1438,7 @@ static av_cold int vtenc_close(AVCodecContext *avctx)
 }
 
 static const enum AVPixelFormat pix_fmts[] = {
+AV_PIX_FMT_VIDEOTOOLBOX,
 AV_PIX_FMT_NV12,
 AV_PIX_FMT_YUV420P,
 AV_PIX_FMT_NONE
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 07/12] lavc/videotoolboxenc: Handle out-of-memory and fix memory leak

2016-04-27 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 72c7ba8..f53015a 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -706,9 +706,14 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
   kCFNumberIntType,
   &avctx->gop_size);
+if (!interval) {
+return AVERROR(ENOMEM);
+}
+
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_MaxKeyFrameInterval,
   interval);
+CFRelease(interval);
 
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame 
interval' property: %d\n", status);
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 11/12] lavc/videotoolboxenc: Support pixel aspect ratio

2016-04-27 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 63 
 1 file changed, 63 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 5f02756..4a40f35 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -748,6 +748,69 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (avctx->sample_aspect_ratio.num != 0) {
+CFNumberRef num;
+CFNumberRef den;
+CFMutableDictionaryRef par;
+AVRational *avpar = &avctx->sample_aspect_ratio;
+
+av_reduce(&avpar->num, &avpar->den,
+   avpar->num,  avpar->den,
+  0x);
+
+num = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberIntType,
+ &avpar->num);
+
+den = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberIntType,
+ &avpar->den);
+
+
+
+par = CFDictionaryCreateMutable(kCFAllocatorDefault,
+2,
+&kCFCopyStringDictionaryKeyCallBacks,
+&kCFTypeDictionaryValueCallBacks);
+
+if (!par || !num || !den) {
+if (par) CFRelease(par);
+if (num) CFRelease(num);
+if (den) CFRelease(den);
+
+return AVERROR(ENOMEM);
+}
+
+CFDictionarySetValue(
+par,
+kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
+num);
+
+CFDictionarySetValue(
+par,
+kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
+den);
+
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_PixelAspectRatio,
+  par);
+
+CFRelease(par);
+CFRelease(num);
+CFRelease(den);
+
+if (status) {
+av_log(avctx,
+   AV_LOG_ERROR,
+   "Error setting pixel aspect ratio to %d:%d: %d.\n",
+   avctx->sample_aspect_ratio.num,
+   avctx->sample_aspect_ratio.den,
+   status);
+
+return AVERROR_EXTERNAL;
+}
+}
+
 if (!vtctx->has_b_frames) {
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_AllowFrameReordering,
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 12/12] lavc/videotoolboxenc: Set colorimetry values

2016-04-27 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 configure|   5 +-
 libavcodec/videotoolboxenc.c | 209 +++
 2 files changed, 213 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index ef0b689..43d21fa 100755
--- a/configure
+++ b/configure
@@ -2078,6 +2078,7 @@ CONFIG_EXTRA="
 vp3dsp
 vp56dsp
 vp8dsp
+vt_bt2020
 wma_freqs
 wmv2dsp
 "
@@ -2740,7 +2741,8 @@ nvenc_hevc_encoder_deps="nvenc"
 videotoolbox_deps="VideoToolbox_VideoToolbox_h"
 videotoolbox_extralibs="-framework CoreFoundation -framework VideoToolbox 
-framework CoreMedia -framework CoreVideo"
 videotoolbox_encoder_deps="videotoolbox 
VTCompressionSessionPrepareToEncodeFrames"
-videotoolbox_encoder_suggest="vda_framework"
+videotoolbox_encoder_suggest="vda_framework vt_bt2020"
+vt_bt2020_deps="kCVImageBufferColorPrimaries_ITU_R_2020"
 
 # demuxers / muxers
 ac3_demuxer_select="ac3_parser"
@@ -5442,6 +5444,7 @@ check_header vdpau/vdpau_x11.h
 check_header VideoDecodeAcceleration/VDADecoder.h
 check_header VideoToolbox/VideoToolbox.h
 check_func_headers VideoToolbox/VTCompressionSession.h 
VTCompressionSessionPrepareToEncodeFrames -framework VideoToolbox
+enabled videotoolbox && check_func_headers CoreVideo/CVImageBuffer.h 
kCVImageBufferColorPrimaries_ITU_R_2020 -framework CoreVideo
 check_header windows.h
 check_header X11/extensions/XvMClib.h
 check_header asm/types.h
diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 4a40f35..57ea2b3 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -33,6 +33,12 @@
 #include "internal.h"
 #include 
 
+#if !CONFIG_VT_BT2020
+# define kCVImageBufferColorPrimaries_ITU_R_2020   CFSTR("ITU_R_2020")
+# define kCVImageBufferTransferFunction_ITU_R_2020 CFSTR("ITU_R_2020")
+# define kCVImageBufferYCbCrMatrix_ITU_R_2020  CFSTR("ITU_R_2020")
+#endif
+
 typedef enum VT_H264Profile {
 H264_PROF_AUTO,
 H264_PROF_BASELINE,
@@ -58,6 +64,9 @@ typedef struct BufNode {
 typedef struct VTEncContext {
 AVClass *class;
 VTCompressionSessionRef session;
+CFStringRef ycbcr_matrix;
+CFStringRef color_primaries;
+CFStringRef transfer_function;
 
 pthread_mutex_t lock;
 pthread_cond_t  cv_sample_sent;
@@ -527,6 +536,28 @@ static int get_cv_pixel_format(AVCodecContext* avctx,
 return 0;
 }
 
+static void add_color_attr(AVCodecContext *avctx, CFMutableDictionaryRef dict) 
{
+VTEncContext *vtctx = avctx->priv_data;
+
+if (vtctx->color_primaries) {
+CFDictionarySetValue(dict,
+ kCVImageBufferColorPrimariesKey,
+ vtctx->color_primaries);
+}
+
+if (vtctx->transfer_function) {
+CFDictionarySetValue(dict,
+ kCVImageBufferTransferFunctionKey,
+ vtctx->transfer_function);
+}
+
+if (vtctx->ycbcr_matrix) {
+CFDictionarySetValue(dict,
+ kCVImageBufferYCbCrMatrixKey,
+ vtctx->ycbcr_matrix);
+}
+}
+
 static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
CFMutableDictionaryRef* dict)
 {
@@ -580,6 +611,8 @@ static int create_cv_pixel_buffer_info(AVCodecContext* 
avctx,
  height_num);
 vt_release_num(&height_num);
 
+add_color_attr(avctx, pixel_buffer_info);
+
 *dict = pixel_buffer_info;
 return 0;
 
@@ -592,6 +625,110 @@ pbinfo_nomem:
 return AVERROR(ENOMEM);
 }
 
+static int get_cv_color_primaries(AVCodecContext *avctx,
+  CFStringRef *primaries)
+{
+enum AVColorPrimaries pri = avctx->color_primaries;
+switch (pri) {
+case AVCOL_PRI_UNSPECIFIED:
+*primaries = NULL;
+break;
+
+case AVCOL_PRI_BT709:
+*primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
+break;
+
+case AVCOL_PRI_BT2020:
+*primaries = kCVImageBufferColorPrimaries_ITU_R_2020;
+break;
+
+default:
+av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not 
supported.\n", av_color_primaries_name(pri));
+*primaries = NULL;
+return -1;
+}
+
+return 0;
+}
+
+static int get_cv_transfer_function(AVCodecContext *avctx,
+CFStringRef *transfer_fnc,
+CFNumberRef *gamma_level)
+{
+enum AVColorTransferCharacteristic trc = avctx->color_trc;
+Float32 gamma;
+*gamma_level = NULL;
+
+switch (trc) {
+case AVCOL_TRC_UNSPECIFIED:
+*transfer_fnc = NULL;
+break;
+
+case AVCOL_TRC_BT709:
+*transfer_fnc = kCVImageBufferTra

[FFmpeg-devel] [PATCH v3 10/12] lavc/videotoolboxenc: Support for forced I-frames

2016-04-27 Thread Rick Kern
Setting AVFrame.pic_type to AV_PICTURE_TYPE_I will force an I-frame.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 26 +-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index e155383..5f02756 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -1414,27 +1414,51 @@ static int create_cv_pixel_buffer(AVCodecContext   
*avctx,
 return 0;
 }
 
+static int create_encoder_dict_h264(const AVFrame *frame,
+CFDictionaryRef* dict_out)
+{
+CFDictionaryRef dict = NULL;
+if (frame->pict_type == AV_PICTURE_TYPE_I) {
+const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
+const void *vals[] = { kCFBooleanTrue };
+
+dict = CFDictionaryCreate(NULL, keys, vals, 1, NULL, NULL);
+if(!dict) return AVERROR(ENOMEM);
+}
+
+*dict_out = dict;
+return 0;
+}
+
 static int vtenc_send_frame(AVCodecContext *avctx,
 VTEncContext   *vtctx,
 const AVFrame  *frame)
 {
 CMTime time;
+CFDictionaryRef frame_dict;
 CVPixelBufferRef cv_img = NULL;
 int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
 
 if (status) return status;
 
+status = create_encoder_dict_h264(frame, &frame_dict);
+if (status) {
+CFRelease(cv_img);
+return status;
+}
+
 time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
 status = VTCompressionSessionEncodeFrame(
 vtctx->session,
 cv_img,
 time,
 kCMTimeInvalid,
-NULL,
+frame_dict,
 NULL,
 NULL
 );
 
+if (frame_dict) CFRelease(frame_dict);
 CFRelease(cv_img);
 
 if (status) {
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 04/12] lavc/videotoolboxenc: Add entropy setting

2016-04-27 Thread Rick Kern
Add an entropy setting to choose between CAVLC and CABAC.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 33 +
 1 file changed, 33 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 90b9a96..bd153e7 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -41,6 +41,12 @@ typedef enum VT_H264Profile {
 H264_PROF_COUNT
 } VT_H264Profile;
 
+typedef enum VTH264Entropy{
+VT_ENTROPY_NOT_SET,
+VT_CAVLC,
+VT_CABAC
+} VTH264Entropy;
+
 static const uint8_t start_code[] = { 0, 0, 0, 1 };
 
 typedef struct BufNode {
@@ -69,6 +75,7 @@ typedef struct VTEncContext {
 
 int64_t profile;
 int64_t level;
+int64_t entropy;
 
 int64_t allow_sw;
 
@@ -605,6 +612,11 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 vtctx->has_b_frames = false;
 }
 
+if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
+av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' 
profile, but baseline was requested. Encode will not use CABAC entropy.\n");
+vtctx->entropy = VT_ENTROPY_NOT_SET;
+}
+
 if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
 
 vtctx->session = NULL;
@@ -714,6 +726,21 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->entropy != VT_ENTROPY_NOT_SET) {
+CFStringRef entropy = vtctx->entropy == VT_CABAC ?
+kVTH264EntropyMode_CABAC:
+kVTH264EntropyMode_CAVLC;
+
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_H264EntropyMode,
+  entropy);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: 
%d\n", status);
+return AVERROR_EXTERNAL;
+}
+}
+
 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", 
status);
@@ -1460,6 +1487,12 @@ static const AVOption options[] = {
 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), 
AV_OPT_TYPE_BOOL,
 { .i64 = 0 }, 0, 1, VE },
 
+{ "coder", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = 
VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "coder" },
+{ "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC 
}, INT_MIN, INT_MAX, VE, "coder" },
+{ "vlc",   "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC 
}, INT_MIN, INT_MAX, VE, "coder" },
+{ "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "coder" },
+{ "ac","CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "coder" },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 09/12] lavc/videotoolboxenc: add concatentation properties

2016-04-27 Thread Rick Kern
Add frames_before and frames_after as hints that there will be frames before
or after the frames produced in this session. This may help with
concatenation issues like bit rate spikes.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 31 +++
 1 file changed, 31 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 45a6a33..e155383 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -77,6 +77,8 @@ typedef struct VTEncContext {
 int64_t level;
 int64_t entropy;
 int64_t realtime;
+int64_t frames_before;
+int64_t frames_after;
 
 int64_t allow_sw;
 
@@ -722,6 +724,30 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->frames_before) {
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_MoreFramesBeforeStart,
+  kCFBooleanTrue);
+
+if (status == kVTPropertyNotSupportedErr) {
+av_log(avctx, AV_LOG_WARNING, "frames_before property is not 
supported on this device. Ignoring.\n");
+} else if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting frames_before property: 
%d\n", status);
+}
+}
+
+if (vtctx->frames_after) {
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_MoreFramesAfterEnd,
+  kCFBooleanTrue);
+
+if (status == kVTPropertyNotSupportedErr) {
+av_log(avctx, AV_LOG_WARNING, "frames_after property is not 
supported on this device. Ignoring.\n");
+} else if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting frames_after property: 
%d\n", status);
+}
+}
+
 if (!vtctx->has_b_frames) {
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_AllowFrameReordering,
@@ -1534,6 +1560,11 @@ static const AVOption options[] = {
 { "realtime", "Hint that encoding should happen in real-time if not faster 
(e.g. capturing from camera).",
 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
 
+{ "frames_before", "Other frames will come before the frames in this 
session. This helps smooth concatenation issues.",
+OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
+{ "frames_after", "Other frames will come after the frames in this 
session. This helps smooth concatenation issues.",
+OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 01/12] lavc/videotoolboxenc: Use shared pixel buffer pool

2016-04-27 Thread Rick Kern
This reduces the chance of a memcpy in the media server.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 204 +--
 1 file changed, 160 insertions(+), 44 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 3177074..ea8ff70 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -29,6 +29,7 @@
 #include "libavutil/atomic.h"
 #include "libavutil/avstring.h"
 #include "libavcodec/avcodec.h"
+#include "libavutil/pixdesc.h"
 #include "internal.h"
 #include 
 
@@ -74,6 +75,18 @@ typedef struct VTEncContext {
 bool warned_color_range;
 } VTEncContext;
 
+/**
+ * NULL-safe release of *refPtr, and sets value to NULL.
+ */
+static void vt_release_num(CFNumberRef* refPtr){
+if (!*refPtr) {
+return;
+}
+
+CFRelease(*refPtr);
+*refPtr = NULL;
+}
+
 static void set_async_error(VTEncContext *vtctx, int err)
 {
 BufNode *info;
@@ -477,9 +490,100 @@ static bool get_vt_profile_level(AVCodecContext *avctx,
 return true;
 }
 
+static int get_cv_pixel_format(AVCodecContext* avctx,
+   enum AVPixelFormat fmt,
+   enum AVColorRange range,
+   int* av_pixel_format,
+   int* range_guessed)
+{
+if (range_guessed) *range_guessed = range != AVCOL_RANGE_MPEG &&
+range != AVCOL_RANGE_JPEG;
+
+//MPEG range is used when no range is set
+if (fmt == AV_PIX_FMT_NV12) {
+*av_pixel_format = range == AVCOL_RANGE_JPEG ?
+
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
+
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
+} else if (fmt == AV_PIX_FMT_YUV420P) {
+*av_pixel_format = range == AVCOL_RANGE_JPEG ?
+
kCVPixelFormatType_420YpCbCr8PlanarFullRange :
+kCVPixelFormatType_420YpCbCr8Planar;
+} else {
+return AVERROR(EINVAL);
+}
+
+return 0;
+}
+
+static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
+   CFMutableDictionaryRef* dict)
+{
+CFNumberRef cv_color_format_num = NULL;
+CFNumberRef width_num = NULL;
+CFNumberRef height_num = NULL;
+CFMutableDictionaryRef pixel_buffer_info = NULL;
+int cv_color_format;
+int status = get_cv_pixel_format(avctx,
+ avctx->pix_fmt,
+ avctx->color_range,
+ &cv_color_format,
+ NULL);
+if (status) return status;
+
+pixel_buffer_info = CFDictionaryCreateMutable(
+kCFAllocatorDefault,
+20,
+&kCFCopyStringDictionaryKeyCallBacks,
+&kCFTypeDictionaryValueCallBacks);
+
+if (!pixel_buffer_info) goto pbinfo_nomem;
+
+cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberSInt32Type,
+ &cv_color_format);
+if (!cv_color_format_num) goto pbinfo_nomem;
+
+CFDictionarySetValue(pixel_buffer_info,
+ kCVPixelBufferPixelFormatTypeKey,
+ cv_color_format_num);
+vt_release_num(&cv_color_format_num);
+
+width_num = CFNumberCreate(kCFAllocatorDefault,
+   kCFNumberSInt32Type,
+   &avctx->width);
+if (!width_num) return AVERROR(ENOMEM);
+
+CFDictionarySetValue(pixel_buffer_info,
+ kCVPixelBufferWidthKey,
+ width_num);
+vt_release_num(&width_num);
+
+height_num = CFNumberCreate(kCFAllocatorDefault,
+kCFNumberSInt32Type,
+&avctx->height);
+if (!height_num) goto pbinfo_nomem;
+
+CFDictionarySetValue(pixel_buffer_info,
+ kCVPixelBufferHeightKey,
+ height_num);
+vt_release_num(&height_num);
+
+*dict = pixel_buffer_info;
+return 0;
+
+pbinfo_nomem:
+vt_release_num(&cv_color_format_num);
+vt_release_num(&width_num);
+vt_release_num(&height_num);
+if (pixel_buffer_info) CFRelease(pixel_buffer_info);
+
+return AVERROR(ENOMEM);
+}
+
 static av_cold int vtenc_init(AVCodecContext *avctx)
 {
 CFMutableDictionaryRef enc_info;
+CFMutableDictionaryRef pixel_buffer_info;
 CMVideoCodecType   codec_type;
 VTEncContext   *vtctx = avctx->priv_data;
 CFStringRefpro

[FFmpeg-devel] [PATCH v3 03/12] lavc/videotoolboxenc: Require hardware encoding

2016-04-27 Thread Rick Kern
Software encoding can be allowed by setting allow_sw to 1.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 38 +-
 1 file changed, 17 insertions(+), 21 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 381bc12..90b9a96 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -70,6 +70,8 @@ typedef struct VTEncContext {
 int64_t profile;
 int64_t level;
 
+int64_t allow_sw;
+
 bool flushing;
 bool has_b_frames;
 bool warned_color_range;
@@ -617,8 +619,11 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 if (!enc_info) return AVERROR(ENOMEM);
 
 #if !TARGET_OS_IPHONE
-CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
-CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,  
kCFBooleanTrue);
+if (!vtctx->allow_sw) {
+CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
+} else {
+CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,  
kCFBooleanTrue);
+}
 #endif
 
 if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
@@ -644,30 +649,18 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 &vtctx->session
 );
 
-#if !TARGET_OS_IPHONE
-if (status != 0 || !vtctx->session) {
-CFDictionaryRemoveValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder);
-
-status = VTCompressionSessionCreate(
-kCFAllocatorDefault,
-avctx->width,
-avctx->height,
-codec_type,
-enc_info,
-NULL,
-kCFAllocatorDefault,
-vtenc_output_callback,
-avctx,
-&vtctx->session
-);
-}
-#endif
-
 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
 CFRelease(enc_info);
 
 if (status || !vtctx->session) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: 
%d\n", status);
+
+#if !TARGET_OS_IPHONE
+if (!vtctx->allow_sw) {
+av_log(avctx, AV_LOG_ERROR, "Try -allow_sw 1. The hardware encoder 
may be busy, or not supported.\n");
+}
+#endif
+
 return AVERROR_EXTERNAL;
 }
 
@@ -1464,6 +1457,9 @@ static const AVOption options[] = {
 { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, 
INT_MAX, VE, "level" },
 { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, 
INT_MAX, VE, "level" },
 
+{ "allow_sw", "Allow software encoding", OFFSET(allow_sw), 
AV_OPT_TYPE_BOOL,
+{ .i64 = 0 }, 0, 1, VE },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v3 00/12] videotoolbox encoder features and fixes

2016-04-27 Thread Rick Kern
Updates from code review:
Have allow_sw for both OS X and iOS
Changed "entropy" option to "coder" to match libx264
Updated configure for CONFIG_VT_BT2020 macro
Bool option usage
Formatting


Rick Kern (12):
  lavc/videotoolboxenc: Use shared pixel buffer pool
  lavc/videotoolboxenc: Handle hwaccel format as input
  lavc/videotoolboxenc: Require hardware encoding
  lavc/videotoolboxenc: Add entropy setting
  lavc/videotoolboxenc: Fix AVCodecContext.has_b_frames usage.
  lavc/videotoolboxenc: Fix DTS
  lavc/videotoolboxenc: Handle out-of-memory and fix memory leak
  lavc/videotoolboxenc: Add realtime encoding property
  lavc/videotoolboxenc: add concatentation properties
  lavc/videotoolboxenc: Support for forced I-frames
  lavc/videotoolboxenc: Support pixel aspect ratio
  lavc/videotoolboxenc: Set colorimetry values

 configure|   5 +-
 libavcodec/videotoolboxenc.c | 663 ++-
 2 files changed, 600 insertions(+), 68 deletions(-)

-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 06/12] lavc/videotoolboxenc: Fix DTS

2016-04-22 Thread Rick Kern
Some devices output an invalid DTS when B-frames aren't used. Using PTS for
the DTS in this case.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 6a9a2fe..5594e69 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -1056,6 +1056,15 @@ static int vtenc_cm_to_avpacket(
 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
 dts = CMSampleBufferGetDecodeTimeStamp  (sample_buffer);
 
+if (CMTIME_IS_INVALID(dts)) {
+if (!vtctx->has_b_frames) {
+dts = pts;
+} else {
+av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
+return AVERROR_EXTERNAL;
+}
+}
+
 dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
 time_base_num = avctx->time_base.num;
 pkt->pts = pts.value / time_base_num;
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 12/12] lavc/videotoolboxenc: Set colorimetry values

2016-04-22 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 configure|   5 +-
 libavcodec/videotoolboxenc.c | 209 +++
 2 files changed, 213 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index 97f374b..c484a10 100755
--- a/configure
+++ b/configure
@@ -2078,6 +2078,7 @@ CONFIG_EXTRA="
 vp3dsp
 vp56dsp
 vp8dsp
+vt_bt2020
 wma_freqs
 wmv2dsp
 "
@@ -2740,6 +2741,7 @@ videotoolbox_deps="VideoToolbox_VideoToolbox_h"
 videotoolbox_extralibs="-framework CoreFoundation -framework VideoToolbox 
-framework CoreMedia -framework CoreVideo"
 videotoolbox_encoder_deps="videotoolbox 
VTCompressionSessionPrepareToEncodeFrames"
 videotoolbox_encoder_suggest="vda_framework"
+vt_bt2020_deps="kCVImageBufferColorPrimaries_ITU_R_2020"
 
 # demuxers / muxers
 ac3_demuxer_select="ac3_parser"
@@ -3128,7 +3130,7 @@ enable audiotoolbox
 enable d3d11va dxva2 vaapi vda vdpau videotoolbox_hwaccel xvmc
 enable xlib
 
-enable vda_framework videotoolbox videotoolbox_encoder
+enable vda_framework videotoolbox videotoolbox_encoder vt_bt2020
 
 # build settings
 SHFLAGS='-shared -Wl,-soname,$$(@F)'
@@ -5441,6 +5443,7 @@ check_header vdpau/vdpau_x11.h
 check_header VideoDecodeAcceleration/VDADecoder.h
 check_header VideoToolbox/VideoToolbox.h
 check_func_headers VideoToolbox/VTCompressionSession.h 
VTCompressionSessionPrepareToEncodeFrames -framework VideoToolbox
+check_func_headers CoreVideo/CVImageBuffer.h 
kCVImageBufferColorPrimaries_ITU_R_2020 -framework CoreVideo
 check_header windows.h
 check_header X11/extensions/XvMClib.h
 check_header asm/types.h
diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 768405d..1201004 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -33,6 +33,12 @@
 #include "internal.h"
 #include 
 
+#if !CONFIG_VT_BT2020
+# define kCVImageBufferColorPrimaries_ITU_R_2020   CFSTR("ITU_R_2020")
+# define kCVImageBufferTransferFunction_ITU_R_2020 CFSTR("ITU_R_2020")
+# define kCVImageBufferYCbCrMatrix_ITU_R_2020  CFSTR("ITU_R_2020")
+#endif
+
 typedef enum VT_H264Profile {
 H264_PROF_AUTO,
 H264_PROF_BASELINE,
@@ -58,6 +64,9 @@ typedef struct BufNode {
 typedef struct VTEncContext {
 AVClass *class;
 VTCompressionSessionRef session;
+CFStringRef ycbcr_matrix;
+CFStringRef color_primaries;
+CFStringRef transfer_function;
 
 pthread_mutex_t lock;
 pthread_cond_t  cv_sample_sent;
@@ -549,6 +558,28 @@ static int get_cv_pixel_format(
 return 0;
 }
 
+static void add_color_attr(AVCodecContext *avctx, CFMutableDictionaryRef dict) 
{
+VTEncContext *vtctx = avctx->priv_data;
+
+if (vtctx->color_primaries) {
+CFDictionarySetValue(dict,
+ kCVImageBufferColorPrimariesKey,
+ vtctx->color_primaries);
+}
+
+if (vtctx->transfer_function) {
+CFDictionarySetValue(dict,
+ kCVImageBufferTransferFunctionKey,
+ vtctx->transfer_function);
+}
+
+if (vtctx->ycbcr_matrix) {
+CFDictionarySetValue(dict,
+ kCVImageBufferYCbCrMatrixKey,
+ vtctx->ycbcr_matrix);
+}
+}
+
 static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
CFMutableDictionaryRef* dict)
 {
@@ -602,6 +633,8 @@ static int create_cv_pixel_buffer_info(AVCodecContext* 
avctx,
  height_num);
 vt_release_num(&height_num);
 
+add_color_attr(avctx, pixel_buffer_info);
+
 *dict = pixel_buffer_info;
 return 0;
 
@@ -614,6 +647,110 @@ pbinfo_nomem:
 return AVERROR(ENOMEM);
 }
 
+static int get_cv_color_primaries(AVCodecContext *avctx,
+  CFStringRef *primaries)
+{
+enum AVColorPrimaries pri = avctx->color_primaries;
+switch (pri) {
+case AVCOL_PRI_UNSPECIFIED:
+*primaries = NULL;
+break;
+
+case AVCOL_PRI_BT709:
+*primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
+break;
+
+case AVCOL_PRI_BT2020:
+*primaries = kCVImageBufferColorPrimaries_ITU_R_2020;
+break;
+
+default:
+av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not 
supported.\n", av_color_primaries_name(pri));
+*primaries = NULL;
+return -1;
+}
+
+return 0;
+}
+
+static int get_cv_transfer_function(AVCodecContext *avctx,
+CFStringRef *transfer_fnc,
+CFNumberRef *gamma_level)
+{
+enum AVColorTransferCharacteristic trc = avctx->color_trc;
+Float32 gamma;
+*gamma_level = NULL;
+
+switch (trc) {
+case AVCOL_TRC_UNSPECIFIED

[FFmpeg-devel] [PATCH v2 11/12] lavc/videotoolboxenc: Support pixel aspect ratio

2016-04-22 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 63 
 1 file changed, 63 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 5935d09..768405d 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -768,6 +768,69 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (avctx->sample_aspect_ratio.num != 0) {
+CFNumberRef num;
+CFNumberRef den;
+CFMutableDictionaryRef par;
+AVRational *avpar = &avctx->sample_aspect_ratio;
+
+av_reduce(&avpar->num, &avpar->den,
+   avpar->num,  avpar->den,
+  0x);
+
+num = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberIntType,
+ &avpar->num);
+
+den = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberIntType,
+ &avpar->den);
+
+
+
+par = CFDictionaryCreateMutable(kCFAllocatorDefault,
+2,
+&kCFCopyStringDictionaryKeyCallBacks,
+&kCFTypeDictionaryValueCallBacks);
+
+if (!par || !num || !den) {
+if (par) CFRelease(par);
+if (num) CFRelease(num);
+if (den) CFRelease(den);
+
+return AVERROR(ENOMEM);
+}
+
+CFDictionarySetValue(
+par,
+kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
+num);
+
+CFDictionarySetValue(
+par,
+kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
+den);
+
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_PixelAspectRatio,
+  par);
+
+CFRelease(par);
+CFRelease(num);
+CFRelease(den);
+
+if (status) {
+av_log(avctx,
+   AV_LOG_ERROR,
+   "Error setting pixel aspect ratio to %d:%d: %d.\n",
+   avctx->sample_aspect_ratio.num,
+   avctx->sample_aspect_ratio.den,
+   status);
+
+return AVERROR_EXTERNAL;
+}
+}
+
 if (!vtctx->has_b_frames) {
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_AllowFrameReordering,
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 07/12] lavc/videotoolboxenc: Handle out-of-memory and fix memory leak

2016-04-22 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 5594e69..073b87a 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -728,9 +728,12 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
   kCFNumberIntType,
   &avctx->gop_size);
+if (!interval) return AVERROR(ENOMEM);
+
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_MaxKeyFrameInterval,
   interval);
+CFRelease(interval);
 
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame 
interval' property: %d\n", status);
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 05/12] lavc/videotoolboxenc: Fix AVCodecContext.has_b_frames usage.

2016-04-22 Thread Rick Kern
Now set by the encoder, not used as an input parameter.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 07577b6..6a9a2fe 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -620,6 +620,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFStringRefprofile_level;
 SInt32 bit_rate = avctx->bit_rate;
 CFNumberRefbit_rate_num;
+CFBooleanRef   has_b_frames_cfbool;
 intstatus;
 
 codec_type = get_cm_codec_type(avctx->codec_id);
@@ -628,7 +629,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 return AVERROR(EINVAL);
 }
 
-vtctx->has_b_frames = avctx->has_b_frames || avctx->max_b_frames > 0;
+vtctx->has_b_frames = avctx->max_b_frames > 0;
 if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
 av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline 
profile. Output will not contain B-frames.\n");
 vtctx->has_b_frames = false;
@@ -773,6 +774,18 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
 vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
 
+status = VTSessionCopyProperty(vtctx->session,
+   
kVTCompressionPropertyKey_AllowFrameReordering,
+   kCFAllocatorDefault,
+   &has_b_frames_cfbool);
+
+if (!status) {
+//Some devices don't output B-frames for main profile, even if 
requested.
+vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
+CFRelease(has_b_frames_cfbool);
+}
+avctx->has_b_frames = vtctx->has_b_frames;
+
 return 0;
 }
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 10/12] lavc/videotoolboxenc: Support for forced I-frames

2016-04-22 Thread Rick Kern
Setting AVFrame.pic_type to AV_PICTURE_TYPE_I will force an I-frame.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 26 +-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index c630825..5935d09 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -1434,27 +1434,51 @@ static int create_cv_pixel_buffer(AVCodecContext   
*avctx,
 return 0;
 }
 
+static int create_encoder_dict_h264(const AVFrame *frame,
+CFDictionaryRef* dict_out)
+{
+CFDictionaryRef dict = NULL;
+if (frame->pict_type == AV_PICTURE_TYPE_I) {
+const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
+const void *vals[] = { kCFBooleanTrue };
+
+dict = CFDictionaryCreate(NULL, keys, vals, 1, NULL, NULL);
+if(!dict) return AVERROR(ENOMEM);
+}
+
+*dict_out = dict;
+return 0;
+}
+
 static int vtenc_send_frame(AVCodecContext *avctx,
 VTEncContext   *vtctx,
 const AVFrame  *frame)
 {
 CMTime time;
+CFDictionaryRef frame_dict;
 CVPixelBufferRef cv_img = NULL;
 int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
 
 if (status) return status;
 
+status = create_encoder_dict_h264(frame, &frame_dict);
+if (status) {
+CFRelease(cv_img);
+return status;
+}
+
 time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
 status = VTCompressionSessionEncodeFrame(
 vtctx->session,
 cv_img,
 time,
 kCMTimeInvalid,
-NULL,
+frame_dict,
 NULL,
 NULL
 );
 
+if (frame_dict) CFRelease(frame_dict);
 CFRelease(cv_img);
 
 if (status) {
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 09/12] lavc/videotoolboxenc: add concatentation properties

2016-04-22 Thread Rick Kern
Add frames_before and frames_after as hints that there will be frames before
or after the frames produced in this session. This may help with
concatenation issues like bit rate spikes.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 30 ++
 1 file changed, 30 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index a30a3ca..c630825 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -77,6 +77,8 @@ typedef struct VTEncContext {
 int64_t level;
 int64_t entropy;
 int64_t realtime;
+int64_t frames_before;
+int64_t frames_after;
 
 #if !TARGET_OS_IPHONE
 int64_t allow_sw;
@@ -742,6 +744,30 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->frames_before == 1) {
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_MoreFramesBeforeStart,
+  kCFBooleanTrue);
+
+if (status == kVTPropertyNotSupportedErr) {
+av_log(avctx, AV_LOG_WARNING, "frames_before property is not 
supported on this device. Ignoring.\n");
+} else if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting frames_before property: 
%d\n", status);
+}
+}
+
+if (vtctx->frames_after == 1) {
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_MoreFramesAfterEnd,
+  kCFBooleanTrue);
+
+if (status == kVTPropertyNotSupportedErr) {
+av_log(avctx, AV_LOG_WARNING, "frames_after property is not 
supported on this device. Ignoring.\n");
+} else if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting frames_after property: 
%d\n", status);
+}
+}
+
 if (!vtctx->has_b_frames) {
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_AllowFrameReordering,
@@ -1553,6 +1579,10 @@ static const AVOption options[] = {
 
 { "realtime", "Hint that encoding should happen in real-time if not faster 
(e.g. capturing from camera).",
 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
+{ "frames_before", "Other frames will come before the frames in this 
session. This helps smooth concatenation issues.",
+OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
+{ "frames_after", "Other frames will come after the frames in this 
session. This helps smooth concatenation issues.",
+OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
 
 { NULL },
 };
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 08/12] lavc/videotoolboxenc: Add realtime encoding property

2016-04-22 Thread Rick Kern
Hint to the encoder that encoding should be done in real-time, even at the
expense of quality.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 073b87a..a30a3ca 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -76,6 +76,7 @@ typedef struct VTEncContext {
 int64_t profile;
 int64_t level;
 int64_t entropy;
+int64_t realtime;
 
 #if !TARGET_OS_IPHONE
 int64_t allow_sw;
@@ -767,6 +768,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->realtime) {
+status = VTSessionSetProperty(vtctx->session,
+  kVTCompressionPropertyKey_RealTime,
+  kCFBooleanTrue);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting realtime property: 
%d\n", status);
+}
+}
+
 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", 
status);
@@ -1540,6 +1551,9 @@ static const AVOption options[] = {
 { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC 
}, INT_MIN, INT_MAX, VE, "entropy" },
 { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "entropy" },
 
+{ "realtime", "Hint that encoding should happen in real-time if not faster 
(e.g. capturing from camera).",
+OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 04/12] lavc/videotoolboxenc: Add entropy setting

2016-04-22 Thread Rick Kern
Add an entropy setting to choose between CAVLC and CABAC.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 31 +++
 1 file changed, 31 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index ec69bb1..07577b6 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -41,6 +41,12 @@ typedef enum VT_H264Profile {
 H264_PROF_COUNT
 } VT_H264Profile;
 
+typedef enum VTH264Entropy{
+VT_ENTROPY_NOT_SET,
+VT_CAVLC,
+VT_CABAC
+} VTH264Entropy;
+
 static const uint8_t start_code[] = { 0, 0, 0, 1 };
 
 typedef struct BufNode {
@@ -69,6 +75,7 @@ typedef struct VTEncContext {
 
 int64_t profile;
 int64_t level;
+int64_t entropy;
 
 #if !TARGET_OS_IPHONE
 int64_t allow_sw;
@@ -627,6 +634,11 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 vtctx->has_b_frames = false;
 }
 
+if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
+av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' 
profile, but baseline was requested. Encode will not use CABAC entropy.\n");
+vtctx->entropy = VT_ENTROPY_NOT_SET;
+}
+
 if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
 
 vtctx->session = NULL;
@@ -736,6 +748,21 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->entropy != VT_ENTROPY_NOT_SET) {
+CFStringRef entropy = vtctx->entropy == VT_CABAC ?
+kVTH264EntropyMode_CABAC:
+kVTH264EntropyMode_CAVLC;
+
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_H264EntropyMode,
+  entropy);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: 
%d\n", status);
+return AVERROR_EXTERNAL;
+}
+}
+
 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", 
status);
@@ -1484,6 +1511,10 @@ static const AVOption options[] = {
 { .i64 = -1 }, -1, 1, VE },
 #endif
 
+{ "entropy", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = 
VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "entropy" },
+{ "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC 
}, INT_MIN, INT_MAX, VE, "entropy" },
+{ "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "entropy" },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 02/12] lavc/videotoolboxenc: Handle hwaccel format as input

2016-04-22 Thread Rick Kern
Handle AV_PIX_FMT_VIDEOTOOLBOX.
This results in better energy usage and faster encoding, especially on iOS.
When the buffer comes from the media server, no memcpy's are needed.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 26 +-
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index b578260..7b52073 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -641,10 +641,14 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,  
kCFBooleanTrue);
 #endif
 
-status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
-if (status) {
-CFRelease(enc_info);
-return status;
+if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
+status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
+if (status) {
+CFRelease(enc_info);
+return status;
+}
+} else {
+pixel_buffer_info = NULL;
 }
 
 status = VTCompressionSessionCreate(
@@ -679,7 +683,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 #endif
 
-CFRelease(pixel_buffer_info);
+if (pixel_buffer_info) CFRelease(pixel_buffer_info);
 CFRelease(enc_info);
 
 if (status || !vtctx->session) {
@@ -1247,6 +1251,17 @@ static int create_cv_pixel_buffer(AVCodecContext   
*avctx,
 CVPixelBufferPoolRef pix_buf_pool;
 VTEncContext* vtctx = avctx->priv_data;
 
+
+if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) {
+av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX);
+
+*cv_img = (CVPixelBufferRef)frame->data[3];
+av_assert0(*cv_img);
+
+CFRetain(*cv_img);
+return 0;
+}
+
 memset(widths,  0, sizeof(widths));
 memset(heights, 0, sizeof(heights));
 memset(strides, 0, sizeof(strides));
@@ -1443,6 +1458,7 @@ static av_cold int vtenc_close(AVCodecContext *avctx)
 }
 
 static const enum AVPixelFormat pix_fmts[] = {
+AV_PIX_FMT_VIDEOTOOLBOX,
 AV_PIX_FMT_NV12,
 AV_PIX_FMT_YUV420P,
 AV_PIX_FMT_NONE
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 03/12] lavc/videotoolboxenc: Require hardware encoding

2016-04-22 Thread Rick Kern
Software encoding can be allowed by setting allow_sw to 1.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 42 +-
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 7b52073..ec69bb1 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -70,6 +70,10 @@ typedef struct VTEncContext {
 int64_t profile;
 int64_t level;
 
+#if !TARGET_OS_IPHONE
+int64_t allow_sw;
+#endif
+
 bool flushing;
 bool has_b_frames;
 bool warned_color_range;
@@ -637,8 +641,11 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 if (!enc_info) return AVERROR(ENOMEM);
 
 #if !TARGET_OS_IPHONE
-CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
-CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,  
kCFBooleanTrue);
+if (vtctx->allow_sw != 1) {
+CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
+} else {
+CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,  
kCFBooleanTrue);
+}
 #endif
 
 if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
@@ -664,30 +671,18 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 &vtctx->session
 );
 
-#if !TARGET_OS_IPHONE
-if (status != 0 || !vtctx->session) {
-CFDictionaryRemoveValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder);
-
-status = VTCompressionSessionCreate(
-kCFAllocatorDefault,
-avctx->width,
-avctx->height,
-codec_type,
-enc_info,
-NULL,
-kCFAllocatorDefault,
-vtenc_output_callback,
-avctx,
-&vtctx->session
-);
-}
-#endif
-
 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
 CFRelease(enc_info);
 
 if (status || !vtctx->session) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: 
%d\n", status);
+
+#if !TARGET_OS_IPHONE
+if (!vtctx->allow_sw) {
+av_log(avctx, AV_LOG_ERROR, "Try -allow_sw 1. The hardware encoder 
may be busy, or not supported.\n");
+}
+#endif
+
 return AVERROR_EXTERNAL;
 }
 
@@ -1484,6 +1479,11 @@ static const AVOption options[] = {
 { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, 
INT_MAX, VE, "level" },
 { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, 
INT_MAX, VE, "level" },
 
+#if !TARGET_OS_IPHONE
+{ "allow_sw", "Allow software encoding", OFFSET(allow_sw), 
AV_OPT_TYPE_BOOL,
+{ .i64 = -1 }, -1, 1, VE },
+#endif
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 00/12] videotoolbox encoder features and fixes

2016-04-22 Thread Rick Kern
Sorry for the delay. The commit for the keyframes_only property was removed - 
this was already supported with -g 1.

Rick Kern (12):
  lavc/videotoolboxenc: Use shared pixel buffer pool
  lavc/videotoolboxenc: Handle hwaccel format as input
  lavc/videotoolboxenc: Require hardware encoding
  lavc/videotoolboxenc: Add entropy setting
  lavc/videotoolboxenc: Fix AVCodecContext.has_b_frames usage.
  lavc/videotoolboxenc: Fix DTS
  lavc/videotoolboxenc: Handle out-of-memory and fix memory leak
  lavc/videotoolboxenc: Add realtime encoding property
  lavc/videotoolboxenc: add concatentation properties
  lavc/videotoolboxenc: Support for forced I-frames
  lavc/videotoolboxenc: Support pixel aspect ratio
  lavc/videotoolboxenc: Set colorimetry values

 configure|   5 +-
 libavcodec/videotoolboxenc.c | 682 ++-
 2 files changed, 619 insertions(+), 68 deletions(-)

-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 01/12] lavc/videotoolboxenc: Use shared pixel buffer pool

2016-04-22 Thread Rick Kern
This reduces the chance of a memcpy in the media server.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 224 ++-
 1 file changed, 180 insertions(+), 44 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 3177074..b578260 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -29,6 +29,7 @@
 #include "libavutil/atomic.h"
 #include "libavutil/avstring.h"
 #include "libavcodec/avcodec.h"
+#include "libavutil/pixdesc.h"
 #include "internal.h"
 #include 
 
@@ -74,6 +75,15 @@ typedef struct VTEncContext {
 bool warned_color_range;
 } VTEncContext;
 
+/**
+ * NULL-safe release of *refPtr, and sets value to NULL.
+ */
+static void vt_release_num(CFNumberRef* refPtr){
+if(!*refPtr) return;
+CFRelease(*refPtr);
+*refPtr = NULL;
+}
+
 static void set_async_error(VTEncContext *vtctx, int err)
 {
 BufNode *info;
@@ -477,9 +487,123 @@ static bool get_vt_profile_level(AVCodecContext *avctx,
 return true;
 }
 
+static int get_cv_pixel_format(
+   AVCodecContext* avctx,
+   enum AVPixelFormat fmt,
+   enum AVColorRange range,
+   int* av_pixel_format,
+   int* range_guessed)
+{
+if (range_guessed) *range_guessed = 0;
+
+switch (fmt) {
+case AV_PIX_FMT_NV12:
+switch (range) {
+case AVCOL_RANGE_MPEG:
+*av_pixel_format = 
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
+break;
+
+case AVCOL_RANGE_JPEG:
+*av_pixel_format = 
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
+break;
+
+default:
+if (range_guessed) *range_guessed = 1;
+*av_pixel_format = 
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
+}
+break;
+
+case AV_PIX_FMT_YUV420P:
+switch (range) {
+case AVCOL_RANGE_MPEG:
+*av_pixel_format = kCVPixelFormatType_420YpCbCr8Planar;
+break;
+
+case AVCOL_RANGE_JPEG:
+*av_pixel_format = 
kCVPixelFormatType_420YpCbCr8PlanarFullRange;
+break;
+
+default:
+if(range_guessed) *range_guessed = 1;
+*av_pixel_format = kCVPixelFormatType_420YpCbCr8Planar;
+}
+break;
+
+default: return AVERROR(EINVAL);
+}
+
+return 0;
+}
+
+static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
+   CFMutableDictionaryRef* dict)
+{
+CFNumberRef cv_color_format_num = NULL;
+CFNumberRef width_num = NULL;
+CFNumberRef height_num = NULL;
+CFMutableDictionaryRef pixel_buffer_info = NULL;
+int cv_color_format;
+int status = get_cv_pixel_format(avctx,
+ avctx->pix_fmt,
+ avctx->color_range,
+ &cv_color_format,
+ NULL);
+if (status) return status;
+
+pixel_buffer_info = CFDictionaryCreateMutable(
+kCFAllocatorDefault,
+20,
+&kCFCopyStringDictionaryKeyCallBacks,
+&kCFTypeDictionaryValueCallBacks);
+
+if (!pixel_buffer_info) goto pbinfo_nomem;
+
+cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberSInt32Type,
+ &cv_color_format);
+if (!cv_color_format_num) goto pbinfo_nomem;
+
+CFDictionarySetValue(pixel_buffer_info,
+ kCVPixelBufferPixelFormatTypeKey,
+ cv_color_format_num);
+vt_release_num(&cv_color_format_num);
+
+width_num = CFNumberCreate(kCFAllocatorDefault,
+   kCFNumberSInt32Type,
+   &avctx->width);
+if (!width_num) return AVERROR(ENOMEM);
+
+CFDictionarySetValue(pixel_buffer_info,
+ kCVPixelBufferWidthKey,
+ width_num);
+vt_release_num(&width_num);
+
+height_num = CFNumberCreate(kCFAllocatorDefault,
+kCFNumberSInt32Type,
+&avctx->height);
+if (!height_num) goto pbinfo_nomem;
+
+CFDictionarySetValue(pixel_buffer_info,
+ kCVPixelBufferHeightKey,
+ height_num);
+vt_release_num(&height_num);
+
+*dict = pixel_buffer_info;
+return 0;
+
+pbinfo_nomem:
+vt_release_num(&cv_color_format_num);
+ 

[FFmpeg-devel] [PATCH 11/13] lavc/videotoolboxenc: Support for forced I-frames

2016-04-09 Thread Rick Kern
Setting AVFrame.pic_type to AV_PICTURE_TYPE_I will force an I-frame.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 26 +-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index ff914f7..0a79ee0 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -1431,27 +1431,51 @@ static int create_cv_pixel_buffer(AVCodecContext   
*avctx,
 return 0;
 }
 
+static int create_encoder_dict_h264(const AVFrame *frame,
+CFDictionaryRef* dict_out)
+{
+CFDictionaryRef dict = NULL;
+if (frame->pict_type == AV_PICTURE_TYPE_I) {
+const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
+const void *vals[] = { kCFBooleanTrue };
+
+dict = CFDictionaryCreate(NULL, keys, vals, 1, NULL, NULL);
+if(!dict) return AVERROR(ENOMEM);
+}
+
+*dict_out = dict;
+return 0;
+}
+
 static int vtenc_send_frame(AVCodecContext *avctx,
 VTEncContext   *vtctx,
 const AVFrame  *frame)
 {
 CMTime time;
+CFDictionaryRef frame_dict;
 CVPixelBufferRef cv_img = NULL;
 int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
 
 if (status) return status;
 
+status = create_encoder_dict_h264(frame, &frame_dict);
+if (status) {
+CFRelease(cv_img);
+return status;
+}
+
 time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
 status = VTCompressionSessionEncodeFrame(
 vtctx->session,
 cv_img,
 time,
 kCMTimeInvalid,
-NULL,
+frame_dict,
 NULL,
 NULL
 );
 
+if (frame_dict) CFRelease(frame_dict);
 CFRelease(cv_img);
 
 if (status) {
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 04/13] lavc/videotoolboxenc: Add entropy setting

2016-04-09 Thread Rick Kern
Add an entropy setting to choose between CAVLC and CABAC.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 32 
 1 file changed, 32 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 2bcd3ef..6586ca7 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -41,6 +41,12 @@ typedef enum VT_H264Profile {
 H264_PROF_COUNT
 } VT_H264Profile;
 
+typedef enum VTH264Entropy{
+VT_ENTROPY_NOT_SET,
+VT_CAVLC,
+VT_CABAC
+} VTH264Entropy;
+
 static const uint8_t start_code[] = { 0, 0, 0, 1 };
 
 typedef struct BufNode {
@@ -70,6 +76,8 @@ typedef struct VTEncContext {
 int64_t profile;
 int64_t level;
 
+int64_t entropy;
+
 bool flushing;
 bool has_b_frames;
 bool warned_color_range;
@@ -623,6 +631,11 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 vtctx->has_b_frames = false;
 }
 
+if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
+av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' 
profile, but baseline was requested. Encode will not use CABAC entropy.\n");
+vtctx->entropy = VT_ENTROPY_NOT_SET;
+}
+
 if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
 
 vtctx->session = NULL;
@@ -721,6 +734,21 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->entropy != VT_ENTROPY_NOT_SET) {
+CFStringRef entropy = vtctx->entropy == VT_CABAC ?
+kVTH264EntropyMode_CABAC:
+kVTH264EntropyMode_CAVLC;
+
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_H264EntropyMode,
+  entropy);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: 
%d\n", status);
+return AVERROR_EXTERNAL;
+}
+}
+
 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", 
status);
@@ -1464,6 +1492,10 @@ static const AVOption options[] = {
 { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, 
INT_MAX, VE, "level" },
 { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, 
INT_MAX, VE, "level" },
 
+{ "entropy", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = 
VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "entropy" },
+{ "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC 
}, INT_MIN, INT_MAX, VE, "entropy" },
+{ "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "entropy" },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 09/13] lavc/videotoolboxenc: Add keyframes_only property

2016-04-09 Thread Rick Kern
Outputs only I-frames when set.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 5430479..c9a60b7 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -77,6 +77,7 @@ typedef struct VTEncContext {
 int64_t level;
 
 int64_t entropy;
+int64_t keyframes_only;
 int64_t realtime;
 
 bool flushing;
@@ -728,6 +729,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->keyframes_only == 1) {
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_AllowTemporalCompression,
+  kCFBooleanFalse);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting keyframes_only 
property: %d\n", status);
+}
+}
+
 if (!vtctx->has_b_frames) {
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_AllowFrameReordering,
@@ -1532,6 +1543,7 @@ static const AVOption options[] = {
 { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC 
}, INT_MIN, INT_MAX, VE, "entropy" },
 { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "entropy" },
 
+{ "keyframes_only", "Output only I-frames", OFFSET(keyframes_only), 
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
 { "realtime", "Hint that encoding should happen in real-time if not faster 
(e.g. capturing from camera).",
 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 13/13] lavc/videotoolboxenc: Set colorimetry values

2016-04-09 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 configure|   3 +
 libavcodec/videotoolboxenc.c | 209 +++
 2 files changed, 212 insertions(+)

diff --git a/configure b/configure
index 94a66d8..10d4bab 100755
--- a/configure
+++ b/configure
@@ -5425,6 +5425,7 @@ check_header vdpau/vdpau_x11.h
 check_header VideoDecodeAcceleration/VDADecoder.h
 check_header VideoToolbox/VideoToolbox.h
 check_func_headers VideoToolbox/VTCompressionSession.h 
VTCompressionSessionPrepareToEncodeFrames -framework VideoToolbox
+check_func_headers CoreVideo/CVImageBuffer.h 
kCVImageBufferColorPrimaries_ITU_R_2020 -framework CoreVideo
 check_header windows.h
 check_header X11/extensions/XvMClib.h
 check_header asm/types.h
@@ -5677,6 +5678,8 @@ enabled openssl   && { use_pkg_config openssl 
openssl/ssl.h SSL_library_
die "ERROR: openssl not found"; }
 enabled qtkit_indev  && { check_header_objcc QTKit/QTKit.h || disable 
qtkit_indev; }
 
+enabled kCVImageBufferColorPrimaries_ITU_R_2020 && add_cppflags 
-DHAVE_VT_BT2020_KEYS
+
 # libdc1394 check
 if enabled libdc1394; then
 { require_pkg_config libdc1394-2 dc1394/dc1394.h dc1394_new &&
diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 72743b5..7f5b216 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -33,6 +33,12 @@
 #include "internal.h"
 #include 
 
+#ifndef HAVE_VT_BT2020_KEYS
+# define kCVImageBufferColorPrimaries_ITU_R_2020   CFSTR("ITU_R_2020")
+# define kCVImageBufferTransferFunction_ITU_R_2020 CFSTR("ITU_R_2020")
+# define kCVImageBufferYCbCrMatrix_ITU_R_2020  CFSTR("ITU_R_2020")
+#endif
+
 typedef enum VT_H264Profile {
 H264_PROF_AUTO,
 H264_PROF_BASELINE,
@@ -58,6 +64,9 @@ typedef struct BufNode {
 typedef struct VTEncContext {
 AVClass *class;
 VTCompressionSessionRef session;
+CFStringRef ycbcr_matrix;
+CFStringRef color_primaries;
+CFStringRef transfer_function;
 
 pthread_mutex_t lock;
 pthread_cond_t  cv_sample_sent;
@@ -547,6 +556,28 @@ static int get_cv_pixel_format(
 return 0;
 }
 
+static void add_color_attr(AVCodecContext *avctx, CFMutableDictionaryRef dict) 
{
+VTEncContext *vtctx = avctx->priv_data;
+
+if (vtctx->color_primaries) {
+CFDictionarySetValue(dict,
+ kCVImageBufferColorPrimariesKey,
+ vtctx->color_primaries);
+}
+
+if (vtctx->transfer_function) {
+CFDictionarySetValue(dict,
+ kCVImageBufferTransferFunctionKey,
+ vtctx->transfer_function);
+}
+
+if (vtctx->ycbcr_matrix) {
+CFDictionarySetValue(dict,
+ kCVImageBufferYCbCrMatrixKey,
+ vtctx->ycbcr_matrix);
+}
+}
+
 static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
CFMutableDictionaryRef* dict)
 {
@@ -600,6 +631,8 @@ static int create_cv_pixel_buffer_info(AVCodecContext* 
avctx,
  height_num);
 vt_release_num(&height_num);
 
+add_color_attr(avctx, pixel_buffer_info);
+
 *dict = pixel_buffer_info;
 return 0;
 
@@ -612,6 +645,110 @@ pbinfo_nomem:
 return AVERROR(ENOMEM);
 }
 
+static int get_cv_color_primaries(AVCodecContext *avctx,
+  CFStringRef *primaries)
+{
+enum AVColorPrimaries pri = avctx->color_primaries;
+switch (pri) {
+case AVCOL_PRI_UNSPECIFIED:
+*primaries = NULL;
+break;
+
+case AVCOL_PRI_BT709:
+*primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
+break;
+
+case AVCOL_PRI_BT2020:
+*primaries = kCVImageBufferColorPrimaries_ITU_R_2020;
+break;
+
+default:
+av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not 
supported.\n", av_color_primaries_name(pri));
+*primaries = NULL;
+return -1;
+}
+
+return 0;
+}
+
+static int get_cv_transfer_function(AVCodecContext *avctx,
+CFStringRef *transfer_fnc,
+CFNumberRef *gamma_level)
+{
+enum AVColorTransferCharacteristic trc = avctx->color_trc;
+Float32 gamma;
+*gamma_level = NULL;
+
+switch (trc) {
+case AVCOL_TRC_UNSPECIFIED:
+*transfer_fnc = NULL;
+break;
+
+case AVCOL_TRC_BT709:
+*transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
+break;
+
+case AVCOL_TRC_SMPTE240M:
+*transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
+break;
+
+case AVCOL_TRC_GAMMA22:
+gamma = 2.2;
+*transfer_fnc = kCVImageBufferTr

[FFmpeg-devel] [PATCH 00/13] videotoolbox encoder features and fixes

2016-04-09 Thread Rick Kern
Added several encoding parameters (ticket #5357), and fixed some bugs.

Rick Kern (13):
  lavc/videotoolboxenc: Use shared pixel buffer pool
  lavc/videotoolboxenc: Handle hwaccel format as input
  lavc/videotoolboxenc: Remove redundant code
  lavc/videotoolboxenc: Add entropy setting
  lavc/videotoolboxenc: Fix AVCodecContext.has_b_frames usage.
  lavc/videotoolboxenc: Fix DTS
  lavc/videotoolboxenc: Handle out-of-memory and fix memory leak
  lavc/videotoolboxenc: Add realtime encoding property
  lavc/videotoolboxenc: Add keyframes_only property
  lavc/videotoolboxenc: add concatentation properties
  lavc/videotoolboxenc: Support for forced I-frames
  lavc/videotoolboxenc: Support pixel aspect ratio
  lavc/videotoolboxenc: Set colorimetry values

 configure|   3 +
 libavcodec/videotoolboxenc.c | 664 ++-
 2 files changed, 601 insertions(+), 66 deletions(-)

-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 12/13] lavc/videotoolboxenc: Support pixel aspect ratio

2016-04-09 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 54 
 1 file changed, 54 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 0a79ee0..72743b5 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -765,6 +765,60 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (avctx->sample_aspect_ratio.num != 0) {
+CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberIntType,
+ &avctx->sample_aspect_ratio.num);
+
+CFNumberRef den = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberIntType,
+ &avctx->sample_aspect_ratio.den);
+
+CFMutableDictionaryRef par = CFDictionaryCreateMutable(
+ kCFAllocatorDefault,
+ 20,
+ &kCFCopyStringDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+
+if (!par || !num || !den) {
+if (par) CFRelease(par);
+if (num) CFRelease(num);
+if (den) CFRelease(den);
+
+return AVERROR(ENOMEM);
+}
+
+CFDictionarySetValue(
+par,
+kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
+num);
+
+CFDictionarySetValue(
+par,
+kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
+den);
+
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_PixelAspectRatio,
+  par);
+
+CFRelease(par);
+CFRelease(num);
+CFRelease(den);
+
+if (status) {
+av_log(
+   avctx,
+   AV_LOG_ERROR,
+   "Error setting pixel aspect ratio to %d:%d: %d.\n",
+   avctx->sample_aspect_ratio.num,
+   avctx->sample_aspect_ratio.den,
+   status);
+
+return AVERROR_EXTERNAL;
+}
+}
+
 if (!vtctx->has_b_frames) {
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_AllowFrameReordering,
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 05/13] lavc/videotoolboxenc: Fix AVCodecContext.has_b_frames usage.

2016-04-09 Thread Rick Kern
Now set by the encoder, not used as an input parameter.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 6586ca7..8352ba9 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -617,6 +617,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFStringRefprofile_level;
 SInt32 bit_rate = avctx->bit_rate;
 CFNumberRefbit_rate_num;
+CFBooleanRef   has_b_frames_cfbool;
 intstatus;
 
 codec_type = get_cm_codec_type(avctx->codec_id);
@@ -625,7 +626,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 return AVERROR(EINVAL);
 }
 
-vtctx->has_b_frames = avctx->has_b_frames || avctx->max_b_frames > 0;
+vtctx->has_b_frames = avctx->max_b_frames > 0;
 if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
 av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline 
profile. Output will not contain B-frames.\n");
 vtctx->has_b_frames = false;
@@ -759,6 +760,18 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
 vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
 
+status = VTSessionCopyProperty(vtctx->session,
+   
kVTCompressionPropertyKey_AllowFrameReordering,
+   kCFAllocatorDefault,
+   &has_b_frames_cfbool);
+
+if (!status) {
+//Some devices don't output B-frames for main profile, even if 
requested.
+vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
+CFRelease(has_b_frames_cfbool);
+}
+avctx->has_b_frames = vtctx->has_b_frames;
+
 return 0;
 }
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 10/13] lavc/videotoolboxenc: add concatentation properties

2016-04-09 Thread Rick Kern
Add frames_before and frames_after as hints that there will be frames before
or after the frames produced in this session. This may help with
concatenation issues like bit rate spikes.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 30 ++
 1 file changed, 30 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index c9a60b7..ff914f7 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -79,6 +79,8 @@ typedef struct VTEncContext {
 int64_t entropy;
 int64_t keyframes_only;
 int64_t realtime;
+int64_t frames_before;
+int64_t frames_after;
 
 bool flushing;
 bool has_b_frames;
@@ -739,6 +741,30 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->frames_before == 1) {
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_MoreFramesBeforeStart,
+  kCFBooleanTrue);
+
+if (status == kVTPropertyNotSupportedErr) {
+av_log(avctx, AV_LOG_WARNING, "frames_before property is not 
supported on this device. Ignoring.\n");
+} else if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting frames_before property: 
%d\n", status);
+}
+}
+
+if (vtctx->frames_after == 1) {
+status = VTSessionSetProperty(vtctx->session,
+  
kVTCompressionPropertyKey_MoreFramesAfterEnd,
+  kCFBooleanTrue);
+
+if (status == kVTPropertyNotSupportedErr) {
+av_log(avctx, AV_LOG_WARNING, "frames_after property is not 
supported on this device. Ignoring.\n");
+} else if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting frames_after property: 
%d\n", status);
+}
+}
+
 if (!vtctx->has_b_frames) {
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_AllowFrameReordering,
@@ -1546,6 +1572,10 @@ static const AVOption options[] = {
 { "keyframes_only", "Output only I-frames", OFFSET(keyframes_only), 
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
 { "realtime", "Hint that encoding should happen in real-time if not faster 
(e.g. capturing from camera).",
 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
+{ "frames_before", "Other frames will come before the frames in this 
session. This helps smooth concatenation issues.",
+OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
+{ "frames_after", "Other frames will come after the frames in this 
session. This helps smooth concatenation issues.",
+OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
 
 { NULL },
 };
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 07/13] lavc/videotoolboxenc: Handle out-of-memory and fix memory leak

2016-04-09 Thread Rick Kern
Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 196f00b..a3f7e5e 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -714,9 +714,12 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
   kCFNumberIntType,
   &avctx->gop_size);
+if (!interval) return AVERROR(ENOMEM);
+
 status = VTSessionSetProperty(vtctx->session,
   
kVTCompressionPropertyKey_MaxKeyFrameInterval,
   interval);
+CFRelease(interval);
 
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame 
interval' property: %d\n", status);
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 06/13] lavc/videotoolboxenc: Fix DTS

2016-04-09 Thread Rick Kern
Some devices output an invalid DTS when B-frames aren't used. Using PTS for
the DTS in this case.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 8352ba9..196f00b 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -1042,6 +1042,15 @@ static int vtenc_cm_to_avpacket(
 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
 dts = CMSampleBufferGetDecodeTimeStamp  (sample_buffer);
 
+if (CMTIME_IS_INVALID(dts)) {
+if (!vtctx->has_b_frames) {
+dts = pts;
+} else {
+av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
+return AVERROR_EXTERNAL;
+}
+}
+
 dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
 time_base_num = avctx->time_base.num;
 pkt->pts = pts.value / time_base_num;
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 08/13] lavc/videotoolboxenc: Add realtime encoding property

2016-04-09 Thread Rick Kern
Hint to the encoder that encoding should be done in real-time, even at the
expense of quality.

Signed-off-by: Rick Kern 
---
 libavcodec/videotoolboxenc.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index a3f7e5e..5430479 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -77,6 +77,7 @@ typedef struct VTEncContext {
 int64_t level;
 
 int64_t entropy;
+int64_t realtime;
 
 bool flushing;
 bool has_b_frames;
@@ -753,6 +754,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
 }
 }
 
+if (vtctx->realtime) {
+status = VTSessionSetProperty(vtctx->session,
+  kVTCompressionPropertyKey_RealTime,
+  kCFBooleanTrue);
+
+if (status) {
+av_log(avctx, AV_LOG_ERROR, "Error setting realtime property: 
%d\n", status);
+}
+}
+
 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
 if (status) {
 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", 
status);
@@ -1521,6 +1532,9 @@ static const AVOption options[] = {
 { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC 
}, INT_MIN, INT_MAX, VE, "entropy" },
 { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC 
}, INT_MIN, INT_MAX, VE, "entropy" },
 
+{ "realtime", "Hint that encoding should happen in real-time if not faster 
(e.g. capturing from camera).",
+OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
+
 { NULL },
 };
 
-- 
2.7.4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


  1   2   >