2011-09-17 13:52:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Baptiste Coudurier
|
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
2012-11-29 02:28:37 +00:00
|
|
|
* Copyright (c) 2012 Clément Bœsch
|
2011-09-17 13:52:19 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.
|
|
|
|
*
|
|
|
|
* FFmpeg 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Libass subtitles burning filter.
|
|
|
|
*
|
|
|
|
* @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ass/ass.h>
|
|
|
|
|
2012-11-29 02:28:37 +00:00
|
|
|
#include "config.h"
|
|
|
|
#if CONFIG_SUBTITLES_FILTER
|
|
|
|
# include "libavcodec/avcodec.h"
|
|
|
|
# include "libavformat/avformat.h"
|
|
|
|
#endif
|
2011-09-17 13:52:19 +00:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
2012-03-19 18:28:56 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
2011-09-17 13:52:19 +00:00
|
|
|
#include "drawutils.h"
|
|
|
|
#include "avfilter.h"
|
2012-06-22 12:33:09 +00:00
|
|
|
#include "internal.h"
|
2012-06-21 09:07:35 +00:00
|
|
|
#include "formats.h"
|
|
|
|
#include "video.h"
|
2011-09-17 13:52:19 +00:00
|
|
|
|
2017-05-12 18:00:49 +00:00
|
|
|
typedef struct AssContext {
|
2012-03-19 18:28:56 +00:00
|
|
|
const AVClass *class;
|
2011-09-17 13:52:19 +00:00
|
|
|
ASS_Library *library;
|
|
|
|
ASS_Renderer *renderer;
|
|
|
|
ASS_Track *track;
|
|
|
|
char *filename;
|
2015-08-30 22:52:10 +00:00
|
|
|
char *fontsdir;
|
2012-12-31 10:15:59 +00:00
|
|
|
char *charenc;
|
2015-01-28 12:11:42 +00:00
|
|
|
char *force_style;
|
2014-05-01 10:51:17 +00:00
|
|
|
int stream_index;
|
2017-08-30 19:04:02 +00:00
|
|
|
int alpha;
|
2011-09-17 13:52:19 +00:00
|
|
|
uint8_t rgba_map[4];
|
|
|
|
int pix_step[4]; ///< steps per pixel for each plane of the main output
|
2012-03-22 19:59:36 +00:00
|
|
|
int original_w, original_h;
|
2014-09-11 19:10:43 +00:00
|
|
|
int shaping;
|
2012-03-28 13:51:13 +00:00
|
|
|
FFDrawContext draw;
|
2011-09-17 13:52:19 +00:00
|
|
|
} AssContext;
|
|
|
|
|
2012-03-19 18:28:56 +00:00
|
|
|
#define OFFSET(x) offsetof(AssContext, x)
|
2012-08-13 11:40:01 +00:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
2012-03-19 18:28:56 +00:00
|
|
|
|
2012-12-31 10:07:29 +00:00
|
|
|
#define COMMON_OPTIONS \
|
2020-03-17 21:46:36 +00:00
|
|
|
{"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \
|
|
|
|
{"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \
|
|
|
|
{"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \
|
|
|
|
{"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \
|
2017-08-30 19:04:02 +00:00
|
|
|
{"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \
|
2012-03-19 18:28:56 +00:00
|
|
|
|
2011-09-17 13:52:19 +00:00
|
|
|
/* libass supports a log level ranging from 0 to 7 */
|
2012-09-16 00:59:43 +00:00
|
|
|
static const int ass_libavfilter_log_level_map[] = {
|
2014-09-11 19:10:03 +00:00
|
|
|
[0] = AV_LOG_FATAL, /* MSGL_FATAL */
|
|
|
|
[1] = AV_LOG_ERROR, /* MSGL_ERR */
|
|
|
|
[2] = AV_LOG_WARNING, /* MSGL_WARN */
|
|
|
|
[3] = AV_LOG_WARNING, /* <undefined> */
|
|
|
|
[4] = AV_LOG_INFO, /* MSGL_INFO */
|
|
|
|
[5] = AV_LOG_INFO, /* <undefined> */
|
|
|
|
[6] = AV_LOG_VERBOSE, /* MSGL_V */
|
|
|
|
[7] = AV_LOG_DEBUG, /* MSGL_DBG2 */
|
2011-09-17 13:52:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
|
|
|
|
{
|
2014-09-11 19:09:39 +00:00
|
|
|
const int ass_level_clip = av_clip(ass_level, 0,
|
|
|
|
FF_ARRAY_ELEMS(ass_libavfilter_log_level_map) - 1);
|
|
|
|
const int level = ass_libavfilter_log_level_map[ass_level_clip];
|
2011-09-17 13:52:19 +00:00
|
|
|
|
|
|
|
av_vlog(ctx, level, fmt, args);
|
|
|
|
av_log(ctx, level, "\n");
|
|
|
|
}
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-09-17 13:52:19 +00:00
|
|
|
{
|
|
|
|
AssContext *ass = ctx->priv;
|
2012-10-15 08:06:52 +00:00
|
|
|
|
|
|
|
if (!ass->filename) {
|
2011-09-17 13:52:19 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ass->library = ass_library_init();
|
|
|
|
if (!ass->library) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
ass_set_message_cb(ass->library, ass_log, ctx);
|
|
|
|
|
2015-08-30 22:52:10 +00:00
|
|
|
ass_set_fonts_dir(ass->library, ass->fontsdir);
|
2021-05-02 21:02:02 +00:00
|
|
|
ass_set_extract_fonts(ass->library, 1);
|
2015-08-30 22:52:10 +00:00
|
|
|
|
2011-09-17 13:52:19 +00:00
|
|
|
ass->renderer = ass_renderer_init(ass->library);
|
|
|
|
if (!ass->renderer) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AssContext *ass = ctx->priv;
|
|
|
|
|
|
|
|
if (ass->track)
|
|
|
|
ass_free_track(ass->track);
|
|
|
|
if (ass->renderer)
|
|
|
|
ass_renderer_done(ass->renderer);
|
|
|
|
if (ass->library)
|
|
|
|
ass_library_done(ass->library);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2015-04-03 17:55:18 +00:00
|
|
|
return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
|
2011-09-17 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AssContext *ass = inlink->dst->priv;
|
|
|
|
|
2017-08-30 19:04:02 +00:00
|
|
|
ff_draw_init(&ass->draw, inlink->format, ass->alpha ? FF_DRAW_PROCESS_ALPHA : 0);
|
2011-09-17 13:52:19 +00:00
|
|
|
|
|
|
|
ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
|
2012-03-22 19:59:36 +00:00
|
|
|
if (ass->original_w && ass->original_h)
|
|
|
|
ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
|
|
|
|
(double)ass->original_w / ass->original_h);
|
2014-09-11 19:10:43 +00:00
|
|
|
if (ass->shaping != -1)
|
|
|
|
ass_set_shaper(ass->renderer, ass->shaping);
|
2011-09-17 13:52:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
|
|
|
|
#define AR(c) ( (c)>>24)
|
|
|
|
#define AG(c) (((c)>>16)&0xFF)
|
|
|
|
#define AB(c) (((c)>>8) &0xFF)
|
2015-02-19 00:51:54 +00:00
|
|
|
#define AA(c) ((0xFF-(c)) &0xFF)
|
2011-09-17 13:52:19 +00:00
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
static void overlay_ass_image(AssContext *ass, AVFrame *picref,
|
2011-09-17 13:52:19 +00:00
|
|
|
const ASS_Image *image)
|
|
|
|
{
|
|
|
|
for (; image; image = image->next) {
|
|
|
|
uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
|
2012-03-28 13:51:13 +00:00
|
|
|
FFDrawColor color;
|
|
|
|
ff_draw_color(&ass->draw, &color, rgba_color);
|
|
|
|
ff_blend_mask(&ass->draw, &color,
|
|
|
|
picref->data, picref->linesize,
|
2013-03-10 00:30:30 +00:00
|
|
|
picref->width, picref->height,
|
2012-03-28 13:51:13 +00:00
|
|
|
image->bitmap, image->stride, image->w, image->h,
|
|
|
|
3, 0, image->dst_x, image->dst_y);
|
2011-09-17 13:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
|
2011-09-17 13:52:19 +00:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
AssContext *ass = ctx->priv;
|
|
|
|
int detect_change = 0;
|
|
|
|
double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
|
|
|
|
ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
|
|
|
|
time_ms, &detect_change);
|
|
|
|
|
|
|
|
if (detect_change)
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
|
|
|
|
|
2012-03-28 13:51:13 +00:00
|
|
|
overlay_ass_image(ass, picref, image);
|
2011-09-17 13:52:19 +00:00
|
|
|
|
2012-11-29 01:34:09 +00:00
|
|
|
return ff_filter_frame(outlink, picref);
|
2011-09-17 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 19:01:59 +00:00
|
|
|
static const AVFilterPad ass_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2021-08-15 22:07:41 +00:00
|
|
|
.flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
|
2012-11-29 01:34:09 +00:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 19:01:59 +00:00
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad ass_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-11-29 02:28:37 +00:00
|
|
|
#if CONFIG_ASS_FILTER
|
|
|
|
|
2012-12-31 10:07:29 +00:00
|
|
|
static const AVOption ass_options[] = {
|
|
|
|
COMMON_OPTIONS
|
2014-09-11 19:10:43 +00:00
|
|
|
{"shaping", "set shaping engine", OFFSET(shaping), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "shaping_mode"},
|
|
|
|
{"auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"},
|
|
|
|
{"simple", "simple shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_SIMPLE}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"},
|
|
|
|
{"complex", "complex shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_COMPLEX}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"},
|
2012-12-31 10:07:29 +00:00
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
2012-11-29 02:28:37 +00:00
|
|
|
AVFILTER_DEFINE_CLASS(ass);
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int init_ass(AVFilterContext *ctx)
|
2012-11-29 02:28:37 +00:00
|
|
|
{
|
|
|
|
AssContext *ass = ctx->priv;
|
2013-04-12 09:13:33 +00:00
|
|
|
int ret = init(ctx);
|
2012-11-29 02:28:37 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2014-04-09 03:18:35 +00:00
|
|
|
/* Initialize fonts */
|
|
|
|
ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
|
|
|
|
|
2012-11-29 02:28:37 +00:00
|
|
|
ass->track = ass_read_file(ass->library, ass->filename, NULL);
|
|
|
|
if (!ass->track) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Could not create a libass track when reading file '%s'\n",
|
|
|
|
ass->filename);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_ass = {
|
2011-09-17 13:52:19 +00:00
|
|
|
.name = "ass",
|
2013-02-17 11:33:55 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."),
|
2011-09-17 13:52:19 +00:00
|
|
|
.priv_size = sizeof(AssContext),
|
2012-11-29 02:28:37 +00:00
|
|
|
.init = init_ass,
|
2011-09-17 13:52:19 +00:00
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(ass_inputs),
|
|
|
|
FILTER_OUTPUTS(ass_outputs),
|
2012-11-28 19:01:59 +00:00
|
|
|
.priv_class = &ass_class,
|
2011-09-17 13:52:19 +00:00
|
|
|
};
|
2012-11-29 02:28:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_SUBTITLES_FILTER
|
|
|
|
|
2012-12-31 10:07:29 +00:00
|
|
|
static const AVOption subtitles_options[] = {
|
|
|
|
COMMON_OPTIONS
|
2020-03-17 21:46:36 +00:00
|
|
|
{"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
|
2014-05-01 10:51:17 +00:00
|
|
|
{"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS},
|
|
|
|
{"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS},
|
2020-03-17 21:46:36 +00:00
|
|
|
{"force_style", "force subtitle style", OFFSET(force_style), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
|
2012-12-31 10:07:29 +00:00
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
2014-08-28 22:32:32 +00:00
|
|
|
static const char * const font_mimetypes[] = {
|
2014-04-09 03:18:35 +00:00
|
|
|
"application/x-truetype-font",
|
|
|
|
"application/vnd.ms-opentype",
|
|
|
|
"application/x-font-ttf",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int attachment_is_font(AVStream * st)
|
|
|
|
{
|
|
|
|
const AVDictionaryEntry *tag = NULL;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
tag = av_dict_get(st->metadata, "mimetype", NULL, AV_DICT_MATCH_CASE);
|
|
|
|
|
|
|
|
if (tag) {
|
|
|
|
for (n = 0; font_mimetypes[n]; n++) {
|
|
|
|
if (av_strcasecmp(font_mimetypes[n], tag->value) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-29 02:28:37 +00:00
|
|
|
AVFILTER_DEFINE_CLASS(subtitles);
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int init_subtitles(AVFilterContext *ctx)
|
2012-11-29 02:28:37 +00:00
|
|
|
{
|
2014-04-09 03:18:35 +00:00
|
|
|
int j, ret, sid;
|
2014-05-01 10:51:17 +00:00
|
|
|
int k = 0;
|
2012-12-31 10:15:59 +00:00
|
|
|
AVDictionary *codec_opts = NULL;
|
2012-11-29 02:28:37 +00:00
|
|
|
AVFormatContext *fmt = NULL;
|
|
|
|
AVCodecContext *dec_ctx = NULL;
|
2020-09-10 20:11:57 +00:00
|
|
|
const AVCodec *dec;
|
2013-02-20 18:47:32 +00:00
|
|
|
const AVCodecDescriptor *dec_desc;
|
2012-11-29 02:28:37 +00:00
|
|
|
AVStream *st;
|
|
|
|
AVPacket pkt;
|
|
|
|
AssContext *ass = ctx->priv;
|
|
|
|
|
|
|
|
/* Init libass */
|
2013-04-12 09:13:33 +00:00
|
|
|
ret = init(ctx);
|
2012-11-29 02:28:37 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ass->track = ass_new_track(ass->library);
|
|
|
|
if (!ass->track) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open subtitles file */
|
|
|
|
ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ret = avformat_find_stream_info(fmt, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Locate subtitles stream */
|
2014-05-01 10:51:17 +00:00
|
|
|
if (ass->stream_index < 0)
|
|
|
|
ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
|
|
|
|
else {
|
|
|
|
ret = -1;
|
|
|
|
if (ass->stream_index < fmt->nb_streams) {
|
|
|
|
for (j = 0; j < fmt->nb_streams; j++) {
|
2016-06-25 14:41:38 +00:00
|
|
|
if (fmt->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
|
2014-05-01 10:51:17 +00:00
|
|
|
if (ass->stream_index == k) {
|
|
|
|
ret = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 02:28:37 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
|
|
|
|
ass->filename);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
sid = ret;
|
|
|
|
st = fmt->streams[sid];
|
|
|
|
|
2014-04-09 03:18:35 +00:00
|
|
|
/* Load attached fonts */
|
|
|
|
for (j = 0; j < fmt->nb_streams; j++) {
|
|
|
|
AVStream *st = fmt->streams[j];
|
2016-06-25 14:41:38 +00:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT &&
|
2014-04-09 03:18:35 +00:00
|
|
|
attachment_is_font(st)) {
|
|
|
|
const AVDictionaryEntry *tag = NULL;
|
|
|
|
tag = av_dict_get(st->metadata, "filename", NULL,
|
|
|
|
AV_DICT_MATCH_CASE);
|
|
|
|
|
|
|
|
if (tag) {
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Loading attached font: %s\n",
|
|
|
|
tag->value);
|
|
|
|
ass_add_font(ass->library, tag->value,
|
2016-06-25 14:41:38 +00:00
|
|
|
st->codecpar->extradata,
|
|
|
|
st->codecpar->extradata_size);
|
2014-04-09 03:18:35 +00:00
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"Font attachment has no filename, ignored.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize fonts */
|
|
|
|
ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
|
|
|
|
|
2012-11-29 02:28:37 +00:00
|
|
|
/* Open decoder */
|
2016-06-25 14:41:38 +00:00
|
|
|
dec = avcodec_find_decoder(st->codecpar->codec_id);
|
2012-11-29 02:28:37 +00:00
|
|
|
if (!dec) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
|
2016-06-25 14:41:38 +00:00
|
|
|
avcodec_get_name(st->codecpar->codec_id));
|
2020-09-10 19:39:28 +00:00
|
|
|
ret = AVERROR_DECODER_NOT_FOUND;
|
|
|
|
goto end;
|
2012-11-29 02:28:37 +00:00
|
|
|
}
|
2016-06-25 14:41:38 +00:00
|
|
|
dec_desc = avcodec_descriptor_get(st->codecpar->codec_id);
|
2013-04-16 13:00:13 +00:00
|
|
|
if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) {
|
2013-02-17 12:33:52 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Only text based subtitles are currently supported\n");
|
2020-09-10 19:39:28 +00:00
|
|
|
ret = AVERROR_PATCHWELCOME;
|
|
|
|
goto end;
|
2013-02-17 12:33:52 +00:00
|
|
|
}
|
2012-12-31 10:15:59 +00:00
|
|
|
if (ass->charenc)
|
|
|
|
av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0);
|
2016-06-25 14:41:38 +00:00
|
|
|
|
|
|
|
dec_ctx = avcodec_alloc_context3(dec);
|
2020-09-10 19:39:28 +00:00
|
|
|
if (!dec_ctx) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-25 14:41:38 +00:00
|
|
|
|
|
|
|
ret = avcodec_parameters_to_context(dec_ctx, st->codecpar);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is required by the decoding process in order to rescale the
|
|
|
|
* timestamps: in the current API the decoded subtitles have their pts
|
|
|
|
* expressed in AV_TIME_BASE, and thus the lavc internals need to know the
|
|
|
|
* stream time base in order to achieve the rescaling.
|
|
|
|
*
|
|
|
|
* That API is old and needs to be reworked to match behaviour with A/V.
|
|
|
|
*/
|
2017-11-13 03:11:34 +00:00
|
|
|
dec_ctx->pkt_timebase = st->time_base;
|
2016-06-25 14:41:38 +00:00
|
|
|
|
|
|
|
ret = avcodec_open2(dec_ctx, NULL, &codec_opts);
|
2012-11-29 02:28:37 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
|
2015-01-28 12:11:42 +00:00
|
|
|
if (ass->force_style) {
|
|
|
|
char **list = NULL;
|
|
|
|
char *temp = NULL;
|
|
|
|
char *ptr = av_strtok(ass->force_style, ",", &temp);
|
|
|
|
int i = 0;
|
|
|
|
while (ptr) {
|
|
|
|
av_dynarray_add(&list, &i, ptr);
|
|
|
|
if (!list) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ptr = av_strtok(NULL, ",", &temp);
|
|
|
|
}
|
|
|
|
av_dynarray_add(&list, &i, NULL);
|
|
|
|
if (!list) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ass_set_style_overrides(ass->library, list);
|
|
|
|
av_free(list);
|
|
|
|
}
|
2012-11-29 02:28:37 +00:00
|
|
|
/* Decode subtitles and push them into the renderer (libass) */
|
|
|
|
if (dec_ctx->subtitle_header)
|
|
|
|
ass_process_codec_private(ass->track,
|
|
|
|
dec_ctx->subtitle_header,
|
|
|
|
dec_ctx->subtitle_header_size);
|
|
|
|
while (av_read_frame(fmt, &pkt) >= 0) {
|
|
|
|
int i, got_subtitle;
|
2013-04-24 22:56:36 +00:00
|
|
|
AVSubtitle sub = {0};
|
2012-11-29 02:28:37 +00:00
|
|
|
|
|
|
|
if (pkt.stream_index == sid) {
|
|
|
|
ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
|
2013-02-13 09:26:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n",
|
|
|
|
av_err2str(ret));
|
|
|
|
} else if (got_subtitle) {
|
2016-02-21 11:41:49 +00:00
|
|
|
const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000));
|
|
|
|
const int64_t duration = sub.end_display_time;
|
2013-02-14 15:35:49 +00:00
|
|
|
for (i = 0; i < sub.num_rects; i++) {
|
|
|
|
char *ass_line = sub.rects[i]->ass;
|
|
|
|
if (!ass_line)
|
|
|
|
break;
|
2020-09-10 19:55:52 +00:00
|
|
|
ass_process_chunk(ass->track, ass_line, strlen(ass_line),
|
|
|
|
start_time, duration);
|
2013-02-14 15:35:49 +00:00
|
|
|
}
|
2013-02-13 09:26:42 +00:00
|
|
|
}
|
2012-11-29 02:28:37 +00:00
|
|
|
}
|
2015-10-27 13:35:30 +00:00
|
|
|
av_packet_unref(&pkt);
|
2012-11-29 02:28:37 +00:00
|
|
|
avsubtitle_free(&sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2012-12-31 10:15:59 +00:00
|
|
|
av_dict_free(&codec_opts);
|
2016-06-25 17:11:51 +00:00
|
|
|
avcodec_free_context(&dec_ctx);
|
2016-06-25 17:14:21 +00:00
|
|
|
avformat_close_input(&fmt);
|
2012-11-29 02:28:37 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_subtitles = {
|
2012-11-29 02:28:37 +00:00
|
|
|
.name = "subtitles",
|
2013-02-17 11:33:55 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."),
|
2012-11-29 02:28:37 +00:00
|
|
|
.priv_size = sizeof(AssContext),
|
|
|
|
.init = init_subtitles,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(ass_inputs),
|
|
|
|
FILTER_OUTPUTS(ass_outputs),
|
2012-11-29 02:28:37 +00:00
|
|
|
.priv_class = &subtitles_class,
|
|
|
|
};
|
|
|
|
#endif
|