Commit Graph

543 Commits

Author SHA1 Message Date
Anton Khirnov 9f61abc811 lavf: allow custom IO for all files
Some (de)muxers open additional files beyond the main IO context.
Currently, they call avio_open() directly, which prevents the caller
from using custom IO for such streams.

This commit adds callbacks to AVFormatContext that default to
avio_open2()/avio_close(), but can be overridden by the caller. All
muxers and demuxers using AVIO are switched to using those callbacks
instead of calling avio_open()/avio_close() directly.

(de)muxers that use the URLProtocol layer directly instead of AVIO
remain unconverted for now. This should be fixed in later commits.
2016-01-24 16:45:32 +01:00
Anton Khirnov 5845a8273e movenc: use the CPB props side data
Do not access the encoder options, since it makes no sense when the
AVStream codec context is not the encoding context.
2015-12-06 10:26:01 +01:00
John Stebbins ca489564ae movenc: add fallback audio track tref support
This feature allows making associations between audio tracks
that apple players recognize.  E.g. when an ac3 track has a
tref that points to an aac track, devices that don't support
ac3 will automatically fall back to the aac track.

Apple used to *guess* these associations, but new products
(AppleTV 4) no longer guess and this association can only
be made explicitly now using the "fall" tref.

Signed-off-by: Anton Khirnov <anton@khirnov.net>
2015-11-18 11:38:03 +01:00
Martin Storsjö bef3b1f59f movenc: Allow setting start_dts/start_cts before writing actual packets
By writing a zero-sized packet, the caller can communicate the
start_dts/start_cts for the stream without actually writing
the first packet.

This allows doing random-access writing of fragments when the
start dts of the stream isn't zero, so that the edit list in the moov
is written based on timestamps from the nominal start time signaled
via the zero-sized packet, while the first proper packet written
corresponds to a later fragment.

To avoid potential unexpected behaviour, empty packets only set
start_dts if the frag_discont flag is set.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-11-11 22:53:31 +02:00
Martin Storsjö 09e431b9e3 movenc: Assume streams starting at pts=0 for discontinuous fragments with editlists
This allows producing fragments discontinously where the video
stream has b-frames (but starts at pts=0), but doesn't work for the
cases with audio with preroll.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-11-11 22:52:36 +02:00
Martin Storsjö 8ad5124b7e movenc: Automatically flush after writing the initial moov
In most other cases when writing fragmented mp4 files, the output
IO context is flushed after each fragment. Also flush it after
writing the initial moov, to have it behave in the same way.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-11-10 21:46:03 +02:00
Martin Storsjö 5ea5a24eb7 movenc: Honor flush requests with delay_moov, when some tracks lack samples
This also makes sure that a fragmented file without the empty_moov
flag (i.e. with a non-empty initial moov fragment) actually gets
written, if some of the tracks turn out to not have any samples.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-10-23 10:37:49 +03:00
Martin Storsjö 26ac22e5e7 movenc: Add a new flag for writing global sidx indexes for dash
The double meaning of the faststart flag (moving a moov atom
to the start of files, making them streamable, for non-fragmented
files, vs inserting a global sidx index at the start of files
for fragmented files) is confusing - see 40ed1cbf1 for
explanation of its origins.

Since the second meaning of the flag hasn't been part of any
libav release yet, just rename it to get rid of the confusion
without any extra deprecation (which wouldn't get rid of the
potential confusion, of users adding -movflags faststart
even for fragmented files, where it isn't needed for making
them "streamable").

This gets back the old behaviour, where -movflags faststart
doesn't have any effect for fragmented files.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-08-12 13:41:46 +03:00
Martin Storsjö 5f200bbf98 movenc: Place the sidx index after the initial moov/mdat pair
For fragmented files with non-empty moov, with a fragment index
(sidx), place the index after the initial moov/mdat pair.

Previously, for this pathological case, the index was written
at the start of the file.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-08-10 09:46:14 +03:00
Martin Storsjö 83f254e65f movenc: Rename reserved_moov_pos to reserved_header_pos
The same field is also used for writing the sidx index header,
for fragmented files, when the faststart flag is used.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-08-10 09:46:07 +03:00
Martin Storsjö 8e34089e26 movenc: Check that frag_info entries exist in mov_write_sidx_tag
This fixes crashes with pathological cases when trying to write
a sidx index (via the -movflags faststart option, in combination
with fragmenting options), when no fragments actually have been
written. (This is possible if the empty_moov flag isn't used,
so that all actual packet data is written in the moov/mdat pair,
and no moof/mdat pairs have been written.)

