// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rsi, -0xc8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x8(%rbp)
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x50e0
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x38e00
jmp 0x391f7
leaq -0x48(%rbp), %rdi
callq 0x5830
movq -0xc0(%rbp), %rax
leaq 0x15afa(%rip), %rcx     # 0x4ed08
movq %rcx, (%rax)
movups -0x10(%rbp), %xmm0
movups %xmm0, 0x58(%rax)
movl -0x24(%rbp), %ecx
movl %ecx, 0x68(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rcx
movq %rcx, 0x50(%rax)
movaps -0x90(%rbp), %xmm0
movaps -0x80(%rbp), %xmm1
movups %xmm1, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movq $0x0, -0xa0(%rbp)
callq 0x5300
jmp 0x39262
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rax
addq $0x28, %rax
movq %rax, -0xd0(%rbp)
callq 0x5530
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x58f0
jmp 0x39298
movq -0xc0(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x3934e
movq 0x15cf7(%rip), %rdi     # 0x4efa8
leaq 0x448b(%rip), %rsi      # 0x3d743
callq 0x5780
movq %rax, -0xd8(%rbp)
jmp 0x392c6
movq -0xd8(%rbp), %rdi
movq 0x15cec(%rip), %rsi     # 0x4efc0
callq 0x5960
jmp 0x392db
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rax
addq $0x28, %rax
movq %rax, -0xe0(%rbp)
callq 0x5530
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
leaq 0x4472(%rip), %rdx      # 0x3d77c
xorl %eax, %eax
movl %eax, %esi
callq 0x58f0
jmp 0x39315
jmp 0x3934e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x5830
jmp 0x39760
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0x38e90
jmp 0x39760
movq -0xc0(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x39397
movq 0x15c45(%rip), %rdi     # 0x4efa8
leaq 0x4417(%rip), %rsi      # 0x3d781
callq 0x5780
movq %rax, -0xe8(%rbp)
jmp 0x39378
movq -0xe8(%rbp), %rdi
movq 0x15c3a(%rip), %rsi     # 0x4efc0
callq 0x5960
jmp 0x3938d
movl $0x1, %edi
callq 0x57d0
movq -0xc0(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpl $0x0, 0x24(%rax)
jne 0x393e7
movq 0x15bed(%rip), %rdi     # 0x4efa8
leaq 0x43dc(%rip), %rsi      # 0x3d79e
callq 0x5780
movq %rax, -0xf0(%rbp)
jmp 0x393d0
movq -0xf0(%rbp), %rdi
movq 0x15be2(%rip), %rsi     # 0x4efc0
callq 0x5960
jmp 0x393e5
jmp 0x393e7
movq -0xc0(%rbp), %rax
movq %rax, %rdi
addq $0x30, %rdi
movq 0x28(%rax), %rsi
movq -0x60(%rbp), %rax
movl 0x24(%rax), %ecx
leaq -0x98(%rbp), %rdx
callq 0x39770
jmp 0x3940e
movq -0x98(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x394e1
movq -0xc0(%rbp), %rax
movq -0x60(%rbp), %rcx
movl 0x24(%rcx), %edx
movq 0x38(%rax), %rcx
movl %edx, 0x18(%rcx)
cvttsd2si 0x58(%rax), %edx
movq 0x38(%rax), %rcx
movl %edx, 0x74(%rcx)
cvttsd2si 0x60(%rax), %edx
movq 0x38(%rax), %rcx
movl %edx, 0x78(%rcx)
movl $0x1, -0xa8(%rbp)
movl 0x68(%rax), %ecx
movl %ecx, -0xa4(%rbp)
movq 0x38(%rax), %rcx
movq -0xa8(%rbp), %rdx
movq %rdx, 0x64(%rcx)
movq 0x38(%rax), %rdx
movq 0x30(%rax), %rcx
movq 0x64(%rdx), %rdx
movq %rdx, 0x18(%rcx)
movq 0x38(%rax), %rcx
movl $0xc, 0x84(%rcx)
movq 0x38(%rax), %rcx
movl $0x0, 0x88(%rcx)
movq 0x38(%rax), %rax
cmpl $0x2, 0x18(%rax)
jne 0x394b9
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
movl $0x2, 0xa0(%rax)
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
cmpl $0x1, 0x18(%rax)
jne 0x394df
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
movl $0x2, 0x120(%rax)
jmp 0x394e1
movq -0xc0(%rbp), %rdx
movq 0x28(%rdx), %rdi
movq -0x98(%rbp), %rsi
addq $0x30, %rdx
movq -0xa0(%rbp), %rcx
callq 0x398c0
jmp 0x39505
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xf8(%rbp)
callq 0x5530
movq -0xf8(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
movl $0x1, %ecx
callq 0x5620
jmp 0x3953b
movl $0x0, -0xac(%rbp)
movq -0x60(%rbp), %rax
movl 0x2c(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x39628
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rax
movq 0x28(%rax), %rax
addq $0x20, %rax
movq %rax, -0x108(%rbp)
callq 0x5530
movq -0x108(%rbp), %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x5180
movl %eax, -0xfc(%rbp)
jmp 0x39596
movl -0xfc(%rbp), %eax
movl %eax, -0xac(%rbp)
cmpl $0x0, -0xac(%rbp)
jge 0x39626
movq 0x159f6(%rip), %rdi     # 0x4efa8
leaq 0x41f8(%rip), %rsi      # 0x3d7b1
callq 0x5780
movq %rax, -0x110(%rbp)
jmp 0x395c7
movq -0xc8(%rbp), %rdi
callq 0x5530
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x5780
movq %rax, -0x118(%rbp)
jmp 0x395eb
movq -0x118(%rbp), %rdi
leaq 0x41c9(%rip), %rsi      # 0x3d7c2
callq 0x5780
movq %rax, -0x120(%rbp)
jmp 0x39607
movq -0x120(%rbp), %rdi
movq 0x159ab(%rip), %rsi     # 0x4efc0
callq 0x5960
jmp 0x3961c
movl $0x1, %edi
callq 0x57d0
jmp 0x39628
movq -0xc0(%rbp), %rax
movq 0x28(%rax), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x52e0
movl %eax, -0x124(%rbp)
jmp 0x39647
movl -0x124(%rbp), %eax
movl %eax, -0xac(%rbp)
cmpl $0x0, -0xac(%rbp)
jge 0x39697
movq 0x15945(%rip), %rdi     # 0x4efa8
leaq 0x415b(%rip), %rsi      # 0x3d7c5
callq 0x5780
movq %rax, -0x130(%rbp)
jmp 0x39678
movq -0x130(%rbp), %rdi
movq 0x1593a(%rip), %rsi     # 0x4efc0
callq 0x5960
jmp 0x3968d
movl $0x1, %edi
callq 0x57d0
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
cmpl $0x1c, 0x88(%rax)
je 0x39757
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
movl 0x74(%rax), %ecx
movl 0x78(%rax), %r8d
movl 0x88(%rax), %r9d
movq %rsp, %rax
movq $0x0, 0x18(%rax)
movq $0x0, 0x10(%rax)
movq $0x0, 0x8(%rax)
movl $0x4, (%rax)
movl $0x1c, %edx
movl %ecx, %edi
movl %r8d, %esi
callq 0x5760
movq %rax, -0x138(%rbp)
jmp 0x39701
movq -0xc0(%rbp), %rax
movq -0x138(%rbp), %rcx
movq %rcx, 0x50(%rax)
cmpq $0x0, 0x50(%rax)
jne 0x39755
movq 0x15887(%rip), %rdi     # 0x4efa8
leaq 0x40c6(%rip), %rsi      # 0x3d7ee
callq 0x5780
movq %rax, -0x140(%rbp)
jmp 0x39736
movq -0x140(%rbp), %rdi
movq 0x1587c(%rip), %rsi     # 0x4efc0
callq 0x5960
jmp 0x3974b
movl $0x1, %edi
callq 0x57d0
jmp 0x39757
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x52d0
nopl (%rax)