vlc_image: add extradata as codec parameter
[vlc.git] / src / misc / image.c
blob8e9f6fa3a9f1edae538a163612998c9298839025
1 /*****************************************************************************
2 * image.c : wrapper for image reading/writing facilities
3 *****************************************************************************
4 * Copyright (C) 2004-2007 VLC authors and VideoLAN
5 * $Id$
7 * Author: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /**
25 * \file
26 * This file contains the functions to handle the image_handler_t type
29 /*****************************************************************************
30 * Preamble
31 *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
37 #include <errno.h>
38 #include <limits.h>
40 #include <vlc_common.h>
41 #include <vlc_codec.h>
42 #include <vlc_meta.h>
43 #include <vlc_filter.h>
44 #include <vlc_es.h>
45 #include <vlc_image.h>
46 #include <vlc_stream.h>
47 #include <vlc_fs.h>
48 #include <vlc_sout.h>
49 #include <libvlc.h>
50 #include <vlc_modules.h>
52 struct decoder_owner
54 decoder_t dec;
55 image_handler_t *p_image;
58 static inline struct decoder_owner *dec_get_owner( decoder_t *p_dec )
60 return container_of( p_dec, struct decoder_owner, dec );
63 static picture_t *ImageRead( image_handler_t *, block_t *,
64 const video_format_t *, const uint8_t *, size_t,
65 video_format_t * );
66 static picture_t *ImageReadUrl( image_handler_t *, const char *,
67 video_format_t *, video_format_t * );
68 static block_t *ImageWrite( image_handler_t *, picture_t *,
69 const video_format_t *, const video_format_t * );
70 static int ImageWriteUrl( image_handler_t *, picture_t *,
71 const video_format_t *, video_format_t *, const char * );
73 static picture_t *ImageConvert( image_handler_t *, picture_t *,
74 const video_format_t *, video_format_t * );
76 static decoder_t *CreateDecoder( image_handler_t *, const video_format_t *,
77 const uint8_t *, size_t );
78 static void DeleteDecoder( decoder_t * );
79 static encoder_t *CreateEncoder( vlc_object_t *, const video_format_t *,
80 const video_format_t * );
81 static void DeleteEncoder( encoder_t * );
82 static filter_t *CreateFilter( vlc_object_t *, const es_format_t *,
83 const video_format_t * );
84 static void DeleteFilter( filter_t * );
86 vlc_fourcc_t image_Type2Fourcc( const char * );
87 vlc_fourcc_t image_Ext2Fourcc( const char * );
88 /*static const char *Fourcc2Ext( vlc_fourcc_t );*/
90 #undef image_HandlerCreate
91 /**
92 * Create an image_handler_t instance
95 image_handler_t *image_HandlerCreate( vlc_object_t *p_this )
97 image_handler_t *p_image = calloc( 1, sizeof(image_handler_t) );
98 if( !p_image )
99 return NULL;
101 p_image->p_parent = p_this;
103 p_image->pf_read = ImageRead;
104 p_image->pf_read_url = ImageReadUrl;
105 p_image->pf_write = ImageWrite;
106 p_image->pf_write_url = ImageWriteUrl;
107 p_image->pf_convert = ImageConvert;
109 p_image->outfifo = picture_fifo_New();
111 return p_image;
115 * Delete the image_handler_t instance
118 void image_HandlerDelete( image_handler_t *p_image )
120 if( !p_image ) return;
122 if( p_image->p_dec ) DeleteDecoder( p_image->p_dec );
123 if( p_image->p_enc ) DeleteEncoder( p_image->p_enc );
124 if( p_image->p_filter ) DeleteFilter( p_image->p_filter );
126 picture_fifo_Delete( p_image->outfifo );
128 free( p_image );
129 p_image = NULL;
133 * Read an image
137 static void ImageQueueVideo( decoder_t *p_dec, picture_t *p_pic )
139 struct decoder_owner *p_owner = dec_get_owner( p_dec );
140 picture_fifo_Push( p_owner->p_image->outfifo, p_pic );
143 static picture_t *ImageRead( image_handler_t *p_image, block_t *p_block,
144 const video_format_t *p_fmt_in,
145 const uint8_t *p_extra, size_t i_extra,
146 video_format_t *p_fmt_out )
148 picture_t *p_pic = NULL;
150 /* Check if we can reuse the current decoder */
151 if( p_image->p_dec &&
152 p_image->p_dec->fmt_in.i_codec != p_fmt_in->i_chroma )
154 DeleteDecoder( p_image->p_dec );
155 p_image->p_dec = 0;
158 /* Start a decoder */
159 if( !p_image->p_dec )
161 p_image->p_dec = CreateDecoder( p_image, p_fmt_in,
162 p_extra, i_extra );
163 if( !p_image->p_dec )
165 block_Release(p_block);
166 return NULL;
168 if( p_image->p_dec->fmt_out.i_cat != VIDEO_ES )
170 DeleteDecoder( p_image->p_dec );
171 p_image->p_dec = NULL;
172 block_Release(p_block);
173 return NULL;
177 p_block->i_pts = p_block->i_dts = vlc_tick_now();
178 int ret = p_image->p_dec->pf_decode( p_image->p_dec, p_block );
179 if( ret == VLCDEC_SUCCESS )
181 /* Drain */
182 p_image->p_dec->pf_decode( p_image->p_dec, NULL );
184 p_pic = picture_fifo_Pop( p_image->outfifo );
186 unsigned lostcount = 0;
187 picture_t *lostpic;
188 while( ( lostpic = picture_fifo_Pop( p_image->outfifo ) ) != NULL )
190 picture_Release( lostpic );
191 lostcount++;
193 if( lostcount > 0 )
194 msg_Warn( p_image->p_parent, "Image decoder output more than one "
195 "picture (%u)", lostcount );
198 if( p_pic == NULL )
200 msg_Warn( p_image->p_parent, "no image decoded" );
201 return 0;
204 if( !p_fmt_out->i_chroma )
205 p_fmt_out->i_chroma = p_image->p_dec->fmt_out.video.i_chroma;
206 if( !p_fmt_out->i_width && p_fmt_out->i_height )
207 p_fmt_out->i_width = (int64_t)p_image->p_dec->fmt_out.video.i_width *
208 p_image->p_dec->fmt_out.video.i_sar_num *
209 p_fmt_out->i_height /
210 p_image->p_dec->fmt_out.video.i_height /
211 p_image->p_dec->fmt_out.video.i_sar_den;
213 if( !p_fmt_out->i_height && p_fmt_out->i_width )
214 p_fmt_out->i_height = (int64_t)p_image->p_dec->fmt_out.video.i_height *
215 p_image->p_dec->fmt_out.video.i_sar_den *
216 p_fmt_out->i_width /
217 p_image->p_dec->fmt_out.video.i_width /
218 p_image->p_dec->fmt_out.video.i_sar_num;
219 if( !p_fmt_out->i_width )
220 p_fmt_out->i_width = p_image->p_dec->fmt_out.video.i_width;
221 if( !p_fmt_out->i_height )
222 p_fmt_out->i_height = p_image->p_dec->fmt_out.video.i_height;
223 if( !p_fmt_out->i_visible_width )
224 p_fmt_out->i_visible_width = p_fmt_out->i_width;
225 if( !p_fmt_out->i_visible_height )
226 p_fmt_out->i_visible_height = p_fmt_out->i_height;
228 /* Check if we need chroma conversion or resizing */
229 if( p_image->p_dec->fmt_out.video.i_chroma != p_fmt_out->i_chroma ||
230 p_image->p_dec->fmt_out.video.i_width != p_fmt_out->i_width ||
231 p_image->p_dec->fmt_out.video.i_height != p_fmt_out->i_height )
233 if( p_image->p_filter )
234 if( p_image->p_filter->fmt_in.video.i_chroma !=
235 p_image->p_dec->fmt_out.video.i_chroma ||
236 p_image->p_filter->fmt_out.video.i_chroma != p_fmt_out->i_chroma )
238 /* We need to restart a new filter */
239 DeleteFilter( p_image->p_filter );
240 p_image->p_filter = 0;
243 /* Start a filter */
244 if( !p_image->p_filter )
246 p_image->p_filter =
247 CreateFilter( p_image->p_parent, &p_image->p_dec->fmt_out,
248 p_fmt_out );
250 if( !p_image->p_filter )
252 picture_Release( p_pic );
253 return NULL;
256 else
258 /* Filters should handle on-the-fly size changes */
259 p_image->p_filter->fmt_in = p_image->p_dec->fmt_out;
260 p_image->p_filter->fmt_out = p_image->p_dec->fmt_out;
261 p_image->p_filter->fmt_out.i_codec = p_fmt_out->i_chroma;
262 p_image->p_filter->fmt_out.video = *p_fmt_out;
265 p_pic = p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
267 video_format_Clean( p_fmt_out );
268 video_format_Copy( p_fmt_out, &p_image->p_filter->fmt_out.video );
270 else
272 video_format_Clean( p_fmt_out );
273 video_format_Copy( p_fmt_out, &p_image->p_dec->fmt_out.video );
276 return p_pic;
279 static picture_t *ImageReadUrl( image_handler_t *p_image, const char *psz_url,
280 video_format_t *p_fmt_in,
281 video_format_t *p_fmt_out )
283 block_t *p_block;
284 picture_t *p_pic;
285 stream_t *p_stream = NULL;
286 uint64_t i_size;
288 p_stream = vlc_stream_NewURL( p_image->p_parent, psz_url );
290 if( !p_stream )
292 msg_Dbg( p_image->p_parent, "could not open %s for reading",
293 psz_url );
294 return NULL;
297 if( vlc_stream_GetSize( p_stream, &i_size ) || i_size > SSIZE_MAX )
299 msg_Dbg( p_image->p_parent, "could not read %s", psz_url );
300 goto error;
303 p_block = vlc_stream_Block( p_stream, i_size );
304 if( p_block == NULL )
305 goto error;
307 if( !p_fmt_in->i_chroma )
309 char *psz_mime = stream_MimeType( p_stream );
310 if( psz_mime != NULL )
312 p_fmt_in->i_chroma = image_Mime2Fourcc( psz_mime );
313 free( psz_mime );
316 vlc_stream_Delete( p_stream );
318 if( !p_fmt_in->i_chroma )
320 /* Try to guess format from file name */
321 p_fmt_in->i_chroma = image_Ext2Fourcc( psz_url );
324 p_pic = ImageRead( p_image, p_block, p_fmt_in, NULL, 0, p_fmt_out );
326 return p_pic;
327 error:
328 vlc_stream_Delete( p_stream );
329 return NULL;
332 /* FIXME: refactor by splitting video_format_IsSimilar() API */
333 static bool BitMapFormatIsSimilar( const video_format_t *f1,
334 const video_format_t *f2 )
336 if( f1->i_chroma == VLC_CODEC_RGB15 ||
337 f1->i_chroma == VLC_CODEC_RGB16 ||
338 f1->i_chroma == VLC_CODEC_RGB24 ||
339 f1->i_chroma == VLC_CODEC_RGB32 )
341 video_format_t v1 = *f1;
342 video_format_t v2 = *f2;
344 video_format_FixRgb( &v1 );
345 video_format_FixRgb( &v2 );
347 if( v1.i_rmask != v2.i_rmask ||
348 v1.i_gmask != v2.i_gmask ||
349 v1.i_bmask != v2.i_bmask )
350 return false;
352 return true;
356 * Write an image
360 static block_t *ImageWrite( image_handler_t *p_image, picture_t *p_pic,
361 const video_format_t *p_fmt_in,
362 const video_format_t *p_fmt_out )
364 block_t *p_block;
366 /* Check if we can reuse the current encoder */
367 if( p_image->p_enc &&
368 ( p_image->p_enc->fmt_out.i_codec != p_fmt_out->i_chroma ||
369 p_image->p_enc->fmt_out.video.i_width != p_fmt_out->i_width ||
370 p_image->p_enc->fmt_out.video.i_height != p_fmt_out->i_height ) )
372 DeleteEncoder( p_image->p_enc );
373 p_image->p_enc = 0;
376 /* Start an encoder */
377 if( !p_image->p_enc )
379 p_image->p_enc = CreateEncoder( p_image->p_parent,
380 p_fmt_in, p_fmt_out );
381 if( !p_image->p_enc ) return NULL;
384 /* Check if we need chroma conversion or resizing */
385 if( p_image->p_enc->fmt_in.video.i_chroma != p_fmt_in->i_chroma ||
386 p_image->p_enc->fmt_in.video.i_width != p_fmt_in->i_width ||
387 p_image->p_enc->fmt_in.video.i_height != p_fmt_in->i_height ||
388 !BitMapFormatIsSimilar( &p_image->p_enc->fmt_in.video, p_fmt_in ) )
390 picture_t *p_tmp_pic;
392 if( p_image->p_filter )
393 if( p_image->p_filter->fmt_in.video.i_chroma != p_fmt_in->i_chroma ||
394 p_image->p_filter->fmt_out.video.i_chroma !=
395 p_image->p_enc->fmt_in.video.i_chroma ||
396 !BitMapFormatIsSimilar( &p_image->p_filter->fmt_in.video, p_fmt_in ) )
398 /* We need to restart a new filter */
399 DeleteFilter( p_image->p_filter );
400 p_image->p_filter = 0;
403 /* Start a filter */
404 if( !p_image->p_filter )
406 es_format_t fmt_in;
407 es_format_Init( &fmt_in, VIDEO_ES, p_fmt_in->i_chroma );
408 fmt_in.video = *p_fmt_in;
410 p_image->p_filter =
411 CreateFilter( p_image->p_parent, &fmt_in,
412 &p_image->p_enc->fmt_in.video );
414 if( !p_image->p_filter )
416 return NULL;
419 else
421 /* Filters should handle on-the-fly size changes */
422 p_image->p_filter->fmt_in.i_codec = p_fmt_in->i_chroma;
423 p_image->p_filter->fmt_out.video = *p_fmt_in;
424 p_image->p_filter->fmt_out.i_codec =p_image->p_enc->fmt_in.i_codec;
425 p_image->p_filter->fmt_out.video = p_image->p_enc->fmt_in.video;
428 picture_Hold( p_pic );
430 p_tmp_pic =
431 p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
433 if( likely(p_tmp_pic != NULL) )
435 p_block = p_image->p_enc->pf_encode_video( p_image->p_enc,
436 p_tmp_pic );
437 picture_Release( p_tmp_pic );
439 else
440 p_block = NULL;
442 else
444 p_block = p_image->p_enc->pf_encode_video( p_image->p_enc, p_pic );
447 if( !p_block )
449 msg_Dbg( p_image->p_parent, "no image encoded" );
450 return 0;
453 return p_block;
456 static int ImageWriteUrl( image_handler_t *p_image, picture_t *p_pic,
457 const video_format_t *p_fmt_in, video_format_t *p_fmt_out,
458 const char *psz_url )
460 block_t *p_block;
461 FILE *file;
463 if( !p_fmt_out->i_chroma )
465 /* Try to guess format from file name */
466 p_fmt_out->i_chroma = image_Ext2Fourcc( psz_url );
469 file = vlc_fopen( psz_url, "wb" );
470 if( !file )
472 msg_Err( p_image->p_parent, "%s: %s", psz_url, vlc_strerror_c(errno) );
473 return VLC_EGENERIC;
476 p_block = ImageWrite( p_image, p_pic, p_fmt_in, p_fmt_out );
478 int err = 0;
479 if( p_block )
481 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, file ) != 1 )
482 err = errno;
483 block_Release( p_block );
486 if( fclose( file ) && !err )
487 err = errno;
489 if( err )
491 errno = err;
492 msg_Err( p_image->p_parent, "%s: %s", psz_url, vlc_strerror_c(errno) );
495 return err ? VLC_EGENERIC : VLC_SUCCESS;
499 * Convert an image to a different format
503 static picture_t *ImageConvert( image_handler_t *p_image, picture_t *p_pic,
504 const video_format_t *p_fmt_in,
505 video_format_t *p_fmt_out )
507 picture_t *p_pif;
509 if( !p_fmt_out->i_width && !p_fmt_out->i_height &&
510 p_fmt_out->i_sar_num && p_fmt_out->i_sar_den &&
511 p_fmt_out->i_sar_num * p_fmt_in->i_sar_den !=
512 p_fmt_out->i_sar_den * p_fmt_in->i_sar_num )
514 p_fmt_out->i_width =
515 p_fmt_in->i_sar_num * (int64_t)p_fmt_out->i_sar_den *
516 p_fmt_in->i_width / p_fmt_in->i_sar_den / p_fmt_out->i_sar_num;
517 p_fmt_out->i_visible_width =
518 p_fmt_in->i_sar_num * (int64_t)p_fmt_out->i_sar_den *
519 p_fmt_in->i_visible_width / p_fmt_in->i_sar_den /
520 p_fmt_out->i_sar_num;
523 if( !p_fmt_out->i_chroma ) p_fmt_out->i_chroma = p_fmt_in->i_chroma;
524 if( !p_fmt_out->i_width )
525 p_fmt_out->i_width = p_fmt_out->i_visible_width = p_fmt_in->i_width;
526 if( !p_fmt_out->i_height )
527 p_fmt_out->i_height = p_fmt_out->i_visible_height = p_fmt_in->i_height;
528 if( !p_fmt_out->i_sar_num ) p_fmt_out->i_sar_num = p_fmt_in->i_sar_num;
529 if( !p_fmt_out->i_sar_den ) p_fmt_out->i_sar_den = p_fmt_in->i_sar_den;
531 if( p_image->p_filter )
532 if( p_image->p_filter->fmt_in.video.i_chroma != p_fmt_in->i_chroma ||
533 p_image->p_filter->fmt_out.video.i_chroma != p_fmt_out->i_chroma )
535 /* We need to restart a new filter */
536 DeleteFilter( p_image->p_filter );
537 p_image->p_filter = NULL;
540 /* Start a filter */
541 if( !p_image->p_filter )
543 es_format_t fmt_in;
544 es_format_Init( &fmt_in, VIDEO_ES, p_fmt_in->i_chroma );
545 fmt_in.video = *p_fmt_in;
547 p_image->p_filter =
548 CreateFilter( p_image->p_parent, &fmt_in, p_fmt_out );
550 if( !p_image->p_filter )
552 return NULL;
555 else
557 /* Filters should handle on-the-fly size changes */
558 p_image->p_filter->fmt_in.video = *p_fmt_in;
559 p_image->p_filter->fmt_out.video = *p_fmt_out;
562 picture_Hold( p_pic );
564 p_pif = p_image->p_filter->pf_video_filter( p_image->p_filter, p_pic );
566 if( p_fmt_in->i_chroma == p_fmt_out->i_chroma &&
567 p_fmt_in->i_width == p_fmt_out->i_width &&
568 p_fmt_in->i_height == p_fmt_out->i_height )
570 /* Duplicate image */
571 picture_Release( p_pif ); /* XXX: Better fix must be possible */
572 p_pif = filter_NewPicture( p_image->p_filter );
573 if( p_pif )
574 picture_Copy( p_pif, p_pic );
577 return p_pif;
581 * Misc functions
584 static const struct
586 vlc_fourcc_t i_codec;
587 const char psz_ext[7];
589 } ext_table[] =
591 { VLC_CODEC_JPEG, "jpeg" },
592 { VLC_CODEC_JPEG, "jpg" },
593 { VLC_CODEC_JPEGLS, "ljpg" },
594 { VLC_CODEC_BPG, "bpg" },
595 { VLC_CODEC_PNG, "png" },
596 { VLC_CODEC_PGM, "pgm" },
597 { VLC_CODEC_PGMYUV, "pgmyuv" },
598 { VLC_FOURCC('p','b','m',' '), "pbm" },
599 { VLC_FOURCC('p','a','m',' '), "pam" },
600 { VLC_CODEC_TARGA, "tga" },
601 { VLC_CODEC_BMP, "bmp" },
602 { VLC_CODEC_PNM, "pnm" },
603 { VLC_FOURCC('x','p','m',' '), "xpm" },
604 { VLC_FOURCC('x','c','f',' '), "xcf" },
605 { VLC_CODEC_PCX, "pcx" },
606 { VLC_CODEC_GIF, "gif" },
607 { VLC_CODEC_SVG, "svg" },
608 { VLC_CODEC_TIFF, "tif" },
609 { VLC_CODEC_TIFF, "tiff" },
610 { VLC_FOURCC('l','b','m',' '), "lbm" },
611 { VLC_CODEC_PPM, "ppm" },
614 vlc_fourcc_t image_Type2Fourcc( const char *psz_type )
616 for( unsigned i = 0; i < ARRAY_SIZE(ext_table); i++ )
617 if( !strcasecmp( ext_table[i].psz_ext, psz_type ) )
618 return ext_table[i].i_codec;
620 return 0;
623 vlc_fourcc_t image_Ext2Fourcc( const char *psz_name )
625 psz_name = strrchr( psz_name, '.' );
626 if( !psz_name ) return 0;
627 psz_name++;
629 return image_Type2Fourcc( psz_name );
632 static const struct
634 vlc_fourcc_t i_codec;
635 const char *psz_mime;
636 } mime_table[] =
638 { VLC_CODEC_BMP, "image/bmp" },
639 { VLC_CODEC_BMP, "image/x-bmp" },
640 { VLC_CODEC_BMP, "image/x-bitmap" },
641 { VLC_CODEC_BMP, "image/x-ms-bmp" },
642 { VLC_CODEC_PNM, "image/x-portable-anymap" },
643 { VLC_CODEC_PNM, "image/x-portable-bitmap" },
644 { VLC_CODEC_PNM, "image/x-portable-graymap" },
645 { VLC_CODEC_PNM, "image/x-portable-pixmap" },
646 { VLC_CODEC_GIF, "image/gif" },
647 { VLC_CODEC_JPEG, "image/jpeg" },
648 { VLC_CODEC_BPG, "image/bpg" },
649 { VLC_CODEC_PCX, "image/pcx" },
650 { VLC_CODEC_PNG, "image/png" },
651 { VLC_CODEC_SVG, "image/svg+xml" },
652 { VLC_CODEC_TIFF, "image/tiff" },
653 { VLC_CODEC_TARGA, "image/x-tga" },
654 { VLC_FOURCC('x','p','m',' '), "image/x-xpixmap" },
655 { 0, NULL }
658 vlc_fourcc_t image_Mime2Fourcc( const char *psz_mime )
660 for( int i = 0; mime_table[i].i_codec; i++ )
661 if( !strcmp( psz_mime, mime_table[i].psz_mime ) )
662 return mime_table[i].i_codec;
663 return 0;
666 static int video_update_format( decoder_t *p_dec )
668 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
669 return 0;
672 static picture_t *video_new_buffer( decoder_t *p_dec )
674 return picture_NewFromFormat( &p_dec->fmt_out.video );
677 static decoder_t *CreateDecoder( image_handler_t *p_image, const video_format_t *fmt,
678 const uint8_t *p_extra, size_t i_extra )
680 decoder_t *p_dec;
681 struct decoder_owner *p_owner;
683 p_owner = vlc_custom_create( p_image->p_parent, sizeof( *p_owner ), "image decoder" );
684 if( p_owner == NULL )
685 return NULL;
686 p_dec = &p_owner->dec;
687 p_owner->p_image = p_image;
689 p_dec->p_module = NULL;
690 es_format_InitFromVideo( &p_dec->fmt_in, fmt );
691 if( i_extra )
693 p_dec->fmt_in.p_extra = malloc( i_extra );
694 if( p_dec->fmt_in.p_extra )
696 memcpy( p_dec->fmt_in.p_extra, p_extra, i_extra );
697 p_dec->fmt_in.i_extra = i_extra;
700 es_format_Init( &p_dec->fmt_out, VIDEO_ES, 0 );
701 p_dec->b_frame_drop_allowed = false;
703 static const struct decoder_owner_callbacks dec_cbs =
705 .video = {
706 .format_update = video_update_format,
707 .buffer_new = video_new_buffer,
708 .queue = ImageQueueVideo,
711 p_dec->cbs = &dec_cbs;
713 /* Find a suitable decoder module */
714 p_dec->p_module = module_need_var( p_dec, "video decoder", "codec" );
715 if( !p_dec->p_module )
717 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
718 "VLC probably does not support this image format.",
719 (char*)&p_dec->fmt_in.i_codec );
721 DeleteDecoder( p_dec );
722 p_dec = NULL;
725 return p_dec;
728 static void DeleteDecoder( decoder_t * p_dec )
730 if( p_dec->p_module ) module_unneed( p_dec, p_dec->p_module );
732 es_format_Clean( &p_dec->fmt_in );
733 es_format_Clean( &p_dec->fmt_out );
735 if( p_dec->p_description )
736 vlc_meta_Delete( p_dec->p_description );
738 vlc_object_release( p_dec );
739 p_dec = NULL;
742 static encoder_t *CreateEncoder( vlc_object_t *p_this, const video_format_t *fmt_in,
743 const video_format_t *fmt_out )
745 encoder_t *p_enc;
747 p_enc = sout_EncoderCreate( p_this );
748 if( p_enc == NULL )
749 return NULL;
751 p_enc->p_module = NULL;
752 es_format_InitFromVideo( &p_enc->fmt_in, fmt_in );
754 if( p_enc->fmt_in.video.i_visible_width == 0 ||
755 p_enc->fmt_in.video.i_visible_height == 0 ||
756 p_enc->fmt_out.video.i_visible_width == 0 ||
757 p_enc->fmt_out.video.i_visible_height == 0 )
759 if( fmt_out->i_width > 0 && fmt_out->i_height > 0 )
761 p_enc->fmt_in.video.i_width = fmt_out->i_width;
762 p_enc->fmt_in.video.i_height = fmt_out->i_height;
764 if( fmt_out->i_visible_width > 0 &&
765 fmt_out->i_visible_height > 0 )
767 p_enc->fmt_in.video.i_visible_width = fmt_out->i_visible_width;
768 p_enc->fmt_in.video.i_visible_height = fmt_out->i_visible_height;
770 else
772 p_enc->fmt_in.video.i_visible_width = fmt_out->i_width;
773 p_enc->fmt_in.video.i_visible_height = fmt_out->i_height;
776 } else if( fmt_out->i_sar_num && fmt_out->i_sar_den &&
777 fmt_out->i_sar_num * fmt_in->i_sar_den !=
778 fmt_out->i_sar_den * fmt_in->i_sar_num )
780 p_enc->fmt_in.video.i_width =
781 fmt_in->i_sar_num * (int64_t)fmt_out->i_sar_den * fmt_in->i_width /
782 fmt_in->i_sar_den / fmt_out->i_sar_num;
783 p_enc->fmt_in.video.i_visible_width =
784 fmt_in->i_sar_num * (int64_t)fmt_out->i_sar_den *
785 fmt_in->i_visible_width / fmt_in->i_sar_den / fmt_out->i_sar_num;
788 p_enc->fmt_in.video.i_frame_rate = 25;
789 p_enc->fmt_in.video.i_frame_rate_base = 1;
791 es_format_InitFromVideo( &p_enc->fmt_out, fmt_out );
792 p_enc->fmt_out.video.i_width = p_enc->fmt_in.video.i_width;
793 p_enc->fmt_out.video.i_height = p_enc->fmt_in.video.i_height;
795 /* Find a suitable decoder module */
796 p_enc->p_module = module_need( p_enc, "encoder", NULL, false );
797 if( !p_enc->p_module )
799 msg_Err( p_enc, "no suitable encoder module for fourcc `%4.4s'.\n"
800 "VLC probably does not support this image format.",
801 (char*)&p_enc->fmt_out.i_codec );
803 DeleteEncoder( p_enc );
804 return NULL;
806 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
808 return p_enc;
811 static void DeleteEncoder( encoder_t * p_enc )
813 if( p_enc->p_module ) module_unneed( p_enc, p_enc->p_module );
815 es_format_Clean( &p_enc->fmt_in );
816 es_format_Clean( &p_enc->fmt_out );
818 vlc_object_release( p_enc );
819 p_enc = NULL;
822 static picture_t *filter_new_picture( filter_t *p_filter )
824 return picture_NewFromFormat( &p_filter->fmt_out.video );
827 static const struct filter_video_callbacks image_filter_cbs =
829 .buffer_new = filter_new_picture,
832 static filter_t *CreateFilter( vlc_object_t *p_this, const es_format_t *p_fmt_in,
833 const video_format_t *p_fmt_out )
835 filter_t *p_filter;
837 p_filter = vlc_custom_create( p_this, sizeof(filter_t), "filter" );
838 p_filter->owner.video = &image_filter_cbs;
840 es_format_Copy( &p_filter->fmt_in, p_fmt_in );
841 es_format_Copy( &p_filter->fmt_out, p_fmt_in );
842 video_format_Copy( &p_filter->fmt_out.video, p_fmt_out );
844 /* whatever the input offset, write at offset 0 in the target image */
845 p_filter->fmt_out.video.i_x_offset = 0;
846 p_filter->fmt_out.video.i_y_offset = 0;
848 p_filter->fmt_out.i_codec = p_fmt_out->i_chroma;
849 p_filter->p_module = module_need( p_filter, "video converter", NULL, false );
851 if( !p_filter->p_module )
853 msg_Dbg( p_filter, "no video converter found" );
854 DeleteFilter( p_filter );
855 return NULL;
858 return p_filter;
861 static void DeleteFilter( filter_t * p_filter )
863 if( p_filter->p_module ) module_unneed( p_filter, p_filter->p_module );
865 es_format_Clean( &p_filter->fmt_in );
866 es_format_Clean( &p_filter->fmt_out );
868 vlc_object_release( p_filter );