// File: /workspace/github_cpp_makefile_star2_O1/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 $0x3a8, %rsp            # imm = 0x3A8
cmpl $0x1, %edi
jg 0x2a0b
movq (%rsi), %rsi
leaq 0x26fc(%rip), %rdi      # 0x50f6
xorl %eax, %eax
callq 0x2060
movl $0x1, %eax
jmp 0x2ba2
movq 0x8(%rsi), %rsi
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bf8
movq 0x10(%rbx), %rax
subq 0x8(%rbx), %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %esi # imm = 0xCCCCCCCD
leaq 0x26d2(%rip), %rdi      # 0x5107
xorl %eax, %eax
callq 0x2060
movq 0x2c8(%rbx), %rax
leaq 0x24(%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 0x2696(%rip), %rdi      # 0x5113
movq %r14, %rsi
xorl %eax, %eax
callq 0x2060
movl $0x474e504d, %eax       # imm = 0x474E504D
xorl (%r14), %eax
movzbl 0x4(%r14), %ecx
orl %eax, %ecx
jne 0x2bb4
movl 0x14(%rsp), %ebp
movl 0x18(%rsp), %r12d
leaq 0x2681(%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 0x2bc2
movq %r14, %rdi
callq 0x22d0
testq %rax, %rax
je 0x2b8e
movq %rax, 0x8(%rsp)
movl 0x20(%rsp), %ecx
xorl %eax, %eax
xorl %edi, %edi
movl %ebp, %esi
movl %r12d, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0x2310
addq $0x10, %rsp
movq %rax, %r12
leaq 0x48(%rsp), %r13
movl $0x3, (%r13)
movl $0x0, 0x10(%r13)
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x32aa
movl %eax, %r15d
testl %eax, %eax
js 0x2b84
je 0x2b7b
movq %r12, %rdi
callq 0x2300
movq 0x50(%rsp), %rdi
movl 0x20(%rsp), %edx
movq 0x20(%r12), %rcx
movl %r15d, %esi
callq 0x263a
movl %eax, %r15d
movq %r12, %rdi
callq 0x2330
testl %r15d, %r15d
jne 0x2b7b
movq %r12, %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x21b0
movq %r14, %rdi
callq 0x2240
movl %ebx, %edi
callq 0x2320
jmp 0x2b20
movq 0x8(%rsp), %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x2030
leaq 0x60(%rsp), %rdi
callq 0x3216
xorl %eax, %eax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2581(%rip), %rdi      # 0x513c
callq 0x2390
jmp 0x2bc2
movl $0x1, %edi
callq 0x2290
jmp 0x2bd6
jmp 0x2bd6
jmp 0x2bd6
jmp 0x2bd6
jmp 0x2bd6
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3216
movq %rbx, %rdi
callq 0x2400