History

avformat/movenc: creating producer reference time (PRFT) box

The producer reference time box supplies relative wall-clock times at which movie fragments, or files containing movie fragments (such as segments) were produced. The box is mainly useful in live streaming use cases. A media player can parse the box and utilize the time fields to measure and improve the latency during real time playout.

Vishwanath Dixit committed 8 years ago

Commit 5717cd81 parent f09635f3 files · +69 −0Browse files

Unified · Split

3 files
  1. doc/muxers.texi +10 −0
  2. libavformat/movenc.c +50 −0
  3. libavformat/movenc.h +9 −0
doc/muxers.texi+10−0
@@ -1344,6 +1344,16 @@ be negative. This enables the initial sample to have DTS/CTS of zero, and · 1343 unmodified lines
13441344 reduces the need for edit lists for some cases such as video tracks with
13451345 B-frames. Additionally, eases conformance with the DASH-IF interoperability
13461346 guidelines.
1347+@item -write_prft
1348+Write producer time reference box (PRFT) with a specified time source for the
1349+NTP field in the PRFT box. Set value as @samp{wallclock} to specify timesource
1350+as wallclock time and @samp{pts} to specify timesource as input packets' PTS
1351+values.
1352+
1353+Setting value to @samp{pts} is applicable only for a live encoding use case,
1354+where PTS values are set as as wallclock time at the source. For example, an
1355+encoding use case with decklink capture source where @option{video_pts} and
1356+@option{audio_pts} are set to @samp{abs_wallclock}.
13471357 @end table
13481358
13491359 @subsection Example
libavformat/movenc.c+50−0
@@ -98,6 +98,9 @@ static const AVOption options[] = { · 97 unmodified lines
9898 { "encryption_kid", "The media encryption key identifier (hex)", offsetof(MOVMuxContext, encryption_kid), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
9999 { "use_stream_ids_as_track_ids", "use stream ids as track ids", offsetof(MOVMuxContext, use_stream_ids_as_track_ids), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
100100 { "write_tmcd", "force or disable writing tmcd", offsetof(MOVMuxContext, write_tmcd), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
101+ { "write_prft", "Write producer reference time box with specified time source", offsetof(MOVMuxContext, write_prft), AV_OPT_TYPE_INT, {.i64 = MOV_PRFT_NONE}, 0, MOV_PRFT_NB-1, AV_OPT_FLAG_ENCODING_PARAM, "prft"},
102+ { "wallclock", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_WALLCLOCK}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, "prft"},
103+ { "pts", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_PTS}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, "prft"},
101104 { NULL },
102105 };
103106
@@ -4514,6 +4517,49 @@ static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov, · 4410 unmodified lines
45144517 return 0;
45154518 }
45164519
4520+static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
4521+{
4522+ int64_t pos = avio_tell(pb), pts_us, ntp_ts;
4523+ MOVTrack *first_track;
4524+
4525+ /* PRFT should be associated with at most one track. So, choosing only the
4526+ * first track. */
4527+ if (tracks > 0)
4528+ return 0;
4529+ first_track = &(mov->tracks[0]);
4530+
4531+ if (!first_track->entry) {
4532+ av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, no entries in the track\n");
4533+ return 0;
4534+ }
4535+
4536+ if (first_track->cluster[0].pts == AV_NOPTS_VALUE) {
4537+ av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, first PTS is invalid\n");
4538+ return 0;
4539+ }
4540+
4541+ if (mov->write_prft == MOV_PRFT_SRC_WALLCLOCK) {
4542+ ntp_ts = ff_get_formatted_ntp_time(ff_ntp_time());
4543+ } else if (mov->write_prft == MOV_PRFT_SRC_PTS) {
4544+ pts_us = av_rescale_q(first_track->cluster[0].pts,
4545+ first_track->st->time_base, AV_TIME_BASE_Q);
4546+ ntp_ts = ff_get_formatted_ntp_time(pts_us + NTP_OFFSET_US);
4547+ } else {
4548+ av_log(mov->fc, AV_LOG_WARNING, "Unsupported PRFT box configuration: %d\n",
4549+ mov->write_prft);
4550+ return 0;
4551+ }
4552+
4553+ avio_wb32(pb, 0); // Size place holder
4554+ ffio_wfourcc(pb, "prft"); // Type
4555+ avio_w8(pb, 1); // Version
4556+ avio_wb24(pb, 0); // Flags
4557+ avio_wb32(pb, first_track->track_id); // reference track ID
4558+ avio_wb64(pb, ntp_ts); // NTP time stamp
4559+ avio_wb64(pb, first_track->cluster[0].pts); //media time
4560+ return update_size(pb, pos);
4561+}
4562+
45174563 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
45184564 int64_t mdat_size)
45194565 {
@@ -4528,6 +4574,9 @@ static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks, · 8 unmodified lines
45284574 if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX))
45294575 mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
45304576
4577+ if (mov->write_prft > MOV_PRFT_NONE && mov->write_prft < MOV_PRFT_NB)
4578+ mov_write_prft_tag(pb, mov, tracks);
4579+
45314580 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX ||
45324581 !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) ||
45334582 mov->ism_lookahead) {
@@ -5317,6 +5366,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) · 783 unmodified lines
53175366 trk->cluster[trk->entry].size = size;
53185367 trk->cluster[trk->entry].entries = samples_in_chunk;
53195368 trk->cluster[trk->entry].dts = pkt->dts;
5369+ trk->cluster[trk->entry].pts = pkt->pts;
53205370 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
53215371 if (!trk->frag_discont) {
53225372 /* First packet of a new fragment. We already wrote the duration
libavformat/movenc.h+9−0
@@ -46,6 +46,7 @@ · 45 unmodified lines
4646 typedef struct MOVIentry {
4747 uint64_t pos;
4848 int64_t dts;
49+ int64_t pts;
4950 unsigned int size;
5051 unsigned int samples_in_chunk;
5152 unsigned int chunkNum; ///< Chunk number if the current entry is a chunk start otherwise 0
@@ -169,6 +170,13 @@ typedef enum { · 117 unmodified lines
169170 MOV_ENC_CENC_AES_CTR,
170171 } MOVEncryptionScheme;
171172
173+typedef enum {
174+ MOV_PRFT_NONE = 0,
175+ MOV_PRFT_SRC_WALLCLOCK,
176+ MOV_PRFT_SRC_PTS,
177+ MOV_PRFT_NB
178+} MOVPrftBox;
179+
172180 typedef struct MOVMuxContext {
173181 const AVClass *av_class;
174182 int mode;
@@ -224,6 +232,7 @@ typedef struct MOVMuxContext { · 49 unmodified lines
224232 int use_stream_ids_as_track_ids;
225233 int track_ids_ok;
226234 int write_tmcd;
235+ MOVPrftBox write_prft;
227236 } MOVMuxContext;
228237
229238 #define FF_MOV_FLAG_RTP_HINT (1 << 0)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t