]> git.sesse.net Git - vlc/blob - modules/misc/dummy/decoder.c
8f31c72829f7e2ae73e7ef8c2603fa17015cc53e
[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 #include <vlc/vlc.h>
28 #include <vlc_codec.h>
29
30 #ifdef HAVE_UNISTD_H
31 #   include <unistd.h> /* write(), close() */
32 #elif defined( WIN32 ) && !defined( UNDER_CE )
33 #   include <io.h>
34 #endif
35 #ifdef HAVE_SYS_TYPES_H
36 #   include <sys/types.h> /* open() */
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #   include <fcntl.h>
43 #endif
44
45 #ifdef HAVE_LIMITS_H
46 #   include <limits.h> /* PATH_MAX */
47 #endif
48
49 #include <stdio.h> /* sprintf() */
50
51 #include "dummy.h"
52
53 /*****************************************************************************
54  * decoder_sys_t : theora decoder descriptor
55  *****************************************************************************/
56 struct decoder_sys_t
57 {
58     int i_fd;
59 };
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block );
65
66 /*****************************************************************************
67  * OpenDecoder: Open the decoder
68  *****************************************************************************/
69 int E_(OpenDecoder) ( vlc_object_t *p_this )
70 {
71     decoder_t *p_dec = (decoder_t*)p_this;
72     decoder_sys_t *p_sys;
73     char psz_file[ PATH_MAX ];
74     vlc_value_t val;
75
76     /* Allocate the memory needed to store the decoder's structure */
77     if( ( p_dec->p_sys = p_sys =
78           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
79     {
80         msg_Err( p_dec, "out of memory" );
81         return VLC_EGENERIC;
82     }
83
84     sprintf( psz_file, "stream.%i", p_dec->i_object_id );
85
86 #ifndef UNDER_CE
87     var_Create( p_dec, "dummy-save-es", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
88     var_Get( p_dec, "dummy-save-es", &val );
89     if( val.b_bool )
90     {
91         p_sys->i_fd = open( psz_file, O_WRONLY | O_CREAT | O_TRUNC, 00644 );
92
93         if( p_sys->i_fd == -1 )
94         {
95             msg_Err( p_dec, "cannot create `%s'", psz_file );
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     return VLC_SUCCESS;
116 }
117
118 /****************************************************************************
119  * RunDecoder: the whole thing
120  ****************************************************************************
121  * This function must be fed with ogg packets.
122  ****************************************************************************/
123 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
124 {
125     decoder_sys_t *p_sys = p_dec->p_sys;
126     block_t *p_block;
127
128     if( !pp_block || !*pp_block ) return NULL;
129     p_block = *pp_block;
130
131     if( p_sys->i_fd >= 0 && p_block->i_buffer )
132     {
133 #ifndef UNDER_CE
134         write( p_sys->i_fd, p_block->p_buffer, p_block->i_buffer );
135 #endif
136
137         msg_Dbg( p_dec, "dumped %i bytes", p_block->i_buffer );
138     }
139
140     block_Release( p_block );
141     return NULL;
142 }
143
144 /*****************************************************************************
145  * CloseDecoder: decoder destruction
146  *****************************************************************************/
147 void E_(CloseDecoder) ( vlc_object_t *p_this )
148 {
149     decoder_t *p_dec = (decoder_t *)p_this;
150     decoder_sys_t *p_sys = p_dec->p_sys;
151
152 #ifndef UNDER_CE
153     if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
154 #endif
155
156     free( p_sys );
157 }