// File: 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
movq %rsp, %rbp
subq $0x460, %rsp            # imm = 0x460
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x2b30
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x35d9(%rip), %rdi      # 0x60f6
movb $0x0, %al
callq 0x2060
movl $0x1, -0x4(%rbp)
jmp 0x2e17
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x358(%rbp), %rdi
movq %rdi, -0x430(%rbp)
callq 0x2f10
movq -0x430(%rbp), %rdi
callq 0x2e40
movl %eax, -0x424(%rbp)
jmp 0x2b5f
movl -0x424(%rbp), %eax
movl %eax, -0x35c(%rbp)
movl -0x35c(%rbp), %esi
leaq 0x358f(%rip), %rdi      # 0x6107
xorl %eax, %eax
callq 0x2060
jmp 0x2b81
leaq -0x3a8(%rbp), %rdi
leaq -0x358(%rbp), %rsi
callq 0x2e60
jmp 0x2b96
leaq -0x3e0(%rbp), %rdi
leaq -0x358(%rbp), %rsi
callq 0x2ea0
jmp 0x2bab
leaq -0x394(%rbp), %rsi
leaq 0x355a(%rip), %rdi      # 0x6113
xorl %eax, %eax
callq 0x2060
jmp 0x2bc2
leaq -0x3a8(%rbp), %rdi
addq $0x14, %rdi
leaq 0x3554(%rip), %rsi      # 0x6128
callq 0x2320
cmpl $0x0, %eax
je 0x2c1b
leaq 0x3548(%rip), %rdi      # 0x612d
xorl %eax, %eax
callq 0x2060
jmp 0x2bee
movl $0x1, %edi
callq 0x22b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x368(%rbp)
movl %eax, -0x36c(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x3880
jmp 0x2e23
movl -0x3a4(%rbp), %eax
movl %eax, -0x3e4(%rbp)
movl -0x3a0(%rbp), %eax
movl %eax, -0x3e8(%rbp)
movl -0x3e4(%rbp), %ecx
movl -0x3e8(%rbp), %r8d
leaq 0x34fb(%rip), %rdi      # 0x6142
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
xorl %r9d, %r9d
movl %edx, %esi
callq 0x2070
movq %rax, -0x438(%rbp)
jmp 0x2c5f
movq -0x438(%rbp), %rax
movq %rax, -0x3f0(%rbp)
cmpq $0x0, -0x3f0(%rbp)
jne 0x2c81
movl $0x1, %edi
callq 0x22b0
movq -0x3f0(%rbp), %rdi
callq 0x22e0
movq %rax, -0x440(%rbp)
jmp 0x2c96
movq -0x440(%rbp), %rax
movq %rax, -0x3f8(%rbp)
cmpq $0x0, -0x3f8(%rbp)
je 0x2df6
movl -0x3e4(%rbp), %esi
movl -0x3e8(%rbp), %edx
movl -0x398(%rbp), %ecx
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
xorl %r9d, %r9d
movl %r9d, %edi
movl %r9d, %r8d
callq 0x2330
movq %rax, -0x448(%rbp)
jmp 0x2ceb
movq -0x448(%rbp), %rax
movq %rax, -0x400(%rbp)
movl $0x3, -0x418(%rbp)
movl $0x0, -0x408(%rbp)
leaq -0x358(%rbp), %rdi
leaq -0x418(%rbp), %rsi
callq 0x3970
movl %eax, -0x44c(%rbp)
jmp 0x2d28
movl -0x44c(%rbp), %eax
movl %eax, -0x41c(%rbp)
cmpl $0x0, %eax
jl 0x2de6
cmpl $0x0, -0x41c(%rbp)
jle 0x2dd4
movq -0x400(%rbp), %rdi
callq 0x2310
jmp 0x2d58
movq -0x410(%rbp), %rdi
movl -0x41c(%rbp), %esi
movl -0x398(%rbp), %edx
movq -0x400(%rbp), %rax
movq 0x20(%rax), %rcx
callq 0x25c0
movl %eax, -0x450(%rbp)
jmp 0x2d83
movl -0x450(%rbp), %eax
movl %eax, -0x420(%rbp)
movq -0x400(%rbp), %rdi
callq 0x2350
jmp 0x2d9d
cmpl $0x0, -0x420(%rbp)
jne 0x2dd2
movq -0x400(%rbp), %rdi
movq -0x3f8(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
callq 0x21c0
jmp 0x2dc2
movq -0x3f0(%rbp), %rdi
callq 0x2270
jmp 0x2dd0
jmp 0x2dd2
jmp 0x2dd4
movl -0x3e0(%rbp), %edi
callq 0x2340
jmp 0x2de1
jmp 0x2d0d
movq -0x3f8(%rbp), %rdi
callq 0x2200
jmp 0x2df4
jmp 0x2df6
movq -0x3f0(%rbp), %rdi
callq 0x2030
jmp 0x2e04
movl $0x0, -0x4(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x3880
movl -0x4(%rbp), %eax
addq $0x460, %rsp            # imm = 0x460
popq %rbp
retq
movq -0x368(%rbp), %rdi
callq 0x2410
nop