cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / libmpcodecs / ad_ffmpeg.c
blob4a5062ba00575a4fa58da092c32b7b5ba134fe3d
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/opt.h>
28 #include "talloc.h"
30 #include "config.h"
31 #include "mp_msg.h"
32 #include "options.h"
34 #include "ad_internal.h"
35 #include "libaf/reorder_ch.h"
37 #include "mpbswap.h"
39 static const ad_info_t info =
41 "FFmpeg/libavcodec audio decoders",
42 "ffmpeg",
43 "Nick Kurshev",
44 "ffmpeg.sf.net",
48 LIBAD_EXTERN(ffmpeg)
50 struct priv {
51 AVCodecContext *avctx;
52 int previous_data_left;
55 static int preinit(sh_audio_t *sh)
57 sh->audio_out_minsize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
58 return 1;
61 /* Prefer playing audio with the samplerate given in container data
62 * if available, but take number the number of channels and sample format
63 * from the codec, since if the codec isn't using the correct values for
64 * those everything breaks anyway.
66 static int setup_format(sh_audio_t *sh_audio,
67 const AVCodecContext *lavc_context)
69 int sample_format = sh_audio->sample_format;
70 switch (lavc_context->sample_fmt) {
71 case AV_SAMPLE_FMT_U8: sample_format = AF_FORMAT_U8; break;
72 case AV_SAMPLE_FMT_S16: sample_format = AF_FORMAT_S16_NE; break;
73 case AV_SAMPLE_FMT_S32: sample_format = AF_FORMAT_S32_NE; break;
74 case AV_SAMPLE_FMT_FLT: sample_format = AF_FORMAT_FLOAT_NE; break;
75 default:
76 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
79 bool broken_srate = false;
80 int samplerate = lavc_context->sample_rate;
81 int container_samplerate = sh_audio->container_out_samplerate;
82 if (!container_samplerate && sh_audio->wf)
83 container_samplerate = sh_audio->wf->nSamplesPerSec;
84 if (lavc_context->codec_id == CODEC_ID_AAC
85 && samplerate == 2 * container_samplerate)
86 broken_srate = true;
87 else if (container_samplerate)
88 samplerate = container_samplerate;
90 if (lavc_context->channels != sh_audio->channels ||
91 samplerate != sh_audio->samplerate ||
92 sample_format != sh_audio->sample_format) {
93 sh_audio->channels = lavc_context->channels;
94 sh_audio->samplerate = samplerate;
95 sh_audio->sample_format = sample_format;
96 sh_audio->samplesize = af_fmt2bits(sh_audio->sample_format) / 8;
97 if (broken_srate)
98 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
99 "Ignoring broken container sample rate for AAC with SBR\n");
100 return 1;
102 return 0;
105 static int init(sh_audio_t *sh_audio)
107 struct MPOpts *opts = sh_audio->opts;
108 AVCodecContext *lavc_context;
109 AVCodec *lavc_codec;
111 mp_msg(MSGT_DECAUDIO, MSGL_V, "FFmpeg's libavcodec audio codec\n");
113 lavc_codec = avcodec_find_decoder_by_name(sh_audio->codec->dll);
114 if (!lavc_codec) {
115 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
116 "Cannot find codec '%s' in libavcodec...\n",
117 sh_audio->codec->dll);
118 return 0;
121 struct priv *ctx = talloc_zero(NULL, struct priv);
122 sh_audio->context = ctx;
123 lavc_context = avcodec_alloc_context3(lavc_codec);
124 ctx->avctx = lavc_context;
126 // Always try to set - option only exists for AC3 at the moment
127 av_opt_set_double(lavc_context, "drc_scale", opts->drc_level,
128 AV_OPT_SEARCH_CHILDREN);
129 lavc_context->sample_rate = sh_audio->samplerate;
130 lavc_context->bit_rate = sh_audio->i_bps * 8;
131 if (sh_audio->wf) {
132 lavc_context->channels = sh_audio->wf->nChannels;
133 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
134 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
135 lavc_context->block_align = sh_audio->wf->nBlockAlign;
136 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
138 lavc_context->request_channels = opts->audio_output_channels;
139 lavc_context->codec_tag = sh_audio->format; //FOURCC
140 lavc_context->codec_type = AVMEDIA_TYPE_AUDIO;
141 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
143 /* alloc extra data */
144 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
145 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
146 lavc_context->extradata_size = sh_audio->wf->cbSize;
147 memcpy(lavc_context->extradata, sh_audio->wf + 1,
148 lavc_context->extradata_size);
151 // for QDM2
152 if (sh_audio->codecdata_len && sh_audio->codecdata &&
153 !lavc_context->extradata) {
154 lavc_context->extradata = av_malloc(sh_audio->codecdata_len +
155 FF_INPUT_BUFFER_PADDING_SIZE);
156 lavc_context->extradata_size = sh_audio->codecdata_len;
157 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
158 lavc_context->extradata_size);
161 /* open it */
162 if (avcodec_open2(lavc_context, lavc_codec, NULL) < 0) {
163 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Could not open codec.\n");
164 uninit(sh_audio);
165 return 0;
167 mp_msg(MSGT_DECAUDIO, MSGL_V, "INFO: libavcodec \"%s\" init OK!\n",
168 lavc_codec->name);
170 if (sh_audio->format == 0x3343414D) {
171 // MACE 3:1
172 sh_audio->ds->ss_div = 2 * 3; // 1 samples/packet
173 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
174 } else if (sh_audio->format == 0x3643414D) {
175 // MACE 6:1
176 sh_audio->ds->ss_div = 2 * 6; // 1 samples/packet
177 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
180 // Decode at least 1 byte: (to get header filled)
181 for (int tries = 0;;) {
182 int x = decode_audio(sh_audio, sh_audio->a_buffer, 1,
183 sh_audio->a_buffer_size);
184 if (x > 0) {
185 sh_audio->a_buffer_len = x;
186 break;
188 if (++tries >= 5) {
189 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
190 "ad_ffmpeg: initial decode failed\n");
191 uninit(sh_audio);
192 return 0;
196 sh_audio->i_bps = lavc_context->bit_rate / 8;
197 if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec)
198 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
200 switch (lavc_context->sample_fmt) {
201 case AV_SAMPLE_FMT_U8:
202 case AV_SAMPLE_FMT_S16:
203 case AV_SAMPLE_FMT_S32:
204 case AV_SAMPLE_FMT_FLT:
205 break;
206 default:
207 uninit(sh_audio);
208 return 0;
210 return 1;
213 static void uninit(sh_audio_t *sh)
215 struct priv *ctx = sh->context;
216 if (!ctx)
217 return;
218 AVCodecContext *lavc_context = ctx->avctx;
220 if (lavc_context) {
221 if (avcodec_close(lavc_context) < 0)
222 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
223 av_freep(&lavc_context->extradata);
224 av_freep(&lavc_context);
226 talloc_free(ctx);
227 sh->context = NULL;
230 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
232 struct priv *ctx = sh->context;
233 switch (cmd) {
234 case ADCTRL_RESYNC_STREAM:
235 avcodec_flush_buffers(ctx->avctx);
236 ds_clear_parser(sh->ds);
237 ctx->previous_data_left = 0;
238 return CONTROL_TRUE;
240 return CONTROL_UNKNOWN;
243 static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen,
244 int maxlen)
246 struct priv *ctx = sh_audio->context;
247 AVCodecContext *avctx = ctx->avctx;
249 unsigned char *start = NULL;
250 int y, len = -1;
251 while (len < minlen) {
252 AVPacket pkt;
253 int len2 = maxlen;
254 double pts = MP_NOPTS_VALUE;
255 int x;
256 bool packet_already_used = ctx->previous_data_left;
257 struct demux_packet *mpkt = ds_get_packet2(sh_audio->ds,
258 ctx->previous_data_left);
259 if (!mpkt) {
260 assert(!ctx->previous_data_left);
261 start = NULL;
262 x = 0;
263 ds_parse(sh_audio->ds, &start, &x, pts, 0);
264 if (x <= 0)
265 break; // error
266 } else {
267 assert(mpkt->len >= ctx->previous_data_left);
268 if (!ctx->previous_data_left) {
269 ctx->previous_data_left = mpkt->len;
270 pts = mpkt->pts;
272 x = ctx->previous_data_left;
273 start = mpkt->buffer + mpkt->len - ctx->previous_data_left;
274 int consumed = ds_parse(sh_audio->ds, &start, &x, pts, 0);
275 ctx->previous_data_left -= consumed;
277 av_init_packet(&pkt);
278 pkt.data = start;
279 pkt.size = x;
280 if (mpkt && mpkt->avpacket) {
281 pkt.side_data = mpkt->avpacket->side_data;
282 pkt.side_data_elems = mpkt->avpacket->side_data_elems;
284 if (pts != MP_NOPTS_VALUE && !packet_already_used) {
285 sh_audio->pts = pts;
286 sh_audio->pts_bytes = 0;
288 y = avcodec_decode_audio3(avctx, (int16_t *)buf, &len2, &pkt);
289 // LATM may need many packets to find mux info
290 if (y == AVERROR(EAGAIN))
291 continue;
292 if (y < 0) {
293 mp_msg(MSGT_DECAUDIO, MSGL_V, "lavc_audio: error\n");
294 break;
296 if (!sh_audio->parser)
297 ctx->previous_data_left += x - y;
298 if (len2 > 0) {
299 if (avctx->channels >= 5) {
300 int samplesize = av_get_bytes_per_sample(avctx->sample_fmt);
301 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
302 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
303 avctx->channels,
304 len2 / samplesize, samplesize);
306 if (len < 0)
307 len = len2;
308 else
309 len += len2;
310 buf += len2;
311 maxlen -= len2;
312 sh_audio->pts_bytes += len2;
314 mp_dbg(MSGT_DECAUDIO, MSGL_DBG2, "Decoded %d -> %d \n", y, len2);
316 if (setup_format(sh_audio, avctx))
317 break;
319 return len;