vo_corevideo: Simplify update_screen_info
[mplayer/glamo.git] / libmpcodecs / ad_ffmpeg.c
blob6d27a314f6d289a7c6bd71ee4e20451bcc051cb9
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 "libaf/reorder_ch.h"
30 #include "mpbswap.h"
32 static const ad_info_t info =
34 "FFmpeg/libavcodec audio decoders",
35 "ffmpeg",
36 "Nick Kurshev",
37 "ffmpeg.sf.net",
41 LIBAD_EXTERN(ffmpeg)
43 #define assert(x)
45 #include "libavcodec/avcodec.h"
47 extern int avcodec_initialized;
49 static int preinit(sh_audio_t *sh)
51 sh->audio_out_minsize=AVCODEC_MAX_AUDIO_FRAME_SIZE;
52 return 1;
55 static int setup_format(sh_audio_t *sh_audio, const AVCodecContext *lavc_context)
57 int broken_srate = 0;
58 int samplerate = lavc_context->sample_rate;
59 int sample_format = sh_audio->sample_format;
60 switch (lavc_context->sample_fmt) {
61 case SAMPLE_FMT_U8: sample_format = AF_FORMAT_U8; break;
62 case SAMPLE_FMT_S16: sample_format = AF_FORMAT_S16_NE; break;
63 case SAMPLE_FMT_S32: sample_format = AF_FORMAT_S32_NE; break;
64 case SAMPLE_FMT_FLT: sample_format = AF_FORMAT_FLOAT_NE; break;
65 default:
66 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
68 if(sh_audio->wf){
69 // If the decoder uses the wrong number of channels all is lost anyway.
70 // sh_audio->channels=sh_audio->wf->nChannels;
72 if (lavc_context->codec_id == CODEC_ID_AAC &&
73 samplerate == 2*sh_audio->wf->nSamplesPerSec) {
74 broken_srate = 1;
75 } else if (sh_audio->wf->nSamplesPerSec)
76 samplerate=sh_audio->wf->nSamplesPerSec;
78 if (lavc_context->channels != sh_audio->channels ||
79 samplerate != sh_audio->samplerate ||
80 sample_format != sh_audio->sample_format) {
81 sh_audio->channels=lavc_context->channels;
82 sh_audio->samplerate=samplerate;
83 sh_audio->sample_format = sample_format;
84 sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8;
85 if (broken_srate)
86 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
87 "Ignoring broken container sample rate for AAC with SBR\n");
88 return 1;
90 return 0;
93 static int init(sh_audio_t *sh_audio)
95 struct MPOpts *opts = sh_audio->opts;
96 int tries = 0;
97 int x;
98 AVCodecContext *lavc_context;
99 AVCodec *lavc_codec;
101 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
102 if(!avcodec_initialized){
103 avcodec_init();
104 avcodec_register_all();
105 avcodec_initialized=1;
108 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
109 if(!lavc_codec){
110 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n",sh_audio->codec->dll);
111 return 0;
114 lavc_context = avcodec_alloc_context();
115 sh_audio->context=lavc_context;
117 lavc_context->drc_scale = opts->drc_level;
118 lavc_context->sample_rate = sh_audio->samplerate;
119 lavc_context->bit_rate = sh_audio->i_bps * 8;
120 if(sh_audio->wf){
121 lavc_context->channels = sh_audio->wf->nChannels;
122 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
123 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
124 lavc_context->block_align = sh_audio->wf->nBlockAlign;
125 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
127 lavc_context->request_channels = audio_output_channels;
128 lavc_context->codec_tag = sh_audio->format; //FOURCC
129 lavc_context->codec_type = CODEC_TYPE_AUDIO;
130 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
132 /* alloc extra data */
133 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
134 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
135 lavc_context->extradata_size = sh_audio->wf->cbSize;
136 memcpy(lavc_context->extradata, (char *)sh_audio->wf + sizeof(WAVEFORMATEX),
137 lavc_context->extradata_size);
140 // for QDM2
141 if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata)
143 lavc_context->extradata = av_malloc(sh_audio->codecdata_len);
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 do {
170 x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size);
171 } while (x <= 0 && tries++ < 5);
172 if(x>0) sh_audio->a_buffer_len=x;
174 sh_audio->i_bps=lavc_context->bit_rate/8;
175 if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec)
176 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
178 switch (lavc_context->sample_fmt) {
179 case SAMPLE_FMT_U8:
180 case SAMPLE_FMT_S16:
181 case SAMPLE_FMT_S32:
182 case SAMPLE_FMT_FLT:
183 break;
184 default:
185 return 0;
187 return 1;
190 static void uninit(sh_audio_t *sh)
192 AVCodecContext *lavc_context = sh->context;
194 if (avcodec_close(lavc_context) < 0)
195 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
196 av_freep(&lavc_context->extradata);
197 av_freep(&lavc_context);
200 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
202 AVCodecContext *lavc_context = sh->context;
203 switch(cmd){
204 case ADCTRL_RESYNC_STREAM:
205 avcodec_flush_buffers(lavc_context);
206 ds_clear_parser(sh->ds);
207 return CONTROL_TRUE;
209 return CONTROL_UNKNOWN;
212 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
214 unsigned char *start=NULL;
215 int y,len=-1;
216 while(len<minlen){
217 AVPacket pkt;
218 int len2=maxlen;
219 double pts;
220 int x=ds_get_packet_pts(sh_audio->ds,&start, &pts);
221 if(x<=0) {
222 start = NULL;
223 x = 0;
224 ds_parse(sh_audio->ds, &start, &x, MP_NOPTS_VALUE, 0);
225 if (x <= 0)
226 break; // error
227 } else {
228 int in_size = x;
229 int consumed = ds_parse(sh_audio->ds, &start, &x, pts, 0);
230 sh_audio->ds->buffer_pos -= in_size - consumed;
232 av_init_packet(&pkt);
233 pkt.data = start;
234 pkt.size = x;
235 if (pts != MP_NOPTS_VALUE) {
236 sh_audio->pts = pts;
237 sh_audio->pts_bytes = 0;
239 y=avcodec_decode_audio3(sh_audio->context,(int16_t*)buf,&len2,&pkt);
240 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
241 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
242 if(!sh_audio->parser && y<x)
243 sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!)
244 if(len2>0){
245 if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
246 int samplesize = av_get_bits_per_sample_format(((AVCodecContext *)
247 sh_audio->context)->sample_fmt) / 8;
248 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
249 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
250 ((AVCodecContext *)sh_audio->context)->channels,
251 len2 / samplesize, samplesize);
253 //len=len2;break;
254 if(len<0) len=len2; else len+=len2;
255 buf+=len2;
256 maxlen -= len2;
257 sh_audio->pts_bytes += len2;
259 mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2);
261 if (setup_format(sh_audio, sh_audio->context))
262 break;
264 return len;