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