Ejemplo n.º 1
0
int avcodec_get_context_defaults3(AVCodecContext *s, AVCodec *codec){
    avcodec_get_context_defaults2(s, codec ? codec->type : AVMEDIA_TYPE_UNKNOWN);
    if(codec && codec->priv_data_size){
        if(!s->priv_data){
            s->priv_data= av_mallocz(codec->priv_data_size);
            if (!s->priv_data) {
                return AVERROR(ENOMEM);
            }
        }
        if(codec->priv_class){
            *(AVClass**)s->priv_data= codec->priv_class;
            av_opt_set_defaults(s->priv_data);
        }
    }
    if (codec && codec->defaults) {
        int ret;
        AVCodecDefault *d = codec->defaults;
        while (d->key) {
            ret = av_set_string3(s, d->key, d->value, 0, NULL);
            av_assert0(ret >= 0);
            d++;
        }
    }
    return 0;
}
Ejemplo n.º 2
0
int avcodec_get_context_defaults3(AVCodecContext *s, AVCodec *codec)
{
/*
	参数:
		1、
		
	返回:
		1、
		
	说明:
		1、
*/
	avcodec_get_context_defaults2(s, codec ? codec->type : AVMEDIA_TYPE_UNKNOWN);
	if(codec && codec->priv_data_size)
	{
		if(!s->priv_data)
		{
			s->priv_data= av_mallocz(codec->priv_data_size);
			if (!s->priv_data) 
			{
				return AVERROR(ENOMEM);
			}
		}
		
		if(codec->priv_class)
		{
			*(AVClass**)s->priv_data= codec->priv_class;
			av_opt_set_defaults(s->priv_data);
		}
	}
	return 0;
}
Ejemplo n.º 3
0
int janus_pp_h264_create(char *destination) {
	if(destination == NULL)
		return -1;
	/* Setup FFmpeg */
	av_register_all();
	/* Adjust logging to match the postprocessor's */
	av_log_set_level(janus_log_level <= LOG_NONE ? AV_LOG_QUIET :
		(janus_log_level == LOG_FATAL ? AV_LOG_FATAL :
			(janus_log_level == LOG_ERR ? AV_LOG_ERROR :
				(janus_log_level == LOG_WARN ? AV_LOG_WARNING :
					(janus_log_level == LOG_INFO ? AV_LOG_INFO :
						(janus_log_level == LOG_VERB ? AV_LOG_VERBOSE : AV_LOG_DEBUG))))));
	/* MP4 output */
	fctx = avformat_alloc_context();
	if(fctx == NULL) {
		JANUS_LOG(LOG_ERR, "Error allocating context\n");
		return -1;
	}
	fctx->oformat = av_guess_format("mp4", NULL, NULL);
	if(fctx->oformat == NULL) {
		JANUS_LOG(LOG_ERR, "Error guessing format\n");
		return -1;
	}
	snprintf(fctx->filename, sizeof(fctx->filename), "%s", destination);
	vStream = avformat_new_stream(fctx, 0);
	if(vStream == NULL) {
		JANUS_LOG(LOG_ERR, "Error adding stream\n");
		return -1;
	}
#if LIBAVCODEC_VER_AT_LEAST(53, 21)
	avcodec_get_context_defaults3(vStream->codec, AVMEDIA_TYPE_VIDEO);
#else
	avcodec_get_context_defaults2(vStream->codec, AVMEDIA_TYPE_VIDEO);
#endif
#if LIBAVCODEC_VER_AT_LEAST(54, 25)
	vStream->codec->codec_id = AV_CODEC_ID_H264;
#else
	vStream->codec->codec_id = CODEC_ID_H264;
#endif
	vStream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
	vStream->codec->time_base = (AVRational){1, fps};
	vStream->time_base = (AVRational){1, 90000};
	vStream->codec->width = max_width;
	vStream->codec->height = max_height;
	vStream->codec->pix_fmt = PIX_FMT_YUV420P;
	//~ if (fctx->flags & AVFMT_GLOBALHEADER)
		vStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
	if(avio_open(&fctx->pb, fctx->filename, AVIO_FLAG_WRITE) < 0) {
		JANUS_LOG(LOG_ERR, "Error opening file for output\n");
		return -1;
	}
	if(avformat_write_header(fctx, NULL) < 0) {
		JANUS_LOG(LOG_ERR, "Error writing header\n");
		return -1;
	}
	return 0;
}
Ejemplo n.º 4
0
AVCodecContext *avcodec_alloc_context2(enum AVMediaType codec_type){
    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));

    if(avctx==NULL) return NULL;

    avcodec_get_context_defaults2(avctx, codec_type);

    return avctx;
}
Ejemplo n.º 5
0
int CFFDecode::Init(int codec_id, int width, int height, int fps, char *pFileNameThumbBmp)
{
	if ((width & 1) == 1) return E_INVALIDARG;
	if ((height & 1) == 1) return E_INVALIDARG;

	strcpy_s(m_fileNamePic, pFileNameThumbBmp);
	DbgOut(m_fileNamePic);
	m_codec = avcodec_find_decoder((CodecID)codec_id);
	if (m_codec == NULL) return E_FAIL;

	m_c = avcodec_alloc_context(); 
	if (m_c == NULL) return E_OUTOFMEMORY;

	avcodec_get_context_defaults2(m_c, CODEC_TYPE_VIDEO);
	avcodec_get_frame_defaults(&m_picture);
	
	m_c->codec_id = (CodecID) codec_id;
	m_c->codec_type = CODEC_TYPE_VIDEO;
	m_c->strict_std_compliance = 0;

	/* put sample parameters */
	//m_c->bit_rate = bitrate;
	/* resolution must be a multiple of two */
	m_c->width = width;
	m_c->height = height;
	/* time base: this is the fundamental unit of time (in seconds) in terms
	   of which frame timestamps are represented. for fixed-fps content,
	   timebase should be 1/framerate and timestamp increments should be
	   identically 1. */
	m_c->time_base.den = fps;
	m_c->time_base.num = 1;
	m_c->gop_size = 12; /* emit one intra frame every twelve frames at most */
	m_c->pix_fmt = PIX_FMT_YUV420P;
	if (m_c->codec_id == CODEC_ID_MPEG2VIDEO) {
		/* just for testing, we also add B frames */
		m_c->max_b_frames = 2;
	}
	if (m_c->codec_id == CODEC_ID_MPEG1VIDEO){
		/* needed to avoid using macroblocks in which some coeffs overflow
		   this doesnt happen with normal video, it just happens here as the
		   motion of the chroma plane doesnt match the luma plane */
		m_c->mb_decision=2;
	}

	/* open the codec */
	if (avcodec_open(m_c, m_codec) < 0) {
		return E_FAIL;
	}

	tmp_picture = NULL;
	
	return S_OK;
}
Ejemplo n.º 6
0
void avcodec_get_context_defaults(AVCodecContext *s)
{
/*
	参数:
		1、
		
	返回:
		1、
		
	说明:
		1、
*/
    	avcodec_get_context_defaults2(s, AVMEDIA_TYPE_UNKNOWN);
}
Ejemplo n.º 7
0
static VALUE output_initialize(VALUE self, VALUE input) {
	AVFormatContext *f = NULL;
	AVCodecContext *codecContext = NULL;
	Data_Get_Struct(self, AVCodecContext, codecContext);
	avcodec_get_context_defaults2(codecContext, CODEC_TYPE_VIDEO);
	AVCodec *codec = avcodec_find_encoder(CODEC_ID_RAWVIDEO);
	codecContext->pix_fmt = PIX_FMT_YUV420P;
	codecContext->time_base = (AVRational){1,25};
	codecContext->width = FIX2INT(rb_iv_get(input, "@width"));
	codecContext->height = FIX2INT(rb_iv_get(input, "@height"));
	if(avcodec_open(codecContext, codec) != 0) {
		fprintf(stderr, "Failed to open codec.");
		exit(-1);
	}
	DATA_PTR(self) = codecContext;
	return self;
}
Ejemplo n.º 8
0
AVCodecContext *avcodec_alloc_context2(enum AVMediaType codec_type)
{
/*
	参数:
		1、codec_type	: 传入一个解码的类型
		
	返回:
		1、
		
	说明:
		1、
*/
	AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext)); /* 分配内存*/

	if(avctx==NULL) 
		return NULL;

	avcodec_get_context_defaults2(avctx, codec_type); /* 获取默认值填充AVCodecContext   内存*/

	return avctx;
}
Ejemplo n.º 9
0
/* 
 * add an audio output stream
 */
