Skip to content

Instantly share code, notes, and snippets.

@rectalogic
Created April 20, 2012 20:42
Show Gist options
  • Save rectalogic/2431768 to your computer and use it in GitHub Desktop.
Save rectalogic/2431768 to your computer and use it in GitHub Desktop.
Demonstrates swr_convert corruption of packed audio
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
int main(int argc, char* argv[]) {
int r;
int nb_samples = 10;
int output_nb_samples = nb_samples;
int nb_channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);
int bytes_per_sample = av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * nb_channels;
int bufsize = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,
AV_SAMPLE_FMT_S16, 1);
int i;
uint8_t inbuf[bufsize];
uint8_t *input[] = { inbuf };
uint8_t outbuf[bufsize];
uint8_t *output[] = { outbuf };
struct SwrContext* swr_ctx =
swr_alloc_set_opts(NULL,
AV_CH_LAYOUT_STEREO,
AV_SAMPLE_FMT_S16,
44100,
AV_CH_LAYOUT_STEREO,
AV_SAMPLE_FMT_S16,
44100,
0, NULL);
swr_init(swr_ctx);
for (i = 0; i < sizeof(inbuf); i++)
inbuf[i] = i;
// Buffer input
if ((r = swr_convert(swr_ctx, output, output_nb_samples / 2,
(const uint8_t**)input, nb_samples)) < 0)
return -1;
output[0] = &outbuf[r * bytes_per_sample];
output_nb_samples -= r;
// Drain buffer
while ((r = swr_convert(swr_ctx, output, output_nb_samples, NULL, 0)) > 0) {
output[0] = &outbuf[r * bytes_per_sample];
output_nb_samples -= r;
}
// Resample should have been a straight copy
for (i = 0; i < sizeof(inbuf); i++) {
if (inbuf[i] != outbuf[i])
fprintf(stderr, "Byte %d does not match %x != %x\n", i, inbuf[i], outbuf[i]);
}
fprintf(stderr, "Finished\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment