]> git.sesse.net Git - vlc/blob - modules/video_output/yuv.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / video_output / yuv.c
1 /*****************************************************************************
2  * yuv.c : yuv video output
3  *****************************************************************************
4  * Copyright (C) 2008, M2X BV
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman@videolan.org>
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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout.h>
35 #include <vlc_charset.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static int  Init      ( vout_thread_t * );
44 static void End       ( vout_thread_t *p_vout );
45 static void Display   ( vout_thread_t *, picture_t * );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50
51 #define YUV_FILE_TEXT N_("device, fifo or filename")
52 #define YUV_FILE_LONGTEXT N_("device, fifo or filename to write yuv frames too." )
53
54 #define CHROMA_TEXT N_("Chroma used.")
55 #define CHROMA_LONGTEXT N_( \
56     "Force use of a specific chroma for output. Default is I420." )
57
58 #define YUV4MPEG2_TEXT N_( "YUV4MPEG2 header (default disabled)" )
59 #define YUV4MPEG2_LONGTEXT N_( "The YUV4MPEG2 header is compatible " \
60     "with mplayer yuv video ouput and requires YV12/I420 fourcc. By default "\
61     "vlc writes the fourcc of the picture frame into the output destination." )
62
63 #define CFG_PREFIX "yuv-"
64
65 vlc_module_begin ()
66     set_shortname( N_( "YUV output" ) )
67     set_description( N_( "YUV video output" ) )
68     set_category( CAT_VIDEO )
69     set_subcategory( SUBCAT_VIDEO_VOUT )
70     set_capability( "video output", 0 )
71
72     add_string( CFG_PREFIX "file", "stream.yuv", NULL,
73                 YUV_FILE_TEXT, YUV_FILE_LONGTEXT, false )
74     add_string( CFG_PREFIX "chroma", NULL, NULL,
75                 CHROMA_TEXT, CHROMA_LONGTEXT, true )
76     add_bool  ( CFG_PREFIX "yuv4mpeg2", false, NULL,
77                 YUV4MPEG2_TEXT, YUV4MPEG2_LONGTEXT, true )
78
79     set_callbacks( Create, Destroy )
80 vlc_module_end ()
81
82 static const char *const ppsz_vout_options[] = {
83     "file", "chroma", "yuv4mpeg2", NULL
84 };
85
86 static void WriteYUV( vout_thread_t *p_vout, video_format_t fmt,
87                       picture_t *p_pic );
88
89 /*****************************************************************************
90  * vout_sys_t: video output descriptor
91  *****************************************************************************/
92 struct vout_sys_t
93 {
94     char *psz_file;
95     FILE *p_fd;
96     bool  b_header;
97     bool  b_yuv4mpeg2;
98     vlc_fourcc_t i_chroma;
99 };
100
101 /*****************************************************************************
102  * Create: allocates video thread
103  *****************************************************************************
104  * This function allocates and initializes a vout method.
105  *****************************************************************************/
106 static int Create( vlc_object_t *p_this )
107 {
108     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
109     vout_sys_t *p_sys;
110     char *psz_fcc;
111
112     config_ChainParse( p_vout, CFG_PREFIX, ppsz_vout_options,
113                        p_vout->p_cfg );
114
115     /* Allocate instance and initialize some members */
116     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
117     if( !p_vout->p_sys )
118         return VLC_ENOMEM;
119
120     p_sys->b_header = false;
121     p_sys->p_fd = NULL;
122
123     p_sys->b_yuv4mpeg2 = var_CreateGetBool( p_this, CFG_PREFIX "yuv4mpeg2" );
124     p_sys->i_chroma = VLC_FOURCC('I','4','2','0');
125
126     p_sys->psz_file =
127             var_CreateGetString( p_this, CFG_PREFIX "file" );
128     p_sys->p_fd = utf8_fopen( p_sys->psz_file, "wb" );
129     if( !p_sys->p_fd )
130     {
131         free( p_sys->psz_file );
132         free( p_sys );
133         return VLC_EGENERIC;
134     }
135
136     psz_fcc = var_CreateGetNonEmptyString( p_this, CFG_PREFIX "chroma" );
137     if( psz_fcc && (strlen( psz_fcc ) == 4) )
138     {
139         p_sys->i_chroma = VLC_FOURCC( psz_fcc[0], psz_fcc[1],
140                                       psz_fcc[2], psz_fcc[3] );
141     }
142     free( psz_fcc );
143
144     if( p_sys->b_yuv4mpeg2 )
145     {
146         switch( p_sys->i_chroma )
147         {
148             case VLC_FOURCC('Y','V','1','2'):
149             case VLC_FOURCC('I','4','2','0'):
150             case VLC_FOURCC('I','Y','U','V'):
151             case VLC_FOURCC('J','4','2','0'):
152                 break;
153             default:
154                 msg_Err( p_this,
155                     "YUV4MPEG2 mode needs chroma YV12 not %4s as requested",
156                     (char *)&(p_sys->i_chroma) );
157                 fclose( p_vout->p_sys->p_fd );
158                 free( p_vout->p_sys->psz_file );
159                 free( p_vout->p_sys );
160                 return VLC_EGENERIC;
161         }
162     }
163
164     msg_Dbg( p_this, "using chroma %4s", (char *)&(p_sys->i_chroma) );
165
166     p_vout->pf_init = Init;
167     p_vout->pf_end = End;
168     p_vout->pf_manage = NULL;
169     p_vout->pf_render = NULL;
170     p_vout->pf_display = Display;
171
172     return VLC_SUCCESS;
173 }
174
175 /*****************************************************************************
176  * Init: initialize video thread
177  *****************************************************************************/
178 static int Init( vout_thread_t *p_vout )
179 {
180     vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
181     int i_index;
182     picture_t *p_pic;
183
184     /* Initialize the output structure */
185     if( p_vout->render.i_chroma != p_sys->i_chroma )
186         p_vout->output.i_chroma = p_vout->fmt_out.i_chroma = p_sys->i_chroma;
187     else
188        p_vout->output.i_chroma = p_vout->render.i_chroma;
189
190     p_vout->output.pf_setpalette = NULL;
191     p_vout->output.i_width = p_vout->render.i_width;
192     p_vout->output.i_height = p_vout->render.i_height;
193     p_vout->output.i_aspect = p_vout->output.i_width
194                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
195
196     p_vout->output.i_rmask = 0xff0000;
197     p_vout->output.i_gmask = 0x00ff00;
198     p_vout->output.i_bmask = 0x0000ff;
199
200     /* Try to initialize 1 direct buffer */
201     p_pic = NULL;
202
203     /* Find an empty picture slot */
204     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
205     {
206         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
207         {
208             p_pic = p_vout->p_picture + i_index;
209             break;
210         }
211     }
212
213     /* Allocate the picture */
214     if( p_pic == NULL )
215         return VLC_EGENERIC;
216
217     vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
218                           p_vout->output.i_width, p_vout->output.i_height,
219                           p_vout->output.i_aspect );
220
221     if( p_pic->i_planes == 0 )
222     {
223         return VLC_EGENERIC;
224     }
225
226     p_pic->i_status = DESTROYED_PICTURE;
227     p_pic->i_type   = DIRECT_PICTURE;
228
229     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
230     I_OUTPUTPICTURES++;
231     return VLC_SUCCESS;
232 }
233
234 /*****************************************************************************
235  * Destroy: destroy video thread
236  *****************************************************************************
237  * Terminate an output method created by Create
238  *****************************************************************************/
239 static void Destroy( vlc_object_t *p_this )
240 {
241     int i_index;
242     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
243
244     for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
245     {
246         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
247     }
248
249     /* Destroy structure */
250     fclose( p_vout->p_sys->p_fd );
251
252     free( p_vout->p_sys->psz_file );
253     free( p_vout->p_sys );
254 }
255
256 /*****************************************************************************
257  * Display: displays previously rendered output
258  *****************************************************************************
259  * This function copies the rendered picture into our circular buffer.
260  *****************************************************************************/
261 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
262 {
263     video_format_t fmt_in;
264
265     memset( &fmt_in, 0, sizeof( fmt_in ) );
266     video_format_Copy( &fmt_in, &p_pic->format );
267     /* assume square pixels if i_sar_num = 0 */
268     if( p_pic->format.i_sar_num == 0 )
269         fmt_in.i_sar_num = fmt_in.i_sar_den = 1;
270
271     WriteYUV( p_vout, fmt_in, p_pic );
272     video_format_Clean( &fmt_in );
273     return;
274 }
275
276 static void End( vout_thread_t *p_vout )
277 {
278     VLC_UNUSED(p_vout);
279 }
280
281 static void WriteYUV( vout_thread_t *p_vout, video_format_t fmt,
282                       picture_t *p_pic )
283 {
284     vout_sys_t *p_sys = p_vout->p_sys;
285
286     if( !p_pic || !p_sys ) return;
287
288 #if 0
289     msg_Dbg( p_vout, "writing %d bytes of fourcc %4s",
290              i_bytes, (char *)&fmt.i_chroma );
291 #endif
292     if( !p_sys->b_header )
293     {
294         const char *p_hdr = "";
295         if( p_sys->b_yuv4mpeg2 )
296         {
297             /* MPlayer compatible header, unfortunately it doesn't tell you
298              * the exact fourcc used. */
299             p_hdr = "YUV4MPEG2";
300         }
301         else
302         {
303             p_hdr = (const char*)&fmt.i_chroma;
304         }
305         fprintf( p_sys->p_fd, "%4s W%d H%d F%d:%d I%c A%d:%d\n",
306                  p_hdr,
307                  fmt.i_width, fmt.i_height,
308                  fmt.i_frame_rate, fmt.i_frame_rate_base,
309                  (p_pic->b_progressive ? 'p' :
310                          (p_pic->b_top_field_first ? 't' : 'b')),
311                  fmt.i_sar_num, fmt.i_sar_den );
312         fflush( p_sys->p_fd );
313         p_sys->b_header = true;
314     }
315
316     fprintf( p_sys->p_fd, "FRAME\n" );
317     if( p_pic->b_progressive )
318     {
319         size_t i_bytes = (fmt.i_width * fmt.i_height * fmt.i_bits_per_pixel) >> 3;
320         size_t i_written = 0;
321
322         i_written = fwrite( p_pic->p_data, 1, i_bytes, p_sys->p_fd );
323         if( i_written != i_bytes )
324         {
325             msg_Warn( p_vout, "only %d of %d bytes written",
326                       i_written, i_bytes );
327         }
328     }
329     else
330     {
331         msg_Warn( p_vout, "only progressive frames are supported, "
332                           "use a deinterlace filter" );
333     }
334     fflush( p_sys->p_fd );
335 }