]> git.sesse.net Git - vlc/blob - src/misc/image.c
* src/misc/image.c, include/vlc_image.h: new image handler facility making use of...
[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 <vlc/vlc.h>
33 #include <vlc/decoder.h>
34 #include <vlc_filter.h>
35 #include <vlc_image.h>
36
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 * );
45
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 *,
49                                video_format_t * );
50 static void DeleteFilter( filter_t * );
51
52 /**
53  * Create an image_handler_t instance
54  *
55  */
56 image_handler_t *__image_HandlerCreate( vlc_object_t *p_this )
57 {
58     image_handler_t *p_image = malloc( sizeof(image_handler_t) );
59
60     memset( p_image, 0, sizeof(image_handler_t) );
61     p_image->p_parent = p_this;
62
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;
67
68     return p_image;
69 }
70
71 /**
72  * Delete the image_handler_t instance
73  *
74  */
75 void image_HandlerDelete( image_handler_t *p_image )
76 {
77     if( !p_image ) return;
78
79     if( p_image->p_dec ) DeleteDecoder( p_image->p_dec );
80     if( p_image->p_filter ) DeleteFilter( p_image->p_filter );
81
82     free( p_image );
83 }
84
85 /**
86  * Read an image
87  *
88  */
89
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 )
93 {
94     picture_t *p_pic;
95
96     /* Check if we can reuse the current decoder */
97     if( p_image->p_dec &&
98         p_image->p_dec->fmt_in.i_codec != p_fmt_in->i_chroma )
99     {
100         DeleteDecoder( p_image->p_dec );
101         p_image->p_dec = 0;
102     }
103
104     /* Start a decoder */
105     if( !p_image->p_dec )
106     {
107         p_image->p_dec = CreateDecoder( p_image->p_parent, p_fmt_in );
108         if( !p_image->p_dec ) return NULL;
109     }
110
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 );
114
115     if( !p_pic )
116     {
117         msg_Dbg( p_image->p_parent, "no image decoded" );
118         return 0;
119     }
120
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;
127
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 )
132     {
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 )
143         {
144             /* We need to restart a new filter */
145             DeleteFilter( p_image->p_filter );
146             p_image->p_filter = 0;
147         }
148
149         /* Start a filter */
150         if( !p_image->p_filter )
151         {
152             p_image->p_filter =
153                 CreateFilter( p_image->p_parent, &p_image->p_dec->fmt_out,
154                               p_fmt_out );
155
156             if( !p_image->p_filter )
157             {
158                 p_pic->pf_release( p_pic );
159                 return NULL;
160             }
161         }
162
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;
165     }
166     else *p_fmt_out = p_image->p_dec->fmt_out.video;
167
168     return p_pic;
169 }
170
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 )
174 {
175     block_t *p_block;
176     picture_t *p_pic;
177     FILE *file;
178     int i_size;
179
180     file = fopen( psz_url, "rb" );
181     if( !file )
182     {
183         msg_Dbg( p_image->p_parent, "could not open file %s for reading",
184                  psz_url );
185         return NULL;
186     }
187
188     fseek( file, 0, SEEK_END );
189     i_size = ftell( file );
190     fseek( file, 0, SEEK_SET );
191
192     p_block = block_New( p_image->p_parent, i_size );
193     fread( p_block->p_buffer, sizeof(char), i_size, file );
194     fclose( file );
195
196     p_pic = ImageRead( p_image, p_block, p_fmt_in, p_fmt_out );
197
198     return p_pic;
199 }
200
201 /**
202  * Write an image
203  *
204  */
205
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 )
209 {
210     return NULL;
211 }
212
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 )
216 {
217     return VLC_EGENERIC;
218 }
219
220 /**
221  * Misc functions
222  *
223  */
224 static void video_release_buffer( picture_t *p_pic )
225 {
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 );
229 }
230
231 static picture_t *video_new_buffer( decoder_t *p_dec )
232 {
233     picture_t *p_pic = malloc( sizeof(picture_t) );
234
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 );
241
242     if( !p_pic->i_planes )
243     {
244         free( p_pic );
245         return 0;
246     }
247
248     p_pic->pf_release = video_release_buffer;
249     p_pic->i_status = RESERVED_PICTURE;
250     p_pic->p_sys = NULL;
251
252     return p_pic;
253 }
254
255 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
256 {
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 );
260 }
261
262 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
263 {
264 }
265
266 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
267 {
268 }
269
270 static decoder_t *CreateDecoder( vlc_object_t *p_this, video_format_t *fmt )
271 {
272     decoder_t *p_dec;
273     static es_format_t null_es_format = {0};
274
275     p_dec = vlc_object_create( p_this, VLC_OBJECT_DECODER );
276     if( p_dec == NULL )
277     {
278         msg_Err( p_this, "out of memory" );
279         return NULL;
280     }
281
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;
288
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;
293
294     vlc_object_attach( p_dec, p_this );
295
296     /* Find a suitable decoder module */
297     p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
298     if( !p_dec->p_module )
299     {
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 );
303
304         DeleteDecoder( p_dec );
305         return NULL;
306     }
307
308     return p_dec;
309 }
310
311 static void DeleteDecoder( decoder_t * p_dec )
312 {
313     vlc_object_detach( p_dec );
314
315     if( p_dec->p_module ) module_Unneed( p_dec, p_dec->p_module );
316
317     es_format_Clean( &p_dec->fmt_in );
318     es_format_Clean( &p_dec->fmt_out );
319
320     vlc_object_destroy( p_dec );
321 }
322
323 static filter_t *CreateFilter( vlc_object_t *p_this, es_format_t *p_fmt_in,
324                                video_format_t *p_fmt_out )
325 {
326     filter_t *p_filter;
327
328     p_filter = vlc_object_create( p_this, VLC_OBJECT_FILTER );
329     vlc_object_attach( p_filter, p_this );
330
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;
335
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 );
341
342     if( !p_filter->p_module )
343     {
344         msg_Dbg( p_filter, "no video filter found" );
345         DeleteFilter( p_filter );
346         return NULL;
347     }
348
349     return p_filter;
350 }
351
352 static void DeleteFilter( filter_t * p_filter )
353 {
354     vlc_object_detach( p_filter );
355
356     if( p_filter->p_module ) module_Unneed( p_filter, p_filter->p_module );
357
358     es_format_Clean( &p_filter->fmt_in );
359     es_format_Clean( &p_filter->fmt_out );
360
361     vlc_object_destroy( p_filter );
362 }