]> git.sesse.net Git - vlc/blob - modules/codec/bpg.c
mediacodec: fix warning
[vlc] / modules / codec / bpg.c
1 /*****************************************************************************
2  * bpg.c: bpg decoder module using libbpg.
3  *****************************************************************************
4  * Copyright (C) 2015 VLC authors and VideoLAN
5  *
6  * Author: Tristan Matthews <tmatth@videolan.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_codec.h>
30 #include <libbpg.h>
31
32 struct decoder_sys_t
33 {
34     struct BPGDecoderContext *p_bpg;
35 };
36
37 static int  OpenDecoder(vlc_object_t *);
38 static void CloseDecoder(vlc_object_t *);
39
40 static picture_t *DecodeBlock(decoder_t *, block_t **);
41
42 /*
43  * Module descriptor
44  */
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( "decoder", 60 )
51     set_callbacks( OpenDecoder, CloseDecoder )
52     add_shortcut( "bpg" )
53 vlc_module_end()
54
55
56 static int OpenDecoder(vlc_object_t *p_this)
57 {
58     decoder_t *p_dec = (decoder_t *)p_this;
59
60     if( p_dec->fmt_in.i_codec != VLC_CODEC_BPG )
61     {
62         return VLC_EGENERIC;
63     }
64
65     decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
66     if( p_sys == NULL )
67     {
68         return VLC_ENOMEM;
69     }
70
71     p_dec->p_sys = p_sys;
72
73     p_sys->p_bpg = bpg_decoder_open();
74     if( !p_sys->p_bpg )
75     {
76         return VLC_EGENERIC;
77     }
78
79     /* Set output properties */
80     p_dec->fmt_out.i_cat = VIDEO_ES;
81
82     /* Set callbacks */
83     p_dec->pf_decode_video = DecodeBlock;
84
85     return VLC_SUCCESS;
86 }
87
88 /*
89  * This function must be fed with a complete compressed frame.
90  */
91 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
92 {
93     decoder_sys_t *p_sys = p_dec->p_sys;
94     block_t *p_block;
95     picture_t *p_pic = 0;
96     BPGImageInfo img_info;
97
98     if( !pp_block || !*pp_block )
99     {
100         return NULL;
101     }
102
103     p_block = *pp_block;
104
105     if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
106     {
107         block_Release(p_block);
108         *pp_block = NULL;
109         return NULL;
110     }
111
112     /* Decode picture */
113
114     if( bpg_decoder_decode( p_sys->p_bpg,
115                             p_block->p_buffer,
116                             p_block->i_buffer ) < 0 )
117     {
118         msg_Err( p_dec, "Could not decode block" );
119         goto error;
120     }
121
122     if( bpg_decoder_get_info( p_sys->p_bpg, &img_info ) )
123     {
124         msg_Err( p_dec, "Could not get info for decoder" );
125         goto error;
126     }
127
128     if( bpg_decoder_start( p_sys->p_bpg, BPG_OUTPUT_FORMAT_RGB24 ) )
129     {
130         msg_Err( p_dec, "Could not start decoder" );
131         goto error;
132     }
133
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;
143
144     /* Get a new picture */
145     p_pic = decoder_NewPicture( p_dec );
146     if( !p_pic )
147     {
148         goto error;
149     }
150
151     const int img_height = img_info.height;
152     for (int i = 0; i < img_height; i++)
153     {
154         if( bpg_decoder_get_line( p_sys->p_bpg,
155                                   p_pic->p->p_pixels + p_pic->p->i_pitch * i )
156                                   < 0 )
157         {
158             msg_Err( p_dec, "Could not decode line" );
159             goto error;
160         }
161     }
162
163     p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
164
165     block_Release( p_block );
166     *pp_block = NULL;
167
168     return p_pic;
169
170 error:
171
172     block_Release( p_block );
173     *pp_block = NULL;
174
175     return NULL;
176 }
177
178 static void CloseDecoder( vlc_object_t *p_this )
179 {
180     decoder_t *p_dec = (decoder_t *)p_this;
181     decoder_sys_t *p_sys = p_dec->p_sys;
182
183     if( p_sys->p_bpg )
184         bpg_decoder_close( p_sys->p_bpg );
185
186     free( p_sys );
187 }