audio: add af_lavrresample, remove old resampling filters
[mplayer.git] / libmpcodecs / ad_ffmpeg.c
blob0aea120d42da92933bede8e217a5cdec1f9d92f4
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <assert.h>
25 #include <libavcodec/avcodec.h>
26 #include <libavutil/audioconvert.h>
27 #include <libavutil/opt.h>
28 #include <libavutil/samplefmt.h>
29 #include <libavresample/avresample.h>
31 #include "talloc.h"
33 #include "config.h"
34 #include "mp_msg.h"
35 #include "options.h"
37 #include "ad_internal.h"
38 #include "libaf/reorder_ch.h"
40 #include "mpbswap.h"
42 static const ad_info_t info =
44 "libavcodec audio decoders",
45 "ffmpeg",
46 "",
47 "",
48 "",
49 .print_name = "libavcodec",
52 LIBAD_EXTERN(ffmpeg)
54 struct priv {
55 AVCodecContext *avctx;
56 AVFrame *avframe;
57 char *output;
58 int output_left;
59 int unitsize;
60 int previous_data_left; // input demuxer packet data
62 AVAudioResampleContext *avr;
63 enum AVSampleFormat resample_fmt;
64 enum AVSampleFormat out_fmt;
65 int resample_channels;
66 uint8_t *resample_buf;
67 uint64_t resample_buf_size;
70 static int preinit(sh_audio_t *sh)
72 return 1;
75 static const int sample_fmt_map[][2] = {
76 { AV_SAMPLE_FMT_U8, AF_FORMAT_U8 },
77 { AV_SAMPLE_FMT_S16, AF_FORMAT_S16_NE },
78 { AV_SAMPLE_FMT_S32, AF_FORMAT_S32_NE },
79 { AV_SAMPLE_FMT_FLT, AF_FORMAT_FLOAT_NE },
82 static int sample_fmt_lavc2native(enum AVSampleFormat sample_fmt)
84 for (int i = 0; i < FF_ARRAY_ELEMS(sample_fmt_map); i++)
85 if (sample_fmt_map[i][0] == sample_fmt)
86 return sample_fmt_map[i][1];
87 return AF_FORMAT_UNKNOWN;
90 /* Prefer playing audio with the samplerate given in container data
91 * if available, but take number the number of channels and sample format
92 * from the codec, since if the codec isn't using the correct values for
93 * those everything breaks anyway.
95 static int setup_format(sh_audio_t *sh_audio)
97 struct priv *priv = sh_audio->context;
98 AVCodecContext *codec = priv->avctx;
100 int sample_format = sample_fmt_lavc2native(codec->sample_fmt);
101 if (sample_format == AF_FORMAT_UNKNOWN) {
102 if (priv->avr && (priv->resample_fmt != codec->sample_fmt ||
103 priv->resample_channels != codec->channels))
104 avresample_free(&priv->avr);
106 if (!priv->avr) {
107 int ret;
108 uint8_t error[128];
109 enum AVSampleFormat out_fmt =
110 av_get_packed_sample_fmt(codec->sample_fmt);
111 uint64_t ch_layout = codec->channel_layout;
113 mp_msg(MSGT_DECAUDIO, MSGL_V,
114 "(Re)initializing libavresample format conversion...\n");
116 if (!ch_layout)
117 ch_layout = av_get_default_channel_layout(codec->channels);
119 /* if lavc format is planar, try just getting packed equivalent */
120 sample_format = sample_fmt_lavc2native(out_fmt);
121 if (sample_format == AF_FORMAT_UNKNOWN) {
122 /* fallback to s16 */
123 out_fmt = AV_SAMPLE_FMT_S16;
124 sample_format = AF_FORMAT_S16_NE;
127 priv->avr = avresample_alloc_context();
128 if (!priv->avr) {
129 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Out of memory.\n");
130 abort();
132 av_opt_set_int(priv->avr, "in_channel_layout", ch_layout, 0);
133 av_opt_set_int(priv->avr, "out_channel_layout", ch_layout, 0);
134 av_opt_set_int(priv->avr, "in_sample_rate", codec->sample_rate, 0);
135 av_opt_set_int(priv->avr, "out_sample_rate", codec->sample_rate, 0);
136 av_opt_set_int(priv->avr, "in_sample_fmt", codec->sample_fmt, 0);
137 av_opt_set_int(priv->avr, "out_sample_fmt", out_fmt, 0);
139 if ((ret = avresample_open(priv->avr)) < 0) {
140 av_strerror(ret, error, sizeof(error));
141 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
142 "Error opening libavresample: %s.\n", error);
143 goto error;
145 priv->resample_fmt = codec->sample_fmt;
146 priv->resample_channels = codec->channels;
147 priv->out_fmt = out_fmt;
148 priv->unitsize = av_get_bytes_per_sample(out_fmt) *
149 codec->channels;
150 } else
151 sample_format = sh_audio->sample_format;
152 } else if (priv->avr) {
153 avresample_free(&priv->avr);
156 bool broken_srate = false;
157 int samplerate = codec->sample_rate;
158 int container_samplerate = sh_audio->container_out_samplerate;
159 if (!container_samplerate && sh_audio->wf)
160 container_samplerate = sh_audio->wf->nSamplesPerSec;
161 if (codec->codec_id == AV_CODEC_ID_AAC
162 && samplerate == 2 * container_samplerate)
163 broken_srate = true;
164 else if (container_samplerate)
165 samplerate = container_samplerate;
167 if (codec->channels != sh_audio->channels ||
168 samplerate != sh_audio->samplerate ||
169 sample_format != sh_audio->sample_format) {
170 sh_audio->channels = codec->channels;
171 sh_audio->samplerate = samplerate;
172 sh_audio->sample_format = sample_format;
173 sh_audio->samplesize = af_fmt2bits(sh_audio->sample_format) / 8;
174 if (broken_srate)
175 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
176 "Ignoring broken container sample rate for AAC with SBR\n");
177 return 1;
179 return 0;
180 error:
181 avresample_free(&priv->avr);
182 return -1;
185 static int init(sh_audio_t *sh_audio)
187 struct MPOpts *opts = sh_audio->opts;
188 AVCodecContext *lavc_context;
189 AVCodec *lavc_codec;
191 if (sh_audio->codec->dll) {
192 lavc_codec = avcodec_find_decoder_by_name(sh_audio->codec->dll);
193 if (!lavc_codec) {
194 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
195 "Cannot find codec '%s' in libavcodec...\n",
196 sh_audio->codec->dll);
197 return 0;
199 } else if (!sh_audio->libav_codec_id) {
200 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "No Libav codec ID known. "
201 "Generic lavc decoder is not applicable.\n");
202 return 0;
203 } else {
204 lavc_codec = avcodec_find_decoder(sh_audio->libav_codec_id);
205 if (!lavc_codec) {
206 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Libavcodec has no decoder "
207 "for this codec\n");
208 return 0;
212 sh_audio->codecname = lavc_codec->long_name;
213 if (!sh_audio->codecname)
214 sh_audio->codecname = lavc_codec->name;
216 struct priv *ctx = talloc_zero(NULL, struct priv);
217 sh_audio->context = ctx;
218 lavc_context = avcodec_alloc_context3(lavc_codec);
219 ctx->avctx = lavc_context;
220 ctx->avframe = avcodec_alloc_frame();
222 // Always try to set - option only exists for AC3 at the moment
223 av_opt_set_double(lavc_context, "drc_scale", opts->drc_level,
224 AV_OPT_SEARCH_CHILDREN);
225 lavc_context->sample_rate = sh_audio->samplerate;
226 lavc_context->bit_rate = sh_audio->i_bps * 8;
227 if (sh_audio->wf) {
228 lavc_context->channels = sh_audio->wf->nChannels;
229 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
230 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
231 lavc_context->block_align = sh_audio->wf->nBlockAlign;
232 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
234 lavc_context->request_channels = opts->audio_output_channels;
235 lavc_context->codec_tag = sh_audio->format; //FOURCC
236 lavc_context->codec_type = AVMEDIA_TYPE_AUDIO;
237 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
239 /* alloc extra data */
240 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
241 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
242 lavc_context->extradata_size = sh_audio->wf->cbSize;
243 memcpy(lavc_context->extradata, sh_audio->wf + 1,
244 lavc_context->extradata_size);
247 // for QDM2
248 if (sh_audio->codecdata_len && sh_audio->codecdata &&
249 !lavc_context->extradata) {
250 lavc_context->extradata = av_malloc(sh_audio->codecdata_len +
251 FF_INPUT_BUFFER_PADDING_SIZE);
252 lavc_context->extradata_size = sh_audio->codecdata_len;
253 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
254 lavc_context->extradata_size);
257 /* open it */
258 if (avcodec_open2(lavc_context, lavc_codec, NULL) < 0) {
259 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Could not open codec.\n");
260 uninit(sh_audio);
261 return 0;
263 mp_msg(MSGT_DECAUDIO, MSGL_V, "INFO: libavcodec \"%s\" init OK!\n",
264 lavc_codec->name);
266 if (sh_audio->format == 0x3343414D) {
267 // MACE 3:1
268 sh_audio->ds->ss_div = 2 * 3; // 1 samples/packet
269 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
270 } else if (sh_audio->format == 0x3643414D) {
271 // MACE 6:1
272 sh_audio->ds->ss_div = 2 * 6; // 1 samples/packet
273 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
276 // Decode at least 1 byte: (to get header filled)
277 for (int tries = 0;;) {
278 int x = decode_audio(sh_audio, sh_audio->a_buffer, 1,
279 sh_audio->a_buffer_size);
280 if (x > 0) {
281 sh_audio->a_buffer_len = x;
282 break;
284 if (++tries >= 5) {
285 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
286 "ad_ffmpeg: initial decode failed\n");
287 uninit(sh_audio);
288 return 0;
292 sh_audio->i_bps = lavc_context->bit_rate / 8;
293 if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec)
294 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
296 return 1;
299 static void uninit(sh_audio_t *sh)
301 sh->codecname = NULL;
302 struct priv *ctx = sh->context;
303 if (!ctx)
304 return;
305 AVCodecContext *lavc_context = ctx->avctx;
307 if (lavc_context) {
308 if (avcodec_close(lavc_context) < 0)
309 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
310 av_freep(&lavc_context->extradata);
311 av_freep(&lavc_context);
313 avresample_free(&ctx->avr);
314 #if LIBAVCODEC_VERSION_INT >= (54 << 16 | 28 << 8)
315 avcodec_free_frame(&ctx->avframe);
316 #else
317 av_free(ctx->avframe);
318 #endif
319 talloc_free(ctx);
320 sh->context = NULL;
323 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
325 struct priv *ctx = sh->context;
326 switch (cmd) {
327 case ADCTRL_RESYNC_STREAM:
328 avcodec_flush_buffers(ctx->avctx);
329 ds_clear_parser(sh->ds);
330 ctx->previous_data_left = 0;
331 ctx->output_left = 0;
332 return CONTROL_TRUE;
334 return CONTROL_UNKNOWN;
337 static int decode_new_packet(struct sh_audio *sh)
339 struct priv *priv = sh->context;
340 AVCodecContext *avctx = priv->avctx;
341 double pts = MP_NOPTS_VALUE;
342 int insize;
343 bool packet_already_used = priv->previous_data_left;
344 bool eof = false;
345 struct demux_packet *mpkt = ds_get_packet2(sh->ds,
346 priv->previous_data_left);
347 unsigned char *start;
348 if (!mpkt) {
349 assert(!priv->previous_data_left);
350 start = NULL;
351 insize = 0;
352 ds_parse(sh->ds, &start, &insize, pts, 0);
353 if (insize <= 0)
354 eof = true;
355 } else {
356 assert(mpkt->len >= priv->previous_data_left);
357 if (!priv->previous_data_left) {
358 priv->previous_data_left = mpkt->len;
359 pts = mpkt->pts;
361 insize = priv->previous_data_left;
362 start = mpkt->buffer + mpkt->len - priv->previous_data_left;
363 int consumed = ds_parse(sh->ds, &start, &insize, pts, 0);
364 priv->previous_data_left -= consumed;
365 priv->previous_data_left = FFMAX(priv->previous_data_left, 0);
368 AVPacket pkt;
369 av_init_packet(&pkt);
370 pkt.data = start;
371 pkt.size = insize;
372 if (mpkt && mpkt->avpacket) {
373 pkt.side_data = mpkt->avpacket->side_data;
374 pkt.side_data_elems = mpkt->avpacket->side_data_elems;
376 if (pts != MP_NOPTS_VALUE && !packet_already_used) {
377 sh->pts = pts;
378 sh->pts_bytes = 0;
380 int got_frame = 0;
381 int ret = avcodec_decode_audio4(avctx, priv->avframe, &got_frame, &pkt);
382 // LATM may need many packets to find mux info
383 if (ret == AVERROR(EAGAIN))
384 return 0;
385 if (ret < 0) {
386 mp_msg(MSGT_DECAUDIO, MSGL_V, "lavc_audio: error\n");
387 return -1;
389 // The "insize >= ret" test is sanity check against decoder overreads
390 if (!sh->parser && insize >= ret)
391 priv->previous_data_left = insize - ret;
392 if (!got_frame)
393 return eof ? -1 : 0;
395 int format_result = setup_format(sh);
396 if (format_result < 0)
397 return format_result;
399 if (priv->avr) {
400 int ret;
401 uint64_t needed_size = av_samples_get_buffer_size(
402 NULL, priv->resample_channels, priv->avframe->nb_samples,
403 priv->resample_fmt, 0);
404 if (needed_size > priv->resample_buf_size) {
405 priv->resample_buf = talloc_realloc(priv, priv->resample_buf,
406 uint8_t, needed_size);
407 priv->resample_buf_size = needed_size;
410 ret = avresample_convert(priv->avr, &priv->resample_buf,
411 priv->resample_buf_size, priv->avframe->nb_samples,
412 priv->avframe->extended_data, priv->avframe->linesize[0],
413 priv->avframe->nb_samples);
414 if (ret < 0) {
415 uint8_t error[128];
416 av_strerror(ret, error, sizeof(error));
417 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
418 "Error during sample format conversion: %s.\n", error);
419 return -1;
422 assert(ret == priv->avframe->nb_samples);
424 priv->output = priv->resample_buf;
425 priv->output_left = priv->unitsize * ret;
426 } else {
427 uint64_t unitsize = av_get_bytes_per_sample(avctx->sample_fmt) *
428 (uint64_t)avctx->channels;
429 if (unitsize > 100000)
430 abort();
431 priv->unitsize = unitsize;
432 uint64_t output_left = unitsize * priv->avframe->nb_samples;
433 if (output_left > 500000000)
434 abort();
435 priv->output_left = output_left;
436 priv->output = priv->avframe->data[0];
439 mp_dbg(MSGT_DECAUDIO, MSGL_DBG2, "Decoded %d -> %d \n", insize,
440 priv->output_left);
441 return format_result;
445 static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen,
446 int maxlen)
448 struct priv *priv = sh_audio->context;
449 AVCodecContext *avctx = priv->avctx;
451 int len = -1;
452 while (len < minlen) {
453 if (!priv->output_left) {
454 if (decode_new_packet(sh_audio) != 0)
455 break;
456 continue;
458 int size = (minlen - len + priv->unitsize - 1);
459 size -= size % priv->unitsize;
460 size = FFMIN(size, priv->output_left);
461 if (size > maxlen)
462 abort();
463 memcpy(buf, priv->output, size);
464 priv->output += size;
465 priv->output_left -= size;
466 if (avctx->channels >= 5) {
467 int samplesize = av_get_bytes_per_sample(avctx->sample_fmt);
468 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
469 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
470 avctx->channels,
471 size / samplesize, samplesize);
473 if (len < 0)
474 len = size;
475 else
476 len += size;
477 buf += size;
478 maxlen -= size;
479 sh_audio->pts_bytes += size;
481 return len;