History

avplay: use libavresample for sample format conversion and channel mixing

SDL only supports s16 sample format and a limited number of channel layouts. Some versions of SDL on some systems support 4-channel and 6-channel output, but it's safer overall to downmix any layout with more than 2 channels to stereo.

Justin Ruggles committed 14 years ago

Commit f1ffb011 parent e5b7d771 file · +86 −38Browse files

Unified · Split

avplay.c+86−38
@@ -34,7 +34,7 @@ · 33 unmodified lines
3434 #include "libavformat/avformat.h"
3535 #include "libavdevice/avdevice.h"
3636 #include "libswscale/swscale.h"
37−#include "libavcodec/audioconvert.h"
37+#include "libavresample/avresample.h"
3838 #include "libavutil/opt.h"
3939 #include "libavcodec/avfft.h"
4040
@@ -159,8 +159,12 @@ typedef struct VideoState { · 118 unmodified lines
159159 int audio_buf_index; /* in bytes */
160160 AVPacket audio_pkt_temp;
161161 AVPacket audio_pkt;
162− enum AVSampleFormat audio_src_fmt;
163− AVAudioConvert *reformat_ctx;
162+ enum AVSampleFormat sdl_sample_fmt;
163+ uint64_t sdl_channel_layout;
164+ int sdl_channels;
165+ enum AVSampleFormat resample_sample_fmt;
166+ uint64_t resample_channel_layout;
167+ AVAudioResampleContext *avr;
164168 AVFrame *frame;
165169
166170 int show_audio; /* if true, display audio samples */
@@ -743,7 +747,7 @@ static void video_audio_display(VideoState *s) · 576 unmodified lines
743747 nb_freq = 1 << (rdft_bits - 1);
744748
745749 /* compute display index : center on currently output samples */
746− channels = s->audio_st->codec->channels;
750+ channels = s->sdl_channels;
747751 nb_display_channels = channels;
748752 if (!s->paused) {
749753 int data_used = s->show_audio == 1 ? s->width : (2 * nb_freq);
@@ -957,8 +961,8 @@ static double get_audio_clock(VideoState *is) · 207 unmodified lines
957961 hw_buf_size = audio_write_get_buf_size(is);
958962 bytes_per_sec = 0;
959963 if (is->audio_st) {
960− bytes_per_sec = is->audio_st->codec->sample_rate *
961− 2 * is->audio_st->codec->channels;
964+ bytes_per_sec = is->audio_st->codec->sample_rate * is->sdl_channels *
965+ av_get_bytes_per_sample(is->sdl_sample_fmt);
962966 }
963967 if (bytes_per_sec)
964968 pts -= (double)hw_buf_size / bytes_per_sec;
@@ -1937,7 +1941,7 @@ static int synchronize_audio(VideoState *is, short *samples, · 972 unmodified lines
19371941 int n, samples_size;
19381942 double ref_clock;
19391943
1940− n = 2 * is->audio_st->codec->channels;
1944+ n = is->sdl_channels * av_get_bytes_per_sample(is->sdl_sample_fmt);
19411945 samples_size = samples_size1;
19421946
19431947 /* if not master, then we try to remove or add samples to correct the clock */
@@ -2018,6 +2022,8 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) · 74 unmodified lines
20182022 for (;;) {
20192023 /* NOTE: the audio packet can contain several frames */
20202024 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2025+ int resample_changed, audio_resample;
2026+
20212027 if (!is->frame) {
20222028 if (!(is->frame = avcodec_alloc_frame()))
20232029 return AVERROR(ENOMEM);
@@ -2047,39 +2053,67 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) · 23 unmodified lines
20472053 is->frame->nb_samples,
20482054 dec->sample_fmt, 1);
20492055
2050− if (dec->sample_fmt != is->audio_src_fmt) {
2051− if (is->reformat_ctx)
2052− av_audio_convert_free(is->reformat_ctx);
2053− is->reformat_ctx= av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
2054− dec->sample_fmt, 1, NULL, 0);
2055− if (!is->reformat_ctx) {
2056− fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
2057− av_get_sample_fmt_name(dec->sample_fmt),
2058− av_get_sample_fmt_name(AV_SAMPLE_FMT_S16));
2056+ audio_resample = dec->sample_fmt != is->sdl_sample_fmt ||
2057+ dec->channel_layout != is->sdl_channel_layout;
2058+
2059+ resample_changed = dec->sample_fmt != is->resample_sample_fmt ||
2060+ dec->channel_layout != is->resample_channel_layout;
2061+
2062+ if ((!is->avr && audio_resample) || resample_changed) {
2063+ if (is->avr)
2064+ avresample_close(is->avr);
2065+ else if (audio_resample) {
2066+ int ret;
2067+ is->avr = avresample_alloc_context();
2068+ if (!is->avr) {
2069+ fprintf(stderr, "error allocating AVAudioResampleContext\n");
20592070 break;
2071+ }
2072+ av_opt_set_int(is->avr, "in_channel_layout", dec->channel_layout, 0);
2073+ av_opt_set_int(is->avr, "in_sample_fmt", dec->sample_fmt, 0);
2074+ av_opt_set_int(is->avr, "in_sample_rate", dec->sample_rate, 0);
2075+ av_opt_set_int(is->avr, "out_channel_layout", is->sdl_channel_layout, 0);
2076+ av_opt_set_int(is->avr, "out_sample_fmt", is->sdl_sample_fmt, 0);
2077+ av_opt_set_int(is->avr, "out_sample_rate", dec->sample_rate, 0);
2078+ if (av_get_bytes_per_sample(dec->sample_fmt) <= 2)
2079+ av_opt_set_int(is->avr, "internal_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
2080+
2081+ if ((ret = avresample_open(is->avr)) < 0) {
2082+ fprintf(stderr, "error initializing libavresample\n");
2083+ break;
2084+ }
20602085 }
2061− is->audio_src_fmt= dec->sample_fmt;
2086+ is->resample_sample_fmt = dec->sample_fmt;
2087+ is->resample_channel_layout = dec->channel_layout;
20622088 }
20632089
2064− if (is->reformat_ctx) {
2065− const void *ibuf[6] = { is->frame->data[0] };
2066− void *obuf[6];
2067− int istride[6] = { av_get_bytes_per_sample(dec->sample_fmt) };
2068− int ostride[6] = { 2 };
2069− int len= data_size/istride[0];
2070− obuf[0] = av_realloc(is->audio_buf1, FFALIGN(len * ostride[0], 32));
2071− if (!obuf[0]) {
2090+ if (audio_resample) {
2091+ void *tmp_out;
2092+ int out_samples, out_size, out_linesize;
2093+ int osize = av_get_bytes_per_sample(is->sdl_sample_fmt);
2094+ int nb_samples = is->frame->nb_samples;
2095+
2096+ out_size = av_samples_get_buffer_size(&out_linesize,
2097+ is->sdl_channels,
2098+ nb_samples,
2099+ is->sdl_sample_fmt, 0);
2100+ tmp_out = av_realloc(is->audio_buf1, out_size);
2101+ if (!tmp_out)
20722102 return AVERROR(ENOMEM);
2073− }
2074− is->audio_buf1 = obuf[0];
2075− if (av_audio_convert(is->reformat_ctx, obuf, ostride, ibuf, istride, len) < 0) {
2076− printf("av_audio_convert() failed\n");
2103+ is->audio_buf1 = tmp_out;
2104+
2105+ out_samples = avresample_convert(is->avr,
2106+ (void **)&is->audio_buf1,
2107+ out_linesize, nb_samples,
2108+ (void **)is->frame->data,
2109+ is->frame->linesize[0],
2110+ is->frame->nb_samples);
2111+ if (out_samples < 0) {
2112+ fprintf(stderr, "avresample_convert() failed\n");
20772113 break;
20782114 }
20792115 is->audio_buf = is->audio_buf1;
2080− /* FIXME: existing code assume that data_size equals framesize*channels*2
2081− remove this legacy cruft */
2082− data_size = len * 2;
2116+ data_size = out_samples * osize * is->sdl_channels;
20832117 } else {
20842118 is->audio_buf = is->frame->data[0];
20852119 }
@@ -2087,7 +2121,7 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) · 1 unmodified line
20872121 /* if no pts, then compute it */
20882122 pts = is->audio_clock;
20892123 *pts_ptr = pts;
2090− n = 2 * dec->channels;
2124+ n = is->sdl_channels * av_get_bytes_per_sample(is->sdl_sample_fmt);
20912125 is->audio_clock += (double)data_size /
20922126 (double)(n * dec->sample_rate);
20932127 #ifdef DEBUG
@@ -2206,7 +2240,20 @@ static int stream_component_open(VideoState *is, int stream_index) · 112 unmodified lines
22062240 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
22072241 wanted_spec.freq = avctx->sample_rate;
22082242 wanted_spec.format = AUDIO_S16SYS;
2209− wanted_spec.channels = avctx->channels;
2243+
2244+ if (!avctx->channel_layout)
2245+ avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
2246+ if (!avctx->channel_layout) {
2247+ fprintf(stderr, "unable to guess channel layout\n");
2248+ return -1;
2249+ }
2250+ if (avctx->channels == 1)
2251+ is->sdl_channel_layout = AV_CH_LAYOUT_MONO;
2252+ else
2253+ is->sdl_channel_layout = AV_CH_LAYOUT_STEREO;
2254+ is->sdl_channels = av_get_channel_layout_nb_channels(is->sdl_channel_layout);
2255+
2256+ wanted_spec.channels = is->sdl_channels;
22102257 wanted_spec.silence = 0;
22112258 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
22122259 wanted_spec.callback = sdl_audio_callback;
@@ -2216,7 +2263,9 @@ static int stream_component_open(VideoState *is, int stream_index) · 3 unmodified lines
22162263 return -1;
22172264 }
22182265 is->audio_hw_buf_size = spec.size;
2219− is->audio_src_fmt = AV_SAMPLE_FMT_S16;
2266+ is->sdl_sample_fmt = AV_SAMPLE_FMT_S16;
2267+ is->resample_sample_fmt = is->sdl_sample_fmt;
2268+ is->resample_channel_layout = is->sdl_channel_layout;
22202269 }
22212270
22222271 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
@@ -2275,9 +2324,8 @@ static void stream_component_close(VideoState *is, int stream_index) · 52 unmodified lines
22752324
22762325 packet_queue_end(&is->audioq);
22772326 av_free_packet(&is->audio_pkt);
2278− if (is->reformat_ctx)
2279− av_audio_convert_free(is->reformat_ctx);
2280− is->reformat_ctx = NULL;
2327+ if (is->avr)
2328+ avresample_free(&is->avr);
22812329 av_freep(&is->audio_buf1);
22822330 is->audio_buf = NULL;
22832331 av_freep(&is->frame);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t