static AVStream *add_audio_stream(AVFormatContext *oc, enum CodecID codec_id)
{
    AVCodecContext *c;
    AVStream *st;

    st = av_new_stream(oc, 1);
    if (!st) {
        fprintf(stderr, "Could not alloc stream\n");
        return NULL;
    }
    avcodec_get_context_defaults2(st->codec, CODEC_TYPE_AUDIO);

    c = st->codec;
    c->codec_id = codec_id;
    c->codec_type = CODEC_TYPE_AUDIO;

    /* put sample parameters */
    c->bit_rate = 256000;
    c->sample_rate = 48000;
    c->channels = 2;
    return st;
}
Ejemplo n.º 10
0
void avcodec_get_context_defaults(AVCodecContext *s){
    avcodec_get_context_defaults2(s, CODEC_TYPE_UNKNOWN);
}
Ejemplo n.º 11
0
int janus_pp_h264_create(char *destination, char *metadata) {
	if(destination == NULL)
		return -1;
	/* Setup FFmpeg */
	av_register_all();
	/* Adjust logging to match the postprocessor's */
	av_log_set_level(janus_log_level <= LOG_NONE ? AV_LOG_QUIET :
		(janus_log_level == LOG_FATAL ? AV_LOG_FATAL :
			(janus_log_level == LOG_ERR ? AV_LOG_ERROR :
				(janus_log_level == LOG_WARN ? AV_LOG_WARNING :
					(janus_log_level == LOG_INFO ? AV_LOG_INFO :
						(janus_log_level == LOG_VERB ? AV_LOG_VERBOSE : AV_LOG_DEBUG))))));
	/* MP4 output */
	fctx = avformat_alloc_context();
	if(fctx == NULL) {
		JANUS_LOG(LOG_ERR, "Error allocating context\n");
		return -1;
	}
	/* We save the metadata part as a comment (see #1189) */
	if(metadata)
		av_dict_set(&fctx->metadata, "comment", metadata, 0);
	fctx->oformat = av_guess_format("mp4", NULL, NULL);
	if(fctx->oformat == NULL) {
		JANUS_LOG(LOG_ERR, "Error guessing format\n");
		return -1;
	}
	snprintf(fctx->filename, sizeof(fctx->filename), "%s", destination);
#ifdef USE_CODECPAR
	AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
	if(!codec) {
		/* Error opening video codec */
		JANUS_LOG(LOG_ERR, "Encoder not available\n");
		return -1;
	}
	fctx->video_codec = codec;
	fctx->oformat->video_codec = codec->id;
	vStream = avformat_new_stream(fctx, codec);
	vStream->id = fctx->nb_streams-1;
	vEncoder = avcodec_alloc_context3(codec);
	vEncoder->width = max_width;
	vEncoder->height = max_height;
	vEncoder->time_base = (AVRational){ 1, fps };
	vEncoder->pix_fmt = AV_PIX_FMT_YUV420P;
	vEncoder->flags |= CODEC_FLAG_GLOBAL_HEADER;
	if(avcodec_open2(vEncoder, codec, NULL) < 0) {
		/* Error opening video codec */
		JANUS_LOG(LOG_ERR, "Encoder error\n");
		return -1;
	}
	avcodec_parameters_from_context(vStream->codecpar, vEncoder);
#else
	vStream = avformat_new_stream(fctx, 0);
	if(vStream == NULL) {
		JANUS_LOG(LOG_ERR, "Error adding stream\n");
		return -1;
	}
#if LIBAVCODEC_VER_AT_LEAST(53, 21)
	avcodec_get_context_defaults3(vStream->codec, AVMEDIA_TYPE_VIDEO);
#else
	avcodec_get_context_defaults2(vStream->codec, AVMEDIA_TYPE_VIDEO);
#endif
#if LIBAVCODEC_VER_AT_LEAST(54, 25)
	vStream->codec->codec_id = AV_CODEC_ID_H264;
#else
	vStream->codec->codec_id = CODEC_ID_H264;
#endif
	vStream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
	vStream->codec->time_base = (AVRational){1, fps};
	vStream->time_base = (AVRational){1, 90000};
	vStream->codec->width = max_width;
	vStream->codec->height = max_height;
	vStream->codec->pix_fmt = PIX_FMT_YUV420P;
	//~ if (fctx->flags & AVFMT_GLOBALHEADER)
		vStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
#endif
	if(avio_open(&fctx->pb, fctx->filename, AVIO_FLAG_WRITE) < 0) {
		JANUS_LOG(LOG_ERR, "Error opening file for output\n");
		return -1;
	}
	if(avformat_write_header(fctx, NULL) < 0) {
		JANUS_LOG(LOG_ERR, "Error writing header\n");
		return -1;
	}
	return 0;
}
Ejemplo n.º 12
0
int CFfContainer::AddVideoStream( int codec_id, int width, int height, int fps )
{_STT();
	if ( !m_pFormatContext )
		return -1;

	if ( !m_nWrite )
		return -1;

	if ( 0 <= m_nVideoStream )
		return m_nVideoStream;

	// Sanity check on video parameters
	if ( 0 >= width || 0 >= height || 0 >= fps )
		return -1;

	AVStream *pst = avformat_new_stream( m_pFormatContext, avcodec_find_encoder( (CodecID)codec_id ) );
//	AVStream *pst = av_new_stream( m_pFormatContext, 0 );
	if ( !pst )
		return -1;

	AVCodecContext *pcc = pst->codec;
	if ( !pcc )
		return -1;

	// Save stream index
	m_nVideoStream = pst->index;

	// Get defaults
	avcodec_get_context_defaults2( pcc, AVMEDIA_TYPE_VIDEO );

	// Fill in codec info
	pcc->codec_id = (CodecID)codec_id;
	pcc->codec_type = AVMEDIA_TYPE_VIDEO;
	pcc->bit_rate = width * height * fps / 3;
	pcc->width = width;
	pcc->height = height;
//	pcc->pix_fmt = PIX_FMT_YUV420P;

	pcc->time_base.num = 1;
	pcc->time_base.den = ( 0 < m_time_base ) ? m_time_base : fps;

	pcc->gop_size = ( 0 < m_nKeyFrameInterval ) ? m_nKeyFrameInterval : fps;

	// Signal global headers if needed
	if ( 0 != ( m_pFormatContext->oformat->flags & AVFMT_GLOBALHEADER ) )
		pcc->flags |= CODEC_FLAG_GLOBAL_HEADER;

	// Set extra codec data
	if ( m_video_extra.getUsed() )
	{	m_video_extra.Resize( m_video_extra.getUsed() + FF_INPUT_BUFFER_PADDING_SIZE * 2 );
		pcc->flags |= CODEC_FLAG_GLOBAL_HEADER;
		pcc->extradata = (uint8_t*)m_video_extra._Ptr();
		pcc->extradata_size = m_video_extra.getUsed();
	} // end if

	else
	{	oex::CStr sFName( m_pFormatContext->oformat->name );
		if (  sFName == oexT( "3gp" ) || sFName == oexT( "mov" ) || sFName == oexT( "mp4" ) )
			pcc->flags |= CODEC_FLAG_GLOBAL_HEADER;
	} // end else

	return m_nVideoStream;
}
Ejemplo n.º 13
0
/* add a video output stream */
AVStream *add_video_stream(AVFormatContext *oc, int codec_id, int bit_rate, int thread_count)
{
    //Modifiying all values to dvd format values
    AVCodecContext *c;
    AVStream *st;

    oc->packet_size = 2048;
    oc->mux_rate = 10080000;
	
    st = av_new_stream(oc, 0);
    if (!st) {
        fprintf(stderr, "Could not alloc stream\n");
        return NULL;
    }
    avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);

    st->r_frame_rate.num = 25;
    st->r_frame_rate.den = 1;
    c = st->codec;
    c->codec_id = codec_id;
    c->codec_type = CODEC_TYPE_VIDEO;

    /* put dvd parameters */
    c->bit_rate = bit_rate * 1000;
    c->rc_max_rate = bit_rate * 1000; //9000000;
    c->rc_min_rate = bit_rate * 1000; //0
    c->rc_buffer_size = 224*1024*8;
    c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;

    /* parameters which were found to help the encoder cope better with difficult picture */    
    //c->max_qdiff = 10;
    //c->rc_eq = "tex^qComp";
    //c->qcompress = 0.1;
    
    /* resolution must be a multiple of two */
    c->width = 720;  
    c->height = 576;
    c->sample_aspect_ratio = av_d2q(4.0/3*576/720, 255);
    /* time base: this is the fundamental unit of time (in seconds) in terms
       of which frame timestamps are represented. for fixed-fps content,
       timebase should be 1/framerate and timestamp increments should be
       identically 1. */
    c->time_base.den = 25;  
    c->time_base.num = 1;
    c->gop_size = 15; /* emit one intra frame every 15 frames at most */
    c->pix_fmt = PIX_FMT_YUV420P;
    if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
        /* just for testing, we also add B frames */
        c->max_b_frames = 2;
    }
    if (c->codec_id == CODEC_ID_MPEG1VIDEO){
        /* needed to avoid using macroblocks in which some coeffs overflow 
           this doesnt happen with normal video, it just happens here as the 
           motion of the chroma plane doesnt match the luma plane */
        c->mb_decision=2;
    }
    // some formats want stream headers to be seperate
    if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
    
    /* initialise the threads */
    if (thread_count > 0)
    {
        avcodec_thread_init(c, thread_count);
    }

