// File: /workspace/github_cpp_makefile_star2_O2/olegk0[P]readAVI/player.cpp
// Function: main
int main(int argc, char *argv[])
{
    if (argc < 2) {
	printf("Use %s file.avi\n", argv[0]);
	return 1;
    }

    ReadAVI readAVI(argv[1]);

    int indexes = readAVI.GetIndexCnt();
    printf("indexes:%d\n", indexes);

    ReadAVI::stream_format_t stream_format = readAVI.GetVideoFormat();
    ReadAVI::avi_header_t avi_header = readAVI.GetAviHeader();

    printf("compression_type:%s\n", stream_format.compression_type);

    if (strcmp(stream_format.compression_type, "MPNG")) {
	printf("Only MPNG supported\n");
	exit(1);
    }

    int width = stream_format.image_width, height = stream_format.image_height;
    SDL_Window *MainWindow = SDL_CreateWindow("My Game Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
	    height, 0);
    if (!MainWindow)
	exit(1);

    SDL_Surface *MainSurface = SDL_GetWindowSurface(MainWindow);

    if (MainSurface) {
	SDL_Surface *BufSurface = SDL_CreateRGBSurface(0, width, height, stream_format.bits_per_pixel, 0, 0, 0, 0);

	ReadAVI::frame_entry_t frame_entry;
	frame_entry.type = ReadAVI::ctype_video_data;
	frame_entry.pointer = 0;

	int frame_size;
	while ((frame_size = readAVI.GetFrameFromIndex(&frame_entry)) >= 0) {
	    if (frame_size > 0) {
		SDL_LockSurface(BufSurface);
		int ret = UnpackPNG(frame_entry.buf, frame_size, stream_format.bits_per_pixel, (unsigned char*)BufSurface->pixels);
		SDL_UnlockSurface(BufSurface);
		if (ret == 0) {
		    SDL_BlitSurface(BufSurface, NULL, MainSurface, NULL);
		    SDL_UpdateWindowSurface(MainWindow);
		}
	    }

	    usleep(avi_header.TimeBetweenFrames);
	}

	SDL_FreeSurface(MainSurface);
    }

    SDL_DestroyWindow(MainWindow);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3b8, %rsp            # imm = 0x3B8
cmpl $0x1, %edi
jg 0x2991
movq (%rsi), %rsi
leaq 0x2774(%rip), %rdi      # 0x50f6
xorl %eax, %eax
callq 0x2060
pushq $0x1
popq %rax
jmp 0x2b2b
movq 0x8(%rsi), %rsi
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0x2b7c
movq 0x10(%rbx), %rax
subq 0x8(%rbx), %rax
pushq $0x14
popq %rcx
cqto
idivq %rcx
leaq 0x274e(%rip), %rdi      # 0x5107
movl %eax, %esi
xorl %eax, %eax
callq 0x2060
movq 0x2c8(%rbx), %rax
leaq 0x34(%rsp), %r14
movq %rax, 0x1c(%r14)
movups 0x2b8(%rbx), %xmm0
movaps %xmm0, 0xc(%r14)
movups 0x2a8(%rbx), %xmm0
movaps %xmm0, -0x4(%r14)
movups 0x298(%rbx), %xmm0
movaps %xmm0, -0x14(%r14)
movl 0x228(%rbx), %ebx
leaq 0x2710(%rip), %rdi      # 0x5113
movq %r14, %rsi
xorl %eax, %eax
callq 0x2060
leaq 0x2714(%rip), %rsi      # 0x5128
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
jne 0x2b3d
movl 0x24(%rsp), %ebp
movl 0x28(%rsp), %r12d
leaq 0x26f6(%rip), %rdi      # 0x512d
movl $0x1fff0000, %esi       # imm = 0x1FFF0000
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
movl %ebp, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x2070
movq %rax, %r14
testq %rax, %rax
je 0x2b49
movq %r14, %rdi
callq 0x2310
testq %rax, %rax
je 0x2b17
movq %rax, 0x18(%rsp)
movl 0x30(%rsp), %ecx
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
xorl %edi, %edi
movl %ebp, %esi
movl %r12d, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2350
movq %rax, %r12
leaq 0x58(%rsp), %r13
movl $0x3, (%r13)
andl $0x0, 0x10(%r13)
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x319e
movl %eax, %r15d
testl %eax, %eax
js 0x2b0d
je 0x2b04
movq %r12, %rdi
callq 0x2340
movq 0x60(%rsp), %rdi
movl 0x30(%rsp), %edx
movq 0x20(%r12), %rcx
movl %r15d, %esi
callq 0x25aa
movl %eax, %r15d
movq %r12, %rdi
callq 0x2370
testl %r15d, %r15d
jne 0x2b04
movq %r12, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x21d0
movq %r14, %rdi
callq 0x2290
movl %ebx, %edi
callq 0x2360
jmp 0x2aa9
movq 0x18(%rsp), %rdi
callq 0x2210
movq %r14, %rdi
callq 0x2030
leaq 0x70(%rsp), %rdi
callq 0x3114
xorl %eax, %eax
addq $0x3b8, %rsp            # imm = 0x3B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25f8(%rip), %rdi      # 0x513c
callq 0x23d0
pushq $0x1
popq %rdi
callq 0x22d0
jmp 0x2b5b
jmp 0x2b5b
jmp 0x2b5b
jmp 0x2b5b
jmp 0x2b5b
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3114
movq %rbx, %rdi
callq 0x2430