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
25 #include "mpegplayer.h"
29 static size_t bufsize
;
30 static unsigned char* mallocbuf
;
32 /* libmpeg2 allocator */
33 static off_t mpeg2_mem_ptr SHAREDBSS_ATTR
;
34 static size_t mpeg2_bufsize SHAREDBSS_ATTR
;
35 static unsigned char *mpeg2_mallocbuf SHAREDBSS_ATTR
;
36 static unsigned char *mpeg2_bufallocbuf SHAREDBSS_ATTR
;
38 #if defined(DEBUG) || defined(SIMULATOR)
39 const char * mpeg_get_reason_str(int reason
)
45 case MPEG2_ALLOC_MPEG2DEC
:
46 str
= "MPEG2_ALLOC_MPEG2DEC";
48 case MPEG2_ALLOC_CHUNK
:
49 str
= "MPEG2_ALLOC_CHUNK";
52 str
= "MPEG2_ALLOC_YUV";
54 case MPEG2_ALLOC_CONVERT_ID
:
55 str
= "MPEG2_ALLOC_CONVERT_ID";
57 case MPEG2_ALLOC_CONVERTED
:
58 str
= "MPEG2_ALLOC_CONVERTED";
60 case MPEG_ALLOC_MPEG2_BUFFER
:
61 str
= "MPEG_ALLOC_MPEG2_BUFFER";
63 case MPEG_ALLOC_AUDIOBUF
:
64 str
= "MPEG_ALLOC_AUDIOBUF";
66 case MPEG_ALLOC_PCMOUT
:
67 str
= "MPEG_ALLOC_PCMOUT";
69 case MPEG_ALLOC_DISKBUF
:
70 str
= "MPEG_ALLOC_DISKBUF";
72 case MPEG_ALLOC_CODEC_MALLOC
:
73 str
= "MPEG_ALLOC_CODEC_MALLOC";
75 case MPEG_ALLOC_CODEC_CALLOC
:
76 str
= "MPEG_ALLOC_CODEC_CALLOC";
86 static void * mpeg_malloc_internal (unsigned char *mallocbuf
,
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");
103 x
= &mallocbuf
[*mem_ptr
];
104 *mem_ptr
+= (size
+ 3) & ~3; /* Keep memory 32-bit aligned */
110 void *mpeg_malloc(size_t size
, mpeg2_alloc_t reason
)
112 return mpeg_malloc_internal(mallocbuf
, &mem_ptr
, bufsize
, size
,
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
)
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
)
129 /* Cache-align buffer or 4-byte align */
131 bufsize
= align_buffer(PUN_PTR(void **, &mallocbuf
),
132 mallocsize
, CACHEALIGN_UP(4));
134 /* Separate allocator for video */
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
)
147 IF_COP(invalidate_icache());
151 /* gcc may want to use memcpy before rb is initialised, so here's a trivial
154 void *memcpy(void *dest
, const void *src
, size_t n
)
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 */
173 rb
->memset(ptr
, 0, size
);
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
);
187 mpeg2_bufallocbuf
= &mpeg2_mallocbuf
[mpeg2_mem_ptr
];
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
)
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
)
213 ptr
= mpeg_malloc_internal(mallocbuf
, &mem_ptr
,
214 bufsize
, size
, MPEG_ALLOC_CODEC_MALLOC
);
217 rb
->memset(ptr
,0,size
);
222 void * codec_calloc(size_t nmemb
, size_t size
)
226 ptr
= mpeg_malloc_internal(mallocbuf
, &mem_ptr
,
228 MPEG_ALLOC_CODEC_CALLOC
);
231 rb
->memset(ptr
,0,size
);
236 void codec_free(void* ptr
)
238 DEBUGF("codec_free - %p\n", ptr
);
240 mem_ptr
= (void *)ptr
- (void *)mallocbuf
;
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
);
257 rb
->lcd_putsxy(0,0,"ABORT!");
261 /* Let's hope this is never called */