]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_hdcd.c
matroskaenc: factor ts_offset into block timecode computation
[ffmpeg] / libavfilter / af_hdcd.c
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /**
20  * @file
21  * HDCD decoding filter, using libhdcd
22  */
23
24 #include <hdcd/hdcd_simple.h>
25
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/opt.h"
28
29 #include "audio.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33
34 typedef struct HDCDContext {
35     const AVClass *class;
36
37     hdcd_simple *shdcd;
38
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. */
43     int analyze_mode;
44     /* end AVOption members */
45 } HDCDContext;
46
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" },
60     { NULL }
61 };
62
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,
68 };
69
70 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
71 {
72     AVFilterContext *ctx = inlink->dst;
73     HDCDContext *s = ctx->priv;
74     AVFilterLink *outlink = ctx->outputs[0];
75     AVFrame *out;
76     const int16_t *in_data;
77     int32_t *out_data;
78     int n, result;
79     int channel_count = av_get_channel_layout_nb_channels(in->channel_layout);
80
81     out = ff_get_audio_buffer(outlink, in->nb_samples);
82     if (!out) {
83         av_frame_free(&in);
84         return AVERROR(ENOMEM);
85     }
86     result = av_frame_copy_props(out, in);
87     if (result) {
88         av_frame_free(&out);
89         av_frame_free(&in);
90         return result;
91     }
92
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];
97
98     hdcd_process(s->shdcd, out_data, in->nb_samples);
99
100     av_frame_free(&in);
101     return ff_filter_frame(outlink, out);
102 }
103
104 static int query_formats(AVFilterContext *ctx)
105 {
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];
110
111     static const enum AVSampleFormat sample_fmts_in[] = {
112         AV_SAMPLE_FMT_S16,
113         AV_SAMPLE_FMT_NONE
114     };
115     static const enum AVSampleFormat sample_fmts_out[] = {
116         AV_SAMPLE_FMT_S32,
117         AV_SAMPLE_FMT_NONE
118     };
119
120     ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
121
122     ff_set_common_channel_layouts(ctx, layouts);
123
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);
128
129     ff_formats_ref(in_formats, &inlink->out_formats);
130     ff_formats_ref(out_formats, &outlink->in_formats);
131
132     ff_add_format(&sample_rates, 44100);
133     ff_set_common_samplerates(ctx, sample_rates);
134     return 0;
135 }
136
137 static av_cold void uninit(AVFilterContext *ctx)
138 {
139     HDCDContext *s = ctx->priv;
140     char detect_str[256] = "";
141
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);
145
146     hdcd_free(s->shdcd);
147 }
148
149 /** callback for error logging */
150 static void af_hdcd_log(const void *priv, const char *fmt, va_list args)
151 {
152     av_vlog((AVFilterContext *)priv, AV_LOG_VERBOSE, fmt, args);
153 }
154
155 static av_cold int init(AVFilterContext *ctx)
156 {
157     HDCDContext *s = ctx->priv;
158
159     s->shdcd = hdcd_new();
160     hdcd_logger_attach(s->shdcd, af_hdcd_log, ctx);
161
162     if (s->analyze_mode)
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));
166
167     return 0;
168 }
169
170 static const AVFilterPad avfilter_af_hdcd_inputs[] = {
171     {
172         .name         = "default",
173         .type         = AVMEDIA_TYPE_AUDIO,
174         .filter_frame = filter_frame,
175     },
176     { NULL }
177 };
178
179 static const AVFilterPad avfilter_af_hdcd_outputs[] = {
180     {
181         .name = "default",
182         .type = AVMEDIA_TYPE_AUDIO,
183     },
184     { NULL }
185 };
186
187 AVFilter ff_af_hdcd = {
188     .name          = "hdcd",
189     .description   = NULL_IF_CONFIG_SMALL("Apply High Definition Compatible Digital (HDCD) decoding."),
190     .priv_size     = sizeof(HDCDContext),
191     .priv_class    = &hdcd_class,
192     .init          = init,
193     .uninit        = uninit,
194     .query_formats = query_formats,
195     .inputs        = avfilter_af_hdcd_inputs,
196     .outputs       = avfilter_af_hdcd_outputs,
197 };