In these pathological cases, no sidx should be written at all.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-08-10 09:46:03 +03:00
Vittorio Giovara 059a934806 lavc: Consistently prefix input buffer defines
Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
2015-07-27 15:24:59 +01:00
Luca Barbato e95c7a6185 mov: Preserve the metadata even when bit-exactness is requested
Make sure to not write the custom `encoder` string in that case.

Bug-Id: 845
CC: libav-stable@libav.org
2015-06-30 15:34:38 +02:00
Janne Grunau 529c05698e movenc: fixes a questionable valgrind uninitialized value warning
display_matrix_size is only initialized when av_stream_get_side_data()
returns a side data pointer. The code is safe since the only effect this
has is setting the display_matrix pointer to NULL which it was already
anyway.
2015-06-10 14:32:11 +02:00
Martin Storsjö e0046bc9c9 movenc: Write the make and model metadata keys for mov style files
These are essential allowing QuickTime to keep detecting content
as slow-motion - this allows preserving them on stream copy.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-23 12:43:35 +02:00
Anton Khirnov cf18e777ae movenc: write the display transform matrix 2015-03-19 21:02:59 +01:00
Martin Storsjö 67a2912307 movenc: Don't assume that fragment durations in pts is equal to duration in dts
For strict CFR, they should be pretty much equal, but if the stream
is VFR, there can be a sometimes significant difference.

Calculate the pts duration separately, used in sidx atoms and for
tfrf/tfxd boxes in smooth streaming ismv files.

Also make sure to reduce the duration of sidx entries according to
edit lists.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-19 10:27:20 +02:00
Martin Storsjö 5c337353a2 movenc: Move sidx edit list timestamp adjustment into a block
When reading these values from track->frag_info, the same adjustment
has already been done.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-19 10:27:17 +02:00
Martin Storsjö eee13d6539 movenc: Only adjust the cts offset at the start of fragments if necessary
Adjusting it is only necessary when a sidx/tfrf/tfxd atom already has
been written for the previous fragment (since the sidx/tfrf/tfxd atoms
include the duration between the first pts of the previous fragment, to
the first pts of the new fragment).

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-19 10:27:15 +02:00
Martin Storsjö 5cf892d694 movenc: Write pts timestamps in tfxd, instead of dts timestamps
This matches what we write in tfra and tfrf since 9cbf70fa0e.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-19 10:27:12 +02:00
Martin Storsjö b81b0cc22b movenc: Set the last packet duration based on the next packet when autoflushing
When automatically flushing fragments based on set conditions
(fragmentation on keyframes, after some interval or byte size),
we already have the next packet for one stream - use this for setting
the duration of the last packet in the flushed fragment correctly.

This avoids having to adjust the timestamp of the first packet in
the new fragment since the last duration was unknown.

Unfortunately, this only works for automatic flushing (not for
caller-triggered flushing, like in the dash muxer), and only for the
one single track that triggered the flushing. The duration of the
last sample in all other tracks still is dependent on AVPacket
duration (or heuristics).

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-19 10:27:07 +02:00
Martin Storsjö 2889c5e167 movenc: Heuristically set the duration of the last sample in a fragment if not set
Even if this is a guess, it is way better than writing a zero duration
of the last sample in a fragment (because if the duration is zero,
the first sample of the next fragment will have the same timestamp
as the last sample in the previous one).

Since we normally don't require libavformat muxer users to set
the duration field in AVPacket, we probably can't strictly require
it here either, so don't log this as a strict warning, only as info.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-19 10:26:52 +02:00
Martin Storsjö 2cb9c2fc59 movenc: Allow interleaving samples when writing fragmented files
This is incompatible with the omit_tfhd_offset flag (writing
position independent fragments with interleaving requires the
default_base_moof flag).

