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 $
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_t *p_dec = (decoder_t*)p_this;
81 switch( p_dec->p_fifo->i_fourcc )
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'):
93 case VLC_FOURCC('Y','U','Y','2'):
94 case VLC_FOURCC('U','Y','V','Y'):
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'):
102 p_dec->pf_run = RunDecoder;
110 /*****************************************************************************
111 * RunDecoder: this function is called just after the thread is created
112 *****************************************************************************/
113 static int RunDecoder( decoder_fifo_t *p_fifo )
115 vdec_thread_t *p_vdec;
118 if( !( p_vdec = malloc( sizeof( vdec_thread_t ) ) ) )
120 msg_Err( p_fifo, "out of memory" );
121 DecoderError( p_fifo );
124 memset( p_vdec, 0, sizeof( vdec_thread_t ) );
126 p_vdec->p_fifo = p_fifo;
128 if( InitThread( p_vdec ) != 0 )
130 DecoderError( p_fifo );
134 while( ( !p_vdec->p_fifo->b_die )&&( !p_vdec->p_fifo->b_error ) )
136 DecodeThread( p_vdec );
140 if( ( b_error = p_vdec->p_fifo->b_error ) )
142 DecoderError( p_vdec->p_fifo );
155 #define FREE( p ) if( p ) { free( p ); p = NULL; }
158 /*****************************************************************************
159 * InitThread: initialize data before entering main loop
160 *****************************************************************************/
161 static int InitThread( vdec_thread_t * p_vdec )
166 #define bih ((BITMAPINFOHEADER*)p_vdec->p_fifo->p_bitmapinfoheader)
169 msg_Err( p_vdec->p_fifo,
170 "info missing, fatal" );
171 return( VLC_EGENERIC );
173 if( bih->biWidth <= 0 || bih->biHeight <= 0 )
175 msg_Err( p_vdec->p_fifo,
176 "invalid display size %dx%d",
177 bih->biWidth, bih->biHeight );
178 return( VLC_EGENERIC );
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 /
187 if( p_vdec->p_vout == NULL )
189 msg_Err( p_vdec->p_fifo, "failed created vout" );
190 return( VLC_EGENERIC );
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++ )
199 p_vdec->i_raw_size += pic.p[i].i_lines * pic.p[i].i_visible_pitch;
202 return( VLC_SUCCESS );
207 static void FillPicture( pes_packet_t *p_pes, picture_t *p_pic )
211 data_packet_t *p_data;
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;
219 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
232 i_copy = __MIN( i_src, i_dst );
235 memcpy( p_dst, p_src, i_copy );
246 p_data = p_data->p_next;
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 );
259 /*****************************************************************************
260 * DecodeThread: decodes a frame
261 *****************************************************************************/
262 static void DecodeThread( vdec_thread_t *p_vdec )
268 /* **** get frame **** */
269 input_ExtractPES( p_vdec->p_fifo, &p_pes );
272 p_vdec->p_fifo->b_error = 1;
275 i_size = p_pes->i_pes_size;
277 if( i_size < p_vdec->i_raw_size )
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 );
285 /* **** get video picture **** */
286 while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
288 if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
292 msleep( VOUT_OUTMEM_SLEEP );
296 /* **** fill p_pic **** */
297 FillPicture( p_pes, p_pic );
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 );
304 input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
308 /*****************************************************************************
309 * EndThread : faad decoder thread destruction
310 *****************************************************************************/
311 static void EndThread (vdec_thread_t *p_vdec)
315 vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
318 msg_Dbg( p_vdec->p_fifo, "raw video decoder closed" );