summaryrefslogtreecommitdiffstats
path: root/libavformat
diff options
context:
space:
mode:
authorStefano Sabatini <stefasab@gmail.com>2015-11-18 19:23:03 +0100
committerStefano Sabatini <stefasab@gmail.com>2016-02-23 18:44:12 +0100
commit14f7a3d55a43c1082ee1186a1990a431c641052d (patch)
tree8413603eb9ebbdfb8f6e84b8356a314d9101e19f /libavformat
parent3ba57bfe8ddcf151d0dcfc6bdfe76b2bb57ba018 (diff)
downloadffmpeg-streaming-14f7a3d55a43c1082ee1186a1990a431c641052d.zip
ffmpeg-streaming-14f7a3d55a43c1082ee1186a1990a431c641052d.tar.gz
lavc/lavf: transmit stream_id information for mpegts KLV data packets
This allows to copy information related to the stream ID from the demuxer to the muxer, thus allowing for example to retain information related to synchronous and asynchronous KLV data packets. This information is used in the muxer when remuxing to distinguish the two kind of packets (if the information is lacking, data packets are considered synchronous). The fate reference changes are due to the use of av_packet_merge_side_data(), which increases the size of the output packet size, since side data is merged into the packet data.
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/mpegts.c31
-rw-r--r--libavformat/mpegtsenc.c26
2 files changed, 45 insertions, 12 deletions
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 22874e6..e44da1f 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -235,6 +235,7 @@ typedef struct PESContext {
int total_size;
int pes_header_size;
int extended_stream_id;
+ uint8_t stream_id;
int64_t pts, dts;
int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
uint8_t header[MAX_PES_HEADER_SIZE];
@@ -862,8 +863,10 @@ static void reset_pes_packet_state(PESContext *pes)
av_buffer_unref(&pes->buffer);
}
-static void new_pes_packet(PESContext *pes, AVPacket *pkt)
+static int new_pes_packet(PESContext *pes, AVPacket *pkt)
{
+ char *sd;
+
av_init_packet(pkt);
pkt->buf = pes->buffer;
@@ -891,6 +894,13 @@ static void new_pes_packet(PESContext *pes, AVPacket *pkt)
pes->buffer = NULL;
reset_pes_packet_state(pes);
+
+ sd = av_packet_new_side_data(pkt, AV_PKT_DATA_MPEGTS_STREAM_ID, 1);
+ if (!sd)
+ return AVERROR(ENOMEM);
+ *sd = pes->stream_id;
+
+ return 0;
}
static uint64_t get_ts64(GetBitContext *gb, int bits)
@@ -979,14 +989,16 @@ static int mpegts_push_data(MpegTSFilter *filter,
PESContext *pes = filter->u.pes_filter.opaque;
MpegTSContext *ts = pes->ts;
const uint8_t *p;
- int len, code;
+ int ret, len, code;
if (!ts->pkt)
return 0;
if (is_start) {
if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
- new_pes_packet(pes, ts->pkt);
+ ret = new_pes_packet(pes, ts->pkt);
+ if (ret < 0)
+ return ret;
ts->stop_parse = 1;
} else {
reset_pes_packet_state(pes);
@@ -1014,6 +1026,7 @@ static int mpegts_push_data(MpegTSFilter *filter,
code = pes->header[3] | 0x100;
av_log(pes->stream, AV_LOG_TRACE, "pid=%x pes_code=%#x\n", pes->pid,
code);
+ pes->stream_id = pes->header[3];
if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
(!pes->sub_st ||
@@ -1197,7 +1210,9 @@ skip:
if (pes->buffer) {
if (pes->data_index > 0 &&
pes->data_index + buf_size > pes->total_size) {
- new_pes_packet(pes, ts->pkt);
+ ret = new_pes_packet(pes, ts->pkt);
+ if (ret < 0)
+ return ret;
pes->total_size = MAX_PES_PAYLOAD;
pes->buffer = av_buffer_alloc(pes->total_size +
AV_INPUT_BUFFER_PADDING_SIZE);
@@ -1220,7 +1235,9 @@ skip:
if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
ts->stop_parse = 1;
- new_pes_packet(pes, ts->pkt);
+ ret = new_pes_packet(pes, ts->pkt);
+ if (ret < 0)
+ return ret;
}
}
buf_size = 0;
@@ -2663,7 +2680,9 @@ static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
- new_pes_packet(pes, pkt);
+ ret = new_pes_packet(pes, pkt);
+ if (ret < 0)
+ return ret;
pes->state = MPEGTS_SKIP;
ret = 0;
break;
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 7656720..68f9867 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -1111,7 +1111,7 @@ static uint8_t *get_ts_payload_start(uint8_t *pkt)
* NOTE: 'payload' contains a complete PES payload. */
static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
const uint8_t *payload, int payload_size,
- int64_t pts, int64_t dts, int key)
+ int64_t pts, int64_t dts, int key, int stream_id)
{
MpegTSWriteStream *ts_st = st->priv_data;
MpegTSWrite *ts = s->priv_data;
@@ -1208,6 +1208,11 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
st->codec->codec_id == AV_CODEC_ID_AC3 &&
ts->m2ts_mode) {
*q++ = 0xfd;
+ } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
+ *q++ = stream_id != -1 ? stream_id : 0xfc;
+
+ if (stream_id == 0xbd) /* asynchronous KLV */
+ pts = dts = AV_NOPTS_VALUE;
} else {
*q++ = 0xbd;
if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
@@ -1451,6 +1456,15 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
int64_t dts = pkt->dts, pts = pkt->pts;
int opus_samples = 0;
+ int side_data_size;
+ char *side_data = NULL;
+ int stream_id = -1;
+
+ side_data = av_packet_get_side_data(pkt,
+ AV_PKT_DATA_MPEGTS_STREAM_ID,
+ &side_data_size);
+ if (side_data)
+ stream_id = side_data[0];
if (ts->reemit_pat_pmt) {
av_log(s, AV_LOG_WARNING,
@@ -1629,8 +1643,8 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
if ( ts_st2->payload_size
&& (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
- ts_st2->payload_pts, ts_st2->payload_dts,
- ts_st2->payload_flags & AV_PKT_FLAG_KEY);
+ ts_st2->payload_pts, ts_st2->payload_dts,
+ ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
ts_st2->payload_size = 0;
}
}
@@ -1643,7 +1657,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
ts_st->payload_pts, ts_st->payload_dts,
- ts_st->payload_flags & AV_PKT_FLAG_KEY);
+ ts_st->payload_flags & AV_PKT_FLAG_KEY, stream_id);
ts_st->payload_size = 0;
ts_st->opus_queued_samples = 0;
}
@@ -1652,7 +1666,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
av_assert0(!ts_st->payload_size);
// for video and subtitle, write a single pes packet
mpegts_write_pes(s, st, buf, size, pts, dts,
- pkt->flags & AV_PKT_FLAG_KEY);
+ pkt->flags & AV_PKT_FLAG_KEY, stream_id);
ts_st->opus_queued_samples = 0;
av_free(data);
return 0;
@@ -1684,7 +1698,7 @@ static void mpegts_write_flush(AVFormatContext *s)
if (ts_st->payload_size > 0) {
mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
ts_st->payload_pts, ts_st->payload_dts,
- ts_st->payload_flags & AV_PKT_FLAG_KEY);
+ ts_st->payload_flags & AV_PKT_FLAG_KEY, -1);
ts_st->payload_size = 0;
ts_st->opus_queued_samples = 0;
}
OpenPOWER on IntegriCloud