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