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