int CAudioMuxer::CreateOutputContext(const char* pFileName, int nBitrate, int nSampleRate, int nChannels) { // avoid re-creating the context if (m_pCtx) { return E_HANDLED; } /* allocate the output media context */ avformat_alloc_output_context2(&m_pCtx, NULL, NULL, pFileName); if (!m_pCtx) { Log("Could not deduce output format from file extension: using MPEG.\n"); avformat_alloc_output_context2(&m_pCtx, NULL, "mpeg", pFileName); if (!m_pCtx) { return E_FAIL; } } m_pFmt = m_pCtx->oformat; // set the input bitrate, sample rate and channels m_nBitrate = nBitrate; m_nSampleRate = nSampleRate; m_nChannels = nChannels; /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ AVCodec *pAudioCodec; m_pAudioStream = NULL; AVCodecID eCodecID = m_pFmt->audio_codec; if (eCodecID != AV_CODEC_ID_NONE) { m_pAudioStream = AddAudioStream(&pAudioCodec, eCodecID); } /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (m_pAudioStream) { OpenAudio(pAudioCodec); } /* open the output file, if needed */ if (!(m_pFmt->flags & AVFMT_NOFILE)) { if (avio_open(&m_pCtx->pb, pFileName, AVIO_FLAG_WRITE) < 0) { Log("Could not open '%s'\n", pFileName); return E_IO; } } return S_OK; }
bool AVFormatWriter::Init(void) { if (m_videoOutBuf) delete [] m_videoOutBuf; if (m_width && m_height) m_videoOutBuf = new unsigned char[m_width * m_height * 2 + 10]; AVOutputFormat *fmt = av_guess_format(m_container.toAscii().constData(), NULL, NULL); if (!fmt) { LOG(VB_RECORD, LOG_ERR, LOC + QString("Init(): Unable to guess AVOutputFormat from container %1") .arg(m_container)); return false; } m_fmt = *fmt; if (m_width && m_height) { m_avVideoCodec = avcodec_find_encoder_by_name( m_videoCodec.toAscii().constData()); if (!m_avVideoCodec) { LOG(VB_RECORD, LOG_ERR, LOC + QString("Init(): Unable to find video codec %1").arg(m_videoCodec)); return false; } m_fmt.video_codec = m_avVideoCodec->id; } else m_fmt.video_codec = CODEC_ID_NONE; m_avAudioCodec = avcodec_find_encoder_by_name( m_audioCodec.toAscii().constData()); if (!m_avAudioCodec) { LOG(VB_RECORD, LOG_ERR, LOC + QString("Init(): Unable to find audio codec %1").arg(m_audioCodec)); return false; } m_fmt.audio_codec = m_avAudioCodec->id; m_ctx = avformat_alloc_context(); if (!m_ctx) { LOG(VB_RECORD, LOG_ERR, LOC + "Init(): Unable to allocate AVFormatContext"); return false; } m_ctx->oformat = &m_fmt; if (m_container == "mpegts") m_ctx->packet_size = 2324; snprintf(m_ctx->filename, sizeof(m_ctx->filename), "%s", m_filename.toAscii().constData()); if (m_fmt.video_codec != CODEC_ID_NONE) m_videoStream = AddVideoStream(); if (m_fmt.audio_codec != CODEC_ID_NONE) m_audioStream = AddAudioStream(); m_pkt = new AVPacket; if (!m_pkt) { LOG(VB_RECORD, LOG_ERR, LOC + "Init(): error allocating AVPacket"); return false; } if (av_set_parameters(m_ctx, NULL) < 0) { LOG(VB_RECORD, LOG_ERR, "Init(): Invalid output format parameters"); return false; } if ((m_videoStream) && (!OpenVideo())) { LOG(VB_RECORD, LOG_ERR, LOC + "Init(): OpenVideo() failed"); return false; } if ((m_audioStream) && (!OpenAudio())) { LOG(VB_RECORD, LOG_ERR, LOC + "Init(): OpenAudio() failed"); return false; } return true; }
bool AVFormatWriter::Init(void) { AVOutputFormat *fmt = av_guess_format(m_container.toLatin1().constData(), NULL, NULL); if (!fmt) { LOG(VB_RECORD, LOG_ERR, LOC + QString("Init(): Unable to guess AVOutputFormat from container %1") .arg(m_container)); return false; } m_fmt = *fmt; if (m_width && m_height) { m_avVideoCodec = avcodec_find_encoder_by_name( m_videoCodec.toLatin1().constData()); if (!m_avVideoCodec) { LOG(VB_RECORD, LOG_ERR, LOC + QString("Init(): Unable to find video codec %1").arg(m_videoCodec)); return false; } m_fmt.video_codec = m_avVideoCodec->id; } else m_fmt.video_codec = AV_CODEC_ID_NONE; m_avAudioCodec = avcodec_find_encoder_by_name( m_audioCodec.toLatin1().constData()); if (!m_avAudioCodec) { LOG(VB_RECORD, LOG_ERR, LOC + QString("Init(): Unable to find audio codec %1").arg(m_audioCodec)); return false; } m_fmt.audio_codec = m_avAudioCodec->id; m_ctx = avformat_alloc_context(); if (!m_ctx) { LOG(VB_RECORD, LOG_ERR, LOC + "Init(): Unable to allocate AVFormatContext"); return false; } m_ctx->oformat = &m_fmt; if (m_container == "mpegts") m_ctx->packet_size = 2324; snprintf(m_ctx->filename, sizeof(m_ctx->filename), "%s", m_filename.toLatin1().constData()); if (m_fmt.video_codec != AV_CODEC_ID_NONE) m_videoStream = AddVideoStream(); if (m_fmt.audio_codec != AV_CODEC_ID_NONE) m_audioStream = AddAudioStream(); if ((m_videoStream) && (!OpenVideo())) { LOG(VB_RECORD, LOG_ERR, LOC + "Init(): OpenVideo() failed"); return false; } if ((m_audioStream) && (!OpenAudio())) { LOG(VB_RECORD, LOG_ERR, LOC + "Init(): OpenAudio() failed"); return false; } return true; }
AVWRAP_DECL int AVWrapper_Init( void (*pAddFileLogRaw)(const char*), const char* pFilename, const char* pDesc, const char* pSoundFile, const char* pFormatName, const char* pVCodecName, const char* pACodecName, int Width, int Height, int FramerateNum, int FramerateDen, int VQuality) { int ret; AddFileLogRaw = pAddFileLogRaw; av_log_set_callback( &LogCallback ); g_Width = Width; g_Height = Height; g_Framerate.num = FramerateNum; g_Framerate.den = FramerateDen; g_VQuality = VQuality; // initialize libav and register all codecs and formats av_register_all(); // find format g_pFormat = av_guess_format(pFormatName, NULL, NULL); if (!g_pFormat) return FatalError("Format \"%s\" was not found", pFormatName); // allocate the output media context g_pContainer = avformat_alloc_context(); if (!g_pContainer) return FatalError("Could not allocate output context"); g_pContainer->oformat = g_pFormat; // store description of file av_dict_set(&g_pContainer->metadata, "comment", pDesc, 0); // append extesnion to filename char ext[16]; strncpy(ext, g_pFormat->extensions, 16); ext[15] = 0; ext[strcspn(ext,",")] = 0; snprintf(g_pContainer->filename, sizeof(g_pContainer->filename), "%s.%s", pFilename, ext); // find codecs g_pVCodec = avcodec_find_encoder_by_name(pVCodecName); g_pACodec = avcodec_find_encoder_by_name(pACodecName); // add audio and video stream to container g_pVStream = NULL; g_pAStream = NULL; if (g_pVCodec) { ret = AddVideoStream(); if (ret < 0) return ret; } else Log("Video codec \"%s\" was not found; video will be ignored.\n", pVCodecName); if (g_pACodec) { g_pSoundFile = fopen(pSoundFile, "rb"); if (g_pSoundFile) { fread(&g_Frequency, 4, 1, g_pSoundFile); fread(&g_Channels, 4, 1, g_pSoundFile); AddAudioStream(); } else Log("Could not open %s\n", pSoundFile); } else Log("Audio codec \"%s\" was not found; audio will be ignored.\n", pACodecName); if (!g_pAStream && !g_pVStream) return FatalError("No video, no audio, aborting..."); // write format info to log av_dump_format(g_pContainer, 0, g_pContainer->filename, 1); // open the output file, if needed if (!(g_pFormat->flags & AVFMT_NOFILE)) { if (avio_open(&g_pContainer->pb, g_pContainer->filename, AVIO_FLAG_WRITE) < 0) return FatalError("Could not open output file (%s)", g_pContainer->filename); } // write the stream header, if any avformat_write_header(g_pContainer, NULL); g_pVFrame->pts = -1; return 0; }
UINT CFlvUtils::InitStreams() { HRESULT hr = S_OK; if (!m_szFlvFile) hr = E_FAIL; // Add the audio and video streams using the default format codecs // and initialize the codecs if (SUCCEEDED(hr)) { if (m_pAVOutputFormat->video_codec != CODEC_ID_NONE) { //_tprintf(_T("### codec supports video.\n")); m_pAVStreamVideo = AddVideoStream(m_pAVOutputFormat->video_codec); if (m_pAVStreamVideo == NULL) { hr = E_FAIL; _ftprintf(stderr, _T("Error in CFlvUtils::Init():\n Could not allocate video stream!\n")); // TODO: error handling? } } } if (SUCCEEDED(hr)) { if (m_pAVOutputFormat->audio_codec != CODEC_ID_NONE) { //_tprintf(_T("### codec supports audio.\n")); m_pAVStreamAudio = AddAudioStream(m_pAVOutputFormat->audio_codec); if (m_pAVStreamAudio == NULL) { hr = E_FAIL; _ftprintf(stderr, _T("Error in CFlvUtils::Init():\n Could not allocate audio stream!\n")); // TODO: error handling? } } } if (SUCCEEDED(hr)) { // Set the output parameters (must be done even if no parameters). if (av_set_parameters(m_pAVFormatContext, NULL) < 0) { _ftprintf(stderr, _T("Error in CFlvUtils::Init():\n Invalid output format parameters!\n")); hr = E_FAIL; // TODO: error handling? } } if (SUCCEEDED(hr)) { // Dump codec format info to the shell dump_format(m_pAVFormatContext, 0, m_szFlvFile, 1); } // Now that all the parameters are set, we can open the audio and // video codecs and allocate the necessary encode buffers if (SUCCEEDED(hr)) { if (m_pAVStreamVideo) hr = OpenVideoStream(); } if (SUCCEEDED(hr)) { if (m_pAVStreamAudio) hr = OpenAudioStream(); } return hr; }
int CAVMuxer::Init(InputParams* pInputParam) { m_pParams = (InputParams*)malloc(sizeof(InputParams)); memcpy(m_pParams,pInputParam,sizeof(InputParams)); //m_pParams = pInputParam; char ouputName[256]; memset(ouputName,0,sizeof(ouputName)); // snprintf(ouputName,sizeof(ouputName),"udp://%s:%d?pkt_size=188",m_pParams->destip,m_pParams->destport); // snprintf(ouputName,sizeof(ouputName),"udp://%s:%d?pkt_size=1316?buffer_size=0",m_pParams->destip,m_pParams->destport); snprintf(ouputName,sizeof(ouputName),"bbcvudp://%s:%d?bit_rate=%d&pkt_size=1316&buffer_size=0",m_pParams->destip, m_pParams->destport,m_pParams->nPeakBitRate); av_register_all(); avformat_network_init(); printf("m_pParams monitor is %s \n",m_pParams->monitorName); /* allocate the output media context */ /*avformat_alloc_output_context2(&m_pFormatCtx, NULL, "mpegts", filename); if (!m_pFormatCtx) { fprintf(stderr, "Could not deduce output format '%s'\n", filename); return 0; } m_pOutputFmt = m_pFormatCtx->oformat; */ m_pOutputFmt = av_guess_format("mpegts", NULL, NULL); CodecID curCodecID; if (m_pParams->codecID == KY_CODEC_ID_MPEG2VIDEO) { curCodecID = CODEC_ID_MPEG2VIDEO; } else if (m_pParams->codecID == KY_CODEC_ID_H264) { curCodecID = CODEC_ID_H264; } else { //default curCodecID = CODEC_ID_MPEG2VIDEO; } m_pOutputFmt->video_codec = curCodecID; m_pOutputFmt->audio_codec = CODEC_ID_MP2; m_pFormatCtx = avformat_alloc_context(); if (!m_pFormatCtx) { fprintf(stderr, "FFMPEG: avformat_alloc_context error\n"); return -1; } m_pFormatCtx->oformat = m_pOutputFmt; strcpy(m_pFormatCtx->filename, ouputName); if (m_pOutputFmt->video_codec != CODEC_ID_NONE) { AddVideoStream(); } if (m_pOutputFmt->audio_codec != CODEC_ID_NONE) { AddAudioStream(); } av_dump_format(m_pFormatCtx, 0, ouputName, 1); /* open the output file, if needed */ if (!(m_pOutputFmt->flags & AVFMT_NOFILE)) { if (avio_open(&m_pFormatCtx->pb, ouputName, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Could not open '%s'\n", ouputName); return 1; } } fprintf(stderr, "open sucess '%s'\n", ouputName); AVDictionary *options = NULL; /* av_dict_set(&options, "mpegts_transport_stream_id", "1", 0); av_dict_set(&options, "mpegts_original_network_id", "1", 0); av_dict_set(&options, "mpegts_service_id", "1", 0); av_dict_set(&options, "mpegts_pmt_start_pid", "4096", 0); av_dict_set(&options, "mpegts_start_pid", "256", 0); av_dict_set(&options, "muxrate", "1", 0); */ char pmt_pid_str[64]; char service_id_str[64]; char video_pid_str[64]; char audio_pid_str[64]; //char muxrate_str[64]; snprintf(pmt_pid_str,sizeof(pmt_pid_str),"%d",pInputParam->pmt_pid); snprintf(service_id_str,sizeof(service_id_str),"%d",pInputParam->service_id); snprintf(video_pid_str,sizeof(video_pid_str),"%d",pInputParam->video_pid); snprintf(audio_pid_str,sizeof(audio_pid_str),"%d",pInputParam->audio_pid); //snprintf(muxrate_str,sizeof(muxrate_str),"%d",pInputParam->nBitRate); av_dict_set(&options, "mpegts_use_this_pid","1", 0); av_dict_set(&options, "mpegts_pmt_pid" ,pmt_pid_str , 0); av_dict_set(&options, "mpegts_service_id" ,service_id_str , 0); av_dict_set(&options, "mpegts_video_pid",video_pid_str, 0); av_dict_set(&options, "mpegts_audio_pid",audio_pid_str, 0); //av_dict_set(&options, "muxrate",muxrate_str, 0); /* Write the stream header, if any. */ if (avformat_write_header(m_pFormatCtx, &options)) { fprintf(stderr ,"FFMPEG: avformat_write_header error!\n"); av_dict_free(&options); return -1; } av_dict_free(&options); return 0; }