Update to MPlayer SVN rev 29978 and FFmpeg SVN rev 20757.
[vaapi:challenzhous-mplayer.git] / libmpcodecs / ad_ffmpeg.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include "config.h"
6 #include "mp_msg.h"
7 #include "help_mp.h"
8
9 #include "ad_internal.h"
10 #include "libaf/reorder_ch.h"
11
12 #include "mpbswap.h"
13
14 static ad_info_t info =
15 {
16         "FFmpeg/libavcodec audio decoders",
17         "ffmpeg",
18         "Nick Kurshev",
19         "ffmpeg.sf.net",
20         ""
21 };
22
23 LIBAD_EXTERN(ffmpeg)
24
25 #define assert(x)
26
27 #include "libavcodec/avcodec.h"
28
29 extern int avcodec_initialized;
30
31 static int preinit(sh_audio_t *sh)
32 {
33   sh->audio_out_minsize=AVCODEC_MAX_AUDIO_FRAME_SIZE;
34   return 1;
35 }
36
37 static int init(sh_audio_t *sh_audio)
38 {
39     int tries = 0;
40     int x;
41     AVCodecContext *lavc_context;
42     AVCodec *lavc_codec;
43
44     mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
45     if(!avcodec_initialized){
46       avcodec_init();
47       avcodec_register_all();
48       avcodec_initialized=1;
49     }
50
51     lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
52     if(!lavc_codec){
53         mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh_audio->codec->dll);
54         return 0;
55     }
56
57     lavc_context = avcodec_alloc_context();
58     sh_audio->context=lavc_context;
59
60     lavc_context->sample_rate = sh_audio->samplerate;
61     lavc_context->bit_rate = sh_audio->i_bps * 8;
62     if(sh_audio->wf){
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;
68     }
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
73
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);
80     }
81
82     // for QDM2
83     if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata)
84     {
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);
89     }
90
91     /* open it */
92     if (avcodec_open(lavc_context, lavc_codec) < 0) {
93         mp_msg(MSGT_DECAUDIO,MSGL_ERR, MSGTR_CantOpenCodec);
94         return 0;
95     }
96    mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec->name);
97
98 //   printf("\nFOURCC: 0x%X\n",sh_audio->format);
99    if(sh_audio->format==0x3343414D){
100        // MACE 3:1
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
103    } else
104    if(sh_audio->format==0x3643414D){
105        // MACE 6:1
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
108    }
109
110    // Decode at least 1 byte:  (to get header filled)
111    do {
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;
115
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;
124       default:
125           mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n");
126           return 0;
127   }
128   if(sh_audio->wf){
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;
135   }
136   sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8;
137   return 1;
138 }
139
140 static void uninit(sh_audio_t *sh)
141 {
142     AVCodecContext *lavc_context = sh->context;
143
144     if (avcodec_close(lavc_context) < 0)
145         mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantCloseCodec);
146     av_freep(&lavc_context->extradata);
147     av_freep(&lavc_context);
148 }
149
150 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
151 {
152     AVCodecContext *lavc_context = sh->context;
153     switch(cmd){
154     case ADCTRL_RESYNC_STREAM:
155         avcodec_flush_buffers(lavc_context);
156     return CONTROL_TRUE;
157     }
158     return CONTROL_UNKNOWN;
159 }
160
161 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
162 {
163     unsigned char *start=NULL;
164     int y,len=-1;
165     while(len<minlen){
166         AVPacket pkt;
167         int len2=maxlen;
168         double pts;
169         int x=ds_get_packet_pts(sh_audio->ds,&start, &pts);
170         if(x<=0) break; // error
171         av_init_packet(&pkt);
172         pkt.data = start;
173         pkt.size = x;
174         if (pts != MP_NOPTS_VALUE) {
175             sh_audio->pts = pts;
176             sh_audio->pts_bytes = 0;
177         }
178         y=avcodec_decode_audio3(sh_audio->context,(int16_t*)buf,&len2,&pkt);
179 //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout);
180         if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
181         if(y<x) sh_audio->ds->buffer_pos+=y-x;  // put back data (HACK!)
182         if(len2>0){
183           if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
184             int samplesize = av_get_bits_per_sample_format(((AVCodecContext *)
185                                     sh_audio->context)->sample_fmt) / 8;
186             reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
187                                 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
188                                 ((AVCodecContext *)sh_audio->context)->channels,
189                                 len2 / samplesize, samplesize);
190           }
191           //len=len2;break;
192           if(len<0) len=len2; else len+=len2;
193           buf+=len2;
194           maxlen -= len2;
195           sh_audio->pts_bytes += len2;
196         }
197         mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d  \n",y,len2);
198     }
199   return len;
200 }