// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/main.c
// Function: create_video
bool create_video(int canvasWidth, int canvasHeight,
                  struct Scene *scene,float aspectRatio,float yFov,
                  struct Ray **rayStacks,
                  struct KdTraverseNode **traverseStacks)  {
  int canvasSize=canvasWidth*canvasHeight;

  float viewRot[9]={1.0f,0.0f,0.0f,
                    0.0f,1.0f,0.0f,
                    0.0f,0.0f, 1.0f};

  sobol_state_t qrnd;
  sobol_init(&qrnd,2);

  float *canvas;
  canvas=(float*)malloc(sizeof(float)*canvasSize*3);

  //

  unsigned char *bitmap;
  bitmap=(unsigned char*)malloc(canvasSize*3);

  avi_t *avifile;
  createDir("output");
  createDir("output/tmp");
  int fileNum=lastFileNum("output")+1;
  //     printf("fn %i\n",fileNum);
  char fn[512];
  snprintf(fn,sizeof(fn),"output/output%04i.avi",fileNum);

  if(access(fn, F_OK )!=-1) {
    fprintf(stderr,"'%s' already exists.\n",fn);

    return false;
  }



  //
  float viewPosKfs[]={
    2.0f,10.0f,35.0f,
    -9.0f,-10.0f,30.0f,
    -11.0f,10.0f,25.0f,

    12.0f,13.0f,20.0f,
    -12.0f,13.0f,40.0f,
    12.0f,-12.0f,35.0f,

    13.0f,13.0f,40.0f,

    0.0f,0.0f,-10.0f,

    };
  float viewPosInds[]={0.0f,5.0f,10.0f,
                       15.0f,20.0f,25.0f,
                       35.0f,45.0f};


    float frame;
    int count=0;
    for(frame=0.0f;frame<=45.0f;frame+=1.0f/(float)FPS) {

      sobol_init(&qrnd,2);
      float pos[3];
      keyframer(frame,viewPosInds,
                viewPosKfs,8, 3,
                0.0f,true, pos);

      renderScene(0,canvasSize,
                  canvasWidth,canvasHeight,
                  aspectRatio,yFov,
                  scene->minBound,scene->maxBound,
                  pos,
                  viewRot,
                  rayStacks[0],
                  traverseStacks[0],
                  scene->rootNode,
                  scene->triangles,scene->trianglesNum,
                  scene->spheres,scene->spheresNum,
                  scene->pointLights, scene->pointLightsNum,

                  &qrnd,

                  canvas,
                  RAY_MAX_BOUNCES,RAY_MAX_SPLITS
                  );

      //
      int i;
      for(i=0;i<canvasSize;i++) {
        bitmap[i*3]=floatToByte(canvas[i*3]);
        bitmap[i*3+1]=floatToByte(canvas[i*3+1]);
        bitmap[i*3+2]=floatToByte(canvas[i*3+2]);
      }



      char fn2[256];
      snprintf(fn2,sizeof(fn2),"output/tmp/tmp%04i_%i.jpg",fileNum,count);
      jo_write_jpg(fn2,bitmap,canvasWidth,canvasHeight,3, 100);

// #ifdef WIN32
//       char fn3[256];
//       snprintf(fn3,sizeof(fn3),"output/tmp/tmp%04i_%i_w32.bmp",fileNum,count);
//       printf("a\n");
//       // jo_write_jpg(fn3,bitmap,CANVAS_WIDTH,CANVAS_HEIGHT,3, 100);
//       SaveBMPToFile(fn3,bitmap,CANVAS_WIDTH,CANVAS_HEIGHT);
// #endif
      printf("frame %g\n",frame);

      count++;
      //



    }
    int i;
    avifile = AVI_open_output_file(fn);


    if(!avifile) {
      printf("aa\n");
      return false;
    }



    AVI_set_video(avifile,canvasWidth,canvasHeight,(double)FPS,"MJPG");


    for(i=0;i<count;i++) {

      char fn2[256];
      snprintf(fn2,sizeof(fn2),"output/tmp/tmp%04i_%i.jpg",fileNum,i);

      FILE *in;
      unsigned char *buffer=0;
      int buffer_len=0;

      in=fopen(fn2,"rb");

      if (in==0) {
        printf("Cannot open test.jpeg %s\n",fn2);
        return false;
      }

      fseek(in, 0, SEEK_END);
      buffer_len=(int)ftell(in);
      fseek(in,0,SEEK_SET);
      buffer=(unsigned char *)malloc(buffer_len);
      fread(buffer,1,buffer_len,in);
      fclose(in);
      // add_avifile_jpeg_frame( avifile,  bitmap, CANVAS_WIDTH,CANVAS_HEIGHT);
      AVI_write_frame(avifile,(char*)buffer,buffer_len,1);
      free(buffer);
    }

    AVI_close(avifile);

    //

    printf("Wrote '%s'.\n",fn);

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1778, %rsp           # imm = 0x1778
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movss %xmm1, 0x18(%rsp)
movss %xmm0, 0x14(%rsp)
movq %rdx, %r13
movl %edi, %r14d
movl %esi, 0x8(%rsp)
movl %esi, %ebp
imull %edi, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0x3f800000, %eax       # imm = 0x3F800000
movl %eax, 0x50(%rsp)
movl %eax, 0x60(%rsp)
movl %eax, 0x70(%rsp)
leaq 0x408(%rsp), %rdi
movl $0x2, %esi
callq 0x40a294
movslq %ebp, %rbx
leaq (,%rbx,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4458c0
movq %rax, %r12
leal (%rbx,%rbx,2), %eax
movslq %eax, %rdi
callq 0x4458c0
movq %rax, %rbx
leaq 0xd14e6(%rip), %r15     # 0x4d7a10
movq %r15, %rdi
callq 0x4175e6
leaq 0xd14de(%rip), %rdi     # 0x4d7a17
callq 0x4175e6
movq %r15, %rdi
callq 0x4175f0
incl %eax
leaq 0xd14d3(%rip), %rdx     # 0x4d7a22
leaq 0x200(%rsp), %r15
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movl %eax, 0xc(%rsp)
movl %eax, %ecx
xorl %eax, %eax
callq 0x428400
movq %r15, %rdi
xorl %esi, %esi
callq 0x482750
cmpl $-0x1, %eax
jne 0x4068e0
leaq 0xd165a(%rip), %rsi     # 0x4d7be0
leaq 0xa0(%rsp), %rdi
movl $0xc, %ecx
rep  movsq (%rsi), %es:(%rdi)
movaps 0xd16d3(%rip), %xmm0    # 0x4d7c70
leaq 0x80(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps 0xd16b0(%rip), %xmm0    # 0x4d7c60
movaps %xmm0, (%rax)
leaq 0x34(%r13), %rax
movq %rax, 0x30(%rsp)
movq %r13, 0x38(%rsp)
leaq 0x40(%r13), %rax
movq %rax, 0x28(%rsp)
movl %ebp, 0x10(%rsp)
movl %ebp, %eax
movl %r14d, %r13d
leaq (%rax,%rax,2), %r14
xorps %xmm0, %xmm0
movl $0x1, %eax
xorl %ebp, %ebp
movl 0x8(%rsp), %r15d
movl %r13d, 0x4(%rsp)
movss %xmm0, (%rsp)
movl %ebp, 0x1c(%rsp)
movq %rax, 0x20(%rsp)
leaq 0x408(%rsp), %rdi
movl $0x2, %esi
callq 0x40a294
xorps %xmm1, %xmm1
movss (%rsp), %xmm0
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movl $0x8, %edx
movl $0x3, %ecx
movl $0x1, %r8d
leaq 0x74(%rsp), %r9
callq 0x4087ad
movq 0x38(%rsp), %rbp
movl 0x18(%rbp), %eax
movl 0x1c(%rbp), %r10d
movl 0x20(%rbp), %r11d
subq $0x8, %rsp
xorl %edi, %edi
movl 0x18(%rsp), %esi
movl %r13d, %edx
movl %r15d, %ecx
movss 0x1c(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %r9
pushq $0x8
pushq $0x1
pushq %r12
leaq 0x428(%rsp), %r15
pushq %r15
pushq %r11
pushq 0x10(%rbp)
pushq %r10
pushq 0x8(%rbp)
pushq %rax
pushq (%rbp)
pushq 0x28(%rbp)
movq 0xa8(%rsp), %rax
pushq (%rax)
movq 0xa8(%rsp), %rax
pushq (%rax)
leaq 0xc0(%rsp), %rax
pushq %rax
leaq 0xec(%rsp), %rax
pushq %rax
callq 0x416a36
addq $0x80, %rsp
cmpl $0x0, 0x10(%rsp)
jle 0x406706
xorl %ebp, %ebp
movss (%r12,%rbp,4), %xmm0
callq 0x41770c
movb %al, (%rbx,%rbp)
movss 0x4(%r12,%rbp,4), %xmm0
callq 0x41770c
movb %al, 0x1(%rbx,%rbp)
movss 0x8(%r12,%rbp,4), %xmm0
callq 0x41770c
movb %al, 0x2(%rbx,%rbp)
addq $0x3, %rbp
cmpq %rbp, %r14
jne 0x4066cf
movl $0x100, %esi            # imm = 0x100
leaq 0x100(%rsp), %r15
movq %r15, %rdi
leaq 0xd1331(%rip), %rdx     # 0x4d7a4e
movl 0xc(%rsp), %ecx
movl 0x1c(%rsp), %ebp
movl %ebp, %r8d
xorl %eax, %eax
callq 0x428400
movq %r15, %rdi
movq %rbx, %rsi
movl 0x4(%rsp), %r13d
movl %r13d, %edx
movl 0x8(%rsp), %r15d
movl %r15d, %ecx
movl $0x3, %r8d
movl $0x64, %r9d
callq 0x404afc
xorps %xmm0, %xmm0
cvtss2sd (%rsp), %xmm0
leaq 0xd1303(%rip), %rdi     # 0x4d7a68
movb $0x1, %al
callq 0x428330
movss (%rsp), %xmm0
incl %ebp
addss 0xd11f5(%rip), %xmm0    # 0x4d7970
movq 0x20(%rsp), %rax
incl %eax
movss 0xd11ea(%rip), %xmm1    # 0x4d7974
ucomiss %xmm0, %xmm1
jae 0x4065eb
leaq 0x200(%rsp), %rdi
callq 0x4019c8
testq %rax, %rax
je 0x4068a4
movq %rax, %rbx
leaq 0xd12bf(%rip), %rcx     # 0x4d7a72
movsd 0xd11c5(%rip), %xmm0    # 0x4d7980
movq %rax, %rdi
movl 0x4(%rsp), %esi
movl %r15d, %edx
callq 0x401ac4
leaq 0x100(%rsp), %r15
xorl %ebp, %ebp
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
leaq 0xd126b(%rip), %rdx     # 0x4d7a4e
movl 0xc(%rsp), %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x428400
movq %r15, %rdi
leaq 0xd127c(%rip), %rsi     # 0x4d7a77
callq 0x42f2a0
testq %rax, %rax
je 0x4068b4
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x433520
movq %r13, %rdi
callq 0x42f4a0
movq %rax, %r14
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x433520
movslq %r14d, %r14
movq %r14, %rdi
callq 0x4458c0
movq %rax, %r12
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %r13, %rcx
callq 0x42f3a0
movq %r13, %rdi
callq 0x42eb70
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x4021e9
movq %r12, %rdi
callq 0x445c00
incl %ebp
cmpl %ebp, 0x20(%rsp)
jne 0x4067d4
movq %rbx, %rdi
callq 0x40266b
leaq 0xd1203(%rip), %rdi     # 0x4d7a94
leaq 0x200(%rsp), %rsi
xorl %eax, %eax
callq 0x428330
movb $0x1, %bl
jmp 0x4068cc
leaq 0xd12c2(%rip), %rdi     # 0x4d7b6d
callq 0x42f8c0
xorl %ebx, %ebx
jmp 0x4068cc
leaq 0xd11bf(%rip), %rdi     # 0x4d7a7a
xorl %ebx, %ebx
leaq 0x100(%rsp), %rsi
xorl %eax, %eax
callq 0x428330
movl %ebx, %eax
addq $0x1778, %rsp           # imm = 0x1778
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x519848, %rax         # imm = 0x519848
movq (%rax), %rdi
leaq 0xd1147(%rip), %rsi     # 0x4d7a38
xorl %ebx, %ebx
leaq 0x200(%rsp), %rdx
xorl %eax, %eax
callq 0x428270
jmp 0x4068cc