bool MediaEngine::openContext() { #ifdef USE_FFMPEG #ifdef _DEBUG av_log_set_level(AV_LOG_VERBOSE); av_log_set_callback(&ffmpeg_logger); #endif u8* tempbuf = (u8*)av_malloc(m_bufSize); AVFormatContext *pFormatCtx = avformat_alloc_context(); m_pFormatCtx = (void*)pFormatCtx; m_pIOContext = (void*)avio_alloc_context(tempbuf, m_bufSize, 0, (void*)this, _MpegReadbuffer, NULL, _MpegSeekbuffer); pFormatCtx->pb = (AVIOContext*)m_pIOContext; // Open video file if(avformat_open_input((AVFormatContext**)&m_pFormatCtx, NULL, NULL, NULL) != 0) return false; if(avformat_find_stream_info(pFormatCtx, NULL) < 0) return false; // Find the first video stream for(int i = 0; i < (int)pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { m_videoStream = i; break; } } if(m_videoStream == -1) return false; // Get a pointer to the codec context for the video stream m_pCodecCtx = (void*)pFormatCtx->streams[m_videoStream]->codec; AVCodecContext *pCodecCtx = (AVCodecContext*)m_pCodecCtx; // Find the decoder for the video stream AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec == NULL) return false; // Open codec AVDictionary *optionsDict = 0; if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<0) return false; // Could not open codec setVideoDim(); int mpegoffset = bswap32(*(int*)(m_pdata + 8)); m_demux = new MpegDemux(m_pdata, m_streamSize, mpegoffset); m_demux->setReadSize(m_readSize); m_demux->demux(); m_audioPos = 0; m_audioContext = Atrac3plus_Decoder::OpenContext(); m_isVideoEnd = false; #endif // USE_FFMPEG return true; }
bool MediaEngine::openContext() { #ifdef USE_FFMPEG InitFFmpeg(); if (m_pFormatCtx || !m_pdata) return false; m_mpegheaderReadPos = 0; m_decodingsize = 0; u8* tempbuf = (u8*)av_malloc(m_bufSize); m_pFormatCtx = avformat_alloc_context(); m_pIOContext = avio_alloc_context(tempbuf, m_bufSize, 0, (void*)this, _MpegReadbuffer, NULL, 0); m_pFormatCtx->pb = m_pIOContext; // Open video file if (avformat_open_input((AVFormatContext**)&m_pFormatCtx, NULL, NULL, NULL) != 0) return false; if (avformat_find_stream_info(m_pFormatCtx, NULL) < 0) { closeContext(); return false; } if (m_videoStream >= (int)m_pFormatCtx->nb_streams) { WARN_LOG_REPORT(ME, "Bad video stream %d", m_videoStream); m_videoStream = -1; } if (m_videoStream == -1) { // Find the first video stream for(int i = 0; i < (int)m_pFormatCtx->nb_streams; i++) { if(m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { m_videoStream = i; break; } } if(m_videoStream == -1) return false; } if (!setVideoStream(m_videoStream, true)) return false; setVideoDim(); m_audioContext = AT3Create(); m_isVideoEnd = false; m_noAudioData = false; m_mpegheaderReadPos++; av_seek_frame(m_pFormatCtx, m_videoStream, 0, 0); #endif // USE_FFMPEG return true; }
bool MediaEngine::openContext() { #ifdef USE_FFMPEG #ifdef _DEBUG av_log_set_level(AV_LOG_VERBOSE); av_log_set_callback(&ffmpeg_logger); #endif if (m_pFormatCtx || !m_pdata) return false; m_mpegheaderReadPos = 0; m_decodingsize = 0; u8* tempbuf = (u8*)av_malloc(m_bufSize); m_pFormatCtx = avformat_alloc_context(); m_pIOContext = avio_alloc_context(tempbuf, m_bufSize, 0, (void*)this, _MpegReadbuffer, NULL, 0); m_pFormatCtx->pb = m_pIOContext; // Open video file if(avformat_open_input((AVFormatContext**)&m_pFormatCtx, NULL, NULL, NULL) != 0) return false; if(avformat_find_stream_info(m_pFormatCtx, NULL) < 0) return false; if (m_videoStream >= (int)m_pFormatCtx->nb_streams) { WARN_LOG_REPORT(ME, "Bad video stream %d", m_videoStream); m_videoStream = -1; } if (m_videoStream == -1) { // Find the first video stream for(int i = 0; i < (int)m_pFormatCtx->nb_streams; i++) { if(m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { m_videoStream = i; break; } } if(m_videoStream == -1) return false; } // Get a pointer to the codec context for the video stream m_pCodecCtx = m_pFormatCtx->streams[m_videoStream]->codec; // Find the decoder for the video stream AVCodec *pCodec = avcodec_find_decoder(m_pCodecCtx->codec_id); if(pCodec == NULL) return false; // Open codec AVDictionary *optionsDict = 0; if(avcodec_open2(m_pCodecCtx, pCodec, &optionsDict)<0) return false; // Could not open codec setVideoDim(); m_audioContext = Atrac3plus_Decoder::OpenContext(); m_isVideoEnd = false; m_noAudioData = false; m_mpegheaderReadPos++; av_seek_frame(m_pFormatCtx, m_videoStream, 0, 0); #endif // USE_FFMPEG return true; }