changeset 1697:f3287b5d8d9f libavcodec

vbv_delay
author michael
date Wed, 17 Dec 2003 19:53:05 +0000
parents f5af91b8be17
children 420cac032278
files mpeg12.c mpegvideo.c mpegvideo.h ratecontrol.c
diffstat 4 files changed, 43 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mpeg12.c	Tue Dec 16 11:17:06 2003 +0000
+++ b/mpeg12.c	Wed Dec 17 19:53:05 2003 +0000
@@ -408,7 +408,9 @@
     s->fake_picture_number++;
     
     put_bits(&s->pb, 3, s->pict_type);
-    put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
+
+    s->vbv_delay_ptr= s->pb.buf + get_bit_count(&s->pb)/8;
+    put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
     
     // RAL: Forward f_code also needed for B frames
     if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
@@ -1758,7 +1760,7 @@
 {
     Mpeg1Context *s1 = avctx->priv_data;
     MpegEncContext *s = &s1->mpeg_enc_ctx;
-    int ref, f_code;
+    int ref, f_code, vbv_delay;
 
     init_get_bits(&s->gb, buf, buf_size*8);
 
@@ -1766,7 +1768,7 @@
     s->pict_type = get_bits(&s->gb, 3);
     dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
 
-    skip_bits(&s->gb, 16);
+    vbv_delay= get_bits(&s->gb, 16);
     if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
         s->full_pel[0] = get_bits1(&s->gb);
         f_code = get_bits(&s->gb, 3);
--- a/mpegvideo.c	Tue Dec 16 11:17:06 2003 +0000
+++ b/mpegvideo.c	Wed Dec 17 19:53:05 2003 +0000
@@ -1865,7 +1865,23 @@
         flush_put_bits(&s->pb);
         s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
     }
-    
+
+    /* update mpeg1/2 vbv_delay for CBR */    
+    if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate){
+        int vbv_delay;
+
+        assert(s->repeat_first_field==0 && s->avctx->repeat_pic==0);
+        
+        vbv_delay= lrint(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
+        assert(vbv_delay < 0xFFFF);
+
+        s->vbv_delay_ptr[0] &= 0xF8;
+        s->vbv_delay_ptr[0] |= vbv_delay>>13;
+        s->vbv_delay_ptr[1]  = vbv_delay>>5;
+        s->vbv_delay_ptr[2] &= 0x07;
+        s->vbv_delay_ptr[2] |= vbv_delay<<3;
+    }
+
     s->total_bits += s->frame_bits;
     avctx->frame_bits  = s->frame_bits;
     
--- a/mpegvideo.h	Tue Dec 16 11:17:06 2003 +0000
+++ b/mpegvideo.h	Wed Dec 17 19:53:05 2003 +0000
@@ -605,6 +605,7 @@
     int gop_picture_number;  ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific 
     int last_mv_dir;         ///< last mv_dir, used for b frame encoding 
     int broken_link;         ///< no_output_of_prior_pics_flag
+    uint8_t *vbv_delay_ptr;  ///< pointer to vbv_delay in the bitstream 
     
     /* MPEG2 specific - I wish I had not to support this mess. */
     int progressive_sequence;
--- a/ratecontrol.c	Tue Dec 16 11:17:06 2003 +0000
+++ b/ratecontrol.c	Wed Dec 17 19:53:05 2003 +0000
@@ -198,11 +198,11 @@
 int ff_vbv_update(MpegEncContext *s, int frame_size){
     RateControlContext *rcc= &s->rc_context;
     const double fps= (double)s->avctx->frame_rate / (double)s->avctx->frame_rate_base;
-    const double buffer_size= s->avctx->rc_buffer_size;
+    const int buffer_size= s->avctx->rc_buffer_size;
     const double min_rate= s->avctx->rc_min_rate/fps;
     const double max_rate= s->avctx->rc_max_rate/fps;
-
-//printf("%f %f %d %f %f\n", buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
+    
+//printf("%d %f %d %f %f\n", buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
     if(buffer_size){
         int left;
 
@@ -215,8 +215,8 @@
         left= buffer_size - rcc->buffer_index - 1;
         rcc->buffer_index += clip(left, min_rate, max_rate);
 
-        if(rcc->buffer_index > s->avctx->rc_buffer_size){
-            int stuffing= ceil((rcc->buffer_index - s->avctx->rc_buffer_size)/8);
+        if(rcc->buffer_index > buffer_size){
+            int stuffing= ceil((rcc->buffer_index - buffer_size)/8);
             
             if(stuffing < 4 && s->codec_id == CODEC_ID_MPEG4)
                 stuffing=4;
@@ -413,6 +413,7 @@
     /* buffer overflow/underflow protection */
     if(buffer_size){
         double expected_size= rcc->buffer_index;
+        double q_limit;
 
         if(min_rate){
             double d= 2*(buffer_size - expected_size)/buffer_size;
@@ -420,7 +421,13 @@
             else if(d<0.0001) d=0.0001;
             q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
 
-            q= FFMIN(q, bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index)*3, 1)));
+            q_limit= bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index)*3, 1));
+            if(q > q_limit){
+                if(s->avctx->debug&FF_DEBUG_RC){
+                    av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit);
+                }
+                q= q_limit;
+            }
         }
 
         if(max_rate){
@@ -429,7 +436,13 @@
             else if(d<0.0001) d=0.0001;
             q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
 
-            q= FFMAX(q, bits2qp(rce, FFMAX(rcc->buffer_index/3, 1)));
+            q_limit= bits2qp(rce, FFMAX(rcc->buffer_index/3, 1));
+            if(q < q_limit){
+                if(s->avctx->debug&FF_DEBUG_RC){
+                    av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit);
+                }
+                q= q_limit;
+            }
         }
     }
 //printf("q:%f max:%f min:%f size:%f index:%d bits:%f agr:%f\n", q,max_rate, min_rate, buffer_size, rcc->buffer_index, bits, s->avctx->rc_buffer_aggressivity);