Skip to content

Commit

Permalink
-- ability to discard internal cached frames
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavriloaie Eugen-Andrei committed Dec 16, 2018
1 parent 9636e7c commit 4da1f2a
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*~
.configured
*.a
*.diff
*.orig
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ default:
SRCS = common/osdep.c common/base.c common/cpu.c common/tables.c \
encoder/api.c

SRCS_X = common/mc.c common/predict.c common/pixel.c common/macroblock.c \
SRCS_X = common/counters.c common/mc.c common/predict.c common/pixel.c common/macroblock.c \
common/frame.c common/dct.c common/cabac.c \
common/common.c common/rectangle.c \
common/set.c common/quant.c common/deblock.c common/vlc.c \
Expand Down
64 changes: 63 additions & 1 deletion common/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ void x264_log_internal( int i_level, const char *psz_fmt, ... )
/****************************************************************************
* x264_malloc:
****************************************************************************/
void *x264_malloc( int i_size )
#if 1
void *x264_malloc__(const char *pFile, int lineNumber, int i_size)
{
uint8_t *align_buf = NULL;
#if HAVE_MALLOC_H
Expand Down Expand Up @@ -150,6 +151,63 @@ void x264_free( void *p )
#endif
}
}
#else
#define POINTER_SIZE NATIVE_ALIGN
#define ALIGN_TO_POINTER_SIZE(newSize) ((((newSize)+POINTER_SIZE-1)/POINTER_SIZE)*POINTER_SIZE)

struct __attribute__((__packed__)) memory_zone_t {
size_t requestedSize;
size_t allocatedSize;
uint8_t *pData;
};

union __attribute__((__packed__)) padded_memory_zone_t{
struct memory_zone_t mz;
unsigned char padding[ALIGN_TO_POINTER_SIZE(sizeof(struct memory_zone_t))];
};

static volatile size_t gTotalAllocated=0;

static __thread char zonePrint[512];
const char * printZone(const union padded_memory_zone_t *pZone) {
sprintf(zonePrint,"pZone: %p; R: %8zu; A: %8zu; pData: %p",pZone,pZone->mz.requestedSize,pZone->mz.allocatedSize,pZone->mz.pData);
return zonePrint;
}

