Insert null packets to keep pcr values correct, patch by Mike Scheutzow,

scheutzow at alcatel-lucent dot com and Niobos, niobos at dest-unreach dot be.

Originally committed as revision 21199 to svn://svn.ffmpeg.org/ffmpeg/trunk
release/0.6
Mike Scheutzow 15 years ago committed by Baptiste Coudurier
parent 49f6fd8f3f
commit 4df3bbbc68
  1. 59
      libavformat/mpegtsenc.c
  2. 4
      tests/lavf.regression.ref
  3. 52
      tests/seek.regression.ref

@ -523,6 +523,55 @@ static void retransmit_si_info(AVFormatContext *s)
}
}
/* Write a single null transport stream packet */
static void mpegts_insert_null_packet(AVFormatContext *s)
{
MpegTSWrite *ts = s->priv_data;
uint8_t *q;
uint8_t buf[TS_PACKET_SIZE];
q = buf;
*q++ = 0x47;
*q++ = 0x00 | 0x1f;
*q++ = 0xff;
*q++ = 0x10;
memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
put_buffer(s->pb, buf, TS_PACKET_SIZE);
ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
}
/* Write a single transport stream packet with a PCR and no payload */
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
{
MpegTSWrite *ts = s->priv_data;
MpegTSWriteStream *ts_st = st->priv_data;
uint8_t *q;
uint64_t pcr = ts->cur_pcr;
uint8_t buf[TS_PACKET_SIZE];
q = buf;
*q++ = 0x47;
*q++ = ts_st->pid >> 8;
*q++ = ts_st->pid;
*q++ = 0x20 | ts_st->cc; /* Adaptation only */
/* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
*q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
*q++ = 0x10; /* Adaptation flags: PCR present */
/* PCR coded into 6 bytes */
*q++ = pcr >> 25;
*q++ = pcr >> 17;
*q++ = pcr >> 9;
*q++ = pcr >> 1;
*q++ = (pcr & 1) << 7;
*q++ = 0;
/* stuffing bytes */
memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
put_buffer(s->pb, buf, TS_PACKET_SIZE);
ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
}
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
{
int val;
@ -549,6 +598,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
int val, is_start, len, header_len, write_pcr, private_code, flags;
int afc_len, stuffing_len;
int64_t pcr = -1; /* avoid warning */
int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
is_start = 1;
while (payload_size > 0) {
@ -564,6 +614,15 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
}
}
if (dts != AV_NOPTS_VALUE && (dts - (int64_t)ts->cur_pcr) > delay) {
/* pcr insert gets priority over null packet insert */
if (write_pcr)
mpegts_insert_pcr_only(s, st);
else
mpegts_insert_null_packet(s);
continue; /* recalculate write_pcr and possibly retransmit si_info */
}
/* prepare packet header */
q = buf;
*q++ = 0x47;

@ -15,8 +15,8 @@ e36a005ecca9add8bda6e0655d03716f *./tests/data/b-lavf.mpg
b3174e2db508564c1cce0b5e3c1bc1bd *./tests/data/b-lavf.mxf_d10
5330989 ./tests/data/b-lavf.mxf_d10
./tests/data/b-lavf.mxf_d10 CRC=0xc3f4f92e
5db794c5f69f934bbcdbe32de1df9f3a *./tests/data/b-lavf.ts
436160 ./tests/data/b-lavf.ts
da0d4fea54e7473e3cddb1607294c9e2 *./tests/data/b-lavf.ts
430144 ./tests/data/b-lavf.ts
./tests/data/b-lavf.ts CRC=0x133216c1
62c5aeb636fc82cf6ba6277d36e42cb5 *./tests/data/b-lavf.swf
329479 ./tests/data/b-lavf.swf

