You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
11KB

  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. * @file
  24. * Libass subtitles burning filter.
  25. *
  26. * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
  27. */
  28. #include <ass/ass.h>
  29. #include "config.h"
  30. #if CONFIG_SUBTITLES_FILTER
  31. # include "libavcodec/avcodec.h"
  32. # include "libavformat/avformat.h"
  33. #endif
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/parseutils.h"
  38. #include "drawutils.h"
  39. #include "avfilter.h"
  40. #include "internal.h"
  41. #include "formats.h"
  42. #include "video.h"
  43. typedef struct {
  44. const AVClass *class;
  45. ASS_Library *library;
  46. ASS_Renderer *renderer;
  47. ASS_Track *track;
  48. char *filename;
  49. char *charenc;
  50. uint8_t rgba_map[4];
  51. int pix_step[4]; ///< steps per pixel for each plane of the main output
  52. int original_w, original_h;
  53. FFDrawContext draw;
  54. } AssContext;
  55. #define OFFSET(x) offsetof(AssContext, x)
  56. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  57. #define COMMON_OPTIONS \
  58. {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
  59. {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
  60. {"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 }, \
  61. /* libass supports a log level ranging from 0 to 7 */
  62. static const int ass_libavfilter_log_level_map[] = {
  63. AV_LOG_QUIET, /* 0 */
  64. AV_LOG_PANIC, /* 1 */
  65. AV_LOG_FATAL, /* 2 */
  66. AV_LOG_ERROR, /* 3 */
  67. AV_LOG_WARNING, /* 4 */
  68. AV_LOG_INFO, /* 5 */
  69. AV_LOG_VERBOSE, /* 6 */
  70. AV_LOG_DEBUG, /* 7 */
  71. };
  72. static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
  73. {
  74. int level = ass_libavfilter_log_level_map[ass_level];
  75. av_vlog(ctx, level, fmt, args);
  76. av_log(ctx, level, "\n");
  77. }
  78. static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
  79. {
  80. AssContext *ass = ctx->priv;
  81. static const char *shorthand[] = { "filename", NULL };
  82. int ret;
  83. ass->class = class;
  84. av_opt_set_defaults(ass);
  85. if ((ret = av_opt_set_from_string(ass, args, shorthand, "=", ":")) < 0)
  86. return ret;
  87. if (!ass->filename) {
  88. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  89. return AVERROR(EINVAL);
  90. }
  91. ass->library = ass_library_init();
  92. if (!ass->library) {
  93. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
  94. return AVERROR(EINVAL);
  95. }
  96. ass_set_message_cb(ass->library, ass_log, ctx);
  97. ass->renderer = ass_renderer_init(ass->library);
  98. if (!ass->renderer) {
  99. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
  100. return AVERROR(EINVAL);
  101. }
  102. ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
  103. return 0;
  104. }
  105. static av_cold void uninit(AVFilterContext *ctx)
  106. {
  107. AssContext *ass = ctx->priv;
  108. av_opt_free(ass);
  109. if (ass->track)
  110. ass_free_track(ass->track);
  111. if (ass->renderer)
  112. ass_renderer_done(ass->renderer);
  113. if (ass->library)
  114. ass_library_done(ass->library);
  115. }
  116. static int query_formats(AVFilterContext *ctx)
  117. {
  118. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  119. return 0;
  120. }
  121. static int config_input(AVFilterLink *inlink)
  122. {
  123. AssContext *ass = inlink->dst->priv;
  124. ff_draw_init(&ass->draw, inlink->format, 0);
  125. ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
  126. if (ass->original_w && ass->original_h)
  127. ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
  128. (double)ass->original_w / ass->original_h);
  129. return 0;
  130. }
  131. /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
  132. #define AR(c) ( (c)>>24)
  133. #define AG(c) (((c)>>16)&0xFF)
  134. #define AB(c) (((c)>>8) &0xFF)
  135. #define AA(c) ((0xFF-c) &0xFF)
  136. static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
  137. const ASS_Image *image)
  138. {
  139. for (; image; image = image->next) {
  140. uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
  141. FFDrawColor color;
  142. ff_draw_color(&ass->draw, &color, rgba_color);
  143. ff_blend_mask(&ass->draw, &color,
  144. picref->data, picref->linesize,
  145. picref->video->w, picref->video->h,
  146. image->bitmap, image->stride, image->w, image->h,
  147. 3, 0, image->dst_x, image->dst_y);
  148. }
  149. }
  150. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  151. {
  152. AVFilterContext *ctx = inlink->dst;
  153. AVFilterLink *outlink = ctx->outputs[0];
  154. AssContext *ass = ctx->priv;
  155. int detect_change = 0;
  156. double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
  157. ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
  158. time_ms, &detect_change);
  159. if (detect_change)
  160. av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
  161. overlay_ass_image(ass, picref, image);
  162. return ff_filter_frame(outlink, picref);
  163. }
  164. static const AVFilterPad ass_inputs[] = {
  165. {
  166. .name = "default",
  167. .type = AVMEDIA_TYPE_VIDEO,
  168. .filter_frame = filter_frame,
  169. .config_props = config_input,
  170. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  171. },
  172. { NULL }
  173. };
  174. static const AVFilterPad ass_outputs[] = {
  175. {
  176. .name = "default",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. },
  179. { NULL }
  180. };
  181. #if CONFIG_ASS_FILTER
  182. static const AVOption ass_options[] = {
  183. COMMON_OPTIONS
  184. {NULL},
  185. };
  186. AVFILTER_DEFINE_CLASS(ass);
  187. static av_cold int init_ass(AVFilterContext *ctx, const char *args)
  188. {
  189. AssContext *ass = ctx->priv;
  190. int ret = init(ctx, args, &ass_class);
  191. if (ret < 0)
  192. return ret;
  193. ass->track = ass_read_file(ass->library, ass->filename, NULL);
  194. if (!ass->track) {
  195. av_log(ctx, AV_LOG_ERROR,
  196. "Could not create a libass track when reading file '%s'\n",
  197. ass->filename);
  198. return AVERROR(EINVAL);
  199. }
  200. return 0;
  201. }
  202. AVFilter avfilter_vf_ass = {
  203. .name = "ass",
  204. .description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."),
  205. .priv_size = sizeof(AssContext),
  206. .init = init_ass,
  207. .uninit = uninit,
  208. .query_formats = query_formats,
  209. .inputs = ass_inputs,
  210. .outputs = ass_outputs,
  211. .priv_class = &ass_class,
  212. };
  213. #endif
  214. #if CONFIG_SUBTITLES_FILTER
  215. static const AVOption subtitles_options[] = {
  216. COMMON_OPTIONS
  217. {"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
  218. {NULL},
  219. };
  220. AVFILTER_DEFINE_CLASS(subtitles);
  221. static av_cold int init_subtitles(AVFilterContext *ctx, const char *args)
  222. {
  223. int ret, sid;
  224. AVDictionary *codec_opts = NULL;
  225. AVFormatContext *fmt = NULL;
  226. AVCodecContext *dec_ctx = NULL;
  227. AVCodec *dec = NULL;
  228. const AVCodecDescriptor *dec_desc;
  229. AVStream *st;
  230. AVPacket pkt;
  231. AssContext *ass = ctx->priv;
  232. /* Init libass */
  233. ret = init(ctx, args, &subtitles_class);
  234. if (ret < 0)
  235. return ret;
  236. ass->track = ass_new_track(ass->library);
  237. if (!ass->track) {
  238. av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
  239. return AVERROR(EINVAL);
  240. }
  241. /* Open subtitles file */
  242. ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
  243. if (ret < 0) {
  244. av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
  245. goto end;
  246. }
  247. ret = avformat_find_stream_info(fmt, NULL);
  248. if (ret < 0)
  249. goto end;
  250. /* Locate subtitles stream */
  251. ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
  252. if (ret < 0) {
  253. av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
  254. ass->filename);
  255. goto end;
  256. }
  257. sid = ret;
  258. st = fmt->streams[sid];
  259. /* Open decoder */
  260. dec_ctx = st->codec;
  261. dec = avcodec_find_decoder(dec_ctx->codec_id);
  262. if (!dec) {
  263. av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
  264. avcodec_get_name(dec_ctx->codec_id));
  265. return AVERROR(EINVAL);
  266. }
  267. dec_desc = avcodec_descriptor_get(dec_ctx->codec_id);
  268. if (dec_desc && (dec_desc->props & AV_CODEC_PROP_BITMAP_SUB)) {
  269. av_log(ctx, AV_LOG_ERROR,
  270. "Only text based subtitles are currently supported\n");
  271. return AVERROR_PATCHWELCOME;
  272. }
  273. if (ass->charenc)
  274. av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0);
  275. ret = avcodec_open2(dec_ctx, dec, &codec_opts);
  276. if (ret < 0)
  277. goto end;
  278. /* Decode subtitles and push them into the renderer (libass) */
  279. if (dec_ctx->subtitle_header)
  280. ass_process_codec_private(ass->track,
  281. dec_ctx->subtitle_header,
  282. dec_ctx->subtitle_header_size);
  283. av_init_packet(&pkt);
  284. pkt.data = NULL;
  285. pkt.size = 0;
  286. while (av_read_frame(fmt, &pkt) >= 0) {
  287. int i, got_subtitle;
  288. AVSubtitle sub;
  289. if (pkt.stream_index == sid) {
  290. ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
  291. if (ret < 0) {
  292. av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n",
  293. av_err2str(ret));
  294. } else if (got_subtitle) {
  295. for (i = 0; i < sub.num_rects; i++) {
  296. char *ass_line = sub.rects[i]->ass;
  297. if (!ass_line)
  298. break;
  299. ass_process_data(ass->track, ass_line, strlen(ass_line));
  300. }
  301. }
  302. }
  303. av_free_packet(&pkt);
  304. avsubtitle_free(&sub);
  305. }
  306. end:
  307. av_dict_free(&codec_opts);
  308. if (dec_ctx)
  309. avcodec_close(dec_ctx);
  310. if (fmt)
  311. avformat_close_input(&fmt);
  312. return ret;
  313. }
  314. AVFilter avfilter_vf_subtitles = {
  315. .name = "subtitles",
  316. .description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."),
  317. .priv_size = sizeof(AssContext),
  318. .init = init_subtitles,
  319. .uninit = uninit,
  320. .query_formats = query_formats,
  321. .inputs = ass_inputs,
  322. .outputs = ass_outputs,
  323. .priv_class = &subtitles_class,
  324. };
  325. #endif