From: Clément Bœsch <ubi...@gmail.com> Signed-off-by: Josh Allmann <joshua.allm...@gmail.com> --- Changelog | 1 + configure | 5 + libavfilter/Makefile | 1 + libavfilter/allfilters.c | 2 + libavfilter/vf_subtitles.c | 366 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 375 insertions(+) create mode 100644 libavfilter/vf_subtitles.c
diff --git a/Changelog b/Changelog index 7a2a7c8..6b78363 100644 --- a/Changelog +++ b/Changelog @@ -25,6 +25,7 @@ version 10: - support for WavPack muxing (raw and in Matroska) - Go2Webinar decoder - WavPack encoding through libwavpack +- subtitles filter version 9: diff --git a/configure b/configure index 0bd54dd..0d3cf43 100755 --- a/configure +++ b/configure @@ -177,6 +177,7 @@ External library support: --enable-bzlib enable bzlib [autodetect] --enable-frei0r enable frei0r video filtering --enable-gnutls enable gnutls [no] + --enable-libass enable libass subtitles rendering [no] --enable-libcdio enable audio CD grabbing with libcdio --enable-libdc1394 enable IIDC-1394 grabbing using libdc1394 and libraw1394 [no] @@ -1039,6 +1040,7 @@ EXTERNAL_LIBRARY_LIST=" bzlib frei0r gnutls + libass libcdio libdc1394 libfaac @@ -1897,9 +1899,11 @@ frei0r_src_filter_deps="frei0r dlopen strtok_r" frei0r_src_filter_extralibs='$ldl' hqdn3d_filter_deps="gpl" interlace_filter_deps="gpl" +libass_filter_deps="libass" resample_filter_deps="avresample" ocv_filter_deps="libopencv" scale_filter_deps="swscale" +subtitles_filter_deps="avformat avcodec libass" yadif_filter_deps="gpl" # libraries @@ -3718,6 +3722,7 @@ done enabled avisynth && require2 vfw32 "windows.h vfw.h" AVIFileInit -lavifil32 enabled frei0r && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; } enabled gnutls && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init +enabled libass && require_pkg_config libass ass/ass.h ass_library_init enabled libfaac && require2 libfaac "stdint.h faac.h" faacEncGetVersion -lfaac enabled libfdk_aac && require libfdk_aac fdk-aac/aacenc_lib.h aacEncOpen -lfdk-aac enabled libfreetype && require_pkg_config freetype2 "ft2build.h freetype/freetype.h" FT_Init_FreeType diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 96fa8c0..5aaf90d 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -77,6 +77,7 @@ OBJS-$(CONFIG_SETSAR_FILTER) += vf_aspect.o OBJS-$(CONFIG_SETTB_FILTER) += vf_settb.o OBJS-$(CONFIG_SHOWINFO_FILTER) += vf_showinfo.o OBJS-$(CONFIG_SPLIT_FILTER) += split.o +OBJS-$(CONFIG_SUBTITLES_FILTER) += vf_subtitles.o OBJS-$(CONFIG_TRANSPOSE_FILTER) += vf_transpose.o OBJS-$(CONFIG_TRIM_FILTER) += trim.o OBJS-$(CONFIG_UNSHARP_FILTER) += vf_unsharp.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 20407ce..9cd6c0f 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -79,6 +79,7 @@ void avfilter_register_all(void) REGISTER_FILTER(HFLIP, hflip, vf); REGISTER_FILTER(HQDN3D, hqdn3d, vf); REGISTER_FILTER(INTERLACE, interlace, vf); + REGISTER_FILTER(LIBASS, libass, vf); REGISTER_FILTER(LUT, lut, vf); REGISTER_FILTER(LUTRGB, lutrgb, vf); REGISTER_FILTER(LUTYUV, lutyuv, vf); @@ -97,6 +98,7 @@ void avfilter_register_all(void) REGISTER_FILTER(SETTB, settb, vf); REGISTER_FILTER(SHOWINFO, showinfo, vf); REGISTER_FILTER(SPLIT, split, vf); + REGISTER_FILTER(SUBTITLES, subtitles, vf); REGISTER_FILTER(TRANSPOSE, transpose, vf); REGISTER_FILTER(TRIM, trim, vf); REGISTER_FILTER(UNSHARP, unsharp, vf); diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c new file mode 100644 index 0000000..c7be4e5 --- /dev/null +++ b/libavfilter/vf_subtitles.c @@ -0,0 +1,366 @@ +/* + * Copyright (c) 2011 Baptiste Coudurier + * Copyright (c) 2011 Stefano Sabatini + * Copyright (c) 2012 Clément Bœsch + * + * 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> + +#include "config.h" +#if CONFIG_SUBTITLES_FILTER +# include "libavcodec/avcodec.h" +# include "libavformat/avformat.h" +#endif +#include "libavutil/avstring.h" +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "drawutils.h" +#include "avfilter.h" +#include "internal.h" +#include "formats.h" +#include "video.h" + +typedef struct { + const AVClass *class; + ASS_Library *library; + ASS_Renderer *renderer; + ASS_Track *track; + char *filename; + char *charenc; + uint8_t rgba_map[4]; + int pix_step[4]; ///< steps per pixel for each plane of the main output + int original_w, original_h; + FFDrawContext draw; +} AssContext; + +#define OFFSET(x) offsetof(AssContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +#define COMMON_OPTIONS \ + {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \ + {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \ + {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \ + +/* libass supports a log level ranging from 0 to 7 */ +static const int ass_libavfilter_log_level_map[] = { + AV_LOG_QUIET, /* 0 */ + AV_LOG_PANIC, /* 1 */ + AV_LOG_FATAL, /* 2 */ + AV_LOG_ERROR, /* 3 */ + AV_LOG_WARNING, /* 4 */ + AV_LOG_INFO, /* 5 */ + AV_LOG_VERBOSE, /* 6 */ + AV_LOG_DEBUG, /* 7 */ +}; + +static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx) +{ + int level = ass_libavfilter_log_level_map[ass_level]; + + av_vlog(ctx, level, fmt, args); + av_log(ctx, level, "\n"); +} + +static av_cold int init(AVFilterContext *ctx) +{ + AssContext *ass = ctx->priv; + + if (!ass->filename) { + 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); + + 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); + } + + ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1); + 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) +{ + ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0)); + return 0; +} + +static int config_input(AVFilterLink *inlink) +{ + AssContext *ass = inlink->dst->priv; + + ff_draw_init(&ass->draw, inlink->format, 0); + + ass_set_frame_size (ass->renderer, inlink->w, inlink->h); + 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); + + 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) +#define AA(c) ((0xFF-c) &0xFF) + +static void overlay_ass_image(AssContext *ass, AVFrame *picref, + 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)}; + FFDrawColor color; + ff_draw_color(&ass->draw, &color, rgba_color); + ff_blend_mask(&ass->draw, &color, + picref->data, picref->linesize, + picref->width, picref->height, + image->bitmap, image->stride, image->w, image->h, + 3, 0, image->dst_x, image->dst_y); + } +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *picref) +{ + 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); + + overlay_ass_image(ass, picref, image); + + return ff_filter_frame(outlink, picref); +} + +static const AVFilterPad ass_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + .needs_writable = 1, + }, + { NULL } +}; + +static const AVFilterPad ass_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#if CONFIG_LIBASS_FILTER + +static const AVOption ass_options[] = { + COMMON_OPTIONS + {NULL}, +}; + +AVFILTER_DEFINE_CLASS(ass); + +static av_cold int init_ass(AVFilterContext *ctx) +{ + AssContext *ass = ctx->priv; + int ret = init(ctx); + + if (ret < 0) + return ret; + + 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; +} + +AVFilter avfilter_vf_libass = { + .name = "libass", + .description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."), + .priv_size = sizeof(AssContext), + .init = init_ass, + .uninit = uninit, + .query_formats = query_formats, + .inputs = ass_inputs, + .outputs = ass_outputs, + .priv_class = &ass_class, +}; +#endif + +#if CONFIG_SUBTITLES_FILTER + +static const AVOption subtitles_options[] = { + COMMON_OPTIONS + {"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS}, + {NULL}, +}; + +AVFILTER_DEFINE_CLASS(subtitles); + +static av_cold int init_subtitles(AVFilterContext *ctx) +{ + int ret, sid; + AVDictionary *codec_opts = NULL; + AVFormatContext *fmt = NULL; + AVCodecContext *dec_ctx = NULL; + AVCodec *dec = NULL; + const AVCodecDescriptor *dec_desc; + AVStream *st; + AVPacket pkt; + AssContext *ass = ctx->priv; + + /* Init libass */ + ret = init(ctx); + 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 */ + ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0); + 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]; + + /* Open decoder */ + dec_ctx = st->codec; + dec = avcodec_find_decoder(dec_ctx->codec_id); + if (!dec) { + av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n", + avcodec_get_name(dec_ctx->codec_id)); + return AVERROR(EINVAL); + } + dec_desc = avcodec_descriptor_get(dec_ctx->codec_id); + if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) { + av_log(ctx, AV_LOG_ERROR, + "Only text based subtitles are currently supported\n"); + return AVERROR_PATCHWELCOME; + } + if (ass->charenc) + av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0); + ret = avcodec_open2(dec_ctx, dec, &codec_opts); + if (ret < 0) + goto end; + + /* 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); + av_init_packet(&pkt); + pkt.data = NULL; + pkt.size = 0; + while (av_read_frame(fmt, &pkt) >= 0) { + int i, got_subtitle; + AVSubtitle sub = {0}; + + if (pkt.stream_index == sid) { + ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt); + if (ret < 0) { + av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n", + av_err2str(ret)); + } else if (got_subtitle) { + for (i = 0; i < sub.num_rects; i++) { + char *ass_line = sub.rects[i]->ass; + if (!ass_line) + break; + ass_process_data(ass->track, ass_line, strlen(ass_line)); + } + } + } + av_free_packet(&pkt); + avsubtitle_free(&sub); + } + +end: + av_dict_free(&codec_opts); + if (dec_ctx) + avcodec_close(dec_ctx); + if (fmt) + avformat_close_input(&fmt); + return ret; +} + +AVFilter avfilter_vf_subtitles = { + .name = "subtitles", + .description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."), + .priv_size = sizeof(AssContext), + .init = init_subtitles, + .uninit = uninit, + .query_formats = query_formats, + .inputs = ass_inputs, + .outputs = ass_outputs, + .priv_class = &subtitles_class, +}; +#endif -- 1.7.9.5 _______________________________________________ libav-devel mailing list libav-devel@libav.org https://lists.libav.org/mailman/listinfo/libav-devel