From 5177b24b25931b83adea9de6f9c578654640346c Mon Sep 17 00:00:00 2001 From: Uoti Urpala Date: Thu, 10 Feb 2011 12:15:21 +0200 Subject: cleanup: demuxer.[ch]: remove unused code, make functions static Remove some unused lines from demuxer.h. Make some demuxer.c functions static. Move new_ds_stream() declaration from demuxer.h to stream.h (the function is defined in stream.c). Clean up some code in mplayer.c that had commented-out free_demuxer_stream() calls. --- libmpdemux/demuxer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libmpdemux/demuxer.c') diff --git a/libmpdemux/demuxer.c b/libmpdemux/demuxer.c index 6607f2399f..2383deb73d 100644 --- a/libmpdemux/demuxer.c +++ b/libmpdemux/demuxer.c @@ -253,13 +253,13 @@ void free_demux_packet(struct demux_packet *dp) free(dp); } -void free_demuxer_stream(demux_stream_t *ds) +static void free_demuxer_stream(struct demux_stream *ds) { ds_free_packs(ds); free(ds); } -demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id) +static struct demux_stream *new_demuxer_stream(struct demuxer *demuxer, int id) { demux_stream_t *ds = malloc(sizeof(demux_stream_t)); *ds = (demux_stream_t){ @@ -906,7 +906,7 @@ void demuxer_help(void) * May be NULL. * @return DEMUXER_TYPE_xxx, -1 if error or not found */ -int get_demuxer_type_from_name(char *demuxer_name, int *force) +static int get_demuxer_type_from_name(char *demuxer_name, int *force) { int i; long type_int; -- cgit v1.2.3 From f7643ddde624451b01f2cab688b733326b7b8ca5 Mon Sep 17 00:00:00 2001 From: Uoti Urpala Date: Fri, 11 Feb 2011 19:52:44 +0200 Subject: options: drop support for numeric -demuxer values Drop support for specifying demuxer types by numeric ID (options -demuxer, -audio-demuxer and -sub-demuxer). Stop printing the numeric values in "-demuxer help" output. Convert the list of DEMUXER_TYPE_XXX defines to "enum demuxer_type". --- DOCS/man/en/mplayer.1 | 8 +--- libmpdemux/demuxer.c | 31 +++++--------- libmpdemux/demuxer.h | 114 +++++++++++++++++++++++++------------------------- 3 files changed, 68 insertions(+), 85 deletions(-) (limited to 'libmpdemux/demuxer.c') diff --git a/DOCS/man/en/mplayer.1 b/DOCS/man/en/mplayer.1 index 91ae431846..82d53b5d6d 100644 --- a/DOCS/man/en/mplayer.1 +++ b/DOCS/man/en/mplayer.1 @@ -1177,9 +1177,7 @@ Plays a Matroska file in Japanese. Force audio demuxer type for \-audiofile. Use a '+' before the name to force it, this will skip some checks! Give the demuxer name as printed by \-audio\-demuxer help. -For backward compatibility it also accepts the demuxer ID as defined in -libmpdemux/\:demuxer.h. -\-audio\-demuxer audio or \-audio\-demuxer 17 forces MP3. +\-audio\-demuxer audio forces MP3. . .TP .B \-audiofile @@ -1356,8 +1354,6 @@ This nullifies stream delays. Force demuxer type. Use a '+' before the name to force it, this will skip some checks! Give the demuxer name as printed by \-demuxer help. -For backward compatibility it also accepts the demuxer ID as defined in -libmpdemux/\:demuxer.h. . .TP .B \-dumpaudio @@ -2416,8 +2412,6 @@ intensity of the color. Force subtitle demuxer type for \-subfile. Use a '+' before the name to force it, this will skip some checks! Give the demuxer name as printed by \-sub\-demuxer help. -For backward compatibility it also accepts the demuxer ID as defined in -subreader.h. . .TP .B \-sub\-fuzziness diff --git a/libmpdemux/demuxer.c b/libmpdemux/demuxer.c index 2383deb73d..bbcce939be 100644 --- a/libmpdemux/demuxer.c +++ b/libmpdemux/demuxer.c @@ -881,19 +881,18 @@ void demuxer_help(void) int i; mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n"); - mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n"); + mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: info: (comment)\n"); mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n"); for (i = 0; demuxer_list[i]; i++) { - if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers + if (demuxer_list[i]->type >= DEMUXER_TYPE_END) // internal type continue; if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment)) - mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n", - demuxer_list[i]->name, demuxer_list[i]->type, - demuxer_list[i]->info, demuxer_list[i]->comment); + mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %s (%s)\n", + demuxer_list[i]->name, demuxer_list[i]->info, + demuxer_list[i]->comment); else - mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n", - demuxer_list[i]->name, demuxer_list[i]->type, - demuxer_list[i]->info); + mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %s\n", + demuxer_list[i]->name, demuxer_list[i]->info); } } @@ -908,30 +907,20 @@ void demuxer_help(void) */ static int get_demuxer_type_from_name(char *demuxer_name, int *force) { - int i; - long type_int; - char *endptr; - 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]; - for (i = 0; demuxer_list[i]; i++) { - if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline + for (int i = 0; demuxer_list[i]; i++) { + if (demuxer_list[i]->type >= DEMUXER_TYPE_END) + // Can't select special demuxers from commandline continue; if (strcmp(demuxer_name, demuxer_list[i]->name) == 0) return demuxer_list[i]->type; } - // No match found, try to parse name as an integer (demuxer number) - type_int = strtol(demuxer_name, &endptr, 0); - if (*endptr) // Conversion failed - return -1; - if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX)) - return (int) type_int; - return -1; } diff --git a/libmpdemux/demuxer.h b/libmpdemux/demuxer.h index 86523c3df0..a4f62ec822 100644 --- a/libmpdemux/demuxer.h +++ b/libmpdemux/demuxer.h @@ -42,60 +42,60 @@ struct MPOpts; #define MAX_PACKS 4096 #define MAX_PACK_BYTES 0x8000000 // 128 MiB -#define DEMUXER_TYPE_UNKNOWN 0 -#define DEMUXER_TYPE_MPEG_ES 1 -#define DEMUXER_TYPE_MPEG_PS 2 -#define DEMUXER_TYPE_AVI 3 -#define DEMUXER_TYPE_AVI_NI 4 -#define DEMUXER_TYPE_AVI_NINI 5 -#define DEMUXER_TYPE_ASF 6 -#define DEMUXER_TYPE_MOV 7 -#define DEMUXER_TYPE_VIVO 8 -#define DEMUXER_TYPE_TV 9 -#define DEMUXER_TYPE_FLI 10 -#define DEMUXER_TYPE_REAL 11 -#define DEMUXER_TYPE_Y4M 12 -#define DEMUXER_TYPE_FILM 14 -#define DEMUXER_TYPE_ROQ 15 -#define DEMUXER_TYPE_MF 16 -#define DEMUXER_TYPE_AUDIO 17 -#define DEMUXER_TYPE_OGG 18 -#define DEMUXER_TYPE_RAWAUDIO 20 -#define DEMUXER_TYPE_RTP 21 -#define DEMUXER_TYPE_RAWDV 22 -#define DEMUXER_TYPE_PVA 23 -#define DEMUXER_TYPE_SMJPEG 24 -#define DEMUXER_TYPE_XMMS 25 -#define DEMUXER_TYPE_RAWVIDEO 26 -#define DEMUXER_TYPE_MPEG4_ES 27 -#define DEMUXER_TYPE_GIF 28 -#define DEMUXER_TYPE_MPEG_TS 29 -#define DEMUXER_TYPE_H264_ES 30 -#define DEMUXER_TYPE_MATROSKA 31 -#define DEMUXER_TYPE_REALAUDIO 32 -#define DEMUXER_TYPE_MPEG_TY 33 -#define DEMUXER_TYPE_LMLM4 34 -#define DEMUXER_TYPE_LAVF 35 -#define DEMUXER_TYPE_NSV 36 -#define DEMUXER_TYPE_VQF 37 -#define DEMUXER_TYPE_AVS 38 -#define DEMUXER_TYPE_AAC 39 -#define DEMUXER_TYPE_MPC 40 -#define DEMUXER_TYPE_MPEG_PES 41 -#define DEMUXER_TYPE_MPEG_GXF 42 -#define DEMUXER_TYPE_NUT 43 -#define DEMUXER_TYPE_LAVF_PREFERRED 44 -#define DEMUXER_TYPE_RTP_NEMESI 45 -#define DEMUXER_TYPE_MNG 46 - -// This should always match the higest demuxer type number. -// Unless you want to disallow users to force the demuxer to some types -#define DEMUXER_TYPE_MIN 0 -#define DEMUXER_TYPE_MAX 46 - -#define DEMUXER_TYPE_DEMUXERS (1 << 16) -// A virtual demuxer type for the network code -#define DEMUXER_TYPE_PLAYLIST (2 << 16) +enum demuxer_type { + DEMUXER_TYPE_UNKNOWN = 0, + DEMUXER_TYPE_MPEG_ES, + DEMUXER_TYPE_MPEG_PS, + DEMUXER_TYPE_AVI, + DEMUXER_TYPE_AVI_NI, + DEMUXER_TYPE_AVI_NINI, + DEMUXER_TYPE_ASF, + DEMUXER_TYPE_MOV, + DEMUXER_TYPE_VIVO, + DEMUXER_TYPE_TV, + DEMUXER_TYPE_FLI, + DEMUXER_TYPE_REAL, + DEMUXER_TYPE_Y4M, + DEMUXER_TYPE_FILM, + DEMUXER_TYPE_ROQ, + DEMUXER_TYPE_MF, + DEMUXER_TYPE_AUDIO, + DEMUXER_TYPE_OGG, + DEMUXER_TYPE_RAWAUDIO, + DEMUXER_TYPE_RTP, + DEMUXER_TYPE_RAWDV, + DEMUXER_TYPE_PVA, + DEMUXER_TYPE_SMJPEG, + DEMUXER_TYPE_XMMS, + DEMUXER_TYPE_RAWVIDEO, + DEMUXER_TYPE_MPEG4_ES, + DEMUXER_TYPE_GIF, + DEMUXER_TYPE_MPEG_TS, + DEMUXER_TYPE_H264_ES, + DEMUXER_TYPE_MATROSKA, + DEMUXER_TYPE_REALAUDIO, + DEMUXER_TYPE_MPEG_TY, + DEMUXER_TYPE_LMLM4, + DEMUXER_TYPE_LAVF, + DEMUXER_TYPE_NSV, + DEMUXER_TYPE_VQF, + DEMUXER_TYPE_AVS, + DEMUXER_TYPE_AAC, + DEMUXER_TYPE_MPC, + DEMUXER_TYPE_MPEG_PES, + DEMUXER_TYPE_MPEG_GXF, + DEMUXER_TYPE_NUT, + DEMUXER_TYPE_LAVF_PREFERRED, + DEMUXER_TYPE_RTP_NEMESI, + DEMUXER_TYPE_MNG, + + /* Values after this are for internal use and can not be selected + * as demuxer type by the user (-demuxer option). */ + DEMUXER_TYPE_END, + + DEMUXER_TYPE_DEMUXERS, + DEMUXER_TYPE_PLAYLIST, +}; enum timestamp_type { TIMESTAMP_TYPE_PTS, @@ -191,7 +191,7 @@ typedef struct demuxer_desc { const char *author; // Demuxer author(s) const char *comment; // Comment, printed with -demuxer help - int type; // DEMUXER_TYPE_xxx + enum demuxer_type type; // If 1 detection is safe and fast, do it before file extension check int safe_check; @@ -249,14 +249,14 @@ typedef struct demuxer { double reference_clock; char *filename; // Needed by avs_check_file int synced; // stream synced (used by mpeg) - int type; // DEMUXER_TYPE_xxx + enum demuxer_type type; /* Normally the file_format field is just a copy of the type field above. * There are 2 exceptions I noticed. Internal demux_avi may force * ->type to DEMUXER_TYPE_AVI_[NI|NINI] while leaving ->file_format at * DEMUXER_TYPE_AVI. Internal demux_mov may set ->type to * DEMUXER_TYPE_PLAYLIST and also return that from the check function * or not (looks potentially buggy). */ - int file_format; + enum demuxer_type file_format; int seekable; // flag /* Set if using absolute seeks for small movements is OK (no pts resets * that would make pts ambigious, preferably supports back/forward flags */ -- cgit v1.2.3 From 968154ba77f8e8974d6dd36d1109a473b3ff5b6c Mon Sep 17 00:00:00 2001 From: Uoti Urpala Date: Mon, 14 Feb 2011 13:05:35 +0200 Subject: 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 --- Makefile | 2 + libmpdemux/demux_edl.c | 58 +++++++ libmpdemux/demuxer.c | 2 + libmpdemux/demuxer.h | 3 + mp_core.h | 2 + mplayer.c | 26 ++++ timeline/tl_edl.c | 398 +++++++++++++++++++++++++++++++++++++++++++++++++ timeline/tl_matroska.c | 18 --- 8 files changed, 491 insertions(+), 18 deletions(-) create mode 100644 libmpdemux/demux_edl.c create mode 100644 timeline/tl_edl.c (limited to 'libmpdemux/demuxer.c') diff --git a/Makefile b/Makefile index 90802b0ae2..43d1937c25 100644 --- a/Makefile +++ b/Makefile @@ -389,6 +389,7 @@ SRCS_COMMON = asxparser.c \ libmpdemux/demux_audio.c \ libmpdemux/demux_avi.c \ libmpdemux/demux_demuxers.c \ + libmpdemux/demux_edl.c \ libmpdemux/demux_film.c \ libmpdemux/demux_fli.c \ libmpdemux/demux_lmlm4.c \ @@ -441,6 +442,7 @@ SRCS_COMMON = asxparser.c \ sub/subassconvert.c \ sub/subreader.c \ sub/vobsub.c \ + timeline/tl_edl.c \ timeline/tl_matroska.c \ $(SRCS_COMMON-yes) diff --git a/libmpdemux/demux_edl.c b/libmpdemux/demux_edl.c new file mode 100644 index 0000000000..4c864cfe42 --- /dev/null +++ b/libmpdemux/demux_edl.c @@ -0,0 +1,58 @@ +/* + * 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. + */ + +#include +#include +#include + +#include "demuxer.h" +#include "stream/stream.h" + +static int try_open_file(struct demuxer *demuxer) +{ + struct stream *s = demuxer->stream; + const char header[] = "mplayer EDL file"; + const int len = sizeof(header) - 1; + char buf[len]; + if (stream_read(s, buf, len) < len) + return 0; + if (strncmp(buf, header, len)) + return 0; + stream_seek(s, 0); + demuxer->file_contents = stream_read_complete(s, demuxer, 1000000, 0); + if (demuxer->file_contents.start == NULL) + return 0; + return DEMUXER_TYPE_EDL; +} + +static int dummy_fill_buffer(struct demuxer *demuxer, struct demux_stream *ds) +{ + return 0; +} + +const struct demuxer_desc demuxer_desc_edl = { + .info = "EDL file demuxer", + .name = "edl", + .shortdesc = "EDL", + .author = "Uoti Urpala", + .comment = "", + .type = DEMUXER_TYPE_EDL, + .safe_check = true, + .check_file = try_open_file, // no separate .open + .fill_buffer = dummy_fill_buffer, +}; diff --git a/libmpdemux/demuxer.c b/libmpdemux/demuxer.c index bbcce939be..07b929b588 100644 --- a/libmpdemux/demuxer.c +++ b/libmpdemux/demuxer.c @@ -53,6 +53,7 @@ static void clear_parser(sh_common_t *sh); // Demuxer list +extern const struct demuxer_desc demuxer_desc_edl; extern const demuxer_desc_t demuxer_desc_rawaudio; extern const demuxer_desc_t demuxer_desc_rawvideo; extern const demuxer_desc_t demuxer_desc_tv; @@ -101,6 +102,7 @@ extern const demuxer_desc_t demuxer_desc_mng; * libraries and demuxers requiring binary support. */ const demuxer_desc_t *const demuxer_list[] = { + &demuxer_desc_edl, &demuxer_desc_rawaudio, &demuxer_desc_rawvideo, #ifdef CONFIG_TV diff --git a/libmpdemux/demuxer.h b/libmpdemux/demuxer.h index a4f62ec822..4a8a7545d1 100644 --- a/libmpdemux/demuxer.h +++ b/libmpdemux/demuxer.h @@ -88,6 +88,7 @@ enum demuxer_type { DEMUXER_TYPE_LAVF_PREFERRED, DEMUXER_TYPE_RTP_NEMESI, DEMUXER_TYPE_MNG, + DEMUXER_TYPE_EDL, /* Values after this are for internal use and can not be selected * as demuxer type by the user (-demuxer option). */ @@ -282,6 +283,8 @@ typedef struct demuxer { int num_attachments; struct matroska_data matroska_data; + // for trivial demuxers which just read the whole file for codec to use + struct bstr file_contents; void *priv; // demuxer-specific internal data char **info; // metadata diff --git a/mp_core.h b/mp_core.h index 6f4d1d8557..bbb93bce48 100644 --- a/mp_core.h +++ b/mp_core.h @@ -254,5 +254,7 @@ void update_subtitles(struct MPContext *mpctx, double refpts, // timeline/tl_matroska.c void build_ordered_chapter_timeline(struct MPContext *mpctx); +// timeline/tl_edl.c +void build_edl_timeline(struct MPContext *mpctx); #endif /* MPLAYER_MP_CORE_H */ diff --git a/mplayer.c b/mplayer.c index 161f1c9a2c..2afbbbb821 100644 --- a/mplayer.c +++ b/mplayer.c @@ -4349,6 +4349,32 @@ if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST) if (mpctx->demuxer->matroska_data.ordered_chapters) build_ordered_chapter_timeline(mpctx); + if (mpctx->demuxer->type == DEMUXER_TYPE_EDL) + build_edl_timeline(mpctx); + + if (mpctx->timeline) { + mpctx->timeline_part = 0; + mpctx->demuxer = mpctx->timeline[0].source->demuxer; + + int part_count = mpctx->num_timeline_parts; + mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d " + "sources. Total length %.3f seconds.\n", part_count, + mpctx->num_sources, mpctx->timeline[part_count].start); + mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n"); + for (int i = 0; i < mpctx->num_sources; i++) + mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i, + filename_recode(mpctx->sources[i].demuxer->filename)); + mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, " + "source_start, source):\n"); + for (int i = 0; i < part_count; i++) { + struct timeline_part *p = mpctx->timeline + i; + mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start, + p->source_start, p->source - mpctx->sources); + } + mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", + mpctx->timeline[part_count].start); + } + if (!mpctx->sources) { mpctx->sources = talloc_ptrtype(NULL, mpctx->sources); *mpctx->sources = (struct content_source){.stream = mpctx->stream, diff --git a/timeline/tl_edl.c b/timeline/tl_edl.c new file mode 100644 index 0000000000..99b9ddad82 --- /dev/null +++ b/timeline/tl_edl.c @@ -0,0 +1,398 @@ +/* + * 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. + */ + +#include +#include +#include +#include + +#include "talloc.h" + +#include "mp_core.h" +#include "mp_msg.h" +#include "libmpdemux/demuxer.h" +#include "path.h" +#include "bstr.h" +#include "mpcommon.h" + + +struct edl_source { + struct bstr id; + char *filename; + int lineno; +}; + +struct edl_time { + int64_t start; + int64_t end; + bool implied_start; + bool implied_end; +}; + +struct edl_part { + struct edl_time tl; + struct edl_time src; + int64_t duration; + int id; + int lineno; +}; + +static int find_edl_source(struct edl_source *sources, int num_sources, + struct bstr name) +{ + for (int i = 0; i < num_sources; i++) + if (!bstrcmp(sources[i].id, name)) + return i; + return -1; +} + +void build_edl_timeline(struct MPContext *mpctx) +{ + const struct bstr file_prefix = BSTR("<"); + void *tmpmem = talloc_new(NULL); + + struct bstr *lines = bstr_splitlines(tmpmem, mpctx->demuxer->file_contents); + int linec = MP_TALLOC_ELEMS(lines); + struct bstr header = BSTR("mplayer EDL file, version "); + if (!linec || !bstr_startswith(lines[0], header)) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Bad EDL header!\n"); + goto out; + } + struct bstr version = bstr_strip(bstr_cut(lines[0], header.len)); + if (bstrcmp(BSTR("2"), version)) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Unsupported EDL file version!\n"); + goto out; + } + int num_sources = 0; + int num_parts = 0; + for (int i = 1; i < linec; i++) { + if (bstr_startswith(lines[i], file_prefix)) { + num_sources++; + } else { + int comment = bstrchr(lines[i], '#'); + if (comment >= 0) + lines[i] = bstr_splice(lines[i], 0, comment); + if (bstr_strip(lines[i]).len) + num_parts++; + } + } + if (!num_parts) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "No parts in timeline!\n"); + goto out; + } + + // Parse source filename definitions + + struct edl_source *edl_ids = talloc_array_ptrtype(tmpmem, edl_ids, + num_sources); + num_sources = 0; + for (int i = 1; i < linec; i++) { + struct bstr line = lines[i]; + if (!bstr_startswith(line, file_prefix)) + continue; + line = bstr_cut(line, file_prefix.len); + struct bstr id = bstr_split(line, WHITESPACE, &line); + if (find_edl_source(edl_ids, num_sources, id) >= 0) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Repeated ID on line %d!\n", + i+1); + goto out; + } + if (!isalpha(*id.start)) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Invalid ID on line %d!\n", + i+1); + goto out; + } + char *filename = mp_basename(bstrdup0(tmpmem, bstr_strip(line))); + if (!strlen(filename)) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, + "EDL: Invalid filename on line %d!\n", i+1); + goto out; + } + struct bstr dirname = mp_dirname(mpctx->demuxer->filename); + char *fullname = mp_path_join(tmpmem, dirname, BSTR(filename)); + edl_ids[num_sources++] = (struct edl_source){id, fullname, i+1}; + } + + // Parse timeline part definitions + + struct edl_part *parts = talloc_array_ptrtype(tmpmem, parts, num_parts); + int total_parts = num_parts; + num_parts = 0; + for (int i = 1; i < linec; i++) { + struct bstr line = bstr_strip(lines[i]); + if (!line.len || bstr_startswith(line, file_prefix)) + continue; + parts[num_parts] = (struct edl_part){{-1, -1}, {-1, -1}, 0, -1}; + parts[num_parts].lineno = i + 1; + for (int s = 0; s < 2; s++) { + struct edl_time *p = !s ? &parts[num_parts].tl : + &parts[num_parts].src; + while (1) { + struct bstr t = bstr_split(line, WHITESPACE, &line); + if (!t.len) { + if (!s && num_parts < total_parts - 1) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: missing source " + "identifier on line %d (not last)!\n", i+1); + goto out; + } + break; + } + if (isalpha(*t.start)) { + if (s) + goto bad; + parts[num_parts].id = find_edl_source(edl_ids, num_sources, + t); + if (parts[num_parts].id < 0) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Undefined source " + "identifier on line %d!\n", i+1); + goto out; + } + break; + } + while (t.len) { + struct bstr next; + struct bstr arg = bstr_split(t, "+-", &next); + if (!arg.len) { + next = bstr_split(line, WHITESPACE, &line); + arg = bstr_split(next, "+-", &next); + } + if (!arg.len) + goto bad; + int64_t val; + if (!bstrcmp(arg, BSTR("*"))) + val = -1; + else if (isdigit(*arg.start)) { + val = bstrtoll(arg, &arg, 10) * 1000000000; + if (arg.len && *arg.start == '.') { + int len = arg.len - 1; + arg = bstr_splice(arg, 1, 10); + int64_t val2 = bstrtoll(arg, &arg, 10); + if (arg.len) + goto bad; + for (; len < 9; len++) + val2 *= 10; + val += val2; + } + } else + goto bad; + int c = *t.start; + if (isdigit(c) || c == '*') { + if (val < 0) + p->implied_start = true; + else + p->start = val; + } else if (c == '-') { + if (val < 0) + p->implied_end = true; + else + p->end = val; + } else if (c == '+') { + if (val < 0) + goto bad; + if (val == 0) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: zero duration " + "on line %d!\n", i+1); + goto out; + } + parts[num_parts].duration = val; + } else + goto bad; + t = next; + } + } + } + num_parts++; + continue; + bad: + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Malformed line %d!\n", i+1); + goto out; + } + + // Fill in implied start/stop/duration values + + int64_t *times = talloc_zero_array(tmpmem, int64_t, num_sources); + while (1) { + int64_t time = 0; + for (int i = 0; i < num_parts; i++) { + for (int s = 0; s < 2; s++) { + struct edl_time *p = s ? &parts[i].tl : &parts[i].src; + if (!s && parts[i].id == -1) + continue; + int64_t *t = s ? &time : times + parts[i].id; + p->implied_start |= s && *t >= 0; + if (p->implied_start && p->start >= 0 && *t >= 0 + && p->start != *t) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line " + "%d!\n", parts[i].lineno); + goto out; + } + if (p->start >= 0) + *t = p->start; + if (p->implied_start) + p->start = *t; + if (*t >= 0 && parts[i].duration) + *t += parts[i].duration; + else + *t = -1; + if (p->end >= 0) + *t = p->end; + } + } + for (int i = 0; i < num_sources; i++) + times[i] = -1; + time = -1; + for (int i = num_parts - 1; i >= 0; i--) { + for (int s = 0; s < 2; s++) { + struct edl_time *p = s ? &parts[i].tl : &parts[i].src; + if (!s && parts[i].id == -1) + continue; + int64_t *t = s ? &time : times + parts[i].id; + p->implied_end |= s && *t >= 0; + if (p->implied_end && p->end >= 0 && *t >=0 && p->end != *t) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line " + "%d!\n", parts[i].lineno); + goto out; + } + if (p->end >= 0) + *t = p->end; + if (p->implied_end) + p->end = *t; + if (*t >= 0 && parts[i].duration) { + *t -= parts[i].duration; + if (t < 0) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Negative time " + "on line %d!\n", parts[i].lineno); + goto out; + } + } else + *t = -1; + if (p->start >= 0) + *t = p->start; + } + } + int missing_duration = -1; + int missing_srcstart = -1; + bool anything_done = false; + for (int i = 0; i < num_parts; i++) { + int64_t duration = parts[i].duration; + if (parts[i].tl.start >= 0 && parts[i].tl.end >= 0) { + int64_t duration2 = parts[i].tl.end - parts[i].tl.start; + if (duration && duration != duration2) + goto incons; + duration = duration2; + if (duration <= 0) + goto neg; + } + if (parts[i].src.start >= 0 && parts[i].src.end >= 0) { + int64_t duration2 = parts[i].src.end - parts[i].src.start; + if (duration && duration != duration2) { + incons: + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line " + "%d!\n", i+1); + goto out; + } + duration = duration2; + if (duration <= 0) { + neg: + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: duration <= 0 on " + "line %d!\n", parts[i].lineno); + goto out; + } + } + if (parts[i].id == -1) + continue; + if (!duration) + missing_duration = i; + else if (!parts[i].duration) + anything_done = true; + parts[i].duration = duration; + if (duration && parts[i].src.start < 0) + if (parts[i].src.end < 0) + missing_srcstart = i; + else + parts[i].src.start = parts[i].src.end - duration; + } + if (!anything_done) { + if (missing_duration >= 0) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not determine " + "duration for line %d!\n", + parts[missing_duration].lineno); + goto out; + } + if (missing_srcstart >= 0) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: no source start time for " + "line %d!\n", parts[missing_srcstart].lineno); + goto out; + } + break; + } + } + + // Open source files + + struct content_source *sources = talloc_array_ptrtype(NULL, sources, + num_sources + 1); + mpctx->sources = sources; + sources[0].stream = mpctx->stream; + sources[0].demuxer = mpctx->demuxer; + mpctx->num_sources = 1; + + for (int i = 0; i < num_sources; i++) { + int format = 0; + struct stream *s = open_stream(edl_ids[i].filename, &mpctx->opts, + &format); + if (!s) + goto openfail; + struct demuxer *d = demux_open(&mpctx->opts, s, format, + mpctx->opts.audio_id, + mpctx->opts.video_id, + mpctx->opts.sub_id, + edl_ids[i].filename); + if (!d) { + free_stream(s); + openfail: + mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not open source " + "file on line %d!\n", edl_ids[i].lineno); + goto out; + } + sources[mpctx->num_sources].stream = s; + sources[mpctx->num_sources].demuxer = d; + mpctx->num_sources++; + } + + // Write final timeline structure + + struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline, + num_parts + 1); + int64_t starttime = 0; + for (int i = 0; i < num_parts; i++) { + timeline[i].start = starttime / 1e9; + starttime += parts[i].duration; + timeline[i].source_start = parts[i].src.start / 1e9; + timeline[i].source = sources + parts[i].id + 1; + } + if (parts[num_parts - 1].id != -1) { + timeline[num_parts].start = starttime / 1e9; + num_parts++; + } + mpctx->timeline = timeline; + mpctx->num_timeline_parts = num_parts - 1; + + out: + talloc_free(tmpmem); +} diff --git a/timeline/tl_matroska.c b/timeline/tl_matroska.c index 46d0e33a45..03b6cc3dc4 100644 --- a/timeline/tl_matroska.c +++ b/timeline/tl_matroska.c @@ -259,31 +259,13 @@ void build_ordered_chapter_timeline(struct MPContext *mpctx) return; } - mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d " - "sources. Total length %.3f seconds.\n", part_count, num_sources, - timeline[part_count].start); if (missing_time) mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing " "from the timeline!\n", missing_time / 1e9); - mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n"); - for (int i = 0; i < num_sources; i++) - mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i, - filename_recode(sources[i].demuxer->filename)); - mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, " - "source_start, source):\n"); - for (int i = 0; i < part_count; i++) { - struct timeline_part *p = timeline + i; - mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start, - p->source_start, p->source - sources); - } - mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start); mpctx->sources = sources; mpctx->num_sources = num_sources; mpctx->timeline = timeline; mpctx->num_timeline_parts = part_count; mpctx->num_chapters = num_chapters; mpctx->chapters = chapters; - - mpctx->timeline_part = 0; - mpctx->demuxer = timeline[0].source->demuxer; } -- cgit v1.2.3