]> git.sesse.net Git - ffmpeg/commitdiff
vc2enc: clip and warn when user bitrate set too low
authorRostislav Pehlivanov <atomnuker@gmail.com>
Sat, 27 Feb 2016 18:38:09 +0000 (18:38 +0000)
committerRostislav Pehlivanov <atomnuker@gmail.com>
Sun, 28 Feb 2016 19:06:29 +0000 (19:06 +0000)
The encoder crashed on verly low bitrates since there wasn't enough
space allocated.

Signed-off-by: Rostislav Pehlivanov <atomnuker@gmail.com>
libavcodec/vc2enc.c

index 5e11aa1e43d6f7d9b839506ec3c1f8aaa376bf8d..27db6c0194e53f717715faef00187a7df38feb14 100644 (file)
@@ -1008,12 +1008,24 @@ static av_cold int vc2_encode_end(AVCodecContext *avctx)
     return 0;
 }
 
+static int minimum_frame_bits(VC2EncContext *s)
+{
+    int slice_x, slice_y, bits = 0;
+    s->size_scaler = 64;
+    for (slice_y = 0; slice_y < s->num_y; slice_y++) {
+        for (slice_x = 0; slice_x < s->num_x; slice_x++) {
+            bits += count_hq_slice(s, NULL, NULL, slice_x, slice_y, s->q_ceil);
+        }
+    }
+    return bits;
+}
 
 static av_cold int vc2_encode_init(AVCodecContext *avctx)
 {
     Plane *p;
     SubBand *b;
     int i, j, level, o, shift;
+    int64_t bits_per_frame, min_bits_per_frame;
     VC2EncContext *s = avctx->priv_data;
 
     s->picture_number = 0;
@@ -1161,6 +1173,17 @@ static av_cold int vc2_encode_init(AVCodecContext *avctx)
         }
     }
 
+    bits_per_frame = av_rescale(avctx->bit_rate, avctx->time_base.num,
+                                 avctx->time_base.den);
+    min_bits_per_frame = minimum_frame_bits(s) + 8*sizeof(LIBAVCODEC_IDENT) + 8*40 + 8*20000;
+    if (bits_per_frame < min_bits_per_frame) {
+        avctx->bit_rate = av_rescale(min_bits_per_frame, avctx->time_base.den,
+                                     avctx->time_base.num);
+        av_log(avctx, AV_LOG_WARNING,
+               "Bitrate too low, clipping to minimum = %.2lf Mbps!\n",
+               (double)avctx->bit_rate/1000000.0f);
+    }
+
     return 0;
 
 alloc_fail: