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.

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