]> git.sesse.net Git - vlc/blob - modules/codec/subtitles/t140.c
01b31629c641c5a282ac73677497abb68afc3d62
[vlc] / modules / codec / subtitles / t140.c
1 /*****************************************************************************
2  * t140.c : trivial T.140 text encoder
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_vout.h>
29 #include <vlc_codec.h>
30 #include <vlc_sout.h>
31
32 static int  Open ( vlc_object_t * );
33 static void Close( vlc_object_t * );
34
35 vlc_module_begin();
36     add_submodule();
37     set_description( N_("T.140 text encoder") );
38     set_capability( "encoder", 100 );
39     set_callbacks( Open, Close );
40 vlc_module_end();
41
42
43 static block_t *Encode ( encoder_t *, subpicture_t * );
44
45
46 static int Open( vlc_object_t *p_this )
47 {
48     encoder_t *p_enc = (encoder_t *)p_this;
49
50     switch( p_enc->fmt_out.i_codec )
51     {
52         case VLC_FOURCC('s','u','b','t'):
53             if( ( p_enc->fmt_out.subs.psz_encoding != NULL )
54              && strcasecmp( p_enc->fmt_out.subs.psz_encoding, "utf8" )
55              && strcasecmp( p_enc->fmt_out.subs.psz_encoding, "UTF-8" ) )
56             {
57                 msg_Err( p_this, "Only UTF-8 encoding supported" );
58                 return VLC_EGENERIC;
59             }
60         case VLC_FOURCC('t','1','4','0'):
61             break;
62
63         default:
64             if( !p_enc->b_force )
65                 return VLC_EGENERIC;
66
67             p_enc->fmt_out.i_codec = VLC_FOURCC('t','1','4','0');
68     }
69
70     p_enc->p_sys = NULL;
71
72     p_enc->pf_encode_sub = Encode;
73     return VLC_SUCCESS;
74 }
75
76
77 static void Close( vlc_object_t *p_this )
78 {
79     (void)p_this;
80 }
81
82
83 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_spu )
84 {
85     subpicture_region_t *p_region;
86     block_t *p_block;
87     size_t len;
88
89     if( p_spu == NULL )
90         return NULL;
91
92     p_region = p_spu->p_region;
93     if( ( p_region == NULL )
94      || ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') )
95      || ( p_region->psz_text == NULL ) )
96         return NULL;
97
98     /* This should already be UTF-8 encoded, so not much effort... */
99     len = strlen( p_region->psz_text );
100     p_block = block_New( p_enc, len );
101     memcpy( p_block->p_buffer, p_region->psz_text, len );
102
103     return p_block;
104 }