// File: /workspace/github_cpp_makefile_star2_O1/zhangk145[P]video_envalue/Decoder.cpp
// Function: Decoder::open_codec_context(int*, AVCodecContext**, AVFormatContext*, AVMediaType)
int Decoder::open_codec_context(int *stream_idx,
                              AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type)
{
    int ret, stream_index;
    AVStream *st;
    AVCodec *dec = NULL;
    AVDictionary *opts = NULL;

    ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
    if (ret < 0) {
        fprintf(stderr, "Could not find %s stream\n",
                av_get_media_type_string(type));
        return ret;
    } else {
        stream_index = ret;
        st = fmt_ctx->streams[stream_index];

        /* find decoder for the stream */
        dec = avcodec_find_decoder(st->codecpar->codec_id);
        if (!dec) {
            fprintf(stderr, "Failed to find %s codec\n",
                    av_get_media_type_string(type));
            return AVERROR(EINVAL);
        }

        /* Allocate a codec context for the decoder */
        *dec_ctx = avcodec_alloc_context3(dec);
        if (!*dec_ctx) {
            fprintf(stderr, "Failed to allocate the %s codec context\n",
                    av_get_media_type_string(type));
            return AVERROR(ENOMEM);
        }

        /* Copy codec parameters from input stream to output codec context */
        if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) {
            fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
                    av_get_media_type_string(type));
            return ret;
        }

        /* Init the decoders, with or without reference counting */
        // av_dict_set(&opts, "refcounted_frames", mRefcount ? "1" : "0", 0);
        if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) {
            fprintf(stderr, "Failed to open %s codec\n",
                    av_get_media_type_string(type));
            return ret;
        }
        *stream_idx = stream_index;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebx
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r14
movq $0x0, (%rsp)
movq %rcx, %rdi
movl %r8d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2090
movl %eax, %ebp
testl %eax, %eax
js 0x28b1
movq 0x30(%r12), %rax
movl %ebp, %ecx
movq (%rax,%rcx,8), %r13
movq 0xd0(%r13), %rax
movl 0x4(%rax), %edi
callq 0x22c0
testq %rax, %rax
je 0x28ba
movq %rax, %r12
movq %rax, %rdi
callq 0x2200
movq %rax, (%r15)
testq %rax, %rax
je 0x28c8
movq 0xd0(%r13), %rsi
movq %rax, %rdi
callq 0x2280
testl %eax, %eax
js 0x28d6
movq (%r15), %rdi
movq %rsp, %rdx
movq %r12, %rsi
callq 0x2130
testl %eax, %eax
js 0x28e5
movl %ebp, (%r14)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %edi
callq 0x2391
jmp 0x28a0
movl %ebx, %edi
callq 0x236c
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
jmp 0x28a0
movl %ebx, %edi
callq 0x2347
movl $0xfffffff4, %ebp       # imm = 0xFFFFFFF4
jmp 0x28a0
movl %eax, %r13d
movl %ebx, %edi
callq 0x2322
movl %r13d, %ebp
jmp 0x28a0
movl %eax, %r15d
movl %ebx, %edi
callq 0x22fc
movl %r15d, %ebp
jmp 0x28a0