Guest User

Untitled

a guest
Jun 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. //main.c
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "ds2io.h"
  6. #include "ds2_cpu.h"
  7. #include "ds2_malloc.h"
  8. #include "fs_api.h"
  9. #include "key.h"
  10. #include <math.h>
  11. #include "gba-jpeg-decode.h"
  12. //#include "wave.h"
  13.  
  14. struct rtc Date;
  15. short * Audio_Buf_Addr = NULL;
  16.  
  17. struct rtc Date;
  18.  
  19. void load_jpg(void *jpeg, char width, char height) {
  20.     JPEG_DecompressImage((u8*)jpeg, &((unsigned short*)down_screen_addr)[256*height+width], 256, 192);
  21. }
  22. FILE* vidFile;
  23. struct VIDEO_HEADER
  24. {
  25.   int   height;
  26.   int   framecount;
  27.   int   samplesize;
  28. } vidhead;
  29. int framecounter=0;
  30.  
  31. void timerhandle(unsigned int arg)
  32. {
  33. //  sti();
  34. //  printf("T %d\n", GetSysTime());
  35.     framecounter++;
  36. }
  37.  
  38. /*==========================================
  39. Copies stereo and or mono data to the
  40.     ds two io layer
  41. ===========================================*/
  42. void Transfer_Stream(void *data, char channels)
  43. {
  44.     if(NULL != Audio_Buf_Addr && data != NULL)
  45.     {
  46.         short *src = (short*)data;
  47.        
  48.         short *dst0 = Audio_Buf_Addr;
  49.         short *dst1 = Audio_Buf_Addr + audio_samples_per_trans;
  50.        
  51.         int n=0;
  52.         while(n++ < audio_samples_per_trans)
  53.         {
  54.             switch(channels)
  55.             {
  56.                 case 2://stereo
  57.                     *dst0++ = *src++;
  58.                     *dst1++ = *src++;
  59.                 break;
  60.                 case 1://mono
  61.                     *dst0++ = *src++;
  62.                 break;
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69.  
  70. void play_smf(char * filename)
  71. {
  72.     u16 frame_count=0;
  73.     u16 pixSize=0;
  74.     char* pixBuf = (char*) malloc(16000);
  75.     int old_sec = 0;
  76.     int fps=0;
  77.     int framespeed = 10000 / 24;
  78.    
  79.     FILE* vidFile;
  80.     vidFile = fopen (filename, "rb"); //rb = read
  81.     pixBuf = (char*) malloc(16000);
  82.  
  83.     fread(&vidhead.height,4,1,vidFile);
  84.     fread(&vidhead.framecount,4,1,vidFile);
  85.     fread(&vidhead.samplesize,4,1,vidFile);
  86.    
  87.     //Interrupt period =1s
  88.     initTimer(1, 100, timerhandle, 0);
  89.     runTimer(1);
  90.     // sound
  91.     short* sndBuf = NULL;
  92.  
  93.     int sndptr = 0;
  94.     short* ConvertBuf = (short*)calloc(audio_samples_per_trans, 4);
  95.     short* FinalBuf =(short*)calloc(audio_samples_per_trans, 4);
  96.     sndBuf = (short*)calloc(audio_samples_per_trans, 4);
  97.     ds2_setVolume(127);
  98.  
  99.  
  100. unsigned short *audio_buffer_addr;
  101.  
  102. int buffNum = 0;
  103.  
  104. while(vidhead.framecount >= frame_count)
  105. {
  106.  
  107.     // replace the fread(audiobuf, 1, len, fp) from the wave example with this ?
  108.     int i=0;
  109.     for(i=0; i<audio_samples_per_trans*2; i++){
  110.         FinalBuf[i] = sndBuf[i+(audio_samples_per_trans * 2 * buffNum)];
  111.     }
  112.     //
  113.     Audio_Buf_Addr = (short*)ds2_getAudiobuff();
  114.     Transfer_Stream(FinalBuf, 2);
  115.     ds2_updateAudio();
  116.    
  117.     buffNum++;
  118.    
  119.         // when the whole sample has transfered, load next sample and video frame
  120.     if(buffNum >= vidhead.samplesize / (audio_samples_per_trans*2)){
  121.     buffNum=0;
  122. //  if(framecounter >= framespeed){
  123.         framecounter=0;
  124.         fread(&pixSize,4,1,vidFile);
  125.         fread(pixBuf, 1, pixSize, vidFile);
  126.         fread(sndBuf,1,vidhead.samplesize,vidFile);
  127.         load_jpg(pixBuf,256,(192-vidhead.height)>>1);
  128.        
  129.         frame_count++;
  130.         fps++;
  131.         ds2_flipScreen(DOWN_SCREEN, 1);
  132.  
  133.         ds2_getTime(&Date);
  134.         if(Date.seconds != old_sec){
  135.             printf("%d fps\n",fps);
  136.             fps=0;
  137.             old_sec=Date.seconds;
  138.         }
  139.     }//framecounter
  140.    
  141.     do {
  142.         audio_buffer_addr = (unsigned short*)ds2_getAudiobuff();
  143.     } while(NULL == audio_buffer_addr);
  144. }
  145.     fclose (vidFile);
  146. }  
  147.  
  148.  
  149. ////////////////////////////////////////////////////////////////////////////////
  150. void main(int argc, char* argv[])
  151. {
  152.  
  153.     ds2_setCPUclocklevel(6);
  154.     play_smf("video.smf");
  155.    
  156.     while(1)
  157.     {
  158.  
  159.         //ds2_flipScreen(DOWN_SCREEN, 1);
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment