// File: /workspace/github_cpp_makefile_star2_O2/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
andq $0x0, (%rsp)
pushq $-0x1
popq %rcx
movq %r12, %rdi
movl %r8d, %esi
movl %ecx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2090
movl %eax, %ebp
testl %eax, %eax
js 0x25f1
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 0x2618
movq %rax, %r12
movq %rax, %rdi
callq 0x2200
movq %rax, (%r15)
testq %rax, %rax
je 0x2641
movq 0xd0(%r13), %rsi
movq %rax, %rdi
callq 0x2280
testl %eax, %eax
js 0x266e
movq (%r15), %rdi
movq %rsp, %rdx
movq %r12, %rsi
callq 0x2130
testl %eax, %eax
js 0x269e
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
movq 0x39e8(%rip), %rax      # 0x5fe0
movq (%rax), %r14
movl %ebx, %edi
callq 0x2270
leaq 0x1a6c(%rip), %rsi      # 0x4075
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2080
jmp 0x25e0
movq 0x39c1(%rip), %rax      # 0x5fe0
movq (%rax), %r14
movl %ebx, %edi
callq 0x2270
leaq 0x1a5f(%rip), %rsi      # 0x408f
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2080
pushq $-0x16
jmp 0x2668
movq 0x3998(%rip), %rax      # 0x5fe0
movq (%rax), %r14
movl %ebx, %edi
callq 0x2270
leaq 0x1a4f(%rip), %rsi      # 0x40a8
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2080
pushq $-0xc
popq %rbp
jmp 0x25e0
movl %eax, %r13d
movq 0x3968(%rip), %rax      # 0x5fe0
movq (%rax), %r14
movl %ebx, %edi
callq 0x2270
leaq 0x1a48(%rip), %rsi      # 0x40d1
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2080
movl %r13d, %ebp
jmp 0x25e0
movl %eax, %r15d
movq 0x3938(%rip), %rax      # 0x5fe0
movq (%rax), %r14
movl %ebx, %edi
callq 0x2270
leaq 0x1a4f(%rip), %rsi      # 0x4108
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2080
movl %r15d, %ebp
jmp 0x25e0