]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
* src/video_output/video_output.c: small bugfix + vout_Create() is now blocking until...
[vlc] / modules / codec / rawvideo.c
1 /*****************************************************************************
2  * rawvideo.c: Pseudo audio decoder; for raw video data
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: rawvideo.c,v 1.3 2003/04/27 17:53:20 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 <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                              /* strdup() */
34 #include "codecs.h"
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38
39 typedef struct
40 {
41     /* Input properties */
42     decoder_fifo_t *p_fifo;
43     int            i_raw_size;
44
45     /* Output properties */
46
47     mtime_t             pts;
48
49     vout_thread_t       *p_vout;
50
51 } vdec_thread_t;
52
53 static int  OpenDecoder    ( vlc_object_t * );
54
55 static int  RunDecoder     ( decoder_fifo_t * );
56 static int  InitThread     ( vdec_thread_t * );
57 static void DecodeThread   ( vdec_thread_t * );
58 static void EndThread      ( vdec_thread_t * );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63
64 vlc_module_begin();
65     set_description( _("Pseudo Raw Video decoder") );
66     set_capability( "decoder", 50 );
67     set_callbacks( OpenDecoder, NULL );
68 vlc_module_end();
69
70
71 /*****************************************************************************
72  * OpenDecoder: probe the decoder and return score
73  *****************************************************************************
74  * Tries to launch a decoder and return score so that the interface is able
75  * to choose.
76  *****************************************************************************/
77 static int OpenDecoder( vlc_object_t *p_this )
78 {
79     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
80
81     switch( p_fifo->i_fourcc )
82     {
83         /* Planar YUV */
84         case VLC_FOURCC('I','4','4','4'):
85         case VLC_FOURCC('I','4','2','2'):
86         case VLC_FOURCC('I','4','2','0'):
87         case VLC_FOURCC('Y','V','1','2'):
88         case VLC_FOURCC('I','Y','U','V'):
89         case VLC_FOURCC('I','4','1','1'):
90         case VLC_FOURCC('I','4','1','0'):
91
92         /* Packed YUV */
93         case VLC_FOURCC('Y','U','Y','2'):
94         case VLC_FOURCC('U','Y','V','Y'):
95
96         /* RGB */
97         case VLC_FOURCC('R','V','3','2'):
98         case VLC_FOURCC('R','V','2','4'):
99         case VLC_FOURCC('R','V','1','6'):
100         case VLC_FOURCC('R','V','1','5'):
101
102             p_fifo->pf_run = RunDecoder;
103             return VLC_SUCCESS;
104
105         default:
106             return VLC_EGENERIC;
107     }
108
109 }
110
111 /*****************************************************************************
112  * RunDecoder: this function is called just after the thread is created
113  *****************************************************************************/
114 static int RunDecoder( decoder_fifo_t *p_fifo )
115 {
116     vdec_thread_t *p_vdec;
117     int b_error;
118
119     if( !( p_vdec = malloc( sizeof( vdec_thread_t ) ) ) )
120     {
121         msg_Err( p_fifo, "out of memory" );
122         DecoderError( p_fifo );
123         return( -1 );
124     }
125     memset( p_vdec, 0, sizeof( vdec_thread_t ) );
126
127     p_vdec->p_fifo = p_fifo;
128
129     if( InitThread( p_vdec ) != 0 )
130     {
131         DecoderError( p_fifo );
132         return( -1 );
133     }
134
135     while( ( !p_vdec->p_fifo->b_die )&&( !p_vdec->p_fifo->b_error ) )
136     {
137         DecodeThread( p_vdec );
138     }
139
140
141     if( ( b_error = p_vdec->p_fifo->b_error ) )
142     {
143         DecoderError( p_vdec->p_fifo );
144     }
145
146     EndThread( p_vdec );
147     if( b_error )
148     {
149         return( -1 );
150     }
151
152     return( 0 );
153 }
154
155
156 #define FREE( p ) if( p ) { free( p ); p = NULL; }
157
158
159 /*****************************************************************************
160  * InitThread: initialize data before entering main loop
161  *****************************************************************************/
162 static int InitThread( vdec_thread_t * p_vdec )
163 {
164     picture_t *p_pic;
165     int i;
166
167 #define bih ((BITMAPINFOHEADER*)p_vdec->p_fifo->p_bitmapinfoheader)
168     if( bih == NULL )
169     {
170         msg_Err( p_vdec->p_fifo,
171                  "info missing, fatal" );
172         return( VLC_EGENERIC );
173     }
174     if( bih->biWidth <= 0 || bih->biHeight <= 0 )
175     {
176         msg_Err( p_vdec->p_fifo,
177                  "invalid display size %dx%d",
178                  bih->biWidth, bih->biHeight );
179         return( VLC_EGENERIC );
180     }
181
182     p_vdec->p_vout = vout_Request( p_vdec->p_fifo, NULL,
183                                    bih->biWidth, bih->biHeight,
184                                    p_vdec->p_fifo->i_fourcc,
185                                    VOUT_ASPECT_FACTOR * bih->biWidth /
186                                    bih->biHeight );
187
188     if( p_vdec->p_vout == NULL )
189     {
190         msg_Err( p_vdec->p_fifo, "failled created vout" );
191         return( VLC_EGENERIC );
192     }
193
194     /* Get a 1 picture to be able to compute p_vdec->i_raw_size */
195     p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 );
196     if( p_pic == NULL )
197     {
198         msg_Err( p_vdec->p_fifo, "failled to get a vout picture" );
199         return( VLC_EGENERIC );
200     }
201
202     p_vdec->i_raw_size = 0;
203     for( i = 0; i < p_pic->i_planes; i++ )
204     {
205         p_vdec->i_raw_size += p_pic->p[i].i_lines * p_pic->p[i].i_visible_pitch
206             * p_pic->p[i].i_pixel_pitch;
207     }
208
209     vout_DestroyPicture( p_vdec->p_vout, p_pic );
210
211     return( VLC_SUCCESS );
212 #undef bih
213 }
214
215
216 static void FillPicture( pes_packet_t *p_pes, picture_t *p_pic )
217 {
218     int i_plane;
219
220     data_packet_t   *p_data;
221     uint8_t *p_src;
222     int     i_src;
223
224     p_data = p_pes->p_first;
225     p_src  = p_data->p_payload_start;
226     i_src = p_data->p_payload_end - p_data->p_payload_start;
227
228     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
229     {
230
231         uint8_t *p_dst;
232         int     i_dst;
233
234         p_dst = p_pic->p[i_plane].p_pixels;
235         i_dst = p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_lines;
236
237         while( i_dst > 0 )
238         {
239             int i_copy;
240
241             i_copy = __MIN( i_src, i_dst );
242             if( i_copy > 0 )
243             {
244                 memcpy( p_dst, p_src, i_copy );
245             }
246             i_dst -= i_copy;
247             p_dst += i_copy;
248
249             i_src -= i_copy;
250             if( i_src <= 0 )
251             {
252                 do
253                 {
254                     p_data = p_data->p_next;
255                     if( p_data == NULL )
256                     {
257                         return;
258                     }
259                     p_src  = p_data->p_payload_start;
260                     i_src = p_data->p_payload_end - p_data->p_payload_start;
261                 } while( i_src <= 0 );
262             }
263         }
264     }
265 }
266
267 /*****************************************************************************
268  * DecodeThread: decodes a frame
269  *****************************************************************************/
270 static void DecodeThread( vdec_thread_t *p_vdec )
271 {
272     int             i_size;
273     pes_packet_t    *p_pes;
274     picture_t       *p_pic;
275
276     /* **** get frame **** */
277     input_ExtractPES( p_vdec->p_fifo, &p_pes );
278     if( !p_pes )
279     {
280         p_vdec->p_fifo->b_error = 1;
281         return;
282     }
283     i_size = p_pes->i_pes_size;
284
285     if( i_size < p_vdec->i_raw_size )
286     {
287         msg_Warn( p_vdec->p_fifo, "invalid frame size (%d < %d)", i_size, p_vdec->i_raw_size );
288         input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
289         return;
290     }
291
292     /* **** get video picture **** */
293     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
294     {
295         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
296         {
297             return;
298         }
299         msleep( VOUT_OUTMEM_SLEEP );
300     }
301
302
303     /* **** fill p_pic **** */
304     FillPicture( p_pes, p_pic );
305
306     /* **** display p_pic **** */
307     vout_DatePicture( p_vdec->p_vout, p_pic, p_pes->i_pts);
308     vout_DisplayPicture( p_vdec->p_vout, p_pic );
309
310
311     input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
312 }
313
314
315 /*****************************************************************************
316  * EndThread : faad decoder thread destruction
317  *****************************************************************************/
318 static void EndThread (vdec_thread_t *p_vdec)
319 {
320     if( p_vdec->p_vout )
321     {
322         vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
323     }
324
325     msg_Dbg( p_vdec->p_fifo, "raw video decoder closed" );
326
327     free( p_vdec );
328 }