Added STL demuxer/decoder.
[vlc/solaris.git] / modules / codec / stl.c
blob51138546ed14efa316ba8fdbed5329c0fd13b57a
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
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 /*****************************************************************************
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>
37 /*****************************************************************************
38 * Module descriptor
39 *****************************************************************************/
40 static int Open (vlc_object_t *);
41 static void Close(vlc_object_t *);
43 vlc_module_begin()
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)
49 vlc_module_end()
51 /*****************************************************************************
52 * Local definitions/prototypes
53 *****************************************************************************/
54 struct decoder_sys_t {
55 int dummy;
58 static char *ParseText(uint8_t *data, int size)
60 char *text = strdup("");
61 int text_size = 0;
63 for (int i = 0; i < size; i++) {
64 uint8_t code = data[i];
66 if (code == 0x8f)
67 break;
69 char tmp[16] = "";
70 char *t = tmp;
71 if (code >= 0x20 && code <= 0x7f)
72 snprintf(tmp, sizeof(tmp), "%c", code);
73 #if 0
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>");
82 #endif
83 else if (code == 0x8a)
84 snprintf(tmp, sizeof(tmp), "\n");
85 else {
86 fprintf(stderr, "--> %2.2x\n", code);
87 t = NULL;
90 if (!t)
91 continue;
92 size_t t_size = strlen(t);
93 text = realloc_or_free(text, t_size + text_size + 1);
94 if (!text)
95 continue;
96 memcpy(&text[text_size], t, t_size);
97 text_size += t_size;
98 text[text_size] = '\0';
100 return text;
103 static subpicture_t *Decode(decoder_t *dec, block_t **block)
105 if (block == NULL || *block == NULL)
106 return NULL;
108 subpicture_t *sub = NULL;
110 block_t *b = *block; *block = NULL;
111 if (b->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
112 goto exit;
113 if (b->i_buffer < 128)
114 goto exit;
116 int payload_size = (b->i_buffer / 128) * 112;
117 uint8_t *payload = malloc(payload_size);
118 if (!payload)
119 goto exit;
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);
124 if (!sub) {
125 free(payload);
126 goto exit;
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;
135 video_format_t fmt;
136 video_format_Init(&fmt, VLC_CODEC_TEXT);
137 sub->p_region = subpicture_region_New(&fmt);
138 video_format_Clean(&fmt);
140 if (sub->p_region) {
141 sub->p_region->psz_text = ParseText(payload, payload_size);
142 sub->p_region->psz_html = NULL;
145 free(payload);
147 exit:
148 block_Release(b);
149 return sub;
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)
157 return VLC_EGENERIC;
159 decoder_sys_t *sys = malloc(sizeof(*sys));
161 dec->p_sys = sys;
162 dec->pf_decode_sub = Decode;
163 dec->fmt_out.i_cat = SPU_ES;
164 dec->fmt_out.i_codec = 0;
165 return VLC_SUCCESS;
168 static void Close(vlc_object_t *object)
170 decoder_t *dec = (decoder_t*)object;
171 decoder_sys_t *sys = dec->p_sys;
173 free(sys);