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