forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sceJpeg decoding problem...

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
ardatan



Joined: 12 Jan 2008
Posts: 44

PostPosted: Sun Apr 26, 2009 6:12 am    Post subject: sceJpeg decoding problem... Reply with quote

This is the small part of my app. It doesn't decode JPEG to RGB... I think there is a problem on reading the image...
It loads avcodec.prx and inits JPEG library but doesn't decode...
Could you please help me?
Code:


int StartJpegPsp()
{
  printf("Loading 'flash0:/kd/avcodec.prx'...");
  if (LoadStartModule("flash0:/kd/avcodec.prx") == 0) {
    printf("ok\n");
  }
  else {
    printf("failed\n");
    return -1;
  }

  printf("init Jpeg decompression...");
  if (sceJpegInitMJpeg() == 0) {
    if (sceJpegCreateMJpeg(480, 272) == 0) {
      fprintf(stderr, "ok\n");
      return 0;
    }
  }

  printf("failed\n");
  return -1;
}

void free_img(my_bmp_type *img)
{
  if (img)
  {
    free(img->data);
   free(img);
  }
}

#include <pspiofilemgr.h> // Include for Sony IO Functions
#include <stdio.h> // Include for Seek Constants
#include <string.h> // Include for NULL Macro

SceOff fileSize(SceUID file)
{
  // File Length
  SceOff length = 0;
 
  // Sanity Check for Valid File Handler
  if(file >= 0)
  {
    // Save Position in File
    SceOff pos = sceIoLseek(file, 0, SEEK_CUR);
   
    // Move to EOF & Grab Location (Size)
    length = sceIoLseek(file, 0, SEEK_END);
   
    // Restore Position in File
    sceIoLseek(file, pos, SEEK_SET);
  }
 
  // Return File Length
  return length;
}

typedef struct
{
  short w;
  short h;
  short frame;
  short current_frame;
  u32 *data;
} my_bmp_type;

my_bmp_type* read_jpeg_file(const char * from)
{
  my_bmp_type *img = (my_bmp_type *)malloc((480 * 65536) + 272);

  // Sanity Check to avoid NULL Pointers
  if(from)
  {
    // Open Input File
    SceUID infile = sceIoOpen(from, PSP_O_RDONLY, 0777);
   
    // Opened Input File
    if(infile >= 0)
    {
      // Get Filesize
      SceOff insize = fileSize(infile);
     img->data = (u32 *)malloc(480 * 272 * 4);
       // Transfer Buffer
        unsigned char buffer[480 * 272 * 4];
       
        // Copy Buffer Blocks
        int copied = 0;
        while(copied < insize)
        {
          // Read Block from Input File
          int read = sceIoRead(infile, buffer, 480 * 272 * 4);
         
          // Written Bytes
         sceJpegDecodeMJpeg(buffer, 480 * 272 * 4, img->data, 0);   
         if(read < 1)
         break;
        }
     
    }
    sceIoClose(infile);
  }

  // Return Result
  return img;
}

_________________
I'm sorry for my bad English.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun Apr 26, 2009 10:57 am    Post subject: Reply with quote

Probably allocating too much memory. malloc((480 * 65536) + 272) allocates over 30 MB alone. Unless you have a Slim with large memory activated, you won't get more than maybe 23 MB, depending on all the other allocations you've done. Remember, the PSP has next to no memory. You can't just toss memory at a problem like on a PC.
Back to top
View user's profile Send private message AIM Address
ardatan



Joined: 12 Jan 2008
Posts: 44

PostPosted: Mon Apr 27, 2009 2:17 am    Post subject: Reply with quote

I can't copy the image into the buffer... How can I do it?
MS light blinks very long... and PSP powers off...
_________________
I'm sorry for my bad English.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Mon Apr 27, 2009 4:49 am    Post subject: Reply with quote

If your images are too big to fit in memory, you either need to scale them before putting them on the PSP, or you need to load them a piece at a time (say, one line at a time) and scale them on the fly.
Back to top
View user's profile Send private message AIM Address
ardatan



Joined: 12 Jan 2008
Posts: 44

PostPosted: Tue Apr 28, 2009 2:51 am    Post subject: Reply with quote

No, it isn't very big...
It is only 480x272....
_________________
I'm sorry for my bad English.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Tue Apr 28, 2009 6:05 am    Post subject: Reply with quote

Have you adjusted the size of your buffers to be smaller yet? I did point out the one that was too big. Post an updated listing of the code.
Back to top
View user's profile Send private message AIM Address
ardatan



Joined: 12 Jan 2008
Posts: 44

PostPosted: Wed Apr 29, 2009 3:01 am    Post subject: Reply with quote

Code:
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pspdisplay.h>
#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <psputility.h>
#include <netinet/in.h>
#include <kubridge.h>
#include <sys/select.h>
#include <pspctrl.h>
#include <errno.h>
#include <math.h>
#include <psputility.h>
#include <pspgu.h>
#include <pspgum.h>
#include <pspjpeg.h>

