The reason for removing it from the global context ist that it is an
encoding-only parameter, and not all encoders use it.
Moreover it introudces unpredicted behaviour since some encoders fail,
some warn and some just clip on an invalid value.

Replace it with a codec private option, and always set it to the current
default, with appropriate bounds. During the deprecation period, the
global context field will be read if the codec context field is set to
the default value.

Signed-off-by: Vittorio Giovara <[email protected]>
---
 libavcodec/alacenc.c        | 29 +++++++++++++++++++++++++++--
 libavcodec/avcodec.h        |  7 ++++---
 libavcodec/flacenc.c        | 14 +++++++++++---
 libavcodec/libmp3lame.c     | 16 ++++++++++++----
 libavcodec/libopenjpegenc.c | 11 ++++++++++-
 libavcodec/libopusenc.c     | 14 +++++++++++---
 libavcodec/libspeexenc.c    | 26 +++++++++++++++++++-------
 libavcodec/libwavpackenc.c  | 41 ++++++++++++++++++++++++++++++++---------
 libavcodec/libwebpenc.c     | 24 +++++++++++++++++-------
 libavcodec/options_table.h  |  2 ++
 libavcodec/pngenc.c         | 37 ++++++++++++++++++++++++++++++++-----
 libavcodec/version.h        |  3 +++
 libavcodec/zmbvenc.c        | 32 ++++++++++++++++++++++++++++----
 13 files changed, 208 insertions(+), 48 deletions(-)

diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c
index 74e5ae7..530b103 100644
--- a/libavcodec/alacenc.c
+++ b/libavcodec/alacenc.c
@@ -19,6 +19,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/opt.h"
+
 #include "avcodec.h"
 #include "put_bits.h"
 #include "internal.h"
@@ -57,6 +59,7 @@ typedef struct AlacLPCContext {
 } AlacLPCContext;
 
 typedef struct AlacEncodeContext {
+    AVClass *class;
     int frame_size;                     /**< current frame size               
*/
     int verbatim;                       /**< current frame verbatim mode flag 
*/
     int compression_level;
@@ -504,11 +507,17 @@ static av_cold int alac_encode_init(AVCodecContext *avctx)
         s->extra_bits              = 0;
     }
 
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (s->compression_level == 2)
+        s->compression_level = avctx->compression_level;
     // Set default compression level
-    if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
+    if (s->compression_level == FF_COMPRESSION_DEFAULT)
         s->compression_level = 2;
     else
-        s->compression_level = av_clip(avctx->compression_level, 0, 2);
+        s->compression_level = av_clip(s->compression_level, 0, 2);
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
     // Initialize default Rice parameters
     s->rc.history_mult    = 40;
@@ -641,12 +650,28 @@ static int alac_encode_frame(AVCodecContext *avctx, 
AVPacket *avpkt,
     return 0;
 }
 
+#define OFFSET(x) offsetof(AlacEncodeContext, x)
+#define FLAGS     AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, { 
.i64 = 2 }, 0, 2, FLAGS, },
+
+    { NULL },
+};
+
+static const AVClass alac_class = {
+    .class_name = "ALAC encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_alac_encoder = {
     .name           = "alac",
     .long_name      = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio 
Codec)"),
     .type           = AVMEDIA_TYPE_AUDIO,
     .id             = AV_CODEC_ID_ALAC,
     .priv_data_size = sizeof(AlacEncodeContext),
