diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e249ad..87274b9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -216,7 +216,6 @@ CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_D ###################################################################################### # Target App ###################################################################################### - ADD_EXECUTABLE( ${PROJECT_NAME} ${SOURCES_FILES} ${HEADERS_MOC_FILES} ${FORMS_HEADERS_FILES} ${RESOURCES_RCC_FILES} ) TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${LINKER_LIBS} ) diff --git a/config.h.in b/config.h.in index d2aea32..1bd0901 100644 --- a/config.h.in +++ b/config.h.in @@ -19,11 +19,6 @@ /* FFMPEG */ #cmakedefine USE_FFMPEG -#ifdef USE_FFMPEG - #ifndef __STDC_CONSTANT_MACROS - //#define __STDC_CONSTANT_MACROS - #define smprintf _snprintf - #endif -#endif + #endif // __CONFIG_PLAYUVER_H__ diff --git a/source/PlaYUVerFrame.h b/source/PlaYUVerFrame.h index 0cf6f06..a5f44bd 100755 --- a/source/PlaYUVerFrame.h +++ b/source/PlaYUVerFrame.h @@ -42,11 +42,6 @@ public: Void FrameFromBuffer( Pel *input_buffer, Int pel_format ); - enum InputStreamColorSpace - { - NO_FMT = -1, YUV420, YUV400, YUV422, RGB, - }; - UInt64 getBytesPerFrame(); Pel*** getPelBufferYUV() diff --git a/source/TypeDef.h b/source/TypeDef.h index c4b6142..4f21545 100644 --- a/source/TypeDef.h +++ b/source/TypeDef.h @@ -24,7 +24,9 @@ #ifndef __TYPEDEF_H__ #define __TYPEDEF_H__ +#ifndef __MAKE_LIB__ #include "config.h" +#endif namespace plaYUVer { @@ -104,6 +106,18 @@ typedef UChar Pel; ///< 8-bit pixel type #define QStringLiteral QString::fromUtf8 #endif +// ==================================================================================================================== +// PlaYUVer definitions +// ==================================================================================================================== + +enum InputStreamColorSpace +{ + NO_FMT = -1, YUV420, YUV400, YUV422, RGB, +}; + + } + + #endif // __TYPEDEF_H__ diff --git a/source/lib/ffmpeg/LibAvContextHandle.cpp b/source/lib/ffmpeg/LibAvContextHandle.cpp index 11a3af0..969b270 100755 --- a/source/lib/ffmpeg/LibAvContextHandle.cpp +++ b/source/lib/ffmpeg/LibAvContextHandle.cpp @@ -21,223 +21,220 @@ * \brief Interface with libav libs */ -#include "LibAvContextHandle.h" - #include -#include "LibMemAlloc.h" -#include "PlaYUVerFrame.h" +#include "LibAvContextHandle.h" namespace plaYUVer { - static int open_codec_context( int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type ) - { - int ret; - AVStream *st; - AVCodecContext *dec_ctx = NULL; - AVCodec *dec = NULL; +static int open_codec_context( int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type ) +{ + int ret; + AVStream *st; + AVCodecContext *dec_ctx = NULL; + AVCodec *dec = NULL; - ret = av_find_best_stream( fmt_ctx, type, -1, -1, NULL, 0 ); - if( ret < 0 ) + ret = av_find_best_stream( fmt_ctx, type, -1, -1, NULL, 0 ); + if( ret < 0 ) + { + return ret; + } + else + { + *stream_idx = ret; + st = fmt_ctx->streams[*stream_idx]; + + /* find decoder for the stream */ + dec_ctx = st->codec; + dec = avcodec_find_decoder( dec_ctx->codec_id ); + if( !dec ) { + printf( "Failed to find %s codec\n", av_get_media_type_string( type ) ); return ret; } - else + + if( ( ret = avcodec_open2( dec_ctx, dec, NULL ) ) < 0 ) { - *stream_idx = ret; - st = fmt_ctx->streams[*stream_idx]; - - /* find decoder for the stream */ - dec_ctx = st->codec; - dec = avcodec_find_decoder( dec_ctx->codec_id ); - if( !dec ) - { - printf( "Failed to find %s codec\n", av_get_media_type_string( type ) ); - return ret; - } - - if( ( ret = avcodec_open2( dec_ctx, dec, NULL ) ) < 0 ) - { - printf( "Failed to open %s codec\n", av_get_media_type_string( type ) ); - return ret; - } + printf( "Failed to open %s codec\n", av_get_media_type_string( type ) ); + return ret; } - - return 0; } - Void LibAvContextHandle::closeAvFormat() + return 0; +} + +Void LibAvContextHandle::closeAvFormat() +{ + if( m_bHasStream ) { - if( m_bHasStream ) - { - if( video_dec_ctx ) + if( video_dec_ctx ) avcodec_close( video_dec_ctx ); - if( fmt_ctx ) + if( fmt_ctx ) avformat_close_input( &fmt_ctx ); - av_free( frame ); - av_free( video_dst_data[0] ); - } - m_bHasStream = false; + av_free( frame ); + av_free( video_dst_data[0] ); + } + m_bHasStream = false; +} + +Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height, Int& pixel_format, UInt& frame_rate ) +{ + Bool bRet = true; + int ret = 0; + + fmt_ctx = NULL; + video_dec_ctx = NULL; + video_stream = NULL; + video_dst_data[0] = NULL; + video_dst_data[1] = NULL; + video_dst_data[2] = NULL; + video_dst_data[3] = NULL; + video_stream_idx = -1; + frame = NULL; + m_bHasStream = false; + + char *src_filename = filename; + AVDictionary *format_opts = NULL; + + /* register all formats and codecs */ + av_register_all(); + + if( width >= 0 && height >= 0 ) + { + Char aux_string[10]; + sprintf( aux_string, "%dx%d", width, height ); + av_dict_set( &format_opts, "video_size", aux_string, 0 ); + } + switch( pixel_format ) + { + case NO_FMT: + break; + case YUV420: + av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV420P ), 0 ); + break; + case YUV400: + av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_GRAY8 ), 0 ); + break; } - Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height, Int& pixel_format, UInt& frame_rate ) + /* open input file, and allocate format context */ + if( avformat_open_input( &fmt_ctx, src_filename, NULL, &format_opts ) < 0 ) { - Bool bRet = true; - int ret = 0; + //qDebug( ) << " Could not open source file %s !!!" << filename << endl; + return false; + } - fmt_ctx = NULL; - video_dec_ctx = NULL; - video_stream = NULL; - video_dst_data[0] = NULL; - video_dst_data[1] = NULL; - video_dst_data[2] = NULL; - video_dst_data[3] = NULL; - video_stream_idx = -1; - frame = NULL; - m_bHasStream = false; + /* retrieve stream information */ + if( avformat_find_stream_info( fmt_ctx, NULL ) < 0 ) + { + //qDebug( ) << " Could not find stream information !!!" << endl; + return false; + } - char *src_filename = filename; - AVDictionary *format_opts = NULL; + if( open_codec_context( &video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO ) >= 0 ) + { + video_stream = fmt_ctx->streams[video_stream_idx]; + video_dec_ctx = video_stream->codec; - /* register all formats and codecs */ - av_register_all(); - - if( width >= 0 && height >= 0 ) + /* allocate image where the decoded image will be put */ + ret = av_image_alloc( video_dst_data, video_dst_linesize, video_dec_ctx->width, video_dec_ctx->height, video_dec_ctx->pix_fmt, 1 ); + if( ret < 0 ) { - Char aux_string[10]; - sprintf( aux_string, "%dx%d", width, height ); - av_dict_set( &format_opts, "video_size", aux_string, 0 ); - } - switch( pixel_format ) - { - case PlaYUVerFrame::NO_FMT: - break; - case PlaYUVerFrame::YUV420: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV420P ), 0 ); - break; - case PlaYUVerFrame::YUV400: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_GRAY8 ), 0 ); - break; - } - - /* open input file, and allocate format context */ - if( avformat_open_input( &fmt_ctx, src_filename, NULL, &format_opts ) < 0 ) - { - //qDebug( ) << " Could not open source file %s !!!" << filename << endl; + //qDebug( ) << " Could not allocate raw video buffer !!!" << endl; + closeAvFormat(); return false; } + video_dst_bufsize = ret; + } - /* retrieve stream information */ - if( avformat_find_stream_info( fmt_ctx, NULL ) < 0 ) - { - //qDebug( ) << " Could not find stream information !!!" << endl; - return false; - } - - if( open_codec_context( &video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO ) >= 0 ) - { - video_stream = fmt_ctx->streams[video_stream_idx]; - video_dec_ctx = video_stream->codec; - - /* allocate image where the decoded image will be put */ - ret = av_image_alloc( video_dst_data, video_dst_linesize, video_dec_ctx->width, video_dec_ctx->height, video_dec_ctx->pix_fmt, 1 ); - if( ret < 0 ) - { - //qDebug( ) << " Could not allocate raw video buffer !!!" << endl; - closeAvFormat(); - return false; - } - video_dst_bufsize = ret; - } - - Double fr = 30; - if( video_stream->avg_frame_rate.den && video_stream->avg_frame_rate.num ) + Double fr = 30; + if( video_stream->avg_frame_rate.den && video_stream->avg_frame_rate.num ) fr = av_q2d( video_stream->avg_frame_rate ); #if FF_API_R_FRAME_RATE - else if( video_stream->r_frame_rate.den && video_stream->r_frame_rate.num ) + else if( video_stream->r_frame_rate.den && video_stream->r_frame_rate.num ) fr = av_q2d( video_stream->r_frame_rate ); #endif - else if( video_stream->time_base.den && video_stream->time_base.num ) + else if( video_stream->time_base.den && video_stream->time_base.num ) fr = 1 / av_q2d( video_stream->time_base ); - else if( video_stream->codec->time_base.den && video_stream->codec->time_base.num ) + else if( video_stream->codec->time_base.den && video_stream->codec->time_base.num ) fr = 1 / av_q2d( video_stream->codec->time_base ); - frame_rate = ( UInt )fr; + frame_rate = ( UInt )fr; - width = video_dec_ctx->width; - height = video_dec_ctx->height; - switch( video_dec_ctx->pix_fmt ) - { - case AV_PIX_FMT_YUV420P: - pixel_format = PlaYUVerFrame::YUV420; - break; - case AV_PIX_FMT_GRAY8: - pixel_format = PlaYUVerFrame::YUV400; - break; - } - - /* dump input information to stderr */ - av_dump_format( fmt_ctx, 0, src_filename, 0 ); - if( !video_stream ) - { - //qDebug( ) << " Could not find audio or video stream in the input, aborting !!!" << endl; - bRet = false; - closeAvFormat(); - return false; - } - - frame = avcodec_alloc_frame(); - if( !frame ) - { - //qDebug( ) << " Could not allocate frame !!!" << endl; - ret = AVERROR( ENOMEM ); - closeAvFormat(); - return false; - } - - /* initialize packet, set data to NULL, let the demuxer fill it */ - av_init_packet( &pkt ); - pkt.data = NULL; - pkt.size = 0; - - m_bHasStream = true; - - return true; + width = video_dec_ctx->width; + height = video_dec_ctx->height; + switch( video_dec_ctx->pix_fmt ) + { + case AV_PIX_FMT_YUV420P: + pixel_format = YUV420; + break; + case AV_PIX_FMT_GRAY8: + pixel_format = YUV400; + break; } - Bool LibAvContextHandle::decodeAvFormat() + /* dump input information to stderr */ + av_dump_format( fmt_ctx, 0, src_filename, 0 ); + if( !video_stream ) { - Int got_frame; - int ret; - /* read frames from the file */ + //qDebug( ) << " Could not find audio or video stream in the input, aborting !!!" << endl; + bRet = false; + closeAvFormat(); + return false; + } - if( av_read_frame( fmt_ctx, &pkt ) >= 0 ) + frame = avcodec_alloc_frame(); + if( !frame ) + { + //qDebug( ) << " Could not allocate frame !!!" << endl; + ret = AVERROR( ENOMEM ); + closeAvFormat(); + return false; + } + + /* initialize packet, set data to NULL, let the demuxer fill it */ + av_init_packet( &pkt ); + pkt.data = NULL; + pkt.size = 0; + + m_bHasStream = true; + + return true; +} + +Bool LibAvContextHandle::decodeAvFormat() +{ + Int got_frame; + int ret; + /* read frames from the file */ + + if( av_read_frame( fmt_ctx, &pkt ) >= 0 ) + { + if( pkt.stream_index == video_stream_idx ) { - if( pkt.stream_index == video_stream_idx ) + /* decode video frame */ + ret = avcodec_decode_video2( video_dec_ctx, frame, &got_frame, &pkt ); + if( ret < 0 ) { - /* decode video frame */ - ret = avcodec_decode_video2( video_dec_ctx, frame, &got_frame, &pkt ); - if( ret < 0 ) - { - fprintf( stderr, "Error decoding video frame\n" ); - return false; - } - - if( got_frame ) - { - /* copy decoded frame to destination buffer: - * this is required since rawvideo expects non aligned data */ - av_image_copy( video_dst_data, video_dst_linesize, ( const uint8_t ** )( frame->data ), frame->linesize, video_dec_ctx->pix_fmt, video_dec_ctx->width, - video_dec_ctx->height ); - - /* write to rawvideo file */ - //fwrite( video_dst_data[0], 1, video_dst_bufsize, video_dst_file ); - } + fprintf( stderr, "Error decoding video frame\n" ); + return false; + } + + if( got_frame ) + { + /* copy decoded frame to destination buffer: + * this is required since rawvideo expects non aligned data */ + av_image_copy( video_dst_data, video_dst_linesize, ( const uint8_t ** )( frame->data ), frame->linesize, video_dec_ctx->pix_fmt, video_dec_ctx->width, + video_dec_ctx->height ); + + /* write to rawvideo file */ + //fwrite( video_dst_data[0], 1, video_dst_bufsize, video_dst_file ); } - av_free_packet( &pkt ); } + av_free_packet( &pkt ); + } // /* flush cached frames */ // pkt.data = NULL; @@ -248,12 +245,12 @@ namespace plaYUVer // } // while( got_frame ); - true; - } + true; +} - Void LibAvContextHandle::seekAvFormat( UInt frame_num ) - { - av_seek_frame( fmt_ctx, video_stream_idx, frame_num, AVSEEK_FLAG_ANY ); - } +Void LibAvContextHandle::seekAvFormat( UInt frame_num ) +{ + av_seek_frame( fmt_ctx, video_stream_idx, frame_num, AVSEEK_FLAG_ANY ); +} } // NAMESPACE diff --git a/source/lib/ffmpeg/LibAvContextHandle.h b/source/lib/ffmpeg/LibAvContextHandle.h index 28f8652..435dd7e 100755 --- a/source/lib/ffmpeg/LibAvContextHandle.h +++ b/source/lib/ffmpeg/LibAvContextHandle.h @@ -34,39 +34,43 @@ extern "C" #include "libavformat/avformat.h" } +#ifndef __STDC_CONSTANT_MACROS +#define smprintf _snprintf +#endif + #include "TypeDef.h" namespace plaYUVer { - class LibAvContextHandle +class LibAvContextHandle +{ +public: + Bool initAvFormat( char* filename, UInt& width, UInt& height, Int& pixel_format, UInt& frame_rate ); + Void closeAvFormat(); + Bool decodeAvFormat(); + + Void seekAvFormat( UInt frame_num ); + + Bool getStatus() { - public: - Bool initAvFormat( char* filename, UInt& width, UInt& height, Int& pixel_format, UInt& frame_rate ); - Void closeAvFormat(); - Bool decodeAvFormat(); + return m_bHasStream; + } - Void seekAvFormat( UInt frame_num ); + uint8_t *video_dst_data[4]; + int video_dst_linesize[4]; + int video_dst_bufsize; - Bool getStatus() - { - return m_bHasStream; - } +private: + AVFormatContext *fmt_ctx; + AVCodecContext *video_dec_ctx; + AVStream *video_stream; + Int video_stream_idx; + AVFrame *frame; + AVPacket pkt; - uint8_t *video_dst_data[4]; - int video_dst_linesize[4]; - int video_dst_bufsize; - - private: - AVFormatContext *fmt_ctx; - AVCodecContext *video_dec_ctx; - AVStream *video_stream; - Int video_stream_idx; - AVFrame *frame; - AVPacket pkt; - - Bool m_bHasStream; - }; + Bool m_bHasStream; +}; } // NAMESPACE diff --git a/source/lib/ffmpeg/Makefile b/source/lib/ffmpeg/Makefile deleted file mode 100644 index 6e73b6c..0000000 --- a/source/lib/ffmpeg/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -# Makefile template for shared library - -CC = gcc # C compiler -CFLAGS = -fPIC -Wall -Wextra -O2 -g -I../.. # C flags -LDFLAGS = -shared # linking flags -RM = rm -f # rm command -TARGET_LIB = LibAvContextHandle.so # target lib - -SRCS = LibAvContextHandle.cpp # source files -OBJS = $(SRCS:.c=.o) - -.PHONY: all -all: ${TARGET_LIB} - -$(TARGET_LIB): $(OBJS) - $(CC) ${LDFLAGS} -o $@ $^ - -$(SRCS:.c=.d):%.d:%.cpp - $(CC) $(CFLAGS) -MM $< >$@ - -include $(SRCS:.c=.d) - -.PHONY: clean -clean: - -${RM} ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d) \ No newline at end of file