summaryrefslogtreecommitdiff
path: root/apps/plugins/mpegplayer/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/mpegplayer/alloc.c')
-rw-r--r--apps/plugins/mpegplayer/alloc.c233
1 files changed, 233 insertions, 0 deletions
diff --git a/apps/plugins/mpegplayer/alloc.c b/apps/plugins/mpegplayer/alloc.c
new file mode 100644
index 0000000000..cbf930a7eb
--- /dev/null
+++ b/apps/plugins/mpegplayer/alloc.c
@@ -0,0 +1,233 @@
1/*
2 * alloc.c
3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5 *
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7 * See http://libmpeg2.sourceforge.net/ for updates.
8 *
9 * mpeg2dec 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 * mpeg2dec 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * $Id$
24 * libmpeg2 sync history:
25 * 2008-07-01 - CVS revision 1.13
26 */
27
28#include "plugin.h"
29#include "mpegplayer.h"
30#include <system.h>
31
32/* Main allocator */
33static off_t mem_ptr;
34static size_t bufsize;
35static unsigned char* mallocbuf;
36
37/* libmpeg2 allocator */
38static off_t mpeg2_mem_ptr SHAREDBSS_ATTR;
39static size_t mpeg2_bufsize SHAREDBSS_ATTR;
40static unsigned char *mpeg2_mallocbuf SHAREDBSS_ATTR;
41static unsigned char *mpeg2_bufallocbuf SHAREDBSS_ATTR;
42
43#if defined(DEBUG) || defined(SIMULATOR)
44const char * mpeg_get_reason_str(int reason)
45{
46 const char *str;
47
48 switch (reason)
49 {
50 case MPEG2_ALLOC_MPEG2DEC:
51 str = "MPEG2_ALLOC_MPEG2DEC";
52 break;
53 case MPEG2_ALLOC_CHUNK:
54 str = "MPEG2_ALLOC_CHUNK";
55 break;
56 case MPEG2_ALLOC_YUV:
57 str = "MPEG2_ALLOC_YUV";
58 break;
59 case MPEG2_ALLOC_CONVERT_ID:
60 str = "MPEG2_ALLOC_CONVERT_ID";
61 break;
62 case MPEG2_ALLOC_CONVERTED:
63 str = "MPEG2_ALLOC_CONVERTED";
64 break;
65 case MPEG_ALLOC_MPEG2_BUFFER:
66 str = "MPEG_ALLOC_MPEG2_BUFFER";
67 break;
68 case MPEG_ALLOC_AUDIOBUF:
69 str = "MPEG_ALLOC_AUDIOBUF";
70 break;
71 case MPEG_ALLOC_PCMOUT:
72 str = "MPEG_ALLOC_PCMOUT";
73 break;
74 case MPEG_ALLOC_DISKBUF:
75 str = "MPEG_ALLOC_DISKBUF";
76 break;
77 case MPEG_ALLOC_CODEC_MALLOC:
78 str = "MPEG_ALLOC_CODEC_MALLOC";
79 break;
80 case MPEG_ALLOC_CODEC_CALLOC:
81 str = "MPEG_ALLOC_CODEC_CALLOC";
82 break;
83 default:
84 str = "Unknown";
85 }
86
87 return str;
88}
89#endif
90
91static void * mpeg_malloc_internal (unsigned char *mallocbuf,
92 off_t *mem_ptr,
93 size_t bufsize,
94 unsigned size,
95 int reason)
96{
97 void *x;
98
99 DEBUGF("mpeg_alloc_internal: bs:%lu s:%u reason:%s (%d)\n",
100 (unsigned long)bufsize, size, mpeg_get_reason_str(reason), reason);
101
102 if ((size_t) (*mem_ptr + size) > bufsize)
103 {
104 DEBUGF("OUT OF MEMORY\n");
105 return NULL;
106 }
107
108 x = &mallocbuf[*mem_ptr];
109 *mem_ptr += (size + 3) & ~3; /* Keep memory 32-bit aligned */
110
111 return x;
112 (void)reason;
113}
114
115void *mpeg_malloc(size_t size, mpeg2_alloc_t reason)
116{
117 return mpeg_malloc_internal(mallocbuf, &mem_ptr, bufsize, size,
118 reason);
119}
120
121void *mpeg_malloc_all(size_t *size_out, mpeg2_alloc_t reason)
122{
123 /* Can steal all but MIN_MEMMARGIN */
124 if (bufsize - mem_ptr < MIN_MEMMARGIN)
125 return NULL;
126
127 *size_out = bufsize - mem_ptr - MIN_MEMMARGIN;
128 return mpeg_malloc(*size_out, reason);
129}
130
131bool mpeg_alloc_init(unsigned char *buf, size_t mallocsize)
132{
133 mem_ptr = 0;
134 /* Cache-align buffer or 4-byte align */
135 mallocbuf = buf;
136 bufsize = mallocsize;
137 ALIGN_BUFFER(mallocbuf, bufsize, CACHEALIGN_UP(4));
138
139 /* Separate allocator for video */
140 mpeg2_mem_ptr = 0;
141 mpeg2_mallocbuf = mallocbuf;
142 mpeg2_bufallocbuf = mallocbuf;
143 mpeg2_bufsize = CACHEALIGN_UP(LIBMPEG2_ALLOC_SIZE);
144
145 if (mpeg_malloc_internal(mallocbuf, &mem_ptr,
146 bufsize, mpeg2_bufsize,
147 MPEG_ALLOC_MPEG2_BUFFER) == NULL)
148 {
149 return false;
150 }
151
152 IF_COP(rb->commit_discard_dcache());
153 return true;
154}
155
156/* allocate non-dedicated buffer space which mpeg2_mem_reset will free */
157void * mpeg2_malloc(unsigned size, mpeg2_alloc_t reason)
158{
159 void *ptr = mpeg_malloc_internal(mpeg2_mallocbuf, &mpeg2_mem_ptr,
160 mpeg2_bufsize, size, reason);
161 /* libmpeg2 expects zero-initialized allocations */
162 if (ptr)
163 rb->memset(ptr, 0, size);
164
165 return ptr;
166}
167
168/* allocate dedicated buffer - memory behind buffer pointer becomes dedicated
169 so order is important */
170void * mpeg2_bufalloc(unsigned size, mpeg2_alloc_t reason)
171{
172 void *buf = mpeg2_malloc(size, reason);
173
174 if (buf == NULL)
175 return NULL;
176
177 mpeg2_bufallocbuf = &mpeg2_mallocbuf[mpeg2_mem_ptr];
178 return buf;
179}
180
181/* return unused buffer portion and size */
182void * mpeg2_get_buf(size_t *size)
183{
184 if ((size_t)mpeg2_mem_ptr + 32 >= mpeg2_bufsize)
185 return NULL;
186
187 *size = mpeg2_bufsize - mpeg2_mem_ptr;
188 return &mpeg2_mallocbuf[mpeg2_mem_ptr];
189}
190
191/* de-allocate all non-dedicated buffer space */
192void mpeg2_mem_reset(void)
193{
194 DEBUGF("mpeg2_mem_reset\n");
195 mpeg2_mem_ptr = mpeg2_bufallocbuf - mpeg2_mallocbuf;
196}
197
198/* The following are expected by libmad */
199void * codec_malloc(size_t size)
200{
201 void* ptr;
202
203 ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
204 bufsize, size, MPEG_ALLOC_CODEC_MALLOC);
205
206 if (ptr)
207 rb->memset(ptr,0,size);
208
209 return ptr;
210}
211
212void * codec_calloc(size_t nmemb, size_t size)
213{
214 void* ptr;
215
216 ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
217 bufsize, nmemb*size,
218 MPEG_ALLOC_CODEC_CALLOC);
219
220 if (ptr)
221 rb->memset(ptr,0,size);
222
223 return ptr;
224}
225
226void codec_free(void* ptr)
227{
228 DEBUGF("codec_free - %p\n", ptr);
229#if 0
230 mem_ptr = (void *)ptr - (void *)mallocbuf;
231#endif
232 (void)ptr;
233}