Revert "codec: don't drop discontinue blocks"
[vlc.git] / modules / codec / stl.c
blobe2f8a20c9e83a9e72d6c4c6971488a0cdb717f57
1 /*****************************************************************************
2 * stl.c: EBU STL decoder
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <assert.h>
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>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 static int Open (vlc_object_t *);
42 static void Close(vlc_object_t *);
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()
52 /*****************************************************************************
53 * Local definitions/prototypes
54 *****************************************************************************/
55 #define GSI_BLOCK_SIZE 1024
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;
65 typedef struct {
66 cct_number_value_t value;
67 const char *str;
68 } cct_number_t;
70 struct decoder_sys_t {
71 cct_number_value_t cct;
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"} };
81 static char *ParseText(const uint8_t *data, size_t size, const char *charset)
83 char *text = malloc(size);
84 if (text == NULL)
85 return NULL;
87 size_t text_size = 0;
89 for (size_t i = 0; i < size; i++) {
90 uint8_t code = data[i];
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';
103 char *u8 = FromCharset(charset, text, text_size);
104 free(text);
105 return u8;
108 static subpicture_t *Decode(decoder_t *dec, block_t **block)
110 if (block == NULL || *block == NULL)
111 return NULL;
113 subpicture_t *sub = NULL;
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;
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);
128 sub = decoder_NewSubpicture(dec, NULL);
129 if (!sub) {
130 free(payload);
131 goto exit;
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;
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);
145 if (sub->p_region) {
146 sub->p_region->p_text = text_segment_New( ParseText(payload,
147 payload_size,
148 cct_nums[dec->p_sys->cct - CCT_BEGIN].str) );
149 sub->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
152 free(payload);
154 exit:
155 block_Release(b);
156 return sub;
159 static int ExtractCCT(const decoder_t *dec, cct_number_value_t *cct_number)
161 uint8_t *header = dec->fmt_in.p_extra;
162 if (!header) {
163 msg_Err(dec, "NULL EBU header (GSI block)\n");
164 return VLC_EGENERIC;
167 if (GSI_BLOCK_SIZE != dec->fmt_in.i_extra) {
168 msg_Err(dec, "EBU header is not in expected size (%d)\n", dec->fmt_in.i_extra);
169 return VLC_EGENERIC;
172 int cct = (header[12] << 8) | header[13];
173 if (CCT_BEGIN > cct || CCT_END < cct) {
174 msg_Err(dec, "EBU header contains illegal CCT (0x%x)\n", cct);
175 return VLC_EGENERIC;
178 *cct_number = cct;
180 return VLC_SUCCESS;
183 static int Open(vlc_object_t *object)
185 decoder_t *dec = (decoder_t*)object;
187 if (dec->fmt_in.i_codec != VLC_CODEC_EBU_STL)
188 return VLC_EGENERIC;
190 cct_number_value_t cct;
191 int rc = ExtractCCT(dec, &cct);
192 if (VLC_SUCCESS != rc)
193 return rc;
195 msg_Dbg(dec, "CCT=0x%x", cct);
197 decoder_sys_t *sys = malloc(sizeof(*sys));
198 if (!sys)
199 return VLC_ENOMEM;
201 sys->cct = cct;
203 dec->p_sys = sys;
204 dec->pf_decode_sub = Decode;
205 dec->fmt_out.i_cat = SPU_ES;
206 dec->fmt_out.i_codec = 0;
207 return VLC_SUCCESS;
210 static void Close(vlc_object_t *object)
212 decoder_t *dec = (decoder_t*)object;
213 decoder_sys_t *sys = dec->p_sys;
215 free(sys);