hlsdemux: Enable support for external subtitles
[platform/upstream/gstreamer.git] / ext / closedcaption / decoder.c
1 /*
2  *  libzvbi -- Old raw VBI decoder
3  *
4  *  Copyright (C) 2000, 2001, 2002 Michael H. Schimek
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, write to the 
18  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
19  *  Boston, MA  02110-1301  USA.
20  */
21
22 /* $Id: decoder.c,v 1.25 2008-02-19 00:35:15 mschimek Exp $ */
23
24 /* Note this code is only retained for compatibility with older versions
25    of libzvbi. vbi_raw_decoder is now just a wrapper for the new raw
26    decoder (raw_decoder.c) and bit slicer (bit_slicer.c). We'll drop
27    the old API in libzvbi 0.3. Other modules (e.g. io-v4l2k.c) should
28    already use the new raw VBI decoder directly. */
29
30 #ifdef HAVE_CONFIG_H
31 #  include "config.h"
32 #endif
33
34 #include <glib.h>
35
36 #include "misc.h"
37 #include "decoder.h"
38 #include "raw_decoder.h"
39
40 /**
41  * @addtogroup Rawdec Raw VBI decoder
42  * @ingroup Raw
43  * @brief Converting raw VBI samples to bits and bytes.
44  *
45  * The libzvbi already offers hardware interfaces to obtain sliced
46  * VBI data for further processing. However if you want to write your own
47  * interface or decode data services not covered by libzvbi you can use
48  * these lower level functions.
49  */
50
51 #if 0                           /* LEGACY BIT SLICER */
52 /*
53  *  Bit Slicer
54  */
55
56 #define OVERSAMPLING 4          /* 1, 2, 4, 8 */
57 #define THRESH_FRAC 9
58
59 /*
60  * Note this is just a template. The code is inlined,
61  * with bpp and endian being const.
62  *
63  * This function translates from the image format to
64  * plain bytes, with linear interpolation of samples.
65  * Could be further improved with a lowpass filter.
66  */
67 static inline unsigned int
68 sample (uint8_t * raw, int offs, int bpp, int endian)
69 {
70   unsigned char frac = offs;
71   int raw0, raw1;
72
73   switch (bpp) {
74     case 14:                   /* 1:5:5:5 LE/BE */
75       raw += (offs >> 8) * 2;
76       raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & 0x07C0;
77       raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & 0x07C0;
78       return (raw1 - raw0) * frac + (raw0 << 8);
79
80     case 15:                   /* 5:5:5:1 LE/BE */
81       raw += (offs >> 8) * 2;
82       raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & 0x03E0;
83       raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & 0x03E0;
84       return (raw1 - raw0) * frac + (raw0 << 8);
85
86     case 16:                   /* 5:6:5 LE/BE */
87       raw += (offs >> 8) * 2;
88       raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & 0x07E0;
89       raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & 0x07E0;
90       return (raw1 - raw0) * frac + (raw0 << 8);
91
92     default:                   /* 8 (intermediate bytes skipped by caller) */
93       raw += (offs >> 8) * bpp;
94       return (raw[bpp] - raw[0]) * frac + (raw[0] << 8);
95   }
96 }
97
98 /*
99  * Note this is just a template. The code is inlined,
100  * with bpp being const.
101  */
102 static inline vbi_bool
103 bit_slicer_tmpl (vbi_bit_slicer * d, uint8_t * raw,
104     uint8_t * buf, int bpp, int endian)
105 {
106   unsigned int i, j, k;
107   unsigned int cl = 0, thresh0 = d->thresh, tr;
108   unsigned int c = 0, t;
109   unsigned char b, b1 = 0;
110   int raw0, raw1, mask;
111
112   raw += d->skip;
113
114   if (bpp == 14)
115     mask = 0x07C0;
116   else if (bpp == 15)
117     mask = 0x03E0;
118   else if (bpp == 16)
119     mask = 0x07E0;
120
121   for (i = d->cri_bytes; i > 0; raw += (bpp >= 14 && bpp <= 16) ? 2 : bpp, i--) {
122     if (bpp >= 14 && bpp <= 16) {
123       raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & mask;
124       raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & mask;
125       tr = d->thresh >> THRESH_FRAC;
126       d->thresh += ((raw0 - tr) * (int) ABS (raw1 - raw0)) >>
127           ((bpp == 15) ? 2 : 3);
128       t = raw0 * OVERSAMPLING;
129     } else {
130       tr = d->thresh >> THRESH_FRAC;
131       d->thresh += ((int) raw[0] - tr) * (int) ABS (raw[bpp] - raw[0]);
132       t = raw[0] * OVERSAMPLING;
133     }
134
135     for (j = OVERSAMPLING; j > 0; j--) {
136       b = ((t + (OVERSAMPLING / 2)) / OVERSAMPLING >= tr);
137
138       if (b ^ b1) {
139         cl = d->oversampling_rate >> 1;
140       } else {
141         cl += d->cri_rate;
142
143         if (cl >= (unsigned int) d->oversampling_rate) {
144           cl -= d->oversampling_rate;
145
146           c = c * 2 + b;
147
148           if ((c & d->cri_mask) == d->cri) {
149             i = d->phase_shift;
150             tr *= 256;
151             c = 0;
152
153             for (j = d->frc_bits; j > 0; j--) {
154               c = c * 2 + (sample (raw, i, bpp, endian) >= tr);
155               i += d->step;
156             }
157
158             if (c ^= d->frc)
159               return FALSE;
160
161             /* CRI/FRC found, now get the
162                payload and exit */
163
164             switch (d->endian) {
165               case 3:
166                 for (j = 0; j < (unsigned int) d->payload; j++) {
167                   c >>= 1;
168                   c += (sample (raw, i, bpp, endian) >= tr) << 7;
169                   i += d->step;
170
171                   if ((j & 7) == 7)
172                     *buf++ = c;
173                 }
174
175                 *buf = c >> ((8 - d->payload) & 7);
176                 break;
177
178               case 2:
179                 for (j = 0; j < (unsigned int) d->payload; j++) {
180                   c = c * 2 + (sample (raw, i, bpp, endian) >= tr);
181                   i += d->step;
182
183                   if ((j & 7) == 7)
184                     *buf++ = c;
185                 }
186
187                 *buf = c & ((1 << (d->payload & 7)) - 1);
188                 break;
189
190               case 1:
191                 for (j = d->payload; j > 0; j--) {
192                   for (k = 0; k < 8; k++) {
193                     c >>= 1;
194                     c += (sample (raw, i, bpp, endian) >= tr) << 7;
195                     i += d->step;
196                   }
197
198                   *buf++ = c;
199                 }
200
201                 break;
202
203               case 0:
204                 for (j = d->payload; j > 0; j--) {
205                   for (k = 0; k < 8; k++) {
206                     c = c * 2 + (sample (raw, i, bpp, endian) >= tr);
207                     i += d->step;
208                   }
209
210                   *buf++ = c;
211                 }
212
213                 break;
214             }
215
216             return TRUE;
217           }
218         }
219       }
220
221       b1 = b;
222
223       if (OVERSAMPLING > 1) {
224         if (bpp >= 14 && bpp <= 16) {
225           t += raw1;
226           t -= raw0;
227         } else {
228           t += raw[bpp];
229           t -= raw[0];
230         }
231       }
232     }
233   }
234
235   d->thresh = thresh0;
236
237   return FALSE;
238 }
239
240 static vbi_bool
241 bit_slicer_1 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
242 {
243   return bit_slicer_tmpl (d, raw, buf, 1, 0);
244 }
245
246 static vbi_bool
247 bit_slicer_2 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
248 {
249   return bit_slicer_tmpl (d, raw, buf, 2, 0);
250 }
251
252 static vbi_bool
253 bit_slicer_3 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
254 {
255   return bit_slicer_tmpl (d, raw, buf, 3, 0);
256 }
257
258 static vbi_bool
259 bit_slicer_4 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
260 {
261   return bit_slicer_tmpl (d, raw, buf, 4, 0);
262 }
263
264 static vbi_bool
265 bit_slicer_1555_le (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
266 {
267   return bit_slicer_tmpl (d, raw, buf, 14, 0);
268 }
269
270 static vbi_bool
271 bit_slicer_5551_le (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
272 {
273   return bit_slicer_tmpl (d, raw, buf, 15, 0);
274 }
275
276 static vbi_bool
277 bit_slicer_565_le (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
278 {
279   return bit_slicer_tmpl (d, raw, buf, 16, 0);
280 }
281
282 static vbi_bool
283 bit_slicer_1555_be (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
284 {
285   return bit_slicer_tmpl (d, raw, buf, 14, 1);
286 }
287
288 static vbi_bool
289 bit_slicer_5551_be (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
290 {
291   return bit_slicer_tmpl (d, raw, buf, 15, 1);
292 }
293
294 static vbi_bool
295 bit_slicer_565_be (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
296 {
297   return bit_slicer_tmpl (d, raw, buf, 16, 1);
298 }
299
300 /**
301  * @param slicer Pointer to vbi_bit_slicer object to be initialized. 
302  * @param raw_samples Number of samples or pixels in one raw vbi line
303  *   later passed to vbi_bit_slice(). This limits the number of
304  *   bytes read from the sample buffer.
305  * @param sampling_rate Raw vbi sampling rate in Hz, that is the number of
306  *   samples or pixels sampled per second by the hardware. 
307  * @param cri_rate The Clock Run In is a NRZ modulated
308  *   sequence of '0' and '1' bits prepending most data transmissions to
309  *   synchronize data acquisition circuits. This parameter gives the CRI bit
310  *   rate in Hz, that is the number of CRI bits transmitted per second.
311  * @param bit_rate The transmission bit rate of all data bits following the CRI
312  *   in Hz.
313  * @param cri_frc The FRaming Code usually following the CRI is a bit sequence
314  *   identifying the data service, and per libzvbi definition modulated
315  *   and transmitted at the same bit rate as the payload (however nothing
316  *   stops you from counting all nominal CRI and FRC bits as CRI).
317  *   The bit slicer compares the bits in this word, lsb last transmitted,
318  *   against the transmitted CRI and FRC. Decoding of payload starts
319  *   with the next bit after a match.
320  * @param cri_mask Of the CRI bits in @c cri_frc, only these bits are
321  *   actually significant for a match. For instance it is wise
322  *   not to rely on the very first CRI bits transmitted. Note this
323  *   mask is not shifted left by @a frc_bits.
324  * @param cri_bits 
325  * @param frc_bits Number of CRI and FRC bits in @a cri_frc, respectively.
326  *   Their sum is limited to 32.
327  * @param payload Number of payload <em>bits</em>. Only this data
328  *   will be stored in the vbi_bit_slice() output. If this number
329  *   is no multiple of eight, the most significant bits of the
330  *   last byte are undefined.
331  * @param modulation Modulation of the vbi data, see vbi_modulation.
332  * @param fmt Format of the raw data, see vbi_pixfmt.
333  * 
334  * Initializes vbi_bit_slicer object. Usually you will not use this
335  * function but vbi_raw_decode(), the vbi image decoder which handles
336  * all these details.
337  */
338 void
339 vbi_bit_slicer_init (vbi_bit_slicer * slicer,
340     int raw_samples, int sampling_rate,
341     int cri_rate, int bit_rate,
342     unsigned int cri_frc, unsigned int cri_mask,
343     int cri_bits, int frc_bits, int payload,
344     vbi_modulation modulation, vbi_pixfmt fmt)
345 {
346   unsigned int c_mask = (unsigned int) (-(cri_bits > 0)) >> (32 - cri_bits);
347   unsigned int f_mask = (unsigned int) (-(frc_bits > 0)) >> (32 - frc_bits);
348   int gsh = 0;
349
350   slicer->func = bit_slicer_1;
351
352   switch (fmt) {
353     case VBI_PIXFMT_RGB24:
354     case VBI_PIXFMT_BGR24:
355       slicer->func = bit_slicer_3;
356       slicer->skip = 1;
357       break;
358
359     case VBI_PIXFMT_RGBA32_LE:
360     case VBI_PIXFMT_BGRA32_LE:
361       slicer->func = bit_slicer_4;
362       slicer->skip = 1;
363       break;
364
365     case VBI_PIXFMT_RGBA32_BE:
366     case VBI_PIXFMT_BGRA32_BE:
367       slicer->func = bit_slicer_4;
368       slicer->skip = 2;
369       break;
370
371     case VBI_PIXFMT_RGB16_LE:
372     case VBI_PIXFMT_BGR16_LE:
373       slicer->func = bit_slicer_565_le;
374       gsh = 3;                  /* (green << 3) & 0x07E0 */
375       slicer->skip = 0;
376       break;
377
378     case VBI_PIXFMT_RGBA15_LE:
379     case VBI_PIXFMT_BGRA15_LE:
380       slicer->func = bit_slicer_5551_le;
381       gsh = 2;                  /* (green << 2) & 0x03E0 */
382       slicer->skip = 0;
383       break;
384
385     case VBI_PIXFMT_ARGB15_LE:
386     case VBI_PIXFMT_ABGR15_LE:
387       slicer->func = bit_slicer_1555_le;
388       gsh = 3;                  /* (green << 2) & 0x07C0 */
389       slicer->skip = 0;
390       break;
391
392     case VBI_PIXFMT_RGB16_BE:
393     case VBI_PIXFMT_BGR16_BE:
394       slicer->func = bit_slicer_565_be;
395       gsh = 3;                  /* (green << 3) & 0x07E0 */
396       slicer->skip = 0;
397       break;
398
399     case VBI_PIXFMT_RGBA15_BE:
400     case VBI_PIXFMT_BGRA15_BE:
401       slicer->func = bit_slicer_5551_be;
402       gsh = 2;                  /* (green << 2) & 0x03E0 */
403       slicer->skip = 0;
404       break;
405
406     case VBI_PIXFMT_ARGB15_BE:
407     case VBI_PIXFMT_ABGR15_BE:
408       slicer->func = bit_slicer_1555_be;
409       gsh = 3;                  /* (green << 2) & 0x07C0 */
410       slicer->skip = 0;
411       break;
412
413     case VBI_PIXFMT_YUV420:
414       slicer->func = bit_slicer_1;
415       slicer->skip = 0;
416       break;
417
418     case VBI_PIXFMT_YUYV:
419     case VBI_PIXFMT_YVYU:
420       slicer->func = bit_slicer_2;
421       slicer->skip = 0;
422       break;
423
424     case VBI_PIXFMT_UYVY:
425     case VBI_PIXFMT_VYUY:
426       slicer->func = bit_slicer_2;
427       slicer->skip = 1;
428       break;
429
430     default:
431       fprintf (stderr, "vbi_bit_slicer_init: unknown pixfmt %d\n", fmt);
432       exit (EXIT_FAILURE);
433   }
434
435   slicer->cri_mask = cri_mask & c_mask;
436   slicer->cri = (cri_frc >> frc_bits) & slicer->cri_mask;
437   /* We stop searching for CRI/FRC when the payload
438      cannot possibly fit anymore. */
439   slicer->cri_bytes = raw_samples
440       - ((long long) sampling_rate * (payload + frc_bits)) / bit_rate;
441   slicer->cri_rate = cri_rate;
442   /* Raw vbi data is oversampled to account for low sampling rates. */
443   slicer->oversampling_rate = sampling_rate * OVERSAMPLING;
444   /* 0/1 threshold */
445   slicer->thresh = 105 << (THRESH_FRAC + gsh);
446   slicer->frc = cri_frc & f_mask;
447   slicer->frc_bits = frc_bits;
448   /* Payload bit distance in 1/256 raw samples. */
449   slicer->step = (int) (sampling_rate * 256.0 / bit_rate);
450
451   if (payload & 7) {
452     slicer->payload = payload;
453     slicer->endian = 3;
454   } else {
455     slicer->payload = payload >> 3;
456     slicer->endian = 1;
457   }
458
459   switch (modulation) {
460     case VBI_MODULATION_NRZ_MSB:
461       slicer->endian--;
462     case VBI_MODULATION_NRZ_LSB:
463       slicer->phase_shift = (int)
464           (sampling_rate * 256.0 / cri_rate * .5
465           + sampling_rate * 256.0 / bit_rate * .5 + 128);
466       break;
467
468     case VBI_MODULATION_BIPHASE_MSB:
469       slicer->endian--;
470     case VBI_MODULATION_BIPHASE_LSB:
471       /* Phase shift between the NRZ modulated CRI and the rest */
472       slicer->phase_shift = (int)
473           (sampling_rate * 256.0 / cri_rate * .5
474           + sampling_rate * 256.0 / bit_rate * .25 + 128);
475       break;
476   }
477 }
478
479 #endif
480
481 /**
482  * @example examples/wss.c
483  * WSS capture example.
484  */
485
486 /**
487  * @param rd Initialized vbi_raw_decoder structure.
488  * @param raw A raw vbi image as defined in the vbi_raw_decoder structure
489  *   (rd->sampling_format, rd->bytes_per_line, rd->count[0] + rd->count[1]
490  *    scan lines).
491  * @param out Buffer to store the decoded vbi_sliced data. Since every
492  *   vbi scan line may contain data, this must be an array of vbi_sliced
493  *   with the same number of entries as scan lines in the raw image
494  *   (rd->count[0] + rd->count[1]).
495  * 
496  * Decode a raw vbi image, consisting of several scan lines of raw vbi data,
497  * into sliced vbi data. The output is sorted by line number.
498  * 
499  * Note this function attempts to learn which lines carry which data
500  * service, or none, to speed up decoding. You should avoid using the same
501  * vbi_raw_decoder structure for different sources.
502  *
503  * @return
504  * The number of lines decoded, i. e. the number of vbi_sliced records
505  * written.
506  */
507 int
508 vbi_raw_decode (vbi_raw_decoder * rd, uint8_t * raw, vbi_sliced * out)
509 {
510   vbi3_raw_decoder *rd3;
511   unsigned int n_lines;
512
513   assert (NULL != rd);
514   assert (NULL != raw);
515   assert (NULL != out);
516
517   rd3 = (vbi3_raw_decoder *) rd->pattern;
518   n_lines = rd->count[0] + rd->count[1];
519
520   g_mutex_lock (&rd->mutex);
521
522   {
523     n_lines = vbi3_raw_decoder_decode (rd3, out, n_lines, raw);
524   }
525
526   g_mutex_unlock (&rd->mutex);
527
528   return n_lines;
529 }
530
531 /**
532  * @param rd Initialized vbi_raw_decoder structure.
533  * @param start Array of start line indices for both fields
534  * @param count Array of line counts for both fields
535  * 
536  * Grows or shrinks the internal state arrays for VBI geometry changes
537  */
538 void
539 vbi_raw_decoder_resize (vbi_raw_decoder * rd, int *start, unsigned int *count)
540 {
541 #if 0                           /* Set but unused */
542   vbi_service_set service_set;
543 #endif
544   vbi3_raw_decoder *rd3;
545
546   assert (NULL != rd);
547   assert (NULL != start);
548   assert (NULL != count);
549
550   rd3 = (vbi3_raw_decoder *) rd->pattern;
551
552   g_mutex_lock (&rd->mutex);
553
554   {
555     if ((rd->start[0] == start[0])
556         && (rd->start[1] == start[1])
557         && (rd->count[0] == (int) count[0])
558         && (rd->count[1] == (int) count[1])) {
559       g_mutex_unlock (&rd->mutex);
560       return;
561     }
562
563     rd->start[0] = start[0];
564     rd->start[1] = start[1];
565     rd->count[0] = count[0];
566     rd->count[1] = count[1];
567
568 #if 0                           /* Set but unused */
569     service_set = vbi3_raw_decoder_set_sampling_par
570         (rd3, (vbi_sampling_par *) rd, /* strict */ 0);
571 #else
572     vbi3_raw_decoder_set_sampling_par
573         (rd3, (vbi_sampling_par *) rd, /* strict */ 0);
574 #endif
575   }
576
577   g_mutex_unlock (&rd->mutex);
578 }
579
580 /**
581  * @param rd Initialized vbi_raw_decoder structure.
582  * @param services Set of @ref VBI_SLICED_ symbols.
583  * 
584  * Removes one or more data services to be decoded from the
585  * vbi_raw_decoder structure. This function can be called at any
586  * time and does not touch sampling parameters. 
587  * 
588  * @return 
589  * Set of @ref VBI_SLICED_ symbols describing the remaining data
590  * services that will be decoded.
591  */
592 unsigned int
593 vbi_raw_decoder_remove_services (vbi_raw_decoder * rd, unsigned int services)
594 {
595   vbi_service_set service_set;
596   vbi3_raw_decoder *rd3;
597
598   assert (NULL != rd);
599
600   rd3 = (vbi3_raw_decoder *) rd->pattern;
601   service_set = services;
602
603   g_mutex_lock (&rd->mutex);
604
605   {
606     service_set = vbi3_raw_decoder_remove_services (rd3, service_set);
607   }
608
609   g_mutex_unlock (&rd->mutex);
610
611   return service_set;
612 }
613
614 /**
615  * @param rd Initialized vbi_raw_decoder structure.
616  * @param services Set of @ref VBI_SLICED_ symbols.
617  * @param strict See description of vbi_raw_decoder_add_services()
618  *
619  * Check which of the given services can be decoded with current capture
620  * parameters at a given strictness level.
621  *
622  * @return
623  * Subset of services actually decodable.
624  */
625 unsigned int
626 vbi_raw_decoder_check_services (vbi_raw_decoder * rd,
627     unsigned int services, int strict)
628 {
629   vbi_service_set service_set;
630
631   assert (NULL != rd);
632
633   service_set = services;
634
635   g_mutex_lock (&rd->mutex);
636
637   {
638     service_set = vbi_sampling_par_check_services
639         ((vbi_sampling_par *) rd, service_set, strict);
640   }
641
642   g_mutex_unlock (&rd->mutex);
643
644   return (unsigned int) service_set;
645 }
646
647 /**
648  * @param rd Initialized vbi_raw_decoder structure.
649  * @param services Set of @ref VBI_SLICED_ symbols.
650  * @param strict A value of 0, 1 or 2 requests loose, reliable or strict
651  *  matching of sampling parameters. For example if the data service
652  *  requires knowledge of line numbers while they are not known, @c 0
653  *  will accept the service (which may work if the scan lines are
654  *  populated in a non-confusing way) but @c 1 or @c 2 will not. If the
655  *  data service <i>may</i> use more lines than are sampled, @c 1 will
656  *  accept but @c 2 will not. If unsure, set to @c 1.
657  * 
658  * After you initialized the sampling parameters in @a rd (according to
659  * the abilities of your raw vbi source), this function adds one or more
660  * data services to be decoded. The libzvbi raw vbi decoder can decode up
661  * to eight data services in parallel. You can call this function while
662  * already decoding, it does not change sampling parameters and you must
663  * not change them either after calling this.
664  * 
665  * @return
666  * Set of @ref VBI_SLICED_ symbols describing the data services that actually
667  * will be decoded. This excludes those services not decodable given
668  * the sampling parameters in @a rd.
669  */
670 unsigned int
671 vbi_raw_decoder_add_services (vbi_raw_decoder * rd,
672     unsigned int services, int strict)
673 {
674   vbi_service_set service_set;
675   vbi3_raw_decoder *rd3;
676
677   assert (NULL != rd);
678
679   rd3 = (vbi3_raw_decoder *) rd->pattern;
680   service_set = services;
681
682   g_mutex_lock (&rd->mutex);
683
684   {
685     vbi3_raw_decoder_set_sampling_par (rd3, (vbi_sampling_par *) rd, strict);
686
687     service_set = vbi3_raw_decoder_add_services (rd3, service_set, strict);
688   }
689
690   g_mutex_unlock (&rd->mutex);
691
692   return service_set;
693 }
694
695 /**
696  * @param rd Initialized vbi_raw_decoder structure.
697  * @param services Set of VBI_SLICED_ symbols. Here (and only here) you
698  *   can add @c VBI_SLICED_VBI_625 or @c VBI_SLICED_VBI_525 to include all
699  *   vbi scan lines in the calculated sampling parameters.
700  * @param scanning When 525 accept only NTSC services, when 625
701  *   only PAL/SECAM services. When scanning is 0, determine the scanning
702  *   from the requested services, an ambiguous set will pick
703  *   a 525 or 625 line system at random.
704  * @param max_rate If given, the highest data bit rate in Hz of all
705  *   services requested is stored here. (The sampling rate
706  *   should be at least twice as high; rd->sampling_rate will
707  *   be set to a more reasonable value of 27 MHz derived
708  *   from ITU-R Rec. 601.)
709  * 
710  * Calculate the sampling parameters in @a rd required to receive and
711  * decode the requested data @a services. rd->sampling_format will be
712  * @c VBI_PIXFMT_YUV420, rd->bytes_per_line set accordingly to a
713  * reasonable minimum. This function can be used to initialize hardware
714  * prior to calling vbi_raw_decoder_add_service().
715  * 
716  * @return
717  * Set of @ref VBI_SLICED_ symbols describing the data services covered
718  * by the calculated sampling parameters. This excludes services the libzvbi
719  * raw decoder cannot decode.
720  */
721 unsigned int
722 vbi_raw_decoder_parameters (vbi_raw_decoder * rd,
723     unsigned int services, int scanning, int *max_rate)
724 {
725   vbi_videostd_set videostd_set;
726   vbi_service_set service_set;
727
728   switch (scanning) {
729     case 525:
730       videostd_set = VBI_VIDEOSTD_SET_525_60;
731       break;
732
733     case 625:
734       videostd_set = VBI_VIDEOSTD_SET_625_50;
735       break;
736
737     default:
738       videostd_set = 0;
739       break;
740   }
741
742   service_set = services;
743
744   g_mutex_lock (&rd->mutex);
745
746   {
747     service_set = vbi_sampling_par_from_services
748         ((vbi_sampling_par *) rd,
749         (unsigned int *) max_rate, videostd_set, service_set);
750   }
751
752   g_mutex_unlock (&rd->mutex);
753
754   return (unsigned int) service_set;
755 }
756
757 /**
758  * @param rd Initialized vbi_raw_decoder structure.
759  * 
760  * Reset a vbi_raw_decoder structure. This removes
761  * all previously added services to be decoded (if any)
762  * but does not touch the sampling parameters. You are
763  * free to change the sampling parameters after calling this.
764  */
765 void
766 vbi_raw_decoder_reset (vbi_raw_decoder * rd)
767 {
768   vbi3_raw_decoder *rd3;
769
770   if (!rd)
771     return;                     /* compatibility */
772
773   assert (NULL != rd);
774
775   rd3 = (vbi3_raw_decoder *) rd->pattern;
776
777   g_mutex_lock (&rd->mutex);
778
779   {
780     vbi3_raw_decoder_reset (rd3);
781   }
782
783   g_mutex_unlock (&rd->mutex);
784 }
785
786 /**
787  * @param rd Pointer to initialized vbi_raw_decoder
788  *  structure, can be @c NULL.
789  * 
790  * Free all resources associated with @a rd.
791  */
792 void
793 vbi_raw_decoder_destroy (vbi_raw_decoder * rd)
794 {
795   vbi3_raw_decoder *rd3;
796
797   assert (NULL != rd);
798
799   rd3 = (vbi3_raw_decoder *) rd->pattern;
800
801   vbi3_raw_decoder_delete (rd3);
802
803   g_mutex_clear (&rd->mutex);
804
805   CLEAR (*rd);
806 }
807
808 /**
809  * @param rd Pointer to a vbi_raw_decoder structure.
810  * 
811  * Initializes a vbi_raw_decoder structure.
812  */
813 void
814 vbi_raw_decoder_init (vbi_raw_decoder * rd)
815 {
816   vbi3_raw_decoder *rd3;
817
818   assert (NULL != rd);
819
820   CLEAR (*rd);
821
822   g_mutex_init (&rd->mutex);
823
824   rd3 = vbi3_raw_decoder_new ( /* sampling_par */ NULL);
825   assert (NULL != rd3);
826
827   rd->pattern = (int8_t *) rd3;
828 }
829
830 GST_DEBUG_CATEGORY (libzvbi_debug);
831 void
832 vbi_initialize_gst_debug (void)
833 {
834   GST_DEBUG_CATEGORY_INIT (libzvbi_debug, "libzvbi", 0, "libzvbi");
835 }
836
837 /*
838 Local variables:
839 c-set-style: K&R
840 c-basic-offset: 8
841 End:
842 */