// tests
    //c->qcompress = 0.05;
    //c->qblur = 0;
    //c->rc_max_rate = bit_rate * 1000;
    //c->rc_min_rate = bit_rate * 1000;
    //c->rc_buffer_aggressivity = 1.0;//0.25;
    //c->flags |= CODEC_FLAG_INTERLACED_DCT;

    //c->bit_rate_tolerance = c->bit_rate;
    //c->mb_decision = FF_MB_DECISION_RD;

    //c->rc_buffer_size=224*1024*8*4.0;

    //c->max_qdiff = 3;
    //c->frame_skip_cmp = FF_CMP_DCTMAX;
    
    //c->rc_override_count=0;
    //c->me_threshold= 0;
    //c->intra_dc_precision= 8 - 8;
    //c->strict_std_compliance = 0;
    
    return st;
}
Ejemplo n.º 14
0
int CFfAudioDecoder::Create( int x_nCodec, int x_nFmt, int x_nChannels, int x_nSampleRate, int x_nBps )
{_STT();

	oexAutoLock ll( _g_ffmpeg_lock );
	if ( !ll.IsLocked() ) return 0;

	// Lose previous codec
	Destroy();

	m_pCodec = avcodec_find_decoder( (CodecID)x_nCodec );
	if ( !m_pCodec )
	{	oexERROR( 0, oexMks( oexT( "avcodec_find_decoder() failed : " ),
					 (int)x_nCodec, oexT( " - " ), LookupCodecName( x_nCodec ).c_str() ) );
		return 0;
	} // end if

	// Allocate context
	m_pCodecContext = avcodec_alloc_context();
	if ( !m_pCodecContext )
	{	oexERROR( 0, oexT( "avcodec_alloc_context() failed" ) );
		Destroy();
		return 0;
	} // end if

//	avcodec_get_context_defaults( m_pCodecContext );
	avcodec_get_context_defaults2( m_pCodecContext, AVMEDIA_TYPE_AUDIO );

    m_pCodecContext->codec_id = (CodecID)x_nCodec;
    m_pCodecContext->codec_type = AVMEDIA_TYPE_AUDIO;

#	define CNVTYPE( t, v ) case oex::obj::t : m_pCodecContext->sample_fmt = v; break;
	switch( x_nFmt )
	{	CNVTYPE( tUInt8, AV_SAMPLE_FMT_U8 );
		CNVTYPE( tInt16, AV_SAMPLE_FMT_S16 );
		CNVTYPE( tInt32, AV_SAMPLE_FMT_S32 );
		CNVTYPE( tFloat, AV_SAMPLE_FMT_FLT );
		CNVTYPE( tDouble, AV_SAMPLE_FMT_DBL );
		default : m_pCodecContext->sample_fmt = AV_SAMPLE_FMT_NONE; break;
	} // end switch

    m_pCodecContext->channels = x_nChannels;
	m_pCodecContext->sample_rate = x_nSampleRate;
    m_pCodecContext->bits_per_coded_sample = x_nBps;
    m_pCodecContext->bit_rate = m_pCodecContext->sample_rate * m_pCodecContext->channels * 8;
    m_pCodecContext->block_align = 0;

//    m_pCodecContext->strict_std_compliance = ( ( m && m->isset( oexT( "cmp" ) ) ) ? (*m)[ oexT( "cmp" ) ].toint() : 0 );

	if( 0 != ( m_pCodec->capabilities & CODEC_CAP_TRUNCATED ) )
		m_pCodecContext->flags |= CODEC_FLAG_TRUNCATED;

	// Codec context
	if ( m_extra.getUsed() )
	{	m_pCodecContext->extradata_size = m_extra.getUsed();
		m_pCodecContext->extradata = (uint8_t*)m_extra._Ptr();
		m_pCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
	} // end if

	int res = avcodec_open( m_pCodecContext, m_pCodec );
	if ( 0 > res )
	{	oexERROR( res, oexT( "avcodec_open() failed" ) );
		m_pCodecContext = oexNULL;
		Destroy();
		return 0;
	} // end if

	return 1;
}
Ejemplo n.º 15
0
/* add a video output stream */
AVStream *add_video_stream(AVFormatContext *oc, enum CodecID codec_id, int width, int height, int bit_rate, int thread_count)
{
    //Modifiying all values to ts format values
    AVCodecContext *c;
    AVStream *st;

    oc->packet_size = 2048;
    oc->mux_rate = 10080000;
	
    st = av_new_stream(oc, 0);
    if (!st) {
        fprintf(stderr, "Could not alloc stream\n");
        return NULL;
    }
    avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);

    st->r_frame_rate.num = 25;
    st->r_frame_rate.den = 1;
