FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_subtitles.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  char *charenc;
54  uint8_t rgba_map[4];
55  int pix_step[4]; ///< steps per pixel for each plane of the main output
56  int original_w, original_h;
58 } AssContext;
59 
60 #define OFFSET(x) offsetof(AssContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62 
63 #define COMMON_OPTIONS \
64  {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
65  {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
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 }, \
67 
68 /* libass supports a log level ranging from 0 to 7 */
69 static const int ass_libavfilter_log_level_map[] = {
70  AV_LOG_QUIET, /* 0 */
71  AV_LOG_PANIC, /* 1 */
72  AV_LOG_FATAL, /* 2 */
73  AV_LOG_ERROR, /* 3 */
74  AV_LOG_WARNING, /* 4 */
75  AV_LOG_INFO, /* 5 */
76  AV_LOG_VERBOSE, /* 6 */
77  AV_LOG_DEBUG, /* 7 */
78 };
79 
80 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
81 {
82  int level = ass_libavfilter_log_level_map[ass_level];
83 
84  av_vlog(ctx, level, fmt, args);
85  av_log(ctx, level, "\n");
86 }
87 
88 static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
89 {
90  AssContext *ass = ctx->priv;
91  static const char *shorthand[] = { "filename", NULL };
92  int ret;
93 
94  ass->class = class;
96 
97  if ((ret = av_opt_set_from_string(ass, args, shorthand, "=", ":")) < 0)
98  return ret;
99 
100  if (!ass->filename) {
101  av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
102  return AVERROR(EINVAL);
103  }
104 
105  ass->library = ass_library_init();
106  if (!ass->library) {
107  av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
108  return AVERROR(EINVAL);
109  }
110  ass_set_message_cb(ass->library, ass_log, ctx);
111 
112  ass->renderer = ass_renderer_init(ass->library);
113  if (!ass->renderer) {
114  av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
115  return AVERROR(EINVAL);
116  }
117 
118  ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
119  return 0;
120 }
121 
122 static av_cold void uninit(AVFilterContext *ctx)
123 {
124  AssContext *ass = ctx->priv;
125 
126  av_opt_free(ass);
127  if (ass->track)
128  ass_free_track(ass->track);
129  if (ass->renderer)
130  ass_renderer_done(ass->renderer);
131  if (ass->library)
132  ass_library_done(ass->library);
133 }
134 
136 {
138  return 0;
139 }
140 
141 static int config_input(AVFilterLink *inlink)
142 {
143  AssContext *ass = inlink->dst->priv;
144 
145  ff_draw_init(&ass->draw, inlink->format, 0);
146 
147  ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
148  if (ass->original_w && ass->original_h)
149  ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
150  (double)ass->original_w / ass->original_h);
151 
152  return 0;
153 }
154 
155 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
156 #define AR(c) ( (c)>>24)
157 #define AG(c) (((c)>>16)&0xFF)
158 #define AB(c) (((c)>>8) &0xFF)
159 #define AA(c) ((0xFF-c) &0xFF)
160 
162  const ASS_Image *image)
163 {
164  for (; image; image = image->next) {
165  uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
167  ff_draw_color(&ass->draw, &color, rgba_color);
168  ff_blend_mask(&ass->draw, &color,
169  picref->data, picref->linesize,
170  picref->video->w, picref->video->h,
171  image->bitmap, image->stride, image->w, image->h,
172  3, 0, image->dst_x, image->dst_y);
173  }
174 }
175 
176 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
177 {
178  AVFilterContext *ctx = inlink->dst;
179  AVFilterLink *outlink = ctx->outputs[0];
180  AssContext *ass = ctx->priv;
181  int detect_change = 0;
182  double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
183  ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
184  time_ms, &detect_change);
185 
186  if (detect_change)
187  av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
188 
189  overlay_ass_image(ass, picref, image);
190 
191  return ff_filter_frame(outlink, picref);
192 }
193 
194 static const AVFilterPad ass_inputs[] = {
195  {
196  .name = "default",
197  .type = AVMEDIA_TYPE_VIDEO,
198  .filter_frame = filter_frame,
199  .config_props = config_input,
200  .min_perms = AV_PERM_READ | AV_PERM_WRITE,
201  },
202  { NULL }
203 };
204 
205 static const AVFilterPad ass_outputs[] = {
206  {
207  .name = "default",
208  .type = AVMEDIA_TYPE_VIDEO,
209  },
210  { NULL }
211 };
212 
213 #if CONFIG_ASS_FILTER
214 
215 static const AVOption ass_options[] = {
217  {NULL},
218 };
219 
221 
222 static av_cold int init_ass(AVFilterContext *ctx, const char *args)
223 {
224  AssContext *ass = ctx->priv;
225  int ret = init(ctx, args, &ass_class);
226 
227  if (ret < 0)
228  return ret;
229 
230  ass->track = ass_read_file(ass->library, ass->filename, NULL);
231  if (!ass->track) {
232  av_log(ctx, AV_LOG_ERROR,
233  "Could not create a libass track when reading file '%s'\n",
234  ass->filename);
235  return AVERROR(EINVAL);
236  }
237  return 0;
238 }
239 
240 AVFilter avfilter_vf_ass = {
241  .name = "ass",
242  .description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."),
243  .priv_size = sizeof(AssContext),
244  .init = init_ass,
245  .uninit = uninit,
247  .inputs = ass_inputs,
248  .outputs = ass_outputs,
249  .priv_class = &ass_class,
250 };
251 #endif
252 
253 #if CONFIG_SUBTITLES_FILTER
254 
255 static const AVOption subtitles_options[] = {
257  {"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
258  {NULL},
259 };
260 
261 AVFILTER_DEFINE_CLASS(subtitles);
262 
263 static av_cold int init_subtitles(AVFilterContext *ctx, const char *args)
264 {
265  int ret, sid;
269  AVCodec *dec = NULL;
270  const AVCodecDescriptor *dec_desc;
271  AVStream *st;
272  AVPacket pkt;
273  AssContext *ass = ctx->priv;
274 
275  /* Init libass */
276  ret = init(ctx, args, &subtitles_class);
277  if (ret < 0)
278  return ret;
279  ass->track = ass_new_track(ass->library);
280  if (!ass->track) {
281  av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
282  return AVERROR(EINVAL);
283  }
284 
285  /* Open subtitles file */
286  ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
287  if (ret < 0) {
288  av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
289  goto end;
290  }
291  ret = avformat_find_stream_info(fmt, NULL);
292  if (ret < 0)
293  goto end;
294 
295  /* Locate subtitles stream */
296  ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
297  if (ret < 0) {
298  av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
299  ass->filename);
300  goto end;
301  }
302  sid = ret;
303  st = fmt->streams[sid];
304 
305  /* Open decoder */
306  dec_ctx = st->codec;
307  dec = avcodec_find_decoder(dec_ctx->codec_id);
308  if (!dec) {
309  av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
310  avcodec_get_name(dec_ctx->codec_id));
311  return AVERROR(EINVAL);
312  }
313  dec_desc = avcodec_descriptor_get(dec_ctx->codec_id);
314  if (dec_desc && (dec_desc->props & AV_CODEC_PROP_BITMAP_SUB)) {
315  av_log(ctx, AV_LOG_ERROR,
316  "Only text based subtitles are currently supported\n");
317  return AVERROR_PATCHWELCOME;
318  }
319  if (ass->charenc)
320  av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0);
321  ret = avcodec_open2(dec_ctx, dec, &codec_opts);
322  if (ret < 0)
323  goto end;
324 
325  /* Decode subtitles and push them into the renderer (libass) */
326  if (dec_ctx->subtitle_header)
327  ass_process_codec_private(ass->track,
328  dec_ctx->subtitle_header,
329  dec_ctx->subtitle_header_size);
330  av_init_packet(&pkt);
331  pkt.data = NULL;
332  pkt.size = 0;
333  while (av_read_frame(fmt, &pkt) >= 0) {
334  int i, got_subtitle;
335  AVSubtitle sub = {0};
336 
337  if (pkt.stream_index == sid) {
338  ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
339  if (ret < 0) {
340  av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n",
341  av_err2str(ret));
342  } else if (got_subtitle) {
343  for (i = 0; i < sub.num_rects; i++) {
344  char *ass_line = sub.rects[i]->ass;
345  if (!ass_line)
346  break;
347  ass_process_data(ass->track, ass_line, strlen(ass_line));
348  }
349  }
350  }
351  av_free_packet(&pkt);
352  avsubtitle_free(&sub);
353  }
354 
355 end:
356  av_dict_free(&codec_opts);
357  if (dec_ctx)
358  avcodec_close(dec_ctx);
359  if (fmt)
360  avformat_close_input(&fmt);
361  return ret;
362 }
363 
364 AVFilter avfilter_vf_subtitles = {
365  .name = "subtitles",
366  .description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."),
367  .priv_size = sizeof(AssContext),
368  .init = init_subtitles,
369  .uninit = uninit,
371  .inputs = ass_inputs,
372  .outputs = ass_outputs,
373  .priv_class = &subtitles_class,
374 };
375 #endif