Ignore svn change r30495
[mplayer/kovensky.git] / libmpcodecs / ad_ffmpeg.c
blob9cf0567d6ad7cc33100ba26e54b517d7958c5e67
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 "help_mp.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 init(sh_audio_t *sh_audio)
57 int tries = 0;
58 int x;
59 AVCodecContext *lavc_context;
60 AVCodec *lavc_codec;
62 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
63 if(!avcodec_initialized){
64 avcodec_init();
65 avcodec_register_all();
66 avcodec_initialized=1;
69 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
70 if(!lavc_codec){
71 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n",sh_audio->codec->dll);
72 return 0;
75 lavc_context = avcodec_alloc_context();
76 sh_audio->context=lavc_context;
78 lavc_context->sample_rate = sh_audio->samplerate;
79 lavc_context->bit_rate = sh_audio->i_bps * 8;
80 if(sh_audio->wf){
81 lavc_context->channels = sh_audio->wf->nChannels;
82 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
83 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
84 lavc_context->block_align = sh_audio->wf->nBlockAlign;
85 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
87 lavc_context->request_channels = audio_output_channels;
88 lavc_context->codec_tag = sh_audio->format; //FOURCC
89 lavc_context->codec_type = CODEC_TYPE_AUDIO;
90 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
92 /* alloc extra data */
93 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
94 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
95 lavc_context->extradata_size = sh_audio->wf->cbSize;
96 memcpy(lavc_context->extradata, (char *)sh_audio->wf + sizeof(WAVEFORMATEX),
97 lavc_context->extradata_size);
100 // for QDM2
101 if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata)
103 lavc_context->extradata = av_malloc(sh_audio->codecdata_len);
104 lavc_context->extradata_size = sh_audio->codecdata_len;
105 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
106 lavc_context->extradata_size);
109 /* open it */
110 if (avcodec_open(lavc_context, lavc_codec) < 0) {
111 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR, "Could not open codec.\n");
112 return 0;
114 mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec->name);
116 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
117 if(sh_audio->format==0x3343414D){
118 // MACE 3:1
119 sh_audio->ds->ss_div = 2*3; // 1 samples/packet
120 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
121 } else
122 if(sh_audio->format==0x3643414D){
123 // MACE 6:1
124 sh_audio->ds->ss_div = 2*6; // 1 samples/packet
125 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
128 // Decode at least 1 byte: (to get header filled)
129 do {
130 x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size);
131 } while (x <= 0 && tries++ < 5);
132 if(x>0) sh_audio->a_buffer_len=x;
134 sh_audio->channels=lavc_context->channels;
135 sh_audio->samplerate=lavc_context->sample_rate;
136 sh_audio->i_bps=lavc_context->bit_rate/8;
137 switch (lavc_context->sample_fmt) {
138 case SAMPLE_FMT_U8: sh_audio->sample_format = AF_FORMAT_U8; break;
139 case SAMPLE_FMT_S16: sh_audio->sample_format = AF_FORMAT_S16_NE; break;
140 case SAMPLE_FMT_S32: sh_audio->sample_format = AF_FORMAT_S32_NE; break;
141 case SAMPLE_FMT_FLT: sh_audio->sample_format = AF_FORMAT_FLOAT_NE; break;
142 default:
143 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
144 return 0;
146 if(sh_audio->wf){
147 // If the decoder uses the wrong number of channels all is lost anyway.
148 // sh_audio->channels=sh_audio->wf->nChannels;
149 if (sh_audio->wf->nSamplesPerSec)
150 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
151 if (sh_audio->wf->nAvgBytesPerSec)
152 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
154 sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8;
155 return 1;
158 static void uninit(sh_audio_t *sh)
160 AVCodecContext *lavc_context = sh->context;
162 if (avcodec_close(lavc_context) < 0)
163 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
164 av_freep(&lavc_context->extradata);
165 av_freep(&lavc_context);
168 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
170 AVCodecContext *lavc_context = sh->context;
171 switch(cmd){
172 case ADCTRL_RESYNC_STREAM:
173 avcodec_flush_buffers(lavc_context);
174 ds_clear_parser(sh->ds);
175 return CONTROL_TRUE;
177 return CONTROL_UNKNOWN;
180 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
182 unsigned char *start=NULL;
183 int y,len=-1;
184 while(len<minlen){
185 AVPacket pkt;
186 int len2=maxlen;
187 double pts;
188 int x=ds_get_packet_pts(sh_audio->ds,&start, &pts);
189 if(x<=0) {
190 start = NULL;
191 x = 0;
192 ds_parse(sh_audio->ds, &start, &x, MP_NOPTS_VALUE, 0);
193 if (x <= 0)
194 break; // error
195 } else {
196 int in_size = x;
197 int consumed = ds_parse(sh_audio->ds, &start, &x, pts, 0);
198 sh_audio->ds->buffer_pos -= in_size - consumed;
200 av_init_packet(&pkt);
201 pkt.data = start;
202 pkt.size = x;
203 if (pts != MP_NOPTS_VALUE) {
204 sh_audio->pts = pts;
205 sh_audio->pts_bytes = 0;
207 y=avcodec_decode_audio3(sh_audio->context,(int16_t*)buf,&len2,&pkt);
208 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
209 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
210 if(!sh_audio->parser && y<x)
211 sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!)
212 if(len2>0){
213 if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
214 int samplesize = av_get_bits_per_sample_format(((AVCodecContext *)
215 sh_audio->context)->sample_fmt) / 8;
216 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
217 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
218 ((AVCodecContext *)sh_audio->context)->channels,
219 len2 / samplesize, samplesize);
221 //len=len2;break;
222 if(len<0) len=len2; else len+=len2;
223 buf+=len2;
224 maxlen -= len2;
225 sh_audio->pts_bytes += len2;
227 mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2);
229 return len;