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.
26 #include "ad_internal.h"
27 #include "libaf/reorder_ch.h"
31 static const ad_info_t info
=
33 "FFmpeg/libavcodec audio decoders",
44 #include "libavcodec/avcodec.h"
46 extern int avcodec_initialized
;
48 static int preinit(sh_audio_t
*sh
)
50 sh
->audio_out_minsize
=AVCODEC_MAX_AUDIO_FRAME_SIZE
;
54 static int init(sh_audio_t
*sh_audio
)
58 AVCodecContext
*lavc_context
;
61 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FFmpeg's libavcodec audio codec\n");
62 if(!avcodec_initialized
){
64 avcodec_register_all();
65 avcodec_initialized
=1;
68 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
70 mp_tmsg(MSGT_DECAUDIO
,MSGL_ERR
,"Cannot find codec '%s' in libavcodec...\n",sh_audio
->codec
->dll
);
74 lavc_context
= avcodec_alloc_context();
75 sh_audio
->context
=lavc_context
;
77 lavc_context
->sample_rate
= sh_audio
->samplerate
;
78 lavc_context
->bit_rate
= sh_audio
->i_bps
* 8;
80 lavc_context
->channels
= sh_audio
->wf
->nChannels
;
81 lavc_context
->sample_rate
= sh_audio
->wf
->nSamplesPerSec
;
82 lavc_context
->bit_rate
= sh_audio
->wf
->nAvgBytesPerSec
* 8;
83 lavc_context
->block_align
= sh_audio
->wf
->nBlockAlign
;
84 lavc_context
->bits_per_coded_sample
= sh_audio
->wf
->wBitsPerSample
;
86 lavc_context
->request_channels
= audio_output_channels
;
87 lavc_context
->codec_tag
= sh_audio
->format
; //FOURCC
88 lavc_context
->codec_type
= CODEC_TYPE_AUDIO
;
89 lavc_context
->codec_id
= lavc_codec
->id
; // not sure if required, imho not --A'rpi
91 /* alloc extra data */
92 if (sh_audio
->wf
&& sh_audio
->wf
->cbSize
> 0) {
93 lavc_context
->extradata
= av_mallocz(sh_audio
->wf
->cbSize
+ FF_INPUT_BUFFER_PADDING_SIZE
);
94 lavc_context
->extradata_size
= sh_audio
->wf
->cbSize
;
95 memcpy(lavc_context
->extradata
, (char *)sh_audio
->wf
+ sizeof(WAVEFORMATEX
),
96 lavc_context
->extradata_size
);
100 if (sh_audio
->codecdata_len
&& sh_audio
->codecdata
&& !lavc_context
->extradata
)
102 lavc_context
->extradata
= av_malloc(sh_audio
->codecdata_len
);
103 lavc_context
->extradata_size
= sh_audio
->codecdata_len
;
104 memcpy(lavc_context
->extradata
, (char *)sh_audio
->codecdata
,
105 lavc_context
->extradata_size
);
109 if (avcodec_open(lavc_context
, lavc_codec
) < 0) {
110 mp_tmsg(MSGT_DECAUDIO
,MSGL_ERR
, "Could not open codec.\n");
113 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec
->name
);
115 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
116 if(sh_audio
->format
==0x3343414D){
118 sh_audio
->ds
->ss_div
= 2*3; // 1 samples/packet
119 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
121 if(sh_audio
->format
==0x3643414D){
123 sh_audio
->ds
->ss_div
= 2*6; // 1 samples/packet
124 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
127 // Decode at least 1 byte: (to get header filled)
129 x
=decode_audio(sh_audio
,sh_audio
->a_buffer
,1,sh_audio
->a_buffer_size
);
130 } while (x
<= 0 && tries
++ < 5);
131 if(x
>0) sh_audio
->a_buffer_len
=x
;
133 sh_audio
->channels
=lavc_context
->channels
;
134 sh_audio
->samplerate
=lavc_context
->sample_rate
;
135 sh_audio
->i_bps
=lavc_context
->bit_rate
/8;
136 switch (lavc_context
->sample_fmt
) {
137 case SAMPLE_FMT_U8
: sh_audio
->sample_format
= AF_FORMAT_U8
; break;
138 case SAMPLE_FMT_S16
: sh_audio
->sample_format
= AF_FORMAT_S16_NE
; break;
139 case SAMPLE_FMT_S32
: sh_audio
->sample_format
= AF_FORMAT_S32_NE
; break;
140 case SAMPLE_FMT_FLT
: sh_audio
->sample_format
= AF_FORMAT_FLOAT_NE
; break;
142 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Unsupported sample format\n");
145 /* If the audio is AAC the container level data may be unreliable
146 * because of SBR handling problems (possibly half real sample rate at
147 * container level). Default AAC decoding with ad_faad has used codec-level
148 * values for a long time without generating complaints so it should be OK.
150 if (sh_audio
->wf
&& lavc_context
->codec_id
!= CODEC_ID_AAC
) {
151 // If the decoder uses the wrong number of channels all is lost anyway.
152 // sh_audio->channels=sh_audio->wf->nChannels;
153 if (sh_audio
->wf
->nSamplesPerSec
)
154 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
155 if (sh_audio
->wf
->nAvgBytesPerSec
)
156 sh_audio
->i_bps
=sh_audio
->wf
->nAvgBytesPerSec
;
158 sh_audio
->samplesize
=af_fmt2bits(sh_audio
->sample_format
)/ 8;
162 static void uninit(sh_audio_t
*sh
)
164 AVCodecContext
*lavc_context
= sh
->context
;
166 if (avcodec_close(lavc_context
) < 0)
167 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not close codec.\n");
168 av_freep(&lavc_context
->extradata
);
169 av_freep(&lavc_context
);
172 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
174 AVCodecContext
*lavc_context
= sh
->context
;
176 case ADCTRL_RESYNC_STREAM
:
177 avcodec_flush_buffers(lavc_context
);
178 ds_clear_parser(sh
->ds
);
181 return CONTROL_UNKNOWN
;
184 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
186 unsigned char *start
=NULL
;
192 int x
=ds_get_packet_pts(sh_audio
->ds
,&start
, &pts
);
196 ds_parse(sh_audio
->ds
, &start
, &x
, MP_NOPTS_VALUE
, 0);
201 int consumed
= ds_parse(sh_audio
->ds
, &start
, &x
, pts
, 0);
202 sh_audio
->ds
->buffer_pos
-= in_size
- consumed
;
204 av_init_packet(&pkt
);
207 if (pts
!= MP_NOPTS_VALUE
) {
209 sh_audio
->pts_bytes
= 0;
211 y
=avcodec_decode_audio3(sh_audio
->context
,(int16_t*)buf
,&len2
,&pkt
);
212 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
213 if(y
<0){ mp_msg(MSGT_DECAUDIO
,MSGL_V
,"lavc_audio: error\n");break; }
214 if(!sh_audio
->parser
&& y
<x
)
215 sh_audio
->ds
->buffer_pos
+=y
-x
; // put back data (HACK!)
217 if (((AVCodecContext
*)sh_audio
->context
)->channels
>= 5) {
218 int samplesize
= av_get_bits_per_sample_format(((AVCodecContext
*)
219 sh_audio
->context
)->sample_fmt
) / 8;
220 reorder_channel_nch(buf
, AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
221 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
222 ((AVCodecContext
*)sh_audio
->context
)->channels
,
223 len2
/ samplesize
, samplesize
);
226 if(len
<0) len
=len2
; else len
+=len2
;
229 sh_audio
->pts_bytes
+= len2
;
231 mp_dbg(MSGT_DECAUDIO
,MSGL_DBG2
,"Decoded %d -> %d \n",y
,len2
);