+    .priv_class     = &alac_class,
     .init           = alac_encode_init,
     .encode2        = alac_encode_frame,
     .close          = alac_encode_close,
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index f47bfc0..ba3007c 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1145,12 +1145,13 @@ typedef struct AVCodecContext {
      */
     int global_quality;
 
+#if FF_API_COMPRESSION_LEVEL
     /**
-     * - encoding: Set by user.
-     * - decoding: unused
+     * @deprecated use encoder private option instead
      */
-    int compression_level;
+    attribute_deprecated int compression_level;
 #define FF_COMPRESSION_DEFAULT -1
+#endif
 
     /**
      * AV_CODEC_FLAG_*.
diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index c4c60ee..4c3ac45 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -233,8 +233,9 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
     int freq = avctx->sample_rate;
     int channels = avctx->channels;
     FlacEncodeContext *s = avctx->priv_data;
-    int i, level, ret;
+    int level = s->options.compression_level;
     uint8_t *streaminfo;
+    int i, ret;
 
     s->avctx = avctx;
 
@@ -283,11 +284,15 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
         s->samplerate = freq;
     }
 
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
     /* set compression option defaults based on avctx->compression_level */
-    if (avctx->compression_level < 0)
+    if (s->options.compression_level == 5)
+        s->options.compression_level = avctx->compression_level;
+    if (s->options.compression_level < 0)
         s->options.compression_level = 5;
     else
-        s->options.compression_level = avctx->compression_level;
+        s->options.compression_level = s->options.compression_level;
 
     level = s->options.compression_level;
     if (level > 12) {
@@ -295,6 +300,8 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
                s->options.compression_level);
         return -1;
     }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
     s->options.block_time_ms = ((int[]){ 27, 27, 
27,105,105,105,105,105,105,105,105,105,105})[level];
 
@@ -1324,6 +1331,7 @@ static const AVOption options[] = {
 { "left_side",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE   
}, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
 { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE  
}, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
 { "mid_side",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE    
}, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
+{ "compression_level", NULL, offsetof(FlacEncodeContext, 
options.compression_level), AV_OPT_TYPE_INT, { .i64 = 5 }, 0, 12, FLAGS },
 { NULL },
 };
 
diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index 966212d..bf11802 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -53,6 +53,7 @@ typedef struct LAMEContext {
     float *samples_flt[2];
     AudioFrameQueue afq;
     AVFloatDSPContext fdsp;
+    int compression_level;
 } LAMEContext;
 
 
@@ -105,10 +106,15 @@ static av_cold int mp3lame_encode_init(AVCodecContext 
*avctx)
     lame_set_out_samplerate(s->gfp, avctx->sample_rate);
 
     /* algorithmic quality */