#if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0))
    st->sample_aspect_ratio = av_d2q(16.0/9 * height/width, 255);
#endif    
    c = st->codec;
    c->codec_id = codec_id;
    c->codec_type = CODEC_TYPE_VIDEO;

    /* put ts parameters */
    c->bit_rate = bit_rate * 1000;
    c->rc_max_rate = bit_rate * 1000; //9000000;
    c->rc_min_rate = bit_rate * 1000; //0
    c->rc_buffer_size = 224*1024*8;
    c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;

    /* parameters which were found to help the encoder cope better with difficult picture */    
    //c->max_qdiff = 10;
    //c->rc_eq = "tex^qComp";
    //c->qcompress = 0.1;
    
    /* resolution must be a multiple of two */
    c->width = width;  
    c->height = height;
#if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0))
    c->sample_aspect_ratio = st->sample_aspect_ratio;
#else
    c->sample_aspect_ratio = av_d2q(16.0/9 * height/width, 255);
#endif
    /* time base: this is the fundamental unit of time (in seconds) in terms
       of which frame timestamps are represented. for fixed-fps content,
       timebase should be 1/framerate and timestamp increments should be
       identically 1. */
    c->time_base.den = 25;  
    c->time_base.num = 1;
    c->gop_size = 1;		/* I-frame only encoding */
    c->pix_fmt = PIX_FMT_YUV420P;
    if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
        /* just for testing, we also add B frames */
        c->max_b_frames = 2;
    }

    // some formats want stream headers to be seperate
    if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
    
    /* initialise the threads */
    if (thread_count > 0)
    {
        avcodec_thread_init(c, thread_count);
    }

