mpegplayer: Make playback engine fully seekable and frame-accurate and split into...
[Rockbox.git] / apps / plugins / mpegplayer / alloc.c
blob1ff75d4d641023a1165f9e2e2892163205414351
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>
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7 * See http://libmpeg2.sourceforge.net/ for updates.
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.
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.
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
24 #include "plugin.h"
25 #include "mpegplayer.h"
27 /* Main allocator */
28 static off_t mem_ptr;
29 static size_t bufsize;
30 static unsigned char* mallocbuf;
32 /* libmpeg2 allocator */
33 static off_t mpeg2_mem_ptr NOCACHEBSS_ATTR;
34 static size_t mpeg2_bufsize NOCACHEBSS_ATTR;
35 static unsigned char *mpeg2_mallocbuf NOCACHEBSS_ATTR;
36 static unsigned char *mpeg2_bufallocbuf NOCACHEBSS_ATTR;
38 #if defined(DEBUG) || defined(SIMULATOR)
39 const char * mpeg_get_reason_str(int reason)
41 const char *str;
43 switch (reason)
45 case MPEG2_ALLOC_MPEG2DEC:
46 str = "MPEG2_ALLOC_MPEG2DEC";
47 break;
48 case MPEG2_ALLOC_CHUNK:
49 str = "MPEG2_ALLOC_CHUNK";
50 break;
51 case MPEG2_ALLOC_YUV:
52 str = "MPEG2_ALLOC_YUV";
53 break;
54 case MPEG2_ALLOC_CONVERT_ID:
55 str = "MPEG2_ALLOC_CONVERT_ID";
56 break;
57 case MPEG2_ALLOC_CONVERTED:
58 str = "MPEG2_ALLOC_CONVERTED";
59 break;
60 case MPEG_ALLOC_MPEG2_BUFFER:
61 str = "MPEG_ALLOC_MPEG2_BUFFER";
62 break;
63 case MPEG_ALLOC_AUDIOBUF:
64 str = "MPEG_ALLOC_AUDIOBUF";
65 break;
66 case MPEG_ALLOC_PCMOUT:
67 str = "MPEG_ALLOC_PCMOUT";
68 break;
69 case MPEG_ALLOC_DISKBUF:
70 str = "MPEG_ALLOC_DISKBUF";
71 break;
72 case MPEG_ALLOC_CODEC_MALLOC:
73 str = "MPEG_ALLOC_CODEC_MALLOC";
74 break;
75 case MPEG_ALLOC_CODEC_CALLOC:
76 str = "MPEG_ALLOC_CODEC_CALLOC";
77 break;
78 default:
79 str = "Unknown";
82 return str;
84 #endif
86 static void * mpeg_malloc_internal (unsigned char *mallocbuf,
87 off_t *mem_ptr,
88 size_t bufsize,
89 unsigned size,
90 int reason)
92 void *x;
94 DEBUGF("mpeg_alloc_internal: bs:%lu s:%u reason:%s (%d)\n",
95 bufsize, size, mpeg_get_reason_str(reason), reason);
97 if ((size_t) (*mem_ptr + size) > bufsize)
99 DEBUGF("OUT OF MEMORY\n");
100 return NULL;
103 x = &mallocbuf[*mem_ptr];
104 *mem_ptr += (size + 3) & ~3; /* Keep memory 32-bit aligned */
106 return x;
107 (void)reason;
110 void *mpeg_malloc(size_t size, mpeg2_alloc_t reason)
112 return mpeg_malloc_internal(mallocbuf, &mem_ptr, bufsize, size,
113 reason);
116 void *mpeg_malloc_all(size_t *size_out, mpeg2_alloc_t reason)
118 /* Can steal all but MIN_MEMMARGIN */
119 if (bufsize - mem_ptr < MIN_MEMMARGIN)
120 return NULL;
122 *size_out = bufsize - mem_ptr - MIN_MEMMARGIN;
123 return mpeg_malloc(*size_out, reason);
126 bool mpeg_alloc_init(unsigned char *buf, size_t mallocsize)
128 mem_ptr = 0;
129 /* Cache-align buffer or 4-byte align */
130 mallocbuf = buf;
131 bufsize = align_buffer(PUN_PTR(void **, &mallocbuf),
132 mallocsize, CACHEALIGN_UP(4));
134 /* Separate allocator for video */
135 mpeg2_mem_ptr = 0;
136 mpeg2_mallocbuf = mallocbuf;
137 mpeg2_bufallocbuf = mallocbuf;
138 mpeg2_bufsize = CACHEALIGN_UP(LIBMPEG2_ALLOC_SIZE);
140 if (mpeg_malloc_internal(mallocbuf, &mem_ptr,
141 bufsize, mpeg2_bufsize,
142 MPEG_ALLOC_MPEG2_BUFFER) == NULL)
144 return false;
147 IF_COP(flush_icache());
148 return true;
151 /* gcc may want to use memcpy before rb is initialised, so here's a trivial
152 implementation */
154 void *memcpy(void *dest, const void *src, size_t n)
156 size_t i;
157 char* d=(char*)dest;
158 char* s=(char*)src;
160 for (i=0;i<n;i++)
161 d[i]=s[i];
163 return dest;
166 /* allocate non-dedicated buffer space which mpeg2_mem_reset will free */
167 void * mpeg2_malloc(unsigned size, mpeg2_alloc_t reason)
169 void *ptr = mpeg_malloc_internal(mpeg2_mallocbuf, &mpeg2_mem_ptr,
170 mpeg2_bufsize, size, reason);
171 /* libmpeg2 expects zero-initialized allocations */
172 if (ptr)
173 rb->memset(ptr, 0, size);
175 return ptr;
178 /* allocate dedicated buffer - memory behind buffer pointer becomes dedicated
179 so order is important */
180 void * mpeg2_bufalloc(unsigned size, mpeg2_alloc_t reason)
182 void *buf = mpeg2_malloc(size, reason);
184 if (buf == NULL)
185 return NULL;
187 mpeg2_bufallocbuf = &mpeg2_mallocbuf[mpeg2_mem_ptr];
188 return buf;
191 /* return unused buffer portion and size */
192 void * mpeg2_get_buf(size_t *size)
194 if ((size_t)mpeg2_mem_ptr + 32 >= mpeg2_bufsize)
195 return NULL;
197 *size = mpeg2_bufsize - mpeg2_mem_ptr;
198 return &mpeg2_mallocbuf[mpeg2_mem_ptr];
201 /* de-allocate all non-dedicated buffer space */
202 void mpeg2_mem_reset(void)
204 DEBUGF("mpeg2_mem_reset\n");
205 mpeg2_mem_ptr = mpeg2_bufallocbuf - mpeg2_mallocbuf;
208 /* The following are expected by libmad */
209 void * codec_malloc(size_t size)
211 void* ptr;
213 ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
214 bufsize, size, MPEG_ALLOC_CODEC_MALLOC);
216 if (ptr)
217 rb->memset(ptr,0,size);
219 return ptr;
222 void * codec_calloc(size_t nmemb, size_t size)
224 void* ptr;
226 ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
227 bufsize, nmemb*size,
228 MPEG_ALLOC_CODEC_CALLOC);
230 if (ptr)
231 rb->memset(ptr,0,size);
233 return ptr;
236 void codec_free(void* ptr)
238 DEBUGF("codec_free - %p\n", ptr);
239 #if 0
240 mem_ptr = (void *)ptr - (void *)mallocbuf;
241 #endif
242 (void)ptr;
245 void *memmove(void *dest, const void *src, size_t n)
247 return rb->memmove(dest,src,n);
250 void *memset(void *s, int c, size_t n)
252 return rb->memset(s,c,n);
255 void abort(void)
257 rb->lcd_putsxy(0,0,"ABORT!");
258 rb->lcd_update();
260 while (1);
261 /* Let's hope this is never called */