1 /*****************************************************************************
2 * es_format.c : es_format_t helpers.
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
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 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
38 /*****************************************************************************
39 * BinaryLog: computes the base 2 log of a binary value
40 *****************************************************************************
41 * This functions is used by MaskToShift, to get a bit index from a binary
43 *****************************************************************************/
44 static int BinaryLog( uint32_t i )
48 if( i == 0 ) return -31337;
50 if( i & 0xffff0000 ) i_log += 16;
51 if( i & 0xff00ff00 ) i_log += 8;
52 if( i & 0xf0f0f0f0 ) i_log += 4;
53 if( i & 0xcccccccc ) i_log += 2;
54 if( i & 0xaaaaaaaa ) i_log += 1;
60 * It transforms a color mask into right and left shifts
61 * FIXME copied from video_output.c
63 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
65 uint32_t i_low, i_high; /* lower and higher bits of the mask */
69 *pi_left = *pi_right = 0;
74 i_low = i_high = i_mask;
76 i_low &= - (int32_t)i_low; /* lower bit of the mask */
77 i_high += i_low; /* higher bit of the mask */
79 /* Transform bits into an index. Also deal with i_high overflow, which
80 * is faster than changing the BinaryLog code to handle 64 bit integers. */
81 i_low = BinaryLog (i_low);
82 i_high = i_high ? BinaryLog (i_high) : 32;
84 /* Update pointers and return */
86 *pi_right = (8 - i_high + i_low);
90 void video_format_FixRgb( video_format_t *p_fmt )
92 /* FIXME find right default mask */
93 if( !p_fmt->i_rmask || !p_fmt->i_gmask || !p_fmt->i_bmask )
95 switch( p_fmt->i_chroma )
98 p_fmt->i_rmask = 0x7c00;
99 p_fmt->i_gmask = 0x03e0;
100 p_fmt->i_bmask = 0x001f;
103 case VLC_CODEC_RGB16:
104 p_fmt->i_rmask = 0xf800;
105 p_fmt->i_gmask = 0x07e0;
106 p_fmt->i_bmask = 0x001f;
109 case VLC_CODEC_RGB24:
110 p_fmt->i_rmask = 0xff0000;
111 p_fmt->i_gmask = 0x00ff00;
112 p_fmt->i_bmask = 0x0000ff;
114 case VLC_CODEC_RGB32:
115 p_fmt->i_rmask = 0x00ff0000;
116 p_fmt->i_gmask = 0x0000ff00;
117 p_fmt->i_bmask = 0x000000ff;
125 MaskToShift( &p_fmt->i_lrshift, &p_fmt->i_rrshift,
127 MaskToShift( &p_fmt->i_lgshift, &p_fmt->i_rgshift,
129 MaskToShift( &p_fmt->i_lbshift, &p_fmt->i_rbshift,
133 void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma,
134 int i_width, int i_height,
135 int i_visible_width, int i_visible_height,
136 int i_sar_num, int i_sar_den )
138 p_fmt->i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
139 p_fmt->i_width = i_width;
140 p_fmt->i_visible_width = i_visible_width;
141 p_fmt->i_height = i_height;
142 p_fmt->i_visible_height = i_visible_height;
144 p_fmt->i_y_offset = 0;
145 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
146 i_sar_num, i_sar_den, 0 );
148 switch( p_fmt->i_chroma )
151 p_fmt->i_bits_per_pixel = 32;
153 case VLC_CODEC_YUV420A:
154 p_fmt->i_bits_per_pixel = 20;
156 case VLC_CODEC_YUV422A:
157 p_fmt->i_bits_per_pixel = 24;
161 p_fmt->i_bits_per_pixel = 24;
169 p_fmt->i_bits_per_pixel = 16;
173 p_fmt->i_bits_per_pixel = 16;
179 p_fmt->i_bits_per_pixel = 12;
183 p_fmt->i_bits_per_pixel = 9;
186 p_fmt->i_bits_per_pixel = 8;
189 p_fmt->i_bits_per_pixel = 8;
192 case VLC_CODEC_RGB32:
196 p_fmt->i_bits_per_pixel = 32;
198 case VLC_CODEC_RGB24:
199 p_fmt->i_bits_per_pixel = 24;
201 case VLC_CODEC_RGB15:
202 case VLC_CODEC_RGB16:
203 p_fmt->i_bits_per_pixel = 16;
206 p_fmt->i_bits_per_pixel = 8;
211 p_fmt->i_bits_per_pixel = 8;
214 case VLC_CODEC_XYZ12:
215 p_fmt->i_bits_per_pixel = 48;
219 p_fmt->i_bits_per_pixel = 0;
224 void video_format_CopyCrop( video_format_t *p_dst, const video_format_t *p_src )
226 p_dst->i_x_offset = p_src->i_x_offset;
227 p_dst->i_y_offset = p_src->i_y_offset;
228 p_dst->i_visible_width = p_src->i_visible_width;
229 p_dst->i_visible_height = p_src->i_visible_height;
232 void video_format_ScaleCropAr( video_format_t *p_dst, const video_format_t *p_src )
234 p_dst->i_x_offset = (uint64_t)p_src->i_x_offset * p_dst->i_width / p_src->i_width;
235 p_dst->i_y_offset = (uint64_t)p_src->i_y_offset * p_dst->i_height / p_src->i_height;
236 p_dst->i_visible_width = (uint64_t)p_src->i_visible_width * p_dst->i_width / p_src->i_width;
237 p_dst->i_visible_height = (uint64_t)p_src->i_visible_height * p_dst->i_height / p_src->i_height;
239 p_dst->i_sar_num *= p_src->i_width;
240 p_dst->i_sar_den *= p_dst->i_width;
241 vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
242 p_dst->i_sar_num, p_dst->i_sar_den, 65536);
244 p_dst->i_sar_num *= p_dst->i_height;
245 p_dst->i_sar_den *= p_src->i_height;
246 vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
247 p_dst->i_sar_num, p_dst->i_sar_den, 65536);
250 //Simplify transforms to have something more managable. Order: angle, hflip.
251 static void transform_GetBasicOps( video_transform_t transform,
252 unsigned *restrict angle,
253 bool *restrict hflip )
255 *hflip = ORIENT_IS_MIRROR(transform);
260 case TRANSFORM_TRANSPOSE:
264 case TRANSFORM_VFLIP:
268 case TRANSFORM_ANTI_TRANSPOSE:
271 case TRANSFORM_HFLIP:
272 case TRANSFORM_IDENTITY:
278 static video_transform_t transform_FromBasicOps( unsigned angle, bool hflip )
283 return hflip ? TRANSFORM_TRANSPOSE : TRANSFORM_R90;
285 return hflip ? TRANSFORM_VFLIP : TRANSFORM_R180;
287 return hflip ? TRANSFORM_ANTI_TRANSPOSE : TRANSFORM_R270;
289 return hflip ? TRANSFORM_HFLIP : TRANSFORM_IDENTITY;
293 video_transform_t video_format_GetTransform( video_orientation_t src,
294 video_orientation_t dst )
296 unsigned angle1, angle2;
299 transform_GetBasicOps( (video_transform_t)src, &angle1, &hflip1 );
300 transform_GetBasicOps( transform_Inverse( (video_transform_t)dst ),
303 int angle = (angle1 + angle2) % 360;
304 bool hflip = hflip1 ^ hflip2;
306 return transform_FromBasicOps(angle, hflip);
309 void video_format_TransformBy( video_format_t *fmt, video_transform_t transform )
311 /* Get destination orientation */
312 unsigned angle1, angle2;
315 transform_GetBasicOps( transform, &angle1, &hflip1 );
316 transform_GetBasicOps( (video_transform_t)fmt->orientation, &angle2, &hflip2 );
318 unsigned angle = (angle2 - angle1 + 360) % 360;
319 bool hflip = hflip2 ^ hflip1;
321 video_orientation_t dst_orient = ORIENT_NORMAL;
326 dst_orient = ORIENT_HFLIPPED;
327 else if( angle == 90 )
328 dst_orient = ORIENT_ANTI_TRANSPOSED;
329 else if( angle == 180 )
330 dst_orient = ORIENT_VFLIPPED;
331 else if( angle == 270 )
332 dst_orient = ORIENT_TRANSPOSED;
337 dst_orient = ORIENT_ROTATED_90;
338 else if( angle == 180 )
339 dst_orient = ORIENT_ROTATED_180;
340 else if( angle == 270 )
341 dst_orient = ORIENT_ROTATED_270;
344 /* Apply transform */
345 if( ORIENT_IS_SWAP( fmt->orientation ) != ORIENT_IS_SWAP( dst_orient ) )
347 video_format_t scratch = *fmt;
349 fmt->i_width = scratch.i_height;
350 fmt->i_visible_width = scratch.i_visible_height;
351 fmt->i_height = scratch.i_width;
352 fmt->i_visible_height = scratch.i_visible_width;
353 fmt->i_x_offset = scratch.i_y_offset;
354 fmt->i_y_offset = scratch.i_x_offset;
355 fmt->i_sar_num = scratch.i_sar_den;
356 fmt->i_sar_den = scratch.i_sar_num;
359 fmt->orientation = dst_orient;
362 void video_format_TransformTo( video_format_t *restrict fmt,
363 video_orientation_t dst_orientation )
365 video_transform_t transform = video_format_GetTransform(fmt->orientation,
367 video_format_TransformBy(fmt, transform);
370 void video_format_ApplyRotation( video_format_t *restrict out,
371 const video_format_t *restrict in )
375 video_format_TransformTo(out, ORIENT_NORMAL);
378 bool video_format_IsSimilar( const video_format_t *f1,
379 const video_format_t *f2 )
381 if( f1->i_chroma != f2->i_chroma )
384 if( f1->i_width != f2->i_width || f1->i_height != f2->i_height ||
385 f1->i_visible_width != f2->i_visible_width ||
386 f1->i_visible_height != f2->i_visible_height ||
387 f1->i_x_offset != f2->i_x_offset || f1->i_y_offset != f2->i_y_offset )
389 if( f1->i_sar_num * f2->i_sar_den != f2->i_sar_num * f1->i_sar_den )
392 if( f1->orientation != f2->orientation)
395 if( f1->i_chroma == VLC_CODEC_RGB15 ||
396 f1->i_chroma == VLC_CODEC_RGB16 ||
397 f1->i_chroma == VLC_CODEC_RGB24 ||
398 f1->i_chroma == VLC_CODEC_RGB32 )
400 video_format_t v1 = *f1;
401 video_format_t v2 = *f2;
403 video_format_FixRgb( &v1 );
404 video_format_FixRgb( &v2 );
406 if( v1.i_rmask != v2.i_rmask ||
407 v1.i_gmask != v2.i_gmask ||
408 v1.i_bmask != v2.i_bmask )
413 void video_format_Print( vlc_object_t *p_this,
414 const char *psz_text, const video_format_t *fmt )
417 "%s sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, sar %i:%i, msk r0x%x g0x%x b0x%x",
419 fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
420 fmt->i_visible_width, fmt->i_visible_height,
421 (char*)&fmt->i_chroma,
422 fmt->i_sar_num, fmt->i_sar_den,
423 fmt->i_rmask, fmt->i_gmask, fmt->i_bmask );
426 void es_format_Init( es_format_t *fmt,
427 int i_cat, vlc_fourcc_t i_codec )
430 fmt->i_codec = i_codec;
431 fmt->i_original_fourcc = 0;
436 fmt->i_priority = ES_PRIORITY_SELECTABLE_MIN;
437 fmt->psz_language = NULL;
438 fmt->psz_description = NULL;
440 fmt->i_extra_languages = 0;
441 fmt->p_extra_languages = NULL;
443 memset( &fmt->audio, 0, sizeof(audio_format_t) );
444 memset( &fmt->audio_replay_gain, 0, sizeof(audio_replay_gain_t) );
445 memset( &fmt->video, 0, sizeof(video_format_t) );
446 memset( &fmt->subs, 0, sizeof(subs_format_t) );
448 fmt->b_packetized = true;
454 void es_format_InitFromVideo( es_format_t *p_es, const video_format_t *p_fmt )
456 es_format_Init( p_es, VIDEO_ES, p_fmt->i_chroma );
457 video_format_Copy( &p_es->video, p_fmt );
460 int es_format_Copy(es_format_t *restrict dst, const es_format_t *src)
462 int ret = VLC_SUCCESS;
466 if (src->psz_language != NULL)
468 dst->psz_language = strdup(src->psz_language);
469 if (unlikely(dst->psz_language == NULL))
472 if (src->psz_description != NULL)
474 dst->psz_description = strdup(src->psz_description);
475 if (unlikely(dst->psz_description == NULL))
479 if (src->i_extra > 0)
481 assert(src->p_extra != NULL);
482 dst->p_extra = malloc( src->i_extra );
484 if( likely(dst->p_extra != NULL) )
485 memcpy(dst->p_extra, src->p_extra, src->i_extra);
493 if (src->subs.psz_encoding != NULL)
495 dst->subs.psz_encoding = strdup(src->subs.psz_encoding);
496 if (unlikely(dst->subs.psz_encoding == NULL))
499 if (src->subs.p_style != NULL)
501 dst->subs.p_style = text_style_Duplicate(src->subs.p_style);
502 if (unlikely(dst->subs.p_style == NULL))
506 if (src->video.p_palette != NULL)
508 dst->video.p_palette = malloc(sizeof (video_palette_t));
509 if (likely(dst->video.p_palette != NULL))
510 *dst->video.p_palette = *src->video.p_palette;
515 if (src->i_extra_languages > 0)
517 assert(src->p_extra_languages != NULL);
518 dst->p_extra_languages = calloc(dst->i_extra_languages,
519 sizeof (*dst->p_extra_languages));
520 if (likely(dst->p_extra_languages != NULL))
522 for (unsigned i = 0; i < dst->i_extra_languages; i++)
524 if (src->p_extra_languages[i].psz_language != NULL)
525 dst->p_extra_languages[i].psz_language = strdup(src->p_extra_languages[i].psz_language);
526 if (src->p_extra_languages[i].psz_description != NULL)
527 dst->p_extra_languages[i].psz_description = strdup(src->p_extra_languages[i].psz_description);
529 dst->i_extra_languages = src->i_extra_languages;
533 dst->i_extra_languages = 0;
540 void es_format_Clean(es_format_t *fmt)
542 free(fmt->psz_language);
543 free(fmt->psz_description);
544 assert(fmt->i_extra == 0 || fmt->p_extra != NULL);
547 free(fmt->video.p_palette);
548 free(fmt->subs.psz_encoding);
550 if (fmt->subs.p_style != NULL)
551 text_style_Delete(fmt->subs.p_style);
553 for (unsigned i = 0; i < fmt->i_extra_languages; i++)
555 free(fmt->p_extra_languages[i].psz_language);
556 free(fmt->p_extra_languages[i].psz_description);
558 free(fmt->p_extra_languages);
560 /* es_format_Clean can be called multiple times */
561 es_format_Init(fmt, UNKNOWN_ES, 0);
564 bool es_format_IsSimilar( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
566 if( p_fmt1->i_cat != p_fmt2->i_cat ||
567 vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec ) !=
568 vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec ) )
571 switch( p_fmt1->i_cat )
575 audio_format_t a1 = p_fmt1->audio;
576 audio_format_t a2 = p_fmt2->audio;
578 if( a1.i_format && a2.i_format && a1.i_format != a2.i_format )
580 if( a1.i_rate != a2.i_rate ||
581 a1.i_physical_channels != a2.i_physical_channels ||
582 a1.i_original_channels != a2.i_original_channels )
589 video_format_t v1 = p_fmt1->video;
590 video_format_t v2 = p_fmt2->video;
592 v1.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec );
594 v2.i_chroma = vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec );
595 return video_format_IsSimilar( &p_fmt1->video, &p_fmt2->video );