Coverage Report

Created: 2025-11-14 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/cdg.c
Line
Count
Source
1
/*****************************************************************************
2
 * cdg.c : cdg file demux module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2007 Laurent Aimar
5
 *
6
 * Authors: Laurent Aimar <fenrir # via.ecp.fr>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
/*****************************************************************************
24
 * Preamble
25
 *****************************************************************************/
26
27
#ifdef HAVE_CONFIG_H
28
# include "config.h"
29
#endif
30
31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
33
#include <vlc_demux.h>
34
35
/*****************************************************************************
36
 * Module descriptor
37
 *****************************************************************************/
38
static int  Open ( vlc_object_t * );
39
40
108
vlc_module_begin ()
41
54
    set_description( N_("CDG demuxer") )
42
54
    set_subcategory( SUBCAT_INPUT_DEMUX )
43
54
    set_capability( "demux", 6 )
44
54
    set_callback( Open )
45
54
    add_shortcut( "cdg", "subtitle" )
46
54
vlc_module_end ()
47
48
/*****************************************************************************
49
 * Local prototypes
50
 *****************************************************************************/
51
static int Demux  ( demux_t * );
52
static int Control( demux_t *, int i_query, va_list args );
53
54
typedef struct
55
{
56
    es_format_t     fmt;
57
    es_out_id_t     *p_es;
58
59
    date_t          pts;
60
} demux_sys_t;
61
62
0
#define CDG_FRAME_SIZE (96)
63
0
#define CDG_FRAME_RATE (75)
64
0
#define CDG_FRAME_DELTA (CLOCK_FREQ / CDG_FRAME_RATE)
65
66
/*****************************************************************************
67
 * Open: check file and initializes structures
68
 *****************************************************************************/
69
static int Open( vlc_object_t * p_this )
70
6.81k
{
71
6.81k
    demux_t     *p_demux = (demux_t*)p_this;
72
73
    /* Identify cdg file by extension, as there is no simple way to
74
     * detect it */
75
6.81k
    if( !demux_IsPathExtension( p_demux, ".cdg" ) && !demux_IsForced( p_demux, "cdg" ) )
76
6.81k
        return VLC_EGENERIC;
77
78
    /* CDG file size has to be multiple of CDG_FRAME_SIZE (it works even
79
     * if size is unknown ie 0) */
80
//    if( (stream_Size( p_demux->s ) % CDG_FRAME_SIZE) != 0 )
81
//    {
82
//        msg_Err( p_demux, "Reject CDG file based on its size" );
83
//        return VLC_EGENERIC;
84
//    }
85
86
0
    demux_sys_t *p_sys = vlc_obj_malloc( p_this, sizeof (*p_sys) );
87
0
    if( unlikely(p_sys == NULL) )
88
0
        return VLC_ENOMEM;
89
90
0
    es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_CODEC_CDG );
91
0
    p_sys->fmt.video.i_width  = 300-2*6;
92
0
    p_sys->fmt.video.i_height = 216-2*12 ;
93
0
    p_sys->fmt.video.i_visible_width = p_sys->fmt.video.i_width;
94
0
    p_sys->fmt.video.i_visible_height = p_sys->fmt.video.i_height;
95
96
0
    p_sys->fmt.i_id = 0;
97
0
    p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
98
0
    if( unlikely(p_sys->p_es == NULL) )
99
0
        return VLC_ENOMEM;
100
101
    /* There is CDG_FRAME_RATE frames per second */
102
0
    date_Init( &p_sys->pts, CDG_FRAME_RATE, 1 );
103
0
    date_Set( &p_sys->pts, VLC_TICK_0 );
104
105
0
    p_demux->pf_demux   = Demux;
106
0
    p_demux->pf_control = Control;
107
0
    p_demux->p_sys      = p_sys;
108
0
    return VLC_SUCCESS;
109
0
}
110
111
static vlc_tick_t PosToDate( demux_t *p_demux )
112
0
{
113
0
    return vlc_stream_Tell( p_demux->s ) / CDG_FRAME_SIZE * CDG_FRAME_DELTA;
114
0
}
115
116
/*****************************************************************************
117
 * Demux: read packet and send them to decoders
118
 *****************************************************************************
119
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
120
 *****************************************************************************/
121
static int Demux( demux_t *p_demux )
122
0
{
123
0
    demux_sys_t *p_sys = p_demux->p_sys;
124
0
    block_t     *p_block;
125
0
    vlc_tick_t  i_date;
126
127
0
    p_block = vlc_stream_Block( p_demux->s, CDG_FRAME_SIZE );
128
0
    if( p_block == NULL )
129
0
    {
130
0
        msg_Dbg( p_demux, "cannot read data, eof" );
131
0
        return VLC_DEMUXER_EOF;
132
0
    }
133
134
0
    i_date = PosToDate( p_demux );
135
0
    if( i_date >= date_Get( &p_sys->pts ) + CDG_FRAME_DELTA )
136
0
    {
137
0
        p_block->i_dts = p_block->i_pts = VLC_TICK_0 + i_date;
138
0
        date_Set( &p_sys->pts, VLC_TICK_0 + i_date );
139
0
    }
140
0
    else
141
0
    {
142
0
        p_block->i_dts = VLC_TICK_0 + i_date;
143
0
        p_block->i_pts = date_Get( &p_sys->pts );
144
0
    }
145
146
0
    es_out_SetPCR( p_demux->out, p_block->i_pts );
147
0
    es_out_Send( p_demux->out, p_sys->p_es, p_block );
148
0
    return VLC_DEMUXER_SUCCESS;
149
0
}
150
151
/*****************************************************************************
152
 * Control:
153
 *****************************************************************************/
154
static int Control( demux_t *p_demux, int i_query, va_list args )
155
0
{
156
0
    demux_sys_t *p_sys = p_demux->p_sys;
157
0
    uint64_t i_old_offset = vlc_stream_Tell( p_demux->s );
158
0
    int i_ret = demux_vaControlHelper( p_demux->s, 0, -1,
159
0
                                       8*CDG_FRAME_SIZE*CDG_FRAME_RATE, CDG_FRAME_SIZE,
160
0
                                       i_query, args );
161
0
    if( !i_ret && ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
162
0
    {
163
0
        date_Set( &p_sys->pts, PosToDate( p_demux ) );
164
0
        if ( i_old_offset > vlc_stream_Tell( p_demux->s ) )
165
0
            i_ret = vlc_stream_Seek( p_demux->s, 0 );
166
0
        else
167
0
            i_ret = vlc_stream_Seek( p_demux->s, i_old_offset );
168
0
    }
169
170
0
    return i_ret;
171
0
}
172