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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
36 /*****************************************************************************
37 * decoder_sys_t : raw video decoder descriptor
38 *****************************************************************************/
57 /****************************************************************************
59 ****************************************************************************/
60 static int OpenDecoder ( vlc_object_t * );
61 static int OpenPacketizer( vlc_object_t * );
62 static void CloseDecoder ( vlc_object_t * );
64 static void *DecodeBlock ( decoder_t *, block_t ** );
66 static picture_t *DecodeFrame( decoder_t *, block_t * );
67 static block_t *SendFrame ( decoder_t *, block_t * );
69 /*****************************************************************************
71 *****************************************************************************/
73 set_description( N_("Pseudo raw video decoder") )
74 set_capability( "decoder", 50 )
75 set_category( CAT_INPUT )
76 set_subcategory( SUBCAT_INPUT_VCODEC )
77 set_callbacks( OpenDecoder, CloseDecoder )
80 set_description( N_("Pseudo raw video packetizer") )
81 set_capability( "packetizer", 100 )
82 set_callbacks( OpenPacketizer, CloseDecoder )
85 /*****************************************************************************
86 * OpenDecoder: probe the decoder and return score
87 *****************************************************************************/
88 static int OpenDecoder( vlc_object_t *p_this )
90 decoder_t *p_dec = (decoder_t*)p_this;
93 switch( p_dec->fmt_in.i_codec )
96 case VLC_FOURCC('I','4','4','4'):
97 case VLC_FOURCC('I','4','2','2'):
98 case VLC_FOURCC('I','4','2','0'):
99 case VLC_FOURCC('Y','V','1','2'):
100 case VLC_FOURCC('I','Y','U','V'):
101 case VLC_FOURCC('I','4','1','1'):
102 case VLC_FOURCC('I','4','1','0'):
103 case VLC_FOURCC('Y','V','U','9'):
104 case VLC_FOURCC('Y','4','2','B'):
105 case VLC_FOURCC('Y','4','1','B'):
108 case VLC_FOURCC('Y','U','Y','2'):
109 case VLC_FOURCC('Y','8','0','0'):
110 case VLC_FOURCC('U','Y','V','Y'):
111 case VLC_FOURCC('H','D','Y','C'):
114 case VLC_FOURCC('R','V','3','2'):
115 case VLC_FOURCC('R','V','2','4'):
116 case VLC_FOURCC('R','V','1','6'):
117 case VLC_FOURCC('R','V','1','5'):
119 case VLC_FOURCC('2','V','u','y'):
120 case VLC_FOURCC('2','v','u','y'):
121 case VLC_FOURCC('A','V','U','I'):
122 p_dec->fmt_in.i_codec = VLC_FOURCC('U','Y','V','Y');
124 case VLC_FOURCC('y','v','1','2'):
125 p_dec->fmt_in.i_codec = VLC_FOURCC('Y','V','1','2');
132 /* Allocate the memory needed to store the decoder's structure */
133 if( ( p_dec->p_sys = p_sys =
134 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
137 p_dec->p_sys->b_packetizer = false;
141 if( (int)p_dec->fmt_in.video.i_height < 0 )
143 /* Frames are coded from bottom to top */
144 p_dec->fmt_in.video.i_height =
145 (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
146 p_sys->b_invert = true;
149 if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
151 msg_Err( p_dec, "invalid display size %dx%d",
152 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
156 /* Find out p_vdec->i_raw_size */
157 vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
158 p_dec->fmt_in.video.i_width,
159 p_dec->fmt_in.video.i_height,
160 p_dec->fmt_in.video.i_aspect );
161 p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
162 p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
164 /* Set output properties */
165 p_dec->fmt_out.i_cat = VIDEO_ES;
166 p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
167 if( !p_dec->fmt_in.video.i_aspect )
169 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
170 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
172 else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
174 if( p_dec->fmt_in.video.i_rmask )
175 p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask;
176 if( p_dec->fmt_in.video.i_gmask )
177 p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask;
178 if( p_dec->fmt_in.video.i_bmask )
179 p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask;
182 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
184 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
190 static int OpenPacketizer( vlc_object_t *p_this )
192 decoder_t *p_dec = (decoder_t*)p_this;
194 int i_ret = OpenDecoder( p_this );
196 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
201 /****************************************************************************
202 * DecodeBlock: the whole thing
203 ****************************************************************************
204 * This function must be fed with complete frames.
205 ****************************************************************************/
206 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
208 decoder_sys_t *p_sys = p_dec->p_sys;
212 if( !pp_block || !*pp_block ) return NULL;
216 if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
218 /* We've just started the stream, wait for the first PTS. */
219 block_Release( p_block );
223 /* Date management */
224 if( p_block->i_pts > 0 || p_block->i_dts > 0 )
226 if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
227 else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
230 if( p_block->i_buffer < p_sys->i_raw_size )
232 msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
233 p_block->i_buffer, p_sys->i_raw_size );
235 block_Release( p_block );
239 if( p_sys->b_packetizer )
241 p_buf = SendFrame( p_dec, p_block );
245 p_buf = DecodeFrame( p_dec, p_block );
248 /* Date management: 1 frame per packet */
249 p_sys->i_pts += ( INT64_C(1000000) * 1.0 / 25 /*FIXME*/ );
255 /*****************************************************************************
257 *****************************************************************************/
258 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
261 decoder_sys_t *p_sys = p_dec->p_sys;
262 uint8_t *p_src = p_block->p_buffer;
264 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
266 int i_pitch = p_pic->p[i_plane].i_pitch;
267 int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
268 int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
269 uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
270 uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
272 if( p_sys->b_invert )
273 for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
274 p_dst_end -= i_pitch, p_src += i_visible_pitch )
275 vlc_memcpy( p_dst_end, p_src, i_visible_pitch );
277 for( ; p_dst < p_dst_end;
278 p_dst += i_pitch, p_src += i_visible_pitch )
279 vlc_memcpy( p_dst, p_src, i_visible_pitch );
283 /*****************************************************************************
284 * DecodeFrame: decodes a video frame.
285 *****************************************************************************/
286 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
288 decoder_sys_t *p_sys = p_dec->p_sys;
291 /* Get a new picture */
292 p_pic = decoder_NewPicture( p_dec );
295 block_Release( p_block );
299 FillPicture( p_dec, p_block, p_pic );
301 p_pic->date = p_sys->i_pts;
303 block_Release( p_block );
307 /*****************************************************************************
308 * SendFrame: send a video frame to the stream output.
309 *****************************************************************************/
310 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
312 decoder_sys_t *p_sys = p_dec->p_sys;
314 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
316 if( p_sys->b_invert )
319 uint8_t *p_tmp, *p_pixels;
322 /* Fill in picture_t fields */
323 vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
324 p_dec->fmt_out.video.i_width,
325 p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
329 msg_Err( p_dec, "unsupported chroma" );
333 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 vlc_memcpy( p_tmp, p_bottom, pic.p[i].i_visible_pitch );
347 vlc_memcpy( p_bottom, p_top, pic.p[i].i_visible_pitch );
348 vlc_memcpy( p_top, p_tmp, pic.p[i].i_visible_pitch );
353 p_pixels += i_pitch * pic.p[i].i_lines;
361 /*****************************************************************************
362 * CloseDecoder: decoder destruction
363 *****************************************************************************/
364 static void CloseDecoder( vlc_object_t *p_this )
366 decoder_t *p_dec = (decoder_t*)p_this;
367 free( p_dec->p_sys );