Debian Codegraph @

gst/dvdspu/gstspu-pgs.c

raw symbols SCIP
1 /* GStreamer Sub-Picture Unit - PGS handling
2 * Copyright (C) 2009 Jan Schmidt <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <string.h>
24 #include <gst/gst.h>
25
26 #include "gstdvdspu.h"
27 #include "gstspu-pgs.h"
28
29 const struct PgsFrameRateEntry
30 {
31 guint8 id;
32 guint fps_n;
33 guint fps_d;
34 } PgsFrameRates[] = {
35 {
36 64, 30000, 1001} /* 29.97 FPS */
37 };
38
39 typedef enum PgsCommandType PgsCommandType;
40
41 enum PgsCommandType
42 {
43 PGS_COMMAND_SET_PALETTE = 0x14,
44 PGS_COMMAND_SET_OBJECT_DATA = 0x15,
45 PGS_COMMAND_PRESENTATION_SEGMENT = 0x16,
46 PGS_COMMAND_SET_WINDOW = 0x17,
47 PGS_COMMAND_INTERACTIVE_SEGMENT = 0x18,
48
49 PGS_COMMAND_END_DISPLAY = 0x80,
50
51 PGS_COMMAND_INVALID = 0xFFFF
52 };
53
54 static gint gstspu_exec_pgs_buffer (GstDVDSpu * dvdspu, GstBuffer * buf);
55
56 #define DUMP_CMDS 0
57 #define DUMP_FULL_IMAGE 0
58 #define DUMP_FULL_PALETTE 0
59
60 #if DUMP_CMDS
61 #define PGS_DUMP(...) g_print(__VA_ARGS__)
62 #else
63 #define PGS_DUMP(...)
64 #endif
65
66 static void
67 dump_bytes (guint8 * data, guint16 len)
68 {
69 gint i;
70
71 /* Dump the numbers */
72 for (i = 0; i < len; i++) {
73 PGS_DUMP ("0x%02x ", data[i]);
74 if (!((i + 1) % 16))
75 PGS_DUMP ("\n");
76 }
77 if (len > 0 && (i % 16))
78 PGS_DUMP ("\n");
79 }
80
81 static void
82 dump_rle_data (GstDVDSpu * dvdspu, guint8 * data, guint32 len)
83 {
84 #if DUMP_FULL_IMAGE
85 guint16 obj_h;
86 guint16 obj_w;
87 guint8 *end = data + len;
88 guint x = 0;
89
90 if (data + 4 > end)
91 return;
92
93 /* RLE data: */
94 obj_w = GST_READ_UINT16_BE (data);
95 obj_h = GST_READ_UINT16_BE (data + 2);
96 data += 4;
97 PGS_DUMP ("RLE image is %ux%u\n", obj_w, obj_h);
98
99 while (data < end) {
100 guint8 pal_id;
101 guint16 run_len;
102
103 pal_id = *data++;
104 if (pal_id != 0) {
105 // PGS_DUMP ("data 0x%02x\n", data[0]);
106 run_len = 1;
107 } else {
108 if (data + 1 > end)
109 return;
110 switch (data[0] & 0xC0) {
111 case 0x00:
112 //PGS_DUMP ("data 0x%02x\n", data[0]);
113 run_len = (data[0] & 0x3f);
114 data++;
115 break;
116 case 0x40:
117 if (data + 2 > end)
118 return;
119 //PGS_DUMP ("data 0x%02x 0x%02x\n", data[0], data[1]);
120 run_len = ((data[0] << 8) | data[1]) & 0x3fff;
121 data += 2;
122 break;
123 case 0x80:
124 if (data + 2 > end)
125 return;
126 //PGS_DUMP ("data 0x%02x 0x%02x\n", data[0], data[1]);
127 run_len = (data[0] & 0x3f);
128 pal_id = data[1];
129 data += 2;
130 break;
131 case 0xC0:
132 if (data + 3 > end)
133 return;
134 //PGS_DUMP ("data 0x%02x 0x%02x 0x%02x\n", data[0], data[1], data[2]);
135 run_len = ((data[0] << 8) | data[1]) & 0x3fff;
136 pal_id = data[2];
137 data += 3;
138 break;
139 default:
140 run_len = 0;
141 break;
142 }
143 }
144
145 {
146 gint i;
147 #if 1
148 if (dvdspu->spu_state.pgs.palette[pal_id].A) {
149 guint8 val = dvdspu->spu_state.pgs.palette[pal_id].A;
150 for (i = 0; i < run_len; i++)
151 PGS_DUMP ("%02x ", val);
152 } else {
153 for (i = 0; i < run_len; i++)
154 PGS_DUMP (" ");
155 }
156 if (!run_len || (x + run_len) > obj_w)
157 PGS_DUMP ("\n");
158 #else
159 PGS_DUMP ("Run x: %d pix: %d col: %d\n", x, run_len, pal_id);
160 #endif
161 }
162
163 x += run_len;
164 if (!run_len || x > obj_w)
165 x = 0;
166 };
167
168 PGS_DUMP ("\n");
169 #endif
170 }
171
172 static void
173 pgs_composition_object_render (PgsCompositionObject * obj, SpuState * state,
174 GstVideoFrame * window)
175 {
176 SpuColour *colour;
177 guint8 *pixels, *p;
178 gint stride;
179 gint win_w;
180 gint win_h;
181 guint8 *data, *end;
182 guint16 obj_w, obj_h;
183 gint obj_x, obj_y;
184 gint min_x, max_x;
185 gint min_y, max_y;
186 gint x, y, i;
187
188 if (G_UNLIKELY (obj->rle_data == NULL || obj->rle_data_size == 0
189 || obj->rle_data_used != obj->rle_data_size))
190 return;
191
192 data = obj->rle_data;
193 end = data + obj->rle_data_used;
194
195 if (data + 4 > end)
196 return;
197
198 pixels = GST_VIDEO_FRAME_PLANE_DATA (window, 0);
199 stride = GST_VIDEO_FRAME_PLANE_STRIDE (window, 0);
200 win_w = GST_VIDEO_FRAME_WIDTH (window);
201 win_h = GST_VIDEO_FRAME_HEIGHT (window);
202
203 obj_w = GST_READ_UINT16_BE (data);
204 obj_h = GST_READ_UINT16_BE (data + 2);
205 data += 4;
206
207 /* window frame is located at (obj_x, obj_y) with size (obj_w, obj_h) */
208 g_assert (obj_w <= win_w);
209 g_assert (obj_h <= win_h);
210 min_x = obj_x = 0;
211 min_y = obj_y = 0;
212
213 if (obj->flags & PGS_COMPOSITION_OBJECT_FLAG_CROPPED) {
214 obj_x -= obj->crop_x;
215 obj_y -= obj->crop_y;
216 obj_w = MIN (obj_w, obj->crop_w);
217 obj_h = MIN (obj_h, obj->crop_h);
218 }
219
220 max_x = min_x + obj_w;
221 max_y = min_y + obj_h;
222
223 /* Early out if object is out of the window */
224 if (max_x <= 0 || max_y < 0 || min_x >= win_w || min_y >= win_h)
225 return;
226
227 /* Crop inside window */
228 if (min_x < 0)
229 min_x = 0;
230 if (max_x > win_w)
231 max_x = win_w;
232 if (min_y < 0)
233 min_y = 0;
234 if (max_y > win_h)
235 max_y = win_h;
236
237 /* Write RLE data to the plane */
238 x = obj_x;
239 y = obj_y;
240 p = pixels + y * stride;
241
242 while (data < end) {
243 guint8 pal_id;
244 guint16 run_len;
245
246 pal_id = *data++;
247 if (pal_id != 0) {
248 run_len = 1;
249 } else {
250 if (data + 1 > end)
251 return;
252 switch (data[0] & 0xC0) {
253 case 0x00:
254 run_len = (data[0] & 0x3f);
255 data++;
256 break;
257 case 0x40:
258 if (data + 2 > end)
259 return;
260 run_len = ((data[0] << 8) | data[1]) & 0x3fff;
261 data += 2;
262 break;
263 case 0x80:
264 if (data + 2 > end)
265 return;
266 run_len = (data[0] & 0x3f);
267 pal_id = data[1];
268 data += 2;
269 break;
270 case 0xC0:
271 if (data + 3 > end)
272 return;
273 run_len = ((data[0] << 8) | data[1]) & 0x3fff;
274 pal_id = data[2];
275 data += 3;
276 break;
277 default:
278 run_len = 0;
279 break;
280 }
281 }
282
283 if (!run_len) {
284 x = obj_x;
285 y++;
286 if (y >= max_y)
287 break;
288
289 p = pixels + y * stride;
290 continue;
291 }
292
293 if (y < min_y)
294 continue;
295
296 if (x >= max_x)
297 continue;
298
299 if (x < min_x) {
300 if (x + run_len <= min_x) {
301 x += run_len;
302 continue;
303 } else {
304 run_len -= min_x - x;
305 x = min_x;
306 }
307 }
308
309 colour = &state->pgs.palette[pal_id];
310
311 if (colour->A > 0) {
312 guint8 inv_A = 255 - colour->A;
313
314 if (G_UNLIKELY (x + run_len > max_x))
315 run_len = max_x - x;
316
317 for (i = 0; i < run_len; i++) {
318 SpuColour *pix = &((SpuColour *) p)[x++];
319
320 if (pix->A == 0) {
321 memcpy (pix, colour, sizeof (*pix));
322 } else {
323 pix->A = colour->A;
324 pix->R = colour->R + pix->R * inv_A / 255;
325 pix->G = colour->G + pix->G * inv_A / 255;
326 pix->B = colour->B + pix->B * inv_A / 255;
327 }
328 }
329 } else {
330 x += run_len;
331 }
332 }
333 }
334
335 static void
336 pgs_composition_object_clear (PgsCompositionObject * obj)
337 {
338 if (obj->rle_data) {
339 g_free (obj->rle_data);
340 obj->rle_data = NULL;
341 }
342 /* restore to cleared allocated state */
343 memset ((char *) obj, 0, sizeof (*obj));
344 }
345
346 static void
347 pgs_presentation_segment_set_object_count (PgsPresentationSegment * ps,
348 guint8 n_objects)
349 {
350 if (ps->objects == NULL) {
351 if (n_objects == 0)
352 return;
353 ps->objects =
354 g_array_sized_new (FALSE, TRUE, sizeof (PgsCompositionObject),
355 n_objects);
356 g_array_set_size (ps->objects, n_objects);
357 return;
358 }
359
360 /* Clear memory in any extraneous objects */
361 if (ps->objects->len > n_objects) {
362 guint i;
363 for (i = n_objects; i < ps->objects->len; i++) {
364 PgsCompositionObject *cur =
365 &g_array_index (ps->objects, PgsCompositionObject, i);
366 pgs_composition_object_clear (cur);
367 }
368 }
369
370 g_array_set_size (ps->objects, n_objects);
371
372 if (n_objects == 0) {
373 g_array_free (ps->objects, TRUE);
374 ps->objects = NULL;
375 }
376 }
377
378 static PgsCompositionObject *
379 pgs_presentation_segment_find_object (PgsPresentationSegment * ps,
380 guint16 obj_id)
381 {
382 guint i;
383 if (ps->objects == NULL)
384 return NULL;
385
386 for (i = 0; i < ps->objects->len; i++) {
387 PgsCompositionObject *cur =
388 &g_array_index (ps->objects, PgsCompositionObject, i);
389 if (cur->id == obj_id)
390 return cur;
391 }
392
393 return NULL;
394 }
395
396 static int
397 parse_presentation_segment (GstDVDSpu * dvdspu, guint8 type, guint8 * payload,
398 guint16 len)
399 {
400 guint8 *end = payload + len;
401 PgsPresentationSegment *ps = &dvdspu->spu_state.pgs.pres_seg;
402 guint8 n_objects, palette_id;
403 gint i;
404
405 /* Parse video descriptor */
406 if (payload + 5 > end)
407 return 0;
408
409 ps->vid_w = GST_READ_UINT16_BE (payload);
410 ps->vid_h = GST_READ_UINT16_BE (payload + 2);
411 ps->vid_fps_code = payload[4];
412 payload += 5;
413
414 /* Parse composition descriptor */
415 if (payload + 3 > end)
416 return 0;
417 ps->composition_no = GST_READ_UINT16_BE (payload);
418 ps->composition_state = payload[2];
419 payload += 3;
420
421 /* Parse other bits */
422 if (payload + 3 > end)
423 return 0;
424
425 ps->flags = payload[0];
426
427 palette_id = payload[1];
428 n_objects = payload[2];
429 payload += 3;
430
431 if (ps->flags & PGS_PRES_SEGMENT_FLAG_UPDATE_PALETTE)
432 ps->palette_id = palette_id;
433
434 PGS_DUMP ("Video width %u height %u fps code %u\n", ps->vid_w, ps->vid_h,
435 ps->vid_fps_code);
436 PGS_DUMP
437 ("Composition num %u state 0x%02x flags 0x%02x palette id %u n_objects %u\n",
438 ps->composition_no, ps->composition_state, ps->flags, ps->palette_id,
439 n_objects);
440
441 pgs_presentation_segment_set_object_count (ps, n_objects);
442
443 for (i = 0; i < (gint) n_objects; i++) {
444 PgsCompositionObject *obj =
445 &g_array_index (ps->objects, PgsCompositionObject, i);
446
447 if (payload + 8 > end)
448 break;
449 obj->id = GST_READ_UINT16_BE (payload);
450 obj->win_id = payload[2];
451 obj->flags = payload[3];
452 obj->x = GST_READ_UINT16_BE (payload + 4);
453 obj->y = GST_READ_UINT16_BE (payload + 6);
454 obj->rle_data_size = obj->rle_data_used = 0;
455
456 payload += 8;
457
458 PGS_DUMP ("Composition object %d Object ID %u Window ID %u flags 0x%02x "
459 "x %u y %u\n", i, obj->id, obj->win_id, obj->flags, obj->x, obj->y);
460
461 if (obj->flags & PGS_COMPOSITION_OBJECT_FLAG_CROPPED) {
462 if (payload + 8 > end) {
463 obj->flags &= ~PGS_COMPOSITION_OBJECT_FLAG_CROPPED;
464 break;
465 }
466
467 obj->crop_x = GST_READ_UINT16_BE (payload);
468 obj->crop_y = GST_READ_UINT16_BE (payload + 2);
469 obj->crop_w = GST_READ_UINT16_BE (payload + 4);
470 obj->crop_h = GST_READ_UINT16_BE (payload + 6);
471
472 payload += 8;
473
474 PGS_DUMP ("Cropping window x %u y %u w %u h %u\n",
475 obj->crop_x, obj->crop_y, obj->crop_w, obj->crop_h);
476 }
477
478 if (obj->flags & ~(PGS_COMPOSITION_OBJECT_FLAG_CROPPED |
479 PGS_COMPOSITION_OBJECT_FLAG_FORCED))
480 GST_ERROR ("PGS Composition Object has unknown flags: 0x%02x",
481 obj->flags);
482 }
483
484 if (payload != end) {
485 GST_ERROR ("PGS Composition Object: %" G_GSSIZE_FORMAT
486 " bytes not consumed", (gssize) (end - payload));
487 dump_bytes (payload, end - payload);
488 }
489
490 return 0;
491 }
492
493 static int
494 parse_set_palette (GstDVDSpu * dvdspu, guint8 type, guint8 * payload,
495 guint16 len)
496 {
497 SpuState *state = &dvdspu->spu_state;
498
499 const gint PGS_PALETTE_ENTRY_SIZE = 5;
500 guint8 *end = payload + len;
501 guint8 palette_id G_GNUC_UNUSED;
502 guint8 palette_version G_GNUC_UNUSED;
503 gint n_entries, i;
504
505 if (len < 2) /* Palette command too short */
506 return 0;
507 palette_id = payload[0];
508 palette_version = payload[1];
509 payload += 2;
510
511 n_entries = (len - 2) / PGS_PALETTE_ENTRY_SIZE;
512
513 PGS_DUMP ("Palette ID %u version %u. %d entries\n",
514 palette_id, palette_version, n_entries);
515 for (i = 0; i < 256; i++)
516 state->pgs.palette[i].A = 0;
517 for (i = 0; i < n_entries; i++) {
518 guint8 n, Y, U, V, A;
519 gint R, G, B;
520 n = payload[0];
521 Y = payload[1];
522 V = payload[2];
523 U = payload[3];
524 A = payload[4];
525
526 #if DUMP_FULL_PALETTE
527 PGS_DUMP ("Entry %3d: Y %3d U %3d V %3d A %3d ", n, Y, U, V, A);
528 #endif
529
530 /* Convert to ARGB */
531 R = (298 * Y + 459 * V - 63514) >> 8;
532 G = (298 * Y - 55 * U - 136 * V + 19681) >> 8;
533 B = (298 * Y + 541 * U - 73988) >> 8;
534
535 R = CLAMP (R, 0, 255);
536 G = CLAMP (G, 0, 255);
537 B = CLAMP (B, 0, 255);
538
539 #if DUMP_FULL_PALETTE
540 PGS_DUMP ("Entry %3d: A %3d R %3d G %3d B %3d\n", n, A, R, G, B);
541 #endif
542
543 /* Premultiply the palette entries by the alpha */
544 state->pgs.palette[n].A = A;
545 state->pgs.palette[n].R = R * A / 255;
546 state->pgs.palette[n].G = G * A / 255;
547 state->pgs.palette[n].B = B * A / 255;
548
549 payload += PGS_PALETTE_ENTRY_SIZE;
550 }
551
552 #if DUMP_FULL_PALETTE
553 if (n_entries > 0 && (i % 2))
554 PGS_DUMP ("\n");
555 #endif
556
557 if (payload != end) {
558 GST_ERROR ("PGS Set Palette: %" G_GSSIZE_FORMAT " bytes not consumed",
559 (gssize) (end - payload));
560 dump_bytes (payload, end - payload);
561 }
562
563 return 0;
564 }
565
566 static int
567 parse_set_window (GstDVDSpu * dvdspu, guint8 type, guint8 * payload,
568 guint16 len)
569 {
570 SpuState *state = &dvdspu->spu_state;
571 guint8 *end = payload + len;
572 guint8 win_count, win_id G_GNUC_UNUSED;
573 gint i;
574
575 if (payload + 1 > end)
576 return 0;
577
578 dump_bytes (payload, len);
579
580 win_count = payload[0];
581 payload++;
582
583 for (i = 0; i < win_count; i++) {
584 if (payload + 9 > end)
585 return 0;
586
587 /* FIXME: Store each window ID separately into an array */
588 win_id = payload[0];
589 state->pgs.win_x = GST_READ_UINT16_BE (payload + 1);
590 state->pgs.win_y = GST_READ_UINT16_BE (payload + 3);
591 state->pgs.win_w = GST_READ_UINT16_BE (payload + 5);
592 state->pgs.win_h = GST_READ_UINT16_BE (payload + 7);
593 payload += 9;
594
595 PGS_DUMP ("Win ID %u x %d y %d w %d h %d\n",
596 win_id, state->pgs.win_x, state->pgs.win_y, state->pgs.win_w,
597 state->pgs.win_h);
598 }
599
600 if (payload != end) {
601 GST_ERROR ("PGS Set Window: %" G_GSSIZE_FORMAT " bytes not consumed",
602 (gssize) (end - payload));
603 dump_bytes (payload, end - payload);
604 }
605
606 return 0;
607 }
608
609 static int
610 parse_set_object_data (GstDVDSpu * dvdspu, guint8 type, guint8 * payload,
611 guint16 len)
612 {
613 SpuPgsState *pgs_state = &dvdspu->spu_state.pgs;
614 PgsCompositionObject *obj;
615 guint8 *end = payload + len;
616 guint16 obj_id;
617 guint8 obj_ver, flags;
618
619 if (payload + 4 > end)
620 return 0;
621
622 obj_id = GST_READ_UINT16_BE (payload);
623 obj_ver = payload[2];
624 flags = payload[3];
625 payload += 4;
626
627 obj = pgs_presentation_segment_find_object (&(pgs_state->pres_seg), obj_id);
628
629 PGS_DUMP ("Object ID %d ver %u flags 0x%02x\n", obj_id, obj_ver, flags);
630
631 if (!obj) {
632 GST_ERROR ("unknown Object ID %d", obj_id);
633 return 0;
634 }
635
636 if (flags & PGS_OBJECT_UPDATE_FLAG_START_RLE) {
637 obj->rle_data_ver = obj_ver;
638
639 if (payload + 3 > end)
640 return 0;
641
642 obj->rle_data_size = GST_READ_UINT24_BE (payload);
643 payload += 3;
644
645 if (end - payload > obj->rle_data_size)
646 return 0;
647
648 PGS_DUMP ("%d bytes of RLE data, of %d bytes total.\n",
649 (int) (end - payload), obj->rle_data_size);
650
651 obj->rle_data = g_realloc (obj->rle_data, obj->rle_data_size);
652 obj->rle_data_used = end - payload;
653 memcpy (obj->rle_data, payload, end - payload);
654 payload = end;
655 } else {
656 PGS_DUMP ("%d bytes of additional RLE data\n", (int) (end - payload));
657 /* Check that the data chunk is for this object version, and fits in the buffer */
658 if (obj->rle_data_ver == obj_ver &&
659 end - payload <= obj->rle_data_size &&
660 obj->rle_data_used <= obj->rle_data_size - (end - payload)) {
661
662 memcpy (obj->rle_data + obj->rle_data_used, payload, end - payload);
663 obj->rle_data_used += end - payload;
664 payload = end;
665 }
666 }
667
668 if (obj->rle_data_size == obj->rle_data_used) {
669 dump_rle_data (dvdspu, obj->rle_data, obj->rle_data_size);
670 if (obj->rle_data_size >= 4) {
671 guint8 *data = obj->rle_data;
672
673 obj->width = GST_READ_UINT16_BE (data);
674 obj->height = GST_READ_UINT16_BE (data + 2);
675 }
676 }
677
678 if (payload != end) {
679 GST_ERROR ("PGS Set Object Data: %" G_GSSIZE_FORMAT " bytes not consumed",
680 (gssize) (end - payload));
681 dump_bytes (payload, end - payload);
682 }
683
684 return 0;
685 }
686
687 static int
688 parse_pgs_packet (GstDVDSpu * dvdspu, guint8 type, guint8 * payload,
689 guint16 len)
690 {
691 SpuPgsState *pgs_state = &dvdspu->spu_state.pgs;
692 int ret = 0;
693
694 if (!pgs_state->in_presentation_segment
695 && type != PGS_COMMAND_PRESENTATION_SEGMENT) {
696 PGS_DUMP ("Expected BEGIN PRESENTATION SEGMENT command. "
697 "Got command type 0x%02x len %u. Skipping\n", type, len);
698 return 0;
699 }
700
701 switch (type) {
702 case PGS_COMMAND_PRESENTATION_SEGMENT:
703 PGS_DUMP ("*******************************************\n"
704 "Begin PRESENTATION_SEGMENT (0x%02x) packet len %u\n", type, len);
705 pgs_state->in_presentation_segment =
706 pgs_state->have_presentation_segment = TRUE;
707 ret = parse_presentation_segment (dvdspu, type, payload, len);
708 break;
709 case PGS_COMMAND_SET_OBJECT_DATA:
710 PGS_DUMP ("*** Set Object Data (0x%02x) packet len %u\n", type, len);
711 ret = parse_set_object_data (dvdspu, type, payload, len);
712 break;
713 case PGS_COMMAND_SET_PALETTE:
714 PGS_DUMP ("*** Set Palette (0x%02x) packet len %u\n", type, len);
715 ret = parse_set_palette (dvdspu, type, payload, len);
716 break;
717 case PGS_COMMAND_SET_WINDOW:
718 PGS_DUMP ("*** Set Window command (0x%02x) packet len %u\n", type, len);
719 ret = parse_set_window (dvdspu, type, payload, len);
720 break;
721 case PGS_COMMAND_INTERACTIVE_SEGMENT:
722 PGS_DUMP ("*** Interactive Segment command(0x%02x) packet len %u\n",
723 type, len);
724 dump_bytes (payload, len);
725 break;
726 case PGS_COMMAND_END_DISPLAY:
727 PGS_DUMP ("*** End Display command (0x%02x) packet len %u\n", type,
728 len);
729 pgs_state->in_presentation_segment = FALSE;
730 break;
731 default:
732 GST_ERROR ("Unknown PGS command: type 0x%02x len %u", type, len);
733 dump_bytes (payload, len);
734 break;
735 }
736 PGS_DUMP ("\n");
737
738 return ret;
739 }
740
741 gint
742 gstspu_exec_pgs_buffer (GstDVDSpu * dvdspu, GstBuffer * buf)
743 {
744 GstMapInfo map;
745 guint8 *pos, *end;
746 guint8 type;
747 guint16 packet_len;
748 gint remaining;
749
750 gst_buffer_map (buf, &map, GST_MAP_READ);
751
752 pos = map.data;
753 end = pos + map.size;
754
755 /* Need at least 3 bytes */
756 if (pos + 3 > end) {
757 PGS_DUMP ("Not enough bytes to be a PGS packet\n");
758 goto error;
759 }
760
761 PGS_DUMP ("Begin dumping command buffer of size %u ts %" GST_TIME_FORMAT "\n",
762 (guint) (end - pos), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
763 do {
764 type = *pos++;
765 packet_len = GST_READ_UINT16_BE (pos);
766 pos += 2;
767
768 if (pos + packet_len > end) {
769 PGS_DUMP ("Invalid packet length %u (only have %u bytes)\n", packet_len,
770 (guint) (end - pos));
771 goto error;
772 }
773
774 if (parse_pgs_packet (dvdspu, type, pos, packet_len))
775 goto error;
776
777 pos += packet_len;
778 } while (pos + 3 <= end);
779
780 PGS_DUMP ("End dumping command buffer with %u bytes remaining\n",
781 (guint) (end - pos));
782 remaining = (gint) (pos - map.data);
783 gst_buffer_unmap (buf, &map);
784 return remaining;
785
786 /* ERRORS */
787 error:
788 {
789 gst_buffer_unmap (buf, &map);
790 return -1;
791 }
792 }
793
794 void
795 gstspu_pgs_handle_new_buf (GstDVDSpu * dvdspu, GstClockTime event_ts,
796 GstBuffer * buf)
797 {
798 SpuState *state = &dvdspu->spu_state;
799
800 state->next_ts = event_ts;
801 state->pgs.pending_cmd = buf;
802 }
803
804 gboolean
805 gstspu_pgs_execute_event (GstDVDSpu * dvdspu)
806 {
807 SpuState *state = &dvdspu->spu_state;
808
809 if (state->pgs.pending_cmd) {
810 gstspu_exec_pgs_buffer (dvdspu, state->pgs.pending_cmd);
811 gst_buffer_unref (state->pgs.pending_cmd);
812 state->pgs.pending_cmd = NULL;
813 }
814
815 state->next_ts = GST_CLOCK_TIME_NONE;
816
817 state->flags &= ~SPU_STATE_DISPLAY;
818 if (state->pgs.have_presentation_segment) {
819 if (state->pgs.pres_seg.objects && state->pgs.pres_seg.objects->len > 0)
820 state->flags |= SPU_STATE_DISPLAY;
821 }
822 return FALSE;
823 }
824
825 void
826 gstspu_pgs_render (GstDVDSpu * dvdspu, GstVideoFrame * window)
827 {
828 SpuState *state = &dvdspu->spu_state;
829 PgsPresentationSegment *ps = &state->pgs.pres_seg;
830 guint i;
831
832 if (ps->objects == NULL)
833 return;
834
835 for (i = 0; i < ps->objects->len; i++) {
836 PgsCompositionObject *cur =
837 &g_array_index (ps->objects, PgsCompositionObject, i);
838 pgs_composition_object_render (cur, state, window);
839 }
840 }
841
842 gboolean
843 gstspu_pgs_handle_dvd_event (GstDVDSpu * dvdspu, GstEvent * event)
844 {
845 gst_event_unref (event);
846 return FALSE;
847 }
848
849 void
850 gstspu_pgs_get_render_geometry (GstDVDSpu * dvdspu,
851 gint * display_width, gint * display_height, gint * count)
852 {
853 PgsPresentationSegment *ps = &dvdspu->spu_state.pgs.pres_seg;
854
855 if (count)
856 *count = ps->objects->len;
857
858 if (display_width)
859 *display_width = ps->vid_w;
860
861 if (display_height)
862 *display_height = ps->vid_h;
863 }
864
865 void
866 gstspu_pgs_get_render_geometry_n (GstDVDSpu * dvdspu, gint index,
867 GstVideoRectangle * window_rect)
868 {
869 PgsPresentationSegment *ps = &dvdspu->spu_state.pgs.pres_seg;
870
871 if (window_rect && index < ps->objects->len) {
872 PgsCompositionObject *cur =
873 &g_array_index (ps->objects, PgsCompositionObject, index);
874
875 window_rect->x = cur->x;
876 window_rect->y = cur->y;
877 window_rect->w = cur->width;
878 window_rect->h = cur->height;
879 }
880 }
881
882 void
883 gstspu_pgs_flush (GstDVDSpu * dvdspu)
884 {
885 SpuPgsState *pgs_state = &dvdspu->spu_state.pgs;
886
887 if (pgs_state->pending_cmd) {
888 gst_buffer_unref (pgs_state->pending_cmd);
889 pgs_state->pending_cmd = NULL;
890 }
891
892 pgs_state->have_presentation_segment = FALSE;
893 pgs_state->in_presentation_segment = FALSE;
894 pgs_presentation_segment_set_object_count (&pgs_state->pres_seg, 0);
895
896 pgs_state->win_x = pgs_state->win_y = pgs_state->win_w = pgs_state->win_h = 0;
897 }