]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
* configure.ac: fix for wxWindows headers detection.
[vlc] / modules / codec / rawvideo.c
1 /*****************************************************************************
2  * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: rawvideo.c,v 1.7 2003/10/24 21:27:06 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                              /* strdup() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35
36 #include "codecs.h"
37
38 /*****************************************************************************
39  * decoder_sys_t : raw video decoder descriptor
40  *****************************************************************************/
41 struct decoder_sys_t
42 {
43     /* Module mode */
44     vlc_bool_t b_packetizer;
45
46     /*
47      * Input properties
48      */
49     int i_raw_size;
50
51     /*
52      * Output properties
53      */
54     vout_thread_t *p_vout;
55
56     /*
57      * Packetizer output properties
58      */
59     sout_packetizer_input_t *p_sout_input;
60     sout_format_t           sout_format;
61
62     /*
63      * Common properties
64      */
65     mtime_t i_pts;
66
67 };
68
69 /****************************************************************************
70  * Local prototypes
71  ****************************************************************************/
72 static int OpenDecoder   ( vlc_object_t * );
73 static int OpenPacketizer( vlc_object_t * );
74
75 static int InitDecoder   ( decoder_t * );
76 static int RunDecoder    ( decoder_t *, block_t * );
77 static int EndDecoder    ( decoder_t * );
78
79 static int DecodeFrame   ( decoder_t *, block_t * );
80 static int SendFrame     ( decoder_t *, block_t * );
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin();
86     set_description( _("Pseudo Raw Video decoder") );
87     set_capability( "decoder", 50 );
88     set_callbacks( OpenDecoder, NULL );
89
90     add_submodule();
91     set_description( _("Pseudo Raw Video packetizer") );
92     set_capability( "packetizer", 100 );
93     set_callbacks( OpenPacketizer, NULL );
94 vlc_module_end();
95
96 /*****************************************************************************
97  * OpenDecoder: probe the decoder and return score
98  *****************************************************************************
99  * Tries to launch a decoder and return score so that the interface is able
100  * to choose.
101  *****************************************************************************/
102 static int OpenDecoder( vlc_object_t *p_this )
103 {
104     decoder_t *p_dec = (decoder_t*)p_this;
105
106     switch( p_dec->p_fifo->i_fourcc )
107     {
108         /* Planar YUV */
109         case VLC_FOURCC('I','4','4','4'):
110         case VLC_FOURCC('I','4','2','2'):
111         case VLC_FOURCC('I','4','2','0'):
112         case VLC_FOURCC('Y','V','1','2'):
113         case VLC_FOURCC('I','Y','U','V'):
114         case VLC_FOURCC('I','4','1','1'):
115         case VLC_FOURCC('I','4','1','0'):
116
117         /* Packed YUV */
118         case VLC_FOURCC('Y','U','Y','2'):
119         case VLC_FOURCC('U','Y','V','Y'):
120
121         /* RGB */
122         case VLC_FOURCC('R','V','3','2'):
123         case VLC_FOURCC('R','V','2','4'):
124         case VLC_FOURCC('R','V','1','6'):
125         case VLC_FOURCC('R','V','1','5'):
126             break;
127
128         default:
129             return VLC_EGENERIC;
130     }
131
132     p_dec->pf_init = InitDecoder;
133     p_dec->pf_decode = RunDecoder;
134     p_dec->pf_end = EndDecoder;
135
136     /* Allocate the memory needed to store the decoder's structure */
137     if( ( p_dec->p_sys =
138           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
139     {
140         msg_Err( p_dec, "out of memory" );
141         return VLC_EGENERIC;
142     }
143     p_dec->p_sys->b_packetizer = VLC_FALSE;
144
145     return VLC_SUCCESS;
146 }
147
148 static int OpenPacketizer( vlc_object_t *p_this )
149 {
150     decoder_t *p_dec = (decoder_t*)p_this;
151
152     int i_ret = OpenDecoder( p_this );
153
154     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
155
156     return i_ret;
157 }
158
159 /*****************************************************************************
160  * InitDecoder: Initalize the decoder
161  *****************************************************************************/
162 static int InitDecoder( decoder_t *p_dec )
163 {
164     decoder_sys_t *p_sys = p_dec->p_sys;
165     video_frame_format_t format;
166
167     p_sys->i_pts = 0;
168
169     p_sys->p_sout_input = NULL;
170     p_sys->sout_format.i_cat = VIDEO_ES;
171     p_sys->sout_format.i_block_align = 0;
172     p_sys->sout_format.i_bitrate     = 0;
173     p_sys->sout_format.i_extra_data  = 0;
174     p_sys->sout_format.p_extra_data  = NULL;
175
176 #define bih ((BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader)
177     if( bih == NULL )
178     {
179         msg_Err( p_dec, "info missing, fatal" );
180         return VLC_EGENERIC;
181     }
182     if( bih->biWidth <= 0 || bih->biHeight <= 0 )
183     {
184         msg_Err( p_dec, "invalid display size %dx%d",
185                  bih->biWidth, bih->biHeight );
186         return VLC_EGENERIC;
187     }
188
189     if( p_sys->b_packetizer )
190     {
191         /* add an input for the stream ouput */
192         p_sys->sout_format.i_width  = bih->biWidth;
193         p_sys->sout_format.i_height = bih->biHeight;
194         p_sys->sout_format.i_fourcc = p_dec->p_fifo->i_fourcc;
195
196         p_sys->p_sout_input =
197             sout_InputNew( p_dec, &p_sys->sout_format );
198
199         if( !p_sys->p_sout_input )
200         {
201             msg_Err( p_dec, "cannot add a new stream" );
202             return VLC_EGENERIC;
203         }
204     }
205     else
206     {
207         /* Initialize video output */
208         p_sys->p_vout = vout_Request( p_dec, NULL,
209                                       bih->biWidth, bih->biHeight,
210                                       p_dec->p_fifo->i_fourcc,
211                                       VOUT_ASPECT_FACTOR * bih->biWidth /
212                                       bih->biHeight );
213         if( p_sys->p_vout == NULL )
214         {
215             msg_Err( p_dec, "failed to create vout" );
216             return VLC_EGENERIC;
217         }
218     }
219
220     /* Find out p_vdec->i_raw_size */
221     vout_InitFormat( &format, p_dec->p_fifo->i_fourcc,
222                      bih->biWidth, bih->biHeight,
223                      bih->biWidth * VOUT_ASPECT_FACTOR / bih->biHeight );
224     p_sys->i_raw_size = format.i_bits_per_pixel *
225         format.i_width * format.i_height / 8;
226 #undef bih
227
228     return VLC_SUCCESS;
229 }
230
231 /****************************************************************************
232  * RunDecoder: the whole thing
233  ****************************************************************************
234  * This function must be fed with ogg packets.
235  ****************************************************************************/
236 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
237 {
238     decoder_sys_t *p_sys = p_dec->p_sys;
239     int i_ret;
240
241 #if 0
242     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
243     {
244         /* We've just started the stream, wait for the first PTS. */
245         block_Release( p_block );
246         return VLC_SUCCESS;
247     }
248 #endif
249
250     /* Date management */
251     if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
252     {
253         p_sys->i_pts = p_block->i_pts;
254     }
255
256     if( p_block->i_buffer < p_sys->i_raw_size )
257     {
258         msg_Warn( p_dec, "invalid frame size (%d < %d)",
259                   p_block->i_buffer, p_sys->i_raw_size );
260
261         block_Release( p_block );
262         return VLC_EGENERIC;
263     }
264
265     if( p_sys->b_packetizer )
266     {
267         i_ret = SendFrame( p_dec, p_block );
268     }
269     else
270     {
271         i_ret = DecodeFrame( p_dec, p_block );
272     }
273
274     /* Date management: 1 frame per packet */
275     p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
276
277     block_Release( p_block );
278     return i_ret;
279 }
280
281 /*****************************************************************************
282  * FillPicture:
283  *****************************************************************************/
284 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
285 {
286     uint8_t *p_src, *p_dst;
287     int i_src, i_plane, i_line, i_width;
288
289     p_src  = p_block->p_buffer;
290     i_src = p_block->i_buffer;
291
292     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
293     {
294         p_dst = p_pic->p[i_plane].p_pixels;
295         i_width = p_pic->p[i_plane].i_visible_pitch;
296
297         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
298         {
299             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
300             p_src += i_width;
301             p_dst += p_pic->p[i_plane].i_pitch;
302         }
303     }
304 }
305
306 /*****************************************************************************
307  * DecodeFrame: decodes a video frame.
308  *****************************************************************************/
309 static int DecodeFrame( decoder_t *p_dec, block_t *p_block )
310 {
311     decoder_sys_t *p_sys = p_dec->p_sys;
312     picture_t *p_pic;
313
314     /* Get a new picture */
315     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
316     {
317         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
318         {
319             return VLC_EGENERIC;
320         }
321         msleep( VOUT_OUTMEM_SLEEP );
322     }
323     if( !p_pic ) return VLC_EGENERIC;
324
325     FillPicture( p_dec, p_block, p_pic );
326
327     vout_DatePicture( p_sys->p_vout, p_pic, p_sys->i_pts );
328     vout_DisplayPicture( p_sys->p_vout, p_pic );
329
330     return VLC_SUCCESS;
331 }
332
333 /*****************************************************************************
334  * SendFrame: send a video frame to the stream output.
335  *****************************************************************************/
336 static int SendFrame( decoder_t *p_dec, block_t *p_block )
337 {
338     decoder_sys_t *p_sys = p_dec->p_sys;
339
340     sout_buffer_t *p_sout_buffer =
341         sout_BufferNew( p_sys->p_sout_input->p_sout, p_block->i_buffer );
342
343     if( !p_sout_buffer ) return VLC_EGENERIC;
344
345     p_dec->p_vlc->pf_memcpy( p_sout_buffer->p_buffer,
346                              p_block->p_buffer, p_block->i_buffer );
347
348     p_sout_buffer->i_dts = p_sout_buffer->i_pts = p_sys->i_pts;
349
350     sout_InputSendBuffer( p_sys->p_sout_input, p_sout_buffer );
351
352     return VLC_SUCCESS;
353 }
354
355 /*****************************************************************************
356  * EndDecoder: decoder destruction
357  *****************************************************************************/
358 static int EndDecoder( decoder_t *p_dec )
359 {
360     decoder_sys_t *p_sys = p_dec->p_sys;
361
362     if( !p_sys->b_packetizer )
363     {
364         vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
365     }
366
367     if( p_sys->p_sout_input != NULL )
368     {
369         sout_InputDelete( p_sys->p_sout_input );
370     }
371
372     free( p_sys );
373
374     return VLC_SUCCESS;
375 }