[Redbutton-devel] SF.net SVN: redbutton: [229] redbutton-browser/trunk
Brought to you by:
skilvington
|
From: <ski...@us...> - 2007-02-25 10:21:54
|
Revision: 229
http://svn.sourceforge.net/redbutton/?rev=229&view=rev
Author: skilvington
Date: 2007-02-25 02:21:50 -0800 (Sun, 25 Feb 2007)
Log Message:
-----------
don't keep malloc/free'ing memory for a/v frames
Modified Paths:
--------------
redbutton-browser/trunk/MHEGStreamPlayer.c
redbutton-browser/trunk/MHEGStreamPlayer.h
redbutton-browser/trunk/TODO
redbutton-browser/trunk/utils.c
redbutton-browser/trunk/utils.h
Modified: redbutton-browser/trunk/MHEGStreamPlayer.c
===================================================================
--- redbutton-browser/trunk/MHEGStreamPlayer.c 2007-02-19 17:47:32 UTC (rev 228)
+++ redbutton-browser/trunk/MHEGStreamPlayer.c 2007-02-25 10:21:50 UTC (rev 229)
@@ -23,12 +23,32 @@
static void thread_usleep(unsigned long);
static enum CodecID find_av_codec_id(int);
+/* global pool of spare VideoFrame's */
+LIST_OF(VideoFrame) *free_vframes = NULL;
+pthread_mutex_t free_vframes_lock = PTHREAD_MUTEX_INITIALIZER;
+
LIST_TYPE(VideoFrame) *
new_VideoFrameListItem(double pts, enum PixelFormat pix_fmt, unsigned int width, unsigned int height, AVFrame *frame)
{
- LIST_TYPE(VideoFrame) *vf = safe_malloc(sizeof(LIST_TYPE(VideoFrame)));
+ LIST_TYPE(VideoFrame) *vf;
int frame_size;
+ /* do we have a spare frame we can use */
+ pthread_mutex_lock(&free_vframes_lock);
+ if(free_vframes != NULL)
+ {
+ vf = free_vframes;
+ LIST_REMOVE(&free_vframes, vf);
+ }
+ else
+ {
+ vf = safe_malloc(sizeof(LIST_TYPE(VideoFrame)));
+ vf->item.frame_data = NULL;
+ vf->item.nalloced = 0;
+ }
+ pthread_mutex_unlock(&free_vframes_lock);
+
+ /* frame info */
vf->item.pts = pts;
vf->item.pix_fmt = pix_fmt;
vf->item.width = width;
@@ -40,7 +60,7 @@
*/
if((frame_size = avpicture_get_size(pix_fmt, width, height)) < 0)
fatal("Invalid frame_size");
- vf->item.frame_data = safe_malloc(frame_size);
+ vf->item.frame_data = safe_fast_realloc(vf->item.frame_data, &vf->item.nalloced, frame_size);
avpicture_fill(&vf->item.frame, vf->item.frame_data, pix_fmt, width, height);
img_copy(&vf->item.frame, (AVPicture*) frame, pix_fmt, width, height);
@@ -50,18 +70,36 @@
void
free_VideoFrameListItem(LIST_TYPE(VideoFrame) *vf)
{
- safe_free(vf->item.frame_data);
+ /* add it to the free list */
+ pthread_mutex_lock(&free_vframes_lock);
+ LIST_APPEND(&free_vframes, vf);
+ pthread_mutex_unlock(&free_vframes_lock);
- safe_free(vf);
-
return;
}
+/* global pool of spare AudioFrame's */
+LIST_OF(AudioFrame) *free_aframes = NULL;
+pthread_mutex_t free_aframes_lock = PTHREAD_MUTEX_INITIALIZER;
+
LIST_TYPE(AudioFrame) *
new_AudioFrameListItem(void)
{
- LIST_TYPE(AudioFrame) *af = safe_malloc(sizeof(LIST_TYPE(AudioFrame)));
+ LIST_TYPE(AudioFrame) *af;
+ /* do we have a spare frame we can use */
+ pthread_mutex_lock(&free_aframes_lock);
+ if(free_aframes != NULL)
+ {
+ af = free_aframes;
+ LIST_REMOVE(&free_aframes, af);
+ }
+ else
+ {
+ af = safe_malloc(sizeof(LIST_TYPE(AudioFrame)));
+ }
+ pthread_mutex_unlock(&free_aframes_lock);
+
af->item.pts = AV_NOPTS_VALUE;
af->item.size = 0;
@@ -72,7 +110,10 @@
void
free_AudioFrameListItem(LIST_TYPE(AudioFrame) *af)
{
- safe_free(af);
+ /* add it to the free list */
+ pthread_mutex_lock(&free_aframes_lock);
+ LIST_APPEND(&free_aframes, af);
+ pthread_mutex_unlock(&free_aframes_lock);
return;
}
Modified: redbutton-browser/trunk/MHEGStreamPlayer.h
===================================================================
--- redbutton-browser/trunk/MHEGStreamPlayer.h 2007-02-19 17:47:32 UTC (rev 228)
+++ redbutton-browser/trunk/MHEGStreamPlayer.h 2007-02-25 10:21:50 UTC (rev 229)
@@ -28,6 +28,7 @@
unsigned int height;
AVPicture frame;
unsigned char *frame_data;
+ size_t nalloced; /* number of bytes malloc'ed to frame_data */
} VideoFrame;
DEFINE_LIST_OF(VideoFrame);
Modified: redbutton-browser/trunk/TODO
===================================================================
--- redbutton-browser/trunk/TODO 2007-02-19 17:47:32 UTC (rev 228)
+++ redbutton-browser/trunk/TODO 2007-02-25 10:21:50 UTC (rev 229)
@@ -1,9 +1,6 @@
VideoDecodeOffset
-don't keep malloc/free'ing memory for a/v frames
-
-
if video size != VideoClass size, centre and draw a smaller transparent hole
Modified: redbutton-browser/trunk/utils.c
===================================================================
--- redbutton-browser/trunk/utils.c 2007-02-19 17:47:32 UTC (rev 228)
+++ redbutton-browser/trunk/utils.c 2007-02-25 10:21:50 UTC (rev 229)
@@ -188,6 +188,23 @@
}
/*
+ * only calls safe_realloc if nbytes > *oldsize
+ * updates *oldsize if it calls safe_realloc
+ */
+
+void *
+safe_fast_realloc(void *oldbuf, size_t *oldsize, size_t nbytes)
+{
+ if(nbytes > *oldsize)
+ {
+ oldbuf = safe_realloc(oldbuf, nbytes);
+ *oldsize = nbytes;
+ }
+
+ return oldbuf;
+}
+
+/*
* safe_free(NULL) is okay
*/
Modified: redbutton-browser/trunk/utils.h
===================================================================
--- redbutton-browser/trunk/utils.h 2007-02-19 17:47:32 UTC (rev 228)
+++ redbutton-browser/trunk/utils.h 2007-02-25 10:21:50 UTC (rev 229)
@@ -43,6 +43,7 @@
void *safe_malloc(size_t);
void *safe_mallocz(size_t);
void *safe_realloc(void *, size_t);
+void *safe_fast_realloc(void *, size_t *, size_t);
void safe_free(void *);
char *safe_strdup(const char *);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|