34 #ifndef __INCLUDE_IO_VIDEO_H__
35 #define __INCLUDE_IO_VIDEO_H__
37 #ifndef __INCLUDE_MIST_H__
41 #ifndef __INCLUDE_MIST_COLOR_H__
42 #include "../config/color.h"
45 #ifndef __INCLUDE_MIST_SINGLETON__
46 #include "../singleton.h"
49 #ifndef __INCLUDE_MIST_LIMITS__
50 #include "../limits.h"
57 #include <libavcodec/avcodec.h>
58 #include <libavformat/avformat.h>
59 #include <libswscale/swscale.h>
60 #include <libavutil/avutil.h>
61 #include <libavutil/mathematics.h>
125 typedef size_t size_type;
126 typedef ptrdiff_t difference_type;
128 #if LIBAVCODEC_VERSION_MAJOR < 55
129 typedef CodecID codec_id_type;
131 typedef AVCodecID codec_id_type;
134 #if LIBAVCODEC_VERSION_MAJOR < 53
135 static const CodecType avmedia_type_video_id = CODEC_TYPE_VIDEO;
137 static const AVMediaType avmedia_type_video_id = AVMEDIA_TYPE_VIDEO;
141 virtual ~video_io_vase( ){ }
143 bool open(
const std::wstring &filename ){
return( open(
wstr2str( filename ) ) ); }
145 virtual bool open(
const std::string &filename ) = 0;
146 virtual bool close( ) = 0;
147 virtual bool is_open( )
const = 0;
148 virtual bool is_eof( )
const = 0;
149 virtual bool dump( )
const = 0;
150 virtual long double time( )
const = 0;
151 virtual long double duration( )
const = 0;
152 virtual const std::string filename( )
const = 0;
153 virtual size_type bit_rate( )
const = 0;
154 virtual size_type width( )
const = 0;
155 virtual size_type height( )
const = 0;
156 virtual long double frame_rate_numerator( )
const = 0;
157 virtual long double frame_rate_denominator( )
const = 0;
158 virtual long double frame_aspect_ratio( )
const = 0;
161 virtual long double frame_rate( )
const
163 return( frame_rate_denominator( ) / frame_rate_numerator( ) );
167 virtual long double seconds_per_frame( )
const
169 return( frame_rate_numerator( ) / frame_rate_denominator( ) );
173 AVOutputFormat *guess_video_format(
const std::string &filename,
const std::string &video_type,
const std::string &mime_type )
175 AVOutputFormat *fmt = NULL;
177 const char *fname = filename.empty( ) ? NULL : filename.c_str( );
178 const char *sname = video_type.empty( ) ? NULL : video_type.c_str( );
179 const char *mime = mime_type.empty( ) ? NULL : mime_type.c_str( );
181 if( fname != NULL || sname != NULL || mime != NULL )
184 #if LIBAVFORMAT_VERSION_MAJOR < 53
185 fmt = guess_format( sname, fname, mime );
187 fmt = av_guess_format( sname, fname, mime );
191 std::cout <<
"Could not determine output format from file extension. MPEG is used as default." << std::endl;
192 #if LIBAVFORMAT_VERSION_MAJOR < 53
193 fmt = guess_format(
"mpeg", NULL, NULL );
195 fmt = av_guess_format(
"mpeg", NULL, NULL );
199 std::cerr <<
"Could not find MPEG encoder." << std::endl;
206 #if LIBAVFORMAT_VERSION_MAJOR < 53
207 fmt = guess_format(
"mpeg", NULL, NULL );
209 fmt = av_guess_format(
"mpeg", NULL, NULL );
214 std::cerr <<
"Could not find specified video format." << std::endl;
222 AVOutputFormat *find_video_format( codec_id_type codec_id )
224 AVOutputFormat *fmt = av_oformat_next( NULL );
227 if( fmt->video_codec == codec_id )
232 fmt = av_oformat_next( fmt );
238 AVFrame *allocate_frame(
int w,
int h, PixelFormat pix_fmt )
241 AVFrame *frame = avcodec_alloc_frame( );
248 int size = avpicture_get_size( pix_fmt, w, h );
249 uint8_t *buff = ( uint8_t * )av_malloc( size );
250 avpicture_fill( ( AVPicture * )frame, buff, pix_fmt, w, h );
256 void free_frame( AVFrame **frame )
260 av_free( ( *frame )->data[ 0 ] );
261 av_free( ( *frame ) );
274 AVFormatContext *p_fctx_;
275 AVFrame *p_frame_src_;
276 AVFrame *p_frame_rgb_;
279 int video_stream_index_;
280 SwsContext *p_swscale_;
288 decoder( ) : p_fctx_( NULL ), p_frame_src_( NULL ), p_frame_rgb_( NULL ), is_open_( false ), is_eof_( true ), video_stream_index_( -1 ), p_swscale_( NULL ), frame_pts_( -1 )
293 avcodec_register_all( );
304 decoder(
const std::string &filename ) : p_fctx_( NULL ), p_frame_src_( NULL ), p_frame_rgb_( NULL ), is_open_( false ), is_eof_( true ), video_stream_index_( -1 ), p_swscale_( NULL ), frame_pts_( -1 )
309 avcodec_register_all( );
315 if( !open( filename ) )
331 virtual bool is_open( )
const {
return( is_open_ ); }
334 virtual bool is_eof( )
const {
return( is_eof_ ); }
337 virtual int codec_id( )
const
339 if( is_open( ) && video_stream_index_ >= 0 )
341 return( p_fctx_->streams[ video_stream_index_ ]->codec->codec_id );
345 return( CODEC_ID_NONE );
350 virtual bool dump( )
const
354 #if LIBAVFORMAT_VERSION_MAJOR < 54
355 dump_format( p_fctx_, 0, p_fctx_->filename,
false );
357 av_dump_format( p_fctx_, 0, p_fctx_->filename,
false );
368 virtual long double time( )
const
372 return( static_cast< long double >( frame_pts_ ) / static_cast< long double >( AV_TIME_BASE ) );
381 virtual int64_t frame_id( )
const
385 AVStream *stream = p_fctx_->streams[ video_stream_index_ ];
386 return( av_rescale( frame_pts_, stream->time_base.den, stream->time_base.num, AV_TIME_BASE ) );
395 virtual long double duration( )
const
399 AVStream *stream = p_fctx_->streams[ video_stream_index_ ];
400 int64_t pts = p_fctx_->streams[ video_stream_index_ ]->duration * AV_TIME_BASE;
401 return( static_cast< long double >( av_rescale( pts, stream->time_base.num, stream->time_base.den, AV_TIME_BASE ) ) );
410 virtual int64_t number_of_frames( )
const
414 return( p_fctx_->streams[ video_stream_index_ ]->duration );
423 virtual const std::string filename( )
const
427 return( p_fctx_->filename );
436 virtual size_type bit_rate( )
const
440 return( p_fctx_->streams[ video_stream_index_ ]->codec->bit_rate );
449 virtual size_type width( )
const
453 return( p_fctx_->streams[ video_stream_index_ ]->codec->width );
462 virtual size_type height( )
const
466 return( p_fctx_->streams[ video_stream_index_ ]->codec->height );
475 virtual long double frame_rate_numerator( )
const
479 return( p_fctx_->streams[ video_stream_index_ ]->codec->time_base.num );
488 virtual long double frame_rate_denominator( )
const
492 return( p_fctx_->streams[ video_stream_index_ ]->codec->time_base.den );
501 virtual long double frame_aspect_ratio( )
const
505 return( av_q2d( p_fctx_->streams[ video_stream_index_ ]->codec->sample_aspect_ratio ) * (
double )this->width( ) / this->height( ) );
518 virtual bool open(
const std::string &filename )
522 if( p_fctx_ != NULL )
524 av_close_input_file( p_fctx_ );
528 p_fctx_ = avformat_alloc_context( );
529 if( p_fctx_ == NULL )
531 printf(
"Couldn't allocate avformat context\n" );
540 #if LIBAVFORMAT_VERSION_MAJOR < 54
541 if( av_open_input_file( &p_fctx_, filename.c_str( ), NULL, 0, NULL ) != 0 )
543 if( avformat_open_input( &p_fctx_, filename.c_str( ), NULL, NULL ) != 0 )
546 printf(
"Couldn't open file %s\n", filename.c_str( ) );
551 #if LIBAVFORMAT_VERSION_MAJOR < 55
552 if( av_find_stream_info( p_fctx_ ) < 0 )
554 if( avformat_find_stream_info( p_fctx_, NULL ) < 0 )
557 printf(
"Couldn't find stream information\n" );
562 video_stream_index_ = -1;
563 for(
unsigned int i = 0 ; i < p_fctx_->nb_streams ; i++ )
565 if( p_fctx_->streams[ i ]->codec->codec_type == avmedia_type_video_id )
567 video_stream_index_ = i;
573 if( video_stream_index_ == -1 )
575 printf(
"Didn't find a video stream\n" );
579 AVCodecContext *p_cctx = p_fctx_->streams[ video_stream_index_ ]->codec;
582 AVCodec *p_codec = avcodec_find_decoder( p_cctx->codec_id );
583 if( p_codec == NULL )
585 printf(
"Codec %d not found\n", p_cctx->codec_id );
590 #if LIBAVCODEC_VERSION_MAJOR < 55
591 if( avcodec_open( p_cctx, p_codec ) < 0 )
593 if( avcodec_open2( p_cctx, p_codec, NULL ) < 0 )
596 printf(
"Could not open codec\n" );
601 p_frame_src_ = avcodec_alloc_frame( );
602 p_frame_rgb_ = allocate_frame( p_cctx->width, p_cctx->height, PIX_FMT_BGR24 );
605 p_swscale_ = sws_getContext( p_cctx->width, p_cctx->height, p_cctx->pix_fmt, p_cctx->width, p_cctx->height, PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
615 if( frame_pts_ != 0 )
617 this->seek_frame( 0 );
628 virtual bool close( )
633 sws_freeContext( p_swscale_ );
636 avcodec_close( p_fctx_->streams[ video_stream_index_ ]->codec );
639 av_close_input_file( p_fctx_ );
643 av_free( p_frame_src_);
646 free_frame( &p_frame_rgb_ );
660 bool skip( difference_type nskips = 1 )
662 if( is_open( ) && !this->is_eof( ) )
664 return(
decode( nskips ) && !is_eof( ) );
676 bool seek(
long double tm )
678 if( is_open( ) && tm >= 0 )
680 AVStream *stream = p_fctx_->streams[ video_stream_index_ ];
681 int64_t tgt =
static_cast< int64_t
>( AV_TIME_BASE * tm );
682 int64_t pts = av_rescale( tgt, stream->time_base.den, stream->time_base.num, AV_TIME_BASE );
684 if( av_seek_frame( p_fctx_, video_stream_index_, pts, AVSEEK_FLAG_BACKWARD ) >= 0 )
686 return(
decode( -1, tgt ) );
697 bool seek_frame( int64_t fno )
699 if( is_open( ) && fno >= 0 )
701 if( av_seek_frame( p_fctx_, video_stream_index_, fno, AVSEEK_FLAG_BACKWARD ) >= 0 )
703 AVStream *stream = p_fctx_->streams[ video_stream_index_ ];
704 return(
decode( -1, av_rescale_( AV_TIME_BASE, fno, stream->time_base.num, stream->time_base.den ) ) );
715 template <
class T,
class Allocator >
718 typedef _pixel_converter_< T > pixel_converter;
719 typedef typename pixel_converter::color_type color_type;
721 if( is_open( ) && !this->is_eof( ) )
723 AVCodecContext *p_cctx = p_fctx_->streams[ video_stream_index_ ]->codec;
726 image.
resize( p_cctx->width, p_cctx->height );
728 unsigned char *p = p_frame_rgb_->data[ 0 ];
730 if( ( image.
width( ) % 2 ) == 0 )
732 for( size_type i = 0 ; i < image.
size( ) ; i++ )
734 image[ i ] = pixel_converter::convert_to( p[ 2 ], p[ 1 ], p[ 0 ] );
741 size_type stride = p_frame_rgb_->linesize[ 0 ];
742 for( size_type j = 0 ; j < image.
height( ) ; j++ )
744 for( size_type i = 0 ; i < stride ; i += 3 )
746 *pi++ = pixel_converter::convert_to( p[ i + 2 ], p[ i + 1 ], p[ i ] );
769 if( read_buffer( image ) )
790 frame_time = this->time( );
791 if( read_buffer( image ) )
810 if( is_open( ) && !this->is_eof( ) && pts >= 0 )
820 AVStream *stream = p_fctx_->streams[ video_stream_index_ ];
821 AVCodecContext *p_cctx = stream->codec;
825 for( difference_type i = 1 ; !is_eof( ) && i <= ntimes && frame_pts_ < pts ; i++ )
828 while( bFinished == 0 && !is_eof( ) )
830 is_eof_ = av_read_frame( p_fctx_, &packet ) < 0;
833 if( packet.stream_index == video_stream_index_ )
835 frame_pts_ = av_rescale_( AV_TIME_BASE, packet.pts < 0 ? packet.dts : packet.pts, ( int64_t ) stream->time_base.num, stream->time_base.den );
840 avcodec_get_frame_defaults( p_frame_src_ );
841 #if LIBAVCODEC_VERSION_MAJOR < 53
842 avcodec_decode_video( p_cctx, p_frame_src_, &bFinished, packet.data, packet.size );
844 avcodec_decode_video2( p_cctx, p_frame_src_, &bFinished, &packet );
849 if( bFinished != 0 && ( i == ntimes || frame_pts_ >= pts ) )
852 sws_scale( p_swscale_, p_frame_src_->data, p_frame_src_->linesize, 0, p_cctx->height, p_frame_rgb_->data, p_frame_rgb_->linesize );
855 av_free_packet( &packet );
859 return( bFinished != 0 );
864 AVStream *stream = p_fctx_->streams[ video_stream_index_ ];
865 AVCodecContext *p_cctx = stream->codec;
868 frame_pts_ = pts - 1;
869 for( difference_type i = 1 ; !is_eof( ) && frame_pts_ < pts ; i++ )
872 while( bFinished == 0 && !is_eof( ) )
874 is_eof_ = av_read_frame( p_fctx_, &packet ) < 0;
877 if( packet.stream_index == video_stream_index_ )
879 frame_pts_ = av_rescale_( AV_TIME_BASE, packet.pts < 0 ? packet.dts : packet.pts, ( int64_t ) stream->time_base.num, stream->time_base.den );
884 avcodec_get_frame_defaults( p_frame_src_ );
885 #if defined( LIBAVCODEC_VERSION_MAJOR ) && LIBAVCODEC_VERSION_MAJOR >= 53
886 avcodec_decode_video2( p_cctx, p_frame_src_, &bFinished, &packet );
888 avcodec_decode_video( p_cctx, p_frame_src_, &bFinished, packet.data, packet.size );
893 if( bFinished != 0 && frame_pts_ >= pts )
896 sws_scale( p_swscale_, p_frame_src_->data, p_frame_src_->linesize, 0, p_cctx->height, p_frame_rgb_->data, p_frame_rgb_->linesize );
899 av_free_packet( &packet );
903 return( bFinished != 0 );
912 int64_t av_rescale( int64_t a, int64_t b, int64_t c )
const
914 long double A =
static_cast< long double >( a );
915 long double B =
static_cast< long double >( b );
916 long double C =
static_cast< long double >( c );
917 long double D = A * ( B / C );
918 return( static_cast< int64_t >( D + 0.5 ) );
921 int64_t av_rescale_( int64_t s, int64_t a, int64_t b, int64_t c )
const
923 long double S =
static_cast< long double >( s );
924 long double A =
static_cast< long double >( a );
925 long double B =
static_cast< long double >( b );
926 long double C =
static_cast< long double >( c );
927 long double D = S * ( A * ( B / C ) );
928 return( static_cast< int64_t >( D + 0.5 ) );
931 int64_t av_rescale( int64_t a, int64_t b, int64_t c, int64_t d )
const
933 long double A =
static_cast< long double >( a );
934 long double B =
static_cast< long double >( b );
935 long double C =
static_cast< long double >( c );
936 long double D =
static_cast< long double >( d );
937 long double BC = B / C;
938 long double AD = A / D;
939 return( static_cast< int64_t >( AD * BC + 0.5 ) );
953 this->seek_frame( this->frame_id( ) - 1 );
974 this->seek_frame( this->frame_id( ) - fno );
1030 encoder( size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0,
1031 size_type bit_rate = 1150000, size_type qmin = 0, size_type qmax = 0, size_type gop_size = 12, size_type max_b_frames = 2, size_type audio_bit_rate = 64000, size_type audio_sampling_rate = 44100, size_type audio_channels = 2 )
1032 : p_fctx_( NULL ), p_frame_dst_( NULL ), p_frame_rgb_( NULL ), p_swscale_( NULL ), frame_pts_( 0 ), is_open_( false ), encode_buf_( NULL ), encode_buf_size_( 0 ),
1033 width_( w ), height_( h ), source_width_( w ), source_height_( h ), frame_rate_num_( frame_rate_num ), frame_rate_den_( frame_rate_den ), frame_aspect_ratio_( frame_aspect_ratio ), bit_rate_( bit_rate ), qmin_( qmin ), qmax_( qmax ),
1034 gop_size_( gop_size ), max_b_frames_( max_b_frames ), audio_bit_rate_( audio_bit_rate ), audio_sampling_rate_( audio_bit_rate ), audio_channels_( audio_channels )
1039 avcodec_register_all( );
1042 bInitialized =
true;
1055 virtual bool is_open( )
const {
return( is_open_ ); }
1058 virtual bool is_eof( )
const {
return(
false ); }
1061 virtual bool dump( )
const
1065 #if LIBAVFORMAT_VERSION_MAJOR < 54
1066 dump_format( p_fctx_, 0, p_fctx_->filename,
true );
1068 av_dump_format( p_fctx_, 0, p_fctx_->filename,
true );
1079 virtual long double time( )
const
1083 AVStream *stream = p_fctx_->streams[ 0 ];
1084 int64_t pts = av_rescale( frame_pts_ * AV_TIME_BASE, stream->time_base.num, stream->time_base.den );
1085 return( static_cast< long double >( pts ) / static_cast< long double >( AV_TIME_BASE ) );
1094 virtual long double duration( )
const
1107 virtual const std::string filename( )
const
1111 return( p_fctx_->filename );
1120 virtual size_type bit_rate( )
const
1122 return( bit_rate_ );
1126 virtual size_type width( )
const
1132 virtual size_type height( )
const
1138 virtual long double frame_rate_numerator( )
const
1140 return( frame_rate_num_ );
1144 virtual long double frame_rate_denominator( )
const
1146 return( frame_rate_den_ );
1150 virtual long double frame_aspect_ratio( )
const
1152 return( frame_aspect_ratio_ );
1156 virtual bool open(
const std::string &filename )
1158 return( open( filename, filename,
"",
"" ) );
1169 bool open(
const std::string &filename,
const std::string &format_type,
const std::string &video_type,
const std::string &mime_type, codec_id_type codec_id = CODEC_ID_NONE )
1174 AVOutputFormat *format = guess_video_format( format_type, video_type, mime_type );
1175 if( format == NULL )
1177 std::cerr <<
"Could not find specified video format" << std::endl;
1182 return( open( filename, format, codec_id ) );
1198 sws_freeContext( p_swscale_ );
1201 av_write_trailer( p_fctx_ );
1203 if( ( p_fctx_->oformat->flags & AVFMT_NOFILE ) == 0 )
1205 #if defined( LIBAVCODEC_VERSION_MAJOR ) && LIBAVCODEC_VERSION_MAJOR >= 53
1206 if( avio_close( p_fctx_->pb ) < 0 )
1208 if( url_fclose( p_fctx_->pb ) < 0 )
1211 std::cerr <<
"Failed to close output file." << std::endl;
1217 for( size_type i = 0 ; i < p_fctx_->nb_streams ; i++ )
1220 if( p_fctx_->streams[ i ]->codec->codec_id != CODEC_ID_NONE )
1222 avcodec_close( p_fctx_->streams[ i ]->codec );
1225 av_freep( &( p_fctx_->streams[ i ] ->codec ) );
1226 av_freep( &( p_fctx_->streams[ i ] ) );
1230 if( encode_buf_ != NULL )
1232 av_freep( &encode_buf_ );
1236 free_frame( &p_frame_dst_ );
1237 free_frame( &p_frame_rgb_ );
1238 av_freep( &p_fctx_ );
1259 bool open(
const std::string &filename, AVOutputFormat *format, codec_id_type video_codec_id = CODEC_ID_NONE, codec_id_type audio_codec_id = CODEC_ID_NONE, PixelFormat default_pixel_format = PIX_FMT_YUV420P )
1263 if( format == NULL )
1265 std::cerr <<
"Could not find specified video format" << std::endl;
1269 #if LIBAVFORMAT_VERSION_MAJOR < 53
1270 p_fctx_ = av_alloc_format_context( );
1272 p_fctx_ = avformat_alloc_context( );
1275 if( p_fctx_ == NULL )
1277 std::cerr <<
"Could not allocate format context" << std::endl;
1281 p_fctx_->oformat = format;
1283 _snprintf( p_fctx_->filename,
sizeof( p_fctx_->filename ),
"%s", filename.c_str( ) );
1285 snprintf( p_fctx_->filename,
sizeof( p_fctx_->filename ),
"%s", filename.c_str( ) );
1288 AVStream *vstream = NULL;
1289 if( p_fctx_->oformat->video_codec != CODEC_ID_NONE )
1291 vstream = av_new_stream( p_fctx_, 0 );
1292 if( vstream == NULL )
1294 std::cerr <<
"Could not allocate video encode stream" << std::endl;
1299 if( video_codec_id == CODEC_ID_NONE )
1301 video_codec_id = av_guess_codec( p_fctx_->oformat, NULL, p_fctx_->filename, NULL, avmedia_type_video_id );
1305 AVCodec *codec = avcodec_find_encoder( video_codec_id );
1306 if( codec == NULL && p_fctx_->oformat->video_codec != CODEC_ID_NONE )
1308 std::cerr <<
"Could not find appropriate Video Codec from the database." << std::endl;
1313 #if LIBAVCODEC_VERSION_MAJOR < 55
1314 avcodec_get_context_defaults2( vstream->codec, avmedia_type_video_id );
1316 avcodec_get_context_defaults3( vstream->codec, codec );
1320 AVCodecContext *cctx = vstream->codec;
1321 cctx->codec_id = codec == NULL ? video_codec_id : codec->id;
1322 cctx->codec_type = avmedia_type_video_id;
1325 cctx->width =
static_cast< int >( width( ) );
1326 cctx->height =
static_cast< int >( height( ) );
1329 cctx->pix_fmt = default_pixel_format;
1332 if( codec->pix_fmts )
1334 const PixelFormat *p = codec->pix_fmts;
1335 for( ; *p != -1 ; p++ )
1337 if( *p == cctx->pix_fmt )
1346 cctx->pix_fmt = codec->pix_fmts[ 0 ];
1351 cctx->bit_rate =
static_cast< int >( bit_rate( ) );
1354 if( qmin_ > 0 && qmax_ > 0 )
1361 cctx->time_base.den =
static_cast< int >( frame_rate_denominator( ) );
1362 cctx->time_base.num =
static_cast< int >( frame_rate_numerator( ) );
1364 cctx->gop_size = gop_size_;
1367 cctx->sample_aspect_ratio = vstream->sample_aspect_ratio = av_d2q( frame_aspect_ratio_ * cctx->height / cctx->width, 255 );
1369 if( cctx->codec_id == CODEC_ID_MPEG2VIDEO )
1371 cctx->max_b_frames =
static_cast< int >( max_b_frames_ );
1373 if( cctx->codec_id == CODEC_ID_MPEG1VIDEO )
1375 cctx->mb_decision = 2;
1379 if( ( format->flags & AVFMT_GLOBALHEADER ) != 0 )
1381 cctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
1384 #if LIBAVFORMAT_VERSION_MAJOR < 54
1386 if( av_set_parameters( p_fctx_, NULL ) < 0 )
1388 std::cerr <<
"Invalid output format parameters" << std::endl;
1394 #if LIBAVCODEC_VERSION_MAJOR < 55
1395 if( avcodec_open( cctx, codec ) < 0 )
1397 if( avcodec_open2( cctx, codec, NULL ) < 0 )
1400 std::cerr <<
"Could not open codec." << std::endl;
1405 if( audio_codec_id != CODEC_ID_NONE )
1407 AVStream *astream = av_new_stream( p_fctx_, 1 );
1408 if( astream == NULL )
1410 std::cerr <<
"Could not allocate audio encode stream" << std::endl;
1414 astream->codec->codec_id = audio_codec_id;
1415 astream->codec->codec_type = avmedia_type_video_id;
1418 astream->codec->bit_rate =
static_cast< int >( audio_bit_rate_ );
1419 astream->codec->sample_rate =
static_cast< int >( audio_sampling_rate_ );
1420 astream->codec->channels =
static_cast< int >( audio_channels_ );
1423 AVCodec *codec = avcodec_find_encoder( astream->codec->codec_id );
1426 std::cerr <<
"Could not find appropriate Audio Codec from the database." << std::endl;
1431 #if LIBAVCODEC_VERSION_MAJOR < 55
1432 if( avcodec_open( astream->codec, codec ) < 0 )
1434 if( avcodec_open2( astream->codec, codec, NULL ) < 0 )
1437 std::cerr <<
"Could not open codec." << std::endl;
1443 if( ( p_fctx_->oformat->flags & AVFMT_NOFILE ) == 0 )
1445 #if defined( AVIO_FLAG_WRITE )
1446 if( avio_open( &p_fctx_->pb, p_fctx_->filename, AVIO_FLAG_WRITE ) < 0 )
1447 #elif defined( URL_WRONLY )
1448 if( url_fopen( &p_fctx_->pb, p_fctx_->filename, URL_WRONLY ) < 0 )
1450 if( avio_open( &p_fctx_->pb, p_fctx_->filename, AVIO_WRONLY ) < 0 )
1453 std::cerr <<
"Could not open file " << filename << std::endl;
1459 p_fctx_->max_delay =
static_cast< int >( 0.7 * AV_TIME_BASE );
1463 #if LIBAVFORMAT_VERSION_MAJOR < 54
1464 if( av_write_header( p_fctx_ ) != 0 )
1466 if( avformat_write_header( p_fctx_, NULL ) != 0 )
1469 std::cerr <<
"Failed to write video header" << std::endl;
1474 encode_buf_size_ = width( ) * height( ) * 4;
1475 if( encode_buf_size_ < FF_MIN_BUFFER_SIZE )
1477 encode_buf_size_ = FF_MIN_BUFFER_SIZE;
1480 if( ( p_fctx_->oformat->flags & AVFMT_RAWPICTURE ) == 0 )
1482 encode_buf_ = ( uint8_t * )av_malloc( encode_buf_size_ );
1485 PixelFormat pix_fmt = vstream == NULL ? PIX_FMT_BGR24 : vstream->codec->pix_fmt;
1488 p_frame_dst_ = allocate_frame( width( ), height( ), pix_fmt );
1489 if( p_frame_dst_ == NULL )
1491 std::cerr <<
"Could not allocate frame buffer." << std::endl;
1496 p_frame_rgb_ = allocate_frame( width( ), height( ), PIX_FMT_BGR24 );
1497 if( p_frame_dst_ == NULL )
1499 std::cerr <<
"Could not allocate temporal frame buffer." << std::endl;
1504 source_width_ = width( );
1505 source_height_ = height( );
1506 p_swscale_ = sws_getContext( source_width_, source_height_, PIX_FMT_BGR24, width( ), height( ), pix_fmt, SWS_LANCZOS, NULL, NULL, NULL);
1525 template <
class T,
class Allocator >
1532 if( source_width_ != image.
width( ) || source_height_ != image.
height( ) )
1534 if( p_swscale_ != NULL )
1537 sws_freeContext( p_swscale_ );
1540 free_frame( &p_frame_rgb_ );
1543 PixelFormat pix_fmt = p_fctx_->streams[ 0 ] == NULL ? PIX_FMT_BGR24 : p_fctx_->streams[ 0 ]->codec->pix_fmt;
1545 if( source_width_ < image.
width( ) )
1547 p_swscale_ = sws_getContext( image.
width( ), image.
height( ), PIX_FMT_BGR24, width( ), height( ), pix_fmt, SWS_AREA, NULL, NULL, NULL);
1551 p_swscale_ = sws_getContext( image.
width( ), image.
height( ), PIX_FMT_BGR24, width( ), height( ), pix_fmt, SWS_LANCZOS, NULL, NULL, NULL);
1554 source_width_ = image.
width( );
1555 source_height_ = image.
height( );
1556 p_frame_rgb_ = allocate_frame( source_width_, source_height_, PIX_FMT_BGR24 );
1559 typedef _pixel_converter_< T > pixel_converter;
1560 typedef typename pixel_converter::color_type color_type;
1562 unsigned char *p = p_frame_rgb_->data[ 0 ];
1563 if( ( source_width_ % 2 ) == 0 )
1565 for( size_type i = 0 ; i < image.
size( ) ; i++ )
1567 color_type c = pixel_converter::convert_from( image[ i ] );
1577 size_type stride = p_frame_rgb_->linesize[ 0 ];
1578 for( size_type j = 0 ; j < image.
height( ) ; j++ )
1580 for( size_type i = 0 ; i < stride ; i += 3 )
1582 color_type c = pixel_converter::convert_from( *pi++ );
1593 sws_scale( p_swscale_, p_frame_rgb_->data, p_frame_rgb_->linesize, 0, source_height_, p_frame_dst_->data, p_frame_dst_->linesize );
1595 if( ( p_fctx_->oformat->flags & AVFMT_RAWPICTURE ) != 0 )
1598 av_init_packet( &packet );
1600 #if defined( AV_PKT_FLAG_KEY )
1601 packet.flags |= AV_PKT_FLAG_KEY;
1603 packet.flags |= PKT_FLAG_KEY;
1605 packet.stream_index = p_fctx_->streams[ 0 ]->index;
1606 packet.data = ( uint8_t * )p_frame_dst_;
1607 packet.size =
sizeof( AVPicture );
1610 ret = av_write_frame( p_fctx_, &packet );
1615 AVStream *stream = p_fctx_->streams[ 0 ];
1616 AVCodecContext *c = stream->codec;
1618 p_frame_dst_->quality = 1;
1619 int out_size = avcodec_encode_video( c, encode_buf_, encode_buf_size_, p_frame_dst_ );
1624 av_init_packet( &packet );
1626 packet.stream_index = stream->index;
1627 packet.data = encode_buf_;
1628 packet.size = out_size;
1631 packet.pts = av_rescale_q( c->coded_frame->pts, c->time_base, stream->time_base );
1632 frame_pts_ =
static_cast< difference_type
>( packet.pts );
1636 packet.pts = av_rescale( static_cast< int64_t >( tm * AV_TIME_BASE ), stream->time_base.den, stream->time_base.num * AV_TIME_BASE );
1637 frame_pts_ =
static_cast< difference_type
>( packet.pts );
1640 if( c->coded_frame->key_frame )
1642 #if defined( AV_PKT_FLAG_KEY )
1643 packet.flags |= AV_PKT_FLAG_KEY;
1645 packet.flags |= PKT_FLAG_KEY;
1649 ret = av_interleaved_write_frame( p_fctx_, &packet );
1692 encoder( size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000, size_type qmin = 0, size_type qmax = 0, size_type gop_size = 12, size_type max_b_frames = 2 )
1693 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate, qmin, qmax, gop_size, max_b_frames )
1713 encoder(
const std::string &filename, size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000, size_type qmin = 0, size_type qmax = 0, size_type gop_size = 12, size_type max_b_frames = 2 )
1714 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate, qmin, qmax, gop_size, max_b_frames )
1716 if( !open( filename ) )
1730 virtual bool open(
const std::string &filename )
1732 return( base::open( filename,
".mpg",
"mpeg",
"" ) );
1758 encoder( size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000 )
1759 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate )
1775 encoder(
const std::string &filename, size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000 )
1776 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate )
1778 if( !open( filename ) )
1792 virtual bool open(
const std::string &filename )
1795 return( base::open( filename,
".avi",
"",
"" ) );
1823 encoder( size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 11500000, size_type qmin = 0, size_type qmax = 0 )
1824 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate, qmin, qmax, 0 )
1842 encoder(
const std::string &filename, size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 11500000, size_type qmin = 0, size_type qmax = 0 )
1843 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate, qmin, qmax, 0 )
1845 if( !open( filename ) )
1859 virtual bool open(
const std::string &filename )
1862 return( base::open( filename,
".avi",
"",
"", CODEC_ID_MJPEG ) );
1888 encoder( size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000 )
1889 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate )
1905 encoder(
const std::string &filename, size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000 )
1906 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate )
1908 if( !open( filename ) )
1922 virtual bool open(
const std::string &filename )
1925 return( base::open( filename,
".avi",
"",
"", CODEC_ID_H264 ) );
1951 encoder( size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000 )
1952 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate )
1968 encoder(
const std::string &filename, size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0, size_type bit_rate = 1150000 )
1969 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio, bit_rate )
1971 if( !open( filename ) )
1985 virtual bool open(
const std::string &filename )
1988 return( base::open( filename,
".wmv",
"",
"", CODEC_ID_WMV2 ) );
2011 encoder( size_type w = 720, size_type h = 480,
double frame_aspect_ratio = 4.0 / 3.0 )
2012 :
base( w, h, 1001, 30000, frame_aspect_ratio )
2014 audio_sampling_rate_ = 48000;
2026 encoder(
const std::string &filename, size_type w = 720, size_type h = 480,
double frame_aspect_ratio = 4.0 / 3.0 )
2027 :
base( w, h, 1001, 30000, frame_aspect_ratio )
2029 audio_sampling_rate_ = 48000;
2031 if( !open( filename ) )
2045 virtual bool open(
const std::string &filename )
2047 AVOutputFormat *fmt = guess_video_format(
".avi",
"",
"" );
2048 return( base::open( filename, fmt, CODEC_ID_DVVIDEO, CODEC_ID_NONE, PIX_FMT_YUV411P ) );
2073 encoder( size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0 )
2074 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio )
2089 encoder(
const std::string &filename, size_type w = 320, size_type h = 240, size_type frame_rate_num = 1, size_type frame_rate_den = 30,
double frame_aspect_ratio = 4.0 / 3.0 )
2090 :
base( w, h, frame_rate_num, frame_rate_den, frame_aspect_ratio )
2092 if( !open( filename ) )
2106 virtual bool open(
const std::string &filename )
2108 return( base::open( filename,
".avi",
"",
"", CODEC_ID_RAWVIDEO ) );
2114 template <
class T,
class Allocator >
2115 inline decoder &operator >>( decoder &in, array2< T, Allocator > &img )
2121 template <
class T,
class Allocator >
2122 inline encoder &operator <<( encoder &in, array2< T, Allocator > &img )
2134 template<
typename Stream1,
typename Stream2 >
2139 for(
size_t i = 0 ; i < num ; i ++ )
2141 if( src.read( tmp ) )
2143 if( !dst.write( tmp ) )
2168 #endif // __INCLUDE_IO_VIDEO_H__