9 #include "ad_internal.h"
10 #include "libaf/reorder_ch.h"
14 static const 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
)
41 AVCodecContext
*lavc_context
;
44 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FFmpeg's libavcodec audio codec\n");
45 if(!avcodec_initialized
){
47 avcodec_register_all();
48 avcodec_initialized
=1;
51 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
53 mp_tmsg(MSGT_DECAUDIO
,MSGL_ERR
,"Cannot find codec '%s' in libavcodec...\n",sh_audio
->codec
->dll
);
57 lavc_context
= avcodec_alloc_context();
58 sh_audio
->context
=lavc_context
;
60 lavc_context
->sample_rate
= sh_audio
->samplerate
;
61 lavc_context
->bit_rate
= sh_audio
->i_bps
* 8;
63 lavc_context
->channels
= sh_audio
->wf
->nChannels
;
64 lavc_context
->sample_rate
= sh_audio
->wf
->nSamplesPerSec
;
65 lavc_context
->bit_rate
= sh_audio
->wf
->nAvgBytesPerSec
* 8;
66 lavc_context
->block_align
= sh_audio
->wf
->nBlockAlign
;
67 lavc_context
->bits_per_coded_sample
= sh_audio
->wf
->wBitsPerSample
;
69 lavc_context
->request_channels
= audio_output_channels
;
70 lavc_context
->codec_tag
= sh_audio
->format
; //FOURCC
71 lavc_context
->codec_type
= CODEC_TYPE_AUDIO
;
72 lavc_context
->codec_id
= lavc_codec
->id
; // not sure if required, imho not --A'rpi
74 /* alloc extra data */
75 if (sh_audio
->wf
&& sh_audio
->wf
->cbSize
> 0) {
76 lavc_context
->extradata
= av_mallocz(sh_audio
->wf
->cbSize
+ FF_INPUT_BUFFER_PADDING_SIZE
);
77 lavc_context
->extradata_size
= sh_audio
->wf
->cbSize
;
78 memcpy(lavc_context
->extradata
, (char *)sh_audio
->wf
+ sizeof(WAVEFORMATEX
),
79 lavc_context
->extradata_size
);
83 if (sh_audio
->codecdata_len
&& sh_audio
->codecdata
&& !lavc_context
->extradata
)
85 lavc_context
->extradata
= av_malloc(sh_audio
->codecdata_len
);
86 lavc_context
->extradata_size
= sh_audio
->codecdata_len
;
87 memcpy(lavc_context
->extradata
, (char *)sh_audio
->codecdata
,
88 lavc_context
->extradata_size
);
92 if (avcodec_open(lavc_context
, lavc_codec
) < 0) {
93 mp_tmsg(MSGT_DECAUDIO
,MSGL_ERR
, "Could not open codec.\n");
96 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec
->name
);
98 // printf("\nFOURCC: 0x%X\n",sh_audio->format);
99 if(sh_audio
->format
==0x3343414D){
101 sh_audio
->ds
->ss_div
= 2*3; // 1 samples/packet
102 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
104 if(sh_audio
->format
==0x3643414D){
106 sh_audio
->ds
->ss_div
= 2*6; // 1 samples/packet
107 sh_audio
->ds
->ss_mul
= 2*sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
110 // Decode at least 1 byte: (to get header filled)
112 x
=decode_audio(sh_audio
,sh_audio
->a_buffer
,1,sh_audio
->a_buffer_size
);
113 } while (x
<= 0 && tries
++ < 5);
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;
119 switch (lavc_context
->sample_fmt
) {
120 case SAMPLE_FMT_U8
: sh_audio
->sample_format
= AF_FORMAT_U8
; break;
121 case SAMPLE_FMT_S16
: sh_audio
->sample_format
= AF_FORMAT_S16_NE
; break;
122 case SAMPLE_FMT_S32
: sh_audio
->sample_format
= AF_FORMAT_S32_NE
; break;
123 case SAMPLE_FMT_FLT
: sh_audio
->sample_format
= AF_FORMAT_FLOAT_NE
; break;
125 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Unsupported sample format\n");
129 // If the decoder uses the wrong number of channels all is lost anyway.
130 // sh_audio->channels=sh_audio->wf->nChannels;
131 if (sh_audio
->wf
->nSamplesPerSec
)
132 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
133 if (sh_audio
->wf
->nAvgBytesPerSec
)
134 sh_audio
->i_bps
=sh_audio
->wf
->nAvgBytesPerSec
;
136 sh_audio
->samplesize
=af_fmt2bits(sh_audio
->sample_format
)/ 8;
140 static void uninit(sh_audio_t
*sh
)
142 AVCodecContext
*lavc_context
= sh
->context
;
144 if (avcodec_close(lavc_context
) < 0)
145 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not close codec.\n");
146 av_freep(&lavc_context
->extradata
);
147 av_freep(&lavc_context
);
150 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
152 AVCodecContext
*lavc_context
= sh
->context
;
154 case ADCTRL_RESYNC_STREAM
:
155 avcodec_flush_buffers(lavc_context
);
158 return CONTROL_UNKNOWN
;
161 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
163 unsigned char *start
=NULL
;
169 int x
=ds_get_packet_pts(sh_audio
->ds
,&start
, &pts
);
173 ds_parse(sh_audio
->ds
, &start
, &x
, MP_NOPTS_VALUE
, 0);
178 int consumed
= ds_parse(sh_audio
->ds
, &start
, &x
, pts
, 0);
179 sh_audio
->ds
->buffer_pos
-= in_size
- consumed
;
181 av_init_packet(&pkt
);
184 if (pts
!= MP_NOPTS_VALUE
) {
186 sh_audio
->pts_bytes
= 0;
188 y
=avcodec_decode_audio3(sh_audio
->context
,(int16_t*)buf
,&len2
,&pkt
);
189 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
190 if(y
<0){ mp_msg(MSGT_DECAUDIO
,MSGL_V
,"lavc_audio: error\n");break; }
191 if(!sh_audio
->needs_parsing
&& y
<x
)
192 sh_audio
->ds
->buffer_pos
+=y
-x
; // put back data (HACK!)
194 if (((AVCodecContext
*)sh_audio
->context
)->channels
>= 5) {
195 int samplesize
= av_get_bits_per_sample_format(((AVCodecContext
*)
196 sh_audio
->context
)->sample_fmt
) / 8;
197 reorder_channel_nch(buf
, AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
198 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
199 ((AVCodecContext
*)sh_audio
->context
)->channels
,
200 len2
/ samplesize
, samplesize
);
203 if(len
<0) len
=len2
; else len
+=len2
;
206 sh_audio
->pts_bytes
+= len2
;
208 mp_dbg(MSGT_DECAUDIO
,MSGL_DBG2
,"Decoded %d -> %d \n",y
,len2
);