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