// File: /workspace/github_c_makefile_star2_O1/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, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %rsi, %rax
movq %rdi, %rbx
leaq 0x68(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rsi), %rsi
movq %rax, 0x30(%rsp)
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r15
movq %r15, %rdi
callq 0x6cf2
leaq 0xb169(%rip), %rax      # 0x2ccd0
movq %rax, (%rbx)
leaq 0x8(%rbx), %rdi
leaq 0x18(%rbx), %rax
movq %rax, 0x38(%rsp)
movq %rax, 0x8(%rbx)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
callq 0x6cf2
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x21ba1
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x50c0
leaq 0xb150(%rip), %rax      # 0x2ccf8
movq %rax, (%rbx)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x58(%rbx)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x60(%rbx)
movl %ebp, 0x68(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x40(%rbx)
xorl %eax, %eax
movq %rax, 0x50(%rbx)
movq %rax, 0x40(%rsp)
callq 0x5580
leaq 0x28(%rbx), %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rcx
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x56d0
movq 0x20(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x21c66
movq 0xb3ad(%rip), %r12      # 0x2cfb8
leaq 0x4011(%rip), %rsi      # 0x25c23
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 0xb37a(%rip), %rdi      # 0x2cfb8
callq 0x5440
movq %rax, %rdi
callq 0x53d0
movq 0x30(%rsp), %rax
movq (%rax), %rcx
leaq 0x4002(%rip), %rdx      # 0x25c5c
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x56d0
movq 0x20(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x21f3c
movq 0x10(%rax), %rax
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x24(%rax)
jne 0x21ccd
movq 0xb32b(%rip), %r12      # 0x2cfb8
leaq 0x3fea(%rip), %rsi      # 0x25c7e
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 0xb2f8(%rip), %rdi      # 0x2cfb8
callq 0x5440
movq %rax, %rdi
callq 0x53d0
movq 0x20(%rsp), %rax
movq (%rax), %r13
movq 0x28(%rsp), %rax
movl 0x24(%rax), %ebp
movl %ebp, %edi
callq 0x5260
movq %rax, %r12
testq %rax, %rax
je 0x21f65
movq %r13, %rdi
xorl %esi, %esi
callq 0x5040
leaq 0x30(%rbx), %rcx
movq %rcx, 0x50(%rsp)
movq %rax, (%rcx)
testq %rax, %rax
je 0x21fa0
movl 0x2c(%r13), %ecx
decl %ecx
movl %ecx, 0x4(%rax)
movq %r12, %rdi
callq 0x5730
testq %rax, %rax
je 0x21fc9
movq %rax, 0x38(%rbx)
movq 0x10(%r13), %rcx
testb $0x40, 0x2c(%rcx)
je 0x21d3b
orb $0x40, 0x4e(%rax)
cmpl $0x0, 0x10(%r12)
jne 0x21daa
movq 0x28(%rsp), %rax
movl 0x24(%rax), %eax
movq 0x38(%rbx), %rcx
movl %eax, 0x18(%rcx)
cvttsd2si 0x58(%rbx), %eax
movl %eax, 0x74(%rcx)
cvttsd2si 0x60(%rbx), %eax
movl %eax, 0x78(%rcx)
movl 0x68(%rbx), %eax
movl $0x1, 0x64(%rcx)
movl %eax, 0x68(%rcx)
movq 0x30(%rbx), %rax
movq 0x64(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq 0x38(%rbx), %rax
movq $0xc, 0x84(%rax)
movl 0x18(%rax), %ecx
cmpl $0x2, %ecx
je 0x21d9e
cmpl $0x1, %ecx
jne 0x21daa
movl $0x120, %ecx            # imm = 0x120
jmp 0x21da3
movl $0xa0, %ecx
movl $0x2, (%rax,%rcx)
movq 0x40(%rsp), %rsi
movq 0x38(%rbx), %r13
leaq 0x48(%rsp), %rdi
movq $0x0, (%rdi)
xorl %edx, %edx
callq 0x52c0
leaq 0x48(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x53f0
movl %eax, %ebp
leaq 0x48(%rsp), %rdi
callq 0x51f0
testl %ebp, %ebp
js 0x21ff2
movl 0x88(%r13), %ebp
movl 0x74(%r13), %r14d
movl 0x78(%r13), %r15d
callq 0x53b0
movq %rax, %r12
testq %rax, %rax
je 0x21e4c
movl %ebp, 0x74(%r12)
movl %r14d, 0x68(%r12)
movl %r15d, 0x6c(%r12)
movq %r12, %rdi
xorl %esi, %esi
callq 0x5160
testl %eax, %eax
jns 0x21e4f
movq 0xb18e(%rip), %rdi      # 0x2cfb8
leaq 0x3ff3(%rip), %rsi      # 0x25e24
movl $0x1e, %edx
callq 0x5760
movq 0xb176(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
xorl %r12d, %r12d
movq %r12, 0x48(%rbx)
testq %r12, %r12
je 0x2201b
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq 0xd0(%rax), %rdi
movq %r13, %rsi
callq 0x56a0
testl %eax, %eax
js 0x22044
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rdx
xorl %esi, %esi
movl $0x1, %ecx
callq 0x5210
movq 0x28(%rsp), %rax
testb $0x1, 0x2c(%rax)
jne 0x21ec8
movq 0x20(%rsp), %rax
movq (%rax), %rdi
addq $0x20, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movl $0x2, %edx
callq 0x5200
testl %eax, %eax
js 0x22093
movq 0x20(%rsp), %rax
movq (%rax), %rdi
leaq 0x40(%rsp), %rsi
callq 0x5560
testl %eax, %eax
js 0x2206d
movq 0x38(%rbx), %rax
movl 0x88(%rax), %r9d
cmpl $0x1c, %r9d
je 0x21f2d
movl 0x74(%rax), %ecx
movl 0x78(%rax), %esi
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x4, (%rsp)
movl %ecx, %edi
movl $0x1c, %edx
movl %esi, %r8d
callq 0x5470
movq %rax, 0x50(%rbx)
testq %rax, %rax
je 0x220e0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb075(%rip), %rdi      # 0x2cfb8
leaq 0x3d17(%rip), %rsi      # 0x25c61
movl $0x1c, %edx
callq 0x5760
movq 0xb05d(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
movq 0xb04c(%rip), %rdi      # 0x2cfb8
leaq 0x3df7(%rip), %rsi      # 0x25d6a
movl $0x1b, %edx
callq 0x5760
movl %ebp, %edi
callq 0x52d0
movq 0xb02d(%rip), %rdi      # 0x2cfb8
movq %rax, %rsi
callq 0x5480
movq %rax, %rdi
callq 0x57a0
jmp 0x22104
movq 0xb011(%rip), %rdi      # 0x2cfb8
leaq 0x3dd8(%rip), %rsi      # 0x25d86
movl $0x19, %edx
callq 0x5760
movq 0xaff9(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
movq 0xafe8(%rip), %rdi      # 0x2cfb8
leaq 0x3dc9(%rip), %rsi      # 0x25da0
movl $0x23, %edx
callq 0x5760
movq 0xafd0(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
movq 0xafbf(%rip), %rdi      # 0x2cfb8
leaq 0x3dc4(%rip), %rsi      # 0x25dc4
movl $0x1b, %edx
callq 0x5760
movq 0xafa7(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
movq 0xaf96(%rip), %rdi      # 0x2cfb8
leaq 0x3db7(%rip), %rsi      # 0x25de0
movl $0x1e, %edx
callq 0x5760
movq 0xaf7e(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
movq 0xaf6d(%rip), %rdi      # 0x2cfb8
leaq 0x3dad(%rip), %rsi      # 0x25dff
movl $0x24, %edx
callq 0x5760
movq 0xaf55(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
movq 0xaf44(%rip), %rdi      # 0x2cfb8
leaq 0x3c2a(%rip), %rsi      # 0x25ca5
movl $0x28, %edx
callq 0x5760
movq 0xaf2c(%rip), %rdi      # 0x2cfb8
callq 0x57a0
jmp 0x22104
movq 0xaf1e(%rip), %rdi      # 0x2cfb8
leaq 0x3bf0(%rip), %rsi      # 0x25c91
movl $0x10, %edx
callq 0x5760
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movq 0xaefe(%rip), %rdi      # 0x2cfb8
callq 0x5480
movq %rax, %r14
leaq 0x3bd9(%rip), %rsi      # 0x25ca2
movl $0x2, %edx
movq %rax, %rdi
callq 0x5760
movq %r14, %rdi
callq 0x57a0
jmp 0x22104
movq 0xaed1(%rip), %rdi      # 0x2cfb8
leaq 0x3be0(%rip), %rsi      # 0x25cce
movl $0x2b, %edx
callq 0x5760
movq 0xaeb9(%rip), %rdi      # 0x2cfb8
callq 0x57a0
movl $0x1, %edi
callq 0x5530
movq %rax, %r14
movq 0x58(%rsp), %rdi
movq %r12, 0x38(%rsp)
cmpq %r12, %rdi
jne 0x2213c
jmp 0x2214c
jmp 0x22124
movq %rax, %r14
leaq 0xaba2(%rip), %rax      # 0x2ccd0
movq %rax, (%rbx)
movq 0x8(%rbx), %rdi
cmpq 0x38(%rsp), %rdi
je 0x2214c
movq 0x38(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x50c0
movq %r14, %rdi
callq 0x5510