vf_ass.c 11 KB

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