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>
35 /*****************************************************************************
36 * decoder_sys_t : raw video decoder descriptor
37 *****************************************************************************/
55 /****************************************************************************
57 ****************************************************************************/
58 static int OpenDecoder ( vlc_object_t * );
59 static int OpenPacketizer( vlc_object_t * );
60 static void CloseDecoder ( vlc_object_t * );
62 static void *DecodeBlock ( decoder_t *, block_t ** );
64 static picture_t *DecodeFrame( decoder_t *, block_t * );
65 static block_t *SendFrame ( decoder_t *, block_t * );
67 /*****************************************************************************
69 *****************************************************************************/
71 set_description( N_("Pseudo raw video decoder") )
72 set_capability( "decoder", 50 )
73 set_category( CAT_INPUT )
74 set_subcategory( SUBCAT_INPUT_VCODEC )
75 set_callbacks( OpenDecoder, CloseDecoder )
78 set_description( N_("Pseudo raw video packetizer") )
79 set_capability( "packetizer", 100 )
80 set_callbacks( OpenPacketizer, CloseDecoder )
83 /*****************************************************************************
84 * OpenDecoder: probe the decoder and return score
85 *****************************************************************************/
86 static int OpenDecoder( vlc_object_t *p_this )
88 decoder_t *p_dec = (decoder_t*)p_this;
91 switch( p_dec->fmt_in.i_codec )
114 case VLC_CODEC_RGB32:
115 case VLC_CODEC_RGB24:
116 case VLC_CODEC_RGB16:
117 case VLC_CODEC_RGB15:
126 /* Allocate the memory needed to store the decoder's structure */
127 if( ( p_dec->p_sys = p_sys =
128 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
131 p_dec->p_sys->b_packetizer = false;
134 if( (int)p_dec->fmt_in.video.i_height < 0 )
136 /* Frames are coded from bottom to top */
137 p_dec->fmt_in.video.i_height =
138 (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
139 p_sys->b_invert = true;
142 if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
144 msg_Err( p_dec, "invalid display size %dx%d",
145 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
149 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
151 date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
152 p_dec->fmt_out.video.i_frame_rate_base );
153 if( p_dec->fmt_out.video.i_frame_rate == 0 ||
154 p_dec->fmt_out.video.i_frame_rate_base == 0)
156 msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
157 p_dec->fmt_out.video.i_frame_rate,
158 p_dec->fmt_out.video.i_frame_rate_base);
159 date_Init( &p_sys->pts, 25, 1 );
162 /* Find out p_vdec->i_raw_size */
163 video_format_Setup( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
164 p_dec->fmt_in.video.i_width,
165 p_dec->fmt_in.video.i_height,
166 p_dec->fmt_in.video.i_sar_num,
167 p_dec->fmt_in.video.i_sar_den );
169 picture_Setup( &picture, p_dec->fmt_out.i_codec,
170 p_dec->fmt_in.video.i_width,
171 p_dec->fmt_in.video.i_height, 0, 1 );
172 p_sys->i_raw_size = 0;
173 for( int i = 0; i < picture.i_planes; i++ )
174 p_sys->i_raw_size += picture.p[i].i_visible_pitch *
175 picture.p[i].i_visible_lines;
177 if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
179 p_dec->fmt_out.video.i_sar_num = 1;
180 p_dec->fmt_out.video.i_sar_den = 1;
184 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
186 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
192 static int OpenPacketizer( vlc_object_t *p_this )
194 decoder_t *p_dec = (decoder_t*)p_this;
196 int i_ret = OpenDecoder( p_this );
198 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
203 /****************************************************************************
204 * DecodeBlock: the whole thing
205 ****************************************************************************
206 * This function must be fed with complete frames.
207 ****************************************************************************/
208 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
210 decoder_sys_t *p_sys = p_dec->p_sys;
214 if( !pp_block || !*pp_block ) return NULL;
219 if( p_block->i_pts <= VLC_TS_INVALID && p_block->i_dts <= VLC_TS_INVALID &&
220 !date_Get( &p_sys->pts ) )
222 /* We've just started the stream, wait for the first PTS. */
223 block_Release( p_block );
227 /* Date management: If there is a pts avaliable, use that. */
228 if( p_block->i_pts > VLC_TS_INVALID )
230 date_Set( &p_sys->pts, p_block->i_pts );
232 else if( p_block->i_dts > VLC_TS_INVALID )
234 /* NB, davidf doesn't quite agree with this in general, it is ok
235 * for rawvideo since it is in order (ie pts=dts), however, it
236 * may not be ok for an out-of-order codec, so don't copy this
237 * without thinking */
238 date_Set( &p_sys->pts, p_block->i_dts );
241 if( p_block->i_buffer < p_sys->i_raw_size )
243 msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
244 p_block->i_buffer, p_sys->i_raw_size );
246 block_Release( p_block );
250 if( p_sys->b_packetizer )
252 p_buf = SendFrame( p_dec, p_block );
256 p_buf = DecodeFrame( p_dec, p_block );
259 /* Date management: 1 frame per packet */
260 date_Increment( &p_sys->pts, 1 );
266 /*****************************************************************************
268 *****************************************************************************/
269 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
272 decoder_sys_t *p_sys = p_dec->p_sys;
273 uint8_t *p_src = p_block->p_buffer;
275 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
277 int i_pitch = p_pic->p[i_plane].i_pitch;
278 int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
279 int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
280 uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
281 uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
283 if( p_sys->b_invert )
284 for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
285 p_dst_end -= i_pitch, p_src += i_visible_pitch )
286 vlc_memcpy( p_dst_end, p_src, i_visible_pitch );
288 for( ; p_dst < p_dst_end;
289 p_dst += i_pitch, p_src += i_visible_pitch )
290 vlc_memcpy( p_dst, p_src, i_visible_pitch );
294 /*****************************************************************************
295 * DecodeFrame: decodes a video frame.
296 *****************************************************************************/
297 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
299 decoder_sys_t *p_sys = p_dec->p_sys;
302 /* Get a new picture */
303 p_pic = decoder_NewPicture( p_dec );
306 block_Release( p_block );
310 FillPicture( p_dec, p_block, p_pic );
312 p_pic->date = date_Get( &p_sys->pts );
313 if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK )
315 p_pic->b_progressive = false;
316 p_pic->i_nb_fields = 2;
317 if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST )
318 p_pic->b_top_field_first = true;
320 p_pic->b_top_field_first = false;
323 p_pic->b_progressive = true;
325 block_Release( p_block );
329 /*****************************************************************************
330 * SendFrame: send a video frame to the stream output.
331 *****************************************************************************/
332 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
334 decoder_sys_t *p_sys = p_dec->p_sys;
336 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
338 if( p_sys->b_invert )
341 uint8_t *p_tmp, *p_pixels;
344 /* Fill in picture_t fields */
345 picture_Setup( &pic, p_dec->fmt_out.i_codec,
346 p_dec->fmt_out.video.i_width,
347 p_dec->fmt_out.video.i_height, 0, 1 );
351 msg_Err( p_dec, "unsupported chroma" );
355 p_tmp = malloc( pic.p[0].i_pitch );
358 p_pixels = p_block->p_buffer;
359 for( i = 0; i < pic.i_planes; i++ )
361 int i_pitch = pic.p[i].i_pitch;
362 uint8_t *p_top = p_pixels;
363 uint8_t *p_bottom = p_pixels + i_pitch *
364 (pic.p[i].i_visible_lines - 1);
366 for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
368 vlc_memcpy( p_tmp, p_bottom, pic.p[i].i_visible_pitch );
369 vlc_memcpy( p_bottom, p_top, pic.p[i].i_visible_pitch );
370 vlc_memcpy( p_top, p_tmp, pic.p[i].i_visible_pitch );
375 p_pixels += i_pitch * pic.p[i].i_lines;
383 /*****************************************************************************
384 * CloseDecoder: decoder destruction
385 *****************************************************************************/
386 static void CloseDecoder( vlc_object_t *p_this )
388 decoder_t *p_dec = (decoder_t*)p_this;
389 free( p_dec->p_sys );