]> git.sesse.net Git - vlc/blob - modules/codec/substext.h
omxil: Generalize code for hardcoded component/role mappings
[vlc] / modules / codec / substext.h
1 struct subpicture_updater_sys_t {
2     char *text;
3     char *html;
4
5     int  align;
6     int  x;
7     int  y;
8     int  i_font_height_percent;
9
10     bool is_fixed;
11     int  fixed_width;
12     int  fixed_height;
13     bool renderbg;
14 };
15
16 static int SubpictureTextValidate(subpicture_t *subpic,
17                                   bool has_src_changed, const video_format_t *fmt_src,
18                                   bool has_dst_changed, const video_format_t *fmt_dst,
19                                   mtime_t ts)
20 {
21     subpicture_updater_sys_t *sys = subpic->updater.p_sys;
22     VLC_UNUSED(fmt_src); VLC_UNUSED(fmt_dst); VLC_UNUSED(ts);
23
24     if (!has_src_changed && !has_dst_changed)
25         return VLC_SUCCESS;
26     if (!sys->is_fixed && subpic->b_absolute && subpic->p_region &&
27         subpic->i_original_picture_width > 0 &&
28         subpic->i_original_picture_height > 0) {
29
30         sys->is_fixed     = true;
31         sys->x            = subpic->p_region->i_x;
32         sys->y            = subpic->p_region->i_y;
33         sys->fixed_width  = subpic->i_original_picture_width;
34         sys->fixed_height = subpic->i_original_picture_height;
35     }
36     return VLC_EGENERIC;
37 }
38 static void SubpictureTextUpdate(subpicture_t *subpic,
39                                  const video_format_t *fmt_src,
40                                  const video_format_t *fmt_dst,
41                                  mtime_t ts)
42 {
43     subpicture_updater_sys_t *sys = subpic->updater.p_sys;
44     VLC_UNUSED(fmt_src); VLC_UNUSED(ts);
45
46     if (fmt_dst->i_sar_num <= 0 || fmt_dst->i_sar_den <= 0)
47         return;
48
49     subpic->i_original_picture_width  = fmt_dst->i_width * fmt_dst->i_sar_num / fmt_dst->i_sar_den;
50     subpic->i_original_picture_height = fmt_dst->i_height;
51
52     video_format_t fmt;
53     video_format_Init(&fmt, VLC_CODEC_TEXT);
54     fmt.i_sar_num = 1;
55     fmt.i_sar_den = 1;
56
57     subpicture_region_t *r = subpic->p_region = subpicture_region_New(&fmt);
58     if (!r)
59         return;
60
61     r->psz_text = sys->text ? strdup(sys->text) : NULL;
62     r->psz_html = sys->html ? strdup(sys->html) : NULL;
63     r->i_align  = sys->align;
64     r->b_renderbg = sys->renderbg;
65     if (!sys->is_fixed) {
66         const float margin_ratio = 0.04;
67         const int   margin_h     = margin_ratio * fmt_dst->i_visible_width;
68         const int   margin_v     = margin_ratio * fmt_dst->i_visible_height;
69
70         r->i_x = 0;
71         if (r->i_align & SUBPICTURE_ALIGN_LEFT)
72             r->i_x += margin_h + fmt_dst->i_x_offset;
73         else if (r->i_align & SUBPICTURE_ALIGN_RIGHT)
74             r->i_x += margin_h + fmt_dst->i_width - (fmt_dst->i_visible_width + fmt_dst->i_x_offset);
75
76         r->i_y = 0;
77         if (r->i_align & SUBPICTURE_ALIGN_TOP )
78             r->i_y += margin_v + fmt_dst->i_y_offset;
79         else if (r->i_align & SUBPICTURE_ALIGN_BOTTOM )
80             r->i_y += margin_v + fmt_dst->i_height - (fmt_dst->i_visible_height + fmt_dst->i_y_offset);
81     } else {
82         /* FIXME it doesn't adapt on crop settings changes */
83         r->i_x = sys->x * fmt_dst->i_width  / sys->fixed_width;
84         r->i_y = sys->y * fmt_dst->i_height / sys->fixed_height;
85     }
86
87     if (sys->i_font_height_percent != 0)
88     {
89         r->p_style = text_style_New();
90         if (r->p_style)
91         {
92             r->p_style->i_font_size = sys->i_font_height_percent *
93               subpic->i_original_picture_height / 100;
94             r->p_style->i_font_color = 0xffffff;
95             r->p_style->i_font_alpha = 0xff;
96         }
97     }
98 }
99 static void SubpictureTextDestroy(subpicture_t *subpic)
100 {
101     subpicture_updater_sys_t *sys = subpic->updater.p_sys;
102
103     free(sys->text);
104     free(sys->html);
105     free(sys);
106 }
107
108 static inline subpicture_t *decoder_NewSubpictureText(decoder_t *decoder)
109 {
110     subpicture_updater_sys_t *sys = calloc(1, sizeof(*sys));
111     subpicture_updater_t updater = {
112         .pf_validate = SubpictureTextValidate,
113         .pf_update   = SubpictureTextUpdate,
114         .pf_destroy  = SubpictureTextDestroy,
115         .p_sys       = sys,
116     };
117     subpicture_t *subpic = decoder_NewSubpicture(decoder, &updater);
118     if (!subpic)
119         free(sys);
120     return subpic;
121 }