2006-08-07 22:11:07 +00:00
|
|
|
/*
|
|
|
|
* alloc.c
|
|
|
|
* Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
|
|
|
|
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
|
|
|
*
|
|
|
|
* This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
|
|
|
|
* See http://libmpeg2.sourceforge.net/ for updates.
|
|
|
|
*
|
|
|
|
* mpeg2dec is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpeg2dec is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2008-07-01 10:27:48 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
* libmpeg2 sync history:
|
|
|
|
* 2008-07-01 - CVS revision 1.13
|
2006-08-07 22:11:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
2007-12-29 19:46:35 +00:00
|
|
|
#include "mpegplayer.h"
|
2009-01-13 13:48:26 +00:00
|
|
|
#include <system.h>
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
/* Main allocator */
|
|
|
|
static off_t mem_ptr;
|
|
|
|
static size_t bufsize;
|
|
|
|
static unsigned char* mallocbuf;
|
|
|
|
|
|
|
|
/* libmpeg2 allocator */
|
2008-04-06 04:34:57 +00:00
|
|
|
static off_t mpeg2_mem_ptr SHAREDBSS_ATTR;
|
|
|
|
static size_t mpeg2_bufsize SHAREDBSS_ATTR;
|
|
|
|
static unsigned char *mpeg2_mallocbuf SHAREDBSS_ATTR;
|
|
|
|
static unsigned char *mpeg2_bufallocbuf SHAREDBSS_ATTR;
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
#if defined(DEBUG) || defined(SIMULATOR)
|
|
|
|
const char * mpeg_get_reason_str(int reason)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case MPEG2_ALLOC_MPEG2DEC:
|
|
|
|
str = "MPEG2_ALLOC_MPEG2DEC";
|
|
|
|
break;
|
|
|
|
case MPEG2_ALLOC_CHUNK:
|
|
|
|
str = "MPEG2_ALLOC_CHUNK";
|
|
|
|
break;
|
|
|
|
case MPEG2_ALLOC_YUV:
|
|
|
|
str = "MPEG2_ALLOC_YUV";
|
|
|
|
break;
|
|
|
|
case MPEG2_ALLOC_CONVERT_ID:
|
|
|
|
str = "MPEG2_ALLOC_CONVERT_ID";
|
|
|
|
break;
|
|
|
|
case MPEG2_ALLOC_CONVERTED:
|
|
|
|
str = "MPEG2_ALLOC_CONVERTED";
|
|
|
|
break;
|
|
|
|
case MPEG_ALLOC_MPEG2_BUFFER:
|
|
|
|
str = "MPEG_ALLOC_MPEG2_BUFFER";
|
|
|
|
break;
|
|
|
|
case MPEG_ALLOC_AUDIOBUF:
|
|
|
|
str = "MPEG_ALLOC_AUDIOBUF";
|
|
|
|
break;
|
|
|
|
case MPEG_ALLOC_PCMOUT:
|
|
|
|
str = "MPEG_ALLOC_PCMOUT";
|
|
|
|
break;
|
|
|
|
case MPEG_ALLOC_DISKBUF:
|
|
|
|
str = "MPEG_ALLOC_DISKBUF";
|
|
|
|
break;
|
|
|
|
case MPEG_ALLOC_CODEC_MALLOC:
|
|
|
|
str = "MPEG_ALLOC_CODEC_MALLOC";
|
|
|
|
break;
|
|
|
|
case MPEG_ALLOC_CODEC_CALLOC:
|
|
|
|
str = "MPEG_ALLOC_CODEC_CALLOC";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = "Unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
#endif
|
2007-09-28 11:12:45 +00:00
|
|
|
|
|
|
|
static void * mpeg_malloc_internal (unsigned char *mallocbuf,
|
|
|
|
off_t *mem_ptr,
|
|
|
|
size_t bufsize,
|
|
|
|
unsigned size,
|
|
|
|
int reason)
|
2006-08-07 22:11:07 +00:00
|
|
|
{
|
2007-09-28 11:12:45 +00:00
|
|
|
void *x;
|
2007-03-22 09:18:46 +00:00
|
|
|
|
2010-07-18 12:42:47 +00:00
|
|
|
DEBUGF("mpeg_alloc_internal: bs:%lu s:%u reason:%s (%d)\n",
|
|
|
|
(unsigned long)bufsize, size, mpeg_get_reason_str(reason), reason);
|
2007-12-29 19:46:35 +00:00
|
|
|
|
2007-10-02 08:39:55 +00:00
|
|
|
if ((size_t) (*mem_ptr + size) > bufsize)
|
2007-09-28 11:12:45 +00:00
|
|
|
{
|
|
|
|
DEBUGF("OUT OF MEMORY\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-29 19:46:35 +00:00
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
x = &mallocbuf[*mem_ptr];
|
|
|
|
*mem_ptr += (size + 3) & ~3; /* Keep memory 32-bit aligned */
|
|
|
|
|
|
|
|
return x;
|
|
|
|
(void)reason;
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
void *mpeg_malloc(size_t size, mpeg2_alloc_t reason)
|
2006-08-07 22:11:07 +00:00
|
|
|
{
|
2007-09-28 11:12:45 +00:00
|
|
|
return mpeg_malloc_internal(mallocbuf, &mem_ptr, bufsize, size,
|
|
|
|
reason);
|
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
void *mpeg_malloc_all(size_t *size_out, mpeg2_alloc_t reason)
|
|
|
|
{
|
|
|
|
/* Can steal all but MIN_MEMMARGIN */
|
|
|
|
if (bufsize - mem_ptr < MIN_MEMMARGIN)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*size_out = bufsize - mem_ptr - MIN_MEMMARGIN;
|
|
|
|
return mpeg_malloc(*size_out, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpeg_alloc_init(unsigned char *buf, size_t mallocsize)
|
2007-09-28 11:12:45 +00:00
|
|
|
{
|
|
|
|
mem_ptr = 0;
|
2007-12-29 19:46:35 +00:00
|
|
|
/* Cache-align buffer or 4-byte align */
|
|
|
|
mallocbuf = buf;
|
2009-01-13 13:48:26 +00:00
|
|
|
bufsize = mallocsize;
|
|
|
|
ALIGN_BUFFER(mallocbuf, bufsize, CACHEALIGN_UP(4));
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
/* Separate allocator for video */
|
2007-12-29 19:46:35 +00:00
|
|
|
mpeg2_mem_ptr = 0;
|
|
|
|
mpeg2_mallocbuf = mallocbuf;
|
|
|
|
mpeg2_bufallocbuf = mallocbuf;
|
|
|
|
mpeg2_bufsize = CACHEALIGN_UP(LIBMPEG2_ALLOC_SIZE);
|
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
if (mpeg_malloc_internal(mallocbuf, &mem_ptr,
|
2007-12-29 19:46:35 +00:00
|
|
|
bufsize, mpeg2_bufsize,
|
|
|
|
MPEG_ALLOC_MPEG2_BUFFER) == NULL)
|
2007-09-28 11:12:45 +00:00
|
|
|
{
|
2007-12-29 19:46:35 +00:00
|
|
|
return false;
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 07:27:24 +00:00
|
|
|
IF_COP(rb->commit_discard_dcache());
|
2007-12-29 19:46:35 +00:00
|
|
|
return true;
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
/* allocate non-dedicated buffer space which mpeg2_mem_reset will free */
|
2007-09-28 11:12:45 +00:00
|
|
|
void * mpeg2_malloc(unsigned size, mpeg2_alloc_t reason)
|
|
|
|
{
|
2007-12-29 19:46:35 +00:00
|
|
|
void *ptr = mpeg_malloc_internal(mpeg2_mallocbuf, &mpeg2_mem_ptr,
|
|
|
|
mpeg2_bufsize, size, reason);
|
|
|
|
/* libmpeg2 expects zero-initialized allocations */
|
|
|
|
if (ptr)
|
|
|
|
rb->memset(ptr, 0, size);
|
|
|
|
|
|
|
|
return ptr;
|
2007-09-28 11:12:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
/* allocate dedicated buffer - memory behind buffer pointer becomes dedicated
|
|
|
|
so order is important */
|
|
|
|
void * mpeg2_bufalloc(unsigned size, mpeg2_alloc_t reason)
|
2007-09-28 11:12:45 +00:00
|
|
|
{
|
2007-12-29 19:46:35 +00:00
|
|
|
void *buf = mpeg2_malloc(size, reason);
|
|
|
|
|
|
|
|
if (buf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mpeg2_bufallocbuf = &mpeg2_mallocbuf[mpeg2_mem_ptr];
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return unused buffer portion and size */
|
|
|
|
void * mpeg2_get_buf(size_t *size)
|
|
|
|
{
|
|
|
|
if ((size_t)mpeg2_mem_ptr + 32 >= mpeg2_bufsize)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*size = mpeg2_bufsize - mpeg2_mem_ptr;
|
|
|
|
return &mpeg2_mallocbuf[mpeg2_mem_ptr];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* de-allocate all non-dedicated buffer space */
|
|
|
|
void mpeg2_mem_reset(void)
|
|
|
|
{
|
|
|
|
DEBUGF("mpeg2_mem_reset\n");
|
|
|
|
mpeg2_mem_ptr = mpeg2_bufallocbuf - mpeg2_mallocbuf;
|
2007-09-28 11:12:45 +00:00
|
|
|
}
|
2007-01-01 12:59:32 +00:00
|
|
|
|
|
|
|
/* The following are expected by libmad */
|
2007-09-28 11:12:45 +00:00
|
|
|
void * codec_malloc(size_t size)
|
2007-01-01 12:59:32 +00:00
|
|
|
{
|
2007-12-29 19:46:35 +00:00
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
|
|
|
|
bufsize, size, MPEG_ALLOC_CODEC_MALLOC);
|
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
rb->memset(ptr,0,size);
|
|
|
|
|
|
|
|
return ptr;
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
void * codec_calloc(size_t nmemb, size_t size)
|
2007-01-01 12:59:32 +00:00
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
|
2007-12-29 19:46:35 +00:00
|
|
|
bufsize, nmemb*size,
|
|
|
|
MPEG_ALLOC_CODEC_CALLOC);
|
2007-01-01 12:59:32 +00:00
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
rb->memset(ptr,0,size);
|
2007-12-29 19:46:35 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2007-09-28 11:12:45 +00:00
|
|
|
void codec_free(void* ptr)
|
|
|
|
{
|
2007-12-29 19:46:35 +00:00
|
|
|
DEBUGF("codec_free - %p\n", ptr);
|
|
|
|
#if 0
|
2007-10-09 20:42:20 +00:00
|
|
|
mem_ptr = (void *)ptr - (void *)mallocbuf;
|
2007-12-29 19:46:35 +00:00
|
|
|
#endif
|
|
|
|
(void)ptr;
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|