2009-05-08 21:51:13 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-11-27 00:42:39 +00:00
|
|
|
#include <string.h>
|
2011-08-20 17:25:43 +00:00
|
|
|
#include <assert.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-03-28 11:38:42 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-07-21 22:37:55 +00:00
|
|
|
#include "config.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2009-03-16 03:11:22 +00:00
|
|
|
#include "talloc.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-21 19:24:20 +00:00
|
|
|
#include "common/global.h"
|
2001-07-21 22:37:55 +00:00
|
|
|
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "demux.h"
|
2001-07-21 22:37:55 +00:00
|
|
|
#include "stheader.h"
|
2002-02-06 20:16:35 +00:00
|
|
|
#include "mf.h"
|
2001-07-21 22:37:55 +00:00
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
2002-03-15 15:53:11 +00:00
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
// Demuxer list
|
EDL: add support for new EDL file format
The timeline code previously added to support Matroska ordered
chapters allows constructing a playback timeline from segments picked
from multiple source files. Add support for a new EDL format to make
this machinery available for use with file formats other than Matroska
and in a manner easier to use than creating files with ordered
chapters.
Unlike the old -edl option which specifies an additional file with
edits to apply to the video file given as the main argument, the new
EDL format is used by giving only the EDL file as the file to play;
that file then contains the filename(s) to use as source files where
actual video segments come from. Filename paths in the EDL file are
ignored. Currently the source files are only searched for in the
directory of the EDL file; support for a search path option will
likely be added in the future.
Format of the EDL files
The first line in the file must be "mplayer EDL file, version 2".
The rest of the lines belong to one of these classes:
1) lines specifying source files
2) empty lines
3) lines specifying timeline segments.
Lines beginning with '<' specify source files. These lines first
contain an identifier used to refer to the source file later, then the
filename separated by whitespace. The identifier must start with a
letter. Filenames that start or end with whitespace or contain
newlines are not supported.
On other lines '#' characters delimit comments. Lines that contain
only whitespace after comments have been removed are ignored.
Timeline segments must appear in the file in chronological order. Each
segment has the following information associated with it:
- duration
- output start time
- output end time (= output start time + duration)
- source id (specifies the file the content of the segment comes from)
- source start time (timestamp in the source file)
- source end time (= source start time + duration)
The output timestamps must form a continuous timeline from 0 to the
end of the last segment, such that each new segment starts from the
time the previous one ends at. Source files and times may change
arbitrarily between segments.
The general format for lines specifying timeline segments is
[output time info] source_id [source time info]
source_id must be an identifier defined on a '<' line. Both the time
info parts consists of zero or more of the following elements:
1) timestamp
2) -timestamp
3) +duration
4) *
5) -*
, where "timestamp" and "duration" are decimal numbers (computations
are done with nanosecond precision). Whitespace around "+" and "-" is
optional. 1) and 2) specify start and end time of the segment on
output or source side. 3) specifies duration; the semantics are the
same whether this appears on output or source side. 4) and 5) are
ignored on the output side (they're always implicitly assumed). On the
source side 4) specifies that the segment starts where the previous
segment _using this source_ ended; if there was no previous segment
time 0 is used. 5) specifies that the segment ends where the next
segment using this source starts.
Redundant information may be omitted. It will be filled in using the
following rules:
- output start for first segment is 0
- two of [output start, output end, duration] imply third
- two of [source start, source end, duration] imply third
- output start = output end of previous segment
- output end = output start of next segment
- if "*", source start = source end of earlier segment
- if "-*", source end = source start of a later segment
As a special rule, a last zero-duration segment without a source
specification may appear. This will produce no corresponding segment
in the resulting timeline, but can be used as syntax to specify the
end time of the timeline (with effect equal to adding -time on the
previous line).
Examples:
----- begin -----
mplayer EDL file, version 2
< id1 filename
0 id1 123
100 id1 456
200 id1 789
300
----- end -----
All segments come from the source file "filename". First segment
(output time 0-100) comes from time 123-223, second 456-556, third
789-889.
----- begin -----
mplayer EDL file, version 2
< f filename
f 60-120
f 600-660
f 30- 90
----- end -----
Play first seconds 60-120 from the file, then 600-660, then 30-90.
----- begin -----
mplayer EDL file, version 2
< id1 filename1
< id2 filename2
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
----- end -----
This plays time 0-10 from filename1, then 0-10 from filename1, then
10-20 from filename1, then 10-20 from filename2, then 20-30 from
filename1, then 20-30 from filename2.
----- begin -----
mplayer EDL file, version 2
< t1 filename1
< t2 filename2
t1 * +2 # segment 1
+2 t2 100 # segment 2
t1 * # segment 3
t2 *-* # segment 4
t1 3 -* # segment 5
+0.111111 t2 102.5 # segment 6
7.37 t1 5 +1 # segment 7
----- end -----
This rather pathological example illustrates the rules for filling in
implied data. All the values can be determined by recursively applying
the rules given above, and the full end result is this:
+2 0-2 t1 0-2 # segment 1
+2 2-4 t2 100-102 # segment 2
+0.758889 4-4.758889 t1 2-2.758889 # segment 3
+0.5 4.4758889-5.258889 t2 102-102.5 # segment 4
+2 5.258889-7.258889 t1 3-5 # segment 5
+0.111111 7.258889-7.37 t2 102.5-102.611111 # segment 6
+1 7.37-8.37 t1 5-6 # segment 7
2011-02-14 11:05:35 +00:00
|
|
|
extern const struct demuxer_desc demuxer_desc_edl;
|
2012-01-01 16:45:24 +00:00
|
|
|
extern const struct demuxer_desc demuxer_desc_cue;
|
2008-01-13 16:00:39 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_rawaudio;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_rawvideo;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_tv;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_mf;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_matroska;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_lavf;
|
2013-06-22 00:09:52 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_libass;
|
2013-06-21 19:34:55 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_subreader;
|
2013-08-25 18:40:21 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_playlist;
|
2014-07-05 14:57:56 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_disc;
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2008-01-28 16:03:22 +00:00
|
|
|
/* Please do not add any new demuxers here. If you want to implement a new
|
2008-01-28 22:09:21 +00:00
|
|
|
* demuxer, add it to libavformat, except for wrappers around external
|
|
|
|
* libraries and demuxers requiring binary support. */
|
2008-01-28 16:03:22 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
const demuxer_desc_t *const demuxer_list[] = {
|
2014-07-05 14:57:56 +00:00
|
|
|
&demuxer_desc_disc,
|
EDL: add support for new EDL file format
The timeline code previously added to support Matroska ordered
chapters allows constructing a playback timeline from segments picked
from multiple source files. Add support for a new EDL format to make
this machinery available for use with file formats other than Matroska
and in a manner easier to use than creating files with ordered
chapters.
Unlike the old -edl option which specifies an additional file with
edits to apply to the video file given as the main argument, the new
EDL format is used by giving only the EDL file as the file to play;
that file then contains the filename(s) to use as source files where
actual video segments come from. Filename paths in the EDL file are
ignored. Currently the source files are only searched for in the
directory of the EDL file; support for a search path option will
likely be added in the future.
Format of the EDL files
The first line in the file must be "mplayer EDL file, version 2".
The rest of the lines belong to one of these classes:
1) lines specifying source files
2) empty lines
3) lines specifying timeline segments.
Lines beginning with '<' specify source files. These lines first
contain an identifier used to refer to the source file later, then the
filename separated by whitespace. The identifier must start with a
letter. Filenames that start or end with whitespace or contain
newlines are not supported.
On other lines '#' characters delimit comments. Lines that contain
only whitespace after comments have been removed are ignored.
Timeline segments must appear in the file in chronological order. Each
segment has the following information associated with it:
- duration
- output start time
- output end time (= output start time + duration)
- source id (specifies the file the content of the segment comes from)
- source start time (timestamp in the source file)
- source end time (= source start time + duration)
The output timestamps must form a continuous timeline from 0 to the
end of the last segment, such that each new segment starts from the
time the previous one ends at. Source files and times may change
arbitrarily between segments.
The general format for lines specifying timeline segments is
[output time info] source_id [source time info]
source_id must be an identifier defined on a '<' line. Both the time
info parts consists of zero or more of the following elements:
1) timestamp
2) -timestamp
3) +duration
4) *
5) -*
, where "timestamp" and "duration" are decimal numbers (computations
are done with nanosecond precision). Whitespace around "+" and "-" is
optional. 1) and 2) specify start and end time of the segment on
output or source side. 3) specifies duration; the semantics are the
same whether this appears on output or source side. 4) and 5) are
ignored on the output side (they're always implicitly assumed). On the
source side 4) specifies that the segment starts where the previous
segment _using this source_ ended; if there was no previous segment
time 0 is used. 5) specifies that the segment ends where the next
segment using this source starts.
Redundant information may be omitted. It will be filled in using the
following rules:
- output start for first segment is 0
- two of [output start, output end, duration] imply third
- two of [source start, source end, duration] imply third
- output start = output end of previous segment
- output end = output start of next segment
- if "*", source start = source end of earlier segment
- if "-*", source end = source start of a later segment
As a special rule, a last zero-duration segment without a source
specification may appear. This will produce no corresponding segment
in the resulting timeline, but can be used as syntax to specify the
end time of the timeline (with effect equal to adding -time on the
previous line).
Examples:
----- begin -----
mplayer EDL file, version 2
< id1 filename
0 id1 123
100 id1 456
200 id1 789
300
----- end -----
All segments come from the source file "filename". First segment
(output time 0-100) comes from time 123-223, second 456-556, third
789-889.
----- begin -----
mplayer EDL file, version 2
< f filename
f 60-120
f 600-660
f 30- 90
----- end -----
Play first seconds 60-120 from the file, then 600-660, then 30-90.
----- begin -----
mplayer EDL file, version 2
< id1 filename1
< id2 filename2
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
----- end -----
This plays time 0-10 from filename1, then 0-10 from filename1, then
10-20 from filename1, then 10-20 from filename2, then 20-30 from
filename1, then 20-30 from filename2.
----- begin -----
mplayer EDL file, version 2
< t1 filename1
< t2 filename2
t1 * +2 # segment 1
+2 t2 100 # segment 2
t1 * # segment 3
t2 *-* # segment 4
t1 3 -* # segment 5
+0.111111 t2 102.5 # segment 6
7.37 t1 5 +1 # segment 7
----- end -----
This rather pathological example illustrates the rules for filling in
implied data. All the values can be determined by recursively applying
the rules given above, and the full end result is this:
+2 0-2 t1 0-2 # segment 1
+2 2-4 t2 100-102 # segment 2
+0.758889 4-4.758889 t1 2-2.758889 # segment 3
+0.5 4.4758889-5.258889 t2 102-102.5 # segment 4
+2 5.258889-7.258889 t1 3-5 # segment 5
+0.111111 7.258889-7.37 t2 102.5-102.611111 # segment 6
+1 7.37-8.37 t1 5-6 # segment 7
2011-02-14 11:05:35 +00:00
|
|
|
&demuxer_desc_edl,
|
2012-01-01 16:45:24 +00:00
|
|
|
&demuxer_desc_cue,
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_rawaudio,
|
|
|
|
&demuxer_desc_rawvideo,
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_TV
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_tv,
|
2005-08-05 19:57:47 +00:00
|
|
|
#endif
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_LIBASS
|
2013-06-22 00:09:52 +00:00
|
|
|
&demuxer_desc_libass,
|
2013-07-07 23:40:13 +00:00
|
|
|
#endif
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_matroska,
|
|
|
|
&demuxer_desc_lavf,
|
2013-08-12 18:40:39 +00:00
|
|
|
&demuxer_desc_mf,
|
2013-08-25 18:40:21 +00:00
|
|
|
&demuxer_desc_playlist,
|
2013-08-12 18:40:39 +00:00
|
|
|
// Pretty aggressive, so should be last.
|
|
|
|
&demuxer_desc_subreader,
|
2008-04-12 15:51:08 +00:00
|
|
|
NULL
|
2005-08-05 19:57:47 +00:00
|
|
|
};
|
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
struct demux_stream {
|
2014-07-05 14:59:44 +00:00
|
|
|
struct demuxer *demuxer;
|
2013-07-11 17:20:25 +00:00
|
|
|
int selected; // user wants packets from this stream
|
2013-07-11 17:17:51 +00:00
|
|
|
int eof; // end of demuxed stream? (true if all buffer empty)
|
|
|
|
int packs; // number of packets in buffer
|
|
|
|
int bytes; // total bytes of packets in buffer
|
|
|
|
struct demux_packet *head;
|
|
|
|
struct demux_packet *tail;
|
2013-07-11 17:20:25 +00:00
|
|
|
};
|
2013-07-11 17:17:51 +00:00
|
|
|
|
2014-03-25 01:05:48 +00:00
|
|
|
void demuxer_sort_chapters(demuxer_t *demuxer);
|
stream: report chapter times, use time seeks for DVD chapters
Allow the stream layer to report chapter times. Extend stream_dvd to do
this. I'm not 100% sure whether the re-used code is bug-free (because it
was used for slave-mode and/or debugging only).
MAke the frontend do time-based seeks when switching DVD chapters. I'm
not sure if there's a real reason STREAM_CTRL_SEEK_TO_CHAPTER exists
(maybe/hopefully not), but we will see.
Note that querying chapter times in demuxer_chapter_time() with the new
STREAM_CTRL_GET_CHAPTER_TIME could be excessively slow, especially with
the cache enabled. The frontend likes to query chapter times very often.
Additionally, stream_dvd uses some sort of quadratic algorithm to list
times for all chapters. For this reason, we try to query all chapters on
start (after the demuxer is opened), and add the chapters to the demuxer
chapter list. demuxer_chapter_time() will get the time from that list,
instead of asking the stream layer over and over again.
This assumes stream_dvd knows the list of chapters at the start, and
also that the list of chapters never changes during playback. This
seems to be true, and the only exception, switching DVD titles, is not
supported at runtime (and doesn't need to be supported).
2013-05-03 23:20:39 +00:00
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
static void ds_free_packs(struct demux_stream *ds)
|
|
|
|
{
|
|
|
|
demux_packet_t *dp = ds->head;
|
|
|
|
while (dp) {
|
|
|
|
demux_packet_t *dn = dp->next;
|
|
|
|
free_demux_packet(dp);
|
|
|
|
dp = dn;
|
|
|
|
}
|
|
|
|
ds->head = ds->tail = NULL;
|
|
|
|
ds->packs = 0; // !!!!!
|
|
|
|
ds->bytes = 0;
|
|
|
|
ds->eof = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-07 23:02:45 +00:00
|
|
|
struct sh_stream *new_sh_stream(demuxer_t *demuxer, enum stream_type type)
|
2012-08-03 10:24:55 +00:00
|
|
|
{
|
2013-07-07 23:02:45 +00:00
|
|
|
if (demuxer->num_streams > MAX_SH_STREAMS) {
|
2014-02-09 17:59:57 +00:00
|
|
|
MP_WARN(demuxer, "Too many streams.\n");
|
2013-04-14 17:19:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-07 23:02:45 +00:00
|
|
|
int demuxer_id = 0;
|
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
if (demuxer->streams[n]->type == type)
|
|
|
|
demuxer_id++;
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
struct sh_stream *sh = talloc_ptrtype(demuxer, sh);
|
|
|
|
*sh = (struct sh_stream) {
|
2012-08-03 10:24:55 +00:00
|
|
|
.type = type,
|
2012-08-19 16:01:30 +00:00
|
|
|
.demuxer = demuxer,
|
2012-08-03 10:24:55 +00:00
|
|
|
.index = demuxer->num_streams,
|
2013-04-29 20:34:36 +00:00
|
|
|
.demuxer_id = demuxer_id, // may be overwritten by demuxer
|
2013-07-11 17:20:25 +00:00
|
|
|
.ds = talloc_zero(sh, struct demux_stream),
|
|
|
|
};
|
2014-07-05 14:59:44 +00:00
|
|
|
sh->ds->demuxer = demuxer;
|
2014-07-06 17:02:49 +00:00
|
|
|
sh->ds->selected = demuxer->stream_select_default;
|
2012-08-03 10:24:55 +00:00
|
|
|
MP_TARRAY_APPEND(demuxer, demuxer->streams, demuxer->num_streams, sh);
|
|
|
|
switch (sh->type) {
|
2014-07-05 14:55:43 +00:00
|
|
|
case STREAM_VIDEO: sh->video = talloc_zero(demuxer, struct sh_video); break;
|
|
|
|
case STREAM_AUDIO: sh->audio = talloc_zero(demuxer, struct sh_audio); break;
|
|
|
|
case STREAM_SUB: sh->sub = talloc_zero(demuxer, struct sh_sub); break;
|
2012-08-03 10:24:55 +00:00
|
|
|
}
|
2013-07-11 17:22:24 +00:00
|
|
|
|
2012-08-03 10:24:55 +00:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
void free_demuxer(demuxer_t *demuxer)
|
|
|
|
{
|
2013-08-25 18:40:21 +00:00
|
|
|
if (!demuxer)
|
|
|
|
return;
|
2008-04-12 15:51:08 +00:00
|
|
|
if (demuxer->desc->close)
|
|
|
|
demuxer->desc->close(demuxer);
|
2001-08-22 23:54:57 +00:00
|
|
|
// free streams:
|
2013-07-07 23:26:13 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++)
|
2013-11-14 18:51:09 +00:00
|
|
|
ds_free_packs(demuxer->streams[n]->ds);
|
2009-03-16 03:11:22 +00:00
|
|
|
talloc_free(demuxer);
|
2001-08-22 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 15:48:56 +00:00
|
|
|
const char *stream_type_name(enum stream_type type)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
switch (type) {
|
|
|
|
case STREAM_VIDEO: return "video";
|
|
|
|
case STREAM_AUDIO: return "audio";
|
|
|
|
case STREAM_SUB: return "sub";
|
|
|
|
default: return "unknown";
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int count_packs(struct demuxer *demux, enum stream_type type)
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
for (int n = 0; n < demux->num_streams; n++)
|
|
|
|
c += demux->streams[n]->type == type ? demux->streams[n]->ds->packs : 0;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int count_bytes(struct demuxer *demux, enum stream_type type)
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
for (int n = 0; n < demux->num_streams; n++)
|
|
|
|
c += demux->streams[n]->type == type ? demux->streams[n]->ds->bytes : 0;
|
|
|
|
return c;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:17:51 +00:00
|
|
|
// Returns the same value as demuxer->fill_buffer: 1 ok, 0 EOF/not selected.
|
2014-07-05 14:59:44 +00:00
|
|
|
int demux_add_packet(struct sh_stream *stream, demux_packet_t *dp)
|
2013-07-11 17:17:51 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
struct demux_stream *ds = stream ? stream->ds : NULL;
|
2013-07-11 18:05:43 +00:00
|
|
|
if (!dp || !ds || !ds->selected) {
|
2013-07-11 17:20:25 +00:00
|
|
|
talloc_free(dp);
|
2013-07-11 17:17:51 +00:00
|
|
|
return 0;
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
2014-07-05 14:59:44 +00:00
|
|
|
struct demuxer *demuxer = ds->demuxer;
|
2013-07-11 17:20:25 +00:00
|
|
|
|
2013-11-16 20:28:59 +00:00
|
|
|
dp->stream = stream->index;
|
|
|
|
dp->next = NULL;
|
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
ds->packs++;
|
|
|
|
ds->bytes += dp->len;
|
|
|
|
if (ds->tail) {
|
|
|
|
// next packet in stream
|
|
|
|
ds->tail->next = dp;
|
|
|
|
ds->tail = dp;
|
2013-07-11 17:17:51 +00:00
|
|
|
} else {
|
2013-07-11 17:20:25 +00:00
|
|
|
// first packet in stream
|
|
|
|
ds->head = ds->tail = dp;
|
2013-07-11 17:17:51 +00:00
|
|
|
}
|
2014-07-05 14:59:44 +00:00
|
|
|
// obviously not true anymore
|
2013-11-16 19:40:37 +00:00
|
|
|
ds->eof = 0;
|
|
|
|
|
2013-11-25 22:13:01 +00:00
|
|
|
// For video, PTS determination is not trivial, but for other media types
|
|
|
|
// distinguishing PTS and DTS is not useful.
|
|
|
|
if (stream->type != STREAM_VIDEO && dp->pts == MP_NOPTS_VALUE)
|
|
|
|
dp->pts = dp->dts;
|
|
|
|
|
2014-07-05 14:59:44 +00:00
|
|
|
if (mp_msg_test(demuxer->log, MSGL_DEBUG)) {
|
|
|
|
MP_DBG(demuxer, "DEMUX: Append packet to %s, len=%d pts=%5.3f pos="
|
|
|
|
"%"PRIi64" [A=%d V=%d S=%d]\n", stream_type_name(stream->type),
|
|
|
|
dp->len, dp->pts, dp->pos, count_packs(demuxer, STREAM_AUDIO),
|
|
|
|
count_packs(demuxer, STREAM_VIDEO), count_packs(demuxer, STREAM_SUB));
|
|
|
|
}
|
2013-07-11 17:20:25 +00:00
|
|
|
return 1;
|
2013-07-11 17:17:51 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:49:50 +00:00
|
|
|
static bool demux_check_queue_full(demuxer_t *demux)
|
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
for (int n = 0; n < demux->num_streams; n++) {
|
|
|
|
struct sh_stream *sh = demux->streams[n];
|
|
|
|
if (sh->ds->packs > MAX_PACKS || sh->ds->bytes > MAX_PACK_BYTES)
|
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
return false;
|
2013-02-14 13:49:50 +00:00
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
overflow:
|
2013-02-14 13:49:50 +00:00
|
|
|
|
|
|
|
if (!demux->warned_queue_overflow) {
|
2014-02-09 17:59:57 +00:00
|
|
|
MP_ERR(demux, "Too many packets in the demuxer "
|
2013-12-21 19:24:20 +00:00
|
|
|
"packet queue (video: %d packets in %d bytes, audio: %d "
|
|
|
|
"packets in %d bytes, sub: %d packets in %d bytes).\n",
|
|
|
|
count_packs(demux, STREAM_VIDEO), count_bytes(demux, STREAM_VIDEO),
|
|
|
|
count_packs(demux, STREAM_AUDIO), count_bytes(demux, STREAM_AUDIO),
|
|
|
|
count_packs(demux, STREAM_SUB), count_bytes(demux, STREAM_SUB));
|
|
|
|
MP_INFO(demux, "Maybe you are playing a non-"
|
2013-07-07 21:54:11 +00:00
|
|
|
"interleaved stream/file or the codec failed?\n");
|
2013-02-14 13:49:50 +00:00
|
|
|
}
|
|
|
|
demux->warned_queue_overflow = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// return value:
|
|
|
|
// 0 = EOF or no stream found or invalid type
|
|
|
|
// 1 = successfully read a packet
|
|
|
|
|
2013-07-11 17:17:51 +00:00
|
|
|
static int demux_fill_buffer(demuxer_t *demux)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2013-07-11 17:17:51 +00:00
|
|
|
return demux->desc->fill_buffer ? demux->desc->fill_buffer(demux) : 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
static void ds_get_packets(struct sh_stream *sh)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
struct demux_stream *ds = sh->ds;
|
|
|
|
demuxer_t *demux = sh->demuxer;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_TRACE(demux, "ds_get_packets (%s) called\n",
|
|
|
|
stream_type_name(sh->type));
|
2008-04-12 15:51:08 +00:00
|
|
|
while (1) {
|
2013-11-16 19:40:37 +00:00
|
|
|
if (ds->head)
|
2013-07-11 17:20:25 +00:00
|
|
|
return;
|
2009-07-06 22:15:02 +00:00
|
|
|
|
2013-02-14 13:49:50 +00:00
|
|
|
if (demux_check_queue_full(demux))
|
2008-04-12 15:51:08 +00:00
|
|
|
break;
|
2013-02-14 13:49:50 +00:00
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
if (!demux_fill_buffer(demux))
|
2008-04-12 15:51:08 +00:00
|
|
|
break; // EOF
|
2001-07-29 21:07:34 +00:00
|
|
|
}
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demux, "ds_get_packets: EOF reached (stream: %s)\n",
|
|
|
|
stream_type_name(sh->type));
|
2008-04-12 15:51:08 +00:00
|
|
|
ds->eof = 1;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-04-20 23:00:11 +00:00
|
|
|
|
2013-07-11 17:10:33 +00:00
|
|
|
// Read a packet from the given stream. The returned packet belongs to the
|
|
|
|
// caller, who has to free it with talloc_free(). Might block. Returns NULL
|
|
|
|
// on EOF.
|
|
|
|
struct demux_packet *demux_read_packet(struct sh_stream *sh)
|
2006-04-24 10:58:40 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
struct demux_stream *ds = sh ? sh->ds : NULL;
|
2013-07-11 17:10:33 +00:00
|
|
|
if (ds) {
|
2013-07-11 17:20:25 +00:00
|
|
|
ds_get_packets(sh);
|
2013-07-11 17:10:33 +00:00
|
|
|
struct demux_packet *pkt = ds->head;
|
|
|
|
if (pkt) {
|
|
|
|
ds->head = pkt->next;
|
|
|
|
pkt->next = NULL;
|
|
|
|
if (!ds->head)
|
|
|
|
ds->tail = NULL;
|
|
|
|
ds->bytes -= pkt->len;
|
|
|
|
ds->packs--;
|
|
|
|
|
2014-07-05 14:56:36 +00:00
|
|
|
if (pkt && pkt->pos >= 0)
|
|
|
|
sh->demuxer->filepos = pkt->pos;
|
2013-07-11 17:10:33 +00:00
|
|
|
|
|
|
|
return pkt;
|
|
|
|
}
|
2013-06-02 23:28:14 +00:00
|
|
|
}
|
2013-07-11 17:10:33 +00:00
|
|
|
return NULL;
|
2001-04-20 23:00:11 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:10:33 +00:00
|
|
|
// Return the pts of the next packet that demux_read_packet() would return.
|
|
|
|
// Might block. Sometimes used to force a packet read, without removing any
|
|
|
|
// packets from the queue.
|
|
|
|
double demux_get_next_pts(struct sh_stream *sh)
|
2011-08-20 17:25:43 +00:00
|
|
|
{
|
2013-08-22 17:06:51 +00:00
|
|
|
if (sh && sh->ds->selected) {
|
2013-07-11 17:20:25 +00:00
|
|
|
ds_get_packets(sh);
|
|
|
|
if (sh->ds->head)
|
|
|
|
return sh->ds->head->pts;
|
|
|
|
}
|
|
|
|
return MP_NOPTS_VALUE;
|
2011-08-20 17:25:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:10:33 +00:00
|
|
|
// Return whether a packet is queued. Never blocks, never forces any reads.
|
|
|
|
bool demux_has_packet(struct sh_stream *sh)
|
2006-04-27 11:13:21 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
return sh && sh->ds->head;
|
2002-05-02 10:25:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:17:51 +00:00
|
|
|
// Return whether EOF was returned with an earlier packet read.
|
|
|
|
bool demux_stream_eof(struct sh_stream *sh)
|
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
return !sh || sh->ds->eof;
|
2013-07-11 17:17:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 14:57:56 +00:00
|
|
|
// Read and return any packet we find.
|
|
|
|
struct demux_packet *demux_read_any_packet(struct demuxer *demuxer)
|
|
|
|
{
|
2014-07-06 17:02:58 +00:00
|
|
|
for (int retry = 0; retry < 2; retry++) {
|
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
struct sh_stream *sh = demuxer->streams[n];
|
|
|
|
if (sh->ds->head)
|
|
|
|
return demux_read_packet(sh);
|
|
|
|
}
|
|
|
|
// retry after calling this
|
|
|
|
demux_fill_buffer(demuxer);
|
2014-07-05 14:57:56 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-07-21 22:37:55 +00:00
|
|
|
// ====================================================================
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
void demuxer_help(struct mp_log *log)
|
2005-08-05 19:57:47 +00:00
|
|
|
{
|
2008-04-12 15:51:08 +00:00
|
|
|
int i;
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_info(log, "Available demuxers:\n");
|
|
|
|
mp_info(log, " demuxer: info:\n");
|
2008-04-12 15:51:08 +00:00
|
|
|
for (i = 0; demuxer_list[i]; i++) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_info(log, "%10s %s\n",
|
|
|
|
demuxer_list[i]->name, demuxer_list[i]->desc);
|
2008-04-12 15:51:08 +00:00
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
}
|
2005-02-13 13:39:19 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
static const char *d_level(enum demux_check level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case DEMUX_CHECK_FORCE: return "force";
|
|
|
|
case DEMUX_CHECK_UNSAFE: return "unsafe";
|
|
|
|
case DEMUX_CHECK_REQUEST:return "request";
|
|
|
|
case DEMUX_CHECK_NORMAL: return "normal";
|
2008-04-12 15:51:08 +00:00
|
|
|
}
|
2013-07-12 19:58:11 +00:00
|
|
|
abort();
|
2005-08-05 19:57:47 +00:00
|
|
|
}
|
2001-12-27 21:24:56 +00:00
|
|
|
|
2014-03-28 11:38:42 +00:00
|
|
|
static int decode_float(char *str, float *out)
|
|
|
|
{
|
|
|
|
char *rest;
|
|
|
|
float dec_val;
|
|
|
|
|
|
|
|
dec_val = strtod(str, &rest);
|
|
|
|
if (!rest || (rest == str) || !isfinite(dec_val))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*out = dec_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_gain(demuxer_t *demuxer, const char *tag, float *out)
|
|
|
|
{
|
|
|
|
char *tag_val = NULL;
|
|
|
|
float dec_val;
|
|
|
|
|
|
|
|
tag_val = mp_tags_get_str(demuxer->metadata, tag);
|
|
|
|
if (!tag_val) {
|
|
|
|
mp_msg(demuxer->log, MSGL_V, "Replaygain tags not found\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decode_float(tag_val, &dec_val)) {
|
|
|
|
mp_msg(demuxer->log, MSGL_ERR, "Invalid replaygain value\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = dec_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_peak(demuxer_t *demuxer, const char *tag, float *out)
|
|
|
|
{
|
|
|
|
char *tag_val = NULL;
|
|
|
|
float dec_val;
|
|
|
|
|
|
|
|
*out = 1.0;
|
|
|
|
|
|
|
|
tag_val = mp_tags_get_str(demuxer->metadata, tag);
|
|
|
|
if (!tag_val)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (decode_float(tag_val, &dec_val))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (dec_val == 0.0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*out = dec_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void demux_export_replaygain(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
float tg, tp, ag, ap;
|
|
|
|
|
2014-07-05 14:45:41 +00:00
|
|
|
if (!decode_gain(demuxer, "REPLAYGAIN_TRACK_GAIN", &tg) &&
|
2014-03-28 11:38:42 +00:00
|
|
|
!decode_peak(demuxer, "REPLAYGAIN_TRACK_PEAK", &tp) &&
|
|
|
|
!decode_gain(demuxer, "REPLAYGAIN_ALBUM_GAIN", &ag) &&
|
|
|
|
!decode_peak(demuxer, "REPLAYGAIN_ALBUM_PEAK", &ap))
|
|
|
|
{
|
|
|
|
struct replaygain_data *rgain = talloc_ptrtype(demuxer, rgain);
|
|
|
|
|
|
|
|
rgain->track_gain = tg;
|
|
|
|
rgain->track_peak = tp;
|
|
|
|
rgain->album_gain = ag;
|
|
|
|
rgain->album_peak = ap;
|
|
|
|
|
2014-07-05 14:45:41 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
struct sh_stream *sh = demuxer->streams[n];
|
|
|
|
if (sh->audio && !sh->audio->replaygain_data)
|
|
|
|
sh->audio->replaygain_data = rgain;
|
|
|
|
}
|
2014-03-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
static struct demuxer *open_given_type(struct mpv_global *global,
|
|
|
|
struct mp_log *log,
|
2010-11-10 12:37:15 +00:00
|
|
|
const struct demuxer_desc *desc,
|
2013-07-12 19:58:11 +00:00
|
|
|
struct stream *stream,
|
|
|
|
struct demuxer_params *params,
|
|
|
|
enum demux_check check)
|
|
|
|
{
|
|
|
|
struct demuxer *demuxer = talloc_ptrtype(NULL, demuxer);
|
|
|
|
*demuxer = (struct demuxer) {
|
|
|
|
.desc = desc,
|
|
|
|
.type = desc->type,
|
|
|
|
.stream = stream,
|
2014-05-24 12:04:09 +00:00
|
|
|
.seekable = stream->seekable,
|
2013-07-12 19:58:11 +00:00
|
|
|
.filepos = -1,
|
2013-12-21 19:24:20 +00:00
|
|
|
.opts = global->opts,
|
|
|
|
.global = global,
|
|
|
|
.log = mp_log_new(demuxer, log, desc->name),
|
2013-12-21 20:55:41 +00:00
|
|
|
.glog = log,
|
2013-07-12 19:58:11 +00:00
|
|
|
.filename = talloc_strdup(demuxer, stream->url),
|
2013-09-08 04:32:48 +00:00
|
|
|
.metadata = talloc_zero(demuxer, struct mp_tags),
|
2014-07-07 15:36:50 +00:00
|
|
|
.events = DEMUX_EVENT_METADATA,
|
2013-07-12 19:58:11 +00:00
|
|
|
};
|
|
|
|
demuxer->params = params; // temporary during open()
|
2014-05-24 12:03:07 +00:00
|
|
|
int64_t start_pos = stream_tell(stream);
|
2013-07-12 19:58:11 +00:00
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_verbose(log, "Trying demuxer: %s (force-level: %s)\n",
|
|
|
|
desc->name, d_level(check));
|
2013-07-12 19:58:11 +00:00
|
|
|
|
|
|
|
int ret = demuxer->desc->open(demuxer, check);
|
|
|
|
if (ret >= 0) {
|
|
|
|
demuxer->params = NULL;
|
2010-11-10 13:38:36 +00:00
|
|
|
if (demuxer->filetype)
|
2014-05-31 20:07:36 +00:00
|
|
|
mp_verbose(log, "Detected file format: %s (%s)\n",
|
|
|
|
demuxer->filetype, desc->desc);
|
2010-11-10 13:38:36 +00:00
|
|
|
else
|
2014-05-31 20:07:36 +00:00
|
|
|
mp_verbose(log, "Detected file format: %s\n", desc->desc);
|
2013-04-12 11:20:37 +00:00
|
|
|
demuxer_sort_chapters(demuxer);
|
2013-07-02 10:18:04 +00:00
|
|
|
demux_info_update(demuxer);
|
2014-03-28 11:38:42 +00:00
|
|
|
demux_export_replaygain(demuxer);
|
2013-11-03 18:21:47 +00:00
|
|
|
// Pretend we can seek if we can't seek, but there's a cache.
|
|
|
|
if (!demuxer->seekable && stream->uncached_stream) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_warn(log,
|
|
|
|
"File is not seekable, but there's a cache: enabling seeking.\n");
|
2013-11-03 18:21:47 +00:00
|
|
|
demuxer->seekable = true;
|
|
|
|
}
|
2010-11-10 12:37:15 +00:00
|
|
|
return demuxer;
|
|
|
|
}
|
2013-07-12 19:58:11 +00:00
|
|
|
|
2010-11-10 12:37:15 +00:00
|
|
|
free_demuxer(demuxer);
|
2014-05-24 12:03:07 +00:00
|
|
|
stream_seek(stream, start_pos);
|
2010-11-10 12:37:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
static const int d_normal[] = {DEMUX_CHECK_NORMAL, DEMUX_CHECK_UNSAFE, -1};
|
|
|
|
static const int d_request[] = {DEMUX_CHECK_REQUEST, -1};
|
|
|
|
static const int d_force[] = {DEMUX_CHECK_FORCE, -1};
|
|
|
|
|
2013-07-11 19:10:42 +00:00
|
|
|
struct demuxer *demux_open(struct stream *stream, char *force_format,
|
2013-12-21 19:24:20 +00:00
|
|
|
struct demuxer_params *params,
|
|
|
|
struct mpv_global *global)
|
2010-11-10 12:37:15 +00:00
|
|
|
{
|
2013-07-12 19:58:11 +00:00
|
|
|
const int *check_levels = d_normal;
|
|
|
|
const struct demuxer_desc *check_desc = NULL;
|
2013-12-21 19:24:20 +00:00
|
|
|
struct mp_log *log = mp_log_new(NULL, global->log, "!demux");
|
|
|
|
struct demuxer *demuxer = NULL;
|
2013-07-12 19:58:11 +00:00
|
|
|
|
|
|
|
if (!force_format)
|
|
|
|
force_format = stream->demuxer;
|
2008-04-12 15:51:08 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
if (force_format && force_format[0]) {
|
|
|
|
check_levels = d_request;
|
|
|
|
if (force_format[0] == '+') {
|
|
|
|
force_format += 1;
|
|
|
|
check_levels = d_force;
|
|
|
|
}
|
|
|
|
for (int n = 0; demuxer_list[n]; n++) {
|
|
|
|
if (strcmp(demuxer_list[n]->name, force_format) == 0)
|
|
|
|
check_desc = demuxer_list[n];
|
|
|
|
}
|
|
|
|
if (!check_desc) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_err(log, "Demuxer %s does not exist.\n", force_format);
|
|
|
|
goto done;
|
2006-10-19 18:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-19 00:21:54 +00:00
|
|
|
|
2013-08-25 20:58:29 +00:00
|
|
|
// Peek this much data to avoid that stream_read() run by some demuxers
|
|
|
|
// or stream filters will flush previous peeked data.
|
|
|
|
stream_peek(stream, STREAM_BUFFER_SIZE);
|
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
// Test demuxers from first to last, one pass for each check_levels[] entry
|
|
|
|
for (int pass = 0; check_levels[pass] != -1; pass++) {
|
|
|
|
enum demux_check level = check_levels[pass];
|
|
|
|
for (int n = 0; demuxer_list[n]; n++) {
|
|
|
|
const struct demuxer_desc *desc = demuxer_list[n];
|
|
|
|
if (!check_desc || desc == check_desc) {
|
2013-12-21 19:24:20 +00:00
|
|
|
demuxer = open_given_type(global, log, desc, stream, params, level);
|
2013-12-21 20:55:41 +00:00
|
|
|
if (demuxer) {
|
|
|
|
talloc_steal(demuxer, log);
|
|
|
|
log = NULL;
|
2013-12-21 19:24:20 +00:00
|
|
|
goto done;
|
2013-12-21 20:55:41 +00:00
|
|
|
}
|
2013-07-12 19:58:11 +00:00
|
|
|
}
|
2006-10-19 18:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-12-26 16:34:06 +00:00
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
done:
|
|
|
|
talloc_free(log);
|
|
|
|
return demuxer;
|
2001-07-21 22:37:55 +00:00
|
|
|
}
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2008-06-04 05:10:48 +00:00
|
|
|
void demux_flush(demuxer_t *demuxer)
|
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++)
|
|
|
|
ds_free_packs(demuxer->streams[n]->ds);
|
|
|
|
demuxer->warned_queue_overflow = false;
|
2008-06-02 10:17:48 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 17:13:29 +00:00
|
|
|
int demux_seek(demuxer_t *demuxer, float rel_seek_secs, int flags)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
|
|
|
if (!demuxer->seekable) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Cannot seek in this file.\n");
|
2008-04-12 15:51:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-08 12:12:46 +00:00
|
|
|
|
|
|
|
if (rel_seek_secs == MP_NOPTS_VALUE && (flags & SEEK_ABSOLUTE))
|
|
|
|
return 0;
|
|
|
|
|
2014-07-05 14:57:56 +00:00
|
|
|
// clear the packet queues
|
2008-06-04 05:10:48 +00:00
|
|
|
demux_flush(demuxer);
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
if (demuxer->desc->seek)
|
2013-08-22 17:13:29 +00:00
|
|
|
demuxer->desc->seek(demuxer, rel_seek_secs, flags);
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return 1;
|
2001-08-12 17:28:16 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 12:41:20 +00:00
|
|
|
static int demux_info_print(demuxer_t *demuxer)
|
2001-11-21 16:14:14 +00:00
|
|
|
{
|
2013-09-08 04:32:48 +00:00
|
|
|
struct mp_tags *info = demuxer->metadata;
|
2002-02-14 13:04:35 +00:00
|
|
|
int n;
|
2001-11-21 16:14:14 +00:00
|
|
|
|
2013-09-22 23:22:48 +00:00
|
|
|
if (!info || !info->num_keys)
|
2008-04-12 15:51:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-02-06 12:43:30 +00:00
|
|
|
mp_info(demuxer->glog, "File tags:\n");
|
2013-09-08 04:32:48 +00:00
|
|
|
for (n = 0; n < info->num_keys; n++) {
|
2013-12-21 20:55:41 +00:00
|
|
|
mp_info(demuxer->glog, " %s: %s\n", info->keys[n], info->values[n]);
|
2005-06-20 23:07:35 +00:00
|
|
|
}
|
2002-01-16 03:25:07 +00:00
|
|
|
|
|
|
|
return 0;
|
2001-11-21 16:14:14 +00:00
|
|
|
}
|
2002-02-20 16:59:25 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
char *demux_info_get(demuxer_t *demuxer, const char *opt)
|
|
|
|
{
|
2013-09-08 04:32:48 +00:00
|
|
|
return mp_tags_get_str(demuxer->metadata, opt);
|
2002-02-20 16:59:25 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 15:00:37 +00:00
|
|
|
bool demux_info_update(struct demuxer *demuxer)
|
2013-07-02 10:18:04 +00:00
|
|
|
{
|
2014-07-05 14:45:56 +00:00
|
|
|
bool r = false;
|
2013-07-02 10:18:04 +00:00
|
|
|
// Take care of stream metadata as well
|
2014-07-05 14:45:56 +00:00
|
|
|
struct mp_tags *s_meta = NULL;
|
|
|
|
if (stream_control(demuxer->stream, STREAM_CTRL_GET_METADATA, &s_meta) > 0) {
|
|
|
|
talloc_free(demuxer->stream_metadata);
|
|
|
|
demuxer->stream_metadata = talloc_steal(demuxer, s_meta);
|
|
|
|
demuxer->events |= DEMUX_EVENT_METADATA;
|
2013-07-02 10:18:04 +00:00
|
|
|
}
|
2014-07-05 14:45:56 +00:00
|
|
|
if (demuxer->events & DEMUX_EVENT_METADATA) {
|
|
|
|
demuxer->events &= ~DEMUX_EVENT_METADATA;
|
|
|
|
if (demuxer->stream_metadata)
|
|
|
|
mp_tags_merge(demuxer->metadata, demuxer->stream_metadata);
|
2014-02-06 12:41:20 +00:00
|
|
|
demux_info_print(demuxer);
|
2014-07-05 14:45:56 +00:00
|
|
|
r = true;
|
2014-02-06 12:41:20 +00:00
|
|
|
}
|
2014-07-05 14:45:56 +00:00
|
|
|
return r;
|
2013-07-02 10:18:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demux_control(demuxer_t *demuxer, int cmd, void *arg)
|
|
|
|
{
|
2005-08-05 19:57:47 +00:00
|
|
|
|
|
|
|
if (demuxer->desc->control)
|
2008-04-12 15:51:08 +00:00
|
|
|
return demuxer->desc->control(demuxer, cmd, arg);
|
2005-08-05 19:57:47 +00:00
|
|
|
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
2002-11-16 03:42:14 +00:00
|
|
|
}
|
|
|
|
|
2012-08-19 16:01:30 +00:00
|
|
|
struct sh_stream *demuxer_stream_by_demuxer_id(struct demuxer *d,
|
|
|
|
enum stream_type t, int id)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2012-08-19 16:01:30 +00:00
|
|
|
for (int n = 0; n < d->num_streams; n++) {
|
|
|
|
struct sh_stream *s = d->streams[n];
|
|
|
|
if (s->type == t && s->demuxer_id == id)
|
|
|
|
return d->streams[n];
|
2010-05-22 05:19:23 +00:00
|
|
|
}
|
2012-08-19 16:01:30 +00:00
|
|
|
return NULL;
|
2005-04-03 14:08:28 +00:00
|
|
|
}
|
2006-08-06 18:55:34 +00:00
|
|
|
|
2012-08-19 16:01:30 +00:00
|
|
|
void demuxer_switch_track(struct demuxer *demuxer, enum stream_type type,
|
|
|
|
struct sh_stream *stream)
|
|
|
|
{
|
|
|
|
assert(!stream || stream->type == type);
|
2013-04-14 04:20:31 +00:00
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
struct sh_stream *cur = demuxer->streams[n];
|
2013-07-11 17:22:24 +00:00
|
|
|
if (cur->type == type)
|
|
|
|
demuxer_select_track(demuxer, cur, cur == stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void demuxer_select_track(struct demuxer *demuxer, struct sh_stream *stream,
|
|
|
|
bool selected)
|
|
|
|
{
|
|
|
|
// don't flush buffers if stream is already selected / unselected
|
|
|
|
if (stream->ds->selected != selected) {
|
|
|
|
stream->ds->selected = selected;
|
|
|
|
ds_free_packs(stream->ds);
|
|
|
|
demux_control(demuxer, DEMUXER_CTRL_SWITCHED_TRACKS, NULL);
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
2006-11-16 21:23:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 17:02:21 +00:00
|
|
|
bool demux_stream_is_selected(struct sh_stream *stream)
|
2013-04-14 17:19:35 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
return stream && stream->ds->selected;
|
2013-04-14 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 10:44:37 +00:00
|
|
|
int demuxer_add_attachment(demuxer_t *demuxer, struct bstr name,
|
|
|
|
struct bstr type, struct bstr data)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2010-01-26 01:31:31 +00:00
|
|
|
if (!(demuxer->num_attachments % 32))
|
|
|
|
demuxer->attachments = talloc_realloc(demuxer, demuxer->attachments,
|
|
|
|
struct demux_attachment,
|
|
|
|
demuxer->num_attachments + 32);
|
|
|
|
|
|
|
|
struct demux_attachment *att =
|
|
|
|
demuxer->attachments + demuxer->num_attachments;
|
2010-05-19 10:44:37 +00:00
|
|
|
att->name = talloc_strndup(demuxer->attachments, name.start, name.len);
|
|
|
|
att->type = talloc_strndup(demuxer->attachments, type.start, type.len);
|
|
|
|
att->data = talloc_size(demuxer->attachments, data.len);
|
|
|
|
memcpy(att->data, data.start, data.len);
|
|
|
|
att->data_size = data.len;
|
2008-01-12 01:12:36 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return demuxer->num_attachments++;
|
2008-01-12 01:12:36 +00:00
|
|
|
}
|
|
|
|
|
2012-02-26 20:12:53 +00:00
|
|
|
static int chapter_compare(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
struct demux_chapter *c1 = (void *)p1;
|
|
|
|
struct demux_chapter *c2 = (void *)p2;
|
|
|
|
|
|
|
|
if (c1->start > c2->start)
|
|
|
|
return 1;
|
|
|
|
else if (c1->start < c2->start)
|
|
|
|
return -1;
|
2013-04-12 11:20:37 +00:00
|
|
|
return c1->original_index > c2->original_index ? 1 :-1; // never equal
|
2012-02-26 20:12:53 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 11:20:37 +00:00
|
|
|
void demuxer_sort_chapters(demuxer_t *demuxer)
|
2012-02-26 20:12:53 +00:00
|
|
|
{
|
|
|
|
qsort(demuxer->chapters, demuxer->num_chapters,
|
|
|
|
sizeof(struct demux_chapter), chapter_compare);
|
|
|
|
}
|
|
|
|
|
2010-05-19 10:44:37 +00:00
|
|
|
int demuxer_add_chapter(demuxer_t *demuxer, struct bstr name,
|
2013-09-08 05:42:05 +00:00
|
|
|
uint64_t start, uint64_t end, uint64_t demuxer_id)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2013-04-12 11:38:20 +00:00
|
|
|
struct demux_chapter new = {
|
|
|
|
.original_index = demuxer->num_chapters,
|
|
|
|
.start = start,
|
|
|
|
.end = end,
|
2013-05-06 21:33:38 +00:00
|
|
|
.name = name.len ? bstrdup0(demuxer, name) : NULL,
|
2013-09-08 05:42:05 +00:00
|
|
|
.metadata = talloc_zero(demuxer, struct mp_tags),
|
|
|
|
.demuxer_id = demuxer_id,
|
2013-04-12 11:38:20 +00:00
|
|
|
};
|
2013-09-08 21:07:02 +00:00
|
|
|
mp_tags_set_bstr(new.metadata, bstr0("TITLE"), name);
|
2013-04-12 11:38:20 +00:00
|
|
|
MP_TARRAY_APPEND(demuxer, demuxer->chapters, demuxer->num_chapters, new);
|
2014-02-06 12:43:01 +00:00
|
|
|
return demuxer->num_chapters - 1;
|
2013-09-08 05:42:05 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 18:07:04 +00:00
|
|
|
double demuxer_get_time_length(struct demuxer *demuxer)
|
|
|
|
{
|
2013-05-03 18:26:48 +00:00
|
|
|
double len;
|
|
|
|
if (demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, &len) > 0)
|
|
|
|
return len;
|
2013-05-03 18:07:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|