+ {
+ // Add audio.
+ AVFrame *frame = avcodec_alloc_frame();
+ frame->nb_samples = task.audio.size() / 2;
+ frame->format = AV_SAMPLE_FMT_FLT;
+ frame->channel_layout = AV_CH_LAYOUT_STEREO;
+
+ unique_ptr<float[]> planar_samples(new float[task.audio.size()]);
+ avcodec_fill_audio_frame(frame, 2, AV_SAMPLE_FMT_FLTP, (const uint8_t*)planar_samples.get(), task.audio.size() * sizeof(float), 0);
+ for (int i = 0; i < frame->nb_samples; ++i) {
+ planar_samples[i] = task.audio[i * 2 + 0];
+ planar_samples[i + frame->nb_samples] = task.audio[i * 2 + 1];
+ }
+
+ AVPacket pkt;
+ av_init_packet(&pkt);
+ pkt.data = nullptr;
+ pkt.size = 0;
+ int got_output;
+ avcodec_encode_audio2(avstream_audio->codec, &pkt, frame, &got_output);
+ if (got_output) {
+ pkt.pts = av_rescale_q(task.display_order, AVRational{1, frame_rate}, avstream_audio->time_base); // FIXME
+ pkt.stream_index = 1;
+ av_interleaved_write_frame(avctx, &pkt);
+ }
+ // TODO: Delayed frames.
+ avcodec_free_frame(&frame);
+ }
+
+ static FILE *audiofp = fopen("audio.raw", "wb");
+ fwrite(&task.audio[0], 4 * task.audio.size(), 1, audiofp);