/* For free support for VSIDE, please visit www.vsdsp-forum.com */

// Starting point template for creating VSOS applications.
// This will create a <projectname>.APP file, which you can copy to 
// your VS1005 Developer Board and run it from there.

// If you rename your application to INIT.APP, then the kernel will
// load it automatically after booting.

#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include <libdecaac.h>
#include <sysmemory.h>
#include <kernel.h>

struct Codec *cod = NULL;
void (*Delete)(struct Codec *cod) = NULL;
void MyDelete(void);
void fini(void);

u_int16 preloadResult;

DLLENTRY(Create)
struct Codec *Create(void) {
  preloadResult = AllocMemXY(2048+128, 1024); 
  if (cod = CodAacCreate()) {
    // Remember original Delete() function address
    Delete = cod->Delete;
    // Grab Delete() so that we can do our own clean-up
    cod->Delete = (void *)MyDelete;
  }
  return cod; 
}


void MyDelete(void) {
  if (cod) {
    if (Delete) {
      Delete(cod);
      Delete = NULL;
    }
    cod = NULL;
  }
  UnloadAac();
}

void fini(void) {
  MyDelete();
}
