// File: olegk0[P]readAVI/test.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();

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

    const char *ext = "raw";
    if (!strcmp(stream_format.compression_type, "MJPG")) {
	ext = "jpg";
    } else if (!strcmp(stream_format.compression_type, "MPNG")) {
	ext = "png";
    }

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

#define MAX_FRAMES 20

    char filename[100];
    int frame_size;
    int cnt = 0;
    while ((frame_size = readAVI.GetFrameFromIndex(&frame_entry)) >= 0) {
	if (frame_size > 0) {
	    FILE *fp;
	    sprintf(filename, "out/frame_%4.4d.%s", cnt, ext);
	    fp = fopen(filename, "wb");
	    if (fp) {
		fwrite(frame_entry.buf, 1, frame_size, fp);
		fclose(fp);
	    } else
		break;

	    cnt++;
	    if(cnt >= MAX_FRAMES)
		break;
	}
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x2410
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x3c07(%rip), %rdi      # 0x6004
movb $0x0, %al
callq 0x2040
movl $0x1, -0x4(%rbp)
jmp 0x2635
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x358(%rbp), %rdi
movq %rdi, -0x450(%rbp)
callq 0x26e0
movq -0x450(%rbp), %rdi
callq 0x2650
movl %eax, -0x444(%rbp)
jmp 0x243f
movl -0x444(%rbp), %eax
movl %eax, -0x35c(%rbp)
movl -0x35c(%rbp), %esi
leaq 0x3bbd(%rip), %rdi      # 0x6015
xorl %eax, %eax
callq 0x2040
jmp 0x2461
leaq -0x3a8(%rbp), %rdi
leaq -0x358(%rbp), %rsi
callq 0x2670
jmp 0x2476
leaq -0x394(%rbp), %rsi
leaq 0x3b9d(%rip), %rdi      # 0x6021
xorl %eax, %eax
callq 0x2040
jmp 0x248d
leaq 0x3ba2(%rip), %rax      # 0x6036
movq %rax, -0x3b0(%rbp)
leaq -0x3a8(%rbp), %rdi
addq $0x14, %rdi
leaq 0x3b8d(%rip), %rsi      # 0x603a
callq 0x21f0
cmpl $0x0, %eax
jne 0x24ea
leaq 0x3b81(%rip), %rax      # 0x603f
movq %rax, -0x3b0(%rbp)
jmp 0x2516
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x368(%rbp)
movl %eax, -0x36c(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x3050
jmp 0x2641
leaq -0x3a8(%rbp), %rdi
addq $0x14, %rdi
leaq 0x3b47(%rip), %rsi      # 0x6043
callq 0x21f0
cmpl $0x0, %eax
jne 0x2514
leaq 0x3b3b(%rip), %rax      # 0x6048
movq %rax, -0x3b0(%rbp)
jmp 0x2516
movl $0x3, -0x3c8(%rbp)
movl $0x0, -0x3b8(%rbp)
movl $0x0, -0x438(%rbp)
leaq -0x358(%rbp), %rdi
leaq -0x3c8(%rbp), %rsi
callq 0x3140
movl %eax, -0x454(%rbp)
jmp 0x254f
movl -0x454(%rbp), %eax
movl %eax, -0x434(%rbp)
cmpl $0x0, %eax
jl 0x2622
cmpl $0x0, -0x434(%rbp)
jle 0x261d
movl -0x438(%rbp), %edx
movq -0x3b0(%rbp), %rcx
leaq 0x3ac7(%rip), %rsi      # 0x604c
xorl %eax, %eax
leaq -0x430(%rbp), %rdi
movq %rdi, -0x468(%rbp)
callq 0x2050
movq -0x468(%rbp), %rdi
leaq 0x3ab7(%rip), %rsi      # 0x605f
callq 0x21b0
movq %rax, -0x460(%rbp)
jmp 0x25b6
movq -0x460(%rbp), %rax
movq %rax, -0x440(%rbp)
cmpq $0x0, -0x440(%rbp)
je 0x25ff
movq -0x3c0(%rbp), %rdi
movslq -0x434(%rbp), %rdx
movq -0x440(%rbp), %rcx
movl $0x1, %esi
callq 0x2280
jmp 0x25ef
movq -0x440(%rbp), %rdi
callq 0x2150
jmp 0x25fd
jmp 0x2601
jmp 0x2622
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
cmpl $0x14, -0x438(%rbp)
jl 0x261b
jmp 0x2622
jmp 0x261d
jmp 0x2534
movl $0x0, -0x4(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x3050
movl -0x4(%rbp), %eax
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
movq -0x368(%rbp), %rdi
callq 0x2270
nopl (%rax)