// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq 0x88(%rdi), %rax
cmpq %rax, 0x90(%rdi)
je 0x8c8d
movq %rax, 0x90(%r14)
leaq 0x14(%rsp), %r15
andl $0x0, (%r15)
leaq 0x88(%r14), %rax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
leaq 0x18(%rsp), %r12
leaq 0x70(%rsp), %r13
leaq 0x49bd(%rip), %rbx      # 0xd672
xorl %ebp, %ebp
testl %eax, %eax
jne 0x8d28
movq (%r14), %rdi
movq %r12, %rsi
callq 0x4500
testl %eax, %eax
js 0x8df1
movl 0x3c(%rsp), %eax
cmpl 0x10(%r14), %eax
jne 0x8cee
movq 0x8(%r14), %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x4120
testl %eax, %eax
js 0x8cfc
movq %r12, %rdi
callq 0x4360
movl 0x14(%rsp), %eax
jmp 0x8cb7
movq 0xc2a5(%rip), %rdi      # 0x14fa8
movq %rbx, %rsi
callq 0x44c0
movq %rax, %rdi
callq 0x42d0
movq %r12, %rdi
callq 0x4360
cmpl $0x4, %ebp
jae 0x8e26
incl %ebp
jmp 0x8cf6
movq 0x8(%r14), %rax
movl 0xe0(%rsp), %edx
movl 0x194(%rax), %esi
movl 0x198(%rax), %ecx
pushq $0x1
popq %r8
xorl %edi, %edi
callq 0x41a0
movq 0x20(%rsp), %rcx
movq %rcx, %rdx
negq %rdx
jo 0x8d8d
cvtsi2sd %rcx, %xmm0
movq (%r14), %rcx
movq 0x30(%rcx), %rcx
movslq 0x10(%r14), %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
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x14(%r14)
movq 0x90(%r14), %rbx
cmpb $0x1, 0xa0(%r14)
jne 0x8e0e
subq 0x88(%r14), %rbx
movslq %eax, %rsi
addq %rbx, %rsi
movq 0x8(%rsp), %rdi
callq 0x8f70
addq 0x88(%r14), %rbx
movq 0x8(%r14), %rax
movl 0x198(%rax), %ecx
cmpl $0x8, %ecx
je 0x8e55
cmpl $0x7, %ecx
je 0x8e3d
cmpl $0x6, %ecx
jne 0x8e26
leaq 0x70(%rsp), %rsi
movl 0x70(%rsi), %edx
movl 0x194(%rax), %ecx
movq %rbx, %rdi
callq 0x8f9c
jmp 0x8e26
movq 0xc1b0(%rip), %rdi      # 0x14fa8
leaq 0x485b(%rip), %rsi      # 0xd65a
callq 0x44c0
movq %rax, %rdi
callq 0x42d0
jmp 0x8e26
movq 0x70(%rsp), %rdx
movslq %eax, %rcx
addq %rdx, %rcx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x907a
movq 0x8(%rsp), %rax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x70(%rsp), %rsi
movl 0x70(%rsi), %edx
movl 0x194(%rax), %ecx
movq %rbx, %rdi
callq 0x8fe6
jmp 0x8e26
leaq 0x70(%rsp), %rsi
movl 0x70(%rsi), %edx
movl 0x194(%rax), %ecx
movq %rbx, %rdi
callq 0x902f
jmp 0x8e26
nop