virtual ~LibAVFilterPrivate() { avfilter_graph_free(&filter_graph); if (avframe) { av_frame_free(&avframe); avframe = 0; } }
void groove_playlist_destroy(struct GroovePlaylist *playlist) { groove_playlist_clear(playlist); struct GroovePlaylistPrivate *p = (struct GroovePlaylistPrivate *) playlist; // wait for decode thread to finish p->abort_request = 1; pthread_cond_signal(&p->decode_head_cond); pthread_cond_signal(&p->sink_drain_cond); pthread_join(p->thread_id, NULL); every_sink(playlist, groove_sink_detach, 0); avfilter_graph_free(&p->filter_graph); av_frame_free(&p->in_frame); if (p->decode_head_mutex_inited) pthread_mutex_destroy(&p->decode_head_mutex); if (p->decode_head_cond_inited) pthread_cond_destroy(&p->decode_head_cond); if (p->sink_drain_cond_inited) pthread_cond_destroy(&p->sink_drain_cond); av_free(p); }
int configure_filtergraph(FilterGraph *fg) { AVFilterInOut *inputs, *outputs, *cur; int ret, i, init = !fg->graph, simple = !fg->graph_desc; const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter : fg->graph_desc; avfilter_graph_free(&fg->graph); if (!(fg->graph = avfilter_graph_alloc())) return AVERROR(ENOMEM); if (simple) { OutputStream *ost = fg->outputs[0]->ost; char args[255]; snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags); fg->graph->scale_sws_opts = av_strdup(args); } if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0) return ret; if (simple && (!inputs || inputs->next || !outputs || outputs->next)) { av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have " "exactly one input and output.\n", graph_desc); return AVERROR(EINVAL); } for (cur = inputs; !simple && init && cur; cur = cur->next) init_input_filter(fg, cur); for (cur = inputs, i = 0; cur; cur = cur->next, i++) if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) return ret; avfilter_inout_free(&inputs); if (!init || simple) { /* we already know the mappings between lavfi outputs and output streams, * so we can finish the setup */ for (cur = outputs, i = 0; cur; cur = cur->next, i++) configure_output_filter(fg, fg->outputs[i], cur); avfilter_inout_free(&outputs); if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0) return ret; } else { /* wait until output mappings are processed */ for (cur = outputs; cur;) { fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs), &fg->nb_outputs, fg->nb_outputs + 1); if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0])))) exit(1); fg->outputs[fg->nb_outputs - 1]->graph = fg; fg->outputs[fg->nb_outputs - 1]->out_tmp = cur; cur = cur->next; fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL; } } return 0; }
Filters::~Filters() { if (mFilterGraphPtr) { avfilter_graph_free(&mFilterGraphPtr); mFilterGraphPtr = nullptr; } }
bool setup() { avfilter_graph_free(&filter_graph); filter_graph = avfilter_graph_alloc(); //QString sws_flags_str; QString buffersrc_args = QString("video_size=%1x%2:pix_fmt=%3:time_base=%4/%5:sar=1") .arg(width).arg(height).arg(pixfmt).arg(1).arg(AV_TIME_BASE); qDebug("buffersrc_args=%s", buffersrc_args.toUtf8().constData()); AVFilter *buffersrc = avfilter_get_by_name("buffer"); Q_ASSERT(buffersrc); int ret = avfilter_graph_create_filter(&in_filter_ctx, buffersrc, "in", buffersrc_args.toUtf8().constData(), NULL, filter_graph); if (ret < 0) { qWarning("Can not create buffer source: %s", av_err2str(ret)); return false; } /* buffer video sink: to terminate the filter chain. */ AVFilter *buffersink = avfilter_get_by_name("buffersink"); Q_ASSERT(buffersink); if ((ret = avfilter_graph_create_filter(&out_filter_ctx, buffersink, "out", NULL, NULL, filter_graph)) < 0) { qWarning("Can not create buffer sink: %s", av_err2str(ret)); return false; } /* Endpoints for the filter graph. */ AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); outputs->name = av_strdup("in"); outputs->filter_ctx = in_filter_ctx; outputs->pad_idx = 0; outputs->next = NULL; inputs->name = av_strdup("out"); inputs->filter_ctx = out_filter_ctx; inputs->pad_idx = 0; inputs->next = NULL; //avfilter_graph_parse, avfilter_graph_parse2? if ((ret = avfilter_graph_parse_ptr(filter_graph, options.toUtf8().constData(), &inputs, &outputs, NULL)) < 0) { qWarning("avfilter_graph_parse_ptr fail: %s", av_err2str(ret)); avfilter_inout_free(&outputs); avfilter_inout_free(&inputs); return false; } if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) { qWarning("avfilter_graph_config fail: %s", av_err2str(ret)); avfilter_inout_free(&outputs); avfilter_inout_free(&inputs); return false; } avfilter_inout_free(&outputs); avfilter_inout_free(&inputs); avframe = av_frame_alloc(); return true; }
void MovieDecoder::deleteFilterGraph() { if (m_filterGraph) { av_frame_free(&m_filterFrame); avfilter_graph_free(&m_filterGraph); m_filterGraph = nullptr; } }
JNIEXPORT void JNICALL Java_org_jitsi_impl_neomedia_codec_FFmpeg_avfilter_1graph_1free (JNIEnv *env, jclass clazz, jlong graph) { AVFilterGraph *graph_ = (AVFilterGraph *) (intptr_t) graph; avfilter_graph_free(&graph_); }
static void destroy_graph(struct af_instance *af) { struct priv *p = af->priv; avfilter_graph_free(&p->graph); p->in = p->out = NULL; p->samples_in = 0; p->eof = false; }
Utility::FilterApplier::~FilterApplier() { if(filterGraph_) { avfilter_graph_free(&filterGraph_); } isRunning_=false; if(applier_.joinable()) { applier_.join(); } }
FilterGraph::~FilterGraph() { _inputAudioFrameBuffers.clear(); for(std::vector<Filter*>::iterator it = _filters.begin(); it < _filters.end(); ++it) { delete(*it); } avfilter_graph_free(&_graph); }
filter_wrapper& filter_wrapper::operator = (filter_wrapper&& t) { if (this == &t) return *this; if (frame_in != NULL) av_frame_free(&frame_in); if (frame_out != NULL) av_frame_free(&frame_out); if (frame_buffer_out != NULL) av_free(frame_buffer_out); if (frame_buffer_in != NULL) av_free(frame_buffer_in); if (outputs != NULL) avfilter_inout_free(&outputs); if (inputs != NULL) avfilter_inout_free(&inputs); if (filter_graph != NULL) avfilter_graph_free(&filter_graph); this->frame_in = t.frame_in; t.frame_in = NULL; this->frame_out = t.frame_out; t.frame_out = NULL; this->frame_buffer_in = t.frame_buffer_in; t.frame_buffer_in = NULL; this->frame_buffer_out = t.frame_buffer_out; t.frame_buffer_out = NULL; this->filter_graph = t.filter_graph; t.filter_graph = NULL; this->outputs = t.outputs; t.outputs = NULL; this->inputs = t.inputs; t.inputs = NULL; this->buffersink_ctx = t.buffersink_ctx; t.buffersink_ctx = NULL; this->buffersrc_ctx = t.buffersrc_ctx; t.buffersrc_ctx = NULL; this->width_ = t.width_; this->height_ = t.height_; this->width_out = t.width_out; this->height_out = t.height_out; this->fps_ = t.fps_; this->filter_descr_ = t.filter_descr_; t.filter_descr_.clear(); return *this; }
void CDVDVideoCodecFFmpeg::FilterClose() { if (m_pFilterGraph) { avfilter_graph_free(&m_pFilterGraph); // Disposed by above code m_pFilterIn = nullptr; m_pFilterOut = nullptr; } }
void CDVDVideoCodecFFmpeg::FilterClose() { if (m_pFilterGraph) { CLog::Log(LOGDEBUG, LOGVIDEO, "CDVDVideoCodecFFmpeg::FilterClose - Freeing filter graph"); avfilter_graph_free(&m_pFilterGraph); // Disposed by above code m_pFilterIn = nullptr; m_pFilterOut = nullptr; } }
int configure_filtergraph(FilterGraph *fg) { AVFilterInOut *inputs, *outputs, *cur; int ret, i, simple = !fg->graph_desc; const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter : fg->graph_desc; avfilter_graph_free(&fg->graph); if (!(fg->graph = avfilter_graph_alloc())) return AVERROR(ENOMEM); if (simple) { OutputStream *ost = fg->outputs[0]->ost; char args[512]; AVDictionaryEntry *e = NULL; snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags); fg->graph->scale_sws_opts = av_strdup(args); args[0] = '\0'; while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e, AV_DICT_IGNORE_SUFFIX))) { av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value); } if (strlen(args)) args[strlen(args) - 1] = '\0'; fg->graph->resample_lavr_opts = av_strdup(args); } if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0) return ret; if (simple && (!inputs || inputs->next || !outputs || outputs->next)) { av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have " "exactly one input and output.\n", graph_desc); return AVERROR(EINVAL); } for (cur = inputs, i = 0; cur; cur = cur->next, i++) if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) return ret; avfilter_inout_free(&inputs); for (cur = outputs, i = 0; cur; cur = cur->next, i++) configure_output_filter(fg, fg->outputs[i], cur); avfilter_inout_free(&outputs); if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0) return ret; return 0; }
void audio_filter_free_state(void* context) { audio_filter_state_t* state = (audio_filter_state_t*)context; avfilter_graph_free(&state->filter_graph); avcodec_close(state->decoder); av_free(state->decoder); avcodec_close(state->encoder); av_free(state->encoder); av_frame_free(&state->decoded_frame); av_frame_free(&state->filtered_frame); }
static void finish (player_t * const player) { ao_close (player->aoDev); player->aoDev = NULL; if (player->fgraph != NULL) { avfilter_graph_free (&player->fgraph); player->fgraph = NULL; } if (player->st != NULL && player->st->codec != NULL) { avcodec_close (player->st->codec); player->st = NULL; } if (player->fctx != NULL) { avformat_close_input (&player->fctx); } }
static void uninit(struct vf_instance *vf) { unsigned i; #define FREE_MPI_ARRAY(field) \ for (i = 0; i < FF_ARRAY_ELEMS(vf->imgctx.field); i++) \ if (vf->imgctx.field[i]) \ avfilter_unref_buffer(vf->imgctx.field[i]->priv); FREE_MPI_ARRAY(static_images); FREE_MPI_ARRAY(temp_images); FREE_MPI_ARRAY(export_images); FREE_MPI_ARRAY(numbered_images); avfilter_graph_free(&vf->priv->graph); av_free(vf->priv); }
int configure_filter(struct liveStream *ctx) { int ret = 0; int i = 0; /** Input Id */ long in_id; AVFilterInOut *outputs; AVFilterInOut *inputs; AVFilterInOut *cur; take_filter_lock(&ctx->filter_lock); avfilter_graph_free(&ctx->filter_graph); ctx->filter_graph = avfilter_graph_alloc(); if (NULL == ctx->filter_graph) { av_log(NULL,AV_LOG_ERROR,"Unable to allocate Filter\n"); return -1; } ret = avfilter_graph_parse2(ctx->filter_graph, ctx->graph_desc.str, &inputs, &outputs); if(ret < 0) return ret; for (i = 0,cur = inputs; cur; cur = cur->next,i++) { in_id = strtol(cur->name,NULL,0); if(in_id < 0 || in_id >= ctx->nb_input) { /** Invalid index of video provided */ ret = -1; break; } configure_input_filter(ctx, in_id, cur); } avfilter_inout_free(&inputs); ret = configure_output_filter(ctx,outputs); if(ret < 0) { printf("unable to configure output filter\n"); return ret; } avfilter_inout_free(&outputs); if ((ret = avfilter_graph_config(ctx->filter_graph, NULL)) < 0) return ret; give_filter_lock(&ctx->filter_lock); return 0; }
int init_complex_filtergraph(FilterGraph *fg) { AVFilterInOut *inputs, *outputs, *cur; AVFilterGraph *graph; int ret = 0; /* this graph is only used for determining the kinds of inputs * and outputs we have, and is discarded on exit from this function */ graph = avfilter_graph_alloc(); if(!graph) { return AVERROR(ENOMEM); } ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs); if(ret < 0) { goto fail; } for(cur = inputs; cur; cur = cur->next) { init_input_filter(fg, cur); } for(cur = outputs; cur;) { GROW_ARRAY(fg->outputs, fg->nb_outputs); fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0])); if(!fg->outputs[fg->nb_outputs - 1]) { exit_program(1); } fg->outputs[fg->nb_outputs - 1]->graph = fg; fg->outputs[fg->nb_outputs - 1]->out_tmp = cur; fg->outputs[fg->nb_outputs - 1]->type = avfilter_pad_get_type(cur->filter_ctx->output_pads, cur->pad_idx); cur = cur->next; fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL; } fail: avfilter_inout_free(&inputs); avfilter_graph_free(&graph); return ret; }
// Free the libavfilter graph (not c), reset all state. // Does not free pending data intentionally. static void free_graph(struct lavfi *c) { avfilter_graph_free(&c->graph); for (int n = 0; n < c->num_all_pads; n++) { struct lavfi_pad *pad = c->all_pads[n]; pad->filter = NULL; pad->filter_pad = -1; pad->buffer = NULL; mp_frame_unref(&pad->in_fmt); pad->buffer_is_eof = false; } c->initialized = false; c->draining_recover = false; c->in_pts = MP_NOPTS_VALUE; c->in_samples = 0; c->delay = 0; }
void audio_filter_free_state(void* context) { audio_filter_state_t* state = (audio_filter_state_t*)context; audio_filter_source_t* sources_cur; for (sources_cur = state->sources; sources_cur < state->sources_end; sources_cur++) { avcodec_close(sources_cur->decoder); av_free(sources_cur->decoder); } avcodec_close(state->sink.encoder); av_free(state->sink.encoder); avfilter_graph_free(&state->filter_graph); av_frame_free(&state->filtered_frame); av_frame_free(&state->decoded_frame); vod_memzero(state, sizeof(*state)); // support calling free twice }
static void mix_shutdown(mix_t *mix) { if (mix->amix_ctx) avfilter_free(mix->amix_ctx); mix->amix_ctx = NULL; if (mix->sink_ctx) avfilter_free(mix->sink_ctx); mix->sink_ctx = NULL; for (int i = 0; i < NUM_INPUTS; i++) { if (mix->src_ctxs[i]) avfilter_free(mix->src_ctxs[i]); mix->src_ctxs[i] = NULL; } resample_shutdown(&mix->resample); avfilter_graph_free(&mix->graph); format_init(&mix->format); }
void MovieDecoder::getScaledVideoFrame(int scaledSize, bool maintainAspectRatio, VideoFrame& videoFrame) { initializeFilterGraph(m_pFormatContext->streams[m_VideoStream]->time_base, scaledSize, maintainAspectRatio); auto del = [] (AVFrame* f) { av_frame_free(&f); }; std::unique_ptr<AVFrame, decltype(del)> res(av_frame_alloc(), del); checkRc(av_buffersrc_write_frame(m_pFilterSource, m_pFrame), "Failed to write frame to filter graph"); int attempts = 0; int rc = av_buffersink_get_frame(m_pFilterSink, res.get()); while (rc == AVERROR(EAGAIN) && attempts++ < 10) { decodeVideoFrame(); checkRc(av_buffersrc_write_frame(m_pFilterSource, m_pFrame), "Failed to write frame to filter graph"); rc = av_buffersink_get_frame(m_pFilterSink, res.get()); } checkRc(rc, "Failed to get buffer from filter"); videoFrame.width = res->width; videoFrame.height = res->height; videoFrame.lineSize = videoFrame.width * 4; if(videoFrame.frameData != nullptr) delete videoFrame.frameData; uint8_t * framedata = res->data[0]; videoFrame.frameData = new uint8_t[videoFrame.width * 4 * videoFrame.height]; for(int y = 0;y < videoFrame.height;y++) { memcpy(videoFrame.frameData + ((videoFrame.height - y - 1) * videoFrame.lineSize), framedata + (y * res->linesize[0]), videoFrame.lineSize); } if (m_pFilterGraph) { avfilter_graph_free(&m_pFilterGraph); } }
filter_wrapper::~filter_wrapper() { if (frame_in != NULL) av_frame_free(&frame_in); if (frame_out != NULL) av_frame_free(&frame_out); if (frame_buffer_out != NULL) av_free(frame_buffer_out); if (frame_buffer_in != NULL) av_free(frame_buffer_in); if (outputs != NULL) avfilter_inout_free(&outputs); if (inputs != NULL) avfilter_inout_free(&inputs); if (filter_graph != NULL) avfilter_graph_free(&filter_graph); }
int main(int argc, char **argv) { int ret; AVPacket packet; AVFrame *frame = av_frame_alloc(); AVFrame *filt_frame = av_frame_alloc(); int got_frame; if (!frame || !filt_frame) { perror("Could not allocate frame"); exit(1); } if (argc != 2) { fprintf(stderr, "Usage: %s file\n", argv[0]); exit(1); } avcodec_register_all(); av_register_all(); avfilter_register_all(); if ((ret = open_input_file(argv[1])) < 0) goto end; if ((ret = init_filters(filter_descr)) < 0) goto end; /* read all packets */ while (1) { if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) break; if (packet.stream_index == video_stream_index) { avcodec_get_frame_defaults(frame); got_frame = 0; ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error decoding video\n"); break; } if (got_frame) { frame->pts = av_frame_get_best_effort_timestamp(frame); /* push the decoded frame into the filtergraph */ if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) { av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n"); break; } /* pull filtered frames from the filtergraph */ while (1) { ret = av_buffersink_get_frame(buffersink_ctx, filt_frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; if (ret < 0) goto end; display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base); av_frame_unref(filt_frame); } av_frame_unref(frame); } } av_free_packet(&packet); } end: avfilter_graph_free(&filter_graph); if (dec_ctx) avcodec_close(dec_ctx); avformat_close_input(&fmt_ctx); av_frame_free(&frame); av_frame_free(&filt_frame); if (ret < 0 && ret != AVERROR_EOF) { char buf[1024]; av_strerror(ret, buf, sizeof(buf)); fprintf(stderr, "Error occurred: %s\n", buf); exit(1); } exit(0); }
int main(int argc, char **argv) { //curses int color_pair; if(initscr() == NULL){ fprintf(stderr, "init failure\n"); exit(EXIT_FAILURE); } /* start_colorは色属性を使用するときは最初に必ず実行する. initscrの直後に実行するのがよい習慣らしい. */ if(has_colors() == FALSE || start_color() == ERR){ endwin(); fprintf(stderr, "This term seems not to having Color\n"); exit(EXIT_FAILURE); } if(signal(SIGINT, sig_handler) == SIG_ERR || signal(SIGQUIT, sig_handler) == SIG_ERR){ fprintf(stderr, "signal failure\n"); exit(EXIT_FAILURE); } curs_set(0); /* 色のペアを作る */ color_pair = 1; for(color_pair = 1; color_pair < 256; color_pair++){ init_pair(color_pair, color_pair, color_pair); } refresh(); char filter_descr[10000]; int w, h; w = 80;//COLS; h = 25;//LINES; bak = malloc(sizeof (int) * LINES*COLS+1); sprintf(filter_descr, "scale=%d:%d", w, h); int ret; AVPacket packet; AVFrame frame; int got_frame; if (argc != 2) { fprintf(stderr, "Usage: %s file\n", argv[0]); exit(1); } avcodec_register_all(); av_register_all(); avfilter_register_all(); if ((ret = open_input_file(argv[1])) < 0) goto end; if ((ret = init_filters(filter_descr)) < 0) goto end; /* read all packets */ while (1) { AVFilterBufferRef *picref; if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) break; if (packet.stream_index == video_stream_index) { avcodec_get_frame_defaults(&frame); got_frame = 0; ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error decoding video\n"); break; } if (got_frame) { frame.pts = av_frame_get_best_effort_timestamp(&frame); /* push the decoded frame into the filtergraph */ if (av_buffersrc_add_frame(buffersrc_ctx, &frame, 0) < 0) { av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n"); break; } /* pull filtered pictures from the filtergraph */ while (repeat_flag) { ret = av_buffersink_get_buffer_ref(buffersink_ctx, &picref, 0); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; if (ret < 0) goto end; if (picref) { display_picref(picref, buffersink_ctx->inputs[0]->time_base); avfilter_unref_bufferp(&picref); } } } } av_free_packet(&packet); } end: endwin(); avfilter_graph_free(&filter_graph); if (dec_ctx) avcodec_close(dec_ctx); avformat_close_input(&fmt_ctx); if (ret < 0 && ret != AVERROR_EOF) { char buf[1024]; av_strerror(ret, buf, sizeof(buf)); fprintf(stderr, "Error occurred: %s\n", buf); exit(1); } exit(0); }
void close_filters(void) { avfilter_graph_free(&filter_graph); }
int main_dummy(int argc, char **argv) #endif { AVOutputFormat *ofmt = NULL; AVFormatContext *inVideoFmtCtx = NULL, *inAudioFmtCtx = NULL, *outFmtCtx = NULL; AVPacket pkt; const char *inVideo_filename, *inAudio_filename, *out_filename; int ret, i; if (argc < 3) { printf("usage: %s input output\n" "API example program to remux a media file with libavformat and libavcodec.\n" "The output format is guessed according to the file extension.\n" "\n", argv[0]); return 1; } inVideo_filename = argv[1]; inAudio_filename = argv[2]; out_filename = argv[3]; av_register_all(); /* =============== OPEN STREAMS ================*/ if ((ret = open_input_file2(argv[1], &inVideoFmtCtx)) < 0) goto end; if ((ret = open_input_file2(argv[2], &inAudioFmtCtx)) < 0) goto end; /* ========== ALLOCATE OUTPUT CONTEXT ==========*/ avformat_alloc_output_context2(&outFmtCtx, NULL, NULL, out_filename); if (!outFmtCtx) { fprintf(stderr, "Could not create output context\n"); ret = AVERROR_UNKNOWN; goto end; } ofmt = outFmtCtx->oformat; /* =============== SETUP VIDEO CODEC ================*/ for (i = 0; i < inVideoFmtCtx->nb_streams; i++) { AVStream *in_stream = inVideoFmtCtx->streams[i]; AVStream *out_stream = avformat_new_stream(outFmtCtx, in_stream->codec->codec); if (!out_stream) { fprintf(stderr, "Failed allocating output stream\n"); ret = AVERROR_UNKNOWN; goto end; } ret = avcodec_copy_context(out_stream->codec, in_stream->codec); if (ret < 0) { fprintf(stderr, "Failed to copy context from input to output stream codec context\n"); goto end; } out_stream->codec->codec_tag = 0; if (outFmtCtx->oformat->flags & AVFMT_GLOBALHEADER) out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; } /* =============== SETUP AUDIO CODEC ================*/ for (i = 0; i < inAudioFmtCtx->nb_streams; i++) { setup_mp3_audio_codec(outFmtCtx); } av_dump_format(outFmtCtx, 0, out_filename, 1); if (!(ofmt->flags & AVFMT_NOFILE)) { ret = avio_open(&outFmtCtx->pb, out_filename, AVIO_FLAG_WRITE); if (ret < 0) { fprintf(stderr, "Could not open output file '%s'", out_filename); goto end; } } ret = avformat_write_header(outFmtCtx, NULL); if (ret < 0) { fprintf(stderr, "Error occurred when opening output file\n"); goto end; } /* =============== SETUP FILTERS ================*/ init_filters(inAudioFmtCtx, outFmtCtx); AVStream *in_stream, *out_stream; AVFrame* frame; while (1) { /* =============== VIDEO STREAM ================*/ ret = av_read_frame(inVideoFmtCtx, &pkt); if (ret < 0) break; in_stream = inVideoFmtCtx->streams[pkt.stream_index]; out_stream = outFmtCtx->streams[pkt.stream_index]; log_packet(inVideoFmtCtx, &pkt, "in"); /* copy packet */ pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt.duration = (int)av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base); pkt.pos = -1; log_packet(outFmtCtx, &pkt, "out"); ret = av_interleaved_write_frame(outFmtCtx, &pkt); if (ret < 0) { fprintf(stderr, "Error muxing packet\n"); break; } av_free_packet(&pkt); /* =============== AUDIO STREAM ================*/ #if 0 ret = av_read_frame(inAudioFmtCtx, &pkt); if (ret < 0) break; in_stream = inAudioFmtCtx->streams[pkt.stream_index]; pkt.stream_index++; out_stream = outFmtCtx->streams[pkt.stream_index]; log_packet(inAudioFmtCtx, &pkt, "in"); /* copy packet */ pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base); pkt.pos = -1; log_packet(outFmtCtx, &pkt, "out"); ret = av_interleaved_write_frame(outFmtCtx, &pkt); if (ret < 0) { fprintf(stderr, "Error muxing packet\n"); break; } av_free_packet(&pkt); #else if ((ret = av_read_frame(inAudioFmtCtx, &pkt)) < 0) break; int streamIndex = pkt.stream_index; int gotFrame; if (_filterCtx[streamIndex].FilterGraph) { av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n"); frame = av_frame_alloc(); if (!frame) { ret = AVERROR(ENOMEM); break; } av_packet_rescale_ts(&pkt, inAudioFmtCtx->streams[streamIndex]->time_base, inAudioFmtCtx->streams[streamIndex]->codec->time_base); ret = avcodec_decode_audio4(inAudioFmtCtx->streams[streamIndex]->codec, frame, &gotFrame, &pkt); if (ret < 0) { av_frame_free(&frame); av_log(NULL, AV_LOG_ERROR, "Decoding failed\n"); break; } if (gotFrame) { frame->pts = av_frame_get_best_effort_timestamp(frame); ret = filter_encode_write_frame(frame, inAudioFmtCtx, outFmtCtx, streamIndex); av_frame_free(&frame); if (ret < 0) goto end; } else { av_frame_free(&frame); } } else { /* remux this frame without reencoding */ av_packet_rescale_ts(&pkt, inAudioFmtCtx->streams[streamIndex]->time_base, outFmtCtx->streams[streamIndex+1]->time_base); ret = av_interleaved_write_frame(outFmtCtx, &pkt); if (ret < 0) goto end; } av_free_packet(&pkt); #endif // 0 } av_write_trailer(outFmtCtx); end: av_free_packet(&pkt); av_frame_free(&frame); for (i = 0; i < inAudioFmtCtx->nb_streams; i++) { avcodec_close(inAudioFmtCtx->streams[i]->codec); if (outFmtCtx && outFmtCtx->nb_streams > i && outFmtCtx->streams[i] && outFmtCtx->streams[i]->codec) avcodec_close(outFmtCtx->streams[i]->codec); if (_filterCtx && _filterCtx[i].FilterGraph) avfilter_graph_free(&_filterCtx[i].FilterGraph); } av_free(_filterCtx); avformat_close_input(&inVideoFmtCtx); avformat_close_input(&inAudioFmtCtx); /* close output */ if (outFmtCtx && !(ofmt->flags & AVFMT_NOFILE)) avio_closep(&outFmtCtx->pb); avformat_free_context(outFmtCtx); if (ret < 0 && ret != AVERROR_EOF) { //fprintf(stderr, "Error occurred: %s\n", av_err2str(ret)); return 1; } return 0; }
int VideoDecoder::video_thread(void *arg) { VideoState *is = (VideoState *) arg; AVStreamsParser* ps = is->getAVStreamsParser(); AVFrame *frame = av_frame_alloc(); double pts; double duration; int ret; AVRational tb = ps->video_st->time_base; AVRational frame_rate = av_guess_frame_rate(ps->ic, ps->video_st, NULL); #if CONFIG_AVFILTER AVFilterGraph *graph = avfilter_graph_alloc(); AVFilterContext *filt_out = NULL, *filt_in = NULL; int last_w = 0; int last_h = 0; enum AVPixelFormat last_format = (AVPixelFormat) (-2); int last_serial = -1; int last_vfilter_idx = 0; if (!graph) { av_frame_free(&frame); return AVERROR(ENOMEM); } #endif if (!frame) { #if CONFIG_AVFILTER avfilter_graph_free(&graph); #endif return AVERROR(ENOMEM); } for (;;) { ret = is->viddec().get_video_frame(is, frame); if (ret < 0) goto the_end; if (!ret) continue; #if CONFIG_AVFILTER if ( last_w != frame->width || last_h != frame->height || last_format != frame->format || last_serial != is->viddec().pkt_serial || last_vfilter_idx != is->vfilter_idx) { av_log(NULL, AV_LOG_DEBUG, "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n", last_w, last_h, (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial, frame->width, frame->height, (const char *)av_x_if_null(av_get_pix_fmt_name((AVPixelFormat)frame->format), "none"), is->viddec().pkt_serial); avfilter_graph_free(&graph); graph = avfilter_graph_alloc(); if ((ret = configure_video_filters(graph, is,gOptions.vfilters_list ? gOptions.vfilters_list[is->vfilter_idx] : NULL, frame)) < 0) { SDL_Event event; event.type = FF_QUIT_EVENT; event.user.data1 = is; SDL_PushEvent(&event); goto the_end; } filt_in = is->in_video_filter; filt_out = is->out_video_filter; last_w = frame->width; last_h = frame->height; last_format = (AVPixelFormat) frame->format; last_serial = is->viddec().pkt_serial; last_vfilter_idx = is->vfilter_idx; frame_rate = filt_out->inputs[0]->frame_rate; } ret = av_buffersrc_add_frame(filt_in, frame); if (ret < 0) goto the_end; while (ret >= 0) { is->frame_last_returned_time = av_gettime_relative() / 1000000.0; ret = av_buffersink_get_frame_flags(filt_out, frame, 0); if (ret < 0) { if (ret == AVERROR_EOF) is->viddec().finished = is->viddec().pkt_serial; ret = 0; break; } is->frame_last_filter_delay = av_gettime_relative() / 1000000.0 - is->frame_last_returned_time; if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0) is->frame_last_filter_delay = 0; tb = filt_out->inputs[0]->time_base; #endif duration = (frame_rate.num && frame_rate.den ? av_q2d((AVRational){frame_rate.den, frame_rate.num}) : 0); pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb); ret = queue_picture(is, frame, pts, duration, av_frame_get_pkt_pos(frame), is->viddec().pkt_serial); av_frame_unref(frame); #if CONFIG_AVFILTER } #endif if (ret < 0) goto the_end; } the_end: #if CONFIG_AVFILTER avfilter_graph_free(&graph); #endif av_frame_free(&frame); return 0; }
int AudioDecoder::audio_thread(void *arg) { VideoState *is = (VideoState *) arg; AVStreamsParser* ps = is->getAVStreamsParser(); AVFrame *frame = av_frame_alloc(); Frame *af; #if CONFIG_AVFILTER int last_serial = -1; int64_t dec_channel_layout; int reconfigure; #endif int got_frame = 0; AVRational tb; int ret = 0; if (!frame) return AVERROR(ENOMEM); do { if ((got_frame = is->auddec().decode_frame(frame)) < 0) goto the_end; if (got_frame) { tb = (AVRational){1, frame->sample_rate}; #if CONFIG_AVFILTER dec_channel_layout = get_valid_channel_layout(frame->channel_layout, av_frame_get_channels(frame)); reconfigure = cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels, (AVSampleFormat)frame->format, av_frame_get_channels(frame)) || is->audio_filter_src.channel_layout != dec_channel_layout || is->audio_filter_src.freq != frame->sample_rate || is->auddec().pkt_serial != last_serial; if (reconfigure) { char buf1[1024], buf2[1024]; av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout); av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout); av_log(NULL, AV_LOG_DEBUG, "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n", is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, last_serial, frame->sample_rate, av_frame_get_channels(frame), av_get_sample_fmt_name((AVSampleFormat)frame->format), buf2, is->auddec().pkt_serial); is->audio_filter_src.fmt = (AVSampleFormat)frame->format; is->audio_filter_src.channels = av_frame_get_channels(frame); is->audio_filter_src.channel_layout = dec_channel_layout; is->audio_filter_src.freq = frame->sample_rate; last_serial = is->auddec().pkt_serial; if ((ret = configure_audio_filters(is,gOptions. afilters, 1)) < 0) goto the_end; } if ((ret = av_buffersrc_add_frame(is->in_audio_filter, frame)) < 0) goto the_end; while ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, frame, 0)) >= 0) { tb = is->out_audio_filter->inputs[0]->time_base; #endif if (!(af = is->sampq().peek_writable())) goto the_end; af->pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb); af->pos = av_frame_get_pkt_pos(frame); af->serial = is->auddec().pkt_serial; af->duration = av_q2d((AVRational){frame->nb_samples, frame->sample_rate}); av_frame_move_ref(af->frame, frame); is->sampq().push(); #if CONFIG_AVFILTER if (ps->audioq.serial != is->auddec().pkt_serial) break; } if (ret == AVERROR_EOF) is->auddec().finished = is->auddec().pkt_serial; #endif } } while (ret >= 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF); the_end: #if CONFIG_AVFILTER avfilter_graph_free(&is->agraph); #endif av_frame_free(&frame); return ret; }