Esempio n. 1
0
float fps(const char *filename)
{
        float result = 0;
	AVFormatContext *pFormatCtx;
	
	// Open video file
	if (av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL))
	  return -1 ; // Couldn't open file
				 
	// Retrieve stream information
	if(av_find_stream_info(pFormatCtx)<0)
	  return -1; // Couldn't find stream information
			
	// Find the first video stream
	int videoStream=-1;
	for(unsigned int i=0; i<pFormatCtx->nb_streams; i++)
	{
		     if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) 
		     {
			    videoStream=i;
			    break;
		     }
	}
        if(videoStream==-1)
	    return -1; // Didn't find a video stream
	
	int num = (pFormatCtx->streams[videoStream]->r_frame_rate).num;
	int den = (pFormatCtx->streams[videoStream]->r_frame_rate).den;
	result = num/den;

	av_close_input_file(pFormatCtx);
	
	return result;

}
Esempio n. 2
0
int	 av_vid_init(char *file)
{
	int i;

	if (av_open_input_file(&pFormatCtx, file, NULL, 0, NULL)!=0)
		return -1;

	if (av_find_stream_info(pFormatCtx)<0)
		return -1;

	dump_format(pFormatCtx, 0, file, 0);
	videoStream=-1;
	for (i=0; i<pFormatCtx->nb_streams; i++) {
		if (pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
			videoStream=i;
			break;
		}
	}
	if (videoStream==-1)
		return -1;

	pCodecCtx=pFormatCtx->streams[videoStream]->codec;

	pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
	if (pCodec==NULL)
		return -1;

	if (avcodec_open(pCodecCtx, pCodec)<0)
		return -1;

	pFrame=avcodec_alloc_frame();

	return 0;
}
Esempio n. 3
0
static int open_file(AVFormatContext **	ic_ptr, const char * 	filename, AVInputFormat * 	fmt) {
#ifdef USE_PRE_0_7
	return av_open_input_file(ic_ptr, filename, fmt, 0, NULL);
#else
	return avformat_open_input(ic_ptr, filename, fmt, NULL);
#endif
}
Esempio n. 4
0
/* Fill info structure with data from ffmpeg comments */
static void ffmpeg_info (const char *file_name,
		struct file_tags *info,
		const int tags_sel)
{
	AVFormatParameters ap;
	AVFormatContext *ic;
	int err;
	
	memset (&ap, 0, sizeof(ap));

	if ((err = av_open_input_file(&ic, file_name, NULL, 0, &ap)) < 0) {
		logit ("av_open_input_file() failed (%d)", err);
		return;
	}
	if ((err = av_find_stream_info(ic)) < 0) {
		logit ("av_find_stream_info() failed (%d)", err);
		return;
	}

	if (tags_sel & TAGS_COMMENTS) {
		if (ic->track != 0)
			info->track = ic->track;
		if (ic->title[0] != 0)
			info->title = xstrdup (ic->title);
		if (ic->author[0] != 0)
			info->artist = xstrdup (ic->author);
		if (ic->album[0] != 0)
			info->album = xstrdup (ic->album);
	}

	if (tags_sel & TAGS_TIME)
		info->time = ic->duration >= 0 ? ic->duration / AV_TIME_BASE
			: -1;
}
Esempio n. 5
0
static int startread(sox_format_t * ft)
{
  priv_t * ffmpeg = (priv_t *)ft->priv;
  AVFormatParameters params;
  int ret;
  int i;

  ffmpeg->audio_buf_raw = lsx_calloc(1, (size_t)AVCODEC_MAX_AUDIO_FRAME_SIZE + 32);
  ffmpeg->audio_buf_aligned = ALIGN16(ffmpeg->audio_buf_raw);

  /* Signal audio stream not found */
  ffmpeg->audio_index = -1;

  /* register all CODECs, demux and protocols */
  av_register_all();

  /* Open file and get format */
  memset(&params, 0, sizeof(params));
  if ((ret = av_open_input_file(&ffmpeg->ctxt, ft->filename, NULL, 0, &params)) < 0) {
    lsx_fail("ffmpeg cannot open file for reading: %s (code %d)", ft->filename, ret);
    return SOX_EOF;
  }

  /* Get CODEC parameters */
  if ((ret = av_find_stream_info(ffmpeg->ctxt)) < 0) {
    lsx_fail("ffmpeg could not find CODEC parameters for %s", ft->filename);
    return SOX_EOF;
  }

  /* Now we can begin to play (RTSP stream only) */
  av_read_play(ffmpeg->ctxt);

  /* Find audio stream (FIXME: allow different stream to be selected) */
  for (i = 0; (unsigned)i < ffmpeg->ctxt->nb_streams; i++) {
    AVCodecContext *enc = ffmpeg->ctxt->streams[i]->codec;
    if (enc->codec_type == CODEC_TYPE_AUDIO && ffmpeg->audio_index < 0) {
      ffmpeg->audio_index = i;
      break;
    }
  }

  /* Open the stream */
  if (ffmpeg->audio_index < 0 ||
      stream_component_open(ffmpeg, ffmpeg->audio_index) < 0 ||
      ffmpeg->audio_stream < 0) {
    lsx_fail("ffmpeg could not open CODECs for %s", ft->filename);
    return SOX_EOF;
  }

  /* Copy format info */
  ft->signal.rate = ffmpeg->audio_st->codec->sample_rate;
  ft->encoding.bits_per_sample = 16;
  ft->encoding.encoding = SOX_ENCODING_SIGN2;
  ft->signal.channels = ffmpeg->audio_st->codec->channels;
  ft->signal.length = 0; /* Currently we can't seek; no idea how to get this
		     info from ffmpeg anyway (in time, yes, but not in
		     samples); but ffmpeg *can* seek */

  return SOX_SUCCESS;
}
Esempio n. 6
0
JNIEXPORT void JNICALL
Java_net_sourceforge_servestream_media_MediaMetadataRetriever__1setDataSource(JNIEnv * env, jclass obj, jstring jpath) {
	//__android_log_write(ANDROID_LOG_INFO, "Java_net_sourceforge_servestream_media_MediaMetadataRetriever", "setDataSource called");

	metadata = NULL;
	duration = 0;

    AVFormatContext *fmt_ctx = NULL;
    const char * path;

    path = (*env)->GetStringUTFChars(env, jpath, NULL);

    //__android_log_write(ANDROID_LOG_INFO, "Java_net_sourceforge_servestream_media_MediaMetadataRetriever", path);

    if (av_open_input_file(&fmt_ctx, path, NULL, 0, NULL)) {
	    __android_log_write(ANDROID_LOG_INFO, "Java_net_sourceforge_servestream_media_MediaMetadataRetriever", "Metadata could not be retrieved");
        av_free(fmt_ctx);
    	fmt_ctx = NULL;
    	return;
    }

    metadata = fmt_ctx->metadata;
    duration = getDuration(fmt_ctx);

	__android_log_write(ANDROID_LOG_INFO, "Java_net_sourceforge_servestream_media_MediaMetadataRetriever", "Found metadata");
	//AVMetadataTag *tag = NULL;
	//while ((tag = av_metadata_get(metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
    //	__android_log_write(ANDROID_LOG_INFO, "Java_net_sourceforge_servestream_media_MediaMetadataRetriever", tag->key);
    //	__android_log_write(ANDROID_LOG_INFO, "Java_net_sourceforge_servestream_media_MediaMetadataRetriever", tag->value);
    //}

    av_free(fmt_ctx);
    fmt_ctx = NULL;
}
Esempio n. 7
0
// sprawdz, czy plik to MP3 i dodaj, je¶li tak
static void checkAddFile(const char *fullname)
{
	AVFormatContext *pFormatCtx = NULL;
	int i = av_open_input_file( &pFormatCtx, fullname, NULL, 0, NULL);
	// Cannot open file
	if ( i != 0 )
		return;
	i = av_find_stream_info( pFormatCtx );
	// No info about opened file
	if ( i < 0 )
	{
		av_close_input_file( pFormatCtx );
		return;
	}
	// wyszukaj strumien audio
	for (i=0; i<pFormatCtx->nb_streams; i++)
	{
		if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
		{
			doAction(ACTION_PLAYLIST_ADDFILE, fullname);
			av_close_input_file( pFormatCtx );
			return;
		}
	}
	av_close_input_file( pFormatCtx );
	return;
}
Esempio n. 8
0
bool Movie::open(const string& filename) {

  av_register_all();

  if (av_open_input_file(&ctx, filename.c_str(), NULL, 0, NULL) != 0) {
    fprintf(stderr, "ERROR: Cannot open file: '%s'.\n", filename.c_str());
    return false;
  }

  if (av_find_stream_info(ctx) < 0) {
    fprintf(stderr, "ERROR: Cannot find stream info.\n");
    return false;
  }
  
  AVCodecContext* codecCtx = ctx->streams[0]->codec;
  AVCodec* codec = avcodec_find_decoder(codecCtx->codec_id);
  if (!codec) {
    fprintf(stderr, "ERROR: Cannot find codec.");
    return false;
  }
  
  if (avcodec_open(codecCtx, codec) < 0) {
    fprintf(stderr, "ERROR: Cannot open codec.");
    return false;
  }

  return true;
}
Esempio n. 9
0
JNIEXPORT jint JNICALL Java_com_example_restreaming_StreamingHeadlessCamcorder_nativeOpenFromFile(JNIEnv* env, jobject thiz, jstring mediafile)
    {
    __android_log_print(ANDROID_LOG_INFO, "com.example.ffmpegav", "nativeOpenFromFile()");

    avcodec_register_all();
    av_register_all();

    const char* mfileName = (*env)->GetStringUTFChars(env, mediafile, 0);
    if (mfileName == 0)
        {
        __android_log_print(ANDROID_LOG_ERROR, "com.example.ffmpegav", "failed to retrieve media file name");
        return -1;
        }

    __android_log_print(ANDROID_LOG_INFO, "com.example.ffmpegav", "opening %s", mfileName);

    int result = av_open_input_file(&gFormatCtx, mfileName, NULL, 0, NULL);
    (*env)->ReleaseStringUTFChars(env, mediafile, mfileName); //always release the java string reference
    if (result != 0)
        {
        __android_log_print(ANDROID_LOG_ERROR, "com.example.ffmpegav", "av_open_input_file() failed");
        return -2;
        }

    if (av_find_stream_info(gFormatCtx) < 0)
        {
        __android_log_print(ANDROID_LOG_ERROR, "com.example.ffmpegav", "av_find_stream_info() failed");
        return -3;
        }

    return 0;
    }
