]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ratecontrol.c
qtrle: Properly use AVFrame API
[ffmpeg] / libavcodec / ratecontrol.c
index 620c854ad4cc2f149f2170129fa6628ec0ad8941..c0eac6daf4d60439da20d5deb0f0444870874211 100644 (file)
@@ -26,6 +26,8 @@
  */
 
 #include "libavutil/attributes.h"
+#include "libavutil/internal.h"
+
 #include "avcodec.h"
 #include "internal.h"
 #include "ratecontrol.h"
@@ -135,6 +137,13 @@ av_cold int ff_rate_control_init(MpegEncContext *s)
         return res;
     }
 
+#if FF_API_RC_STRATEGY
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (!s->rc_strategy)
+        s->rc_strategy = s->avctx->rc_strategy;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
     for (i = 0; i < 5; i++) {
         rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
         rcc->pred[i].count = 1.0;
@@ -150,7 +159,7 @@ av_cold int ff_rate_control_init(MpegEncContext *s)
     }
     rcc->buffer_index = s->avctx->rc_initial_buffer_occupancy;
 
-    if (s->avctx->flags & CODEC_FLAG_PASS2) {
+    if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
         int i;
         char *p;
 
@@ -163,6 +172,8 @@ av_cold int ff_rate_control_init(MpegEncContext *s)
             return -1;
         rcc->entry       = av_mallocz(i * sizeof(RateControlEntry));
         rcc->num_entries = i;
+        if (!rcc->entry)
+            return AVERROR(ENOMEM);
 
         /* init all to skipped p frames
          * (with b frames we might have a not encoded frame at the end FIXME) */
@@ -210,11 +221,13 @@ av_cold int ff_rate_control_init(MpegEncContext *s)
             p = next;
         }
 
-        if (init_pass2(s) < 0)
+        if (init_pass2(s) < 0) {
+            ff_rate_control_uninit(s);
             return -1;
+        }
 
         // FIXME maybe move to end
-        if ((s->avctx->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) {
+        if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == 1) {
 #if CONFIG_LIBXVID
             return ff_xvid_rate_control_init(s);
 #else
@@ -225,7 +238,7 @@ av_cold int ff_rate_control_init(MpegEncContext *s)
         }
     }
 
-    if (!(s->avctx->flags & CODEC_FLAG_PASS2)) {
+    if (!(s->avctx->flags & AV_CODEC_FLAG_PASS2)) {
         rcc->short_term_qsum   = 0.001;
         rcc->short_term_qcount = 0.001;
 
@@ -294,7 +307,7 @@ av_cold void ff_rate_control_uninit(MpegEncContext *s)
     av_freep(&rcc->entry);
 
 #if CONFIG_LIBXVID
-    if ((s->avctx->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
+    if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == 1)
         ff_xvid_rate_control_uninit(s);
 #endif
 }
@@ -744,8 +757,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
     emms_c();
 
 #if CONFIG_LIBXVID
-    if ((s->avctx->flags & CODEC_FLAG_PASS2) &&
-        s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
+    if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == 1)
         return ff_xvid_rate_estimate_qscale(s, dry_run);
 #endif
 
@@ -761,7 +773,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
                          sqrt(last_var), s->frame_bits);
     }
 
-    if (s->avctx->flags & CODEC_FLAG_PASS2) {
+    if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
         assert(picture_number >= 0);
         assert(picture_number < rcc->num_entries);
         rce         = &rcc->entry[picture_number];
@@ -792,7 +804,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
     var = pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum;
 
     short_term_q = 0; /* avoid warning */
-    if (s->avctx->flags & CODEC_FLAG_PASS2) {
+    if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
         if (pict_type != AV_PICTURE_TYPE_I)
             assert(pict_type == rce->new_pict_type);
 
@@ -932,6 +944,11 @@ static int init_pass2(MpegEncContext *s)
 
     qscale         = av_malloc(sizeof(double) * rcc->num_entries);
     blurred_qscale = av_malloc(sizeof(double) * rcc->num_entries);
+    if (!qscale || !blurred_qscale) {
+        av_free(qscale);
+        av_free(blurred_qscale);
+        return AVERROR(ENOMEM);
+    }
     toobig = 0;
 
     for (step = 256 * 256; step > 0.0000001; step *= 0.5) {