You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.1KB

  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. * @file
  20. * HDCD decoding filter, using libhdcd
  21. */
  22. #include <hdcd/hdcd_simple.h>
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/opt.h"
  25. #include "audio.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. typedef struct HDCDContext {
  30. const AVClass *class;
  31. hdcd_simple *shdcd;
  32. /* AVOption members */
  33. /** analyze mode replaces the audio with a solid tone and adjusts
  34. * the amplitude to signal some specific aspect of the decoding
  35. * process. See docs or HDCD_ANA_* defines. */
  36. int analyze_mode;
  37. /* end AVOption members */
  38. } HDCDContext;
  39. #define OFFSET(x) offsetof(HDCDContext, x)
  40. #define A AV_OPT_FLAG_AUDIO_PARAM
  41. #define HDCD_ANA_MAX 6
  42. static const AVOption hdcd_options[] = {
  43. { "analyze_mode", "Replace audio with solid tone and signal some processing aspect in the amplitude.",
  44. OFFSET(analyze_mode), AV_OPT_TYPE_INT, { .i64=HDCD_ANA_OFF }, 0, HDCD_ANA_MAX, A, "analyze_mode"},
  45. { "off", HDCD_ANA_OFF_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_OFF}, 0, 0, A, "analyze_mode" },
  46. { "lle", HDCD_ANA_LLE_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_LLE}, 0, 0, A, "analyze_mode" },
  47. { "pe", HDCD_ANA_PE_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_PE}, 0, 0, A, "analyze_mode" },
  48. { "cdt", HDCD_ANA_CDT_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_CDT}, 0, 0, A, "analyze_mode" },
  49. { "tgm", HDCD_ANA_TGM_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_TGM}, 0, 0, A, "analyze_mode" },
  50. { "pel", HDCD_ANA_PEL_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_PEL}, 0, 0, A, "analyze_mode" },
  51. { "ltgm", HDCD_ANA_LTGM_DESC, 0, AV_OPT_TYPE_CONST, { .i64 = HDCD_ANA_LTGM}, 0, 0, A, "analyze_mode" },
  52. { NULL }
  53. };
  54. static const AVClass hdcd_class = {
  55. .class_name = "HDCD filter",
  56. .item_name = av_default_item_name,
  57. .option = hdcd_options,
  58. .version = LIBAVFILTER_VERSION_INT,
  59. };
  60. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  61. {
  62. AVFilterContext *ctx = inlink->dst;
  63. HDCDContext *s = ctx->priv;
  64. AVFilterLink *outlink = ctx->outputs[0];
  65. AVFrame *out;
  66. const int16_t *in_data;
  67. int32_t *out_data;
  68. int n, result;
  69. int channel_count = av_get_channel_layout_nb_channels(in->channel_layout);
  70. out = ff_get_audio_buffer(outlink, in->nb_samples);
  71. if (!out) {
  72. av_frame_free(&in);
  73. return AVERROR(ENOMEM);
  74. }
  75. result = av_frame_copy_props(out, in);
  76. if (result) {
  77. av_frame_free(&out);
  78. av_frame_free(&in);
  79. return result;
  80. }
  81. in_data = (int16_t *)in->data[0];
  82. out_data = (int32_t *)out->data[0];
  83. for (n = 0; n < in->nb_samples * channel_count; n++)
  84. out_data[n] = in_data[n];
  85. hdcd_process(s->shdcd, out_data, in->nb_samples);
  86. av_frame_free(&in);
  87. return ff_filter_frame(outlink, out);
  88. }
  89. static int query_formats(AVFilterContext *ctx)
  90. {
  91. AVFilterFormats *in_formats, *out_formats, *sample_rates = NULL;
  92. AVFilterChannelLayouts *layouts = NULL;
  93. AVFilterLink *inlink = ctx->inputs[0];
  94. AVFilterLink *outlink = ctx->outputs[0];
  95. static const enum AVSampleFormat sample_fmts_in[] = {
  96. AV_SAMPLE_FMT_S16,
  97. AV_SAMPLE_FMT_NONE
  98. };
  99. static const enum AVSampleFormat sample_fmts_out[] = {
  100. AV_SAMPLE_FMT_S32,
  101. AV_SAMPLE_FMT_NONE
  102. };
  103. ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
  104. ff_set_common_channel_layouts(ctx, layouts);
  105. in_formats = ff_make_format_list(sample_fmts_in);
  106. out_formats = ff_make_format_list(sample_fmts_out);
  107. if (!in_formats || !out_formats)
  108. return AVERROR(ENOMEM);
  109. ff_formats_ref(in_formats, &inlink->out_formats);
  110. ff_formats_ref(out_formats, &outlink->in_formats);
  111. ff_add_format(&sample_rates, 44100);
  112. ff_set_common_samplerates(ctx, sample_rates);
  113. return 0;
  114. }
  115. static av_cold void uninit(AVFilterContext *ctx)
  116. {
  117. HDCDContext *s = ctx->priv;
  118. char detect_str[256] = "";
  119. /* log the HDCD decode information */
  120. hdcd_detect_str(s->shdcd, detect_str, sizeof(detect_str));
  121. av_log(ctx, AV_LOG_INFO, "%s\n", detect_str);
  122. hdcd_free(s->shdcd);
  123. }
  124. /** callback for error logging */
  125. static void af_hdcd_log(const void *priv, const char *fmt, va_list args)
  126. {
  127. av_vlog((AVFilterContext *)priv, AV_LOG_VERBOSE, fmt, args);
  128. }
  129. static av_cold int init(AVFilterContext *ctx)
  130. {
  131. HDCDContext *s = ctx->priv;
  132. s->shdcd = hdcd_new();
  133. hdcd_logger_attach(s->shdcd, af_hdcd_log, ctx);
  134. if (s->analyze_mode)
  135. hdcd_analyze_mode(s->shdcd, s->analyze_mode);
  136. av_log(ctx, AV_LOG_VERBOSE, "Analyze mode: [%d] %s\n",
  137. s->analyze_mode, hdcd_str_analyze_mode_desc(s->analyze_mode));
  138. return 0;
  139. }
  140. static const AVFilterPad avfilter_af_hdcd_inputs[] = {
  141. {
  142. .name = "default",
  143. .type = AVMEDIA_TYPE_AUDIO,
  144. .filter_frame = filter_frame,
  145. },
  146. { NULL }
  147. };
  148. static const AVFilterPad avfilter_af_hdcd_outputs[] = {
  149. {
  150. .name = "default",
  151. .type = AVMEDIA_TYPE_AUDIO,
  152. },
  153. { NULL }
  154. };
  155. AVFilter ff_af_hdcd = {
  156. .name = "hdcd",
  157. .description = NULL_IF_CONFIG_SMALL("Apply High Definition Compatible Digital (HDCD) decoding."),
  158. .priv_size = sizeof(HDCDContext),
  159. .priv_class = &hdcd_class,
  160. .init = init,
  161. .uninit = uninit,
  162. .query_formats = query_formats,
  163. .inputs = avfilter_af_hdcd_inputs,
  164. .outputs = avfilter_af_hdcd_outputs,
  165. };