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