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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/*
* libzvbi -- Raw VBI sampling parameters
*
* Copyright (C) 2000-2004 Michael H. Schimek
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
/* $Id: sampling_par.c,v 1.12 2013-08-28 14:45:00 mschimek Exp $ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <errno.h>
#include "misc.h"
#include "raw_decoder.h"
#include "sampling_par.h"
#include "sliced.h"
# define vbi_pixfmt_bytes_per_pixel VBI_PIXFMT_BPP
# define sp_sample_format sampling_format
/**
* @addtogroup Sampling Raw VBI sampling
* @ingroup Raw
* @brief Raw VBI data sampling interface.
*/
/**
* @internal
* Compatibility.
*/
vbi_videostd_set
_vbi_videostd_set_from_scanning (int scanning)
{
switch (scanning) {
case 525:
return VBI_VIDEOSTD_SET_525_60;
case 625:
return VBI_VIDEOSTD_SET_625_50;
default:
break;
}
return 0;
}
_vbi_inline vbi_bool
range_check (unsigned int start,
unsigned int count, unsigned int min, unsigned int max)
{
/* Check bounds and overflow. */
return (start >= min && (start + count) <= max && (start + count) >= start);
}
/**
* @internal
* @param sp Sampling parameters to verify.
*
* @return
* TRUE if the sampling parameters are valid (as far as we can tell).
*/
vbi_bool
_vbi_sampling_par_valid_log (const vbi_sampling_par * sp, _vbi_log_hook * log)
{
vbi_videostd_set videostd_set;
unsigned int bpp;
assert (NULL != sp);
switch (sp->sp_sample_format) {
case VBI_PIXFMT_YUV420:
/* This conflicts with the ivtv driver, which returns an
odd number of bytes per line. The driver format is
_GREY but libzvbi 0.2 has no VBI_PIXFMT_Y8. */
break;
default:
bpp = vbi_pixfmt_bytes_per_pixel (sp->sp_sample_format);
if (0 != (sp->bytes_per_line % bpp))
goto bad_samples;
break;
}
if (0 == sp->bytes_per_line)
goto no_samples;
if (0 == sp->count[0]
&& 0 == sp->count[1])
goto bad_range;
videostd_set = _vbi_videostd_set_from_scanning (sp->scanning);
if (VBI_VIDEOSTD_SET_525_60 & videostd_set) {
if (VBI_VIDEOSTD_SET_625_50 & videostd_set)
goto ambiguous;
if (0 != sp->start[0]
&& !range_check (sp->start[0], sp->count[0], 1, 262))
goto bad_range;
if (0 != sp->start[1]
&& !range_check (sp->start[1], sp->count[1], 263, 525))
goto bad_range;
} else if (VBI_VIDEOSTD_SET_625_50 & videostd_set) {
if (0 != sp->start[0]
&& !range_check (sp->start[0], sp->count[0], 1, 311))
goto bad_range;
if (0 != sp->start[1]
&& !range_check (sp->start[1], sp->count[1], 312, 625))
goto bad_range;
} else {
ambiguous:
info (log, "Ambiguous videostd_set 0x%lx.", (unsigned long) videostd_set);
return FALSE;
}
if (sp->interlaced && (sp->count[0] != sp->count[1]
|| 0 == sp->count[0])) {
info (log,
"Line counts %u, %u must be equal and "
"non-zero when raw VBI data is interlaced.",
sp->count[0], sp->count[1]);
return FALSE;
}
return TRUE;
no_samples:
info (log, "samples_per_line is zero.");
return FALSE;
bad_samples:
info (log,
"bytes_per_line value %u is no multiple of "
"the sample size %u.",
sp->bytes_per_line, vbi_pixfmt_bytes_per_pixel (sp->sp_sample_format));
return FALSE;
bad_range:
info (log,
"Invalid VBI scan range %u-%u (%u lines), "
"%u-%u (%u lines).",
sp->start[0], sp->start[0] + sp->count[0] - 1,
sp->count[0],
sp->start[1], sp->start[1] + sp->count[1] - 1, sp->count[1]);
return FALSE;
}
static vbi_bool
_vbi_sampling_par_permit_service
(const vbi_sampling_par * sp,
const _vbi_service_par * par, unsigned int strict, _vbi_log_hook * log)
{
const unsigned int unknown = 0;
double signal;
unsigned int field;
unsigned int samples_per_line;
vbi_videostd_set videostd_set;
assert (NULL != sp);
assert (NULL != par);
videostd_set = _vbi_videostd_set_from_scanning (sp->scanning);
if (0 == (par->videostd_set & videostd_set)) {
info (log,
"Service 0x%08x (%s) requires "
"videostd_set 0x%lx, "
"have 0x%lx.",
par->id, par->label,
(unsigned long) par->videostd_set, (unsigned long) videostd_set);
return FALSE;
}
if (par->flags & _VBI_SP_LINE_NUM) {
if ((par->first[0] > 0 && unknown == (unsigned int) sp->start[0])
|| (par->first[1] > 0 && unknown == (unsigned int) sp->start[1])) {
info (log,
"Service 0x%08x (%s) requires known "
"line numbers.", par->id, par->label);
return FALSE;
}
}
{
unsigned int rate;
rate = MAX (par->cri_rate, par->bit_rate);
switch (par->id) {
case VBI_SLICED_WSS_625:
/* Effective bit rate is just 1/3 max_rate,
so 1 * max_rate should suffice. */
break;
default:
rate = (rate * 3) >> 1;
break;
}
if (rate > (unsigned int) sp->sampling_rate) {
info (log,
"Sampling rate %f MHz too low "
"for service 0x%08x (%s).",
sp->sampling_rate / 1e6, par->id, par->label);
return FALSE;
}
}
signal = par->cri_bits / (double) par->cri_rate
+ (par->frc_bits + par->payload) / (double) par->bit_rate;
samples_per_line = sp->bytes_per_line / VBI_PIXFMT_BPP (sp->sampling_format);
if (0 && sp->offset > 0 && strict > 0) {
double sampling_rate;
double offset;
double end;
sampling_rate = (double) sp->sampling_rate;
offset = sp->offset / sampling_rate;
end = (sp->offset + samples_per_line) / sampling_rate;
if (offset > (par->offset / 1e3 - 0.5e-6)) {
info (log,
"Sampling starts at 0H + %f us, too "
"late for service 0x%08x (%s) at "
"%f us.", offset * 1e6, par->id, par->label, par->offset / 1e3);
return FALSE;
}
if (end < (par->offset / 1e9 + signal + 0.5e-6)) {
info (log,
"Sampling ends too early at 0H + "
"%f us for service 0x%08x (%s) "
"which ends at %f us",
end * 1e6,
par->id, par->label, par->offset / 1e3 + signal * 1e6 + 0.5);
return FALSE;
}
} else {
double samples;
samples = samples_per_line / (double) sp->sampling_rate;
if (strict > 0)
samples -= 1e-6; /* headroom */
if (samples < signal) {
info (log,
"Service 0x%08x (%s) signal length "
"%f us exceeds %f us sampling length.",
par->id, par->label, signal * 1e6, samples * 1e6);
return FALSE;
}
}
if ((par->flags & _VBI_SP_FIELD_NUM)
&& !sp->synchronous) {
info (log,
"Service 0x%08x (%s) requires "
"synchronous field order.", par->id, par->label);
return FALSE;
}
for (field = 0; field < 2; ++field) {
unsigned int start;
unsigned int end;
start = sp->start[field];
end = start + sp->count[field] - 1;
if (0 == par->first[field]
|| 0 == par->last[field]) {
/* No data on this field. */
continue;
}
if (0 == sp->count[field]) {
info (log,
"Service 0x%08x (%s) requires "
"data from field %u", par->id, par->label, field + 1);
return FALSE;
}
/* (int) <= 0 for compatibility with libzvbi 0.2.x */
if ((int) strict <= 0 || 0 == sp->start[field])
continue;
if (1 == strict && par->first[field] > par->last[field]) {
/* May succeed if not all scanning lines
available for the service are actually used. */
continue;
}
if (start > par->first[field]
|| end < par->last[field]) {
info (log,
"Service 0x%08x (%s) requires "
"lines %u-%u, have %u-%u.",
par->id, par->label, par->first[field], par->last[field], start, end);
return FALSE;
}
}
return TRUE;
}
/**
* @internal
*/
vbi_service_set
_vbi_sampling_par_check_services_log
(const vbi_sampling_par * sp,
vbi_service_set services, unsigned int strict, _vbi_log_hook * log) {
const _vbi_service_par *par;
vbi_service_set rservices;
assert (NULL != sp);
rservices = 0;
for (par = _vbi_service_table; par->id; ++par) {
if (0 == (par->id & services))
continue;
if (_vbi_sampling_par_permit_service (sp, par, strict, log))
rservices |= par->id;
}
return rservices;
}
/**
* @internal
*/
vbi_service_set
_vbi_sampling_par_from_services_log
(vbi_sampling_par * sp,
unsigned int *max_rate,
vbi_videostd_set videostd_set_req,
vbi_service_set services, _vbi_log_hook * log) {
const _vbi_service_par *par;
vbi_service_set rservices;
vbi_videostd_set videostd_set;
unsigned int rate;
unsigned int samples_per_line;
assert (NULL != sp);
videostd_set = 0;
if (0 != videostd_set_req) {
if (0 == (VBI_VIDEOSTD_SET_ALL & videostd_set_req)
|| ((VBI_VIDEOSTD_SET_525_60 & videostd_set_req)
&& (VBI_VIDEOSTD_SET_625_50 & videostd_set_req))) {
warning (log,
"Ambiguous videostd_set 0x%lx.", (unsigned long) videostd_set_req);
CLEAR (*sp);
return 0;
}
videostd_set = videostd_set_req;
}
samples_per_line = 0;
sp->sampling_rate = 27000000; /* ITU-R BT.601 */
sp->offset = (int) (64e-6 * sp->sampling_rate);
sp->start[0] = 30000;
sp->count[0] = 0;
sp->start[1] = 30000;
sp->count[1] = 0;
sp->interlaced = FALSE;
sp->synchronous = TRUE;
rservices = 0;
rate = 0;
for (par = _vbi_service_table; par->id; ++par) {
#if 0 /* Set but unused */
double margin;
#endif
double signal;
int offset;
unsigned int samples;
unsigned int i;
if (0 == (par->id & services))
continue;
if (0 == videostd_set_req) {
vbi_videostd_set set;
set = par->videostd_set | videostd_set;
if (0 == (set & ~VBI_VIDEOSTD_SET_525_60)
|| 0 == (set & ~VBI_VIDEOSTD_SET_625_50))
videostd_set |= par->videostd_set;
}
#if 0 /* Set but unused */
if (VBI_VIDEOSTD_SET_525_60 & videostd_set)
margin = 1.0e-6;
else
margin = 2.0e-6;
#endif
if (0 == (par->videostd_set & videostd_set)) {
info (log,
"Service 0x%08x (%s) requires "
"videostd_set 0x%lx, "
"have 0x%lx.",
par->id, par->label,
(unsigned long) par->videostd_set, (unsigned long) videostd_set);
continue;
}
rate = MAX (rate, par->cri_rate);
rate = MAX (rate, par->bit_rate);
signal = par->cri_bits / (double) par->cri_rate
+ ((par->frc_bits + par->payload) / (double) par->bit_rate);
offset = (int) ((par->offset / 1e9) * sp->sampling_rate);
samples = (int) ((signal + 1.0e-6) * sp->sampling_rate);
sp->offset = MIN (sp->offset, offset);
samples_per_line = MAX (samples_per_line + sp->offset,
samples + offset) - sp->offset;
for (i = 0; i < 2; ++i)
if (par->first[i] > 0 && par->last[i] > 0) {
sp->start[i] = MIN
((unsigned int) sp->start[i], (unsigned int) par->first[i]);
sp->count[i] = MAX ((unsigned int) sp->start[i]
+ sp->count[i], (unsigned int) par->last[i] + 1)
- sp->start[i];
}
rservices |= par->id;
}
if (0 == rservices) {
CLEAR (*sp);
return 0;
}
if (0 == sp->count[1]) {
sp->start[1] = 0;
if (0 == sp->count[0]) {
sp->start[0] = 0;
sp->offset = 0;
}
} else if (0 == sp->count[0]) {
sp->start[0] = 0;
}
sp->scanning = (videostd_set & VBI_VIDEOSTD_SET_525_60)
? 525 : 625;
sp->sp_sample_format = VBI_PIXFMT_YUV420;
/* Note bpp is 1. */
sp->bytes_per_line = MAX (1440U, samples_per_line);
if (max_rate)
*max_rate = rate;
return rservices;
}
/**
* @param sp Sampling parameters to check against.
* @param services Set of data services.
* @param strict See description of vbi_raw_decoder_add_services().
*
* Check which of the given services can be decoded with the given
* sampling parameters at the given strictness level.
*
* @return
* Subset of @a services decodable with the given sampling parameters.
*/
vbi_service_set
vbi_sampling_par_check_services
(const vbi_sampling_par * sp,
vbi_service_set services, unsigned int strict) {
return _vbi_sampling_par_check_services_log (sp, services, strict,
/* log_hook */ NULL);
}
/**
* @param sp Sampling parameters calculated by this function
* will be stored here.
* @param max_rate If not NULL, the highest data bit rate in Hz of
* all services requested will be stored here. The sampling rate
* should be at least twice as high; @sp sampling_rate will
* be set to a more reasonable value of 27 MHz, which is twice
* the video sampling rate defined by ITU-R Rec. BT.601.
* @param videostd_set Create sampling parameters matching these
* video standards. When 0 determine video standard from requested
* services.
* @param services Set of VBI_SLICED_ symbols. Here (and only here) you
* can add @c VBI_SLICED_VBI_625 or @c VBI_SLICED_VBI_525 to include all
* vbi scan lines in the calculated sampling parameters.
*
* Calculate the sampling parameters required to receive and decode the
* requested data @a services. The @a sp sampling_format will be
* @c VBI_PIXFMT_Y8, offset and bytes_per_line will be set to
* reasonable minimums. This function can be used to initialize hardware
* prior to creating a vbi_raw_decoder object.
*
* @return
* Subset of @a services covered by the calculated sampling parameters.
*/
vbi_service_set
vbi_sampling_par_from_services (vbi_sampling_par * sp,
unsigned int *max_rate,
vbi_videostd_set videostd_set, vbi_service_set services)
{
return _vbi_sampling_par_from_services_log (sp, max_rate,
videostd_set, services,
/* log_hook */ NULL);
}
/*
Local variables:
c-set-style: K&R
c-basic-offset: 8
End:
*/
|