// tests
    //c->qcompress = 0.05;
    //c->qblur = 0;
    //c->rc_max_rate = bit_rate * 1000;
    //c->rc_min_rate = bit_rate * 1000;
    //c->rc_buffer_aggressivity = 1.0;//0.25;
    //c->flags |= CODEC_FLAG_INTERLACED_DCT;

    //c->bit_rate_tolerance = c->bit_rate;
    //c->mb_decision = FF_MB_DECISION_RD;

    //c->rc_buffer_size=224*1024*8*4.0;

    //c->max_qdiff = 3;
    //c->frame_skip_cmp = FF_CMP_DCTMAX;
    
    //c->rc_override_count=0;
    //c->me_threshold= 0;
    //c->intra_dc_precision= 8 - 8;
    //c->strict_std_compliance = 0;
    
    return st;
}
Ejemplo n.º 16
0
void avcodec_get_context_defaults(AVCodecContext *s){
    avcodec_get_context_defaults2(s, AVMEDIA_TYPE_UNKNOWN);
}
Ejemplo n.º 17
0
int FFMpegEncoder::configVideoStream()
{
	if (strcmp(profile.videoCodec,"NONE") == 0)
		return 0;

    AVStream *st = av_new_stream(pFormatCtx, 0);
    if (!st) return ERR_CREATE_VIDEO_STREAM;
	videoStream = st;
	avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);

	AVCodec *pVideoCodec = avcodec_find_encoder_by_name(profile.videoCodec);
	if (!pVideoCodec) 
		return ERR_FIND_VIDEO_CODEC;
	
	pVideoCodecCtx = st->codec;
	pVideoCodecCtx->codec_id = pVideoCodec->id;
    pVideoCodecCtx->codec_type = CODEC_TYPE_VIDEO;
    pVideoCodecCtx->bit_rate = profile.videoBitrate;
	//pVideoCodecCtx->bit_rate_tolerance = profile.videoBitrate * profile.videoFrameRate;
	pVideoCodecCtx->width = profile.videoWidth;
	pVideoCodecCtx->height = profile.videoHeight;
	pVideoCodecCtx->time_base = av_d2q(1/profile.videoFrameRate,65535);
    pVideoCodecCtx->gop_size = 12; /* emit one intra frame every twelve frames at most */
    
	const PixelFormat *fmt = pVideoCodec->pix_fmts;
	pVideoCodecCtx->pix_fmt = *fmt;
	

	//ray: CBR
	
	/*
	pVideoCodecCtx->rc_min_rate = profile.videoBitrate;
	pVideoCodecCtx->rc_max_rate = profile.videoBitrate;  
	pVideoCodecCtx->bit_rate_tolerance = profile.videoBitrate;
	pVideoCodecCtx->rc_buffer_size = profile.videoBitrate;
	pVideoCodecCtx->rc_initial_buffer_occupancy = pVideoCodecCtx->rc_buffer_size*3/4;
	pVideoCodecCtx->rc_buffer_aggressivity= (float)1.0;
	pVideoCodecCtx->rc_initial_cplx= 0.5; 
*/
	//

	//ray: constant quality
