Remove HAVE_MATRIXVIEW references
[mplayer/kovensky.git] / libmpcodecs / ad_ffmpeg.c
blobc99cd47fecfe02d286e9e416ecfc3cafba455747
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 const 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 tries = 0;
40 int x;
41 AVCodecContext *lavc_context;
42 AVCodec *lavc_codec;
44 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
45 if(!avcodec_initialized){
46 avcodec_init();
47 avcodec_register_all();
48 avcodec_initialized=1;
51 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
52 if(!lavc_codec){
53 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n",sh_audio->codec->dll);
54 return 0;
57 lavc_context = avcodec_alloc_context();
58 sh_audio->context=lavc_context;
60 lavc_context->sample_rate = sh_audio->samplerate;
61 lavc_context->bit_rate = sh_audio->i_bps * 8;
62 if(sh_audio->wf){
63 lavc_context->channels = sh_audio->wf->nChannels;
64 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
65 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
66 lavc_context->block_align = sh_audio->wf->nBlockAlign;
67 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
69 lavc_context->request_channels = audio_output_channels;
70 lavc_context->codec_tag = sh_audio->format; //FOURCC
71 lavc_context->codec_type = CODEC_TYPE_AUDIO;
72 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
74 /* alloc extra data */
75 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
76 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
77 lavc_context->extradata_size = sh_audio->wf->cbSize;
78 memcpy(lavc_context->extradata, (char *)sh_audio->wf + sizeof(WAVEFORMATEX),
79 lavc_context->extradata_size);
82 // for QDM2
83 if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata)
85 lavc_context->extradata = av_malloc(sh_audio->codecdata_len);
86 lavc_context->extradata_size = sh_audio->codecdata_len;
87 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
88 lavc_context->extradata_size);
91 /* open it */
92 if (avcodec_open(lavc_context, lavc_codec) < 0) {
93 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR, "Could not open codec.\n");
94 return 0;
96 mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec->name);
98 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
99 if(sh_audio->format==0x3343414D){
100 // MACE 3:1
101 sh_audio->ds->ss_div = 2*3; // 1 samples/packet
102 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
103 } else
104 if(sh_audio->format==0x3643414D){
105 // MACE 6:1
106 sh_audio->ds->ss_div = 2*6; // 1 samples/packet
107 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
110 // Decode at least 1 byte: (to get header filled)
111 do {
112 x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size);
113 } while (x <= 0 && tries++ < 5);
114 if(x>0) sh_audio->a_buffer_len=x;
116 sh_audio->channels=lavc_context->channels;
117 sh_audio->samplerate=lavc_context->sample_rate;
118 sh_audio->i_bps=lavc_context->bit_rate/8;
119 switch (lavc_context->sample_fmt) {
120 case SAMPLE_FMT_U8: sh_audio->sample_format = AF_FORMAT_U8; break;
121 case SAMPLE_FMT_S16: sh_audio->sample_format = AF_FORMAT_S16_NE; break;
122 case SAMPLE_FMT_S32: sh_audio->sample_format = AF_FORMAT_S32_NE; break;
123 case SAMPLE_FMT_FLT: sh_audio->sample_format = AF_FORMAT_FLOAT_NE; break;
124 default:
125 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
126 return 0;
128 if(sh_audio->wf){
129 // If the decoder uses the wrong number of channels all is lost anyway.
130 // sh_audio->channels=sh_audio->wf->nChannels;
131 if (sh_audio->wf->nSamplesPerSec)
132 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
133 if (sh_audio->wf->nAvgBytesPerSec)
134 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
136 sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8;
137 return 1;
140 static void uninit(sh_audio_t *sh)
142 AVCodecContext *lavc_context = sh->context;
144 if (avcodec_close(lavc_context) < 0)
145 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
146 av_freep(&lavc_context->extradata);
147 av_freep(&lavc_context);
150 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
152 AVCodecContext *lavc_context = sh->context;
153 switch(cmd){
154 case ADCTRL_RESYNC_STREAM:
155 avcodec_flush_buffers(lavc_context);
156 ds_clear_parser(sh->ds);
157 return CONTROL_TRUE;
159 return CONTROL_UNKNOWN;
162 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
164 unsigned char *start=NULL;
165 int y,len=-1;
166 while(len<minlen){
167 AVPacket pkt;
168 int len2=maxlen;
169 double pts;
170 int x=ds_get_packet_pts(sh_audio->ds,&start, &pts);
171 if(x<=0) {
172 start = NULL;
173 x = 0;
174 ds_parse(sh_audio->ds, &start, &x, MP_NOPTS_VALUE, 0);
175 if (x <= 0)
176 break; // error
177 } else {
178 int in_size = x;
179 int consumed = ds_parse(sh_audio->ds, &start, &x, pts, 0);
180 sh_audio->ds->buffer_pos -= in_size - consumed;
182 av_init_packet(&pkt);
183 pkt.data = start;
184 pkt.size = x;
185 if (pts != MP_NOPTS_VALUE) {
186 sh_audio->pts = pts;
187 sh_audio->pts_bytes = 0;
189 y=avcodec_decode_audio3(sh_audio->context,(int16_t*)buf,&len2,&pkt);
190 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
191 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
192 if(!sh_audio->parser && y<x)
193 sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!)
194 if(len2>0){
195 if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
196 int samplesize = av_get_bits_per_sample_format(((AVCodecContext *)
197 sh_audio->context)->sample_fmt) / 8;
198 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
199 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
200 ((AVCodecContext *)sh_audio->context)->channels,
201 len2 / samplesize, samplesize);
203 //len=len2;break;
204 if(len<0) len=len2; else len+=len2;
205 buf+=len2;
206 maxlen -= len2;
207 sh_audio->pts_bytes += len2;
209 mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2);
211 return len;