// File: /workspace/github_cpp_makefile_star2_O3/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 0x2530
movq 0x8(%rsi), %rsi
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
callq 0x255c
movq 0x10(%rbx), %rax
subq 0x8(%rbx), %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %esi # imm = 0xCCCCCCCD
leaq 0x2c0b(%rip), %rdi      # 0x5015
xorl %eax, %eax
callq 0x2040
movq 0x2c8(%rbx), %rax
leaq 0x34(%rsp), %r14
movq %rax, 0x1c(%r14)
movups 0x298(%rbx), %xmm0
movups 0x2a8(%rbx), %xmm1
movups 0x2b8(%rbx), %xmm2
movaps %xmm2, 0xc(%r14)
movaps %xmm1, -0x4(%r14)
movaps %xmm0, -0x14(%r14)
leaq 0x2bd5(%rip), %rdi      # 0x5021
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movl $0x47504a4d, %eax       # imm = 0x47504A4D
xorl (%r14), %eax
movzbl 0x4(%r14), %ecx
orl %eax, %ecx
je 0x248b
movl $0x474e504d, %eax       # imm = 0x474E504D
xorl 0x34(%rsp), %eax
movzbl 0x38(%rsp), %ecx
orl %eax, %ecx
leaq 0x2bca(%rip), %rax      # 0x5048
leaq 0x2bb1(%rip), %rbx      # 0x5036
cmoveq %rax, %rbx
jmp 0x2492
leaq 0x2bad(%rip), %rbx      # 0x503f
leaq 0x8(%rsp), %r14
movl $0x3, (%r14)
movl $0x0, 0x10(%r14)
leaq 0xd0(%rsp), %r15
xorl %r12d, %r12d
movq %r15, %rdi
movq %r14, %rsi
callq 0x2c0e
movl %eax, %ebp
testl %eax, %eax
js 0x2521
je 0x24b1
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x2b79(%rip), %rsi      # 0x504c
movl %r12d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
movq %r13, %rdi
leaq 0x2b75(%rip), %rsi      # 0x505f
callq 0x2180
testq %rax, %rax
je 0x2521
movq %rax, %r13
movq 0x10(%rsp), %rdi
movl %ebp, %edx
movl $0x1, %esi
movq %rax, %rcx
callq 0x2250
movq %r13, %rdi
callq 0x2110
leal 0x1(%r12), %eax
cmpl $0x12, %r12d
movl %eax, %r12d
jbe 0x24b1
leaq 0xd0(%rsp), %rdi
callq 0x2b7a
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 0x2b7a
movq %rbx, %rdi
callq 0x2240
nop