📄 v4l.c
字号:
if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 ) { msg_Err( p_input, "cannot set audio (%s)", strerror( errno ) ); goto vdev_failed; } } } /* establish basic params with input and norm before feeling width * or height */ if( p_sys->b_mjpeg ) { struct quicktime_mjpeg_app1 *p_app1; int32_t i_offset; if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 ) { msg_Err( p_input, "cannot get mjpeg params (%s)", strerror( errno ) ); goto vdev_failed; } mjpeg.input = p_sys->i_channel; mjpeg.norm = p_sys->i_norm; mjpeg.decimation = p_sys->i_decimation; if( p_sys->i_width ) mjpeg.img_width = p_sys->i_width / p_sys->i_decimation; if( p_sys->i_height ) mjpeg.img_height = p_sys->i_height / p_sys->i_decimation; /* establish Quicktime APP1 marker while we are here */ mjpeg.APPn = 1; mjpeg.APP_len = 40; /* aligned */ p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data; p_app1->i_reserved = 0; p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' ); p_app1->i_field_size = 0; p_app1->i_padded_field_size = 0; p_app1->i_next_field = 0; /* XXX WARNING XXX */ /* these's nothing magic about these values. We are dangerously * assuming the encoder card is encoding mjpeg-a and is not throwing * in marker tags we aren't expecting. It's bad enough we have to * search through the jpeg output for every frame we grab just to * find the first field's end marker, so we take this risk to boost * performance. * This is really something the driver could do for us because this * does conform to standards outside of Apple Quicktime. */ i_offset = 0x2e; p_app1->i_DQT_offset = hton32( i_offset ); i_offset = 0xb4; p_app1->i_DHT_offset = hton32( i_offset ); i_offset = 0x258; p_app1->i_SOF_offset = hton32( i_offset ); i_offset = 0x26b; p_app1->i_SOS_offset = hton32( i_offset ); i_offset = 0x279; p_app1->i_data_offset = hton32( i_offset ); /* SOF and SOS aren't specified by the mjpeg API because they aren't * optional. They will be present in the output. */ mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT; if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 ) { msg_Err( p_input, "cannot set mjpeg params (%s)", strerror( errno ) ); goto vdev_failed; } p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm; p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm * mjpeg.field_per_buff; } /* fix width/height */ if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) ) { struct video_window vid_win; if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 ) { msg_Err( p_input, "cannot get win (%s)", strerror( errno ) ); goto vdev_failed; } p_sys->i_width = vid_win.width; p_sys->i_height = vid_win.height; msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height ); } p_sys->p_video_frame = NULL; if( !p_sys->b_mjpeg ) { /* set hue/color/.. */ if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 ) { struct video_picture vid_picture = p_sys->vid_picture; if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 ) { vid_picture.brightness = p_sys->i_brightness; } if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 ) { vid_picture.colour = p_sys->i_colour; } if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 ) { vid_picture.hue = p_sys->i_hue; } if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 ) { vid_picture.contrast = p_sys->i_contrast; } if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) { msg_Dbg( p_input, "v4l device uses brightness: %d", vid_picture.brightness ); msg_Dbg( p_input, "v4l device uses colour: %d", vid_picture.colour ); msg_Dbg( p_input, "v4l device uses hue: %d", vid_picture.hue ); msg_Dbg( p_input, "v4l device uses contrast: %d", vid_picture.contrast ); p_sys->vid_picture = vid_picture; } } /* Find out video format used by device */ if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 ) { struct video_picture vid_picture = p_sys->vid_picture; vlc_value_t val; int i; vid_picture.palette = 0; p_sys->i_fourcc = 0; var_Create( p_input, "v4l-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT ); var_Get( p_input, "v4l-chroma", &val ); if( val.psz_string && strlen( val.psz_string ) >= 4 ) { int i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1], val.psz_string[2], val.psz_string[3] ); /* Find out v4l chroma code */ for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ ) { if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma ) { vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l; break; } } } if( val.psz_string ) free( val.psz_string ); if( vid_picture.palette && !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) ) { p_sys->vid_picture = vid_picture; } else { /* Try to set the format to something easy to encode */ vid_picture.palette = VIDEO_PALETTE_YUV420P; if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) { p_sys->vid_picture = vid_picture; } else { vid_picture.palette = VIDEO_PALETTE_YUV422P; if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) { p_sys->vid_picture = vid_picture; } } } /* Find out final format */ for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ ) { if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette) { p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc; break; } } } else { msg_Err( p_input, "ioctl VIDIOCGPICT failed" ); goto vdev_failed; } } if( p_sys->b_mjpeg ) { int i; p_sys->mjpeg_buffers.count = 8; p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE; if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 ) { msg_Err( p_input, "mmap unsupported" ); goto vdev_failed; } p_sys->p_video_mmap = mmap( 0, p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 ); if( p_sys->p_video_mmap == MAP_FAILED ) { msg_Err( p_input, "mmap failed" ); goto vdev_failed; } p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' ); p_sys->i_frame_pos = -1; /* queue up all the frames */ for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ ) { if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 ) { msg_Err( p_input, "unable to queue frame" ); goto vdev_failed; } } } else { /* Fill in picture_t fields */ vout_InitPicture( VLC_OBJECT(p_input), &p_sys->pic, p_sys->i_fourcc, p_sys->i_width, p_sys->i_height, p_sys->i_width * VOUT_ASPECT_FACTOR / p_sys->i_height ); if( !p_sys->pic.i_planes ) { msg_Err( p_input, "unsupported chroma" ); goto vdev_failed; } p_sys->i_video_frame_size = 0; for( i = 0; i < p_sys->pic.i_planes; i++ ) { p_sys->i_video_frame_size += p_sys->pic.p[i].i_lines * p_sys->pic.p[i].i_visible_pitch; } msg_Dbg( p_input, "v4l device uses frame size: %i", p_sys->i_video_frame_size ); msg_Dbg( p_input, "v4l device uses chroma: %4.4s", (char*)&p_sys->i_fourcc ); /* Allocate mmap buffer */ if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 ) { msg_Err( p_input, "mmap unsupported" ); goto vdev_failed; } p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size, PROT_READ|PROT_WRITE, MAP_SHARED, i_fd, 0 ); if( p_sys->p_video_mmap == MAP_FAILED ) { /* FIXME -> normal read */ msg_Err( p_input, "mmap failed" ); goto vdev_failed; } /* init grabbing */ p_sys->vid_mmap.frame = 0; p_sys->vid_mmap.width = p_sys->i_width; p_sys->vid_mmap.height = p_sys->i_height; p_sys->vid_mmap.format = p_sys->vid_picture.palette; if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 ) { msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_fourcc ); msg_Err( p_input, "chroma selection failed" ); goto vdev_failed; } } return i_fd;vdev_failed: if( i_fd >= 0 ) close( i_fd ); return -1;}/***************************************************************************** * OpenAudioDev: *****************************************************************************/int OpenAudioDev( input_thread_t *p_input, char *psz_device ){ access_sys_t *p_sys = p_input->p_access_data; int i_fd, i_format; if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 ) { msg_Err( p_input, "cannot open audio device (%s)", strerror( errno ) ); goto adev_fail; } i_format = AFMT_S16_LE; if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 || i_format != AFMT_S16_LE ) { msg_Err( p_input, "cannot set audio format (16b little endian) " "(%s)", strerror( errno ) ); goto adev_fail; } if( ioctl( i_fd, SNDCTL_DSP_STEREO, &p_sys->b_stereo ) < 0 ) { msg_Err( p_input, "cannot set audio channels count (%s)", strerror( errno ) ); goto adev_fail; } if( ioctl( i_fd, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate ) < 0 ) { msg_Err( p_input, "cannot set audio sample rate (%s)", strerror( errno ) ); goto adev_fail; } msg_Dbg( p_input, "openened adev=`%s' %s %dHz", psz_device, p_sys->b_stereo ? "stereo" : "mono", p_sys->i_sample_rate ); p_sys->i_audio_frame_size = 0; p_sys->i_audio_frame_size_allocated = 6*1024; p_sys->p_audio_frame = malloc( p_sys->i_audio_frame_size_allocated ); return i_fd; adev_fail: if( i_fd >= 0 ) close( i_fd ); return -1;}/***************************************************************************** * AccessClose: close device, free resources *****************************************************************************/static void AccessClose( vlc_object_t *p_this ){ input_thread_t *p_input = (input_thread_t *)p_this; access_sys_t *p_sys = p_input->p_access_data; if( p_sys->psz_device ) free( p_sys->psz_device ); if( p_sys->psz_vdev ) free( p_sys->psz_vdev ); if( p_sys->psz_adev ) free( p_sys->psz_adev ); if( p_sys->fd_video >= 0 ) close( p_sys->fd_video ); if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio ); if( p_sys->p_header ) free( p_sys->p_header ); if( p_sys->p_audio_frame ) free( p_sys->p_audio_frame ); if( p_sys->b_mjpeg ) { int i_noframe = -1; ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe ); } if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED ) { if( p_sys->b_mjpeg ) munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count ); else munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size ); } free( p_sys );}/***************************************************************************** * GrabAudio: grab audio *****************************************************************************/static int GrabAudio( input_thread_t * p_input, uint8_t **pp_data, int *pi_data, mtime_t *pi_pts ){ access_sys_t *p_sys = p_input->p_access_data; struct audio_buf_info buf_info; int i_read; int i_correct; i_read = read( p_sys->fd_audio, p_sys->p_audio_frame, p_sys->i_audio_frame_size_allocated ); if( i_read <= 0 ) { return VLC_EGENERIC; } p_sys->i_audio_frame_size = i_read; /* from vls : correct the date because of kernel buffering */ i_correct = i_read; if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 ) { i_correct += buf_info.bytes; } *pp_data = p_sys->p_audio_frame; *pi_data = p_sys->i_audio_frame_size; *pi_pts = mdate() - (mtime_t)1000000 * (mtime_t)i_correct / 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate; return VLC_SUCCESS;}/***************************************************************************** * GrabVideo: *****************************************************************************/static uint8_t *GrabCapture( input_thread_t *p_input ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -