]> git.sesse.net Git - vlc/blob - src/misc/es_format.c
es_format: avoid copy in video_format_IsSimilar() if possible
[vlc] / src / misc / es_format.c
1 /*****************************************************************************
2  * es_format.c : es_format_t helpers.
3  *****************************************************************************
4  * Copyright (C) 2008 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@videolan.org>
8  *
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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
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,
132                          int i_width, int i_height,
133                          int i_visible_width, int i_visible_height,
134                          int i_sar_num, int i_sar_den )
135 {
136     p_fmt->i_chroma         = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
137     p_fmt->i_width          = i_width;
138     p_fmt->i_visible_width  = i_visible_width;
139     p_fmt->i_height         = i_height;
140     p_fmt->i_visible_height = i_visible_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 );
145
146     switch( p_fmt->i_chroma )
147     {
148     case VLC_CODEC_YUVA:
149         p_fmt->i_bits_per_pixel = 32;
150         break;
151     case VLC_CODEC_YUV420A:
152         p_fmt->i_bits_per_pixel = 20;
153         break;
154     case VLC_CODEC_YUV422A:
155         p_fmt->i_bits_per_pixel = 24;
156         break;
157     case VLC_CODEC_I444:
158     case VLC_CODEC_J444:
159         p_fmt->i_bits_per_pixel = 24;
160         break;
161     case VLC_CODEC_I422:
162     case VLC_CODEC_YUYV:
163     case VLC_CODEC_YVYU:
164     case VLC_CODEC_UYVY:
165     case VLC_CODEC_VYUY:
166     case VLC_CODEC_J422:
167         p_fmt->i_bits_per_pixel = 16;
168         break;
169     case VLC_CODEC_I440:
170     case VLC_CODEC_J440:
171         p_fmt->i_bits_per_pixel = 16;
172         break;
173     case VLC_CODEC_I411:
174     case VLC_CODEC_YV12:
175     case VLC_CODEC_I420:
176     case VLC_CODEC_J420:
177         p_fmt->i_bits_per_pixel = 12;
178         break;
179     case VLC_CODEC_YV9:
180     case VLC_CODEC_I410:
181         p_fmt->i_bits_per_pixel = 9;
182         break;
183     case VLC_CODEC_Y211:
184         p_fmt->i_bits_per_pixel = 8;
185         break;
186     case VLC_CODEC_YUVP:
187         p_fmt->i_bits_per_pixel = 8;
188         break;
189
190     case VLC_CODEC_RGB32:
191     case VLC_CODEC_RGBA:
192     case VLC_CODEC_ARGB:
193     case VLC_CODEC_BGRA:
194         p_fmt->i_bits_per_pixel = 32;
195         break;
196     case VLC_CODEC_RGB24:
197         p_fmt->i_bits_per_pixel = 24;
198         break;
199     case VLC_CODEC_RGB15:
200     case VLC_CODEC_RGB16:
201         p_fmt->i_bits_per_pixel = 16;
202         break;
203     case VLC_CODEC_RGB8:
204         p_fmt->i_bits_per_pixel = 8;
205         break;
206
207     case VLC_CODEC_GREY:
208     case VLC_CODEC_RGBP:
209         p_fmt->i_bits_per_pixel = 8;
210         break;
211
212     case VLC_CODEC_XYZ12:
213         p_fmt->i_bits_per_pixel = 48;
214         break;
215
216     default:
217         p_fmt->i_bits_per_pixel = 0;
218         break;
219     }
220 }
221
222 void video_format_CopyCrop( video_format_t *p_dst, const video_format_t *p_src )
223 {
224     p_dst->i_x_offset       = p_src->i_x_offset;
225     p_dst->i_y_offset       = p_src->i_y_offset;
226     p_dst->i_visible_width  = p_src->i_visible_width;
227     p_dst->i_visible_height = p_src->i_visible_height;
228 }
229
230 void video_format_ScaleCropAr( video_format_t *p_dst, const video_format_t *p_src )
231 {
232     p_dst->i_x_offset       = (uint64_t)p_src->i_x_offset       * p_dst->i_width  / p_src->i_width;
233     p_dst->i_y_offset       = (uint64_t)p_src->i_y_offset       * p_dst->i_height / p_src->i_height;
234     p_dst->i_visible_width  = (uint64_t)p_src->i_visible_width  * p_dst->i_width  / p_src->i_width;
235     p_dst->i_visible_height = (uint64_t)p_src->i_visible_height * p_dst->i_height / p_src->i_height;
236
237     p_dst->i_sar_num *= p_src->i_width;
238     p_dst->i_sar_den *= p_dst->i_width;
239     vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
240                 p_dst->i_sar_num, p_dst->i_sar_den, 65536);
241
242     p_dst->i_sar_num *= p_dst->i_height;
243     p_dst->i_sar_den *= p_src->i_height;
244     vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
245                 p_dst->i_sar_num, p_dst->i_sar_den, 65536);
246 }
247
248 //Simplify transforms to have something more managable. Order: angle, hflip.
249 static void transform_GetBasicOps( video_transform_t transform,
250                                    unsigned *restrict angle,
251                                    bool *restrict hflip )
252 {
253     *hflip = ORIENT_IS_MIRROR(transform);
254
255     switch ( transform )
256     {
257         case TRANSFORM_R90:
258         case TRANSFORM_TRANSPOSE:
259             *angle = 90;
260             break;
261         case TRANSFORM_R180:
262         case TRANSFORM_VFLIP:
263             *angle = 180;
264             break;
265         case TRANSFORM_R270:
266         case TRANSFORM_ANTI_TRANSPOSE:
267             *angle = 270;
268             break;
269         case TRANSFORM_HFLIP:
270         case TRANSFORM_IDENTITY:
271             *angle = 0;
272             break;
273     }
274 }
275
276 static video_transform_t transform_FromBasicOps( unsigned angle, bool hflip )
277 {
278     switch ( angle )
279     {
280         case 90:
281             return hflip ? TRANSFORM_TRANSPOSE : TRANSFORM_R90;
282         case 180:
283             return hflip ? TRANSFORM_VFLIP : TRANSFORM_R180;
284         case 270:
285             return hflip ? TRANSFORM_ANTI_TRANSPOSE : TRANSFORM_R270;
286         default:
287             return hflip ? TRANSFORM_HFLIP : TRANSFORM_IDENTITY;
288     }
289 }
290
291 video_transform_t video_format_GetTransform( video_orientation_t src,
292                                              video_orientation_t dst )
293 {
294     unsigned angle1, angle2;
295     bool hflip1, hflip2;
296
297     transform_GetBasicOps(  (video_transform_t)src, &angle1, &hflip1 );
298     transform_GetBasicOps( transform_Inverse( (video_transform_t)dst ),
299                            &angle2, &hflip2 );
300
301     int angle = (angle1 + angle2) % 360;
302     bool hflip = hflip1 ^ hflip2;
303
304     return transform_FromBasicOps(angle, hflip);
305 }
306
307 void video_format_TransformBy( video_format_t *fmt, video_transform_t transform )
308 {
309     /* Get destination orientation */
310     unsigned angle1, angle2;
311     bool hflip1, hflip2;
312
313     transform_GetBasicOps( transform, &angle1, &hflip1 );
314     transform_GetBasicOps( (video_transform_t)fmt->orientation, &angle2, &hflip2 );
315
316     unsigned angle = (angle2 - angle1 + 360) % 360;
317     bool hflip = hflip2 ^ hflip1;
318
319     video_orientation_t dst_orient = ORIENT_NORMAL;
320
321     if( hflip ) {
322
323         if( angle == 0 )
324             dst_orient = ORIENT_HFLIPPED;
325         else if( angle == 90 )
326             dst_orient = ORIENT_ANTI_TRANSPOSED;
327         else if( angle == 180 )
328             dst_orient = ORIENT_VFLIPPED;
329         else if( angle == 270 )
330             dst_orient = ORIENT_TRANSPOSED;
331     }
332     else {
333
334         if( angle == 90 )
335             dst_orient = ORIENT_ROTATED_90;
336         else if( angle == 180 )
337             dst_orient = ORIENT_ROTATED_180;
338         else if( angle == 270 )
339             dst_orient = ORIENT_ROTATED_270;
340     }
341
342     /* Apply transform */
343     if( ORIENT_IS_SWAP( fmt->orientation ) != ORIENT_IS_SWAP( dst_orient ) )
344     {
345         video_format_t scratch = *fmt;
346
347         fmt->i_width = scratch.i_height;
348         fmt->i_visible_width = scratch.i_visible_height;
349         fmt->i_height = scratch.i_width;
350         fmt->i_visible_height = scratch.i_visible_width;
351         fmt->i_x_offset = scratch.i_y_offset;
352         fmt->i_y_offset = scratch.i_x_offset;
353         fmt->i_sar_num = scratch.i_sar_den;
354         fmt->i_sar_den = scratch.i_sar_num;
355     }
356
357     fmt->orientation = dst_orient;
358 }
359
360 void video_format_TransformTo( video_format_t *restrict fmt,
361                                video_orientation_t dst_orientation )
362 {
363     video_transform_t transform = video_format_GetTransform(fmt->orientation,
364                                                             dst_orientation);
365     video_format_TransformBy(fmt, transform);
366 }
367
368 void video_format_ApplyRotation( video_format_t *restrict out,
369                                  const video_format_t *restrict in )
370 {
371     *out = *in;
372
373     video_format_TransformTo(out, ORIENT_NORMAL);
374 }
375
376 bool video_format_IsSimilar( const video_format_t *f1,
377                              const video_format_t *f2 )
378 {
379     if( f1->i_chroma != f2->i_chroma )
380         return false;
381
382     if( f1->i_width != f2->i_width || f1->i_height != f2->i_height ||
383         f1->i_visible_width != f2->i_visible_width ||
384         f1->i_visible_height != f2->i_visible_height ||
385         f1->i_x_offset != f2->i_x_offset || f1->i_y_offset != f2->i_y_offset )
386         return false;
387     if( f1->i_sar_num * f2->i_sar_den != f2->i_sar_num * f1->i_sar_den )
388         return false;
389
390     if( f1->orientation != f2->orientation)
391         return false;
392
393     if( f1->i_chroma == VLC_CODEC_RGB15 ||
394         f1->i_chroma == VLC_CODEC_RGB16 ||
395         f1->i_chroma == VLC_CODEC_RGB24 ||
396         f1->i_chroma == VLC_CODEC_RGB32 )
397     {
398         video_format_t v1 = *f1;
399         video_format_t v2 = *f2;
400
401         video_format_FixRgb( &v1 );
402         video_format_FixRgb( &v2 );
403
404         if( v1.i_rmask != v2.i_rmask ||
405             v1.i_gmask != v2.i_gmask ||
406             v1.i_bmask != v2.i_bmask )
407             return false;
408     }
409     return true;
410 }
411 void video_format_Print( vlc_object_t *p_this,
412                          const char *psz_text, const video_format_t *fmt )
413 {
414     msg_Dbg( p_this,
415              "%s sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, sar %i:%i, msk r0x%x g0x%x b0x%x",
416              psz_text,
417              fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
418              fmt->i_visible_width, fmt->i_visible_height,
419              (char*)&fmt->i_chroma,
420              fmt->i_sar_num, fmt->i_sar_den,
421              fmt->i_rmask, fmt->i_gmask, fmt->i_bmask );
422 }
423
424 void es_format_Init( es_format_t *fmt,
425                      int i_cat, vlc_fourcc_t i_codec )
426 {
427     fmt->i_cat                  = i_cat;
428     fmt->i_codec                = i_codec;
429     fmt->i_original_fourcc      = 0;
430     fmt->i_profile              = -1;
431     fmt->i_level                = -1;
432     fmt->i_id                   = -1;
433     fmt->i_group                = 0;
434     fmt->i_priority             = ES_PRIORITY_SELECTABLE_MIN;
435     fmt->psz_language           = NULL;
436     fmt->psz_description        = NULL;
437
438     fmt->i_extra_languages      = 0;
439     fmt->p_extra_languages      = NULL;
440
441     memset( &fmt->audio, 0, sizeof(audio_format_t) );
442     memset( &fmt->audio_replay_gain, 0, sizeof(audio_replay_gain_t) );
443     memset( &fmt->video, 0, sizeof(video_format_t) );
444     memset( &fmt->subs, 0, sizeof(subs_format_t) );
445
446     fmt->b_packetized           = true;
447     fmt->i_bitrate              = 0;
448     fmt->i_extra                = 0;
449     fmt->p_extra                = NULL;
450 }
451
452 void es_format_InitFromVideo( es_format_t *p_es, const video_format_t *p_fmt )
453 {
454     es_format_Init( p_es, VIDEO_ES, p_fmt->i_chroma );
455     video_format_Copy( &p_es->video, p_fmt );
456 }
457
458 int es_format_Copy( es_format_t *dst, const es_format_t *src )
459 {
460     int i;
461     memcpy( dst, src, sizeof( es_format_t ) );
462     dst->psz_language = src->psz_language ? strdup( src->psz_language ) : NULL;
463     dst->psz_description = src->psz_description ? strdup( src->psz_description ) : NULL;
464     if( src->i_extra > 0 && dst->p_extra )
465     {
466         dst->i_extra = src->i_extra;
467         dst->p_extra = malloc( src->i_extra );
468         if(dst->p_extra)
469             memcpy( dst->p_extra, src->p_extra, src->i_extra );
470         else
471             dst->i_extra = 0;
472     }
473     else
474     {
475         dst->i_extra = 0;
476         dst->p_extra = NULL;
477     }
478
479     dst->subs.psz_encoding = dst->subs.psz_encoding ? strdup( src->subs.psz_encoding ) : NULL;
480     dst->subs.p_style = src->subs.p_style ? text_style_Duplicate( src->subs.p_style ) : NULL;
481
482     if( src->video.p_palette )
483     {
484         dst->video.p_palette =
485             (video_palette_t*)malloc( sizeof( video_palette_t ) );
486         if(dst->video.p_palette)
487         {
488             memcpy( dst->video.p_palette, src->video.p_palette,
489                 sizeof( video_palette_t ) );
490         }
491     }
492
493     if( dst->i_extra_languages && src->p_extra_languages)
494     {
495         dst->i_extra_languages = src->i_extra_languages;
496         dst->p_extra_languages = (extra_languages_t*)
497             malloc(dst->i_extra_languages * sizeof(*dst->p_extra_languages ));
498         if( dst->p_extra_languages )
499         {
500             for( i = 0; i < dst->i_extra_languages; i++ ) {
501                 if( src->p_extra_languages[i].psz_language )
502                     dst->p_extra_languages[i].psz_language = strdup( src->p_extra_languages[i].psz_language );
503                 else
504                     dst->p_extra_languages[i].psz_language = NULL;
505                 if( src->p_extra_languages[i].psz_description )
506                     dst->p_extra_languages[i].psz_description = strdup( src->p_extra_languages[i].psz_description );
507                 else
508                     dst->p_extra_languages[i].psz_description = NULL;
509             }
510         }
511         else
512             dst->i_extra_languages = 0;
513     }
514     else
515         dst->i_extra_languages = 0;
516     return VLC_SUCCESS;
517 }
518
519 void es_format_Clean( es_format_t *fmt )
520 {
521     free( fmt->psz_language );
522     free( fmt->psz_description );
523
524     if( fmt->i_extra > 0 ) free( fmt->p_extra );
525
526     free( fmt->video.p_palette );
527     free( fmt->subs.psz_encoding );
528
529     if ( fmt->subs.p_style ) text_style_Delete( fmt->subs.p_style );
530
531     if( fmt->i_extra_languages > 0 && fmt->p_extra_languages )
532     {
533         int i;
534         for( i = 0; i < fmt->i_extra_languages; i++ )
535         {
536             free( fmt->p_extra_languages[i].psz_language );
537             free( fmt->p_extra_languages[i].psz_description );
538         }
539         free( fmt->p_extra_languages );
540     }
541
542     /* es_format_Clean can be called multiple times */
543     memset( fmt, 0, sizeof(*fmt) );
544 }
545
546 bool es_format_IsSimilar( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
547 {
548     if( p_fmt1->i_cat != p_fmt2->i_cat ||
549         vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec ) !=
550         vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec ) )
551         return false;
552
553     switch( p_fmt1->i_cat )
554     {
555     case AUDIO_ES:
556     {
557         audio_format_t a1 = p_fmt1->audio;
558         audio_format_t a2 = p_fmt2->audio;
559
560         if( a1.i_format && a2.i_format && a1.i_format != a2.i_format )
561             return false;
562         if( a1.i_rate != a2.i_rate ||
563             a1.i_physical_channels != a2.i_physical_channels ||
564             a1.i_original_channels != a2.i_original_channels )
565             return false;
566         return true;
567     }
568
569     case VIDEO_ES:
570     {
571         video_format_t v1 = p_fmt1->video;
572         video_format_t v2 = p_fmt2->video;
573         if( !v1.i_chroma )
574             v1.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec );
575         if( !v2.i_chroma )
576             v2.i_chroma = vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec );
577         return video_format_IsSimilar( &p_fmt1->video, &p_fmt2->video );
578     }
579
580     case SPU_ES:
581     default:
582         return true;
583     }
584 }
585