9 #include "ad_internal.h"
10 #include "libaf/reorder_ch.h"
14 static ad_info_t info
=
16 "FFmpeg/libavcodec audio decoders",
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
;
37 static int init(sh_audio_t
*sh_audio
)
40 AVCodecContext
*lavc_context
;
43 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FFmpeg's libavcodec audio codec\n");
44 if(!avcodec_initialized
){
46 avcodec_register_all();
47 avcodec_initialized
=1;
50 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
52 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_MissingLAVCcodec
,sh_audio
->codec
->dll
);
56 lavc_context
= avcodec_alloc_context();
57 sh_audio
->context
=lavc_context
;
59 lavc_context
->sample_rate
= sh_audio
->samplerate
;
60 lavc_context
->bit_rate
= sh_audio
->i_bps
* 8;
62 lavc_context
->channels
= sh_audio
->wf
->nChannels
;
63 lavc_context
->sample_rate
= sh_audio
->wf
->nSamplesPerSec
;
64 lavc_context
->bit_rate
= sh_audio
->wf
->nAvgBytesPerSec
* 8;
65 lavc_context
->block_align
= sh_audio
->wf
->nBlockAlign
;
66 lavc_context
->bits_per_coded_sample
= sh_audio
->wf
->wBitsPerSample
;
68 lavc_context
->request_channels
= audio_output_channels
;
69 lavc_context
->codec_tag
= sh_audio
->format
; //FOURCC
70 lavc_context
->codec_id
= lavc_codec
->id
; // not sure if required, imho not --A'rpi
72 /* alloc extra data */
73 if (sh_audio
->wf
&& sh_audio
->wf
->cbSize
> 0) {
74 lavc_context
->extradata
= av_mallocz(sh_audio
->wf
->cbSize
+ FF_INPUT_BUFFER_PADDING_SIZE
);
75 lavc_context
->extradata_size
= sh_audio
->wf
->cbSize
;
76 memcpy(lavc_context
->extradata
, (char *)sh_audio
->wf
+ sizeof(WAVEFORMATEX
),
77 lavc_context
->extradata_size
);
81 if (sh_audio
->codecdata_len
&& sh_audio
->codecdata
&& !lavc_context
->extradata
)
83 lavc_context
->extradata
= av_malloc(sh_audio
->codecdata_len
);
84 lavc_context
->extradata_size
= sh_audio
->codecdata_len
;
85 memcpy(lavc_context
->extradata
, (char *)sh_audio
->codecdata
,
86 lavc_context
->extradata_size
);
90 if (avcodec_open(lavc_context
, lavc_codec
) < 0) {
91 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
, MSGTR_CantOpenCodec
);
94 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"INFO: libavcodec init OK!\n");
96 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
97 if(sh_audio
->format
==0x3343414D){
99 sh_audio
->ds
->ss_div
= 2*3; // 1 samples/packet
100 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
102 if(sh_audio
->format
==0x3643414D){
104 sh_audio
->ds
->ss_div
= 2*6; // 1 samples/packet
105 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
108 // Decode at least 1 byte: (to get header filled)
109 x
=decode_audio(sh_audio
,sh_audio
->a_buffer
,1,sh_audio
->a_buffer_size
);
110 if(x
>0) sh_audio
->a_buffer_len
=x
;
112 sh_audio
->channels
=lavc_context
->channels
;
113 sh_audio
->samplerate
=lavc_context
->sample_rate
;
114 sh_audio
->i_bps
=lavc_context
->bit_rate
/8;
116 // If the decoder uses the wrong number of channels all is lost anyway.
117 // sh_audio->channels=sh_audio->wf->nChannels;
118 if (sh_audio
->wf
->nSamplesPerSec
)
119 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
120 if (sh_audio
->wf
->nAvgBytesPerSec
)
121 sh_audio
->i_bps
=sh_audio
->wf
->nAvgBytesPerSec
;
123 sh_audio
->samplesize
=2;
127 static void uninit(sh_audio_t
*sh
)
129 AVCodecContext
*lavc_context
= sh
->context
;
131 if (avcodec_close(lavc_context
) < 0)
132 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_CantCloseCodec
);
133 av_freep(&lavc_context
->extradata
);
134 av_freep(&lavc_context
);
137 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
139 AVCodecContext
*lavc_context
= sh
->context
;
141 case ADCTRL_RESYNC_STREAM
:
142 avcodec_flush_buffers(lavc_context
);
145 return CONTROL_UNKNOWN
;
148 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
150 unsigned char *start
=NULL
;
155 int x
=ds_get_packet_pts(sh_audio
->ds
,&start
, &pts
);
156 if(x
<=0) break; // error
157 if (pts
!= MP_NOPTS_VALUE
) {
159 sh_audio
->pts_bytes
= 0;
161 y
=avcodec_decode_audio2(sh_audio
->context
,(int16_t*)buf
,&len2
,start
,x
);
162 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
163 if(y
<0){ mp_msg(MSGT_DECAUDIO
,MSGL_V
,"lavc_audio: error\n");break; }
164 if(y
<x
) sh_audio
->ds
->buffer_pos
+=y
-x
; // put back data (HACK!)
166 if (((AVCodecContext
*)sh_audio
->context
)->channels
>= 5) {
167 int src_ch_layout
= AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
;
168 const char *codec
=((AVCodecContext
*)sh_audio
->context
)->codec
->name
;
169 if (!strcasecmp(codec
, "ac3"))
170 src_ch_layout
= AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT
;
171 else if (!strcasecmp(codec
, "dca"))
172 src_ch_layout
= AF_CHANNEL_LAYOUT_LAVC_DCA_DEFAULT
;
173 else if (!strcasecmp(codec
, "libfaad")
174 || !strcasecmp(codec
, "mpeg4aac"))
175 src_ch_layout
= AF_CHANNEL_LAYOUT_AAC_DEFAULT
;
176 else if (!strcasecmp(codec
, "liba52"))
177 src_ch_layout
= AF_CHANNEL_LAYOUT_LAVC_LIBA52_DEFAULT
;
178 else if (!strcasecmp(codec
, "vorbis"))
179 src_ch_layout
= AF_CHANNEL_LAYOUT_VORBIS_DEFAULT
;
180 else if (!strcasecmp(codec
, "flac"))
181 src_ch_layout
= AF_CHANNEL_LAYOUT_FLAC_DEFAULT
;
183 src_ch_layout
= AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
;
184 reorder_channel_nch(buf
, src_ch_layout
,
185 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
186 ((AVCodecContext
*)sh_audio
->context
)->channels
,
190 if(len
<0) len
=len2
; else len
+=len2
;
193 sh_audio
->pts_bytes
+= len2
;
195 mp_dbg(MSGT_DECAUDIO
,MSGL_DBG2
,"Decoded %d -> %d \n",y
,len2
);