]> git.sesse.net Git - vlc/blob - modules/misc/dummy/decoder.c
5098ac60a2ae4bb85d4d5e9a38c9d11a35efd15c
[vlc] / modules / misc / dummy / decoder.c
1 /*****************************************************************************
2  * decoder.c: dummy decoder plugin for vlc.
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_codec.h>
33 #include <vlc_charset.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h> /* write(), close() */
37 #elif defined( WIN32 ) && !defined( UNDER_CE )
38 #   include <io.h>
39 #endif
40 #ifdef HAVE_SYS_TYPES_H
41 #   include <sys/types.h> /* open() */
42 #endif
43 #ifdef HAVE_SYS_STAT_H
44 #   include <sys/stat.h>
45 #endif
46 #ifdef HAVE_FCNTL_H
47 #   include <fcntl.h>
48 #endif
49
50 #include <limits.h> /* PATH_MAX */
51
52
53 #include "dummy.h"
54
55 /*****************************************************************************
56  * decoder_sys_t : theora decoder descriptor
57  *****************************************************************************/
58 struct decoder_sys_t
59 {
60     int i_fd;
61 };
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block );
67
68 /*****************************************************************************
69  * OpenDecoder: Open the decoder
70  *****************************************************************************/
71 static int OpenDecoderCommon( vlc_object_t *p_this, bool b_force_dump )
72 {
73     decoder_t *p_dec = (decoder_t*)p_this;
74     decoder_sys_t *p_sys;
75     char psz_file[ PATH_MAX ];
76     vlc_value_t val;
77
78     /* Allocate the memory needed to store the decoder's structure */
79     if( ( p_dec->p_sys = p_sys =
80           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
81     {
82         return VLC_ENOMEM;
83     }
84
85     snprintf( psz_file, sizeof( psz_file), "stream.%p", p_dec );
86
87 #ifndef UNDER_CE
88     if( !b_force_dump )
89     {
90         var_Create( p_dec, "dummy-save-es", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
91         var_Get( p_dec, "dummy-save-es", &val );
92         b_force_dump = val.b_bool;
93     }
94     if( b_force_dump )
95     {
96         p_sys->i_fd = utf8_open( psz_file, O_WRONLY | O_CREAT | O_TRUNC, 00644 );
97
98         if( p_sys->i_fd == -1 )
99         {
100             msg_Err( p_dec, "cannot create `%s'", psz_file );
101             free( p_sys );
102             return VLC_EGENERIC;
103         }
104
105         msg_Dbg( p_dec, "dumping stream to file `%s'", psz_file );
106     }
107     else
108 #endif
109     {
110         p_sys->i_fd = -1;
111     }
112
113     /* Set callbacks */
114     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
115         DecodeBlock;
116     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
117         DecodeBlock;
118     p_dec->pf_decode_sub = (subpicture_t *(*)(decoder_t *, block_t **))
119         DecodeBlock;
120
121     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
122
123     return VLC_SUCCESS;
124 }
125
126 int OpenDecoder( vlc_object_t *p_this )
127 {
128     return OpenDecoderCommon( p_this, false );
129 }
130 int  OpenDecoderDump( vlc_object_t *p_this )
131 {
132     return OpenDecoderCommon( p_this, true );
133 }
134
135 /****************************************************************************
136  * RunDecoder: the whole thing
137  ****************************************************************************
138  * This function must be fed with ogg packets.
139  ****************************************************************************/
140 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
141 {
142     decoder_sys_t *p_sys = p_dec->p_sys;
143     block_t *p_block;
144
145     if( !pp_block || !*pp_block ) return NULL;
146     p_block = *pp_block;
147
148     if( p_sys->i_fd >= 0 &&
149         p_block->i_buffer > 0 &&
150         (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) == 0 )
151     {
152 #ifndef UNDER_CE
153         write( p_sys->i_fd, p_block->p_buffer, p_block->i_buffer );
154 #endif
155
156         msg_Dbg( p_dec, "dumped %zu bytes", p_block->i_buffer );
157     }
158
159     block_Release( p_block );
160     return NULL;
161 }
162
163 /*****************************************************************************
164  * CloseDecoder: decoder destruction
165  *****************************************************************************/
166 void CloseDecoder ( vlc_object_t *p_this )
167 {
168     decoder_t *p_dec = (decoder_t *)p_this;
169     decoder_sys_t *p_sys = p_dec->p_sys;
170
171 #ifndef UNDER_CE
172     if( p_sys->i_fd >= 0 )
173         close( p_sys->i_fd );
174 #endif
175
176     free( p_sys );
177 }
178