2015-05-19 19:36:21 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-05-19 19:36:21 +00:00
|
|
|
*
|
|
|
|
* mpv 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
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2015-05-19 19:36:21 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2015-05-19 19:36:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2015-05-19 19:36:21 +00:00
|
|
|
|
|
|
|
#include "misc/bstr.h"
|
|
|
|
#include "common/common.h"
|
2015-10-01 19:57:35 +00:00
|
|
|
#include "common/tags.h"
|
2015-05-19 19:36:21 +00:00
|
|
|
|
|
|
|
#include "cue.h"
|
|
|
|
|
|
|
|
#define SECS_PER_CUE_FRAME (1.0/75.0)
|
|
|
|
|
|
|
|
enum cue_command {
|
|
|
|
CUE_ERROR = -1, // not a valid CUE command, or an unknown extension
|
|
|
|
CUE_EMPTY, // line with whitespace only
|
|
|
|
CUE_UNUSED, // valid CUE command, but ignored by this code
|
|
|
|
CUE_FILE,
|
|
|
|
CUE_TRACK,
|
|
|
|
CUE_INDEX,
|
|
|
|
CUE_TITLE,
|
2015-10-01 19:57:35 +00:00
|
|
|
CUE_PERFORMER,
|
2015-05-19 19:36:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
enum cue_command command;
|
|
|
|
const char *text;
|
|
|
|
} cue_command_strings[] = {
|
|
|
|
{ CUE_FILE, "FILE" },
|
|
|
|
{ CUE_TRACK, "TRACK" },
|
|
|
|
{ CUE_INDEX, "INDEX" },
|
|
|
|
{ CUE_TITLE, "TITLE" },
|
|
|
|
{ CUE_UNUSED, "CATALOG" },
|
|
|
|
{ CUE_UNUSED, "CDTEXTFILE" },
|
|
|
|
{ CUE_UNUSED, "FLAGS" },
|
|
|
|
{ CUE_UNUSED, "ISRC" },
|
2015-10-01 19:57:35 +00:00
|
|
|
{ CUE_PERFORMER, "PERFORMER" },
|
2015-05-19 19:36:21 +00:00
|
|
|
{ CUE_UNUSED, "POSTGAP" },
|
|
|
|
{ CUE_UNUSED, "PREGAP" },
|
|
|
|
{ CUE_UNUSED, "REM" },
|
|
|
|
{ CUE_UNUSED, "SONGWRITER" },
|
|
|
|
{ CUE_UNUSED, "MESSAGE" },
|
|
|
|
{ -1 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum cue_command read_cmd(struct bstr *data, struct bstr *out_params)
|
|
|
|
{
|
|
|
|
struct bstr line = bstr_strip_linebreaks(bstr_getline(*data, data));
|
|
|
|
line = bstr_lstrip(line);
|
|
|
|
if (line.len == 0)
|
|
|
|
return CUE_EMPTY;
|
|
|
|
for (int n = 0; cue_command_strings[n].command != -1; n++) {
|
|
|
|
struct bstr name = bstr0(cue_command_strings[n].text);
|
2017-01-24 07:57:51 +00:00
|
|
|
if (bstr_case_startswith(line, name)) {
|
2015-05-19 19:36:21 +00:00
|
|
|
struct bstr rest = bstr_cut(line, name.len);
|
|
|
|
if (rest.len && !strchr(WHITESPACE, rest.start[0]))
|
|
|
|
continue;
|
|
|
|
if (out_params)
|
2018-01-30 13:01:15 +00:00
|
|
|
*out_params = bstr_lstrip(rest);
|
2015-05-19 19:36:21 +00:00
|
|
|
return cue_command_strings[n].command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CUE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool eat_char(struct bstr *data, char ch)
|
|
|
|
{
|
|
|
|
if (data->len && data->start[0] == ch) {
|
|
|
|
*data = bstr_cut(*data, 1);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 12:18:51 +00:00
|
|
|
static char *read_quoted(void *talloc_ctx, struct bstr *data)
|
2015-05-19 19:36:21 +00:00
|
|
|
{
|
|
|
|
*data = bstr_lstrip(*data);
|
|
|
|
if (!eat_char(data, '"'))
|
2015-06-24 12:18:51 +00:00
|
|
|
return NULL;
|
2015-05-19 19:36:21 +00:00
|
|
|
int end = bstrchr(*data, '"');
|
|
|
|
if (end < 0)
|
2015-06-24 12:18:51 +00:00
|
|
|
return NULL;
|
2015-05-19 19:36:21 +00:00
|
|
|
struct bstr res = bstr_splice(*data, 0, end);
|
|
|
|
*data = bstr_cut(*data, end + 1);
|
2015-06-24 12:18:51 +00:00
|
|
|
return bstrto0(talloc_ctx, res);
|
2015-05-19 19:36:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 13:01:15 +00:00
|
|
|
static struct bstr strip_quotes(struct bstr data)
|
|
|
|
{
|
|
|
|
bstr s = data;
|
|
|
|
if (bstr_eatstart0(&s, "\"") && bstr_eatend0(&s, "\""))
|
|
|
|
return s;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-02-16 23:02:13 +00:00
|
|
|
// Read an unsigned decimal integer.
|
|
|
|
// Optionally check if it is 2 digit.
|
2015-05-19 19:36:21 +00:00
|
|
|
// Return -1 on failure.
|
2019-02-16 23:02:13 +00:00
|
|
|
static int read_int(struct bstr *data, bool two_digit)
|
2015-05-19 19:36:21 +00:00
|
|
|
{
|
|
|
|
*data = bstr_lstrip(*data);
|
|
|
|
if (data->len && data->start[0] == '-')
|
|
|
|
return -1;
|
|
|
|
struct bstr s = *data;
|
|
|
|
int res = (int)bstrtoll(s, &s, 10);
|
2019-02-16 23:02:13 +00:00
|
|
|
if (data->len == s.len || (two_digit && data->len - s.len > 2))
|
2015-05-19 19:36:21 +00:00
|
|
|
return -1;
|
|
|
|
*data = s;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double read_time(struct bstr *data)
|
|
|
|
{
|
|
|
|
struct bstr s = *data;
|
|
|
|
bool ok = true;
|
2019-02-16 23:02:13 +00:00
|
|
|
double t1 = read_int(&s, false);
|
2015-05-19 19:36:21 +00:00
|
|
|
ok = eat_char(&s, ':') && ok;
|
2019-02-16 23:02:13 +00:00
|
|
|
double t2 = read_int(&s, true);
|
2015-05-19 19:36:21 +00:00
|
|
|
ok = eat_char(&s, ':') && ok;
|
2019-02-16 23:02:13 +00:00
|
|
|
double t3 = read_int(&s, true);
|
2015-05-19 19:36:21 +00:00
|
|
|
ok = ok && t1 >= 0 && t2 >= 0 && t3 >= 0;
|
|
|
|
return ok ? t1 * 60.0 + t2 + t3 * SECS_PER_CUE_FRAME : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bstr skip_utf8_bom(struct bstr data)
|
|
|
|
{
|
|
|
|
return bstr_startswith0(data, "\xEF\xBB\xBF") ? bstr_cut(data, 3) : data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the text in data is most likely CUE data. This is used by the
|
|
|
|
// demuxer code to check the file type.
|
|
|
|
// data is the start of the probed file, possibly cut off at a random point.
|
|
|
|
bool mp_probe_cue(struct bstr data)
|
|
|
|
{
|
|
|
|
bool valid = false;
|
|
|
|
data = skip_utf8_bom(data);
|
|
|
|
for (;;) {
|
|
|
|
enum cue_command cmd = read_cmd(&data, NULL);
|
|
|
|
// End reached. Since the line was most likely cut off, don't use the
|
|
|
|
// result of the last parsing call.
|
|
|
|
if (data.len == 0)
|
|
|
|
break;
|
|
|
|
if (cmd == CUE_ERROR)
|
|
|
|
return false;
|
|
|
|
if (cmd != CUE_EMPTY)
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cue_file *mp_parse_cue(struct bstr data)
|
|
|
|
{
|
|
|
|
struct cue_file *f = talloc_zero(NULL, struct cue_file);
|
2015-10-01 19:57:35 +00:00
|
|
|
f->tags = talloc_zero(f, struct mp_tags);
|
2015-05-19 19:36:21 +00:00
|
|
|
|
|
|
|
data = skip_utf8_bom(data);
|
|
|
|
|
2015-06-24 12:18:51 +00:00
|
|
|
char *filename = NULL;
|
2015-05-19 19:36:21 +00:00
|
|
|
// Global metadata, and copied into new tracks.
|
|
|
|
struct cue_track proto_track = {0};
|
2015-10-01 19:57:35 +00:00
|
|
|
struct cue_track *cur_track = NULL;
|
2015-05-19 19:36:21 +00:00
|
|
|
|
|
|
|
while (data.len) {
|
|
|
|
struct bstr param;
|
2015-10-01 19:57:35 +00:00
|
|
|
int cmd = read_cmd(&data, ¶m);
|
|
|
|
switch (cmd) {
|
2015-05-19 19:36:21 +00:00
|
|
|
case CUE_ERROR:
|
|
|
|
talloc_free(f);
|
|
|
|
return NULL;
|
|
|
|
case CUE_TRACK: {
|
|
|
|
MP_TARRAY_GROW(f, f->tracks, f->num_tracks);
|
|
|
|
f->num_tracks += 1;
|
|
|
|
cur_track = &f->tracks[f->num_tracks - 1];
|
|
|
|
*cur_track = proto_track;
|
2015-10-01 19:57:35 +00:00
|
|
|
cur_track->tags = talloc_zero(f, struct mp_tags);
|
2015-05-19 19:36:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CUE_TITLE:
|
2015-10-01 19:57:35 +00:00
|
|
|
case CUE_PERFORMER: {
|
|
|
|
static const char *metanames[] = {
|
|
|
|
[CUE_TITLE] = "title",
|
|
|
|
[CUE_PERFORMER] = "performer",
|
|
|
|
};
|
|
|
|
struct mp_tags *tags = cur_track ? cur_track->tags : f->tags;
|
2018-01-30 13:01:15 +00:00
|
|
|
mp_tags_set_bstr(tags, bstr0(metanames[cmd]), strip_quotes(param));
|
2015-05-19 19:36:21 +00:00
|
|
|
break;
|
2015-10-01 19:57:35 +00:00
|
|
|
}
|
2015-05-19 19:36:21 +00:00
|
|
|
case CUE_INDEX: {
|
2019-02-16 23:02:13 +00:00
|
|
|
int type = read_int(¶m, true);
|
2015-05-19 19:36:21 +00:00
|
|
|
double time = read_time(¶m);
|
2015-10-01 19:57:35 +00:00
|
|
|
if (cur_track) {
|
|
|
|
if (type == 1) {
|
|
|
|
cur_track->start = time;
|
|
|
|
cur_track->filename = filename;
|
|
|
|
} else if (type == 0) {
|
|
|
|
cur_track->pregap_start = time;
|
|
|
|
}
|
2015-05-19 19:36:21 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CUE_FILE:
|
|
|
|
// NOTE: FILE comes before TRACK, so don't use cur_track->filename
|
2015-06-24 12:18:51 +00:00
|
|
|
filename = read_quoted(f, ¶m);
|
2015-05-19 19:36:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
2015-12-17 09:40:31 +00:00
|
|
|
|
|
|
|
int mp_check_embedded_cue(struct cue_file *f)
|
|
|
|
{
|
|
|
|
char *fn0 = f->tracks[0].filename;
|
|
|
|
for (int n = 1; n < f->num_tracks; n++) {
|
|
|
|
char *fn = f->tracks[n].filename;
|
|
|
|
// both filenames have the same address (including NULL)
|
|
|
|
if (fn0 == fn)
|
|
|
|
continue;
|
|
|
|
// only one filename is NULL, or the strings don't match
|
|
|
|
if (!fn0 || !fn || strcmp(fn0, fn) != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|