// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
cmpl $0x1, %edi
jg 0x23dd
movq (%rsi), %rsi
leaq 0x2c38(%rip), %rdi      # 0x5004
xorl %eax, %eax
callq 0x2040
movl $0x1, %eax
jmp 0x254b
movq 0x8(%rsi), %rsi
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
callq 0x2578
movq 0x10(%r14), %rax
subq 0x8(%r14), %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %esi # imm = 0xCCCCCCCD
leaq 0x2c0b(%rip), %rdi      # 0x5015
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2040
movq 0x2c8(%r14), %rax
leaq 0x34(%rsp), %r15
movq %rax, 0x1c(%r15)
movups 0x298(%r14), %xmm0
movups 0x2a8(%r14), %xmm1
movups 0x2b8(%r14), %xmm2
movaps %xmm2, 0xc(%r15)
movaps %xmm1, -0x4(%r15)
movaps %xmm0, -0x14(%r15)
leaq 0x2bd0(%rip), %rdi      # 0x5021
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movl $0x47504a4d, %eax       # imm = 0x47504A4D
xorl (%r15), %eax
movzbl 0x4(%r15), %ecx
orl %eax, %ecx
je 0x2490
movl $0x474e504d, %eax       # imm = 0x474E504D
xorl 0x34(%rsp), %eax
movzbl 0x38(%rsp), %ecx
orl %eax, %ecx
leaq 0x2bc5(%rip), %rax      # 0x5048
leaq 0x2bac(%rip), %r14      # 0x5036
cmoveq %rax, %r14
jmp 0x2497
leaq 0x2ba8(%rip), %r14      # 0x503f
leaq 0x8(%rsp), %r15
movl $0x3, (%r15)
movl $0x0, 0x10(%r15)
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x2c2a
movl %eax, %r13d
testl %eax, %eax
js 0x253c
je 0x24b3
xorl %ebx, %ebx
leaq 0x60(%rsp), %rdi
leaq 0x2b77(%rip), %rsi      # 0x504c
movl %ebp, %edx
movq %r14, (%rsp)
movq %r14, %rcx
xorl %eax, %eax
callq 0x2050
leaq 0x60(%rsp), %rdi
leaq 0x2b6e(%rip), %rsi      # 0x505f
callq 0x2180
movq %rbp, %r14
testq %rax, %rax
je 0x252d
movq %rax, %rbp
movq 0x10(%rsp), %rdi
movl %r13d, %edx
movl $0x1, %esi
movq %rax, %rcx
callq 0x2250
movq %rbp, %rdi
callq 0x2110
cmpl $0x13, %r14d
leal 0x1(%r14), %eax
setl %bl
movl %eax, %ebp
jmp 0x2530
movq %r14, %rbp
testb %bl, %bl
movq (%rsp), %r14
jne 0x24b3
leaq 0xd0(%rsp), %rdi
callq 0x2b96
xorl %eax, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x2b96
movq %rbx, %rdi
callq 0x2240
nopl (%rax)