2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * HDCD decoding filter, using libhdcd
24 #include <hdcd/hdcd_simple.h>
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/opt.h"
34 typedef struct HDCDContext {
39 /* AVOption members */
40 /** analyze mode replaces the audio with a solid tone and adjusts
41 * the amplitude to signal some specific aspect of the decoding
42 * process. See docs or HDCD_ANA_* defines. */
44 /* end AVOption members */
47 #define OFFSET(x) offsetof(HDCDContext, x)
48 #define A AV_OPT_FLAG_AUDIO_PARAM
49 #define HDCD_ANA_MAX 6
50 static const AVOption hdcd_options[] = {
51 { "analyze_mode", "Replace audio with solid tone and signal some processing aspect in the amplitude.",
52 OFFSET(analyze_mode), AV_OPT_TYPE_INT, { .i64=HDCD_ANA_OFF }, 0, HDCD_ANA_MAX, A, "analyze_mode"},
53 { "off", HDCD_ANA_OFF_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_OFF}, 0, 0, A, "analyze_mode" },
54 { "lle", HDCD_ANA_LLE_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_LLE}, 0, 0, A, "analyze_mode" },
55 { "pe", HDCD_ANA_PE_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_PE}, 0, 0, A, "analyze_mode" },
56 { "cdt", HDCD_ANA_CDT_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_CDT}, 0, 0, A, "analyze_mode" },
57 { "tgm", HDCD_ANA_TGM_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_TGM}, 0, 0, A, "analyze_mode" },
58 { "pel", HDCD_ANA_PEL_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_PEL}, 0, 0, A, "analyze_mode" },
59 { "ltgm", HDCD_ANA_LTGM_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_LTGM}, 0, 0, A, "analyze_mode" },
63 static const AVClass hdcd_class = {
64 .class_name = "HDCD filter",
65 .item_name = av_default_item_name,
66 .option = hdcd_options,
67 .version = LIBAVFILTER_VERSION_INT,
70 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
72 AVFilterContext *ctx = inlink->dst;
73 HDCDContext *s = ctx->priv;
74 AVFilterLink *outlink = ctx->outputs[0];
76 const int16_t *in_data;
79 int channel_count = av_get_channel_layout_nb_channels(in->channel_layout);
81 out = ff_get_audio_buffer(outlink, in->nb_samples);
84 return AVERROR(ENOMEM);
86 result = av_frame_copy_props(out, in);
93 in_data = (int16_t *)in->data[0];
94 out_data = (int32_t *)out->data[0];
95 for (n = 0; n < in->nb_samples * channel_count; n++)
96 out_data[n] = in_data[n];
98 hdcd_process(s->shdcd, out_data, in->nb_samples);
101 return ff_filter_frame(outlink, out);
104 static int query_formats(AVFilterContext *ctx)
106 AVFilterFormats *in_formats, *out_formats, *sample_rates = NULL;
107 AVFilterChannelLayouts *layouts = NULL;
108 AVFilterLink *inlink = ctx->inputs[0];
109 AVFilterLink *outlink = ctx->outputs[0];
111 static const enum AVSampleFormat sample_fmts_in[] = {
115 static const enum AVSampleFormat sample_fmts_out[] = {
120 ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
122 ff_set_common_channel_layouts(ctx, layouts);
124 in_formats = ff_make_format_list(sample_fmts_in);
125 out_formats = ff_make_format_list(sample_fmts_out);
126 if (!in_formats || !out_formats)
127 return AVERROR(ENOMEM);
129 ff_formats_ref(in_formats, &inlink->out_formats);
130 ff_formats_ref(out_formats, &outlink->in_formats);
132 ff_add_format(&sample_rates, 44100);
133 ff_set_common_samplerates(ctx, sample_rates);
137 static av_cold void uninit(AVFilterContext *ctx)
139 HDCDContext *s = ctx->priv;
140 char detect_str[256] = "";
142 /* log the HDCD decode information */
143 hdcd_detect_str(s->shdcd, detect_str, sizeof(detect_str));
144 av_log(ctx, AV_LOG_INFO, "%s\n", detect_str);
149 /** callback for error logging */
150 static void af_hdcd_log(const void *priv, const char *fmt, va_list args)
152 av_vlog((AVFilterContext *)priv, AV_LOG_VERBOSE, fmt, args);
155 static av_cold int init(AVFilterContext *ctx)
157 HDCDContext *s = ctx->priv;
159 s->shdcd = hdcd_new();
160 hdcd_logger_attach(s->shdcd, af_hdcd_log, ctx);
163 hdcd_analyze_mode(s->shdcd, s->analyze_mode);
164 av_log(ctx, AV_LOG_VERBOSE, "Analyze mode: [%d] %s\n",
165 s->analyze_mode, hdcd_str_analyze_mode_desc(s->analyze_mode));
170 static const AVFilterPad avfilter_af_hdcd_inputs[] = {
173 .type = AVMEDIA_TYPE_AUDIO,
174 .filter_frame = filter_frame,
179 static const AVFilterPad avfilter_af_hdcd_outputs[] = {
182 .type = AVMEDIA_TYPE_AUDIO,
187 AVFilter ff_af_hdcd = {
189 .description = NULL_IF_CONFIG_SMALL("Apply High Definition Compatible Digital (HDCD) decoding."),
190 .priv_size = sizeof(HDCDContext),
191 .priv_class = &hdcd_class,
194 .query_formats = query_formats,
195 .inputs = avfilter_af_hdcd_inputs,
196 .outputs = avfilter_af_hdcd_outputs,