Commit 66be80d6 authored by Tomas Härdin's avatar Tomas Härdin

mpegtsenc: Improve PCR generation and output

This fixes PCR drift due to accumulating TS_PACKET_SIZE*8*90000LL/ts->mux_rate each packet, due to rounding errors when mux_rate does not evenly divide 135360000.
This patch also increases the PCR precision to 27 MHz from 90 kHz and takes the location of the PCR data into account (+11 bytes according to the spec).

Originally committed as revision 25864 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 77b7c334
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include "mpegts.h" #include "mpegts.h"
#include "adts.h" #include "adts.h"
#define PCR_TIME_BASE 27000000
/* write DVB SI sections */ /* write DVB SI sections */
/*********************************************/ /*********************************************/
...@@ -60,7 +62,7 @@ typedef struct MpegTSWrite { ...@@ -60,7 +62,7 @@ typedef struct MpegTSWrite {
int nb_services; int nb_services;
int onid; int onid;
int tsid; int tsid;
uint64_t cur_pcr; int64_t first_pcr;
int mux_rate; ///< set to 1 when VBR int mux_rate; ///< set to 1 when VBR
} MpegTSWrite; } MpegTSWrite;
...@@ -109,8 +111,6 @@ static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len) ...@@ -109,8 +111,6 @@ static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
buf_ptr += len1; buf_ptr += len1;
len -= len1; len -= len1;
ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
} }
} }
...@@ -489,7 +489,7 @@ static int mpegts_write_header(AVFormatContext *s) ...@@ -489,7 +489,7 @@ static int mpegts_write_header(AVFormatContext *s)
ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) / ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
(TS_PACKET_SIZE * 8 * 1000); (TS_PACKET_SIZE * 8 * 1000);
ts->cur_pcr = av_rescale(s->max_delay, 90000, AV_TIME_BASE); ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
} else { } else {
/* Arbitrary values, PAT/PMT could be written on key frames */ /* Arbitrary values, PAT/PMT could be written on key frames */
ts->sdt_packet_period = 200; ts->sdt_packet_period = 200;
...@@ -556,6 +556,26 @@ static void retransmit_si_info(AVFormatContext *s) ...@@ -556,6 +556,26 @@ static void retransmit_si_info(AVFormatContext *s)
} }
} }
static int64_t get_pcr(const MpegTSWrite *ts, ByteIOContext *pb)
{
return av_rescale(url_ftell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
ts->first_pcr;
}
static uint8_t* write_pcr_bits(uint8_t *buf, int64_t pcr)
{
int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
*buf++ = pcr_high >> 25;
*buf++ = pcr_high >> 17;
*buf++ = pcr_high >> 9;
*buf++ = pcr_high >> 1;
*buf++ = ((pcr_high & 1) << 7) | (pcr_low >> 8);
*buf++ = pcr_low;
return buf;
}
/* Write a single null transport stream packet */ /* Write a single null transport stream packet */
static void mpegts_insert_null_packet(AVFormatContext *s) static void mpegts_insert_null_packet(AVFormatContext *s)
{ {
...@@ -570,7 +590,6 @@ static void mpegts_insert_null_packet(AVFormatContext *s) ...@@ -570,7 +590,6 @@ static void mpegts_insert_null_packet(AVFormatContext *s)
*q++ = 0x10; *q++ = 0x10;
memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf)); memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
put_buffer(s->pb, buf, TS_PACKET_SIZE); 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 */ /* Write a single transport stream packet with a PCR and no payload */
...@@ -579,7 +598,6 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) ...@@ -579,7 +598,6 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
MpegTSWrite *ts = s->priv_data; MpegTSWrite *ts = s->priv_data;
MpegTSWriteStream *ts_st = st->priv_data; MpegTSWriteStream *ts_st = st->priv_data;
uint8_t *q; uint8_t *q;
uint64_t pcr = ts->cur_pcr;
uint8_t buf[TS_PACKET_SIZE]; uint8_t buf[TS_PACKET_SIZE];
q = buf; q = buf;
...@@ -592,17 +610,11 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) ...@@ -592,17 +610,11 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
*q++ = 0x10; /* Adaptation flags: PCR present */ *q++ = 0x10; /* Adaptation flags: PCR present */
/* PCR coded into 6 bytes */ /* PCR coded into 6 bytes */
*q++ = pcr >> 25; q = write_pcr_bits(q, get_pcr(ts, s->pb));
*q++ = pcr >> 17;
*q++ = pcr >> 9;
*q++ = pcr >> 1;
*q++ = (pcr & 1) << 7;
*q++ = 0;
/* stuffing bytes */ /* stuffing bytes */
memset(q, 0xFF, TS_PACKET_SIZE - (q - buf)); memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
put_buffer(s->pb, buf, TS_PACKET_SIZE); 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) static void write_pts(uint8_t *q, int fourbits, int64_t pts)
...@@ -653,7 +665,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, ...@@ -653,7 +665,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
} }
if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE && if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
(dts - (int64_t)ts->cur_pcr) > delay) { (dts - get_pcr(ts, s->pb)/300) > delay) {
/* pcr insert gets priority over null packet insert */ /* pcr insert gets priority over null packet insert */
if (write_pcr) if (write_pcr)
mpegts_insert_pcr_only(s, st); mpegts_insert_pcr_only(s, st);
...@@ -675,19 +687,14 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, ...@@ -675,19 +687,14 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
if (write_pcr) { if (write_pcr) {
// add 11, pcr references the last byte of program clock reference base // add 11, pcr references the last byte of program clock reference base
if (ts->mux_rate > 1) if (ts->mux_rate > 1)
pcr = ts->cur_pcr + (4+7)*8*90000LL / ts->mux_rate; pcr = get_pcr(ts, s->pb);
else else
pcr = dts - delay; pcr = (dts - delay)*300;
if (dts != AV_NOPTS_VALUE && dts < pcr) if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n"); av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
*q++ = 7; /* AFC length */ *q++ = 7; /* AFC length */
*q++ = 0x10; /* flags: PCR present */ *q++ = 0x10; /* flags: PCR present */
*q++ = pcr >> 25; q = write_pcr_bits(q, pcr);
*q++ = pcr >> 17;
*q++ = pcr >> 9;
*q++ = pcr >> 1;
*q++ = (pcr & 1) << 7;
*q++ = 0;
} }
if (is_start) { if (is_start) {
int pes_extension = 0; int pes_extension = 0;
...@@ -802,7 +809,6 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, ...@@ -802,7 +809,6 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
payload += len; payload += len;
payload_size -= len; payload_size -= len;
put_buffer(s->pb, buf, TS_PACKET_SIZE); put_buffer(s->pb, buf, TS_PACKET_SIZE);
ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
} }
put_flush_packet(s->pb); put_flush_packet(s->pb);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment