X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fcodec%2Frawvideo.c;h=331c216346d069eb976e617fec6311ce2b7514f2;hb=600e3556efd41d366fbd1f89ef4c455ed4c9ba91;hp=ad64e22ed70ab9bae484cc80ad3faf3aea04c8b1;hpb=d3fe7f28797d4dba65ffcdd60bf932e758a48a9e;p=vlc diff --git a/modules/codec/rawvideo.c b/modules/codec/rawvideo.c index ad64e22ed7..331c216346 100644 --- a/modules/codec/rawvideo.c +++ b/modules/codec/rawvideo.c @@ -1,52 +1,55 @@ /***************************************************************************** * rawvideo.c: Pseudo video decoder/packetizer for raw video data ***************************************************************************** - * Copyright (C) 2001, 2002 the VideoLAN team + * Copyright (C) 2001, 2002 VLC authors and VideoLAN * $Id$ * * Authors: Laurent Aimar * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** * Preamble *****************************************************************************/ -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include #include -#include /***************************************************************************** * decoder_sys_t : raw video decoder descriptor *****************************************************************************/ struct decoder_sys_t { - /* Module mode */ - vlc_bool_t b_packetizer; - /* * Input properties */ - int i_raw_size; - vlc_bool_t b_invert; + bool b_invert; + + size_t size; + unsigned pitches[PICTURE_PLANE_MAX]; + unsigned lines[PICTURE_PLANE_MAX]; /* * Common properties */ - mtime_t i_pts; - + date_t pts; }; /**************************************************************************** @@ -54,140 +57,87 @@ struct decoder_sys_t ****************************************************************************/ static int OpenDecoder ( vlc_object_t * ); static int OpenPacketizer( vlc_object_t * ); -static void CloseDecoder ( vlc_object_t * ); - -static void *DecodeBlock ( decoder_t *, block_t ** ); - -static picture_t *DecodeFrame( decoder_t *, block_t * ); -static block_t *SendFrame ( decoder_t *, block_t * ); +static void CloseCommon ( vlc_object_t * ); /***************************************************************************** * Module descriptor *****************************************************************************/ -vlc_module_begin(); - set_description( _("Pseudo raw video decoder") ); - set_capability( "decoder", 50 ); - set_category( CAT_INPUT ); - set_subcategory( SUBCAT_INPUT_VCODEC ); - set_callbacks( OpenDecoder, CloseDecoder ); - - add_submodule(); - set_description( _("Pseudo raw video packetizer") ); - set_capability( "packetizer", 100 ); - set_callbacks( OpenPacketizer, CloseDecoder ); -vlc_module_end(); - -/***************************************************************************** - * OpenDecoder: probe the decoder and return score - *****************************************************************************/ -static int OpenDecoder( vlc_object_t *p_this ) +vlc_module_begin () + set_description( N_("Pseudo raw video decoder") ) + set_capability( "decoder", 50 ) + set_category( CAT_INPUT ) + set_subcategory( SUBCAT_INPUT_VCODEC ) + set_callbacks( OpenDecoder, CloseCommon ) + + add_submodule () + set_description( N_("Pseudo raw video packetizer") ) + set_capability( "packetizer", 100 ) + set_callbacks( OpenPacketizer, CloseCommon ) +vlc_module_end () + +/** + * Common initialization for decoder and packetizer + */ +static int OpenCommon( decoder_t *p_dec ) { - decoder_t *p_dec = (decoder_t*)p_this; - decoder_sys_t *p_sys; + const vlc_chroma_description_t *dsc = + vlc_fourcc_GetChromaDescription( p_dec->fmt_in.i_codec ); + if( dsc == NULL || dsc->plane_count == 0 ) + return VLC_EGENERIC; - switch( p_dec->fmt_in.i_codec ) + if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height == 0 ) { - /* Planar YUV */ - case VLC_FOURCC('I','4','4','4'): - case VLC_FOURCC('I','4','2','2'): - case VLC_FOURCC('I','4','2','0'): - case VLC_FOURCC('Y','V','1','2'): - case VLC_FOURCC('I','Y','U','V'): - case VLC_FOURCC('I','4','1','1'): - case VLC_FOURCC('I','4','1','0'): - case VLC_FOURCC('Y','V','U','9'): - - /* Packed YUV */ - case VLC_FOURCC('Y','U','Y','2'): - case VLC_FOURCC('U','Y','V','Y'): - - /* RGB */ - case VLC_FOURCC('R','V','3','2'): - case VLC_FOURCC('R','V','2','4'): - case VLC_FOURCC('R','V','1','6'): - case VLC_FOURCC('R','V','1','5'): - break; - - case VLC_FOURCC('y','v','1','2'): - p_dec->fmt_in.i_codec = VLC_FOURCC('Y','V','1','2'); - break; - - default: - return VLC_EGENERIC; + msg_Err( p_dec, "invalid display size %dx%d", + p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height ); + return VLC_EGENERIC; } /* Allocate the memory needed to store the decoder's structure */ - if( ( p_dec->p_sys = p_sys = - (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL ) - { - msg_Err( p_dec, "out of memory" ); - return VLC_EGENERIC; - } - /* Misc init */ - p_dec->p_sys->b_packetizer = VLC_FALSE; - p_sys->i_pts = 0; - p_sys->b_invert = 0; + decoder_sys_t *p_sys = calloc(1, sizeof(*p_sys)); + if( unlikely(p_sys == NULL) ) + return VLC_ENOMEM; if( (int)p_dec->fmt_in.video.i_height < 0 ) { /* Frames are coded from bottom to top */ p_dec->fmt_in.video.i_height = (unsigned int)(-(int)p_dec->fmt_in.video.i_height); - p_sys->b_invert = VLC_TRUE; + p_sys->b_invert = true; } + if( !p_dec->fmt_in.video.i_visible_width ) + p_dec->fmt_in.video.i_visible_width = p_dec->fmt_in.video.i_width; + if( !p_dec->fmt_in.video.i_visible_height ) + p_dec->fmt_in.video.i_visible_height = p_dec->fmt_in.video.i_height; + + es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in ); - if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 ) + date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate, + p_dec->fmt_out.video.i_frame_rate_base ); + if( p_dec->fmt_out.video.i_frame_rate == 0 || + p_dec->fmt_out.video.i_frame_rate_base == 0) { - msg_Err( p_dec, "invalid display size %dx%d", - p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height ); - return VLC_EGENERIC; + msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead", + p_dec->fmt_out.video.i_frame_rate, + p_dec->fmt_out.video.i_frame_rate_base); + date_Init( &p_sys->pts, 25, 1 ); } - /* Find out p_vdec->i_raw_size */ - vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec, - p_dec->fmt_in.video.i_width, - p_dec->fmt_in.video.i_height, - p_dec->fmt_in.video.i_aspect ); - p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel * - p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8; - - /* Set output properties */ - p_dec->fmt_out.i_cat = VIDEO_ES; - p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec; - if( !p_dec->fmt_in.video.i_aspect ) + for( unsigned i = 0; i < dsc->plane_count; i++ ) { - p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * - p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height; + unsigned pitch = p_dec->fmt_in.video.i_width * dsc->pixel_size + * dsc->p[i].w.num / dsc->p[i].w.den; + unsigned lines = p_dec->fmt_in.video.i_height + * dsc->p[i].h.num / dsc->p[i].h.den; + + p_sys->pitches[i] = pitch; + p_sys->lines[i] = lines; + p_sys->size += pitch * lines; } - else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect; - - if( p_dec->fmt_in.video.i_rmask ) - p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask; - if( p_dec->fmt_in.video.i_gmask ) - p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask; - if( p_dec->fmt_in.video.i_bmask ) - p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask; - - /* Set callbacks */ - p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **)) - DecodeBlock; - p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **)) - DecodeBlock; + p_dec->p_sys = p_sys; return VLC_SUCCESS; } -static int OpenPacketizer( vlc_object_t *p_this ) -{ - decoder_t *p_dec = (decoder_t*)p_this; - - int i_ret = OpenDecoder( p_this ); - - if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE; - - return i_ret; -} - /**************************************************************************** * DecodeBlock: the whole thing **************************************************************************** @@ -196,50 +146,45 @@ static int OpenPacketizer( vlc_object_t *p_this ) static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; - block_t *p_block; - void *p_buf; - if( !pp_block || !*pp_block ) return NULL; + if( pp_block == NULL || *pp_block == NULL ) + return NULL; - p_block = *pp_block; + block_t *p_block = *pp_block; - if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts ) + if( p_block->i_pts <= VLC_TS_INVALID && p_block->i_dts <= VLC_TS_INVALID && + !date_Get( &p_sys->pts ) ) { /* We've just started the stream, wait for the first PTS. */ block_Release( p_block ); return NULL; } - /* Date management */ - if( p_block->i_pts > 0 || p_block->i_dts > 0 ) + /* Date management: If there is a pts avaliable, use that. */ + if( p_block->i_pts > VLC_TS_INVALID ) { - if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts; - else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts; + date_Set( &p_sys->pts, p_block->i_pts ); + } + else if( p_block->i_dts > VLC_TS_INVALID ) + { + /* NB, davidf doesn't quite agree with this in general, it is ok + * for rawvideo since it is in order (ie pts=dts), however, it + * may not be ok for an out-of-order codec, so don't copy this + * without thinking */ + date_Set( &p_sys->pts, p_block->i_dts ); } - if( p_block->i_buffer < p_sys->i_raw_size ) + if( p_block->i_buffer < p_sys->size ) { - msg_Warn( p_dec, "invalid frame size (%d < %d)", - p_block->i_buffer, p_sys->i_raw_size ); + msg_Warn( p_dec, "invalid frame size (%zu < %zu)", + p_block->i_buffer, p_sys->size ); block_Release( p_block ); return NULL; } - if( p_sys->b_packetizer ) - { - p_buf = SendFrame( p_dec, p_block ); - } - else - { - p_buf = DecodeFrame( p_dec, p_block ); - } - - /* Date management: 1 frame per packet */ - p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ ); *pp_block = NULL; - - return p_buf; + return p_block; } /***************************************************************************** @@ -247,44 +192,56 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) *****************************************************************************/ static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic ) { - uint8_t *p_src, *p_dst; - int i_src, i_plane, i_line, i_width; decoder_sys_t *p_sys = p_dec->p_sys; + const uint8_t *p_src = p_block->p_buffer; - p_src = p_block->p_buffer; - i_src = p_block->i_buffer; - - for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ ) - { - p_dst = p_pic->p[i_plane].p_pixels; - i_width = p_pic->p[i_plane].i_visible_pitch; + if( p_sys->b_invert ) + for( int i = 0; i < p_pic->i_planes; i++ ) + { + uint8_t *p_dst = p_pic->p[i].p_pixels + + (p_pic->p[i].i_pitch * p_pic->p[i].i_visible_lines); - if( p_sys->b_invert ) - p_src += (i_width * (p_pic->p[i_plane].i_visible_lines - 1)); + for( int x = 0; x < p_pic->p[i].i_visible_lines; x++ ) + { + p_dst -= p_pic->p[i].i_pitch; + memcpy( p_dst, p_src, p_pic->p[i].i_visible_pitch ); + p_src += p_sys->pitches[i]; + } - for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ ) - { - p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width ); - p_src += p_sys->b_invert ? -i_width : i_width; - p_dst += p_pic->p[i_plane].i_pitch; + p_src += p_sys->pitches[i] + * (p_sys->lines[i] - p_pic->p[i].i_visible_lines); } + else + for( int i = 0; i < p_pic->i_planes; i++ ) + { + uint8_t *p_dst = p_pic->p[i].p_pixels; - if( p_sys->b_invert ) - p_src += (i_width * (p_pic->p[i_plane].i_visible_lines + 1)); - } + for( int x = 0; x < p_pic->p[i].i_visible_lines; x++ ) + { + memcpy( p_dst, p_src, p_pic->p[i].i_visible_pitch ); + p_src += p_sys->pitches[i]; + p_dst += p_pic->p[i].i_pitch; + } + + p_src += p_sys->pitches[i] + * (p_sys->lines[i] - p_pic->p[i].i_visible_lines); + } } /***************************************************************************** * DecodeFrame: decodes a video frame. *****************************************************************************/ -static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block ) +static picture_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block ) { + block_t *p_block = DecodeBlock( p_dec, pp_block ); + if( p_block == NULL ) + return NULL; + decoder_sys_t *p_sys = p_dec->p_sys; - picture_t *p_pic; /* Get a new picture */ - p_pic = p_dec->pf_vout_buffer_new( p_dec ); - if( !p_pic ) + picture_t *p_pic = decoder_NewPicture( p_dec ); + if( p_pic == NULL ) { block_Release( p_block ); return NULL; @@ -292,71 +249,96 @@ static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block ) FillPicture( p_dec, p_block, p_pic ); - p_pic->date = p_sys->i_pts; + /* Date management: 1 frame per packet */ + p_pic->date = date_Get( &p_dec->p_sys->pts ); + date_Increment( &p_sys->pts, 1 ); + + if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK ) + { + p_pic->b_progressive = false; + p_pic->i_nb_fields = 2; + if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST ) + p_pic->b_top_field_first = true; + else + p_pic->b_top_field_first = false; + } + else + p_pic->b_progressive = true; block_Release( p_block ); return p_pic; } +static int OpenDecoder( vlc_object_t *p_this ) +{ + decoder_t *p_dec = (decoder_t *)p_this; + + int ret = OpenCommon( p_dec ); + if( ret == VLC_SUCCESS ) + p_dec->pf_decode_video = DecodeFrame; + return ret; +} + /***************************************************************************** * SendFrame: send a video frame to the stream output. *****************************************************************************/ -static block_t *SendFrame( decoder_t *p_dec, block_t *p_block ) +static block_t *SendFrame( decoder_t *p_dec, block_t **pp_block ) { + block_t *p_block = DecodeBlock( p_dec, pp_block ); + if( p_block == NULL ) + return NULL; + decoder_sys_t *p_sys = p_dec->p_sys; - p_block->i_dts = p_block->i_pts = p_sys->i_pts; + /* Date management: 1 frame per packet */ + p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts ); + date_Increment( &p_sys->pts, 1 ); if( p_sys->b_invert ) { - picture_t pic; - uint8_t *p_tmp, *p_pixels; - int i, j; - - /* Fill in picture_t fields */ - vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec, - p_dec->fmt_out.video.i_width, - p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR ); - - if( !pic.i_planes ) + block_t *out = block_Alloc( p_block->i_buffer ); + if( likely(out != NULL) ) { - msg_Err( p_dec, "unsupported chroma" ); - return p_block; - } + block_CopyProperties( out, p_block ); - p_tmp = malloc( pic.p[0].i_visible_pitch ); - p_pixels = p_block->p_buffer; - for( i = 0; i < pic.i_planes; i++ ) - { - int i_pitch = pic.p[i].i_visible_pitch; - uint8_t *p_top = p_pixels; - uint8_t *p_bottom = p_pixels + i_pitch * - (pic.p[i].i_visible_lines - 1); + const uint8_t *p_src = p_block->p_buffer; + uint8_t *p_pixels = out->p_buffer; - for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ ) + for( unsigned i = 0; i < PICTURE_PLANE_MAX; i++ ) { - p_dec->p_libvlc->pf_memcpy( p_tmp, p_bottom, - pic.p[i].i_visible_pitch ); - p_dec->p_libvlc->pf_memcpy( p_bottom, p_top, - pic.p[i].i_visible_pitch ); - p_dec->p_libvlc->pf_memcpy( p_top, p_tmp, - pic.p[i].i_visible_pitch ); - p_top += i_pitch; - p_bottom -= i_pitch; + unsigned pitch = p_sys->pitches[i]; + unsigned lines = p_sys->lines[i]; + uint8_t *p_dst = p_pixels + (pitch * lines); + + for( unsigned x = 0; x < lines; x++ ) + { + p_dst -= p_sys->pitches[i]; + memcpy( p_dst, p_src, p_sys->pitches[i] ); + p_src += p_sys->pitches[i]; + } } - - p_pixels += i_pitch * pic.p[i].i_lines; } - free( p_tmp ); + block_Release( p_block ); + p_block = out; } return p_block; } -/***************************************************************************** - * CloseDecoder: decoder destruction - *****************************************************************************/ -static void CloseDecoder( vlc_object_t *p_this ) +static int OpenPacketizer( vlc_object_t *p_this ) +{ + decoder_t *p_dec = (decoder_t *)p_this; + + int ret = OpenCommon( p_dec ); + if( ret == VLC_SUCCESS ) + p_dec->pf_packetize = SendFrame; + return ret; +} + +/** + * Common deinitialization + */ +static void CloseCommon( vlc_object_t *p_this ) { decoder_t *p_dec = (decoder_t*)p_this; free( p_dec->p_sys );