mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Live muxer cleanup
This commit is contained in:
parent
db1541b2c4
commit
33d4eef7a6
@ -19,8 +19,6 @@
|
|||||||
int *streams_list = NULL;
|
int *streams_list = NULL;
|
||||||
int number_of_streams = 0;
|
int number_of_streams = 0;
|
||||||
|
|
||||||
struct SwrContext *swr_ctx = NULL;
|
|
||||||
|
|
||||||
in_filename = [path UTF8String];
|
in_filename = [path UTF8String];
|
||||||
out_filename = [outPath UTF8String];
|
out_filename = [outPath UTF8String];
|
||||||
|
|
||||||
@ -41,34 +39,6 @@
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AVCodec *aac_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
|
|
||||||
if (!aac_codec) {
|
|
||||||
fprintf(stderr, "Could not find AAC encoder\n");
|
|
||||||
ret = AVERROR_UNKNOWN;
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVCodecContext *aac_codec_context = avcodec_alloc_context3(aac_codec);
|
|
||||||
if (!aac_codec_context) {
|
|
||||||
fprintf(stderr, "Could not allocate AAC codec context\n");
|
|
||||||
ret = AVERROR_UNKNOWN;
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
const AVCodec *opus_decoder = avcodec_find_decoder(AV_CODEC_ID_AAC);
|
|
||||||
if (!opus_decoder) {
|
|
||||||
fprintf(stderr, "Could not find Opus decoder\n");
|
|
||||||
ret = AVERROR_UNKNOWN;
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVCodecContext *opus_decoder_context = avcodec_alloc_context3(opus_decoder);
|
|
||||||
if (!opus_decoder_context) {
|
|
||||||
fprintf(stderr, "Could not allocate Opus decoder context\n");
|
|
||||||
ret = AVERROR_UNKNOWN;
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
number_of_streams = input_format_context->nb_streams;
|
number_of_streams = input_format_context->nb_streams;
|
||||||
streams_list = av_malloc_array(number_of_streams, sizeof(*streams_list));
|
streams_list = av_malloc_array(number_of_streams, sizeof(*streams_list));
|
||||||
|
|
||||||
@ -77,14 +47,12 @@
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasAudio = false;
|
|
||||||
|
|
||||||
for (i = 0; i < input_format_context->nb_streams; i++) {
|
for (i = 0; i < input_format_context->nb_streams; i++) {
|
||||||
AVStream *out_stream;
|
AVStream *out_stream;
|
||||||
AVStream *in_stream = input_format_context->streams[i];
|
AVStream *in_stream = input_format_context->streams[i];
|
||||||
AVCodecParameters *in_codecpar = in_stream->codecpar;
|
AVCodecParameters *in_codecpar = in_stream->codecpar;
|
||||||
|
|
||||||
if (/*in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO && */in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
|
if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO && in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
|
||||||
streams_list[i] = -1;
|
streams_list[i] = -1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -106,75 +74,24 @@
|
|||||||
out_stream->time_base = in_stream->time_base;
|
out_stream->time_base = in_stream->time_base;
|
||||||
out_stream->duration = in_stream->duration;
|
out_stream->duration = in_stream->duration;
|
||||||
} else if (in_codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
} else if (in_codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||||||
out_stream = avformat_new_stream(output_format_context, aac_codec);
|
if (in_codecpar->codec_id != AV_CODEC_ID_AAC) {
|
||||||
|
streams_list[i] = -1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_stream = avformat_new_stream(output_format_context, NULL);
|
||||||
if (!out_stream) {
|
if (!out_stream) {
|
||||||
fprintf(stderr, "Failed allocating output stream\n");
|
fprintf(stderr, "Failed allocating output stream\n");
|
||||||
ret = AVERROR_UNKNOWN;
|
ret = AVERROR_UNKNOWN;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
|
||||||
hasAudio = true;
|
|
||||||
|
|
||||||
// Set the codec parameters for the AAC encoder
|
|
||||||
aac_codec_context->sample_rate = in_codecpar->sample_rate;
|
|
||||||
aac_codec_context->channel_layout = in_codecpar->channel_layout ? in_codecpar->channel_layout : AV_CH_LAYOUT_STEREO;
|
|
||||||
aac_codec_context->channels = av_get_channel_layout_nb_channels(aac_codec_context->channel_layout);
|
|
||||||
aac_codec_context->sample_fmt = aac_codec->sample_fmts ? aac_codec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP; // Use the first supported sample format
|
|
||||||
aac_codec_context->bit_rate = 128000; // Set a default bitrate, you can adjust this as needed
|
|
||||||
//aac_codec_context->time_base = (AVRational){1, 90000};
|
|
||||||
|
|
||||||
ret = avcodec_open2(aac_codec_context, aac_codec, NULL);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "Could not open AAC encoder\n");
|
fprintf(stderr, "Failed to copy codec parameters\n");
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
out_stream->time_base = in_stream->time_base;
|
||||||
ret = avcodec_parameters_from_context(out_stream->codecpar, aac_codec_context);
|
out_stream->duration = in_stream->duration;
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "Failed initializing audio output stream\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
out_stream->time_base = (AVRational){1, 90000};
|
|
||||||
out_stream->duration = av_rescale_q(in_stream->duration, in_stream->time_base, out_stream->time_base);
|
|
||||||
|
|
||||||
// Set up the Opus decoder context
|
|
||||||
ret = avcodec_parameters_to_context(opus_decoder_context, in_codecpar);
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "Could not copy codec parameters to decoder context\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (opus_decoder_context->channel_layout == 0) {
|
|
||||||
opus_decoder_context->channel_layout = av_get_default_channel_layout(opus_decoder_context->channels);
|
|
||||||
}
|
|
||||||
ret = avcodec_open2(opus_decoder_context, opus_decoder, NULL);
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "Could not open Opus decoder\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset the channel layout if it was unset before opening the codec
|
|
||||||
if (opus_decoder_context->channel_layout == 0) {
|
|
||||||
opus_decoder_context->channel_layout = av_get_default_channel_layout(opus_decoder_context->channels);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasAudio) {
|
|
||||||
// Set up the resampling context
|
|
||||||
swr_ctx = swr_alloc_set_opts(NULL,
|
|
||||||
aac_codec_context->channel_layout, aac_codec_context->sample_fmt, aac_codec_context->sample_rate,
|
|
||||||
opus_decoder_context->channel_layout, opus_decoder_context->sample_fmt, opus_decoder_context->sample_rate,
|
|
||||||
0, NULL);
|
|
||||||
if (!swr_ctx) {
|
|
||||||
fprintf(stderr, "Could not allocate resampler context\n");
|
|
||||||
ret = AVERROR(ENOMEM);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((ret = swr_init(swr_ctx)) < 0) {
|
|
||||||
fprintf(stderr, "Failed to initialize the resampling context\n");
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,92 +126,19 @@
|
|||||||
out_stream = output_format_context->streams[packet.stream_index];
|
out_stream = output_format_context->streams[packet.stream_index];
|
||||||
|
|
||||||
if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||||||
ret = avcodec_send_packet(opus_decoder_context, &packet);
|
packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
||||||
if (ret < 0) {
|
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
||||||
fprintf(stderr, "Error sending packet to decoder\n");
|
packet.pts += (int64_t)(offsetSeconds * out_stream->time_base.den);
|
||||||
av_packet_unref(&packet);
|
packet.dts += (int64_t)(offsetSeconds * out_stream->time_base.den);
|
||||||
continue;
|
packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
|
||||||
}
|
packet.pos = -1;
|
||||||
|
|
||||||
AVFrame *frame = av_frame_alloc();
|
ret = av_interleaved_write_frame(output_format_context, &packet);
|
||||||
ret = avcodec_receive_frame(opus_decoder_context, frame);
|
|
||||||
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
|
|
||||||
fprintf(stderr, "Error receiving frame from decoder\n");
|
|
||||||
av_frame_free(&frame);
|
|
||||||
av_packet_unref(&packet);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret >= 0) {
|
|
||||||
frame->pts = frame->best_effort_timestamp;
|
|
||||||
|
|
||||||
AVFrame *resampled_frame = av_frame_alloc();
|
|
||||||
resampled_frame->channel_layout = aac_codec_context->channel_layout;
|
|
||||||
resampled_frame->sample_rate = aac_codec_context->sample_rate;
|
|
||||||
resampled_frame->format = aac_codec_context->sample_fmt;
|
|
||||||
resampled_frame->nb_samples = aac_codec_context->frame_size;
|
|
||||||
|
|
||||||
if ((ret = av_frame_get_buffer(resampled_frame, 0)) < 0) {
|
|
||||||
fprintf(stderr, "Could not allocate resampled frame buffer\n");
|
|
||||||
av_frame_free(&resampled_frame);
|
|
||||||
av_frame_free(&frame);
|
|
||||||
av_packet_unref(&packet);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(resampled_frame->data[0], 0, resampled_frame->nb_samples * 2 * 2);
|
|
||||||
//arc4random_buf(resampled_frame->data[0], resampled_frame->nb_samples * 2 * 2);
|
|
||||||
//memset(frame->data[0], 0, frame->nb_samples * 2 * 2);
|
|
||||||
|
|
||||||
if ((ret = swr_convert(swr_ctx,
|
|
||||||
resampled_frame->data, resampled_frame->nb_samples,
|
|
||||||
(const uint8_t **)frame->data, frame->nb_samples)) < 0) {
|
|
||||||
fprintf(stderr, "Error while converting\n");
|
|
||||||
av_frame_free(&resampled_frame);
|
|
||||||
av_frame_free(&frame);
|
|
||||||
av_packet_unref(&packet);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
resampled_frame->pts = av_rescale_q(frame->pts, opus_decoder_context->time_base, aac_codec_context->time_base);
|
|
||||||
|
|
||||||
ret = avcodec_send_frame(aac_codec_context, resampled_frame);
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "Error sending frame to encoder\n");
|
|
||||||
av_frame_free(&resampled_frame);
|
|
||||||
av_frame_free(&frame);
|
|
||||||
av_packet_unref(&packet);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVPacket out_packet;
|
|
||||||
av_init_packet(&out_packet);
|
|
||||||
out_packet.data = NULL;
|
|
||||||
out_packet.size = 0;
|
|
||||||
|
|
||||||
ret = avcodec_receive_packet(aac_codec_context, &out_packet);
|
|
||||||
if (ret >= 0) {
|
|
||||||
out_packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
|
||||||
out_packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
|
||||||
out_packet.pts += (int64_t)(offsetSeconds * out_stream->time_base.den);
|
|
||||||
out_packet.dts += (int64_t)(offsetSeconds * out_stream->time_base.den);
|
|
||||||
out_packet.duration = av_rescale_q(out_packet.duration, aac_codec_context->time_base, out_stream->time_base);
|
|
||||||
out_packet.stream_index = packet.stream_index;
|
|
||||||
|
|
||||||
ret = av_interleaved_write_frame(output_format_context, &out_packet);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "Error muxing packet\n");
|
fprintf(stderr, "Error muxing packet\n");
|
||||||
av_packet_unref(&out_packet);
|
|
||||||
av_frame_free(&resampled_frame);
|
|
||||||
av_frame_free(&frame);
|
|
||||||
av_packet_unref(&packet);
|
av_packet_unref(&packet);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
av_packet_unref(&out_packet);
|
|
||||||
}
|
|
||||||
av_frame_free(&resampled_frame);
|
|
||||||
av_frame_free(&frame);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
||||||
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
||||||
@ -317,16 +161,17 @@
|
|||||||
av_write_trailer(output_format_context);
|
av_write_trailer(output_format_context);
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
if (input_format_context) {
|
||||||
avformat_close_input(&input_format_context);
|
avformat_close_input(&input_format_context);
|
||||||
|
}
|
||||||
if (output_format_context && !(output_format_context->oformat->flags & AVFMT_NOFILE)) {
|
if (output_format_context && !(output_format_context->oformat->flags & AVFMT_NOFILE)) {
|
||||||
avio_closep(&output_format_context->pb);
|
avio_closep(&output_format_context->pb);
|
||||||
}
|
}
|
||||||
|
if (output_format_context) {
|
||||||
avformat_free_context(output_format_context);
|
avformat_free_context(output_format_context);
|
||||||
avcodec_free_context(&aac_codec_context);
|
}
|
||||||
avcodec_free_context(&opus_decoder_context);
|
if (streams_list) {
|
||||||
av_freep(&streams_list);
|
av_freep(&streams_list);
|
||||||
if (swr_ctx) {
|
|
||||||
swr_free(&swr_ctx);
|
|
||||||
}
|
}
|
||||||
if (ret < 0 && ret != AVERROR_EOF) {
|
if (ret < 0 && ret != AVERROR_EOF) {
|
||||||
fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
|
fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user