ZVBI Library  0.2.35
examples/pdc1.c
/*
* libzvbi VPS/PDC example 1
*
* Copyright (C) 2009 Michael H. Schimek
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* $Id: pdc1.c,v 1.2 2009/03/23 01:30:42 mschimek Exp $ */
/* This example shows how to receive Program IDs transmitted in VPS
and Teletext packets using the libzvbi VBI decoder. Example pdc2.c
demonstrates how video recorders respond to Program IDs.
If you prefer to decode the VPS and Teletext packets directly see
test/decode.c for an example.
gcc -o pdc1 pdc1.c `pkg-config zvbi-0.2 --cflags --libs` */
#undef NDEBUG
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
#include <libzvbi.h>
static vbi_capture * cap;
static vbi_decoder * dec;
/* Attention! This function returns a static string. */
static const char *
pil_str (vbi_pil pil)
{
static char buffer[32];
switch (pil) {
case VBI_PIL_TIMER_CONTROL: return "TC";
case VBI_PIL_INHIBIT_TERMINATE: return "RI/T";
case VBI_PIL_INTERRUPTION: return "INT";
case VBI_PIL_CONTINUE: return "CONT";
case VBI_PIL_NSPV: return "NSPV/END";
default:
snprintf (buffer, sizeof (buffer),
"%02u%02uT%02u%02u",
VBI_PIL_DAY (pil),
VBI_PIL_HOUR (pil),
return buffer;
}
}
static void
void * user_data)
{
const vbi_program_id *pid;
user_data = user_data; /* unused, no warning please */
pid = ev->ev.prog_id;
printf ("Received PIL %s/%02X on LC %u.\n",
pil_str (pid->pil), pid->pty, pid->channel);
}
static void
mainloop (void)
{
struct timeval timeout;
/* Don't wait more than two seconds for the driver
to return data. */
timeout.tv_sec = 2;
timeout.tv_usec = 0;
for (;;) {
vbi_capture_buffer *sliced_buffer;
unsigned int n_lines;
int r;
r = vbi_capture_pull (cap,
/* raw_buffer */ NULL,
&sliced_buffer,
&timeout);
switch (r) {
case -1:
fprintf (stderr, "VBI read error %d (%s)\n",
errno, strerror (errno));
/* Could be ignored, esp. EIO with some drivers. */
exit (EXIT_FAILURE);
case 0:
fprintf (stderr, "VBI read timeout\n");
exit (EXIT_FAILURE);
case 1: /* success */
break;
default:
assert (0);
}
n_lines = sliced_buffer->size / sizeof (vbi_sliced);
vbi_decode (dec,
(vbi_sliced *) sliced_buffer->data,
n_lines,
sliced_buffer->timestamp);
}
}
int
main (void)
{
char *errstr;
vbi_bool success;
unsigned int services;
setlocale (LC_ALL, "");
services = (VBI_SLICED_TELETEXT_B |
VBI_SLICED_VPS);
cap = vbi_capture_v4l2_new ("/dev/vbi",
/* buffers */ 5,
&services,
/* strict */ 0,
&errstr,
/* verbose */ FALSE);
if (NULL == cap) {
fprintf (stderr,
"Cannot capture VBI data with V4L2 interface:\n"
"%s\n",
errstr);
free (errstr);
exit (EXIT_FAILURE);
}
dec = vbi_decoder_new ();
assert (NULL != dec);
success = vbi_event_handler_add (dec,
VBI_EVENT_PROG_ID,
/* user_data */ NULL);
assert (success);
mainloop ();
exit (EXIT_SUCCESS);
}
vbi_event
Event union.
Definition: event.h:736
vbi_decoder_new
vbi_decoder * vbi_decoder_new(void)
Allocate a new data service decoder instance.
Definition: vbi.c:870
VBI_PIL_DAY
#define VBI_PIL_DAY(pil)
Definition: pdc.h:75
vbi_capture_v4l2_new
vbi_capture * vbi_capture_v4l2_new(const char *dev_name, int buffers, unsigned int *services, int strict, char **errstr, vbi_bool trace)
Definition: io-v4l2.c:203
vbi_capture
Definition: io.h:252
vbi_capture_delete
void vbi_capture_delete(vbi_capture *capture)
Definition: io.c:446
VBI_PIL_MINUTE
#define VBI_PIL_MINUTE(pil)
Definition: pdc.h:81
event_handler
Definition: vbi.h:41
VBI_PIL_INTERRUPTION
@ VBI_PIL_INTERRUPTION
Definition: pdc.h:131
vbi_event_handler_add
vbi_bool vbi_event_handler_add(vbi_decoder *vbi, int event_mask, vbi_event_handler handler, void *user_data)
Definition: vbi.c:186
vbi_program_id
Program Identification.
Definition: pdc.h:347
vbi_capture_buffer
Definition: io.h:38
vbi_capture_pull
int vbi_capture_pull(vbi_capture *capture, vbi_capture_buffer **raw_buffer, vbi_capture_buffer **sliced_buffer, struct timeval *timeout)
Definition: io.c:251
vbi_program_id::channel
vbi_pid_channel channel
Definition: pdc.h:349
VBI_PIL_TIMER_CONTROL
@ VBI_PIL_TIMER_CONTROL
Definition: pdc.h:104
vbi_sliced
This structure holds one scan line of sliced vbi data.
Definition: sliced.h:320
vbi_decoder_delete
void vbi_decoder_delete(vbi_decoder *vbi)
Delete a data service decoder instance.
Definition: vbi.c:832
VBI_PIL_MONTH
#define VBI_PIL_MONTH(pil)
Definition: pdc.h:72
vbi_decode
void vbi_decode(vbi_decoder *vbi, vbi_sliced *sliced, int lines, double time)
Main function of the data service decoder.
Definition: vbi.c:421
vbi_program_id::pty
unsigned int pty
Definition: pdc.h:417
vbi_decoder
Definition: vbi.h:48
VBI_PIL_INHIBIT_TERMINATE
@ VBI_PIL_INHIBIT_TERMINATE
Definition: pdc.h:116
VBI_PIL_NSPV
@ VBI_PIL_NSPV
Definition: pdc.h:150
VBI_PIL_HOUR
#define VBI_PIL_HOUR(pil)
Definition: pdc.h:78
vbi_pil
unsigned int vbi_pil
Program Identification Label.
Definition: pdc.h:53
VBI_PIL_CONTINUE
@ VBI_PIL_CONTINUE
Definition: pdc.h:141
vbi_program_id::pil
vbi_pil pil
Definition: pdc.h:371