// File: /workspace/github_cpp_makefile_star2_O2/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 0x23eb
movq (%rsi), %rsi
leaq 0x1c28(%rip), %rdi      # 0x4004
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rax
jmp 0x2542
movq 0x8(%rsi), %rsi
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
callq 0x256c
movq 0x10(%r14), %rax
subq 0x8(%r14), %rax
pushq $0x14
popq %rcx
cqto
idivq %rcx
leaq 0x1bff(%rip), %rdi      # 0x4015
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
movq 0x2c8(%r14), %rax
leaq 0x34(%rsp), %rbx
movq %rax, 0x1c(%rbx)
movups 0x298(%r14), %xmm0
movups 0x2a8(%r14), %xmm1
movups 0x2b8(%r14), %xmm2
movaps %xmm2, 0xc(%rbx)
movaps %xmm1, -0x4(%rbx)
movaps %xmm0, -0x14(%rbx)
leaq 0x1bc7(%rip), %rdi      # 0x4021
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
leaq 0x1bcf(%rip), %rsi      # 0x403a
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x2180
testl %eax, %eax
je 0x24a2
leaq 0x1bc2(%rip), %rsi      # 0x4043
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x2180
testl %eax, %eax
leaq 0x1bb3(%rip), %rax      # 0x4048
leaq 0x1b9a(%rip), %rbx      # 0x4036
cmoveq %rax, %rbx
jmp 0x24a9
leaq 0x1b96(%rip), %rbx      # 0x403f
leaq 0x8(%rsp), %r14
movl $0x3, (%r14)
andl $0x0, 0x10(%r14)
leaq 0xd0(%rsp), %r15
xorl %r12d, %r12d
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b8e
movl %eax, %ebp
testl %eax, %eax
js 0x2533
je 0x24c5
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x1b65(%rip), %rsi      # 0x404c
movl %r12d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
movq %r13, %rdi
leaq 0x1b61(%rip), %rsi      # 0x405f
callq 0x21c0
testq %rax, %rax
je 0x2533
movq %rax, %r13
movq 0x10(%rsp), %rdi
movl %ebp, %edx
pushq $0x1
popq %rsi
movq %rax, %rcx
callq 0x2280
movq %r13, %rdi
callq 0x2150
leal 0x1(%r12), %eax
cmpl $0x12, %r12d
movl %eax, %r12d
jbe 0x24c5
leaq 0xd0(%rsp), %rdi
callq 0x2b04
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 0x2b04
movq %rbx, %rdi
callq 0x2270