1 /*
2  * audio filter for runtime AC-3 encoding with libavcodec.
3  *
4  * Copyright (C) 2007 Ulion <ulion A gmail P com>
5  *
6  * This file is part of MPlayer.
7  *
8  * MPlayer is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * MPlayer is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 
28 #include "config.h"
29 #include "af.h"
30 #include "help_mp.h"
31 #include "mp_msg.h"
32 #include "reorder_ch.h"
33 #include "av_helpers.h"
34 
35 #include "libavcodec/avcodec.h"
36 #include "libavutil/common.h"
37 #include "libavutil/intreadwrite.h"
38 
39 #define AC3_MAX_CHANNELS            6
40 #define AC3_FRAME_SIZE           1536
41 #define AC3_MAX_CODED_FRAME_SIZE 3840
42 //#define AC3_BIT_RATES_COUNT        19
43 
44 static const int ac3_bit_rates[] = {
45     32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000,
46     192000, 224000, 256000, 320000, 384000, 448000, 512000, 576000, 640000
47 };
48 
49 // Data for specific instances of this filter
50 typedef struct af_ac3enc_s {
51     struct AVCodec        *lavc_acodec;
52     struct AVCodecContext *lavc_actx;
53     int add_iec61937_header;
54     int bit_rate;
55     char *pending_data;
56     int pending_len;
57     int expect_len;
58     int min_channel_num;
59 } af_ac3enc_t;
60 
61 // Initialization and runtime control
control(struct af_instance_s * af,int cmd,void * arg)62 static int control(struct af_instance_s *af, int cmd, void *arg)
63 {
64     af_ac3enc_t *s  = af->setup;
65     af_data_t *data = arg;
66     int i, bit_rate, test_output_res;
67     static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
68         {0, 96000, 192000, 256000, 384000, 448000, 448000};
69 
70     switch (cmd){
71     case AF_CONTROL_REINIT:
72         if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
73             return AF_DETACH;
74 
75         s->pending_len = 0;
76         s->expect_len = AC3_FRAME_SIZE * data->nch * data->bps;
77         if (s->add_iec61937_header)
78             af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
79         else
80             af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
81 
82         mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
83                data->nch, data->rate, af->mul, s->expect_len);
84 
85         af->data->format = AF_FORMAT_S16_NE;
86         if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
87             af->data->rate = data->rate;
88         else
89             af->data->rate = 48000;
90         if (data->nch > AC3_MAX_CHANNELS)
91             af->data->nch = AC3_MAX_CHANNELS;
92         else
93             af->data->nch = data->nch;
94         af->data->bps = 2;
95         test_output_res = af_test_output(af, data);
96 
97         bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
98 
99         if (s->lavc_actx->channels != af->data->nch ||
100                 s->lavc_actx->sample_rate != af->data->rate ||
101                 s->lavc_actx->bit_rate != bit_rate) {
102 
103             if (s->lavc_actx->codec)
104                 avcodec_close(s->lavc_actx);
105 
106             // Put sample parameters
107             s->lavc_actx->channels = af->data->nch;
108             s->lavc_actx->sample_rate = af->data->rate;
109             s->lavc_actx->sample_fmt  = AV_SAMPLE_FMT_S16P;
110             s->lavc_actx->bit_rate = bit_rate;
111 
112             if(avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) {
113                 mp_msg(MSGT_AFILTER, MSGL_ERR, MSGTR_CouldntOpenCodec, "ac3", bit_rate);
114                 return AF_ERROR;
115             }
116         }
117         af->data->format = AF_FORMAT_AC3_BE;
118         af->data->nch = 2;
119         return test_output_res;
120     case AF_CONTROL_COMMAND_LINE:
121         mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
122         s->bit_rate = 0;
123         s->min_channel_num = 0;
124         s->add_iec61937_header = 0;
125         sscanf(arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
126                &s->min_channel_num);
127         if (s->bit_rate < 1000)
128             s->bit_rate *= 1000;
129         if (s->bit_rate) {
130             for (i = 0; i < FF_ARRAY_ELEMS(ac3_bit_rates); ++i)
131                 if (ac3_bit_rates[i] == s->bit_rate)
132                     break;
133             if (i >= FF_ARRAY_ELEMS(ac3_bit_rates)) {
134                 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
135                        "bitrate %d, use default bitrate (check manpage to see "
136                        "supported bitrates).\n", s->bit_rate);
137                 s->bit_rate = 0;
138             }
139         }
140         if (s->min_channel_num == 0)
141             s->min_channel_num = 5;
142         mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
143                "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
144                s->min_channel_num);
145         return AF_OK;
146     }
147     return AF_UNKNOWN;
148 }
149 
150 // Deallocate memory
uninit(struct af_instance_s * af)151 static void uninit(struct af_instance_s* af)
152 {
153     if (af->data)
154         free(af->data->audio);
155     free(af->data);
156     if (af->setup) {
157         af_ac3enc_t *s = af->setup;
158         af->setup = NULL;
159         if(s->lavc_actx) {
160             if (s->lavc_actx->codec)
161                 avcodec_close(s->lavc_actx);
162             free(s->lavc_actx);
163         }
164         free(s->pending_data);
165         free(s);
166     }
167 }
168 
169 // Filter data through filter
play(struct af_instance_s * af,af_data_t * data)170 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
171 {
172     af_ac3enc_t *s = af->setup;
173     af_data_t *c = data;    // Current working data
174     af_data_t *l;
175     int len, left, outsize = 0, destsize;
176     char *buf, *src, *dest;
177     int max_output_len;
178     int frame_num = (data->len + s->pending_len) / s->expect_len;
179 
180     if (s->add_iec61937_header)
181         max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
182     else
183         max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
184 
185     if (af->data->len < max_output_len) {
186         mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
187                "old len = %i, new len = %i\n", af->info->name, af->data->len,
188                 max_output_len);
189         free(af->data->audio);
190         af->data->audio = malloc(max_output_len);
191         if (!af->data->audio) {
192             mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
193             return NULL;
194         }
195         af->data->len = max_output_len;
196     }
197 
198     l = af->data;           // Local data
199     buf = l->audio;
200     src = c->audio;
201     left = c->len;
202 
203 
204     while (left > 0) {
205         void *in = NULL;
206         if (left + s->pending_len < s->expect_len) {
207             memcpy(s->pending_data + s->pending_len, src, left);
208             src += left;
209             s->pending_len += left;
210             left = 0;
211             break;
212         }
213 
214         dest = s->add_iec61937_header ? buf + 8 : buf;
215         destsize = (char *)l->audio + l->len - buf;
216 
217         if (s->pending_len) {
218             int needs = s->expect_len - s->pending_len;
219             if (needs > 0) {
220                 memcpy(s->pending_data + s->pending_len, src, needs);
221                 src += needs;
222                 left -= needs;
223             }
224             in = s->pending_data;
225             s->pending_len = 0;
226         }
227         else {
228             in = src;
229             src += s->expect_len;
230             left -= s->expect_len;
231         }
232         len = lavc_encode_audio(s->lavc_actx, in, s->expect_len, dest, destsize);
233         mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
234                len, s->pending_len);
235         if (len < 0) len = 0;
236 
237         if (s->add_iec61937_header) {
238             int bsmod = dest[5] & 0x7;
239 
240             AV_WB16(buf,     0xF872);   // iec 61937 syncword 1
241             AV_WB16(buf + 2, 0x4E1F);   // iec 61937 syncword 2
242             buf[4] = bsmod;             // bsmod
243             buf[5] = 0x01;              // data-type ac3
244             AV_WB16(buf + 6, len << 3); // number of bits in payload
245 
246             memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
247             len = AC3_FRAME_SIZE * 2 * 2;
248         }
249 
250         outsize += len;
251         buf += len;
252     }
253     c->audio = l->audio;
254     c->nch   = 2;
255     c->bps   = 2;
256     c->len   = outsize;
257     mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
258            outsize, s->pending_len);
259     return c;
260 }
261 
af_open(af_instance_t * af)262 static int af_open(af_instance_t* af){
263 
264     af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
265     int pending_space = 2 * AC3_MAX_CHANNELS * AC3_FRAME_SIZE;
266     s->pending_data = calloc(pending_space, sizeof(char));
267 
268     af->control=control;
269     af->uninit=uninit;
270     af->play=play;
271     af->mul=1;
272     af->data=calloc(1,sizeof(af_data_t));
273     af->setup=s;
274 
275     init_avcodec();
276 
277     s->lavc_acodec = avcodec_find_encoder_by_name("ac3_fixed");
278     if (!s->lavc_acodec) {
279         mp_msg(MSGT_AFILTER, MSGL_ERR, MSGTR_LavcAudioCodecNotFound, "ac3_fixed");
280         return AF_ERROR;
281     }
282 
283     s->lavc_actx = avcodec_alloc_context3(NULL);
284     if (!s->lavc_actx) {
285         mp_msg(MSGT_AFILTER, MSGL_ERR, MSGTR_CouldntAllocateLavcContext);
286         return AF_ERROR;
287     }
288 
289     return AF_OK;
290 }
291 
292 const af_info_t af_info_lavcac3enc = {
293     "runtime encode to ac3 using libavcodec",
294     "lavcac3enc",
295     "Ulion",
296     "",
297     AF_FLAGS_REENTRANT,
298     af_open
299 };
300