-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
225 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "converter.h" | ||
#include <libavutil/channel_layout.h> | ||
#include <libavutil/opt.h> | ||
#include <libavutil/samplefmt.h> | ||
#include <libswresample/swresample.h> | ||
#include <stdint.h> | ||
|
||
#include "utils.h" | ||
|
||
int converter_init(struct Converter *c, AVChannelLayout in_chlayout, int in_sample_rate, | ||
enum AVSampleFormat in_sample_fmt, AVChannelLayout out_chlayout, | ||
int out_sample_rate, enum AVSampleFormat out_sample_fmt) { | ||
c->swr_ctx = swr_alloc(); | ||
c->in_sample_rate = in_sample_rate; | ||
c->out_sample_rate = out_sample_rate; | ||
c->out_chlayout = out_chlayout; | ||
c->out_sample_fmt = out_sample_fmt; | ||
|
||
av_opt_set_chlayout(c->swr_ctx, "in_chlayout", &in_chlayout, 0); | ||
av_opt_set_chlayout(c->swr_ctx, "out_chlayout", &out_chlayout, 0); | ||
|
||
av_opt_set_int(c->swr_ctx, "in_sample_rate", in_sample_rate, 0); | ||
av_opt_set_int(c->swr_ctx, "out_sample_rate", out_sample_rate, 0); | ||
|
||
av_opt_set_sample_fmt(c->swr_ctx, "in_sample_fmt", in_sample_fmt, 0); | ||
av_opt_set_sample_fmt(c->swr_ctx, "out_sample_fmt", out_sample_fmt, 0); | ||
|
||
return swr_init(c->swr_ctx); | ||
} | ||
|
||
int converter_convert(struct Converter *c, AVFrame *src_frame, uint8_t ***out_data, | ||
int *out_count) { | ||
// int alt_max_out_nb_samples = | ||
// av_rescale_rnd(src_frame->nb_samples, c->out_sample_rate, c->in_sample_rate, AV_ROUND_UP); | ||
|
||
// AVChannelLayout out_chlayout; | ||
// av_opt_get_chlayout(c->swr_ctx, "out_chlayout", 0, &out_chlayout); | ||
|
||
// XAV_LOG_DEBUG("swr ctx in nb channels %d", out_ch) | ||
|
||
// int max_out_nb_samples = swr_get_out_samples(c->swr_ctx, src_frame->nb_samples); | ||
int max_out_nb_samples = swr_get_out_samples(c->swr_ctx, src_frame->nb_samples); | ||
int out_bytes_per_sample = av_get_bytes_per_sample(c->out_sample_fmt); | ||
int out_linesize; | ||
uint8_t **out_data_tmp = NULL; | ||
XAV_LOG_DEBUG("max out nb samples %d", max_out_nb_samples); | ||
// XAV_LOG_DEBUG("alt max out nb samples %d", alt_max_out_nb_samples); | ||
XAV_LOG_DEBUG("out_chlayout.nb_channels %d", c->out_chlayout.nb_channels); | ||
XAV_LOG_DEBUG("out_bytes_per_sample %d", out_bytes_per_sample); | ||
XAV_LOG_DEBUG("out buffer size: %d", | ||
max_out_nb_samples * c->out_chlayout.nb_channels * out_bytes_per_sample); | ||
|
||
int ret = | ||
av_samples_alloc_array_and_samples(&out_data_tmp, &out_linesize, c->out_chlayout.nb_channels, | ||
max_out_nb_samples, c->out_sample_fmt, 1); | ||
|
||
XAV_LOG_DEBUG("ret: %d", ret); | ||
|
||
*out_data = out_data_tmp; | ||
|
||
// uint8_t **in_data = NULL; | ||
// int in_linesize; | ||
// int in_bytes_per_sample = av_get_bytes_per_sample(src_frame->format); | ||
|
||
// ret = av_samples_alloc_array_and_samples(&in_data, &in_linesize, | ||
// src_frame->ch_layout.nb_channels, | ||
// src_frame->nb_samples, src_frame->format, 1); | ||
|
||
// XAV_LOG_DEBUG("src_frame->nb_samples: %d", src_frame->nb_samples); | ||
// XAV_LOG_DEBUG("src_frame->channels: %d", src_frame->ch_layout.nb_channels); | ||
// XAV_LOG_DEBUG("src_bytes_per_sample: %d", in_bytes_per_sample); | ||
// XAV_LOG_DEBUG("in linesize: %d", in_linesize); | ||
// XAV_LOG_DEBUG("in ret: %d", ret); | ||
// XAV_LOG_DEBUG("src_frame->linsize[0]: %d", src_frame->linesize[0]); | ||
|
||
// int in_size = src_frame->linesize[0] * src_frame->ch_layout.nb_channels; | ||
|
||
// XAV_LOG_DEBUG("in size: %d", in_size); | ||
|
||
// memcpy(in_data, src_frame->data, ret); | ||
|
||
*out_count = swr_convert(c->swr_ctx, out_data_tmp, max_out_nb_samples, | ||
(const uint8_t **)src_frame->data, src_frame->nb_samples); | ||
|
||
XAV_LOG_DEBUG("out_count: %d", *out_count); | ||
if (*out_count < 0) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void converter_free(struct Converter *c) { swr_free(&c->swr_ctx); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <libavutil/channel_layout.h> | ||
#include <libswresample/swresample.h> | ||
#include <stdint.h> | ||
|
||
struct Converter { | ||
SwrContext *swr_ctx; | ||
int64_t in_sample_rate; | ||
int64_t out_sample_rate; | ||
AVChannelLayout out_chlayout; | ||
enum AVSampleFormat out_sample_fmt; | ||
}; | ||
|
||
int converter_init(struct Converter *c, AVChannelLayout in_chlayout, int in_sample_rate, | ||
enum AVSampleFormat in_sample_fmt, AVChannelLayout out_chlaout, | ||
int out_sample_rate, enum AVSampleFormat out_sample_fmt); | ||
int converter_convert(struct Converter *c, AVFrame *src_frame, uint8_t ***out_data, int *out_count); | ||
void converter_free(struct Converter *converter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.