yading@10: /* yading@10: * Copyright (c) 2012 Clément Bœsch yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or modify yading@10: * it under the terms of the GNU General Public License as published by yading@10: * the Free Software Foundation; either version 2 of the License, or yading@10: * (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the yading@10: * GNU General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU General Public License along yading@10: * with FFmpeg; if not, write to the Free Software Foundation, Inc., yading@10: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * EBU R.128 implementation yading@10: * @see http://tech.ebu.ch/loudness yading@10: * @see https://www.youtube.com/watch?v=iuEtQqC-Sqo "EBU R128 Introduction - Florian Camerer" yading@10: * @todo True Peak yading@10: * @todo implement start/stop/reset through filter command injection yading@10: * @todo support other frequencies to avoid resampling yading@10: */ yading@10: yading@10: #include yading@10: yading@10: #include "libavutil/avassert.h" yading@10: #include "libavutil/avstring.h" yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/dict.h" yading@10: #include "libavutil/xga_font_data.h" yading@10: #include "libavutil/opt.h" yading@10: #include "libavutil/timestamp.h" yading@10: #include "audio.h" yading@10: #include "avfilter.h" yading@10: #include "formats.h" yading@10: #include "internal.h" yading@10: yading@10: #define MAX_CHANNELS 63 yading@10: yading@10: /* pre-filter coefficients */ yading@10: #define PRE_B0 1.53512485958697 yading@10: #define PRE_B1 -2.69169618940638 yading@10: #define PRE_B2 1.19839281085285 yading@10: #define PRE_A1 -1.69065929318241 yading@10: #define PRE_A2 0.73248077421585 yading@10: yading@10: /* RLB-filter coefficients */ yading@10: #define RLB_B0 1.0 yading@10: #define RLB_B1 -2.0 yading@10: #define RLB_B2 1.0 yading@10: #define RLB_A1 -1.99004745483398 yading@10: #define RLB_A2 0.99007225036621 yading@10: yading@10: #define ABS_THRES -70 ///< silence gate: we discard anything below this absolute (LUFS) threshold yading@10: #define ABS_UP_THRES 10 ///< upper loud limit to consider (ABS_THRES being the minimum) yading@10: #define HIST_GRAIN 100 ///< defines histogram precision yading@10: #define HIST_SIZE ((ABS_UP_THRES - ABS_THRES) * HIST_GRAIN + 1) yading@10: yading@10: /** yading@10: * An histogram is an array of HIST_SIZE hist_entry storing all the energies yading@10: * recorded (with an accuracy of 1/HIST_GRAIN) of the loudnesses from ABS_THRES yading@10: * (at 0) to ABS_UP_THRES (at HIST_SIZE-1). yading@10: * This fixed-size system avoids the need of a list of energies growing yading@10: * infinitely over the time and is thus more scalable. yading@10: */ yading@10: struct hist_entry { yading@10: int count; ///< how many times the corresponding value occurred yading@10: double energy; ///< E = 10^((L + 0.691) / 10) yading@10: double loudness; ///< L = -0.691 + 10 * log10(E) yading@10: }; yading@10: yading@10: struct integrator { yading@10: double *cache[MAX_CHANNELS]; ///< window of filtered samples (N ms) yading@10: int cache_pos; ///< focus on the last added bin in the cache array yading@10: double sum[MAX_CHANNELS]; ///< sum of the last N ms filtered samples (cache content) yading@10: int filled; ///< 1 if the cache is completely filled, 0 otherwise yading@10: double rel_threshold; ///< relative threshold yading@10: double sum_kept_powers; ///< sum of the powers (weighted sums) above absolute threshold yading@10: int nb_kept_powers; ///< number of sum above absolute threshold yading@10: struct hist_entry *histogram; ///< histogram of the powers, used to compute LRA and I yading@10: }; yading@10: yading@10: struct rect { int x, y, w, h; }; yading@10: yading@10: typedef struct { yading@10: const AVClass *class; ///< AVClass context for log and options purpose yading@10: yading@10: /* video */ yading@10: int do_video; ///< 1 if video output enabled, 0 otherwise yading@10: int w, h; ///< size of the video output yading@10: struct rect text; ///< rectangle for the LU legend on the left yading@10: struct rect graph; ///< rectangle for the main graph in the center yading@10: struct rect gauge; ///< rectangle for the gauge on the right yading@10: AVFrame *outpicref; ///< output picture reference, updated regularly yading@10: int meter; ///< select a EBU mode between +9 and +18 yading@10: int scale_range; ///< the range of LU values according to the meter yading@10: int y_zero_lu; ///< the y value (pixel position) for 0 LU yading@10: int *y_line_ref; ///< y reference values for drawing the LU lines in the graph and the gauge yading@10: yading@10: /* audio */ yading@10: int nb_channels; ///< number of channels in the input yading@10: double *ch_weighting; ///< channel weighting mapping yading@10: int sample_count; ///< sample count used for refresh frequency, reset at refresh yading@10: yading@10: /* Filter caches. yading@10: * The mult by 3 in the following is for X[i], X[i-1] and X[i-2] */ yading@10: double x[MAX_CHANNELS * 3]; ///< 3 input samples cache for each channel yading@10: double y[MAX_CHANNELS * 3]; ///< 3 pre-filter samples cache for each channel yading@10: double z[MAX_CHANNELS * 3]; ///< 3 RLB-filter samples cache for each channel yading@10: yading@10: #define I400_BINS (48000 * 4 / 10) yading@10: #define I3000_BINS (48000 * 3) yading@10: struct integrator i400; ///< 400ms integrator, used for Momentary loudness (M), and Integrated loudness (I) yading@10: struct integrator i3000; ///< 3s integrator, used for Short term loudness (S), and Loudness Range (LRA) yading@10: yading@10: /* I and LRA specific */ yading@10: double integrated_loudness; ///< integrated loudness in LUFS (I) yading@10: double loudness_range; ///< loudness range in LU (LRA) yading@10: double lra_low, lra_high; ///< low and high LRA values yading@10: yading@10: /* misc */ yading@10: int loglevel; ///< log level for frame logging yading@10: int metadata; ///< whether or not to inject loudness results in frames yading@10: } EBUR128Context; yading@10: yading@10: #define OFFSET(x) offsetof(EBUR128Context, x) yading@10: #define A AV_OPT_FLAG_AUDIO_PARAM yading@10: #define V AV_OPT_FLAG_VIDEO_PARAM yading@10: #define F AV_OPT_FLAG_FILTERING_PARAM yading@10: static const AVOption ebur128_options[] = { yading@10: { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, V|F }, yading@10: { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, V|F }, yading@10: { "meter", "set scale meter (+9 to +18)", OFFSET(meter), AV_OPT_TYPE_INT, {.i64 = 9}, 9, 18, V|F }, yading@10: { "framelog", "force frame logging level", OFFSET(loglevel), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, A|V|F, "level" }, yading@10: { "info", "information logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_INFO}, INT_MIN, INT_MAX, A|V|F, "level" }, yading@10: { "verbose", "verbose logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_VERBOSE}, INT_MIN, INT_MAX, A|V|F, "level" }, yading@10: { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, A|V|F }, yading@10: { NULL }, yading@10: }; yading@10: yading@10: AVFILTER_DEFINE_CLASS(ebur128); yading@10: yading@10: static const uint8_t graph_colors[] = { yading@10: 0xdd, 0x66, 0x66, // value above 0LU non reached yading@10: 0x66, 0x66, 0xdd, // value below 0LU non reached yading@10: 0x96, 0x33, 0x33, // value above 0LU reached yading@10: 0x33, 0x33, 0x96, // value below 0LU reached yading@10: 0xdd, 0x96, 0x96, // value above 0LU line non reached yading@10: 0x96, 0x96, 0xdd, // value below 0LU line non reached yading@10: 0xdd, 0x33, 0x33, // value above 0LU line reached yading@10: 0x33, 0x33, 0xdd, // value below 0LU line reached yading@10: }; yading@10: yading@10: static const uint8_t *get_graph_color(const EBUR128Context *ebur128, int v, int y) yading@10: { yading@10: const int below0 = y > ebur128->y_zero_lu; yading@10: const int reached = y >= v; yading@10: const int line = ebur128->y_line_ref[y] || y == ebur128->y_zero_lu; yading@10: const int colorid = 4*line + 2*reached + below0; yading@10: return graph_colors + 3*colorid; yading@10: } yading@10: yading@10: static inline int lu_to_y(const EBUR128Context *ebur128, double v) yading@10: { yading@10: v += 2 * ebur128->meter; // make it in range [0;...] yading@10: v = av_clipf(v, 0, ebur128->scale_range); // make sure it's in the graph scale yading@10: v = ebur128->scale_range - v; // invert value (y=0 is on top) yading@10: return v * ebur128->graph.h / ebur128->scale_range; // rescale from scale range to px height yading@10: } yading@10: yading@10: #define FONT8 0 yading@10: #define FONT16 1 yading@10: yading@10: static const uint8_t font_colors[] = { yading@10: 0xdd, 0xdd, 0x00, yading@10: 0x00, 0x96, 0x96, yading@10: }; yading@10: yading@10: static void drawtext(AVFrame *pic, int x, int y, int ftid, const uint8_t *color, const char *fmt, ...) yading@10: { yading@10: int i; yading@10: char buf[128] = {0}; yading@10: const uint8_t *font; yading@10: int font_height; yading@10: va_list vl; yading@10: yading@10: if (ftid == FONT16) font = avpriv_vga16_font, font_height = 16; yading@10: else if (ftid == FONT8) font = avpriv_cga_font, font_height = 8; yading@10: else return; yading@10: yading@10: va_start(vl, fmt); yading@10: vsnprintf(buf, sizeof(buf), fmt, vl); yading@10: va_end(vl); yading@10: yading@10: for (i = 0; buf[i]; i++) { yading@10: int char_y, mask; yading@10: uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*3; yading@10: yading@10: for (char_y = 0; char_y < font_height; char_y++) { yading@10: for (mask = 0x80; mask; mask >>= 1) { yading@10: if (font[buf[i] * font_height + char_y] & mask) yading@10: memcpy(p, color, 3); yading@10: else yading@10: memcpy(p, "\x00\x00\x00", 3); yading@10: p += 3; yading@10: } yading@10: p += pic->linesize[0] - 8*3; yading@10: } yading@10: } yading@10: } yading@10: yading@10: static void drawline(AVFrame *pic, int x, int y, int len, int step) yading@10: { yading@10: int i; yading@10: uint8_t *p = pic->data[0] + y*pic->linesize[0] + x*3; yading@10: yading@10: for (i = 0; i < len; i++) { yading@10: memcpy(p, "\x00\xff\x00", 3); yading@10: p += step; yading@10: } yading@10: } yading@10: yading@10: static int config_video_output(AVFilterLink *outlink) yading@10: { yading@10: int i, x, y; yading@10: uint8_t *p; yading@10: AVFilterContext *ctx = outlink->src; yading@10: EBUR128Context *ebur128 = ctx->priv; yading@10: AVFrame *outpicref; yading@10: yading@10: /* check if there is enough space to represent everything decently */ yading@10: if (ebur128->w < 640 || ebur128->h < 480) { yading@10: av_log(ctx, AV_LOG_ERROR, "Video size %dx%d is too small, " yading@10: "minimum size is 640x480\n", ebur128->w, ebur128->h); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: outlink->w = ebur128->w; yading@10: outlink->h = ebur128->h; yading@10: yading@10: #define PAD 8 yading@10: yading@10: /* configure text area position and size */ yading@10: ebur128->text.x = PAD; yading@10: ebur128->text.y = 40; yading@10: ebur128->text.w = 3 * 8; // 3 characters yading@10: ebur128->text.h = ebur128->h - PAD - ebur128->text.y; yading@10: yading@10: /* configure gauge position and size */ yading@10: ebur128->gauge.w = 20; yading@10: ebur128->gauge.h = ebur128->text.h; yading@10: ebur128->gauge.x = ebur128->w - PAD - ebur128->gauge.w; yading@10: ebur128->gauge.y = ebur128->text.y; yading@10: yading@10: /* configure graph position and size */ yading@10: ebur128->graph.x = ebur128->text.x + ebur128->text.w + PAD; yading@10: ebur128->graph.y = ebur128->gauge.y; yading@10: ebur128->graph.w = ebur128->gauge.x - ebur128->graph.x - PAD; yading@10: ebur128->graph.h = ebur128->gauge.h; yading@10: yading@10: /* graph and gauge share the LU-to-pixel code */ yading@10: av_assert0(ebur128->graph.h == ebur128->gauge.h); yading@10: yading@10: /* prepare the initial picref buffer */ yading@10: av_frame_free(&ebur128->outpicref); yading@10: ebur128->outpicref = outpicref = yading@10: ff_get_video_buffer(outlink, outlink->w, outlink->h); yading@10: if (!outpicref) yading@10: return AVERROR(ENOMEM); yading@10: outlink->sample_aspect_ratio = (AVRational){1,1}; yading@10: yading@10: /* init y references values (to draw LU lines) */ yading@10: ebur128->y_line_ref = av_calloc(ebur128->graph.h + 1, sizeof(*ebur128->y_line_ref)); yading@10: if (!ebur128->y_line_ref) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: /* black background */ yading@10: memset(outpicref->data[0], 0, ebur128->h * outpicref->linesize[0]); yading@10: yading@10: /* draw LU legends */ yading@10: drawtext(outpicref, PAD, PAD+16, FONT8, font_colors+3, " LU"); yading@10: for (i = ebur128->meter; i >= -ebur128->meter * 2; i--) { yading@10: y = lu_to_y(ebur128, i); yading@10: x = PAD + (i < 10 && i > -10) * 8; yading@10: ebur128->y_line_ref[y] = i; yading@10: y -= 4; // -4 to center vertically yading@10: drawtext(outpicref, x, y + ebur128->graph.y, FONT8, font_colors+3, yading@10: "%c%d", i < 0 ? '-' : i > 0 ? '+' : ' ', FFABS(i)); yading@10: } yading@10: yading@10: /* draw graph */ yading@10: ebur128->y_zero_lu = lu_to_y(ebur128, 0); yading@10: p = outpicref->data[0] + ebur128->graph.y * outpicref->linesize[0] yading@10: + ebur128->graph.x * 3; yading@10: for (y = 0; y < ebur128->graph.h; y++) { yading@10: const uint8_t *c = get_graph_color(ebur128, INT_MAX, y); yading@10: yading@10: for (x = 0; x < ebur128->graph.w; x++) yading@10: memcpy(p + x*3, c, 3); yading@10: p += outpicref->linesize[0]; yading@10: } yading@10: yading@10: /* draw fancy rectangles around the graph and the gauge */ yading@10: #define DRAW_RECT(r) do { \ yading@10: drawline(outpicref, r.x, r.y - 1, r.w, 3); \ yading@10: drawline(outpicref, r.x, r.y + r.h, r.w, 3); \ yading@10: drawline(outpicref, r.x - 1, r.y, r.h, outpicref->linesize[0]); \ yading@10: drawline(outpicref, r.x + r.w, r.y, r.h, outpicref->linesize[0]); \ yading@10: } while (0) yading@10: DRAW_RECT(ebur128->graph); yading@10: DRAW_RECT(ebur128->gauge); yading@10: yading@10: outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int config_audio_input(AVFilterLink *inlink) yading@10: { yading@10: AVFilterContext *ctx = inlink->dst; yading@10: EBUR128Context *ebur128 = ctx->priv; yading@10: yading@10: /* force 100ms framing in case of metadata injection: the frames must have yading@10: * a granularity of the window overlap to be accurately exploited */ yading@10: if (ebur128->metadata) yading@10: inlink->min_samples = yading@10: inlink->max_samples = yading@10: inlink->partial_buf_size = inlink->sample_rate / 10; yading@10: return 0; yading@10: } yading@10: yading@10: static int config_audio_output(AVFilterLink *outlink) yading@10: { yading@10: int i; yading@10: int idx_bitposn = 0; yading@10: AVFilterContext *ctx = outlink->src; yading@10: EBUR128Context *ebur128 = ctx->priv; yading@10: const int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout); yading@10: yading@10: #define BACK_MASK (AV_CH_BACK_LEFT |AV_CH_BACK_CENTER |AV_CH_BACK_RIGHT| \ yading@10: AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_CENTER|AV_CH_TOP_BACK_RIGHT| \ yading@10: AV_CH_SIDE_LEFT |AV_CH_SIDE_RIGHT| \ yading@10: AV_CH_SURROUND_DIRECT_LEFT |AV_CH_SURROUND_DIRECT_RIGHT) yading@10: yading@10: ebur128->nb_channels = nb_channels; yading@10: ebur128->ch_weighting = av_calloc(nb_channels, sizeof(*ebur128->ch_weighting)); yading@10: if (!ebur128->ch_weighting) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: for (i = 0; i < nb_channels; i++) { yading@10: yading@10: /* find the next bit that is set starting from the right */ yading@10: while ((outlink->channel_layout & 1ULL<ch_weighting[i] = 0; yading@10: } else if (1ULL<ch_weighting[i] = 1.41; yading@10: } else { yading@10: ebur128->ch_weighting[i] = 1.0; yading@10: } yading@10: yading@10: idx_bitposn++; yading@10: yading@10: if (!ebur128->ch_weighting[i]) yading@10: continue; yading@10: yading@10: /* bins buffer for the two integration window (400ms and 3s) */ yading@10: ebur128->i400.cache[i] = av_calloc(I400_BINS, sizeof(*ebur128->i400.cache[0])); yading@10: ebur128->i3000.cache[i] = av_calloc(I3000_BINS, sizeof(*ebur128->i3000.cache[0])); yading@10: if (!ebur128->i400.cache[i] || !ebur128->i3000.cache[i]) yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: yading@10: outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: #define ENERGY(loudness) (pow(10, ((loudness) + 0.691) / 10.)) yading@10: #define LOUDNESS(energy) (-0.691 + 10 * log10(energy)) yading@10: yading@10: static struct hist_entry *get_histogram(void) yading@10: { yading@10: int i; yading@10: struct hist_entry *h = av_calloc(HIST_SIZE, sizeof(*h)); yading@10: yading@10: if (!h) yading@10: return NULL; yading@10: for (i = 0; i < HIST_SIZE; i++) { yading@10: h[i].loudness = i / (double)HIST_GRAIN + ABS_THRES; yading@10: h[i].energy = ENERGY(h[i].loudness); yading@10: } yading@10: return h; yading@10: } yading@10: yading@10: static av_cold int init(AVFilterContext *ctx) yading@10: { yading@10: EBUR128Context *ebur128 = ctx->priv; yading@10: AVFilterPad pad; yading@10: yading@10: if (ebur128->loglevel != AV_LOG_INFO && yading@10: ebur128->loglevel != AV_LOG_VERBOSE) { yading@10: if (ebur128->do_video || ebur128->metadata) yading@10: ebur128->loglevel = AV_LOG_VERBOSE; yading@10: else yading@10: ebur128->loglevel = AV_LOG_INFO; yading@10: } yading@10: yading@10: // if meter is +9 scale, scale range is from -18 LU to +9 LU (or 3*9) yading@10: // if meter is +18 scale, scale range is from -36 LU to +18 LU (or 3*18) yading@10: ebur128->scale_range = 3 * ebur128->meter; yading@10: yading@10: ebur128->i400.histogram = get_histogram(); yading@10: ebur128->i3000.histogram = get_histogram(); yading@10: if (!ebur128->i400.histogram || !ebur128->i3000.histogram) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: ebur128->integrated_loudness = ABS_THRES; yading@10: ebur128->loudness_range = 0; yading@10: yading@10: /* insert output pads */ yading@10: if (ebur128->do_video) { yading@10: pad = (AVFilterPad){ yading@10: .name = av_strdup("out0"), yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .config_props = config_video_output, yading@10: }; yading@10: if (!pad.name) yading@10: return AVERROR(ENOMEM); yading@10: ff_insert_outpad(ctx, 0, &pad); yading@10: } yading@10: pad = (AVFilterPad){ yading@10: .name = av_asprintf("out%d", ebur128->do_video), yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .config_props = config_audio_output, yading@10: }; yading@10: if (!pad.name) yading@10: return AVERROR(ENOMEM); yading@10: ff_insert_outpad(ctx, ebur128->do_video, &pad); yading@10: yading@10: /* summary */ yading@10: av_log(ctx, AV_LOG_VERBOSE, "EBU +%d scale\n", ebur128->meter); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: #define HIST_POS(power) (int)(((power) - ABS_THRES) * HIST_GRAIN) yading@10: yading@10: /* loudness and power should be set such as loudness = -0.691 + yading@10: * 10*log10(power), we just avoid doing that calculus two times */ yading@10: static int gate_update(struct integrator *integ, double power, yading@10: double loudness, int gate_thres) yading@10: { yading@10: int ipower; yading@10: double relative_threshold; yading@10: int gate_hist_pos; yading@10: yading@10: /* update powers histograms by incrementing current power count */ yading@10: ipower = av_clip(HIST_POS(loudness), 0, HIST_SIZE - 1); yading@10: integ->histogram[ipower].count++; yading@10: yading@10: /* compute relative threshold and get its position in the histogram */ yading@10: integ->sum_kept_powers += power; yading@10: integ->nb_kept_powers++; yading@10: relative_threshold = integ->sum_kept_powers / integ->nb_kept_powers; yading@10: if (!relative_threshold) yading@10: relative_threshold = 1e-12; yading@10: integ->rel_threshold = LOUDNESS(relative_threshold) + gate_thres; yading@10: gate_hist_pos = av_clip(HIST_POS(integ->rel_threshold), 0, HIST_SIZE - 1); yading@10: yading@10: return gate_hist_pos; yading@10: } yading@10: yading@10: static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) yading@10: { yading@10: int i, ch, idx_insample; yading@10: AVFilterContext *ctx = inlink->dst; yading@10: EBUR128Context *ebur128 = ctx->priv; yading@10: const int nb_channels = ebur128->nb_channels; yading@10: const int nb_samples = insamples->nb_samples; yading@10: const double *samples = (double *)insamples->data[0]; yading@10: AVFrame *pic = ebur128->outpicref; yading@10: yading@10: for (idx_insample = 0; idx_insample < nb_samples; idx_insample++) { yading@10: const int bin_id_400 = ebur128->i400.cache_pos; yading@10: const int bin_id_3000 = ebur128->i3000.cache_pos; yading@10: yading@10: #define MOVE_TO_NEXT_CACHED_ENTRY(time) do { \ yading@10: ebur128->i##time.cache_pos++; \ yading@10: if (ebur128->i##time.cache_pos == I##time##_BINS) { \ yading@10: ebur128->i##time.filled = 1; \ yading@10: ebur128->i##time.cache_pos = 0; \ yading@10: } \ yading@10: } while (0) yading@10: yading@10: MOVE_TO_NEXT_CACHED_ENTRY(400); yading@10: MOVE_TO_NEXT_CACHED_ENTRY(3000); yading@10: yading@10: for (ch = 0; ch < nb_channels; ch++) { yading@10: double bin; yading@10: yading@10: ebur128->x[ch * 3] = *samples++; // set X[i] yading@10: yading@10: if (!ebur128->ch_weighting[ch]) yading@10: continue; yading@10: yading@10: /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */ yading@10: #define FILTER(Y, X, name) do { \ yading@10: double *dst = ebur128->Y + ch*3; \ yading@10: double *src = ebur128->X + ch*3; \ yading@10: dst[2] = dst[1]; \ yading@10: dst[1] = dst[0]; \ yading@10: dst[0] = src[0]*name##_B0 + src[1]*name##_B1 + src[2]*name##_B2 \ yading@10: - dst[1]*name##_A1 - dst[2]*name##_A2; \ yading@10: } while (0) yading@10: yading@10: // TODO: merge both filters in one? yading@10: FILTER(y, x, PRE); // apply pre-filter yading@10: ebur128->x[ch * 3 + 2] = ebur128->x[ch * 3 + 1]; yading@10: ebur128->x[ch * 3 + 1] = ebur128->x[ch * 3 ]; yading@10: FILTER(z, y, RLB); // apply RLB-filter yading@10: yading@10: bin = ebur128->z[ch * 3] * ebur128->z[ch * 3]; yading@10: yading@10: /* add the new value, and limit the sum to the cache size (400ms or 3s) yading@10: * by removing the oldest one */ yading@10: ebur128->i400.sum [ch] = ebur128->i400.sum [ch] + bin - ebur128->i400.cache [ch][bin_id_400]; yading@10: ebur128->i3000.sum[ch] = ebur128->i3000.sum[ch] + bin - ebur128->i3000.cache[ch][bin_id_3000]; yading@10: yading@10: /* override old cache entry with the new value */ yading@10: ebur128->i400.cache [ch][bin_id_400 ] = bin; yading@10: ebur128->i3000.cache[ch][bin_id_3000] = bin; yading@10: } yading@10: yading@10: /* For integrated loudness, gating blocks are 400ms long with 75% yading@10: * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms yading@10: * (4800 samples at 48kHz). */ yading@10: if (++ebur128->sample_count == 4800) { yading@10: double loudness_400, loudness_3000; yading@10: double power_400 = 1e-12, power_3000 = 1e-12; yading@10: AVFilterLink *outlink = ctx->outputs[0]; yading@10: const int64_t pts = insamples->pts + yading@10: av_rescale_q(idx_insample, (AVRational){ 1, inlink->sample_rate }, yading@10: outlink->time_base); yading@10: yading@10: ebur128->sample_count = 0; yading@10: yading@10: #define COMPUTE_LOUDNESS(m, time) do { \ yading@10: if (ebur128->i##time.filled) { \ yading@10: /* weighting sum of the last