@ -3599,57 +3599,57 @@ ret:-1 st:-1 flags:1 ts:-0.645825
tests/data/b-lavf.ts
ret: 0 st: 0 flags:1 dts: 0.660000 pts: 0.700000 pos: 564 size: 24801
ret: 0 st:-1 flags:0 ts:-1.000000
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st:-1 flags:1 ts: 1.894167
ret: 0 st: 0 flags:0 dts: 0.820000 pts: 0.860000 pos: 80088 size: 13388
ret: 0 st: 0 flags:0 dts: 0.820000 pts: 0.860000 pos: 78960 size: 13388
ret: 0 st: 0 flags:0 ts: 0.788333
ret: 0 st: 0 flags:0 dts: 0.740000 pts: 0.780000 pos: 48316 size: 14502
ret: 0 st: 0 flags:0 dts: 0.740000 pts: 0.780000 pos: 47564 size: 14502
ret: 0 st: 0 flags:1 ts:-0.317500
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:1 dts: 0.660000 pts: 0.700000 pos: 564 size: 24801
ret: 0 st: 1 flags:0 ts: 2.576667
ret: 0 st: 0 flags:0 dts: 0.900000 pts: 0.940000 pos: 110544 size: 12781
ret: 0 st: 0 flags:0 dts: 0.900000 pts: 0.940000 pos: 109416 size: 12781
ret: 0 st: 1 flags:1 ts: 1.470833
ret: 0 st: 0 flags:0 dts: 0.780000 pts: 0.820000 pos: 65236 size: 12623
ret: 0 st: 0 flags:0 dts: 0.780000 pts: 0.820000 pos: 64108 size: 12623
ret: 0 st:-1 flags:0 ts: 0.365002
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st:-1 flags:1 ts:-0.740831
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:1 dts: 0.660000 pts: 0.700000 pos: 564 size: 24801
ret: 0 st: 0 flags:0 ts: 2.153333
ret: 0 st: 0 flags:0 dts: 0.860000 pts: 0.900000 pos: 95128 size: 13086
ret: 0 st: 0 flags:0 dts: 0.860000 pts: 0.900000 pos: 94188 size: 13086
ret: 0 st: 0 flags:1 ts: 1.047500
ret: 0 st: 0 flags:0 dts: 0.740000 pts: 0.780000 pos: 48316 size: 14502
ret: 0 st: 0 flags:0 dts: 0.740000 pts: 0.780000 pos: 47564 size: 14502
ret: 0 st: 1 flags:0 ts:-0.058333
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st: 1 flags:1 ts: 2.835833
ret: 0 st: 0 flags:0 dts: 0.900000 pts: 0.940000 pos: 110544 size: 12781
ret: 0 st: 0 flags:0 dts: 0.940000 pts: 0.980000 pos: 124080 size: 11944
ret: 0 st:-1 flags:0 ts: 1.730004
ret: 0 st: 0 flags:0 dts: 0.780000 pts: 0.820000 pos: 65236 size: 12623
ret: 0 st: 0 flags:0 dts: 0.820000 pts: 0.860000 pos: 78960 size: 13388
ret: 0 st:-1 flags:1 ts: 0.624171
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st: 0 flags:0 ts:-0.481667
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st: 0 flags:1 ts: 2.412500
ret: 0 st: 0 flags:0 dts: 0.860000 pts: 0.900000 pos: 95128 size: 13086
ret: 0 st: 0 flags:0 dts: 0.900000 pts: 0.940000 pos: 109416 size: 12781
ret: 0 st: 1 flags:0 ts: 1.306667
ret: 0 st: 0 flags:0 dts: 0.780000 pts: 0.820000 pos: 65236 size: 12623
ret: 0 st: 0 flags:0 dts: 0.780000 pts: 0.820000 pos: 64108 size: 12623
ret: 0 st: 1 flags:1 ts: 0.200844
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st:-1 flags:0 ts:-0.904994
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st:-1 flags:1 ts: 1.989173
ret: 0 st: 0 flags:0 dts: 0.820000 pts: 0.860000 pos: 80088 size: 13388
ret: 0 st: 0 flags:0 dts: 0.860000 pts: 0.900000 pos: 94188 size: 13086
ret: 0 st: 0 flags:0 ts: 0.883344
ret: 0 st: 0 flags:0 dts: 0.740000 pts: 0.780000 pos: 48316 size: 14502
ret: 0 st: 0 flags:0 dts: 0.740000 pts: 0.780000 pos: 47564 size: 14502
ret: 0 st: 0 flags:1 ts:-0.222489
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:1 dts: 0.660000 pts: 0.700000 pos: 564 size: 24801
ret: 0 st: 1 flags:0 ts: 2.671678
ret: 0 st: 0 flags:0 dts: 0.900000 pts: 0.940000 pos: 110544 size: 12781
ret: 0 st: 0 flags:0 dts: 0.900000 pts: 0.940000 pos: 109416 size: 12781
ret: 0 st: 1 flags:1 ts: 1.565844
ret: 0 st: 0 flags:0 dts: 0.780000 pts: 0.820000 pos: 65236 size: 12623
ret: 0 st: 0 flags:0 dts: 0.780000 pts: 0.820000 pos: 64108 size: 12623
ret: 0 st:-1 flags:0 ts: 0.460008
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 28764 size: 16433
ret: 0 st:-1 flags:1 ts:-0.645825
ret: 0 st: 0 flags:0 dts: 0.700000 pts: 0.740000 pos: 29140 size: 16433
ret: 0 st: 0 flags:1 dts: 0.660000 pts: 0.700000 pos: 564 size: 24801
----------------
tests/data/b-lavf.ul
ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 0 size: 1024

Loading…
Cancel
Save