Esempio n. 10
0
File: IoAVCodec.c Progetto: BMeph/io
int IoAVCodec_openFile(IoAVCodec *self)
{
	//AVInputFormat *inputFormat;
	IoObject *fileName = IoObject_symbolGetSlot_(self, IOSYMBOL("path"));
	int err = av_open_input_file(&DATA(self)->formatContext, CSTRING(fileName), NULL, 0, NULL);
	return err;
}
Esempio n. 11
0
/*
    Used to reopen a video if the slower fallback function for seeking is used.
*/
bool CvCapture_FFMPEG::reopen()
{
    if ( filename==NULL ) return false;

#if LIBAVFORMAT_BUILD > 4628
    avcodec_close( video_st->codec );
#else
    avcodec_close( &video_st->codec );
#endif
    av_close_input_file(ic);

    // reopen video
    av_open_input_file(&ic, filename, NULL, 0, NULL);
    av_find_stream_info(ic);
#if LIBAVFORMAT_BUILD > 4628
    AVCodecContext *enc = ic->streams[video_stream]->codec;
#else
    AVCodecContext *enc = &ic->streams[video_stream]->codec;
#endif
    AVCodec *codec = avcodec_find_decoder(enc->codec_id);
    avcodec_open(enc, codec);
    video_st = ic->streams[video_stream];

    // reset framenumber to zero
    picture_pts=0;

    return true;
}
AVFormatContext *openVideo(char* filename) {
  AVFormatContext *pFormatCtx;
  av_register_all();
  if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
    mexErrMsgIdAndTxt("ffmpeg:open", "error: Can't open video '%s'", filename);
  return pFormatCtx;
}
Esempio n. 13
0
static void parse_mp3_comments(const char *file) {
	AVFormatContext *fmt = NULL;
	int i;

	for (i = 0; i < COMMENT_ID_NUM; i++)
		comment[i][0] = 0;
	
	if (av_open_input_file(&fmt, file, NULL, 0, NULL))
		return;

	snprintf(comment[COMMENT_ID_ARTIST], MAX_COMMENT_LEN, "%s",
	         fmt->author);
	
	snprintf(comment[COMMENT_ID_TITLE], MAX_COMMENT_LEN, "%s",
	         fmt->title);
	
	snprintf(comment[COMMENT_ID_ALBUM], MAX_COMMENT_LEN, "%s",
	         fmt->album);

	for (i = 0; i < COMMENT_ID_NUM; i++)
		if (comment[i][0])
			strchomp(comment[i]);

	return;
}
Esempio n. 14
0
EmErrorCode WmaDecoder::Open(const std::string& url)
{
    m_FormatCtx = nullptr;
    int err = av_open_input_file(&m_FormatCtx, url.c_str(), nullptr, 0, nullptr);
    if (err < 0)
        return ErrorCode::DecoderFailedToOpen;

    for (int i = 0; i < m_FormatCtx->nb_streams; ++i) {
        m_CodecCtx = &m_FormatCtx->streams[i]->codec;
        if (m_CodecCtx->codec_type == CODEC_TYPE_AUDIO)
            break;
    }

    av_find_stream_info(m_FormatCtx);
    AVCodec* codec = avcodec_find_decoder(m_CodecCtx->codec_id);
    
    // open it
    if (codec == nullptr || avcodec_open(m_CodecCtx, codec))
        return ErrorCode::DecoderFailedToInit;

    // read info
    m_Channels = m_CodecCtx->channels;
    m_SampleRate = m_CodecCtx->sample_rate;
    m_BitsPerSample = m_CodecCtx->bits_per_sample;
    m_Duration = m_FormatCtx->duration;

    m_UnitIndex = 0;
    m_UnitCount = m_Duration;

    return ErrorCode::Ok;
}
void probe_ffmpeg(info_t *ipipe)
{
    /* to be completed */
    AVFormatContext *lavf_dmx_context = NULL;
    int ret = 0;

    close(ipipe->fd_in);

    TC_INIT_LIBAVCODEC;

    ret = av_open_input_file(&lavf_dmx_context, ipipe->name,
                             NULL, 0, NULL);
    if (ret != 0) {
        tc_log_error(__FILE__, "unable to open '%s'"
                               " (libavformat failure)",
                     ipipe->name);
        ipipe->error = 1;
        return;
    }

    ret = av_find_stream_info(lavf_dmx_context);
    if (ret < 0) {
        tc_log_error(__FILE__, "unable to fetch informations from '%s'"
                               " (libavformat failure)",
                     ipipe->name);
        ipipe->error = 1;
        return;
    }

    translate_info(lavf_dmx_context, ipipe->probe_info);

    av_close_input_file(lavf_dmx_context);
    return;
}
Esempio n. 16
0
int FfmpegCamera::PrimeCapture()
{
    Info( "Priming capture from %s", mPath.c_str() );

    // Open the input, not necessarily a file
    if ( av_open_input_file( &mFormatContext, mPath.c_str(), NULL, 0, NULL ) !=0 )
        Fatal( "Unable to open input %s due to: %s", mPath.c_str(), strerror(errno) );

    // Locate stream info from input
    if ( av_find_stream_info( mFormatContext ) < 0 )
        Fatal( "Unable to find stream info from %s due to: %s", mPath.c_str(), strerror(errno) );

    // Find first video stream present
    mVideoStreamId = -1;
    for ( int i=0; i < mFormatContext->nb_streams; i++ )
    {
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
        if ( mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO )
#else
        if ( mFormatContext->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO )
#endif
        {
            mVideoStreamId = i;
            break;
        }
    }
    if ( mVideoStreamId == -1 )
        Fatal( "Unable to locate video stream in %s", mPath.c_str() );

    mCodecContext = mFormatContext->streams[mVideoStreamId]->codec;

    // Try and get the codec from the codec context
    if ( (mCodec = avcodec_find_decoder( mCodecContext->codec_id )) == NULL )
        Fatal( "Can't find codec for video stream from %s", mPath.c_str() );

    // Open the codec
    if ( avcodec_open( mCodecContext, mCodec ) < 0 )
        Fatal( "Unable to open codec for video stream from %s", mPath.c_str() );

    // Allocate space for the native video frame
    mRawFrame = avcodec_alloc_frame();

    // Allocate space for the converted video frame
    mFrame = avcodec_alloc_frame();

    // Determine required buffer size and allocate buffer
    int pictureSize = avpicture_get_size( PIX_FMT_RGB24, mCodecContext->width, mCodecContext->height );
    mBuffer.size( pictureSize );

    avpicture_fill( (AVPicture *)mFrame, (unsigned char *)mBuffer, PIX_FMT_RGB24, mCodecContext->width, mCodecContext->height);

#if HAVE_LIBSWSCALE
    if ( (mConvertContext = sws_getCachedContext( mConvertContext, mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL )) == NULL )
        Fatal( "Unable to create conversion context for %s", mPath.c_str() );
#else // HAVE_LIBSWSCALE
    Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
#endif // HAVE_LIBSWSCALE

    return( 0 );
}
Esempio n. 17
0
jboolean Java_sysu_ss_xu_FFmpeg_avOpenInputFile( JNIEnv* env, jobject thiz, jstring jfilePath )
{
	char* filePath = (char *)(*env)->GetStringUTFChars(env, jfilePath, NULL);
	if( av_open_input_file(&pFormatCtx, filePath, NULL, 0, NULL) != 0)
		return 0;
	else
		return 1;
}
Esempio n. 18
0
int main(int argc,char* argv[]) 
{
    char file[255] = {""};
    int  err, i;
    
    if (argc < 2)
    {
        printf("give me a filename please\n");
        return -1;
    }
    
    if (strstr(argv[1], "://") == NULL)
    {
        strcpy(file, "file://");    
    }
    
    strcat(file, argv[1]);

    av_register_all();

    if ((err = av_open_input_file(&avContext, file, NULL, 0, NULL)) != 0) {
        char error[512];

        printf("av_open_input_file failed %d (%s)\n", err, file);
	
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 64, 0)	
        av_strerror(err, error, 512);
        printf("Cause: %s\n", error);
#endif	

        return -1;
    }

    if (av_find_stream_info(avContext) < 0) 
    {
        printf("Error av_find_stream_info\n");
    }

    printf("\n***\n");
    dump_metadata();
     
    printf("\nstream specific metadata:\n");
    for (i = 0; i < avContext->nb_streams; i++)
    {
       AVStream* stream = avContext->streams[i];
    
       if (stream)
       {
          AVMetadataTag *tag = NULL;
          
          if (stream->metadata != NULL)
             while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
                printf("%s: %s\n", tag->key, tag->value);
       }
    }
     
    return 0;
}
Esempio n. 19
0
bool K3bFFMpegFile::open()
{
  close();

  // open the file
  int err = av_open_input_file( &d->formatContext, m_filename.local8Bit(), 0, 0, 0 );
  if( err < 0 ) {
    kdDebug() << "(K3bFFMpegFile) unable to open " << m_filename << " with error " << err << endl;
    return false;
  }

  // analyze the streams
  av_find_stream_info( d->formatContext );

  // we only handle files containing one audio stream
  if( d->formatContext->nb_streams != 1 ) {
    kdDebug() << "(K3bFFMpegFile) more than one stream in " << m_filename << endl;
    return false;
  }

  // urgh... ugly
#ifdef FFMPEG_BUILD_PRE_4629
  AVCodecContext* codecContext =  &d->formatContext->streams[0]->codec;
#else
  AVCodecContext* codecContext =  d->formatContext->streams[0]->codec;
#endif
  if( codecContext->codec_type != CODEC_TYPE_AUDIO ) {
    kdDebug() << "(K3bFFMpegFile) not a simple audio stream: " << m_filename << endl;
    return false;
  }

  // get the codec
  d->codec = avcodec_find_decoder(codecContext->codec_id);
  if( !d->codec ) {
    kdDebug() << "(K3bFFMpegFile) no codec found for " << m_filename << endl;
    return false;
  }

  // open the codec on our context
  kdDebug() << "(K3bFFMpegFile) found codec for " << m_filename << endl;
  if( avcodec_open( codecContext, d->codec ) < 0 ) {
    kdDebug() << "(K3bFFMpegDecoderFactory) could not open codec." << endl;
    return false;
  }

  // determine the length of the stream
  d->length = K3b::Msf::fromSeconds( (double)d->formatContext->duration / (double)AV_TIME_BASE );

  if( d->length == 0 ) {
    kdDebug() << "(K3bFFMpegDecoderFactory) invalid length." << endl;
    return false;
  }

  // dump some debugging info
  dump_format( d->formatContext, 0, m_filename.local8Bit(), 0 );

  return true;
}
Esempio n. 20
0
//return value
//true : success
//false : fail
bool VideoIO::openInputCodec(void)
{
	//open video file
	///TODO : 20byte lost
	if(av_open_input_file(&pFormatCtx, inputFilename, NULL, 0, NULL) != 0)
	{
		fprintf(stderr, "couldn't open file : %s\n", inputFilename);
		return false;	//couldn't open file
	}

	//retrieve stream information
	if(av_find_stream_info(pFormatCtx) < 0)
	{
		fprintf(stderr, "couldn't find stream information\n");
		return false;
	}

	//dump information about file onto standard error
	dump_format(pFormatCtx, 0, inputFilename, 0);

	//find the first video stream
	videoStream = -1;
	for(unsigned int i = 0 ; i < pFormatCtx->nb_streams ; i++)
	{
		if(pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO)
		{
			videoStream = i;
			break;
		}
	}
	if(videoStream == -1)
	{
		fprintf(stderr, "didn't find a video stream");
		return false;
	}

	//get a pointer to the reading codec context for the video stream
	pInputCodecCtx = pFormatCtx->streams[videoStream]->codec;

	//find the decoder for the video stream(reaing video)
	pInputCodec = avcodec_find_decoder(pInputCodecCtx->codec_id);
	if(pInputCodec == NULL)
	{
		fprintf(stderr, "Unsupported coded!\n");
		return false;	//codec not found
	}

	//open codec
	if(avcodec_open(pInputCodecCtx, pInputCodec) < 0)
	{
		fprintf(stderr, "could not open codec\n");
		return false;
	}

	//success
	return true;
}
Esempio n. 21
0
int get_file_info(FileState *file)
{
    if(av_open_input_file(&file->pFormatCtx, file->fileName, NULL, 0, NULL) != 0)
	return -1;
    if(av_find_stream_info(file->pFormatCtx) < 0)
	return -1;
    dump_format(file->pFormatCtx, 0, file->fileName, 0);
    return 0;
}
	int
