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 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 *,
51 static void DeleteEncoder( encoder_t * );
52 static filter_t *CreateFilter( vlc_object_t *, es_format_t *,
54 static void DeleteFilter( filter_t * );
56 static vlc_fourcc_t Ext2Fourcc( const char * );
57 static const char *Fourcc2Ext( vlc_fourcc_t );
60 * Create an image_handler_t instance
63 image_handler_t *__image_HandlerCreate( vlc_object_t *p_this )
65 image_handler_t *p_image = malloc( sizeof(image_handler_t) );
67 memset( p_image, 0, sizeof(image_handler_t) );
68 p_image->p_parent = p_this;
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;
79 * Delete the image_handler_t instance
82 void image_HandlerDelete( image_handler_t *p_image )
84 if( !p_image ) return;
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 );
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 )
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 )
108 DeleteDecoder( p_image->p_dec );
112 /* Start a decoder */
113 if( !p_image->p_dec )
115 p_image->p_dec = CreateDecoder( p_image->p_parent, p_fmt_in );
116 if( !p_image->p_dec ) return NULL;
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 );
125 msg_Dbg( p_image->p_parent, "no image decoded" );
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;
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 )
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 )
146 /* We need to restart a new filter */
147 DeleteFilter( p_image->p_filter );
148 p_image->p_filter = 0;
152 if( !p_image->p_filter )
155 CreateFilter( p_image->p_parent, &p_image->p_dec->fmt_out,
158 if( !p_image->p_filter )
160 p_pic->pf_release( p_pic );
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;
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;
176 else *p_fmt_out = p_image->p_dec->fmt_out.video;
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 )
190 file = fopen( psz_url, "rb" );
193 msg_Dbg( p_image->p_parent, "could not open file %s for reading",
198 fseek( file, 0, SEEK_END );
199 i_size = ftell( file );
200 fseek( file, 0, SEEK_SET );
202 p_block = block_New( p_image->p_parent, i_size );
203 fread( p_block->p_buffer, sizeof(char), i_size, file );
206 if( !p_fmt_in->i_chroma )
208 /* Try to guess format from file name */
209 p_fmt_in->i_chroma = Ext2Fourcc( psz_url );
212 p_pic = ImageRead( p_image, p_block, p_fmt_in, p_fmt_out );
222 void PicRelease( picture_t *p_pic ){};
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 )
229 void (*pf_release)( picture_t * );
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 ) )
237 DeleteEncoder( p_image->p_enc );
241 /* Start an encoder */
242 if( !p_image->p_enc )
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;
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 )
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 )
261 /* We need to restart a new filter */
262 DeleteFilter( p_image->p_filter );
263 p_image->p_filter = 0;
267 if( !p_image->p_filter )
270 es_format_Init( &fmt_in, VIDEO_ES, p_fmt_in->i_chroma );
271 fmt_in.video = *p_fmt_in;
274 CreateFilter( p_image->p_parent, &fmt_in,
275 &p_image->p_enc->fmt_in.video );
277 if( !p_image->p_filter )
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;
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;
298 p_block = p_image->p_enc->pf_encode_video( p_image->p_enc, p_pic );
302 msg_Dbg( p_image->p_parent, "no image encoded" );
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 )
316 if( !p_fmt_out->i_chroma )
318 /* Try to guess format from file name */
319 p_fmt_out->i_chroma = Ext2Fourcc( psz_url );
322 file = fopen( psz_url, "wb" );
325 msg_Dbg( p_image->p_parent, "could not open file %s for writing",
330 p_block = ImageWrite( p_image, p_pic, p_fmt_in, p_fmt_out );
334 fwrite( p_block->p_buffer, sizeof(char), p_block->i_buffer, file );
335 block_Release( p_block );
340 return p_block ? VLC_SUCCESS : VLC_EGENERIC;
349 vlc_fourcc_t i_codec;
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" },
365 static vlc_fourcc_t Ext2Fourcc( const char *psz_name )
369 psz_name = strrchr( psz_name, '.' );
370 if( !psz_name ) return 0;
373 for( i = 0; ext_table[i].i_codec; i++ )
376 for( j = 0; toupper(ext_table[i].psz_ext[j]) == toupper(psz_name[j]);
379 if( !ext_table[i].psz_ext[j] && !psz_name[j] )
380 return ext_table[i].i_codec;
387 static const char *Fourcc2Ext( vlc_fourcc_t i_codec )
391 for( i = 0; ext_table[i].i_codec != 0; i++ )
393 if( ext_table[i].i_codec == i_codec ) return ext_table[i].psz_ext;
399 static void video_release_buffer( picture_t *p_pic )
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 );
406 static picture_t *video_new_buffer( decoder_t *p_dec )
408 picture_t *p_pic = malloc( sizeof(picture_t) );
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 );
417 if( !p_pic->i_planes )
423 p_pic->pf_release = video_release_buffer;
424 p_pic->i_status = RESERVED_PICTURE;
430 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
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 );
437 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
441 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
445 static decoder_t *CreateDecoder( vlc_object_t *p_this, video_format_t *fmt )
449 p_dec = vlc_object_create( p_this, VLC_OBJECT_DECODER );
452 msg_Err( p_this, "out of memory" );
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;
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;
467 vlc_object_attach( p_dec, p_this );
469 /* Find a suitable decoder module */
470 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
471 if( !p_dec->p_module )
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 );
477 DeleteDecoder( p_dec );
484 static void DeleteDecoder( decoder_t * p_dec )
486 vlc_object_detach( p_dec );
488 if( p_dec->p_module ) module_Unneed( p_dec, p_dec->p_module );
490 es_format_Clean( &p_dec->fmt_in );
491 es_format_Clean( &p_dec->fmt_out );
493 vlc_object_destroy( p_dec );
496 static encoder_t *CreateEncoder( vlc_object_t *p_this, video_format_t *fmt_in,
497 video_format_t *fmt_out )
501 p_enc = vlc_object_create( p_this, VLC_OBJECT_ENCODER );
504 msg_Err( p_this, "out of memory" );
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 )
513 p_enc->fmt_in.video.i_width = fmt_out->i_width;
514 p_enc->fmt_in.video.i_height = fmt_out->i_height;
516 p_enc->fmt_in.video.i_frame_rate = 25;
517 p_enc->fmt_in.video.i_frame_rate_base = 1;
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;
524 vlc_object_attach( p_enc, p_this );
526 /* Find a suitable decoder module */
527 p_enc->p_module = module_Need( p_enc, "encoder", 0, 0 );
528 if( !p_enc->p_module )
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 );
534 DeleteEncoder( p_enc );
537 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
542 static void DeleteEncoder( encoder_t * p_enc )
544 vlc_object_detach( p_enc );
546 if( p_enc->p_module ) module_Unneed( p_enc, p_enc->p_module );
548 es_format_Clean( &p_enc->fmt_in );
549 es_format_Clean( &p_enc->fmt_out );
551 vlc_object_destroy( p_enc );
554 static filter_t *CreateFilter( vlc_object_t *p_this, es_format_t *p_fmt_in,
555 video_format_t *p_fmt_out )
559 p_filter = vlc_object_create( p_this, VLC_OBJECT_FILTER );
560 vlc_object_attach( p_filter, p_this );
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;
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 );
573 if( !p_filter->p_module )
575 msg_Dbg( p_filter, "no video filter found" );
576 DeleteFilter( p_filter );
583 static void DeleteFilter( filter_t * p_filter )
585 vlc_object_detach( p_filter );
587 if( p_filter->p_module ) module_Unneed( p_filter, p_filter->p_module );
589 es_format_Clean( &p_filter->fmt_in );
590 es_format_Clean( &p_filter->fmt_out );
592 vlc_object_destroy( p_filter );