00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include <ass/ass.h>
00030
00031 #include "libavutil/avstring.h"
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/opt.h"
00034 #include "libavutil/parseutils.h"
00035 #include "drawutils.h"
00036 #include "avfilter.h"
00037 #include "internal.h"
00038 #include "formats.h"
00039 #include "video.h"
00040
00041 typedef struct {
00042 const AVClass *class;
00043 ASS_Library *library;
00044 ASS_Renderer *renderer;
00045 ASS_Track *track;
00046 char *filename;
00047 uint8_t rgba_map[4];
00048 int pix_step[4];
00049 int original_w, original_h;
00050 FFDrawContext draw;
00051 } AssContext;
00052
00053 #define OFFSET(x) offsetof(AssContext, x)
00054 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
00055
00056 static const AVOption ass_options[] = {
00057 {"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 },
00058 {NULL},
00059 };
00060
00061 AVFILTER_DEFINE_CLASS(ass);
00062
00063
00064 static const int ass_libavfilter_log_level_map[] = {
00065 AV_LOG_QUIET,
00066 AV_LOG_PANIC,
00067 AV_LOG_FATAL,
00068 AV_LOG_ERROR,
00069 AV_LOG_WARNING,
00070 AV_LOG_INFO,
00071 AV_LOG_VERBOSE,
00072 AV_LOG_DEBUG,
00073 };
00074
00075 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
00076 {
00077 int level = ass_libavfilter_log_level_map[ass_level];
00078
00079 av_vlog(ctx, level, fmt, args);
00080 av_log(ctx, level, "\n");
00081 }
00082
00083 static av_cold int init(AVFilterContext *ctx, const char *args)
00084 {
00085 AssContext *ass = ctx->priv;
00086 int ret;
00087
00088 ass->class = &ass_class;
00089 av_opt_set_defaults(ass);
00090
00091 if (args)
00092 ass->filename = av_get_token(&args, ":");
00093 if (!ass->filename || !*ass->filename) {
00094 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
00095 return AVERROR(EINVAL);
00096 }
00097
00098 if (*args++ == ':' && (ret = av_set_options_string(ass, args, "=", ":")) < 0)
00099 return ret;
00100
00101 ass->library = ass_library_init();
00102 if (!ass->library) {
00103 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
00104 return AVERROR(EINVAL);
00105 }
00106 ass_set_message_cb(ass->library, ass_log, ctx);
00107
00108 ass->renderer = ass_renderer_init(ass->library);
00109 if (!ass->renderer) {
00110 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
00111 return AVERROR(EINVAL);
00112 }
00113
00114 ass->track = ass_read_file(ass->library, ass->filename, NULL);
00115 if (!ass->track) {
00116 av_log(ctx, AV_LOG_ERROR,
00117 "Could not create a libass track when reading file '%s'\n",
00118 ass->filename);
00119 return AVERROR(EINVAL);
00120 }
00121
00122 ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
00123 return 0;
00124 }
00125
00126 static av_cold void uninit(AVFilterContext *ctx)
00127 {
00128 AssContext *ass = ctx->priv;
00129
00130 av_freep(&ass->filename);
00131 if (ass->track)
00132 ass_free_track(ass->track);
00133 if (ass->renderer)
00134 ass_renderer_done(ass->renderer);
00135 if (ass->library)
00136 ass_library_done(ass->library);
00137 }
00138
00139 static int query_formats(AVFilterContext *ctx)
00140 {
00141 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
00142 return 0;
00143 }
00144
00145 static int config_input(AVFilterLink *inlink)
00146 {
00147 AssContext *ass = inlink->dst->priv;
00148
00149 ff_draw_init(&ass->draw, inlink->format, 0);
00150
00151 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
00152 if (ass->original_w && ass->original_h)
00153 ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
00154 (double)ass->original_w / ass->original_h);
00155
00156 return 0;
00157 }
00158
00159 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
00160
00161
00162 #define AR(c) ( (c)>>24)
00163 #define AG(c) (((c)>>16)&0xFF)
00164 #define AB(c) (((c)>>8) &0xFF)
00165 #define AA(c) ((0xFF-c) &0xFF)
00166
00167 static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
00168 const ASS_Image *image)
00169 {
00170 for (; image; image = image->next) {
00171 uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
00172 FFDrawColor color;
00173 ff_draw_color(&ass->draw, &color, rgba_color);
00174 ff_blend_mask(&ass->draw, &color,
00175 picref->data, picref->linesize,
00176 picref->video->w, picref->video->h,
00177 image->bitmap, image->stride, image->w, image->h,
00178 3, 0, image->dst_x, image->dst_y);
00179 }
00180 }
00181
00182 static int end_frame(AVFilterLink *inlink)
00183 {
00184 AVFilterContext *ctx = inlink->dst;
00185 AVFilterLink *outlink = ctx->outputs[0];
00186 AssContext *ass = ctx->priv;
00187 AVFilterBufferRef *picref = inlink->cur_buf;
00188 int detect_change = 0;
00189 double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
00190 ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
00191 time_ms, &detect_change);
00192
00193 if (detect_change)
00194 av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
00195
00196 overlay_ass_image(ass, picref, image);
00197
00198 ff_draw_slice(outlink, 0, picref->video->h, 1);
00199 return ff_end_frame(outlink);
00200 }
00201
00202 AVFilter avfilter_vf_ass = {
00203 .name = "ass",
00204 .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
00205 .priv_size = sizeof(AssContext),
00206 .init = init,
00207 .uninit = uninit,
00208 .query_formats = query_formats,
00209
00210 .inputs = (const AVFilterPad[]) {
00211 { .name = "default",
00212 .type = AVMEDIA_TYPE_VIDEO,
00213 .get_video_buffer = ff_null_get_video_buffer,
00214 .start_frame = ff_null_start_frame,
00215 .draw_slice = null_draw_slice,
00216 .end_frame = end_frame,
00217 .config_props = config_input,
00218 .min_perms = AV_PERM_WRITE | AV_PERM_READ },
00219 { .name = NULL}
00220 },
00221 .outputs = (const AVFilterPad[]) {
00222 { .name = "default",
00223 .type = AVMEDIA_TYPE_VIDEO, },
00224 { .name = NULL}
00225 },
00226 .priv_class = &ass_class,
00227 };