ad_ffmpeg: remove incorrect request_sample_fmt setting
[mplayer.git] / libmpcodecs / ad_ffmpeg.c
blob47807498ab8e24dd2af727473b91e3ab6f207239
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>
30 #include "talloc.h"
32 #include "config.h"
33 #include "mp_msg.h"
34 #include "options.h"
36 #include "ad_internal.h"
37 #include "libaf/reorder_ch.h"
39 #include "mpbswap.h"
41 #ifdef CONFIG_LIBAVRESAMPLE
42 #include <libavresample/avresample.h>
43 #endif
45 static const ad_info_t info =
47 "libavcodec audio decoders",
48 "ffmpeg",
49 "",
50 "",
51 "",
52 .print_name = "libavcodec",
55 LIBAD_EXTERN(ffmpeg)
57 struct priv {
58 AVCodecContext *avctx;
59 AVFrame *avframe;
60 char *output;
61 int output_left;
62 int unitsize;
63 int previous_data_left; // input demuxer packet data
65 #ifdef CONFIG_LIBAVRESAMPLE
66 AVAudioResampleContext *avr;
67 enum AVSampleFormat resample_fmt;
68 enum AVSampleFormat out_fmt;
69 int resample_channels;
70 uint8_t *resample_buf;
71 uint64_t resample_buf_size;
72 #endif
75 static int preinit(sh_audio_t *sh)
77 return 1;
80 static const int sample_fmt_map[][2] = {
81 { AV_SAMPLE_FMT_U8, AF_FORMAT_U8 },
82 { AV_SAMPLE_FMT_S16, AF_FORMAT_S16_NE },
83 { AV_SAMPLE_FMT_S32, AF_FORMAT_S32_NE },
84 { AV_SAMPLE_FMT_FLT, AF_FORMAT_FLOAT_NE },
87 static int sample_fmt_lavc2native(enum AVSampleFormat sample_fmt)
89 for (int i = 0; i < FF_ARRAY_ELEMS(sample_fmt_map); i++)
90 if (sample_fmt_map[i][0] == sample_fmt)
91 return sample_fmt_map[i][1];
92 return AF_FORMAT_UNKNOWN;
95 /* Prefer playing audio with the samplerate given in container data
96 * if available, but take number the number of channels and sample format
97 * from the codec, since if the codec isn't using the correct values for
98 * those everything breaks anyway.
100 static int setup_format(sh_audio_t *sh_audio)
102 struct priv *priv = sh_audio->context;
103 AVCodecContext *codec = priv->avctx;
104 int sample_format = sh_audio->sample_format;
106 sample_format = sample_fmt_lavc2native(codec->sample_fmt);
107 if (sample_format == AF_FORMAT_UNKNOWN) {
108 #ifndef CONFIG_LIBAVRESAMPLE
109 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
110 sample_format = AF_FORMAT_UNKNOWN;
111 #else
112 if (priv->avr && (priv->resample_fmt != codec->sample_fmt ||
113 priv->resample_channels != codec->channels))
114 avresample_free(&priv->avr);
116 if (!priv->avr) {
117 int ret;
118 uint8_t error[128];
119 enum AVSampleFormat out_fmt =
120 av_get_packed_sample_fmt(codec->sample_fmt);
121 uint64_t ch_layout = codec->channel_layout;
123 mp_msg(MSGT_DECAUDIO, MSGL_V,
124 "(Re)initializing libavresample format conversion...\n");
126 if (!ch_layout)
127 ch_layout = av_get_default_channel_layout(codec->channels);
129 /* if lavc format is planar, try just getting packed equivalent */
130 sample_format = sample_fmt_lavc2native(out_fmt);
131 if (sample_format == AF_FORMAT_UNKNOWN) {
132 /* fallback to s16 */
133 out_fmt = AV_SAMPLE_FMT_S16;
134 sample_format = AF_FORMAT_S16_NE;
137 priv->avr = avresample_alloc_context();
138 if (!priv->avr) {
139 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Out of memory.\n");
140 abort();
142 av_opt_set_int(priv->avr, "in_channel_layout", ch_layout, 0);
143 av_opt_set_int(priv->avr, "out_channel_layout", ch_layout, 0);
144 av_opt_set_int(priv->avr, "in_sample_rate", codec->sample_rate, 0);
145 av_opt_set_int(priv->avr, "out_sample_rate", codec->sample_rate, 0);
146 av_opt_set_int(priv->avr, "in_sample_fmt", codec->sample_fmt, 0);
147 av_opt_set_int(priv->avr, "out_sample_fmt", out_fmt, 0);
149 if ((ret = avresample_open(priv->avr)) < 0) {
150 av_strerror(ret, error, sizeof(error));
151 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
152 "Error opening libavresample: %s.\n", error);
154 priv->resample_fmt = codec->sample_fmt;
155 priv->resample_channels = codec->channels;
156 priv->out_fmt = out_fmt;
157 priv->unitsize = av_get_bytes_per_sample(out_fmt) *
158 codec->channels;
159 } else
160 sample_format = sh_audio->sample_format;
161 } else if (priv->avr) {
162 avresample_free(&priv->avr);
163 #endif
166 bool broken_srate = false;
167 int samplerate = codec->sample_rate;
168 int container_samplerate = sh_audio->container_out_samplerate;
169 if (!container_samplerate && sh_audio->wf)
170 container_samplerate = sh_audio->wf->nSamplesPerSec;
171 if (codec->codec_id == CODEC_ID_AAC
172 && samplerate == 2 * container_samplerate)
173 broken_srate = true;
174 else if (container_samplerate)
175 samplerate = container_samplerate;
177 if (codec->channels != sh_audio->channels ||
178 samplerate != sh_audio->samplerate ||
179 sample_format != sh_audio->sample_format) {
180 sh_audio->channels = codec->channels;
181 sh_audio->samplerate = samplerate;
182 sh_audio->sample_format = sample_format;
183 sh_audio->samplesize = af_fmt2bits(sh_audio->sample_format) / 8;
184 if (broken_srate)
185 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
186 "Ignoring broken container sample rate for AAC with SBR\n");
187 return 1;
189 return 0;
192 static int init(sh_audio_t *sh_audio)
194 struct MPOpts *opts = sh_audio->opts;
195 AVCodecContext *lavc_context;
196 AVCodec *lavc_codec;
198 if (sh_audio->codec->dll) {
199 lavc_codec = avcodec_find_decoder_by_name(sh_audio->codec->dll);
200 if (!lavc_codec) {
201 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
202 "Cannot find codec '%s' in libavcodec...\n",
203 sh_audio->codec->dll);
204 return 0;
206 } else if (!sh_audio->libav_codec_id) {
207 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "No Libav codec ID known. "
208 "Generic lavc decoder is not applicable.\n");
209 return 0;
210 } else {
211 lavc_codec = avcodec_find_decoder(sh_audio->libav_codec_id);
212 if (!lavc_codec) {
213 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Libavcodec has no decoder "
214 "for this codec\n");
215 return 0;
219 sh_audio->codecname = lavc_codec->long_name;
220 if (!sh_audio->codecname)
221 sh_audio->codecname = lavc_codec->name;
223 struct priv *ctx = talloc_zero(NULL, struct priv);
224 sh_audio->context = ctx;
225 lavc_context = avcodec_alloc_context3(lavc_codec);
226 ctx->avctx = lavc_context;
227 ctx->avframe = avcodec_alloc_frame();
229 // Always try to set - option only exists for AC3 at the moment
230 av_opt_set_double(lavc_context, "drc_scale", opts->drc_level,
231 AV_OPT_SEARCH_CHILDREN);
232 lavc_context->sample_rate = sh_audio->samplerate;
233 lavc_context->bit_rate = sh_audio->i_bps * 8;
234 if (sh_audio->wf) {
235 lavc_context->channels = sh_audio->wf->nChannels;
236 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
237 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
238 lavc_context->block_align = sh_audio->wf->nBlockAlign;
239 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
241 lavc_context->request_channels = opts->audio_output_channels;
242 lavc_context->codec_tag = sh_audio->format; //FOURCC
243 lavc_context->codec_type = AVMEDIA_TYPE_AUDIO;
244 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
246 /* alloc extra data */
247 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
248 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
249 lavc_context->extradata_size = sh_audio->wf->cbSize;
250 memcpy(lavc_context->extradata, sh_audio->wf + 1,
251 lavc_context->extradata_size);
254 // for QDM2
255 if (sh_audio->codecdata_len && sh_audio->codecdata &&
256 !lavc_context->extradata) {
257 lavc_context->extradata = av_malloc(sh_audio->codecdata_len +
258 FF_INPUT_BUFFER_PADDING_SIZE);
259 lavc_context->extradata_size = sh_audio->codecdata_len;
260 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
261 lavc_context->extradata_size);
264 /* open it */
265 if (avcodec_open2(lavc_context, lavc_codec, NULL) < 0) {
266 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Could not open codec.\n");
267 uninit(sh_audio);
268 return 0;
270 mp_msg(MSGT_DECAUDIO, MSGL_V, "INFO: libavcodec \"%s\" init OK!\n",
271 lavc_codec->name);
273 if (sh_audio->format == 0x3343414D) {
274 // MACE 3:1
275 sh_audio->ds->ss_div = 2 * 3; // 1 samples/packet
276 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
277 } else if (sh_audio->format == 0x3643414D) {
278 // MACE 6:1
279 sh_audio->ds->ss_div = 2 * 6; // 1 samples/packet
280 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
283 // Decode at least 1 byte: (to get header filled)
284 for (int tries = 0;;) {
285 int x = decode_audio(sh_audio, sh_audio->a_buffer, 1,
286 sh_audio->a_buffer_size);
287 if (x > 0) {
288 sh_audio->a_buffer_len = x;
289 break;
291 if (++tries >= 5) {
292 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
293 "ad_ffmpeg: initial decode failed\n");
294 uninit(sh_audio);
295 return 0;
299 sh_audio->i_bps = lavc_context->bit_rate / 8;
300 if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec)
301 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
303 return 1;
306 static void uninit(sh_audio_t *sh)
308 sh->codecname = NULL;
309 struct priv *ctx = sh->context;
310 if (!ctx)
311 return;
312 AVCodecContext *lavc_context = ctx->avctx;
314 if (lavc_context) {
315 if (avcodec_close(lavc_context) < 0)
316 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
317 av_freep(&lavc_context->extradata);
318 av_freep(&lavc_context);
320 #ifdef CONFIG_LIBAVRESAMPLE
321 avresample_free(&ctx->avr);
322 #endif
323 #if LIBAVCODEC_VERSION_INT >= (54 << 16 | 28 << 8)
324 avcodec_free_frame(&ctx->avframe);
325 #else
326 av_free(ctx->avframe);
327 #endif
328 talloc_free(ctx);
329 sh->context = NULL;
332 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
334 struct priv *ctx = sh->context;
335 switch (cmd) {
336 case ADCTRL_RESYNC_STREAM:
337 avcodec_flush_buffers(ctx->avctx);
338 ds_clear_parser(sh->ds);
339 ctx->previous_data_left = 0;
340 ctx->output_left = 0;
341 return CONTROL_TRUE;
343 return CONTROL_UNKNOWN;
346 static int decode_new_packet(struct sh_audio *sh)
348 struct priv *priv = sh->context;
349 AVCodecContext *avctx = priv->avctx;
350 double pts = MP_NOPTS_VALUE;
351 int insize;
352 bool packet_already_used = priv->previous_data_left;
353 struct demux_packet *mpkt = ds_get_packet2(sh->ds,
354 priv->previous_data_left);
355 unsigned char *start;
356 if (!mpkt) {
357 assert(!priv->previous_data_left);
358 start = NULL;
359 insize = 0;
360 ds_parse(sh->ds, &start, &insize, pts, 0);
361 if (insize <= 0)
362 return -1; // error or EOF
363 } else {
364 assert(mpkt->len >= priv->previous_data_left);
365 if (!priv->previous_data_left) {
366 priv->previous_data_left = mpkt->len;
367 pts = mpkt->pts;
369 insize = priv->previous_data_left;
370 start = mpkt->buffer + mpkt->len - priv->previous_data_left;
371 int consumed = ds_parse(sh->ds, &start, &insize, pts, 0);
372 priv->previous_data_left -= consumed;
373 priv->previous_data_left = FFMAX(priv->previous_data_left, 0);
376 AVPacket pkt;
377 av_init_packet(&pkt);
378 pkt.data = start;
379 pkt.size = insize;
380 if (mpkt && mpkt->avpacket) {
381 pkt.side_data = mpkt->avpacket->side_data;
382 pkt.side_data_elems = mpkt->avpacket->side_data_elems;
384 if (pts != MP_NOPTS_VALUE && !packet_already_used) {
385 sh->pts = pts;
386 sh->pts_bytes = 0;
388 int got_frame = 0;
389 int ret = avcodec_decode_audio4(avctx, priv->avframe, &got_frame, &pkt);
390 // LATM may need many packets to find mux info
391 if (ret == AVERROR(EAGAIN))
392 return 0;
393 if (ret < 0) {
394 mp_msg(MSGT_DECAUDIO, MSGL_V, "lavc_audio: error\n");
395 return -1;
397 // The "insize >= ret" test is sanity check against decoder overreads
398 if (!sh->parser && insize >= ret)
399 priv->previous_data_left = insize - ret;
400 if (!got_frame)
401 return 0;
403 setup_format(sh);
405 #ifdef CONFIG_LIBAVRESAMPLE
406 if (priv->avr) {
407 int ret;
408 uint64_t needed_size = av_samples_get_buffer_size(NULL, priv->resample_channels,
409 priv->avframe->nb_samples,
410 priv->resample_fmt, 0);
411 if (needed_size > priv->resample_buf_size) {
412 priv->resample_buf = talloc_realloc(priv, priv->resample_buf,
413 uint8_t, needed_size);
414 priv->resample_buf_size = needed_size;
417 ret = avresample_convert(priv->avr, &priv->resample_buf,
418 priv->resample_buf_size, priv->avframe->nb_samples,
419 priv->avframe->extended_data, priv->avframe->linesize[0],
420 priv->avframe->nb_samples);
421 if (ret < 0) {
422 uint8_t error[128];
423 av_strerror(ret, error, sizeof(error));
424 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Error during sample format conversion: %s.\n",
425 error);
426 return -1;
429 assert(ret == priv->avframe->nb_samples);
431 priv->output = priv->resample_buf;
432 priv->output_left = priv->unitsize * ret;
433 } else {
434 #else
435 /* An error is reported later from output format checking, but make
436 * sure we don't crash by overreading first plane. */
437 if (!av_sample_fmt_is_planar(avctx->sample_fmt) || avctx->channels == 1) {
438 #endif
439 uint64_t unitsize = (uint64_t)av_get_bytes_per_sample(avctx->sample_fmt) *
440 avctx->channels;
441 if (unitsize > 100000)
442 abort();
443 priv->unitsize = unitsize;
444 uint64_t output_left = unitsize * priv->avframe->nb_samples;
445 if (output_left > 500000000)
446 abort();
447 priv->output_left = output_left;
448 priv->output = priv->avframe->data[0];
451 mp_dbg(MSGT_DECAUDIO, MSGL_DBG2, "Decoded %d -> %d \n", insize,
452 priv->output_left);
453 return 0;
457 static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen,
458 int maxlen)
460 struct priv *priv = sh_audio->context;
461 AVCodecContext *avctx = priv->avctx;
463 int len = -1;
464 while (len < minlen) {
465 if (!priv->output_left) {
466 if (decode_new_packet(sh_audio) < 0)
467 break;
468 continue;
470 int size = (minlen - len + priv->unitsize - 1);
471 size -= size % priv->unitsize;
472 size = FFMIN(size, priv->output_left);
473 if (size > maxlen)
474 abort();
475 memcpy(buf, priv->output, size);
476 priv->output += size;
477 priv->output_left -= size;
478 if (avctx->channels >= 5) {
479 int samplesize = av_get_bytes_per_sample(avctx->sample_fmt);
480 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
481 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
482 avctx->channels,
483 size / samplesize, samplesize);
485 if (len < 0)
486 len = size;
487 else
488 len += size;
489 buf += size;
490 maxlen -= size;
491 sh_audio->pts_bytes += size;
493 return len;