xine-lib: xine-lib-1.2.13/src/input/multirate_pref.c Source File

xine-lib 1.2.13-20230125hg15249

multirate_pref.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 the xine project
3 *
4 * This file is part of xine, a free video player.
5 *
6 * xine is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * xine is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 */
20
21 typedef struct {
22 uint32_t video_width;
23 uint32_t video_height;
24 uint32_t bitrate;
25 char lang[4];
26} multirate_pref_t;
27
28 static const char * const multirate_video_size_labels[] = {
29 "Audio only", "Small", "SD", "HD", "Full HD", "4K", NULL
30};
31
32 static void multirate_set_video_size (multirate_pref_t *pref, int n) {
33 static const uint32_t w[] = { 0, 360, 720, 1280, 1920, 3840 };
34 static const uint32_t h[] = { 0, 240, 576, 720, 1080, 2160 };
35 if ((n >= 0) && (n < (int)(sizeof (w) / sizeof (w[0])))) {
36 pref->video_width = w[n];
37 pref->video_height = h[n];
38 }
39}
40
41 static void multirate_cb_video_size (void *pref_gen, xine_cfg_entry_t *entry) {
42 multirate_pref_t *pref = (multirate_pref_t *)pref_gen;
43 multirate_set_video_size (pref, entry->num_value);
44}
45
46 static void multirate_set_lang (multirate_pref_t *pref, const char *lang) {
47 if (lang)
48 strlcpy (pref->lang, lang, sizeof (pref->lang));
49}
50
51 static void multirate_cb_lang (void *pref_gen, xine_cfg_entry_t *entry) {
52 multirate_pref_t *pref = (multirate_pref_t *)pref_gen;
53 multirate_set_lang (pref, entry->str_value);
54}
55
56 static void multirate_cb_bitrate (void *pref_gen, xine_cfg_entry_t *entry) {
57 multirate_pref_t *pref = (multirate_pref_t *)pref_gen;
58 pref->bitrate = entry->num_value;
59}
60
61 static void multirate_pref_get (config_values_t *config, multirate_pref_t *pref) {
62 multirate_set_video_size (pref, config->register_enum (config,
63 "media.multirate.preferred_video_size", 3,
64 (char **)multirate_video_size_labels,
65 _("Preferred video size"),
66 _("What size of video to play when there are multiple versions."),
67 10,
68 multirate_cb_video_size,
69 pref));
70 multirate_set_lang (pref, config->register_string (config,
71 "media.multirate.preferred_language", "",
72 _("Preferred language"),
73 _("What language to play when there are multiple versions."),
74 10,
75 multirate_cb_lang,
76 pref));
77 pref->bitrate = config->register_num (config,
78 "media.multirate.preferred_bitrate", 2000000,
79 _("Preferred bitrate"),
80 _("What bitrate to play when there are multiple versions of same size."),
81 10,
82 multirate_cb_bitrate,
83 pref);
84}
85
86 static int multirate_autoselect (multirate_pref_t *pref, multirate_pref_t *list, int list_size) {
87 multirate_pref_t *item;
88 int w, h, n, best_n, best_s, best_b;
89 if (list_size <= 0)
90 return -1;
91 if (list_size == 1)
92 return 0;
93 w = pref->video_width > 0 ? pref->video_width : 1;
94 h = pref->video_height > 0 ? pref->video_height : 1;
95 best_n = 0;
96 best_s = 0x7fffffff;
97 best_b = 0x7fffffff;
98 item = list;
99 for (n = 0; n < list_size; n++) {
100 int dw, dh, s, b;
101 b = item->bitrate - pref->bitrate;
102 b = b < 0 ? -b : b;
103 dw = item->video_width - w;
104 dh = item->video_height - h;
105 dw = dw < 0 ? -dw : dw;
106 dh = dh < 0 ? -dh : dh;
107 s = (dw << 10) / w + (dh << 10) / h;
108 if (s < best_s) {
109 best_b = b;
110 best_n = n;
111 best_s = s;
112 } else if (s == best_s) {
113 if (b < best_b) {
114 best_n = n;
115 best_b = b;
116 }
117 }
118 item++;
119 }
120 return best_n;
121}
122
123 static inline int multirate_audio_autoselect (multirate_pref_t *pref, multirate_pref_t *list, int list_size) {
124 multirate_pref_t *item;
125 int n, best_n, best_b;
126 if (list_size <= 0)
127 return -1;
128 if (list_size == 1)
129 return 0;
130 best_n = -1;
131 best_b = 0x7fffffff;
132 for (item = list, n = 0; n < list_size; item++, n++) {
133 int b;
134 if ((item->video_width | item->video_height))
135 continue;
136 b = item->bitrate - pref->bitrate;
137 b = b < 0 ? -b : b;
138 if (item->lang[0] && pref->lang[0] && strcasecmp (item->lang, pref->lang))
139 b += 0x40000000;
140 if (b < best_b) {
141 best_n = n;
142 best_b = b;
143 }
144 }
145 return best_n;
146}
multirate_cb_bitrate
static void multirate_cb_bitrate(void *pref_gen, xine_cfg_entry_t *entry)
Definition multirate_pref.c:56
multirate_video_size_labels
static const char *const multirate_video_size_labels[]
Definition multirate_pref.c:28
multirate_pref_get
static void multirate_pref_get(config_values_t *config, multirate_pref_t *pref)
Definition multirate_pref.c:61
multirate_cb_video_size
static void multirate_cb_video_size(void *pref_gen, xine_cfg_entry_t *entry)
Definition multirate_pref.c:41
multirate_set_lang
static void multirate_set_lang(multirate_pref_t *pref, const char *lang)
Definition multirate_pref.c:46
multirate_autoselect
static int multirate_autoselect(multirate_pref_t *pref, multirate_pref_t *list, int list_size)
Definition multirate_pref.c:86
multirate_audio_autoselect
static int multirate_audio_autoselect(multirate_pref_t *pref, multirate_pref_t *list, int list_size)
Definition multirate_pref.c:123
multirate_cb_lang
static void multirate_cb_lang(void *pref_gen, xine_cfg_entry_t *entry)
Definition multirate_pref.c:51
multirate_set_video_size
static void multirate_set_video_size(multirate_pref_t *pref, int n)
Definition multirate_pref.c:32
config_values_s
Definition configfile.h:83
config_values_s::register_enum
int(* register_enum)(config_values_t *self, const char *key, int def_value, char **values, const char *description, const char *help, int exp_level, xine_config_cb_t changed_cb, void *cb_data)
Definition configfile.h:139
config_values_s::register_string
char *(* register_string)(config_values_t *self, const char *key, const char *def_value, const char *description, const char *help, int exp_level, xine_config_cb_t changed_cb, void *cb_data)
Definition configfile.h:110
config_values_s::register_num
int(* register_num)(config_values_t *self, const char *key, int def_value, const char *description, const char *help, int exp_level, xine_config_cb_t changed_cb, void *cb_data)
Definition configfile.h:149
multirate_pref_t
Definition multirate_pref.c:21
multirate_pref_t::lang
char lang[4]
Definition multirate_pref.c:25
multirate_pref_t::bitrate
uint32_t bitrate
Definition multirate_pref.c:24
multirate_pref_t::video_width
uint32_t video_width
Definition multirate_pref.c:22
multirate_pref_t::video_height
uint32_t video_height
Definition multirate_pref.c:23
xine_cfg_entry_s
Definition xine.h:1649
xine_cfg_entry_s::str_value
char * str_value
Definition xine.h:1663
xine_cfg_entry_s::num_value
int num_value
Definition xine.h:1670
_
#define _(String)
Definition vcdplayer.h:39
NULL
NULL
Definition xine_plugin.c:78