PSP_MODULE_INFO("JpegSample", PSP_MODULE_USER, 1, 1);
PSP_HEAP_SIZE_MAX();

#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define PIXEL_SIZE (4)
#define FRAME_SIZE (BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE)
#define ZBUF_SIZE (BUF_WIDTH SCR_HEIGHT * 2)
#define printf pspDebugScreenPrintf
#define PTR2HND(PTR)   ((PTR) - 1)
#define HND2PTR(HND)   (HND + 1)

static unsigned int __attribute__((aligned(16))) list[262144];

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{

   sceKernelExitGame();

   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();

   return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}


int FileRead_open(const char* FileName)
{
   return PTR2HND(sceIoOpen(FileName,PSP_O_RDWR,0777));
//   return PTR2HND(fopen(FileName,"rb"));
}
int FileRead_close(int FileHandle)
{
   if(FileHandle == -1)return -1;
   sceIoClose(HND2PTR(FileHandle));
//   fclose(HND2PTR(FileHandle));
   return 0;
}
int FileRead_size(const char *FileName)
{
   SceIoStat stat;
   if(sceIoGetstat(FileName,&stat) < 0) return -1;
   if(stat.st_size > 0x00000000ffffffff)return 0xffffffff;
   return stat.st_size;
}

int FileRead_read(void * Buffer,int Size,int FileHandle)
{
   if(FileHandle == -1)return -1;
   return sceIoRead(HND2PTR(FileHandle),Buffer,Size);
}

u8* convertjpeg(const char *FileName)
{
      printf("ConvertStarts\n");
   int size = FileRead_size(FileName);
   u8* rgbabuf = (u8*)malloc(4*480*272);
   u8* databuf = (u8*)malloc(size);
   if(databuf == NULL || rgbabuf == NULL)
   {
      printf("Malloc Error!\n");
      free(databuf);
      free(rgbabuf);
      sceJpegDeleteMJpeg();
      sceJpegFinishMJpeg();
      return NULL;
   }
   int fh = FileRead_open(FileName);
   if(fh == -1)
   {
      printf("File couldn't open!\n");
      free(databuf);
      free(rgbabuf);
      sceJpegDeleteMJpeg();
      sceJpegFinishMJpeg();
      return NULL;
   }
   FileRead_read(databuf,size,fh);
   FileRead_close(fh);
   int res = sceJpegDecodeMJpeg(databuf,size,rgbabuf,0);
   free(databuf);
   sceJpegDeleteMJpeg();
   sceJpegFinishMJpeg();
   if(res < 0)
   {
      printf("Decode Error!\n");
      free(rgbabuf);
      return NULL;
   }
   printf("Convert is completed!\n");
   return rgbabuf;
}

void initGraphics()
{
   sceGuInit();
   sceDisplayWaitVblankStart();
   sceGuDisplay(1);
   sceGuStart(GU_DIRECT, list);
}

void drawFrame(unsigned char* textureData)
{
sceGuStart(GU_DIRECT, list);
//sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT, 0,BUF_WIDTH);
   void* framebuffer = 0;

  sceGuCopyImage(GU_PSM_8888, 0, 0, 480, 272, 480, textureData, 0, 0, 512, (void*)(((unsigned int)framebuffer) + 0x4000000));

  sceGuFinish();
  sceGuSync(0,0);

  sceDisplayWaitVblankStart();
  sceGuSwapBuffers();
}

int LoadStartModule(char *path)
{
  u32 loadResult;
  u32 startResult;
  int status;

  loadResult = kuKernelLoadModule(path, 0, NULL);
  if (loadResult & 0x80000000) {
    return -1;
  }
  else {
    startResult = sceKernelStartModule(loadResult, 0, NULL, &status, NULL);
  }

  if (loadResult != startResult) {
    return -2;
  }
  return 0;
}

int StartJpegPsp()
{
  printf("Loading 'flash0:/kd/avcodec.prx'...");
  if (LoadStartModule("flash0:/kd/avcodec.prx") == 0) {
    printf("ok\n");
  }
  else {
    printf("failed\n");
    return -1;
  }

  printf("init Jpeg decompression...");
  if (sceJpegInitMJpeg() == 0) {
    if (sceJpegCreateMJpeg(480, 272) == 0) {
      printf("ok\n");
      return 0;
    }
  }

  printf("failed\n");
  return -1;
}


int main(void)
{
SetupCallbacks();
pspDebugScreenInit();
StartJpegPsp();
u8* bitmap = convertjpeg("background.jpg");
initGraphics();
drawFrame(bitmap);
sceKernelSleepThread();
return 0;
}


When I try to run this. It gaves Decode error....
What should I do?
_________________
I'm sorry for my bad English.
Back to top
View user's profile Send private message
ardatan



Joined: 12 Jan 2008
Posts: 44

PostPosted: Wed Apr 29, 2009 3:31 am    Post subject: Reply with quote

Oh Sorry... I solved it... A bad image caused this error...
Now I fixed the problem... :D Thanks J.F
_________________
I'm sorry for my bad English.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group