Libav
af_hdcd.c
Go to the documentation of this file.
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 
24 #include <hdcd/hdcd_simple.h>
25 
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 */
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 
105 {
106  AVFilterFormats *in_formats, *out_formats, *sample_rates = NULL;
108  AVFilterLink *inlink = ctx->inputs[0];
109  AVFilterLink *outlink = ctx->outputs[0];
110 
111  static const enum AVSampleFormat sample_fmts_in[] = {
114  };
115  static const enum AVSampleFormat sample_fmts_out[] = {
117  AV_SAMPLE_FMT_NONE
118  };
119 
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 
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 
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 
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 
171  {
172  .name = "default",
173  .type = AVMEDIA_TYPE_AUDIO,
174  .filter_frame = filter_frame,
175  },
176  { NULL }
177 };
178 
180  {
181  .name = "default",
182  .type = AVMEDIA_TYPE_AUDIO,
183  },
184  { NULL }
185 };
186 
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,
195  .inputs = avfilter_af_hdcd_inputs,
196  .outputs = avfilter_af_hdcd_outputs,
197 };
static void af_hdcd_log(const void *priv, const char *fmt, va_list args)
callback for error logging
Definition: af_hdcd.c:150
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:259
Main libavfilter public API header.
static av_cold int init(AVFilterContext *ctx)
Definition: af_hdcd.c:155
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define LIBAVFILTER_VERSION_INT
Definition: version.h:36
#define AV_CH_LAYOUT_STEREO
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
const char * name
Pad name.
Definition: internal.h:41
hdcd_simple * shdcd
Definition: af_hdcd.c:37
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:270
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:747
#define av_cold
Definition: attributes.h:66
AVOptions.
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
signed 32 bits
Definition: samplefmt.h:64
A filter pad used for either input or output.
Definition: internal.h:35
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:213
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
void * priv
private data for use by the filter
Definition: avfilter.h:277
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
static const AVOption hdcd_options[]
Definition: af_hdcd.c:50
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_hdcd.c:70
#define HDCD_ANA_MAX
Definition: af_hdcd.c:49
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:294
static const AVFilterPad avfilter_af_hdcd_outputs[]
Definition: af_hdcd.c:179
audio channel layout utility functions
#define OFFSET(x)
Definition: af_hdcd.c:47
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
int analyze_mode
analyze mode replaces the audio with a solid tone and adjusts the amplitude to signal some specific a...
Definition: af_hdcd.c:43
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_hdcd.c:137
#define A
Definition: af_hdcd.c:48
static const AVFilterPad avfilter_af_hdcd_inputs[]
Definition: af_hdcd.c:170
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:276
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
av_default_item_name
Definition: dnxhdenc.c:55
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:34
Filter definition.
Definition: avfilter.h:120
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:248
AVFilter ff_af_hdcd
Definition: af_hdcd.c:187
void av_vlog(void *avcl, int level, const char *fmt, va_list vl)
Send the specified message to the log if the level is less than or equal to the current av_log_level...
Definition: log.c:195
const char * name
Filter name.
Definition: avfilter.h:124
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:274
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:433
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:377
static const AVClass hdcd_class
Definition: af_hdcd.c:63
signed 16 bits
Definition: samplefmt.h:63
int ff_add_format(AVFilterFormats **avff, int fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:208
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:262
FILE * out
Definition: movenc.c:54
static int query_formats(AVFilterContext *ctx)
Definition: af_hdcd.c:104
void ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:370
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:386