9 #include "ad_internal.h"
13 static ad_info_t info
=
15 "FFmpeg/libavcodec audio decoders",
26 #ifdef USE_LIBAVCODEC_SO
27 #include <ffmpeg/avcodec.h>
32 extern int avcodec_inited
;
34 static int preinit(sh_audio_t
*sh
)
36 sh
->audio_out_minsize
=AVCODEC_MAX_AUDIO_FRAME_SIZE
;
40 static int init(sh_audio_t
*sh_audio
)
43 AVCodecContext
*lavc_context
;
46 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FFmpeg's libavcodec audio codec\n");
49 avcodec_register_all();
53 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
55 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_MissingLAVCcodec
,sh_audio
->codec
->dll
);
59 lavc_context
= avcodec_alloc_context();
60 sh_audio
->context
=lavc_context
;
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_sample
= sh_audio
->wf
->wBitsPerSample
;
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 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
119 sh_audio
->i_bps
=sh_audio
->wf
->nAvgBytesPerSec
;
121 sh_audio
->samplesize
=2;
125 static void uninit(sh_audio_t
*sh
)
127 AVCodecContext
*lavc_context
= sh
->context
;
129 if (avcodec_close(lavc_context
) < 0)
130 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_CantCloseCodec
);
131 av_freep(&lavc_context
->extradata
);
132 av_freep(&lavc_context
);
135 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
137 AVCodecContext
*lavc_context
= sh
->context
;
139 case ADCTRL_RESYNC_STREAM
:
140 avcodec_flush_buffers(lavc_context
);
143 return CONTROL_UNKNOWN
;
146 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
148 unsigned char *start
=NULL
;
153 int x
=ds_get_packet_pts(sh_audio
->ds
,&start
, &pts
);
154 if(x
<=0) break; // error
155 if (pts
!= MP_NOPTS_VALUE
) {
157 sh_audio
->pts_bytes
= 0;
159 y
=avcodec_decode_audio(sh_audio
->context
,(int16_t*)buf
,&len2
,start
,x
);
160 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
161 if(y
<0){ mp_msg(MSGT_DECAUDIO
,MSGL_V
,"lavc_audio: error\n");break; }
162 if(y
<x
) sh_audio
->ds
->buffer_pos
+=y
-x
; // put back data (HACK!)
165 if(len
<0) len
=len2
; else len
+=len2
;
167 sh_audio
->pts_bytes
+= len2
;
169 mp_dbg(MSGT_DECAUDIO
,MSGL_DBG2
,"Decoded %d -> %d \n",y
,len2
);