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