// File: /workspace/github_cpp_makefile_star2_O3/Themaister[P]umusd/ffmpeg.cpp
// Function: FF::decode()
const FF::Buffer& FF::decode()
{
   buffer.clear();
   AVPacket pkt;
   AVFrame frame;
   int got_ptr = 0;
   unsigned retry_cnt = 0;

   while (!got_ptr)
   {
      if (av_read_frame(fctx, &pkt) < 0)
      {
         std::cerr << "av_read_frame() failed." << std::endl;
         return buffer;
      }

      if (pkt.stream_index != aud_stream)
      {
         av_free_packet(&pkt);
         continue;
      }

      if (avcodec_decode_audio4(actx, &frame, &got_ptr, &pkt) < 0)
      {
         std::cerr << "avcodec_decode_audio4() failed." << std::endl;
         av_free_packet(&pkt);

         if (retry_cnt++ < 4)
            continue;

         return buffer;
      }

      av_free_packet(&pkt);
   }

   int size = av_samples_get_buffer_size(nullptr, actx->channels,
         frame.nb_samples, actx->sample_fmt, 1);

   if (pkt.pts != static_cast<std::int64_t>(AV_NOPTS_VALUE))
      last_pos = pkt.pts * av_q2d(fctx->streams[aud_stream]->time_base);

   if (planar_audio)
   {
      std::size_t current_size = buffer.size();
      buffer.resize(buffer.size() + size);
      std::uint8_t* out_ptr = buffer.data() + current_size;

      switch (actx->sample_fmt)
      {
         case AV_SAMPLE_FMT_S16P:
            copy_deplanar<int16_t>(out_ptr, frame.data, frame.nb_samples, actx->channels);
            break;

         case AV_SAMPLE_FMT_S32P:
            copy_deplanar<int32_t>(out_ptr, frame.data, frame.nb_samples, actx->channels);
            break;

         case AV_SAMPLE_FMT_FLTP:
            copy_deplanar<float>(out_ptr, frame.data, frame.nb_samples, actx->channels);
            break;

         default:
            break;
      }
   }
   else
      buffer.insert(buffer.end(), frame.data[0], frame.data[0] + size);

   return buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdi, %r15
movq 0x88(%rdi), %rax
cmpq %rax, 0x90(%rdi)
je 0xab15
movq %rax, 0x90(%r15)
leaq 0x88(%r15), %rax
movq %rax, 0x8(%rsp)
leaq 0x14(%rsp), %r12
movl $0x0, (%r12)
xorl %ebx, %ebx
leaq 0x18(%rsp), %r13
leaq 0x70(%rsp), %rbp
movq 0xb45f(%rip), %r14      # 0x15fa0
movq (%r15), %rdi
movq %r13, %rsi
callq 0x5510
testl %eax, %eax
js 0xabd8
movl 0x3c(%rsp), %eax
cmpl 0x10(%r15), %eax
jne 0xab74
movq 0x8(%r15), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x5110
testl %eax, %eax
js 0xab88
movq %r13, %rdi
callq 0x5350
cmpl $0x0, 0x14(%rsp)
je 0xab41
jmp 0xac18
movl $0x1f, %edx
movq %r14, %rdi
leaq 0x4b5b(%rip), %rsi      # 0xf6f2
callq 0x52b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5030
movsbl %al, %esi
movq %r14, %rdi
callq 0x54a0
movq %rax, %rdi
callq 0x5450
movq %r13, %rdi
callq 0x5350
cmpl $0x4, %ebx
jae 0xad34
incl %ebx
jmp 0xab7c
leaq 0x4afb(%rip), %rsi      # 0xf6da
movl $0x17, %edx
movq %r14, %rdi
callq 0x52b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5030
movsbl %al, %esi
movq %r14, %rdi
callq 0x54a0
movq %rax, %rdi
callq 0x5450
jmp 0xad34
movq 0x8(%r15), %rax
movl 0xe0(%rsp), %edx
movl 0x194(%rax), %esi
movl 0x198(%rax), %ecx
xorl %edi, %edi
movl $0x1, %r8d
callq 0x51a0
movq 0x20(%rsp), %rcx
movq %rcx, %rdx
negq %rdx
jo 0xac82
cvtsi2sd %rcx, %xmm0
movq (%r15), %rcx
movq 0x30(%rcx), %rcx
movslq 0x10(%r15), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x18(%rcx), %rcx
cvtsi2sd %ecx, %xmm1
shrq $0x20, %rcx
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x14(%r15)
movq 0x90(%r15), %r14
cmpb $0x1, 0xa0(%r15)
jne 0xad1c
subq 0x88(%r15), %r14
movslq %eax, %rsi
addq %r14, %rsi
movq 0x8(%rsp), %rdi
callq 0xae76
addq 0x88(%r15), %r14
movq 0x8(%r15), %rax
movl 0x198(%rax), %ecx
cmpl $0x8, %ecx
je 0xad97
cmpl $0x7, %ecx
je 0xad4b
cmpl $0x6, %ecx
jne 0xad34
movslq 0x194(%rax), %rax
testq %rax, %rax
jle 0xad34
movl 0xe0(%rsp), %ecx
leaq (%rax,%rax), %rdx
xorl %esi, %esi
testl %ecx, %ecx
jle 0xad0e
movq 0x70(%rsp,%rsi,8), %rdi
movq %r14, %r8
xorl %r9d, %r9d
movzwl (%rdi,%r9,2), %r10d
movw %r10w, (%r8)
incq %r9
addq %rdx, %r8
cmpq %r9, %rcx
jne 0xacfa
incq %rsi
addq $0x2, %r14
cmpq %rax, %rsi
jne 0xaceb
jmp 0xad34
movq 0x70(%rsp), %rdx
movslq %eax, %rcx
addq %rdx, %rcx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0xafde
movq 0x8(%rsp), %rax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x194(%rax), %rax
testq %rax, %rax
jle 0xad34
movl 0xe0(%rsp), %ecx
leaq (,%rax,4), %rdx
xorl %esi, %esi
testl %ecx, %ecx
jle 0xad89
movq 0x70(%rsp,%rsi,8), %rdi
movq %r14, %r8
xorl %r9d, %r9d
movl (%rdi,%r9,4), %r10d
movl %r10d, (%r8)
incq %r9
addq %rdx, %r8
cmpq %r9, %rcx
jne 0xad77
incq %rsi
addq $0x4, %r14
cmpq %rax, %rsi
jne 0xad68
jmp 0xad34
movslq 0x194(%rax), %rax
testq %rax, %rax
jle 0xad34
movl 0xe0(%rsp), %ecx
leaq (,%rax,4), %rdx
xorl %esi, %esi
testl %ecx, %ecx
jle 0xadd9
movq 0x70(%rsp,%rsi,8), %rdi
movq %r14, %r8
xorl %r9d, %r9d
movss (%rdi,%r9,4), %xmm0
movss %xmm0, (%r8)
incq %r9
addq %rdx, %r8
cmpq %r9, %rcx
jne 0xadc3
incq %rsi
addq $0x4, %r14
cmpq %rax, %rsi
jne 0xadb4
jmp 0xad34