]> git.sesse.net Git - vlc/blob - src/misc/es_format.c
Cleaned up WMVA fourcc.
[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 #include <vlc_aout.h>
35
36
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 )
44 {
45     int i_log = 0;
46
47     if( i == 0 ) return -31337;
48
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;
54
55     return i_log;
56 }
57
58 /**
59  * It transforms a color mask into right and left shifts
60  * FIXME copied from video_output.c
61  */
62 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
63 {
64     uint32_t i_low, i_high;            /* lower and higher bits of the mask */
65
66     if( !i_mask )
67     {
68         *pi_left = *pi_right = 0;
69         return;
70     }
71
72     /* Get bits */
73     i_low = i_high = i_mask;
74
75     i_low &= - (int32_t)i_low;          /* lower bit of the mask */
76     i_high += i_low;                    /* higher bit of the mask */
77
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;
82
83     /* Update pointers and return */
84     *pi_left =   i_low;
85     *pi_right = (8 - i_high + i_low);
86 }
87
88 /* */
89 void video_format_FixRgb( video_format_t *p_fmt )
90 {
91     /* FIXME find right default mask */
92     if( !p_fmt->i_rmask || !p_fmt->i_gmask || !p_fmt->i_bmask )
93     {
94         switch( p_fmt->i_chroma )
95         {
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;
101
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;
107
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;
118
119         default:
120             return;
121         }
122     }
123
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 );
130 }
131
132 void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
133 {
134     p_fmt->i_chroma         = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
135     p_fmt->i_width          =
136     p_fmt->i_visible_width  = i_width;
137     p_fmt->i_height         =
138     p_fmt->i_visible_height = i_height;
139     p_fmt->i_x_offset       =
140     p_fmt->i_y_offset       = 0;
141     p_fmt->i_aspect         = i_aspect;
142
143     switch( p_fmt->i_chroma )
144     {
145     case VLC_CODEC_YUVA:
146         p_fmt->i_bits_per_pixel = 32;
147         break;
148     case VLC_CODEC_I444:
149     case VLC_CODEC_J444:
150         p_fmt->i_bits_per_pixel = 24;
151         break;
152     case VLC_CODEC_I422:
153     case VLC_CODEC_YUYV:
154     case VLC_CODEC_YVYU:
155     case VLC_CODEC_UYVY:
156     case VLC_CODEC_VYUY:
157     case VLC_CODEC_J422:
158         p_fmt->i_bits_per_pixel = 16;
159         break;
160     case VLC_CODEC_I440:
161     case VLC_CODEC_J440:
162         p_fmt->i_bits_per_pixel = 16;
163         break;
164     case VLC_CODEC_I411:
165     case VLC_CODEC_YV12:
166     case VLC_CODEC_I420:
167     case VLC_CODEC_J420:
168         p_fmt->i_bits_per_pixel = 12;
169         break;
170     case VLC_CODEC_I410:
171         p_fmt->i_bits_per_pixel = 9;
172         break;
173     case VLC_CODEC_Y211:
174         p_fmt->i_bits_per_pixel = 8;
175         break;
176     case VLC_CODEC_YUVP:
177         p_fmt->i_bits_per_pixel = 8;
178         break;
179
180     case VLC_CODEC_RGB32:
181     case VLC_CODEC_RGBA:
182         p_fmt->i_bits_per_pixel = 32;
183         break;
184     case VLC_CODEC_RGB24:
185         p_fmt->i_bits_per_pixel = 24;
186         break;
187     case VLC_CODEC_RGB15:
188     case VLC_CODEC_RGB16:
189         p_fmt->i_bits_per_pixel = 16;
190         break;
191     case VLC_CODEC_RGB8:
192         p_fmt->i_bits_per_pixel = 8;
193         break;
194
195     case VLC_CODEC_GREY:
196     case VLC_CODEC_RGBP:
197         p_fmt->i_bits_per_pixel = 8;
198         break;
199
200     default:
201         p_fmt->i_bits_per_pixel = 0;
202         break;
203     }
204 }
205 bool video_format_IsSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
206 {
207     video_format_t v1 = *p_fmt1;
208     video_format_t v2 = *p_fmt2;
209
210     if( v1.i_chroma != v2.i_chroma )
211         return false;
212
213     if( v1.i_width != v2.i_width || v1.i_height != v2.i_height ||
214         v1.i_visible_width != v2.i_visible_width ||
215         v1.i_visible_height != v2.i_visible_height ||
216         v1.i_x_offset != v2.i_x_offset || v1.i_y_offset != v2.i_y_offset )
217         return false;
218
219     if( v1.i_chroma == VLC_CODEC_RGB15 ||
220         v1.i_chroma == VLC_CODEC_RGB16 ||
221         v1.i_chroma == VLC_CODEC_RGB24 ||
222         v1.i_chroma == VLC_CODEC_RGB32 )
223     {
224         video_format_FixRgb( &v1 );
225         video_format_FixRgb( &v2 );
226
227         if( v1.i_rmask != v2.i_rmask ||
228             v1.i_gmask != v2.i_gmask ||
229             v1.i_bmask != v2.i_bmask )
230             return false;
231     }
232     return true;
233 }
234
235 void es_format_Init( es_format_t *fmt,
236                      int i_cat, vlc_fourcc_t i_codec )
237 {
238     fmt->i_cat                  = i_cat;
239     fmt->i_codec                = i_codec;
240     fmt->i_original_fourcc      = 0;
241     fmt->i_id                   = -1;
242     fmt->i_group                = 0;
243     fmt->i_priority             = 0;
244     fmt->psz_language           = NULL;
245     fmt->psz_description        = NULL;
246
247     fmt->i_extra_languages      = 0;
248     fmt->p_extra_languages      = NULL;
249
250     memset( &fmt->audio, 0, sizeof(audio_format_t) );
251     memset( &fmt->audio_replay_gain, 0, sizeof(audio_replay_gain_t) );
252     memset( &fmt->video, 0, sizeof(video_format_t) );
253     memset( &fmt->subs, 0, sizeof(subs_format_t) );
254
255     fmt->b_packetized           = true;
256     fmt->i_bitrate              = 0;
257     fmt->i_extra                = 0;
258     fmt->p_extra                = NULL;
259 }
260
261 void es_format_InitFromVideo( es_format_t *p_es, const video_format_t *p_fmt )
262 {
263     es_format_Init( p_es, VIDEO_ES, p_fmt->i_chroma );
264     video_format_Copy( &p_es->video, p_fmt );
265 }
266
267 int es_format_Copy( es_format_t *dst, const es_format_t *src )
268 {
269     int i;
270     memcpy( dst, src, sizeof( es_format_t ) );
271     dst->psz_language = src->psz_language ? strdup( src->psz_language ) : NULL;
272     dst->psz_description = src->psz_description ? strdup( src->psz_description ) : NULL;
273     if( src->i_extra > 0 && dst->p_extra )
274     {
275         dst->i_extra = src->i_extra;
276         dst->p_extra = malloc( src->i_extra );
277         if(dst->p_extra)
278             memcpy( dst->p_extra, src->p_extra, src->i_extra );
279         else
280             dst->i_extra = 0;
281     }
282     else
283     {
284         dst->i_extra = 0;
285         dst->p_extra = NULL;
286     }
287
288     dst->subs.psz_encoding = dst->subs.psz_encoding ? strdup( src->subs.psz_encoding ) : NULL;
289
290     if( src->video.p_palette )
291     {
292         dst->video.p_palette =
293             (video_palette_t*)malloc( sizeof( video_palette_t ) );
294         if(dst->video.p_palette)
295         {
296             memcpy( dst->video.p_palette, src->video.p_palette,
297                 sizeof( video_palette_t ) );
298         }
299     }
300
301     if( dst->i_extra_languages && src->p_extra_languages)
302     {
303         dst->i_extra_languages = src->i_extra_languages;
304         dst->p_extra_languages = (extra_languages_t*)
305             malloc(dst->i_extra_languages * sizeof(*dst->p_extra_languages ));
306         if( dst->p_extra_languages )
307         {
308             for( i = 0; i < dst->i_extra_languages; i++ ) {
309                 if( src->p_extra_languages[i].psz_language )
310                     dst->p_extra_languages[i].psz_language = strdup( src->p_extra_languages[i].psz_language );
311                 else
312                     dst->p_extra_languages[i].psz_language = NULL;
313                 if( src->p_extra_languages[i].psz_description )
314                     dst->p_extra_languages[i].psz_description = strdup( src->p_extra_languages[i].psz_description );
315                 else
316                     dst->p_extra_languages[i].psz_description = NULL;
317             }
318         }
319         else
320             dst->i_extra_languages = 0;
321     }
322     else
323         dst->i_extra_languages = 0;
324     return VLC_SUCCESS;
325 }
326
327 void es_format_Clean( es_format_t *fmt )
328 {
329     free( fmt->psz_language );
330     free( fmt->psz_description );
331
332     if( fmt->i_extra > 0 ) free( fmt->p_extra );
333
334     free( fmt->video.p_palette );
335     free( fmt->subs.psz_encoding );
336
337     if( fmt->i_extra_languages > 0 && fmt->p_extra_languages )
338     {
339         int i;
340         for( i = 0; i < fmt->i_extra_languages; i++ )
341         {
342             free( fmt->p_extra_languages[i].psz_language );
343             free( fmt->p_extra_languages[i].psz_description );
344         }
345         free( fmt->p_extra_languages );
346     }
347
348     /* es_format_Clean can be called multiple times */
349     memset( fmt, 0, sizeof(*fmt) );
350 }
351
352 bool es_format_IsSimilar( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
353 {
354     if( p_fmt1->i_cat != p_fmt2->i_cat ||
355         vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec ) !=
356         vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec ) )
357         return false;
358
359     switch( p_fmt1->i_cat )
360     {
361     case AUDIO_ES:
362     {
363         audio_format_t a1 = p_fmt1->audio;
364         audio_format_t a2 = p_fmt2->audio;
365
366         if( a1.i_format && a2.i_format && a1.i_format != a2.i_format )
367             return false;
368         if( a1.i_rate != a2.i_rate ||
369             a1.i_physical_channels != a2.i_physical_channels ||
370             a1.i_original_channels != a2.i_original_channels )
371             return false;
372         return true;
373     }
374
375     case VIDEO_ES:
376     {
377         video_format_t v1 = p_fmt1->video;
378         video_format_t v2 = p_fmt2->video;
379         if( !v1.i_chroma )
380             v1.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec );
381         if( !v2.i_chroma )
382             v2.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt2->i_codec );
383         return video_format_IsSimilar( &p_fmt1->video, &p_fmt2->video );
384     }
385
386     case SPU_ES:
387     default:
388         return true;
389     }
390 }
391