History

fftools/ffmpeg: move filtering functions to ffmpeg_filter

Anton Khirnov committed 3 years ago

Commit eb9ce9d1 parent 0add05b3 files · +223 −215Browse files

Unified · Split

3 files
  1. fftools/ffmpeg.c +1 −215
  2. fftools/ffmpeg.h +20 −0
  3. fftools/ffmpeg_filter.c +202 −0
fftools/ffmpeg.c+1−215
@@ -651,66 +651,6 @@ void close_output_stream(OutputStream *ost) · 650 unmodified lines
651 sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));651 sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
652}652}
653 653
654/**
655 * Get and encode new output from any of the filtergraphs, without causing
656 * activity.
657 *
658 * @return 0 for success, <0 for severe errors
659 */
660static int reap_filters(int flush)
661{
662 AVFrame *filtered_frame = NULL;
663
664 /* Reap all buffers present in the buffer sinks */
665 for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
666 AVFilterContext *filter;
667 int ret = 0;
668
669 if (!ost->filter || !ost->filter->graph->graph)
670 continue;
671 filter = ost->filter->filter;
672
673 filtered_frame = ost->filtered_frame;
674
675 while (1) {
676 ret = av_buffersink_get_frame_flags(filter, filtered_frame,
677 AV_BUFFERSINK_FLAG_NO_REQUEST);
678 if (ret < 0) {
679 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
680 av_log(NULL, AV_LOG_WARNING,
681 "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
682 } else if (flush && ret == AVERROR_EOF) {
683 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
684 enc_frame(ost, NULL);
685 }
686 break;
687 }
688 if (ost->finished) {
689 av_frame_unref(filtered_frame);
690 continue;
691 }
692
693 if (filtered_frame->pts != AV_NOPTS_VALUE) {
694 AVRational tb = av_buffersink_get_time_base(filter);
695 ost->filter->last_pts = av_rescale_q(filtered_frame->pts, tb,
696 AV_TIME_BASE_Q);
697 filtered_frame->time_base = tb;
698
699 if (debug_ts)
700 av_log(NULL, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
701 av_ts2str(filtered_frame->pts),
702 av_ts2timestr(filtered_frame->pts, &tb),
703 tb.num, tb.den);
704 }
705
706 enc_frame(ost, filtered_frame);
707 av_frame_unref(filtered_frame);
708 }
709 }
710
711 return 0;
712}
713
714static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)654static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
715{655{
716 AVBPrint buf, buf_script;656 AVBPrint buf, buf_script;
@@ -914,112 +854,6 @@ int ifilter_has_all_input_formats(FilterGraph *fg) · 197 unmodified lines
914 return 1;854 return 1;
915}855}
916 856
917static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference)
918{
919 FilterGraph *fg = ifilter->graph;
920 AVFrameSideData *sd;
921 int need_reinit, ret;
922 int buffersrc_flags = AV_BUFFERSRC_FLAG_PUSH;
923
924 if (keep_reference)
925 buffersrc_flags |= AV_BUFFERSRC_FLAG_KEEP_REF;
926
927 /* determine if the parameters for this input changed */
928 need_reinit = ifilter->format != frame->format;
929
930 switch (ifilter->ist->par->codec_type) {
931 case AVMEDIA_TYPE_AUDIO:
932 need_reinit |= ifilter->sample_rate != frame->sample_rate ||
933 av_channel_layout_compare(&ifilter->ch_layout, &frame->ch_layout);
934 break;
935 case AVMEDIA_TYPE_VIDEO:
936 need_reinit |= ifilter->width != frame->width ||
937 ifilter->height != frame->height;
938 break;
939 }
940
941 if (!ifilter->ist->reinit_filters && fg->graph)
942 need_reinit = 0;
943
944 if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
945 (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
946 need_reinit = 1;
947
948 if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) {
949 if (!ifilter->displaymatrix || memcmp(sd->data, ifilter->displaymatrix, sizeof(int32_t) * 9))
950 need_reinit = 1;
951 } else if (ifilter->displaymatrix)
952 need_reinit = 1;
953
954 if (need_reinit) {
955 ret = ifilter_parameters_from_frame(ifilter, frame);
956 if (ret < 0)
957 return ret;
958 }
959
960 /* (re)init the graph if possible, otherwise buffer the frame and return */
961 if (need_reinit || !fg->graph) {
962 if (!ifilter_has_all_input_formats(fg)) {
963 AVFrame *tmp = av_frame_clone(frame);
964 if (!tmp)
965 return AVERROR(ENOMEM);
966
967 ret = av_fifo_write(ifilter->frame_queue, &tmp, 1);
968 if (ret < 0)
969 av_frame_free(&tmp);
970
971 return ret;
972 }
973
974 ret = reap_filters(1);
975 if (ret < 0 && ret != AVERROR_EOF) {
976 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
977 return ret;
978 }
979
980 ret = configure_filtergraph(fg);
981 if (ret < 0) {
982 av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
983 return ret;
984 }
985 }
986
987 ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, buffersrc_flags);
988 if (ret < 0) {
989 if (ret != AVERROR_EOF)
990 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
991 return ret;
992 }
993
994 return 0;
995}
996
997static int ifilter_send_eof(InputFilter *ifilter, int64_t pts)
998{
999 int ret;
1000
1001 ifilter->eof = 1;
1002
1003 if (ifilter->filter) {
1004 ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
1005 if (ret < 0)
1006 return ret;
1007 } else {
1008 // the filtergraph was never configured
1009 if (ifilter->format < 0) {
1010 ret = ifilter_parameters_from_codecpar(ifilter, ifilter->ist->par);
1011 if (ret < 0)
1012 return ret;
1013 }
1014 if (ifilter->format < 0 && (ifilter->type == AVMEDIA_TYPE_AUDIO || ifilter->type == AVMEDIA_TYPE_VIDEO)) {
1015 av_log(NULL, AV_LOG_ERROR, "Cannot determine format of input stream %d:%d after EOF\n", ifilter->ist->file_index, ifilter->ist->st->index);
1016 return AVERROR_INVALIDDATA;
1017 }
1018 }
1019
1020 return 0;
1021}
1022
1023// This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.857// This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
1024// There is the following difference: if you got a frame, you must call858// There is the following difference: if you got a frame, you must call
1025// it again with pkt=NULL. pkt==NULL is treated differently from pkt->size==0859// it again with pkt=NULL. pkt==NULL is treated differently from pkt->size==0
@@ -2262,54 +2096,6 @@ discard_packet: · 1236 unmodified lines
2262 return 0;2096 return 0;
2263}2097}
2264 2098
2265/**
2266 * Perform a step of transcoding for the specified filter graph.
2267 *
2268 * @param[in] graph filter graph to consider
2269 * @param[out] best_ist input stream where a frame would allow to continue
2270 * @return 0 for success, <0 for error
2271 */
2272static int transcode_from_filter(FilterGraph *graph, InputStream **best_ist)
2273{
2274 int i, ret;
2275 int nb_requests, nb_requests_max = 0;
2276 InputFilter *ifilter;
2277 InputStream *ist;
2278
2279 *best_ist = NULL;
2280 ret = avfilter_graph_request_oldest(graph->graph);
2281 if (ret >= 0)
2282 return reap_filters(0);
2283
2284 if (ret == AVERROR_EOF) {
2285 ret = reap_filters(1);
2286 for (i = 0; i < graph->nb_outputs; i++)
2287 close_output_stream(graph->outputs[i]->ost);
2288 return ret;
2289 }
2290 if (ret != AVERROR(EAGAIN))
2291 return ret;
2292
2293 for (i = 0; i < graph->nb_inputs; i++) {
2294 ifilter = graph->inputs[i];
2295 ist = ifilter->ist;
2296 if (input_files[ist->file_index]->eagain ||
2297 input_files[ist->file_index]->eof_reached)
2298 continue;
2299 nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
2300 if (nb_requests > nb_requests_max) {
2301 nb_requests_max = nb_requests;
2302 *best_ist = ist;
2303 }
2304 }
2305
2306 if (!*best_ist)
2307 for (i = 0; i < graph->nb_outputs; i++)
2308 graph->outputs[i]->ost->unavailable = 1;
2309
2310 return 0;
2311}
2312
2313/**2099/**
2314 * Run a single step of transcoding.2100 * Run a single step of transcoding.
2315 *2101 *
@@ -2343,7 +2129,7 @@ static int transcode_step(void) · 27 unmodified lines
2343 }2129 }
2344 2130
2345 if (ost->filter && ost->filter->graph->graph) {2131 if (ost->filter && ost->filter->graph->graph) {
2346 if ((ret = transcode_from_filter(ost->filter->graph, &ist)) < 0)2132 if ((ret = fg_transcode_step(ost->filter->graph, &ist)) < 0)
2347 return ret;2133 return ret;
2348 if (!ist)2134 if (!ist)
2349 return 0;2135 return 0;
fftools/ffmpeg.h+20−0
@@ -799,6 +799,9 @@ int init_complex_filtergraph(FilterGraph *fg); · 798 unmodified lines
799 799
800void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);800void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
801 801
802int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference);
803int ifilter_send_eof(InputFilter *ifilter, int64_t pts);
804
802int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);805int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
803int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par);806int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par);
804int ifilter_has_all_input_formats(FilterGraph *fg);807int ifilter_has_all_input_formats(FilterGraph *fg);
@@ -811,6 +814,23 @@ int ifilter_has_all_input_formats(FilterGraph *fg); · 6 unmodified lines
811 */814 */
812FilterGraph *fg_create(char *graph_desc);815FilterGraph *fg_create(char *graph_desc);
813 816
817/**
818 * Perform a step of transcoding for the specified filter graph.
819 *
820 * @param[in] graph filter graph to consider
821 * @param[out] best_ist input stream where a frame would allow to continue
822 * @return 0 for success, <0 for error
823 */
824int fg_transcode_step(FilterGraph *graph, InputStream **best_ist);
825
826/**
827 * Get and encode new output from any of the filtergraphs, without causing
828 * activity.
829 *
830 * @return 0 for success, <0 for severe errors
831 */
832int reap_filters(int flush);
833
814int ffmpeg_parse_options(int argc, char **argv);834int ffmpeg_parse_options(int argc, char **argv);
815 835
816void enc_stats_write(OutputStream *ost, EncStats *es,836void enc_stats_write(OutputStream *ost, EncStats *es,
fftools/ffmpeg_filter.c+202−0
@@ -36,6 +36,7 @@ · 35 unmodified lines
36#include "libavutil/pixfmt.h"36#include "libavutil/pixfmt.h"
37#include "libavutil/imgutils.h"37#include "libavutil/imgutils.h"
38#include "libavutil/samplefmt.h"38#include "libavutil/samplefmt.h"
39#include "libavutil/timestamp.h"
39 40
40// FIXME: YUV420P etc. are actually supported with full color range,41// FIXME: YUV420P etc. are actually supported with full color range,
41// yet the latter information isn't available here.42// yet the latter information isn't available here.
@@ -1300,3 +1301,204 @@ int filtergraph_is_simple(FilterGraph *fg) · 1258 unmodified lines
1300{1301{
1301 return !fg->graph_desc;1302 return !fg->graph_desc;
1302}1303}
1304
1305int reap_filters(int flush)
1306{
1307 AVFrame *filtered_frame = NULL;
1308
1309 /* Reap all buffers present in the buffer sinks */
1310 for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
1311 AVFilterContext *filter;
1312 int ret = 0;
1313
1314 if (!ost->filter || !ost->filter->graph->graph)
1315 continue;
1316 filter = ost->filter->filter;
1317
1318 filtered_frame = ost->filtered_frame;
1319
1320 while (1) {
1321 ret = av_buffersink_get_frame_flags(filter, filtered_frame,
1322 AV_BUFFERSINK_FLAG_NO_REQUEST);
1323 if (ret < 0) {
1324 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
1325 av_log(NULL, AV_LOG_WARNING,
1326 "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
1327 } else if (flush && ret == AVERROR_EOF) {
1328 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
1329 enc_frame(ost, NULL);
1330 }
1331 break;
1332 }
1333 if (ost->finished) {
1334 av_frame_unref(filtered_frame);
1335 continue;
1336 }
1337
1338 if (filtered_frame->pts != AV_NOPTS_VALUE) {
1339 AVRational tb = av_buffersink_get_time_base(filter);
1340 ost->filter->last_pts = av_rescale_q(filtered_frame->pts, tb,
1341 AV_TIME_BASE_Q);
1342 filtered_frame->time_base = tb;
1343
1344 if (debug_ts)
1345 av_log(NULL, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
1346 av_ts2str(filtered_frame->pts),
1347 av_ts2timestr(filtered_frame->pts, &tb),
1348 tb.num, tb.den);
1349 }
1350
1351 enc_frame(ost, filtered_frame);
1352 av_frame_unref(filtered_frame);
1353 }
1354 }
1355
1356 return 0;
1357}
1358
1359int ifilter_send_eof(InputFilter *ifilter, int64_t pts)
1360{
1361 int ret;
1362
1363 ifilter->eof = 1;
1364
1365 if (ifilter->filter) {
1366 ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
1367 if (ret < 0)
1368 return ret;
1369 } else {
1370 // the filtergraph was never configured
1371 if (ifilter->format < 0) {
1372 ret = ifilter_parameters_from_codecpar(ifilter, ifilter->ist->par);
1373 if (ret < 0)
1374 return ret;
1375 }
1376 if (ifilter->format < 0 && (ifilter->type == AVMEDIA_TYPE_AUDIO || ifilter->type == AVMEDIA_TYPE_VIDEO)) {
1377 av_log(NULL, AV_LOG_ERROR, "Cannot determine format of input stream %d:%d after EOF\n", ifilter->ist->file_index, ifilter->ist->st->index);
1378 return AVERROR_INVALIDDATA;
1379 }
1380 }
1381
1382 return 0;
1383}
1384
1385int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference)
1386{
1387 FilterGraph *fg = ifilter->graph;
1388 AVFrameSideData *sd;
1389 int need_reinit, ret;
1390 int buffersrc_flags = AV_BUFFERSRC_FLAG_PUSH;
1391
1392 if (keep_reference)
1393 buffersrc_flags |= AV_BUFFERSRC_FLAG_KEEP_REF;
1394
1395 /* determine if the parameters for this input changed */
1396 need_reinit = ifilter->format != frame->format;
1397
1398 switch (ifilter->ist->par->codec_type) {
1399 case AVMEDIA_TYPE_AUDIO:
1400 need_reinit |= ifilter->sample_rate != frame->sample_rate ||
1401 av_channel_layout_compare(&ifilter->ch_layout, &frame->ch_layout);
1402 break;
1403 case AVMEDIA_TYPE_VIDEO:
1404 need_reinit |= ifilter->width != frame->width ||
1405 ifilter->height != frame->height;
1406 break;
1407 }
1408
1409 if (!ifilter->ist->reinit_filters && fg->graph)
1410 need_reinit = 0;
1411
1412 if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
1413 (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
1414 need_reinit = 1;
1415
1416 if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) {
1417 if (!ifilter->displaymatrix || memcmp(sd->data, ifilter->displaymatrix, sizeof(int32_t) * 9))
1418 need_reinit = 1;
1419 } else if (ifilter->displaymatrix)
1420 need_reinit = 1;
1421
1422 if (need_reinit) {
1423 ret = ifilter_parameters_from_frame(ifilter, frame);
1424 if (ret < 0)
1425 return ret;
1426 }
1427
1428 /* (re)init the graph if possible, otherwise buffer the frame and return */
1429 if (need_reinit || !fg->graph) {
1430 if (!ifilter_has_all_input_formats(fg)) {
1431 AVFrame *tmp = av_frame_clone(frame);
1432 if (!tmp)
1433 return AVERROR(ENOMEM);
1434
1435 ret = av_fifo_write(ifilter->frame_queue, &tmp, 1);
1436 if (ret < 0)
1437 av_frame_free(&tmp);
1438
1439 return ret;
1440 }
1441
1442 ret = reap_filters(1);
1443 if (ret < 0 && ret != AVERROR_EOF) {
1444 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
1445 return ret;
1446 }
1447
1448 ret = configure_filtergraph(fg);
1449 if (ret < 0) {
1450 av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
1451 return ret;
1452 }
1453 }
1454
1455 ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, buffersrc_flags);
1456 if (ret < 0) {
1457 if (ret != AVERROR_EOF)
1458 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
1459 return ret;
1460 }
1461
1462 return 0;
1463}
1464
1465int fg_transcode_step(FilterGraph *graph, InputStream **best_ist)
1466{
1467 int i, ret;
1468 int nb_requests, nb_requests_max = 0;
1469 InputFilter *ifilter;
1470 InputStream *ist;
1471
1472 *best_ist = NULL;
1473 ret = avfilter_graph_request_oldest(graph->graph);
1474 if (ret >= 0)
1475 return reap_filters(0);
1476
1477 if (ret == AVERROR_EOF) {
1478 ret = reap_filters(1);
1479 for (i = 0; i < graph->nb_outputs; i++)
1480 close_output_stream(graph->outputs[i]->ost);
1481 return ret;
1482 }
1483 if (ret != AVERROR(EAGAIN))
1484 return ret;
1485
1486 for (i = 0; i < graph->nb_inputs; i++) {
1487 ifilter = graph->inputs[i];
1488 ist = ifilter->ist;
1489 if (input_files[ist->file_index]->eagain ||
1490 input_files[ist->file_index]->eof_reached)
1491 continue;
1492 nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
1493 if (nb_requests > nb_requests_max) {
1494 nb_requests_max = nb_requests;
1495 *best_ist = ist;
1496 }
1497 }
1498
1499 if (!*best_ist)
1500 for (i = 0; i < graph->nb_outputs; i++)
1501 graph->outputs[i]->ost->unavailable = 1;
1502
1503 return 0;
1504}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t