1 /*****************************************************************************
2 * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc_codec.h>
31 /*****************************************************************************
32 * decoder_sys_t : raw video decoder descriptor
33 *****************************************************************************/
37 vlc_bool_t b_packetizer;
52 /****************************************************************************
54 ****************************************************************************/
55 static int OpenDecoder ( vlc_object_t * );
56 static int OpenPacketizer( vlc_object_t * );
57 static void CloseDecoder ( vlc_object_t * );
59 static void *DecodeBlock ( decoder_t *, block_t ** );
61 static picture_t *DecodeFrame( decoder_t *, block_t * );
62 static block_t *SendFrame ( decoder_t *, block_t * );
64 /*****************************************************************************
66 *****************************************************************************/
68 set_description( _("Pseudo raw video decoder") );
69 set_capability( "decoder", 50 );
70 set_category( CAT_INPUT );
71 set_subcategory( SUBCAT_INPUT_VCODEC );
72 set_callbacks( OpenDecoder, CloseDecoder );
75 set_description( _("Pseudo raw video packetizer") );
76 set_capability( "packetizer", 100 );
77 set_callbacks( OpenPacketizer, CloseDecoder );
80 /*****************************************************************************
81 * OpenDecoder: probe the decoder and return score
82 *****************************************************************************/
83 static int OpenDecoder( vlc_object_t *p_this )
85 decoder_t *p_dec = (decoder_t*)p_this;
88 switch( p_dec->fmt_in.i_codec )
91 case VLC_FOURCC('I','4','4','4'):
92 case VLC_FOURCC('I','4','2','2'):
93 case VLC_FOURCC('I','4','2','0'):
94 case VLC_FOURCC('Y','V','1','2'):
95 case VLC_FOURCC('I','Y','U','V'):
96 case VLC_FOURCC('I','4','1','1'):
97 case VLC_FOURCC('I','4','1','0'):
98 case VLC_FOURCC('Y','V','U','9'):
99 case VLC_FOURCC('Y','4','2','B'):
100 case VLC_FOURCC('Y','4','1','B'):
103 case VLC_FOURCC('Y','U','Y','2'):
104 case VLC_FOURCC('Y','8','0','0'):
105 case VLC_FOURCC('U','Y','V','Y'):
106 case VLC_FOURCC('H','D','Y','C'):
109 case VLC_FOURCC('R','V','3','2'):
110 case VLC_FOURCC('R','V','2','4'):
111 case VLC_FOURCC('R','V','1','6'):
112 case VLC_FOURCC('R','V','1','5'):
114 case VLC_FOURCC('2','V','u','y'):
115 case VLC_FOURCC('2','v','u','y'):
116 case VLC_FOURCC('A','V','U','I'):
117 p_dec->fmt_in.i_codec = VLC_FOURCC('U','Y','V','Y');
119 case VLC_FOURCC('y','v','1','2'):
120 p_dec->fmt_in.i_codec = VLC_FOURCC('Y','V','1','2');
127 /* Allocate the memory needed to store the decoder's structure */
128 if( ( p_dec->p_sys = p_sys =
129 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
131 msg_Err( p_dec, "out of memory" );
135 p_dec->p_sys->b_packetizer = VLC_FALSE;
139 if( (int)p_dec->fmt_in.video.i_height < 0 )
141 /* Frames are coded from bottom to top */
142 p_dec->fmt_in.video.i_height =
143 (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
144 p_sys->b_invert = VLC_TRUE;
147 if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
149 msg_Err( p_dec, "invalid display size %dx%d",
150 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
154 /* Find out p_vdec->i_raw_size */
155 vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
156 p_dec->fmt_in.video.i_width,
157 p_dec->fmt_in.video.i_height,
158 p_dec->fmt_in.video.i_aspect );
159 p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
160 p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
162 /* Set output properties */
163 p_dec->fmt_out.i_cat = VIDEO_ES;
164 p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
165 if( !p_dec->fmt_in.video.i_aspect )
167 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
168 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
170 else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
172 if( p_dec->fmt_in.video.i_rmask )
173 p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask;
174 if( p_dec->fmt_in.video.i_gmask )
175 p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask;
176 if( p_dec->fmt_in.video.i_bmask )
177 p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask;
180 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
182 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
188 static int OpenPacketizer( vlc_object_t *p_this )
190 decoder_t *p_dec = (decoder_t*)p_this;
192 int i_ret = OpenDecoder( p_this );
194 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
199 /****************************************************************************
200 * DecodeBlock: the whole thing
201 ****************************************************************************
202 * This function must be fed with complete frames.
203 ****************************************************************************/
204 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
206 decoder_sys_t *p_sys = p_dec->p_sys;
210 if( !pp_block || !*pp_block ) return NULL;
214 if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
216 /* We've just started the stream, wait for the first PTS. */
217 block_Release( p_block );
221 /* Date management */
222 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
224 if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
225 else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
228 if( p_block->i_buffer < p_sys->i_raw_size )
230 msg_Warn( p_dec, "invalid frame size (%d < %d)",
231 p_block->i_buffer, p_sys->i_raw_size );
233 block_Release( p_block );
237 if( p_sys->b_packetizer )
239 p_buf = SendFrame( p_dec, p_block );
243 p_buf = DecodeFrame( p_dec, p_block );
246 /* Date management: 1 frame per packet */
247 p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
253 /*****************************************************************************
255 *****************************************************************************/
256 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
258 uint8_t *p_src, *p_dst;
259 int i_src, i_plane, i_line, i_width;
260 decoder_sys_t *p_sys = p_dec->p_sys;
262 p_src = p_block->p_buffer;
263 i_src = p_block->i_buffer;
265 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
267 p_dst = p_pic->p[i_plane].p_pixels;
268 i_width = p_pic->p[i_plane].i_pitch;
270 if( p_sys->b_invert )
271 p_src += (i_width * (p_pic->p[i_plane].i_visible_lines - 1));
273 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
275 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
276 p_src += p_sys->b_invert ? -i_width : i_width;
280 if( p_sys->b_invert )
281 p_src += (i_width * (p_pic->p[i_plane].i_visible_lines + 1));
285 /*****************************************************************************
286 * DecodeFrame: decodes a video frame.
287 *****************************************************************************/
288 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
290 decoder_sys_t *p_sys = p_dec->p_sys;
293 /* Get a new picture */
294 p_pic = p_dec->pf_vout_buffer_new( p_dec );
297 block_Release( p_block );
301 FillPicture( p_dec, p_block, p_pic );
303 p_pic->date = p_sys->i_pts;
305 block_Release( p_block );
309 /*****************************************************************************
310 * SendFrame: send a video frame to the stream output.
311 *****************************************************************************/
312 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
314 decoder_sys_t *p_sys = p_dec->p_sys;
316 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
318 if( p_sys->b_invert )
321 uint8_t *p_tmp, *p_pixels;
324 /* Fill in picture_t fields */
325 vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
326 p_dec->fmt_out.video.i_width,
327 p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
331 msg_Err( p_dec, "unsupported chroma" );
335 p_tmp = malloc( pic.p[0].i_pitch );
336 p_pixels = p_block->p_buffer;
337 for( i = 0; i < pic.i_planes; i++ )
339 int i_pitch = pic.p[i].i_pitch;
340 uint8_t *p_top = p_pixels;
341 uint8_t *p_bottom = p_pixels + i_pitch *
342 (pic.p[i].i_visible_lines - 1);
344 for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
346 p_dec->p_libvlc->pf_memcpy( p_tmp, p_bottom,
347 pic.p[i].i_visible_pitch );
348 p_dec->p_libvlc->pf_memcpy( p_bottom, p_top,
349 pic.p[i].i_visible_pitch );
350 p_dec->p_libvlc->pf_memcpy( p_top, p_tmp,
351 pic.p[i].i_visible_pitch );
356 p_pixels += i_pitch * pic.p[i].i_lines;
364 /*****************************************************************************
365 * CloseDecoder: decoder destruction
366 *****************************************************************************/
367 static void CloseDecoder( vlc_object_t *p_this )
369 decoder_t *p_dec = (decoder_t*)p_this;
370 free( p_dec->p_sys );