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 * libmpeg2 sync history:
25 * 2008-07-01 - CVS revision 1.13
29 #include "mpegplayer.h"
34 static size_t bufsize
;
35 static unsigned char* mallocbuf
;
37 /* libmpeg2 allocator */
38 static off_t mpeg2_mem_ptr SHAREDBSS_ATTR
;
39 static size_t mpeg2_bufsize SHAREDBSS_ATTR
;
40 static unsigned char *mpeg2_mallocbuf SHAREDBSS_ATTR
;
41 static unsigned char *mpeg2_bufallocbuf SHAREDBSS_ATTR
;
43 #if defined(DEBUG) || defined(SIMULATOR)
44 const char * mpeg_get_reason_str(int reason
)
50 case MPEG2_ALLOC_MPEG2DEC
:
51 str
= "MPEG2_ALLOC_MPEG2DEC";
53 case MPEG2_ALLOC_CHUNK
:
54 str
= "MPEG2_ALLOC_CHUNK";
57 str
= "MPEG2_ALLOC_YUV";
59 case MPEG2_ALLOC_CONVERT_ID
:
60 str
= "MPEG2_ALLOC_CONVERT_ID";
62 case MPEG2_ALLOC_CONVERTED
:
63 str
= "MPEG2_ALLOC_CONVERTED";
65 case MPEG_ALLOC_MPEG2_BUFFER
:
66 str
= "MPEG_ALLOC_MPEG2_BUFFER";
68 case MPEG_ALLOC_AUDIOBUF
:
69 str
= "MPEG_ALLOC_AUDIOBUF";
71 case MPEG_ALLOC_PCMOUT
:
72 str
= "MPEG_ALLOC_PCMOUT";
74 case MPEG_ALLOC_DISKBUF
:
75 str
= "MPEG_ALLOC_DISKBUF";
77 case MPEG_ALLOC_CODEC_MALLOC
:
78 str
= "MPEG_ALLOC_CODEC_MALLOC";
80 case MPEG_ALLOC_CODEC_CALLOC
:
81 str
= "MPEG_ALLOC_CODEC_CALLOC";
91 static void * mpeg_malloc_internal (unsigned char *mallocbuf
,
99 DEBUGF("mpeg_alloc_internal: bs:%lu s:%u reason:%s (%d)\n",
100 bufsize
, size
, mpeg_get_reason_str(reason
), reason
);
102 if ((size_t) (*mem_ptr
+ size
) > bufsize
)
104 DEBUGF("OUT OF MEMORY\n");
108 x
= &mallocbuf
[*mem_ptr
];
109 *mem_ptr
+= (size
+ 3) & ~3; /* Keep memory 32-bit aligned */
115 void *mpeg_malloc(size_t size
, mpeg2_alloc_t reason
)
117 return mpeg_malloc_internal(mallocbuf
, &mem_ptr
, bufsize
, size
,
121 void *mpeg_malloc_all(size_t *size_out
, mpeg2_alloc_t reason
)
123 /* Can steal all but MIN_MEMMARGIN */
124 if (bufsize
- mem_ptr
< MIN_MEMMARGIN
)
127 *size_out
= bufsize
- mem_ptr
- MIN_MEMMARGIN
;
128 return mpeg_malloc(*size_out
, reason
);
131 bool mpeg_alloc_init(unsigned char *buf
, size_t mallocsize
)
134 /* Cache-align buffer or 4-byte align */
136 bufsize
= mallocsize
;
137 ALIGN_BUFFER(mallocbuf
, bufsize
, CACHEALIGN_UP(4));
139 /* Separate allocator for video */
141 mpeg2_mallocbuf
= mallocbuf
;
142 mpeg2_bufallocbuf
= mallocbuf
;
143 mpeg2_bufsize
= CACHEALIGN_UP(LIBMPEG2_ALLOC_SIZE
);
145 if (mpeg_malloc_internal(mallocbuf
, &mem_ptr
,
146 bufsize
, mpeg2_bufsize
,
147 MPEG_ALLOC_MPEG2_BUFFER
) == NULL
)
152 IF_COP(rb
->cpucache_invalidate());
156 /* allocate non-dedicated buffer space which mpeg2_mem_reset will free */
157 void * mpeg2_malloc(unsigned size
, mpeg2_alloc_t reason
)
159 void *ptr
= mpeg_malloc_internal(mpeg2_mallocbuf
, &mpeg2_mem_ptr
,
160 mpeg2_bufsize
, size
, reason
);
161 /* libmpeg2 expects zero-initialized allocations */
163 rb
->memset(ptr
, 0, size
);
168 /* allocate dedicated buffer - memory behind buffer pointer becomes dedicated
169 so order is important */
170 void * mpeg2_bufalloc(unsigned size
, mpeg2_alloc_t reason
)
172 void *buf
= mpeg2_malloc(size
, reason
);
177 mpeg2_bufallocbuf
= &mpeg2_mallocbuf
[mpeg2_mem_ptr
];
181 /* return unused buffer portion and size */
182 void * mpeg2_get_buf(size_t *size
)
184 if ((size_t)mpeg2_mem_ptr
+ 32 >= mpeg2_bufsize
)
187 *size
= mpeg2_bufsize
- mpeg2_mem_ptr
;
188 return &mpeg2_mallocbuf
[mpeg2_mem_ptr
];
191 /* de-allocate all non-dedicated buffer space */
192 void mpeg2_mem_reset(void)
194 DEBUGF("mpeg2_mem_reset\n");
195 mpeg2_mem_ptr
= mpeg2_bufallocbuf
- mpeg2_mallocbuf
;
198 /* The following are expected by libmad */
199 void * codec_malloc(size_t size
)
203 ptr
= mpeg_malloc_internal(mallocbuf
, &mem_ptr
,
204 bufsize
, size
, MPEG_ALLOC_CODEC_MALLOC
);
207 rb
->memset(ptr
,0,size
);
212 void * codec_calloc(size_t nmemb
, size_t size
)
216 ptr
= mpeg_malloc_internal(mallocbuf
, &mem_ptr
,
218 MPEG_ALLOC_CODEC_CALLOC
);
221 rb
->memset(ptr
,0,size
);
226 void codec_free(void* ptr
)
228 DEBUGF("codec_free - %p\n", ptr
);
230 mem_ptr
= (void *)ptr
- (void *)mallocbuf
;