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 *****************************************************************************/
49 unsigned pitches[PICTURE_PLANE_MAX];
50 unsigned lines[PICTURE_PLANE_MAX];
58 /****************************************************************************
60 ****************************************************************************/
61 static int OpenDecoder ( vlc_object_t * );
62 static int OpenPacketizer( vlc_object_t * );
63 static void CloseDecoder ( vlc_object_t * );
65 static void *DecodeBlock ( decoder_t *, block_t ** );
67 static picture_t *DecodeFrame( decoder_t *, block_t * );
68 static block_t *SendFrame ( decoder_t *, block_t * );
70 /*****************************************************************************
72 *****************************************************************************/
74 set_description( N_("Pseudo raw video decoder") )
75 set_capability( "decoder", 50 )
76 set_category( CAT_INPUT )
77 set_subcategory( SUBCAT_INPUT_VCODEC )
78 set_callbacks( OpenDecoder, CloseDecoder )
81 set_description( N_("Pseudo raw video packetizer") )
82 set_capability( "packetizer", 100 )
83 set_callbacks( OpenPacketizer, CloseDecoder )
86 /*****************************************************************************
87 * OpenDecoder: probe the decoder and return score
88 *****************************************************************************/
89 static int OpenDecoder( vlc_object_t *p_this )
91 decoder_t *p_dec = (decoder_t*)p_this;
93 const vlc_chroma_description_t *dsc =
94 vlc_fourcc_GetChromaDescription( p_dec->fmt_in.i_codec );
95 if( dsc == NULL || dsc->plane_count == 0 )
98 if( p_dec->fmt_in.video.i_visible_width < 0
99 || p_dec->fmt_in.video.i_visible_height < 0 )
101 msg_Err( p_dec, "invalid display size %dx%d",
102 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
106 /* Allocate the memory needed to store the decoder's structure */
107 decoder_sys_t *p_sys = calloc(1, sizeof(*p_sys));
108 if( unlikely(p_sys == NULL) )
111 if( (int)p_dec->fmt_in.video.i_height < 0 )
113 /* Frames are coded from bottom to top */
114 p_dec->fmt_in.video.i_height =
115 (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
116 p_sys->b_invert = true;
118 if( !p_dec->fmt_in.video.i_visible_width )
119 p_dec->fmt_in.video.i_visible_width = p_dec->fmt_in.video.i_width;
120 if( !p_dec->fmt_in.video.i_visible_height )
121 p_dec->fmt_in.video.i_visible_height = p_dec->fmt_in.video.i_height;
123 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
125 date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
126 p_dec->fmt_out.video.i_frame_rate_base );
127 if( p_dec->fmt_out.video.i_frame_rate == 0 ||
128 p_dec->fmt_out.video.i_frame_rate_base == 0)
130 msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
131 p_dec->fmt_out.video.i_frame_rate,
132 p_dec->fmt_out.video.i_frame_rate_base);
133 date_Init( &p_sys->pts, 25, 1 );
136 for( unsigned i = 0; i < dsc->plane_count; i++ )
138 unsigned pitch = p_dec->fmt_in.video.i_width * dsc->pixel_size
139 * dsc->p[i].w.num / dsc->p[i].w.den;
140 unsigned lines = p_dec->fmt_in.video.i_height
141 * dsc->p[i].h.num / dsc->p[i].h.den;
143 p_sys->pitches[i] = pitch;
144 p_sys->lines[i] = lines;
145 p_sys->size += pitch * lines;
149 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
151 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
153 p_dec->p_sys = p_sys;
158 static int OpenPacketizer( vlc_object_t *p_this )
160 decoder_t *p_dec = (decoder_t*)p_this;
162 int i_ret = OpenDecoder( p_this );
164 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
169 /****************************************************************************
170 * DecodeBlock: the whole thing
171 ****************************************************************************
172 * This function must be fed with complete frames.
173 ****************************************************************************/
174 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
176 decoder_sys_t *p_sys = p_dec->p_sys;
180 if( !pp_block || !*pp_block ) return NULL;
185 if( p_block->i_pts <= VLC_TS_INVALID && p_block->i_dts <= VLC_TS_INVALID &&
186 !date_Get( &p_sys->pts ) )
188 /* We've just started the stream, wait for the first PTS. */
189 block_Release( p_block );
193 /* Date management: If there is a pts avaliable, use that. */
194 if( p_block->i_pts > VLC_TS_INVALID )
196 date_Set( &p_sys->pts, p_block->i_pts );
198 else if( p_block->i_dts > VLC_TS_INVALID )
200 /* NB, davidf doesn't quite agree with this in general, it is ok
201 * for rawvideo since it is in order (ie pts=dts), however, it
202 * may not be ok for an out-of-order codec, so don't copy this
203 * without thinking */
204 date_Set( &p_sys->pts, p_block->i_dts );
207 if( p_block->i_buffer < p_sys->size )
209 msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
210 p_block->i_buffer, p_sys->size );
212 block_Release( p_block );
216 if( p_sys->b_packetizer )
218 p_buf = SendFrame( p_dec, p_block );
222 p_buf = DecodeFrame( p_dec, p_block );
225 /* Date management: 1 frame per packet */
226 date_Increment( &p_sys->pts, 1 );
232 /*****************************************************************************
234 *****************************************************************************/
235 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
237 decoder_sys_t *p_sys = p_dec->p_sys;
238 const uint8_t *p_src = p_block->p_buffer;
240 if( p_sys->b_invert )
241 for( int i = 0; i < p_pic->i_planes; i++ )
243 uint8_t *p_dst = p_pic->p[i].p_pixels
244 + (p_pic->p[i].i_pitch * p_pic->p[i].i_visible_lines);
246 for( int x = 0; x < p_pic->p[i].i_visible_lines; x++ )
248 p_dst -= p_pic->p[i].i_pitch;
249 memcpy( p_dst, p_src, p_pic->p[i].i_visible_pitch );
250 p_src += p_sys->pitches[i];
253 p_src += p_sys->pitches[i]
254 * (p_sys->lines[i] - p_pic->p[i].i_visible_lines);
257 for( int i = 0; i < p_pic->i_planes; i++ )
259 uint8_t *p_dst = p_pic->p[i].p_pixels;
261 for( int x = 0; x < p_pic->p[i].i_visible_lines; x++ )
263 memcpy( p_dst, p_src, p_pic->p[i].i_visible_pitch );
264 p_src += p_sys->pitches[i];
265 p_dst += p_pic->p[i].i_pitch;
268 p_src += p_sys->pitches[i]
269 * (p_sys->lines[i] - p_pic->p[i].i_visible_lines);
273 /*****************************************************************************
274 * DecodeFrame: decodes a video frame.
275 *****************************************************************************/
276 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
278 decoder_sys_t *p_sys = p_dec->p_sys;
281 /* Get a new picture */
282 p_pic = decoder_NewPicture( p_dec );
285 block_Release( p_block );
289 FillPicture( p_dec, p_block, p_pic );
291 p_pic->date = date_Get( &p_sys->pts );
292 if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK )
294 p_pic->b_progressive = false;
295 p_pic->i_nb_fields = 2;
296 if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST )
297 p_pic->b_top_field_first = true;
299 p_pic->b_top_field_first = false;
302 p_pic->b_progressive = true;
304 block_Release( p_block );
308 /*****************************************************************************
309 * SendFrame: send a video frame to the stream output.
310 *****************************************************************************/
311 static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
313 decoder_sys_t *p_sys = p_dec->p_sys;
315 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
317 if( p_sys->b_invert )
319 block_t *out = block_Alloc( p_block->i_buffer );
320 if( likely(out != NULL) )
322 block_CopyProperties( out, p_block );
324 const uint8_t *p_src = p_block->p_buffer;
325 uint8_t *p_pixels = out->p_buffer;
327 for( unsigned i = 0; i < PICTURE_PLANE_MAX; i++ )
329 unsigned pitch = p_sys->pitches[i];
330 unsigned lines = p_sys->lines[i];
331 uint8_t *p_dst = p_pixels + (pitch * lines);
333 for( unsigned x = 0; x < lines; x++ )
335 p_dst -= p_sys->pitches[i];
336 memcpy( p_dst, p_src, p_sys->pitches[i] );
337 p_src += p_sys->pitches[i];
341 block_Release( p_block );
347 /*****************************************************************************
348 * CloseDecoder: decoder destruction
349 *****************************************************************************/
350 static void CloseDecoder( vlc_object_t *p_this )
352 decoder_t *p_dec = (decoder_t*)p_this;
353 free( p_dec->p_sys );