// File: /workspace/github_c_makefile_star2_O3/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 %rsi, %rax
movq %rdi, %rbx
leaq 0x68(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rsi), %rsi
movq %rax, 0x20(%rsp)
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r15
movq %r15, %rdi
callq 0x6c58
leaq 0xac01(%rip), %rax      # 0x2ccd0
movq %rax, (%rbx)
leaq 0x8(%rbx), %rdi
leaq 0x18(%rbx), %rax
movq %rax, 0x28(%rsp)
movq %rax, 0x8(%rbx)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
callq 0x6c58
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x22109
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x50c0
leaq 0xabe8(%rip), %rax      # 0x2ccf8
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)
movupd %xmm0, 0x40(%rbx)
xorl %eax, %eax
movq %rax, 0x50(%rbx)
movq %rax, 0x30(%rsp)
callq 0x5570
leaq 0x28(%rbx), %r15
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x56d0
movq (%r15), %r13
testq %r13, %r13
jne 0x221d6
movq 0xae47(%rip), %r12      # 0x2cfb8
leaq 0x3beb(%rip), %rsi      # 0x25d63
movl $0x38, %edx
movq %r12, %rdi
callq 0x5760
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x57d0
movsbl %al, %esi
movq 0xae14(%rip), %rdi      # 0x2cfb8
callq 0x5430
movq %rax, %rdi
callq 0x53c0
movq 0x20(%rsp), %rax
movq (%rax), %rcx
leaq 0x3bdc(%rip), %rdx      # 0x25d9c
movq %r15, %rdi
xorl %esi, %esi
callq 0x56d0
movq (%r15), %r13
testq %r13, %r13
je 0x2261a
movq 0x10(%r13), %r14
movl 0x24(%r14), %ebp
testl %ebp, %ebp
jne 0x22230
movq 0xadcf(%rip), %r12      # 0x2cfb8
leaq 0x3bce(%rip), %rsi      # 0x25dbe
movl $0x12, %edx
movq %r12, %rdi
callq 0x5760
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x57d0
movsbl %al, %esi
movq 0xad9c(%rip), %rdi      # 0x2cfb8
callq 0x5430
movq %rax, %rdi
callq 0x53c0
movq (%r15), %r13
movl 0x24(%r14), %ebp
movl %ebp, %edi
callq 0x5250
movq %rax, %r12
testq %rax, %rax
je 0x22445
movq %r13, %rdi
xorl %esi, %esi
callq 0x5040
leaq 0x30(%rbx), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x22480
movl 0x2c(%r13), %ecx
decl %ecx
movl %ecx, 0x4(%rax)
movq %r12, %rdi
callq 0x5730
testq %rax, %rax
je 0x224a9
movq %rax, 0x38(%rbx)
movq 0x10(%r13), %rcx
testb $0x40, 0x2c(%rcx)
je 0x22289
orb $0x40, 0x4e(%rax)
cmpl $0x0, 0x10(%r12)
jne 0x222ee
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 0x222e2
cmpl $0x1, %ecx
jne 0x222ee
movl $0x120, %ecx            # imm = 0x120
jmp 0x222e7
movl $0xa0, %ecx
movl $0x2, (%rax,%rcx)
movq 0x30(%rsp), %rsi
movq 0x38(%rbx), %r13
leaq 0x38(%rsp), %rdi
movq $0x0, (%rdi)
xorl %edx, %edx
callq 0x52b0
leaq 0x38(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x53e0
movl %eax, %ebp
leaq 0x38(%rsp), %rdi
callq 0x51e0
testl %ebp, %ebp
js 0x224d2
movl 0x88(%r13), %ebp
movsd 0x74(%r13), %xmm0
movapd %xmm0, 0x40(%rsp)
callq 0x53a0
movq %rax, %r12
testq %rax, %rax
je 0x224fb
movl %ebp, 0x74(%r12)
movapd 0x40(%rsp), %xmm0
movlpd %xmm0, 0x68(%r12)
movq %r12, %rdi
xorl %esi, %esi
callq 0x5160
testl %eax, %eax
js 0x2252c
movq %r12, 0x48(%rbx)
movq 0x30(%rbx), %rax
movq 0xd0(%rax), %rdi
movq %r13, %rsi
callq 0x56a0
testl %eax, %eax
js 0x22555
movq (%r15), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rdx
xorl %esi, %esi
movl $0x1, %ecx
callq 0x5200
testb $0x1, 0x2c(%r14)
jne 0x223d4
movq (%r15), %rdi
addq $0x20, %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movl $0x2, %edx
callq 0x51f0
testl %eax, %eax
js 0x225a7
movq (%r15), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5550
testl %eax, %eax
js 0x2257e
movq 0x38(%rbx), %rax
movl 0x88(%rax), %r9d
cmpl $0x1c, %r9d
je 0x22436
movl 0x74(%rax), %ecx
movl 0x78(%rax), %esi
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x4, (%rsp)
movl %ecx, %edi
movl $0x1c, %edx
movl %esi, %r8d
callq 0x5460
movq %rax, 0x50(%rbx)
testq %rax, %rax
je 0x225f4
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xab6c(%rip), %rdi      # 0x2cfb8
leaq 0x3a57(%rip), %rsi      # 0x25eaa
movl $0x1b, %edx
callq 0x5760
movl %ebp, %edi
callq 0x52c0
movq 0xab4d(%rip), %rdi      # 0x2cfb8
movq %rax, %rsi
callq 0x5470
movq %rax, %rdi
callq 0x57a0
jmp 0x2263e
movq 0xab31(%rip), %rdi      # 0x2cfb8
leaq 0x3a38(%rip), %rsi      # 0x25ec6
movl $0x19, %edx
callq 0x5760
movq 0xab19(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq 0xab08(%rip), %rdi      # 0x2cfb8
leaq 0x3a29(%rip), %rsi      # 0x25ee0
movl $0x23, %edx
callq 0x5760
movq 0xaaf0(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq 0xaadf(%rip), %rdi      # 0x2cfb8
leaq 0x3a24(%rip), %rsi      # 0x25f04
movl $0x1b, %edx
callq 0x5760
movq 0xaac7(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq $0x0, 0x48(%rbx)
movq 0xaaae(%rip), %rdi      # 0x2cfb8
leaq 0x3a0f(%rip), %rsi      # 0x25f20
movl $0x1e, %edx
callq 0x5760
movq 0xaa96(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq 0xaa85(%rip), %rdi      # 0x2cfb8
leaq 0x3a2a(%rip), %rsi      # 0x25f64
movl $0x1e, %edx
callq 0x5760
movq 0xaa6d(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq 0xaa5c(%rip), %rdi      # 0x2cfb8
leaq 0x39dc(%rip), %rsi      # 0x25f3f
movl $0x24, %edx
callq 0x5760
movq 0xaa44(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq 0xaa33(%rip), %rdi      # 0x2cfb8
leaq 0x3859(%rip), %rsi      # 0x25de5
movl $0x28, %edx
callq 0x5760
movq 0xaa1b(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq 0xaa0a(%rip), %rdi      # 0x2cfb8
leaq 0x381c(%rip), %rsi      # 0x25dd1
movl $0x10, %edx
callq 0x5760
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0xa9ea(%rip), %rdi      # 0x2cfb8
callq 0x5470
movq %rax, %r14
leaq 0x3805(%rip), %rsi      # 0x25de2
movl $0x2, %edx
movq %rax, %rdi
callq 0x5760
movq %r14, %rdi
callq 0x57a0
jmp 0x2263e
movq 0xa9bd(%rip), %rdi      # 0x2cfb8
leaq 0x380c(%rip), %rsi      # 0x25e0e
movl $0x2b, %edx
callq 0x5760
movq 0xa9a5(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x2263e
movq 0xa997(%rip), %rdi      # 0x2cfb8
leaq 0x3779(%rip), %rsi      # 0x25da1
movl $0x1c, %edx
callq 0x5760
movq 0xa97f(%rip), %rdi      # 0x2cfb8
callq 0x57a0
movl $0x1, %edi
callq 0x5520
movq %rax, %r14
movq 0x58(%rsp), %rdi
movq %r12, 0x28(%rsp)
cmpq %r12, %rdi
jne 0x22676
jmp 0x22686
jmp 0x2265e
movq %rax, %r14
leaq 0xa668(%rip), %rax      # 0x2ccd0
movq %rax, (%rbx)
movq 0x8(%rbx), %rdi
cmpq 0x28(%rsp), %rdi
je 0x22686
movq 0x28(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x50c0
movq %r14, %rdi
callq 0x5500