2013-06-01 17:54:31 +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.
|
2013-06-01 17:54:31 +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.
|
2013-06-01 17:54:31 +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/>.
|
2013-06-01 17:54:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavutil/intreadwrite.h>
|
|
|
|
#include <libavutil/common.h>
|
2016-01-18 20:13:42 +00:00
|
|
|
#include <libavutil/opt.h>
|
2013-06-01 17:54:31 +00:00
|
|
|
|
2013-06-02 17:38:57 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
|
|
|
#include "common/av_common.h"
|
2014-08-29 10:09:04 +00:00
|
|
|
#include "misc/bstr.h"
|
2013-06-01 17:54:31 +00:00
|
|
|
#include "sd.h"
|
|
|
|
|
2014-03-16 09:04:46 +00:00
|
|
|
#define HAVE_AV_WEBVTT (LIBAVCODEC_VERSION_MICRO >= 100)
|
2013-08-24 18:06:45 +00:00
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
struct lavc_conv {
|
|
|
|
struct mp_log *log;
|
2013-06-01 17:54:31 +00:00
|
|
|
AVCodecContext *avctx;
|
2015-12-18 00:54:14 +00:00
|
|
|
char *codec;
|
|
|
|
char *extradata;
|
|
|
|
AVSubtitle cur;
|
|
|
|
char **cur_list;
|
2013-06-01 17:54:31 +00:00
|
|
|
};
|
|
|
|
|
2013-08-24 13:17:37 +00:00
|
|
|
static const char *get_lavc_format(const char *format)
|
|
|
|
{
|
|
|
|
// For the hack involving parse_webvtt().
|
|
|
|
if (format && strcmp(format, "webvtt-webm") == 0)
|
|
|
|
format = "webvtt";
|
2016-04-06 10:36:13 +00:00
|
|
|
// Most text subtitles are srt/html style anyway.
|
|
|
|
if (format && strcmp(format, "text") == 0)
|
|
|
|
format = "subrip";
|
2013-08-24 13:17:37 +00:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
sd_lavc_conv: strip style header
Normally, libavcodec subtitle converters will output a style header like
this as part of the extradata:
Style: Default,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,1,1,0,2,10,10,10,0,0
We don't want that, so use some bruteforce to get rid of them.
2013-06-02 18:34:20 +00:00
|
|
|
// Disable style definitions generated by the libavcodec converter.
|
|
|
|
// We always want the user defined style instead.
|
|
|
|
static void disable_styles(bstr header)
|
|
|
|
{
|
|
|
|
while (header.len) {
|
|
|
|
int n = bstr_find(header, bstr0("\nStyle: "));
|
|
|
|
if (n < 0)
|
|
|
|
break;
|
|
|
|
header.start[n + 1] = '#'; // turn into a comment
|
|
|
|
header = bstr_cut(header, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
struct lavc_conv *lavc_conv_create(struct mp_log *log, const char *codec_name,
|
|
|
|
char *extradata, int extradata_len)
|
2013-06-01 17:54:31 +00:00
|
|
|
{
|
2015-12-18 00:54:14 +00:00
|
|
|
struct lavc_conv *priv = talloc_zero(NULL, struct lavc_conv);
|
|
|
|
priv->log = log;
|
|
|
|
priv->cur_list = talloc_array(priv, char*, 0);
|
|
|
|
priv->codec = talloc_strdup(priv, codec_name);
|
2013-06-01 17:54:31 +00:00
|
|
|
AVCodecContext *avctx = NULL;
|
2016-01-18 20:13:42 +00:00
|
|
|
AVDictionary *opts = NULL;
|
2015-12-18 00:54:14 +00:00
|
|
|
const char *fmt = get_lavc_format(priv->codec);
|
2013-08-24 13:17:37 +00:00
|
|
|
AVCodec *codec = avcodec_find_decoder(mp_codec_to_av_codec_id(fmt));
|
2013-06-01 17:54:31 +00:00
|
|
|
if (!codec)
|
|
|
|
goto error;
|
|
|
|
avctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!avctx)
|
|
|
|
goto error;
|
2016-02-29 17:20:27 +00:00
|
|
|
if (mp_lavc_set_extradata(avctx, extradata, extradata_len) < 0)
|
2016-01-18 10:46:28 +00:00
|
|
|
goto error;
|
2016-01-18 20:13:42 +00:00
|
|
|
if (strcmp(codec_name, "eia_608") == 0)
|
|
|
|
av_dict_set(&opts, "real_time", "1", 0);
|
|
|
|
if (avcodec_open2(avctx, codec, &opts) < 0)
|
2013-06-01 17:54:31 +00:00
|
|
|
goto error;
|
2016-01-18 20:13:42 +00:00
|
|
|
av_dict_free(&opts);
|
2013-06-01 17:54:31 +00:00
|
|
|
// Documented as "set by libavcodec", but there is no other way
|
|
|
|
avctx->time_base = (AVRational) {1, 1000};
|
2017-02-18 01:23:05 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MICRO >= 100
|
|
|
|
avctx->pkt_timebase = avctx->time_base;
|
|
|
|
#endif
|
2013-06-01 17:54:31 +00:00
|
|
|
priv->avctx = avctx;
|
2015-12-18 00:54:14 +00:00
|
|
|
priv->extradata = talloc_strndup(priv, avctx->subtitle_header,
|
|
|
|
avctx->subtitle_header_size);
|
|
|
|
disable_styles(bstr0(priv->extradata));
|
|
|
|
return priv;
|
2013-06-01 17:54:31 +00:00
|
|
|
|
|
|
|
error:
|
2015-12-18 00:54:14 +00:00
|
|
|
MP_FATAL(priv, "Could not open libavcodec subtitle converter\n");
|
2016-01-18 20:13:42 +00:00
|
|
|
av_dict_free(&opts);
|
2013-06-01 17:54:31 +00:00
|
|
|
av_free(avctx);
|
|
|
|
talloc_free(priv);
|
2015-12-18 00:54:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *lavc_conv_get_extradata(struct lavc_conv *priv)
|
|
|
|
{
|
|
|
|
return priv->extradata;
|
2013-06-01 17:54:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 18:06:45 +00:00
|
|
|
#if HAVE_AV_WEBVTT
|
|
|
|
|
2013-08-24 13:17:37 +00:00
|
|
|
// FFmpeg WebVTT packets are pre-parsed in some way. The FFmpeg Matroska
|
|
|
|
// demuxer does this on its own. In order to free our demuxer_mkv.c from
|
|
|
|
// codec-specific crud, we do this here.
|
|
|
|
// Copied from libavformat/matroskadec.c (FFmpeg 818ebe9 / 2013-08-19)
|
|
|
|
// License: LGPL v2.1 or later
|
|
|
|
// Author header: The FFmpeg Project
|
|
|
|
// Modified in some ways.
|
|
|
|
static int parse_webvtt(AVPacket *in, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
uint8_t *id, *settings, *text, *buf;
|
|
|
|
int id_len, settings_len, text_len;
|
|
|
|
uint8_t *p, *q;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
uint8_t *data = in->data;
|
|
|
|
int data_len = in->size;
|
|
|
|
|
|
|
|
if (data_len <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
q = data + data_len;
|
|
|
|
|
|
|
|
id = p;
|
|
|
|
id_len = -1;
|
|
|
|
while (p < q) {
|
|
|
|
if (*p == '\r' || *p == '\n') {
|
|
|
|
id_len = p - id;
|
|
|
|
if (*p == '\r')
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p >= q || *p != '\n')
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
settings = p;
|
|
|
|
settings_len = -1;
|
|
|
|
while (p < q) {
|
|
|
|
if (*p == '\r' || *p == '\n') {
|
|
|
|
settings_len = p - settings;
|
|
|
|
if (*p == '\r')
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p >= q || *p != '\n')
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
text = p;
|
|
|
|
text_len = q - p;
|
|
|
|
while (text_len > 0) {
|
|
|
|
const int len = text_len - 1;
|
|
|
|
const uint8_t c = p[len];
|
|
|
|
if (c != '\r' && c != '\n')
|
|
|
|
break;
|
|
|
|
text_len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text_len <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
err = av_new_packet(pkt, text_len);
|
|
|
|
if (err < 0)
|
|
|
|
return AVERROR(err);
|
|
|
|
|
|
|
|
memcpy(pkt->data, text, text_len);
|
|
|
|
|
|
|
|
if (id_len > 0) {
|
|
|
|
buf = av_packet_new_side_data(pkt,
|
|
|
|
AV_PKT_DATA_WEBVTT_IDENTIFIER,
|
|
|
|
id_len);
|
|
|
|
if (buf == NULL) {
|
2015-10-28 22:48:56 +00:00
|
|
|
av_packet_unref(pkt);
|
2013-08-24 13:17:37 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
memcpy(buf, id, id_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings_len > 0) {
|
|
|
|
buf = av_packet_new_side_data(pkt,
|
|
|
|
AV_PKT_DATA_WEBVTT_SETTINGS,
|
|
|
|
settings_len);
|
|
|
|
if (buf == NULL) {
|
2015-10-28 22:48:56 +00:00
|
|
|
av_packet_unref(pkt);
|
2013-08-24 13:17:37 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
memcpy(buf, settings, settings_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->pts = in->pts;
|
|
|
|
pkt->duration = in->duration;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-24 18:06:45 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
static int parse_webvtt(AVPacket *in, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
// Return a NULL-terminated list of ASS event lines.
|
|
|
|
char **lavc_conv_decode(struct lavc_conv *priv, struct demux_packet *packet)
|
2013-06-01 17:54:31 +00:00
|
|
|
{
|
|
|
|
AVCodecContext *avctx = priv->avctx;
|
|
|
|
AVPacket pkt;
|
2013-08-24 13:17:37 +00:00
|
|
|
AVPacket parsed_pkt = {0};
|
2013-06-01 17:54:31 +00:00
|
|
|
int ret, got_sub;
|
2015-12-19 20:21:36 +00:00
|
|
|
int num_cur = 0;
|
2013-06-01 17:54:31 +00:00
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
avsubtitle_free(&priv->cur);
|
|
|
|
|
av_common: add timebase parameter to mp_set_av_packet()
If the timebase is set, it's used for converting the packet timestamps.
Otherwise, the previous method of reinterpret-casting the mpv style
double timestamps to libavcodec style int64_t timestamps is used.
Also replace the kind of awkward mp_get_av_frame_pkt_ts() function by
mp_pts_from_av(), which simply converts timestamps in a way the old
function did. (Plus it takes a timebase parameter, similar to the
addition to mp_set_av_packet().)
Note that this should not change anything yet. The code in ad_lavc.c and
vd_lavc.c passes NULL for the timebase parameters. We could set
AVCodecContext.pkt_timebase and use that if we want to give libavcodec
"proper" timestamps.
This could be important for ad_lavc.c: some codecs (opus, probably mp3
and aac too) have weird requirements about doing decoding preroll on the
container level, and thus require adjusting the audio start timestamps
in some cases. libavcodec doesn't tell us how much was skipped, so we
either get shifted timestamps (by the length of the skipped data), or we
give it proper timestamps. (Note: libavcodec interprets or changes
timestamps only if pkt_timebase is set, which by default it is not.)
This would require selecting a timebase though, so I feel uncomfortable
with the idea. At least this change paves the way, and will allow some
testing.
2013-12-04 19:12:14 +00:00
|
|
|
mp_set_av_packet(&pkt, packet, &avctx->time_base);
|
2013-06-01 17:54:31 +00:00
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
if (strcmp(priv->codec, "webvtt-webm") == 0) {
|
2013-08-24 13:17:37 +00:00
|
|
|
if (parse_webvtt(&pkt, &parsed_pkt) < 0) {
|
2015-12-18 00:54:14 +00:00
|
|
|
MP_ERR(priv, "Error parsing subtitle\n");
|
2013-08-24 13:17:37 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
pkt = parsed_pkt;
|
|
|
|
}
|
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
ret = avcodec_decode_subtitle2(avctx, &priv->cur, &got_sub, &pkt);
|
2013-06-01 17:54:31 +00:00
|
|
|
if (ret < 0) {
|
2015-12-18 00:54:14 +00:00
|
|
|
MP_ERR(priv, "Error decoding subtitle\n");
|
2013-06-01 17:54:31 +00:00
|
|
|
} else if (got_sub) {
|
2015-12-18 00:54:14 +00:00
|
|
|
for (int i = 0; i < priv->cur.num_rects; i++) {
|
|
|
|
if (priv->cur.rects[i]->w > 0 && priv->cur.rects[i]->h > 0)
|
|
|
|
MP_WARN(priv, "Ignoring bitmap subtitle.\n");
|
|
|
|
char *ass_line = priv->cur.rects[i]->ass;
|
2013-06-01 17:54:31 +00:00
|
|
|
if (!ass_line)
|
2015-12-18 00:54:14 +00:00
|
|
|
continue;
|
|
|
|
MP_TARRAY_APPEND(priv, priv->cur_list, num_cur, ass_line);
|
2013-06-01 17:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-24 13:17:37 +00:00
|
|
|
done:
|
2015-10-28 22:48:56 +00:00
|
|
|
av_packet_unref(&parsed_pkt);
|
2015-12-19 20:21:36 +00:00
|
|
|
MP_TARRAY_APPEND(priv, priv->cur_list, num_cur, NULL);
|
2015-12-18 00:54:14 +00:00
|
|
|
return priv->cur_list;
|
2013-06-01 17:54:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
void lavc_conv_reset(struct lavc_conv *priv)
|
2013-06-01 17:54:31 +00:00
|
|
|
{
|
|
|
|
avcodec_flush_buffers(priv->avctx);
|
|
|
|
}
|
|
|
|
|
2015-12-18 00:54:14 +00:00
|
|
|
void lavc_conv_uninit(struct lavc_conv *priv)
|
2013-06-01 17:54:31 +00:00
|
|
|
{
|
2016-01-18 10:46:28 +00:00
|
|
|
avsubtitle_free(&priv->cur);
|
|
|
|
avcodec_free_context(&priv->avctx);
|
2013-06-01 17:54:31 +00:00
|
|
|
talloc_free(priv);
|
|
|
|
}
|