]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
bb2817f71f75088bfdb66d85d8dd31df29a8a64f
[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.5 2003/05/02 03:40:01 fenrir 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 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, "failed created vout" );
191         return( VLC_EGENERIC );
192     }
193
194     /* Find out p_vdec->i_raw_size */
195     vout_InitPicture( VLC_OBJECT(p_vdec->p_fifo), &pic,
196                       bih->biWidth, bih->biHeight, p_vdec->p_fifo->i_fourcc );
197     p_vdec->i_raw_size = 0;
198     for( i = 0; i < pic.i_planes; i++ )
199     {
200         p_vdec->i_raw_size += pic.p[i].i_lines * pic.p[i].i_visible_pitch;
201     }
202
203     return( VLC_SUCCESS );
204 #undef bih
205 }
206
207
208 static void FillPicture( pes_packet_t *p_pes, picture_t *p_pic )
209 {
210     int i_plane;
211
212     data_packet_t   *p_data;
213     uint8_t *p_src;
214     int     i_src;
215
216     p_data = p_pes->p_first;
217     p_src  = p_data->p_payload_start;
218     i_src = p_data->p_payload_end - p_data->p_payload_start;
219
220     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
221     {
222
223         uint8_t *p_dst;
224         int     i_dst;
225
226         p_dst = p_pic->p[i_plane].p_pixels;
227         i_dst = p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_lines;
228
229         while( i_dst > 0 )
230         {
231             int i_copy;
232
233             i_copy = __MIN( i_src, i_dst );
234             if( i_copy > 0 )
235             {
236                 memcpy( p_dst, p_src, i_copy );
237             }
238             i_dst -= i_copy;
239             p_dst += i_copy;
240
241             i_src -= i_copy;
242             p_src += i_copy;
243             if( i_src <= 0 )
244             {
245                 do
246                 {
247                     p_data = p_data->p_next;
248                     if( p_data == NULL )
249                     {
250                         return;
251                     }
252                     p_src  = p_data->p_payload_start;
253                     i_src = p_data->p_payload_end - p_data->p_payload_start;
254                 } while( i_src <= 0 );
255             }
256         }
257     }
258 }
259
260 /*****************************************************************************
261  * DecodeThread: decodes a frame
262  *****************************************************************************/
263 static void DecodeThread( vdec_thread_t *p_vdec )
264 {
265     int             i_size;
266     pes_packet_t    *p_pes;
267     picture_t       *p_pic;
268
269     /* **** get frame **** */
270     input_ExtractPES( p_vdec->p_fifo, &p_pes );
271     if( !p_pes )
272     {
273         p_vdec->p_fifo->b_error = 1;
274         return;
275     }
276     i_size = p_pes->i_pes_size;
277
278     if( i_size < p_vdec->i_raw_size )
279     {
280         msg_Warn( p_vdec->p_fifo, "invalid frame size (%d < %d)",
281                   i_size, p_vdec->i_raw_size );
282         //input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
283         return;
284     }
285
286     /* **** get video picture **** */
287     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
288     {
289         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
290         {
291             return;
292         }
293         msleep( VOUT_OUTMEM_SLEEP );
294     }
295
296
297     /* **** fill p_pic **** */
298     FillPicture( p_pes, p_pic );
299
300     /* **** display p_pic **** */
301     vout_DatePicture( p_vdec->p_vout, p_pic, p_pes->i_pts);
302     vout_DisplayPicture( p_vdec->p_vout, p_pic );
303
304
305     input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
306 }
307
308
309 /*****************************************************************************
310  * EndThread : faad decoder thread destruction
311  *****************************************************************************/
312 static void EndThread (vdec_thread_t *p_vdec)
313 {
314     if( p_vdec->p_vout )
315     {
316         vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
317     }
318
319     msg_Dbg( p_vdec->p_fifo, "raw video decoder closed" );
320
321     free( p_vdec );
322 }