Add forgotten type to variable declaration.
[mplayer/glamo.git] / libmpcodecs / ad_ffmpeg.c
blobc0a638db8c3a45e5e24c2aaa6c16cdc17ad4bd50
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
5 #include "config.h"
6 #include "mp_msg.h"
7 #include "help_mp.h"
9 #include "ad_internal.h"
10 #include "libaf/reorder_ch.h"
12 #include "mpbswap.h"
14 static ad_info_t info =
16 "FFmpeg/libavcodec audio decoders",
17 "ffmpeg",
18 "Nick Kurshev",
19 "ffmpeg.sf.net",
23 LIBAD_EXTERN(ffmpeg)
25 #define assert(x)
27 #include "libavcodec/avcodec.h"
29 extern int avcodec_initialized;
31 static int preinit(sh_audio_t *sh)
33 sh->audio_out_minsize=AVCODEC_MAX_AUDIO_FRAME_SIZE;
34 return 1;
37 static int init(sh_audio_t *sh_audio)
39 int x;
40 AVCodecContext *lavc_context;
41 AVCodec *lavc_codec;
43 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
44 if(!avcodec_initialized){
45 avcodec_init();
46 avcodec_register_all();
47 avcodec_initialized=1;
50 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
51 if(!lavc_codec){
52 mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh_audio->codec->dll);
53 return 0;
56 lavc_context = avcodec_alloc_context();
57 sh_audio->context=lavc_context;
59 lavc_context->sample_rate = sh_audio->samplerate;
60 lavc_context->bit_rate = sh_audio->i_bps * 8;
61 if(sh_audio->wf){
62 lavc_context->channels = sh_audio->wf->nChannels;
63 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
64 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
65 lavc_context->block_align = sh_audio->wf->nBlockAlign;
66 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
68 lavc_context->request_channels = audio_output_channels;
69 lavc_context->codec_tag = sh_audio->format; //FOURCC
70 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
72 /* alloc extra data */
73 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
74 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
75 lavc_context->extradata_size = sh_audio->wf->cbSize;
76 memcpy(lavc_context->extradata, (char *)sh_audio->wf + sizeof(WAVEFORMATEX),
77 lavc_context->extradata_size);
80 // for QDM2
81 if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata)
83 lavc_context->extradata = av_malloc(sh_audio->codecdata_len);
84 lavc_context->extradata_size = sh_audio->codecdata_len;
85 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
86 lavc_context->extradata_size);
89 /* open it */
90 if (avcodec_open(lavc_context, lavc_codec) < 0) {
91 mp_msg(MSGT_DECAUDIO,MSGL_ERR, MSGTR_CantOpenCodec);
92 return 0;
94 mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec init OK!\n");
96 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
97 if(sh_audio->format==0x3343414D){
98 // MACE 3:1
99 sh_audio->ds->ss_div = 2*3; // 1 samples/packet
100 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
101 } else
102 if(sh_audio->format==0x3643414D){
103 // MACE 6:1
104 sh_audio->ds->ss_div = 2*6; // 1 samples/packet
105 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
108 // Decode at least 1 byte: (to get header filled)
109 x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size);
110 if(x>0) sh_audio->a_buffer_len=x;
112 sh_audio->channels=lavc_context->channels;
113 sh_audio->samplerate=lavc_context->sample_rate;
114 sh_audio->i_bps=lavc_context->bit_rate/8;
115 if(sh_audio->wf){
116 // If the decoder uses the wrong number of channels all is lost anyway.
117 // sh_audio->channels=sh_audio->wf->nChannels;
118 if (sh_audio->wf->nSamplesPerSec)
119 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
120 if (sh_audio->wf->nAvgBytesPerSec)
121 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
123 sh_audio->samplesize=2;
124 return 1;
127 static void uninit(sh_audio_t *sh)
129 AVCodecContext *lavc_context = sh->context;
131 if (avcodec_close(lavc_context) < 0)
132 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantCloseCodec);
133 av_freep(&lavc_context->extradata);
134 av_freep(&lavc_context);
137 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
139 AVCodecContext *lavc_context = sh->context;
140 switch(cmd){
141 case ADCTRL_RESYNC_STREAM:
142 avcodec_flush_buffers(lavc_context);
143 return CONTROL_TRUE;
145 return CONTROL_UNKNOWN;
148 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
150 unsigned char *start=NULL;
151 int y,len=-1;
152 while(len<minlen){
153 int len2=maxlen;
154 double pts;
155 int x=ds_get_packet_pts(sh_audio->ds,&start, &pts);
156 if(x<=0) break; // error
157 if (pts != MP_NOPTS_VALUE) {
158 sh_audio->pts = pts;
159 sh_audio->pts_bytes = 0;
161 y=avcodec_decode_audio2(sh_audio->context,(int16_t*)buf,&len2,start,x);
162 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
163 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
164 if(y<x) sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!)
165 if(len2>0){
166 if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
167 int src_ch_layout = AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT;
168 const char *codec=((AVCodecContext*)sh_audio->context)->codec->name;
169 if (!strcasecmp(codec, "ac3"))
170 src_ch_layout = AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT;
171 else if (!strcasecmp(codec, "dca"))
172 src_ch_layout = AF_CHANNEL_LAYOUT_LAVC_DCA_DEFAULT;
173 else if (!strcasecmp(codec, "libfaad")
174 || !strcasecmp(codec, "mpeg4aac"))
175 src_ch_layout = AF_CHANNEL_LAYOUT_AAC_DEFAULT;
176 else if (!strcasecmp(codec, "liba52"))
177 src_ch_layout = AF_CHANNEL_LAYOUT_LAVC_LIBA52_DEFAULT;
178 else if (!strcasecmp(codec, "vorbis"))
179 src_ch_layout = AF_CHANNEL_LAYOUT_VORBIS_DEFAULT;
180 else if (!strcasecmp(codec, "flac"))
181 src_ch_layout = AF_CHANNEL_LAYOUT_FLAC_DEFAULT;
182 else
183 src_ch_layout = AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT;
184 reorder_channel_nch(buf, src_ch_layout,
185 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
186 ((AVCodecContext *)sh_audio->context)->channels,
187 len2 / 2, 2);
189 //len=len2;break;
190 if(len<0) len=len2; else len+=len2;
191 buf+=len2;
192 maxlen -= len2;
193 sh_audio->pts_bytes += len2;
195 mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2);
197 return len;