Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst / ffmpegcolorspace / utils.c
1 /*
2  * utils for libavcodec
3  * Copyright (c) 2001 Fabrice Bellard.
4  * Copyright (c) 2003 Michel Bardiaux for the av_log API
5  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 /**
23  * @file utils.c
24  * utils.
25  */
26
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <limits.h>
33
34 void *
35 av_mallocz (unsigned int size)
36 {
37   void *ptr;
38
39   ptr = av_malloc (size);
40   if (!ptr)
41     return NULL;
42   memset (ptr, 0, size);
43   return ptr;
44 }
45
46 char *
47 av_strdup (const char *s)
48 {
49   char *ptr;
50   int len;
51
52   len = strlen (s) + 1;
53   ptr = av_malloc (len);
54   if (!ptr)
55     return NULL;
56   memcpy (ptr, s, len);
57   return ptr;
58 }
59
60 /**
61  * realloc which does nothing if the block is large enough
62  */
63 void *
64 av_fast_realloc (void *ptr, unsigned int *size, unsigned int min_size)
65 {
66   if (min_size < *size)
67     return ptr;
68
69   *size = 17 * min_size / 16 + 32;
70
71   return av_realloc (ptr, *size);
72 }
73
74
75 static unsigned int last_static = 0;
76 static unsigned int allocated_static = 0;
77 static void **array_static = NULL;
78
79 /**
80  * allocation of static arrays - do not use for normal allocation.
81  */
82 void *
83 av_mallocz_static (unsigned int size)
84 {
85   void *ptr = av_mallocz (size);
86
87   if (ptr) {
88     array_static =
89         av_fast_realloc (array_static, &allocated_static,
90         sizeof (void *) * (last_static + 1));
91     array_static[last_static++] = ptr;
92   }
93
94   return ptr;
95 }
96
97 /**
98  * free all static arrays and reset pointers to 0.
99  */
100 void
101 av_free_static (void)
102 {
103   while (last_static) {
104     av_freep (&array_static[--last_static]);
105   }
106   av_freep (&array_static);
107 }
108
109 /**
110  * Frees memory and sets the pointer to NULL.
111  * @param arg pointer to the pointer which should be freed
112  */
113 void
114 av_freep (void *arg)
115 {
116   void **ptr = (void **) arg;
117
118   av_free (*ptr);
119   *ptr = NULL;
120 }
121
122 void
123 avcodec_get_context_defaults (AVCodecContext * s)
124 {
125   memset (s, 0, sizeof (AVCodecContext));
126
127   s->frame_rate_base = 1;
128   s->frame_rate = 25;
129 }
130
131 /**
132  * allocates a AVCodecContext and set it to defaults.
133  * this can be deallocated by simply calling free() 
134  */
135 AVCodecContext *
136 avcodec_alloc_context (void)
137 {
138   AVCodecContext *avctx = av_malloc (sizeof (AVCodecContext));
139
140   if (avctx == NULL)
141     return NULL;
142
143   avcodec_get_context_defaults (avctx);
144
145   return avctx;
146 }
147
148 /* must be called before any other functions */
149 void
150 avcodec_init (void)
151 {
152   static int inited = 0;
153
154   if (inited != 0)
155     return;
156   inited = 1;
157
158   dsputil_static_init ();
159 }