9 #include "ad_internal.h"
10 #include "libaf/reorder_ch.h"
14 static ad_info_t info
=
16 "FFmpeg/libavcodec audio decoders",
27 #ifdef USE_LIBAVCODEC_SO
28 #include <ffmpeg/avcodec.h>
33 extern int avcodec_inited
;
35 static int preinit(sh_audio_t
*sh
)
37 sh
->audio_out_minsize
=AVCODEC_MAX_AUDIO_FRAME_SIZE
;
41 static int init(sh_audio_t
*sh_audio
)
44 AVCodecContext
*lavc_context
;
47 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FFmpeg's libavcodec audio codec\n");
50 avcodec_register_all();
54 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
56 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_MissingLAVCcodec
,sh_audio
->codec
->dll
);
60 lavc_context
= avcodec_alloc_context();
61 sh_audio
->context
=lavc_context
;
63 lavc_context
->sample_rate
= sh_audio
->samplerate
;
64 lavc_context
->bit_rate
= sh_audio
->i_bps
* 8;
66 lavc_context
->channels
= sh_audio
->wf
->nChannels
;
67 lavc_context
->sample_rate
= sh_audio
->wf
->nSamplesPerSec
;
68 lavc_context
->bit_rate
= sh_audio
->wf
->nAvgBytesPerSec
* 8;
69 lavc_context
->block_align
= sh_audio
->wf
->nBlockAlign
;
70 lavc_context
->bits_per_sample
= sh_audio
->wf
->wBitsPerSample
;
72 lavc_context
->request_channels
= audio_output_channels
;
73 lavc_context
->codec_tag
= sh_audio
->format
; //FOURCC
74 lavc_context
->codec_id
= lavc_codec
->id
; // not sure if required, imho not --A'rpi
76 /* alloc extra data */
77 if (sh_audio
->wf
&& sh_audio
->wf
->cbSize
> 0) {
78 lavc_context
->extradata
= av_mallocz(sh_audio
->wf
->cbSize
+ FF_INPUT_BUFFER_PADDING_SIZE
);
79 lavc_context
->extradata_size
= sh_audio
->wf
->cbSize
;
80 memcpy(lavc_context
->extradata
, (char *)sh_audio
->wf
+ sizeof(WAVEFORMATEX
),
81 lavc_context
->extradata_size
);
85 if (sh_audio
->codecdata_len
&& sh_audio
->codecdata
&& !lavc_context
->extradata
)
87 lavc_context
->extradata
= av_malloc(sh_audio
->codecdata_len
);
88 lavc_context
->extradata_size
= sh_audio
->codecdata_len
;
89 memcpy(lavc_context
->extradata
, (char *)sh_audio
->codecdata
,
90 lavc_context
->extradata_size
);
94 if (avcodec_open(lavc_context
, lavc_codec
) < 0) {
95 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
, MSGTR_CantOpenCodec
);
98 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"INFO: libavcodec init OK!\n");
100 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
101 if(sh_audio
->format
==0x3343414D){
103 sh_audio
->ds
->ss_div
= 2*3; // 1 samples/packet
104 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
106 if(sh_audio
->format
==0x3643414D){
108 sh_audio
->ds
->ss_div
= 2*6; // 1 samples/packet
109 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
112 // Decode at least 1 byte: (to get header filled)
113 x
=decode_audio(sh_audio
,sh_audio
->a_buffer
,1,sh_audio
->a_buffer_size
);
114 if(x
>0) sh_audio
->a_buffer_len
=x
;
116 sh_audio
->channels
=lavc_context
->channels
;
117 sh_audio
->samplerate
=lavc_context
->sample_rate
;
118 sh_audio
->i_bps
=lavc_context
->bit_rate
/8;
120 // If the decoder uses the wrong number of channels all is lost anyway.
121 // sh_audio->channels=sh_audio->wf->nChannels;
122 if (sh_audio
->wf
->nSamplesPerSec
)
123 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
124 if (sh_audio
->wf
->nAvgBytesPerSec
)
125 sh_audio
->i_bps
=sh_audio
->wf
->nAvgBytesPerSec
;
127 sh_audio
->samplesize
=2;
131 static void uninit(sh_audio_t
*sh
)
133 AVCodecContext
*lavc_context
= sh
->context
;
135 if (avcodec_close(lavc_context
) < 0)
136 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_CantCloseCodec
);
137 av_freep(&lavc_context
->extradata
);
138 av_freep(&lavc_context
);
141 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
143 AVCodecContext
*lavc_context
= sh
->context
;
145 case ADCTRL_RESYNC_STREAM
:
146 avcodec_flush_buffers(lavc_context
);
149 return CONTROL_UNKNOWN
;
152 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
154 unsigned char *start
=NULL
;
159 int x
=ds_get_packet_pts(sh_audio
->ds
,&start
, &pts
);
160 if(x
<=0) break; // error
161 if (pts
!= MP_NOPTS_VALUE
) {
163 sh_audio
->pts_bytes
= 0;
165 y
=avcodec_decode_audio2(sh_audio
->context
,(int16_t*)buf
,&len2
,start
,x
);
166 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
167 if(y
<0){ mp_msg(MSGT_DECAUDIO
,MSGL_V
,"lavc_audio: error\n");break; }
168 if(y
<x
) sh_audio
->ds
->buffer_pos
+=y
-x
; // put back data (HACK!)
170 if (((AVCodecContext
*)sh_audio
->context
)->channels
>= 5) {
171 int src_ch_layout
= AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
;
172 const char *codec
=((AVCodecContext
*)sh_audio
->context
)->codec
->name
;
173 if (!strcasecmp(codec
, "ac3"))
174 src_ch_layout
= AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT
;
175 else if (!strcasecmp(codec
, "dca"))
176 src_ch_layout
= AF_CHANNEL_LAYOUT_LAVC_DCA_DEFAULT
;
177 else if (!strcasecmp(codec
, "libfaad")
178 || !strcasecmp(codec
, "mpeg4aac"))
179 src_ch_layout
= AF_CHANNEL_LAYOUT_AAC_DEFAULT
;
180 else if (!strcasecmp(codec
, "liba52"))
181 src_ch_layout
= AF_CHANNEL_LAYOUT_LAVC_LIBA52_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
);