-    if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
-        lame_set_quality(s->gfp, 5);
-    else
-        lame_set_quality(s->gfp, avctx->compression_level);
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (s->compression_level == 5)
+        s->compression_level = avctx->compression_level;
+    if (s->compression_level == FF_COMPRESSION_DEFAULT)
+        s->compression_level = 5;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+    lame_set_quality(s->gfp, s->compression_level);
 
     /* rate control */
     if (avctx->flags & AV_CODEC_FLAG_QSCALE) { // VBR
@@ -275,6 +281,8 @@ static const AVOption options[] = {
     { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { 
.i64 = 1 }, 0, 1, AE },
     { "joint_stereo", "Use joint stereo.", OFFSET(joint_stereo), 
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
     { "abr", "Use ABR", OFFSET(abr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
+    { "compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, { 
.i64 = 5 }, 0, 9, AE },
+
     { NULL },
 };
 
diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c
index 00c0ed6..34b7a95 100644
--- a/libavcodec/libopenjpegenc.c
+++ b/libavcodec/libopenjpegenc.c
@@ -49,6 +49,7 @@ typedef struct LibOpenJPEGContext {
     int disto_alloc;
     int fixed_alloc;
     int fixed_quality;
+    int compression_level
 } LibOpenJPEGContext;
 
 static void error_callback(const char *msg, void *data)
@@ -152,6 +153,13 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext 
*avctx)
 
     opj_set_default_encoder_parameters(&ctx->enc_params);
 
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (ctx->compression_level == 0)
+        ctx->compression_level = FFMAX(avctx->compression_level, 0);
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
     ctx->enc_params.cp_rsiz          = ctx->profile;
     ctx->enc_params.mode             = !!avctx->global_quality;
     ctx->enc_params.cp_cinema        = ctx->cinema_mode;
@@ -161,7 +169,7 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext 
*avctx)
     ctx->enc_params.cp_fixed_alloc   = ctx->fixed_alloc;
     ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
     ctx->enc_params.tcp_numlayers    = ctx->numlayers;
-    ctx->enc_params.tcp_rates[0]     = FFMAX(avctx->compression_level, 0) * 2;
+    ctx->enc_params.tcp_rates[0]     = ctx->compression_level * 2;
 
     ctx->compress = opj_create_compress(ctx->format);
     if (!ctx->compress) {
@@ -401,6 +409,7 @@ static const AVOption options[] = {
     { "disto_alloc",   NULL,                OFFSET(disto_alloc),   
AV_OPT_TYPE_INT,   { .i64 = 1           }, 0,         1,           VE },
     { "fixed_alloc",   NULL,                OFFSET(fixed_alloc),   
AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE },
     { "fixed_quality", NULL,                OFFSET(fixed_quality), 
AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE },
+    { "compression_level", NULL,            OFFSET(compression_level), 
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT_MAX, VE },
     { NULL },
 };
 
diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c
index 08f0f28..8bca7f6 100644
--- a/libavcodec/libopusenc.c
+++ b/libavcodec/libopusenc.c
@@ -209,14 +209,18 @@ static int av_cold libopus_encode_init(AVCodecContext 
*avctx)
         return AVERROR(EINVAL);
     }
 
-    if (avctx->compression_level < 0 || avctx->compression_level > 10) {
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (opus->opts.complexity == 10)
+        opus->opts.complexity = avctx->compression_level;
+    if (opus->opts.complexity < 0 || opus->opts.complexity > 10) {
         av_log(avctx, AV_LOG_WARNING,
                "Compression level must be in the range 0 to 10. "
                "Defaulting to 10.\n");
         opus->opts.complexity = 10;
-    } else {
-        opus->opts.complexity = avctx->compression_level;
     }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
     if (avctx->cutoff) {
         switch (avctx->cutoff) {
@@ -384,6 +388,8 @@ static const AVOption libopus_options[] = {
         { "off",            "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { 
.i64 = 0 }, 0, 0, FLAGS, "vbr" },
         { "on",             "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { 
.i64 = 1 }, 0, 0, FLAGS, "vbr" },
         { "constrained",    "Use constrained VBR",   0, AV_OPT_TYPE_CONST, { 
.i64 = 2 }, 0, 0, FLAGS, "vbr" },
+    { "compression_level", NULL, OFFSET(complexity), AV_OPT_TYPE_INT, { .i64 = 
10 }, 0, 10, FLAGS },
+
     { NULL },
 };
 
@@ -396,7 +402,9 @@ static const AVClass libopus_class = {
 
 static const AVCodecDefault libopus_defaults[] = {
     { "b",                 "0" },
+#if FF_API_COMPRESSION_LEVEL
     { "compression_level", "10" },
+#endif
     { NULL },
 };
 
diff --git a/libavcodec/libspeexenc.c b/libavcodec/libspeexenc.c
index 861f5d2..c86acb6 100644
--- a/libavcodec/libspeexenc.c
+++ b/libavcodec/libspeexenc.c
@@ -52,10 +52,10 @@
  *         ultra-wideband : 4400 - 45200 bps
  *
  * Complexity
- *     Encoding complexity is controlled by setting avctx->compression_level.
- *     The valid range is 0 to 10.  A higher setting gives generally better
- *     quality at the expense of encoding speed.  This does not affect the
- *     bit rate.
+ *     Encoding complexity is controlled by setting codec private option
+ *     "compression_level". The valid range is 0 to 10.
+ *     A higher setting gives generally better quality at the expense of
+ *     encoding speed.  This does not affect the bit rate.
  *
  * Frames-per-Packet
  *     The encoder defaults to using 1 frame-per-packet.  However, it is
@@ -105,6 +105,7 @@ typedef struct LibSpeexEncContext {
     int dtx;                    ///< flag to enable DTX
     int pkt_frame_count;        ///< frame count for the current packet
     AudioFrameQueue afq;        ///< frame queue
+    int compression_level;      ///< complexity
 } LibSpeexEncContext;
 
 static av_cold void print_enc_params(AVCodecContext *avctx,
@@ -130,7 +131,7 @@ static av_cold void print_enc_params(AVCodecContext *avctx,
         av_log(avctx, AV_LOG_DEBUG, "  bitrate: %d bps\n", avctx->bit_rate);
     }
     av_log(avctx, AV_LOG_DEBUG, "complexity: %d\n",
-           avctx->compression_level);
+           s->compression_level);
     av_log(avctx, AV_LOG_DEBUG, "frame size: %d samples\n",
            avctx->frame_size);
     av_log(avctx, AV_LOG_DEBUG, "frames per packet: %d\n",
@@ -147,7 +148,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     const SpeexMode *mode;
     uint8_t *header_data;
     int header_size;
-    int32_t complexity;
+    int32_t complexity = s->compression_level;
 
     /* channels */
     if (avctx->channels < 1 || avctx->channels > 2) {
@@ -223,12 +224,20 @@ static av_cold int encode_init(AVCodecContext *avctx)
     }
 
     /* set encoding complexity */
-    if (avctx->compression_level > FF_COMPRESSION_DEFAULT) {
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (s->compression_level == 3 &&
+        avctx->compression_level > FF_COMPRESSION_DEFAULT) {
         complexity = av_clip(avctx->compression_level, 0, 10);
         speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
     }
     speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
     avctx->compression_level = complexity;
+FF_ENABLE_DEPRECATION_WARNINGS
+#else
+    speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
+#endif
+    s->compression_level = complexity;
 
     /* set packet size */
     avctx->frame_size = s->header.frame_size;
@@ -332,6 +341,7 @@ static const AVOption options[] = {
     { "frames_per_packet", "Number of frames to encode in each packet", 
OFFSET(frames_per_packet), AV_OPT_TYPE_INT, { .i64 = 1 }, 1,   8, AE },
     { "vad",               "Voice Activity Detection",                  
OFFSET(vad),               AV_OPT_TYPE_INT, { .i64 = 0 }, 0,   1, AE },
     { "dtx",               "Discontinuous Transmission",                
OFFSET(dtx),               AV_OPT_TYPE_INT, { .i64 = 0 }, 0,   1, AE },
+    { "compression_level", NULL,                                        
OFFSET(compression_level), AV_OPT_TYPE_INT, { .i64 = 3 }, 0,  10, AE },
     { NULL },
 };
 
@@ -344,7 +354,9 @@ static const AVClass class = {
 
 static const AVCodecDefault defaults[] = {
     { "b",                 "0" },
+#if FF_API_COMPRESSION_LEVEL
     { "compression_level", "3" },
+#endif
     { NULL },
 };
 
diff --git a/libavcodec/libwavpackenc.c b/libavcodec/libwavpackenc.c
index 1455d91..135f23d 100644
--- a/libavcodec/libwavpackenc.c
+++ b/libavcodec/libwavpackenc.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/opt.h"
+#include "libavutil/internal.h"
 #include "libavutil/samplefmt.h"
 
 #include "audio_frame_queue.h"
@@ -38,6 +39,7 @@ typedef struct LibWavpackContext {
     int user_size;
 
     int got_output;
+    int compression_level;
 } LibWavpackContext;
 
 static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
@@ -130,23 +132,29 @@ static av_cold int wavpack_encode_init(AVCodecContext 
*avctx)
     config.num_channels     = avctx->channels;
     config.sample_rate      = avctx->sample_rate;
 
-    if (avctx->compression_level != FF_COMPRESSION_DEFAULT) {
-        if (avctx->compression_level >= 3) {
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (s->compression_level == -1)
+        s->compression_level = avctx->compression_level;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+    if (s->compression_level != FF_COMPRESSION_DEFAULT) {
+        if (s->compression_level >= 3) {
             config.flags |= CONFIG_VERY_HIGH_FLAG;
 
-            if      (avctx->compression_level >= 8)
+            if      (s->compression_level >= 8)
                 config.xmode = 6;
-            else if (avctx->compression_level >= 7)
+            else if (s->compression_level >= 7)
                 config.xmode = 5;
-            else if (avctx->compression_level >= 6)
+            else if (s->compression_level >= 6)
                 config.xmode = 4;
-            else if (avctx->compression_level >= 5)
+            else if (s->compression_level >= 5)
                 config.xmode = 3;
-            else if (avctx->compression_level >= 4)
+            else if (s->compression_level >= 4)
                 config.xmode = 2;
-        } else if (avctx->compression_level >= 2)
+        } else if (s->compression_level >= 2)
             config.flags |= CONFIG_HIGH_FLAG;
-        else if (avctx->compression_level < 1)
+        else if (s->compression_level < 1)
             config.flags |= CONFIG_FAST_FLAG;
     }
 
@@ -180,6 +188,21 @@ static av_cold int wavpack_encode_close(AVCodecContext 
*avctx)
     return 0;
 }
 
+#define OFFSET(x) offsetof(LibWavpackContext, x)
+#define FLAGS     AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, { 
.i64 = -1 }, -1, 9, FLAGS },
+
+    { NULL },
+};
+
+static const AVClass pngenc_class = {
+    .class_name = "PNG encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_libwavpack_encoder = {
     .name           = "libwavpack",
     .type           = AVMEDIA_TYPE_AUDIO,
diff --git a/libavcodec/libwebpenc.c b/libavcodec/libwebpenc.c
index ef311b7..61f75c9 100644
--- a/libavcodec/libwebpenc.c
+++ b/libavcodec/libwebpenc.c
@@ -40,6 +40,7 @@ typedef struct LibWebPContext {
     int preset;             // configuration preset
     int chroma_warning;     // chroma linesize mismatch warning has been 
printed
     int conversion_warning; // pixel format conversion warning has been printed
+    int compression_level;  // compression method
     WebPConfig config;      // libwebp configuration
 } LibWebPContext;
 
@@ -67,19 +68,25 @@ static av_cold int libwebp_encode_init(AVCodecContext 
*avctx)
     s->quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
                           0.0f, 100.0f);
 
-    if (avctx->compression_level < 0 || avctx->compression_level > 6) {
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (s->compression_level == 4 )
+        s->compression_level = avctx->compression_level;
+    if (s->compression_level < 0 || s->compression_level > 6) {
         av_log(avctx, AV_LOG_WARNING, "invalid compression level: %d\n",
                avctx->compression_level);
-        avctx->compression_level = av_clip(avctx->compression_level, 0, 6);
+        s->compression_level = av_clip(s->compression_level, 0, 6);
     }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
     if (s->preset >= WEBP_PRESET_DEFAULT) {
         ret = WebPConfigPreset(&s->config, s->preset, s->quality);
         if (!ret)
             return AVERROR_UNKNOWN;
-        s->lossless              = s->config.lossless;
-        s->quality               = s->config.quality;
-        avctx->compression_level = s->config.method;
+        s->lossless          = s->config.lossless;
+        s->quality           = s->config.quality;
+        s->compression_level = s->config.method;
     } else {
         ret = WebPConfigInit(&s->config);
         if (!ret)
@@ -87,7 +94,7 @@ static av_cold int libwebp_encode_init(AVCodecContext *avctx)
 
         s->config.lossless = s->lossless;
         s->config.quality  = s->quality;
-        s->config.method   = avctx->compression_level;
+        s->config.method   = s->compression_level;
 
         ret = WebPValidateConfig(&s->config);
         if (!ret)
@@ -96,7 +103,7 @@ static av_cold int libwebp_encode_init(AVCodecContext *avctx)
 
     av_log(avctx, AV_LOG_DEBUG, "%s - quality=%.1f method=%d\n",
            s->lossless ? "Lossless" : "Lossy", s->quality,
-           avctx->compression_level);
+           s->compression_level);
 
     return 0;
 }
@@ -255,6 +262,7 @@ static const AVOption options[] = {
     { "drawing",    "hand or line drawing, with high-contrast details", 0, 
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DRAWING }, 0, 0, VE, "preset" },
     { "icon",       "small-sized colorful images",                      0, 
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_ICON    }, 0, 0, VE, "preset" },
     { "text",       "text-like",                                        0, 
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_TEXT    }, 0, 0, VE, "preset" },
+    { "compression_level", NULL,               OFFSET(compression_level), 
AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 6,                      VE           },
     { NULL },
 };
 
