2009-05-08 21:51:13 +00:00
|
|
|
/*
|
|
|
|
* DEMUXER v2.5
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#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"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/options.h"
|
2009-03-16 03:11:22 +00:00
|
|
|
#include "talloc.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/mp_msg.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
|
|
|
|
2008-07-17 17:14:12 +00:00
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
#if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
|
|
|
|
#error MP_INPUT_BUFFER_PADDING_SIZE is too small!
|
|
|
|
#endif
|
|
|
|
|
2010-01-24 15:19:14 +00:00
|
|
|
static void clear_parser(sh_common_t *sh);
|
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_avi;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_asf;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_matroska;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_lavf;
|
2008-11-30 13:22:34 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_mng;
|
2012-09-07 12:26:02 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_mpeg_ps;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_mpeg_pes;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_mpeg_es;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_mpeg4_es;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_h264_es;
|
2012-09-07 12:42:25 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_mpeg_ts;
|
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[] = {
|
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,
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_TV
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_tv,
|
2005-08-05 19:57:47 +00:00
|
|
|
#endif
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_matroska,
|
|
|
|
&demuxer_desc_lavf,
|
2012-12-08 19:14:13 +00:00
|
|
|
&demuxer_desc_avi,
|
|
|
|
&demuxer_desc_asf,
|
2008-11-30 13:22:34 +00:00
|
|
|
#ifdef CONFIG_MNG
|
|
|
|
&demuxer_desc_mng,
|
2006-07-04 21:26:41 +00:00
|
|
|
#endif
|
2012-09-07 12:26:02 +00:00
|
|
|
&demuxer_desc_mpeg_ps,
|
|
|
|
&demuxer_desc_mpeg_pes,
|
|
|
|
&demuxer_desc_mpeg_gxf,
|
|
|
|
&demuxer_desc_mpeg_es,
|
|
|
|
&demuxer_desc_mpeg4_es,
|
|
|
|
&demuxer_desc_h264_es,
|
2012-09-07 12:42:25 +00:00
|
|
|
&demuxer_desc_mpeg_ts,
|
2012-11-16 18:12:56 +00:00
|
|
|
// auto-probe last, because it checks file-extensions only
|
|
|
|
&demuxer_desc_mf,
|
2008-04-12 15:51:08 +00:00
|
|
|
/* Please do not add any new demuxers here. If you want to implement a new
|
|
|
|
* demuxer, add it to libavformat, except for wrappers around external
|
|
|
|
* libraries and demuxers requiring binary support. */
|
|
|
|
NULL
|
2005-08-05 19:57:47 +00:00
|
|
|
};
|
|
|
|
|
2011-08-19 01:59:01 +00:00
|
|
|
static struct demux_packet *create_packet(size_t len)
|
2011-01-17 13:47:57 +00:00
|
|
|
{
|
2011-01-17 14:16:39 +00:00
|
|
|
if (len > 1000000000) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Attempt to allocate demux packet "
|
|
|
|
"over 1 GB!\n");
|
|
|
|
abort();
|
|
|
|
}
|
2011-01-17 13:47:57 +00:00
|
|
|
struct demux_packet *dp = malloc(sizeof(struct demux_packet));
|
|
|
|
dp->len = len;
|
|
|
|
dp->next = NULL;
|
|
|
|
dp->pts = MP_NOPTS_VALUE;
|
2011-01-26 18:42:15 +00:00
|
|
|
dp->duration = -1;
|
2011-01-17 13:47:57 +00:00
|
|
|
dp->stream_pts = MP_NOPTS_VALUE;
|
|
|
|
dp->pos = 0;
|
2012-07-24 21:23:27 +00:00
|
|
|
dp->keyframe = false;
|
2011-01-17 13:47:57 +00:00
|
|
|
dp->refcount = 1;
|
|
|
|
dp->master = NULL;
|
|
|
|
dp->buffer = NULL;
|
2011-08-19 01:59:01 +00:00
|
|
|
dp->avpacket = NULL;
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct demux_packet *new_demux_packet(size_t len)
|
|
|
|
{
|
|
|
|
struct demux_packet *dp = create_packet(len);
|
2011-06-18 16:55:13 +00:00
|
|
|
dp->buffer = malloc(len + MP_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!dp->buffer) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Memory allocation failure!\n");
|
|
|
|
abort();
|
2011-01-17 14:16:39 +00:00
|
|
|
}
|
2011-06-18 16:55:13 +00:00
|
|
|
memset(dp->buffer + len, 0, 8);
|
2011-01-17 13:47:57 +00:00
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
2011-08-19 01:59:01 +00:00
|
|
|
// data must already have suitable padding
|
|
|
|
struct demux_packet *new_demux_packet_fromdata(void *data, size_t len)
|
|
|
|
{
|
|
|
|
struct demux_packet *dp = create_packet(len);
|
|
|
|
dp->buffer = data;
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:16:39 +00:00
|
|
|
void resize_demux_packet(struct demux_packet *dp, size_t len)
|
2011-01-17 13:47:57 +00:00
|
|
|
{
|
2011-01-17 14:16:39 +00:00
|
|
|
if (len > 1000000000) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Attempt to realloc demux packet "
|
|
|
|
"over 1 GB!\n");
|
|
|
|
abort();
|
|
|
|
}
|
2011-06-18 16:55:13 +00:00
|
|
|
dp->buffer = realloc(dp->buffer, len + MP_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!dp->buffer) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Memory allocation failure!\n");
|
|
|
|
abort();
|
2011-01-17 13:47:57 +00:00
|
|
|
}
|
2011-06-18 16:55:13 +00:00
|
|
|
memset(dp->buffer + len, 0, 8);
|
2011-01-17 13:47:57 +00:00
|
|
|
dp->len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct demux_packet *clone_demux_packet(struct demux_packet *pack)
|
|
|
|
{
|
|
|
|
struct demux_packet *dp = malloc(sizeof(struct demux_packet));
|
|
|
|
while (pack->master)
|
|
|
|
pack = pack->master; // find the master
|
|
|
|
memcpy(dp, pack, sizeof(struct demux_packet));
|
|
|
|
dp->next = NULL;
|
|
|
|
dp->refcount = 0;
|
|
|
|
dp->master = pack;
|
|
|
|
pack->refcount++;
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_demux_packet(struct demux_packet *dp)
|
|
|
|
{
|
|
|
|
if (dp->master == NULL) { //dp is a master packet
|
|
|
|
dp->refcount--;
|
|
|
|
if (dp->refcount == 0) {
|
2011-08-19 01:59:01 +00:00
|
|
|
if (dp->avpacket)
|
|
|
|
talloc_free(dp->avpacket);
|
|
|
|
else
|
|
|
|
free(dp->buffer);
|
2011-01-17 13:47:57 +00:00
|
|
|
free(dp);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// dp is a clone:
|
|
|
|
free_demux_packet(dp->master);
|
|
|
|
free(dp);
|
|
|
|
}
|
|
|
|
|
2011-02-10 10:15:21 +00:00
|
|
|
static void free_demuxer_stream(struct demux_stream *ds)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2001-08-22 23:54:57 +00:00
|
|
|
ds_free_packs(ds);
|
|
|
|
free(ds);
|
|
|
|
}
|
|
|
|
|
2012-08-19 16:01:30 +00:00
|
|
|
static struct demux_stream *new_demuxer_stream(struct demuxer *demuxer,
|
|
|
|
enum stream_type type, int id)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
|
|
|
demux_stream_t *ds = malloc(sizeof(demux_stream_t));
|
2012-08-19 16:01:30 +00:00
|
|
|
*ds = (demux_stream_t) {
|
|
|
|
.stream_type = type,
|
2009-11-02 01:40:09 +00:00
|
|
|
.id = id,
|
|
|
|
.demuxer = demuxer,
|
|
|
|
.asf_seq = -1,
|
|
|
|
};
|
2008-04-12 15:51:08 +00:00
|
|
|
return ds;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-19 16:01:30 +00:00
|
|
|
struct sh_stream *ds_gsh(struct demux_stream *ds)
|
|
|
|
{
|
|
|
|
// Ideally ds would have a gsh field, but since all the old demuxers set
|
|
|
|
// ds->sh themselves and we don't want to change them, enjoy this hack.
|
|
|
|
if (!ds->sh)
|
|
|
|
return NULL;
|
|
|
|
switch (ds->stream_type) {
|
|
|
|
case STREAM_VIDEO: return ((struct sh_video *)ds->sh)->gsh;
|
|
|
|
case STREAM_AUDIO: return ((struct sh_audio *)ds->sh)->gsh;
|
|
|
|
case STREAM_SUB: return ((struct sh_sub *)ds->sh)->gsh;
|
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get demuxer description structure for a given demuxer type
|
|
|
|
*
|
|
|
|
* @param file_format type of the demuxer
|
|
|
|
* @return structure for the demuxer, NULL if not found
|
|
|
|
*/
|
2008-04-12 15:51:08 +00:00
|
|
|
static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
|
2005-08-05 19:57:47 +00:00
|
|
|
{
|
2008-04-12 15:51:08 +00:00
|
|
|
int i;
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
for (i = 0; demuxer_list[i]; i++)
|
|
|
|
if (file_format == demuxer_list[i]->type)
|
|
|
|
return demuxer_list[i];
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return NULL;
|
2005-08-05 19:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-16 01:23:38 +00:00
|
|
|
demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
|
|
|
|
int a_id, int v_id, int s_id, char *filename)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2009-03-16 03:11:22 +00:00
|
|
|
struct demuxer *d = talloc_zero(NULL, struct demuxer);
|
2008-04-12 15:51:08 +00:00
|
|
|
d->stream = stream;
|
|
|
|
d->stream_pts = MP_NOPTS_VALUE;
|
|
|
|
d->reference_clock = MP_NOPTS_VALUE;
|
|
|
|
d->movi_start = stream->start_pos;
|
|
|
|
d->movi_end = stream->end_pos;
|
|
|
|
d->seekable = 1;
|
|
|
|
d->synced = 0;
|
2009-08-17 05:55:40 +00:00
|
|
|
d->filepos = -1;
|
2012-08-19 16:01:30 +00:00
|
|
|
d->audio = new_demuxer_stream(d, STREAM_VIDEO, a_id);
|
|
|
|
d->video = new_demuxer_stream(d, STREAM_AUDIO, v_id);
|
|
|
|
d->sub = new_demuxer_stream(d, STREAM_SUB, s_id);
|
|
|
|
d->ds[STREAM_VIDEO] = d->video;
|
|
|
|
d->ds[STREAM_AUDIO] = d->audio;
|
|
|
|
d->ds[STREAM_SUB] = d->sub;
|
2008-04-12 15:51:08 +00:00
|
|
|
d->type = type;
|
2008-04-16 01:23:38 +00:00
|
|
|
d->opts = opts;
|
2008-04-12 15:51:08 +00:00
|
|
|
if (type)
|
|
|
|
if (!(d->desc = get_demuxer_desc_from_type(type)))
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_ERR,
|
|
|
|
"BUG! Invalid demuxer type in new_demuxer(), "
|
2012-08-27 07:09:34 +00:00
|
|
|
"big troubles ahead.\n");
|
2008-04-12 15:51:08 +00:00
|
|
|
if (filename) // Filename hack for avs_check_file
|
|
|
|
d->filename = strdup(filename);
|
|
|
|
stream_seek(stream, stream->start_pos);
|
|
|
|
return d;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2010-08-21 16:04:18 +00:00
|
|
|
const char *sh_sub_type2str(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case 't': return "text";
|
|
|
|
case 'm': return "movtext";
|
|
|
|
case 'a': return "ass";
|
|
|
|
case 'v': return "vobsub";
|
|
|
|
case 'x': return "xsub";
|
|
|
|
case 'b': return "dvb";
|
|
|
|
case 'd': return "dvb-teletext";
|
|
|
|
case 'p': return "hdmv pgs";
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
2012-08-03 10:24:55 +00:00
|
|
|
static struct sh_stream *new_sh_stream(demuxer_t *demuxer,
|
|
|
|
enum stream_type type,
|
|
|
|
int stream_index,
|
|
|
|
int tid)
|
|
|
|
{
|
|
|
|
struct sh_stream *sh = talloc_struct(demuxer, struct sh_stream, {
|
|
|
|
.type = type,
|
2012-08-19 16:01:30 +00:00
|
|
|
.demuxer = demuxer,
|
2012-08-03 10:24:55 +00:00
|
|
|
.index = demuxer->num_streams,
|
2012-08-19 16:01:30 +00:00
|
|
|
.demuxer_id = tid, // may be overwritten by demuxer
|
2012-08-03 10:24:55 +00:00
|
|
|
.tid = tid,
|
|
|
|
.stream_index = stream_index,
|
|
|
|
.opts = demuxer->opts,
|
|
|
|
});
|
|
|
|
MP_TARRAY_APPEND(demuxer, demuxer->streams, demuxer->num_streams, sh);
|
|
|
|
switch (sh->type) {
|
|
|
|
case STREAM_VIDEO: {
|
|
|
|
struct sh_video *sht = talloc_zero(demuxer, struct sh_video);
|
|
|
|
sht->vid = sh->tid;
|
|
|
|
sht->ds = demuxer->video;
|
|
|
|
sh->video = sht;
|
|
|
|
sh->common_header = (struct sh_common *) sht;
|
|
|
|
demuxer->v_streams[sh->stream_index] = sht;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STREAM_AUDIO: {
|
|
|
|
struct sh_audio *sht = talloc_zero(demuxer, struct sh_audio);
|
|
|
|
sht->aid = tid;
|
|
|
|
sht->ds = demuxer->audio;
|
|
|
|
sht->samplesize = 2;
|
|
|
|
sht->sample_format = AF_FORMAT_S16_NE;
|
|
|
|
sh->audio = sht;
|
|
|
|
sh->common_header = (struct sh_common *) sht;
|
|
|
|
demuxer->a_streams[sh->stream_index] = sht;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STREAM_SUB: {
|
|
|
|
struct sh_sub *sht = talloc_zero(demuxer, struct sh_sub);
|
|
|
|
sht->sid = tid;
|
|
|
|
sht->ds = demuxer->sub;
|
|
|
|
sh->sub = sht;
|
|
|
|
sh->common_header = (struct sh_common *) sht;
|
|
|
|
demuxer->s_streams[sh->stream_index] = sht;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: assert(false);
|
|
|
|
}
|
|
|
|
sh->common_header->opts = sh->opts;
|
|
|
|
sh->common_header->gsh = sh;
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
|
|
|
|
{
|
|
|
|
if (id > MAX_S_STREAMS - 1 || id < 0) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_WARN,
|
|
|
|
"Requested sub stream id overflow (%d > %d)\n", id,
|
|
|
|
MAX_S_STREAMS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (demuxer->s_streams[id])
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
|
|
|
|
else {
|
2012-08-03 10:24:55 +00:00
|
|
|
new_sh_stream(demuxer, STREAM_SUB, id, sid);
|
2008-04-12 15:51:08 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
|
|
|
|
}
|
|
|
|
return demuxer->s_streams[id];
|
2006-11-13 16:15:23 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 12:42:25 +00:00
|
|
|
struct sh_sub *new_sh_sub_sid_lang(struct demuxer *demuxer, int id, int sid,
|
|
|
|
const char *lang)
|
|
|
|
{
|
|
|
|
struct sh_sub *sh = new_sh_sub_sid(demuxer, id, sid);
|
|
|
|
if (lang && lang[0] && strcmp(lang, "und")) {
|
|
|
|
sh->lang = talloc_strdup(sh, lang);
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid, lang);
|
|
|
|
}
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2008-08-12 10:58:29 +00:00
|
|
|
static void free_sh_sub(sh_sub_t *sh)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2007-06-23 17:39:27 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
|
2008-02-29 17:25:41 +00:00
|
|
|
free(sh->extradata);
|
2010-01-24 15:19:14 +00:00
|
|
|
clear_parser((sh_common_t *)sh);
|
2011-07-03 11:48:00 +00:00
|
|
|
talloc_free(sh);
|
2007-06-23 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
|
|
|
|
{
|
|
|
|
if (id > MAX_A_STREAMS - 1 || id < 0) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_WARN,
|
|
|
|
"Requested audio stream id overflow (%d > %d)\n", id,
|
|
|
|
MAX_A_STREAMS);
|
|
|
|
return NULL;
|
2002-02-10 18:09:20 +00:00
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
if (demuxer->a_streams[id]) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Audio stream header %d redefined.\n", id);
|
2001-07-21 22:37:55 +00:00
|
|
|
} else {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found audio stream: %d\n", id);
|
2012-08-03 10:24:55 +00:00
|
|
|
new_sh_stream(demuxer, STREAM_AUDIO, id, aid);
|
2008-04-12 15:51:08 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
|
2001-07-21 22:37:55 +00:00
|
|
|
}
|
|
|
|
return demuxer->a_streams[id];
|
|
|
|
}
|
|
|
|
|
2012-11-07 23:30:30 +00:00
|
|
|
static void free_sh_audio(demuxer_t *demuxer, int id)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2006-06-14 14:05:59 +00:00
|
|
|
sh_audio_t *sh = demuxer->a_streams[id];
|
|
|
|
demuxer->a_streams[id] = NULL;
|
2008-04-12 15:51:08 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
|
2008-02-29 17:25:41 +00:00
|
|
|
free(sh->wf);
|
|
|
|
free(sh->codecdata);
|
2010-01-24 15:19:14 +00:00
|
|
|
clear_parser((sh_common_t *)sh);
|
2011-07-03 11:48:00 +00:00
|
|
|
talloc_free(sh);
|
2001-08-22 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
|
|
|
|
{
|
|
|
|
if (id > MAX_V_STREAMS - 1 || id < 0) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_WARN,
|
|
|
|
"Requested video stream id overflow (%d > %d)\n", id,
|
|
|
|
MAX_V_STREAMS);
|
|
|
|
return NULL;
|
2002-02-10 18:09:20 +00:00
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
if (demuxer->v_streams[id])
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
|
2008-04-12 15:51:08 +00:00
|
|
|
else {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
|
2012-08-03 10:24:55 +00:00
|
|
|
new_sh_stream(demuxer, STREAM_VIDEO, id, vid);
|
2008-04-12 15:51:08 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
|
2001-07-21 22:37:55 +00:00
|
|
|
}
|
|
|
|
return demuxer->v_streams[id];
|
|
|
|
}
|
|
|
|
|
2012-11-07 23:30:30 +00:00
|
|
|
static void free_sh_video(sh_video_t *sh)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
|
2008-02-29 17:25:41 +00:00
|
|
|
free(sh->bih);
|
2010-01-24 15:19:14 +00:00
|
|
|
clear_parser((sh_common_t *)sh);
|
2011-07-03 11:48:00 +00:00
|
|
|
talloc_free(sh);
|
2001-08-22 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
void free_demuxer(demuxer_t *demuxer)
|
|
|
|
{
|
2001-08-22 23:54:57 +00:00
|
|
|
int i;
|
2010-02-20 17:50:56 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing %s demuxer at %p\n",
|
|
|
|
demuxer->desc->shortdesc, demuxer);
|
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:
|
2008-04-12 15:51:08 +00:00
|
|
|
for (i = 0; i < MAX_A_STREAMS; i++)
|
|
|
|
if (demuxer->a_streams[i])
|
|
|
|
free_sh_audio(demuxer, i);
|
|
|
|
for (i = 0; i < MAX_V_STREAMS; i++)
|
|
|
|
if (demuxer->v_streams[i])
|
|
|
|
free_sh_video(demuxer->v_streams[i]);
|
|
|
|
for (i = 0; i < MAX_S_STREAMS; i++)
|
|
|
|
if (demuxer->s_streams[i])
|
|
|
|
free_sh_sub(demuxer->s_streams[i]);
|
2001-08-22 23:54:57 +00:00
|
|
|
// free demuxers:
|
|
|
|
free_demuxer_stream(demuxer->audio);
|
|
|
|
free_demuxer_stream(demuxer->video);
|
2002-03-31 02:45:14 +00:00
|
|
|
free_demuxer_stream(demuxer->sub);
|
2008-02-29 17:25:41 +00:00
|
|
|
free(demuxer->filename);
|
2009-03-16 03:11:22 +00:00
|
|
|
talloc_free(demuxer);
|
2001-08-22 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
2001-07-21 22:37:55 +00:00
|
|
|
|
2010-12-20 04:42:04 +00:00
|
|
|
void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2012-12-08 12:59:49 +00:00
|
|
|
// demux API can't handle 0-sized packets, but at least some vobsubs
|
|
|
|
// generate them. Skipping them seems to work fine. Not skipping them will
|
|
|
|
// stop demuxing with external vobsubs. See FATE sub/vobsub.{idx,sub} at
|
|
|
|
// pts=185.91.
|
|
|
|
if (dp->len == 0 && ds->stream_type == STREAM_SUB) {
|
|
|
|
mp_dbg(MSGT_DEMUXER, MSGL_INFO, "Discarding empty subtitle packet.\n");
|
|
|
|
free_demux_packet(dp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// append packet to DS stream:
|
|
|
|
++ds->packs;
|
2008-04-12 15:51:08 +00:00
|
|
|
ds->bytes += dp->len;
|
|
|
|
if (ds->last) {
|
|
|
|
// next packet in stream
|
|
|
|
ds->last->next = dp;
|
|
|
|
ds->last = dp;
|
2001-02-24 20:28:24 +00:00
|
|
|
} else {
|
2008-04-12 15:51:08 +00:00
|
|
|
// first packet in stream
|
|
|
|
ds->first = ds->last = dp;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
|
|
|
|
"DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
|
|
|
|
(ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
|
|
|
|
dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
|
|
|
|
ds->demuxer->video->packs);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2009-12-27 15:28:01 +00:00
|
|
|
static void allocate_parser(AVCodecContext **avctx, AVCodecParserContext **parser, unsigned format)
|
|
|
|
{
|
|
|
|
enum CodecID codec_id = CODEC_ID_NONE;
|
2010-09-04 08:11:31 +00:00
|
|
|
|
2009-12-27 15:28:01 +00:00
|
|
|
switch (format) {
|
2010-11-06 15:41:44 +00:00
|
|
|
case MKTAG('M', 'P', '4', 'L'):
|
|
|
|
codec_id = CODEC_ID_AAC_LATM;
|
|
|
|
break;
|
2009-12-27 15:28:01 +00:00
|
|
|
case 0x2000:
|
|
|
|
case 0x332D6361:
|
|
|
|
case 0x332D4341:
|
2010-11-07 11:00:00 +00:00
|
|
|
case 0x20736D:
|
2009-12-27 15:28:01 +00:00
|
|
|
case MKTAG('s', 'a', 'c', '3'):
|
|
|
|
codec_id = CODEC_ID_AC3;
|
|
|
|
break;
|
2011-03-05 12:03:03 +00:00
|
|
|
case MKTAG('d', 'n', 'e', 't'):
|
|
|
|
// DNET/byte-swapped AC-3 - there is no parser for that yet
|
|
|
|
//codec_id = CODEC_ID_DNET;
|
|
|
|
break;
|
2009-12-27 15:28:01 +00:00
|
|
|
case MKTAG('E', 'A', 'C', '3'):
|
|
|
|
codec_id = CODEC_ID_EAC3;
|
|
|
|
break;
|
|
|
|
case 0x2001:
|
|
|
|
case 0x86:
|
|
|
|
codec_id = CODEC_ID_DTS;
|
|
|
|
break;
|
2010-12-09 00:50:17 +00:00
|
|
|
case MKTAG('f', 'L', 'a', 'C'):
|
|
|
|
codec_id = CODEC_ID_FLAC;
|
|
|
|
break;
|
2010-01-24 20:05:28 +00:00
|
|
|
case MKTAG('M', 'L', 'P', ' '):
|
|
|
|
codec_id = CODEC_ID_MLP;
|
|
|
|
break;
|
2009-12-27 15:28:01 +00:00
|
|
|
case 0x55:
|
|
|
|
case 0x5500736d:
|
2010-11-07 11:00:00 +00:00
|
|
|
case 0x55005354:
|
2009-12-27 15:28:01 +00:00
|
|
|
case MKTAG('.', 'm', 'p', '3'):
|
2010-11-07 10:55:01 +00:00
|
|
|
case MKTAG('M', 'P', '3', ' '):
|
2009-12-27 15:28:01 +00:00
|
|
|
case MKTAG('L', 'A', 'M', 'E'):
|
|
|
|
codec_id = CODEC_ID_MP3;
|
|
|
|
break;
|
|
|
|
case 0x50:
|
2010-11-07 11:00:00 +00:00
|
|
|
case 0x5000736d:
|
2009-12-27 15:28:01 +00:00
|
|
|
case MKTAG('.', 'm', 'p', '2'):
|
|
|
|
case MKTAG('.', 'm', 'p', '1'):
|
|
|
|
codec_id = CODEC_ID_MP2;
|
|
|
|
break;
|
2010-01-24 20:05:28 +00:00
|
|
|
case MKTAG('T', 'R', 'H', 'D'):
|
|
|
|
codec_id = CODEC_ID_TRUEHD;
|
|
|
|
break;
|
2009-12-27 15:28:01 +00:00
|
|
|
}
|
|
|
|
if (codec_id != CODEC_ID_NONE) {
|
2012-01-28 11:41:36 +00:00
|
|
|
*avctx = avcodec_alloc_context3(NULL);
|
2009-12-27 15:28:01 +00:00
|
|
|
if (!*avctx)
|
|
|
|
return;
|
|
|
|
*parser = av_parser_init(codec_id);
|
|
|
|
if (!*parser)
|
|
|
|
av_freep(avctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_parser(sh_common_t *sh, AVCodecContext **avctx, AVCodecParserContext **parser)
|
|
|
|
{
|
|
|
|
*avctx = NULL;
|
|
|
|
*parser = NULL;
|
|
|
|
|
|
|
|
if (!sh || !sh->needs_parsing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*avctx = sh->avctx;
|
|
|
|
*parser = sh->parser;
|
|
|
|
if (*parser)
|
|
|
|
return;
|
|
|
|
|
|
|
|
allocate_parser(avctx, parser, sh->format);
|
|
|
|
sh->avctx = *avctx;
|
|
|
|
sh->parser = *parser;
|
|
|
|
}
|
|
|
|
|
2012-11-18 19:46:12 +00:00
|
|
|
int ds_parse(demux_stream_t *ds, uint8_t **buffer, int *len, double pts, int64_t pos)
|
2009-12-27 15:28:01 +00:00
|
|
|
{
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
AVCodecParserContext *parser;
|
|
|
|
get_parser(ds->sh, &avctx, &parser);
|
|
|
|
if (!parser)
|
|
|
|
return *len;
|
|
|
|
return av_parser_parse2(parser, avctx, buffer, len, *buffer, *len, pts, pts, pos);
|
|
|
|
}
|
2010-01-24 15:19:14 +00:00
|
|
|
|
|
|
|
static void clear_parser(sh_common_t *sh)
|
|
|
|
{
|
|
|
|
av_parser_close(sh->parser);
|
|
|
|
sh->parser = NULL;
|
|
|
|
av_freep(&sh->avctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ds_clear_parser(demux_stream_t *ds)
|
|
|
|
{
|
|
|
|
if (!ds->sh)
|
|
|
|
return;
|
|
|
|
clear_parser(ds->sh);
|
|
|
|
}
|
2009-12-27 15:28:01 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
|
2012-11-18 19:46:12 +00:00
|
|
|
double pts, int64_t pos, bool keyframe)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
|
|
|
demux_packet_t *dp = new_demux_packet(len);
|
|
|
|
len = stream_read(stream, dp->buffer, len);
|
2003-09-20 12:50:25 +00:00
|
|
|
resize_demux_packet(dp, len);
|
2008-04-12 15:51:08 +00:00
|
|
|
dp->pts = pts;
|
|
|
|
dp->pos = pos;
|
2012-07-24 21:23:27 +00:00
|
|
|
dp->keyframe = keyframe;
|
2001-02-24 20:28:24 +00:00
|
|
|
// append packet to DS stream:
|
2008-04-12 15:51:08 +00:00
|
|
|
ds_add_packet(ds, dp);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return value:
|
|
|
|
// 0 = EOF or no stream found or invalid type
|
|
|
|
// 1 = successfully read a packet
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
|
|
|
|
{
|
|
|
|
// Note: parameter 'ds' can be NULL!
|
|
|
|
return demux->desc->fill_buffer(demux, ds);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return value:
|
|
|
|
// 0 = EOF
|
2007-12-01 13:35:25 +00:00
|
|
|
// 1 = successful
|
2008-04-12 15:51:08 +00:00
|
|
|
int ds_fill_buffer(demux_stream_t *ds)
|
|
|
|
{
|
|
|
|
demuxer_t *demux = ds->demuxer;
|
|
|
|
if (ds->current)
|
|
|
|
free_demux_packet(ds->current);
|
|
|
|
ds->current = NULL;
|
2011-06-30 20:05:50 +00:00
|
|
|
mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer (%s) called\n",
|
|
|
|
ds == demux->audio ? "d_audio" : ds == demux->video ? "d_video" :
|
|
|
|
ds == demux->sub ? "d_sub" : "unknown");
|
2008-04-12 15:51:08 +00:00
|
|
|
while (1) {
|
2011-01-27 20:37:51 +00:00
|
|
|
int apacks = demux->audio ? demux->audio->packs : 0;
|
|
|
|
int abytes = demux->audio ? demux->audio->bytes : 0;
|
|
|
|
int vpacks = demux->video ? demux->video->packs : 0;
|
|
|
|
int vbytes = demux->video ? demux->video->bytes : 0;
|
2008-04-12 15:51:08 +00:00
|
|
|
if (ds->packs) {
|
|
|
|
demux_packet_t *p = ds->first;
|
2011-01-27 20:37:51 +00:00
|
|
|
// obviously not yet EOF after all
|
|
|
|
ds->eof = 0;
|
|
|
|
ds->fill_count = 0;
|
2008-04-12 15:51:08 +00:00
|
|
|
// copy useful data:
|
|
|
|
ds->buffer = p->buffer;
|
|
|
|
ds->buffer_pos = 0;
|
|
|
|
ds->buffer_size = p->len;
|
|
|
|
ds->pos = p->pos;
|
|
|
|
ds->dpos += p->len; // !!!
|
|
|
|
++ds->pack_no;
|
|
|
|
if (p->pts != MP_NOPTS_VALUE) {
|
|
|
|
ds->pts = p->pts;
|
|
|
|
ds->pts_bytes = 0;
|
|
|
|
}
|
|
|
|
ds->pts_bytes += p->len; // !!!
|
|
|
|
if (p->stream_pts != MP_NOPTS_VALUE)
|
|
|
|
demux->stream_pts = p->stream_pts;
|
2012-07-24 21:23:27 +00:00
|
|
|
ds->keyframe = p->keyframe;
|
2008-04-12 15:51:08 +00:00
|
|
|
// unlink packet:
|
|
|
|
ds->bytes -= p->len;
|
|
|
|
ds->current = p;
|
|
|
|
ds->first = p->next;
|
|
|
|
if (!ds->first)
|
|
|
|
ds->last = NULL;
|
|
|
|
--ds->packs;
|
2008-10-04 04:21:21 +00:00
|
|
|
/* The code below can set ds->eof to 1 when another stream runs
|
|
|
|
* out of buffer space. That makes sense because in that situation
|
|
|
|
* the calling code should not count on being able to demux more
|
|
|
|
* packets from this stream.
|
|
|
|
* If however the situation improves and we're called again
|
|
|
|
* despite the eof flag then it's better to clear it to avoid
|
|
|
|
* weird behavior. */
|
|
|
|
ds->eof = 0;
|
2008-04-12 15:51:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-01-27 20:37:51 +00:00
|
|
|
// avoid buffering too far ahead in e.g. badly interleaved files
|
|
|
|
// or when one stream is shorter, without breaking large audio
|
|
|
|
// delay with well interleaved files.
|
|
|
|
// This needs to be enough for at least 1 second of packets
|
|
|
|
// since libavformat mov demuxer does not try to interleave
|
|
|
|
// with more than 1s precision.
|
|
|
|
if (ds->fill_count > 80)
|
|
|
|
break;
|
|
|
|
// avoid printing the "too many ..." message over and over
|
|
|
|
if (ds->eof)
|
|
|
|
break;
|
2009-07-06 22:15:02 +00:00
|
|
|
|
|
|
|
#define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
|
2012-10-28 19:23:15 +00:00
|
|
|
"For AVI files, try to force non-interleaved mode with the --demuxer=avi --avi-ni options.\n")
|
2009-07-06 22:15:02 +00:00
|
|
|
|
2011-01-27 20:37:51 +00:00
|
|
|
if (apacks >= MAX_PACKS || abytes >= MAX_PACK_BYTES) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
|
2011-01-27 20:37:51 +00:00
|
|
|
apacks, abytes);
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
|
2008-04-12 15:51:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-01-27 20:37:51 +00:00
|
|
|
if (vpacks >= MAX_PACKS || vbytes >= MAX_PACK_BYTES) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
|
2011-01-27 20:37:51 +00:00
|
|
|
vpacks, vbytes);
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
|
2008-04-12 15:51:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!demux_fill_buffer(demux, ds)) {
|
|
|
|
mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
|
|
|
|
"ds_fill_buffer()->demux_fill_buffer() failed\n");
|
|
|
|
break; // EOF
|
|
|
|
}
|
2011-01-27 20:37:51 +00:00
|
|
|
if (demux->audio)
|
|
|
|
ds->fill_count += demux->audio->packs - apacks;
|
|
|
|
if (demux->video && demux->video->packs > vpacks &&
|
|
|
|
// Empty packets or "skip" packets in e.g. AVI can cause issues.
|
|
|
|
demux->video->bytes > vbytes + 100 &&
|
|
|
|
// when video needs parsing we will have lots of video packets
|
|
|
|
// in-between audio packets, so ignore them in that case.
|
|
|
|
demux->video->sh && !((sh_video_t *)demux->video->sh)->needs_parsing)
|
|
|
|
ds->fill_count++;
|
2001-07-29 21:07:34 +00:00
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
ds->buffer_pos = ds->buffer_size = 0;
|
|
|
|
ds->buffer = NULL;
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_V,
|
|
|
|
"ds_fill_buffer: EOF reached (stream: %s) \n",
|
|
|
|
ds == demux->audio ? "audio" : "video");
|
|
|
|
ds->eof = 1;
|
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int bytes = 0;
|
|
|
|
while (len > 0) {
|
|
|
|
x = ds->buffer_size - ds->buffer_pos;
|
|
|
|
if (x == 0) {
|
|
|
|
if (!ds_fill_buffer(ds))
|
|
|
|
return bytes;
|
|
|
|
} else {
|
|
|
|
if (x > len)
|
|
|
|
x = len;
|
|
|
|
if (mem)
|
2012-11-09 00:31:49 +00:00
|
|
|
memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
|
2008-04-12 15:51:08 +00:00
|
|
|
bytes += x;
|
|
|
|
len -= x;
|
|
|
|
ds->buffer_pos += x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bytes;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 12:26:02 +00:00
|
|
|
/**
|
|
|
|
* \brief read data until the given 3-byte pattern is encountered, up to maxlen
|
|
|
|
* \param mem memory to read data into, may be NULL to discard data
|
|
|
|
* \param maxlen maximum number of bytes to read
|
|
|
|
* \param read number of bytes actually read
|
|
|
|
* \param pattern pattern to search for (lowest 8 bits are ignored)
|
|
|
|
* \return whether pattern was found
|
|
|
|
*/
|
|
|
|
int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
|
|
|
|
int *read, uint32_t pattern)
|
|
|
|
{
|
|
|
|
register uint32_t head = 0xffffff00;
|
|
|
|
register uint32_t pat = pattern & 0xffffff00;
|
|
|
|
int total_len = 0;
|
|
|
|
do {
|
|
|
|
register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
|
|
|
|
int len = ds->buffer_size - ds->buffer_pos;
|
|
|
|
register long pos = -len;
|
|
|
|
if (unlikely(pos >= 0)) { // buffer is empty
|
|
|
|
ds_fill_buffer(ds);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
head |= ds_buf[pos];
|
|
|
|
head <<= 8;
|
|
|
|
} while (++pos && head != pat);
|
|
|
|
len += pos;
|
|
|
|
if (total_len + len > maxlen)
|
|
|
|
len = maxlen - total_len;
|
|
|
|
len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
|
|
|
|
total_len += len;
|
|
|
|
} while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
|
|
|
|
if (read)
|
|
|
|
*read = total_len;
|
|
|
|
return total_len >= 3 && head == pat;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
void ds_free_packs(demux_stream_t *ds)
|
|
|
|
{
|
|
|
|
demux_packet_t *dp = ds->first;
|
|
|
|
while (dp) {
|
|
|
|
demux_packet_t *dn = dp->next;
|
|
|
|
free_demux_packet(dp);
|
|
|
|
dp = dn;
|
|
|
|
}
|
|
|
|
if (ds->asf_packet) {
|
|
|
|
// free unfinished .asf fragments:
|
|
|
|
free(ds->asf_packet->buffer);
|
|
|
|
free(ds->asf_packet);
|
|
|
|
ds->asf_packet = NULL;
|
|
|
|
}
|
|
|
|
ds->first = ds->last = NULL;
|
|
|
|
ds->packs = 0; // !!!!!
|
|
|
|
ds->bytes = 0;
|
|
|
|
if (ds->current)
|
|
|
|
free_demux_packet(ds->current);
|
|
|
|
ds->current = NULL;
|
|
|
|
ds->buffer = NULL;
|
|
|
|
ds->buffer_pos = ds->buffer_size;
|
2011-07-30 18:05:59 +00:00
|
|
|
ds->pts = MP_NOPTS_VALUE;
|
2008-04-12 15:51:08 +00:00
|
|
|
ds->pts_bytes = 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int ds_get_packet(demux_stream_t *ds, unsigned char **start)
|
|
|
|
{
|
2007-10-25 19:21:24 +00:00
|
|
|
int len;
|
2008-04-12 15:51:08 +00:00
|
|
|
if (ds->buffer_pos >= ds->buffer_size) {
|
|
|
|
if (!ds_fill_buffer(ds)) {
|
2001-02-24 20:28:24 +00:00
|
|
|
// EOF
|
|
|
|
*start = NULL;
|
|
|
|
return -1;
|
2008-04-12 15:51:08 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
len = ds->buffer_size - ds->buffer_pos;
|
2007-10-25 19:21:24 +00:00
|
|
|
*start = &ds->buffer[ds->buffer_pos];
|
2008-04-12 15:51:08 +00:00
|
|
|
ds->buffer_pos += len;
|
2007-10-25 19:21:24 +00:00
|
|
|
return len;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-04-20 23:00:11 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
|
2006-04-24 10:58:40 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
*pts = MP_NOPTS_VALUE;
|
2009-08-23 15:32:42 +00:00
|
|
|
len = ds_get_packet(ds, start);
|
|
|
|
if (len < 0)
|
|
|
|
return len;
|
2006-07-06 06:58:17 +00:00
|
|
|
// Return pts unless this read starts from the middle of a packet
|
2009-08-23 15:32:42 +00:00
|
|
|
if (len == ds->buffer_pos)
|
2008-04-12 15:51:08 +00:00
|
|
|
*pts = ds->current->pts;
|
2006-04-24 10:58:40 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
|
|
|
|
{
|
2007-10-25 19:21:24 +00:00
|
|
|
int len;
|
2008-04-12 15:51:08 +00:00
|
|
|
if (ds->buffer_pos >= ds->buffer_size) {
|
|
|
|
*start = NULL;
|
|
|
|
if (!ds->packs)
|
|
|
|
return -1; // no sub
|
|
|
|
if (!ds_fill_buffer(ds))
|
|
|
|
return -1; // EOF
|
2001-04-20 23:00:11 +00:00
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
len = ds->buffer_size - ds->buffer_pos;
|
2007-10-25 19:21:24 +00:00
|
|
|
*start = &ds->buffer[ds->buffer_pos];
|
2008-04-12 15:51:08 +00:00
|
|
|
ds->buffer_pos += len;
|
2007-10-25 19:21:24 +00:00
|
|
|
return len;
|
2001-04-20 23:00:11 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 03:54:48 +00:00
|
|
|
struct demux_packet *ds_get_packet2(struct demux_stream *ds, bool repeat_last)
|
2011-08-20 17:25:43 +00:00
|
|
|
{
|
|
|
|
// This shouldn't get used together with partial reads
|
2011-12-22 03:54:48 +00:00
|
|
|
assert(ds->buffer_pos == 0 || ds->buffer_pos >= ds->buffer_size);
|
|
|
|
if (!repeat_last)
|
|
|
|
ds_fill_buffer(ds);
|
2011-08-20 17:25:43 +00:00
|
|
|
ds->buffer_pos = ds->buffer_size;
|
|
|
|
return ds->current;
|
|
|
|
}
|
|
|
|
|
2006-04-27 11:13:21 +00:00
|
|
|
double ds_get_next_pts(demux_stream_t *ds)
|
|
|
|
{
|
2008-04-12 15:51:08 +00:00
|
|
|
demuxer_t *demux = ds->demuxer;
|
2010-06-06 13:59:44 +00:00
|
|
|
// if we have not read from the "current" packet, consider it
|
|
|
|
// as the next, otherwise we never get the pts for the first packet.
|
|
|
|
while (!ds->first && (!ds->current || ds->buffer_pos)) {
|
2008-04-12 15:51:08 +00:00
|
|
|
if (demux->audio->packs >= MAX_PACKS
|
|
|
|
|| demux->audio->bytes >= MAX_PACK_BYTES) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
|
2008-04-12 15:51:08 +00:00
|
|
|
demux->audio->packs, demux->audio->bytes);
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
|
2008-04-12 15:51:08 +00:00
|
|
|
return MP_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
if (demux->video->packs >= MAX_PACKS
|
|
|
|
|| demux->video->bytes >= MAX_PACK_BYTES) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
|
2008-04-12 15:51:08 +00:00
|
|
|
demux->video->packs, demux->video->bytes);
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
|
2008-04-12 15:51:08 +00:00
|
|
|
return MP_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
if (!demux_fill_buffer(demux, ds))
|
|
|
|
return MP_NOPTS_VALUE;
|
2002-05-02 10:25:48 +00:00
|
|
|
}
|
2010-06-06 13:59:44 +00:00
|
|
|
// take pts from "current" if we never read from it.
|
|
|
|
if (ds->current && !ds->buffer_pos)
|
|
|
|
return ds->current->pts;
|
2008-04-12 15:51:08 +00:00
|
|
|
return ds->first->pts;
|
2002-05-02 10:25:48 +00:00
|
|
|
}
|
|
|
|
|
2001-07-21 22:37:55 +00:00
|
|
|
// ====================================================================
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
void demuxer_help(void)
|
|
|
|
{
|
2008-04-12 15:51:08 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
|
2011-02-11 17:52:44 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: info: (comment)\n");
|
2008-04-12 15:51:08 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
|
|
|
|
for (i = 0; demuxer_list[i]; i++) {
|
2011-02-11 17:52:44 +00:00
|
|
|
if (demuxer_list[i]->type >= DEMUXER_TYPE_END) // internal type
|
2008-04-12 15:51:08 +00:00
|
|
|
continue;
|
|
|
|
if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
|
2011-02-11 17:52:44 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %s (%s)\n",
|
|
|
|
demuxer_list[i]->name, demuxer_list[i]->info,
|
|
|
|
demuxer_list[i]->comment);
|
2008-04-12 15:51:08 +00:00
|
|
|
else
|
2011-02-11 17:52:44 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %s\n",
|
|
|
|
demuxer_list[i]->name, demuxer_list[i]->info);
|
2008-04-12 15:51:08 +00:00
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
}
|
2005-02-13 13:39:19 +00:00
|
|
|
|
2002-03-16 14:32:03 +00:00
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
/**
|
|
|
|
* Get demuxer type for a given demuxer name
|
|
|
|
*
|
|
|
|
* @param demuxer_name string with demuxer name of demuxer number
|
2005-08-30 23:56:24 +00:00
|
|
|
* @param force will be set if demuxer should be forced.
|
|
|
|
* May be NULL.
|
2005-08-05 19:57:47 +00:00
|
|
|
* @return DEMUXER_TYPE_xxx, -1 if error or not found
|
|
|
|
*/
|
2011-02-10 10:15:21 +00:00
|
|
|
static int get_demuxer_type_from_name(char *demuxer_name, int *force)
|
2005-08-05 19:57:47 +00:00
|
|
|
{
|
2008-04-12 15:51:08 +00:00
|
|
|
if (!demuxer_name || !demuxer_name[0])
|
|
|
|
return DEMUXER_TYPE_UNKNOWN;
|
|
|
|
if (force)
|
|
|
|
*force = demuxer_name[0] == '+';
|
|
|
|
if (demuxer_name[0] == '+')
|
|
|
|
demuxer_name = &demuxer_name[1];
|
2011-02-11 17:52:44 +00:00
|
|
|
for (int i = 0; demuxer_list[i]; i++) {
|
|
|
|
if (demuxer_list[i]->type >= DEMUXER_TYPE_END)
|
|
|
|
// Can't select special demuxers from commandline
|
2008-04-12 15:51:08 +00:00
|
|
|
continue;
|
|
|
|
if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
|
|
|
|
return demuxer_list[i]->type;
|
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return -1;
|
2005-08-05 19:57:47 +00:00
|
|
|
}
|
2001-12-27 21:24:56 +00:00
|
|
|
|
2010-11-10 12:37:15 +00:00
|
|
|
static struct demuxer *open_given_type(struct MPOpts *opts,
|
|
|
|
const struct demuxer_desc *desc,
|
|
|
|
struct stream *stream, bool force,
|
|
|
|
int audio_id, int video_id, int sub_id,
|
2011-08-04 05:38:39 +00:00
|
|
|
char *filename,
|
|
|
|
struct demuxer_params *params)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2010-11-10 12:37:15 +00:00
|
|
|
struct demuxer *demuxer;
|
|
|
|
int fformat;
|
2012-12-08 19:14:13 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_V, "Trying demuxer: %s\n", desc->name);
|
2010-11-10 12:37:15 +00:00
|
|
|
demuxer = new_demuxer(opts, stream, desc->type, audio_id,
|
|
|
|
video_id, sub_id, filename);
|
2011-08-04 05:38:39 +00:00
|
|
|
demuxer->params = params;
|
2010-11-10 12:37:15 +00:00
|
|
|
if (desc->check_file)
|
|
|
|
fformat = desc->check_file(demuxer);
|
|
|
|
else
|
|
|
|
fformat = desc->type;
|
|
|
|
if (force)
|
|
|
|
fformat = desc->type;
|
|
|
|
if (fformat == 0)
|
|
|
|
goto fail;
|
|
|
|
if (fformat == desc->type) {
|
2010-11-10 13:38:36 +00:00
|
|
|
if (demuxer->filetype)
|
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_INFO, "Detected file format: %s (%s)\n",
|
|
|
|
demuxer->filetype, desc->shortdesc);
|
|
|
|
else
|
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_INFO, "Detected file format: %s\n",
|
|
|
|
desc->shortdesc);
|
2010-11-10 12:37:15 +00:00
|
|
|
if (demuxer->desc->open) {
|
|
|
|
struct demuxer *demux2 = demuxer->desc->open(demuxer);
|
|
|
|
if (!demux2) {
|
|
|
|
mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Opening as detected format "
|
|
|
|
"\"%s\" failed.\n", desc->shortdesc);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* At least demux_mov can return a demux_demuxers instance
|
|
|
|
* from open() instead of the original fed in. */
|
|
|
|
demuxer = demux2;
|
|
|
|
}
|
|
|
|
demuxer->file_format = fformat;
|
2012-08-19 16:11:53 +00:00
|
|
|
opts->correct_pts = opts->user_correct_pts;
|
|
|
|
if (opts->correct_pts < 0)
|
|
|
|
opts->correct_pts =
|
|
|
|
demux_control(demuxer, DEMUXER_CTRL_CORRECT_PTS,
|
|
|
|
NULL) == DEMUXER_CTRL_OK;
|
2010-11-10 12:37:15 +00:00
|
|
|
return demuxer;
|
|
|
|
} else {
|
|
|
|
// demux_mov can return playlist instead of mov
|
|
|
|
if (fformat == DEMUXER_TYPE_PLAYLIST)
|
|
|
|
return demuxer; // handled in mplayer.c
|
|
|
|
/* Internal MPEG PS demuxer check can return other MPEG subtypes
|
|
|
|
* which don't have their own checks; recurse to try opening as
|
|
|
|
* the returned type instead. */
|
|
|
|
free_demuxer(demuxer);
|
|
|
|
desc = get_demuxer_desc_from_type(fformat);
|
|
|
|
if (!desc) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_ERR,
|
|
|
|
"BUG: recursion to nonexistent file format\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return open_given_type(opts, desc, stream, false, audio_id,
|
2011-08-04 05:38:39 +00:00
|
|
|
video_id, sub_id, filename, params);
|
2010-11-10 12:37:15 +00:00
|
|
|
}
|
|
|
|
fail:
|
|
|
|
free_demuxer(demuxer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
|
2012-08-19 16:11:53 +00:00
|
|
|
struct demuxer *demux_open_withparams(struct MPOpts *opts,
|
|
|
|
struct stream *stream, int file_format,
|
|
|
|
char *force_format, int audio_id,
|
|
|
|
int video_id, int sub_id, char *filename,
|
|
|
|
struct demuxer_params *params)
|
2010-11-10 12:37:15 +00:00
|
|
|
{
|
|
|
|
struct demuxer *demuxer = NULL;
|
|
|
|
const struct demuxer_desc *desc;
|
2008-04-12 15:51:08 +00:00
|
|
|
|
2012-08-19 16:11:53 +00:00
|
|
|
int force = 0;
|
|
|
|
int demuxer_type;
|
|
|
|
if ((demuxer_type = get_demuxer_type_from_name(force_format, &force)) < 0) {
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_ERR, "Demuxer %s does not exist.\n",
|
|
|
|
force_format);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (demuxer_type)
|
|
|
|
file_format = demuxer_type;
|
|
|
|
|
2012-07-30 16:40:46 +00:00
|
|
|
// Some code (e.g. dvd stuff, network code, or extension.c) explicitly
|
|
|
|
// request certain file formats. The list of formats are always handled by
|
|
|
|
// libavformat.
|
|
|
|
// Maybe attempts should be made to convert the mplayer format to the libav
|
|
|
|
// format, instead of reyling on libav to auto-detect the stream's format
|
|
|
|
// correctly.
|
|
|
|
switch (file_format) {
|
2012-09-07 12:26:02 +00:00
|
|
|
//case DEMUXER_TYPE_MPEG_PS:
|
2012-09-07 12:42:25 +00:00
|
|
|
//case DEMUXER_TYPE_MPEG_TS:
|
2012-07-30 16:40:46 +00:00
|
|
|
case DEMUXER_TYPE_Y4M:
|
|
|
|
case DEMUXER_TYPE_NSV:
|
|
|
|
case DEMUXER_TYPE_AAC:
|
|
|
|
case DEMUXER_TYPE_MPC:
|
|
|
|
file_format = DEMUXER_TYPE_LAVF;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
// If somebody requested a demuxer check it
|
|
|
|
if (file_format) {
|
2010-11-10 12:37:15 +00:00
|
|
|
desc = get_demuxer_desc_from_type(file_format);
|
|
|
|
if (!desc)
|
|
|
|
// should only happen with obsolete -demuxer 99 numeric format
|
2008-04-12 15:51:08 +00:00
|
|
|
return NULL;
|
2012-07-24 06:01:47 +00:00
|
|
|
return open_given_type(opts, desc, stream, force, audio_id,
|
|
|
|
video_id, sub_id, filename, params);
|
2006-10-20 19:36:59 +00:00
|
|
|
}
|
2010-11-10 12:37:15 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
// Test demuxers with safe file checks
|
2010-11-10 12:37:15 +00:00
|
|
|
for (int i = 0; (desc = demuxer_list[i]); i++) {
|
|
|
|
if (desc->safe_check) {
|
|
|
|
demuxer = open_given_type(opts, desc, stream, false, audio_id,
|
2011-08-04 05:38:39 +00:00
|
|
|
video_id, sub_id, filename, params);
|
2010-11-10 12:37:15 +00:00
|
|
|
if (demuxer)
|
2012-07-24 06:01:47 +00:00
|
|
|
return demuxer;
|
2006-10-19 18:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-19 00:21:54 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
// Ok. We're over the stable detectable fileformats, the next ones are
|
|
|
|
// a bit fuzzy. So by default (extension_parsing==1) try extension-based
|
|
|
|
// detection first:
|
2010-11-11 14:24:17 +00:00
|
|
|
if (filename && opts->extension_parsing == 1) {
|
2010-11-10 12:37:15 +00:00
|
|
|
desc = get_demuxer_desc_from_type(demuxer_type_by_filename(filename));
|
|
|
|
if (desc)
|
|
|
|
demuxer = open_given_type(opts, desc, stream, false, audio_id,
|
2011-08-04 05:38:39 +00:00
|
|
|
video_id, sub_id, filename, params);
|
2010-11-10 12:37:15 +00:00
|
|
|
if (demuxer)
|
2012-07-24 06:01:47 +00:00
|
|
|
return demuxer;
|
2008-04-12 15:51:08 +00:00
|
|
|
}
|
2010-11-10 12:37:15 +00:00
|
|
|
|
|
|
|
// Finally try detection for demuxers with unsafe checks
|
|
|
|
for (int i = 0; (desc = demuxer_list[i]); i++) {
|
|
|
|
if (!desc->safe_check && desc->check_file) {
|
|
|
|
demuxer = open_given_type(opts, desc, stream, false, audio_id,
|
2011-08-04 05:38:39 +00:00
|
|
|
video_id, sub_id, filename, params);
|
2010-11-10 12:37:15 +00:00
|
|
|
if (demuxer)
|
2012-07-24 06:01:47 +00:00
|
|
|
return demuxer;
|
2006-10-19 18:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-12-26 16:34:06 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return NULL;
|
2001-07-21 22:37:55 +00:00
|
|
|
}
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2011-08-04 05:38:39 +00:00
|
|
|
struct demuxer *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
|
|
|
|
int audio_id, int video_id, int sub_id,
|
|
|
|
char *filename)
|
|
|
|
{
|
2012-08-19 16:11:53 +00:00
|
|
|
return demux_open_withparams(opts, vs, file_format, opts->demuxer_name,
|
|
|
|
audio_id, video_id, sub_id, filename, NULL);
|
2011-08-04 05:38:39 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 05:10:48 +00:00
|
|
|
void demux_flush(demuxer_t *demuxer)
|
|
|
|
{
|
2008-06-02 10:17:48 +00:00
|
|
|
ds_free_packs(demuxer->video);
|
|
|
|
ds_free_packs(demuxer->audio);
|
|
|
|
ds_free_packs(demuxer->sub);
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
if (!demuxer->seekable) {
|
|
|
|
if (demuxer->file_format == DEMUXER_TYPE_AVI)
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_TV
|
2008-04-12 15:51:08 +00:00
|
|
|
else if (demuxer->file_format == DEMUXER_TYPE_TV)
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
|
2001-11-09 23:46:06 +00:00
|
|
|
#endif
|
2008-04-12 15:51:08 +00:00
|
|
|
else
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_SEEK, MSGL_WARN, "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;
|
|
|
|
|
2001-08-12 17:28:16 +00:00
|
|
|
// clear demux buffers:
|
2008-06-04 05:10:48 +00:00
|
|
|
demux_flush(demuxer);
|
2008-04-12 15:51:08 +00:00
|
|
|
demuxer->video->eof = 0;
|
|
|
|
demuxer->audio->eof = 0;
|
2010-08-29 06:46:06 +00:00
|
|
|
demuxer->sub->eof = 0;
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2010-04-23 18:01:21 +00:00
|
|
|
/* HACK: assume any demuxer used with these streams can cope with
|
|
|
|
* the stream layer suddenly seeking to a different position under it
|
|
|
|
* (nothing actually implements DEMUXER_CTRL_RESYNC now).
|
|
|
|
*/
|
|
|
|
struct stream *stream = demuxer->stream;
|
2012-08-12 16:40:21 +00:00
|
|
|
if (stream->type == STREAMTYPE_DVD) {
|
2010-04-23 18:01:21 +00:00
|
|
|
double pts;
|
|
|
|
|
|
|
|
if (flags & SEEK_ABSOLUTE)
|
|
|
|
pts = 0.0f;
|
|
|
|
else {
|
|
|
|
if (demuxer->stream_pts == MP_NOPTS_VALUE)
|
|
|
|
goto dmx_seek;
|
|
|
|
pts = demuxer->stream_pts;
|
|
|
|
}
|
2006-11-07 22:54:28 +00:00
|
|
|
|
2010-04-23 18:01:21 +00:00
|
|
|
if (flags & SEEK_FACTOR) {
|
|
|
|
double tmp = 0;
|
|
|
|
if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH,
|
|
|
|
&tmp) == STREAM_UNSUPPORTED)
|
|
|
|
goto dmx_seek;
|
|
|
|
pts += tmp * rel_seek_secs;
|
|
|
|
} else
|
|
|
|
pts += rel_seek_secs;
|
|
|
|
|
|
|
|
if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts)
|
|
|
|
!= STREAM_UNSUPPORTED) {
|
|
|
|
demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-11-07 22:54:28 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
dmx_seek:
|
|
|
|
if (demuxer->desc->seek)
|
|
|
|
demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, 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
|
|
|
}
|
|
|
|
|
2006-07-13 05:03:43 +00:00
|
|
|
int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
|
2010-05-19 10:32:32 +00:00
|
|
|
{
|
2012-07-28 21:47:42 +00:00
|
|
|
return demux_info_add_bstr(demuxer, bstr0(opt), bstr0(param));
|
2010-05-19 10:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int demux_info_add_bstr(demuxer_t *demuxer, struct bstr opt, struct bstr param)
|
2001-11-21 16:14:14 +00:00
|
|
|
{
|
2002-02-14 13:04:35 +00:00
|
|
|
char **info = demuxer->info;
|
|
|
|
int n = 0;
|
2001-11-21 16:14:14 +00:00
|
|
|
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
for (n = 0; info && info[2 * n] != NULL; n++) {
|
2012-07-28 21:47:42 +00:00
|
|
|
if (!bstrcasecmp(opt, bstr0(info[2*n]))) {
|
|
|
|
if (!bstrcmp(param, bstr0(info[2*n + 1]))) {
|
2010-05-19 10:32:32 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %.*s set to unchanged value %.*s\n",
|
|
|
|
BSTR_P(opt), BSTR_P(param));
|
2009-09-14 08:29:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-05-19 10:32:32 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %.*s changed to %.*s\n",
|
|
|
|
BSTR_P(opt), BSTR_P(param));
|
|
|
|
talloc_free(info[2*n + 1]);
|
|
|
|
info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
|
2008-04-12 15:51:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-19 10:32:32 +00:00
|
|
|
info = demuxer->info = talloc_realloc(demuxer, info, char *, 2 * (n + 2));
|
|
|
|
info[2*n] = talloc_strndup(demuxer->info, opt.start, opt.len);
|
|
|
|
info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
|
2008-04-12 15:51:08 +00:00
|
|
|
memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
|
2001-11-22 15:43:24 +00:00
|
|
|
|
2002-02-14 13:04:35 +00:00
|
|
|
return 1;
|
2001-11-21 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int demux_info_print(demuxer_t *demuxer)
|
|
|
|
{
|
2002-02-14 13:04:35 +00:00
|
|
|
char **info = demuxer->info;
|
|
|
|
int n;
|
2001-11-21 16:14:14 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
if (!info)
|
|
|
|
return 0;
|
|
|
|
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
|
2008-04-12 15:51:08 +00:00
|
|
|
for (n = 0; info[2 * n] != NULL; n++) {
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
|
|
|
|
info[2 * n + 1]);
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
|
|
|
|
info[2 * n]);
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
|
|
|
|
info[2 * n + 1]);
|
2005-06-20 23:07:35 +00:00
|
|
|
}
|
2006-04-24 07:20:34 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
|
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)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char **info = demuxer->info;
|
2002-02-20 16:59:25 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
for (i = 0; info && info[2 * i] != NULL; i++) {
|
|
|
|
if (!strcasecmp(opt, info[2 * i]))
|
|
|
|
return info[2 * i + 1];
|
|
|
|
}
|
2002-02-20 16:59:25 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return NULL;
|
2002-02-20 16:59:25 +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);
|
|
|
|
int index = stream ? stream->tid : -2;
|
|
|
|
if (type == STREAM_AUDIO) {
|
2012-09-23 12:59:36 +00:00
|
|
|
if (demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index)
|
|
|
|
== DEMUXER_CTRL_NOTIMPL)
|
|
|
|
demuxer->audio->id = index;
|
2012-08-19 16:01:30 +00:00
|
|
|
} else if (type == STREAM_VIDEO) {
|
2012-09-23 12:59:36 +00:00
|
|
|
if (demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index)
|
|
|
|
== DEMUXER_CTRL_NOTIMPL)
|
|
|
|
demuxer->video->id = index;
|
2012-08-19 16:01:30 +00:00
|
|
|
} else if (type == STREAM_SUB) {
|
|
|
|
int index2 = stream ? stream->stream_index : -2;
|
|
|
|
if (demuxer->ds[type]->id != index2)
|
|
|
|
ds_free_packs(demuxer->ds[type]);
|
|
|
|
demuxer->ds[type]->id = index2;
|
2011-03-30 20:22:45 +00:00
|
|
|
}
|
2012-08-19 16:01:30 +00:00
|
|
|
int new_id = demuxer->ds[type]->id;
|
|
|
|
void *new = NULL;
|
|
|
|
if (new_id >= 0) {
|
|
|
|
switch (type) {
|
|
|
|
case STREAM_VIDEO: new = demuxer->v_streams[new_id]; break;
|
|
|
|
case STREAM_AUDIO: new = demuxer->a_streams[new_id]; break;
|
|
|
|
case STREAM_SUB: new = demuxer->s_streams[new_id]; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
demuxer->ds[type]->sh = new;
|
2006-11-16 21:23:06 +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;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void demuxer_sort_chapters(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
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,
|
2010-01-26 01:31:31 +00:00
|
|
|
uint64_t start, uint64_t end)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2010-01-26 01:31:31 +00:00
|
|
|
if (!(demuxer->num_chapters % 32))
|
|
|
|
demuxer->chapters = talloc_realloc(demuxer, demuxer->chapters,
|
|
|
|
struct demux_chapter,
|
|
|
|
demuxer->num_chapters + 32);
|
2006-08-06 18:55:34 +00:00
|
|
|
|
|
|
|
demuxer->chapters[demuxer->num_chapters].start = start;
|
|
|
|
demuxer->chapters[demuxer->num_chapters].end = end;
|
2010-05-19 10:44:37 +00:00
|
|
|
demuxer->chapters[demuxer->num_chapters].name = name.len ?
|
|
|
|
talloc_strndup(demuxer->chapters, name.start, name.len) :
|
2010-01-26 01:31:31 +00:00
|
|
|
talloc_strdup(demuxer->chapters, mp_gtext("unknown"));
|
2006-08-06 18:55:34 +00:00
|
|
|
|
2012-02-26 20:12:53 +00:00
|
|
|
demuxer->num_chapters++;
|
|
|
|
|
|
|
|
if (demuxer->num_chapters > 1
|
|
|
|
&& demuxer->chapters[demuxer->num_chapters - 2].start
|
|
|
|
< demuxer->chapters[demuxer->num_chapters - 1].start)
|
|
|
|
demuxer_sort_chapters(demuxer);
|
|
|
|
|
|
|
|
return 0;
|
2006-08-06 18:55:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-18 16:48:22 +00:00
|
|
|
/**
|
2008-04-12 15:51:08 +00:00
|
|
|
* \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
|
2006-08-18 16:48:22 +00:00
|
|
|
* either using the demuxer->chapters structure set by the demuxer
|
|
|
|
* or asking help to the stream layer (e.g. dvd)
|
2006-08-18 19:03:59 +00:00
|
|
|
* \param chapter - chapter number wished - 0-based
|
2006-08-18 16:48:22 +00:00
|
|
|
* \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
|
|
|
|
* \return -1 on error, current chapter if successful
|
|
|
|
*/
|
|
|
|
|
2011-10-23 02:51:44 +00:00
|
|
|
int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2006-08-18 16:48:22 +00:00
|
|
|
int ris;
|
|
|
|
|
|
|
|
if (!demuxer->num_chapters || !demuxer->chapters) {
|
2008-06-02 10:17:48 +00:00
|
|
|
demux_flush(demuxer);
|
2006-08-18 16:48:22 +00:00
|
|
|
|
2008-06-16 12:54:28 +00:00
|
|
|
ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
|
|
|
|
&chapter);
|
2008-04-12 15:51:08 +00:00
|
|
|
if (ris != STREAM_UNSUPPORTED)
|
2006-11-11 11:53:32 +00:00
|
|
|
demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
|
2006-08-18 16:48:22 +00:00
|
|
|
|
2008-06-16 12:54:28 +00:00
|
|
|
// exit status may be ok, but main() doesn't have to seek itself
|
2008-04-12 15:51:08 +00:00
|
|
|
// (because e.g. dvds depend on sectors, not on pts)
|
2006-08-18 16:48:22 +00:00
|
|
|
*seek_pts = -1.0;
|
2006-08-21 19:19:00 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return ris != STREAM_UNSUPPORTED ? chapter : -1;
|
|
|
|
} else { // chapters structure is set in the demuxer
|
2009-04-01 16:55:26 +00:00
|
|
|
if (chapter >= demuxer->num_chapters)
|
2008-04-12 15:51:08 +00:00
|
|
|
return -1;
|
2009-04-01 16:55:26 +00:00
|
|
|
if (chapter < 0)
|
|
|
|
chapter = 0;
|
2006-08-18 16:48:22 +00:00
|
|
|
|
2011-01-23 22:29:01 +00:00
|
|
|
*seek_pts = demuxer->chapters[chapter].start / 1e9;
|
2006-08-18 16:48:22 +00:00
|
|
|
|
2009-04-01 16:55:26 +00:00
|
|
|
return chapter;
|
2006-08-18 16:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-13 12:51:05 +00:00
|
|
|
|
2010-11-13 13:48:26 +00:00
|
|
|
int demuxer_get_current_chapter(demuxer_t *demuxer, double time_now)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2010-04-24 17:46:54 +00:00
|
|
|
int chapter = -2;
|
2007-12-13 12:51:05 +00:00
|
|
|
if (!demuxer->num_chapters || !demuxer->chapters) {
|
|
|
|
if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
|
|
|
|
&chapter) == STREAM_UNSUPPORTED)
|
2010-04-24 17:46:54 +00:00
|
|
|
chapter = -2;
|
2008-04-12 15:51:08 +00:00
|
|
|
} else {
|
2011-01-23 22:29:01 +00:00
|
|
|
uint64_t now = time_now * 1e9 + 0.5;
|
2007-12-13 12:51:05 +00:00
|
|
|
for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
|
|
|
|
if (demuxer->chapters[chapter].start <= now)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return chapter;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
|
|
|
|
{
|
2007-12-13 12:51:05 +00:00
|
|
|
if (demuxer->num_chapters && demuxer->chapters) {
|
2008-04-12 15:51:08 +00:00
|
|
|
if (chapter >= 0 && chapter < demuxer->num_chapters
|
|
|
|
&& demuxer->chapters[chapter].name)
|
2011-10-23 02:51:44 +00:00
|
|
|
return talloc_strdup(NULL, demuxer->chapters[chapter].name);
|
2007-12-13 12:51:05 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
|
|
|
|
{
|
2007-12-13 12:51:05 +00:00
|
|
|
if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
|
2008-04-12 15:51:08 +00:00
|
|
|
&& chapter < demuxer->num_chapters) {
|
2007-12-13 12:51:05 +00:00
|
|
|
if (end)
|
2011-01-23 22:29:01 +00:00
|
|
|
*end = demuxer->chapters[chapter].end / 1e9;
|
|
|
|
return demuxer->chapters[chapter].start / 1e9;
|
2007-12-13 12:51:05 +00:00
|
|
|
}
|
|
|
|
return -1.0;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demuxer_chapter_count(demuxer_t *demuxer)
|
|
|
|
{
|
2007-12-13 12:51:05 +00:00
|
|
|
if (!demuxer->num_chapters || !demuxer->chapters) {
|
|
|
|
int num_chapters = 0;
|
|
|
|
if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
|
|
|
|
&num_chapters) == STREAM_UNSUPPORTED)
|
|
|
|
num_chapters = 0;
|
|
|
|
return num_chapters;
|
2008-04-12 15:51:08 +00:00
|
|
|
} else
|
2007-12-13 12:51:05 +00:00
|
|
|
return demuxer->num_chapters;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demuxer_angles_count(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
int ris, angles = -1;
|
2008-01-05 14:28:57 +00:00
|
|
|
|
|
|
|
ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
|
2008-04-12 15:51:08 +00:00
|
|
|
if (ris == STREAM_UNSUPPORTED)
|
|
|
|
return -1;
|
2008-01-05 14:28:57 +00:00
|
|
|
return angles;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demuxer_get_current_angle(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
int ris, curr_angle = -1;
|
2008-01-05 14:28:57 +00:00
|
|
|
ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
|
2008-04-12 15:51:08 +00:00
|
|
|
if (ris == STREAM_UNSUPPORTED)
|
|
|
|
return -1;
|
2008-01-05 14:28:57 +00:00
|
|
|
return curr_angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
int demuxer_set_angle(demuxer_t *demuxer, int angle)
|
|
|
|
{
|
|
|
|
int ris, angles = -1;
|
2008-01-05 14:28:57 +00:00
|
|
|
|
|
|
|
angles = demuxer_angles_count(demuxer);
|
2008-04-12 15:51:08 +00:00
|
|
|
if ((angles < 1) || (angle > angles))
|
|
|
|
return -1;
|
2008-01-05 14:28:57 +00:00
|
|
|
|
2008-06-02 10:17:48 +00:00
|
|
|
demux_flush(demuxer);
|
2008-01-05 14:28:57 +00:00
|
|
|
|
|
|
|
ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
|
2008-04-12 15:51:08 +00:00
|
|
|
if (ris == STREAM_UNSUPPORTED)
|
|
|
|
return -1;
|
2008-01-05 14:28:57 +00:00
|
|
|
|
|
|
|
demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
|
|
|
|
|
|
|
|
return angle;
|
|
|
|
}
|