//	pVideoCodecCtx->idct_algo = FF_IDCT_IPP;
	//pVideoCodecCtx->qmin = 51;
	//pVideoCodecCtx->qmax = 51;
	//
	if (pVideoCodecCtx->codec_id == CODEC_ID_MPEG4)
	{
		pVideoCodecCtx->max_b_frames = 2;
	}
    if (pVideoCodecCtx->codec_id == CODEC_ID_MPEG2VIDEO) {
        /* just for testing, we also add B frames */
        //pVideoCodecCtx->max_b_frames = 2;
    }
    if (pVideoCodecCtx->codec_id == CODEC_ID_MPEG1VIDEO){
        /* Needed to avoid using macroblocks in which some coeffs overflow.
           This does not happen with normal video, it just happens here as
           the motion of the chroma plane does not match the luma plane. */
       // pVideoCodecCtx->mb_decision=2;

    }
	if (pVideoCodecCtx->codec_id == CODEC_ID_MSMPEG4V3)
	{
		pVideoCodecCtx->max_b_frames = 0;
		pVideoCodecCtx->mb_decision = 2;
		pVideoCodecCtx->trellis = 2;
	}
    // some formats want stream headers to be separate
    if(pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)
        pVideoCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;

	int ret = avcodec_open(pVideoCodecCtx, pVideoCodec);
	
	if ( ret < 0)
		return ERR_OPEN_VIDEO_CODEC;
	else
		return 0;
}
Ejemplo n.º 18
-1
static int
artwork_rescale(AVFormatContext *src_ctx, int s, int out_w, int out_h, int format, struct evbuffer *evbuf)
{
  uint8_t *buf;
  uint8_t *outbuf;

  AVCodecContext *src;

  AVFormatContext *dst_ctx;
  AVCodecContext *dst;
  AVOutputFormat *dst_fmt;
  AVStream *dst_st;

  AVCodec *img_decoder;
  AVCodec *img_encoder;

  int64_t pix_fmt_mask;
  const enum PixelFormat *pix_fmts;

  AVFrame *i_frame;
  AVFrame *o_frame;

  struct SwsContext *swsctx;

  AVPacket pkt;
  int have_frame;

  int outbuf_len;

  int ret;

  src = src_ctx->streams[s]->codec;

  img_decoder = avcodec_find_decoder(src->codec_id);
  if (!img_decoder)
    {
      DPRINTF(E_LOG, L_ART, "No suitable decoder found for artwork %s\n", src_ctx->filename);

      return -1;
    }

#if LIBAVCODEC_VERSION_MAJOR >= 54 || (LIBAVCODEC_VERSION_MAJOR == 53 && LIBAVCODEC_VERSION_MINOR >= 6)
  ret = avcodec_open2(src, img_decoder, NULL);
#else
  ret = avcodec_open(src, img_decoder);
#endif
  if (ret < 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not open codec for decoding: %s\n", strerror(AVUNERROR(ret)));

      return -1;
    }

  /* Set up output */
#if LIBAVFORMAT_VERSION_MAJOR >= 53 || (LIBAVFORMAT_VERSION_MAJOR == 52 && LIBAVFORMAT_VERSION_MINOR >= 45)
  /* FFmpeg 0.6 */
  dst_fmt = av_guess_format("image2", NULL, NULL);
#else
  dst_fmt = guess_format("image2", NULL, NULL);
#endif
  if (!dst_fmt)
    {
      DPRINTF(E_LOG, L_ART, "ffmpeg image2 muxer not available\n");

      ret = -1;
      goto out_close_src;
    }

  dst_fmt->video_codec = CODEC_ID_NONE;

  /* Try to keep same codec if possible */
  if ((src->codec_id == CODEC_ID_PNG) && (format & ART_CAN_PNG))
    dst_fmt->video_codec = CODEC_ID_PNG;
  else if ((src->codec_id == CODEC_ID_MJPEG) && (format & ART_CAN_JPEG))
    dst_fmt->video_codec = CODEC_ID_MJPEG;

  /* If not possible, select new codec */
  if (dst_fmt->video_codec == CODEC_ID_NONE)
    {
      if (format & ART_CAN_PNG)
	dst_fmt->video_codec = CODEC_ID_PNG;
      else if (format & ART_CAN_JPEG)
	dst_fmt->video_codec = CODEC_ID_MJPEG;
    }

  img_encoder = avcodec_find_encoder(dst_fmt->video_codec);
  if (!img_encoder)
    {
      DPRINTF(E_LOG, L_ART, "No suitable encoder found for codec ID %d\n", dst_fmt->video_codec);

      ret = -1;
      goto out_close_src;
    }

  dst_ctx = avformat_alloc_context();
  if (!dst_ctx)
    {
      DPRINTF(E_LOG, L_ART, "Out of memory for format context\n");

      ret = -1;
      goto out_close_src;
    }

  dst_ctx->oformat = dst_fmt;

#if LIBAVFORMAT_VERSION_MAJOR >= 53
  dst_fmt->flags &= ~AVFMT_NOFILE;
#else
  ret = snprintf(dst_ctx->filename, sizeof(dst_ctx->filename), "evbuffer:%p", evbuf);
  if ((ret < 0) || (ret >= sizeof(dst_ctx->filename)))
    {
      DPRINTF(E_LOG, L_ART, "Output artwork URL too long\n");

      ret = -1;
      goto out_free_dst_ctx;
    }
#endif

#if LIBAVFORMAT_VERSION_MAJOR >= 54 || (LIBAVFORMAT_VERSION_MAJOR == 53 && LIBAVFORMAT_VERSION_MINOR >= 21)
  dst_st = avformat_new_stream(dst_ctx, NULL);
#else
  dst_st = av_new_stream(dst_ctx, 0);
#endif
  if (!dst_st)
    {
      DPRINTF(E_LOG, L_ART, "Out of memory for new output stream\n");

      ret = -1;
      goto out_free_dst_ctx;
    }

  dst = dst_st->codec;

#if LIBAVCODEC_VERSION_MAJOR >= 54 || (LIBAVCODEC_VERSION_MAJOR == 53 && LIBAVCODEC_VERSION_MINOR >= 35)
  avcodec_get_context_defaults3(dst, NULL);
#else
  avcodec_get_context_defaults2(dst, AVMEDIA_TYPE_VIDEO);
#endif

  if (dst_fmt->flags & AVFMT_GLOBALHEADER)
    dst->flags |= CODEC_FLAG_GLOBAL_HEADER;

  dst->codec_id = dst_fmt->video_codec;
#if LIBAVCODEC_VERSION_MAJOR >= 53 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 64)
  dst->codec_type = AVMEDIA_TYPE_VIDEO;
#else
  dst->codec_type = CODEC_TYPE_VIDEO;
#endif

  pix_fmt_mask = 0;
  pix_fmts = img_encoder->pix_fmts;
  while (pix_fmts && (*pix_fmts != -1))
    {
      pix_fmt_mask |= (1 << *pix_fmts);
      pix_fmts++;
    }

  dst->pix_fmt = avcodec_find_best_pix_fmt(pix_fmt_mask, src->pix_fmt, 1, NULL);

  if (dst->pix_fmt < 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not determine best pixel format\n");

      ret = -1;
      goto out_free_dst_ctx;
    }

  DPRINTF(E_DBG, L_ART, "Selected pixel format: %d\n", dst->pix_fmt);

  dst->time_base.num = 1;
  dst->time_base.den = 25;

  dst->width = out_w;
  dst->height = out_h;

#if LIBAVFORMAT_VERSION_MAJOR <= 52 || (LIBAVFORMAT_VERSION_MAJOR == 53 && LIBAVFORMAT_VERSION_MINOR <= 1)
  ret = av_set_parameters(dst_ctx, NULL);
  if (ret < 0)
    {
      DPRINTF(E_LOG, L_ART, "Invalid parameters for artwork output: %s\n", strerror(AVUNERROR(ret)));

      ret = -1;
      goto out_free_dst_ctx;
    }
#endif

  /* Open encoder */
#if LIBAVCODEC_VERSION_MAJOR >= 54 || (LIBAVCODEC_VERSION_MAJOR == 53 && LIBAVCODEC_VERSION_MINOR >= 6)
  ret = avcodec_open2(dst, img_encoder, NULL);
#else
  ret = avcodec_open(dst, img_encoder);
#endif
  if (ret < 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not open codec for encoding: %s\n", strerror(AVUNERROR(ret)));

      ret = -1;
      goto out_free_dst_ctx;
    }

  i_frame = avcodec_alloc_frame();
  o_frame = avcodec_alloc_frame();

  if (!i_frame || !o_frame)
    {
      DPRINTF(E_LOG, L_ART, "Could not allocate input/output frame\n");

      ret = -1;
      goto out_free_frames;
    }

  ret = avpicture_get_size(dst->pix_fmt, src->width, src->height);

  DPRINTF(E_DBG, L_ART, "Artwork buffer size: %d\n", ret);

  buf = (uint8_t *)av_malloc(ret);
  if (!buf)
    {
      DPRINTF(E_LOG, L_ART, "Out of memory for artwork buffer\n");

      ret = -1;
      goto out_free_frames;
    }

  avpicture_fill((AVPicture *)o_frame, buf, dst->pix_fmt, src->width, src->height);

  swsctx = sws_getContext(src->width, src->height, src->pix_fmt,
			  dst->width, dst->height, dst->pix_fmt,
			  SWS_BICUBIC, NULL, NULL, NULL);
  if (!swsctx)
    {
      DPRINTF(E_LOG, L_ART, "Could not get SWS context\n");

      ret = -1;
      goto out_free_buf;
    }

  /* Get frame */
  have_frame = 0;
  while (av_read_frame(src_ctx, &pkt) == 0)
    {
      if (pkt.stream_index != s)
	{
	  av_free_packet(&pkt);
	  continue;
	}

#if LIBAVCODEC_VERSION_MAJOR >= 53 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 32)
      /* FFmpeg 0.6 */
      avcodec_decode_video2(src, i_frame, &have_frame, &pkt);
#else
      avcodec_decode_video(src, i_frame, &have_frame, pkt.data, pkt.size);
#endif

      break;
    }

  if (!have_frame)
    {
      DPRINTF(E_LOG, L_ART, "Could not decode artwork\n");

      av_free_packet(&pkt);
      sws_freeContext(swsctx);

      ret = -1;
      goto out_free_buf;
    }

  /* Scale */
#if LIBSWSCALE_VERSION_MAJOR >= 1 || (LIBSWSCALE_VERSION_MAJOR == 0 && LIBSWSCALE_VERSION_MINOR >= 9)
  /* FFmpeg 0.6, libav 0.6+ */
  sws_scale(swsctx, (const uint8_t * const *)i_frame->data, i_frame->linesize, 0, src->height, o_frame->data, o_frame->linesize);
#else
  sws_scale(swsctx, i_frame->data, i_frame->linesize, 0, src->height, o_frame->data, o_frame->linesize);
#endif

  sws_freeContext(swsctx);
  av_free_packet(&pkt);

  /* Open output file */
#if LIBAVFORMAT_VERSION_MAJOR >= 53
  dst_ctx->pb = avio_evbuffer_open(evbuf);
#else
  ret = url_fopen(&dst_ctx->pb, dst_ctx->filename, URL_WRONLY);
#endif
  if (ret < 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not open artwork destination buffer\n");

      ret = -1;
      goto out_free_buf;
    }

  /* Encode frame */
  outbuf_len = dst->width * dst->height * 3;
  if (outbuf_len < FF_MIN_BUFFER_SIZE)
    outbuf_len = FF_MIN_BUFFER_SIZE;

  outbuf = (uint8_t *)av_malloc(outbuf_len);
  if (!outbuf)
    {
      DPRINTF(E_LOG, L_ART, "Out of memory for encoded artwork buffer\n");

#if LIBAVFORMAT_VERSION_MAJOR >= 53
      avio_evbuffer_close(dst_ctx->pb);
#else
      url_fclose(dst_ctx->pb);
#endif

      ret = -1;
      goto out_free_buf;
    }

#if LIBAVCODEC_VERSION_MAJOR >= 54
  av_init_packet(&pkt);
  pkt.data = outbuf;
  pkt.size = outbuf_len;
  ret = avcodec_encode_video2(dst, &pkt, o_frame, &have_frame);
  if (!ret && have_frame && dst->coded_frame) 
    {
      dst->coded_frame->pts       = pkt.pts;
      dst->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
    }
  else if (ret < 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not encode artwork\n");

      ret = -1;
      goto out_fclose_dst;
    }
#else
  ret = avcodec_encode_video(dst, outbuf, outbuf_len, o_frame);
  if (ret <= 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not encode artwork\n");

      ret = -1;
      goto out_fclose_dst;
    }

  av_init_packet(&pkt);
  pkt.stream_index = 0;
  pkt.data = outbuf;
  pkt.size = ret;
#endif

#if LIBAVFORMAT_VERSION_MAJOR >= 54 || (LIBAVFORMAT_VERSION_MAJOR == 53 && LIBAVFORMAT_VERSION_MINOR >= 3)
  ret = avformat_write_header(dst_ctx, NULL);
#else
  ret = av_write_header(dst_ctx);
#endif
  if (ret != 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not write artwork header: %s\n", strerror(AVUNERROR(ret)));

      ret = -1;
      goto out_fclose_dst;
    }

  ret = av_interleaved_write_frame(dst_ctx, &pkt);

  if (ret != 0)
    {
      DPRINTF(E_LOG, L_ART, "Error writing artwork\n");

      ret = -1;
      goto out_fclose_dst;
    }

  ret = av_write_trailer(dst_ctx);
  if (ret != 0)
    {
      DPRINTF(E_LOG, L_ART, "Could not write artwork trailer: %s\n", strerror(AVUNERROR(ret)));

      ret = -1;
      goto out_fclose_dst;
    }

  switch (dst_fmt->video_codec)
    {
      case CODEC_ID_PNG:
	ret = ART_FMT_PNG;
	break;

      case CODEC_ID_MJPEG:
	ret = ART_FMT_JPEG;
	break;

      default:
	DPRINTF(E_LOG, L_ART, "Unhandled rescale output format\n");
	ret = -1;
	break;
    }

 out_fclose_dst:
#if LIBAVFORMAT_VERSION_MAJOR >= 53
  avio_evbuffer_close(dst_ctx->pb);
#else
  url_fclose(dst_ctx->pb);
#endif
  av_free(outbuf);

 out_free_buf:
  av_free(buf);

 out_free_frames:
  if (i_frame)
    av_free(i_frame);
  if (o_frame)
    av_free(o_frame);
  avcodec_close(dst);

 out_free_dst_ctx:
  avformat_free_context(dst_ctx);

 out_close_src:
  avcodec_close(src);

  return ret;
}