subs: Add support for DVB and XSUB subtitles, not yet working properly
[mplayer/glamo.git] / libmpcodecs / ad_ffmpeg.c
blob8578314bc60aea512a1eb6cace5820e4bd1bdc57
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 init(sh_audio_t *sh_audio)
57 struct MPOpts *opts = sh_audio->opts;
58 int tries = 0;
59 int x;
60 AVCodecContext *lavc_context;
61 AVCodec *lavc_codec;
63 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
64 if(!avcodec_initialized){
65 avcodec_init();
66 avcodec_register_all();
67 avcodec_initialized=1;
70 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
71 if(!lavc_codec){
72 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n",sh_audio->codec->dll);
73 return 0;
76 lavc_context = avcodec_alloc_context();
77 sh_audio->context=lavc_context;
79 lavc_context->drc_scale = opts->drc_level;
80 lavc_context->sample_rate = sh_audio->samplerate;
81 lavc_context->bit_rate = sh_audio->i_bps * 8;
82 if(sh_audio->wf){
83 lavc_context->channels = sh_audio->wf->nChannels;
84 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
85 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
86 lavc_context->block_align = sh_audio->wf->nBlockAlign;
87 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
89 lavc_context->request_channels = audio_output_channels;
90 lavc_context->codec_tag = sh_audio->format; //FOURCC
91 lavc_context->codec_type = CODEC_TYPE_AUDIO;
92 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
94 /* alloc extra data */
95 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
96 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
97 lavc_context->extradata_size = sh_audio->wf->cbSize;
98 memcpy(lavc_context->extradata, (char *)sh_audio->wf + sizeof(WAVEFORMATEX),
99 lavc_context->extradata_size);
102 // for QDM2
103 if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata)
105 lavc_context->extradata = av_malloc(sh_audio->codecdata_len);
106 lavc_context->extradata_size = sh_audio->codecdata_len;
107 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
108 lavc_context->extradata_size);
111 /* open it */
112 if (avcodec_open(lavc_context, lavc_codec) < 0) {
113 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR, "Could not open codec.\n");
114 return 0;
116 mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec->name);
118 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
119 if(sh_audio->format==0x3343414D){
120 // MACE 3:1
121 sh_audio->ds->ss_div = 2*3; // 1 samples/packet
122 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
123 } else
124 if(sh_audio->format==0x3643414D){
125 // MACE 6:1
126 sh_audio->ds->ss_div = 2*6; // 1 samples/packet
127 sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet
130 // Decode at least 1 byte: (to get header filled)
131 do {
132 x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size);
133 } while (x <= 0 && tries++ < 5);
134 if(x>0) sh_audio->a_buffer_len=x;
136 sh_audio->channels=lavc_context->channels;
137 sh_audio->samplerate=lavc_context->sample_rate;
138 sh_audio->i_bps=lavc_context->bit_rate/8;
139 switch (lavc_context->sample_fmt) {
140 case SAMPLE_FMT_U8: sh_audio->sample_format = AF_FORMAT_U8; break;
141 case SAMPLE_FMT_S16: sh_audio->sample_format = AF_FORMAT_S16_NE; break;
142 case SAMPLE_FMT_S32: sh_audio->sample_format = AF_FORMAT_S32_NE; break;
143 case SAMPLE_FMT_FLT: sh_audio->sample_format = AF_FORMAT_FLOAT_NE; break;
144 default:
145 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
146 return 0;
148 /* If the audio is AAC the container level data may be unreliable
149 * because of SBR handling problems (possibly half real sample rate at
150 * container level). Default AAC decoding with ad_faad has used codec-level
151 * values for a long time without generating complaints so it should be OK.
153 if (sh_audio->wf && lavc_context->codec_id != CODEC_ID_AAC) {
154 // If the decoder uses the wrong number of channels all is lost anyway.
155 // sh_audio->channels=sh_audio->wf->nChannels;
156 if (sh_audio->wf->nSamplesPerSec)
157 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
158 if (sh_audio->wf->nAvgBytesPerSec)
159 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
161 sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8;
162 return 1;
165 static void uninit(sh_audio_t *sh)
167 AVCodecContext *lavc_context = sh->context;
169 if (avcodec_close(lavc_context) < 0)
170 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
171 av_freep(&lavc_context->extradata);
172 av_freep(&lavc_context);
175 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
177 AVCodecContext *lavc_context = sh->context;
178 switch(cmd){
179 case ADCTRL_RESYNC_STREAM:
180 avcodec_flush_buffers(lavc_context);
181 ds_clear_parser(sh->ds);
182 return CONTROL_TRUE;
184 return CONTROL_UNKNOWN;
187 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
189 unsigned char *start=NULL;
190 int y,len=-1;
191 while(len<minlen){
192 AVPacket pkt;
193 int len2=maxlen;
194 double pts;
195 int x=ds_get_packet_pts(sh_audio->ds,&start, &pts);
196 if(x<=0) {
197 start = NULL;
198 x = 0;
199 ds_parse(sh_audio->ds, &start, &x, MP_NOPTS_VALUE, 0);
200 if (x <= 0)
201 break; // error
202 } else {
203 int in_size = x;
204 int consumed = ds_parse(sh_audio->ds, &start, &x, pts, 0);
205 sh_audio->ds->buffer_pos -= in_size - consumed;
207 av_init_packet(&pkt);
208 pkt.data = start;
209 pkt.size = x;
210 if (pts != MP_NOPTS_VALUE) {
211 sh_audio->pts = pts;
212 sh_audio->pts_bytes = 0;
214 y=avcodec_decode_audio3(sh_audio->context,(int16_t*)buf,&len2,&pkt);
215 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
216 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
217 if(!sh_audio->parser && y<x)
218 sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!)
219 if(len2>0){
220 if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
221 int samplesize = av_get_bits_per_sample_format(((AVCodecContext *)
222 sh_audio->context)->sample_fmt) / 8;
223 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
224 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
225 ((AVCodecContext *)sh_audio->context)->channels,
226 len2 / samplesize, samplesize);
228 //len=len2;break;
229 if(len<0) len=len2; else len+=len2;
230 buf+=len2;
231 maxlen -= len2;
232 sh_audio->pts_bytes += len2;
234 mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2);
236 return len;