annotate libaf/af_lavcac3enc.c @ 37195:ac6c37d85d65 default tip

configure: Fix initialization of variable def_local_aligned_32 It contiained the #define of HAVE_LOCAL_ALIGNED_16 instead of HAVE_LOCAL_ALIGNED_32.
author al
date Sun, 28 Sep 2014 18:38:41 +0000
parents f77a74ebb95e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
1 /*
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
2 * audio filter for runtime AC-3 encoding with libavcodec.
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
3 *
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
4 * Copyright (C) 2007 Ulion <ulion A gmail P com>
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
5 *
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
6 * This file is part of MPlayer.
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
7 *
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
8 * MPlayer is free software; you can redistribute it and/or modify
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
11 * (at your option) any later version.
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
12 *
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
13 * MPlayer is distributed in the hope that it will be useful,
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
16 * GNU General Public License for more details.
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
17 *
26740
b3a38b361fef Use standard license headers with standard formatting.
diego
parents: 26120
diff changeset
18 * You should have received a copy of the GNU General Public License along
b3a38b361fef Use standard license headers with standard formatting.
diego
parents: 26120
diff changeset
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
b3a38b361fef Use standard license headers with standard formatting.
diego
parents: 26120
diff changeset
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
21 */
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
22
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
23 #include <stdio.h>
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
24 #include <stdlib.h>
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
25 #include <string.h>
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
26 #include <inttypes.h>
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
27
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
28 #include "config.h"
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
29 #include "af.h"
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
30 #include "help_mp.h"
34174
a93891202051 Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents: 33871
diff changeset
31 #include "mp_msg.h"
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
32 #include "reorder_ch.h"
33871
30f5e5cd3676 Move code for setting up libav* logging callbacks from vd_ffmpeg to a
reimar
parents: 33198
diff changeset
33 #include "av_helpers.h"
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
34
26120
24ba95d88181 Add directory names to libavcodec #includes.
diego
parents: 25962
diff changeset
35 #include "libavcodec/avcodec.h"
35712
d206960484fe Add a number of missing libavutil header #includes.
diego
parents: 34652
diff changeset
36 #include "libavutil/common.h"
30243
05f085c36d5b Let the format filter do the AC3 endianness conversion instead of duplicating
reimar
parents: 30241
diff changeset
37 #include "libavutil/intreadwrite.h"
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
38
34652
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
39 #define AC3_MAX_CHANNELS 6
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
40 #define AC3_FRAME_SIZE 1536
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
41 #define AC3_MAX_CODED_FRAME_SIZE 3840
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
42 //#define AC3_BIT_RATES_COUNT 19
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
43
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
44 static const int ac3_bit_rates[] = {
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
45 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000,
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
46 192000, 224000, 256000, 320000, 384000, 448000, 512000, 576000, 640000
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
47 };
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
48
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
49 // Data for specific instances of this filter
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
50 typedef struct af_ac3enc_s {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
51 struct AVCodec *lavc_acodec;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
52 struct AVCodecContext *lavc_actx;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
53 int add_iec61937_header;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
54 int bit_rate;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
55 char *pending_data;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
56 int pending_len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
57 int expect_len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
58 int min_channel_num;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
59 } af_ac3enc_t;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
60
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
61 // Initialization and runtime control
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
62 static int control(struct af_instance_s *af, int cmd, void *arg)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
63 {
34260
07a21268eb84 Remove pointless casts.
reimar
parents: 34174
diff changeset
64 af_ac3enc_t *s = af->setup;
07a21268eb84 Remove pointless casts.
reimar
parents: 34174
diff changeset
65 af_data_t *data = arg;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
66 int i, bit_rate, test_output_res;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
67 static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
68 {0, 96000, 192000, 256000, 384000, 448000, 448000};
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
69
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
70 switch (cmd){
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
71 case AF_CONTROL_REINIT:
30241
02b9c1a452e1 Add support for distinguishing between little- and big-endian SPDIF AC3
reimar
parents: 29491
diff changeset
72 if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
73 return AF_DETACH;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
74
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
75 s->pending_len = 0;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
76 s->expect_len = AC3_FRAME_SIZE * data->nch * data->bps;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
77 if (s->add_iec61937_header)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
78 af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
79 else
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
80 af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
81
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
82 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
83 data->nch, data->rate, af->mul, s->expect_len);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
84
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
85 af->data->format = AF_FORMAT_S16_NE;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
86 if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
87 af->data->rate = data->rate;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
88 else
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
89 af->data->rate = 48000;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
90 if (data->nch > AC3_MAX_CHANNELS)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
91 af->data->nch = AC3_MAX_CHANNELS;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
92 else
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
93 af->data->nch = data->nch;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
94 af->data->bps = 2;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
95 test_output_res = af_test_output(af, data);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
96
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
97 bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
98
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
99 if (s->lavc_actx->channels != af->data->nch ||
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
100 s->lavc_actx->sample_rate != af->data->rate ||
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
101 s->lavc_actx->bit_rate != bit_rate) {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
102
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
103 if (s->lavc_actx->codec)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
104 avcodec_close(s->lavc_actx);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
105
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
106 // Put sample parameters
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
107 s->lavc_actx->channels = af->data->nch;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
108 s->lavc_actx->sample_rate = af->data->rate;
36335
f77a74ebb95e Quick an inefficient hack to enable planar encoding.
reimar
parents: 36334
diff changeset
109 s->lavc_actx->sample_fmt = AV_SAMPLE_FMT_S16P;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
110 s->lavc_actx->bit_rate = bit_rate;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
111
34566
f3d53cd55376 Update deprecated avcodec_alloc_context()/avcodec_open() API calls
siretart
parents: 34260
diff changeset
112 if(avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) {
33198
1f418f605d13 Do not use a deprecated audio encoder.
cehoyos
parents: 33109
diff changeset
113 mp_msg(MSGT_AFILTER, MSGL_ERR, MSGTR_CouldntOpenCodec, "ac3", bit_rate);
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
114 return AF_ERROR;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
115 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
116 }
30243
05f085c36d5b Let the format filter do the AC3 endianness conversion instead of duplicating
reimar
parents: 30241
diff changeset
117 af->data->format = AF_FORMAT_AC3_BE;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
118 af->data->nch = 2;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
119 return test_output_res;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
120 case AF_CONTROL_COMMAND_LINE:
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
121 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
122 s->bit_rate = 0;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
123 s->min_channel_num = 0;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
124 s->add_iec61937_header = 0;
34260
07a21268eb84 Remove pointless casts.
reimar
parents: 34174
diff changeset
125 sscanf(arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
126 &s->min_channel_num);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
127 if (s->bit_rate < 1000)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
128 s->bit_rate *= 1000;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
129 if (s->bit_rate) {
34652
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
130 for (i = 0; i < FF_ARRAY_ELEMS(ac3_bit_rates); ++i)
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
131 if (ac3_bit_rates[i] == s->bit_rate)
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
132 break;
34652
4e7cc799cf54 lavcac3enc: make the filter buildable with shared FFmpeg
diego
parents: 34566
diff changeset
133 if (i >= FF_ARRAY_ELEMS(ac3_bit_rates)) {
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
134 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
135 "bitrate %d, use default bitrate (check manpage to see "
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
136 "supported bitrates).\n", s->bit_rate);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
137 s->bit_rate = 0;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
138 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
139 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
140 if (s->min_channel_num == 0)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
141 s->min_channel_num = 5;
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
142 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
143 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
144 s->min_channel_num);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
145 return AF_OK;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
146 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
147 return AF_UNKNOWN;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
148 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
149
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
150 // Deallocate memory
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
151 static void uninit(struct af_instance_s* af)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
152 {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
153 if (af->data)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
154 free(af->data->audio);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
155 free(af->data);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
156 if (af->setup) {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
157 af_ac3enc_t *s = af->setup;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
158 af->setup = NULL;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
159 if(s->lavc_actx) {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
160 if (s->lavc_actx->codec)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
161 avcodec_close(s->lavc_actx);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
162 free(s->lavc_actx);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
163 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
164 free(s->pending_data);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
165 free(s);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
166 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
167 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
168
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
169 // Filter data through filter
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
170 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
171 {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
172 af_ac3enc_t *s = af->setup;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
173 af_data_t *c = data; // Current working data
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
174 af_data_t *l;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
175 int len, left, outsize = 0, destsize;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
176 char *buf, *src, *dest;
25642
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
177 int max_output_len;
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
178 int frame_num = (data->len + s->pending_len) / s->expect_len;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
179
25642
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
180 if (s->add_iec61937_header)
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
181 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
182 else
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
183 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
184
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
185 if (af->data->len < max_output_len) {
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
186 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
25642
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
187 "old len = %i, new len = %i\n", af->info->name, af->data->len,
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
188 max_output_len);
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
189 free(af->data->audio);
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
190 af->data->audio = malloc(max_output_len);
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
191 if (!af->data->audio) {
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
192 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
25642
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
193 return NULL;
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
194 }
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
195 af->data->len = max_output_len;
0c10c8859be8 Fix buffer overflow bug by calculate the buffer size accurately.
ulion
parents: 25357
diff changeset
196 }
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
197
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
198 l = af->data; // Local data
34260
07a21268eb84 Remove pointless casts.
reimar
parents: 34174
diff changeset
199 buf = l->audio;
07a21268eb84 Remove pointless casts.
reimar
parents: 34174
diff changeset
200 src = c->audio;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
201 left = c->len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
202
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
203
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
204 while (left > 0) {
36333
dc90c43a2674 lavcac3enc: avoid code duplication.
reimar
parents: 35712
diff changeset
205 void *in = NULL;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
206 if (left + s->pending_len < s->expect_len) {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
207 memcpy(s->pending_data + s->pending_len, src, left);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
208 src += left;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
209 s->pending_len += left;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
210 left = 0;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
211 break;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
212 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
213
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
214 dest = s->add_iec61937_header ? buf + 8 : buf;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
215 destsize = (char *)l->audio + l->len - buf;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
216
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
217 if (s->pending_len) {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
218 int needs = s->expect_len - s->pending_len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
219 if (needs > 0) {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
220 memcpy(s->pending_data + s->pending_len, src, needs);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
221 src += needs;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
222 left -= needs;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
223 }
36333
dc90c43a2674 lavcac3enc: avoid code duplication.
reimar
parents: 35712
diff changeset
224 in = s->pending_data;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
225 s->pending_len = 0;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
226 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
227 else {
36333
dc90c43a2674 lavcac3enc: avoid code duplication.
reimar
parents: 35712
diff changeset
228 in = src;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
229 src += s->expect_len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
230 left -= s->expect_len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
231 }
36334
c1033e9288b1 Extract audio encoding code into a separate helper function.
reimar
parents: 36333
diff changeset
232 len = lavc_encode_audio(s->lavc_actx, in, s->expect_len, dest, destsize);
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
233 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
234 len, s->pending_len);
36334
c1033e9288b1 Extract audio encoding code into a separate helper function.
reimar
parents: 36333
diff changeset
235 if (len < 0) len = 0;
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
236
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
237 if (s->add_iec61937_header) {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
238 int bsmod = dest[5] & 0x7;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
239
30243
05f085c36d5b Let the format filter do the AC3 endianness conversion instead of duplicating
reimar
parents: 30241
diff changeset
240 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
05f085c36d5b Let the format filter do the AC3 endianness conversion instead of duplicating
reimar
parents: 30241
diff changeset
241 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
05f085c36d5b Let the format filter do the AC3 endianness conversion instead of duplicating
reimar
parents: 30241
diff changeset
242 buf[4] = bsmod; // bsmod
05f085c36d5b Let the format filter do the AC3 endianness conversion instead of duplicating
reimar
parents: 30241
diff changeset
243 buf[5] = 0x01; // data-type ac3
05f085c36d5b Let the format filter do the AC3 endianness conversion instead of duplicating
reimar
parents: 30241
diff changeset
244 AV_WB16(buf + 6, len << 3); // number of bits in payload
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
245
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
246 memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
247 len = AC3_FRAME_SIZE * 2 * 2;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
248 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
249
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
250 outsize += len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
251 buf += len;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
252 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
253 c->audio = l->audio;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
254 c->nch = 2;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
255 c->bps = 2;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
256 c->len = outsize;
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
257 mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
258 outsize, s->pending_len);
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
259 return c;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
260 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
261
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
262 static int af_open(af_instance_t* af){
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
263
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
264 af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
265 int pending_space = 2 * AC3_MAX_CHANNELS * AC3_FRAME_SIZE;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
266 s->pending_data = calloc(pending_space, sizeof(char));
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
267
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
268 af->control=control;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
269 af->uninit=uninit;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
270 af->play=play;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
271 af->mul=1;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
272 af->data=calloc(1,sizeof(af_data_t));
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
273 af->setup=s;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
274
31960
1ba5eef167aa Refactor more instances of avcodec_initialized handling into init_avcodec().
diego
parents: 30243
diff changeset
275 init_avcodec();
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
276
32695
5f69ed383ff1 Fix -af lavcac3enc after float ac3 encoder was made default in libavcodec.
cehoyos
parents: 31960
diff changeset
277 s->lavc_acodec = avcodec_find_encoder_by_name("ac3_fixed");
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
278 if (!s->lavc_acodec) {
32695
5f69ed383ff1 Fix -af lavcac3enc after float ac3 encoder was made default in libavcodec.
cehoyos
parents: 31960
diff changeset
279 mp_msg(MSGT_AFILTER, MSGL_ERR, MSGTR_LavcAudioCodecNotFound, "ac3_fixed");
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
280 return AF_ERROR;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
281 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
282
34566
f3d53cd55376 Update deprecated avcodec_alloc_context()/avcodec_open() API calls
siretart
parents: 34260
diff changeset
283 s->lavc_actx = avcodec_alloc_context3(NULL);
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
284 if (!s->lavc_actx) {
29049
8c706ce21c6f Remove af_msg special-casing API in libaf.
bircoph
parents: 26740
diff changeset
285 mp_msg(MSGT_AFILTER, MSGL_ERR, MSGTR_CouldntAllocateLavcContext);
25357
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
286 return AF_ERROR;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
287 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
288
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
289 return AF_OK;
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
290 }
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
291
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
292 af_info_t af_info_lavcac3enc = {
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
293 "runtime encode to ac3 using libavcodec",
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
294 "lavcac3enc",
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
295 "Ulion",
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
296 "",
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
297 AF_FLAGS_REENTRANT,
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
298 af_open
b265c001e64a Add new audio filter for encoding multi-channel audio into ac3 at runtime.
ulion
parents:
diff changeset
299 };