]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libkvazaar.c
avcodec: Remove redundant freeing of extradata of encoders
[ffmpeg] / libavcodec / libkvazaar.c
index 5bc5b4ebf16b655f14f43d5b2bca002ceb2d08f7..14eabe4e05a28651a359355e81e9d58fdb15ce09 100644 (file)
@@ -37,6 +37,7 @@
 
 #include "avcodec.h"
 #include "internal.h"
+#include "packet_internal.h"
 
 typedef struct LibkvazaarContext {
     const AVClass *class;
@@ -79,16 +80,24 @@ static av_cold int libkvazaar_init(AVCodecContext *avctx)
     cfg->width  = avctx->width;
     cfg->height = avctx->height;
 
-    if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Could not set framerate for kvazaar: integer overflow\n");
-        return AVERROR(EINVAL);
+    if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
+        cfg->framerate_num   = avctx->framerate.num;
+        cfg->framerate_denom = avctx->framerate.den;
+    } else {
+        if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "Could not set framerate for kvazaar: integer overflow\n");
+            return AVERROR(EINVAL);
+        }
+        cfg->framerate_num   = avctx->time_base.den;
+        cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
     }
-    cfg->framerate_num   = avctx->time_base.den;
-    cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
     cfg->target_bitrate = avctx->bit_rate;
     cfg->vui.sar_width  = avctx->sample_aspect_ratio.num;
     cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
+    if (avctx->bit_rate) {
+        cfg->rc_algorithm = KVZ_LAMBDA;
+    }
 
     if (ctx->kvz_params) {
         AVDictionary *dict = NULL;
@@ -100,8 +109,8 @@ static av_cold int libkvazaar_init(AVCodecContext *avctx)
                            entry->key, entry->value);
                 }
             }
-            av_dict_free(&dict);
         }
+        av_dict_free(&dict);
     }
 
     ctx->encoder = enc = api->encoder_open(cfg);
@@ -143,13 +152,10 @@ static av_cold int libkvazaar_close(AVCodecContext *avctx)
     LibkvazaarContext *ctx = avctx->priv_data;
 
     if (ctx->api) {
-      ctx->api->encoder_close(ctx->encoder);
-      ctx->api->config_destroy(ctx->config);
+        ctx->api->encoder_close(ctx->encoder);
+        ctx->api->config_destroy(ctx->config);
     }
 
-    if (avctx->extradata)
-        av_freep(&avctx->extradata);
-
     return 0;
 }
 
@@ -165,12 +171,13 @@ static int libkvazaar_encode(AVCodecContext *avctx,
     kvz_data_chunk *data_out = NULL;
     uint32_t len_out = 0;
     int retval = 0;
+    int pict_type;
 
     *got_packet_ptr = 0;
 
     if (frame) {
         if (frame->width != ctx->config->width ||
-                frame->height != ctx->config->height) {
+            frame->height != ctx->config->height) {
             av_log(avctx, AV_LOG_ERROR,
                    "Changing video dimensions during encoding is not supported. "
                    "(changed from %dx%d to %dx%d)\n",
@@ -223,8 +230,7 @@ static int libkvazaar_encode(AVCodecContext *avctx,
         av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
         retval = AVERROR_INVALIDDATA;
         goto done;
-    }
-    else
+    } else
         retval = 0; /* kvazaar returns 1 on success */
 
     if (data_out) {
@@ -249,10 +255,27 @@ static int libkvazaar_encode(AVCodecContext *avctx,
         // IRAP VCL NAL unit types span the range
         // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
         if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
-                frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
+            frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
             avpkt->flags |= AV_PKT_FLAG_KEY;
         }
 
+        switch (frame_info.slice_type) {
+        case KVZ_SLICE_I:
+            pict_type = AV_PICTURE_TYPE_I;
+            break;
+        case KVZ_SLICE_P:
+            pict_type = AV_PICTURE_TYPE_P;
+            break;
+        case KVZ_SLICE_B:
+            pict_type = AV_PICTURE_TYPE_B;
+            break;
+        default:
+            av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
+            return AVERROR_EXTERNAL;
+        }
+
+        ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
+
         *got_packet_ptr = 1;
     }
 
@@ -288,12 +311,12 @@ static const AVCodecDefault defaults[] = {
     { NULL },
 };
 
-AVCodec ff_libkvazaar_encoder = {
+const AVCodec ff_libkvazaar_encoder = {
     .name             = "libkvazaar",
     .long_name        = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
     .type             = AVMEDIA_TYPE_VIDEO,
     .id               = AV_CODEC_ID_HEVC,
-    .capabilities     = AV_CODEC_CAP_DELAY,
+    .capabilities     = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
     .pix_fmts         = pix_fmts,
 
     .priv_class       = &class,
@@ -304,7 +327,8 @@ AVCodec ff_libkvazaar_encoder = {
     .encode2          = libkvazaar_encode,
     .close            = libkvazaar_close,
 
-    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
+                        FF_CODEC_CAP_AUTO_THREADS,
 
     .wrapper_name     = "libkvazaar",
 };