]> git.sesse.net Git - vlc/blob - src/misc/es_format.c
Merge branch 1.0-bugfix
[vlc] / src / misc / es_format.c
1 /*****************************************************************************
2  * es_format.c : es_format_t helpers.
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_es.h>
34
35
36 /*****************************************************************************
37  * BinaryLog: computes the base 2 log of a binary value
38  *****************************************************************************
39  * This functions is used by MaskToShift, to get a bit index from a binary
40  * value.
41  *****************************************************************************/
42 static int BinaryLog( uint32_t i )
43 {
44     int i_log = 0;
45
46     if( i == 0 ) return -31337;
47
48     if( i & 0xffff0000 ) i_log += 16;
49     if( i & 0xff00ff00 ) i_log += 8;
50     if( i & 0xf0f0f0f0 ) i_log += 4;
51     if( i & 0xcccccccc ) i_log += 2;
52     if( i & 0xaaaaaaaa ) i_log += 1;
53
54     return i_log;
55 }
56
57 /**
58  * It transforms a color mask into right and left shifts
59  * FIXME copied from video_output.c
60  */
61 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
62 {
63     uint32_t i_low, i_high;            /* lower and higher bits of the mask */
64
65     if( !i_mask )
66     {
67         *pi_left = *pi_right = 0;
68         return;
69     }
70
71     /* Get bits */
72     i_low = i_high = i_mask;
73
74     i_low &= - (int32_t)i_low;          /* lower bit of the mask */
75     i_high += i_low;                    /* higher bit of the mask */
76
77     /* Transform bits into an index. Also deal with i_high overflow, which
78      * is faster than changing the BinaryLog code to handle 64 bit integers. */
79     i_low =  BinaryLog (i_low);
80     i_high = i_high ? BinaryLog (i_high) : 32;
81
82     /* Update pointers and return */
83     *pi_left =   i_low;
84     *pi_right = (8 - i_high + i_low);
85 }
86
87 /* */
88 void video_format_FixRgb( video_format_t *p_fmt )
89 {
90     /* FIXME find right default mask */
91     if( !p_fmt->i_rmask || !p_fmt->i_gmask || !p_fmt->i_bmask )
92     {
93         switch( p_fmt->i_chroma )
94         {
95         case VLC_CODEC_RGB15:
96             p_fmt->i_rmask = 0x7c00;
97             p_fmt->i_gmask = 0x03e0;
98             p_fmt->i_bmask = 0x001f;
99             break;
100
101         case VLC_CODEC_RGB16:
102             p_fmt->i_rmask = 0xf800;
103             p_fmt->i_gmask = 0x07e0;
104             p_fmt->i_bmask = 0x001f;
105             break;
106
107         case VLC_CODEC_RGB24:
108             p_fmt->i_rmask = 0xff0000;
109             p_fmt->i_gmask = 0x00ff00;
110             p_fmt->i_bmask = 0x0000ff;
111             break;
112         case VLC_CODEC_RGB32:
113             p_fmt->i_rmask = 0x00ff0000;
114             p_fmt->i_gmask = 0x0000ff00;
115             p_fmt->i_bmask = 0x000000ff;
116             break;
117
118         default:
119             return;
120         }
121     }
122
123     MaskToShift( &p_fmt->i_lrshift, &p_fmt->i_rrshift,
124                  p_fmt->i_rmask );
125     MaskToShift( &p_fmt->i_lgshift, &p_fmt->i_rgshift,
126                  p_fmt->i_gmask );
127     MaskToShift( &p_fmt->i_lbshift, &p_fmt->i_rbshift,
128                  p_fmt->i_bmask );
129 }
130
131 void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
132 {
133     p_fmt->i_chroma         = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
134     p_fmt->i_width          =
135     p_fmt->i_visible_width  = i_width;
136     p_fmt->i_height         =
137     p_fmt->i_visible_height = i_height;
138     p_fmt->i_x_offset       =
139     p_fmt->i_y_offset       = 0;
140     p_fmt->i_aspect         = i_aspect;
141
142     switch( p_fmt->i_chroma )
143     {
144     case VLC_CODEC_YUVA:
145         p_fmt->i_bits_per_pixel = 32;
146         break;
147     case VLC_CODEC_I444:
148     case VLC_CODEC_J444:
149         p_fmt->i_bits_per_pixel = 24;
150         break;
151     case VLC_CODEC_I422:
152     case VLC_CODEC_YUYV:
153     case VLC_CODEC_YVYU:
154     case VLC_CODEC_UYVY:
155     case VLC_CODEC_VYUY:
156     case VLC_CODEC_J422:
157         p_fmt->i_bits_per_pixel = 16;
158         break;
159     case VLC_CODEC_I440:
160     case VLC_CODEC_J440:
161         p_fmt->i_bits_per_pixel = 16;
162         break;
163     case VLC_CODEC_I411:
164     case VLC_CODEC_YV12:
165     case VLC_CODEC_I420:
166     case VLC_CODEC_J420:
167         p_fmt->i_bits_per_pixel = 12;
168         break;
169     case VLC_CODEC_I410:
170         p_fmt->i_bits_per_pixel = 9;
171         break;
172     case VLC_CODEC_Y211:
173         p_fmt->i_bits_per_pixel = 8;
174         break;
175     case VLC_CODEC_YUVP:
176         p_fmt->i_bits_per_pixel = 8;
177         break;
178
179     case VLC_CODEC_RGB32:
180     case VLC_CODEC_RGBA:
181         p_fmt->i_bits_per_pixel = 32;
182         break;
183     case VLC_CODEC_RGB24:
184         p_fmt->i_bits_per_pixel = 24;
185         break;
186     case VLC_CODEC_RGB15:
187     case VLC_CODEC_RGB16:
188         p_fmt->i_bits_per_pixel = 16;
189         break;
190     case VLC_CODEC_RGB8:
191         p_fmt->i_bits_per_pixel = 8;
192         break;
193
194     case VLC_CODEC_GREY:
195     case VLC_CODEC_RGBP:
196         p_fmt->i_bits_per_pixel = 8;
197         break;
198
199     default:
200         p_fmt->i_bits_per_pixel = 0;
201         break;
202     }
203 }
204
205 void es_format_Init( es_format_t *fmt,
206                      int i_cat, vlc_fourcc_t i_codec )
207 {
208     fmt->i_cat                  = i_cat;
209     fmt->i_codec                = i_codec;
210     fmt->i_original_fourcc      = 0;
211     fmt->i_id                   = -1;
212     fmt->i_group                = 0;
213     fmt->i_priority             = 0;
214     fmt->psz_language           = NULL;
215     fmt->psz_description        = NULL;
216
217     fmt->i_extra_languages      = 0;
218     fmt->p_extra_languages      = NULL;
219
220     memset( &fmt->audio, 0, sizeof(audio_format_t) );
221     memset( &fmt->audio_replay_gain, 0, sizeof(audio_replay_gain_t) );
222     memset( &fmt->video, 0, sizeof(video_format_t) );
223     memset( &fmt->subs, 0, sizeof(subs_format_t) );
224
225     fmt->b_packetized           = true;
226     fmt->i_bitrate              = 0;
227     fmt->i_extra                = 0;
228     fmt->p_extra                = NULL;
229 }
230
231 int es_format_Copy( es_format_t *dst, const es_format_t *src )
232 {
233     int i;
234     memcpy( dst, src, sizeof( es_format_t ) );
235     dst->psz_language = src->psz_language ? strdup( src->psz_language ) : NULL;
236     dst->psz_description = src->psz_description ? strdup( src->psz_description ) : NULL;
237     if( src->i_extra > 0 && dst->p_extra )
238     {
239         dst->i_extra = src->i_extra;
240         dst->p_extra = malloc( src->i_extra );
241         if(dst->p_extra)
242             memcpy( dst->p_extra, src->p_extra, src->i_extra );
243         else
244             dst->i_extra = 0;
245     }
246     else
247     {
248         dst->i_extra = 0;
249         dst->p_extra = NULL;
250     }
251
252     dst->subs.psz_encoding = dst->subs.psz_encoding ? strdup( src->subs.psz_encoding ) : NULL;
253
254     if( src->video.p_palette )
255     {
256         dst->video.p_palette =
257             (video_palette_t*)malloc( sizeof( video_palette_t ) );
258         if(dst->video.p_palette)
259         {
260             memcpy( dst->video.p_palette, src->video.p_palette,
261                 sizeof( video_palette_t ) );
262         }
263     }
264
265     if( dst->i_extra_languages && src->p_extra_languages)
266     {
267         dst->i_extra_languages = src->i_extra_languages;
268         dst->p_extra_languages = (extra_languages_t*)
269             malloc(dst->i_extra_languages * sizeof(*dst->p_extra_languages ));
270         if( dst->p_extra_languages )
271         {
272             for( i = 0; i < dst->i_extra_languages; i++ ) {
273                 if( src->p_extra_languages[i].psz_language )
274                     dst->p_extra_languages[i].psz_language = strdup( src->p_extra_languages[i].psz_language );
275                 else
276                     dst->p_extra_languages[i].psz_language = NULL;
277                 if( src->p_extra_languages[i].psz_description )
278                     dst->p_extra_languages[i].psz_description = strdup( src->p_extra_languages[i].psz_description );
279                 else
280                     dst->p_extra_languages[i].psz_description = NULL;
281             }
282         }
283         else
284             dst->i_extra_languages = 0;
285     }
286     else
287         dst->i_extra_languages = 0;
288     return VLC_SUCCESS;
289 }
290
291 void es_format_Clean( es_format_t *fmt )
292 {
293     free( fmt->psz_language );
294     free( fmt->psz_description );
295
296     if( fmt->i_extra > 0 ) free( fmt->p_extra );
297
298     free( fmt->video.p_palette );
299     free( fmt->subs.psz_encoding );
300
301     if( fmt->i_extra_languages > 0 && fmt->p_extra_languages )
302     {
303         int i;
304         for( i = 0; i < fmt->i_extra_languages; i++ )
305         {
306             free( fmt->p_extra_languages[i].psz_language );
307             free( fmt->p_extra_languages[i].psz_description );
308         }
309         free( fmt->p_extra_languages );
310     }
311
312     /* es_format_Clean can be called multiple times */
313     memset( fmt, 0, sizeof(*fmt) );
314 }
315