[FFmpeg-devel] [PATCH] make packet_size in AVFormatContext unsigned

Ronald S. Bultje rsbultje
Wed Jun 17 15:45:01 CEST 2009


On Tue, Jun 16, 2009 at 9:07 PM, Ronald S. Bultje <rsbultje at gmail.com>wrote:

> Arent you maintainer?or maybe you have the spec handy w/o me having to pay
> for it? :)

 Oh boy, a spec. How exciting. Let's do my reading.

"The PES packet data from elementary stream n is passed to the input buffer
for stream n, Bn. Transfer of byte i from the
system target decoder input to Bn is instantaneous, so that byte i enters
the buffer for stream n, of size BSn, at time t(i).
Bytes present in the pack header, system headers, Program Stream Maps,
Program Stream Directories, or PES packet
headers of the Program Stream such as SCR, DTS, PTS, and packet_length
fields, are not delivered to any of the
buffers, but may be used to control the system."

and then:

"The input buffer sizes BS1 through BSn are given by the P-STD buffer size
parameter in the syntax in equations 2-16
and 2-17." (p.53,

So there is a max packet size, which is minus the packet header, but is
stream-dependent. That's not what we do. But anyway, let's see what the max
value for BSn is.

"P-STD_buffer_size ? The P-STD_buffer_size is a 13-bit unsigned integer, the
meaning of which is only defined if this
PES packet is contained in a Program Stream. It defines the size of the
input buffer, BSn, in the P-STD. If
P-STD_buffer_scale has the value '0', then the P-STD_buffer_size measures
the buffer size in units of 128 bytes. If
P-STD_buffer_scale has the value '1', then the P-STD_buffer_size measures
the buffer size in units of 1024 bytes. Thus:
if (P-STD_buffer_scale == 0)
    BSn = P-STD_buffer_size x 128 (2-16)
    BSn = P-STD_buffer_size x 1024 (2-17)" (p. 40)

so the max buffer size, and thus the max size of the packet data, should fit
10+13 bytes, i.e. 1<<23 = 8MB (ok, ok, +10 bytes for the packet header). Am
I close here? Then the minimum size required is the size of the packet
header, i.e. 10 bytes, and mpegenc.c makes assumptions that it is at least
10 bytes more than this, so 20 bytes.

Attached patch OK?

-------------- next part --------------
Index: ffmpeg-svn/libavformat/mpegenc.c
--- ffmpeg-svn.orig/libavformat/mpegenc.c	2009-06-16 18:06:50.000000000 -0400
+++ ffmpeg-svn/libavformat/mpegenc.c	2009-06-17 09:44:14.000000000 -0400
@@ -304,9 +304,14 @@
                    (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer));
     s->is_dvd =    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &mpeg2dvd_muxer);
-    if(ctx->packet_size)
+    if(ctx->packet_size) {
+        if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
+                   ctx->packet_size);
+            goto fail;
+        }
         s->packet_size = ctx->packet_size;
-    else
+    } else
         s->packet_size = 2048;
     s->vcd_padding_bytes_written = 0;

More information about the ffmpeg-devel mailing list