static void audio_cleanup_spdif_muxer(audio_decoder_t *ad) { if(ad->ad_spdif_muxer == NULL) return; free(ad->ad_spdif_frame); ad->ad_spdif_frame = NULL; ad->ad_spdif_frame_alloc = 0; av_free(ad->ad_spdif_muxer->pb); free(ad->ad_mux_buffer); ad->ad_mux_buffer = NULL; avformat_free_context(ad->ad_spdif_muxer); ad->ad_spdif_muxer = NULL; }
void VideoWriter::endWriting() { qDebug() << "End writing"; if (currentlyWriting) { currentlyWriting = false; int out_size; while ((out_size = avcodec_encode_video(video_st->codec, video_outbuf, video_outbuf_size, NULL)) > 0) { AVPacket pkt; av_init_packet(&pkt); if (video_st->codec->coded_frame->pts != AV_NOPTS_VALUE) pkt.pts= av_rescale_q(video_st->codec->coded_frame->pts, video_st->codec->time_base, video_st->time_base); if(video_st->codec->coded_frame->key_frame) pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index= video_st->index; pkt.data= video_outbuf; pkt.size= out_size; int ret = av_write_frame(oc, &pkt); } // write FFMPEG trailer av_write_trailer(oc); /* close each codec */ if (video_st) { avcodec_close(video_st->codec); av_free(picture->data[0]); av_free(picture); av_free(video_outbuf); video_outbuf=NULL; av_free(tmp_picture); tmp_picture=NULL; } avio_close(oc->pb); /* free the stream and context */ avformat_free_context(oc); oc=NULL; // close(info->fdts); } }
static void free_avformat(struct ffmpeg_mux *ffm) { if (ffm->output) { if ((ffm->output->oformat->flags & AVFMT_NOFILE) == 0) avio_close(ffm->output->pb); avformat_free_context(ffm->output); ffm->output = NULL; } if (ffm->audio_streams) { free(ffm->audio_streams); } ffm->video_stream = NULL; ffm->audio_streams = NULL; ffm->num_audio_streams = 0; }
static void cleanup_avcontext(struct GrooveEncoderPrivate *e) { if (e->stream) { avcodec_close(e->stream->codec); // stream is freed by freeing the AVFormatContext e->stream = NULL; } if (e->fmt_ctx) { avformat_free_context(e->fmt_ctx); e->fmt_ctx = NULL; } e->sent_header = 0; e->encode_head = NULL; e->encode_pos = -1.0; e->encode_pts = 0; e->next_pts = 0; }
void FFmpegDecoder::Close() { if (av && av->stream) { avcodec_close(av->stream->codec); av->stream = NULL; } if (av && av->ctx) { avio_close(av->ctx->pb); avformat_free_context(av->ctx); av->ctx = NULL; } if (av && av->fifo) av_fifo_reset(av->fifo); if (av) av->started = false; }
RtspThread::~RtspThread() { if ( mFormatContext ) { #if LIBAVFORMAT_VERSION_CHECK(52, 96, 0, 96, 0) avformat_free_context( mFormatContext ); #else av_free_format_context( mFormatContext ); #endif mFormatContext = NULL; } if ( mSessDesc ) { delete mSessDesc; mSessDesc = NULL; } delete mAuthenticator; }
VideoStore::~VideoStore(){ /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); avcodec_close(video_st->codec); if (audio_st) avcodec_close(audio_st->codec); if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ avformat_free_context(oc); }
int dc_ffmpeg_video_muxer_close(VideoOutputFile *video_output_file) { u32 i; av_write_trailer(video_output_file->av_fmt_ctx); avio_close(video_output_file->av_fmt_ctx->pb); // free the streams for (i = 0; i < video_output_file->av_fmt_ctx->nb_streams; i++) { avcodec_close(video_output_file->av_fmt_ctx->streams[i]->codec); av_freep(&video_output_file->av_fmt_ctx->streams[i]->info); } //video_output_file->av_fmt_ctx->streams[video_output_file->vstream_idx]->codec = NULL; avformat_free_context(video_output_file->av_fmt_ctx); return 0; }
int H264BS2Video::close() { if ( m_outfctx != NULL ) { //printf("write trailer....\n"); av_write_trailer(m_outfctx); if (m_isfile) { //printf("close...\n"); avio_close(m_outfctx->pb); // 关闭文件 } avformat_free_context(m_outfctx); m_outfctx = NULL; avformat_close_input(&m_infctx); m_infctx = NULL; } return 0; }
void FFMPEGer::reset(){ /* Close each codec. */ if (have_video) close_stream(fmt_ctx, &video_st); if (have_audio) close_stream(fmt_ctx, &audio_st); if (!(fmt->flags & AVFMT_NOFILE)){ /* Close the output file. */ avio_close(fmt_ctx->pb); fmt_ctx->pb = NULL; } /* free the stream */ if(fmt_ctx != NULL){ avformat_free_context(fmt_ctx); fmt_ctx = NULL; } }
int main(int argc, const char * argv[]){ AVFormatContext *pFormatCtx = NULL; VideoResolution *res = NULL; AVRational * aspect_ratio = NULL; fft_stream_info_list *list; if(argc < 2) { return 1; } av_register_all(); if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) { return 1; } av_find_stream_info(pFormatCtx); //av_dump_format(pFormatCtx,0, argv[1], 0); list = fft_get_stream_info(pFormatCtx); if(list) { fft_stream_info *node = list->node; while(node) { char* type[3] = {"AUDIO", "VIDEO", "UNKNOWN"}; printf("%s=%s\n", type[node->type], node->name); node = node->next; } } fft_free_stream_info_list(list); res = fft_get_video_resolution(pFormatCtx); if(res) { printf("Resolution=%dx%d\n", res->width, res->height); } aspect_ratio = fft_get_video_aspect_ratio(pFormatCtx); if(aspect_ratio) { printf("Aspect_Ratio=%d:%d\n", aspect_ratio->num, aspect_ratio->den); } free(res); avio_close(pFormatCtx->pb); avformat_free_context(pFormatCtx); return 0; }
void stream_close(stream_t *stream) { if (!stream) { return; } track_free(stream->track); av_free_packet(&stream->src_packet); av_free_packet(&stream->encode_packet); avcodec_free_frame(&stream->decode_frame); avcodec_free_frame(&stream->resample_frame); avcodec_free_frame(&stream->encode_frame); av_audio_fifo_free(stream->src_buf); resampler_free(&stream->resampler); if (stream->decoder) { avcodec_close(stream->decoder); } if (stream->encoder) { avcodec_close(stream->encoder); av_free(stream->encoder); } av_free(stream->dst_iobuf); av_free(stream->dst_ioctx); av_free(stream->resample_buf); av_free(stream->encode_buf); if (stream->src_ctx) { avformat_close_input(&stream->src_ctx); } if (stream->dst_ctx) { avformat_free_context(stream->dst_ctx); } free(stream); }
void FFmpegEncoderClose(struct FFmpegEncoder* encoder) { if (!encoder->context) { return; } av_write_trailer(encoder->context); avio_close(encoder->context->pb); if (encoder->audioCodec) { av_free(encoder->postaudioBuffer); if (encoder->audioBuffer) { av_free(encoder->audioBuffer); } #if LIBAVCODEC_VERSION_MAJOR >= 55 av_frame_free(&encoder->audioFrame); #else avcodec_free_frame(&encoder->audioFrame); #endif avcodec_close(encoder->audio); if (encoder->resampleContext) { avresample_close(encoder->resampleContext); } if (encoder->absf) { av_bitstream_filter_close(encoder->absf); encoder->absf = 0; } } #if LIBAVCODEC_VERSION_MAJOR >= 55 av_frame_free(&encoder->videoFrame); #else avcodec_free_frame(&encoder->videoFrame); #endif avcodec_close(encoder->video); sws_freeContext(encoder->scaleContext); encoder->scaleContext = NULL; avformat_free_context(encoder->context); encoder->context = 0; }
void VideoEncoder::endCapture() { if(mpOutputContext) { // Flush the codex avcodec_send_frame(mpCodecContext, nullptr); flush(mpCodecContext, mpOutputContext, mpOutputStream, mFilename); av_write_trailer(mpOutputContext); avio_closep(&mpOutputContext->pb); avcodec_free_context(&mpCodecContext); av_frame_free(&mpFrame); sws_freeContext(mpSwsContext); avformat_free_context(mpOutputContext); mpOutputContext = nullptr; mpOutputStream = nullptr; } safe_delete(mpFlippedImage) }
/** \fn closeMuxer */ bool muxerFFmpeg::closeMuxer() { if(oc) { if(initialized==true) { av_write_trailer(oc); avio_close(oc->pb); } avformat_free_context(oc); oc=NULL; } for(int i=0;i<ADM_MAX_AUDIO_STREAM;i++) { audio_st[i]=NULL; } video_st=NULL; return true; }
void AVIDump::CloseVideoFile() { av_frame_free(&s_src_frame); av_frame_free(&s_scaled_frame); avcodec_free_context(&s_codec_context); if (s_format_context) { avio_closep(&s_format_context->pb); } avformat_free_context(s_format_context); s_format_context = nullptr; if (s_sws_context) { sws_freeContext(s_sws_context); s_sws_context = nullptr; } }
CVideoLivRecord::~CVideoLivRecord(void) { av_write_trailer(m_pAVFormatContext); if (m_bHasVideo){ avcodec_close(m_pVideoStream->codec); av_frame_free(&m_pVideoFrame); av_frame_free(&m_pVideoBkFrame); sws_freeContext(m_pVideoSwsctx); swr_free(&m_pVideoSwrctx); } if (m_bHasAudio){ avcodec_close(m_pAudioStream->codec); av_frame_free(&m_pVideoFrame); av_frame_free(&m_pVideoBkFrame); swr_free(&m_pAudioSwrctx); } if (!(m_pAVFormatContext->oformat->flags & AVFMT_NOFILE)) avio_close(m_pAVFormatContext->pb); avformat_free_context(m_pAVFormatContext); }
/** * @brief free segmenter context * @param context segmenter context */ void segmenter_free_context(SegmenterContext* context) { if (context->bfilter) { av_bitstream_filter_close(context->bfilter); } if (context->output) { avformat_free_context(context->output); } if (context->buf) { free(context->buf); } if (context->durations) { free(context->durations); } free(context); }
void Muxer::Free() { if(m_format_context != NULL) { // write trailer (needed to free private muxer data) if(m_started) { if(av_write_trailer(m_format_context) != 0) { // we can't throw exceptions here because this is called from the destructor Logger::LogError("[Muxer::Free] " + Logger::tr("Error: Can't write trailer, continuing anyway.", "Don't translate 'trailer'")); } m_started = false; } // destroy the encoders for(unsigned int i = 0; i < m_format_context->nb_streams; ++i) { if(m_encoders[i] != NULL) { delete m_encoders[i]; // no deadlock: nothing in Muxer is locked in this thread m_encoders[i] = NULL; } } // close file if(m_format_context->pb != NULL) { avio_close(m_format_context->pb); m_format_context->pb = NULL; } // free everything #if SSR_USE_AVFORMAT_FREE_CONTEXT avformat_free_context(m_format_context); m_format_context = NULL; #else for(unsigned int i = 0; i < m_format_context->nb_streams; ++i) { av_freep(&m_format_context->streams[i]->codec); av_freep(&m_format_context->streams[i]); } av_free(m_format_context); m_format_context = NULL; #endif } }
/** * 解码文件 * @param yuvPath 目标的yuv文件路径 * @return */ int MP4Decoder::DecodeFile(const char *yuvPath) { yuv_file = fopen(yuvPath, "wb+"); if (yuv_file == NULL) { LOGE("could not open output file"); return -1; } while (av_read_frame(pFormatCtx, pAvPacket) >= 0) { DecodePacket(pCodecCtx, pAvPacket, pFrame); } //收尾 DecodePacket(pCodecCtx, NULL, pFrame); if (pSwsContext != NULL) { sws_freeContext(pSwsContext); pSwsContext = NULL; } //关闭文件 fclose(yuv_file); if (pCodecCtx != NULL) { avcodec_close(pCodecCtx); avcodec_free_context(&pCodecCtx); pCodecCtx = NULL; } if (pFrame != NULL) { av_free(pFrame); pFrame = NULL; } if (pFrameYUV != NULL) { av_free(pFrameYUV); pFrameYUV = NULL; } if (pFormatCtx != NULL) { avformat_close_input(&pFormatCtx); avformat_free_context(pFormatCtx); pFormatCtx = NULL; } return 0; }
int cv_finance_encoder_video_input_end( struct Encoderinfo* encoder_handle ) { //handle delay int ret = flush_encoder(encoder_handle->pFormatCtx, 0); if (ret < 0) { printf("Flushing encoder failed\n"); return -1; } //Write file trailer av_write_trailer(encoder_handle->pFormatCtx); //Clean if (encoder_handle->video_st) { avcodec_close(encoder_handle->video_st->codec); av_free(encoder_handle->pFrame); // av_free(encoder_handle->picture_buf); } avio_close(encoder_handle->pFormatCtx->pb); avformat_free_context(encoder_handle->pFormatCtx); return 0; }
bool AVMuxer::close() { if (!isOpen()) return true; av_write_trailer(d->format_ctx); // close AVCodecContext* in encoder // custom io will call avio_close in ~MediaIO() if (!(d->format_ctx->oformat->flags & AVFMT_NOFILE) && !(d->format_ctx->flags & AVFMT_FLAG_CUSTOM_IO)) { if (d->format_ctx->pb) { avio_close(d->format_ctx->pb); d->format_ctx->pb = 0; } } avformat_free_context(d->format_ctx); d->format_ctx = 0; d->audio_streams.clear(); d->video_streams.clear(); d->subtitle_streams.clear(); d->started = false; return true; }
ffCanvas::Impl::~Impl() { if (!mError) { AVStream* stream = mOutputCtx->streams[0]; AVPacket pkt; av_init_packet(&pkt); pkt.data = NULL; // packet data will be allocated by the encoder pkt.size = 0; int ret = 0; for (int got_output = 1; got_output; ) { ret = avcodec_encode_video2(stream->codec, &pkt, NULL, &got_output); if (ret < 0) break; if (got_output) { pkt.stream_index = stream->index; if (stream->codec->coded_frame->key_frame) pkt.flags |= AV_PKT_FLAG_KEY; ret = av_write_frame(mOutputCtx, &pkt); if (ret < 0) av_free_packet(&pkt); //av_free_packet(&pkt); } } ret = av_write_trailer(mOutputCtx); if (ret >= 0) ret = avio_close(mOutputCtx->pb); if (ret < 0) mError = "error closing movie file"; } if (mOutputCtx) { avformat_free_context(mOutputCtx); mOutputCtx = NULL; } if (mFrame) { av_free(mFrame); mFrame = NULL; } }
void FFMPEGInvoker::finish(EncodingContext* ctx, const SendRequest& req) { av_write_trailer(ctx->formatCtx); /* Close each codec. */ if (ctx->videoStream) closeVideo(ctx, ctx->formatCtx, ctx->videoStream); if (!(ctx->formatCtx->oformat->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(ctx->formatCtx->pb); /* free the stream */ avformat_free_context(ctx->formatCtx); // read file std::ifstream movieFile(ctx->filename.c_str()); movieFile.seekg(0, std::ios::end); size_t length = movieFile.tellg(); movieFile.seekg(0, std::ios::beg); char* movieBuffer = (char*)malloc(length); movieFile.read(movieBuffer, length); // move to desktop for checking // int err = rename(ctx->filename.c_str(), "/Users/sradomski/Desktop/foo.mpg"); // if (err) { // printf("%s", strerror(errno)); // } std::string context; Event::getParam(req.params, "context", context); Event event; event.name = "render.done"; event.data.compound["context"] = Data(context, Data::INTERPRETED); event.data.compound["movie"] = Data(movieBuffer, length, "video/mpeg", true); event.data.compound["filename"] = Data(std::string("movie.") + ctx->extension, Data::VERBATIM); returnEvent(event); }
int dc_ffmpeg_audio_muxer_close(AudioOutputFile * p_aoutf) { int i; av_write_trailer(p_aoutf->p_fmt); avio_close(p_aoutf->p_fmt->pb); // free the streams for (i = 0; i < p_aoutf->p_fmt->nb_streams; i++) { avcodec_close(p_aoutf->p_fmt->streams[i]->codec); av_freep(&p_aoutf->p_fmt->streams[i]->info); } //p_voutf->p_fmt->streams[p_voutf->i_vstream_idx]->codec = NULL; avformat_free_context(p_aoutf->p_fmt); //p_aoutf->acc_samples = 0; return 0; }
static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt) { WebMChunkContext *wc = s->priv_data; AVFormatContext *oc = wc->avf; AVStream *st = s->streams[pkt->stream_index]; int ret; if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts, st->time_base, (AVRational) {1, 1000}); wc->prev_pts = pkt->pts; } // For video, a new chunk is started only on key frames. For audio, a new // chunk is started based on chunk_duration. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY)) || (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) { wc->duration_written = 0; if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) { goto fail; } } ret = oc->oformat->write_packet(oc, pkt); if (ret < 0) goto fail; fail: if (ret < 0) { oc->streams = NULL; oc->nb_streams = 0; avformat_free_context(oc); } return ret; }
static AVStream* init_mp4_output(AVFormatContext *s, struct camera *cam) { int ret; AVStream *ost; strcpy(s->filename + strlen(s->filename) - 4, "mp4"); if((s->oformat = av_guess_format("mp4", NULL, NULL)) == NULL) { av_err_msg("av_guess_format", 0); return NULL; } if((ret = avio_open2(&s->pb, s->filename, AVIO_FLAG_WRITE, NULL, NULL)) < 0) { av_err_msg("avio_open2", ret); return NULL; } if((ost = copy_ctx_from_input(s, cam)) == NULL) { fprintf(stderr, "copy_ctx_from_input failed\n"); return NULL; } if((ret = avformat_write_header(s, NULL)) < 0) { av_err_msg("avformat_write_header", ret); avformat_free_context(s); return NULL; } return ost; }
/** * ffmpeg_close * Closes a video file. * * Returns * Function returns nothing. */ void ffmpeg_close(struct ffmpeg *ffmpeg) { if (ffmpeg->tlapse != TIMELAPSE_APPEND) { av_write_trailer(ffmpeg->oc); } /* Close each codec */ if (ffmpeg->video_st) { pthread_mutex_lock(&global_lock); avcodec_close(AVSTREAM_CODEC_PTR(ffmpeg->video_st)); pthread_mutex_unlock(&global_lock); } av_freep(&ffmpeg->picture); free(ffmpeg->video_outbuf); if (!(ffmpeg->oc->oformat->flags & AVFMT_NOFILE)) { if (ffmpeg->tlapse != TIMELAPSE_APPEND) { avio_close(ffmpeg->oc->pb); } } avformat_free_context(ffmpeg->oc); free(ffmpeg); }
int player_stop(PLAYER_HANDLE handle){ media_handle_union_t * media_handle = (media_handle_union_t *) handle; if (media_handle == NULL) { log_chris(ANDROID_LOG_ERROR ,TAG ,"media handle is null"); return -1; } media_handle->stop_mark = 1; //stop and exit the player ,free memory //signal the video_queue and audio_queue pthread_cond_t full_cond pthread_cond_signal(&media_handle->decode_video_var->video_queue.full_cond); pthread_cond_signal(&media_handle->decode_audio_var->audio_queue.full_cond); // pthread_cond_signal(&media_handle->decode_video_var->video_queue.cond); pthread_cond_signal(&media_handle->decode_audio_var->audio_queue.cond); //picture_queue pthread_cond_signal(&media_handle->decode_video_var->rgb565_queue.cond_empty); usleep(200 *1000); log_chris(ANDROID_LOG_ERROR ,TAG ,"~~== player_stop function over"); //free memory frame_queue_destroy(&media_handle->decode_video_var->rgb565_queue); //free video decode av_free(media_handle->decode_video_var->yuv_frame); av_free(media_handle->decode_video_var->decoded_frame); av_free(media_handle->decode_video_var->decoded_video_buf); avcodec_close(media_handle->decode_video_var->ptr_video_codec_ctx); sws_freeContext(media_handle->decode_video_var->img_convert_ctx); free(media_handle->decode_video_var); //free audio decode free(media_handle->decode_audio_var); // avformat_free_context(media_handle->ptr_format_ctx); //free media_handle free(media_handle); }
static void audio_setup_spdif_muxer(audio_decoder_t *ad, AVCodec *codec) { AVOutputFormat *ofmt = av_guess_format("spdif", NULL, NULL); if(ofmt == NULL) return; const int mux_buffer_size = 16384; assert(ad->ad_mux_buffer == NULL); ad->ad_mux_buffer = malloc(mux_buffer_size); AVFormatContext *fctx = avformat_alloc_context(); fctx->oformat = ofmt; fctx->pb = avio_alloc_context(ad->ad_mux_buffer, mux_buffer_size, 1, ad, NULL, spdif_mux_write, NULL); AVStream *s = avformat_new_stream(fctx, codec); s->codec->sample_rate = 48000; // ??? if(avcodec_open2(s->codec, codec, NULL)) { TRACE(TRACE_ERROR, "audio", "Unable to open %s codec for SPDIF", codec->name); bad: av_free(fctx->pb); free(ad->ad_mux_buffer); ad->ad_mux_buffer = NULL; avformat_free_context(fctx); return; } if(avformat_write_header(fctx, NULL)) { TRACE(TRACE_ERROR, "audio", "Unable to open SPDIF muxer", codec->name); goto bad; } ad->ad_spdif_muxer = fctx; TRACE(TRACE_DEBUG, "audio", "SPDIF muxer opened"); }