// File: /workspace/github_c_makefile_star2_O2/algorithm-archivists[P]GathVL/src/encoders/video_encoder.cpp
// Function: video_encoder::video_encoder(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, vec, int)
video_encoder::video_encoder(std::string url, vec vid_size, int frame_rate)
    : encoder(url), size(vid_size), frames_per_sec(frame_rate) {
    AVOutputFormat *format;
    ostream = {0};
    AVCodec *codec;
    AVDictionary *opt = nullptr;

    av_register_all();

    avformat_alloc_output_context2(&format_ctx, NULL, NULL, url.c_str());
    if (!format_ctx) {
        std::cout << "Could not deduce format from file extension: using MPEG."
            << std::endl;
        avformat_alloc_output_context2(&format_ctx, NULL, "mpeg", url.c_str());
    }

    if (!format_ctx) {
        std::cout << "Can not make format context." << std::endl;
        std::exit(1);
    }

    format = format_ctx->oformat;

    if (format->video_codec == AV_CODEC_ID_NONE) {
        std::cout << "No video codec ID." << std::endl;
    }

    add_stream(&ostream, format_ctx, &codec, format->video_codec);

    if (codec->type == AVMEDIA_TYPE_VIDEO) {
        ostream.codec_ctx->codec_id = format->video_codec;
        ostream.codec_ctx->width = static_cast<int>(size.x);
        ostream.codec_ctx->height = static_cast<int>(size.y);
        ostream.codec_ctx->time_base = {1, frames_per_sec};
        ostream.stream->time_base = ostream.codec_ctx->time_base;
        ostream.codec_ctx->gop_size = 12;
        ostream.codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;

        if (ostream.codec_ctx->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
            ostream.codec_ctx->max_b_frames = 2;
        }

        if (ostream.codec_ctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
            ostream.codec_ctx->mb_decision = 2;
        }
    }

    open_video(format_ctx, codec, &ostream, opt);

    av_dump_format(format_ctx, 0, url.c_str(), 1);

    int ret = 0;
    if (!(format->flags & AVFMT_NOFILE)) {
        ret = avio_open(&format_ctx->pb, url.c_str(), AVIO_FLAG_WRITE);
        if (ret < 0) {
            std::cout << "Could not open '" << url.c_str() << "'." << std::endl;
            std::exit(1);
        }
    }

    ret = avformat_write_header(format_ctx, &opt);
    if (ret < 0) {
        std::cout << "Error occurred when opening output file." << std::endl;
        std::exit(1);
    }

    if (ostream.codec_ctx->pix_fmt != AV_PIX_FMT_BGRA) {
        ostream.sws_ctx = sws_getContext(ostream.codec_ctx->width,
                                         ostream.codec_ctx->height,
                                         AV_PIX_FMT_BGRA,
                                         ostream.codec_ctx->width,
                                         ostream.codec_ctx->height,
                                         ostream.codec_ctx->pix_fmt,
                                         SWS_BICUBIC, NULL, NULL, NULL);
        if (!ostream.sws_ctx) {
            std::cout << "Could not initialize the conversion context"
                << std::endl;
            std::exit(1);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %edx, %ebp
movsd %xmm1, 0x40(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rdi, %rbx
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %rsi, 0x28(%rsp)
callq 0x5440
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1974e
leaq 0x58(%rsp), %rdi
callq 0x5550
leaq 0xc359(%rip), %rax      # 0x25d08
movq %rax, (%rbx)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x58(%rbx)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x60(%rbx)
movl %ebp, 0x68(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rbx)
andq $0x0, 0x50(%rbx)
movupd %xmm0, 0x40(%rbx)
andq $0x0, 0x30(%rsp)
callq 0x5590
leaq 0x28(%rbx), %r15
movq 0x28(%rsp), %rax
movq (%rax), %rcx
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x56f0
movq (%r15), %r13
testq %r13, %r13
jne 0x19a49
movq 0xc5b0(%rip), %rdi      # 0x25fc0
leaq 0x41ac(%rip), %rsi      # 0x1dbc3
callq 0x5480
movq %rax, %rdi
callq 0x57b0
movq 0x28(%rsp), %rax
movq (%rax), %rcx
leaq 0x41c9(%rip), %rdx      # 0x1dbfc
movq %r15, %rdi
xorl %esi, %esi
callq 0x56f0
movq (%r15), %r13
testq %r13, %r13
je 0x19df0
movq 0x10(%r13), %r14
movl 0x24(%r14), %ebp
testl %ebp, %ebp
jne 0x19a77
movq 0xc564(%rip), %rdi      # 0x25fc0
leaq 0x41bb(%rip), %rsi      # 0x1dc1e
callq 0x5480
movq %rax, %rdi
callq 0x57b0
movq (%r15), %r13
movl 0x24(%r14), %ebp
movl %ebp, %edi
callq 0x5260
movq %rax, %r12
testq %rax, %rax
je 0x19c80
movq %r13, %rdi
xorl %esi, %esi
callq 0x5040
leaq 0x30(%rbx), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x19cb5
movl 0x2c(%r13), %ecx
decl %ecx
movl %ecx, 0x4(%rax)
movq %r12, %rdi
callq 0x5750
testq %rax, %rax
je 0x19cd5
movq %rax, 0x38(%rbx)
movq 0x10(%r13), %rcx
testb $0x40, 0x2c(%rcx)
je 0x19ad0
orb $0x40, 0x4e(%rax)
cmpl $0x0, 0x10(%r12)
jne 0x19b35
movl 0x24(%r14), %ecx
movl %ecx, 0x18(%rax)
movupd 0x58(%rbx), %xmm0
cvttpd2dq %xmm0, %xmm0
movlpd %xmm0, 0x74(%rax)
movl 0x68(%rbx), %ecx
movl $0x1, 0x64(%rax)
movl %ecx, 0x68(%rax)
movq 0x30(%rbx), %rcx
movq 0x64(%rax), %rax
movq %rax, 0x18(%rcx)
movq 0x38(%rbx), %rax
movq $0xc, 0x84(%rax)
movl 0x18(%rax), %ecx
cmpl $0x2, %ecx
je 0x19b29
cmpl $0x1, %ecx
jne 0x19b35
movl $0x120, %ecx            # imm = 0x120
jmp 0x19b2e
movl $0xa0, %ecx
movl $0x2, (%rax,%rcx)
movq 0x30(%rsp), %rsi
movq 0x38(%rbx), %r13
leaq 0x38(%rsp), %rdi
andq $0x0, (%rdi)
xorl %edx, %edx
callq 0x52c0
leaq 0x38(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x53f0
movl %eax, %ebp
leaq 0x38(%rsp), %rdi
callq 0x51e0
testl %ebp, %ebp
js 0x19cf5
movl 0x88(%r13), %ebp
movsd 0x74(%r13), %xmm0
movapd %xmm0, 0x40(%rsp)
callq 0x53c0
movq %rax, %r12
testq %rax, %rax
je 0x19d15
movl %ebp, 0x74(%r12)
movapd 0x40(%rsp), %xmm0
movlpd %xmm0, 0x68(%r12)
movq %r12, %rdi
xorl %esi, %esi
callq 0x5160
testl %eax, %eax
js 0x19d3a
movq %r12, 0x48(%rbx)
movq 0x30(%rbx), %rax
movq 0xd0(%rax), %rdi
movq %r13, %rsi
callq 0x56c0
testl %eax, %eax
js 0x19d5a
movq (%r15), %rdi
movq 0x28(%rsp), %rax
movq (%rax), %rdx
pushq $0x1
popq %rcx
xorl %esi, %esi
callq 0x5200
testb $0x1, 0x2c(%r14)
jne 0x19c14
movq (%r15), %rdi
addq $0x20, %rdi
movq 0x28(%rsp), %rax
movq (%rax), %rsi
pushq $0x2
popq %rdx
callq 0x51f0
testl %eax, %eax
js 0x19d97
movq (%r15), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5560
testl %eax, %eax
js 0x19d7a
movq 0x38(%rbx), %rax
movl 0x88(%rax), %r9d
cmpl $0x1c, %r9d
je 0x19c71
movl 0x74(%rax), %ecx
movl 0x78(%rax), %esi
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
andq $0x0, 0x18(%rsp)
movl $0x4, (%rsp)
pushq $0x1c
popq %rdx
movl %ecx, %edi
movl %esi, %r8d
callq 0x5470
movq %rax, 0x50(%rbx)
testq %rax, %rax
je 0x19dd3
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xc339(%rip), %rdi      # 0x25fc0
leaq 0x407c(%rip), %rsi      # 0x1dd0a
callq 0x5480
movq %rax, %r14
movl %ebp, %edi
callq 0x52e0
movq %r14, %rdi
movq %rax, %rsi
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc304(%rip), %rdi      # 0x25fc0
leaq 0x4063(%rip), %rsi      # 0x1dd26
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc2e4(%rip), %rdi      # 0x25fc0
leaq 0x405d(%rip), %rsi      # 0x1dd40
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc2c4(%rip), %rdi      # 0x25fc0
leaq 0x4061(%rip), %rsi      # 0x1dd64
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
andq $0x0, 0x48(%rbx)
movq 0xc29f(%rip), %rdi      # 0x25fc0
leaq 0x4058(%rip), %rsi      # 0x1dd80
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc27f(%rip), %rdi      # 0x25fc0
leaq 0x407c(%rip), %rsi      # 0x1ddc4
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc25f(%rip), %rdi      # 0x25fc0
leaq 0x4037(%rip), %rsi      # 0x1dd9f
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc23f(%rip), %rdi      # 0x25fc0
leaq 0x3ebd(%rip), %rsi      # 0x1dc45
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc222(%rip), %rdi      # 0x25fc0
leaq 0x3e8c(%rip), %rsi      # 0x1dc31
callq 0x5480
movq 0x28(%rsp), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x5480
leaq 0x3e81(%rip), %rsi      # 0x1dc42
movq %rax, %rdi
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc1e6(%rip), %rdi      # 0x25fc0
leaq 0x3e8d(%rip), %rsi      # 0x1dc6e
callq 0x5480
movq %rax, %rdi
callq 0x57b0
jmp 0x19e0b
movq 0xc1c9(%rip), %rdi      # 0x25fc0
leaq 0x3e03(%rip), %rsi      # 0x1dc01
callq 0x5480
movq %rax, %rdi
callq 0x57b0
pushq $0x1
popq %rdi
callq 0x5520
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x5550
jmp 0x19e2f
jmp 0x19e24
movq %rax, %r14
movq %rbx, %rdi
callq 0x19762
movq %r14, %rdi
callq 0x5500
nop