@@ -266,7 +274,9 @@ static const AVClass class = {
 };
 
 static const AVCodecDefault libwebp_defaults[] = {
+#if FF_API_COMPRESSION_LEVEL
     { "compression_level",  "4"  },
+#endif
     { "global_quality",     "-1" },
     { NULL },
 };
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index dd30eea..c12c3b1 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -383,7 +383,9 @@ static const AVOption avcodec_options[] = {
 #endif /* FF_API_UNUSED_MEMBERS */
 {"mv0_threshold", NULL, OFFSET(mv0_threshold), AV_OPT_TYPE_INT, {.i64 = 256 }, 
0, INT_MAX, V|E},
 {"b_sensitivity", "adjust sensitivity of b_frame_strategy 1", 
OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, V|E},
+#if FF_API_COMPRESSION_LEVEL
 {"compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, {.i64 
= FF_COMPRESSION_DEFAULT }, INT_MIN, INT_MAX, V|A|E},
+#endif
 {"min_prediction_order", NULL, OFFSET(min_prediction_order), AV_OPT_TYPE_INT, 
{.i64 = -1 }, INT_MIN, INT_MAX, A|E},
 {"max_prediction_order", NULL, OFFSET(max_prediction_order), AV_OPT_TYPE_INT, 
{.i64 = -1 }, INT_MIN, INT_MAX, A|E},
 {"timecode_frame_start", "GOP timecode frame start number, in non-drop-frame 
format", OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, 
INT64_MAX, V|E},
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 08c59e6..d7fd35b 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -19,6 +19,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/opt.h"
+
 #include "avcodec.h"
 #include "bytestream.h"
 #include "huffyuvencdsp.h"
