1 /*
2  * Copyright (C) 2004 Nathan Lutchansky <lutchann@litech.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #include <sys/types.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <pthread.h>
26 
27 #include <xvid.h>
28 
29 #include <event.h>
30 #include <log.h>
31 #include <frame.h>
32 #include <stream.h>
33 // #include <decoders.h>
34 #include <conf_parse.h>
35 
36 struct mpeg4_decoder {
37 	struct stream *output;
38 	struct stream_destination *input;
39 	struct frame_exchanger *ex;
40 	pthread_t decoding_thread;
41 	int running; /* only used by the main thread */
42 
43 	int width;
44 	int height;
45 
46 	/* reset_pending is used to indicate that there may have been a long
47 	 * break since the last frame, presumably because nobody was listening
48 	 * and the source stopped sending us frames, so the decoding thread
49 	 * should reset the decoder.  It doesn't really need synchronization
50 	 * because if we reset in the middle of a bunch of contiguous frames
51 	 * it's not the end of the world. */
52 	int reset_pending;
53 
54 	/* All the parameters after the handle indicate the settings used
55 	 * to set up the current decoder.  Therefore, they are invalid
56 	 * when xvid_handle == NULL. */
57 	void *xvid_handle;
58 };
59 
mpeg4_start(struct mpeg4_decoder * en,struct frame * f)60 static void mpeg4_start( struct mpeg4_decoder *en, struct frame *f )
61 {
62 	xvid_dec_create_t xvid_dec_create;
63 
64 	en->reset_pending = 0;
65 
66 	memset( &xvid_dec_create, 0, sizeof( xvid_dec_create ) );
67 	xvid_dec_create.version = XVID_VERSION;
68 	xvid_dec_create.width = 0;
69 	xvid_dec_create.height = 0;
70 
71 	if( xvid_decore( NULL, XVID_DEC_CREATE,
72 				&xvid_dec_create, NULL ) )
73 	{
74 		spook_log( SL_ERR, "mpeg4: unable to start XviD!" );
75 		return;
76 	}
77 
78 	en->xvid_handle = xvid_dec_create.handle;
79 }
80 
mpeg4_stop(struct mpeg4_decoder * en)81 static void mpeg4_stop( struct mpeg4_decoder *en )
82 {
83 	spook_log( SL_DEBUG, "mpeg4: destroying mpeg4 decoder" );
84 
85 	xvid_decore( en->xvid_handle, XVID_DEC_DESTROY, NULL, NULL );
86 	en->xvid_handle = NULL;
87 }
88 
mpeg4_loop(void * d)89 static void *mpeg4_loop( void *d )
90 {
91 	struct mpeg4_decoder *en = (struct mpeg4_decoder *)d;
92 	xvid_dec_frame_t xvid_dec_frame;
93 	xvid_dec_stats_t xvid_dec_stats;
94 	struct frame *out, *input;
95 	int used, pos;
96 
97 	for(;;)
98 	{
99 		input = get_next_frame( en->ex, 1 );
100 
101 		if( en->reset_pending && en->xvid_handle ) mpeg4_stop( en );
102 		if( ! en->xvid_handle ) mpeg4_start( en, input );
103 
104 		out = new_frame();
105 		out->width = en->width;
106 		out->height = en->height;
107 
108 		pos = 0;
109 
110 		while( input->length - pos > 0 )
111 		{
112 			memset( &xvid_dec_frame, 0, sizeof( xvid_dec_frame ) );
113 			xvid_dec_frame.version = XVID_VERSION;
114 			xvid_dec_frame.general = 0;
115 			xvid_dec_frame.bitstream = input->d + pos;
116 			xvid_dec_frame.length = input->length - pos;
117 			xvid_dec_frame.output.plane[0] = out->d;
118 			xvid_dec_frame.output.stride[0] = 2 * out->width;
119 			xvid_dec_frame.output.csp = XVID_CSP_UYVY;
120 			xvid_dec_stats.version = XVID_VERSION;
121 
122 			used = xvid_decore( en->xvid_handle, XVID_DEC_DECODE,
123 					&xvid_dec_frame, &xvid_dec_stats );
124 			if( used < 0 )
125 			{
126 				out->length = 0;
127 				spook_log( SL_WARN, "mpeg4: XviD decoding failed!" );
128 			}
129 			if( xvid_dec_stats.type == XVID_TYPE_VOL )
130 			{
131 				out->width = en->width = xvid_dec_stats.data.vol.width;
132 				out->height = en->height = xvid_dec_stats.data.vol.height;
133 			}
134 			pos += used;
135 		}
136 
137 		out->format = FORMAT_RAW_UYVY;
138 		out->length = 2 * out->width * out->height;
139 		out->key = 1;
140 
141 		deliver_frame( en->ex, out );
142 
143 		unref_frame( input );
144 	}
145 
146 	return NULL;
147 }
148 
mpeg4_decode(struct frame * input,void * d)149 static void mpeg4_decode( struct frame *input, void *d )
150 {
151 	struct mpeg4_decoder *en = (struct mpeg4_decoder *)d;
152 
153 	exchange_frame( en->ex, input );
154 }
155 
get_framerate(struct stream * s,int * fincr,int * fbase)156 static void get_framerate( struct stream *s, int *fincr, int *fbase )
157 {
158 	struct mpeg4_decoder *en = (struct mpeg4_decoder *)s->private;
159 
160 	en->input->stream->get_framerate( en->input->stream, fincr, fbase );
161 }
162 
set_running(struct stream * s,int running)163 static void set_running( struct stream *s, int running )
164 {
165 	struct mpeg4_decoder *en = (struct mpeg4_decoder *)s->private;
166 
167 	spook_log( SL_DEBUG,
168 		"mpeg4 decoder is told to set running to %d", running );
169 
170 	if( ! en->running && running ) en->reset_pending = 1;
171 	set_waiting( en->input, running );
172 	en->running = running;
173 }
174 
175 /************************ CONFIGURATION DIRECTIVES ************************/
176 
start_block(void)177 static void *start_block(void)
178 {
179 	struct mpeg4_decoder *en;
180 
181 	en = (struct mpeg4_decoder *)malloc( sizeof( struct mpeg4_decoder ) );
182 	en->output = NULL;
183 	en->xvid_handle = NULL;
184 	en->width = en->height = 0;
185 
186 	return en;
187 }
188 
end_block(void * d)189 static int end_block( void *d )
190 {
191 	struct mpeg4_decoder *en = (struct mpeg4_decoder *)d;
192 
193 	if( ! en->output )
194 	{
195 		spook_log( SL_ERR, "mpeg4: missing output stream name" );
196 		return -1;
197 	}
198 	if( ! en->input )
199 	{
200 		spook_log( SL_ERR, "mpeg4: missing input stream name" );
201 		return -1;
202 	}
203 
204 	en->ex = new_exchanger( 8, deliver_frame_to_stream, en->output );
205 	pthread_create( &en->decoding_thread, NULL, mpeg4_loop, en );
206 
207 	return 0;
208 }
209 
set_input(int num_tokens,struct token * tokens,void * d)210 static int set_input( int num_tokens, struct token *tokens, void *d )
211 {
212 	struct mpeg4_decoder *en = (struct mpeg4_decoder *)d;
213 	int formats[1] = { FORMAT_MPEG4 };
214 
215 	if( ! ( en->input = connect_to_stream( tokens[1].v.str, mpeg4_decode,
216 						en, formats, 1 ) ) )
217 	{
218 		spook_log( SL_ERR, "mpeg4: unable to connect to stream \"%s\"",
219 				tokens[1].v.str );
220 		return -1;
221 	}
222 	return 0;
223 }
224 
set_output(int num_tokens,struct token * tokens,void * d)225 static int set_output( int num_tokens, struct token *tokens, void *d )
226 {
227 	struct mpeg4_decoder *en = (struct mpeg4_decoder *)d;
228 
229 	en->output = new_stream( tokens[1].v.str, FORMAT_RAW_UYVY, en );
230 	if( ! en->output )
231 	{
232 		spook_log( SL_ERR, "mpeg4: unable to create stream \"%s\"",
233 			tokens[1].v.str );
234 		return -1;
235 	}
236 	en->output->get_framerate = get_framerate;
237 	en->output->set_running = set_running;
238 	return 0;
239 }
240 
241 static struct statement config_statements[] = {
242 	/* directive name, process function, min args, max args, arg types */
243 	{ "input", set_input, 1, 1, { TOKEN_STR } },
244 	{ "output", set_output, 1, 1, { TOKEN_STR } },
245 
246 	/* empty terminator -- do not remove */
247 	{ NULL, NULL, 0, 0, {} }
248 };
249 
mpeg4_dec_init(void)250 int mpeg4_dec_init(void)
251 {
252 	xvid_gbl_init_t xvid_gbl_init;
253 
254 	memset( &xvid_gbl_init, 0, sizeof( xvid_gbl_init ) );
255 	xvid_gbl_init.version = XVID_VERSION;
256 	xvid_global( NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL );
257 	register_config_context( "decoder", "mpeg4", start_block, end_block,
258 					config_statements );
259 	return 0;
260 }
261