Hi,

"hoelschi" <[email protected]> wrote:

try to use "av_malloc()" to allocate memory for picture buffer instead of
"malloc()", this makes an extra alignment on buffer sizes.

Hehe... I'm not so stupid :) First I tried that. I've changed all malloc's to av_malloc's and free's to av_freep's in the sample code. It had no effect. Any GetMem() in my main Delphi program caused the AV in DLL. Then I 've digged into the code and have found this point in mem.c:

/* You can redefine av_malloc and av_free in your project to use your
  memory allocator. You do not need to suppress this file because the
  linker will do it automatically. */

I've tried to do it as much as I can with my C skills :) The linker gave an error like "duplicated declaration...". Have no idea of how to do it correctly. Then I've rewritten mem.c removing those allocation / releasing procedures and inserting my own, doing all memory management in the main program. Something like this:

mem.h:

typedef void __stdcall (*MemProcPtr)(unsigned int size, void **buffer);
typedef void __stdcall (*FreeMemProcPtr)(void *buffer);

extern MemProcPtr enclib_getmem_proc;
extern MemProcPtr enclib_reallocmem_proc;
extern FreeMemProcPtr enclib_freemem_proc;

mem.c:

MemProcPtr enclib_getmem_proc = NULL;
MemProcPtr enclib_reallocmem_proc = NULL;
FreeMemProcPtr enclib_freemem_proc = NULL;

void *av_malloc(unsigned int size)
{
   void *ptr = NULL;
   enclib_getmem_proc(size, &ptr);
   return ptr;
}

And of course my DLL has an exported procedure, which assigns some Delphi procedures for all three memory handlers.

After such modification the AV becomes permanent even without any extra GetMem's in the main code unless I've commented ASM procedures for MMX. It's obvious, as all memory is not aligned now.

Now my conclusion, based on this experience is the next. The code, written for MMX optimization works correctly only if ALL memory in the process (i.e. in both main program and DLL) is aligned. Have no idea why. Probably it's MINGW runtime memory manager's specifics. Therefore we either must use this ASM code in EXE (not in DLL) or align memory, allocated in the main program (what I never tried to do). For example, we could use VirtualAlloc() for that purpose. But this is not my case, where I'm using a lot of code, using usual GetMem's. Of course, I could use some alternative memory manager, than Borland suggests. This is a good idea...

It's useless (in my opinion) to ask anybody from FFMPEG developers to "debug" the ASM code, because this optimisation is based just on the memory alignment. It will not work with unaligned memory pool.

Therefore we have only one conclusion - we must disable MMX/SSE support when we build DLL and we can't provide a memory manager for our process, which would align all allocated memory. Am I correct?

Best regards,
Alexander
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to