[4/4] libopenh264enc: Simplify init by setting FF_CODEC_CAP_INIT_CLEANUP

Message ID 1468525196-59168-4-git-send-email-martin@martin.st
State Committed
Commit 7a76371437f9562c3414f985523f883489e3936a
Headers show

Commit Message

Martin Storsjö July 14, 2016, 7:39 p.m.
Also set FF_CODEC_CAP_INIT_THREADSAFE while adding internal capabilities.
---
 libavcodec/libopenh264enc.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

Comments

Luca Barbato July 14, 2016, 7:53 p.m. | #1
On 14/07/16 21:39, Martin Storsjö wrote:
> Also set FF_CODEC_CAP_INIT_THREADSAFE while adding internal capabilities.
> ---
>  libavcodec/libopenh264enc.c | 27 ++++++++-------------------
>  1 file changed, 8 insertions(+), 19 deletions(-)

Possibly OK.

lu
Diego Biurrun July 25, 2016, 10:50 a.m. | #2
On Thu, Jul 14, 2016 at 10:39:56PM +0300, Martin Storsjö wrote:
> Also set FF_CODEC_CAP_INIT_THREADSAFE while adding internal capabilities.
> ---
>  libavcodec/libopenh264enc.c | 27 ++++++++-------------------
>  1 file changed, 8 insertions(+), 19 deletions(-)

Doesn't 1/4 rely on this being available and thus should come after 4/4
or be squashed into it?

Diego
Martin Storsjö July 25, 2016, 10:52 a.m. | #3
On Mon, 25 Jul 2016, Diego Biurrun wrote:

> On Thu, Jul 14, 2016 at 10:39:56PM +0300, Martin Storsjö wrote:
>> Also set FF_CODEC_CAP_INIT_THREADSAFE while adding internal capabilities.
>> ---
>>  libavcodec/libopenh264enc.c | 27 ++++++++-------------------
>>  1 file changed, 8 insertions(+), 19 deletions(-)
>
> Doesn't 1/4 rely on this being available and thus should come after 4/4
> or be squashed into it?

No, 1/4 is for the decoder (which had these flags set, thanks to Vittorio 
for asking for them), this is for the encoder (which didn't have these 
flags set).

// Martin

Patch

diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c
index bf1cf25..a09b7cf 100644
--- a/libavcodec/libopenh264enc.c
+++ b/libavcodec/libopenh264enc.c
@@ -100,8 +100,6 @@  static av_cold int svc_encode_init(AVCodecContext *avctx)
 
     if ((err = ff_libopenh264_check_version(avctx)) < 0)
         return err;
-    // Use a default error for multiple error paths below
-    err = AVERROR_UNKNOWN;
 
     if (WelsCreateSVCEncoder(&s->encoder)) {
         av_log(avctx, AV_LOG_ERROR, "Unable to create encoder\n");
@@ -167,8 +165,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
         av_log(avctx, AV_LOG_ERROR,
                "Invalid combination -slices %d and -max_nal_size %d.\n",
                avctx->slices, s->max_nal_size);
-        err = AVERROR(EINVAL);
-        goto fail;
+        return AVERROR(EINVAL);
     }
 
     if (avctx->slices > 1)
@@ -196,14 +193,13 @@  FF_ENABLE_DEPRECATION_WARNINGS
         } else {
             av_log(avctx, AV_LOG_ERROR, "Invalid -max_nal_size, "
                    "specify a valid max_nal_size to use -slice_mode dyn\n");
-            err = AVERROR(EINVAL);
-            goto fail;
+            return AVERROR(EINVAL);
         }
     }
 
     if ((*s->encoder)->InitializeExt(s->encoder, &param) != cmResultSuccess) {
         av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
-        goto fail;
+        return AVERROR_UNKNOWN;
     }
 
     if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
@@ -213,27 +209,19 @@  FF_ENABLE_DEPRECATION_WARNINGS
         for (i = 0; i < fbi.sLayerInfo[0].iNalCount; i++)
             size += fbi.sLayerInfo[0].pNalLengthInByte[i];
         avctx->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
-        if (!avctx->extradata) {
-            err = AVERROR(ENOMEM);
-            goto fail;
-        }
+        if (!avctx->extradata)
+            return AVERROR(ENOMEM);
         avctx->extradata_size = size;
         memcpy(avctx->extradata, fbi.sLayerInfo[0].pBsBuf, size);
     }
 
     props = ff_add_cpb_side_data(avctx);
-    if (!props) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
+    if (!props)
+        return AVERROR(ENOMEM);
     props->max_bitrate = param.iMaxBitrate;
     props->avg_bitrate = param.iTargetBitrate;
 
     return 0;
-
-fail:
-    svc_encode_close(avctx);
-    return err;
 }
 
 static int svc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
@@ -306,6 +294,7 @@  AVCodec ff_libopenh264_encoder = {
     .encode2        = svc_encode_frame,
     .close          = svc_encode_close,
     .capabilities   = AV_CODEC_CAP_AUTO_THREADS,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
                                                     AV_PIX_FMT_NONE },
     .priv_class     = &class,