1 /*****************************************************************************
2 * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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 *****************************************************************************/
48 plane_t planes[PICTURE_PLANE_MAX];
56 /****************************************************************************
58 ****************************************************************************/
59 static int OpenDecoder ( vlc_object_t * );
60 static int OpenPacketizer( vlc_object_t * );
61 static void CloseDecoder ( vlc_object_t * );
63 static void *DecodeBlock ( decoder_t *, block_t ** );
65 static picture_t *DecodeFrame( decoder_t *, block_t * );
66 static block_t *SendFrame ( decoder_t *, block_t * );
68 /*****************************************************************************
70 *****************************************************************************/
72 set_description( N_("Pseudo raw video decoder") )
73 set_capability( "decoder", 50 )
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_VCODEC )
76 set_callbacks( OpenDecoder, CloseDecoder )
79 set_description( N_("Pseudo raw video packetizer") )
80 set_capability( "packetizer", 100 )
81 set_callbacks( OpenPacketizer, CloseDecoder )
84 /*****************************************************************************
85 * OpenDecoder: probe the decoder and return score
86 *****************************************************************************/
87 static int OpenDecoder( vlc_object_t *p_this )
89 decoder_t *p_dec = (decoder_t*)p_this;
92 switch( p_dec->fmt_in.i_codec )
111 case VLC_CODEC_I422_10L:
112 case VLC_CODEC_I422_10B:
121 case VLC_CODEC_RGB32:
122 case VLC_CODEC_RGB24:
123 case VLC_CODEC_RGB16:
124 case VLC_CODEC_RGB15:
135 /* Allocate the memory needed to store the decoder's structure */
136 if( ( p_dec->p_sys = p_sys =
137 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
140 p_dec->p_sys->b_packetizer = false;
141 p_sys->b_invert = false;
143 if( (int)p_dec->fmt_in.video.i_height < 0 )
145 /* Frames are coded from bottom to top */
146 p_dec->fmt_in.video.i_height =
147 (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
148 p_sys->b_invert = true;
150 if( !p_dec->fmt_in.video.i_visible_width )
151 p_dec->fmt_in.video.i_visible_width = p_dec->fmt_in.video.i_width;
152 if( !p_dec->fmt_in.video.i_visible_height )
153 p_dec->fmt_in.video.i_visible_height = p_dec->fmt_in.video.i_height;
155 if( p_dec->fmt_in.video.i_visible_width <= 0
156 || p_dec->fmt_in.video.i_visible_height <= 0 )
158 msg_Err( p_dec, "invalid display size %dx%d",
159 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
163 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
165 date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
166 p_dec->fmt_out.video.i_frame_rate_base );
167 if( p_dec->fmt_out.video.i_frame_rate == 0 ||
168 p_dec->fmt_out.video.i_frame_rate_base == 0)
170 msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
171 p_dec->fmt_out.video.i_frame_rate,
172 p_dec->fmt_out.video.i_frame_rate_base);
173 date_Init( &p_sys->pts, 25, 1 );
176 /* Find out p_vdec->i_raw_size */
177 video_format_Setup( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
178 p_dec->fmt_in.video.i_visible_width,
179 p_dec->fmt_in.video.i_visible_height,
180 p_dec->fmt_in.video.i_sar_num,
181 p_dec->fmt_in.video.i_sar_den );
183 picture_Setup( &picture, p_dec->fmt_out.i_codec,
184 p_dec->fmt_in.video.i_width,
185 p_dec->fmt_in.video.i_height, 0, 1 );
186 p_sys->i_raw_size = 0;
187 for( int i = 0; i < picture.i_planes; i++ )
189 p_sys->i_raw_size += picture.p[i].i_visible_pitch *
190 picture.p[i].i_visible_lines;
191 p_sys->planes[i] = picture.p[i];
194 if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
196 p_dec->fmt_out.video.i_sar_num = 1;
197 p_dec->fmt_out.video.i_sar_den = 1;
201 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
203 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
209 static int OpenPacketizer( vlc_object_t *p_this )
211 decoder_t *p_dec = (decoder_t*)p_this;
213 int i_ret = OpenDecoder( p_this );
215 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
220 /****************************************************************************
221 * DecodeBlock: the whole thing
222 ****************************************************************************
223 * This function must be fed with complete frames.
224 ****************************************************************************/
225 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
227 decoder_sys_t *p_sys = p_dec->p_sys;
231 if( !pp_block || !*pp_block ) return NULL;
236 if( p_block->i_pts <= VLC_TS_INVALID && p_block->i_dts <= VLC_TS_INVALID &&
237 !date_Get( &p_sys->pts ) )
239 /* We've just started the stream, wait for the first PTS. */
240 block_Release( p_block );
244 /* Date management: If there is a pts avaliable, use that. */
245 if( p_block->i_pts > VLC_TS_INVALID )
247 date_Set( &p_sys->pts, p_block->i_pts );
249 else if( p_block->i_dts > VLC_TS_INVALID )
251 /* NB, davidf doesn't quite agree with this in general, it is ok
252 * for rawvideo since it is in order (ie pts=dts), however, it
253 * may not be ok for an out-of-order codec, so don't copy this
254 * without thinking */
255 date_Set( &p_sys->pts, p_block->i_dts );
258 if( p_block->i_buffer < p_sys->i_raw_size )
260 msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
261 p_block->i_buffer, p_sys->i_raw_size );
263 block_Release( p_block );
267 if( p_sys->b_packetizer )
269 p_buf = SendFrame( p_dec, p_block );
273 p_buf = DecodeFrame( p_dec, p_block );
276 /* Date management: 1 frame per packet */
277 date_Increment( &p_sys->pts, 1 );
283 /*****************************************************************************
285 *****************************************************************************/
286 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
289 decoder_sys_t *p_sys = p_dec->p_sys;
290 uint8_t *p_src = p_block->p_buffer;
292 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
294 int i_pitch = p_pic->p[i_plane].i_pitch;
295 int i_visible_pitch = p_sys->planes[i_plane].i_visible_pitch;
296 int i_visible_lines = p_sys->planes[i_plane].i_visible_lines;
297 uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
298 uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
300 if( p_sys->b_invert )
301 for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
302 p_dst_end -= i_pitch, p_src += i_visible_pitch )
303 memcpy( p_dst_end, p_src, i_visible_pitch );
305 for( ; p_dst < p_dst_end;
306 p_dst += i_pitch, p_src += i_visible_pitch )
307 memcpy( p_dst, p_src, i_visible_pitch );
311 /*****************************************************************************
312 * DecodeFrame: decodes a video frame.
313 *****************************************************************************/
314 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
316 decoder_sys_t *p_sys = p_dec->p_sys;
319 /* Get a new picture */
320 p_pic = decoder_NewPicture( p_dec );
323 block_Release( p_block );
327 FillPicture( p_dec, p_block, p_pic );
329 p_pic->date = date_Get( &p_sys->pts );
330 if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK )
332 p_pic->b_progressive = false;
333 p_pic->i_nb_fields = 2;
334 if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST )
335 p_pic->b_top_field_first = true;
337 p_pic->b_top_field_first = false;
340 p_pic->b_progressive = true;
342 block_Release( p_block );
346 /*****************************************************************************
347 * SendFrame: send a video frame to the stream output.
348 *****************************************************************************/
349 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
351 decoder_sys_t *p_sys = p_dec->p_sys;
353 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
355 if( p_sys->b_invert )
358 uint8_t *p_tmp, *p_pixels;
361 /* Fill in picture_t fields */
362 picture_Setup( &pic, p_dec->fmt_out.i_codec,
363 p_dec->fmt_out.video.i_width,
364 p_dec->fmt_out.video.i_height, 0, 1 );
368 msg_Err( p_dec, "unsupported chroma" );
372 p_tmp = malloc( pic.p[0].i_pitch );
375 p_pixels = p_block->p_buffer;
376 for( i = 0; i < pic.i_planes; i++ )
378 int i_pitch = pic.p[i].i_pitch;
379 uint8_t *p_top = p_pixels;
380 uint8_t *p_bottom = p_pixels + i_pitch *
381 (pic.p[i].i_visible_lines - 1);
383 for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
385 memcpy( p_tmp, p_bottom, pic.p[i].i_visible_pitch );
386 memcpy( p_bottom, p_top, pic.p[i].i_visible_pitch );
387 memcpy( p_top, p_tmp, pic.p[i].i_visible_pitch );
392 p_pixels += i_pitch * pic.p[i].i_lines;
400 /*****************************************************************************
401 * CloseDecoder: decoder destruction
402 *****************************************************************************/
403 static void CloseDecoder( vlc_object_t *p_this )
405 decoder_t *p_dec = (decoder_t*)p_this;
406 free( p_dec->p_sys );