demux: mp4: set bitmap mask when possible
[vlc.git] / src / input / stats.c
blob92c2bb66e3a2ef7f28054e7da64bdcadae0acb83
1 /*****************************************************************************
2 * stats.c: Statistics handling
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <stdlib.h>
29 #include <string.h>
31 #include <vlc_common.h>
32 #include "input/input_internal.h"
34 /**
35 * Create a statistics counter
37 static void input_rate_Init(input_rate_t *rate)
39 vlc_mutex_init(&rate->lock);
40 rate->updates = 0;
41 rate->value = 0;
42 rate->samples[0].date = VLC_TICK_INVALID;
43 rate->samples[1].date = VLC_TICK_INVALID;
46 static float stats_GetRate(const input_rate_t *rate)
48 if (rate->samples[1].date == VLC_TICK_INVALID)
49 return 0.;
51 return (rate->samples[0].value - rate->samples[1].value)
52 / (float)(rate->samples[0].date - rate->samples[1].date);
55 struct input_stats *input_stats_Create(void)
57 struct input_stats *stats = malloc(sizeof (*stats));
58 if (unlikely(stats == NULL))
59 return NULL;
61 input_rate_Init(&stats->input_bitrate);
62 input_rate_Init(&stats->demux_bitrate);
63 atomic_init(&stats->demux_corrupted, 0);
64 atomic_init(&stats->demux_discontinuity, 0);
65 atomic_init(&stats->decoded_audio, 0);
66 atomic_init(&stats->decoded_video, 0);
67 atomic_init(&stats->played_abuffers, 0);
68 atomic_init(&stats->lost_abuffers, 0);
69 atomic_init(&stats->displayed_pictures, 0);
70 atomic_init(&stats->lost_pictures, 0);
71 return stats;
74 void input_stats_Destroy(struct input_stats *stats)
76 vlc_mutex_destroy(&stats->demux_bitrate.lock);
77 vlc_mutex_destroy(&stats->input_bitrate.lock);
78 free(stats);
81 void input_stats_Compute(struct input_stats *stats, input_stats_t *st)
83 /* Input */
84 vlc_mutex_lock(&stats->input_bitrate.lock);
85 st->i_read_packets = stats->input_bitrate.updates;
86 st->i_read_bytes = stats->input_bitrate.value;
87 st->f_input_bitrate = stats_GetRate(&stats->input_bitrate);
88 vlc_mutex_unlock(&stats->input_bitrate.lock);
90 vlc_mutex_lock(&stats->demux_bitrate.lock);
91 st->i_demux_read_bytes = stats->demux_bitrate.value;
92 st->f_demux_bitrate = stats_GetRate(&stats->demux_bitrate);
93 vlc_mutex_unlock(&stats->demux_bitrate.lock);
94 st->i_demux_corrupted = atomic_load_explicit(&stats->demux_corrupted,
95 memory_order_relaxed);
96 st->i_demux_discontinuity = atomic_load_explicit(
97 &stats->demux_discontinuity, memory_order_relaxed);
99 /* Aout */
100 st->i_decoded_audio = atomic_load_explicit(&stats->decoded_audio,
101 memory_order_relaxed);
102 st->i_played_abuffers = atomic_load_explicit(&stats->played_abuffers,
103 memory_order_relaxed);
104 st->i_lost_abuffers = atomic_load_explicit(&stats->lost_abuffers,
105 memory_order_relaxed);
107 /* Vouts */
108 st->i_decoded_video = atomic_load_explicit(&stats->decoded_video,
109 memory_order_relaxed);
110 st->i_displayed_pictures = atomic_load_explicit(&stats->displayed_pictures,
111 memory_order_relaxed);
112 st->i_lost_pictures = atomic_load_explicit(&stats->lost_pictures,
113 memory_order_relaxed);
116 /** Update a counter element with new values
117 * \param p_counter the counter to update
118 * \param val the vlc_value union containing the new value to aggregate. For
119 * more information on how data is aggregated, \see stats_Create
121 void input_rate_Add(input_rate_t *counter, uintmax_t val)
123 counter->updates++;
124 counter->value += val;
126 /* Ignore samples within a second of another */
127 vlc_tick_t now = vlc_tick_now();
128 if (counter->samples[0].date != VLC_TICK_INVALID
129 && (now - counter->samples[0].date) < VLC_TICK_FROM_SEC(1))
130 return;
132 memcpy(counter->samples + 1, counter->samples,
133 sizeof (counter->samples[0]));
135 counter->samples[0].value = counter->value;
136 counter->samples[0].date = now;