// File: /workspace/github_cpp_makefile_star2_O1/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 0xa8bd
movq %rax, 0x90(%r15)
leaq 0x88(%r15), %rax
movq %rax, 0x10(%rsp)
leaq 0xc(%rsp), %r12
movl $0x0, (%r12)
xorl %ebx, %ebx
leaq 0x18(%rsp), %r13
leaq 0x70(%rsp), %rbp
movq 0xb6b7(%rip), %r14      # 0x15fa0
cmpl $0x0, 0xc(%rsp)
jne 0xa984
movq (%r15), %rdi
movq %r13, %rsi
callq 0x5510
testl %eax, %eax
js 0xaa94
movl 0x3c(%rsp), %eax
cmpl 0x10(%r15), %eax
jne 0xa927
movq 0x8(%r15), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x5110
testl %eax, %eax
js 0xa931
movq %r13, %rdi
callq 0x5350
jmp 0xa8e9
movl $0x1f, %edx
movq %r14, %rdi
leaq 0x4d52(%rip), %rsi      # 0xf692
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
incl %ebx
cmpl $0x5, %ebx
jne 0xa8e9
jmp 0xaae9
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 0xa9ee
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 0xaad1
subq 0x88(%r15), %r14
movslq %eax, %rsi
addq %r14, %rsi
movq 0x10(%rsp), %rdi
callq 0xac2c
addq 0x88(%r15), %r14
movq 0x8(%r15), %rax
movl 0x198(%rax), %ecx
cmpl $0x8, %ecx
je 0xab4c
cmpl $0x7, %ecx
je 0xab00
cmpl $0x6, %ecx
jne 0xaae9
movslq 0x194(%rax), %rax
testq %rax, %rax
jle 0xaae9
movl 0xe0(%rsp), %ecx
leaq (%rax,%rax), %rdx
xorl %esi, %esi
testl %ecx, %ecx
jle 0xaa86
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 0xaa72
incq %rsi
addq $0x2, %r14
cmpq %rax, %rsi
jne 0xaa63
jmp 0xaae9
leaq 0x4bdf(%rip), %rsi      # 0xf67a
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 0xaae9
movq 0x70(%rsp), %rdx
movslq %eax, %rcx
addq %rdx, %rcx
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0xadaa
movq 0x10(%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 0xaae9
movl 0xe0(%rsp), %ecx
leaq (,%rax,4), %rdx
xorl %esi, %esi
testl %ecx, %ecx
jle 0xab3e
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 0xab2c
incq %rsi
addq $0x4, %r14
cmpq %rax, %rsi
jne 0xab1d
jmp 0xaae9
movslq 0x194(%rax), %rax
testq %rax, %rax
jle 0xaae9
movl 0xe0(%rsp), %ecx
leaq (,%rax,4), %rdx
xorl %esi, %esi
testl %ecx, %ecx
jle 0xab8e
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 0xab78
incq %rsi
addq $0x4, %r14
cmpq %rax, %rsi
jne 0xab69
jmp 0xaae9
nop