]> git.sesse.net Git - vlc/blob - modules/codec/fake.c
Moved out optimized VA nv12/yv12 copy functions from dxva2.
[vlc] / modules / codec / fake.c
1 /*****************************************************************************
2  * fake.c: decoder reading from a fake stream, outputting a fixed image
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman at m2x dot nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
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_codec.h>
35
36 #include <vlc_image.h>
37 #include <vlc_filter.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  OpenDecoder   ( vlc_object_t * );
43 static void CloseDecoder  ( vlc_object_t * );
44
45 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
46 static int FakeCallback( vlc_object_t *, char const *,
47                          vlc_value_t, vlc_value_t, void * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 #define FILE_TEXT N_("Image file")
53 #define FILE_LONGTEXT N_( \
54     "Path of the image file for fake input." )
55 #define RELOAD_TEXT N_("Reload image file")
56 #define RELOAD_LONGTEXT N_( \
57     "Reload image file every n seconds." )
58 #define WIDTH_TEXT N_("Video width")
59 #define WIDTH_LONGTEXT N_( \
60     "Output video width." )
61 #define HEIGHT_TEXT N_("Video height")
62 #define HEIGHT_LONGTEXT N_( \
63     "Output video height." )
64 #define KEEP_AR_TEXT N_("Keep aspect ratio")
65 #define KEEP_AR_LONGTEXT N_( \
66     "Consider width and height as maximum values." )
67 #define ASPECT_RATIO_TEXT N_("Background aspect ratio")
68 #define ASPECT_RATIO_LONGTEXT N_( \
69     "Aspect ratio of the image file (4:3, 16:9). Default is square pixels." )
70 #define DEINTERLACE_TEXT N_("Deinterlace video")
71 #define DEINTERLACE_LONGTEXT N_( \
72     "Deinterlace the image after loading it." )
73 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
74 #define DEINTERLACE_MODULE_LONGTEXT N_( \
75     "Deinterlace module to use." )
76 #define CHROMA_TEXT N_("Chroma used")
77 #define CHROMA_LONGTEXT N_( \
78     "Force use of a specific chroma for output. Default is I420." )
79
80 static const char *const ppsz_deinterlace_type[] =
81 {
82     "deinterlace", "ffmpeg-deinterlace"
83 };
84
85 vlc_module_begin ()
86     set_category( CAT_INPUT )
87     set_subcategory( SUBCAT_INPUT_VCODEC )
88     set_shortname( N_("Fake") )
89     set_description( N_("Fake video decoder") )
90     set_capability( "decoder", 1000 )
91     set_callbacks( OpenDecoder, CloseDecoder )
92     add_shortcut( "fake" )
93
94     add_file( "fake-file", "", NULL, FILE_TEXT,
95                 FILE_LONGTEXT, false )
96     add_integer( "fake-file-reload", 0, NULL, RELOAD_TEXT,
97                 RELOAD_LONGTEXT, false )
98     add_integer( "fake-width", 0, NULL, WIDTH_TEXT,
99                  WIDTH_LONGTEXT, true )
100     add_integer( "fake-height", 0, NULL, HEIGHT_TEXT,
101                  HEIGHT_LONGTEXT, true )
102     add_bool( "fake-keep-ar", false, NULL, KEEP_AR_TEXT, KEEP_AR_LONGTEXT,
103               true )
104     add_string( "fake-aspect-ratio", "", NULL,
105                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
106     add_bool( "fake-deinterlace", false, NULL, DEINTERLACE_TEXT,
107               DEINTERLACE_LONGTEXT, false )
108     add_string( "fake-deinterlace-module", "deinterlace", NULL,
109                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
110                 false )
111         change_string_list( ppsz_deinterlace_type, 0, 0 )
112     add_string( "fake-chroma", "I420", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
113                 true )
114 vlc_module_end ()
115
116 struct decoder_sys_t
117 {
118     picture_t *p_image;
119     vlc_mutex_t lock;
120
121     bool  b_reload;
122     mtime_t     i_reload;
123     mtime_t     i_next;
124 };
125
126 /*****************************************************************************
127  * OpenDecoder: probe the decoder and return score
128  *****************************************************************************/
129 static int OpenDecoder( vlc_object_t *p_this )
130 {
131     decoder_t *p_dec = (decoder_t*)p_this;
132     image_handler_t *p_handler;
133     video_format_t fmt_in, fmt_out;
134     picture_t *p_image;
135     char *psz_file, *psz_chroma, *psz_string;
136     bool b_keep_ar;
137     int i_aspect = 0;
138     int i_int;
139
140     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
141     {
142         return VLC_EGENERIC;
143     }
144
145     p_dec->p_sys = calloc( 1, sizeof( *p_dec->p_sys ) );
146     if( !p_dec->p_sys )
147         return VLC_ENOMEM;
148
149     psz_file = var_CreateGetNonEmptyStringCommand( p_dec, "fake-file" );
150     if( !psz_file )
151     {
152         msg_Err( p_dec, "specify a file with --fake-file=..." );
153         free( p_dec->p_sys );
154         return VLC_EGENERIC;
155     }
156
157     memset( &fmt_in, 0, sizeof(fmt_in) );
158     memset( &fmt_out, 0, sizeof(fmt_out) );
159
160     i_int = var_CreateGetIntegerCommand( p_dec, "fake-file-reload" );
161     if( i_int > 0)
162     {
163         p_dec->p_sys->b_reload = true;
164         p_dec->p_sys->i_reload = (mtime_t)(i_int * 1000000);
165         p_dec->p_sys->i_next   = (mtime_t)(p_dec->p_sys->i_reload + mdate());
166     }
167
168     psz_chroma = var_CreateGetString( p_dec, "fake-chroma" );
169     fmt_out.i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_chroma );
170     if( !fmt_out.i_chroma )
171     {
172         msg_Warn( p_dec, "Invalid chroma (%s). Using I420.", psz_chroma );
173         fmt_out.i_chroma = VLC_CODEC_I420;
174     }
175     free( psz_chroma );
176
177     var_Create( p_dec, "fake-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
178     var_Create( p_dec, "fake-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179     psz_string = var_CreateGetString( p_dec, "fake-aspect-ratio" );
180
181     if( psz_string )
182     {
183         char *psz_parser = strchr( psz_string, ':' );
184
185         if( psz_parser )
186         {
187             *psz_parser++ = '\0';
188             i_aspect = atoi( psz_string )
189                                    * VOUT_ASPECT_FACTOR / atoi( psz_parser );
190         }
191         free( psz_string );
192     }
193
194     b_keep_ar = var_CreateGetBool( p_dec, "fake-keep-ar" );
195
196     if( !b_keep_ar )
197     {
198         fmt_out.i_width = var_GetInteger( p_dec, "fake-width" );
199         fmt_out.i_height = var_GetInteger( p_dec, "fake-height" );
200     }
201
202     p_handler = image_HandlerCreate( p_dec );
203     p_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
204     image_HandlerDelete( p_handler );
205
206     if ( p_image == NULL )
207     {
208         msg_Err( p_dec, "unable to read image file %s", psz_file );
209         free( psz_file );
210         free( p_dec->p_sys );
211         return VLC_EGENERIC;
212     }
213     msg_Dbg( p_dec, "file %s loaded successfully", psz_file );
214
215     free( psz_file );
216
217     if ( b_keep_ar )
218     {
219         picture_t *p_old = p_image;
220         int i_width, i_height;
221
222         i_width = var_GetInteger( p_dec, "fake-width" );
223         i_height = var_GetInteger( p_dec, "fake-height" );
224
225         if ( i_width && i_height )
226         {
227             int i_image_ar = fmt_out.i_width * VOUT_ASPECT_FACTOR
228                               / fmt_out.i_height;
229             int i_region_ar = i_width * VOUT_ASPECT_FACTOR / i_height;
230             fmt_in = fmt_out;
231
232             if ( i_aspect == i_image_ar )
233             {
234                 fmt_out.i_width = i_width;
235                 fmt_out.i_height = i_height;
236             }
237             else if ( i_image_ar > i_region_ar )
238             {
239                 fmt_out.i_width = i_width;
240                 fmt_out.i_height = i_width * VOUT_ASPECT_FACTOR
241                                     / i_image_ar;
242                 i_aspect = i_image_ar;
243             }
244             else
245             {
246                 fmt_out.i_height = i_height;
247                 fmt_out.i_width = i_height * i_image_ar
248                                     / VOUT_ASPECT_FACTOR;
249                 i_aspect = i_image_ar;
250             }
251
252             p_handler = image_HandlerCreate( p_dec );
253             p_image = image_Convert( p_handler, p_old, &fmt_in, &fmt_out );
254             image_HandlerDelete( p_handler );
255
256             if ( p_image == NULL )
257             {
258                 msg_Warn( p_dec, "couldn't load resizing module" );
259                 p_image = p_old;
260                 fmt_out = fmt_in;
261             }
262             else
263             {
264                 picture_Release( p_old );
265             }
266         }
267     }
268
269     if ( i_aspect )
270     {
271         fmt_out.i_sar_num = i_aspect           * fmt_out.i_height;
272         fmt_out.i_sar_den = VOUT_ASPECT_FACTOR * fmt_out.i_width;
273     }
274     else
275     {
276         fmt_out.i_sar_num = 1;
277         fmt_out.i_sar_den = 1;
278     }
279
280     if( var_CreateGetBool( p_dec, "fake-deinterlace" ) )
281     {
282         picture_t *p_old = p_image;
283
284         psz_string = var_CreateGetString( p_dec, "fake-deinterlace-module" );
285
286         p_handler = image_HandlerCreate( p_dec );
287         p_image = image_Filter( p_handler, p_old, &fmt_out, psz_string );
288         image_HandlerDelete( p_handler );
289         free( psz_string );
290
291         if ( p_image == NULL )
292         {
293             msg_Warn( p_dec, "couldn't load deinterlace module" );
294             p_image = p_old;
295         }
296         else
297         {
298             picture_Release( p_old );
299         }
300     }
301
302     /* Set output properties */
303     p_dec->fmt_out.i_cat = VIDEO_ES;
304     p_dec->fmt_out.i_codec = fmt_out.i_chroma;
305     p_dec->fmt_out.video = fmt_out;
306
307     /* Set callbacks */
308     p_dec->pf_decode_video = DecodeBlock;
309
310     p_dec->p_sys->p_image = p_image;
311     vlc_mutex_init( &p_dec->p_sys->lock );
312
313     /* Add the callback when every variables are available */
314     var_AddCallback( p_dec, "fake-file", FakeCallback, p_dec );
315     var_AddCallback( p_dec, "fake-file-reload", FakeCallback , p_dec );
316
317     return VLC_SUCCESS;
318 }
319
320 /****************************************************************************
321  * DecodeBlock: the whole thing
322  ****************************************************************************/
323 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
324 {
325     decoder_sys_t *p_sys = (decoder_sys_t*) p_dec->p_sys;
326     picture_t *p_pic;
327
328     if( pp_block == NULL || !*pp_block ) return NULL;
329     p_pic = decoder_NewPicture( p_dec );
330     if( p_pic == NULL )
331     {
332         msg_Err( p_dec, "cannot get picture" );
333         goto error;
334     }
335
336     if( p_sys->b_reload && (mdate() >= p_sys->i_next) )
337     {
338         var_TriggerCallback( p_dec, "fake-file" );
339         /* next period */
340         p_sys->i_next = (mtime_t)(p_sys->i_reload + mdate());
341     }
342     vlc_mutex_lock( &p_dec->p_sys->lock );
343     picture_Copy( p_pic, p_dec->p_sys->p_image );
344     vlc_mutex_unlock( &p_dec->p_sys->lock );
345
346     p_pic->date = (*pp_block)->i_pts;
347
348 error:
349     block_Release( *pp_block );
350     *pp_block = NULL;
351
352     return p_pic;
353 }
354
355 /*****************************************************************************
356  * CloseDecoder: fake decoder destruction
357  *****************************************************************************/
358 static void CloseDecoder( vlc_object_t *p_this )
359 {
360     decoder_t *p_dec = (decoder_t *)p_this;
361     picture_t *p_image = p_dec->p_sys->p_image;
362
363     var_DelCallback( p_dec, "fake-file", FakeCallback, p_dec );
364     var_DelCallback( p_dec, "fake-file-reload", FakeCallback , p_dec );
365
366     if( p_image != NULL )
367         picture_Release( p_image );
368
369     vlc_mutex_destroy( &p_dec->p_sys->lock );
370     free( p_dec->p_sys );
371 }
372
373 /*****************************************************************************
374  * FakeCallback: Image source change callback.
375  *****************************************************************************/
376 static int FakeCallback( vlc_object_t *p_this, char const *psz_var,
377                          vlc_value_t oldval, vlc_value_t newval,
378                          void *p_data )
379 {
380     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
381     decoder_t *p_dec = (decoder_t *)p_data;
382
383     if( !strcmp( psz_var, "fake-file" ) )
384     {
385         image_handler_t *p_handler;
386         picture_t *p_new_image;
387         video_format_t fmt_in, fmt_out;
388         char *psz_file = newval.psz_string;
389         picture_t *p_image;
390
391         vlc_mutex_lock( &p_dec->p_sys->lock );
392         p_image = p_dec->p_sys->p_image;
393
394         if( !psz_file || !*psz_file )
395         {
396             msg_Err( p_dec, "fake-file value must be non empty." );
397             vlc_mutex_unlock( &p_dec->p_sys->lock );
398             return VLC_EGENERIC;
399         }
400         msg_Dbg( p_dec, "Changing fake-file to %s.", psz_file );
401
402         memset( &fmt_in, 0, sizeof(fmt_in) );
403         fmt_out = p_dec->fmt_out.video;
404         p_handler = image_HandlerCreate( p_dec );
405         p_new_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
406         image_HandlerDelete( p_handler );
407
408         if( !p_new_image )
409         {
410             msg_Err( p_dec, "error while reading image (%s)", psz_file );
411             vlc_mutex_unlock( &p_dec->p_sys->lock );
412             return VLC_EGENERIC;
413         }
414
415         p_dec->p_sys->p_image = p_new_image;
416         picture_Release( p_image );
417         vlc_mutex_unlock( &p_dec->p_sys->lock );
418     }
419     else if( !strcmp( psz_var, "fake-file-reload" ) )
420     {
421         if( newval.i_int > 0)
422         {
423             p_dec->p_sys->b_reload = true;
424             p_dec->p_sys->i_reload = (mtime_t)(newval.i_int * 1000000);
425             p_dec->p_sys->i_next   = (mtime_t)(p_dec->p_sys->i_reload + mdate());
426         }
427         else
428         {
429             p_dec->p_sys->b_reload = false;
430         }
431     }
432
433     return VLC_SUCCESS;
434 }