]> git.sesse.net Git - vlc/blob - modules/codec/rawvideo.c
Merge branch 1.0-bugfix
[vlc] / modules / codec / rawvideo.c
1 /*****************************************************************************
2  * rawvideo.c: Pseudo video decoder/packetizer for raw video data
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34
35 /*****************************************************************************
36  * decoder_sys_t : raw video decoder descriptor
37  *****************************************************************************/
38 struct decoder_sys_t
39 {
40     /* Module mode */
41     bool b_packetizer;
42
43     /*
44      * Input properties
45      */
46     size_t i_raw_size;
47     bool b_invert;
48
49     /*
50      * Common properties
51      */
52     date_t pts;
53 };
54
55 /****************************************************************************
56  * Local prototypes
57  ****************************************************************************/
58 static int  OpenDecoder   ( vlc_object_t * );
59 static int  OpenPacketizer( vlc_object_t * );
60 static void CloseDecoder  ( vlc_object_t * );
61
62 static void *DecodeBlock  ( decoder_t *, block_t ** );
63
64 static picture_t *DecodeFrame( decoder_t *, block_t * );
65 static block_t   *SendFrame  ( decoder_t *, block_t * );
66
67 /*****************************************************************************
68  * Module descriptor
69  *****************************************************************************/
70 vlc_module_begin ()
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 )
76
77     add_submodule ()
78     set_description( N_("Pseudo raw video packetizer") )
79     set_capability( "packetizer", 100 )
80     set_callbacks( OpenPacketizer, CloseDecoder )
81 vlc_module_end ()
82
83 /*****************************************************************************
84  * OpenDecoder: probe the decoder and return score
85  *****************************************************************************/
86 static int OpenDecoder( vlc_object_t *p_this )
87 {
88     decoder_t *p_dec = (decoder_t*)p_this;
89     decoder_sys_t *p_sys;
90
91     switch( p_dec->fmt_in.i_codec )
92     {
93         /* Planar YUV */
94         case VLC_CODEC_I444:
95         case VLC_CODEC_I422:
96         case VLC_CODEC_I420:
97         case VLC_CODEC_YV12:
98         case VLC_CODEC_I411:
99         case VLC_CODEC_I410:
100         case VLC_CODEC_GREY:
101         case VLC_CODEC_YUVP:
102
103         /* Packed YUV */
104         case VLC_CODEC_YUYV:
105         case VLC_CODEC_YVYU:
106         case VLC_CODEC_UYVY:
107         case VLC_CODEC_VYUY:
108
109         /* RGB */
110         case VLC_CODEC_RGB32:
111         case VLC_CODEC_RGB24:
112         case VLC_CODEC_RGB16:
113         case VLC_CODEC_RGB15:
114         case VLC_CODEC_RGB8:
115         case VLC_CODEC_RGBP:
116             break;
117
118         default:
119             return VLC_EGENERIC;
120     }
121
122     /* Allocate the memory needed to store the decoder's structure */
123     if( ( p_dec->p_sys = p_sys =
124           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
125         return VLC_ENOMEM;
126     /* Misc init */
127     p_dec->p_sys->b_packetizer = false;
128     p_sys->b_invert = 0;
129
130     if( (int)p_dec->fmt_in.video.i_height < 0 )
131     {
132         /* Frames are coded from bottom to top */
133         p_dec->fmt_in.video.i_height =
134             (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
135         p_sys->b_invert = true;
136     }
137
138     if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
139     {
140         msg_Err( p_dec, "invalid display size %dx%d",
141                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
142         return VLC_EGENERIC;
143     }
144
145     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
146
147     date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
148                p_dec->fmt_out.video.i_frame_rate_base );
149     if( p_dec->fmt_out.video.i_frame_rate == 0 ||
150         p_dec->fmt_out.video.i_frame_rate_base == 0)
151     {
152         msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
153                   p_dec->fmt_out.video.i_frame_rate,
154                   p_dec->fmt_out.video.i_frame_rate_base);
155         date_Init( &p_sys->pts, 25, 1 );
156     }
157
158     /* Find out p_vdec->i_raw_size */
159     video_format_Setup( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
160                         p_dec->fmt_in.video.i_width,
161                         p_dec->fmt_in.video.i_height,
162                         p_dec->fmt_in.video.i_aspect );
163     p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
164         p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
165
166     if( !p_dec->fmt_in.video.i_aspect )
167     {
168         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
169             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
170     }
171
172     /* Set callbacks */
173     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
174         DecodeBlock;
175     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
176         DecodeBlock;
177
178     return VLC_SUCCESS;
179 }
180
181 static int OpenPacketizer( vlc_object_t *p_this )
182 {
183     decoder_t *p_dec = (decoder_t*)p_this;
184
185     int i_ret = OpenDecoder( p_this );
186
187     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
188
189     return i_ret;
190 }
191
192 /****************************************************************************
193  * DecodeBlock: the whole thing
194  ****************************************************************************
195  * This function must be fed with complete frames.
196  ****************************************************************************/
197 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
198 {
199     decoder_sys_t *p_sys = p_dec->p_sys;
200     block_t *p_block;
201     void *p_buf;
202
203     if( !pp_block || !*pp_block ) return NULL;
204
205     p_block = *pp_block;
206
207
208     if( !p_block->i_pts && !p_block->i_dts && !date_Get( &p_sys->pts ) )
209     {
210         /* We've just started the stream, wait for the first PTS. */
211         block_Release( p_block );
212         return NULL;
213     }
214
215     /* Date management: If there is a pts avaliable, use that. */
216     if( p_block->i_pts )
217     {
218         date_Set( &p_sys->pts, p_block->i_pts );
219     }
220     else if( p_block->i_dts )
221     {
222         /* NB, davidf doesn't quite agree with this in general, it is ok
223          * for rawvideo since it is in order (ie pts=dts), however, it
224          * may not be ok for an out-of-order codec, so don't copy this
225          * without thinking */
226         date_Set( &p_sys->pts, p_block->i_dts );
227     }
228
229     if( p_block->i_buffer < p_sys->i_raw_size )
230     {
231         msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
232                   p_block->i_buffer, p_sys->i_raw_size );
233
234         block_Release( p_block );
235         return NULL;
236     }
237
238     if( p_sys->b_packetizer )
239     {
240         p_buf = SendFrame( p_dec, p_block );
241     }
242     else
243     {
244         p_buf = DecodeFrame( p_dec, p_block );
245     }
246
247     /* Date management: 1 frame per packet */
248     date_Increment( &p_sys->pts, 1 );
249     *pp_block = NULL;
250
251     return p_buf;
252 }
253
254 /*****************************************************************************
255  * FillPicture:
256  *****************************************************************************/
257 static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
258 {
259     int i_plane;
260     decoder_sys_t *p_sys = p_dec->p_sys;
261     uint8_t *p_src = p_block->p_buffer;
262
263     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
264     {
265         int i_pitch = p_pic->p[i_plane].i_pitch;
266         int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
267         int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
268         uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
269         uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
270
271         if( p_sys->b_invert )
272             for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
273                  p_dst_end -= i_pitch, p_src += i_visible_pitch )
274                 vlc_memcpy( p_dst_end, p_src, i_visible_pitch );
275         else
276             for( ; p_dst < p_dst_end;
277                  p_dst += i_pitch, p_src += i_visible_pitch )
278                 vlc_memcpy( p_dst, p_src, i_visible_pitch );
279     }
280 }
281
282 /*****************************************************************************
283  * DecodeFrame: decodes a video frame.
284  *****************************************************************************/
285 static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
286 {
287     decoder_sys_t *p_sys = p_dec->p_sys;
288     picture_t *p_pic;
289
290     /* Get a new picture */
291     p_pic = decoder_NewPicture( p_dec );
292     if( !p_pic )
293     {
294         block_Release( p_block );
295         return NULL;
296     }
297
298     FillPicture( p_dec, p_block, p_pic );
299
300     p_pic->date = date_Get( &p_sys->pts );
301     p_pic->b_progressive = true;
302
303     block_Release( p_block );
304     return p_pic;
305 }
306
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 )
311 {
312     decoder_sys_t *p_sys = p_dec->p_sys;
313
314     p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
315
316     if( p_sys->b_invert )
317     {
318         picture_t pic;
319         uint8_t *p_tmp, *p_pixels;
320         int i, j;
321
322         /* Fill in picture_t fields */
323         picture_Setup( &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 );
326
327         if( !pic.i_planes )
328         {
329             msg_Err( p_dec, "unsupported chroma" );
330             return p_block;
331         }
332
333         p_tmp = malloc( pic.p[0].i_pitch );
334         if( !p_tmp )
335             return p_block;
336         p_pixels = p_block->p_buffer;
337         for( i = 0; i < pic.i_planes; i++ )
338         {
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);
343
344             for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
345             {
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  );
349                 p_top += i_pitch;
350                 p_bottom -= i_pitch;
351             }
352
353             p_pixels += i_pitch * pic.p[i].i_lines;
354         }
355         free( p_tmp );
356     }
357
358     return p_block;
359 }
360
361 /*****************************************************************************
362  * CloseDecoder: decoder destruction
363  *****************************************************************************/
364 static void CloseDecoder( vlc_object_t *p_this )
365 {
366     decoder_t *p_dec = (decoder_t*)p_this;
367     free( p_dec->p_sys );
368 }