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