This makes the moof atoms slightly bigger, but can be better for
playback (improving locality of sample data in the mdat).

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-16 13:23:19 +02:00
Martin Storsjö 50ff0c05b6 movenc: Support writing multiple trun atoms
This is needed if all the data for one track isn't continuous
within the mdat. Normally we make sure all the data for one
track is continuous, but in new cases we will need to have
the samples interleaved.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-16 13:23:19 +02:00
Martin Storsjö 448c8cfe4c movenc: Support setting fragment_index before the moov atom is written
This way, the caller doesn't need to coordinate setting the option
after the moov atom has been written. The downside is that it is
no longer possible to use the option for checking whether the moov
atom already has been written, but a caller is able to keep track
of that by other means anyway.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-10 10:41:06 +02:00
Martin Storsjö 0c5e380c2c movenc: Don't rely on the fragment index for vc1 info gathering
The previous use of the mov->fragments field, for determining whether
written packets were part of the first fragment or not, didn't
work as intended when using the empty_moov flag.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-10 10:41:03 +02:00
Martin Storsjö 46d4d85759 movenc: Avoid writing separate flags for the first sample if not necessary
Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-09 10:36:12 +02:00
Martin Storsjö 00d751d4fc movenc: Set tfhd default sample flags based on actual samples, if possible
This avoids assuming that e.g. audio samples are marked as
sync samples.

This allows omitting the sample flags from trun, if the default
flags happen to be right for all the samples.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-09 10:36:02 +02:00
Martin Storsjö 9731cf4001 movenc: Keep writing zero-entry stts atoms as intended
a876585215 had the unintended side effect of returning AVERROR(ENOMEM)
when track->entry is zero, while the code intentionally wants to
continue in that case.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-03-06 16:17:48 +02:00
Hugo Beauzée-Luyssen a876585215 movenc: Check memory allocations
Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
2015-02-17 12:16:43 -05:00
Martin Storsjö 2a1500fb55 movenc: Fix a typo in a comment
Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-06 19:47:18 +02:00
Martin Storsjö e581e88cbd movenc: Readd an accidentally removed condition
This was removed accidentally as part of 847bf598. This could cause
groundless warning logging.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-06 19:47:09 +02:00
Martin Storsjö 46808fdf04 movenc: Enable editlists by default if delay_moov is enabled
Being able to write editlists properly is one of the main points
in the delay_moov flag.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-04 20:04:36 +02:00
Martin Storsjö b3b0b35db2 movenc: Get rid of a hack for updating the dvc1 atom
Use the more generic approach with the delay_moov flag, instead of
having a update mechanism specific to this one single atom.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-03 01:20:40 +02:00
Martin Storsjö 847bf5988f movenc: Add an option for delaying writing the moov with empty_moov
This delays writing the moov until the first fragment is written,
or can be flushed by the caller explicitly when wanted. If the first
sample in all streams is available at this point, we can write
a proper editlist at this point, allowing streams to start at
something else than dts=0. For AC3 and DNXHD, a packet is
needed in order to write the moov header properly.

This isn't added to the normal behaviour for empty_moov, since
the behaviour that ftyp+moov is written during avformat_write_header
would be changed. Callers that split the output stream into header+segments
(either by flushing manually, with the custom_frag flag set, or by
just differentiating between data written during avformat_write_header
and the rest) will need to be adjusted to take this option into use.

For handling streams that start at something else than dts=0, an
alternative would be to use different kinds of heuristics for
guessing the start dts (using AVCodecContext delay or has_b_frames
together with the frame rate), but this is not reliable and doesn't
necessarily work well with stream copy, and wouldn't work for getting
the right initialization data for AC3 or DNXHD either.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-03 01:19:27 +02:00
Martin Storsjö c725faebda movenc: Use start_dts/cts instead of cluster[0] for writing edit lists
This allows writing edit lists even when track->entry == 0, if
the start times have been set.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-03 01:18:52 +02:00
Martin Storsjö 724cbea719 movenc: Remove an unnecessary condition when flushing fragments
If fragments == 0 it means we haven't written any moov atom yet.
If the empty_moov flag is set, we already have written an empty moov
atom at startup. Thus, the check for empty_moov is redundant.

