]> git.sesse.net Git - vlc/blob - modules/codec/stl.c
mediacodec: fix warning
[vlc] / modules / codec / stl.c
1 /*****************************************************************************
2  * stl.c: EBU STL decoder
3  *****************************************************************************
4  * Copyright (C) 2010 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <assert.h>
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_memory.h>
36 #include <vlc_charset.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open (vlc_object_t *);
42 static void Close(vlc_object_t *);
43
44 vlc_module_begin()
45     set_description(N_("EBU STL subtitles decoder"))
46     set_category(CAT_INPUT)
47     set_subcategory(SUBCAT_INPUT_SCODEC)
48     set_capability("decoder", 10)
49     set_callbacks(Open, Close)
50 vlc_module_end()
51
52 /*****************************************************************************
53  * Local definitions/prototypes
54  *****************************************************************************/
55 #define GSI_BLOCK_SIZE 1024
56
57 typedef enum {
58     CCT_ISO_6937_2 = 0x3030, CCT_BEGIN = CCT_ISO_6937_2,
59     CCT_ISO_8859_5 = 0x3031,
60     CCT_ISO_8859_6 = 0x3032,
61     CCT_ISO_8859_7 = 0x3033,
62     CCT_ISO_8859_8 = 0x3034, CCT_END = CCT_ISO_8859_8
63 } cct_number_value_t;
64
65 typedef struct {
66     cct_number_value_t value;
67     const char *str;
68 } cct_number_t;
69
70 struct decoder_sys_t {
71     cct_number_value_t cct;
72 };
73
74 static cct_number_t cct_nums[] = { {CCT_ISO_6937_2, "ISO_6937-2"},
75                                    {CCT_ISO_8859_5, "ISO_8859-5"},
76                                    {CCT_ISO_8859_6, "ISO_8859-6"},
77                                    {CCT_ISO_8859_7, "ISO_8859-7"},
78                                    {CCT_ISO_8859_8, "ISO_8859-8"} };
79
80
81 static char *ParseText(const uint8_t *data, size_t size, const char *charset)
82 {
83     char *text = malloc(size);
84     if (text == NULL)
85         return NULL;
86
87     size_t text_size = 0;
88
89     for (size_t i = 0; i < size; i++) {
90         uint8_t code = data[i];
91
92         if (code == 0x8f)
93             break;
94         if (code == 0x7f)
95             continue;
96         /* TODO: italics begin/end 0x80/0x81, underline being/end 0x82/0x83 */
97         if (code & 0x60)
98             text[text_size++] = code;
99         if (code == 0x8a)
100             text[text_size++] = '\n';
101     }
102
103     char *u8 = FromCharset(charset, text, text_size);
104     free(text);
105     return u8;
106 }
107
108 static subpicture_t *Decode(decoder_t *dec, block_t **block)
109 {
110     if (block == NULL || *block == NULL)
111         return NULL;
112
113     subpicture_t *sub = NULL;
114
115     block_t *b = *block; *block = NULL;
116     if (b->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
117         goto exit;
118     if (b->i_buffer < 128)
119         goto exit;
120
121     int     payload_size = (b->i_buffer / 128) * 112;
122     uint8_t *payload = malloc(payload_size);
123     if (!payload)
124         goto exit;
125     for (unsigned i = 0; i < b->i_buffer / 128; i++)
126         memcpy(&payload[112 * i], &b->p_buffer[128 * i + 16], 112);
127
128     sub = decoder_NewSubpicture(dec, NULL);
129     if (!sub) {
130         free(payload);
131         goto exit;
132     }
133     sub->i_start    = b->i_pts;
134     sub->i_stop     = b->i_pts + b->i_length;
135     sub->b_ephemer  = b->i_length == 0;
136     sub->b_absolute = false;
137     //sub->i_original_picture_width  = 0;
138     //sub->i_original_picture_height = 0;
139
140     video_format_t fmt;
141     video_format_Init(&fmt, VLC_CODEC_TEXT);
142     sub->p_region = subpicture_region_New(&fmt);
143     video_format_Clean(&fmt);
144
145     if (sub->p_region) {
146         sub->p_region->psz_text = ParseText(payload,
147                                             payload_size,
148                                             cct_nums[dec->p_sys->cct - CCT_BEGIN].str);
149         sub->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
150         sub->p_region->psz_html = NULL;
151     }
152
153     free(payload);
154
155 exit:
156     block_Release(b);
157     return sub;
158 }
159
160 static int ExtractCCT(const decoder_t *dec, cct_number_value_t *cct_number)
161 {
162     uint8_t *header = dec->fmt_in.p_extra;
163     if (!header) {
164         msg_Err(dec, "NULL EBU header (GSI block)\n");
165         return VLC_EGENERIC;
166     }
167
168     if (GSI_BLOCK_SIZE != dec->fmt_in.i_extra) {
169         msg_Err(dec, "EBU header is not in expected size (%d)\n", dec->fmt_in.i_extra);
170         return VLC_EGENERIC;
171     }
172
173     int cct = (header[12] << 8) | header[13];
174     if (CCT_BEGIN > cct || CCT_END < cct) {
175         msg_Err(dec, "EBU header contains illegal CCT (0x%x)\n", cct);
176         return VLC_EGENERIC;
177     }
178
179     *cct_number = cct;
180
181     return VLC_SUCCESS;
182 }
183
184 static int Open(vlc_object_t *object)
185 {
186     decoder_t *dec = (decoder_t*)object;
187
188     if (dec->fmt_in.i_codec != VLC_CODEC_EBU_STL)
189         return VLC_EGENERIC;
190
191     cct_number_value_t cct;
192     int rc = ExtractCCT(dec, &cct);
193     if (VLC_SUCCESS != rc)
194         return rc;
195
196     msg_Dbg(dec, "CCT=0x%x", cct);
197
198     decoder_sys_t *sys = malloc(sizeof(*sys));
199     if (!sys)
200         return VLC_ENOMEM;
201
202     sys->cct = cct;
203
204     dec->p_sys = sys;
205     dec->pf_decode_sub = Decode;
206     dec->fmt_out.i_cat = SPU_ES;
207     dec->fmt_out.i_codec = 0;
208     return VLC_SUCCESS;
209 }
210
211 static void Close(vlc_object_t *object)
212 {
213     decoder_t *dec = (decoder_t*)object;
214     decoder_sys_t *sys = dec->p_sys;
215
216     free(sys);
217 }