| @@ -0,0 +1,242 @@ |
| | 1 | /* |
| | 2 | * Copyright (c) 2011 Baptiste Coudurier |
| | 3 | * Copyright (c) 2011 Stefano Sabatini |
| | 4 | * |
| | 5 | * This file is part of FFmpeg. |
| | 6 | * |
| | 7 | * FFmpeg is free software; you can redistribute it and/or |
| | 8 | * modify it under the terms of the GNU Lesser General Public |
| | 9 | * License as published by the Free Software Foundation; either |
| | 10 | * version 2.1 of the License, or (at your option) any later version. |
| | 11 | * |
| | 12 | * FFmpeg is distributed in the hope that it will be useful, |
| | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 15 | * Lesser General Public License for more details. |
| | 16 | * |
| | 17 | * You should have received a copy of the GNU Lesser General Public |
| | 18 | * License along with FFmpeg; if not, write to the Free Software |
| | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| | 20 | */ |
| | 21 | |
| | 22 | /** |
| | 23 | * @file |
| | 24 | * Libass subtitles burning filter. |
| | 25 | * |
| | 26 | * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html} |
| | 27 | */ |
| | 28 | |
| | 29 | #include <ass/ass.h> |
| | 30 | |
| | 31 | #include "libavutil/avstring.h" |
| | 32 | #include "libavutil/imgutils.h" |
| | 33 | #include "libavutil/pixdesc.h" |
| | 34 | #include "drawutils.h" |
| | 35 | #include "avfilter.h" |
| | 36 | |
| | 37 | typedef struct { |
| | 38 | ASS_Library *library; |
| | 39 | ASS_Renderer *renderer; |
| | 40 | ASS_Track *track; |
| | 41 | int hsub, vsub; |
| | 42 | char *filename; |
| | 43 | uint8_t rgba_map[4]; |
| | 44 | int pix_step[4]; ///< steps per pixel for each plane of the main output |
| | 45 | } AssContext; |
| | 46 | |
| | 47 | /* libass supports a log level ranging from 0 to 7 */ |
| | 48 | int ass_libav_log_level_map[] = { |
| | 49 | AV_LOG_QUIET, /* 0 */ |
| | 50 | AV_LOG_PANIC, /* 1 */ |
| | 51 | AV_LOG_FATAL, /* 2 */ |
| | 52 | AV_LOG_ERROR, /* 3 */ |
| | 53 | AV_LOG_WARNING, /* 4 */ |
| | 54 | AV_LOG_INFO, /* 5 */ |
| | 55 | AV_LOG_VERBOSE, /* 6 */ |
| | 56 | AV_LOG_DEBUG, /* 7 */ |
| | 57 | }; |
| | 58 | |
| | 59 | static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx) |
| | 60 | { |
| | 61 | int level = ass_libav_log_level_map[ass_level]; |
| | 62 | |
| | 63 | av_vlog(ctx, level, fmt, args); |
| | 64 | av_log(ctx, level, "\n"); |
| | 65 | } |
| | 66 | |
| | 67 | static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
| | 68 | { |
| | 69 | AssContext *ass = ctx->priv; |
| | 70 | |
| | 71 | if (args) |
| | 72 | ass->filename = av_get_token(&args, ":"); |
| | 73 | if (!ass->filename || !*ass->filename) { |
| | 74 | av_log(ctx, AV_LOG_ERROR, "No filename provided!\n"); |
| | 75 | return AVERROR(EINVAL); |
| | 76 | } |
| | 77 | |
| | 78 | ass->library = ass_library_init(); |
| | 79 | if (!ass->library) { |
| | 80 | av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n"); |
| | 81 | return AVERROR(EINVAL); |
| | 82 | } |
| | 83 | ass_set_message_cb(ass->library, ass_log, ctx); |
| | 84 | |
| | 85 | ass->renderer = ass_renderer_init(ass->library); |
| | 86 | if (!ass->renderer) { |
| | 87 | av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n"); |
| | 88 | return AVERROR(EINVAL); |
| | 89 | } |
| | 90 | |
| | 91 | ass->track = ass_read_file(ass->library, ass->filename, NULL); |
| | 92 | if (!ass->track) { |
| | 93 | av_log(ctx, AV_LOG_ERROR, |
| | 94 | "Could not create a libass track when reading file '%s'\n", |
| | 95 | ass->filename); |
| | 96 | return AVERROR(EINVAL); |
| | 97 | } |
| | 98 | |
| | 99 | ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1); |
| | 100 | |
| | 101 | return 0; |
| | 102 | } |
| | 103 | |
| | 104 | static av_cold void uninit(AVFilterContext *ctx) |
| | 105 | { |
| | 106 | AssContext *ass = ctx->priv; |
| | 107 | |
| | 108 | av_freep(&ass->filename); |
| | 109 | if (ass->track) |
| | 110 | ass_free_track(ass->track); |
| | 111 | if (ass->renderer) |
| | 112 | ass_renderer_done(ass->renderer); |
| | 113 | if (ass->library) |
| | 114 | ass_library_done(ass->library); |
| | 115 | } |
| | 116 | |
| | 117 | static int query_formats(AVFilterContext *ctx) |
| | 118 | { |
| | 119 | static const enum PixelFormat pix_fmts[] = { |
| | 120 | PIX_FMT_ARGB, PIX_FMT_RGBA, |
| | 121 | PIX_FMT_ABGR, PIX_FMT_BGRA, |
| | 122 | PIX_FMT_RGB24, PIX_FMT_BGR24, |
| | 123 | PIX_FMT_NONE |
| | 124 | }; |
| | 125 | |
| | 126 | avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts)); |
| | 127 | |
| | 128 | return 0; |
| | 129 | } |
| | 130 | |
| | 131 | static int config_input(AVFilterLink *inlink) |
| | 132 | { |
| | 133 | AssContext *ass = inlink->dst->priv; |
| | 134 | const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format]; |
| | 135 | double sar = inlink->sample_aspect_ratio.num ? |
| | 136 | av_q2d(inlink->sample_aspect_ratio) : 1; |
| | 137 | double dar = inlink->w / inlink->h * sar; |
| | 138 | |
| | 139 | av_image_fill_max_pixsteps(ass->pix_step, NULL, pix_desc); |
| | 140 | ff_fill_rgba_map(ass->rgba_map, inlink->format); |
| | 141 | |
| | 142 | ass->hsub = pix_desc->log2_chroma_w; |
| | 143 | ass->vsub = pix_desc->log2_chroma_h; |
| | 144 | |
| | 145 | ass_set_frame_size (ass->renderer, inlink->w, inlink->h); |
| | 146 | ass_set_aspect_ratio(ass->renderer, dar, sar); |
| | 147 | |
| | 148 | return 0; |
| | 149 | } |
| | 150 | |
| | 151 | static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { } |
| | 152 | |
| | 153 | #define R 0 |
| | 154 | #define G 1 |
| | 155 | #define B 2 |
| | 156 | #define A 3 |
| | 157 | |
| | 158 | #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off) { \ |
| | 159 | p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \ |
| | 160 | alpha = rgba_color[A] * (val) * 129; \ |
| | 161 | *(p+r_off) = (alpha * rgba_color[R] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \ |
| | 162 | *(p+g_off) = (alpha * rgba_color[G] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \ |
| | 163 | *(p+b_off) = (alpha * rgba_color[B] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \ |
| | 164 | } |
| | 165 | |
| | 166 | /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */ |
| | 167 | #define AR(c) ( (c)>>24) |
| | 168 | #define AG(c) (((c)>>16)&0xFF) |
| | 169 | #define AB(c) (((c)>>8) &0xFF) |
| | 170 | #define AA(c) ((0xFF-c) &0xFF) |
| | 171 | |
| | 172 | static void overlay_ass_image(AVFilterBufferRef *picref, const uint8_t *rgba_map, const int pix_step, |
| | 173 | const ASS_Image *image) |
| | 174 | { |
| | 175 | int i, j; |
| | 176 | int alpha; |
| | 177 | uint8_t *p; |
| | 178 | const int ro = rgba_map[R]; |
| | 179 | const int go = rgba_map[G]; |
| | 180 | const int bo = rgba_map[B]; |
| | 181 | |
| | 182 | for (; image; image = image->next) { |
| | 183 | uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)}; |
| | 184 | unsigned char *row = image->bitmap; |
| | 185 | |
| | 186 | for (i = 0; i < image->h; i++) { |
| | 187 | for (j = 0; j < image->w; j++) { |
| | 188 | if (row[j]) { |
| | 189 | SET_PIXEL_RGB(picref, rgba_color, row[j], image->dst_x + j, image->dst_y + i, pix_step, ro, go, bo); |
| | 190 | } |
| | 191 | } |
| | 192 | row += image->stride; |
| | 193 | } |
| | 194 | } |
| | 195 | } |
| | 196 | |
| | 197 | static void end_frame(AVFilterLink *inlink) |
| | 198 | { |
| | 199 | AVFilterContext *ctx = inlink->dst; |
| | 200 | AVFilterLink *outlink = ctx->outputs[0]; |
| | 201 | AssContext *ass = ctx->priv; |
| | 202 | AVFilterBufferRef *picref = inlink->cur_buf; |
| | 203 | int detect_change = 0; |
| | 204 | double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000; |
| | 205 | ASS_Image *image = ass_render_frame(ass->renderer, ass->track, |
| | 206 | time_ms, &detect_change); |
| | 207 | |
| | 208 | if (detect_change) |
| | 209 | av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms); |
| | 210 | |
| | 211 | overlay_ass_image(picref, ass->rgba_map, ass->pix_step[0], image); |
| | 212 | |
| | 213 | avfilter_draw_slice(outlink, 0, picref->video->h, 1); |
| | 214 | avfilter_end_frame(outlink); |
| | 215 | } |
| | 216 | |
| | 217 | AVFilter avfilter_vf_ass = { |
| | 218 | .name = "ass", |
| | 219 | .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."), |
| | 220 | .priv_size = sizeof(AssContext), |
| | 221 | .init = init, |
| | 222 | .uninit = uninit, |
| | 223 | .query_formats = query_formats, |
| | 224 | |
| | 225 | .inputs = (const AVFilterPad[]) { |
| | 226 | { .name = "default", |
| | 227 | .type = AVMEDIA_TYPE_VIDEO, |
| | 228 | .get_video_buffer = avfilter_null_get_video_buffer, |
| | 229 | .start_frame = avfilter_null_start_frame, |
| | 230 | .draw_slice = null_draw_slice, |
| | 231 | .end_frame = end_frame, |
| | 232 | .config_props = config_input, |
| | 233 | .min_perms = AV_PERM_WRITE | AV_PERM_READ, |
| | 234 | .rej_perms = AV_PERM_PRESERVE }, |
| | 235 | { .name = NULL} |
| | 236 | }, |
| | 237 | .outputs = (const AVFilterPad[]) { |
| | 238 | { .name = "default", |
| | 239 | .type = AVMEDIA_TYPE_VIDEO, }, |
| | 240 | { .name = NULL} |
| | 241 | }, |
| | 242 | }; |