This is in preparation for allowing writing the moov atom later,
even when using the empty moov flag.

Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-03 01:18:38 +02:00
Martin Storsjö 355d01a1bf movenc: Factorize writing ftyp and other identification tags to a separate function
Signed-off-by: Martin Storsjö <martin@martin.st>
2015-01-03 01:18:28 +02:00
Martin Storsjö 59f0275dd0 movenc: Adjust the pts of new fragments similarly to what is done for dts
The pts and the corresponding duration is written in sidx
atoms, thus make sure these match up correctly.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-12-18 23:14:27 +02:00
Martin Storsjö 234fb81e31 movenc: Expose the fragment index as an avoption
This allows setting the right fragment number if doing
random-access writing of fragments, and also allows reading the
current sequence number.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-26 09:58:13 +02:00
Martin Storsjö ee37620b6a movenc: Add a flag for indicating a discontinuous fragment
This allows creating a later mp4 fragment without sequentially
writing the earlier ones before (when called from a segmenter).

Normally when writing a fragmented mp4 file sequentially, the
first timestamps of a fragment are adjusted to match the
end of the previous fragment, to make sure the timestamp is the
same, even if it is calculated as the sum of previous fragment
durations. (And for the first packet in a file, the offset of
the first packet is written using an edit list.)

When writing an individual mp4 fragment discontinuously like this
(with potentially writing the earlier fragments separately later),
there's a risk of getting a gap in the timeline if the duration
field of the last packet in the previous fragment doesn't match up
with the start time of the next fragment.

Using this requires setting -avoid_negative_ts make_non_negative
(or -avoid_negative_ts 0).

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-26 09:56:56 +02:00
Martin Storsjö 40ed1cbf14 movenc: Allow writing a DASH sidx atom at the start of files
This is mapped to the faststart flag (which in this case
perhaps should be called "shift and write index at the
start of the file"), which for fragmented files will
write a sidx index at the start.

When segmenting DASH into files, there's usually one sidx
at the start of each segment (although it's not clear to me
whether that actually is necessary). When storing all of it
in one file, the MPD doesn't necessarily need to describe
the individual segments, but the offsets of the fragments can be
fetched from one large sidx atom at the start of the file. This
allows creating files for the DASH ISO BMFF on-demand profile.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-17 16:17:07 +02:00
Martin Storsjö 3847f3ab58 movenc: Add tfra entries for all tracks in a moof
Previously only tfra entries were added for the first track in each moof.

The frag_info array used for tfra can also be used for writing
other kinds of fragment indexes, where it's more important to
include all tracks.

When the separate_moof option is enabled (as in ismv), we write
a separate moof for each track, so this doesn't make any difference
in that case.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-17 16:17:07 +02:00
Martin Storsjö 2ded57371a movenc: Add support for writing sidx atoms for DASH segments
A flag "dash" is added, which enables the necessary flags for
creating DASH compatible fragments.

When this is enabled, one sidx atom is written for each track
before every moof atom.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-17 16:16:46 +02:00
Martin Storsjö 2d9d6afb8d movenc: Factorize adding fragment info into a separate function
By calling this after writing the moof the first time (for
calculating the moof size), we can avoid intermediate storage
of tfrf_offset in MOVTrack.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-17 16:13:02 +02:00
Martin Storsjö 0f9eb9165b movenc: Include empty tracks in iods when writing fragmented mp4
When writing fragmented streams with an empty initial moov,
we won't have any samples in any tracks when writing the
moov atom, thus trust that any tracks that are added actually
will be present.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-17 16:12:02 +02:00
Martin Storsjö 9a5ac36b69 movenc: Require samples before trying to write edts
This avoids a potential crash if writing a fragmented psp mp4
(which probably is only a hypothetical scenario).

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-08 11:14:21 +02:00
Martin Storsjö 8cb7b7b461 movenc: Avoid leaking locally allocated data when returning on errors
Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-08 11:14:08 +02:00
Martin Storsjö 95a449d3ce movenc: Remove an outdated comment
QuickTime does support files with an empty initial movie
these days.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-08 11:13:37 +02:00
Martin Storsjö 9cbf70fa0e movenc: Write correct presentation timestamps in tfra
Previously we wrote decoding timestamps here, while the specs
say it should be presentation timestamps.

Signed-off-by: Martin Storsjö <martin@martin.st>
2014-11-07 14:50:06 +02:00