Makefile: use $(RM) builtin
[mplayer.git] / libmpcodecs / ad_ffmpeg.c
blobb4ecd628c43eb72c9e2f704b190cc6422b13e13a
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>
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "options.h"
27 #include "ad_internal.h"
28 #include "vd_ffmpeg.h"
29 #include "libaf/reorder_ch.h"
31 #include "mpbswap.h"
33 static const ad_info_t info =
35 "FFmpeg/libavcodec audio decoders",
36 "ffmpeg",
37 "Nick Kurshev",
38 "ffmpeg.sf.net",
42 LIBAD_EXTERN(ffmpeg)
44 #define assert(x)
46 #include "libavcodec/avcodec.h"
49 static int preinit(sh_audio_t *sh)
51 sh->audio_out_minsize=AVCODEC_MAX_AUDIO_FRAME_SIZE;
52 return 1;
55 /* Prefer playing audio with the samplerate given in container data
56 * if available, but take number the number of channels and sample format
57 * from the codec, since if the codec isn't using the correct values for
58 * those everything breaks anyway.
60 static int setup_format(sh_audio_t *sh_audio, const AVCodecContext *lavc_context)
62 int sample_format = sh_audio->sample_format;
63 switch (lavc_context->sample_fmt) {
64 case SAMPLE_FMT_U8: sample_format = AF_FORMAT_U8; break;
65 case SAMPLE_FMT_S16: sample_format = AF_FORMAT_S16_NE; break;
66 case SAMPLE_FMT_S32: sample_format = AF_FORMAT_S32_NE; break;
67 case SAMPLE_FMT_FLT: sample_format = AF_FORMAT_FLOAT_NE; break;
68 default:
69 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
72 bool broken_srate = false;
73 int samplerate = lavc_context->sample_rate;
74 int container_samplerate = sh_audio->container_out_samplerate;
75 if (!container_samplerate && sh_audio->wf)
76 container_samplerate = sh_audio->wf->nSamplesPerSec;
77 if (lavc_context->codec_id == CODEC_ID_AAC
78 && samplerate == 2 * container_samplerate)
79 broken_srate = true;
80 else if (container_samplerate)
81 samplerate = container_samplerate;
83 if (lavc_context->channels != sh_audio->channels ||
84 samplerate != sh_audio->samplerate ||
85 sample_format != sh_audio->sample_format) {
86 sh_audio->channels=lavc_context->channels;
87 sh_audio->samplerate=samplerate;
88 sh_audio->sample_format = sample_format;
89 sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8;
90 if (broken_srate)
91 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
92 "Ignoring broken container sample rate for AAC with SBR\n");
93 return 1;
95 return 0;
98 static int init(sh_audio_t *sh_audio)
100 struct MPOpts *opts = sh_audio->opts;
101 AVCodecContext *lavc_context;
102 AVCodec *lavc_codec;
104 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
105 init_avcodec();
107 lavc_codec = avcodec_find_decoder_by_name(sh_audio->codec->dll);
108 if(!lavc_codec){
109 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n",sh_audio->codec->dll);
110 return 0;
113 lavc_context = avcodec_alloc_context();
114 sh_audio->context=lavc_context;
116 lavc_context->drc_scale = opts->drc_level;
117 lavc_context->sample_rate = sh_audio->samplerate;
118 lavc_context->bit_rate = sh_audio->i_bps * 8;
119 if(sh_audio->wf){
120 lavc_context->channels = sh_audio->wf->nChannels;
121 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
122 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
123 lavc_context->block_align = sh_audio->wf->nBlockAlign;
124 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
126 lavc_context->request_channels = opts->audio_output_channels;
127 lavc_context->codec_tag = sh_audio->format; //FOURCC
128 lavc_context->codec_type = AVMEDIA_TYPE_AUDIO;
129 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
131 /* alloc extra data */
132 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
133 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
134 lavc_context->extradata_size = sh_audio->wf->cbSize;
135 memcpy(lavc_context->extradata, sh_audio->wf + 1,
136 lavc_context->extradata_size);
139 // for QDM2
140 if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata)
142 lavc_context->extradata = av_malloc(sh_audio->codecdata_len +
143 FF_INPUT_BUFFER_PADDING_SIZE);
144 lavc_context->extradata_size = sh_audio->codecdata_len;
145 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
146 lavc_context->extradata_size);
149 /* open it */
150 if (avcodec_open(lavc_context, lavc_codec) < 0) {
151 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR, "Could not open codec.\n");
152 return 0;
154 mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec->name);
156 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
157 if(sh_audio->format==0x3343414D){
158 // MACE 3:1
159 sh_audio->ds->ss_div = 2*3; // 1 samples/packet
160 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
161 } else
162 if(sh_audio->format==0x3643414D){
163 // MACE 6:1
164 sh_audio->ds->ss_div = 2*6; // 1 samples/packet
165 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
168 // Decode at least 1 byte: (to get header filled)
169 for (int tries = 0;;) {
170 int x = decode_audio(sh_audio, sh_audio->a_buffer, 1,
171 sh_audio->a_buffer_size);
172 if (x > 0) {
173 sh_audio->a_buffer_len = x;
174 break;
176 if (++tries >= 5) {
177 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
178 "ad_ffmpeg: initial decode failed\n");
179 return 0;
183 sh_audio->i_bps=lavc_context->bit_rate/8;
184 if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec)
185 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
187 switch (lavc_context->sample_fmt) {
188 case SAMPLE_FMT_U8:
189 case SAMPLE_FMT_S16:
190 case SAMPLE_FMT_S32:
191 case SAMPLE_FMT_FLT:
192 break;
193 default:
194 return 0;
196 return 1;
199 static void uninit(sh_audio_t *sh)
201 AVCodecContext *lavc_context = sh->context;
203 if (avcodec_close(lavc_context) < 0)
204 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
205 av_freep(&lavc_context->extradata);
206 av_freep(&lavc_context);
209 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
211 AVCodecContext *lavc_context = sh->context;
212 switch(cmd){
213 case ADCTRL_RESYNC_STREAM:
214 avcodec_flush_buffers(lavc_context);
215 ds_clear_parser(sh->ds);
216 return CONTROL_TRUE;
218 return CONTROL_UNKNOWN;
221 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
223 unsigned char *start=NULL;
224 int y,len=-1;
225 while(len<minlen){
226 AVPacket pkt;
227 int len2=maxlen;
228 double pts;
229 int x=ds_get_packet_pts(sh_audio->ds,&start, &pts);
230 if(x<=0) {
231 start = NULL;
232 x = 0;
233 ds_parse(sh_audio->ds, &start, &x, MP_NOPTS_VALUE, 0);
234 if (x <= 0)
235 break; // error
236 } else {
237 int in_size = x;
238 int consumed = ds_parse(sh_audio->ds, &start, &x, pts, 0);
239 sh_audio->ds->buffer_pos -= in_size - consumed;
241 av_init_packet(&pkt);
242 pkt.data = start;
243 pkt.size = x;
244 if (pts != MP_NOPTS_VALUE) {
245 sh_audio->pts = pts;
246 sh_audio->pts_bytes = 0;
248 y=avcodec_decode_audio3(sh_audio->context,(int16_t*)buf,&len2,&pkt);
249 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
250 // LATM may need many packets to find mux info
251 if (y == AVERROR(EAGAIN))
252 continue;
253 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
254 if(!sh_audio->parser && y<x)
255 sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!)
256 if(len2>0){
257 if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
258 int samplesize = av_get_bits_per_sample_format(((AVCodecContext *)
259 sh_audio->context)->sample_fmt) / 8;
260 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
261 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
262 ((AVCodecContext *)sh_audio->context)->channels,
263 len2 / samplesize, samplesize);
265 //len=len2;break;
266 if(len<0) len=len2; else len+=len2;
267 buf+=len2;
268 maxlen -= len2;
269 sh_audio->pts_bytes += len2;
271 mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2);
273 if (setup_format(sh_audio, sh_audio->context))
274 break;
276 return len;