1 /*****************************************************************************
2 * linsys_sdi.c: SDI capture for Linear Systems/Computer Modules cards
3 *****************************************************************************
4 * Copyright (C) 2009-2011 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
40 #include <vlc_input.h>
41 #include <vlc_access.h>
42 #include <vlc_demux.h>
46 #include "linsys_sdi.h"
51 #define SDI_DEVICE "/dev/sdirx%u"
52 #define SDI_BUFFERS_FILE "/sys/class/sdi/sdirx%u/buffers"
53 #define SDI_BUFSIZE_FILE "/sys/class/sdi/sdirx%u/bufsize"
54 #define SDI_MODE_FILE "/sys/class/sdi/sdirx%u/mode"
55 #define READ_TIMEOUT 80000
56 #define RESYNC_TIMEOUT 500000
57 #define CLOCK_GAP INT64_C(500000)
58 #define START_DATE INT64_C(4294967296)
60 #define DEMUX_BUFFER_SIZE 1350000
62 #define SAMPLERATE_TOLERANCE 0.1
64 /*****************************************************************************
66 *****************************************************************************/
67 #define LINK_TEXT N_("Link #")
68 #define LINK_LONGTEXT N_( \
69 "Allows you to set the desired link of the board for the capture (starting at 0)." )
70 #define VIDEO_TEXT N_("Video ID")
71 #define VIDEO_LONGTEXT N_( \
72 "Allows you to set the ES ID of the video." )
73 #define VIDEO_ASPECT_TEXT N_("Aspect ratio")
74 #define VIDEO_ASPECT_LONGTEXT N_( \
75 "Allows you to force the aspect ratio of the video." )
76 #define AUDIO_TEXT N_("Audio configuration")
77 #define AUDIO_LONGTEXT N_( \
78 "Allows you to set audio configuration (id=group,pair:id=group,pair...)." )
79 #define TELX_TEXT N_("Teletext configuration")
80 #define TELX_LONGTEXT N_( \
81 "Allows you to set Teletext configuration (id=line1-lineN with both fields)." )
82 #define TELX_LANG_TEXT N_("Teletext language")
83 #define TELX_LANG_LONGTEXT N_( \
84 "Allows you to set Teletext language (page=lang/type,...)." )
86 static int Open ( vlc_object_t * );
87 static void Close( vlc_object_t * );
88 static int DemuxOpen ( vlc_object_t * );
89 static void DemuxClose( vlc_object_t * );
92 set_description( N_("SDI Input") )
93 set_shortname( N_("SDI") )
94 set_category( CAT_INPUT )
95 set_subcategory( SUBCAT_INPUT_ACCESS )
97 add_integer( "linsys-sdi-link", 0,
98 LINK_TEXT, LINK_LONGTEXT, true )
100 add_integer( "linsys-sdi-id-video", 0, VIDEO_TEXT, VIDEO_LONGTEXT, true )
101 add_string( "linsys-sdi-aspect-ratio", "", VIDEO_ASPECT_TEXT,
102 VIDEO_ASPECT_LONGTEXT, true )
103 add_string( "linsys-sdi-audio", "0=1,1", AUDIO_TEXT, AUDIO_LONGTEXT, true )
104 add_string( "linsys-sdi-telx", "", TELX_TEXT, TELX_LONGTEXT, true )
105 add_string( "linsys-sdi-telx-lang", "", TELX_LANG_TEXT, TELX_LANG_LONGTEXT,
108 set_capability( "access_demux", 0 )
109 add_shortcut( "linsys-sdi" )
110 set_callbacks( Open, Close )
113 set_description( N_("SDI Demux") )
114 set_capability( "demux", 0 )
115 set_callbacks( DemuxOpen, DemuxClose )
118 /*****************************************************************************
120 *****************************************************************************/
121 typedef struct sdi_audio_t
123 unsigned int i_group, i_pair;
128 uint8_t i_block_number;
130 unsigned int i_left_samples, i_right_samples, i_nb_samples, i_max_samples;
153 uint8_t **pp_buffers;
154 unsigned int i_buffers, i_current_buffer;
155 unsigned int i_buffer_size;
159 mtime_t i_last_state_change;
160 unsigned int i_anc_size, i_active_size, i_picture_size;
161 unsigned int i_line_offset, i_nb_lines;
164 unsigned int i_line_buffer;
165 unsigned int i_current_line;
166 uint8_t *p_line_buffer;
167 block_t *p_current_picture;
168 uint8_t *p_y, *p_u, *p_v;
169 uint8_t *p_wss_buffer;
170 uint8_t *p_telx_buffer;
172 /* picture decoding */
173 unsigned int i_frame_rate, i_frame_rate_base;
174 unsigned int i_width, i_height, i_aspect, i_forced_aspect;
175 unsigned int i_block_size;
176 unsigned int i_telx_line, i_telx_count;
177 char *psz_telx, *psz_telx_lang;
179 vbi_raw_decoder rd_wss, rd_telx;
185 es_out_id_t *p_es_video;
186 sdi_audio_t p_audios[MAX_AUDIOS];
187 es_out_id_t *p_es_telx;
190 static int Control( demux_t *, int, va_list );
191 static int DemuxControl( demux_t *, int, va_list );
192 static int Demux( demux_t * );
193 static int DemuxDemux( demux_t * );
195 static int InitWSS( demux_t *p_demux );
196 static int InitTelx( demux_t *p_demux );
198 static int HandleSDBuffer( demux_t *p_demux, uint8_t *p_buffer,
199 unsigned int i_buffer_size );
201 static int InitCapture( demux_t *p_demux );
202 static void CloseCapture( demux_t *p_demux );
203 static int Capture( demux_t *p_demux );
205 /*****************************************************************************
207 *****************************************************************************/
208 static int DemuxOpen( vlc_object_t *p_this )
210 demux_t *p_demux = (demux_t *)p_this;
214 /* Fill p_demux field */
215 p_demux->pf_demux = DemuxDemux;
216 p_demux->pf_control = DemuxControl;
217 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
218 if( unlikely(!p_sys ) )
221 p_sys->i_state = STATE_NOSYNC;
222 p_sys->i_last_state_change = mdate();
225 char *psz_ar = var_InheritString( p_demux, "linsys-sdi-aspect-ratio" );
226 if ( psz_ar != NULL )
228 psz_parser = strchr( psz_ar, ':' );
231 *psz_parser++ = '\0';
232 p_sys->i_forced_aspect = p_sys->i_aspect =
233 strtol( psz_ar, NULL, 0 ) * VOUT_ASPECT_FACTOR
234 / strtol( psz_parser, NULL, 0 );
237 p_sys->i_forced_aspect = 0;
242 p_sys->i_id_video = var_InheritInteger( p_demux, "linsys-sdi-id-video" );
245 char *psz_string = psz_parser = var_InheritString( p_demux,
246 "linsys-sdi-audio" );
249 while ( psz_parser != NULL && *psz_parser )
251 int i_id, i_group, i_pair;
252 char *psz_next = strchr( psz_parser, '=' );
253 if ( psz_next != NULL )
256 i_id = strtol( psz_parser, NULL, 0 );
257 psz_parser = psz_next + 1;
262 psz_next = strchr( psz_parser, ':' );
263 if ( psz_next != NULL )
269 if ( sscanf( psz_parser, "%d,%d", &i_group, &i_pair ) == 2 )
271 p_sys->p_audios[i].i_group = i_group;
272 p_sys->p_audios[i].i_pair = i_pair;
273 p_sys->p_audios[i].i_id = i_id;
277 msg_Warn( p_demux, "malformed audio configuration (%s)",
280 psz_parser = psz_next;
285 p_sys->psz_telx = var_InheritString( p_demux, "linsys-sdi-telx" );
287 p_sys->psz_telx_lang = var_InheritString( p_demux, "linsys-sdi-telx-lang" );
292 static int Open( vlc_object_t *p_this )
294 demux_t *p_demux = (demux_t *)p_this;
298 if ( (i_ret = DemuxOpen( p_this )) != VLC_SUCCESS )
301 /* Fill p_demux field */
302 p_demux->pf_demux = Demux;
303 p_demux->pf_control = Control;
304 p_sys = p_demux->p_sys;
306 p_sys->i_link = var_InheritInteger( p_demux, "linsys-sdi-link" );
308 if( InitCapture( p_demux ) != VLC_SUCCESS )
317 /*****************************************************************************
319 *****************************************************************************/
320 static void DemuxClose( vlc_object_t *p_this )
322 demux_t *p_demux = (demux_t *)p_this;
323 demux_sys_t *p_sys = p_demux->p_sys;
325 free( p_sys->psz_telx );
326 free( p_sys->psz_telx_lang );
330 static void Close( vlc_object_t *p_this )
332 demux_t *p_demux = (demux_t *)p_this;
334 CloseCapture( p_demux );
335 DemuxClose( p_this );
338 /*****************************************************************************
340 *****************************************************************************/
341 static int DemuxDemux( demux_t *p_demux )
343 block_t *p_block = stream_Block( p_demux->s, DEMUX_BUFFER_SIZE );
346 if ( p_block == NULL )
349 i_ret = HandleSDBuffer( p_demux, p_block->p_buffer, p_block->i_buffer );
350 block_Release( p_block );
352 return ( i_ret == VLC_SUCCESS );
355 static int Demux( demux_t *p_demux )
357 return ( Capture( p_demux ) == VLC_SUCCESS );
360 /*****************************************************************************
362 *****************************************************************************/
363 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
365 return demux_vaControlHelper( p_demux->s, -1, -1, 270000000, 1, i_query,
369 static int Control( demux_t *p_demux, int i_query, va_list args )
376 /* Special for access_demux */
377 case DEMUX_CAN_PAUSE:
378 case DEMUX_CAN_CONTROL_PACE:
380 pb = (bool*)va_arg( args, bool * );
384 case DEMUX_GET_PTS_DELAY:
385 pi64 = (int64_t*)va_arg( args, int64_t * );
386 *pi64 = INT64_C(1000)
387 * var_InheritInteger( p_demux, "live-caching" );
390 /* TODO implement others */
396 /*****************************************************************************
397 * Video, audio & VBI decoding
398 *****************************************************************************/
401 struct block_extension_t
403 bool b_progressive; /**< is it a progressive frame ? */
404 bool b_top_field_first; /**< which field is first */
405 unsigned int i_nb_fields; /**< # of displayed fields */
406 unsigned int i_aspect; /**< aspect ratio of frame */
409 static int NewFrame( demux_t *p_demux )
411 demux_sys_t *p_sys = p_demux->p_sys;
413 p_sys->p_current_picture = block_New( p_demux, p_sys->i_block_size );
414 if( unlikely( !p_sys->p_current_picture ) )
416 p_sys->p_y = p_sys->p_current_picture->p_buffer;
417 p_sys->p_u = p_sys->p_y + p_sys->i_width * p_sys->i_height;
418 p_sys->p_v = p_sys->p_u + p_sys->i_width * p_sys->i_height / 4;
420 for ( int i = 0; i < MAX_AUDIOS; i++ )
422 sdi_audio_t *p_audio = &p_sys->p_audios[i];
423 p_audio->i_left_samples = p_audio->i_right_samples = 0;
428 static int StartDecode( demux_t *p_demux )
430 demux_sys_t *p_sys = p_demux->p_sys;
434 p_sys->i_next_date = START_DATE;
435 p_sys->i_incr = 1000000 * p_sys->i_frame_rate_base / p_sys->i_frame_rate;
436 p_sys->i_block_size = p_sys->i_width * p_sys->i_height * 3 / 2
437 + sizeof(struct block_extension_t);
438 if( NewFrame( p_demux ) != VLC_SUCCESS )
442 es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_I420 );
443 fmt.i_id = p_sys->i_id_video;
444 fmt.video.i_frame_rate = p_sys->i_frame_rate;
445 fmt.video.i_frame_rate_base = p_sys->i_frame_rate_base;
446 fmt.video.i_width = p_sys->i_width;
447 fmt.video.i_height = p_sys->i_height;
448 int i_aspect = p_sys->i_forced_aspect ? p_sys->i_forced_aspect
450 fmt.video.i_sar_num = i_aspect * fmt.video.i_height
452 fmt.video.i_sar_den = VOUT_ASPECT_FACTOR;
453 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
455 if ( p_sys->b_vbi && InitWSS( p_demux ) != VLC_SUCCESS )
459 psz_parser = p_sys->psz_telx;
460 if ( psz_parser != NULL && *psz_parser )
464 msg_Warn( p_demux, "VBI is unsupported on this input stream" );
469 char *psz_next = strchr( psz_parser, '=' );
470 if ( psz_next != NULL )
473 i_id = strtol( psz_parser, NULL, 0 );
474 psz_parser = psz_next + 1;
479 psz_next = strchr( psz_parser, '-' );
480 if ( psz_next != NULL )
483 p_sys->i_telx_line = strtol( psz_parser, NULL, 0 ) - 1;
484 if ( psz_next != NULL )
485 p_sys->i_telx_count = strtol( psz_next, NULL, 0 )
486 - p_sys->i_telx_line - 1 + 1;
488 p_sys->i_telx_count = 1;
490 if ( InitTelx( p_demux ) == VLC_SUCCESS )
493 uint8_t *p_dr = NULL;
495 msg_Dbg( p_demux, "capturing VBI lines %d-%d and %d-%d",
496 p_sys->i_telx_line + 1,
497 p_sys->i_telx_line + 1 + p_sys->i_telx_count - 1,
498 p_sys->i_telx_line + 1 + 313,
499 p_sys->i_telx_line + 1 + 313
500 + p_sys->i_telx_count - 1 );
502 es_format_Init( &fmt, SPU_ES, VLC_CODEC_TELETEXT );
505 /* Teletext language & type */
506 psz_parser = p_sys->psz_telx_lang;
508 while ( (psz_next = strchr( psz_parser, '=' )) != NULL )
512 if ( !psz_next[0] || !psz_next[1] || !psz_next[2] )
514 i_page = strtol( psz_parser, NULL, 0 );
516 p_dr = realloc( p_dr, i_dr_size );
517 p_dr[i_dr_size - 5] = *psz_next++;
518 p_dr[i_dr_size - 4] = *psz_next++;
519 p_dr[i_dr_size - 3] = *psz_next++;
520 if ( *psz_next == '/' )
523 p_dr[i_dr_size - 2] = strtol( psz_next, &psz_next, 0 )
526 else /* subtitle for hearing impaired */
527 p_dr[i_dr_size - 2] = 0x5 << 3;
528 p_dr[i_dr_size - 2] |= (i_page / 100) & 0x7;
529 p_dr[i_dr_size - 1] = i_page % 100;
531 if ( *psz_next == ',' )
533 psz_parser = psz_next;
536 fmt.i_extra = i_dr_size;
538 p_sys->p_es_telx = es_out_Add( p_demux->out, &fmt );
541 p_sys->i_telx_count = 0;
547 static void StopDecode( demux_t *p_demux )
549 demux_sys_t *p_sys = p_demux->p_sys;
551 if ( p_sys->i_state != STATE_SYNC )
554 free( p_sys->p_line_buffer );
556 block_Release( p_sys->p_current_picture );
557 p_sys->p_current_picture = NULL;
558 es_out_Del( p_demux->out, p_sys->p_es_video );
562 free( p_sys->p_wss_buffer );
563 p_sys->p_wss_buffer = NULL;
564 vbi_raw_decoder_destroy( &p_sys->rd_wss );
566 if ( p_sys->p_es_telx )
568 es_out_Del( p_demux->out, p_sys->p_es_telx );
569 free( p_sys->p_telx_buffer );
570 p_sys->p_telx_buffer = NULL;
571 vbi_raw_decoder_destroy( &p_sys->rd_telx );
575 for ( int i = 0; i < MAX_AUDIOS; i++ )
577 sdi_audio_t *p_audio = &p_sys->p_audios[i];
578 if ( p_audio->i_group && p_audio->p_es != NULL )
580 es_out_Del( p_demux->out, p_audio->p_es );
581 p_audio->p_es = NULL;
582 free( p_audio->p_buffer );
583 p_audio->p_buffer = NULL;
588 static void InitVideo( demux_t *p_demux )
590 demux_sys_t *p_sys = p_demux->p_sys;
591 int i_total_width = (p_sys->i_anc_size + p_sys->i_active_size) * 4 / 5;
592 p_sys->i_width = (p_sys->i_active_size - 5) * 4 / 10;
593 if ( p_sys->i_nb_lines == 625 )
596 p_sys->i_frame_rate = 25;
597 p_sys->i_frame_rate_base = 1;
598 p_sys->i_height = 576;
599 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
602 else if ( p_sys->i_nb_lines == 525 )
605 p_sys->i_frame_rate = 30000;
606 p_sys->i_frame_rate_base = 1001;
607 p_sys->i_height = 480;
608 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
611 else if ( p_sys->i_nb_lines == 1125 && i_total_width == 2640 )
613 /* 1080i50 or 1080p25 */
614 p_sys->i_frame_rate = 25;
615 p_sys->i_frame_rate_base = 1;
616 p_sys->i_height = 1080;
617 p_sys->i_aspect = 16 * VOUT_ASPECT_FACTOR / 9;
620 else if ( p_sys->i_nb_lines == 1125 && i_total_width == 2200 )
622 /* 1080i60 or 1080p30 */
623 p_sys->i_frame_rate = 30000;
624 p_sys->i_frame_rate_base = 1001;
625 p_sys->i_height = 1080;
626 p_sys->i_aspect = 16 * VOUT_ASPECT_FACTOR / 9;
629 else if ( p_sys->i_nb_lines == 750 && i_total_width == 1980 )
632 p_sys->i_frame_rate = 50;
633 p_sys->i_frame_rate_base = 1;
634 p_sys->i_height = 720;
635 p_sys->i_aspect = 16 * VOUT_ASPECT_FACTOR / 9;
638 else if ( p_sys->i_nb_lines == 750 && i_total_width == 1650 )
641 p_sys->i_frame_rate = 60000;
642 p_sys->i_frame_rate_base = 1001;
643 p_sys->i_height = 720;
644 p_sys->i_aspect = 16 * VOUT_ASPECT_FACTOR / 9;
649 msg_Warn( p_demux, "unable to determine video type" );
650 /* Put sensitive defaults */
651 p_sys->i_frame_rate = 25;
652 p_sys->i_frame_rate_base = 1;
653 p_sys->i_height = p_sys->i_nb_lines;
654 p_sys->i_aspect = 16 * VOUT_ASPECT_FACTOR / 9;
657 p_sys->b_vbi = !p_sys->b_hd;
660 static void DecodeVideo( demux_t *p_demux )
662 demux_sys_t *p_sys = p_demux->p_sys;
663 struct block_extension_t ext;
665 /* FIXME: progressive formats ? */
666 ext.b_progressive = false;
668 ext.b_top_field_first = true;
669 ext.i_aspect = p_sys->i_forced_aspect ? p_sys->i_forced_aspect :
672 memcpy( &p_sys->p_current_picture->p_buffer[p_sys->i_block_size
673 - sizeof(struct block_extension_t)],
674 &ext, sizeof(struct block_extension_t) );
676 p_sys->p_current_picture->i_dts = p_sys->p_current_picture->i_pts
677 = p_sys->i_next_date;
678 es_out_Send( p_demux->out, p_sys->p_es_video, p_sys->p_current_picture );
681 static int InitWSS( demux_t *p_demux )
683 demux_sys_t *p_sys = p_demux->p_sys;
684 vbi_raw_decoder_init( &p_sys->rd_wss );
686 p_sys->rd_wss.scanning = 625;
687 p_sys->rd_wss.sampling_format = VBI_PIXFMT_UYVY;
688 p_sys->rd_wss.sampling_rate = 13.5e6;
689 p_sys->rd_wss.bytes_per_line = 720 * 2;
690 p_sys->rd_wss.offset = 9.5e-6 * 13.5e6;
692 p_sys->rd_wss.start[0] = 23;
693 p_sys->rd_wss.count[0] = 1;
694 p_sys->rd_wss.start[1] = 0;
695 p_sys->rd_wss.count[1] = 0;
697 p_sys->rd_wss.interlaced = FALSE;
698 p_sys->rd_wss.synchronous = TRUE;
700 if ( vbi_raw_decoder_add_services( &p_sys->rd_wss,
702 /* strict */ 2 ) == 0 )
704 msg_Warn( p_demux, "cannot initialize zvbi for WSS" );
705 vbi_raw_decoder_destroy ( &p_sys->rd_telx );
709 p_sys->p_wss_buffer = malloc( p_sys->i_width * 2 );
710 if( !p_sys->p_wss_buffer )
712 vbi_raw_decoder_destroy ( &p_sys->rd_telx );
718 static void DecodeWSS( demux_t *p_demux )
720 demux_sys_t *p_sys = p_demux->p_sys;
721 vbi_sliced p_sliced[1];
723 if ( vbi_raw_decode( &p_sys->rd_wss, p_sys->p_wss_buffer, p_sliced ) == 0 )
725 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
729 unsigned int i_old_aspect = p_sys->i_aspect;
730 uint8_t *p = p_sliced[0].data;
731 int i_aspect, i_parity;
733 i_aspect = p[0] & 15;
735 i_parity ^= i_parity >> 2;
736 i_parity ^= i_parity >> 1;
739 if ( !(i_parity & 1) )
740 msg_Warn( p_demux, "WSS parity error" );
741 else if ( i_aspect == 7 )
742 p_sys->i_aspect = 16 * VOUT_ASPECT_FACTOR / 9;
744 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
746 if ( p_sys->i_aspect != i_old_aspect )
747 msg_Dbg( p_demux, "new WSS information (ra=%x md=%x cod=%x hlp=%x rvd=%x sub=%x pos=%x srd=%x c=%x cp=%x)",
748 i_aspect, (p[0] & 0x10) >> 4, (p[0] & 0x20) >> 5,
749 (p[0] & 0x40) >> 6, (p[0] & 0x80) >> 7, p[1] & 0x01,
750 (p[1] >> 1) & 3, (p[1] & 0x08) >> 3, (p[1] & 0x10) >> 4,
751 (p[1] & 0x20) >> 5 );
755 static int InitTelx( demux_t *p_demux )
757 demux_sys_t *p_sys = p_demux->p_sys;
758 vbi_raw_decoder_init( &p_sys->rd_telx );
760 p_sys->rd_telx.scanning = 625;
761 p_sys->rd_telx.sampling_format = VBI_PIXFMT_UYVY;
762 p_sys->rd_telx.sampling_rate = 13.5e6;
763 p_sys->rd_telx.bytes_per_line = 720 * 2;
764 p_sys->rd_telx.offset = 9.5e-6 * 13.5e6;
766 p_sys->rd_telx.start[0] = p_sys->i_telx_line + 1;
767 p_sys->rd_telx.count[0] = p_sys->i_telx_count;
768 p_sys->rd_telx.start[1] = p_sys->i_telx_line + 1 + 313;
769 p_sys->rd_telx.count[1] = p_sys->i_telx_count;
771 p_sys->rd_telx.interlaced = FALSE;
772 p_sys->rd_telx.synchronous = TRUE;
774 if ( vbi_raw_decoder_add_services( &p_sys->rd_telx, VBI_SLICED_TELETEXT_B,
777 msg_Warn( p_demux, "cannot initialize zvbi for Teletext" );
778 vbi_raw_decoder_destroy ( &p_sys->rd_telx );
782 p_sys->p_telx_buffer = malloc( p_sys->i_telx_count * p_sys->i_width * 4 );
783 if( !p_sys->p_telx_buffer )
785 vbi_raw_decoder_destroy ( &p_sys->rd_telx );
791 static int DecodeTelx( demux_t *p_demux )
793 demux_sys_t *p_sys = p_demux->p_sys;
794 vbi_sliced p_sliced[p_sys->i_telx_count * 2];
795 int i_nb_slices = vbi_raw_decode( &p_sys->rd_telx, p_sys->p_telx_buffer,
800 /* 3, 7, 11, 15, etc. */
801 int i_nb_slices_rounded = 3 + (i_nb_slices / 4) * 4;
804 block_t *p_block = block_New( p_demux,
805 1 + i_nb_slices_rounded * 46 );
806 if( unlikely( !p_block ) )
808 p_block->p_buffer[0] = 0x10; /* FIXME ? data_identifier */
809 p = p_block->p_buffer + 1;
811 for ( i = 0; i < i_nb_slices; i++ )
813 int i_line = p_sliced[i].line;
814 p[0] = 0x3; /* FIXME data_unit_id == subtitles */
815 p[1] = 0x2c; /* data_unit_length */
816 /* reserved | field_parity (kind of inverted) | line */
817 p[2] = 0xc0 | (i_line > 313 ? 0 : 0x20) | (i_line % 313);
818 p[3] = 0xe4; /* framing code */
819 for ( int j = 0; j < 42; j++ )
820 p[4 + j] = vbi_rev8( p_sliced[i].data[j] );
825 for ( ; i < i_nb_slices_rounded; i++ )
829 memset( p + 2, 0xff, 44 );
833 p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
834 es_out_Send( p_demux->out, p_sys->p_es_telx, p_block );
839 static int InitAudio( demux_t *p_demux, sdi_audio_t *p_audio )
841 demux_sys_t *p_sys = p_demux->p_sys;
844 msg_Dbg( p_demux, "starting audio %u/%u rate:%u delay:%d", p_audio->i_group,
845 p_audio->i_pair, p_audio->i_rate, p_audio->i_delay );
847 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
848 fmt.i_id = p_audio->i_id;
849 fmt.audio.i_channels = 2;
850 fmt.audio.i_physical_channels = 6;
851 fmt.audio.i_original_channels = 6;
852 fmt.audio.i_rate = p_audio->i_rate;
853 fmt.audio.i_bitspersample = 16;
854 fmt.audio.i_blockalign = fmt.audio.i_channels *
855 fmt.audio.i_bitspersample / 8;
856 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
857 fmt.audio.i_bitspersample;
858 p_audio->p_es = es_out_Add( p_demux->out, &fmt );
860 p_audio->i_nb_samples = p_audio->i_rate * p_sys->i_frame_rate_base
861 / p_sys->i_frame_rate;
862 p_audio->i_max_samples = (float)p_audio->i_nb_samples *
863 (1. + SAMPLERATE_TOLERANCE);
865 p_audio->p_buffer = malloc( p_audio->i_max_samples * sizeof(int16_t) * 2 );
866 p_audio->i_left_samples = p_audio->i_right_samples = 0;
867 p_audio->i_block_number = 0;
869 if( unlikely( !p_audio->p_buffer ) )
874 /* Fast and efficient linear resampling routine */
875 static void ResampleAudio( int16_t *p_out, int16_t *p_in,
876 unsigned int i_out, unsigned int i_in )
878 unsigned int i_remainder = 0;
879 float f_last_sample = (float)*p_in / 32768.0;
885 for ( unsigned int i = 1; i < i_in; i++ )
887 float f_in = (float)*p_in / 32768.0;
888 while ( i_remainder < i_out )
890 float f_out = f_last_sample;
891 f_out += (f_in - f_last_sample) * i_remainder / i_out;
892 if ( f_out >= 1.0 ) *p_out = 32767;
893 else if ( f_out < -1.0 ) *p_out = -32768;
894 else *p_out = f_out * 32768.0;
899 f_last_sample = f_in;
901 i_remainder -= i_out;
905 static int DecodeAudio( demux_t *p_demux, sdi_audio_t *p_audio )
907 demux_sys_t *p_sys = p_demux->p_sys;
911 if ( p_audio->p_buffer == NULL )
913 if ( !p_audio->i_left_samples && !p_audio->i_right_samples )
915 msg_Warn( p_demux, "no audio %u/%u", p_audio->i_group,
919 if ( p_audio->i_left_samples <
920 (float)p_audio->i_nb_samples * (1. - SAMPLERATE_TOLERANCE) ||
921 p_audio->i_left_samples >
922 (float)p_audio->i_nb_samples * (1. + SAMPLERATE_TOLERANCE) )
925 "left samplerate out of tolerance for audio %u/%u (%u vs. %u)",
926 p_audio->i_group, p_audio->i_pair,
927 p_audio->i_left_samples, p_audio->i_nb_samples );
930 if ( p_audio->i_right_samples <
931 (float)p_audio->i_nb_samples * (1. - SAMPLERATE_TOLERANCE) ||
932 p_audio->i_right_samples >
933 (float)p_audio->i_nb_samples * (1. + SAMPLERATE_TOLERANCE) )
936 "right samplerate out of tolerance for audio %u/%u (%u vs. %u)",
937 p_audio->i_group, p_audio->i_pair,
938 p_audio->i_right_samples, p_audio->i_nb_samples );
942 p_block = block_New( p_demux, p_audio->i_nb_samples * sizeof(int16_t) * 2 );
943 if( unlikely( !p_block ) )
945 p_block->i_dts = p_block->i_pts = p_sys->i_next_date
946 + (mtime_t)p_audio->i_delay * INT64_C(1000000) / p_audio->i_rate;
947 p_output = (int16_t *)p_block->p_buffer;
949 if ( p_audio->i_left_samples == p_audio->i_nb_samples &&
950 p_audio->i_right_samples == p_audio->i_nb_samples )
951 vlc_memcpy( p_output, p_audio->p_buffer,
952 p_audio->i_nb_samples * sizeof(int16_t) * 2 );
955 ResampleAudio( p_output, p_audio->p_buffer,
956 p_audio->i_nb_samples, p_audio->i_left_samples );
958 ResampleAudio( p_output + 1, p_audio->p_buffer + 1,
959 p_audio->i_nb_samples, p_audio->i_right_samples );
962 es_out_Send( p_demux->out, p_audio->p_es, p_block );
966 static int DecodeFrame( demux_t *p_demux )
968 demux_sys_t *p_sys = p_demux->p_sys;
972 DecodeWSS( p_demux );
974 if ( p_sys->i_height == 576 )
976 /* For PAL, erase first half of line 23, last half of line 623,
977 * and line 624 ; no need to erase chrominance */
978 vlc_memset( p_sys->p_y, 0, p_sys->i_width / 2 );
979 vlc_memset( p_sys->p_y + p_sys->i_width * 574 + p_sys->i_width / 2,
980 0, p_sys->i_width * 3 / 2 );
984 if ( p_sys->i_telx_count )
985 if ( DecodeTelx( p_demux ) != VLC_SUCCESS )
988 for ( int i = 0; i < MAX_AUDIOS; i++ )
990 if ( p_sys->p_audios[i].i_group && p_sys->p_audios[i].p_es != NULL )
991 if( DecodeAudio( p_demux, &p_sys->p_audios[i] ) != VLC_SUCCESS )
995 DecodeVideo( p_demux );
997 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_next_date );
998 p_sys->i_next_date += p_sys->i_incr;
1000 if( NewFrame( p_demux ) != VLC_SUCCESS )
1006 /*****************************************************************************
1007 * SDI syntax parsing stuff
1008 *****************************************************************************/
1009 #define FIELD_1_VBLANK_EAV 0xB6
1010 #define FIELD_1_VBLANK_SAV 0xAB
1011 #define FIELD_1_ACTIVE_EAV 0x9D
1012 #define FIELD_1_ACTIVE_SAV 0x80
1013 #define FIELD_2_VBLANK_EAV 0xF1
1014 #define FIELD_2_VBLANK_SAV 0xEC
1015 #define FIELD_2_ACTIVE_EAV 0xDA
1016 #define FIELD_2_ACTIVE_SAV 0xC7
1018 static const uint8_t *FindReferenceCode( uint8_t i_code,
1019 const uint8_t *p_parser,
1020 const uint8_t *p_end )
1022 while ( p_parser <= p_end - 5 )
1024 if ( p_parser[0] == 0xff && p_parser[1] == 0x3 && p_parser[2] == 0x0
1025 && p_parser[3] == 0x0 && p_parser[4] == i_code )
1033 static const uint8_t *CountReference( unsigned int *pi_count, uint8_t i_code,
1034 const uint8_t *p_parser,
1035 const uint8_t *p_end )
1037 const uint8_t *p_tmp = FindReferenceCode( i_code, p_parser, p_end );
1038 if ( p_tmp == NULL )
1040 *pi_count += p_end - p_parser;
1043 *pi_count += p_tmp - p_parser;
1047 static const uint8_t *GetLine( demux_t *p_demux, const uint8_t **pp_parser,
1048 const uint8_t *p_end )
1050 demux_sys_t *p_sys = p_demux->p_sys;
1051 unsigned int i_total_size = p_sys->i_anc_size + p_sys->i_active_size;
1052 const uint8_t *p_tmp;
1054 if ( p_sys->i_line_buffer )
1056 unsigned int i_remaining = i_total_size - p_sys->i_line_buffer;
1057 vlc_memcpy( p_sys->p_line_buffer + p_sys->i_line_buffer,
1058 *pp_parser, i_remaining );
1059 *pp_parser += i_remaining;
1060 p_sys->i_line_buffer = 0;
1062 return p_sys->p_line_buffer;
1065 if ( p_end - *pp_parser < (int)i_total_size )
1067 vlc_memcpy( p_sys->p_line_buffer, *pp_parser,
1068 p_end - *pp_parser );
1069 p_sys->i_line_buffer = p_end - *pp_parser;
1074 *pp_parser += i_total_size;
1078 #define U (uint16_t)((p_line[0]) | ((p_line[1] & 0x3) << 8))
1079 #define Y1 (uint16_t)((p_line[1] >> 2) | ((p_line[2] & 0xf) << 6))
1080 #define V (uint16_t)((p_line[2] >> 4) | ((p_line[3] & 0x3f) << 4))
1081 #define Y2 (uint16_t)((p_line[3] >> 6) | (p_line[4] << 2))
1083 static void UnpackVBI( const uint8_t *p_line, unsigned int i_size,
1086 const uint8_t *p_end = p_line + i_size;
1088 while ( p_line < p_end )
1090 *p_dest++ = (U + 2) / 4;
1091 *p_dest++ = (Y1 + 2) / 4;
1092 *p_dest++ = (V + 2) / 4;
1093 *p_dest++ = (Y2 + 2) / 4;
1098 /* For lines 0 [4] or 1 [4] */
1099 static void Unpack01( const uint8_t *p_line, unsigned int i_size,
1100 uint8_t *p_y, uint8_t *p_u, uint8_t *p_v )
1102 const uint8_t *p_end = p_line + i_size;
1104 while ( p_line < p_end )
1106 *p_u++ = (U + 2) / 4;
1107 *p_y++ = (Y1 + 2) / 4;
1108 *p_v++ = (V + 2) / 4;
1109 *p_y++ = (Y2 + 2) / 4;
1114 /* For lines 2 [4] */
1115 static void Unpack2( const uint8_t *p_line, unsigned int i_size,
1116 uint8_t *p_y, uint8_t *p_u, uint8_t *p_v )
1118 const uint8_t *p_end = p_line + i_size;
1120 while ( p_line < p_end )
1126 *p_y++ = (Y1 + 2) / 4;
1130 *p_y++ = (Y2 + 2) / 4;
1135 /* For lines 3 [4] */
1136 static void Unpack3( const uint8_t *p_line, unsigned int i_size,
1137 uint8_t *p_y, uint8_t *p_u, uint8_t *p_v )
1139 const uint8_t *p_end = p_line + i_size;
1141 while ( p_line < p_end )
1145 tmp += 3 * (U + 2) / 4;
1147 *p_y++ = (Y1 + 2) / 4;
1149 tmp += 3 * (V + 2) / 4;
1151 *p_y++ = (Y2 + 2) / 4;
1161 #define A0 (uint16_t)((p_anc[0]) | ((p_anc[1] & 0x3) << 8))
1162 #define A1 (uint16_t)((p_anc[1] >> 2) | ((p_anc[2] & 0xf) << 6))
1163 #define A2 (uint16_t)((p_anc[2] >> 4) | ((p_anc[3] & 0x3f) << 4))
1164 #define A3 (uint16_t)((p_anc[3] >> 6) | (p_anc[4] << 2))
1166 static void UnpackAnc( const uint8_t *p_anc, unsigned int i_size,
1169 const uint8_t *p_end = p_anc + i_size;
1171 while ( p_anc <= p_end - 5 )
1186 static int HasAncillary( const uint8_t *p_anc )
1188 return ( (p_anc[0] == 0x0 && p_anc[1] == 0xfc && p_anc[2] == 0xff
1189 && (p_anc[3] & 0x3f) == 0x3f) );
1192 static void HandleAudioData( demux_t *p_demux, const uint16_t *p_anc,
1193 uint8_t i_data_count, uint8_t i_group,
1194 uint8_t i_block_number )
1196 demux_sys_t *p_sys = p_demux->p_sys;
1198 if ( i_data_count % 3 )
1200 msg_Warn( p_demux, "malformed audio data for group %u", i_group );
1204 for ( int i = 0; i < MAX_AUDIOS; i++ )
1206 sdi_audio_t *p_audio = &p_sys->p_audios[i];
1207 if ( p_audio->i_group == i_group )
1209 const uint16_t *x = p_anc;
1211 /* SMPTE 272M says that when parsing a frame, if an audio config
1212 * structure is present we will encounter it first. Otherwise
1213 * it is assumed to be 48 kHz. */
1214 if ( p_audio->p_es == NULL )
1216 p_audio->i_rate = 48000;
1217 p_audio->i_delay = 0;
1218 if( InitAudio( p_demux, p_audio ) != VLC_SUCCESS )
1222 if ( i_block_number )
1224 if ( p_audio->i_block_number + 1 != i_block_number )
1226 "audio data block discontinuity (%"PRIu8"->%"PRIu8") for group %"PRIu8,
1227 p_audio->i_block_number, i_block_number,
1229 if ( i_block_number == 0xff )
1230 p_audio->i_block_number = 0;
1232 p_audio->i_block_number = i_block_number;
1235 while ( x < p_anc + i_data_count )
1237 if ( ((*x & 0x4) && p_audio->i_pair == 2)
1238 || (!(*x & 0x4) && p_audio->i_pair == 1) )
1240 uint32_t i_tmp = (uint32_t)((x[0] & 0x1f1) >> 3)
1241 | ((x[1] & 0x1ff) << 6)
1242 | ((x[2] & 0x1f) << 15);
1245 i_sample = i_tmp | 0xfff00000;
1251 if ( p_audio->i_right_samples < p_audio->i_max_samples )
1252 p_audio->p_buffer[2 * p_audio->i_right_samples
1253 + 1] = (i_sample + 8) / 16;
1254 p_audio->i_right_samples++;
1258 if ( p_audio->i_left_samples < p_audio->i_max_samples )
1259 p_audio->p_buffer[2 * p_audio->i_left_samples]
1260 = (i_sample + 8) / 16;
1261 p_audio->i_left_samples++;
1270 static void HandleAudioConfig( demux_t *p_demux, const uint16_t *p_anc,
1271 uint8_t i_data_count, uint8_t i_group )
1273 demux_sys_t *p_sys = p_demux->p_sys;
1275 if ( i_data_count != 18 )
1277 msg_Warn( p_demux, "malformed audio config for group %u", i_group );
1281 for ( int i = 0; i < MAX_AUDIOS; i++ )
1283 sdi_audio_t *p_audio = &p_sys->p_audios[i];
1284 if ( p_audio->i_group == i_group && p_audio->p_es == NULL )
1286 unsigned int i_rate;
1288 if ( p_audio->i_pair == 2 )
1290 i_rate = (p_anc[2] & 0xe0) >> 5;
1291 if ( p_anc[7] & 0x1 )
1293 uint32_t i_tmp = ((p_anc[7] & 0x1fe) >> 1)
1294 | ((p_anc[8] & 0x1ff) << 8)
1295 | ((p_anc[9] & 0x1ff) << 17);
1296 if ( p_anc[9] & 0x80 )
1297 p_audio->i_delay = i_tmp | 0xfc000000;
1299 p_audio->i_delay = i_tmp;
1301 if ( p_anc[13] & 0x1 )
1302 msg_Warn( p_demux, "asymetric audio is not supported" );
1306 i_rate = (p_anc[2] & 0xe) >> 1;
1307 if ( p_anc[4] & 0x1 )
1309 uint32_t i_tmp = ((p_anc[4] & 0x1fe) >> 1)
1310 | ((p_anc[5] & 0x1ff) << 8)
1311 | ((p_anc[6] & 0x1ff) << 17);
1312 if ( p_anc[6] & 0x80 )
1313 p_audio->i_delay = i_tmp | 0xfc000000;
1315 p_audio->i_delay = i_tmp;
1317 if ( p_anc[10] & 0x1 )
1318 msg_Warn( p_demux, "asymetric audio is not supported" );
1323 case 0: p_audio->i_rate = 48000; break;
1324 case 1: p_audio->i_rate = 44100; break;
1325 case 2: p_audio->i_rate = 32000; break;
1327 msg_Warn( p_demux, "unknown rate for audio %u/%u (%u)",
1328 i_group, p_sys->p_audios[i].i_pair, i_rate );
1332 if( InitAudio( p_demux, p_audio ) != VLC_SUCCESS )
1339 * Ancillary packet structure:
1340 * byte 0: Ancillary Data Flag (0)
1341 * byte 1: Ancillary Data Flag (0x3ff)
1342 * byte 2: Ancillary Data Flag (0x3ff)
1343 * byte 3: Data ID (2 high order bits = parity)
1344 * byte 4: Data Block Number 1-255 or 0=unknown (if DID < 0x80)
1345 * or Secondary Data ID (if DID >= 0x80)
1346 * byte 5: Data Count (10 bits)
1347 * byte 6+DC: Checksum
1349 static void HandleAncillary( demux_t *p_demux, const uint16_t *p_anc,
1350 unsigned int i_size )
1352 uint8_t i_data_count;
1355 || p_anc[0] != 0x0 || p_anc[1] != 0x3ff || p_anc[2] != 0x3ff )
1358 i_data_count = p_anc[5] & 0xff;
1359 if ( i_size - 6 < i_data_count )
1361 msg_Warn( p_demux, "malformed ancillary packet (size %u > %u)",
1362 i_data_count, i_size - 6 );
1366 switch ( p_anc[3] ) /* Data ID */
1369 HandleAudioData( p_demux, p_anc + 6, i_data_count, 1, p_anc[4] & 0xff );
1372 HandleAudioData( p_demux, p_anc + 6, i_data_count, 2, p_anc[4] & 0xff );
1375 HandleAudioData( p_demux, p_anc + 6, i_data_count, 3, p_anc[4] & 0xff );
1378 HandleAudioData( p_demux, p_anc + 6, i_data_count, 4, p_anc[4] & 0xff );
1382 HandleAudioConfig( p_demux, p_anc + 6, i_data_count, 1 );
1385 HandleAudioConfig( p_demux, p_anc + 6, i_data_count, 2 );
1388 HandleAudioConfig( p_demux, p_anc + 6, i_data_count, 3 );
1391 HandleAudioConfig( p_demux, p_anc + 6, i_data_count, 4 );
1394 /* Extended data packets, same order */
1403 case 0x88: /* non-conforming ANC packet */
1406 while ( i_size >= 7 && (p_anc[0] != 0x0 || p_anc[1] != 0x3ff
1407 || p_anc[2] != 0x3ff) )
1413 HandleAncillary( p_demux, p_anc, i_size );
1417 return HandleAncillary( p_demux, p_anc + i_data_count + 7,
1418 i_size - i_data_count - 7 );
1422 static int HandleSDBuffer( demux_t *p_demux, uint8_t *p_buffer,
1423 unsigned int i_buffer_size )
1425 demux_sys_t *p_sys = p_demux->p_sys;
1426 const uint8_t *p_parser = p_buffer;
1427 const uint8_t *p_end = p_parser + i_buffer_size;
1428 const uint8_t *p_line;
1430 if ( p_sys->i_state != STATE_SYNC
1431 && p_sys->i_last_state_change < mdate() - RESYNC_TIMEOUT )
1433 p_sys->i_state = STATE_NOSYNC;
1434 p_sys->i_last_state_change = mdate();
1435 return VLC_EGENERIC;
1438 switch ( p_sys->i_state )
1442 p_parser = FindReferenceCode( FIELD_2_VBLANK_SAV, p_parser, p_end );
1443 if ( p_parser == NULL )
1445 p_sys->i_state = STATE_STARTSYNC;
1446 p_sys->i_last_state_change = mdate();
1448 case STATE_STARTSYNC:
1449 p_parser = FindReferenceCode( FIELD_1_VBLANK_EAV, p_parser, p_end );
1450 if ( p_parser == NULL )
1452 p_sys->i_anc_size = 0;
1453 p_sys->i_state = STATE_ANCSYNC;
1454 p_sys->i_last_state_change = mdate();
1457 p_parser = CountReference( &p_sys->i_anc_size,
1458 FIELD_1_VBLANK_SAV, p_parser, p_end );
1459 if ( p_parser == NULL )
1461 p_sys->i_active_size = 0;
1462 p_sys->i_state = STATE_LINESYNC;
1463 p_sys->i_last_state_change = mdate();
1465 case STATE_LINESYNC:
1466 p_parser = CountReference( &p_sys->i_active_size,
1467 FIELD_1_VBLANK_EAV, p_parser, p_end );
1468 if ( p_parser == NULL )
1470 p_sys->i_picture_size = p_sys->i_anc_size + p_sys->i_active_size;
1471 p_sys->i_state = STATE_ACTIVESYNC;
1472 p_sys->i_last_state_change = mdate();
1474 case STATE_ACTIVESYNC:
1475 p_parser = CountReference( &p_sys->i_picture_size,
1476 FIELD_1_ACTIVE_EAV, p_parser, p_end );
1477 if ( p_parser == NULL )
1479 p_sys->i_line_offset = p_sys->i_picture_size
1480 / (p_sys->i_anc_size + p_sys->i_active_size);
1481 p_sys->i_state = STATE_VBLANKSYNC;
1482 p_sys->i_last_state_change = mdate();
1484 case STATE_VBLANKSYNC:
1485 p_parser = CountReference( &p_sys->i_picture_size,
1486 FIELD_2_ACTIVE_EAV, p_parser, p_end );
1487 if ( p_parser == NULL )
1489 p_sys->i_state = STATE_PICSYNC;
1490 p_sys->i_last_state_change = mdate();
1493 p_parser = CountReference( &p_sys->i_picture_size,
1494 FIELD_1_VBLANK_EAV, p_parser, p_end );
1495 if ( p_parser == NULL )
1498 if ( p_sys->i_picture_size
1499 % (p_sys->i_anc_size + p_sys->i_active_size) )
1501 msg_Warn( p_demux, "wrong picture size (anc=%d active=%d total=%d offset=%d), syncing",
1502 p_sys->i_anc_size, p_sys->i_active_size,
1503 p_sys->i_picture_size, p_sys->i_line_offset + 1 );
1504 p_sys->i_state = STATE_NOSYNC;
1505 p_sys->i_last_state_change = mdate();
1509 p_sys->i_nb_lines = p_sys->i_picture_size
1510 / (p_sys->i_anc_size + p_sys->i_active_size);
1511 InitVideo( p_demux );
1513 "acquired sync, anc=%d active=%d lines=%d offset=%d",
1514 p_sys->i_anc_size, p_sys->i_active_size,
1515 p_sys->i_nb_lines, p_sys->i_line_offset + 1 );
1516 p_sys->i_state = STATE_SYNC;
1517 if( StartDecode( p_demux ) != VLC_SUCCESS )
1519 StopDecode( p_demux );
1522 p_sys->i_current_line = 0;
1523 p_sys->p_line_buffer = malloc( p_sys->i_anc_size
1524 + p_sys->i_active_size );
1525 if( !p_sys->p_line_buffer )
1527 StopDecode( p_demux );
1530 p_sys->i_line_buffer = 0;
1533 while ( (p_line = GetLine( p_demux, &p_parser, p_end )) != NULL )
1535 bool b_field = p_sys->b_hd ? false :
1536 (p_sys->i_current_line >= p_sys->i_nb_lines / 2);
1537 unsigned int i_field_height = p_sys->b_hd ? p_sys->i_height :
1538 p_sys->i_height / 2;
1539 unsigned int i_field_line = b_field ?
1540 p_sys->i_current_line - (p_sys->i_nb_lines + 1) / 2 :
1541 p_sys->i_current_line;
1542 bool b_vbi = i_field_line < p_sys->i_line_offset ||
1543 i_field_line >= p_sys->i_line_offset + i_field_height;
1544 unsigned int anc = p_sys->i_anc_size;
1546 if ( p_line[0] != 0xff || p_line[1] != 0x3
1547 || p_line[2] != 0x0 || p_line[3] != 0x0
1548 || p_line[anc+0] != 0xff || p_line[anc+1] != 0x3
1549 || p_line[anc+2] != 0x0 || p_line[anc+3] != 0x0
1550 || (!b_field && b_vbi &&
1551 (p_line[4] != FIELD_1_VBLANK_EAV ||
1552 p_line[anc+4] != FIELD_1_VBLANK_SAV))
1553 || (!b_field && !b_vbi &&
1554 (p_line[4] != FIELD_1_ACTIVE_EAV ||
1555 p_line[anc+4] != FIELD_1_ACTIVE_SAV))
1556 || (b_field && b_vbi &&
1557 (p_line[4] != FIELD_2_VBLANK_EAV ||
1558 p_line[anc+4] != FIELD_2_VBLANK_SAV))
1559 || (b_field && !b_vbi &&
1560 (p_line[4] != FIELD_2_ACTIVE_EAV ||
1561 p_line[anc+4] != FIELD_2_ACTIVE_SAV)) )
1563 msg_Warn( p_demux, "lost sync line:%u SAV:%x EAV:%x",
1564 p_sys->i_current_line + 1, p_line[4], p_line[anc+4] );
1565 StopDecode( p_demux );
1566 p_sys->i_state = STATE_NOSYNC;
1567 p_sys->i_last_state_change = mdate();
1571 if ( HasAncillary( p_line + 5 ) )
1574 unsigned int i_anc_words = (p_sys->i_anc_size - 5) * 4 / 5;
1575 uint16_t p_anc[i_anc_words];
1576 UnpackAnc( p_line + 5, p_sys->i_anc_size - 5, p_anc );
1577 HandleAncillary( p_demux, p_anc, i_anc_words );
1582 unsigned int i_active_field_line = i_field_line
1583 - p_sys->i_line_offset;
1584 unsigned int i_active_line = b_field
1585 + i_active_field_line * 2;
1586 if ( !(i_active_field_line % 2) && !b_field )
1587 Unpack01( p_line + anc + 5, p_sys->i_active_size - 5,
1588 p_sys->p_y + p_sys->i_width * i_active_line,
1589 p_sys->p_u + (p_sys->i_width / 2)
1590 * (i_active_line / 2),
1591 p_sys->p_v + (p_sys->i_width / 2)
1592 * (i_active_line / 2) );
1593 else if ( !(i_active_field_line % 2) )
1594 Unpack01( p_line + anc + 5, p_sys->i_active_size - 5,
1595 p_sys->p_y + p_sys->i_width * i_active_line,
1596 p_sys->p_u + (p_sys->i_width / 2)
1597 * (i_active_line / 2 + 1),
1598 p_sys->p_v + (p_sys->i_width / 2)
1599 * (i_active_line / 2 + 1) );
1600 else if ( !b_field )
1601 Unpack2( p_line + anc + 5, p_sys->i_active_size - 5,
1602 p_sys->p_y + p_sys->i_width * i_active_line,
1603 p_sys->p_u + (p_sys->i_width / 2)
1604 * (i_active_line / 2 - 1),
1605 p_sys->p_v + (p_sys->i_width / 2)
1606 * (i_active_line / 2 - 1) );
1608 Unpack3( p_line + anc + 5, p_sys->i_active_size - 5,
1609 p_sys->p_y + p_sys->i_width * i_active_line,
1610 p_sys->p_u + (p_sys->i_width / 2)
1611 * (i_active_line / 2),
1612 p_sys->p_v + (p_sys->i_width / 2)
1613 * (i_active_line / 2) );
1615 if ( p_sys->b_vbi && p_sys->i_height == 576
1616 && p_sys->i_current_line == p_sys->i_line_offset )
1618 /* Line 23 is half VBI, half active */
1619 UnpackVBI( p_line + anc + 5, p_sys->i_active_size - 5,
1620 p_sys->p_wss_buffer );
1623 else if ( p_sys->b_vbi && p_sys->i_telx_count &&
1624 i_field_line >= p_sys->i_telx_line &&
1625 i_field_line < p_sys->i_telx_line
1626 + p_sys->i_telx_count )
1628 UnpackVBI( p_line + anc + 5, p_sys->i_active_size - 5,
1629 &p_sys->p_telx_buffer[(i_field_line
1630 - p_sys->i_telx_line + b_field * p_sys->i_telx_count)
1631 * p_sys->i_width * 2] );
1633 else if ( b_vbi && HasAncillary( p_line + anc + 5 ) )
1636 unsigned int i_anc_words = (p_sys->i_active_size - 5) * 4 / 5;
1637 uint16_t p_anc[i_anc_words];
1638 UnpackAnc( p_line + 5, p_sys->i_active_size - 5,
1640 HandleAncillary( p_demux, p_anc, i_anc_words );
1643 p_sys->i_current_line++;
1644 if ( p_sys->i_current_line == p_sys->i_nb_lines )
1646 p_sys->i_current_line %= p_sys->i_nb_lines;
1647 if( DecodeFrame( p_demux ) != VLC_SUCCESS )
1648 return VLC_EGENERIC;
1657 /*****************************************************************************
1658 * Low-level device stuff
1659 *****************************************************************************/
1662 static int ReadULSysfs( const char *psz_fmt, unsigned int i_link )
1664 char psz_file[MAXLEN], psz_data[MAXLEN];
1668 unsigned int i_data;
1670 snprintf( psz_file, sizeof(psz_file) - 1, psz_fmt, i_link );
1672 if ( (i_fd = vlc_open( psz_file, O_RDONLY )) < 0 )
1675 i_ret = read( i_fd, psz_data, sizeof(psz_data) );
1681 i_data = strtoul( psz_data, &psz_tmp, 0 );
1682 if ( *psz_tmp != '\n' )
1688 static ssize_t WriteULSysfs( const char *psz_fmt, unsigned int i_link,
1689 unsigned int i_buf )
1691 char psz_file[MAXLEN], psz_data[MAXLEN];
1695 snprintf( psz_file, sizeof(psz_file) -1, psz_fmt, i_link );
1697 snprintf( psz_data, sizeof(psz_data) -1, "%u\n", i_buf );
1699 if ( (i_fd = vlc_open( psz_file, O_WRONLY )) < 0 )
1702 i_ret = write( i_fd, psz_data, strlen(psz_data) + 1 );
1707 static int InitCapture( demux_t *p_demux )
1709 demux_sys_t *p_sys = p_demux->p_sys;
1710 const int i_page_size = getpagesize();
1711 unsigned int i_bufmemsize;
1713 char psz_dev[MAXLEN];
1715 /* 10-bit mode or nothing */
1716 if ( WriteULSysfs( SDI_MODE_FILE, p_sys->i_link, SDI_CTL_MODE_10BIT ) < 0 )
1718 msg_Err( p_demux, "couldn't write file " SDI_MODE_FILE, p_sys->i_link );
1719 return VLC_EGENERIC;
1722 if ( (i_ret = ReadULSysfs( SDI_BUFFERS_FILE, p_sys->i_link )) < 0 )
1724 msg_Err( p_demux, "couldn't read file " SDI_BUFFERS_FILE,
1726 return VLC_EGENERIC;
1728 p_sys->i_buffers = i_ret;
1729 p_sys->i_current_buffer = 0;
1731 if ( (i_ret = ReadULSysfs( SDI_BUFSIZE_FILE, p_sys->i_link )) < 0 )
1733 msg_Err( p_demux, "couldn't read file " SDI_BUFSIZE_FILE,
1735 return VLC_EGENERIC;
1737 p_sys->i_buffer_size = i_ret;
1738 if ( p_sys->i_buffer_size % 20 )
1740 msg_Err( p_demux, "buffer size must be a multiple of 20" );
1741 return VLC_EGENERIC;
1744 snprintf( psz_dev, sizeof(psz_dev) - 1, SDI_DEVICE, p_sys->i_link );
1745 if ( (p_sys->i_fd = vlc_open( psz_dev, O_RDONLY ) ) < 0 )
1747 msg_Err( p_demux, "couldn't open device %s", psz_dev );
1748 return VLC_EGENERIC;
1751 i_bufmemsize = ((p_sys->i_buffer_size + i_page_size - 1) / i_page_size)
1753 p_sys->pp_buffers = malloc( p_sys->i_buffers * sizeof(uint8_t *) );
1754 if( !p_sys->pp_buffers )
1757 for ( unsigned int i = 0; i < p_sys->i_buffers; i++ )
1759 if ( (p_sys->pp_buffers[i] = mmap( NULL, p_sys->i_buffer_size,
1760 PROT_READ, MAP_SHARED, p_sys->i_fd,
1761 i * i_bufmemsize )) == MAP_FAILED )
1763 msg_Err( p_demux, "couldn't mmap(%d): %m", i );
1764 free( p_sys->pp_buffers );
1765 return VLC_EGENERIC;
1772 static void CloseCapture( demux_t *p_demux )
1774 demux_sys_t *p_sys = p_demux->p_sys;
1776 StopDecode( p_demux );
1777 for ( unsigned int i = 0; i < p_sys->i_buffers; i++ )
1778 munmap( p_sys->pp_buffers[i], p_sys->i_buffer_size );
1779 close( p_sys->i_fd );
1780 free( p_sys->pp_buffers );
1783 static int Capture( demux_t *p_demux )
1785 demux_sys_t *p_sys = p_demux->p_sys;
1788 pfd.fd = p_sys->i_fd;
1789 pfd.events = POLLIN | POLLPRI;
1791 if ( poll( &pfd, 1, READ_TIMEOUT ) < 0 )
1793 msg_Warn( p_demux, "couldn't poll(): %m" );
1794 return VLC_EGENERIC;
1797 if ( pfd.revents & POLLPRI )
1801 if ( ioctl( p_sys->i_fd, SDI_IOC_RXGETEVENTS, &i_val ) < 0 )
1802 msg_Warn( p_demux, "couldn't SDI_IOC_RXGETEVENTS %m" );
1805 if ( i_val & SDI_EVENT_RX_BUFFER )
1806 msg_Warn( p_demux, "driver receive buffer queue overrun" );
1807 if ( i_val & SDI_EVENT_RX_FIFO )
1808 msg_Warn( p_demux, "onboard receive FIFO overrun");
1809 if ( i_val & SDI_EVENT_RX_CARRIER )
1810 msg_Warn( p_demux, "carrier status change");
1813 p_sys->i_next_date += CLOCK_GAP;
1816 if ( pfd.revents & POLLIN )
1820 if ( ioctl( p_sys->i_fd, SDI_IOC_DQBUF, p_sys->i_current_buffer ) < 0 )
1822 msg_Warn( p_demux, "couldn't SDI_IOC_DQBUF %m" );
1823 return VLC_EGENERIC;
1826 i_ret = HandleSDBuffer( p_demux,
1827 p_sys->pp_buffers[p_sys->i_current_buffer],
1828 p_sys->i_buffer_size );
1830 if ( ioctl( p_sys->i_fd, SDI_IOC_QBUF, p_sys->i_current_buffer ) < 0 )
1832 msg_Warn( p_demux, "couldn't SDI_IOC_QBUF %m" );
1833 return VLC_EGENERIC;
1836 if ( i_ret == VLC_SUCCESS )
1838 p_sys->i_current_buffer++;
1839 p_sys->i_current_buffer %= p_sys->i_buffers;
1843 /* Reference codes do not start on a multiple of 5. This sometimes
1844 * happen. We really don't want to allow this. */
1845 msg_Warn( p_demux, "resetting board" );
1846 CloseCapture( p_demux );
1847 InitCapture( p_demux );