Mercurial > mplayer.hg
annotate libmpcodecs/vf_ass.c @ 19550:ec7090509b38
Zero fill ass_settings_t before use.
author | eugeni |
---|---|
date | Sun, 27 Aug 2006 10:57:29 +0000 |
parents | 0be2d3583f4f |
children | 661d6c8a4adb |
rev | line source |
---|---|
18937 | 1 #include "config.h" |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include <stdint.h> | |
7 #include <assert.h> | |
8 | |
9 #include "config.h" | |
10 #include "mp_msg.h" | |
11 #include "help_mp.h" | |
12 | |
13 #include "img_format.h" | |
14 #include "mp_image.h" | |
15 #include "vf.h" | |
16 | |
17 #include "libvo/fastmemcpy.h" | |
18 | |
19 #include "m_option.h" | |
20 #include "m_struct.h" | |
21 | |
22 #include "libass/ass.h" | |
23 #include "libass/ass_mp.h" | |
24 | |
25 #define _r(c) ((c)>>24) | |
26 #define _g(c) (((c)>>16)&0xFF) | |
27 #define _b(c) (((c)>>8)&0xFF) | |
28 #define _a(c) ((c)&0xFF) | |
29 #define rgba2y(c) ( (( 263*_r(c) + 516*_g(c) + 100*_b(c)) >> 10) + 16 ) | |
30 #define rgba2u(c) ( ((-152*_r(c) - 298*_g(c) + 450*_b(c)) >> 10) + 128 ) | |
31 #define rgba2v(c) ( (( 450*_r(c) - 376*_g(c) - 73*_b(c)) >> 10) + 128 ) | |
32 | |
33 | |
34 static struct vf_priv_s { | |
35 int outh, outw; | |
36 | |
37 unsigned int outfmt; | |
38 | |
39 // 1 = auto-added filter: insert only if chain does not support EOSD already | |
40 // 0 = insert always | |
41 int auto_insert; | |
42 | |
43 ass_instance_t* ass_priv; | |
44 | |
45 unsigned char* planes[3]; | |
46 unsigned char* dirty_rows; | |
47 } vf_priv_dflt; | |
48 | |
49 extern int opt_screen_size_x; | |
50 extern int opt_screen_size_y; | |
51 | |
52 extern ass_track_t* ass_track; | |
53 extern float sub_delay; | |
54 extern int sub_visibility; | |
55 | |
56 static int config(struct vf_instance_s* vf, | |
57 int width, int height, int d_width, int d_height, | |
58 unsigned int flags, unsigned int outfmt) | |
59 { | |
60 ass_settings_t settings; | |
61 | |
62 if (outfmt == IMGFMT_IF09) return 0; | |
63 | |
64 vf->priv->outh = height + ass_top_margin + ass_bottom_margin; | |
65 vf->priv->outw = width; | |
66 | |
67 if(!opt_screen_size_x && !opt_screen_size_y){ | |
68 d_width = d_width * vf->priv->outw / width; | |
69 d_height = d_height * vf->priv->outh / height; | |
70 } | |
71 | |
19065
26ed3fcd5cd4
drops casts from void * on malloc/calloc, leftover on libmpcodecs
reynaldo
parents:
18937
diff
changeset
|
72 vf->priv->planes[1] = malloc(vf->priv->outw * vf->priv->outh); |
26ed3fcd5cd4
drops casts from void * on malloc/calloc, leftover on libmpcodecs
reynaldo
parents:
18937
diff
changeset
|
73 vf->priv->planes[2] = malloc(vf->priv->outw * vf->priv->outh); |
26ed3fcd5cd4
drops casts from void * on malloc/calloc, leftover on libmpcodecs
reynaldo
parents:
18937
diff
changeset
|
74 vf->priv->dirty_rows = malloc(vf->priv->outh); |
18937 | 75 |
76 if (vf->priv->ass_priv) { | |
19550 | 77 memset(&settings, 0, sizeof(ass_settings_t)); |
18937 | 78 settings.frame_width = vf->priv->outw; |
79 settings.frame_height = vf->priv->outh; | |
80 settings.font_size_coeff = ass_font_scale; | |
81 settings.line_spacing = ass_line_spacing; | |
82 settings.top_margin = ass_top_margin; | |
83 settings.bottom_margin = ass_bottom_margin; | |
84 settings.aspect = ((double)d_width) / d_height; | |
85 | |
86 ass_configure(vf->priv->ass_priv, &settings); | |
87 } | |
88 | |
89 return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width, d_height, flags, outfmt); | |
90 } | |
91 | |
92 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi) | |
93 { | |
94 if(mpi->type == MP_IMGTYPE_IPB) return; | |
95 if(mpi->flags & MP_IMGFLAG_PRESERVE) return; | |
96 if(mpi->imgfmt != vf->priv->outfmt) return; // colorspace differ | |
97 | |
98 // width never changes, always try full DR | |
99 mpi->priv = vf->dmpi = vf_get_image(vf->next, mpi->imgfmt, | |
100 mpi->type, mpi->flags, | |
101 vf->priv->outw, | |
102 vf->priv->outh); | |
103 | |
104 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) && | |
105 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){ | |
106 mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_FullDRNotPossible); | |
107 return; | |
108 } | |
109 | |
110 // set up mpi as a cropped-down image of dmpi: | |
111 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
112 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0]; | |
113 mpi->planes[1]=vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1]; | |
114 mpi->planes[2]=vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2]; | |
115 mpi->stride[1]=vf->dmpi->stride[1]; | |
116 mpi->stride[2]=vf->dmpi->stride[2]; | |
117 } else { | |
118 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0]; | |
119 } | |
120 mpi->stride[0]=vf->dmpi->stride[0]; | |
121 mpi->width=vf->dmpi->width; | |
122 mpi->flags|=MP_IMGFLAG_DIRECT; | |
123 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK; | |
124 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK; | |
125 } | |
126 | |
127 static void blank(mp_image_t *mpi, int y1, int y2) | |
128 { | |
129 int color[3] = {16, 128, 128}; // black (YUV) | |
130 int y; | |
131 unsigned char* dst; | |
132 int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift; | |
133 | |
134 dst = mpi->planes[0] + y1 * mpi->stride[0]; | |
135 for (y = 0; y < y2 - y1; ++y) { | |
136 memset(dst, color[0], mpi->w); | |
137 dst += mpi->stride[0]; | |
138 } | |
139 dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1]; | |
140 for (y = 0; y < chroma_rows ; ++y) { | |
141 memset(dst, color[1], mpi->chroma_width); | |
142 dst += mpi->stride[1]; | |
143 } | |
144 dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2]; | |
145 for (y = 0; y < chroma_rows ; ++y) { | |
146 memset(dst, color[2], mpi->chroma_width); | |
147 dst += mpi->stride[2]; | |
148 } | |
149 } | |
150 | |
151 static int prepare_image(struct vf_instance_s* vf, mp_image_t *mpi) | |
152 { | |
153 if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){ | |
154 vf->dmpi = mpi->priv; | |
155 if (!vf->dmpi) { mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_FunWhydowegetNULL); return 0; } | |
156 mpi->priv = NULL; | |
157 // we've used DR, so we're ready... | |
158 if (ass_top_margin) | |
159 blank(vf->dmpi, 0, ass_top_margin); | |
160 if (ass_bottom_margin) | |
161 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh); | |
162 if(!(mpi->flags&MP_IMGFLAG_PLANAR)) | |
163 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette | |
164 return 0; | |
165 } | |
166 | |
167 // hope we'll get DR buffer: | |
168 vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt, | |
169 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, | |
170 vf->priv->outw, vf->priv->outh); | |
171 | |
172 // copy mpi->dmpi... | |
173 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
174 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0], | |
175 mpi->planes[0], mpi->w, mpi->h, | |
176 vf->dmpi->stride[0], mpi->stride[0]); | |
177 memcpy_pic(vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1], | |
178 mpi->planes[1], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift, | |
179 vf->dmpi->stride[1], mpi->stride[1]); | |
180 memcpy_pic(vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2], | |
181 mpi->planes[2], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift, | |
182 vf->dmpi->stride[2], mpi->stride[2]); | |
183 } else { | |
184 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0], | |
185 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h, | |
186 vf->dmpi->stride[0], mpi->stride[0]); | |
187 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette | |
188 } | |
189 if (ass_top_margin) | |
190 blank(vf->dmpi, 0, ass_top_margin); | |
191 if (ass_bottom_margin) | |
192 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh); | |
193 return 0; | |
194 } | |
195 | |
196 /** | |
197 * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4 | |
198 */ | |
199 static void copy_from_image(struct vf_instance_s* vf, int first_row, int last_row) | |
200 { | |
201 int pl; | |
202 int i, j, k; | |
203 unsigned char val; | |
204 int chroma_rows; | |
205 | |
206 first_row -= (first_row % 2); | |
207 last_row += (last_row % 2); | |
208 chroma_rows = (last_row - first_row) / 2; | |
209 | |
210 for (pl = 1; pl < 3; ++pl) { | |
211 int dst_stride = vf->dmpi->stride[pl] * 2; | |
212 int src_stride = vf->dmpi->stride[pl]; | |
213 | |
214 unsigned char* src = vf->dmpi->planes[pl] + (first_row/2) * src_stride; | |
215 unsigned char* dst = vf->priv->planes[pl] + first_row * dst_stride; | |
216 unsigned char* dst_next = dst + dst_stride; | |
217 for(i = 0; i < chroma_rows; ++i) | |
218 { | |
219 if ((vf->priv->dirty_rows[first_row + i*2] == 0) || | |
220 (vf->priv->dirty_rows[first_row + i*2 + 1] == 0)) { | |
221 for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k+=2) { | |
222 val = *(src + j); | |
223 *(dst + k) = val; | |
224 *(dst + k + 1) = val; | |
225 *(dst_next + k) = val; | |
226 *(dst_next + k + 1) = val; | |
227 } | |
228 } | |
229 src += src_stride; | |
230 dst = dst_next + dst_stride; | |
231 dst_next = dst + dst_stride; | |
232 } | |
233 } | |
234 for (i = first_row; i < last_row; ++i) | |
235 vf->priv->dirty_rows[i] = 1; | |
236 } | |
237 | |
238 /** | |
239 * \brief Copy all previously copied rows back to render_context.dmpi | |
240 */ | |
241 static void copy_to_image(struct vf_instance_s* vf) | |
242 { | |
243 int pl; | |
244 int i, j, k; | |
245 for (pl = 1; pl < 3; ++pl) { | |
246 int dst_stride = vf->dmpi->stride[pl]; | |
247 int src_stride = vf->dmpi->stride[pl] * 2; | |
248 | |
249 unsigned char* dst = vf->dmpi->planes[pl]; | |
250 unsigned char* src = vf->priv->planes[pl]; | |
251 unsigned char* src_next = vf->priv->planes[pl] + src_stride; | |
252 for(i = 0; i < vf->dmpi->chroma_height; ++i) | |
253 { | |
254 if ((vf->priv->dirty_rows[i*2] == 1)) { | |
255 assert(vf->priv->dirty_rows[i*2 + 1] == 1); | |
256 for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k+=2) { | |
257 unsigned val = 0; | |
258 val += *(src + k); | |
259 val += *(src + k + 1); | |
260 val += *(src_next + k); | |
261 val += *(src_next + k + 1); | |
262 *(dst + j) = val >> 2; | |
263 } | |
264 } | |
265 dst += dst_stride; | |
266 src = src_next + src_stride; | |
267 src_next = src + src_stride; | |
268 } | |
269 } | |
270 } | |
271 | |
272 static void my_draw_bitmap(struct vf_instance_s* vf, unsigned char* bitmap, int bitmap_w, int bitmap_h, int stride, int dst_x, int dst_y, unsigned color) | |
273 { | |
274 unsigned char y = rgba2y(color); | |
275 unsigned char u = rgba2u(color); | |
276 unsigned char v = rgba2v(color); | |
277 unsigned char opacity = 255 - _a(color); | |
278 unsigned char* src; | |
279 unsigned char* dst; | |
280 int i, j; | |
281 mp_image_t* dmpi = vf->dmpi; | |
282 | |
283 | |
284 src = bitmap; | |
285 dst = dmpi->planes[0] + dst_x + dst_y * dmpi->stride[0]; | |
286 for(i = 0; i < bitmap_h; ++i) | |
287 { | |
288 for (j = 0; j < bitmap_w; ++j) { | |
289 // unsigned k = *(src+j); | |
290 unsigned k = ((unsigned)*(bitmap + stride * i + j)) * opacity / 255; | |
291 unsigned char orig_color = *(dst+j); | |
292 *(dst+j) = (k*y + (255-k)*orig_color) / 255; | |
293 } | |
294 src += stride; | |
295 dst += dmpi->stride[0]; | |
296 } | |
297 | |
298 for (i = 0; i < bitmap_h; ++i) { | |
299 for (j = 0; j < bitmap_w; ++j) { | |
300 int x = dst_x + j; | |
301 int y = dst_y + i; | |
302 unsigned k; | |
303 unsigned char orig_u, orig_v; | |
304 unsigned char new_u, new_v; | |
305 | |
306 k = ((unsigned)*(bitmap + stride * i + j)) * opacity / 255; | |
307 | |
308 orig_u = *(vf->priv->planes[1] + x + y * 2 * dmpi->chroma_width); | |
309 new_u = (k*u + (255-k)*orig_u) / 255; | |
310 *(vf->priv->planes[1] + x + y * 2 * dmpi->chroma_width) = new_u; | |
311 | |
312 orig_v = *(vf->priv->planes[2] + x + y * 2 * dmpi->chroma_width); | |
313 new_v = (k*v + (255-k)*orig_v) / 255; | |
314 *(vf->priv->planes[2] + x + y * 2 * dmpi->chroma_width) = new_v; | |
315 } | |
316 } | |
317 } | |
318 | |
319 static int render_frame(struct vf_instance_s* vf, mp_image_t *mpi, const ass_image_t* img) | |
320 { | |
321 if (img) { | |
322 memset(vf->priv->dirty_rows, 0, vf->priv->outh); // reset dirty rows | |
323 while (img) { | |
324 copy_from_image(vf, img->dst_y, img->dst_y + img->h); | |
325 my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride, | |
326 img->dst_x, img->dst_y, img->color); | |
327 img = img->next; | |
328 } | |
329 copy_to_image(vf); | |
330 } | |
331 return 0; | |
332 } | |
333 | |
334 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts) | |
335 { | |
336 ass_image_t* images = 0; | |
337 if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE)) | |
338 images = ass_render_frame(vf->priv->ass_priv, ass_track, (pts+sub_delay) * 1000 + .5); | |
339 | |
340 prepare_image(vf, mpi); | |
341 if (images) render_frame(vf, mpi, images); | |
342 | |
343 return vf_next_put_image(vf, vf->dmpi, pts); | |
344 } | |
345 | |
346 static int query_format(struct vf_instance_s* vf, unsigned int fmt) | |
347 { | |
348 switch(fmt){ | |
349 case IMGFMT_YV12: | |
350 case IMGFMT_I420: | |
351 case IMGFMT_IYUV: | |
352 return vf_next_query_format(vf, vf->priv->outfmt); | |
353 } | |
354 return 0; | |
355 } | |
356 | |
357 static int control(vf_instance_t *vf, int request, void *data) | |
358 { | |
19521 | 359 switch (request) { |
360 case VFCTRL_INIT_EOSD: | |
18937 | 361 vf->priv->ass_priv = ass_init(); |
19517 | 362 return vf->priv->ass_priv ? CONTROL_TRUE : CONTROL_FALSE; |
19521 | 363 case VFCTRL_DRAW_EOSD: |
364 if (vf->priv->ass_priv) return CONTROL_TRUE; | |
365 break; | |
18937 | 366 } |
367 return vf_next_control(vf, request, data); | |
368 } | |
369 | |
370 static void uninit(struct vf_instance_s* vf) | |
371 { | |
372 if (vf->priv->ass_priv) | |
373 ass_done(vf->priv->ass_priv); | |
374 if (vf->priv->planes[1]) | |
375 free(vf->priv->planes[1]); | |
376 if (vf->priv->planes[2]) | |
377 free(vf->priv->planes[2]); | |
378 if (vf->priv->dirty_rows) | |
379 free(vf->priv->dirty_rows); | |
380 } | |
381 | |
382 static unsigned int fmt_list[]={ | |
383 IMGFMT_YV12, | |
384 IMGFMT_I420, | |
385 IMGFMT_IYUV, | |
386 0 | |
387 }; | |
388 | |
389 static int open(vf_instance_t *vf, char* args) | |
390 { | |
391 int flags; | |
392 vf->priv->outfmt = vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12); | |
393 if (vf->priv->outfmt) | |
394 flags = vf_next_query_format(vf, vf->priv->outfmt); | |
395 if (!vf->priv->outfmt || (vf->priv->auto_insert && flags&VFCAP_EOSD)) | |
396 { | |
397 uninit(vf); | |
398 return 0; | |
399 } | |
400 | |
401 if (vf->priv->auto_insert) | |
402 mp_msg(MSGT_VFILTER, MSGL_INFO, "[ass] auto-open\n"); | |
403 | |
404 vf->config = config; | |
405 vf->query_format = query_format; | |
406 vf->uninit = uninit; | |
407 vf->control = control; | |
408 vf->get_image = get_image; | |
409 vf->put_image = put_image; | |
410 vf->default_caps=VFCAP_EOSD; | |
411 return 1; | |
412 } | |
413 | |
414 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f) | |
415 static m_option_t vf_opts_fields[] = { | |
416 {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0 , 0, 1, NULL}, | |
417 { NULL, NULL, 0, 0, 0, 0, NULL } | |
418 }; | |
419 | |
420 static m_struct_t vf_opts = { | |
421 "ass", | |
422 sizeof(struct vf_priv_s), | |
423 &vf_priv_dflt, | |
424 vf_opts_fields | |
425 }; | |
426 | |
427 vf_info_t vf_info_ass = { | |
428 "Render ASS/SSA subtitles", | |
429 "ass", | |
430 "Evgeniy Stepanov", | |
431 "", | |
432 open, | |
433 &vf_opts | |
434 }; | |
435 |