void *x264_malloc( int i_size )
{
size_t allocatedSize=i_size+sizeof(union padded_memory_zone_t);
uint8_t *pRaw=malloc(allocatedSize);
if(pRaw==NULL){
x264_log_internal( X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
return NULL;
}
union padded_memory_zone_t *pZone=(union padded_memory_zone_t *)pRaw;
pZone->mz.allocatedSize=allocatedSize;
pZone->mz.requestedSize=i_size;
pZone->mz.pData=pRaw+sizeof(union padded_memory_zone_t);
size_t totalAllocated=__atomic_add_fetch(&gTotalAllocated,allocatedSize,__ATOMIC_SEQ_CST);
//fprintf(stderr,"+return: %p; zone: %s; totalAllocated: %zu\n",pZone->mz.pData,printZone(pZone),totalAllocated);
fprintf(stderr,"+totalAllocated: %zu\n",totalAllocated);
return pZone->mz.pData;
}

/****************************************************************************
* x264_free:
****************************************************************************/
void x264_free( void *p )
{
if( !p )
return;
uint8_t *pRaw=(uint8_t *)p;
uint8_t *pData=pRaw-sizeof(union padded_memory_zone_t);
union padded_memory_zone_t *pZone=(union padded_memory_zone_t *)pData;
size_t totalAllocated=__atomic_sub_fetch(&gTotalAllocated,pZone->mz.allocatedSize,__ATOMIC_SEQ_CST);
//fprintf(stderr,"- param: %p; zone: %s; totalAllocated: %zu\n",p,printZone(pZone),totalAllocated);
//fprintf(stderr,"-totalAllocated: %zu\n",totalAllocated);
free(pData);
}
#endif

/****************************************************************************
* x264_slurp_file:
Expand Down Expand Up @@ -431,6 +489,8 @@ static void param_default( x264_param_t *param )
param->psz_clbin_file = NULL;
param->i_avcintra_class = 0;
param->i_avcintra_flavor = X264_AVCINTRA_FLAVOR_PANASONIC;

param->b_disable_frames_cache = 0;
}

void x264_param_default( x264_param_t *param )
Expand Down Expand Up @@ -890,6 +950,8 @@ static int param_parse( x264_param_t *p, const char *name, const char *value )
#define OPT(STR) else if( !strcmp( name, STR ) )
#define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
if( 0 );
OPT("disableInternalCache")
p->b_disable_frames_cache = atobool(value);
OPT("asm")
{
p->cpu = isdigit(value[0]) ? atoi(value) :
Expand Down
3 changes: 2 additions & 1 deletion common/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ void x264_log_internal( int i_level, const char *psz_fmt, ... );

/* x264_malloc : will do or emulate a memalign
* you have to use x264_free for buffers allocated with x264_malloc */
void *x264_malloc( int );
#define x264_malloc(size) x264_malloc__(__FILE__,__LINE__,size)
void *x264_malloc__(const char *pFile, int lineNumber, int i_size);
void x264_free( void * );

/* x264_slurp_file: malloc space for the whole file and read it */
Expand Down
16 changes: 16 additions & 0 deletions common/counters.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

int volatile totalFramesCounter=0;

int volatile framesCurrentCounter=0;
int volatile framesUnusedCounter=0;
int volatile framesUnusedBlankCounter=0;
int volatile framesReferencesCounter=0;

int volatile lookAheadIbufCounter=0;
int volatile lookAheadObufCounter=0;
int volatile lookAheadNextCounter=0;


int volatile tpUninitCounter=0;
int volatile tpRunCounter=0;
int volatile tpDoneCounter=0;
62 changes: 62 additions & 0 deletions common/counters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

extern int volatile totalFramesCounter;

extern int volatile framesCurrentCounter;
extern int volatile framesUnusedCounter;
extern int volatile framesUnusedBlankCounter;
extern int volatile framesReferencesCounter;

extern int volatile lookAheadIbufCounter;
extern int volatile lookAheadObufCounter;
extern int volatile lookAheadNextCounter;


extern int volatile tpUninitCounter;
extern int volatile tpRunCounter;
extern int volatile tpDoneCounter;

#define _LIST_INDEX_TOTAL_FRAMES &totalFramesCounter

#define _LIST_INDEX_FRAMES_CURRENT &framesCurrentCounter
#define _LIST_INDEX_FRAMES_UNUSED &framesUnusedCounter
#define _LIST_INDEX_FRAMES_UNUSED_BLANK &framesUnusedBlankCounter
#define _LIST_INDEX_FRAMES_REFERENCES &framesReferencesCounter

#define _LIST_INDEX_LOOKAHEAD_IBUF &lookAheadIbufCounter
#define _LIST_INDEX_LOOKAHEAD_OBUF &lookAheadObufCounter
#define _LIST_INDEX_LOOKAHEAD_NEXT &lookAheadNextCounter

#define _LIST_INDEX_TP_UNINIT &tpUninitCounter
#define _LIST_INDEX_TP_RUN &tpRunCounter
#define _LIST_INDEX_TP_DONE &tpDoneCounter

static inline void printCounters() {
fprintf(stderr,"totalFrames: %d; framesCurrent: %d; framesUnused: %d; framesUnusedBlank: %d; framesReferences: %d; lookAheadIbuf: %d; lookAheadObuf: %d; lookAheadNext: %d; tpUninit: %d; tpRun: %d; tpDone: %d\n",
__atomic_load_n(_LIST_INDEX_TOTAL_FRAMES,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_FRAMES_CURRENT,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_FRAMES_UNUSED,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_FRAMES_UNUSED_BLANK,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_FRAMES_REFERENCES,__ATOMIC_ACQUIRE),

__atomic_load_n(_LIST_INDEX_LOOKAHEAD_IBUF,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_LOOKAHEAD_OBUF,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_LOOKAHEAD_NEXT,__ATOMIC_ACQUIRE),

__atomic_load_n(_LIST_INDEX_TP_UNINIT,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_TP_RUN,__ATOMIC_ACQUIRE),
__atomic_load_n(_LIST_INDEX_TP_DONE,__ATOMIC_ACQUIRE)
);
}

static inline void incrementFramesCount(volatile int *pCounter) {
__atomic_add_fetch(pCounter,1,__ATOMIC_RELEASE);
if(pCounter==_LIST_INDEX_TOTAL_FRAMES)
printCounters();
}

static inline void decrementFramesCount(volatile int *pCounter) {
__atomic_sub_fetch(pCounter,1,__ATOMIC_RELEASE);
if(pCounter==_LIST_INDEX_TOTAL_FRAMES)
printCounters();
}
16 changes: 12 additions & 4 deletions common/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,12 @@ void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
{
assert( frame->i_reference_count > 0 );
frame->i_reference_count--;
if( frame->i_reference_count == 0 )
x264_frame_push( h->frames.unused[frame->b_fdec], frame );
if( frame->i_reference_count == 0 ) {
if(h->param.b_disable_frames_cache)
x264_frame_delete(frame);
else
x264_frame_push( h->frames.unused[frame->b_fdec], frame );
}
}

x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
Expand Down Expand Up @@ -797,8 +801,12 @@ void x264_frame_push_blank_unused( x264_t *h, x264_frame_t *frame )
{
assert( frame->i_reference_count > 0 );
frame->i_reference_count--;
if( frame->i_reference_count == 0 )
x264_frame_push( h->frames.blank_unused, frame );
if( frame->i_reference_count == 0 ) {
if(h->param.b_disable_frames_cache)
x264_frame_delete(frame);
else
x264_frame_push( h->frames.blank_unused, frame );
}
}

x264_frame_t *x264_frame_pop_blank_unused( x264_t *h )
Expand Down
15 changes: 15 additions & 0 deletions debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -e

if [ ! -f ./.configured ]
then
./configure \
--prefix=/Users/shiretu/work/evostream/builders/cmake/external_libs/ffmpeg \
--disable-cli \
--enable-static \
--extra-cflags="-mmacosx-version-min=10.11 -funwind-tables -g"
>./.configured
fi

make -j32 install
2 changes: 2 additions & 0 deletions x264.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ typedef struct x264_param_t
int i_slice_count_max; /* Absolute cap on slices per frame; stops applying slice-max-size
* and slice-max-mbs if this is reached. */

int b_disable_frames_cache; /* disables internal frames caching and immediately deallocates the used frames */

/* Optional callback for freeing this x264_param_t when it is done being used.
* Only used when the x264_param_t sits in memory for an indefinite period of time,
* i.e. when an x264_param_t is passed to x264_t in an x264_picture_t or in zones.
Expand Down

0 comments on commit 4da1f2a

Please sign in to comment.