1 /*****************************************************************************
2 * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
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>
31 /*****************************************************************************
32 * decoder_sys_t : raw video decoder descriptor
33 *****************************************************************************/
37 vlc_bool_t b_packetizer;
51 /****************************************************************************
53 ****************************************************************************/
54 static int OpenDecoder ( vlc_object_t * );
55 static int OpenPacketizer( vlc_object_t * );
56 static void CloseDecoder ( vlc_object_t * );
58 static void *DecodeBlock ( decoder_t *, block_t ** );
60 static picture_t *DecodeFrame( decoder_t *, block_t * );
61 static block_t *SendFrame ( decoder_t *, block_t * );
63 /*****************************************************************************
65 *****************************************************************************/
67 set_description( _("Pseudo raw video decoder") );
68 set_capability( "decoder", 50 );
69 set_callbacks( OpenDecoder, CloseDecoder );
72 set_description( _("Pseudo raw video packetizer") );
73 set_capability( "packetizer", 100 );
74 set_callbacks( OpenPacketizer, CloseDecoder );
77 /*****************************************************************************
78 * OpenDecoder: probe the decoder and return score
79 *****************************************************************************/
80 static int OpenDecoder( vlc_object_t *p_this )
82 decoder_t *p_dec = (decoder_t*)p_this;
85 switch( p_dec->fmt_in.i_codec )
88 case VLC_FOURCC('I','4','4','4'):
89 case VLC_FOURCC('I','4','2','2'):
90 case VLC_FOURCC('I','4','2','0'):
91 case VLC_FOURCC('Y','V','1','2'):
92 case VLC_FOURCC('I','Y','U','V'):
93 case VLC_FOURCC('I','4','1','1'):
94 case VLC_FOURCC('I','4','1','0'):
95 case VLC_FOURCC('Y','V','U','9'):
98 case VLC_FOURCC('Y','U','Y','2'):
99 case VLC_FOURCC('U','Y','V','Y'):
102 case VLC_FOURCC('R','V','3','2'):
103 case VLC_FOURCC('R','V','2','4'):
104 case VLC_FOURCC('R','V','1','6'):
105 case VLC_FOURCC('R','V','1','5'):
112 /* Allocate the memory needed to store the decoder's structure */
113 if( ( p_dec->p_sys = p_sys =
114 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
116 msg_Err( p_dec, "out of memory" );
120 p_dec->p_sys->b_packetizer = VLC_FALSE;
123 if( p_dec->fmt_in.video.i_width <= 0 ||
124 p_dec->fmt_in.video.i_height <= 0 )
126 msg_Err( p_dec, "invalid display size %dx%d",
127 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
131 /* Find out p_vdec->i_raw_size */
132 vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
133 p_dec->fmt_in.video.i_width,
134 p_dec->fmt_in.video.i_height,
135 p_dec->fmt_in.video.i_aspect );
136 p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
137 p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
139 /* Set output properties */
140 p_dec->fmt_out.i_cat = VIDEO_ES;
141 p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
142 if( !p_dec->fmt_in.video.i_aspect )
144 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
145 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
147 else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
149 if( p_dec->fmt_in.video.i_rmask )
150 p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask;
151 if( p_dec->fmt_in.video.i_gmask )
152 p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask;
153 if( p_dec->fmt_in.video.i_bmask )
154 p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask;
157 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
159 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
165 static int OpenPacketizer( vlc_object_t *p_this )
167 decoder_t *p_dec = (decoder_t*)p_this;
169 int i_ret = OpenDecoder( p_this );
171 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
176 /****************************************************************************
177 * DecodeBlock: the whole thing
178 ****************************************************************************
179 * This function must be fed with complete frames.
180 ****************************************************************************/
181 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
183 decoder_sys_t *p_sys = p_dec->p_sys;
187 if( !pp_block || !*pp_block ) return NULL;
191 if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
193 /* We've just started the stream, wait for the first PTS. */
194 block_Release( p_block );
198 /* Date management */
199 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
201 if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
202 else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
205 if( p_block->i_buffer < p_sys->i_raw_size )
207 msg_Warn( p_dec, "invalid frame size (%d < %d)",
208 p_block->i_buffer, p_sys->i_raw_size );
210 block_Release( p_block );
214 if( p_sys->b_packetizer )
216 p_buf = SendFrame( p_dec, p_block );
220 p_buf = DecodeFrame( p_dec, p_block );
223 /* Date management: 1 frame per packet */
224 p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
230 /*****************************************************************************
232 *****************************************************************************/
233 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
235 uint8_t *p_src, *p_dst;
236 int i_src, i_plane, i_line, i_width;
238 p_src = p_block->p_buffer;
239 i_src = p_block->i_buffer;
241 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
243 p_dst = p_pic->p[i_plane].p_pixels;
244 i_width = p_pic->p[i_plane].i_visible_pitch;
246 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
248 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
250 p_dst += p_pic->p[i_plane].i_pitch;
255 /*****************************************************************************
256 * DecodeFrame: decodes a video frame.
257 *****************************************************************************/
258 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
260 decoder_sys_t *p_sys = p_dec->p_sys;
263 /* Get a new picture */
264 p_pic = p_dec->pf_vout_buffer_new( p_dec );
267 block_Release( p_block );
271 FillPicture( p_dec, p_block, p_pic );
273 p_pic->date = p_sys->i_pts;
275 block_Release( p_block );
279 /*****************************************************************************
280 * SendFrame: send a video frame to the stream output.
281 *****************************************************************************/
282 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
284 decoder_sys_t *p_sys = p_dec->p_sys;
286 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
291 /*****************************************************************************
292 * CloseDecoder: decoder destruction
293 *****************************************************************************/
294 static void CloseDecoder( vlc_object_t *p_this )
296 decoder_t *p_dec = (decoder_t*)p_this;
297 free( p_dec->p_sys );