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.11 2004/01/07 19:20:29 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 *****************************************************************************/
28 #include <vlc/decoder.h>
30 /*****************************************************************************
31 * decoder_sys_t : raw video decoder descriptor
32 *****************************************************************************/
36 vlc_bool_t b_packetizer;
50 /****************************************************************************
52 ****************************************************************************/
53 static int OpenDecoder ( vlc_object_t * );
54 static int OpenPacketizer( vlc_object_t * );
55 static void CloseDecoder ( vlc_object_t * );
57 static void *DecodeBlock ( decoder_t *, block_t ** );
59 static picture_t *DecodeFrame( decoder_t *, block_t * );
60 static block_t *SendFrame ( decoder_t *, block_t * );
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( _("Pseudo raw video decoder") );
67 set_capability( "decoder", 50 );
68 set_callbacks( OpenDecoder, CloseDecoder );
71 set_description( _("Pseudo raw video packetizer") );
72 set_capability( "packetizer", 100 );
73 set_callbacks( OpenPacketizer, CloseDecoder );
76 /*****************************************************************************
77 * OpenDecoder: probe the decoder and return score
78 *****************************************************************************/
79 static int OpenDecoder( vlc_object_t *p_this )
81 decoder_t *p_dec = (decoder_t*)p_this;
84 switch( p_dec->fmt_in.i_codec )
87 case VLC_FOURCC('I','4','4','4'):
88 case VLC_FOURCC('I','4','2','2'):
89 case VLC_FOURCC('I','4','2','0'):
90 case VLC_FOURCC('Y','V','1','2'):
91 case VLC_FOURCC('I','Y','U','V'):
92 case VLC_FOURCC('I','4','1','1'):
93 case VLC_FOURCC('I','4','1','0'):
96 case VLC_FOURCC('Y','U','Y','2'):
97 case VLC_FOURCC('U','Y','V','Y'):
100 case VLC_FOURCC('R','V','3','2'):
101 case VLC_FOURCC('R','V','2','4'):
102 case VLC_FOURCC('R','V','1','6'):
103 case VLC_FOURCC('R','V','1','5'):
110 /* Allocate the memory needed to store the decoder's structure */
111 if( ( p_dec->p_sys = p_sys =
112 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
114 msg_Err( p_dec, "out of memory" );
118 p_dec->p_sys->b_packetizer = VLC_FALSE;
121 if( p_dec->fmt_in.video.i_width <= 0 ||
122 p_dec->fmt_in.video.i_height <= 0 )
124 msg_Err( p_dec, "invalid display size %dx%d",
125 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
129 /* Find out p_vdec->i_raw_size */
130 vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
131 p_dec->fmt_in.video.i_width,
132 p_dec->fmt_in.video.i_height,
133 p_dec->fmt_in.video.i_aspect );
134 p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
135 p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
137 /* Set output properties */
138 p_dec->fmt_out.i_cat = VIDEO_ES;
139 p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
140 //if( !p_dec->fmt_in.video.i_aspect )
142 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
143 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
147 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
149 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
155 static int OpenPacketizer( vlc_object_t *p_this )
157 decoder_t *p_dec = (decoder_t*)p_this;
159 int i_ret = OpenDecoder( p_this );
161 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
166 /****************************************************************************
167 * DecodeBlock: the whole thing
168 ****************************************************************************
169 * This function must be fed with complete frames.
170 ****************************************************************************/
171 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
173 decoder_sys_t *p_sys = p_dec->p_sys;
177 if( !pp_block || !*pp_block ) return NULL;
181 if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
183 /* We've just started the stream, wait for the first PTS. */
184 block_Release( p_block );
188 /* Date management */
189 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
191 if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
192 else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
195 if( p_block->i_buffer < p_sys->i_raw_size )
197 msg_Warn( p_dec, "invalid frame size (%d < %d)",
198 p_block->i_buffer, p_sys->i_raw_size );
200 block_Release( p_block );
204 if( p_sys->b_packetizer )
206 p_buf = SendFrame( p_dec, p_block );
210 p_buf = DecodeFrame( p_dec, p_block );
213 /* Date management: 1 frame per packet */
214 p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
220 /*****************************************************************************
222 *****************************************************************************/
223 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
225 uint8_t *p_src, *p_dst;
226 int i_src, i_plane, i_line, i_width;
228 p_src = p_block->p_buffer;
229 i_src = p_block->i_buffer;
231 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
233 p_dst = p_pic->p[i_plane].p_pixels;
234 i_width = p_pic->p[i_plane].i_visible_pitch;
236 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
238 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
240 p_dst += p_pic->p[i_plane].i_pitch;
245 /*****************************************************************************
246 * DecodeFrame: decodes a video frame.
247 *****************************************************************************/
248 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
250 decoder_sys_t *p_sys = p_dec->p_sys;
253 /* Get a new picture */
254 p_pic = p_dec->pf_vout_buffer_new( p_dec );
257 block_Release( p_block );
261 FillPicture( p_dec, p_block, p_pic );
263 p_pic->date = p_sys->i_pts;
265 block_Release( p_block );
269 /*****************************************************************************
270 * SendFrame: send a video frame to the stream output.
271 *****************************************************************************/
272 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
274 decoder_sys_t *p_sys = p_dec->p_sys;
276 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
281 /*****************************************************************************
282 * CloseDecoder: decoder destruction
283 *****************************************************************************/
284 static void CloseDecoder( vlc_object_t *p_this )
286 decoder_t *p_dec = (decoder_t*)p_this;
287 free( p_dec->p_sys );