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