// File: /workspace/github_c_makefile_star2_O2/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 $0x1768, %rsp           # imm = 0x1768
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movss %xmm1, 0x1c(%rsp)
movss %xmm0, 0x18(%rsp)
movq %rdx, %r13
movl %esi, 0x8(%rsp)
movl %esi, %ebp
movl %edi, 0x4(%rsp)
imull %edi, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movl $0x3f800000, %eax       # imm = 0x3F800000
movl %eax, 0x40(%rsp)
movl %eax, 0x50(%rsp)
movl %eax, 0x60(%rsp)
leaq 0x3f8(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x409184
movslq %ebp, %r14
imulq $0xc, %r14, %rdi
callq 0x442160
movq %rax, %rbx
leal (%r14,%r14,2), %eax
movslq %eax, %rdi
callq 0x442160
movq %rax, %r15
leaq 0xcdb02(%rip), %r12     # 0x4d39c0
movq %r12, %rdi
callq 0x413e50
leaq 0xcdafa(%rip), %rdi     # 0x4d39c7
callq 0x413e50
movq %r12, %rdi
callq 0x413e5a
incl %eax
leaq 0xcdaef(%rip), %rdx     # 0x4d39d2
leaq 0x1f0(%rsp), %r12
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdi
movl %eax, 0x10(%rsp)
movl %eax, %ecx
xorl %eax, %eax
callq 0x424ca0
movq %r12, %rdi
xorl %esi, %esi
callq 0x47f010
cmpl $-0x1, %eax
jne 0x406244
leaq 0xcdc76(%rip), %rsi     # 0x4d3b90
pushq $0xc
popq %rcx
leaq 0x90(%rsp), %rdi
rep  movsq (%rsi), %es:(%rdi)
movaps 0xcdcf1(%rip), %xmm0    # 0x4d3c20
leaq 0x70(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps 0xcdcd1(%rip), %xmm0    # 0x4d3c10
movaps %xmm0, (%rax)
leaq 0x34(%r13), %rax
movq %rax, 0x28(%rsp)
testl %ebp, %ebp
movl $0x0, %eax
movl %ebp, 0x14(%rsp)
cmovgl %ebp, %eax
leaq 0x40(%r13), %rcx
movq %rcx, 0x20(%rsp)
leaq (%rax,%rax,2), %rbp
xorps %xmm0, %xmm0
xorl %r12d, %r12d
movss 0xcd9b4(%rip), %xmm1    # 0x4d3928
ucomiss %xmm0, %xmm1
jb 0x4060f6
leaq 0x3f8(%rsp), %rdi
pushq $0x2
popq %rsi
movss %xmm0, 0xc(%rsp)
callq 0x409184
xorps %xmm1, %xmm1
movss 0xc(%rsp), %xmm0
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
pushq $0x8
popq %rdx
pushq $0x3
popq %rcx
pushq $0x1
popq %r8
movl %r12d, %r14d
leaq 0x64(%rsp), %r9
callq 0x407b21
movl 0x18(%r13), %eax
movl 0x1c(%r13), %r10d
movl 0x20(%r13), %r11d
subq $0x8, %rsp
xorl %edi, %edi
movl 0x1c(%rsp), %esi
movl 0xc(%rsp), %edx
movl 0x10(%rsp), %ecx
movss 0x20(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
pushq $0x8
pushq $0x1
pushq %rbx
leaq 0x418(%rsp), %r12
pushq %r12
pushq %r11
pushq 0x10(%r13)
pushq %r10
pushq 0x8(%r13)
pushq %rax
pushq (%r13)
pushq 0x28(%r13)
movq 0x98(%rsp), %rax
pushq (%rax)
movq 0x98(%rsp), %rax
pushq (%rax)
leaq 0xb0(%rsp), %rax
pushq %rax
leaq 0xdc(%rsp), %rax
pushq %rax
movl %r14d, %r12d
callq 0x4132fc
addq $0x80, %rsp
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0x40608c
movss (%rbx,%r14,4), %xmm0
callq 0x413f51
movb %al, (%r15,%r14)
movss 0x4(%rbx,%r14,4), %xmm0
callq 0x413f51
movb %al, 0x1(%r15,%r14)
movss 0x8(%rbx,%r14,4), %xmm0
callq 0x413f51
movb %al, 0x2(%r15,%r14)
addq $0x3, %r14
jmp 0x406050
movl $0x100, %esi            # imm = 0x100
leaq 0xf0(%rsp), %r14
movq %r14, %rdi
leaq 0xcd95b(%rip), %rdx     # 0x4d39fe
movl 0x10(%rsp), %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x424ca0
movq %r14, %rdi
movq %r15, %rsi
movl 0x4(%rsp), %edx
movl 0x8(%rsp), %ecx
pushq $0x3
popq %r8
pushq $0x64
popq %r9
callq 0x4047f4
cvtss2sd 0xc(%rsp), %xmm0
leaq 0xcd93f(%rip), %rdi     # 0x4d3a18
movb $0x1, %al
callq 0x424bd0
movss 0xc(%rsp), %xmm0
incl %r12d
addss 0xcd83b(%rip), %xmm0    # 0x4d392c
jmp 0x405f6c
leaq 0x1f0(%rsp), %rdi
callq 0x4018e8
testq %rax, %rax
je 0x4061e6
movq %rax, %rbx
leaq 0xcd90c(%rip), %rcx     # 0x4d3a22
movsd 0xcd81a(%rip), %xmm0    # 0x4d3938
movq %rax, %rdi
movl 0x4(%rsp), %esi
movl 0x8(%rsp), %edx
callq 0x401a04
xorl %r14d, %r14d
cmpl %r14d, %r12d
je 0x4061f6
movl $0x100, %esi            # imm = 0x100
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
leaq 0xcd8ad(%rip), %rdx     # 0x4d39fe
movl 0x10(%rsp), %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x424ca0
movq %r15, %rdi
leaq 0xcd8be(%rip), %rsi     # 0x4d3a27
callq 0x42bb40
testq %rax, %rax
je 0x406218
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0x42fdc0
movq %rbp, %rdi
callq 0x42bd40
movq %rax, %r13
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x42fdc0
movslq %r13d, %r13
movq %r13, %rdi
callq 0x442160
movq %rax, %r15
movq %rax, %rdi
pushq $0x1
popq %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x42bc40
movq %rbp, %rdi
callq 0x42b410
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
pushq $0x1
popq %rcx
callq 0x402209
movq %r15, %rdi
callq 0x4424a0
incl %r14d
jmp 0x406131
leaq 0xcd930(%rip), %rdi     # 0x4d3b1d
callq 0x42c160
xorl %ebx, %ebx
jmp 0x406230
movq %rbx, %rdi
callq 0x40263d
leaq 0xcd83f(%rip), %rdi     # 0x4d3a44
leaq 0x1f0(%rsp), %rsi
xorl %eax, %eax
callq 0x424bd0
movb $0x1, %bl
jmp 0x406230
leaq 0xcd80b(%rip), %rdi     # 0x4d3a2a
xorl %ebx, %ebx
leaq 0xf0(%rsp), %rsi
xorl %eax, %eax
callq 0x424bd0
movl %ebx, %eax
addq $0x1768, %rsp           # imm = 0x1768
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x516868, %rax         # imm = 0x516868
movq (%rax), %rdi
leaq 0xcd793(%rip), %rsi     # 0x4d39e8
xorl %ebx, %ebx
leaq 0x1f0(%rsp), %rdx
xorl %eax, %eax
callq 0x424b10
jmp 0x406230