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