Fix memory leak in video_output on Mac OS X (close #6267)
[vlc/solaris.git] / src / misc / es_format.c
blob51741fecadc20bbe4a3aff70d28b1f250d175fca
1 /*****************************************************************************
2 * es_format.c : es_format_t helpers.
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
5 * $Id$
7 * Author: Laurent Aimar <fenrir@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_es.h>
34 #include <vlc_aout.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
41 * value.
42 *****************************************************************************/
43 static int BinaryLog( uint32_t i )
45 int i_log = 0;
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;
55 return i_log;
58 /**
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 */
66 if( !i_mask )
68 *pi_left = *pi_right = 0;
69 return;
72 /* Get bits */
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 */
84 *pi_left = i_low;
85 *pi_right = (8 - i_high + i_low);
88 /* */
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 )
96 case VLC_CODEC_RGB15:
97 p_fmt->i_rmask = 0x7c00;
98 p_fmt->i_gmask = 0x03e0;
99 p_fmt->i_bmask = 0x001f;
100 break;
102 case VLC_CODEC_RGB16:
103 p_fmt->i_rmask = 0xf800;
104 p_fmt->i_gmask = 0x07e0;
105 p_fmt->i_bmask = 0x001f;
106 break;
108 case VLC_CODEC_RGB24:
109 p_fmt->i_rmask = 0xff0000;
110 p_fmt->i_gmask = 0x00ff00;
111 p_fmt->i_bmask = 0x0000ff;
112 break;
113 case VLC_CODEC_RGB32:
114 p_fmt->i_rmask = 0x00ff0000;
115 p_fmt->i_gmask = 0x0000ff00;
116 p_fmt->i_bmask = 0x000000ff;
117 break;
119 default:
120 return;
124 MaskToShift( &p_fmt->i_lrshift, &p_fmt->i_rrshift,
125 p_fmt->i_rmask );
126 MaskToShift( &p_fmt->i_lgshift, &p_fmt->i_rgshift,
127 p_fmt->i_gmask );
128 MaskToShift( &p_fmt->i_lbshift, &p_fmt->i_rbshift,
129 p_fmt->i_bmask );
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 );
137 p_fmt->i_width =
138 p_fmt->i_visible_width = i_width;
139 p_fmt->i_height =
140 p_fmt->i_visible_height = i_height;
141 p_fmt->i_x_offset =
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 )
148 case VLC_CODEC_YUVA:
149 p_fmt->i_bits_per_pixel = 32;
150 break;
151 case VLC_CODEC_I444:
152 case VLC_CODEC_J444:
153 p_fmt->i_bits_per_pixel = 24;
154 break;
155 case VLC_CODEC_I422:
156 case VLC_CODEC_YUYV:
157 case VLC_CODEC_YVYU:
158 case VLC_CODEC_UYVY:
159 case VLC_CODEC_VYUY:
160 case VLC_CODEC_J422:
161 p_fmt->i_bits_per_pixel = 16;
162 break;
163 case VLC_CODEC_I440:
164 case VLC_CODEC_J440:
165 p_fmt->i_bits_per_pixel = 16;
166 break;
167 case VLC_CODEC_I411:
168 case VLC_CODEC_YV12:
169 case VLC_CODEC_I420:
170 case VLC_CODEC_J420:
171 p_fmt->i_bits_per_pixel = 12;
172 break;
173 case VLC_CODEC_YV9:
174 case VLC_CODEC_I410:
175 p_fmt->i_bits_per_pixel = 9;
176 break;
177 case VLC_CODEC_Y211:
178 p_fmt->i_bits_per_pixel = 8;
179 break;
180 case VLC_CODEC_YUVP:
181 p_fmt->i_bits_per_pixel = 8;
182 break;
184 case VLC_CODEC_RGB32:
185 case VLC_CODEC_RGBA:
186 p_fmt->i_bits_per_pixel = 32;
187 break;
188 case VLC_CODEC_RGB24:
189 p_fmt->i_bits_per_pixel = 24;
190 break;
191 case VLC_CODEC_RGB15:
192 case VLC_CODEC_RGB16:
193 p_fmt->i_bits_per_pixel = 16;
194 break;
195 case VLC_CODEC_RGB8:
196 p_fmt->i_bits_per_pixel = 8;
197 break;
199 case VLC_CODEC_GREY:
200 case VLC_CODEC_RGBP:
201 p_fmt->i_bits_per_pixel = 8;
202 break;
204 default:
205 p_fmt->i_bits_per_pixel = 0;
206 break;
210 void video_format_CopyCrop( video_format_t *p_dst, const video_format_t *p_src )
212 p_dst->i_x_offset = p_src->i_x_offset;
213 p_dst->i_y_offset = p_src->i_y_offset;
214 p_dst->i_visible_width = p_src->i_visible_width;
215 p_dst->i_visible_height = p_src->i_visible_height;
218 void video_format_ScaleCropAr( video_format_t *p_dst, const video_format_t *p_src )
220 p_dst->i_x_offset = (uint64_t)p_src->i_x_offset * p_dst->i_width / p_src->i_width;
221 p_dst->i_y_offset = (uint64_t)p_src->i_y_offset * p_dst->i_height / p_src->i_height;
222 p_dst->i_visible_width = (uint64_t)p_src->i_visible_width * p_dst->i_width / p_src->i_width;
223 p_dst->i_visible_height = (uint64_t)p_src->i_visible_height * p_dst->i_height / p_src->i_height;
225 p_dst->i_sar_num *= p_src->i_width;
226 p_dst->i_sar_den *= p_dst->i_width;
227 vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
228 p_dst->i_sar_num, p_dst->i_sar_den, 65536);
230 p_dst->i_sar_num *= p_dst->i_height;
231 p_dst->i_sar_den *= p_src->i_height;
232 vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
233 p_dst->i_sar_num, p_dst->i_sar_den, 65536);
236 bool video_format_IsSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
238 video_format_t v1 = *p_fmt1;
239 video_format_t v2 = *p_fmt2;
241 if( v1.i_chroma != v2.i_chroma )
242 return false;
244 if( v1.i_width != v2.i_width || v1.i_height != v2.i_height ||
245 v1.i_visible_width != v2.i_visible_width ||
246 v1.i_visible_height != v2.i_visible_height ||
247 v1.i_x_offset != v2.i_x_offset || v1.i_y_offset != v2.i_y_offset )
248 return false;
249 if( v1.i_sar_num * v2.i_sar_den != v2.i_sar_num * v1.i_sar_den )
250 return false;
252 if( v1.i_chroma == VLC_CODEC_RGB15 ||
253 v1.i_chroma == VLC_CODEC_RGB16 ||
254 v1.i_chroma == VLC_CODEC_RGB24 ||
255 v1.i_chroma == VLC_CODEC_RGB32 )
257 video_format_FixRgb( &v1 );
258 video_format_FixRgb( &v2 );
260 if( v1.i_rmask != v2.i_rmask ||
261 v1.i_gmask != v2.i_gmask ||
262 v1.i_bmask != v2.i_bmask )
263 return false;
265 return true;
267 void video_format_Print( vlc_object_t *p_this,
268 const char *psz_text, const video_format_t *fmt )
270 msg_Dbg( p_this,
271 "%s sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, sar %i:%i, msk r0x%x g0x%x b0x%x",
272 psz_text,
273 fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
274 fmt->i_visible_width, fmt->i_visible_height,
275 (char*)&fmt->i_chroma,
276 fmt->i_sar_num, fmt->i_sar_den,
277 fmt->i_rmask, fmt->i_gmask, fmt->i_bmask );
280 void es_format_Init( es_format_t *fmt,
281 int i_cat, vlc_fourcc_t i_codec )
283 fmt->i_cat = i_cat;
284 fmt->i_codec = i_codec;
285 fmt->i_original_fourcc = 0;
286 fmt->i_profile = -1;
287 fmt->i_level = -1;
288 fmt->i_id = -1;
289 fmt->i_group = 0;
290 fmt->i_priority = 0;
291 fmt->psz_language = NULL;
292 fmt->psz_description = NULL;
294 fmt->i_extra_languages = 0;
295 fmt->p_extra_languages = NULL;
297 memset( &fmt->audio, 0, sizeof(audio_format_t) );
298 memset( &fmt->audio_replay_gain, 0, sizeof(audio_replay_gain_t) );
299 memset( &fmt->video, 0, sizeof(video_format_t) );
300 memset( &fmt->subs, 0, sizeof(subs_format_t) );
302 fmt->b_packetized = true;
303 fmt->i_bitrate = 0;
304 fmt->i_extra = 0;
305 fmt->p_extra = NULL;
308 void es_format_InitFromVideo( es_format_t *p_es, const video_format_t *p_fmt )
310 es_format_Init( p_es, VIDEO_ES, p_fmt->i_chroma );
311 video_format_Copy( &p_es->video, p_fmt );
314 int es_format_Copy( es_format_t *dst, const es_format_t *src )
316 int i;
317 memcpy( dst, src, sizeof( es_format_t ) );
318 dst->psz_language = src->psz_language ? strdup( src->psz_language ) : NULL;
319 dst->psz_description = src->psz_description ? strdup( src->psz_description ) : NULL;
320 if( src->i_extra > 0 && dst->p_extra )
322 dst->i_extra = src->i_extra;
323 dst->p_extra = malloc( src->i_extra );
324 if(dst->p_extra)
325 memcpy( dst->p_extra, src->p_extra, src->i_extra );
326 else
327 dst->i_extra = 0;
329 else
331 dst->i_extra = 0;
332 dst->p_extra = NULL;
335 dst->subs.psz_encoding = dst->subs.psz_encoding ? strdup( src->subs.psz_encoding ) : NULL;
337 if( src->video.p_palette )
339 dst->video.p_palette =
340 (video_palette_t*)malloc( sizeof( video_palette_t ) );
341 if(dst->video.p_palette)
343 memcpy( dst->video.p_palette, src->video.p_palette,
344 sizeof( video_palette_t ) );
348 if( dst->i_extra_languages && src->p_extra_languages)
350 dst->i_extra_languages = src->i_extra_languages;
351 dst->p_extra_languages = (extra_languages_t*)
352 malloc(dst->i_extra_languages * sizeof(*dst->p_extra_languages ));
353 if( dst->p_extra_languages )
355 for( i = 0; i < dst->i_extra_languages; i++ ) {
356 if( src->p_extra_languages[i].psz_language )
357 dst->p_extra_languages[i].psz_language = strdup( src->p_extra_languages[i].psz_language );
358 else
359 dst->p_extra_languages[i].psz_language = NULL;
360 if( src->p_extra_languages[i].psz_description )
361 dst->p_extra_languages[i].psz_description = strdup( src->p_extra_languages[i].psz_description );
362 else
363 dst->p_extra_languages[i].psz_description = NULL;
366 else
367 dst->i_extra_languages = 0;
369 else
370 dst->i_extra_languages = 0;
371 return VLC_SUCCESS;
374 void es_format_Clean( es_format_t *fmt )
376 free( fmt->psz_language );
377 free( fmt->psz_description );
379 if( fmt->i_extra > 0 ) free( fmt->p_extra );
381 free( fmt->video.p_palette );
382 free( fmt->subs.psz_encoding );
384 if( fmt->i_extra_languages > 0 && fmt->p_extra_languages )
386 int i;
387 for( i = 0; i < fmt->i_extra_languages; i++ )
389 free( fmt->p_extra_languages[i].psz_language );
390 free( fmt->p_extra_languages[i].psz_description );
392 free( fmt->p_extra_languages );
395 /* es_format_Clean can be called multiple times */
396 memset( fmt, 0, sizeof(*fmt) );
399 bool es_format_IsSimilar( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
401 if( p_fmt1->i_cat != p_fmt2->i_cat ||
402 vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec ) !=
403 vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec ) )
404 return false;
406 switch( p_fmt1->i_cat )
408 case AUDIO_ES:
410 audio_format_t a1 = p_fmt1->audio;
411 audio_format_t a2 = p_fmt2->audio;
413 if( a1.i_format && a2.i_format && a1.i_format != a2.i_format )
414 return false;
415 if( a1.i_rate != a2.i_rate ||
416 a1.i_physical_channels != a2.i_physical_channels ||
417 a1.i_original_channels != a2.i_original_channels )
418 return false;
419 return true;
422 case VIDEO_ES:
424 video_format_t v1 = p_fmt1->video;
425 video_format_t v2 = p_fmt2->video;
426 if( !v1.i_chroma )
427 v1.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec );
428 if( !v2.i_chroma )
429 v2.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt2->i_codec );
430 return video_format_IsSimilar( &p_fmt1->video, &p_fmt2->video );
433 case SPU_ES:
434 default:
435 return true;