32#include "config_components.h"
33#if CONFIG_SUBTITLES_FILTER
48#define FF_ASS_FEATURE_WRAP_UNICODE (LIBASS_VERSION >= 0x01600010)
69#define OFFSET(x) offsetof(AssContext, x)
70#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
72#define COMMON_OPTIONS \
73 {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \
74 {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \
75 {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \
76 {"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \
77 {"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \
79#define SHAPING_OPTIONS \
80 {"shaping", "set shaping engine", OFFSET(shaping), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, .unit = "shaping_mode"}, \
81 {"auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, FLAGS, .unit = "shaping_mode"}, \
82 {"simple", "simple shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_SIMPLE}, INT_MIN, INT_MAX, FLAGS, .unit = "shaping_mode"}, \
83 {"complex", "complex shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_COMPLEX}, INT_MIN, INT_MAX, FLAGS, .unit = "shaping_mode"}, \
99 case YCBCR_NONE:
return inlink_space;
100 case YCBCR_SMPTE240M_TV:
115 switch (ass_matrix) {
116 case YCBCR_NONE:
return inlink_range;
117 case YCBCR_SMPTE240M_PC:
121 case YCBCR_SMPTE240M_TV:
131static void ass_log(
int ass_level,
const char *fmt, va_list args,
void *
ctx)
133 const int ass_level_clip =
av_clip(ass_level, 0,
150 ass->
library = ass_library_init();
158 ass_set_extract_fonts(ass->
library, 1);
174 ass_free_track(ass->
track);
178 ass_library_done(ass->
library);
204 ass_set_frame_size (ass->
renderer, inlink->
w, inlink->
h);
206 ass_set_pixel_aspect(ass->
renderer, (
double)inlink->
w / inlink->
h /
210 ass_set_storage_size(ass->
renderer, inlink->
w, inlink->
h);
219#define AR(c) ( (c)>>24)
220#define AG(c) (((c)>>16)&0xFF)
221#define AB(c) (((c)>>8) &0xFF)
222#define AA(c) ((0xFF-(c)) &0xFF)
225 const ASS_Image *image)
227 for (; image; image = image->next) {
228 uint8_t rgba_color[] = {
AR(image->color),
AG(image->color),
AB(image->color),
AA(image->color)};
234 image->bitmap, image->stride, image->w, image->h,
235 3, 0, image->dst_x, image->dst_y);
244 int detect_change = 0;
246 ASS_Image *image = ass_render_frame(ass->
renderer, ass->
track,
247 time_ms, &detect_change);
269static const AVOption ass_options[] = {
291 "Could not create a libass track when reading file '%s'\n",
300 .p.description =
NULL_IF_CONFIG_SMALL(
"Render ASS subtitles onto input video using the libass library."),
311#if CONFIG_SUBTITLES_FILTER
313static const AVOption subtitles_options[] = {
319#if FF_ASS_FEATURE_WRAP_UNICODE
320 {
"wrap_unicode",
"break lines according to the Unicode Line Breaking Algorithm",
OFFSET(wrap_unicode),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1,
FLAGS },
326static const char *
const font_mimetypes[] = {
332 "application/font-sfnt",
333 "application/font-woff",
334 "application/x-truetype-font",
335 "application/vnd.ms-opentype",
336 "application/x-font-ttf",
340static int attachment_is_font(
AVStream * st)
348 for (n = 0; font_mimetypes[n]; n++) {
421 attachment_is_font(st)) {
434 "Font attachment has no filename, ignored.\n");
453 "Only text based subtitles are currently supported\n");
484#if FF_ASS_FEATURE_WRAP_UNICODE
489 ret = ass_track_set_feature(ass->
track, ASS_FEATURE_WRAP_UNICODE, 1);
492 "libass wasn't built with ASS_FEATURE_WRAP_UNICODE support\n");
514 ass_set_style_overrides(ass->
library, list);
519 ass_process_codec_private(ass->
track,
521 dec_ctx->subtitle_header_size);
526 if (
pkt.stream_index == sid) {
531 }
else if (got_subtitle) {
538 ass_process_chunk(ass->
track, ass_line, strlen(ass_line),
555 .p.name =
"subtitles",
556 .p.description =
NULL_IF_CONFIG_SMALL(
"Render text subtitles onto input video using the libass library."),
557 .p.priv_class = &subtitles_class,
559 .
init = init_subtitles,
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_subtitles
static AVFormatContext * ctx
Libavcodec external API header.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
AVDictionary * codec_opts
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
static AVCodecContext * dec_ctx
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
int ff_draw_init2(FFDrawContext *draw, enum AVPixelFormat format, enum AVColorSpace csp, enum AVColorRange range, enum AVAlphaMode alpha, unsigned flags)
Init a draw context.
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h, const uint8_t *mask, int mask_linesize, int mask_w, int mask_h, int l2depth, unsigned endianness, int x0, int y0)
Blend an alpha mask with an uniform color.
#define FF_DRAW_PROCESS_ALPHA
Process alpha pixel component.
int(* init)(AVBSFContext *ctx)
static int64_t start_time
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
#define AV_CODEC_PROP_TEXT_SUB
Subtitle codec is text based.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
Decode a subtitle message.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_vlog(void *avcl, int level, const char *fmt, va_list vl)
Send the specified message to the log if the level is less than or equal to the current av_log_level.
static AVRational av_make_q(int num, int den)
Create an AVRational.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
#define AVFILTER_DEFINE_CLASS(fname)
#define FILTER_QUERY_FUNC2(func)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Memory handling functions.
AVColorRange
Visual content value range.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_JPEG
Full range content.
AVColorSpace
YUV colorspace type.
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
main external API structure.
This struct describes the properties of a single codec described by an AVCodecID.
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
int extradata_size
Size of the extradata content in bytes.
enum AVMediaType codec_type
General type of the encoded data.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
enum AVColorSpace colorspace
For non-YUV links, these are respectively set to fallback values (as appropriate for that colorspace)...
enum AVAlphaMode alpha_mode
alpha mode (for videos with an alpha channel)
AVFilterContext * dst
dest filter
int format
agreed upon media format
enum AVColorRange color_range
agreed upon YUV color range
A filter pad used for either input or output.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
AVStream ** streams
A list of all streams in the file.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
This structure stores compressed data.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
char * ass
0 terminated ASS/SSA compatible event line.
uint32_t end_display_time
int64_t pts
Same as packet pts, in AV_TIME_BASE.
int pix_step[4]
steps per pixel for each plane of the main output
static void overlay_ass_image(AssContext *ass, AVFrame *picref, const ASS_Image *image)
static const int ass_libavfilter_log_level_map[]
static int config_input(AVFilterLink *inlink)
static const AVFilterPad ass_inputs[]
static enum AVColorSpace ass_get_color_space(ASS_YCbCrMatrix ass_matrix, enum AVColorSpace inlink_space)
static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static av_cold void uninit(AVFilterContext *ctx)
static enum AVColorRange ass_get_color_range(ASS_YCbCrMatrix ass_matrix, enum AVColorRange inlink_range)
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.