1 /*****************************************************************************
2 * es_format.c : es_format_t helpers.
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Author: Laurent Aimar <fenrir@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
37 /*****************************************************************************
38 * BinaryLog: computes the base 2 log of a binary value
39 *****************************************************************************
40 * This functions is used by MaskToShift, to get a bit index from a binary
42 *****************************************************************************/
43 static int BinaryLog( uint32_t i )
47 if( i == 0 ) return -31337;
49 if( i & 0xffff0000 ) i_log += 16;
50 if( i & 0xff00ff00 ) i_log += 8;
51 if( i & 0xf0f0f0f0 ) i_log += 4;
52 if( i & 0xcccccccc ) i_log += 2;
53 if( i & 0xaaaaaaaa ) i_log += 1;
59 * It transforms a color mask into right and left shifts
60 * FIXME copied from video_output.c
62 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
64 uint32_t i_low, i_high; /* lower and higher bits of the mask */
68 *pi_left = *pi_right = 0;
73 i_low = i_high = i_mask;
75 i_low &= - (int32_t)i_low; /* lower bit of the mask */
76 i_high += i_low; /* higher bit of the mask */
78 /* Transform bits into an index. Also deal with i_high overflow, which
79 * is faster than changing the BinaryLog code to handle 64 bit integers. */
80 i_low = BinaryLog (i_low);
81 i_high = i_high ? BinaryLog (i_high) : 32;
83 /* Update pointers and return */
85 *pi_right = (8 - i_high + i_low);
89 void video_format_FixRgb( video_format_t *p_fmt )
91 /* FIXME find right default mask */
92 if( !p_fmt->i_rmask || !p_fmt->i_gmask || !p_fmt->i_bmask )
94 switch( p_fmt->i_chroma )
97 p_fmt->i_rmask = 0x7c00;
98 p_fmt->i_gmask = 0x03e0;
99 p_fmt->i_bmask = 0x001f;
102 case VLC_CODEC_RGB16:
103 p_fmt->i_rmask = 0xf800;
104 p_fmt->i_gmask = 0x07e0;
105 p_fmt->i_bmask = 0x001f;
108 case VLC_CODEC_RGB24:
109 p_fmt->i_rmask = 0xff0000;
110 p_fmt->i_gmask = 0x00ff00;
111 p_fmt->i_bmask = 0x0000ff;
113 case VLC_CODEC_RGB32:
114 p_fmt->i_rmask = 0x00ff0000;
115 p_fmt->i_gmask = 0x0000ff00;
116 p_fmt->i_bmask = 0x000000ff;
124 MaskToShift( &p_fmt->i_lrshift, &p_fmt->i_rrshift,
126 MaskToShift( &p_fmt->i_lgshift, &p_fmt->i_rgshift,
128 MaskToShift( &p_fmt->i_lbshift, &p_fmt->i_rbshift,
132 void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma,
133 int i_width, int i_height,
134 int i_sar_num, int i_sar_den )
136 p_fmt->i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
138 p_fmt->i_visible_width = i_width;
140 p_fmt->i_visible_height = i_height;
142 p_fmt->i_y_offset = 0;
143 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
144 i_sar_num, i_sar_den, 0 );
146 switch( p_fmt->i_chroma )
149 p_fmt->i_bits_per_pixel = 32;
153 p_fmt->i_bits_per_pixel = 24;
161 p_fmt->i_bits_per_pixel = 16;
165 p_fmt->i_bits_per_pixel = 16;
171 p_fmt->i_bits_per_pixel = 12;
175 p_fmt->i_bits_per_pixel = 9;
178 p_fmt->i_bits_per_pixel = 8;
181 p_fmt->i_bits_per_pixel = 8;
184 case VLC_CODEC_RGB32:
186 p_fmt->i_bits_per_pixel = 32;
188 case VLC_CODEC_RGB24:
189 p_fmt->i_bits_per_pixel = 24;
191 case VLC_CODEC_RGB15:
192 case VLC_CODEC_RGB16:
193 p_fmt->i_bits_per_pixel = 16;
196 p_fmt->i_bits_per_pixel = 8;
201 p_fmt->i_bits_per_pixel = 8;
205 p_fmt->i_bits_per_pixel = 0;
209 bool video_format_IsSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
211 video_format_t v1 = *p_fmt1;
212 video_format_t v2 = *p_fmt2;
214 if( v1.i_chroma != v2.i_chroma )
217 if( v1.i_width != v2.i_width || v1.i_height != v2.i_height ||
218 v1.i_visible_width != v2.i_visible_width ||
219 v1.i_visible_height != v2.i_visible_height ||
220 v1.i_x_offset != v2.i_x_offset || v1.i_y_offset != v2.i_y_offset )
223 if( v1.i_chroma == VLC_CODEC_RGB15 ||
224 v1.i_chroma == VLC_CODEC_RGB16 ||
225 v1.i_chroma == VLC_CODEC_RGB24 ||
226 v1.i_chroma == VLC_CODEC_RGB32 )
228 video_format_FixRgb( &v1 );
229 video_format_FixRgb( &v2 );
231 if( v1.i_rmask != v2.i_rmask ||
232 v1.i_gmask != v2.i_gmask ||
233 v1.i_bmask != v2.i_bmask )
239 void es_format_Init( es_format_t *fmt,
240 int i_cat, vlc_fourcc_t i_codec )
243 fmt->i_codec = i_codec;
244 fmt->i_original_fourcc = 0;
250 fmt->psz_language = NULL;
251 fmt->psz_description = NULL;
253 fmt->i_extra_languages = 0;
254 fmt->p_extra_languages = NULL;
256 memset( &fmt->audio, 0, sizeof(audio_format_t) );
257 memset( &fmt->audio_replay_gain, 0, sizeof(audio_replay_gain_t) );
258 memset( &fmt->video, 0, sizeof(video_format_t) );
259 memset( &fmt->subs, 0, sizeof(subs_format_t) );
261 fmt->b_packetized = true;
267 void es_format_InitFromVideo( es_format_t *p_es, const video_format_t *p_fmt )
269 es_format_Init( p_es, VIDEO_ES, p_fmt->i_chroma );
270 video_format_Copy( &p_es->video, p_fmt );
273 int es_format_Copy( es_format_t *dst, const es_format_t *src )
276 memcpy( dst, src, sizeof( es_format_t ) );
277 dst->psz_language = src->psz_language ? strdup( src->psz_language ) : NULL;
278 dst->psz_description = src->psz_description ? strdup( src->psz_description ) : NULL;
279 if( src->i_extra > 0 && dst->p_extra )
281 dst->i_extra = src->i_extra;
282 dst->p_extra = malloc( src->i_extra );
284 memcpy( dst->p_extra, src->p_extra, src->i_extra );
294 dst->subs.psz_encoding = dst->subs.psz_encoding ? strdup( src->subs.psz_encoding ) : NULL;
296 if( src->video.p_palette )
298 dst->video.p_palette =
299 (video_palette_t*)malloc( sizeof( video_palette_t ) );
300 if(dst->video.p_palette)
302 memcpy( dst->video.p_palette, src->video.p_palette,
303 sizeof( video_palette_t ) );
307 if( dst->i_extra_languages && src->p_extra_languages)
309 dst->i_extra_languages = src->i_extra_languages;
310 dst->p_extra_languages = (extra_languages_t*)
311 malloc(dst->i_extra_languages * sizeof(*dst->p_extra_languages ));
312 if( dst->p_extra_languages )
314 for( i = 0; i < dst->i_extra_languages; i++ ) {
315 if( src->p_extra_languages[i].psz_language )
316 dst->p_extra_languages[i].psz_language = strdup( src->p_extra_languages[i].psz_language );
318 dst->p_extra_languages[i].psz_language = NULL;
319 if( src->p_extra_languages[i].psz_description )
320 dst->p_extra_languages[i].psz_description = strdup( src->p_extra_languages[i].psz_description );
322 dst->p_extra_languages[i].psz_description = NULL;
326 dst->i_extra_languages = 0;
329 dst->i_extra_languages = 0;
333 void es_format_Clean( es_format_t *fmt )
335 free( fmt->psz_language );
336 free( fmt->psz_description );
338 if( fmt->i_extra > 0 ) free( fmt->p_extra );
340 free( fmt->video.p_palette );
341 free( fmt->subs.psz_encoding );
343 if( fmt->i_extra_languages > 0 && fmt->p_extra_languages )
346 for( i = 0; i < fmt->i_extra_languages; i++ )
348 free( fmt->p_extra_languages[i].psz_language );
349 free( fmt->p_extra_languages[i].psz_description );
351 free( fmt->p_extra_languages );
354 /* es_format_Clean can be called multiple times */
355 memset( fmt, 0, sizeof(*fmt) );
358 bool es_format_IsSimilar( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
360 if( p_fmt1->i_cat != p_fmt2->i_cat ||
361 vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec ) !=
362 vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec ) )
365 switch( p_fmt1->i_cat )
369 audio_format_t a1 = p_fmt1->audio;
370 audio_format_t a2 = p_fmt2->audio;
372 if( a1.i_format && a2.i_format && a1.i_format != a2.i_format )
374 if( a1.i_rate != a2.i_rate ||
375 a1.i_physical_channels != a2.i_physical_channels ||
376 a1.i_original_channels != a2.i_original_channels )
383 video_format_t v1 = p_fmt1->video;
384 video_format_t v2 = p_fmt2->video;
386 v1.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec );
388 v2.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt2->i_codec );
389 return video_format_IsSimilar( &p_fmt1->video, &p_fmt2->video );