1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
* ratiocodes.c -- database for all ratio/codes (asr, sar, dar, frc...)
* used in transcode
* (C) 2005-2010 - Francesco Romani <fromani -at- gmail -dot- com>
*
* This file is part of transcode, a video stream processing tool.
*
* transcode is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* transcode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "libtc.h"
#include "ratiocodes.h"
#include <stdlib.h>
#include <math.h>
#define TABLE_LEN(tab) (sizeof((tab))/sizeof((tab)[0]))
/* WARNING: this table MUST BE in frc order */
static const double frc_table[16] = {
0,
(24000.0/1001.0),
24,
25,
(30000.0/1001.0),
30,
50,
(2*(30000.0/1001.0)),
60,
1,
5,
10,
12,
15,
0,
0
};
/* WARNING: this table MUST BE in asr order */
static const double asr_table[8] = {
0.0,
1.0,
(4.0/3.0),
(16.0/9.0),
(221.0/100.0),
0.0,
0.0,
0.0,
};
/* WARNING: this table MUST BE in frc order */
static const TCPair frc_ratios[16] = {
{ 0, 0 },
{ 24000, 1001 },
{ 24000, 1000 },
{ 25000, 1000 },
{ 30000, 1001 },
{ 30000, 1000 },
{ 50000, 1000 },
{ 60000, 1001 },
{ 60000, 1000 },
/* XXX */
{ 1000, 1000 },
{ 5000, 1000 },
{ 10000, 1000 },
{ 12000, 1000 },
{ 15000, 1000 },
/* XXX */
{ 0, 0 },
{ 0, 0 },
};
/* WARNING: this table MUST BE in asr order */
static const TCPair asr_ratios[8] = {
{ 0, 0 },
{ 1, 1 },
{ 4, 3 },
{ 16, 9 },
{ 221, 100 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
/*
* XXX: import/tcprobe.c also claims that
* asr == 8 and asr == 12 are 4:3.
* Need further investigation.
*/
};
static const TCPair par_ratios[8] = {
{ 1, 1 },
{ 1, 1 },
{ 1200, 1100 },
{ 1000, 1100 },
{ 1600, 1100 },
{ 4000, 3300 },
{ 1, 1 },
{ 1, 1 }
};
/*************************************************************************/
const char *tc_asr_code_describe(int asr_code)
{
switch (asr_code) {
case 1:
return "encoded @ 1:1";
case 2:
return "encoded @ 4:3";
case 3:
return "encoded @ 16:9";
case 4:
return "encoded @ 2.21:1";
case 8:
return "encoded @ 4:3";
case 12:
return "encoded @ 4:3";
}
return "encoded @ UNKNOWN";
}
#define DELTA 0.0005
static int tc_guess_code_from_value(const double *pairs, size_t len,
int *code, double val)
{
int idx = TC_NULL_MATCH, i = 0;
double mindiff = DELTA;
for (i = 0; i < len; i++) {
double diff = fabs(pairs[i] - val);
if (diff < mindiff) {
mindiff = diff;
idx = i;
}
}
if (code != NULL && idx != TC_NULL_MATCH) {
*code = idx;
}
return idx;
}
#undef DELTA
int tc_asr_code_from_value(int *asr_code, double ratio)
{
return tc_guess_code_from_value(asr_table, TABLE_LEN(frc_table),
asr_code, ratio);
}
int tc_frc_code_from_value(int *frc_code, double fps)
{
return tc_guess_code_from_value(frc_table, TABLE_LEN(frc_table),
frc_code, fps);
}
int tc_frc_code_to_value(int frc_code, double *fps)
{
if ((fps != NULL && frc_code >= 0)
&& frc_code <= TABLE_LEN(frc_table)) {
*fps = frc_table[frc_code];
return 0;
}
return TC_NULL_MATCH;
}
/*
* match_ratio:
* helper for various detection functions. Scans a ratio
* table (that MUST be in frc order) looking for corrispondences
* between a ratio and a ratio code.
*
* Parameters:
* pairs: pointer to an array of TCPair to scan
* len: number of pairs to consider
* n: numerator of ratio to look for. Use TC_NULL_MATCH
* if this function must look for a corrispondency of code
* and not for a corrispondency of ratio.
* d: denominator of ratio to look for. Use TC_NULL_MATCH
* if this function must look for a corrispondency of code
* and not for a corrispondency of ratio.
* code: code of ratio to look for. Use TC_NULL_MATCH if this function
* must look for a corrispondency of ratio.
* Return Value:
* TC_NULL_MATCH if input parameter(s) isn't known.
* >= 0 index in table of given corrispondency.
* Precondintions:
* given pairs table MUST BE in code (frc, asr) order.
* pairs != NULL.
*/
static int match_ratio(const TCPair *pairs, size_t len,
int n, int d, int code)
{
int i = 0, r = TC_NULL_MATCH;
for (i = 0; i < len; i++) {
if (i == code) {
r = i;
break;
}
if (n == pairs[i].a && d == pairs[i].b) {
r = i;
break;
}
}
return r;
}
static int select_table(TCRatioCode rc, const TCPair **table, size_t *len)
{
int ret = 0;
switch (rc) {
case TC_FRC_CODE:
*table = frc_ratios;
*len = TABLE_LEN(frc_ratios);
break;
case TC_ASR_CODE:
*table = asr_ratios;
*len = TABLE_LEN(asr_ratios);
break;
case TC_PAR_CODE:
*table = par_ratios;
*len = TABLE_LEN(par_ratios);
break;
default:
*table = NULL;
*len = 0;
ret = TC_NULL_MATCH;
}
return ret;
}
int tc_code_from_ratio(TCRatioCode rc, int *out_code, int in_n, int in_d)
{
int code = TC_NULL_MATCH;
const TCPair *table = NULL;
size_t len = 0;
select_table(rc, &table, &len);
if (table != NULL) {
code = match_ratio(table, len, in_n, in_d, TC_NULL_MATCH);
if (out_code != NULL && code != TC_NULL_MATCH) {
*out_code = code;
}
}
return code;
}
int tc_code_to_ratio(TCRatioCode rc, int in_code, int *out_n, int *out_d)
{
int code = TC_NULL_MATCH;
const TCPair *table = NULL;
size_t len = 0;
select_table(rc, &table, &len);
if (table != NULL) {
code = match_ratio(table, len,
TC_NULL_MATCH, TC_NULL_MATCH, in_code);
if ((out_n != NULL && out_d != NULL) && code != TC_NULL_MATCH) {
*out_n = table[code].a;
*out_d = table[code].b;
}
}
return code;
}
/*************************************************************************/
/*
* Local variables:
* c-file-style: "stroustrup"
* c-file-offsets: ((case-label . *) (statement-case-intro . *))
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/
|