@@ -33,6 +35,7 @@
 #define IOBUF_SIZE 4096
 
 typedef struct PNGEncContext {
+    AVClass *class;
     HuffYUVEncDSPContext hdsp;
 
     uint8_t *bytestream;
@@ -43,6 +46,8 @@ typedef struct PNGEncContext {
 
     z_stream zstream;
     uint8_t buf[IOBUF_SIZE];
+
+    int compression_level;
 } PNGEncContext;
 
 static void png_get_interlaced_row(uint8_t *dst, int row_size,
@@ -236,7 +241,6 @@ static int encode_frame(AVCodecContext *avctx, AVPacket 
*pkt,
     const AVFrame *const p = pict;
     int bit_depth, color_type, y, len, row_size, ret, is_progressive;
     int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
-    int compression_level;
     uint8_t *ptr, *top, *crow_buf, *crow;
     uint8_t *crow_base       = NULL;
     uint8_t *progressive_buf = NULL;
@@ -286,10 +290,17 @@ static int encode_frame(AVCodecContext *avctx, AVPacket 
*pkt,
     s->zstream.zalloc = ff_png_zalloc;
     s->zstream.zfree  = ff_png_zfree;
     s->zstream.opaque = NULL;
-    compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
-                      ? Z_DEFAULT_COMPRESSION
-                      : av_clip(avctx->compression_level, 0, 9);
-    ret = deflateInit2(&s->zstream, compression_level,
+
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (s->compression_level == Z_DEFAULT_COMPRESSION)
+        s->compression_level = avctx->compression_level;
+    s->compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
+                         ? Z_DEFAULT_COMPRESSION
+                         : av_clip(avctx->compression_level, 0, 9);
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+    ret = deflateInit2(&s->zstream, s->compression_level,
                        Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
     if (ret != Z_OK)
         return -1;
@@ -479,12 +490,28 @@ static av_cold int png_enc_close(AVCodecContext *avctx)
     return 0;
 }
 
+#define OFFSET(x) offsetof(PNGEncContext, x)
+#define FLAGS     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, { 
.i64 = Z_DEFAULT_COMPRESSION }, 0, 9, FLAGS },
+
+    { NULL },
+};
+
+static const AVClass pngenc_class = {
+    .class_name = "PNG encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_png_encoder = {
     .name           = "png",
     .long_name      = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) 
image"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PNG,
     .priv_data_size = sizeof(PNGEncContext),
+    .priv_class     = &pngenc_class,
     .init           = png_enc_init,
     .close          = png_enc_close,
     .encode2        = encode_frame,
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 1fdaea5..c83ef93 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -150,6 +150,9 @@
 #ifndef FF_API_NON_PREFIXED_SYM
 #define FF_API_NON_PREFIXED_SYM  (LIBAVCODEC_VERSION_MAJOR < 59)
 #endif
+#ifndef FF_API_COMPRESSION_LEVEL
+#define FF_API_COMPRESSION_LEVEL (LIBAVCODEC_VERSION_MAJOR < 59)
+#endif
 
 
 #endif /* AVCODEC_VERSION_H */
diff --git a/libavcodec/zmbvenc.c b/libavcodec/zmbvenc.c
index d7b518d..7a01fe3 100644
--- a/libavcodec/zmbvenc.c
+++ b/libavcodec/zmbvenc.c
@@ -29,6 +29,8 @@
 
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/opt.h"
+
 #include "avcodec.h"
 #include "internal.h"
 
@@ -54,6 +56,7 @@ typedef struct ZmbvEncContext {
     int comp_size;
     int keyint, curfrm;
     z_stream zstream;
+    int compression_level;
 } ZmbvEncContext;
 
 static int score_tab[256];
@@ -274,7 +277,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     ZmbvEncContext * const c = avctx->priv_data;
     int zret; // Zlib return code
     int i;
-    int lvl = 9;
+    int lvl = c->compression_level;
 
     for(i=1; i<256; i++)
         score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * 
(256/M_LN2);
@@ -287,12 +290,18 @@ static av_cold int encode_init(AVCodecContext *avctx)
     if(avctx->me_range > 0)
         c->range = FFMIN(avctx->me_range, 127);
 
-    if(avctx->compression_level >= 0)
-        lvl = avctx->compression_level;
-    if(lvl < 0 || lvl > 9){
+#if FF_API_COMPRESSION_LEVEL
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (c->compression_level == 9)
+        c->compression_level = avctx->compression_level;
+    if (c->compression_level >= 0)
+        lvl = c->compression_level;
+    if (lvl < 0 || lvl > 9){
         av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not 
%i\n", lvl);
         return AVERROR(EINVAL);
     }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
     // Needed if zlib unused or init aborted before deflateInit
     memset(&c->zstream, 0, sizeof(z_stream));
@@ -335,6 +344,21 @@ static av_cold int encode_init(AVCodecContext *avctx)
     return 0;
 }
 
+#define OFFSET(x) offsetof(ZmbvEncContext, x)
+#define FLAGS     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, { 
.i64 = 9 }, 0, 9, FLAGS },
+
+    { NULL },
+};
+
+static const AVClass pngenc_class = {
+    .class_name = "PNG encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_zmbv_encoder = {
     .name           = "zmbv",
     .long_name      = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
-- 
1.9.5 (Apple Git-50.3)

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to