]> git.sesse.net Git - vlc/blob - src/misc/es_format.c
picture: do not clobber picture reference count when destroying a pool
[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_sar_num, int i_sar_den )
134 {
135     p_fmt->i_chroma         = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
136     p_fmt->i_width          =
137     p_fmt->i_visible_width  = i_width;
138     p_fmt->i_height         =
139     p_fmt->i_visible_height = i_height;
140     p_fmt->i_x_offset       =
141     p_fmt->i_y_offset       = 0;
142     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
143                  i_sar_num, i_sar_den, 0 );
144
145     switch( p_fmt->i_chroma )
146     {
147     case VLC_CODEC_YUVA:
148         p_fmt->i_bits_per_pixel = 32;
149         break;
150     case VLC_CODEC_YUV420A:
151         p_fmt->i_bits_per_pixel = 20;
152         break;
153     case VLC_CODEC_YUV422A:
154         p_fmt->i_bits_per_pixel = 24;
155         break;
156     case VLC_CODEC_I444:
157     case VLC_CODEC_J444:
158         p_fmt->i_bits_per_pixel = 24;
159         break;
160     case VLC_CODEC_I422:
161     case VLC_CODEC_YUYV:
162     case VLC_CODEC_YVYU:
163     case VLC_CODEC_UYVY:
164     case VLC_CODEC_VYUY:
165     case VLC_CODEC_J422:
166         p_fmt->i_bits_per_pixel = 16;
167         break;
168     case VLC_CODEC_I440:
169     case VLC_CODEC_J440:
170         p_fmt->i_bits_per_pixel = 16;
171         break;
172     case VLC_CODEC_I411:
173     case VLC_CODEC_YV12:
174     case VLC_CODEC_I420:
175     case VLC_CODEC_J420:
176         p_fmt->i_bits_per_pixel = 12;
177         break;
178     case VLC_CODEC_YV9:
179     case VLC_CODEC_I410:
180         p_fmt->i_bits_per_pixel = 9;
181         break;
182     case VLC_CODEC_Y211:
183         p_fmt->i_bits_per_pixel = 8;
184         break;
185     case VLC_CODEC_YUVP:
186         p_fmt->i_bits_per_pixel = 8;
187         break;
188
189     case VLC_CODEC_RGB32:
190     case VLC_CODEC_RGBA:
191         p_fmt->i_bits_per_pixel = 32;
192         break;
193     case VLC_CODEC_RGB24:
194         p_fmt->i_bits_per_pixel = 24;
195         break;
196     case VLC_CODEC_RGB15:
197     case VLC_CODEC_RGB16:
198         p_fmt->i_bits_per_pixel = 16;
199         break;
200     case VLC_CODEC_RGB8:
201         p_fmt->i_bits_per_pixel = 8;
202         break;
203
204     case VLC_CODEC_GREY:
205     case VLC_CODEC_RGBP:
206         p_fmt->i_bits_per_pixel = 8;
207         break;
208
209     case VLC_CODEC_XYZ12:
210         p_fmt->i_bits_per_pixel = 48;
211         break;
212
213     default:
214         p_fmt->i_bits_per_pixel = 0;
215         break;
216     }
217 }
218
219 void video_format_CopyCrop( video_format_t *p_dst, const video_format_t *p_src )
220 {
221     p_dst->i_x_offset       = p_src->i_x_offset;
222     p_dst->i_y_offset       = p_src->i_y_offset;
223     p_dst->i_visible_width  = p_src->i_visible_width;
224     p_dst->i_visible_height = p_src->i_visible_height;
225 }
226
227 void video_format_ScaleCropAr( video_format_t *p_dst, const video_format_t *p_src )
228 {
229     p_dst->i_x_offset       = (uint64_t)p_src->i_x_offset       * p_dst->i_width  / p_src->i_width;
230     p_dst->i_y_offset       = (uint64_t)p_src->i_y_offset       * p_dst->i_height / p_src->i_height;
231     p_dst->i_visible_width  = (uint64_t)p_src->i_visible_width  * p_dst->i_width  / p_src->i_width;
232     p_dst->i_visible_height = (uint64_t)p_src->i_visible_height * p_dst->i_height / p_src->i_height;
233
234     p_dst->i_sar_num *= p_src->i_width;
235     p_dst->i_sar_den *= p_dst->i_width;
236     vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
237                 p_dst->i_sar_num, p_dst->i_sar_den, 65536);
238
239     p_dst->i_sar_num *= p_dst->i_height;
240     p_dst->i_sar_den *= p_src->i_height;
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);
243 }
244
245 bool video_format_IsSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
246 {
247     video_format_t v1 = *p_fmt1;
248     video_format_t v2 = *p_fmt2;
249
250     if( v1.i_chroma != v2.i_chroma )
251         return false;
252
253     if( v1.i_width != v2.i_width || v1.i_height != v2.i_height ||
254         v1.i_visible_width != v2.i_visible_width ||
255         v1.i_visible_height != v2.i_visible_height ||
256         v1.i_x_offset != v2.i_x_offset || v1.i_y_offset != v2.i_y_offset )
257         return false;
258     if( v1.i_sar_num * v2.i_sar_den != v2.i_sar_num * v1.i_sar_den )
259         return false;
260
261     if( v1.i_chroma == VLC_CODEC_RGB15 ||
262         v1.i_chroma == VLC_CODEC_RGB16 ||
263         v1.i_chroma == VLC_CODEC_RGB24 ||
264         v1.i_chroma == VLC_CODEC_RGB32 )
265     {
266         video_format_FixRgb( &v1 );
267         video_format_FixRgb( &v2 );
268
269         if( v1.i_rmask != v2.i_rmask ||
270             v1.i_gmask != v2.i_gmask ||
271             v1.i_bmask != v2.i_bmask )
272             return false;
273     }
274     return true;
275 }
276 void video_format_Print( vlc_object_t *p_this,
277                          const char *psz_text, const video_format_t *fmt )
278 {
279     msg_Dbg( p_this,
280              "%s sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, sar %i:%i, msk r0x%x g0x%x b0x%x",
281              psz_text,
282              fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
283              fmt->i_visible_width, fmt->i_visible_height,
284              (char*)&fmt->i_chroma,
285              fmt->i_sar_num, fmt->i_sar_den,
286              fmt->i_rmask, fmt->i_gmask, fmt->i_bmask );
287 }
288
289 void es_format_Init( es_format_t *fmt,
290                      int i_cat, vlc_fourcc_t i_codec )
291 {
292     fmt->i_cat                  = i_cat;
293     fmt->i_codec                = i_codec;
294     fmt->i_original_fourcc      = 0;
295     fmt->i_profile              = -1;
296     fmt->i_level                = -1;
297     fmt->i_id                   = -1;
298     fmt->i_group                = 0;
299     fmt->i_priority             = 0;
300     fmt->psz_language           = NULL;
301     fmt->psz_description        = NULL;
302
303     fmt->i_extra_languages      = 0;
304     fmt->p_extra_languages      = NULL;
305
306     memset( &fmt->audio, 0, sizeof(audio_format_t) );
307     memset( &fmt->audio_replay_gain, 0, sizeof(audio_replay_gain_t) );
308     memset( &fmt->video, 0, sizeof(video_format_t) );
309     memset( &fmt->subs, 0, sizeof(subs_format_t) );
310
311     fmt->b_packetized           = true;
312     fmt->i_bitrate              = 0;
313     fmt->i_extra                = 0;
314     fmt->p_extra                = NULL;
315 }
316
317 void es_format_InitFromVideo( es_format_t *p_es, const video_format_t *p_fmt )
318 {
319     es_format_Init( p_es, VIDEO_ES, p_fmt->i_chroma );
320     video_format_Copy( &p_es->video, p_fmt );
321 }
322
323 int es_format_Copy( es_format_t *dst, const es_format_t *src )
324 {
325     int i;
326     memcpy( dst, src, sizeof( es_format_t ) );
327     dst->psz_language = src->psz_language ? strdup( src->psz_language ) : NULL;
328     dst->psz_description = src->psz_description ? strdup( src->psz_description ) : NULL;
329     if( src->i_extra > 0 && dst->p_extra )
330     {
331         dst->i_extra = src->i_extra;
332         dst->p_extra = malloc( src->i_extra );
333         if(dst->p_extra)
334             memcpy( dst->p_extra, src->p_extra, src->i_extra );
335         else
336             dst->i_extra = 0;
337     }
338     else
339     {
340         dst->i_extra = 0;
341         dst->p_extra = NULL;
342     }
343
344     dst->subs.psz_encoding = dst->subs.psz_encoding ? strdup( src->subs.psz_encoding ) : NULL;
345
346     if( src->video.p_palette )
347     {
348         dst->video.p_palette =
349             (video_palette_t*)malloc( sizeof( video_palette_t ) );
350         if(dst->video.p_palette)
351         {
352             memcpy( dst->video.p_palette, src->video.p_palette,
353                 sizeof( video_palette_t ) );
354         }
355     }
356
357     if( dst->i_extra_languages && src->p_extra_languages)
358     {
359         dst->i_extra_languages = src->i_extra_languages;
360         dst->p_extra_languages = (extra_languages_t*)
361             malloc(dst->i_extra_languages * sizeof(*dst->p_extra_languages ));
362         if( dst->p_extra_languages )
363         {
364             for( i = 0; i < dst->i_extra_languages; i++ ) {
365                 if( src->p_extra_languages[i].psz_language )
366                     dst->p_extra_languages[i].psz_language = strdup( src->p_extra_languages[i].psz_language );
367                 else
368                     dst->p_extra_languages[i].psz_language = NULL;
369                 if( src->p_extra_languages[i].psz_description )
370                     dst->p_extra_languages[i].psz_description = strdup( src->p_extra_languages[i].psz_description );
371                 else
372                     dst->p_extra_languages[i].psz_description = NULL;
373             }
374         }
375         else
376             dst->i_extra_languages = 0;
377     }
378     else
379         dst->i_extra_languages = 0;
380     return VLC_SUCCESS;
381 }
382
383 void es_format_Clean( es_format_t *fmt )
384 {
385     free( fmt->psz_language );
386     free( fmt->psz_description );
387
388     if( fmt->i_extra > 0 ) free( fmt->p_extra );
389
390     free( fmt->video.p_palette );
391     free( fmt->subs.psz_encoding );
392
393     if( fmt->i_extra_languages > 0 && fmt->p_extra_languages )
394     {
395         int i;
396         for( i = 0; i < fmt->i_extra_languages; i++ )
397         {
398             free( fmt->p_extra_languages[i].psz_language );
399             free( fmt->p_extra_languages[i].psz_description );
400         }
401         free( fmt->p_extra_languages );
402     }
403
404     /* es_format_Clean can be called multiple times */
405     memset( fmt, 0, sizeof(*fmt) );
406 }
407
408 bool es_format_IsSimilar( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
409 {
410     if( p_fmt1->i_cat != p_fmt2->i_cat ||
411         vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec ) !=
412         vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec ) )
413         return false;
414
415     switch( p_fmt1->i_cat )
416     {
417     case AUDIO_ES:
418     {
419         audio_format_t a1 = p_fmt1->audio;
420         audio_format_t a2 = p_fmt2->audio;
421
422         if( a1.i_format && a2.i_format && a1.i_format != a2.i_format )
423             return false;
424         if( a1.i_rate != a2.i_rate ||
425             a1.i_physical_channels != a2.i_physical_channels ||
426             a1.i_original_channels != a2.i_original_channels )
427             return false;
428         return true;
429     }
430
431     case VIDEO_ES:
432     {
433         video_format_t v1 = p_fmt1->video;
434         video_format_t v2 = p_fmt2->video;
435         if( !v1.i_chroma )
436             v1.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec );
437         if( !v2.i_chroma )
438             v2.i_chroma = vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec );
439         return video_format_IsSimilar( &p_fmt1->video, &p_fmt2->video );
440     }
441
442     case SPU_ES:
443     default:
444         return true;
445     }
446 }
447