Exemple #1
0
AVCodecContext *new_software_decoder(uint8_t *decoder_config, uint32_t decoder_config_len)
{
    char decoder_name[10];
    int config_size,i;
    AVCodecContext *dec;
    bzero(decoder_name, sizeof(decoder_name));
    memcpy(decoder_name, decoder_config, 4);
    decoder_config += 4;
    decoder_config_len -= 4;

    // av_vaapi, av_vdpau
    if (!strcmp(decoder_name,"wmv3")){
      dec = avcodec_alloc_context2(AVMEDIA_TYPE_VIDEO);
      memcpy(&config_size,decoder_config,sizeof(config_size));
      decoder_config += sizeof(config_size);
      decoder_config_len -=sizeof(config_size);  
      memcpy(&dec->width,decoder_config,sizeof(dec->width));   
      memcpy(&dec->height,decoder_config+sizeof(dec->width),sizeof(dec->height));
      decoder_config += config_size;
      decoder_config_len -= config_size;
    } else if(!strcmp(decoder_name,"wmav")){
      dec = avcodec_alloc_context2(AVMEDIA_TYPE_AUDIO);
      bzero(decoder_name, sizeof(decoder_name));
      memcpy(decoder_name,decoder_config-4,5);
      decoder_config++; decoder_config_len--;
      memcpy(&dec->channels,decoder_config,sizeof(dec->channels));
      memcpy(&dec->bit_rate,decoder_config+=sizeof(dec->channels),sizeof(dec->bit_rate));
      memcpy(&dec->sample_rate,decoder_config+=sizeof(dec->bit_rate),sizeof(dec->sample_rate));
      decoder_config+=sizeof(dec->sample_rate);decoder_config_len-=sizeof(dec->sample_rate) + sizeof(dec->bit_rate) + sizeof(dec->channels);
    };
    printf("PARAMS: %i %i %i",dec->bit_rate,dec->sample_rate,dec->channels);
    AVCodec *decoder = avcodec_find_decoder_by_name(decoder_name);
    if(!decoder) {
      fprintf(stderr, "Can't find decoder %s\r\n", decoder_name);
      return NULL;
    };
    dec->extradata_size = decoder_config_len;
    dec->extradata = av_mallocz(dec->extradata_size);
    memcpy(dec->extradata, (const char *)decoder_config, dec->extradata_size);
    av_log(dec,AV_LOG_ERROR,"Payload: \n");
    for(i=0;i<dec->extradata_size;i++)
      av_log(dec,AV_LOG_ERROR,"%i, ",(int)dec->extradata[i]);
    if(avcodec_open(dec, decoder) < 0) {
        free(dec->extradata);
        dec->extradata = NULL;
        av_free(dec);
        fprintf(stderr, "Can't open decoder %i\r\n");
        return NULL;
    };
    return dec;    
}
VideoFFmpegWriter::VideoFFmpegWriter()
	: _avformatOptions( 0 )
	, _sws_context( NULL )
	, _stream( 0 )
	, _error( IGNORE_FINISH )
	, _filename( "" )
	, _width( 0 )
	, _height( 0 )
	, _aspectRatio( 1 )
	, _out_pixelFormat( PIX_FMT_YUV420P )
	, _fps( 25.0f )
	, _format( "default" )
	, _codec( "default" )
	, _bitRate( 400000 )
	, _bitRateTolerance( 4000 * 10000 )
	, _gopSize( 12 )
	, _bFrames( 0 )
	, _mbDecision( FF_MB_DECISION_SIMPLE )
{
	av_log_set_level( AV_LOG_WARNING );
	av_register_all();

	for( int i = 0; i < CODEC_TYPE_NB; ++i )
		_avctxOptions[i] = avcodec_alloc_context2( CodecType( i ) );

	_formatsLongNames.push_back( std::string( "default" ) );
	_formatsShortNames.push_back( std::string(  "default" ) );
	AVOutputFormat* fmt = av_oformat_next( NULL );
	while( fmt )
	{
		if( fmt->video_codec != CODEC_ID_NONE )
		{
			if( fmt->long_name )
			{
				_formatsLongNames.push_back( std::string( fmt->long_name ) + std::string( " (" ) + std::string( fmt->name ) + std::string( ")" ) );
				_formatsShortNames.push_back( std::string( fmt->name ) );
			}
		}
		fmt = av_oformat_next( fmt );
	}

	_codecsLongNames.push_back( std::string( "default" ) );
	_codecsShortNames.push_back( std::string( "default" ) );
	AVCodec* c = av_codec_next( NULL );
	while( c )
	{
		if( c->type == CODEC_TYPE_VIDEO && c->encode )
		{
			if( c->long_name )
			{
				_codecsLongNames.push_back( std::string( c->long_name ) );
				_codecsShortNames.push_back( std::string( c->name ) );
			}
		}
		c = av_codec_next( c );
	}
}
Exemple #3
0
void init_opts(void)
{
    int i;
    for (i = 0; i < AVMEDIA_TYPE_NB; i++)
        avcodec_opts[i] = avcodec_alloc_context2(i);
    avformat_opts = avformat_alloc_context();
#if CONFIG_SWSCALE
    sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
#endif
}
Exemple #4
0
int tvh_audio_init(tvh_object_t *tvh, const char *codec) {
  int codec_id = 0;
  acodec_sys_t *cs = tvh->acs;

  DEBUG("Initializing audio codec");
  pthread_mutex_lock(&tvh->mutex);

  if(!strcmp(codec, "AC3")) {
    codec_id = CODEC_ID_AC3;
  } else if(!strcmp(codec, "EAC3")) {
    codec_id = CODEC_ID_EAC3;
  } else if(!strcmp(codec, "AAC")) {
    codec_id = CODEC_ID_AAC;
  } else if(!strcmp(codec, "MPEG2AUDIO")) {
    codec_id = CODEC_ID_MP2;
  } else if(!strcmp(codec, "MP3")) {
    codec_id = CODEC_ID_MP3;
  }

  if(!codec_id) {
    ERROR("Unknown audio codec %s", codec);
    goto error;
  }

  cs->codec = avcodec_find_decoder(codec_id);
  if(!cs->codec) {
    ERROR("Unable to find audio codec %s", codec);
    goto error;
  }

  if(cs->codec->type != AVMEDIA_TYPE_AUDIO) {
    ERROR("Invalid codec type for audio decoding");
    goto error;
  }
  
  cs->ctx = avcodec_alloc_context2(AVMEDIA_TYPE_AUDIO);
  cs->buf = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);

  if(avcodec_open(cs->ctx, cs->codec) < 0) {
    ERROR("Unable to open audio codec");
    tvh_audio_close(tvh);
    goto error;
  }

  tvh->cur_pts = 0;
  pthread_mutex_unlock(&tvh->mutex);

  return 0;

 error:
  pthread_mutex_unlock(&tvh->mutex);
  return -1;
}
Exemple #5
0
AVCodecContext *avcodec_alloc_context(void)
{
/*
	参数:
		1、
		
	返回:
		1、
		
	说明:
		1、分配一个AVCodecContext  类型的内存
*/
    	return avcodec_alloc_context2(AVMEDIA_TYPE_UNKNOWN); /* 解码类型为unknown  的*/
}
Exemple #6
0
int tvh_video_init(tvh_object_t *tvh, const char *codec) {
  int codec_id = 0;
  vcodec_sys_t *cs = tvh->vcs;

  DEBUG("Initializing video codec");
  pthread_mutex_lock(&tvh->mutex);
  
  if(!strcmp(codec, "H264")) {
    codec_id = CODEC_ID_H264;
  } else if(!strcmp(codec, "MPEG2VIDEO")) {
    codec_id = CODEC_ID_MPEG2VIDEO;
  }

  if(!codec_id) {
    DEBUG("Unknown video codec %s", codec);
    goto error;
  }

  cs->codec = avcodec_find_decoder(codec_id);
  if(!cs->codec) {
    DEBUG("Unable to find video codec %s", codec);
    goto error;
  }

  if(cs->codec->type != AVMEDIA_TYPE_VIDEO) {
    DEBUG("Invalid codec type for video decoding");
    goto error;
  }
  
  cs->ctx = avcodec_alloc_context2(AVMEDIA_TYPE_VIDEO);
  cs->frame = avcodec_alloc_frame();
  avcodec_get_frame_defaults(cs->frame);

  if(avcodec_open(cs->ctx, cs->codec) < 0) {
    ERROR("Unable to open video codec");
    tvh_video_close(tvh);
    goto error;
  }

  tvh->cur_pts = 0;
  pthread_mutex_unlock(&tvh->mutex);

  return 0;

 error:
  pthread_mutex_unlock(&tvh->mutex);
  return -1;
}
Exemple #7
0
AVCodecContext *avcodec_alloc_context(void){
    return avcodec_alloc_context2(AVMEDIA_TYPE_UNKNOWN);
}
Exemple #8
0
AVCodecContext *avcodec_alloc_context(void){
    return avcodec_alloc_context2(CODEC_TYPE_UNKNOWN);
}
jint Java_org_devtcg_rojocam_ffmpeg_FFStreamConfig_nativeCreate(JNIEnv *env,
        jclass clazz) {
    FFStreamConfig *defaultConfig = NULL;
    AVCodecContext *videoEnc = NULL;
    AVStream *st = NULL;

    defaultConfig = av_mallocz(sizeof(FFStreamConfig));
    if (defaultConfig == NULL) {
        jniThrowOOM(env);
        goto fail;
    }
    defaultConfig->title = "rojocam feed";
//CODEC_ID_MPEG4
    AVCodec *codec = avcodec_find_encoder(CODEC_ID_MPEG4);

    videoEnc = avcodec_alloc_context2(AVMEDIA_TYPE_VIDEO);
    if (videoEnc == NULL) {
        jniThrowOOM(env);
        goto fail;
    }

    /* XXX: We should attempt to encode with parameters that match our camera
     * preview input to reduce overhead, but being in a proof-of-concept phase
     * I'd rather stick to the simpler, less variable approach. */
    videoEnc->time_base.num =1;
    videoEnc->time_base.den = 25;
    videoEnc->bit_rate = 180000;
    videoEnc->width = 320;
    videoEnc->height =240;
    videoEnc->pix_fmt = PIX_FMT_YUV420P;

    /* This apparently modifies the SDP created by avf_sdp_create.  In my
     * experiments it looks like it does indeed add the "config=" parameter to
     * the fmtp directive in SDP. No idea what that does, though... */
    if (opt_default("flags", "+global_header", videoEnc,
            AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)) {
        LOGE("opt_default: flags, +global_header failed!");
        jniThrowException(env, "java/IO/IOException", "Failed to set encoder options");
        goto fail;
    }

    if (avcodec_open(videoEnc, codec) < 0) {
        LOGE("avcodec_open failed!");
        jniThrowException(env, "java/lang/IllegalStateException", NULL);
        goto fail;
    }

    st = av_mallocz(sizeof(AVStream));
    if (st == NULL) {
        jniThrowOOM(env);
        goto fail;
    }

    st->index = defaultConfig->num_streams;
    st->codec = videoEnc;
    defaultConfig->streams[defaultConfig->num_streams++] = st;

    return (jint)defaultConfig;

fail:
    if (defaultConfig != NULL) {
        av_free(defaultConfig);
    }
    if (videoEnc != NULL) {
        av_free(videoEnc);
    }
    if (st != NULL) {
        av_free(st);
    }
    assert((*env)->ExceptionOccurred(env));
    return 0;
}