1 /*****************************************************************************
2 * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: rawvideo.c,v 1.7 2003/10/24 21:27:06 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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
38 /*****************************************************************************
39 * decoder_sys_t : raw video decoder descriptor
40 *****************************************************************************/
44 vlc_bool_t b_packetizer;
54 vout_thread_t *p_vout;
57 * Packetizer output properties
59 sout_packetizer_input_t *p_sout_input;
60 sout_format_t sout_format;
69 /****************************************************************************
71 ****************************************************************************/
72 static int OpenDecoder ( vlc_object_t * );
73 static int OpenPacketizer( vlc_object_t * );
75 static int InitDecoder ( decoder_t * );
76 static int RunDecoder ( decoder_t *, block_t * );
77 static int EndDecoder ( decoder_t * );
79 static int DecodeFrame ( decoder_t *, block_t * );
80 static int SendFrame ( decoder_t *, block_t * );
82 /*****************************************************************************
84 *****************************************************************************/
86 set_description( _("Pseudo Raw Video decoder") );
87 set_capability( "decoder", 50 );
88 set_callbacks( OpenDecoder, NULL );
91 set_description( _("Pseudo Raw Video packetizer") );
92 set_capability( "packetizer", 100 );
93 set_callbacks( OpenPacketizer, NULL );
96 /*****************************************************************************
97 * OpenDecoder: probe the decoder and return score
98 *****************************************************************************
99 * Tries to launch a decoder and return score so that the interface is able
101 *****************************************************************************/
102 static int OpenDecoder( vlc_object_t *p_this )
104 decoder_t *p_dec = (decoder_t*)p_this;
106 switch( p_dec->p_fifo->i_fourcc )
109 case VLC_FOURCC('I','4','4','4'):
110 case VLC_FOURCC('I','4','2','2'):
111 case VLC_FOURCC('I','4','2','0'):
112 case VLC_FOURCC('Y','V','1','2'):
113 case VLC_FOURCC('I','Y','U','V'):
114 case VLC_FOURCC('I','4','1','1'):
115 case VLC_FOURCC('I','4','1','0'):
118 case VLC_FOURCC('Y','U','Y','2'):
119 case VLC_FOURCC('U','Y','V','Y'):
122 case VLC_FOURCC('R','V','3','2'):
123 case VLC_FOURCC('R','V','2','4'):
124 case VLC_FOURCC('R','V','1','6'):
125 case VLC_FOURCC('R','V','1','5'):
132 p_dec->pf_init = InitDecoder;
133 p_dec->pf_decode = RunDecoder;
134 p_dec->pf_end = EndDecoder;
136 /* Allocate the memory needed to store the decoder's structure */
138 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
140 msg_Err( p_dec, "out of memory" );
143 p_dec->p_sys->b_packetizer = VLC_FALSE;
148 static int OpenPacketizer( vlc_object_t *p_this )
150 decoder_t *p_dec = (decoder_t*)p_this;
152 int i_ret = OpenDecoder( p_this );
154 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
159 /*****************************************************************************
160 * InitDecoder: Initalize the decoder
161 *****************************************************************************/
162 static int InitDecoder( decoder_t *p_dec )
164 decoder_sys_t *p_sys = p_dec->p_sys;
165 video_frame_format_t format;
169 p_sys->p_sout_input = NULL;
170 p_sys->sout_format.i_cat = VIDEO_ES;
171 p_sys->sout_format.i_block_align = 0;
172 p_sys->sout_format.i_bitrate = 0;
173 p_sys->sout_format.i_extra_data = 0;
174 p_sys->sout_format.p_extra_data = NULL;
176 #define bih ((BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader)
179 msg_Err( p_dec, "info missing, fatal" );
182 if( bih->biWidth <= 0 || bih->biHeight <= 0 )
184 msg_Err( p_dec, "invalid display size %dx%d",
185 bih->biWidth, bih->biHeight );
189 if( p_sys->b_packetizer )
191 /* add an input for the stream ouput */
192 p_sys->sout_format.i_width = bih->biWidth;
193 p_sys->sout_format.i_height = bih->biHeight;
194 p_sys->sout_format.i_fourcc = p_dec->p_fifo->i_fourcc;
196 p_sys->p_sout_input =
197 sout_InputNew( p_dec, &p_sys->sout_format );
199 if( !p_sys->p_sout_input )
201 msg_Err( p_dec, "cannot add a new stream" );
207 /* Initialize video output */
208 p_sys->p_vout = vout_Request( p_dec, NULL,
209 bih->biWidth, bih->biHeight,
210 p_dec->p_fifo->i_fourcc,
211 VOUT_ASPECT_FACTOR * bih->biWidth /
213 if( p_sys->p_vout == NULL )
215 msg_Err( p_dec, "failed to create vout" );
220 /* Find out p_vdec->i_raw_size */
221 vout_InitFormat( &format, p_dec->p_fifo->i_fourcc,
222 bih->biWidth, bih->biHeight,
223 bih->biWidth * VOUT_ASPECT_FACTOR / bih->biHeight );
224 p_sys->i_raw_size = format.i_bits_per_pixel *
225 format.i_width * format.i_height / 8;
231 /****************************************************************************
232 * RunDecoder: the whole thing
233 ****************************************************************************
234 * This function must be fed with ogg packets.
235 ****************************************************************************/
236 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
238 decoder_sys_t *p_sys = p_dec->p_sys;
242 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
244 /* We've just started the stream, wait for the first PTS. */
245 block_Release( p_block );
250 /* Date management */
251 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
253 p_sys->i_pts = p_block->i_pts;
256 if( p_block->i_buffer < p_sys->i_raw_size )
258 msg_Warn( p_dec, "invalid frame size (%d < %d)",
259 p_block->i_buffer, p_sys->i_raw_size );
261 block_Release( p_block );
265 if( p_sys->b_packetizer )
267 i_ret = SendFrame( p_dec, p_block );
271 i_ret = DecodeFrame( p_dec, p_block );
274 /* Date management: 1 frame per packet */
275 p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
277 block_Release( p_block );
281 /*****************************************************************************
283 *****************************************************************************/
284 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
286 uint8_t *p_src, *p_dst;
287 int i_src, i_plane, i_line, i_width;
289 p_src = p_block->p_buffer;
290 i_src = p_block->i_buffer;
292 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
294 p_dst = p_pic->p[i_plane].p_pixels;
295 i_width = p_pic->p[i_plane].i_visible_pitch;
297 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
299 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
301 p_dst += p_pic->p[i_plane].i_pitch;
306 /*****************************************************************************
307 * DecodeFrame: decodes a video frame.
308 *****************************************************************************/
309 static int DecodeFrame( decoder_t *p_dec, block_t *p_block )
311 decoder_sys_t *p_sys = p_dec->p_sys;
314 /* Get a new picture */
315 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
317 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
321 msleep( VOUT_OUTMEM_SLEEP );
323 if( !p_pic ) return VLC_EGENERIC;
325 FillPicture( p_dec, p_block, p_pic );
327 vout_DatePicture( p_sys->p_vout, p_pic, p_sys->i_pts );
328 vout_DisplayPicture( p_sys->p_vout, p_pic );
333 /*****************************************************************************
334 * SendFrame: send a video frame to the stream output.
335 *****************************************************************************/
336 static int SendFrame( decoder_t *p_dec, block_t *p_block )
338 decoder_sys_t *p_sys = p_dec->p_sys;
340 sout_buffer_t *p_sout_buffer =
341 sout_BufferNew( p_sys->p_sout_input->p_sout, p_block->i_buffer );
343 if( !p_sout_buffer ) return VLC_EGENERIC;
345 p_dec->p_vlc->pf_memcpy( p_sout_buffer->p_buffer,
346 p_block->p_buffer, p_block->i_buffer );
348 p_sout_buffer->i_dts = p_sout_buffer->i_pts = p_sys->i_pts;
350 sout_InputSendBuffer( p_sys->p_sout_input, p_sout_buffer );
355 /*****************************************************************************
356 * EndDecoder: decoder destruction
357 *****************************************************************************/
358 static int EndDecoder( decoder_t *p_dec )
360 decoder_sys_t *p_sys = p_dec->p_sys;
362 if( !p_sys->b_packetizer )
364 vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
367 if( p_sys->p_sout_input != NULL )
369 sout_InputDelete( p_sys->p_sout_input );