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