]> git.sesse.net Git - vlc/blob - src/misc/image.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / src / misc / image.c
1 /*****************************************************************************
2  * image.c : wrapper for image reading/writing facilities
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Gildas Bazin <gbazin@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  * \file
26  * This file contains the functions to handle the image_handler_t type
27  */
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <ctype.h>
38 #include <errno.h>
39
40 #include <vlc_common.h>
41 #include <vlc_codec.h>
42 #include <vlc_meta.h>
43 #include <vlc_filter.h>
44 #include <vlc_es.h>
45 #include <vlc_image.h>
46 #include <vlc_stream.h>
47 #include <vlc_charset.h>
48 #include <vlc_sout.h>
49 #include <libvlc.h>
50
51 static picture_t *ImageRead( image_handler_t *, block_t *,
52                              video_format_t *, video_format_t * );
53 static picture_t *ImageReadUrl( image_handler_t *, const char *,
54                                 video_format_t *, video_format_t * );
55 static block_t *ImageWrite( image_handler_t *, picture_t *,
56                             video_format_t *, video_format_t * );
57 static int ImageWriteUrl( image_handler_t *, picture_t *,
58                           video_format_t *, video_format_t *, const char * );
59
60 static picture_t *ImageConvert( image_handler_t *, picture_t *,
61                                 video_format_t *, video_format_t * );
62 static picture_t *ImageFilter( image_handler_t *, picture_t *,
63                                video_format_t *, const char *psz_module );
64
65 static decoder_t *CreateDecoder( vlc_object_t *, video_format_t * );
66 static void DeleteDecoder( decoder_t * );
67 static encoder_t *CreateEncoder( vlc_object_t *, video_format_t *,
68                                  video_format_t * );
69 static void DeleteEncoder( encoder_t * );
70 static filter_t *CreateFilter( vlc_object_t *, es_format_t *,
71                                video_format_t *, const char * );
72 static void DeleteFilter( filter_t * );
73
74 vlc_fourcc_t image_Type2Fourcc( const char * );
75 vlc_fourcc_t image_Ext2Fourcc( const char * );
76 /*static const char *Fourcc2Ext( vlc_fourcc_t );*/
77
78 /**
79  * Create an image_handler_t instance
80  *
81  */
82 image_handler_t *__image_HandlerCreate( vlc_object_t *p_this )
83 {
84     image_handler_t *p_image = calloc( 1, sizeof(image_handler_t) );
85     if( !p_image )
86         return NULL;
87
88     p_image->p_parent = p_this;
89
90     p_image->pf_read = ImageRead;
91     p_image->pf_read_url = ImageReadUrl;
92     p_image->pf_write = ImageWrite;
93     p_image->pf_write_url = ImageWriteUrl;
94     p_image->pf_convert = ImageConvert;
95     p_image->pf_filter = ImageFilter;
96
97     return p_image;
98 }
99
100 /**
101  * Delete the image_handler_t instance
102  *
103  */
104 void image_HandlerDelete( image_handler_t *p_image )
105 {
106     if( !p_image ) return;
107
108     if( p_image->p_dec ) DeleteDecoder( p_image->p_dec );
109     if( p_image->p_enc ) DeleteEncoder( p_image->p_enc );
110     if( p_image->p_filter ) DeleteFilter( p_image->p_filter );
111
112     free( p_image );
113     p_image = NULL;
114 }
115
116 /**
117  * Read an image
118  *
119  */
120
121 static picture_t *ImageRead( image_handler_t *p_image, block_t *p_block,
122                              video_format_t *p_fmt_in,
123                              video_format_t *p_fmt_out )
124 {
125     picture_t *p_pic = NULL, *p_tmp;
126
127     /* Check if we can reuse the current decoder */
128     if( p_image->p_dec &&
129         p_image->p_dec->fmt_in.i_codec != p_fmt_in->i_chroma )
130     {
131         DeleteDecoder( p_image->p_dec );
132         p_image->p_dec = 0;
133     }
134
135     /* Start a decoder */
136     if( !p_image->p_dec )
137     {
138         p_image->p_dec = CreateDecoder( p_image->p_parent, p_fmt_in );
139         if( !p_image->p_dec ) return NULL;
140     }
141
142     p_block->i_pts = p_block->i_dts = mdate();
143     while( (p_tmp = p_image->p_dec->pf_decode_video( p_image->p_dec, &p_block ))
144              != NULL )
145     {
146         if( p_pic != NULL )
147             picture_Release( p_pic );
148         p_pic = p_tmp;
149     }
150
151     if( p_pic == NULL )
152     {
153         msg_Warn( p_image->p_parent, "no image decoded" );
154         return 0;
155     }
156
157     if( !p_fmt_out->i_chroma )
158         p_fmt_out->i_chroma = p_image->p_dec->fmt_out.video.i_chroma;
159     if( !p_fmt_out->i_width && p_fmt_out->i_height )
160         p_fmt_out->i_width = p_fmt_out->i_height
161                               * p_image->p_dec->fmt_out.video.i_aspect
162                               / VOUT_ASPECT_FACTOR;
163     if( !p_fmt_out->i_height && p_fmt_out->i_width )
164         p_fmt_out->i_height = p_fmt_out->i_width * VOUT_ASPECT_FACTOR
165                                / p_image->p_dec->fmt_out.video.i_aspect;
166     if( !p_fmt_out->i_width )
167         p_fmt_out->i_width = p_image->p_dec->fmt_out.video.i_width;
168     if( !p_fmt_out->i_height )
169         p_fmt_out->i_height = p_image->p_dec->fmt_out.video.i_height;
170
171     /* Check if we need chroma conversion or resizing */
172     if( p_image->p_dec->fmt_out.video.i_chroma != p_fmt_out->i_chroma ||
173         p_image->p_dec->fmt_out.video.i_width != p_fmt_out->i_width ||
174         p_image->p_dec->fmt_out.video.i_height != p_fmt_out->i_height )
175     {
176         if( p_image->p_filter )
177         if( p_image->p_filter->fmt_in.video.i_chroma !=
178             p_image->p_dec->fmt_out.video.i_chroma ||
179             p_image->p_filter->fmt_out.video.i_chroma != p_fmt_out->i_chroma )
180         {
181             /* We need to restart a new filter */
182             DeleteFilter( p_image->p_filter );
183             p_image->p_filter = 0;
184         }
185
186         /* Start a filter */
187         if( !p_image->p_filter )
188         {
189             p_image->p_filter =
190                 CreateFilter( p_image->p_parent, &p_image->p_dec->fmt_out,
191                               p_fmt_out, NULL );
192
193             if( !p_image->p_filter )
194             {
195                 picture_Release( p_pic );
196                 return NULL;
197             }
198         }
199         else
200         {
201             /* Filters should handle on-the-fly size changes */
202             p_image->p_filter->fmt_in = p_image->p_dec->fmt_out;
203             p_image->p_filter->fmt_out = p_image->p_dec->fmt_out;
204             p_image->p_filter->fmt_out.i_codec = p_fmt_out->i_chroma;
205             p_image->p_filter->fmt_out.video = *p_fmt_out;
206         }
207
208         p_pic = p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
209         *p_fmt_out = p_image->p_filter->fmt_out.video;
210     }
211     else *p_fmt_out = p_image->p_dec->fmt_out.video;
212
213     return p_pic;
214 }
215
216 static picture_t *ImageReadUrl( image_handler_t *p_image, const char *psz_url,
217                                 video_format_t *p_fmt_in,
218                                 video_format_t *p_fmt_out )
219 {
220     block_t *p_block;
221     picture_t *p_pic;
222     stream_t *p_stream = NULL;
223     int i_size;
224
225     p_stream = stream_UrlNew( p_image->p_parent, psz_url );
226
227     if( !p_stream )
228     {
229         msg_Dbg( p_image->p_parent, "could not open %s for reading",
230                  psz_url );
231         return NULL;
232     }
233
234     i_size = stream_Size( p_stream );
235
236     p_block = block_New( p_image->p_parent, i_size );
237
238     stream_Read( p_stream, p_block->p_buffer, i_size );
239
240     if( !p_fmt_in->i_chroma )
241     {
242         char *psz_mime = NULL;
243         stream_Control( p_stream, STREAM_GET_CONTENT_TYPE, &psz_mime );
244         if( psz_mime )
245             p_fmt_in->i_chroma = image_Mime2Fourcc( psz_mime );
246         free( psz_mime );
247     }
248     stream_Delete( p_stream );
249
250     if( !p_fmt_in->i_chroma )
251     {
252         /* Try to guess format from file name */
253         p_fmt_in->i_chroma = image_Ext2Fourcc( psz_url );
254     }
255
256     p_pic = ImageRead( p_image, p_block, p_fmt_in, p_fmt_out );
257
258     return p_pic;
259 }
260
261 /**
262  * Write an image
263  *
264  */
265
266 static block_t *ImageWrite( image_handler_t *p_image, picture_t *p_pic,
267                             video_format_t *p_fmt_in,
268                             video_format_t *p_fmt_out )
269 {
270     block_t *p_block;
271
272     /* Check if we can reuse the current encoder */
273     if( p_image->p_enc &&
274         ( p_image->p_enc->fmt_out.i_codec != p_fmt_out->i_chroma ||
275           p_image->p_enc->fmt_out.video.i_width != p_fmt_out->i_width ||
276           p_image->p_enc->fmt_out.video.i_height != p_fmt_out->i_height ) )
277     {
278         DeleteEncoder( p_image->p_enc );
279         p_image->p_enc = 0;
280     }
281
282     /* Start an encoder */
283     if( !p_image->p_enc )
284     {
285         p_image->p_enc = CreateEncoder( p_image->p_parent,
286                                         p_fmt_in, p_fmt_out );
287         if( !p_image->p_enc ) return NULL;
288     }
289
290     /* Check if we need chroma conversion or resizing */
291     if( p_image->p_enc->fmt_in.video.i_chroma != p_fmt_in->i_chroma ||
292         p_image->p_enc->fmt_in.video.i_width != p_fmt_in->i_width ||
293         p_image->p_enc->fmt_in.video.i_height != p_fmt_in->i_height )
294     {
295         picture_t *p_tmp_pic;
296
297         if( p_image->p_filter )
298         if( p_image->p_filter->fmt_in.video.i_chroma != p_fmt_in->i_chroma ||
299             p_image->p_filter->fmt_out.video.i_chroma !=
300             p_image->p_enc->fmt_in.video.i_chroma )
301         {
302             /* We need to restart a new filter */
303             DeleteFilter( p_image->p_filter );
304             p_image->p_filter = 0;
305         }
306
307         /* Start a filter */
308         if( !p_image->p_filter )
309         {
310             es_format_t fmt_in;
311             es_format_Init( &fmt_in, VIDEO_ES, p_fmt_in->i_chroma );
312             fmt_in.video = *p_fmt_in;
313
314             p_image->p_filter =
315                 CreateFilter( p_image->p_parent, &fmt_in,
316                               &p_image->p_enc->fmt_in.video, NULL );
317
318             if( !p_image->p_filter )
319             {
320                 return NULL;
321             }
322         }
323         else
324         {
325             /* Filters should handle on-the-fly size changes */
326             p_image->p_filter->fmt_in.i_codec = p_fmt_in->i_chroma;
327             p_image->p_filter->fmt_out.video = *p_fmt_in;
328             p_image->p_filter->fmt_out.i_codec =p_image->p_enc->fmt_in.i_codec;
329             p_image->p_filter->fmt_out.video = p_image->p_enc->fmt_in.video;
330         }
331
332         picture_Hold( p_pic );
333
334         p_tmp_pic =
335             p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
336
337         p_block = p_image->p_enc->pf_encode_video( p_image->p_enc, p_tmp_pic );
338
339         p_image->p_filter->pf_vout_buffer_del( p_image->p_filter, p_tmp_pic );
340     }
341     else
342     {
343         p_block = p_image->p_enc->pf_encode_video( p_image->p_enc, p_pic );
344     }
345
346     if( !p_block )
347     {
348         msg_Dbg( p_image->p_parent, "no image encoded" );
349         return 0;
350     }
351
352     return p_block;
353 }
354
355 static int ImageWriteUrl( image_handler_t *p_image, picture_t *p_pic,
356                           video_format_t *p_fmt_in, video_format_t *p_fmt_out,
357                           const char *psz_url )
358 {
359     block_t *p_block;
360     FILE *file;
361
362     if( !p_fmt_out->i_chroma )
363     {
364         /* Try to guess format from file name */
365         p_fmt_out->i_chroma = image_Ext2Fourcc( psz_url );
366     }
367
368     file = utf8_fopen( psz_url, "wb" );
369     if( !file )
370     {
371         msg_Err( p_image->p_parent, "%s: %m", psz_url );
372         return VLC_EGENERIC;
373     }
374
375     p_block = ImageWrite( p_image, p_pic, p_fmt_in, p_fmt_out );
376
377     int err = 0;
378     if( p_block )
379     {
380         if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, file ) != 1 )
381             err = errno;
382         block_Release( p_block );
383     }
384
385     if( fclose( file ) && !err )
386         err = errno;
387
388     if( err )
389     {
390        errno = err;
391        msg_Err( p_image->p_parent, "%s: %m", psz_url );
392     }
393
394     return err ? VLC_EGENERIC : VLC_SUCCESS;
395 }
396
397 /**
398  * Convert an image to a different format
399  *
400  */
401
402 static picture_t *ImageConvert( image_handler_t *p_image, picture_t *p_pic,
403                                 video_format_t *p_fmt_in,
404                                 video_format_t *p_fmt_out )
405 {
406     picture_t *p_pif;
407
408     if( !p_fmt_out->i_width && !p_fmt_out->i_height &&
409         p_fmt_out->i_sar_num && p_fmt_out->i_sar_den &&
410         p_fmt_out->i_sar_num * p_fmt_in->i_sar_den !=
411         p_fmt_out->i_sar_den * p_fmt_in->i_sar_num )
412     {
413         p_fmt_out->i_width =
414             p_fmt_in->i_sar_num * (int64_t)p_fmt_out->i_sar_den *
415             p_fmt_in->i_width / p_fmt_in->i_sar_den / p_fmt_out->i_sar_num;
416         p_fmt_out->i_visible_width =
417             p_fmt_in->i_sar_num * (int64_t)p_fmt_out->i_sar_den *
418             p_fmt_in->i_visible_width / p_fmt_in->i_sar_den /
419             p_fmt_out->i_sar_num;
420     }
421
422     if( !p_fmt_out->i_chroma ) p_fmt_out->i_chroma = p_fmt_in->i_chroma;
423     if( !p_fmt_out->i_width )
424         p_fmt_out->i_width = p_fmt_out->i_visible_width = p_fmt_in->i_width;
425     if( !p_fmt_out->i_height )
426         p_fmt_out->i_height = p_fmt_out->i_visible_height = p_fmt_in->i_height;
427     if( !p_fmt_out->i_sar_num ) p_fmt_out->i_sar_num = p_fmt_in->i_sar_num;
428     if( !p_fmt_out->i_sar_den ) p_fmt_out->i_sar_den = p_fmt_in->i_sar_den;
429     if( !p_fmt_out->i_aspect ) p_fmt_out->i_aspect = p_fmt_in->i_aspect;
430
431     if( p_image->p_filter )
432     if( p_image->p_filter->fmt_in.video.i_chroma != p_fmt_in->i_chroma ||
433         p_image->p_filter->fmt_out.video.i_chroma != p_fmt_out->i_chroma )
434     {
435         /* We need to restart a new filter */
436         DeleteFilter( p_image->p_filter );
437         p_image->p_filter = NULL;
438     }
439
440     /* Start a filter */
441     if( !p_image->p_filter )
442     {
443         es_format_t fmt_in;
444         es_format_Init( &fmt_in, VIDEO_ES, p_fmt_in->i_chroma );
445         fmt_in.video = *p_fmt_in;
446
447         p_image->p_filter =
448             CreateFilter( p_image->p_parent, &fmt_in, p_fmt_out, NULL );
449
450         if( !p_image->p_filter )
451         {
452             return NULL;
453         }
454     }
455     else
456     {
457         /* Filters should handle on-the-fly size changes */
458         p_image->p_filter->fmt_in.video = *p_fmt_in;
459         p_image->p_filter->fmt_out.video = *p_fmt_out;
460     }
461
462     picture_Hold( p_pic );
463
464     p_pif = p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
465
466     if( p_fmt_in->i_chroma == p_fmt_out->i_chroma &&
467         p_fmt_in->i_width == p_fmt_out->i_width &&
468         p_fmt_in->i_height == p_fmt_out->i_height )
469     {
470         /* Duplicate image */
471         picture_Release( p_pif ); /* XXX: Better fix must be possible */
472         p_pif = p_image->p_filter->pf_vout_buffer_new( p_image->p_filter );
473         if( p_pif )
474             picture_Copy( p_pif, p_pic );
475     }
476
477     return p_pif;
478 }
479
480 /**
481  * Filter an image with a psz_module filter
482  *
483  */
484
485 static picture_t *ImageFilter( image_handler_t *p_image, picture_t *p_pic,
486                                video_format_t *p_fmt, const char *psz_module )
487 {
488     /* Start a filter */
489     if( !p_image->p_filter )
490     {
491         es_format_t fmt;
492         es_format_Init( &fmt, VIDEO_ES, p_fmt->i_chroma );
493         fmt.video = *p_fmt;
494
495         p_image->p_filter =
496             CreateFilter( p_image->p_parent, &fmt, &fmt.video, psz_module );
497
498         if( !p_image->p_filter )
499         {
500             return NULL;
501         }
502     }
503     else
504     {
505         /* Filters should handle on-the-fly size changes */
506         p_image->p_filter->fmt_in.video = *p_fmt;
507         p_image->p_filter->fmt_out.video = *p_fmt;
508     }
509
510     picture_Hold( p_pic );
511
512     return p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
513 }
514
515 /**
516  * Misc functions
517  *
518  */
519 static const struct
520 {
521     vlc_fourcc_t i_codec;
522     const char *psz_ext;
523
524 } ext_table[] =
525 {
526     { VLC_FOURCC('j','p','e','g'), "jpeg" },
527     { VLC_FOURCC('j','p','e','g'), "jpg"  },
528     { VLC_FOURCC('l','j','p','g'), "ljpg" },
529     { VLC_FOURCC('p','n','g',' '), "png" },
530     { VLC_FOURCC('p','g','m',' '), "pgm" },
531     { VLC_FOURCC('p','g','m','y'), "pgmyuv" },
532     { VLC_FOURCC('p','b','m',' '), "pbm" },
533     { VLC_FOURCC('p','a','m',' '), "pam" },
534     { VLC_FOURCC('t','g','a',' '), "tga" },
535     { VLC_FOURCC('b','m','p',' '), "bmp" },
536     { VLC_FOURCC('p','n','m',' '), "pnm" },
537     { VLC_FOURCC('x','p','m',' '), "xpm" },
538     { VLC_FOURCC('x','c','f',' '), "xcf" },
539     { VLC_FOURCC('p','c','x',' '), "pcx" },
540     { VLC_FOURCC('g','i','f',' '), "gif" },
541     { VLC_FOURCC('t','i','f','f'), "tif" },
542     { VLC_FOURCC('t','i','f','f'), "tiff" },
543     { VLC_FOURCC('l','b','m',' '), "lbm" },
544     { 0, NULL }
545 };
546
547 vlc_fourcc_t image_Type2Fourcc( const char *psz_type )
548 {
549     int i;
550
551     for( i = 0; ext_table[i].i_codec; i++ )
552         if( !strcasecmp( ext_table[i].psz_ext, psz_type ) )
553             return ext_table[i].i_codec;
554
555     return 0;
556 }
557
558 vlc_fourcc_t image_Ext2Fourcc( const char *psz_name )
559 {
560     psz_name = strrchr( psz_name, '.' );
561     if( !psz_name ) return 0;
562     psz_name++;
563
564     return image_Type2Fourcc( psz_name );
565 }
566
567 /*
568 static const char *Fourcc2Ext( vlc_fourcc_t i_codec )
569 {
570     int i;
571
572     for( i = 0; ext_table[i].i_codec != 0; i++ )
573     {
574         if( ext_table[i].i_codec == i_codec ) return ext_table[i].psz_ext;
575     }
576
577     return NULL;
578 }
579 */
580
581 static const struct
582 {
583     vlc_fourcc_t i_codec;
584     const char *psz_mime;
585 } mime_table[] =
586 {
587     { VLC_FOURCC('b','m','p',' '), "image/bmp" },
588     { VLC_FOURCC('b','m','p',' '), "image/x-bmp" },
589     { VLC_FOURCC('b','m','p',' '), "image/x-bitmap" },
590     { VLC_FOURCC('b','m','p',' '), "image/x-ms-bmp" },
591     { VLC_FOURCC('p','n','m',' '), "image/x-portable-anymap" },
592     { VLC_FOURCC('p','n','m',' '), "image/x-portable-bitmap" },
593     { VLC_FOURCC('p','n','m',' '), "image/x-portable-graymap" },
594     { VLC_FOURCC('p','n','m',' '), "image/x-portable-pixmap" },
595     { VLC_FOURCC('g','i','f',' '), "image/gif" },
596     { VLC_FOURCC('j','p','e','g'), "image/jpeg" },
597     { VLC_FOURCC('p','c','x',' '), "image/pcx" },
598     { VLC_FOURCC('p','n','g',' '), "image/png" },
599     { VLC_FOURCC('t','i','f','f'), "image/tiff" },
600     { VLC_FOURCC('t','g','a',' '), "iamge/x-tga" },
601     { VLC_FOURCC('x','p','m',' '), "image/x-xpixmap" },
602     { 0, NULL }
603 };
604
605 vlc_fourcc_t image_Mime2Fourcc( const char *psz_mime )
606 {
607     int i;
608     for( i = 0; mime_table[i].i_codec; i++ )
609         if( !strcmp( psz_mime, mime_table[i].psz_mime ) )
610             return mime_table[i].i_codec;
611     return 0;
612 }
613
614
615 static picture_t *video_new_buffer( decoder_t *p_dec )
616 {
617     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
618     return picture_New( p_dec->fmt_out.video.i_chroma,
619                         p_dec->fmt_out.video.i_width,
620                         p_dec->fmt_out.video.i_height,
621                         p_dec->fmt_out.video.i_aspect );
622 }
623
624 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
625 {
626     if( p_pic->i_refcount != 1 )
627         msg_Err( p_dec, "invalid picture reference count" );
628
629     p_pic->i_refcount = 0;
630     picture_Delete( p_pic );
631 }
632
633 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
634 {
635     (void)p_dec;
636     picture_Hold( p_pic );
637 }
638
639 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
640 {
641     (void)p_dec;
642     picture_Release( p_pic );
643 }
644
645 static decoder_t *CreateDecoder( vlc_object_t *p_this, video_format_t *fmt )
646 {
647     decoder_t *p_dec;
648
649     p_dec = vlc_object_create( p_this, VLC_OBJECT_DECODER );
650     if( p_dec == NULL )
651         return NULL;
652
653     p_dec->p_module = NULL;
654     es_format_Init( &p_dec->fmt_in, VIDEO_ES, fmt->i_chroma );
655     es_format_Init( &p_dec->fmt_out, VIDEO_ES, 0 );
656     p_dec->fmt_in.video = *fmt;
657     p_dec->b_pace_control = true;
658
659     p_dec->pf_vout_buffer_new = video_new_buffer;
660     p_dec->pf_vout_buffer_del = video_del_buffer;
661     p_dec->pf_picture_link    = video_link_picture;
662     p_dec->pf_picture_unlink  = video_unlink_picture;
663
664     vlc_object_attach( p_dec, p_this );
665
666     /* Find a suitable decoder module */
667     p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
668     if( !p_dec->p_module )
669     {
670         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
671                  "VLC probably does not support this image format.",
672                  (char*)&p_dec->fmt_in.i_codec );
673
674         DeleteDecoder( p_dec );
675         return NULL;
676     }
677
678     return p_dec;
679 }
680
681 static void DeleteDecoder( decoder_t * p_dec )
682 {
683     vlc_object_detach( p_dec );
684
685     if( p_dec->p_module ) module_unneed( p_dec, p_dec->p_module );
686
687     es_format_Clean( &p_dec->fmt_in );
688     es_format_Clean( &p_dec->fmt_out );
689
690     if( p_dec->p_description )
691         vlc_meta_Delete( p_dec->p_description );
692
693     vlc_object_release( p_dec );
694     p_dec = NULL;
695 }
696
697 static encoder_t *CreateEncoder( vlc_object_t *p_this, video_format_t *fmt_in,
698                                  video_format_t *fmt_out )
699 {
700     encoder_t *p_enc;
701
702     p_enc = sout_EncoderCreate( p_this );
703     if( p_enc == NULL )
704         return NULL;
705
706     p_enc->p_module = NULL;
707     es_format_Init( &p_enc->fmt_in, VIDEO_ES, fmt_in->i_chroma );
708     p_enc->fmt_in.video = *fmt_in;
709     if( fmt_out->i_width > 0 && fmt_out->i_height > 0 )
710     {
711         p_enc->fmt_in.video.i_width = fmt_out->i_width;
712         p_enc->fmt_in.video.i_height = fmt_out->i_height;
713
714         if( fmt_out->i_visible_width > 0 &&
715             fmt_out->i_visible_height > 0 )
716         {
717             p_enc->fmt_in.video.i_visible_width = fmt_out->i_visible_width;
718             p_enc->fmt_in.video.i_visible_height = fmt_out->i_visible_height;
719         }
720         else
721         {
722             p_enc->fmt_in.video.i_visible_width = fmt_out->i_width;
723             p_enc->fmt_in.video.i_visible_height = fmt_out->i_height;
724         }
725     }
726     else if( fmt_out->i_sar_num && fmt_out->i_sar_den &&
727              fmt_out->i_sar_num * fmt_in->i_sar_den !=
728              fmt_out->i_sar_den * fmt_in->i_sar_num )
729     {
730         p_enc->fmt_in.video.i_width =
731             fmt_in->i_sar_num * (int64_t)fmt_out->i_sar_den * fmt_in->i_width /
732             fmt_in->i_sar_den / fmt_out->i_sar_num;
733         p_enc->fmt_in.video.i_visible_width =
734             fmt_in->i_sar_num * (int64_t)fmt_out->i_sar_den *
735             fmt_in->i_visible_width / fmt_in->i_sar_den / fmt_out->i_sar_num;
736     }
737
738     p_enc->fmt_in.video.i_frame_rate = 25;
739     p_enc->fmt_in.video.i_frame_rate_base = 1;
740
741     es_format_Init( &p_enc->fmt_out, VIDEO_ES, fmt_out->i_chroma );
742     p_enc->fmt_out.video = *fmt_out;
743     p_enc->fmt_out.video.i_width = p_enc->fmt_in.video.i_width;
744     p_enc->fmt_out.video.i_height = p_enc->fmt_in.video.i_height;
745
746     vlc_object_attach( p_enc, p_this );
747
748     /* Find a suitable decoder module */
749     p_enc->p_module = module_need( p_enc, "encoder", NULL, false );
750     if( !p_enc->p_module )
751     {
752         msg_Err( p_enc, "no suitable encoder module for fourcc `%4.4s'.\n"
753                  "VLC probably does not support this image format.",
754                  (char*)&p_enc->fmt_out.i_codec );
755
756         DeleteEncoder( p_enc );
757         return NULL;
758     }
759     p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
760
761     return p_enc;
762 }
763
764 static void DeleteEncoder( encoder_t * p_enc )
765 {
766     vlc_object_detach( p_enc );
767
768     if( p_enc->p_module ) module_unneed( p_enc, p_enc->p_module );
769
770     es_format_Clean( &p_enc->fmt_in );
771     es_format_Clean( &p_enc->fmt_out );
772
773     vlc_object_release( p_enc );
774     p_enc = NULL;
775 }
776
777 static filter_t *CreateFilter( vlc_object_t *p_this, es_format_t *p_fmt_in,
778                                video_format_t *p_fmt_out,
779                                const char *psz_module )
780 {
781     static const char typename[] = "filter";
782     filter_t *p_filter;
783
784     p_filter = vlc_custom_create( p_this, sizeof(filter_t),
785                                   VLC_OBJECT_GENERIC, typename );
786     vlc_object_attach( p_filter, p_this );
787
788     p_filter->pf_vout_buffer_new =
789         (picture_t *(*)(filter_t *))video_new_buffer;
790     p_filter->pf_vout_buffer_del =
791         (void (*)(filter_t *, picture_t *))video_del_buffer;
792
793     p_filter->fmt_in = *p_fmt_in;
794     p_filter->fmt_out = *p_fmt_in;
795     p_filter->fmt_out.i_codec = p_fmt_out->i_chroma;
796     p_filter->fmt_out.video = *p_fmt_out;
797     p_filter->p_module = module_need( p_filter, "video filter2",
798                                       psz_module, false );
799
800     if( !p_filter->p_module )
801     {
802         msg_Dbg( p_filter, "no video filter found" );
803         DeleteFilter( p_filter );
804         return NULL;
805     }
806
807     return p_filter;
808 }
809
810 static void DeleteFilter( filter_t * p_filter )
811 {
812     vlc_object_detach( p_filter );
813
814     if( p_filter->p_module ) module_unneed( p_filter, p_filter->p_module );
815
816     es_format_Clean( &p_filter->fmt_in );
817     es_format_Clean( &p_filter->fmt_out );
818
819     vlc_object_release( p_filter );
820 }