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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
32 #include <stdlib.h> /* malloc(), free() */
33 #include <string.h> /* strdup() */
35 /*****************************************************************************
37 *****************************************************************************/
41 /* Input properties */
42 decoder_fifo_t *p_fifo;
45 /* Output properties */
49 vout_thread_t *p_vout;
53 static int OpenDecoder ( vlc_object_t * );
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 * );
60 /*****************************************************************************
62 *****************************************************************************/
65 set_description( _("Pseudo Raw Video decoder") );
66 set_capability( "decoder", 50 );
67 set_callbacks( OpenDecoder, NULL );
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
76 *****************************************************************************/
77 static int OpenDecoder( vlc_object_t *p_this )
79 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
81 switch( p_fifo->i_fourcc )
83 case VLC_FOURCC('I','4','2','0'):
84 case VLC_FOURCC('I','4','2','2'):
85 p_fifo->pf_run = RunDecoder;
94 /*****************************************************************************
95 * RunDecoder: this function is called just after the thread is created
96 *****************************************************************************/
97 static int RunDecoder( decoder_fifo_t *p_fifo )
99 vdec_thread_t *p_vdec;
102 if( !( p_vdec = malloc( sizeof( vdec_thread_t ) ) ) )
104 msg_Err( p_fifo, "out of memory" );
105 DecoderError( p_fifo );
108 memset( p_vdec, 0, sizeof( vdec_thread_t ) );
110 p_vdec->p_fifo = p_fifo;
112 if( InitThread( p_vdec ) != 0 )
114 DecoderError( p_fifo );
118 while( ( !p_vdec->p_fifo->b_die )&&( !p_vdec->p_fifo->b_error ) )
120 DecodeThread( p_vdec );
124 if( ( b_error = p_vdec->p_fifo->b_error ) )
126 DecoderError( p_vdec->p_fifo );
139 #define FREE( p ) if( p ) { free( p ); p = NULL; }
142 /*****************************************************************************
143 * InitThread: initialize data before entering main loop
144 *****************************************************************************/
145 static int InitThread( vdec_thread_t * p_vdec )
147 vlc_fourcc_t i_chroma;
149 #define bih ((BITMAPINFOHEADER*)p_vdec->p_fifo->p_bitmapinfoheader)
152 msg_Err( p_vdec->p_fifo,
153 "info missing, fatal" );
154 return( VLC_EGENERIC );
156 if( bih->biWidth <= 0 || bih->biHeight <= 0 )
158 msg_Err( p_vdec->p_fifo,
159 "invalid display size %dx%d",
160 bih->biWidth, bih->biHeight );
161 return( VLC_EGENERIC );
164 switch( p_vdec->p_fifo->i_fourcc )
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;
171 msg_Err( p_vdec->p_fifo, "invalid codec=%4.4s", (char*)&p_vdec->p_fifo->i_fourcc );
172 return( VLC_EGENERIC );
175 p_vdec->p_vout = vout_Request( p_vdec->p_fifo, NULL,
176 bih->biWidth, bih->biHeight,
178 VOUT_ASPECT_FACTOR * bih->biWidth / bih->biHeight );
180 if( p_vdec->p_vout == NULL )
182 msg_Err( p_vdec->p_fifo, "failled created vout" );
183 return( VLC_EGENERIC );
186 return( VLC_SUCCESS );
191 static void FillPicture( pes_packet_t *p_pes, picture_t *p_pic )
195 data_packet_t *p_data;
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;
203 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
216 i_copy = __MIN( i_src, i_dst );
219 memcpy( p_dst, p_src, i_copy );
228 p_data = p_data->p_next;
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 );
241 /*****************************************************************************
242 * DecodeThread: decodes a frame
243 *****************************************************************************/
244 static void DecodeThread( vdec_thread_t *p_vdec )
250 /* **** get frame **** */
251 input_ExtractPES( p_vdec->p_fifo, &p_pes );
254 p_vdec->p_fifo->b_error = 1;
257 i_size = p_pes->i_pes_size;
259 if( i_size < p_vdec->i_raw_size )
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 );
266 /* **** get video picture **** */
267 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
269 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
273 msleep( VOUT_OUTMEM_SLEEP );
277 /* **** fill p_pic **** */
278 FillPicture( p_pes, p_pic );
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 );
285 input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
289 /*****************************************************************************
290 * EndThread : faad decoder thread destruction
291 *****************************************************************************/
292 static void EndThread (vdec_thread_t *p_vdec)
296 vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
299 msg_Dbg( p_vdec->p_fifo, "raw video decoder closed" );