decoder_init( Decoder *pDecoder ) {
	
	if( av_open_input_file( &pDecoder->pFmtCtx, FILENAME, NULL, 0, NULL ) < 0 )
		return ERROR;

	if( av_find_stream_info( pDecoder->pFmtCtx ) < 0 )
		return ERROR;
	return SUCCESS;
}
Esempio n. 23
0
//#define DUMP_INDEX
int thumbnail_find_stream_info(void *handle, const char* filename)
{
    struct video_frame *frame = (struct video_frame *)handle;
    struct stream *stream = &frame->stream;
    int i;
    if (av_open_input_file(&stream->pFormatCtx, filename, NULL, 0, NULL) != 0) {
        log_print("Coundn't open file %s !\n", filename);
        goto err;
    }
    for (i = 0; i < stream->pFormatCtx->nb_streams; i++) {        
        if (stream->pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
                      stream->videoStream = i;
            break;
        }
    }
    //thumbnail just need the info of stream, so do not need fast_switch
    stream->pFormatCtx->pb->local_playback = 1;
    
    if (av_find_stream_info(stream->pFormatCtx) < 0) {
        log_print("Coundn't find stream information !\n");
        goto err1;
    }
#ifdef DUMP_INDEX
    int i, j;
    AVStream *pStream;
    log_print("*********************************************\n");
    for (i = 0; i < stream->pFormatCtx->nb_streams; i ++) {
        pStream = stream->pFormatCtx->streams[i];
        if (pStream) {
            for (j = 0; j < pStream->nb_index_entries; j++) {
                log_print("stream[%d]:idx[%d] pos:%llx time:%llx\n", i, j, pStream->index_entries[j].pos, pStream->index_entries[j].timestamp);
            }
        }
    }
    log_print("*********************************************\n");
#endif
    if(stream->videoStream>=0){
        stream->pCodecCtx = stream->pFormatCtx->streams[stream->videoStream]->codec;
        if (stream->pCodecCtx == NULL) {
            log_print("pCodecCtx is NULL !\n");
        }
        
        frame->width = stream->pCodecCtx->width;
        frame->height = stream->pCodecCtx->height;
    }
    return 0;

err1:
    av_close_input_file(stream->pFormatCtx);
err:
    memset(&frame->stream, 0, sizeof(struct stream));

    return -1;
}
Esempio n. 24
0
void *rtmp_send_thread(void *arg) {
    int type, pkt_ts;
    i64 first_pts = AV_NOPTS_VALUE;
    AVFormatContext *in_ctx;
    AVPacket packet;
    rtmp_state_t *st = (rtmp_state_t *)arg;

    my_log_debug("send_thread started");

    av_register_all();

    if (av_open_input_file(&in_ctx, st->flv_path, NULL, 0, NULL) < 0) {
        fprintf(stderr, "couldn't open %s\n", st->flv_path);
        return (void*)1;
    }

    while (av_read_frame(in_ctx, &packet) >= 0) {

        type = 0;
        switch (in_ctx->streams[packet.stream_index]->codec->codec_type) {
        case AVMEDIA_TYPE_VIDEO:
            type = RTMP_MSG_VIDEO;
            break;
        case AVMEDIA_TYPE_AUDIO:
            type = RTMP_MSG_AUDIO;
            break;
        default: assert(0);
        };

        if (!type) 
            continue;

        pkt_ts = get_ts_from_packet(st, in_ctx->streams[packet.stream_index], 
            &packet, &first_pts);

        my_log_debug("av_read_frame(%s) with ts=%d/size=%d", 
            type == RTMP_MSG_VIDEO ? "video" : "audio", pkt_ts,
            packet.size);

        if (pkt_ts > rtmp_gettime()) {
            (*myos()->sleep)(pkt_ts - rtmp_gettime());
        }

        if (rtmp_send_data(st, type, packet.data, packet.size, pkt_ts)) {
            my_log_debug("rmtp_send_data() failed");
            return (void*)1;
        }
    }

    rtmp_send_eof(st);

    av_close_input_file(in_ctx);
    return NULL;
}
Esempio n. 25
0
int Encoder::openVideoFile(char *_videoFileName) {
  int returnCode;

  returnCode = av_open_input_file(&avFormatContext, _videoFileName, NULL, 0, NULL);
  if (returnCode < 0) {
    systemLog->sysLog(ERROR, "cannot open the video file: %s", strerror(errno));
    return -1;
  }
  videoFileName = _videoFileName;

  return returnCode;
}
Esempio n. 26
0
int openMovie(const char filePath[])
{
	int i;
	if(gFormatCtx != NULL){
		return -1;
	}
	if(av_open_input_file(&gFormatCtx,filePath,NULL,0,NULL)!=0){
		return -2;
	}
	if(av_find_stream_info(gFormatCtx)<0){
		return -3;
	}

	for(i=0; i< gFormatCtx->nb_streams; i++){
		if(gFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
			gVideoStreamIdx = i;
			break;
		}
	}

	if(gVideoStreamIdx == -1){
		return -4;
	}

	gVideoCodecCtx = gFormatCtx->streams[gVideoStreamIdx]->codec;

	gVideoCodec = avcodec_find_decoder(gVideoCodecCtx->codec_id);
	if(gVideoCodec == NULL){
		return -5;
	}

	if(avcodec_open(gVideoCodecCtx, gVideoCodec) <0 ){
		return -6;
	}

	gFrame = avcodec_alloc_frame();
	if(gFrame == NULL){
		return -7;
	}

	gFrameRGB = avcodec_alloc_frame();
	if(gFrameRGB == NULL){
		return -8;
	}

	gPictureSize = avpicture_get_size(PIX_FMT_RGB565LE, gVideoCodecCtx->width, gVideoCodecCtx->height);

	gVideoBuffer = (uint8_t*)(malloc(sizeof(uint8_t)* gPictureSize));
	avpicture_fill ((AVPicture*)gFrameRGB, gVideoBuffer, PIX_FMT_RGB565LE, gVideoCodecCtx->width, gVideoCodecCtx->height);

	return 0;
}
Esempio n. 27
0
int main(int argc, char *argv[])
{
  av_register_all();  
  AVFormatContext *pFormatCtx;
  AVCodecContext *pCodecCtx;
  AVCodec *pCodec;
  int audioStream = -1;
  
  // Open file
  if (av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL) != 0)
    return -1; // Couldn't open file
  printf("opened %s\n", argv[1]);
  
  // Get stream information
  if (av_find_stream_info(pFormatCtx) < 0)
    return -1; // No stream information found
  
  // Debugging function
  dump_format(pFormatCtx, 0, argv[1], 0);
  
  // Find the first audio stream
  for (int i = 0; i < pFormatCtx->nb_streams; i++)
  {
    if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
    {
      audioStream = i;
      break;
    }
  }
  if (audioStream == -1)
    return -1; // No audio stream found
  
  // Return a pointer to the codec context for the video stream
  pCodecCtx = pFormatCtx->streams[audioStream]->codec;
  
  // Find the correct decoder
  pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
  if (pCodec == NULL)
  {
    printf("Unsupported codec!\n");
    return -1; // Codec not found
  }
  printf("found codec %d\n", pCodecCtx->codec_id);
  
  // Open correct codec
  if (avcodec_open(pCodecCtx, pCodec) < 0)
    return -1; // Couldn't open codec
  printf("opened codec %d\n", pCodecCtx->codec_id);
  
}
Esempio n. 28
0
int get_content_info(Content *content)
{
    if(av_open_input_file(&content->format_ctx, content->name, NULL, 0, NULL) != 0)
    {
	return -1;
    }
    if(av_find_stream_info(content->format_ctx) < 0)
    {
	return -1;
    }

    av_dump_format(content->format_ctx, 0, content->name, 0);
    return 0;
}
Esempio n. 29
0
status_t MediaPlayer::setDataSource(const char *url)
{
    status_t err = BAD_VALUE;
	// Open video file
	if(av_open_input_file(&mMovieFile, url, NULL, 0, NULL) != 0) {
		return INVALID_OPERATION;
	}
	// Retrieve stream information
	if(av_find_stream_info(mMovieFile) < 0) {
		return INVALID_OPERATION;
	}
	mCurrentState = MEDIA_PLAYER_INITIALIZED;

    return NO_ERROR;
}
Esempio n. 30
0
static inline int
lav_open(AVFormatContext **ctx, const char *filename)
{
	int ret;
#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
	ret = avformat_open_input(ctx, filename, NULL, NULL);
	if (ret == 0)
		avformat_find_stream_info(*ctx, NULL);
#else
	ret = av_open_input_file(ctx, filename, NULL, 0, NULL);
	if (ret == 0)
		av_find_stream_info(*ctx);
#endif
	return ret;
}