1 /*****************************************************************************
2 * stl.c: EBU STL decoder
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_memory.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open (vlc_object_t
*);
41 static void Close(vlc_object_t
*);
44 set_description(N_("EBU STL subtitles decoder"))
45 set_category(CAT_INPUT
)
46 set_subcategory(SUBCAT_INPUT_SCODEC
)
47 set_capability("decoder", 10)
48 set_callbacks(Open
, Close
)
51 /*****************************************************************************
52 * Local definitions/prototypes
53 *****************************************************************************/
54 struct decoder_sys_t
{
58 static char *ParseText(uint8_t *data
, int size
)
60 char *text
= strdup("");
63 for (int i
= 0; i
< size
; i
++) {
64 uint8_t code
= data
[i
];
71 if (code
>= 0x20 && code
<= 0x7f)
72 snprintf(tmp
, sizeof(tmp
), "%c", code
);
74 else if (code
== 0x80)
75 snprintf(tmp
, sizeof(tmp
), "<i>");
76 else if (code
== 0x81)
77 snprintf(tmp
, sizeof(tmp
), "</i>");
78 else if (code
== 0x82)
79 snprintf(tmp
, sizeof(tmp
), "<u>");
80 else if (code
== 0x83)
81 snprintf(tmp
, sizeof(tmp
), "</u>");
83 else if (code
== 0x8a)
84 snprintf(tmp
, sizeof(tmp
), "\n");
86 fprintf(stderr
, "--> %2.2x\n", code
);
92 size_t t_size
= strlen(t
);
93 text
= realloc_or_free(text
, t_size
+ text_size
+ 1);
96 memcpy(&text
[text_size
], t
, t_size
);
98 text
[text_size
] = '\0';
103 static subpicture_t
*Decode(decoder_t
*dec
, block_t
**block
)
105 if (block
== NULL
|| *block
== NULL
)
108 subpicture_t
*sub
= NULL
;
110 block_t
*b
= *block
; *block
= NULL
;
111 if (b
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
))
113 if (b
->i_buffer
< 128)
116 int payload_size
= (b
->i_buffer
/ 128) * 112;
117 uint8_t *payload
= malloc(payload_size
);
120 for (int i
= 0; i
< b
->i_buffer
/ 128; i
++)
121 memcpy(&payload
[112 * i
], &b
->p_buffer
[128 * i
+ 16], 112);
123 sub
= decoder_NewSubpicture(dec
, NULL
);
128 sub
->i_start
= b
->i_pts
;
129 sub
->i_stop
= b
->i_pts
+ b
->i_length
;
130 sub
->b_ephemer
= b
->i_length
== 0;
131 sub
->b_absolute
= false;
132 //sub->i_original_picture_width = 0;
133 //sub->i_original_picture_height = 0;
136 video_format_Init(&fmt
, VLC_CODEC_TEXT
);
137 sub
->p_region
= subpicture_region_New(&fmt
);
138 video_format_Clean(&fmt
);
141 sub
->p_region
->psz_text
= ParseText(payload
, payload_size
);
142 sub
->p_region
->psz_html
= NULL
;
152 static int Open(vlc_object_t
*object
)
154 decoder_t
*dec
= (decoder_t
*)object
;
156 if (dec
->fmt_in
.i_codec
!= VLC_CODEC_EBU_STL
)
159 decoder_sys_t
*sys
= malloc(sizeof(*sys
));
162 dec
->pf_decode_sub
= Decode
;
163 dec
->fmt_out
.i_cat
= SPU_ES
;
164 dec
->fmt_out
.i_codec
= 0;
168 static void Close(vlc_object_t
*object
)
170 decoder_t
*dec
= (decoder_t
*)object
;
171 decoder_sys_t
*sys
= dec
->p_sys
;