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.
27 #include "ad_internal.h"
28 #include "libaf/reorder_ch.h"
32 static const ad_info_t info
=
34 "FFmpeg/libavcodec audio decoders",
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
;
55 static int init(sh_audio_t
*sh_audio
)
59 AVCodecContext
*lavc_context
;
62 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FFmpeg's libavcodec audio codec\n");
63 if(!avcodec_initialized
){
65 avcodec_register_all();
66 avcodec_initialized
=1;
69 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
71 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_MissingLAVCcodec
,sh_audio
->codec
->dll
);
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;
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
);
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
);
110 if (avcodec_open(lavc_context
, lavc_codec
) < 0) {
111 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
, MSGTR_CantOpenCodec
);
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){
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
122 if(sh_audio
->format
==0x3643414D){
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)
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;
143 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Unsupported sample format\n");
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;
158 static void uninit(sh_audio_t
*sh
)
160 AVCodecContext
*lavc_context
= sh
->context
;
162 if (avcodec_close(lavc_context
) < 0)
163 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_CantCloseCodec
);
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
;
172 case ADCTRL_RESYNC_STREAM
:
173 avcodec_flush_buffers(lavc_context
);
174 ds_clear_parser(sh
->ds
);
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
;
188 int x
=ds_get_packet_pts(sh_audio
->ds
,&start
, &pts
);
192 ds_parse(sh_audio
->ds
, &start
, &x
, MP_NOPTS_VALUE
, 0);
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
);
203 if (pts
!= MP_NOPTS_VALUE
) {
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!)
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
);
222 if(len
<0) len
=len2
; else len
+=len2
;
225 sh_audio
->pts_bytes
+= len2
;
227 mp_dbg(MSGT_DECAUDIO
,MSGL_DBG2
,"Decoded %d -> %d \n",y
,len2
);