1 /*****************************************************************************
2 * image.c : wrapper for image reading/writing facilities
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Author: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
26 * This file contains the functions to handle the image_handler_t type
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <vlc/decoder.h>
35 #include <vlc_filter.h>
36 #include <vlc_image.h>
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 * );
47 static picture_t *ImageConvert( image_handler_t *, picture_t *,
48 video_format_t *, video_format_t * );
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 *,
54 static void DeleteEncoder( encoder_t * );
55 static filter_t *CreateFilter( vlc_object_t *, es_format_t *,
57 static void DeleteFilter( filter_t * );
59 static vlc_fourcc_t Ext2Fourcc( const char * );
60 static const char *Fourcc2Ext( vlc_fourcc_t );
63 * Create an image_handler_t instance
66 image_handler_t *__image_HandlerCreate( vlc_object_t *p_this )
68 image_handler_t *p_image = malloc( sizeof(image_handler_t) );
70 memset( p_image, 0, sizeof(image_handler_t) );
71 p_image->p_parent = p_this;
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;
83 * Delete the image_handler_t instance
86 void image_HandlerDelete( image_handler_t *p_image )
88 if( !p_image ) return;
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 );
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 )
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 )
112 DeleteDecoder( p_image->p_dec );
116 /* Start a decoder */
117 if( !p_image->p_dec )
119 p_image->p_dec = CreateDecoder( p_image->p_parent, p_fmt_in );
120 if( !p_image->p_dec ) return NULL;
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 );
129 msg_Dbg( p_image->p_parent, "no image decoded" );
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;
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 )
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 )
150 /* We need to restart a new filter */
151 DeleteFilter( p_image->p_filter );
152 p_image->p_filter = 0;
156 if( !p_image->p_filter )
159 CreateFilter( p_image->p_parent, &p_image->p_dec->fmt_out,
162 if( !p_image->p_filter )
164 p_pic->pf_release( p_pic );
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;
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;
180 else *p_fmt_out = p_image->p_dec->fmt_out.video;
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 )
194 file = fopen( psz_url, "rb" );
197 msg_Dbg( p_image->p_parent, "could not open file %s for reading",
202 fseek( file, 0, SEEK_END );
203 i_size = ftell( file );
204 fseek( file, 0, SEEK_SET );
206 p_block = block_New( p_image->p_parent, i_size );
207 fread( p_block->p_buffer, sizeof(char), i_size, file );
210 if( !p_fmt_in->i_chroma )
212 /* Try to guess format from file name */
213 p_fmt_in->i_chroma = Ext2Fourcc( psz_url );
216 p_pic = ImageRead( p_image, p_block, p_fmt_in, p_fmt_out );
226 void PicRelease( picture_t *p_pic ){};
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 )
233 void (*pf_release)( picture_t * );
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 ) )
241 DeleteEncoder( p_image->p_enc );
245 /* Start an encoder */
246 if( !p_image->p_enc )
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;
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 )
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 )
265 /* We need to restart a new filter */
266 DeleteFilter( p_image->p_filter );
267 p_image->p_filter = 0;
271 if( !p_image->p_filter )
274 es_format_Init( &fmt_in, VIDEO_ES, p_fmt_in->i_chroma );
275 fmt_in.video = *p_fmt_in;
278 CreateFilter( p_image->p_parent, &fmt_in,
279 &p_image->p_enc->fmt_in.video );
281 if( !p_image->p_filter )
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;
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;
302 p_block = p_image->p_enc->pf_encode_video( p_image->p_enc, p_pic );
306 msg_Dbg( p_image->p_parent, "no image encoded" );
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 )
320 if( !p_fmt_out->i_chroma )
322 /* Try to guess format from file name */
323 p_fmt_out->i_chroma = Ext2Fourcc( psz_url );
326 file = fopen( psz_url, "wb" );
329 msg_Dbg( p_image->p_parent, "could not open file %s for writing",
334 p_block = ImageWrite( p_image, p_pic, p_fmt_in, p_fmt_out );
338 fwrite( p_block->p_buffer, sizeof(char), p_block->i_buffer, file );
339 block_Release( p_block );
344 return p_block ? VLC_SUCCESS : VLC_EGENERIC;
348 * Convert an image to a different format
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 )
356 void (*pf_release)( picture_t * );
359 if( !p_fmt_out->i_chroma ) p_fmt_out->i_chroma = p_fmt_in->i_chroma;
360 if( !p_fmt_out->i_width ) p_fmt_out->i_width = p_fmt_in->i_width;
361 if( !p_fmt_out->i_height ) p_fmt_out->i_height = p_fmt_in->i_height;
363 if( p_image->p_filter )
364 if( p_image->p_filter->fmt_in.video.i_chroma != p_fmt_in->i_chroma ||
365 p_image->p_filter->fmt_out.video.i_chroma != p_fmt_out->i_chroma )
367 /* We need to restart a new filter */
368 DeleteFilter( p_image->p_filter );
369 p_image->p_filter = 0;
373 if( !p_image->p_filter )
376 es_format_Init( &fmt_in, VIDEO_ES, p_fmt_in->i_chroma );
377 fmt_in.video = *p_fmt_in;
380 CreateFilter( p_image->p_parent, &fmt_in, p_fmt_out );
382 if( !p_image->p_filter )
389 /* Filters should handle on-the-fly size changes */
390 p_image->p_filter->fmt_in.video = *p_fmt_in;
391 p_image->p_filter->fmt_out.video = *p_fmt_out;
394 pf_release = p_pic->pf_release;
395 p_pic->pf_release = PicRelease; /* Small hack */
396 p_pif = p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
397 p_pic->pf_release = pf_release;
399 if( p_fmt_in->i_chroma == p_fmt_out->i_chroma &&
400 p_fmt_in->i_width == p_fmt_out->i_width &&
401 p_fmt_in->i_height == p_fmt_out->i_height )
403 /* Duplicate image */
404 p_pif = p_image->p_filter->pf_vout_buffer_new( p_image->p_filter );
405 if( p_pif ) vout_CopyPicture( p_image->p_parent, p_pif, p_pic );
417 vlc_fourcc_t i_codec;
422 { VLC_FOURCC('j','p','e','g'), "jpeg" },
423 { VLC_FOURCC('j','p','e','g'), "jpg" },
424 { VLC_FOURCC('l','j','p','g'), "ljpg" },
425 { VLC_FOURCC('p','n','g',' '), "png" },
426 { VLC_FOURCC('p','g','m',' '), "pgm" },
427 { VLC_FOURCC('p','g','m','y'), "pgmyuv" },
428 { VLC_FOURCC('p','b','m',' '), "pbm" },
429 { VLC_FOURCC('p','a','m',' '), "pam" },
433 static vlc_fourcc_t Ext2Fourcc( const char *psz_name )
437 psz_name = strrchr( psz_name, '.' );
438 if( !psz_name ) return 0;
441 for( i = 0; ext_table[i].i_codec; i++ )
444 for( j = 0; toupper(ext_table[i].psz_ext[j]) == toupper(psz_name[j]);
447 if( !ext_table[i].psz_ext[j] && !psz_name[j] )
448 return ext_table[i].i_codec;
455 static const char *Fourcc2Ext( vlc_fourcc_t i_codec )
459 for( i = 0; ext_table[i].i_codec != 0; i++ )
461 if( ext_table[i].i_codec == i_codec ) return ext_table[i].psz_ext;
467 static void video_release_buffer( picture_t *p_pic )
469 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
470 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
471 if( p_pic ) free( p_pic );
474 static picture_t *video_new_buffer( decoder_t *p_dec )
476 picture_t *p_pic = malloc( sizeof(picture_t) );
478 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
479 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
480 p_dec->fmt_out.video.i_chroma,
481 p_dec->fmt_out.video.i_width,
482 p_dec->fmt_out.video.i_height,
483 p_dec->fmt_out.video.i_aspect );
485 if( !p_pic->i_planes )
491 p_pic->pf_release = video_release_buffer;
492 p_pic->i_status = RESERVED_PICTURE;
498 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
500 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
501 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
502 if( p_pic ) free( p_pic );
505 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
509 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
513 static decoder_t *CreateDecoder( vlc_object_t *p_this, video_format_t *fmt )
517 p_dec = vlc_object_create( p_this, VLC_OBJECT_DECODER );
520 msg_Err( p_this, "out of memory" );
524 p_dec->p_module = NULL;
525 es_format_Init( &p_dec->fmt_in, VIDEO_ES, fmt->i_chroma );
526 es_format_Init( &p_dec->fmt_out, VIDEO_ES, 0 );
527 p_dec->fmt_in.video = *fmt;
528 p_dec->b_pace_control = VLC_TRUE;
530 p_dec->pf_vout_buffer_new = video_new_buffer;
531 p_dec->pf_vout_buffer_del = video_del_buffer;
532 p_dec->pf_picture_link = video_link_picture;
533 p_dec->pf_picture_unlink = video_unlink_picture;
535 vlc_object_attach( p_dec, p_this );
537 /* Find a suitable decoder module */
538 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
539 if( !p_dec->p_module )
541 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
542 "VLC probably does not support this image format.",
543 (char*)&p_dec->fmt_in.i_codec );
545 DeleteDecoder( p_dec );
552 static void DeleteDecoder( decoder_t * p_dec )
554 vlc_object_detach( p_dec );
556 if( p_dec->p_module ) module_Unneed( p_dec, p_dec->p_module );
558 es_format_Clean( &p_dec->fmt_in );
559 es_format_Clean( &p_dec->fmt_out );
561 vlc_object_destroy( p_dec );
564 static encoder_t *CreateEncoder( vlc_object_t *p_this, video_format_t *fmt_in,
565 video_format_t *fmt_out )
569 p_enc = vlc_object_create( p_this, VLC_OBJECT_ENCODER );
572 msg_Err( p_this, "out of memory" );
576 p_enc->p_module = NULL;
577 es_format_Init( &p_enc->fmt_in, VIDEO_ES, fmt_in->i_chroma );
578 p_enc->fmt_in.video = *fmt_in;
579 if( fmt_out->i_width > 0 && fmt_out->i_height > 0 )
581 p_enc->fmt_in.video.i_width = fmt_out->i_width;
582 p_enc->fmt_in.video.i_height = fmt_out->i_height;
584 p_enc->fmt_in.video.i_frame_rate = 25;
585 p_enc->fmt_in.video.i_frame_rate_base = 1;
587 es_format_Init( &p_enc->fmt_out, VIDEO_ES, fmt_out->i_chroma );
588 p_enc->fmt_out.video = *fmt_out;
589 p_enc->fmt_out.video.i_width = p_enc->fmt_in.video.i_width;
590 p_enc->fmt_out.video.i_height = p_enc->fmt_in.video.i_height;
592 vlc_object_attach( p_enc, p_this );
594 /* Find a suitable decoder module */
595 p_enc->p_module = module_Need( p_enc, "encoder", 0, 0 );
596 if( !p_enc->p_module )
598 msg_Err( p_enc, "no suitable encoder module for fourcc `%4.4s'.\n"
599 "VLC probably does not support this image format.",
600 (char*)&p_enc->fmt_out.i_codec );
602 DeleteEncoder( p_enc );
605 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
610 static void DeleteEncoder( encoder_t * p_enc )
612 vlc_object_detach( p_enc );
614 if( p_enc->p_module ) module_Unneed( p_enc, p_enc->p_module );
616 es_format_Clean( &p_enc->fmt_in );
617 es_format_Clean( &p_enc->fmt_out );
619 vlc_object_destroy( p_enc );
622 static filter_t *CreateFilter( vlc_object_t *p_this, es_format_t *p_fmt_in,
623 video_format_t *p_fmt_out )
627 p_filter = vlc_object_create( p_this, VLC_OBJECT_FILTER );
628 vlc_object_attach( p_filter, p_this );
630 p_filter->pf_vout_buffer_new =
631 (picture_t *(*)(filter_t *))video_new_buffer;
632 p_filter->pf_vout_buffer_del =
633 (void (*)(filter_t *, picture_t *))video_del_buffer;
635 p_filter->fmt_in = *p_fmt_in;
636 p_filter->fmt_out = *p_fmt_in;
637 p_filter->fmt_out.i_codec = p_fmt_out->i_chroma;
638 p_filter->fmt_out.video = *p_fmt_out;
639 p_filter->p_module = module_Need( p_filter, "video filter2", 0, 0 );
641 if( !p_filter->p_module )
643 msg_Dbg( p_filter, "no video filter found" );
644 DeleteFilter( p_filter );
651 static void DeleteFilter( filter_t * p_filter )
653 vlc_object_detach( p_filter );
655 if( p_filter->p_module ) module_Unneed( p_filter, p_filter->p_module );
657 es_format_Clean( &p_filter->fmt_in );
658 es_format_Clean( &p_filter->fmt_out );
660 vlc_object_destroy( p_filter );