input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / bpg.c
blobcf6add1a2d76ffec654d8e18631caa3393ca1966
1 /*****************************************************************************
2 * bpg.c: bpg decoder module using libbpg.
3 *****************************************************************************
4 * Copyright (C) 2015 VLC authors and VideoLAN
6 * Author: Tristan Matthews <tmatth@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_codec.h>
30 #include <libbpg.h>
32 typedef struct
34 struct BPGDecoderContext *p_bpg;
35 } decoder_sys_t;
37 static int OpenDecoder(vlc_object_t *);
38 static void CloseDecoder(vlc_object_t *);
40 static int DecodeBlock(decoder_t *, block_t *);
43 * Module descriptor
45 vlc_module_begin()
46 set_category( CAT_INPUT )
47 set_subcategory( SUBCAT_INPUT_VCODEC )
48 /* decoder main module */
49 set_description( N_("BPG image decoder") )
50 set_capability( "video decoder", 60 )
51 set_callbacks( OpenDecoder, CloseDecoder )
52 add_shortcut( "bpg" )
53 vlc_module_end()
56 static int OpenDecoder(vlc_object_t *p_this)
58 decoder_t *p_dec = (decoder_t *)p_this;
60 if( p_dec->fmt_in.i_codec != VLC_CODEC_BPG )
62 return VLC_EGENERIC;
65 decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
66 if( p_sys == NULL )
68 return VLC_ENOMEM;
71 p_dec->p_sys = p_sys;
73 p_sys->p_bpg = bpg_decoder_open();
74 if( !p_sys->p_bpg )
76 return VLC_EGENERIC;
79 /* Set callbacks */
80 p_dec->pf_decode = DecodeBlock;
82 return VLC_SUCCESS;
86 * This function must be fed with a complete compressed frame.
88 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
90 decoder_sys_t *p_sys = p_dec->p_sys;
91 picture_t *p_pic = 0;
92 BPGImageInfo img_info;
94 if( p_block == NULL ) /* No Drain */
95 return VLCDEC_SUCCESS;
97 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
98 goto error;
100 /* Decode picture */
102 if( bpg_decoder_decode( p_sys->p_bpg,
103 p_block->p_buffer,
104 p_block->i_buffer ) < 0 )
106 msg_Err( p_dec, "Could not decode block" );
107 goto error;
110 if( bpg_decoder_get_info( p_sys->p_bpg, &img_info ) )
112 msg_Err( p_dec, "Could not get info for decoder" );
113 goto error;
116 if( bpg_decoder_start( p_sys->p_bpg, BPG_OUTPUT_FORMAT_RGB24 ) )
118 msg_Err( p_dec, "Could not start decoder" );
119 goto error;
122 /* Set output properties */
123 p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
124 p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width = img_info.width;
125 p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height = img_info.height;
126 p_dec->fmt_out.video.i_sar_num = 1;
127 p_dec->fmt_out.video.i_sar_den = 1;
129 /* Get a new picture */
130 if( decoder_UpdateVideoFormat( p_dec ) )
132 goto error;
134 p_pic = decoder_NewPicture( p_dec );
135 if( !p_pic )
137 goto error;
140 const int img_height = img_info.height;
141 for (int i = 0; i < img_height; i++)
143 if( bpg_decoder_get_line( p_sys->p_bpg,
144 p_pic->p->p_pixels + p_pic->p->i_pitch * i )
145 < 0 )
147 msg_Err( p_dec, "Could not decode line" );
148 goto error;
152 p_pic->date = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
154 decoder_QueueVideo( p_dec, p_pic );
155 error:
156 block_Release( p_block );
157 return VLCDEC_SUCCESS;
160 static void CloseDecoder( vlc_object_t *p_this )
162 decoder_t *p_dec = (decoder_t *)p_this;
163 decoder_sys_t *p_sys = p_dec->p_sys;
165 if( p_sys->p_bpg )
166 bpg_decoder_close( p_sys->p_bpg );
168 free( p_sys );