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