annotate ffmpeg/libavfilter/vf_subtitles.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) 2011 Baptiste Coudurier
yading@11 3 * Copyright (c) 2011 Stefano Sabatini
yading@11 4 * Copyright (c) 2012 Clément Bœsch
yading@11 5 *
yading@11 6 * This file is part of FFmpeg.
yading@11 7 *
yading@11 8 * FFmpeg is free software; you can redistribute it and/or
yading@11 9 * modify it under the terms of the GNU Lesser General Public
yading@11 10 * License as published by the Free Software Foundation; either
yading@11 11 * version 2.1 of the License, or (at your option) any later version.
yading@11 12 *
yading@11 13 * FFmpeg is distributed in the hope that it will be useful,
yading@11 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 16 * Lesser General Public License for more details.
yading@11 17 *
yading@11 18 * You should have received a copy of the GNU Lesser General Public
yading@11 19 * License along with FFmpeg; if not, write to the Free Software
yading@11 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 21 */
yading@11 22
yading@11 23 /**
yading@11 24 * @file
yading@11 25 * Libass subtitles burning filter.
yading@11 26 *
yading@11 27 * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
yading@11 28 */
yading@11 29
yading@11 30 #include <ass/ass.h>
yading@11 31
yading@11 32 #include "config.h"
yading@11 33 #if CONFIG_SUBTITLES_FILTER
yading@11 34 # include "libavcodec/avcodec.h"
yading@11 35 # include "libavformat/avformat.h"
yading@11 36 #endif
yading@11 37 #include "libavutil/avstring.h"
yading@11 38 #include "libavutil/imgutils.h"
yading@11 39 #include "libavutil/opt.h"
yading@11 40 #include "libavutil/parseutils.h"
yading@11 41 #include "drawutils.h"
yading@11 42 #include "avfilter.h"
yading@11 43 #include "internal.h"
yading@11 44 #include "formats.h"
yading@11 45 #include "video.h"
yading@11 46
yading@11 47 typedef struct {
yading@11 48 const AVClass *class;
yading@11 49 ASS_Library *library;
yading@11 50 ASS_Renderer *renderer;
yading@11 51 ASS_Track *track;
yading@11 52 char *filename;
yading@11 53 char *charenc;
yading@11 54 uint8_t rgba_map[4];
yading@11 55 int pix_step[4]; ///< steps per pixel for each plane of the main output
yading@11 56 int original_w, original_h;
yading@11 57 FFDrawContext draw;
yading@11 58 } AssContext;
yading@11 59
yading@11 60 #define OFFSET(x) offsetof(AssContext, x)
yading@11 61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
yading@11 62
yading@11 63 #define COMMON_OPTIONS \
yading@11 64 {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
yading@11 65 {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
yading@11 66 {"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 }, \
yading@11 67
yading@11 68 /* libass supports a log level ranging from 0 to 7 */
yading@11 69 static const int ass_libavfilter_log_level_map[] = {
yading@11 70 AV_LOG_QUIET, /* 0 */
yading@11 71 AV_LOG_PANIC, /* 1 */
yading@11 72 AV_LOG_FATAL, /* 2 */
yading@11 73 AV_LOG_ERROR, /* 3 */
yading@11 74 AV_LOG_WARNING, /* 4 */
yading@11 75 AV_LOG_INFO, /* 5 */
yading@11 76 AV_LOG_VERBOSE, /* 6 */
yading@11 77 AV_LOG_DEBUG, /* 7 */
yading@11 78 };
yading@11 79
yading@11 80 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
yading@11 81 {
yading@11 82 int level = ass_libavfilter_log_level_map[ass_level];
yading@11 83
yading@11 84 av_vlog(ctx, level, fmt, args);
yading@11 85 av_log(ctx, level, "\n");
yading@11 86 }
yading@11 87
yading@11 88 static av_cold int init(AVFilterContext *ctx)
yading@11 89 {
yading@11 90 AssContext *ass = ctx->priv;
yading@11 91
yading@11 92 if (!ass->filename) {
yading@11 93 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
yading@11 94 return AVERROR(EINVAL);
yading@11 95 }
yading@11 96
yading@11 97 ass->library = ass_library_init();
yading@11 98 if (!ass->library) {
yading@11 99 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
yading@11 100 return AVERROR(EINVAL);
yading@11 101 }
yading@11 102 ass_set_message_cb(ass->library, ass_log, ctx);
yading@11 103
yading@11 104 ass->renderer = ass_renderer_init(ass->library);
yading@11 105 if (!ass->renderer) {
yading@11 106 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
yading@11 107 return AVERROR(EINVAL);
yading@11 108 }
yading@11 109
yading@11 110 ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
yading@11 111 return 0;
yading@11 112 }
yading@11 113
yading@11 114 static av_cold void uninit(AVFilterContext *ctx)
yading@11 115 {
yading@11 116 AssContext *ass = ctx->priv;
yading@11 117
yading@11 118 if (ass->track)
yading@11 119 ass_free_track(ass->track);
yading@11 120 if (ass->renderer)
yading@11 121 ass_renderer_done(ass->renderer);
yading@11 122 if (ass->library)
yading@11 123 ass_library_done(ass->library);
yading@11 124 }
yading@11 125
yading@11 126 static int query_formats(AVFilterContext *ctx)
yading@11 127 {
yading@11 128 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
yading@11 129 return 0;
yading@11 130 }
yading@11 131
yading@11 132 static int config_input(AVFilterLink *inlink)
yading@11 133 {
yading@11 134 AssContext *ass = inlink->dst->priv;
yading@11 135
yading@11 136 ff_draw_init(&ass->draw, inlink->format, 0);
yading@11 137
yading@11 138 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
yading@11 139 if (ass->original_w && ass->original_h)
yading@11 140 ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
yading@11 141 (double)ass->original_w / ass->original_h);
yading@11 142
yading@11 143 return 0;
yading@11 144 }
yading@11 145
yading@11 146 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
yading@11 147 #define AR(c) ( (c)>>24)
yading@11 148 #define AG(c) (((c)>>16)&0xFF)
yading@11 149 #define AB(c) (((c)>>8) &0xFF)
yading@11 150 #define AA(c) ((0xFF-c) &0xFF)
yading@11 151
yading@11 152 static void overlay_ass_image(AssContext *ass, AVFrame *picref,
yading@11 153 const ASS_Image *image)
yading@11 154 {
yading@11 155 for (; image; image = image->next) {
yading@11 156 uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
yading@11 157 FFDrawColor color;
yading@11 158 ff_draw_color(&ass->draw, &color, rgba_color);
yading@11 159 ff_blend_mask(&ass->draw, &color,
yading@11 160 picref->data, picref->linesize,
yading@11 161 picref->width, picref->height,
yading@11 162 image->bitmap, image->stride, image->w, image->h,
yading@11 163 3, 0, image->dst_x, image->dst_y);
yading@11 164 }
yading@11 165 }
yading@11 166
yading@11 167 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
yading@11 168 {
yading@11 169 AVFilterContext *ctx = inlink->dst;
yading@11 170 AVFilterLink *outlink = ctx->outputs[0];
yading@11 171 AssContext *ass = ctx->priv;
yading@11 172 int detect_change = 0;
yading@11 173 double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
yading@11 174 ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
yading@11 175 time_ms, &detect_change);
yading@11 176
yading@11 177 if (detect_change)
yading@11 178 av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
yading@11 179
yading@11 180 overlay_ass_image(ass, picref, image);
yading@11 181
yading@11 182 return ff_filter_frame(outlink, picref);
yading@11 183 }
yading@11 184
yading@11 185 static const AVFilterPad ass_inputs[] = {
yading@11 186 {
yading@11 187 .name = "default",
yading@11 188 .type = AVMEDIA_TYPE_VIDEO,
yading@11 189 .filter_frame = filter_frame,
yading@11 190 .config_props = config_input,
yading@11 191 .needs_writable = 1,
yading@11 192 },
yading@11 193 { NULL }
yading@11 194 };
yading@11 195
yading@11 196 static const AVFilterPad ass_outputs[] = {
yading@11 197 {
yading@11 198 .name = "default",
yading@11 199 .type = AVMEDIA_TYPE_VIDEO,
yading@11 200 },
yading@11 201 { NULL }
yading@11 202 };
yading@11 203
yading@11 204 #if CONFIG_ASS_FILTER
yading@11 205
yading@11 206 static const AVOption ass_options[] = {
yading@11 207 COMMON_OPTIONS
yading@11 208 {NULL},
yading@11 209 };
yading@11 210
yading@11 211 AVFILTER_DEFINE_CLASS(ass);
yading@11 212
yading@11 213 static av_cold int init_ass(AVFilterContext *ctx)
yading@11 214 {
yading@11 215 AssContext *ass = ctx->priv;
yading@11 216 int ret = init(ctx);
yading@11 217
yading@11 218 if (ret < 0)
yading@11 219 return ret;
yading@11 220
yading@11 221 ass->track = ass_read_file(ass->library, ass->filename, NULL);
yading@11 222 if (!ass->track) {
yading@11 223 av_log(ctx, AV_LOG_ERROR,
yading@11 224 "Could not create a libass track when reading file '%s'\n",
yading@11 225 ass->filename);
yading@11 226 return AVERROR(EINVAL);
yading@11 227 }
yading@11 228 return 0;
yading@11 229 }
yading@11 230
yading@11 231 AVFilter avfilter_vf_ass = {
yading@11 232 .name = "ass",
yading@11 233 .description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."),
yading@11 234 .priv_size = sizeof(AssContext),
yading@11 235 .init = init_ass,
yading@11 236 .uninit = uninit,
yading@11 237 .query_formats = query_formats,
yading@11 238 .inputs = ass_inputs,
yading@11 239 .outputs = ass_outputs,
yading@11 240 .priv_class = &ass_class,
yading@11 241 };
yading@11 242 #endif
yading@11 243
yading@11 244 #if CONFIG_SUBTITLES_FILTER
yading@11 245
yading@11 246 static const AVOption subtitles_options[] = {
yading@11 247 COMMON_OPTIONS
yading@11 248 {"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
yading@11 249 {NULL},
yading@11 250 };
yading@11 251
yading@11 252 AVFILTER_DEFINE_CLASS(subtitles);
yading@11 253
yading@11 254 static av_cold int init_subtitles(AVFilterContext *ctx)
yading@11 255 {
yading@11 256 int ret, sid;
yading@11 257 AVDictionary *codec_opts = NULL;
yading@11 258 AVFormatContext *fmt = NULL;
yading@11 259 AVCodecContext *dec_ctx = NULL;
yading@11 260 AVCodec *dec = NULL;
yading@11 261 const AVCodecDescriptor *dec_desc;
yading@11 262 AVStream *st;
yading@11 263 AVPacket pkt;
yading@11 264 AssContext *ass = ctx->priv;
yading@11 265
yading@11 266 /* Init libass */
yading@11 267 ret = init(ctx);
yading@11 268 if (ret < 0)
yading@11 269 return ret;
yading@11 270 ass->track = ass_new_track(ass->library);
yading@11 271 if (!ass->track) {
yading@11 272 av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
yading@11 273 return AVERROR(EINVAL);
yading@11 274 }
yading@11 275
yading@11 276 /* Open subtitles file */
yading@11 277 ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
yading@11 278 if (ret < 0) {
yading@11 279 av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
yading@11 280 goto end;
yading@11 281 }
yading@11 282 ret = avformat_find_stream_info(fmt, NULL);
yading@11 283 if (ret < 0)
yading@11 284 goto end;
yading@11 285
yading@11 286 /* Locate subtitles stream */
yading@11 287 ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
yading@11 288 if (ret < 0) {
yading@11 289 av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
yading@11 290 ass->filename);
yading@11 291 goto end;
yading@11 292 }
yading@11 293 sid = ret;
yading@11 294 st = fmt->streams[sid];
yading@11 295
yading@11 296 /* Open decoder */
yading@11 297 dec_ctx = st->codec;
yading@11 298 dec = avcodec_find_decoder(dec_ctx->codec_id);
yading@11 299 if (!dec) {
yading@11 300 av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
yading@11 301 avcodec_get_name(dec_ctx->codec_id));
yading@11 302 return AVERROR(EINVAL);
yading@11 303 }
yading@11 304 dec_desc = avcodec_descriptor_get(dec_ctx->codec_id);
yading@11 305 if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) {
yading@11 306 av_log(ctx, AV_LOG_ERROR,
yading@11 307 "Only text based subtitles are currently supported\n");
yading@11 308 return AVERROR_PATCHWELCOME;
yading@11 309 }
yading@11 310 if (ass->charenc)
yading@11 311 av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0);
yading@11 312 ret = avcodec_open2(dec_ctx, dec, &codec_opts);
yading@11 313 if (ret < 0)
yading@11 314 goto end;
yading@11 315
yading@11 316 /* Decode subtitles and push them into the renderer (libass) */
yading@11 317 if (dec_ctx->subtitle_header)
yading@11 318 ass_process_codec_private(ass->track,
yading@11 319 dec_ctx->subtitle_header,
yading@11 320 dec_ctx->subtitle_header_size);
yading@11 321 av_init_packet(&pkt);
yading@11 322 pkt.data = NULL;
yading@11 323 pkt.size = 0;
yading@11 324 while (av_read_frame(fmt, &pkt) >= 0) {
yading@11 325 int i, got_subtitle;
yading@11 326 AVSubtitle sub;
yading@11 327
yading@11 328 if (pkt.stream_index == sid) {
yading@11 329 ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
yading@11 330 if (ret < 0) {
yading@11 331 av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n",
yading@11 332 av_err2str(ret));
yading@11 333 } else if (got_subtitle) {
yading@11 334 for (i = 0; i < sub.num_rects; i++) {
yading@11 335 char *ass_line = sub.rects[i]->ass;
yading@11 336 if (!ass_line)
yading@11 337 break;
yading@11 338 ass_process_data(ass->track, ass_line, strlen(ass_line));
yading@11 339 }
yading@11 340 }
yading@11 341 }
yading@11 342 av_free_packet(&pkt);
yading@11 343 avsubtitle_free(&sub);
yading@11 344 }
yading@11 345
yading@11 346 end:
yading@11 347 av_dict_free(&codec_opts);
yading@11 348 if (dec_ctx)
yading@11 349 avcodec_close(dec_ctx);
yading@11 350 if (fmt)
yading@11 351 avformat_close_input(&fmt);
yading@11 352 return ret;
yading@11 353 }
yading@11 354
yading@11 355 AVFilter avfilter_vf_subtitles = {
yading@11 356 .name = "subtitles",
yading@11 357 .description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."),
yading@11 358 .priv_size = sizeof(AssContext),
yading@11 359 .init = init_subtitles,
yading@11 360 .uninit = uninit,
yading@11 361 .query_formats = query_formats,
yading@11 362 .inputs = ass_inputs,
yading@11 363 .outputs = ass_outputs,
yading@11 364 .priv_class = &subtitles_class,
yading@11 365 };
yading@11 366 #endif