Example #1
0
static bool init_streams(struct ffmpeg_mux *ffm)
{
	if (ffm->params.has_video)
		create_video_stream(ffm);

	if (ffm->params.tracks) {
		ffm->audio_streams =
			calloc(1, ffm->params.tracks * sizeof(void*));

		for (int i = 0; i < ffm->params.tracks; i++)
			create_audio_stream(ffm, i);
	}

	if (!ffm->video_stream && !ffm->num_audio_streams)
		return false;

	return true;
}
bool OStreamVideoEncoder::encoder_initialize () 
{
	char *encoding_str;
	// register all codecs
	av_register_all();
	
	switch ( _encoding_type ) {
		case OSTREAM_ENCODING_MJPEG:
			encoding_str = "mjpeg";
			_out_fmt = guess_format ( encoding_str, NULL, NULL);
		
			if( !_out_fmt ) {
//				DEBUG_PRINT( "Could not find suitable output format.\n");
				return false;
			}
		
			break;
		
		case OSTREAM_ENCODING_H263P:
			encoding_str ="h263";
			_out_fmt = guess_format ( encoding_str, NULL, NULL);
		
			if( !_out_fmt ) {
//				DEBUG_PRINT( "Could not find suitable output format.\n");
				return false;
			}

			// specify the sub-codec we want to use. Without this we won't be able
			// to stream H263P not use 640x480 videos
			_out_fmt->video_codec = CODEC_ID_H263P;
		
			break;
		
		default:
//			DEBUG_PRINT ("could not find known OSTREAM_ENCODING type.\n" );
			break;
	}

	_out_fmt_context = av_alloc_format_context ();
	if( !_out_fmt_context ) {
//		DEBUG_PRINT( "Memory error.\n" );
		return false;
	}
	
	// set output format of AVFormatContext
	_out_fmt_context->oformat = _out_fmt;

	// check if we had a correct video_codec 
    if ( _out_fmt->video_codec == CODEC_ID_NONE ) {
//    	DEBUG_PRINT( "CODEC_ID_NONE\n" );
		return false;
	}
	
	// add the stream
	if(!( _video_stream = create_video_stream( _out_fmt_context, _out_fmt->video_codec ) )) {
//		DEBUG_PRINT( "error in adding stream\n" );
		return false;
	}

    // set the output parameters (must be done even if no parameters)
    if ( av_set_parameters( _out_fmt_context, NULL ) < 0 ) {
//        DEBUG_PRINT( "Invalid output format parameters\n" );
        return false;
    }

	// open codec associated with current video_stream
	if ( !open_codec( _video_stream ) ) {
//        DEBUG_PRINT( "could not open codec\n" );		
		return false;
	}
		
	// we should now create the frame on which we can store the converted bytes
	// ready to be encoded
	_ready_to_encode_frame = create_avframe( _video_stream->codec->pix_fmt,this->_video_width,this->_video_height );
    if ( !_ready_to_encode_frame ) {
//        DEBUG_PRINT( "Could not allocate encoded_frame\n" );
		return false;
    }

	// we'll receive IplImage(s). This _opencv_frame will be converted to a stream-compatible
	// AVFrame [see up _ready_to_encode_frame]
	_opencv_frame = NULL;
	
	
	return true;
}