]> git.sesse.net Git - vlc/blob - modules/video_output/yuv.c
Merge branch 1.0-bugfix
[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_CODEC_I420;
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     const vlc_fourcc_t i_requested_chroma =
138         vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_fcc );
139     if( i_requested_chroma )
140         p_sys->i_chroma = i_requested_chroma;
141     free( psz_fcc );
142
143     if( p_sys->b_yuv4mpeg2 )
144     {
145         switch( p_sys->i_chroma )
146         {
147             case VLC_CODEC_YV12:
148             case VLC_CODEC_I420:
149             case VLC_CODEC_J420:
150                 break;
151             default:
152                 msg_Err( p_this,
153                     "YUV4MPEG2 mode needs chroma YV12 not %4s as requested",
154                     (char *)&(p_sys->i_chroma) );
155                 fclose( p_vout->p_sys->p_fd );
156                 free( p_vout->p_sys->psz_file );
157                 free( p_vout->p_sys );
158                 return VLC_EGENERIC;
159         }
160     }
161
162     msg_Dbg( p_this, "using chroma %4s", (char *)&(p_sys->i_chroma) );
163
164     p_vout->pf_init = Init;
165     p_vout->pf_end = End;
166     p_vout->pf_manage = NULL;
167     p_vout->pf_render = NULL;
168     p_vout->pf_display = Display;
169
170     return VLC_SUCCESS;
171 }
172
173 /*****************************************************************************
174  * Init: initialize video thread
175  *****************************************************************************/
176 static int Init( vout_thread_t *p_vout )
177 {
178     vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
179     int i_index;
180     picture_t *p_pic;
181
182     /* Initialize the output structure */
183     if( p_vout->render.i_chroma != p_sys->i_chroma )
184         p_vout->output.i_chroma = p_vout->fmt_out.i_chroma = p_sys->i_chroma;
185     else
186        p_vout->output.i_chroma = p_vout->render.i_chroma;
187
188     p_vout->output.pf_setpalette = NULL;
189     p_vout->output.i_width = p_vout->render.i_width;
190     p_vout->output.i_height = p_vout->render.i_height;
191     p_vout->output.i_aspect = p_vout->output.i_width
192                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
193
194     p_vout->output.i_rmask = 0xff0000;
195     p_vout->output.i_gmask = 0x00ff00;
196     p_vout->output.i_bmask = 0x0000ff;
197
198     /* Try to initialize 1 direct buffer */
199     p_pic = NULL;
200
201     /* Find an empty picture slot */
202     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
203     {
204         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
205         {
206             p_pic = p_vout->p_picture + i_index;
207             break;
208         }
209     }
210
211     /* Allocate the picture */
212     if( p_pic == NULL )
213         return VLC_EGENERIC;
214
215     vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
216                           p_vout->output.i_width, p_vout->output.i_height,
217                           p_vout->output.i_aspect );
218
219     if( p_pic->i_planes == 0 )
220     {
221         return VLC_EGENERIC;
222     }
223
224     p_pic->i_status = DESTROYED_PICTURE;
225     p_pic->i_type   = DIRECT_PICTURE;
226
227     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
228     I_OUTPUTPICTURES++;
229     return VLC_SUCCESS;
230 }
231
232 /*****************************************************************************
233  * Destroy: destroy video thread
234  *****************************************************************************
235  * Terminate an output method created by Create
236  *****************************************************************************/
237 static void Destroy( vlc_object_t *p_this )
238 {
239     int i_index;
240     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
241
242     for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
243     {
244         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
245     }
246
247     /* Destroy structure */
248     fclose( p_vout->p_sys->p_fd );
249
250     free( p_vout->p_sys->psz_file );
251     free( p_vout->p_sys );
252 }
253
254 /*****************************************************************************
255  * Display: displays previously rendered output
256  *****************************************************************************
257  * This function copies the rendered picture into our circular buffer.
258  *****************************************************************************/
259 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
260 {
261     video_format_t fmt_in;
262
263     memset( &fmt_in, 0, sizeof( fmt_in ) );
264     video_format_Copy( &fmt_in, &p_pic->format );
265     /* assume square pixels if i_sar_num = 0 */
266     if( p_pic->format.i_sar_num == 0 )
267         fmt_in.i_sar_num = fmt_in.i_sar_den = 1;
268
269     WriteYUV( p_vout, fmt_in, p_pic );
270     video_format_Clean( &fmt_in );
271     return;
272 }
273
274 static void End( vout_thread_t *p_vout )
275 {
276     VLC_UNUSED(p_vout);
277 }
278
279 static void WriteYUV( vout_thread_t *p_vout, video_format_t fmt,
280                       picture_t *p_pic )
281 {
282     vout_sys_t *p_sys = p_vout->p_sys;
283
284     if( !p_pic || !p_sys ) return;
285
286 #if 0
287     msg_Dbg( p_vout, "writing %d bytes of fourcc %4s",
288              i_bytes, (char *)&fmt.i_chroma );
289 #endif
290     if( !p_sys->b_header )
291     {
292         const char *p_hdr = "";
293         if( p_sys->b_yuv4mpeg2 )
294         {
295             /* MPlayer compatible header, unfortunately it doesn't tell you
296              * the exact fourcc used. */
297             p_hdr = "YUV4MPEG2";
298         }
299         else
300         {
301             p_hdr = (const char*)&fmt.i_chroma;
302         }
303         fprintf( p_sys->p_fd, "%4s W%d H%d F%d:%d I%c A%d:%d\n",
304                  p_hdr,
305                  fmt.i_width, fmt.i_height,
306                  fmt.i_frame_rate, fmt.i_frame_rate_base,
307                  (p_pic->b_progressive ? 'p' :
308                          (p_pic->b_top_field_first ? 't' : 'b')),
309                  fmt.i_sar_num, fmt.i_sar_den );
310         fflush( p_sys->p_fd );
311         p_sys->b_header = true;
312     }
313
314     fprintf( p_sys->p_fd, "FRAME\n" );
315     if( p_pic->b_progressive )
316     {
317         size_t i_bytes = (fmt.i_width * fmt.i_height * fmt.i_bits_per_pixel) >> 3;
318         size_t i_written = 0;
319
320         i_written = fwrite( p_pic->p_data, 1, i_bytes, p_sys->p_fd );
321         if( i_written != i_bytes )
322         {
323             msg_Warn( p_vout, "only %d of %d bytes written",
324                       i_written, i_bytes );
325         }
326     }
327     else
328     {
329         msg_Warn( p_vout, "only progressive frames are supported, "
330                           "use a deinterlace filter" );
331     }
332     fflush( p_sys->p_fd );
333 }