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