]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
* v4l: updated, now it should grab (and compress if you want) the video.
[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.1 2003/03/31 03:46:11 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         case VLC_FOURCC('I','4','2','0'):
84         case VLC_FOURCC('I','4','2','2'):
85             p_fifo->pf_run = RunDecoder;
86             return VLC_SUCCESS;
87
88         default:
89             return VLC_EGENERIC;
90     }
91
92 }
93
94 /*****************************************************************************
95  * RunDecoder: this function is called just after the thread is created
96  *****************************************************************************/
97 static int RunDecoder( decoder_fifo_t *p_fifo )
98 {
99     vdec_thread_t *p_vdec;
100     int b_error;
101
102     if( !( p_vdec = malloc( sizeof( vdec_thread_t ) ) ) )
103     {
104         msg_Err( p_fifo, "out of memory" );
105         DecoderError( p_fifo );
106         return( -1 );
107     }
108     memset( p_vdec, 0, sizeof( vdec_thread_t ) );
109
110     p_vdec->p_fifo = p_fifo;
111
112     if( InitThread( p_vdec ) != 0 )
113     {
114         DecoderError( p_fifo );
115         return( -1 );
116     }
117
118     while( ( !p_vdec->p_fifo->b_die )&&( !p_vdec->p_fifo->b_error ) )
119     {
120         DecodeThread( p_vdec );
121     }
122
123
124     if( ( b_error = p_vdec->p_fifo->b_error ) )
125     {
126         DecoderError( p_vdec->p_fifo );
127     }
128
129     EndThread( p_vdec );
130     if( b_error )
131     {
132         return( -1 );
133     }
134
135     return( 0 );
136 }
137
138
139 #define FREE( p ) if( p ) { free( p ); p = NULL; }
140
141
142 /*****************************************************************************
143  * InitThread: initialize data before entering main loop
144  *****************************************************************************/
145 static int InitThread( vdec_thread_t * p_vdec )
146 {
147     vlc_fourcc_t i_chroma;
148
149 #define bih ((BITMAPINFOHEADER*)p_vdec->p_fifo->p_bitmapinfoheader)
150     if( bih == NULL )
151     {
152         msg_Err( p_vdec->p_fifo,
153                  "info missing, fatal" );
154         return( VLC_EGENERIC );
155     }
156     if( bih->biWidth <= 0 || bih->biHeight <= 0 )
157     {
158         msg_Err( p_vdec->p_fifo,
159                  "invalid display size %dx%d",
160                  bih->biWidth, bih->biHeight );
161         return( VLC_EGENERIC );
162     }
163
164     switch( p_vdec->p_fifo->i_fourcc )
165     {
166         case VLC_FOURCC( 'I', '4', '2', '0' ):
167             i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
168             p_vdec->i_raw_size = bih->biWidth * bih->biHeight * 3 / 2;
169             break;
170         default:
171             msg_Err( p_vdec->p_fifo, "invalid codec=%4.4s", (char*)&p_vdec->p_fifo->i_fourcc );
172             return( VLC_EGENERIC );
173     }
174
175     p_vdec->p_vout = vout_Request( p_vdec->p_fifo, NULL,
176                                    bih->biWidth, bih->biHeight,
177                                    i_chroma,
178                                    VOUT_ASPECT_FACTOR * bih->biWidth / bih->biHeight );
179
180     if( p_vdec->p_vout == NULL )
181     {
182         msg_Err( p_vdec->p_fifo, "failled created vout" );
183         return( VLC_EGENERIC );
184     }
185
186     return( VLC_SUCCESS );
187 #undef bih
188 }
189
190
191 static void FillPicture( pes_packet_t *p_pes, picture_t *p_pic )
192 {
193     int i_plane;
194
195     data_packet_t   *p_data;
196     uint8_t *p_src;
197     int     i_src;
198
199     p_data = p_pes->p_first;
200     p_src  = p_data->p_payload_start;
201     i_src = p_data->p_payload_end - p_data->p_payload_start;
202
203     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
204     {
205
206         uint8_t *p_dst;
207         int     i_dst;
208
209         p_dst = p_pic->p[i_plane].p_pixels;
210         i_dst = p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_lines;
211
212         while( i_dst > 0 )
213         {
214             int i_copy;
215
216             i_copy = __MIN( i_src, i_dst );
217             if( i_copy > 0 )
218             {
219                 memcpy( p_dst, p_src, i_copy );
220             }
221             i_dst -= i_copy;
222
223             i_src -= i_copy;
224             if( i_src <= 0 )
225             {
226                 do
227                 {
228                     p_data = p_data->p_next;
229                     if( p_data == NULL )
230                     {
231                         return;
232                     }
233                     p_src  = p_data->p_payload_start;
234                     i_src = p_data->p_payload_end - p_data->p_payload_start;
235                 } while( i_src <= 0 );
236             }
237         }
238     }
239 }
240
241 /*****************************************************************************
242  * DecodeThread: decodes a frame
243  *****************************************************************************/
244 static void DecodeThread( vdec_thread_t *p_vdec )
245 {
246     int             i_size;
247     pes_packet_t    *p_pes;
248     picture_t       *p_pic;
249
250     /* **** get frame **** */
251     input_ExtractPES( p_vdec->p_fifo, &p_pes );
252     if( !p_pes )
253     {
254         p_vdec->p_fifo->b_error = 1;
255         return;
256     }
257     i_size = p_pes->i_pes_size;
258
259     if( i_size < p_vdec->i_raw_size )
260     {
261         msg_Warn( p_vdec->p_fifo, "invalid frame size (%d < %d)", i_size, p_vdec->i_raw_size );
262         input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
263         return;
264     }
265
266     /* **** get video picture **** */
267     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
268     {
269         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
270         {
271             return;
272         }
273         msleep( VOUT_OUTMEM_SLEEP );
274     }
275
276
277     /* **** fill p_pic **** */
278     FillPicture( p_pes, p_pic );
279
280     /* **** display p_pic **** */
281     vout_DatePicture( p_vdec->p_vout, p_pic, p_pes->i_pts);
282     vout_DisplayPicture( p_vdec->p_vout, p_pic );
283
284
285     input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
286 }
287
288
289 /*****************************************************************************
290  * EndThread : faad decoder thread destruction
291  *****************************************************************************/
292 static void EndThread (vdec_thread_t *p_vdec)
293 {
294     if( p_vdec->p_vout )
295     {
296         vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
297     }
298
299     msg_Dbg( p_vdec->p_fifo, "raw video decoder closed" );
300
301     free( p_vdec );
302 }
303
304