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 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_codec.h>
34 struct BPGDecoderContext *p_bpg;
37 static int OpenDecoder(vlc_object_t *);
38 static void CloseDecoder(vlc_object_t *);
40 static picture_t *DecodeBlock(decoder_t *, block_t **);
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( "decoder", 60 )
51 set_callbacks( OpenDecoder, CloseDecoder )
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 )
65 decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
73 p_sys->p_bpg = bpg_decoder_open();
79 /* Set output properties */
80 p_dec->fmt_out.i_cat = VIDEO_ES;
83 p_dec->pf_decode_video = DecodeBlock;
89 * This function must be fed with a complete compressed frame.
91 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
93 decoder_sys_t *p_sys = p_dec->p_sys;
96 BPGImageInfo img_info;
98 if( !pp_block || !*pp_block )
105 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
107 block_Release(p_block);
114 if( bpg_decoder_decode( p_sys->p_bpg,
116 p_block->i_buffer ) < 0 )
118 msg_Err( p_dec, "Could not decode block" );
122 if( bpg_decoder_get_info( p_sys->p_bpg, &img_info ) )
124 msg_Err( p_dec, "Could not get info for decoder" );
128 if( bpg_decoder_start( p_sys->p_bpg, BPG_OUTPUT_FORMAT_RGB24 ) )
130 msg_Err( p_dec, "Could not start decoder" );
134 /* Set output properties */
135 p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
136 p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width = img_info.width;
137 p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height = img_info.height;
138 p_dec->fmt_out.video.i_sar_num = 1;
139 p_dec->fmt_out.video.i_sar_den = 1;
140 p_dec->fmt_out.video.i_rmask = 0x000000ff;
141 p_dec->fmt_out.video.i_gmask = 0x0000ff00;
142 p_dec->fmt_out.video.i_bmask = 0x00ff0000;
144 /* Get a new picture */
145 p_pic = decoder_NewPicture( p_dec );
151 const int img_height = img_info.height;
152 for (int i = 0; i < img_height; i++)
154 if( bpg_decoder_get_line( p_sys->p_bpg,
155 p_pic->p->p_pixels + p_pic->p->i_pitch * i )
158 msg_Err( p_dec, "Could not decode line" );
163 p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
165 block_Release( p_block );
172 block_Release( p_block );
178 static void CloseDecoder( vlc_object_t *p_this )
180 decoder_t *p_dec = (decoder_t *)p_this;
181 decoder_sys_t *p_sys = p_dec->p_sys;
184 bpg_decoder_close( p_sys->p_bpg );