]> git.sesse.net Git - vlc/blob - modules/misc/dummy/decoder.c
d4eb09f151d01da65930f27aec8c9c5e9d30d7da
[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_FCNTL_H
44 #   include <fcntl.h>
45 #endif
46
47 #include <limits.h> /* PATH_MAX */
48
49
50 #include "dummy.h"
51
52 /*****************************************************************************
53  * decoder_sys_t : theora decoder descriptor
54  *****************************************************************************/
55 struct decoder_sys_t
56 {
57     int i_fd;
58 };
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block );
64
65 /*****************************************************************************
66  * OpenDecoder: Open the decoder
67  *****************************************************************************/
68 static int OpenDecoderCommon( vlc_object_t *p_this, bool b_force_dump )
69 {
70     decoder_t *p_dec = (decoder_t*)p_this;
71     decoder_sys_t *p_sys;
72     char psz_file[ PATH_MAX ];
73
74     /* Allocate the memory needed to store the decoder's structure */
75     if( ( p_dec->p_sys = p_sys =
76           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
77     {
78         return VLC_ENOMEM;
79     }
80
81     snprintf( psz_file, sizeof( psz_file), "stream.%p", p_dec );
82
83 #ifndef UNDER_CE
84     if( !b_force_dump )
85     {
86         b_force_dump = var_CreateGetBool( p_dec, "dummy-save-es" );
87     }
88     if( b_force_dump )
89     {
90         p_sys->i_fd = utf8_open( psz_file, O_WRONLY | O_CREAT | O_TRUNC, 00644 );
91
92         if( p_sys->i_fd == -1 )
93         {
94             msg_Err( p_dec, "cannot create `%s'", psz_file );
95             free( p_sys );
96             return VLC_EGENERIC;
97         }
98
99         msg_Dbg( p_dec, "dumping stream to file `%s'", psz_file );
100     }
101     else
102 #endif
103     {
104         p_sys->i_fd = -1;
105     }
106
107     /* Set callbacks */
108     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
109         DecodeBlock;
110     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
111         DecodeBlock;
112     p_dec->pf_decode_sub = (subpicture_t *(*)(decoder_t *, block_t **))
113         DecodeBlock;
114
115     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
116
117     return VLC_SUCCESS;
118 }
119
120 int OpenDecoder( vlc_object_t *p_this )
121 {
122     return OpenDecoderCommon( p_this, false );
123 }
124 int  OpenDecoderDump( vlc_object_t *p_this )
125 {
126     return OpenDecoderCommon( p_this, true );
127 }
128
129 /****************************************************************************
130  * RunDecoder: the whole thing
131  ****************************************************************************
132  * This function must be fed with ogg packets.
133  ****************************************************************************/
134 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
135 {
136     decoder_sys_t *p_sys = p_dec->p_sys;
137     block_t *p_block;
138
139     if( !pp_block || !*pp_block ) return NULL;
140     p_block = *pp_block;
141
142     if( p_sys->i_fd >= 0 &&
143         p_block->i_buffer > 0 &&
144         (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) == 0 )
145     {
146 #ifndef UNDER_CE
147         write( p_sys->i_fd, p_block->p_buffer, p_block->i_buffer );
148 #endif
149
150         msg_Dbg( p_dec, "dumped %zu bytes", p_block->i_buffer );
151     }
152
153     block_Release( p_block );
154     return NULL;
155 }
156
157 /*****************************************************************************
158  * CloseDecoder: decoder destruction
159  *****************************************************************************/
160 void CloseDecoder ( vlc_object_t *p_this )
161 {
162     decoder_t *p_dec = (decoder_t *)p_this;
163     decoder_sys_t *p_sys = p_dec->p_sys;
164
165 #ifndef UNDER_CE
166     if( p_sys->i_fd >= 0 )
167         close( p_sys->i_fd );
168 #endif
169
170     free( p_sys );
171 }
172