00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #undef NDEBUG
00031 #include <assert.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036
00037 #include <libzvbi.h>
00038
00039 static vbi_dvb_demux * dvb;
00040 static uint8_t pes_buffer[2048];
00041 static vbi_sampling_par sp;
00042 static uint8_t * image;
00043 static unsigned int image_size;
00044 static unsigned int pixel_mask;
00045 static int64_t last_pts;
00046 static vbi_raw_decoder rd;
00047
00048 static void
00049 raw_test (const vbi_sliced * expect_sliced,
00050 unsigned int expect_n_lines)
00051 {
00052 vbi_sliced sliced[50];
00053 unsigned int n_lines;
00054 unsigned int i;
00055
00056 n_lines = vbi_raw_decode (&rd, image, sliced);
00057 assert (n_lines == expect_n_lines);
00058
00059 for (i = 0; i < n_lines; ++i) {
00060 unsigned int payload;
00061
00062 assert (sliced[i].id == expect_sliced[i].id);
00063 assert (sliced[i].line == expect_sliced[i].line);
00064
00065 payload = (vbi_sliced_payload_bits (sliced[i].id) + 7) / 8;
00066 assert (0 == memcmp (sliced[i].data,
00067 expect_sliced[i].data,
00068 payload));
00069 }
00070 }
00071
00072 static vbi_bool
00073 convert (vbi_dvb_demux * dx,
00074 void * user_data,
00075 const vbi_sliced * sliced,
00076 unsigned int n_lines,
00077 int64_t pts)
00078 {
00079 vbi_bool success;
00080 ssize_t actual;
00081
00082 dx = dx;
00083 user_data = user_data;
00084
00085 pts &= ((int64_t) 1 << 33) - 1;
00086
00087 if (0 == last_pts) {
00088 last_pts = pts;
00089 } else if (pts < last_pts) {
00090 last_pts -= (int64_t) 1 << 33;
00091 }
00092
00093 while (pts - last_pts > 90000 / 25 * 3 / 2) {
00094
00095
00096 success = vbi_raw_video_image (image, image_size, &sp,
00097 0, 0, 0, pixel_mask, FALSE,
00098 NULL, 0);
00099 assert (success);
00100
00101 raw_test (NULL, 0);
00102
00103 actual = write (STDOUT_FILENO, image, image_size);
00104 assert (actual == (ssize_t) image_size);
00105
00106 last_pts += 90000 / 25;
00107 }
00108
00109 success = vbi_raw_video_image (image, image_size, &sp,
00110 0,
00111 0,
00112 0,
00113 pixel_mask,
00114 FALSE,
00115 sliced, n_lines);
00116 assert (success);
00117
00118 raw_test (sliced, n_lines);
00119
00120 actual = write (STDOUT_FILENO, image, image_size);
00121 assert (actual == (ssize_t) image_size);
00122
00123 last_pts = pts;
00124
00125 return TRUE;
00126 }
00127
00128 static void
00129 mainloop (void)
00130 {
00131 while (1 == fread (pes_buffer, sizeof (pes_buffer), 1, stdin)) {
00132 vbi_bool success;
00133
00134 success = vbi_dvb_demux_feed (dvb,
00135 pes_buffer,
00136 sizeof (pes_buffer));
00137 assert (success);
00138 }
00139
00140 fprintf (stderr, "End of stream.\n");
00141 }
00142
00143 int
00144 main (void)
00145 {
00146 if (isatty (STDIN_FILENO)) {
00147 fprintf (stderr, "No DVB PES on standard input.\n");
00148 exit (EXIT_FAILURE);
00149 }
00150
00151 if (isatty (STDOUT_FILENO)) {
00152 fprintf (stderr, "Output is binary image data. Pipe to "
00153 "another tool or redirect to a file.\n");
00154 exit (EXIT_FAILURE);
00155 }
00156
00157
00158 vbi_set_log_fn ( VBI_LOG_NOTICE * 2 - 1,
00159 vbi_log_on_stderr,
00160 NULL);
00161
00162 dvb = vbi_dvb_pes_demux_new (convert, NULL);
00163 assert (NULL != dvb);
00164
00165 memset (&sp, 0, sizeof (sp));
00166
00167 #if 1
00168
00169
00170 sp.scanning = 625;
00171 sp.sampling_format = VBI_PIXFMT_YUYV;
00172 sp.sampling_rate = 13.5e6;
00173 sp.bytes_per_line = 720 * 2;
00174 sp.offset = 9.5e-6 * 13.5e6;
00175 sp.start[0] = 6;
00176 sp.count[0] = 17;
00177 sp.start[1] = 319;
00178 sp.count[1] = 17;
00179 sp.interlaced = TRUE;
00180 sp.synchronous = TRUE;
00181
00182 pixel_mask = 0x000000FF;
00183 #else
00184
00185
00186 sp.scanning = 625;
00187 sp.sampling_format = VBI_PIXFMT_BGRA32_LE;
00188 sp.sampling_rate = 14.75e6;
00189 sp.bytes_per_line = 768 * 4;
00190 sp.offset = 10.2e-6 * 14.75e6;
00191 sp.start[0] = 6;
00192 sp.count[0] = 17;
00193 sp.start[1] = 319;
00194 sp.count[1] = 17;
00195 sp.interlaced = TRUE;
00196 sp.synchronous = TRUE;
00197
00198 pixel_mask = 0x0000FF00;
00199 #endif
00200
00201 image_size = (sp.count[0] + sp.count[1]) * sp.bytes_per_line;
00202 image = malloc (image_size);
00203 assert (NULL != image);
00204
00205 if (VBI_PIXFMT_YUYV == sp.sampling_format) {
00206
00207 memset (image, 0x80, image_size);
00208 } else {
00209 memset (image, 0x00, image_size);
00210 }
00211
00212
00213
00214 vbi_raw_decoder_init (&rd);
00215
00216 rd.scanning = sp.scanning;
00217 rd.sampling_format = sp.sampling_format;
00218 rd.sampling_rate = sp.sampling_rate;
00219 rd.bytes_per_line = sp.bytes_per_line;
00220 rd.offset = sp.offset;
00221 rd.start[0] = sp.start[0];
00222 rd.start[1] = sp.start[1];
00223 rd.count[0] = sp.count[0];
00224 rd.count[1] = sp.count[1];
00225 rd.interlaced = sp.interlaced;
00226 rd.synchronous = sp.synchronous;
00227
00228
00229
00230 vbi_raw_decoder_add_services (&rd,
00231 (VBI_SLICED_TELETEXT_B |
00232 VBI_SLICED_VPS |
00233 VBI_SLICED_CAPTION_625),
00234 0);
00235
00236 mainloop ();
00237
00238 vbi_dvb_demux_delete (dvb);
00239
00240 exit (EXIT_SUCCESS);
00241
00242 return 0;
00243 }