libavfilter/vf_ass.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Baptiste Coudurier
00003  * Copyright (c) 2011 Stefano Sabatini
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00029 #include <ass/ass.h>
00030 
00031 #include "libavutil/avstring.h"
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/pixdesc.h"
00034 #include "drawutils.h"
00035 #include "avfilter.h"
00036 
00037 typedef struct {
00038     ASS_Library  *library;
00039     ASS_Renderer *renderer;
00040     ASS_Track    *track;
00041     int hsub, vsub;
00042     char *filename;
00043     uint8_t rgba_map[4];
00044     int     pix_step[4];       
00045 } AssContext;
00046 
00047 /* libass supports a log level ranging from 0 to 7 */
00048 int ass_libav_log_level_map[] = {
00049     AV_LOG_QUIET,               /* 0 */
00050     AV_LOG_PANIC,               /* 1 */
00051     AV_LOG_FATAL,               /* 2 */
00052     AV_LOG_ERROR,               /* 3 */
00053     AV_LOG_WARNING,             /* 4 */
00054     AV_LOG_INFO,                /* 5 */
00055     AV_LOG_VERBOSE,             /* 6 */
00056     AV_LOG_DEBUG,               /* 7 */
00057 };
00058 
00059 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
00060 {
00061     int level = ass_libav_log_level_map[ass_level];
00062 
00063     av_vlog(ctx, level, fmt, args);
00064     av_log(ctx, level, "\n");
00065 }
00066 
00067 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00068 {
00069     AssContext *ass = ctx->priv;
00070 
00071     if (args)
00072         ass->filename = av_get_token(&args, ":");
00073     if (!ass->filename || !*ass->filename) {
00074         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
00075         return AVERROR(EINVAL);
00076     }
00077 
00078     ass->library = ass_library_init();
00079     if (!ass->library) {
00080         av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
00081         return AVERROR(EINVAL);
00082     }
00083     ass_set_message_cb(ass->library, ass_log, ctx);
00084 
00085     ass->renderer = ass_renderer_init(ass->library);
00086     if (!ass->renderer) {
00087         av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
00088         return AVERROR(EINVAL);
00089     }
00090 
00091     ass->track = ass_read_file(ass->library, ass->filename, NULL);
00092     if (!ass->track) {
00093         av_log(ctx, AV_LOG_ERROR,
00094                "Could not create a libass track when reading file '%s'\n",
00095                ass->filename);
00096         return AVERROR(EINVAL);
00097     }
00098 
00099     ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
00100 
00101     return 0;
00102 }
00103 
00104 static av_cold void uninit(AVFilterContext *ctx)
00105 {
00106     AssContext *ass = ctx->priv;
00107 
00108     av_freep(&ass->filename);
00109     if (ass->track)
00110         ass_free_track(ass->track);
00111     if (ass->renderer)
00112         ass_renderer_done(ass->renderer);
00113     if (ass->library)
00114         ass_library_done(ass->library);
00115 }
00116 
00117 static int query_formats(AVFilterContext *ctx)
00118 {
00119     static const enum PixelFormat pix_fmts[] = {
00120         PIX_FMT_ARGB,  PIX_FMT_RGBA,
00121         PIX_FMT_ABGR,  PIX_FMT_BGRA,
00122         PIX_FMT_RGB24, PIX_FMT_BGR24,
00123         PIX_FMT_NONE
00124     };
00125 
00126     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00127 
00128     return 0;
00129 }
00130 
00131 static int config_input(AVFilterLink *inlink)
00132 {
00133     AssContext *ass = inlink->dst->priv;
00134     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00135     double sar = inlink->sample_aspect_ratio.num ?
00136         av_q2d(inlink->sample_aspect_ratio) : 1;
00137     double dar = inlink->w / inlink->h * sar;
00138 
00139     av_image_fill_max_pixsteps(ass->pix_step, NULL, pix_desc);
00140     ff_fill_rgba_map(ass->rgba_map, inlink->format);
00141 
00142     ass->hsub = pix_desc->log2_chroma_w;
00143     ass->vsub = pix_desc->log2_chroma_h;
00144 
00145     ass_set_frame_size  (ass->renderer, inlink->w, inlink->h);
00146     ass_set_aspect_ratio(ass->renderer, dar, sar);
00147 
00148     return 0;
00149 }
00150 
00151 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
00152 
00153 #define R 0
00154 #define G 1
00155 #define B 2
00156 #define A 3
00157 
00158 #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off) { \
00159     p   = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]);             \
00160     alpha = rgba_color[A] * (val) * 129;                                                \
00161     *(p+r_off) = (alpha * rgba_color[R] + (255*255*129 - alpha) * *(p+r_off)) >> 23;    \
00162     *(p+g_off) = (alpha * rgba_color[G] + (255*255*129 - alpha) * *(p+g_off)) >> 23;    \
00163     *(p+b_off) = (alpha * rgba_color[B] + (255*255*129 - alpha) * *(p+b_off)) >> 23;    \
00164 }
00165 
00166 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
00167 #define AR(c)  ( (c)>>24)
00168 #define AG(c)  (((c)>>16)&0xFF)
00169 #define AB(c)  (((c)>>8) &0xFF)
00170 #define AA(c)  ((0xFF-c) &0xFF)
00171 
00172 static void overlay_ass_image(AVFilterBufferRef *picref, const uint8_t *rgba_map, const int pix_step,
00173                               const ASS_Image *image)
00174 {
00175     int i, j;
00176     int alpha;
00177     uint8_t *p;
00178     const int ro = rgba_map[R];
00179     const int go = rgba_map[G];
00180     const int bo = rgba_map[B];
00181 
00182     for (; image; image = image->next) {
00183         uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
00184         unsigned char *row = image->bitmap;
00185 
00186         for (i = 0; i < image->h; i++) {
00187             for (j = 0; j < image->w; j++) {
00188                 if (row[j]) {
00189                     SET_PIXEL_RGB(picref, rgba_color, row[j], image->dst_x + j, image->dst_y + i, pix_step, ro, go, bo);
00190                 }
00191             }
00192             row += image->stride;
00193         }
00194     }
00195 }
00196 
00197 static void end_frame(AVFilterLink *inlink)
00198 {
00199     AVFilterContext *ctx = inlink->dst;
00200     AVFilterLink *outlink = ctx->outputs[0];
00201     AssContext *ass = ctx->priv;
00202     AVFilterBufferRef *picref = inlink->cur_buf;
00203     int detect_change = 0;
00204     double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
00205     ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
00206                                         time_ms, &detect_change);
00207 
00208     if (detect_change)
00209         av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
00210 
00211     overlay_ass_image(picref, ass->rgba_map, ass->pix_step[0], image);
00212 
00213     avfilter_draw_slice(outlink, 0, picref->video->h, 1);
00214     avfilter_end_frame(outlink);
00215 }
00216 
00217 AVFilter avfilter_vf_ass = {
00218     .name          = "ass",
00219     .description   = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
00220     .priv_size     = sizeof(AssContext),
00221     .init          = init,
00222     .uninit        = uninit,
00223     .query_formats = query_formats,
00224 
00225     .inputs = (const AVFilterPad[]) {
00226         { .name             = "default",
00227           .type             = AVMEDIA_TYPE_VIDEO,
00228           .get_video_buffer = avfilter_null_get_video_buffer,
00229           .start_frame      = avfilter_null_start_frame,
00230           .draw_slice       = null_draw_slice,
00231           .end_frame        = end_frame,
00232           .config_props     = config_input,
00233           .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
00234           .rej_perms        = AV_PERM_PRESERVE },
00235         { .name = NULL}
00236     },
00237     .outputs = (const AVFilterPad[]) {
00238         { .name             = "default",
00239           .type             = AVMEDIA_TYPE_VIDEO, },
00240         { .name = NULL}
00241     },
00242 };