[FFmpeg-devel] [PATCH] Modified to generate PAT/PMT for video keyframes

pkoshevoy at gmail.com pkoshevoy at gmail.com
Sat Feb 11 02:19:08 CET 2012


From: Pavel Koshevoy <pkoshevoy at gmail.com>

This is so that TS fragments produced by
http://code.google.com/p/httpsegmenter/
would be compatible with JW Player.

A new member variable prev_payload_key was added to MpegTSWriteStream
to help detect transition from non-key to key frame, so that
PAT/PMT would not be produced for every keyframe in intra-only videos.

Signed-off-by: Pavel Koshevoy <pkoshevoy at gmail.com>
---
 libavformat/mpegtsenc.c |   12 ++++++++----
 tests/ref/lavf/ts       |    4 ++--
 tests/ref/seek/lavf_ts  |   10 +++++-----
 3 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index a8c5c5c..4b303de 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -211,6 +211,7 @@ typedef struct MpegTSWriteStream {
     int cc;
     int payload_size;
     int first_pts_check; ///< first pts check needed
+    int prev_payload_key;
     int64_t payload_pts;
     int64_t payload_dts;
     int payload_flags;
@@ -589,7 +590,7 @@ static int mpegts_write_header(AVFormatContext *s)
 
         ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
     } else {
-        /* Arbitrary values, PAT/PMT could be written on key frames */
+        /* Arbitrary values, PAT/PMT will also be written on video key frames */
         ts->sdt_packet_period = 200;
         ts->pat_packet_period = 40;
         if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
@@ -650,7 +651,7 @@ static int mpegts_write_header(AVFormatContext *s)
 }
 
 /* send SDT, PAT and PMT tables regulary */
-static void retransmit_si_info(AVFormatContext *s)
+static void retransmit_si_info(AVFormatContext *s, int force_pat)
 {
     MpegTSWrite *ts = s->priv_data;
     int i;
@@ -659,7 +660,7 @@ static void retransmit_si_info(AVFormatContext *s)
         ts->sdt_packet_count = 0;
         mpegts_write_sdt(s);
     }
-    if (++ts->pat_packet_count == ts->pat_packet_period) {
+    if (++ts->pat_packet_count == ts->pat_packet_period || force_pat) {
         ts->pat_packet_count = 0;
         mpegts_write_pat(s);
         for(i = 0; i < ts->nb_services; i++) {
@@ -788,10 +789,12 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
     int afc_len, stuffing_len;
     int64_t pcr = -1; /* avoid warning */
     int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
+    int force_pat = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
 
     is_start = 1;
     while (payload_size > 0) {
-        retransmit_si_info(s);
+        retransmit_si_info(s, force_pat);
+        force_pat = 0;
 
         write_pcr = 0;
         if (ts_st->pid == ts_st->service->pcr_pid) {
@@ -961,6 +964,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
         avio_write(s->pb, buf, TS_PACKET_SIZE);
     }
     avio_flush(s->pb);
+    ts_st->prev_payload_key = key;
 }
 
 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
diff --git a/tests/ref/lavf/ts b/tests/ref/lavf/ts
index 81415b9..18566c4 100644
--- a/tests/ref/lavf/ts
+++ b/tests/ref/lavf/ts
@@ -1,3 +1,3 @@
-8f61dad7652abbab9e49fca274dabf30 *./tests/data/lavf/lavf.ts
-406644 ./tests/data/lavf/lavf.ts
+34f95a300355d474767b436430eba15b *./tests/data/lavf/lavf.ts
+  406644 ./tests/data/lavf/lavf.ts
 ./tests/data/lavf/lavf.ts CRC=0x133216c1
diff --git a/tests/ref/seek/lavf_ts b/tests/ref/seek/lavf_ts
index 0bc7a52..690228d 100644
--- a/tests/ref/seek/lavf_ts
+++ b/tests/ref/seek/lavf_ts
@@ -8,7 +8,7 @@ ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 0 flags:1  ts:-0.317500
 ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 1 flags:0  ts: 2.576667
-ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 403636 size:   209
+ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 404012 size:   209
 ret: 0         st: 1 flags:1  ts: 1.470833
 ret: 0         st: 1 flags:1 dts: 1.400000 pts: 1.400000 pos: 172584 size:   208
 ret: 0         st:-1 flags:0  ts: 0.365002
@@ -16,13 +16,13 @@ ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st:-1 flags:1  ts:-0.740831
 ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 0 flags:0  ts: 2.153333
-ret: 0         st: 0 flags:0 dts: 2.160000 pts: 2.200000 pos: 325240 size: 12679
+ret: 0         st: 0 flags:0 dts: 2.160000 pts: 2.200000 pos: 325616 size: 12679
 ret: 0         st: 0 flags:1  ts: 1.047500
 ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 1 flags:0  ts:-0.058333
 ret: 0         st: 1 flags:1 dts: 1.400000 pts: 1.400000 pos: 172584 size:   208
 ret: 0         st: 1 flags:1  ts: 2.835833
-ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 403636 size:   209
+ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 404012 size:   209
 ret: 0         st:-1 flags:0  ts: 1.730004
 ret: 0         st: 1 flags:1 dts: 1.400000 pts: 1.400000 pos: 172584 size:   208
 ret: 0         st:-1 flags:1  ts: 0.624171
@@ -30,7 +30,7 @@ ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 0 flags:0  ts:-0.481667
 ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 0 flags:1  ts: 2.412500
-ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 403636 size:   209
+ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 404012 size:   209
 ret: 0         st: 1 flags:0  ts: 1.306667
 ret: 0         st: 1 flags:1 dts: 1.400000 pts: 1.400000 pos: 172584 size:   208
 ret: 0         st: 1 flags:1  ts: 0.200844
@@ -44,7 +44,7 @@ ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 0 flags:1  ts:-0.222489
 ret: 0         st: 0 flags:1 dts: 1.360000 pts: 1.400000 pos:    564 size: 24801
 ret: 0         st: 1 flags:0  ts: 2.671678
-ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 403636 size:   209
+ret: 0         st: 1 flags:1 dts: 2.131433 pts: 2.131433 pos: 404012 size:   209
 ret: 0         st: 1 flags:1  ts: 1.565844
 ret: 0         st: 1 flags:1 dts: 1.400000 pts: 1.400000 pos: 172584 size:   208
 ret: 0         st:-1 flags:0  ts: 0.460008
-- 
1.7.9



More information about the ffmpeg-devel mailing list