Debian Codegraph @

gst/dvdsub/gstdvdsubdec.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) <2005> Jan Schmidt <[email protected]>
3 * Copyright (C) <2002> Wim Taymans <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstdvdsubdec.h"
26 #include "gstdvdsubparse.h"
27 #include <string.h>
28
29 GST_DEBUG_CATEGORY_STATIC (gst_dvd_sub_dec_debug);
30 #define GST_CAT_DEFAULT (gst_dvd_sub_dec_debug)
31
32 #define gst_dvd_sub_dec_parent_class parent_class
33 G_DEFINE_TYPE (GstDvdSubDec, gst_dvd_sub_dec, GST_TYPE_ELEMENT);
34 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (dvdsubdec, "dvdsubdec", GST_RANK_NONE,
35 GST_TYPE_DVD_SUB_DEC, GST_DEBUG_CATEGORY_INIT (gst_dvd_sub_dec_debug,
36 "dvdsubdec", 0, "DVD subtitle decoder"));
37
38 static gboolean gst_dvd_sub_dec_src_event (GstPad * srcpad, GstObject * parent,
39 GstEvent * event);
40 static GstFlowReturn gst_dvd_sub_dec_chain (GstPad * pad, GstObject * parent,
41 GstBuffer * buf);
42
43 static gboolean gst_dvd_sub_dec_handle_dvd_event (GstDvdSubDec * dec,
44 GstEvent * event);
45 static void gst_dvd_sub_dec_finalize (GObject * gobject);
46 static void gst_setup_palette (GstDvdSubDec * dec);
47 static void gst_dvd_sub_dec_merge_title (GstDvdSubDec * dec,
48 GstVideoFrame * frame);
49 static GstClockTime gst_dvd_sub_dec_get_event_delay (GstDvdSubDec * dec);
50 static gboolean gst_dvd_sub_dec_sink_event (GstPad * pad, GstObject * parent,
51 GstEvent * event);
52 static gboolean gst_dvd_sub_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
53
54 static GstFlowReturn gst_send_subtitle_frame (GstDvdSubDec * dec,
55 GstClockTime end_ts);
56
57 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_PAD_SRC,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS ("video/x-raw, format = (string) { AYUV, ARGB },"
61 "width = (int) 720, height = (int) 576, framerate = (fraction) 0/1")
62 );
63
64 static GstStaticPadTemplate subtitle_template = GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_PAD_SINK,
66 GST_PAD_ALWAYS,
67 GST_STATIC_CAPS ("subpicture/x-dvd")
68 );
69
70
71 enum
72 {
73 SPU_FORCE_DISPLAY = 0x00,
74 SPU_SHOW = 0x01,
75 SPU_HIDE = 0x02,
76 SPU_SET_PALETTE = 0x03,
77 SPU_SET_ALPHA = 0x04,
78 SPU_SET_SIZE = 0x05,
79 SPU_SET_OFFSETS = 0x06,
80 SPU_WIPE = 0x07,
81 SPU_END = 0xff
82 };
83
84 static const guint32 default_clut[16] = {
85 0xb48080, 0x248080, 0x628080, 0xd78080,
86 0x808080, 0x808080, 0x808080, 0x808080,
87 0x808080, 0x808080, 0x808080, 0x808080,
88 0x808080, 0x808080, 0x808080, 0x808080
89 };
90
91 typedef struct RLE_state
92 {
93 gint id;
94 gint aligned;
95 gint offset[2];
96 gint hl_left;
97 gint hl_right;
98
99 guchar *target;
100
101 guchar next;
102 }
103 RLE_state;
104
105 static void
106 gst_dvd_sub_dec_class_init (GstDvdSubDecClass * klass)
107 {
108 GObjectClass *gobject_class;
109 GstElementClass *gstelement_class;
110
111 gobject_class = (GObjectClass *) klass;
112 gstelement_class = (GstElementClass *) klass;
113
114 gobject_class->finalize = gst_dvd_sub_dec_finalize;
115
116 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
117 gst_element_class_add_static_pad_template (gstelement_class,
118 &subtitle_template);
119
120 gst_element_class_set_static_metadata (gstelement_class,
121 "DVD subtitle decoder", "Codec/Decoder/Video",
122 "Decodes DVD subtitles into AYUV video frames",
123 "Wim Taymans <[email protected]>, "
124 "Jan Schmidt <[email protected]>");
125 }
126
127 static void
128 gst_dvd_sub_dec_init (GstDvdSubDec * dec)
129 {
130 GstPadTemplate *tmpl;
131
132 dec->sinkpad = gst_pad_new_from_static_template (&subtitle_template, "sink");
133 gst_pad_set_chain_function (dec->sinkpad,
134 GST_DEBUG_FUNCPTR (gst_dvd_sub_dec_chain));
135 gst_pad_set_event_function (dec->sinkpad,
136 GST_DEBUG_FUNCPTR (gst_dvd_sub_dec_sink_event));
137 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
138
139 tmpl = gst_static_pad_template_get (&src_template);
140 dec->srcpad = gst_pad_new_from_template (tmpl, "src");
141 gst_pad_set_event_function (dec->srcpad,
142 GST_DEBUG_FUNCPTR (gst_dvd_sub_dec_src_event));
143 gst_pad_use_fixed_caps (dec->srcpad);
144 gst_object_unref (tmpl);
145 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
146
147 /* FIXME: aren't there more possible sizes? (tpm) */
148 dec->in_width = 720;
149 dec->in_height = 576;
150
151 dec->partialbuf = NULL;
152 dec->have_title = FALSE;
153 dec->parse_pos = NULL;
154 dec->forced_display = FALSE;
155 dec->visible = FALSE;
156
157 memcpy (dec->current_clut, default_clut, sizeof (guint32) * 16);
158
159 gst_setup_palette (dec);
160
161 dec->next_ts = 0;
162 dec->next_event_ts = GST_CLOCK_TIME_NONE;
163
164 dec->buf_dirty = TRUE;
165 dec->use_ARGB = FALSE;
166 }
167
168 static void
169 gst_dvd_sub_dec_finalize (GObject * gobject)
170 {
171 GstDvdSubDec *dec = GST_DVD_SUB_DEC (gobject);
172
173 if (dec->partialbuf) {
174 gst_buffer_unmap (dec->partialbuf, &dec->partialmap);
175 gst_buffer_unref (dec->partialbuf);
176 dec->partialbuf = NULL;
177 }
178
179 G_OBJECT_CLASS (parent_class)->finalize (gobject);
180 }
181
182 static gboolean
183 gst_dvd_sub_dec_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
184 {
185 gboolean res = FALSE;
186
187 switch (GST_EVENT_TYPE (event)) {
188 default:
189 res = gst_pad_event_default (pad, parent, event);
190 break;
191 }
192
193 return res;
194 }
195
196 static GstClockTime
197 gst_dvd_sub_dec_get_event_delay (GstDvdSubDec * dec)
198 {
199 guchar *buf;
200 guint16 ticks;
201 GstClockTime event_delay;
202
203 /* If starting a new buffer, follow the first DCSQ ptr */
204 if (dec->parse_pos == dec->partialmap.data) {
205 buf = dec->parse_pos + dec->data_size;
206 } else {
207 buf = dec->parse_pos;
208 }
209
210 ticks = GST_READ_UINT16_BE (buf);
211 event_delay = gst_util_uint64_scale (ticks, 1024 * GST_SECOND, 90000);
212
213 GST_DEBUG_OBJECT (dec, "returning delay %" GST_TIME_FORMAT " from offset %u",
214 GST_TIME_ARGS (event_delay), (guint) (buf - dec->parse_pos));
215
216 return event_delay;
217 }
218
219 /*
220 * Parse the next event time in the current subpicture buffer, stopping
221 * when time advances to the next state.
222 */
223 static void
224 gst_dvd_sub_dec_parse_subpic (GstDvdSubDec * dec)
225 {
226 #define PARSE_BYTES_NEEDED(x) if ((buf+(x)) >= end) \
227 { GST_WARNING("Subtitle stream broken parsing %c", *buf); \
228 broken = TRUE; break; }
229
230 guchar *start = dec->partialmap.data;
231 guchar *buf;
232 guchar *end;
233 gboolean broken = FALSE;
234 gboolean last_seq = FALSE;
235 guchar *next_seq = NULL;
236 GstClockTime event_time;
237
238 /* nothing to do if we finished this buffer already */
239 if (dec->parse_pos == NULL)
240 return;
241
242 g_return_if_fail (dec->packet_size >= 4);
243
244 end = start + dec->packet_size;
245 if (dec->parse_pos == start) {
246 buf = dec->parse_pos + dec->data_size;
247 } else {
248 buf = dec->parse_pos;
249 }
250
251 g_assert (buf >= start && buf < end);
252
253 /* If the next control sequence is at the current offset, this is
254 * the last one */
255 next_seq = start + GST_READ_UINT16_BE (buf + 2);
256 last_seq = (next_seq == buf);
257 buf += 4;
258
259 while ((buf < end) && (!broken)) {
260 switch (*buf) {
261 case SPU_FORCE_DISPLAY: /* Forced display menu subtitle */
262 dec->forced_display = TRUE;
263 dec->buf_dirty = TRUE;
264 GST_DEBUG_OBJECT (dec, "SPU FORCE_DISPLAY");
265 buf++;
266 break;
267 case SPU_SHOW: /* Show the subtitle in this packet */
268 dec->visible = TRUE;
269 dec->buf_dirty = TRUE;
270 GST_DEBUG_OBJECT (dec, "SPU SHOW at %" GST_TIME_FORMAT,
271 GST_TIME_ARGS (dec->next_event_ts));
272 buf++;
273 break;
274 case SPU_HIDE:
275 /* 02 ff (ff) is the end of the packet, hide the subpicture */
276 dec->visible = FALSE;
277 dec->buf_dirty = TRUE;
278
279 GST_DEBUG_OBJECT (dec, "SPU HIDE at %" GST_TIME_FORMAT,
280 GST_TIME_ARGS (dec->next_event_ts));
281 buf++;
282 break;
283 case SPU_SET_PALETTE: /* palette */
284 PARSE_BYTES_NEEDED (3);
285
286 GST_DEBUG_OBJECT (dec, "SPU SET_PALETTE");
287
288 dec->subtitle_index[3] = buf[1] >> 4;
289 dec->subtitle_index[2] = buf[1] & 0xf;
290 dec->subtitle_index[1] = buf[2] >> 4;
291 dec->subtitle_index[0] = buf[2] & 0xf;
292 gst_setup_palette (dec);
293
294 dec->buf_dirty = TRUE;
295 buf += 3;
296 break;
297 case SPU_SET_ALPHA: /* transparency palette */
298 PARSE_BYTES_NEEDED (3);
299
300 GST_DEBUG_OBJECT (dec, "SPU SET_ALPHA");
301
302 dec->subtitle_alpha[3] = buf[1] >> 4;
303 dec->subtitle_alpha[2] = buf[1] & 0xf;
304 dec->subtitle_alpha[1] = buf[2] >> 4;
305 dec->subtitle_alpha[0] = buf[2] & 0xf;
306 gst_setup_palette (dec);
307
308 dec->buf_dirty = TRUE;
309 buf += 3;
310 break;
311 case SPU_SET_SIZE: /* image coordinates */
312 PARSE_BYTES_NEEDED (7);
313
314 dec->top = ((buf[4] & 0x3f) << 4) | ((buf[5] & 0xe0) >> 4);
315 dec->left = ((buf[1] & 0x3f) << 4) | ((buf[2] & 0xf0) >> 4);
316 dec->right = ((buf[2] & 0x03) << 8) | buf[3];
317 dec->bottom = ((buf[5] & 0x03) << 8) | buf[6];
318
319 GST_DEBUG_OBJECT (dec, "SPU SET_SIZE left %d, top %d, right %d, "
320 "bottom %d", dec->left, dec->top, dec->right, dec->bottom);
321
322 dec->buf_dirty = TRUE;
323 buf += 7;
324 break;
325 case SPU_SET_OFFSETS: /* image 1 / image 2 offsets */
326 PARSE_BYTES_NEEDED (5);
327
328 dec->offset[0] = (((guint) buf[1]) << 8) | buf[2];
329 dec->offset[1] = (((guint) buf[3]) << 8) | buf[4];
330 GST_DEBUG_OBJECT (dec, "Offset1 %d, Offset2 %d",
331 dec->offset[0], dec->offset[1]);
332
333 dec->buf_dirty = TRUE;
334 buf += 5;
335 break;
336 case SPU_WIPE:
337 {
338 guint length;
339
340 PARSE_BYTES_NEEDED (3);
341
342 GST_WARNING_OBJECT (dec, "SPU_WIPE not yet implemented");
343
344 length = (buf[1] << 8) | (buf[2]);
345
346 PARSE_BYTES_NEEDED (1 + length);
347 buf += 1 + length;
348
349 dec->buf_dirty = TRUE;
350 break;
351 }
352 case SPU_END:
353 buf = (last_seq) ? end : next_seq;
354
355 /* Start a new control sequence */
356 if (buf + 4 < end) {
357 guint16 ticks = GST_READ_UINT16_BE (buf);
358
359 event_time = gst_util_uint64_scale (ticks, 1024 * GST_SECOND, 90000);
360
361 GST_DEBUG_OBJECT (dec,
362 "Next DCSQ at offset %u, delay %g secs (%d ticks)",
363 (guint) (buf - start),
364 gst_util_guint64_to_gdouble (event_time / GST_SECOND), ticks);
365
366 dec->parse_pos = buf;
367 if (event_time > 0) {
368 dec->next_event_ts += event_time;
369
370 GST_LOG_OBJECT (dec, "Exiting parse loop with time %g",
371 gst_guint64_to_gdouble (dec->next_event_ts) /
372 gst_guint64_to_gdouble (GST_SECOND));
373 return;
374 }
375 break;
376 } else {
377 dec->parse_pos = NULL;
378 dec->next_event_ts = GST_CLOCK_TIME_NONE;
379 GST_LOG_OBJECT (dec, "Finished all cmds. Exiting parse loop");
380 return;
381 }
382 default:
383 GST_ERROR
384 ("Invalid sequence in subtitle packet header (%.2x). Skipping",
385 *buf);
386 broken = TRUE;
387 dec->parse_pos = NULL;
388 break;
389 }
390 }
391 }
392
393 static inline int
394 gst_get_nibble (guchar * buffer, RLE_state * state)
395 {
396 if (state->aligned) {
397 state->next = buffer[state->offset[state->id]++];
398 state->aligned = 0;
399 return state->next >> 4;
400 } else {
401 state->aligned = 1;
402 return state->next & 0xf;
403 }
404 }
405
406 /* Premultiply the current lookup table into the "target" cache */
407 static void
408 gst_setup_palette (GstDvdSubDec * dec)
409 {
410 gint i;
411 guint32 col;
412 Color_val *target_yuv = dec->palette_cache_yuv;
413 Color_val *target2_yuv = dec->hl_palette_cache_yuv;
414 Color_val *target_rgb = dec->palette_cache_rgb;
415 Color_val *target2_rgb = dec->hl_palette_cache_rgb;
416
417 for (i = 0; i < 4; i++, target2_yuv++, target_yuv++) {
418 col = dec->current_clut[dec->subtitle_index[i]];
419 target_yuv->Y_R = (col >> 16) & 0xff;
420 target_yuv->V_B = (col >> 8) & 0xff;
421 target_yuv->U_G = col & 0xff;
422 target_yuv->A = dec->subtitle_alpha[i] * 0xff / 0xf;
423
424 col = dec->current_clut[dec->menu_index[i]];
425 target2_yuv->Y_R = (col >> 16) & 0xff;
426 target2_yuv->V_B = (col >> 8) & 0xff;
427 target2_yuv->U_G = col & 0xff;
428 target2_yuv->A = dec->menu_alpha[i] * 0xff / 0xf;
429
430 /* If ARGB flag set, then convert YUV palette to RGB */
431 /* Using integer arithmetic */
432 if (dec->use_ARGB) {
433 guchar C = target_yuv->Y_R - 16;
434 guchar D = target_yuv->U_G - 128;
435 guchar E = target_yuv->V_B - 128;
436
437 target_rgb->Y_R = CLAMP (((298 * C + 409 * E + 128) >> 8), 0, 255);
438 target_rgb->U_G =
439 CLAMP (((298 * C - 100 * D - 128 * E + 128) >> 8), 0, 255);
440 target_rgb->V_B = CLAMP (((298 * C + 516 * D + 128) >> 8), 0, 255);
441 target_rgb->A = target_yuv->A;
442
443 C = target2_yuv->Y_R - 16;
444 D = target2_yuv->U_G - 128;
445 E = target2_yuv->V_B - 128;
446
447 target2_rgb->Y_R = CLAMP (((298 * C + 409 * E + 128) >> 8), 0, 255);
448 target2_rgb->U_G =
449 CLAMP (((298 * C - 100 * D - 128 * E + 128) >> 8), 0, 255);
450 target2_rgb->V_B = CLAMP (((298 * C + 516 * D + 128) >> 8), 0, 255);
451 target2_rgb->A = target2_yuv->A;
452 }
453 target_rgb++;
454 target2_rgb++;
455 }
456 }
457
458 static inline guint
459 gst_get_rle_code (guchar * buffer, RLE_state * state)
460 {
461 gint code;
462
463 code = gst_get_nibble (buffer, state);
464 if (code < 0x4) { /* 4 .. f */
465 code = (code << 4) | gst_get_nibble (buffer, state);
466 if (code < 0x10) { /* 1x .. 3x */
467 code = (code << 4) | gst_get_nibble (buffer, state);
468 if (code < 0x40) { /* 04x .. 0fx */
469 code = (code << 4) | gst_get_nibble (buffer, state);
470 }
471 }
472 }
473 return code;
474 }
475
476 #define DRAW_RUN(target,len,c) \
477 G_STMT_START { \
478 gint i = 0; \
479 if ((c)->A) { \
480 for (i = 0; i < (len); i++) { \
481 *(target)++ = (c)->A; \
482 *(target)++ = (c)->Y_R; \
483 *(target)++ = (c)->U_G; \
484 *(target)++ = (c)->V_B; \
485 } \
486 } else { \
487 (target) += 4 * (len); \
488 } \
489 } G_STMT_END
490
491 /*
492 * This function steps over each run-length segment, drawing
493 * into the YUVA/ARGB buffers as it goes. UV are composited and then output
494 * at half width/height
495 */
496 static void
497 gst_draw_rle_line (GstDvdSubDec * dec, guchar * buffer, RLE_state * state)
498 {
499 gint length, colourid;
500 guint code;
501 gint x, right;
502 guchar *target;
503
504 target = state->target;
505
506 /* If left/right is still outside the frame, clip it off */
507 x = CLAMP (dec->left, 0, dec->in_width - 1);
508 right = MIN (dec->right + 1, dec->in_width);
509
510 while (x < right) {
511 gboolean in_hl;
512 const Color_val *colour_entry;
513
514 code = gst_get_rle_code (buffer, state);
515 length = code >> 2;
516 colourid = code & 3;
517 if (dec->use_ARGB)
518 colour_entry = dec->palette_cache_rgb + colourid;
519 else
520 colour_entry = dec->palette_cache_yuv + colourid;
521
522 /* Length = 0 implies fill to the end of the line */
523 /* Restrict the colour run to the end of the line */
524 if (length == 0 || x + length > right)
525 length = right - x;
526
527 /* Check if this run of colour touches the highlight region */
528 in_hl = ((x <= state->hl_right) && (x + length) >= state->hl_left);
529 if (in_hl) {
530 gint run;
531
532 /* Draw to the left of the highlight */
533 if (x <= state->hl_left) {
534 run = MIN (length, state->hl_left - x + 1);
535
536 DRAW_RUN (target, run, colour_entry);
537 length -= run;
538 x += run;
539 }
540
541 /* Draw across the highlight region */
542 if (x <= state->hl_right) {
543 const Color_val *hl_colour;
544 if (dec->use_ARGB)
545 hl_colour = dec->hl_palette_cache_rgb + colourid;
546 else
547 hl_colour = dec->hl_palette_cache_yuv + colourid;
548
549 run = MIN (length, state->hl_right - x + 1);
550
551 DRAW_RUN (target, run, hl_colour);
552 length -= run;
553 x += run;
554 }
555 }
556
557 /* Draw the rest of the run */
558 if (length > 0) {
559 DRAW_RUN (target, length, colour_entry);
560 x += length;
561 }
562 }
563 }
564
565 /*
566 * Decode the RLE subtitle image and blend with the current
567 * frame buffer.
568 */
569 static void
570 gst_dvd_sub_dec_merge_title (GstDvdSubDec * dec, GstVideoFrame * frame)
571 {
572 gint y;
573 gint Y_stride;
574 guchar *buffer = dec->partialmap.data;
575 gint hl_top, hl_bottom;
576 gint last_y;
577 RLE_state state;
578 guint8 *Y_data;
579
580 GST_DEBUG_OBJECT (dec, "Merging subtitle on frame");
581
582 Y_data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
583 Y_stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
584
585 state.id = 0;
586 state.aligned = 1;
587 state.next = 0;
588 state.offset[0] = dec->offset[0];
589 state.offset[1] = dec->offset[1];
590
591 /* center the image when display rectangle exceeds the video width */
592 if (dec->in_width <= dec->right) {
593 gint left, disp_width;
594
595 disp_width = dec->right - dec->left + 1;
596 left = (dec->in_width - disp_width) / 2;
597 dec->left = left;
598 dec->right = left + disp_width - 1;
599
600 /* if it clips to the right, shift it left, but only till zero */
601 if (dec->right >= dec->in_width) {
602 gint shift = dec->right - dec->in_width - 1;
603 if (shift > dec->left)
604 shift = dec->left;
605 dec->left -= shift;
606 dec->right -= shift;
607 }
608
609 GST_DEBUG_OBJECT (dec, "clipping width to %d,%d",
610 dec->left, dec->in_width - 1);
611 }
612
613 /* for the height, bring it up till it fits as well as it can. We
614 * assume the picture is in the lower part. We should better check where it
615 * is and do something more clever. */
616 if (dec->in_height <= dec->bottom) {
617
618 /* shift it up, but only till zero */
619 gint shift = dec->bottom - dec->in_height - 1;
620 if (shift > dec->top)
621 shift = dec->top;
622 dec->top -= shift;
623 dec->bottom -= shift;
624
625 /* start on even line */
626 if (dec->top & 1) {
627 dec->top--;
628 dec->bottom--;
629 }
630
631 GST_DEBUG_OBJECT (dec, "clipping height to %d,%d",
632 dec->top, dec->in_height - 1);
633 }
634
635 if (dec->current_button) {
636 hl_top = dec->hl_top;
637 hl_bottom = dec->hl_bottom;
638 } else {
639 hl_top = -1;
640 hl_bottom = -1;
641 }
642
643 /* If top/bottom is still outside the frame, clip it off */
644 last_y = MIN (dec->bottom, dec->in_height - 1);
645 y = CLAMP (dec->top, 0, dec->in_height - 1);
646
647 state.target = Y_data + 4 * dec->left + (y * Y_stride);
648
649 /* Now draw scanlines until we hit last_y or end of RLE data */
650 for (; ((state.offset[state.id] < dec->data_size + 2) && (y <= last_y)); y++) {
651 /* Set up to draw the highlight if we're in the right scanlines */
652 if (y > hl_bottom || y < hl_top) {
653 state.hl_left = -1;
654 state.hl_right = -1;
655 } else {
656 state.hl_left = dec->hl_left;
657 state.hl_right = dec->hl_right;
658 }
659 gst_draw_rle_line (dec, buffer, &state);
660
661 state.target += Y_stride;
662
663 /* Realign the RLE state for the next line */
664 if (!state.aligned)
665 gst_get_nibble (buffer, &state);
666 state.id = !state.id;
667 }
668 }
669
670 static void
671 gst_send_empty_fill (GstDvdSubDec * dec, GstClockTime ts)
672 {
673 if (dec->next_ts < ts) {
674 GST_LOG_OBJECT (dec, "Sending GAP event update to advance time to %"
675 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
676
677 gst_pad_push_event (dec->srcpad,
678 gst_event_new_gap (dec->next_ts, ts - dec->next_ts));
679 }
680 dec->next_ts = ts;
681 }
682
683 static GstFlowReturn
684 gst_send_subtitle_frame (GstDvdSubDec * dec, GstClockTime end_ts)
685 {
686 GstFlowReturn flow;
687 GstBuffer *out_buf;
688 GstVideoFrame frame;
689 guint8 *data;
690 gint x, y;
691 static GstAllocationParams params = { 0, 3, 0, 0, };
692
693 g_assert (dec->have_title);
694 g_assert (dec->next_ts <= end_ts);
695
696 /* Check if we need to redraw the output buffer */
697 if (!dec->buf_dirty) {
698 flow = GST_FLOW_OK;
699 goto out;
700 }
701
702 out_buf =
703 gst_buffer_new_allocate (NULL, GST_VIDEO_INFO_SIZE (&dec->info), &params);
704 gst_video_frame_map (&frame, &dec->info, out_buf, GST_MAP_READWRITE);
705
706 data = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
707
708 /* Clear the buffer */
709 /* FIXME - move this into the buffer rendering code */
710 for (y = 0; y < dec->in_height; y++) {
711 guchar *line = data + 4 * dec->in_width * y;
712
713 for (x = 0; x < dec->in_width; x++) {
714 line[0] = 0; /* A */
715 if (!dec->use_ARGB) {
716 line[1] = 16; /* Y */
717 line[2] = 128; /* U */
718 line[3] = 128; /* V */
719 } else {
720 line[1] = 0; /* R */
721 line[2] = 0; /* G */
722 line[3] = 0; /* B */
723 }
724
725 line += 4;
726 }
727 }
728
729 /* FIXME: do we really want to honour the forced_display flag
730 * for subtitles streans? */
731 if (dec->visible || dec->forced_display) {
732 gst_dvd_sub_dec_merge_title (dec, &frame);
733 }
734
735 gst_video_frame_unmap (&frame);
736
737 dec->buf_dirty = FALSE;
738
739 GST_BUFFER_TIMESTAMP (out_buf) = dec->next_ts;
740 if (GST_CLOCK_TIME_IS_VALID (dec->next_event_ts)) {
741 GST_BUFFER_DURATION (out_buf) = GST_CLOCK_DIFF (dec->next_ts,
742 dec->next_event_ts);
743 } else {
744 GST_BUFFER_DURATION (out_buf) = GST_CLOCK_TIME_NONE;
745 }
746
747 GST_DEBUG_OBJECT (dec, "Sending subtitle buffer with ts %"
748 GST_TIME_FORMAT ", dur %" G_GINT64_FORMAT,
749 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (out_buf)),
750 GST_BUFFER_DURATION (out_buf));
751
752 flow = gst_pad_push (dec->srcpad, out_buf);
753
754 out:
755 dec->next_ts = end_ts;
756 return flow;
757 }
758
759 /* Walk time forward, processing any subtitle events as needed. */
760 static GstFlowReturn
761 gst_dvd_sub_dec_advance_time (GstDvdSubDec * dec, GstClockTime new_ts)
762 {
763 GstFlowReturn ret = GST_FLOW_OK;
764
765 GST_LOG_OBJECT (dec, "Advancing time to %" GST_TIME_FORMAT,
766 GST_TIME_ARGS (new_ts));
767
768 if (!dec->have_title) {
769 gst_send_empty_fill (dec, new_ts);
770 return ret;
771 }
772
773 while (dec->next_ts < new_ts) {
774 GstClockTime next_ts = new_ts;
775
776 if (GST_CLOCK_TIME_IS_VALID (dec->next_event_ts) &&
777 dec->next_event_ts < next_ts) {
778 /* We might need to process the subtitle cmd queue */
779 next_ts = dec->next_event_ts;
780 }
781
782 /*
783 * Now, either output a filler or a frame spanning
784 * dec->next_ts to next_ts
785 */
786 if (dec->visible || dec->forced_display) {
787 ret = gst_send_subtitle_frame (dec, next_ts);
788 } else {
789 gst_send_empty_fill (dec, next_ts);
790 }
791
792 /*
793 * and then process some subtitle cmds if we need
794 */
795 if (next_ts == dec->next_event_ts)
796 gst_dvd_sub_dec_parse_subpic (dec);
797 }
798
799 return ret;
800 }
801
802 static GstFlowReturn
803 gst_dvd_sub_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
804 {
805 GstFlowReturn ret = GST_FLOW_OK;
806 GstDvdSubDec *dec;
807 guint8 *data;
808 glong size = 0;
809
810 dec = GST_DVD_SUB_DEC (parent);
811
812 GST_DEBUG_OBJECT (dec, "Have buffer of size %" G_GSIZE_FORMAT ", ts %"
813 GST_TIME_FORMAT ", dur %" G_GINT64_FORMAT, gst_buffer_get_size (buf),
814 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_BUFFER_DURATION (buf));
815
816 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
817 if (!GST_CLOCK_TIME_IS_VALID (dec->next_ts)) {
818 dec->next_ts = GST_BUFFER_TIMESTAMP (buf);
819 }
820
821 /* Move time forward to the start of the new buffer */
822 ret = gst_dvd_sub_dec_advance_time (dec, GST_BUFFER_TIMESTAMP (buf));
823 }
824
825 if (dec->have_title) {
826 gst_buffer_unmap (dec->partialbuf, &dec->partialmap);
827 gst_buffer_unref (dec->partialbuf);
828 dec->partialbuf = NULL;
829 dec->have_title = FALSE;
830 }
831
832 GST_DEBUG_OBJECT (dec, "Got subtitle buffer, pts %" GST_TIME_FORMAT,
833 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
834
835 /* deal with partial frame from previous buffer */
836 if (dec->partialbuf) {
837 gst_buffer_unmap (dec->partialbuf, &dec->partialmap);
838 dec->partialbuf = gst_buffer_append (dec->partialbuf, buf);
839 } else {
840 dec->partialbuf = buf;
841 }
842
843 gst_buffer_map (dec->partialbuf, &dec->partialmap, GST_MAP_READ);
844
845 data = dec->partialmap.data;
846 size = dec->partialmap.size;
847
848 if (size > 4) {
849 dec->packet_size = GST_READ_UINT16_BE (data);
850
851 if (dec->packet_size == size) {
852 GST_LOG_OBJECT (dec, "Subtitle packet size %d, current size %ld",
853 dec->packet_size, size);
854
855 dec->data_size = GST_READ_UINT16_BE (data + 2);
856
857 /* Reset parameters for a new subtitle buffer */
858 dec->parse_pos = data;
859 dec->forced_display = FALSE;
860 dec->visible = FALSE;
861
862 dec->have_title = TRUE;
863 dec->next_event_ts = GST_BUFFER_TIMESTAMP (dec->partialbuf);
864
865 if (!GST_CLOCK_TIME_IS_VALID (dec->next_event_ts))
866 dec->next_event_ts = dec->next_ts;
867
868 dec->next_event_ts += gst_dvd_sub_dec_get_event_delay (dec);
869 }
870 }
871
872 return ret;
873 }
874
875 static gboolean
876 gst_dvd_sub_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
877 {
878 GstDvdSubDec *dec = GST_DVD_SUB_DEC (gst_pad_get_parent (pad));
879 gboolean ret = FALSE;
880 GstCaps *out_caps = NULL, *peer_caps = NULL;
881
882 GST_DEBUG_OBJECT (dec, "setcaps called with %" GST_PTR_FORMAT, caps);
883
884 out_caps = gst_caps_new_simple ("video/x-raw",
885 "format", G_TYPE_STRING, "AYUV",
886 "width", G_TYPE_INT, dec->in_width,
887 "height", G_TYPE_INT, dec->in_height,
888 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
889
890 peer_caps = gst_pad_get_allowed_caps (dec->srcpad);
891 if (G_LIKELY (peer_caps)) {
892 guint i = 0, n = 0;
893
894 n = gst_caps_get_size (peer_caps);
895 GST_DEBUG_OBJECT (dec, "peer allowed caps (%u structure(s)) are %"
896 GST_PTR_FORMAT, n, peer_caps);
897
898 for (i = 0; i < n; i++) {
899 GstStructure *s = gst_caps_get_structure (peer_caps, i);
900 /* Check if the peer pad support ARGB format, if yes change caps */
901 if (gst_structure_has_name (s, "video/x-raw")) {
902 GstCaps *downstream_caps;
903 gst_caps_unref (out_caps);
904 GST_DEBUG_OBJECT (dec, "trying with ARGB");
905
906 out_caps = gst_caps_new_simple ("video/x-raw",
907 "format", G_TYPE_STRING, "ARGB",
908 "width", G_TYPE_INT, dec->in_width,
909 "height", G_TYPE_INT, dec->in_height,
910 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
911
912 downstream_caps = gst_pad_peer_query_caps (dec->srcpad, NULL);
913 if (gst_caps_can_intersect (downstream_caps, out_caps)) {
914 gst_caps_unref (downstream_caps);
915 GST_DEBUG_OBJECT (dec, "peer accepted ARGB");
916 /* If ARGB format then set the flag */
917 dec->use_ARGB = TRUE;
918 break;
919 }
920 gst_caps_unref (downstream_caps);
921 }
922 }
923 gst_caps_unref (peer_caps);
924 }
925 GST_DEBUG_OBJECT (dec, "setting caps downstream to %" GST_PTR_FORMAT,
926 out_caps);
927 if (gst_pad_set_caps (dec->srcpad, out_caps)) {
928 gst_video_info_from_caps (&dec->info, out_caps);
929 } else {
930 GST_WARNING_OBJECT (dec, "failed setting downstream caps");
931 gst_caps_unref (out_caps);
932 goto beach;
933 }
934
935 gst_caps_unref (out_caps);
936 ret = TRUE;
937
938 beach:
939 gst_object_unref (dec);
940 return ret;
941 }
942
943 static gboolean
944 gst_dvd_sub_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
945 {
946 GstDvdSubDec *dec = GST_DVD_SUB_DEC (parent);
947 gboolean ret = FALSE;
948
949 GST_LOG_OBJECT (dec, "%s event", GST_EVENT_TYPE_NAME (event));
950
951 switch (GST_EVENT_TYPE (event)) {
952 case GST_EVENT_CAPS:
953 {
954 GstCaps *caps;
955
956 gst_event_parse_caps (event, &caps);
957 ret = gst_dvd_sub_dec_sink_setcaps (pad, caps);
958 gst_event_unref (event);
959 break;
960 }
961 case GST_EVENT_CUSTOM_DOWNSTREAM:{
962
963 if (gst_event_has_name (event, "application/x-gst-dvd")) {
964 const GstStructure *s = gst_event_get_structure (event);
965 GstClockTime ts = GST_CLOCK_TIME_NONE;
966
967 if (gst_structure_get_clock_time (s, "ts", &ts)
968 && GST_CLOCK_TIME_IS_VALID (ts))
969 gst_dvd_sub_dec_advance_time (dec, ts);
970
971 if (gst_dvd_sub_dec_handle_dvd_event (dec, event)) {
972 /* gst_dvd_sub_dec_advance_time (dec, dec->next_ts + GST_SECOND / 30.0); */
973 gst_event_unref (event);
974 ret = TRUE;
975 break;
976 }
977 }
978
979 ret = gst_pad_event_default (pad, parent, event);
980 break;
981 }
982 case GST_EVENT_GAP:
983 {
984 GstClockTime start, duration;
985
986 gst_event_parse_gap (event, &start, &duration);
987 if (GST_CLOCK_TIME_IS_VALID (start)) {
988 if (GST_CLOCK_TIME_IS_VALID (duration))
989 start += duration;
990 /* we do not expect another buffer until after gap,
991 * so that is our position now */
992 GST_DEBUG_OBJECT (dec, "Got GAP event, advancing time from %"
993 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
994 GST_TIME_ARGS (dec->next_ts), GST_TIME_ARGS (start));
995
996 gst_dvd_sub_dec_advance_time (dec, start);
997 } else {
998 GST_WARNING_OBJECT (dec, "Got GAP event with invalid position");
999 }
1000
1001 gst_event_unref (event);
1002 ret = TRUE;
1003 break;
1004 }
1005 case GST_EVENT_SEGMENT:
1006 {
1007 GstSegment seg;
1008
1009 gst_event_copy_segment (event, &seg);
1010
1011 {
1012 #if 0
1013 /* Turn off forced highlight display */
1014 dec->forced_display = 0;
1015 dec->current_button = 0;
1016 #endif
1017 if (dec->partialbuf) {
1018 gst_buffer_unmap (dec->partialbuf, &dec->partialmap);
1019 gst_buffer_unref (dec->partialbuf);
1020 dec->partialbuf = NULL;
1021 dec->have_title = FALSE;
1022 }
1023
1024 if (GST_CLOCK_TIME_IS_VALID (seg.time))
1025 dec->next_ts = seg.time;
1026 else
1027 dec->next_ts = GST_CLOCK_TIME_NONE;
1028
1029 GST_DEBUG_OBJECT (dec, "Got newsegment, new time = %"
1030 GST_TIME_FORMAT, GST_TIME_ARGS (dec->next_ts));
1031
1032 ret = gst_pad_event_default (pad, parent, event);
1033 }
1034 break;
1035 }
1036 case GST_EVENT_FLUSH_STOP:{
1037 /* Turn off forced highlight display */
1038 dec->forced_display = 0;
1039 dec->current_button = 0;
1040
1041 if (dec->partialbuf) {
1042 gst_buffer_unmap (dec->partialbuf, &dec->partialmap);
1043 gst_buffer_unref (dec->partialbuf);
1044 dec->partialbuf = NULL;
1045 dec->have_title = FALSE;
1046 }
1047
1048 ret = gst_pad_event_default (pad, parent, event);
1049 break;
1050 }
1051 default:{
1052 ret = gst_pad_event_default (pad, parent, event);
1053 break;
1054 }
1055 }
1056 return ret;
1057 }
1058
1059 static gboolean
1060 gst_dvd_sub_dec_handle_dvd_event (GstDvdSubDec * dec, GstEvent * event)
1061 {
1062 GstStructure *structure;
1063 const gchar *event_name;
1064
1065 structure = (GstStructure *) gst_event_get_structure (event);
1066
1067 if (structure == NULL)
1068 goto not_handled;
1069
1070 event_name = gst_structure_get_string (structure, "event");
1071
1072 GST_LOG_OBJECT (dec,
1073 "DVD event %s with timestamp %" G_GINT64_FORMAT " on sub pad",
1074 GST_STR_NULL (event_name), GST_EVENT_TIMESTAMP (event));
1075
1076 if (event_name == NULL)
1077 goto not_handled;
1078
1079 if (strcmp (event_name, "dvd-spu-highlight") == 0) {
1080 gint button;
1081 gint palette, sx, sy, ex, ey;
1082 gint i;
1083
1084 /* Details for the highlight region to display */
1085 if (!gst_structure_get_int (structure, "button", &button) ||
1086 !gst_structure_get_int (structure, "palette", &palette) ||
1087 !gst_structure_get_int (structure, "sx", &sx) ||
1088 !gst_structure_get_int (structure, "sy", &sy) ||
1089 !gst_structure_get_int (structure, "ex", &ex) ||
1090 !gst_structure_get_int (structure, "ey", &ey)) {
1091 GST_ERROR_OBJECT (dec, "Invalid dvd-spu-highlight event received");
1092 return TRUE;
1093 }
1094 dec->current_button = button;
1095 dec->hl_left = sx;
1096 dec->hl_top = sy;
1097 dec->hl_right = ex;
1098 dec->hl_bottom = ey;
1099 for (i = 0; i < 4; i++) {
1100 dec->menu_alpha[i] = ((guint32) (palette) >> (i * 4)) & 0x0f;
1101 dec->menu_index[i] = ((guint32) (palette) >> (16 + (i * 4))) & 0x0f;
1102 }
1103
1104 GST_DEBUG_OBJECT (dec, "New button activated highlight=(%d,%d) to (%d,%d) "
1105 "palette 0x%x", sx, sy, ex, ey, palette);
1106 gst_setup_palette (dec);
1107
1108 dec->buf_dirty = TRUE;
1109 } else if (strcmp (event_name, "dvd-spu-clut-change") == 0) {
1110 /* Take a copy of the colour table */
1111 gchar name[16];
1112 int i;
1113 gint value;
1114
1115 GST_LOG_OBJECT (dec, "New colour table received");
1116 for (i = 0; i < 16; i++) {
1117 g_snprintf (name, sizeof (name), "clut%02d", i);
1118 if (!gst_structure_get_int (structure, name, &value)) {
1119 GST_ERROR_OBJECT (dec, "dvd-spu-clut-change event did not "
1120 "contain %s field", name);
1121 break;
1122 }
1123 dec->current_clut[i] = (guint32) (value);
1124 }
1125
1126 gst_setup_palette (dec);
1127
1128 dec->buf_dirty = TRUE;
1129 } else if (strcmp (event_name, "dvd-spu-stream-change") == 0
1130 || strcmp (event_name, "dvd-spu-reset-highlight") == 0) {
1131 /* Turn off forced highlight display */
1132 dec->current_button = 0;
1133
1134 GST_LOG_OBJECT (dec, "Clearing button state");
1135 dec->buf_dirty = TRUE;
1136 } else if (strcmp (event_name, "dvd-spu-still-frame") == 0) {
1137 /* Handle a still frame */
1138 GST_LOG_OBJECT (dec, "Received still frame notification");
1139 } else {
1140 goto not_handled;
1141 }
1142
1143 return TRUE;
1144
1145 not_handled:
1146 {
1147 /* Ignore all other unknown events */
1148 GST_LOG_OBJECT (dec, "Ignoring other custom event %" GST_PTR_FORMAT,
1149 structure);
1150 return FALSE;
1151 }
1152 }
1153
1154 static gboolean
1155 plugin_init (GstPlugin * plugin)
1156 {
1157 gboolean ret = FALSE;
1158
1159 ret |= GST_ELEMENT_REGISTER (dvdsubdec, plugin);
1160 ret |= GST_ELEMENT_REGISTER (dvdsubparse, plugin);
1161
1162 return ret;
1163 }
1164
1165 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1166 GST_VERSION_MINOR,
1167 dvdsub,
1168 "DVD subtitle parser and decoder", plugin_init,
1169 VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);