static bool do_aac_encode(struct aac_encoder *enc, struct encoder_packet *packet, bool *received_packet) { AVRational time_base = {1, enc->context->sample_rate}; AVPacket avpacket = {0}; int got_packet; int ret; enc->aframe->nb_samples = enc->frame_size; enc->aframe->pts = av_rescale_q(enc->total_samples, (AVRational){1, enc->context->sample_rate}, enc->context->time_base); ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels, enc->context->sample_fmt, enc->samples[0], enc->frame_size_bytes * enc->context->channels, 1); if (ret < 0) { aac_warn("do_aac_encode", "avcodec_fill_audio_frame failed: %s", av_err2str(ret)); return false; } enc->total_samples += enc->frame_size; ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe, &got_packet); if (ret < 0) { aac_warn("do_aac_encode", "avcodec_encode_audio2 failed: %s", av_err2str(ret)); return false; } *received_packet = !!got_packet; if (!got_packet) return true; da_resize(enc->packet_buffer, 0); da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size); packet->pts = rescale_ts(avpacket.pts, enc->context, time_base); packet->dts = rescale_ts(avpacket.dts, enc->context, time_base); packet->data = enc->packet_buffer.array; packet->size = avpacket.size; packet->type = OBS_ENCODER_AUDIO; packet->timebase_num = 1; packet->timebase_den = (int32_t)enc->context->sample_rate; av_free_packet(&avpacket); return true; }
static inline bool ffmpeg_mux_packet(struct ffmpeg_mux *ffm, uint8_t *buf, struct ffm_packet_info *info) { int idx = get_index(ffm, info); AVPacket packet = {0}; /* The muxer might not support video/audio, or multiple audio tracks */ if (idx == -1) { return true; } av_init_packet(&packet); packet.data = buf; packet.size = (int)info->size; packet.stream_index = idx; packet.pts = rescale_ts(ffm, info->pts, idx); packet.dts = rescale_ts(ffm, info->dts, idx); if (info->keyframe) packet.flags = AV_PKT_FLAG_KEY; return av_interleaved_write_frame(ffm->output, &packet) >= 0; }