audio: add af_lavrresample, remove old resampling filters
[mplayer.git] / libaf / af_lavcac3enc.c
blobc54cc3e91ced4de9334d3e77161b2e6ef09917dc
1 /*
2 * audio filter for runtime AC-3 encoding with libavcodec.
4 * Copyright (C) 2007 Ulion <ulion A gmail P com>
6 * This file is part of MPlayer.
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.
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.
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.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <assert.h>
29 #include <libavcodec/avcodec.h>
30 #include <libavutil/audioconvert.h>
31 #include <libavutil/error.h>
32 #include <libavutil/intreadwrite.h>
33 #include <libavutil/mem.h>
34 #include <libavutil/opt.h>
35 #include <libavutil/samplefmt.h>
36 #include <libavresample/avresample.h>
38 #include "config.h"
39 #include "af.h"
40 #include "reorder_ch.h"
43 #define AC3_MAX_CHANNELS 6
44 #define AC3_MAX_CODED_FRAME_SIZE 3840
45 #define AC3_FRAME_SIZE (6 * 256)
46 const uint16_t ac3_bitrate_tab[19] = {
47 32, 40, 48, 56, 64, 80, 96, 112, 128,
48 160, 192, 224, 256, 320, 384, 448, 512, 576, 640
51 // Data for specific instances of this filter
52 typedef struct af_ac3enc_s {
53 AVAudioResampleContext *avr;
54 uint8_t *resample_buf[AC3_MAX_CHANNELS];
55 int linesize;
56 AVFrame *frame;
57 struct AVCodec *lavc_acodec;
58 struct AVCodecContext *lavc_actx;
59 int add_iec61937_header;
60 int bit_rate;
61 int pending_data_size;
62 char *pending_data;
63 int pending_len;
64 int expect_len;
65 int min_channel_num;
66 int in_sampleformat;
67 } af_ac3enc_t;
69 // Initialization and runtime control
70 static int control(struct af_instance_s *af, int cmd, void *arg)
72 af_ac3enc_t *s = (af_ac3enc_t *)af->setup;
73 af_data_t *data = (af_data_t *)arg;
74 int i, bit_rate, test_output_res;
75 static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
76 {0, 96000, 192000, 256000, 384000, 448000, 448000};
78 switch (cmd){
79 case AF_CONTROL_REINIT:
80 if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
81 return AF_DETACH;
83 af->data->format = s->in_sampleformat;
84 af->data->bps = af_fmt2bits(s->in_sampleformat) / 8;
85 if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
86 af->data->rate = data->rate;
87 else
88 af->data->rate = 48000;
89 if (data->nch > AC3_MAX_CHANNELS)
90 af->data->nch = AC3_MAX_CHANNELS;
91 else
92 af->data->nch = data->nch;
93 test_output_res = af_test_output(af, data);
95 s->pending_len = 0;
96 s->expect_len = AC3_FRAME_SIZE * data->nch * af->data->bps;
97 assert(s->expect_len <= s->pending_data_size);
98 if (s->add_iec61937_header)
99 af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
100 else
101 af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
103 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
104 data->nch, data->rate, af->mul, s->expect_len);
106 bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
108 if (s->lavc_actx->channels != af->data->nch ||
109 s->lavc_actx->sample_rate != af->data->rate ||
110 s->lavc_actx->bit_rate != bit_rate) {
112 avcodec_close(s->lavc_actx);
114 if (s->avr) {
115 uint64_t ch_layout =
116 av_get_default_channel_layout(af->data->nch);
117 enum AVSampleFormat in_sample_fmt =
118 av_get_packed_sample_fmt(s->lavc_actx->sample_fmt);
119 int ret;
121 avresample_close(s->avr);
123 if (af->data->nch != s->lavc_actx->channels) {
124 av_freep(&s->resample_buf[0]);
125 ret = av_samples_alloc(s->resample_buf, &s->linesize,
126 af->data->nch, AC3_FRAME_SIZE,
127 s->lavc_actx->sample_fmt, 0);
128 if (ret < 0) {
129 uint8_t error[128];
130 av_strerror(ret, error, sizeof(error));
131 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error allocating "
132 "resample buffer: %s\n", error);
133 return AF_ERROR;
137 av_opt_set_int(s->avr, "in_channel_layout", ch_layout, 0);
138 av_opt_set_int(s->avr, "out_channel_layout", ch_layout, 0);
139 av_opt_set_int(s->avr, "in_sample_rate", af->data->rate, 0);
140 av_opt_set_int(s->avr, "out_sample_rate", af->data->rate, 0);
141 av_opt_set_int(s->avr, "in_sample_fmt", in_sample_fmt, 0);
142 av_opt_set_int(s->avr, "out_sample_fmt", s->lavc_actx->sample_fmt, 0);
144 if ((ret = avresample_open(s->avr)) < 0) {
145 uint8_t error[128];
146 av_strerror(ret, error, sizeof(error));
147 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error configuring "
148 "libavresample: %s\n", error);
149 return AF_ERROR;
153 // Put sample parameters
154 s->lavc_actx->channels = af->data->nch;
155 s->lavc_actx->sample_rate = af->data->rate;
156 s->lavc_actx->bit_rate = bit_rate;
158 if (avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) {
159 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
160 return AF_ERROR;
163 if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) {
164 mp_msg(MSGT_AFILTER, MSGL_ERR, "lavcac3enc: unexpected ac3 "
165 "encoder frame size %d\n", s->lavc_actx->frame_size);
166 return AF_ERROR;
168 af->data->format = AF_FORMAT_AC3_BE;
169 af->data->bps = 2;
170 af->data->nch = 2;
171 return test_output_res;
172 case AF_CONTROL_COMMAND_LINE:
173 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
174 s->bit_rate = 0;
175 s->min_channel_num = 0;
176 s->add_iec61937_header = 0;
177 sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
178 &s->min_channel_num);
179 if (s->bit_rate < 1000)
180 s->bit_rate *= 1000;
181 if (s->bit_rate) {
182 for (i = 0; i < 19; ++i)
183 if (ac3_bitrate_tab[i] * 1000 == s->bit_rate)
184 break;
185 if (i >= 19) {
186 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
187 "bitrate %d, use default bitrate (check manpage to see "
188 "supported bitrates).\n", s->bit_rate);
189 s->bit_rate = 0;
192 if (s->min_channel_num == 0)
193 s->min_channel_num = 5;
194 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
195 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
196 s->min_channel_num);
197 return AF_OK;
199 return AF_UNKNOWN;
202 // Deallocate memory
203 static void uninit(struct af_instance_s* af)
205 if (af->data)
206 free(af->data->audio);
207 free(af->data);
208 if (af->setup) {
209 af_ac3enc_t *s = af->setup;
210 af->setup = NULL;
211 if(s->lavc_actx) {
212 avcodec_close(s->lavc_actx);
213 av_free(s->lavc_actx);
215 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
216 avcodec_free_frame(&s->frame);
217 #else
218 av_freep(&s->frame);
219 #endif
220 avresample_free(&s->avr);
221 av_freep(&s->resample_buf[0]);
222 free(s->pending_data);
223 free(s);
227 static int encode_data(af_ac3enc_t *s, uint8_t *src, uint8_t *dst, int dst_len)
229 AVPacket pkt;
230 uint8_t error[128];
231 int total_samples = AC3_FRAME_SIZE * s->lavc_actx->channels;
232 int bps = av_get_bytes_per_sample(s->lavc_actx->sample_fmt);
233 int ret, got_frame;
235 if (s->lavc_actx->channels >= 5)
236 reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
237 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
238 s->lavc_actx->channels,
239 total_samples, bps);
241 s->frame->nb_samples = AC3_FRAME_SIZE;
242 s->frame->data[0] = src;
243 s->frame->linesize[0] = total_samples * bps;
245 if (s->avr) {
246 ret = avresample_convert(s->avr, s->resample_buf, s->linesize,
247 AC3_FRAME_SIZE, &src, total_samples * bps,
248 AC3_FRAME_SIZE);
249 if (ret < 0) {
250 av_strerror(ret, error, sizeof(error));
251 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error converting audio sample "
252 "format: %s\n", error);
253 return AF_ERROR;
254 } else if (ret != AC3_FRAME_SIZE) {
255 mp_msg(MSGT_AFILTER, MSGL_ERR, "Not enough converted data.\n");
256 return -1;
259 memcpy(s->frame->data, s->resample_buf, sizeof(s->resample_buf));
260 s->frame->linesize[0] = s->linesize;
263 av_init_packet(&pkt);
264 pkt.data = dst;
265 pkt.size = dst_len;
267 ret = avcodec_encode_audio2(s->lavc_actx, &pkt, s->frame, &got_frame);
268 if (ret < 0) {
269 av_strerror(ret, error, sizeof(error));
270 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error encoding audio: %s\n", error);
271 return ret;
273 return got_frame ? pkt.size : 0;
276 // Filter data through filter
277 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
279 af_ac3enc_t *s = af->setup;
280 af_data_t *c = data; // Current working data
281 af_data_t *l;
282 int len, left, outsize = 0, destsize;
283 char *buf, *src, *dest;
284 int max_output_len;
285 int frame_num = (data->len + s->pending_len) / s->expect_len;
287 if (s->add_iec61937_header)
288 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
289 else
290 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
292 if (af->data->len < max_output_len) {
293 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
294 "old len = %i, new len = %i\n", af->info->name, af->data->len,
295 max_output_len);
296 free(af->data->audio);
297 af->data->audio = malloc(max_output_len);
298 if (!af->data->audio) {
299 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
300 return NULL;
302 af->data->len = max_output_len;
305 l = af->data; // Local data
306 buf = (char *)l->audio;
307 src = (char *)c->audio;
308 left = c->len;
311 while (left > 0) {
312 if (left + s->pending_len < s->expect_len) {
313 memcpy(s->pending_data + s->pending_len, src, left);
314 src += left;
315 s->pending_len += left;
316 left = 0;
317 break;
320 dest = s->add_iec61937_header ? buf + 8 : buf;
321 destsize = (char *)l->audio + l->len - buf;
323 if (s->pending_len) {
324 int needs = s->expect_len - s->pending_len;
325 if (needs > 0) {
326 memcpy(s->pending_data + s->pending_len, src, needs);
327 src += needs;
328 left -= needs;
331 len = encode_data(s, s->pending_data, dest, destsize);
332 s->pending_len = 0;
334 else {
335 len = encode_data(s, src, dest, destsize);
336 src += s->expect_len;
337 left -= s->expect_len;
339 if (len <= 0)
340 return NULL;
342 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
343 len, s->pending_len);
345 if (s->add_iec61937_header) {
346 int bsmod = dest[5] & 0x7;
348 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
349 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
350 buf[4] = bsmod; // bsmod
351 buf[5] = 0x01; // data-type ac3
352 AV_WB16(buf + 6, len << 3); // number of bits in payload
354 memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
355 len = AC3_FRAME_SIZE * 2 * 2;
358 outsize += len;
359 buf += len;
361 c->audio = l->audio;
362 c->nch = 2;
363 c->bps = 2;
364 c->len = outsize;
365 mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
366 outsize, s->pending_len);
367 return c;
370 static int af_open(af_instance_t* af){
372 af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
373 af->control=control;
374 af->uninit=uninit;
375 af->play=play;
376 af->mul=1;
377 af->data=calloc(1,sizeof(af_data_t));
378 af->setup=s;
380 s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
381 if (!s->lavc_acodec) {
382 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
383 return AF_ERROR;
386 s->lavc_actx = avcodec_alloc_context3(s->lavc_acodec);
387 if (!s->lavc_actx) {
388 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
389 return AF_ERROR;
391 s->frame = avcodec_alloc_frame();
392 if (!s->frame)
393 return AF_ERROR;
394 const enum AVSampleFormat *fmts = s->lavc_acodec->sample_fmts;
395 for (int i = 0; ; i++) {
396 if (fmts[i] == AV_SAMPLE_FMT_NONE) {
397 mp_msg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, encoder doesn't "
398 "support expected sample formats!\n");
399 return AF_ERROR;
401 enum AVSampleFormat fmt_packed = av_get_packed_sample_fmt(fmts[i]);
402 if (fmt_packed == AV_SAMPLE_FMT_S16) {
403 s->in_sampleformat = AF_FORMAT_S16_NE;
404 s->lavc_actx->sample_fmt = fmts[i];
405 break;
406 } else if (fmt_packed == AV_SAMPLE_FMT_FLT) {
407 s->in_sampleformat = AF_FORMAT_FLOAT_NE;
408 s->lavc_actx->sample_fmt = fmts[i];
409 break;
412 if (av_sample_fmt_is_planar(s->lavc_actx->sample_fmt)) {
413 s->avr = avresample_alloc_context();
414 if (!s->avr)
415 abort();
417 char buf[100];
418 mp_msg(MSGT_AFILTER, MSGL_V, "[af_lavcac3enc]: in sample format: %s\n",
419 af_fmt2str(s->in_sampleformat, buf, 100));
420 s->pending_data_size = AF_NCH * AC3_FRAME_SIZE *
421 af_fmt2bits(s->in_sampleformat) / 8;
422 s->pending_data = malloc(s->pending_data_size);
424 return AF_OK;
427 af_info_t af_info_lavcac3enc = {
428 "runtime encode to ac3 using libavcodec",
429 "lavcac3enc",
430 "Ulion",
432 AF_FLAGS_REENTRANT,
433 af_open