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 *****************************************************************************/
33 #include <vlc/decoder.h>
34 #include <vlc_filter.h>
35 #include <vlc_image.h>
37 static picture_t *ImageRead( image_handler_t *, block_t *,
38 video_format_t *, video_format_t * );
39 static picture_t *ImageReadUrl( image_handler_t *, const char *,
40 video_format_t *, video_format_t * );
41 static block_t *ImageWrite( image_handler_t *, picture_t *,
42 video_format_t *, video_format_t * );
43 static int ImageWriteUrl( image_handler_t *, picture_t *,
44 video_format_t *, video_format_t *, const char * );
46 static decoder_t *CreateDecoder( vlc_object_t *, video_format_t * );
47 static void DeleteDecoder( decoder_t * );
48 static filter_t *CreateFilter( vlc_object_t *, es_format_t *,
50 static void DeleteFilter( filter_t * );
53 * Create an image_handler_t instance
56 image_handler_t *__image_HandlerCreate( vlc_object_t *p_this )
58 image_handler_t *p_image = malloc( sizeof(image_handler_t) );
60 memset( p_image, 0, sizeof(image_handler_t) );
61 p_image->p_parent = p_this;
63 p_image->pf_read = ImageRead;
64 p_image->pf_read_url = ImageReadUrl;
65 p_image->pf_write = ImageWrite;
66 p_image->pf_write_url = ImageWriteUrl;
72 * Delete the image_handler_t instance
75 void image_HandlerDelete( image_handler_t *p_image )
77 if( !p_image ) return;
79 if( p_image->p_dec ) DeleteDecoder( p_image->p_dec );
80 if( p_image->p_filter ) DeleteFilter( p_image->p_filter );
90 static picture_t *ImageRead( image_handler_t *p_image, block_t *p_block,
91 video_format_t *p_fmt_in,
92 video_format_t *p_fmt_out )
96 /* Check if we can reuse the current decoder */
98 p_image->p_dec->fmt_in.i_codec != p_fmt_in->i_chroma )
100 DeleteDecoder( p_image->p_dec );
104 /* Start a decoder */
105 if( !p_image->p_dec )
107 p_image->p_dec = CreateDecoder( p_image->p_parent, p_fmt_in );
108 if( !p_image->p_dec ) return NULL;
111 p_block->i_pts = p_block->i_dts = mdate();
112 p_pic = p_image->p_dec->pf_decode_video( p_image->p_dec, &p_block );
113 p_image->p_dec->pf_decode_video( p_image->p_dec, &p_block );
117 msg_Dbg( p_image->p_parent, "no image decoded" );
121 if( !p_fmt_out->i_chroma )
122 p_fmt_out->i_chroma = p_image->p_dec->fmt_out.video.i_chroma;
123 if( !p_fmt_out->i_width )
124 p_fmt_out->i_width = p_image->p_dec->fmt_out.video.i_width;
125 if( !p_fmt_out->i_height )
126 p_fmt_out->i_height = p_image->p_dec->fmt_out.video.i_height;
128 /* Check if we need chroma conversion or resizing */
129 if( p_image->p_dec->fmt_out.video.i_chroma != p_fmt_out->i_chroma ||
130 p_image->p_dec->fmt_out.video.i_width != p_fmt_out->i_width ||
131 p_image->p_dec->fmt_out.video.i_height != p_fmt_out->i_height )
133 if( p_image->p_filter )
134 if( p_image->p_filter->fmt_in.video.i_chroma !=
135 p_image->p_dec->fmt_out.video.i_chroma ||
136 p_image->p_filter->fmt_in.video.i_width !=
137 p_image->p_dec->fmt_out.video.i_width ||
138 p_image->p_filter->fmt_in.video.i_height !=
139 p_image->p_dec->fmt_out.video.i_height ||
140 p_image->p_filter->fmt_out.video.i_chroma != p_fmt_out->i_chroma ||
141 p_image->p_filter->fmt_out.video.i_width != p_fmt_out->i_width ||
142 p_image->p_filter->fmt_out.video.i_height != p_fmt_out->i_height )
144 /* We need to restart a new filter */
145 DeleteFilter( p_image->p_filter );
146 p_image->p_filter = 0;
150 if( !p_image->p_filter )
153 CreateFilter( p_image->p_parent, &p_image->p_dec->fmt_out,
156 if( !p_image->p_filter )
158 p_pic->pf_release( p_pic );
163 p_pic = p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
164 *p_fmt_out = p_image->p_filter->fmt_out.video;
166 else *p_fmt_out = p_image->p_dec->fmt_out.video;
171 static picture_t *ImageReadUrl( image_handler_t *p_image, const char *psz_url,
172 video_format_t *p_fmt_in,
173 video_format_t *p_fmt_out )
180 file = fopen( psz_url, "rb" );
183 msg_Dbg( p_image->p_parent, "could not open file %s for reading",
188 fseek( file, 0, SEEK_END );
189 i_size = ftell( file );
190 fseek( file, 0, SEEK_SET );
192 p_block = block_New( p_image->p_parent, i_size );
193 fread( p_block->p_buffer, sizeof(char), i_size, file );
196 p_pic = ImageRead( p_image, p_block, p_fmt_in, p_fmt_out );
206 static block_t *ImageWrite( image_handler_t *p_image, picture_t *p_pic,
207 video_format_t *p_fmt_in,
208 video_format_t *p_fmt_out )
213 static int ImageWriteUrl( image_handler_t *p_image, picture_t *p_pic,
214 video_format_t *p_fmt_in, video_format_t *p_fmt_out,
215 const char *psz_url )
224 static void video_release_buffer( picture_t *p_pic )
226 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
227 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
228 if( p_pic ) free( p_pic );
231 static picture_t *video_new_buffer( decoder_t *p_dec )
233 picture_t *p_pic = malloc( sizeof(picture_t) );
235 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
236 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
237 p_dec->fmt_out.video.i_chroma,
238 p_dec->fmt_out.video.i_width,
239 p_dec->fmt_out.video.i_height,
240 p_dec->fmt_out.video.i_aspect );
242 if( !p_pic->i_planes )
248 p_pic->pf_release = video_release_buffer;
249 p_pic->i_status = RESERVED_PICTURE;
255 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
257 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
258 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
259 if( p_pic ) free( p_pic );
262 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
266 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
270 static decoder_t *CreateDecoder( vlc_object_t *p_this, video_format_t *fmt )
273 static es_format_t null_es_format = {0};
275 p_dec = vlc_object_create( p_this, VLC_OBJECT_DECODER );
278 msg_Err( p_this, "out of memory" );
282 p_dec->p_module = NULL;
283 es_format_Copy( &p_dec->fmt_in, &null_es_format );
284 es_format_Copy( &p_dec->fmt_out, &null_es_format );
285 p_dec->fmt_in.video = *fmt;
286 p_dec->fmt_in.i_cat = VIDEO_ES;
287 p_dec->fmt_in.i_codec = fmt->i_chroma;
289 p_dec->pf_vout_buffer_new = video_new_buffer;
290 p_dec->pf_vout_buffer_del = video_del_buffer;
291 p_dec->pf_picture_link = video_link_picture;
292 p_dec->pf_picture_unlink = video_unlink_picture;
294 vlc_object_attach( p_dec, p_this );
296 /* Find a suitable decoder module */
297 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
298 if( !p_dec->p_module )
300 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
301 "VLC probably does not support this image format.",
302 (char*)&p_dec->fmt_in.i_codec );
304 DeleteDecoder( p_dec );
311 static void DeleteDecoder( decoder_t * p_dec )
313 vlc_object_detach( p_dec );
315 if( p_dec->p_module ) module_Unneed( p_dec, p_dec->p_module );
317 es_format_Clean( &p_dec->fmt_in );
318 es_format_Clean( &p_dec->fmt_out );
320 vlc_object_destroy( p_dec );
323 static filter_t *CreateFilter( vlc_object_t *p_this, es_format_t *p_fmt_in,
324 video_format_t *p_fmt_out )
328 p_filter = vlc_object_create( p_this, VLC_OBJECT_FILTER );
329 vlc_object_attach( p_filter, p_this );
331 p_filter->pf_vout_buffer_new =
332 (picture_t *(*)(filter_t *))video_new_buffer;
333 p_filter->pf_vout_buffer_del =
334 (void (*)(filter_t *, picture_t *))video_del_buffer;
336 p_filter->fmt_in = *p_fmt_in;
337 p_filter->fmt_out = *p_fmt_in;
338 p_filter->fmt_out.i_codec = p_fmt_out->i_chroma;
339 p_filter->fmt_out.video = *p_fmt_out;
340 p_filter->p_module = module_Need( p_filter, "video filter2", 0, 0 );
342 if( !p_filter->p_module )
344 msg_Dbg( p_filter, "no video filter found" );
345 DeleteFilter( p_filter );
352 static void DeleteFilter( filter_t * p_filter )
354 vlc_object_detach( p_filter );
356 if( p_filter->p_module ) module_Unneed( p_filter, p_filter->p_module );
358 es_format_Clean( &p_filter->fmt_in );
359 es_format_Clean( &p_filter->fmt_out );
361 vlc_object_destroy( p_filter );