demux_mkv: fix relative seeks without index
[mplayer/glamo.git] / libaf / af_lavcac3enc.c
blobae557579d68c58064ba2c48bf2e82364f9162e2d
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 "libmpcodecs/vd_ffmpeg.h"
30 #include "config.h"
31 #include "af.h"
32 #include "reorder_ch.h"
34 #include "libavcodec/avcodec.h"
35 #include "ffmpeg_files/intreadwrite.h"
37 #define AC3_MAX_CHANNELS 6
38 #define AC3_MAX_CODED_FRAME_SIZE 3840
39 #define AC3_FRAME_SIZE (6 * 256)
40 const uint16_t ac3_bitrate_tab[19] = {
41 32, 40, 48, 56, 64, 80, 96, 112, 128,
42 160, 192, 224, 256, 320, 384, 448, 512, 576, 640
45 // Data for specific instances of this filter
46 typedef struct af_ac3enc_s {
47 struct AVCodec *lavc_acodec;
48 struct AVCodecContext *lavc_actx;
49 int add_iec61937_header;
50 int bit_rate;
51 int pending_data_size;
52 char *pending_data;
53 int pending_len;
54 int expect_len;
55 int min_channel_num;
56 } af_ac3enc_t;
58 // Initialization and runtime control
59 static int control(struct af_instance_s *af, int cmd, void *arg)
61 af_ac3enc_t *s = (af_ac3enc_t *)af->setup;
62 af_data_t *data = (af_data_t *)arg;
63 int i, bit_rate, test_output_res;
64 static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
65 {0, 96000, 192000, 256000, 384000, 448000, 448000};
67 switch (cmd){
68 case AF_CONTROL_REINIT:
69 if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
70 return AF_DETACH;
72 af->data->format = AF_FORMAT_S16_NE;
73 if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
74 af->data->rate = data->rate;
75 else
76 af->data->rate = 48000;
77 if (data->nch > AC3_MAX_CHANNELS)
78 af->data->nch = AC3_MAX_CHANNELS;
79 else
80 af->data->nch = data->nch;
81 af->data->bps = 2;
82 test_output_res = af_test_output(af, data);
84 s->pending_len = 0;
85 s->expect_len = AC3_FRAME_SIZE * data->nch * af->data->bps;
86 assert(s->expect_len <= s->pending_data_size);
87 if (s->add_iec61937_header)
88 af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
89 else
90 af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
92 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
93 data->nch, data->rate, af->mul, s->expect_len);
95 bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
97 if (s->lavc_actx->channels != af->data->nch ||
98 s->lavc_actx->sample_rate != af->data->rate ||
99 s->lavc_actx->bit_rate != bit_rate) {
101 if (s->lavc_actx->codec)
102 avcodec_close(s->lavc_actx);
104 // Put sample parameters
105 s->lavc_actx->channels = af->data->nch;
106 s->lavc_actx->sample_rate = af->data->rate;
107 s->lavc_actx->bit_rate = bit_rate;
109 if(avcodec_open(s->lavc_actx, s->lavc_acodec) < 0) {
110 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
111 return AF_ERROR;
114 if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) {
115 mp_msg(MSGT_AFILTER, MSGL_ERR, "lavcac3enc: unexpected ac3 "
116 "encoder frame size %d\n", s->lavc_actx->frame_size);
117 return AF_ERROR;
119 af->data->format = AF_FORMAT_AC3_BE;
120 af->data->nch = 2;
121 return test_output_res;
122 case AF_CONTROL_COMMAND_LINE:
123 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
124 s->bit_rate = 0;
125 s->min_channel_num = 0;
126 s->add_iec61937_header = 0;
127 sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
128 &s->min_channel_num);
129 if (s->bit_rate < 1000)
130 s->bit_rate *= 1000;
131 if (s->bit_rate) {
132 for (i = 0; i < 19; ++i)
133 if (ac3_bitrate_tab[i] * 1000 == s->bit_rate)
134 break;
135 if (i >= 19) {
136 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
137 "bitrate %d, use default bitrate (check manpage to see "
138 "supported bitrates).\n", s->bit_rate);
139 s->bit_rate = 0;
142 if (s->min_channel_num == 0)
143 s->min_channel_num = 5;
144 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
145 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
146 s->min_channel_num);
147 return AF_OK;
149 return AF_UNKNOWN;
152 // Deallocate memory
153 static void uninit(struct af_instance_s* af)
155 if (af->data)
156 free(af->data->audio);
157 free(af->data);
158 if (af->setup) {
159 af_ac3enc_t *s = af->setup;
160 af->setup = NULL;
161 if(s->lavc_actx) {
162 if (s->lavc_actx->codec)
163 avcodec_close(s->lavc_actx);
164 free(s->lavc_actx);
166 free(s->pending_data);
167 free(s);
171 // Filter data through filter
172 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
174 af_ac3enc_t *s = af->setup;
175 af_data_t *c = data; // Current working data
176 af_data_t *l;
177 int len, left, outsize = 0, destsize;
178 char *buf, *src, *dest;
179 int max_output_len;
180 int frame_num = (data->len + s->pending_len) / s->expect_len;
182 if (s->add_iec61937_header)
183 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
184 else
185 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
187 if (af->data->len < max_output_len) {
188 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
189 "old len = %i, new len = %i\n", af->info->name, af->data->len,
190 max_output_len);
191 free(af->data->audio);
192 af->data->audio = malloc(max_output_len);
193 if (!af->data->audio) {
194 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
195 return NULL;
197 af->data->len = max_output_len;
200 l = af->data; // Local data
201 buf = (char *)l->audio;
202 src = (char *)c->audio;
203 left = c->len;
206 while (left > 0) {
207 if (left + s->pending_len < s->expect_len) {
208 memcpy(s->pending_data + s->pending_len, src, left);
209 src += left;
210 s->pending_len += left;
211 left = 0;
212 break;
215 dest = s->add_iec61937_header ? buf + 8 : buf;
216 destsize = (char *)l->audio + l->len - buf;
218 if (s->pending_len) {
219 int needs = s->expect_len - s->pending_len;
220 if (needs > 0) {
221 memcpy(s->pending_data + s->pending_len, src, needs);
222 src += needs;
223 left -= needs;
226 if (c->nch >= 5)
227 reorder_channel_nch(s->pending_data,
228 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
229 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
230 c->nch,
231 s->expect_len / 2, 2);
233 len = avcodec_encode_audio(s->lavc_actx, dest, destsize,
234 (void *)s->pending_data);
235 s->pending_len = 0;
237 else {
238 if (c->nch >= 5)
239 reorder_channel_nch(src,
240 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
241 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
242 c->nch,
243 s->expect_len / 2, 2);
244 len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src);
245 src += s->expect_len;
246 left -= s->expect_len;
248 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
249 len, s->pending_len);
251 if (s->add_iec61937_header) {
252 int bsmod = dest[5] & 0x7;
254 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
255 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
256 buf[4] = bsmod; // bsmod
257 buf[5] = 0x01; // data-type ac3
258 AV_WB16(buf + 6, len << 3); // number of bits in payload
260 memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
261 len = AC3_FRAME_SIZE * 2 * 2;
264 outsize += len;
265 buf += len;
267 c->audio = l->audio;
268 c->nch = 2;
269 c->bps = 2;
270 c->len = outsize;
271 mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
272 outsize, s->pending_len);
273 return c;
276 static int af_open(af_instance_t* af){
278 af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
279 s->pending_data_size = 2 * AF_NCH * AC3_FRAME_SIZE;
280 s->pending_data = malloc(s->pending_data_size);
282 af->control=control;
283 af->uninit=uninit;
284 af->play=play;
285 af->mul=1;
286 af->data=calloc(1,sizeof(af_data_t));
287 af->setup=s;
289 init_avcodec();
291 s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
292 if (!s->lavc_acodec) {
293 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
294 return AF_ERROR;
297 s->lavc_actx = avcodec_alloc_context();
298 if (!s->lavc_actx) {
299 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
300 return AF_ERROR;
303 return AF_OK;
306 af_info_t af_info_lavcac3enc = {
307 "runtime encode to ac3 using libavcodec",
308 "lavcac3enc",
309 "Ulion",
311 AF_FLAGS_REENTRANT,
312 af_open