[FFmpeg-devel] [PATCH] Fix bit_rate in MPEG1/2 Video

Hee Suk Jung heesuk.jung at lge.com
Fri Nov 2 16:41:54 CET 2012


Hi,

I review ISO/IEC 12818-2 to clarify bit rate in MPEG1/2 video.
And make patch as below.
(If below patch is corrupted, please use attached patch file.)

Thanks,

>From d8d05eb48342bed9baa88d970d747651244d879b Mon Sep 17 00:00:00 2001
From: Heesuk Jung <heesuk.jung at lge.com>
Date: Fri, 2 Nov 2012 08:25:03 -0700
Subject: [PATCH] Fix bit_rate in MPEG1/2 Video

In ISO/IEC 13818-2, bit rate is differently determined according to video type

1) MPEG1 Video
 Bit_rate and vbv_delay are set to 3FFFF and FFFF respectively
 to indicate variable bitrate. Other values are for constant bitrate.
 VBV is only defined for constant bit rate operation.
 Ths STD supersedes the VBV model for vbr.

2) MPEG2 Video
 Even if the bitrate is constant, the value of bit_rate may not be the actual bitrate
 since bit_rate need only be an upper bound to that actual bitrate.
 VBV is only defined for variable bit rate operation.
 Constant bit rate is viewed as a special case of vbr.
---
 libavcodec/mpeg12.c           |    9 +++++++--
 libavcodec/mpegvideo.h        |    1 +
 libavcodec/mpegvideo_parser.c |    9 +++++++--
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index bdb6b00..b5bf68d 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -1278,8 +1278,12 @@ static int mpeg_decode_postinit(AVCodecContext *avctx)
             return -2;

         avcodec_set_dimensions(avctx, s->width, s->height);
-        if (s->bit_rate && s->bit_rate != 0x3FFFF*400)
-            avctx->bit_rate          = s->bit_rate;
+        if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
+            avctx->rc_max_rate = s->bit_rate;
+        } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
+                   (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
+            avctx->bit_rate = s->bit_rate;
+        }
         s1->save_aspect_info     = s->aspect_ratio_info;
         s1->save_width           = s->width;
         s1->save_height          = s->height;
@@ -1378,6 +1382,7 @@ static int mpeg1_decode_picture(AVCodecContext *avctx,
         return -1;

     vbv_delay = get_bits(&s->gb, 16);
+    s->vbv_delay = vbv_delay;
     if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
         s->full_pel[0] = get_bits1(&s->gb);
         f_code = get_bits(&s->gb, 3);
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 61cc3e1..b04f380 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -343,6 +343,7 @@ typedef struct MpegEncContext {
     int dquant;                 ///< qscale difference to prev qscale
     int closed_gop;             ///< MPEG1/2 GOP is closed
     int pict_type;              ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
+    int vbv_delay;
     int last_pict_type; //FIXME removes
     int last_non_b_pict_type;   ///< used for mpeg4 gmc b-frames & ratecontrol
     int dropable;
diff --git a/libavcodec/mpegvideo_parser.c b/libavcodec/mpegvideo_parser.c
index 5fb2799..c112a94 100644
--- a/libavcodec/mpegvideo_parser.c
+++ b/libavcodec/mpegvideo_parser.c
@@ -44,6 +44,7 @@ static void mpegvideo_extract_headers(AVCodecParserContext *s,
     int horiz_size_ext, vert_size_ext, bit_rate_ext;
     int did_set_size=0;
     int bit_rate = 0;
+    int vbv_delay = 0;
 //FIXME replace the crap with get_bits()
     s->repeat_pict = 0;

@@ -55,6 +56,7 @@ static void mpegvideo_extract_headers(AVCodecParserContext *s,
         case PICTURE_START_CODE:
             if (bytes_left >= 2) {
                 s->pict_type = (buf[1] >> 3) & 7;
+                vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3]  >> 3);
             }
             break;
         case SEQ_START_CODE:
@@ -131,8 +133,11 @@ static void mpegvideo_extract_headers(AVCodecParserContext *s,
         }
     }
  the_end: ;
-    if (bit_rate && bit_rate != 0x3FFFF) {
-        avctx->bit_rate = 400 * bit_rate;
+    if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate) {
+        avctx->rc_max_rate = 400*bit_rate;
+    } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate &&
+               (bit_rate != 0x3FFFF || vbv_delay != 0xFFFF)) {
+        avctx->bit_rate = 400*bit_rate;
     }
 }

--
1.7.9.5
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Fix-bit_rate-in-MPEG1-2-Video.patch
Type: application/octet-stream
Size: 4136 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121103/bedbb3d0/attachment.obj>


More information about the ffmpeg-devel mailing list