fftools/ffmpeg_filter.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * ffmpeg filter configuration | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <stdint.h> | ||
| 22 | |||
| 23 | #include "ffmpeg.h" | ||
| 24 | #include "graph/graphprint.h" | ||
| 25 | |||
| 26 | #include "libavfilter/avfilter.h" | ||
| 27 | #include "libavfilter/buffersink.h" | ||
| 28 | #include "libavfilter/buffersrc.h" | ||
| 29 | |||
| 30 | #include "libavutil/attributes.h" | ||
| 31 | #include "libavutil/avassert.h" | ||
| 32 | #include "libavutil/avstring.h" | ||
| 33 | #include "libavutil/bprint.h" | ||
| 34 | #include "libavutil/channel_layout.h" | ||
| 35 | #include "libavutil/downmix_info.h" | ||
| 36 | #include "libavutil/mem.h" | ||
| 37 | #include "libavutil/opt.h" | ||
| 38 | #include "libavutil/pixdesc.h" | ||
| 39 | #include "libavutil/pixfmt.h" | ||
| 40 | #include "libavutil/samplefmt.h" | ||
| 41 | #include "libavutil/time.h" | ||
| 42 | #include "libavutil/timestamp.h" | ||
| 43 | |||
| 44 | typedef struct FilterGraphPriv { | ||
| 45 | FilterGraph fg; | ||
| 46 | |||
| 47 | // name used for logging | ||
| 48 | char log_name[32]; | ||
| 49 | |||
| 50 | int is_simple; | ||
| 51 | // true when the filtergraph contains only meta filters | ||
| 52 | // that do not modify the frame data | ||
| 53 | int is_meta; | ||
| 54 | int disable_conversions; | ||
| 55 | |||
| 56 | unsigned nb_outputs_done; | ||
| 57 | |||
| 58 | int nb_threads; | ||
| 59 | |||
| 60 | // frame for temporarily holding output from the filtergraph | ||
| 61 | AVFrame *frame; | ||
| 62 | // frame for sending output to the encoder | ||
| 63 | AVFrame *frame_enc; | ||
| 64 | |||
| 65 | Scheduler *sch; | ||
| 66 | unsigned sch_idx; | ||
| 67 | } FilterGraphPriv; | ||
| 68 | |||
| 69 | 4279181 | static FilterGraphPriv *fgp_from_fg(FilterGraph *fg) | |
| 70 | { | ||
| 71 | 4279181 | return (FilterGraphPriv*)fg; | |
| 72 | } | ||
| 73 | |||
| 74 | 41120 | static const FilterGraphPriv *cfgp_from_cfg(const FilterGraph *fg) | |
| 75 | { | ||
| 76 | 41120 | return (const FilterGraphPriv*)fg; | |
| 77 | } | ||
| 78 | |||
| 79 | // data that is local to the filter thread and not visible outside of it | ||
| 80 | typedef struct FilterGraphThread { | ||
| 81 | AVFilterGraph *graph; | ||
| 82 | |||
| 83 | AVFrame *frame; | ||
| 84 | |||
| 85 | // Temporary buffer for output frames, since on filtergraph reset | ||
| 86 | // we cannot send them to encoders immediately. | ||
| 87 | // The output index is stored in frame opaque. | ||
| 88 | AVFifo *frame_queue_out; | ||
| 89 | |||
| 90 | // index of the next input to request from the scheduler | ||
| 91 | unsigned next_in; | ||
| 92 | // set to 1 after at least one frame passed through this output | ||
| 93 | int got_frame; | ||
| 94 | |||
| 95 | // EOF status of each input/output, as received by the thread | ||
| 96 | uint8_t *eof_in; | ||
| 97 | uint8_t *eof_out; | ||
| 98 | } FilterGraphThread; | ||
| 99 | |||
| 100 | typedef struct InputFilterPriv { | ||
| 101 | InputFilter ifilter; | ||
| 102 | |||
| 103 | InputFilterOptions opts; | ||
| 104 | |||
| 105 | // used to hold submitted input | ||
| 106 | AVFrame *frame; | ||
| 107 | |||
| 108 | // For inputs bound to a filtergraph output | ||
| 109 | OutputFilter *ofilter_src; | ||
| 110 | |||
| 111 | // source data type: AVMEDIA_TYPE_SUBTITLE for sub2video, | ||
| 112 | // same as type otherwise | ||
| 113 | enum AVMediaType type_src; | ||
| 114 | |||
| 115 | int eof; | ||
| 116 | int bound; | ||
| 117 | int drop_warned; | ||
| 118 | uint64_t nb_dropped; | ||
| 119 | |||
| 120 | // parameters configured for this input | ||
| 121 | int format; | ||
| 122 | |||
| 123 | int width, height; | ||
| 124 | AVRational sample_aspect_ratio; | ||
| 125 | enum AVColorSpace color_space; | ||
| 126 | enum AVColorRange color_range; | ||
| 127 | enum AVAlphaMode alpha_mode; | ||
| 128 | enum AVChromaLocation chroma_location; | ||
| 129 | |||
| 130 | int sample_rate; | ||
| 131 | AVChannelLayout ch_layout; | ||
| 132 | |||
| 133 | AVRational time_base; | ||
| 134 | |||
| 135 | AVFrameSideData **side_data; | ||
| 136 | int nb_side_data; | ||
| 137 | |||
| 138 | AVFifo *frame_queue; | ||
| 139 | |||
| 140 | AVBufferRef *hw_frames_ctx; | ||
| 141 | |||
| 142 | int displaymatrix_present; | ||
| 143 | int displaymatrix_applied; | ||
| 144 | int32_t displaymatrix[9]; | ||
| 145 | |||
| 146 | int downmixinfo_present; | ||
| 147 | AVDownmixInfo downmixinfo; | ||
| 148 | |||
| 149 | AVBufferRef *downmixmatrix; | ||
| 150 | size_t downmixmatrix_size; | ||
| 151 | int downmixmatrix_present; | ||
| 152 | |||
| 153 | struct { | ||
| 154 | AVFrame *frame; | ||
| 155 | |||
| 156 | int64_t last_pts; | ||
| 157 | int64_t end_pts; | ||
| 158 | |||
| 159 | /// marks if sub2video_update should force an initialization | ||
| 160 | unsigned int initialize; | ||
| 161 | } sub2video; | ||
| 162 | } InputFilterPriv; | ||
| 163 | |||
| 164 | 3352442 | static InputFilterPriv *ifp_from_ifilter(InputFilter *ifilter) | |
| 165 | { | ||
| 166 | 3352442 | return (InputFilterPriv*)ifilter; | |
| 167 | } | ||
| 168 | |||
| 169 | typedef struct FPSConvContext { | ||
| 170 | AVFrame *last_frame; | ||
| 171 | /* number of frames emitted by the video-encoding sync code */ | ||
| 172 | int64_t frame_number; | ||
| 173 | /* history of nb_frames_prev, i.e. the number of times the | ||
| 174 | * previous frame was duplicated by vsync code in recent | ||
| 175 | * do_video_out() calls */ | ||
| 176 | int64_t frames_prev_hist[3]; | ||
| 177 | |||
| 178 | uint64_t dup_warning; | ||
| 179 | |||
| 180 | int last_dropped; | ||
| 181 | int dropped_keyframe; | ||
| 182 | |||
| 183 | enum VideoSyncMethod vsync_method; | ||
| 184 | |||
| 185 | AVRational framerate; | ||
| 186 | AVRational framerate_max; | ||
| 187 | const AVRational *framerate_supported; | ||
| 188 | int framerate_clip; | ||
| 189 | } FPSConvContext; | ||
| 190 | |||
| 191 | typedef struct ReinitOpts { | ||
| 192 | int64_t pts; | ||
| 193 | AVDictionary *dict; | ||
| 194 | } ReinitOpts; | ||
| 195 | |||
| 196 | typedef struct OutputFilterPriv { | ||
| 197 | OutputFilter ofilter; | ||
| 198 | |||
| 199 | void *log_parent; | ||
| 200 | char log_name[32]; | ||
| 201 | |||
| 202 | #define NEEDED_EXPLICIT 1 | ||
| 203 | #define NEEDED_INTERNAL 2 | ||
| 204 | int needed; | ||
| 205 | |||
| 206 | /* desired output stream properties */ | ||
| 207 | int format; | ||
| 208 | int width, height; | ||
| 209 | int sample_rate; | ||
| 210 | AVChannelLayout ch_layout; | ||
| 211 | enum AVColorSpace color_space; | ||
| 212 | enum AVColorRange color_range; | ||
| 213 | enum AVAlphaMode alpha_mode; | ||
| 214 | enum AVChromaLocation chroma_location; | ||
| 215 | |||
| 216 | unsigned crop_top; | ||
| 217 | unsigned crop_bottom; | ||
| 218 | unsigned crop_left; | ||
| 219 | unsigned crop_right; | ||
| 220 | |||
| 221 | AVFrameSideData **side_data; | ||
| 222 | int nb_side_data; | ||
| 223 | |||
| 224 | // time base in which the output is sent to our downstream | ||
| 225 | // does not need to match the filtersink's timebase | ||
| 226 | AVRational tb_out; | ||
| 227 | // at least one frame with the above timebase was sent | ||
| 228 | // to our downstream, so it cannot change anymore | ||
| 229 | int tb_out_locked; | ||
| 230 | |||
| 231 | AVRational sample_aspect_ratio; | ||
| 232 | |||
| 233 | AVDictionary *sws_opts; | ||
| 234 | AVDictionary *swr_opts; | ||
| 235 | |||
| 236 | // those are only set if no format is specified and the encoder gives us multiple options | ||
| 237 | // They point directly to the relevant lists of the encoder. | ||
| 238 | union { | ||
| 239 | const enum AVPixelFormat *pix_fmts; | ||
| 240 | const enum AVSampleFormat *sample_fmts; | ||
| 241 | }; | ||
| 242 | const AVChannelLayout *ch_layouts; | ||
| 243 | const int *sample_rates; | ||
| 244 | const enum AVColorSpace *color_spaces; | ||
| 245 | const enum AVColorRange *color_ranges; | ||
| 246 | const enum AVAlphaMode *alpha_modes; | ||
| 247 | const enum AVChromaLocation *chroma_locations; | ||
| 248 | |||
| 249 | int32_t displaymatrix[9]; | ||
| 250 | |||
| 251 | AVRational enc_timebase; | ||
| 252 | int64_t trim_start_us; | ||
| 253 | int64_t trim_duration_us; | ||
| 254 | // offset for output timestamps, in AV_TIME_BASE_Q | ||
| 255 | int64_t ts_offset; | ||
| 256 | int64_t next_pts; | ||
| 257 | FPSConvContext fps; | ||
| 258 | |||
| 259 | AVFifo *reinit_opts_fifo; | ||
| 260 | ReinitOpts reinit_opts; | ||
| 261 | |||
| 262 | unsigned flags; | ||
| 263 | } OutputFilterPriv; | ||
| 264 | |||
| 265 | 1767424 | static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter) | |
| 266 | { | ||
| 267 | 1767424 | return (OutputFilterPriv*)ofilter; | |
| 268 | } | ||
| 269 | |||
| 270 | typedef struct FilterCommand { | ||
| 271 | char *target; | ||
| 272 | char *command; | ||
| 273 | char *arg; | ||
| 274 | |||
| 275 | double time; | ||
| 276 | int all_filters; | ||
| 277 | } FilterCommand; | ||
| 278 | |||
| 279 | ✗ | static void filter_command_free(void *opaque, uint8_t *data) | |
| 280 | { | ||
| 281 | ✗ | FilterCommand *fc = (FilterCommand*)data; | |
| 282 | |||
| 283 | ✗ | av_freep(&fc->target); | |
| 284 | ✗ | av_freep(&fc->command); | |
| 285 | ✗ | av_freep(&fc->arg); | |
| 286 | |||
| 287 | ✗ | av_free(data); | |
| 288 | ✗ | } | |
| 289 | |||
| 290 | 180 | static int sub2video_get_blank_frame(InputFilterPriv *ifp) | |
| 291 | { | ||
| 292 | 180 | AVFrame *frame = ifp->sub2video.frame; | |
| 293 | int ret; | ||
| 294 | |||
| 295 | 180 | av_frame_unref(frame); | |
| 296 | |||
| 297 | 180 | frame->width = ifp->width; | |
| 298 | 180 | frame->height = ifp->height; | |
| 299 | 180 | frame->format = ifp->format; | |
| 300 | 180 | frame->colorspace = ifp->color_space; | |
| 301 | 180 | frame->color_range = ifp->color_range; | |
| 302 | 180 | frame->alpha_mode = ifp->alpha_mode; | |
| 303 | 180 | frame->chroma_location = ifp->chroma_location; | |
| 304 | |||
| 305 | 180 | ret = av_frame_get_buffer(frame, 0); | |
| 306 | 180 | if (ret < 0) | |
| 307 | ✗ | return ret; | |
| 308 | |||
| 309 | 180 | memset(frame->data[0], 0, frame->height * frame->linesize[0]); | |
| 310 | |||
| 311 | 180 | return 0; | |
| 312 | } | ||
| 313 | |||
| 314 | 89 | static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h, | |
| 315 | AVSubtitleRect *r) | ||
| 316 | { | ||
| 317 | uint32_t *pal, *dst2; | ||
| 318 | uint8_t *src, *src2; | ||
| 319 | int x, y; | ||
| 320 | |||
| 321 | 89 | if (r->type != SUBTITLE_BITMAP) { | |
| 322 | ✗ | av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n"); | |
| 323 | ✗ | return; | |
| 324 | } | ||
| 325 | 89 | if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) { | |
| 326 | ✗ | av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n", | |
| 327 | r->x, r->y, r->w, r->h, w, h | ||
| 328 | ); | ||
| 329 | ✗ | return; | |
| 330 | } | ||
| 331 | |||
| 332 | 89 | dst += r->y * dst_linesize + r->x * 4; | |
| 333 | 89 | src = r->data[0]; | |
| 334 | 89 | pal = (uint32_t *)r->data[1]; | |
| 335 | 3380 | for (y = 0; y < r->h; y++) { | |
| 336 | 3291 | dst2 = (uint32_t *)dst; | |
| 337 | 3291 | src2 = src; | |
| 338 | 1039366 | for (x = 0; x < r->w; x++) | |
| 339 | 1036075 | *(dst2++) = pal[*(src2++)]; | |
| 340 | 3291 | dst += dst_linesize; | |
| 341 | 3291 | src += r->linesize[0]; | |
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | 271 | static void sub2video_push_ref(InputFilterPriv *ifp, int64_t pts) | |
| 346 | { | ||
| 347 | 271 | AVFrame *frame = ifp->sub2video.frame; | |
| 348 | int ret; | ||
| 349 | |||
| 350 | av_assert1(frame->data[0]); | ||
| 351 | 271 | ifp->sub2video.last_pts = frame->pts = pts; | |
| 352 | 271 | ret = av_buffersrc_add_frame_flags(ifp->ifilter.filter, frame, | |
| 353 | AV_BUFFERSRC_FLAG_KEEP_REF | | ||
| 354 | AV_BUFFERSRC_FLAG_PUSH); | ||
| 355 | 271 | if (ret != AVERROR_EOF && ret < 0) | |
| 356 | ✗ | av_log(ifp->ifilter.graph, AV_LOG_WARNING, | |
| 357 | "Error while add the frame to buffer source(%s).\n", | ||
| 358 | ✗ | av_err2str(ret)); | |
| 359 | 271 | } | |
| 360 | |||
| 361 | 180 | static void sub2video_update(InputFilterPriv *ifp, int64_t heartbeat_pts, | |
| 362 | const AVSubtitle *sub) | ||
| 363 | { | ||
| 364 | 180 | AVFrame *frame = ifp->sub2video.frame; | |
| 365 | uint8_t *dst; | ||
| 366 | int dst_linesize; | ||
| 367 | int num_rects; | ||
| 368 | int64_t pts, end_pts; | ||
| 369 | |||
| 370 | 180 | if (sub) { | |
| 371 | 88 | pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL, | |
| 372 | 88 | AV_TIME_BASE_Q, ifp->time_base); | |
| 373 | 88 | end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL, | |
| 374 | 88 | AV_TIME_BASE_Q, ifp->time_base); | |
| 375 | 88 | num_rects = sub->num_rects; | |
| 376 | } else { | ||
| 377 | /* If we are initializing the system, utilize current heartbeat | ||
| 378 | PTS as the start time, and show until the following subpicture | ||
| 379 | is received. Otherwise, utilize the previous subpicture's end time | ||
| 380 | as the fall-back value. */ | ||
| 381 | 92 | pts = ifp->sub2video.initialize ? | |
| 382 | 92 | heartbeat_pts : ifp->sub2video.end_pts; | |
| 383 | 92 | end_pts = INT64_MAX; | |
| 384 | 92 | num_rects = 0; | |
| 385 | } | ||
| 386 | 180 | if (sub2video_get_blank_frame(ifp) < 0) { | |
| 387 | ✗ | av_log(ifp->ifilter.graph, AV_LOG_ERROR, | |
| 388 | "Impossible to get a blank canvas.\n"); | ||
| 389 | ✗ | return; | |
| 390 | } | ||
| 391 | 180 | dst = frame->data [0]; | |
| 392 | 180 | dst_linesize = frame->linesize[0]; | |
| 393 | 269 | for (int i = 0; i < num_rects; i++) | |
| 394 | 89 | sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]); | |
| 395 | 180 | sub2video_push_ref(ifp, pts); | |
| 396 | 180 | ifp->sub2video.end_pts = end_pts; | |
| 397 | 180 | ifp->sub2video.initialize = 0; | |
| 398 | } | ||
| 399 | |||
| 400 | /* Define a function for appending a list of allowed formats | ||
| 401 | * to an AVBPrint. If nonempty, the list will have a header. */ | ||
| 402 | #define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \ | ||
| 403 | static void choose_ ## name (OutputFilterPriv *ofp, AVBPrint *bprint) \ | ||
| 404 | { \ | ||
| 405 | if (ofp->var == none && !ofp->supported_list) \ | ||
| 406 | return; \ | ||
| 407 | av_bprintf(bprint, #name "="); \ | ||
| 408 | if (ofp->var != none) { \ | ||
| 409 | av_bprintf(bprint, printf_format, get_name(ofp->var)); \ | ||
| 410 | } else { \ | ||
| 411 | const type *p; \ | ||
| 412 | \ | ||
| 413 | for (p = ofp->supported_list; *p != none; p++) { \ | ||
| 414 | av_bprintf(bprint, printf_format "|", get_name(*p)); \ | ||
| 415 | } \ | ||
| 416 | if (bprint->len > 0) \ | ||
| 417 | bprint->str[--bprint->len] = '\0'; \ | ||
| 418 | } \ | ||
| 419 | av_bprint_chars(bprint, ':', 1); \ | ||
| 420 | } | ||
| 421 | |||
| 422 | 10033 | DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, pix_fmts, | |
| 423 | AV_PIX_FMT_NONE, "%s", av_get_pix_fmt_name) | ||
| 424 | |||
| 425 | 3525 | DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, sample_fmts, | |
| 426 | AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name) | ||
| 427 | |||
| 428 | 3052 | DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0, | |
| 429 | "%d", ) | ||
| 430 | |||
| 431 | 7414 | DEF_CHOOSE_FORMAT(color_spaces, enum AVColorSpace, color_space, color_spaces, | |
| 432 | AVCOL_SPC_UNSPECIFIED, "%s", av_color_space_name) | ||
| 433 | |||
| 434 | 8003 | DEF_CHOOSE_FORMAT(color_ranges, enum AVColorRange, color_range, color_ranges, | |
| 435 | AVCOL_RANGE_UNSPECIFIED, "%s", av_color_range_name) | ||
| 436 | |||
| 437 | 7444 | DEF_CHOOSE_FORMAT(alpha_modes, enum AVAlphaMode, alpha_mode, alpha_modes, | |
| 438 | AVALPHA_MODE_UNSPECIFIED, "%s", av_alpha_mode_name) | ||
| 439 | |||
| 440 | 7595 | DEF_CHOOSE_FORMAT(chroma_locations, enum AVChromaLocation, chroma_location, | |
| 441 | chroma_locations, AVCHROMA_LOC_UNSPECIFIED, "%s", | ||
| 442 | av_chroma_location_name) | ||
| 443 | |||
| 444 | 1702 | static void choose_channel_layouts(OutputFilterPriv *ofp, AVBPrint *bprint) | |
| 445 | { | ||
| 446 | 1702 | if (av_channel_layout_check(&ofp->ch_layout)) { | |
| 447 | 15 | av_bprintf(bprint, "channel_layouts="); | |
| 448 | 15 | av_channel_layout_describe_bprint(&ofp->ch_layout, bprint); | |
| 449 | 1687 | } else if (ofp->ch_layouts) { | |
| 450 | const AVChannelLayout *p; | ||
| 451 | |||
| 452 | 228 | av_bprintf(bprint, "channel_layouts="); | |
| 453 | 1038 | for (p = ofp->ch_layouts; p->nb_channels; p++) { | |
| 454 | 810 | av_channel_layout_describe_bprint(p, bprint); | |
| 455 | 810 | av_bprintf(bprint, "|"); | |
| 456 | } | ||
| 457 | 228 | if (bprint->len > 0) | |
| 458 | 228 | bprint->str[--bprint->len] = '\0'; | |
| 459 | } else | ||
| 460 | 1459 | return; | |
| 461 | 243 | av_bprint_chars(bprint, ':', 1); | |
| 462 | } | ||
| 463 | |||
| 464 | ✗ | static int read_binary(void *logctx, const char *path, | |
| 465 | uint8_t **data, int *len) | ||
| 466 | { | ||
| 467 | ✗ | AVIOContext *io = NULL; | |
| 468 | int64_t fsize; | ||
| 469 | int ret; | ||
| 470 | |||
| 471 | ✗ | *data = NULL; | |
| 472 | ✗ | *len = 0; | |
| 473 | |||
| 474 | ✗ | ret = avio_open2(&io, path, AVIO_FLAG_READ, &int_cb, NULL); | |
| 475 | ✗ | if (ret < 0) { | |
| 476 | ✗ | av_log(logctx, AV_LOG_ERROR, "Cannot open file '%s': %s\n", | |
| 477 | ✗ | path, av_err2str(ret)); | |
| 478 | ✗ | return ret; | |
| 479 | } | ||
| 480 | |||
| 481 | ✗ | fsize = avio_size(io); | |
| 482 | ✗ | if (fsize < 0 || fsize > INT_MAX) { | |
| 483 | ✗ | av_log(logctx, AV_LOG_ERROR, "Cannot obtain size of file %s\n", path); | |
| 484 | ✗ | ret = AVERROR(EIO); | |
| 485 | ✗ | goto fail; | |
| 486 | } | ||
| 487 | |||
| 488 | ✗ | *data = av_malloc(fsize); | |
| 489 | ✗ | if (!*data) { | |
| 490 | ✗ | ret = AVERROR(ENOMEM); | |
| 491 | ✗ | goto fail; | |
| 492 | } | ||
| 493 | |||
| 494 | ✗ | ret = avio_read(io, *data, fsize); | |
| 495 | ✗ | if (ret != fsize) { | |
| 496 | ✗ | av_log(logctx, AV_LOG_ERROR, "Error reading file %s\n", path); | |
| 497 | ✗ | ret = ret < 0 ? ret : AVERROR(EIO); | |
| 498 | ✗ | goto fail; | |
| 499 | } | ||
| 500 | |||
| 501 | ✗ | *len = fsize; | |
| 502 | |||
| 503 | ✗ | ret = 0; | |
| 504 | ✗ | fail: | |
| 505 | ✗ | avio_close(io); | |
| 506 | ✗ | if (ret < 0) { | |
| 507 | ✗ | av_freep(data); | |
| 508 | ✗ | *len = 0; | |
| 509 | } | ||
| 510 | ✗ | return ret; | |
| 511 | } | ||
| 512 | |||
| 513 | 34548 | static int filter_opt_apply(void *logctx, AVFilterContext *f, | |
| 514 | const char *key, const char *val) | ||
| 515 | { | ||
| 516 | 34548 | const AVOption *o = NULL; | |
| 517 | int ret; | ||
| 518 | |||
| 519 | 34548 | ret = av_opt_set(f, key, val, AV_OPT_SEARCH_CHILDREN); | |
| 520 | 34548 | if (ret >= 0) | |
| 521 | 34548 | return 0; | |
| 522 | |||
| 523 | ✗ | if (ret == AVERROR_OPTION_NOT_FOUND && key[0] == '/') | |
| 524 | ✗ | o = av_opt_find(f, key + 1, NULL, 0, AV_OPT_SEARCH_CHILDREN); | |
| 525 | ✗ | if (!o) | |
| 526 | ✗ | goto err_apply; | |
| 527 | |||
| 528 | // key is a valid option name prefixed with '/' | ||
| 529 | // interpret value as a path from which to load the actual option value | ||
| 530 | ✗ | key++; | |
| 531 | |||
| 532 | ✗ | if (o->type == AV_OPT_TYPE_BINARY) { | |
| 533 | uint8_t *data; | ||
| 534 | int len; | ||
| 535 | |||
| 536 | ✗ | ret = read_binary(logctx, val, &data, &len); | |
| 537 | ✗ | if (ret < 0) | |
| 538 | ✗ | goto err_load; | |
| 539 | |||
| 540 | ✗ | ret = av_opt_set_bin(f, key, data, len, AV_OPT_SEARCH_CHILDREN); | |
| 541 | ✗ | av_freep(&data); | |
| 542 | } else { | ||
| 543 | ✗ | char *data = read_file_to_string(val); | |
| 544 | ✗ | if (!data) { | |
| 545 | ✗ | ret = AVERROR(EIO); | |
| 546 | ✗ | goto err_load; | |
| 547 | } | ||
| 548 | |||
| 549 | ✗ | ret = av_opt_set(f, key, data, AV_OPT_SEARCH_CHILDREN); | |
| 550 | ✗ | av_freep(&data); | |
| 551 | } | ||
| 552 | ✗ | if (ret < 0) | |
| 553 | ✗ | goto err_apply; | |
| 554 | |||
| 555 | ✗ | return 0; | |
| 556 | |||
| 557 | ✗ | err_apply: | |
| 558 | ✗ | av_log(logctx, AV_LOG_ERROR, | |
| 559 | "Error applying option '%s' to filter '%s': %s\n", | ||
| 560 | ✗ | key, f->filter->name, av_err2str(ret)); | |
| 561 | ✗ | return ret; | |
| 562 | ✗ | err_load: | |
| 563 | ✗ | av_log(logctx, AV_LOG_ERROR, | |
| 564 | "Error loading value for option '%s' from file '%s'\n", | ||
| 565 | key, val); | ||
| 566 | ✗ | return ret; | |
| 567 | } | ||
| 568 | |||
| 569 | 17464 | static int graph_opts_apply(void *logctx, AVFilterGraphSegment *seg) | |
| 570 | { | ||
| 571 | 35448 | for (size_t i = 0; i < seg->nb_chains; i++) { | |
| 572 | 17984 | AVFilterChain *ch = seg->chains[i]; | |
| 573 | |||
| 574 | 57155 | for (size_t j = 0; j < ch->nb_filters; j++) { | |
| 575 | 39171 | AVFilterParams *p = ch->filters[j]; | |
| 576 | 39171 | const AVDictionaryEntry *e = NULL; | |
| 577 | |||
| 578 | 39171 | av_assert0(p->filter); | |
| 579 | |||
| 580 | 73719 | while ((e = av_dict_iterate(p->opts, e))) { | |
| 581 | 34548 | int ret = filter_opt_apply(logctx, p->filter, e->key, e->value); | |
| 582 | 34548 | if (ret < 0) | |
| 583 | ✗ | return ret; | |
| 584 | } | ||
| 585 | |||
| 586 | 39171 | av_dict_free(&p->opts); | |
| 587 | } | ||
| 588 | } | ||
| 589 | |||
| 590 | 17464 | return 0; | |
| 591 | } | ||
| 592 | |||
| 593 | 17464 | static int graph_parse(void *logctx, | |
| 594 | AVFilterGraph *graph, const char *desc, | ||
| 595 | AVFilterInOut **inputs, AVFilterInOut **outputs, | ||
| 596 | AVBufferRef *hw_device) | ||
| 597 | { | ||
| 598 | AVFilterGraphSegment *seg; | ||
| 599 | int ret; | ||
| 600 | |||
| 601 | 17464 | *inputs = NULL; | |
| 602 | 17464 | *outputs = NULL; | |
| 603 | |||
| 604 | 17464 | ret = avfilter_graph_segment_parse(graph, desc, 0, &seg); | |
| 605 | 17464 | if (ret < 0) | |
| 606 | ✗ | return ret; | |
| 607 | |||
| 608 | 17464 | ret = avfilter_graph_segment_create_filters(seg, 0); | |
| 609 | 17464 | if (ret < 0) | |
| 610 | ✗ | goto fail; | |
| 611 | |||
| 612 | 17464 | if (hw_device) { | |
| 613 | ✗ | for (int i = 0; i < graph->nb_filters; i++) { | |
| 614 | ✗ | AVFilterContext *f = graph->filters[i]; | |
| 615 | |||
| 616 | ✗ | if (!(f->filter->flags & AVFILTER_FLAG_HWDEVICE)) | |
| 617 | ✗ | continue; | |
| 618 | ✗ | f->hw_device_ctx = av_buffer_ref(hw_device); | |
| 619 | ✗ | if (!f->hw_device_ctx) { | |
| 620 | ✗ | ret = AVERROR(ENOMEM); | |
| 621 | ✗ | goto fail; | |
| 622 | } | ||
| 623 | } | ||
| 624 | } | ||
| 625 | |||
| 626 | 17464 | ret = graph_opts_apply(logctx, seg); | |
| 627 | 17464 | if (ret < 0) | |
| 628 | ✗ | goto fail; | |
| 629 | |||
| 630 | 17464 | ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs); | |
| 631 | |||
| 632 | 17464 | fail: | |
| 633 | 17464 | avfilter_graph_segment_free(&seg); | |
| 634 | 17464 | return ret; | |
| 635 | } | ||
| 636 | |||
| 637 | // Filters can be configured only if the formats of all inputs are known. | ||
| 638 | 16404 | static int ifilter_has_all_input_formats(FilterGraph *fg) | |
| 639 | { | ||
| 640 | 24430 | for (int i = 0; i < fg->nb_inputs; i++) { | |
| 641 | 15548 | InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]); | |
| 642 | 15548 | if (ifp->format < 0) | |
| 643 | 7522 | return 0; | |
| 644 | } | ||
| 645 | 8882 | return 1; | |
| 646 | } | ||
| 647 | |||
| 648 | static int filter_thread(void *arg); | ||
| 649 | |||
| 650 | 16286 | static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in) | |
| 651 | { | ||
| 652 | 16286 | AVFilterContext *ctx = inout->filter_ctx; | |
| 653 | 16286 | AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; | |
| 654 | 16286 | int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; | |
| 655 | |||
| 656 | 16286 | if (nb_pads > 1) | |
| 657 | 307 | return av_strdup(ctx->filter->name); | |
| 658 | 15979 | return av_asprintf("%s:%s", ctx->filter->name, | |
| 659 | avfilter_pad_get_name(pads, inout->pad_idx)); | ||
| 660 | } | ||
| 661 | |||
| 662 | 5 | static const char *ofilter_item_name(void *obj) | |
| 663 | { | ||
| 664 | 5 | OutputFilterPriv *ofp = obj; | |
| 665 | 5 | return ofp->log_name; | |
| 666 | } | ||
| 667 | |||
| 668 | static const AVOption ofilter_options[] = { | ||
| 669 | {"video_size", "set video size", offsetof(OutputFilterPriv, width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, INT_MAX }, | ||
| 670 | {"pixel_format", "set pixel format", offsetof(OutputFilterPriv, format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX }, | ||
| 671 | {"colorspace", "color space", offsetof(OutputFilterPriv, color_space), AV_OPT_TYPE_INT, {.i64 = AVCOL_SPC_UNSPECIFIED }, 0, INT_MAX }, | ||
| 672 | {"color_range", "color range", offsetof(OutputFilterPriv, color_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, INT_MAX }, | ||
| 673 | {"alpha_mode", "color range", offsetof(OutputFilterPriv, alpha_mode), AV_OPT_TYPE_INT, {.i64 = AVALPHA_MODE_UNSPECIFIED }, 0, INT_MAX }, | ||
| 674 | {"chroma_location", "chroma sample location", offsetof(OutputFilterPriv, chroma_location), AV_OPT_TYPE_INT, {.i64 = AVCHROMA_LOC_UNSPECIFIED }, 0, INT_MAX }, | ||
| 675 | {"ar", "set audio sampling rate (in Hz)", offsetof(OutputFilterPriv, sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX }, | ||
| 676 | {"ch_layout", NULL, offsetof(OutputFilterPriv, ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL }, 0, 0 }, | ||
| 677 | { NULL }, | ||
| 678 | }; | ||
| 679 | |||
| 680 | static const AVClass ofilter_class = { | ||
| 681 | .class_name = "OutputFilter", | ||
| 682 | .version = LIBAVUTIL_VERSION_INT, | ||
| 683 | .item_name = ofilter_item_name, | ||
| 684 | .option = ofilter_options, | ||
| 685 | .parent_log_context_offset = offsetof(OutputFilterPriv, log_parent), | ||
| 686 | .category = AV_CLASS_CATEGORY_FILTER, | ||
| 687 | }; | ||
| 688 | |||
| 689 | 8817 | static OutputFilter *ofilter_alloc(FilterGraph *fg, enum AVMediaType type) | |
| 690 | { | ||
| 691 | OutputFilterPriv *ofp; | ||
| 692 | OutputFilter *ofilter; | ||
| 693 | |||
| 694 | 8817 | ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp), &fg->nb_outputs); | |
| 695 | 8817 | if (!ofp) | |
| 696 | ✗ | return NULL; | |
| 697 | |||
| 698 | 8817 | ofilter = &ofp->ofilter; | |
| 699 | 8817 | ofilter->class = &ofilter_class; | |
| 700 | 8817 | ofp->log_parent = fg; | |
| 701 | 8817 | ofilter->graph = fg; | |
| 702 | 8817 | ofilter->type = type; | |
| 703 | 8817 | av_opt_set_defaults(ofp); | |
| 704 | 8817 | ofp->reinit_opts = (ReinitOpts){ .pts = AV_NOPTS_VALUE }; | |
| 705 | 8817 | ofilter->index = fg->nb_outputs - 1; | |
| 706 | |||
| 707 | 17634 | snprintf(ofp->log_name, sizeof(ofp->log_name), "%co%d", | |
| 708 | 8817 | av_get_media_type_string(type)[0], ofilter->index); | |
| 709 | |||
| 710 | 8817 | return ofilter; | |
| 711 | } | ||
| 712 | |||
| 713 | 7429 | static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist, | |
| 714 | const ViewSpecifier *vs) | ||
| 715 | { | ||
| 716 | 7429 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 717 | 7429 | FilterGraphPriv *fgp = fgp_from_fg(ifilter->graph); | |
| 718 | SchedulerNode src; | ||
| 719 | int ret; | ||
| 720 | |||
| 721 | 7429 | av_assert0(!ifp->bound); | |
| 722 | 7429 | ifp->bound = 1; | |
| 723 | |||
| 724 | 7429 | if (ifilter->type != ist->par->codec_type && | |
| 725 | 4 | !(ifilter->type == AVMEDIA_TYPE_VIDEO && ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) { | |
| 726 | ✗ | av_log(fgp, AV_LOG_ERROR, "Tried to connect %s stream to %s filtergraph input\n", | |
| 727 | ✗ | av_get_media_type_string(ist->par->codec_type), av_get_media_type_string(ifilter->type)); | |
| 728 | ✗ | return AVERROR(EINVAL); | |
| 729 | } | ||
| 730 | |||
| 731 | 7429 | ifp->type_src = ist->st->codecpar->codec_type; | |
| 732 | |||
| 733 | 7429 | ifp->opts.fallback = av_frame_alloc(); | |
| 734 | 7429 | if (!ifp->opts.fallback) | |
| 735 | ✗ | return AVERROR(ENOMEM); | |
| 736 | |||
| 737 | 7429 | ret = ist_filter_add(ist, ifilter, filtergraph_is_simple(ifilter->graph), | |
| 738 | vs, &ifp->opts, &src); | ||
| 739 | 7429 | if (ret < 0) | |
| 740 | ✗ | return ret; | |
| 741 | |||
| 742 | 7429 | ifilter->input_name = av_strdup(ifp->opts.name); | |
| 743 | 7429 | if (!ifilter->input_name) | |
| 744 | ✗ | return AVERROR(EINVAL); | |
| 745 | |||
| 746 | 7429 | ret = sch_connect(fgp->sch, | |
| 747 | 7429 | src, SCH_FILTER_IN(fgp->sch_idx, ifilter->index)); | |
| 748 | 7429 | if (ret < 0) | |
| 749 | ✗ | return ret; | |
| 750 | |||
| 751 | 7429 | if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) { | |
| 752 | 4 | ifp->sub2video.frame = av_frame_alloc(); | |
| 753 | 4 | if (!ifp->sub2video.frame) | |
| 754 | ✗ | return AVERROR(ENOMEM); | |
| 755 | |||
| 756 | 4 | ifp->width = ifp->opts.sub2video_width; | |
| 757 | 4 | ifp->height = ifp->opts.sub2video_height; | |
| 758 | |||
| 759 | /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the | ||
| 760 | palettes for all rectangles are identical or compatible */ | ||
| 761 | 4 | ifp->format = AV_PIX_FMT_RGB32; | |
| 762 | |||
| 763 | 4 | ifp->time_base = AV_TIME_BASE_Q; | |
| 764 | |||
| 765 | 4 | av_log(fgp, AV_LOG_VERBOSE, "sub2video: using %dx%d canvas\n", | |
| 766 | ifp->width, ifp->height); | ||
| 767 | } | ||
| 768 | |||
| 769 | 7429 | return 0; | |
| 770 | } | ||
| 771 | |||
| 772 | 1 | static int ifilter_bind_dec(InputFilterPriv *ifp, Decoder *dec, | |
| 773 | const ViewSpecifier *vs) | ||
| 774 | { | ||
| 775 | 1 | FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph); | |
| 776 | SchedulerNode src; | ||
| 777 | int ret; | ||
| 778 | |||
| 779 | 1 | av_assert0(!ifp->bound); | |
| 780 | 1 | ifp->bound = 1; | |
| 781 | |||
| 782 | 1 | if (ifp->ifilter.type != dec->type) { | |
| 783 | ✗ | av_log(fgp, AV_LOG_ERROR, "Tried to connect %s decoder to %s filtergraph input\n", | |
| 784 | av_get_media_type_string(dec->type), av_get_media_type_string(ifp->ifilter.type)); | ||
| 785 | ✗ | return AVERROR(EINVAL); | |
| 786 | } | ||
| 787 | |||
| 788 | 1 | ifp->type_src = ifp->ifilter.type; | |
| 789 | |||
| 790 | 1 | ret = dec_filter_add(dec, &ifp->ifilter, &ifp->opts, vs, &src); | |
| 791 | 1 | if (ret < 0) | |
| 792 | ✗ | return ret; | |
| 793 | |||
| 794 | 1 | ifp->ifilter.input_name = av_strdup(ifp->opts.name); | |
| 795 | 1 | if (!ifp->ifilter.input_name) | |
| 796 | ✗ | return AVERROR(EINVAL); | |
| 797 | |||
| 798 | 1 | ret = sch_connect(fgp->sch, src, SCH_FILTER_IN(fgp->sch_idx, ifp->ifilter.index)); | |
| 799 | 1 | if (ret < 0) | |
| 800 | ✗ | return ret; | |
| 801 | |||
| 802 | 1 | return 0; | |
| 803 | } | ||
| 804 | |||
| 805 | 11 | static int set_channel_layout(OutputFilterPriv *f, const AVChannelLayout *layouts_allowed, | |
| 806 | const AVChannelLayout *layout_requested) | ||
| 807 | { | ||
| 808 | int i, err; | ||
| 809 | |||
| 810 | 11 | if (layout_requested->order != AV_CHANNEL_ORDER_UNSPEC) { | |
| 811 | /* Pass the layout through for all orders but UNSPEC */ | ||
| 812 | 1 | err = av_channel_layout_copy(&f->ch_layout, layout_requested); | |
| 813 | 1 | if (err < 0) | |
| 814 | ✗ | return err; | |
| 815 | 1 | return 0; | |
| 816 | } | ||
| 817 | |||
| 818 | /* Requested layout is of order UNSPEC */ | ||
| 819 | 10 | if (!layouts_allowed) { | |
| 820 | /* Use the default native layout for the requested amount of channels when the | ||
| 821 | encoder doesn't have a list of supported layouts */ | ||
| 822 | 10 | av_channel_layout_default(&f->ch_layout, layout_requested->nb_channels); | |
| 823 | 10 | return 0; | |
| 824 | } | ||
| 825 | /* Encoder has a list of supported layouts. Pick the first layout in it with the | ||
| 826 | same amount of channels as the requested layout */ | ||
| 827 | ✗ | for (i = 0; layouts_allowed[i].nb_channels; i++) { | |
| 828 | ✗ | if (layouts_allowed[i].nb_channels == layout_requested->nb_channels) | |
| 829 | ✗ | break; | |
| 830 | } | ||
| 831 | ✗ | if (layouts_allowed[i].nb_channels) { | |
| 832 | /* Use it if one is found */ | ||
| 833 | ✗ | err = av_channel_layout_copy(&f->ch_layout, &layouts_allowed[i]); | |
| 834 | ✗ | if (err < 0) | |
| 835 | ✗ | return err; | |
| 836 | ✗ | return 0; | |
| 837 | } | ||
| 838 | /* If no layout for the amount of channels requested was found, use the default | ||
| 839 | native layout for it. */ | ||
| 840 | ✗ | av_channel_layout_default(&f->ch_layout, layout_requested->nb_channels); | |
| 841 | |||
| 842 | ✗ | return 0; | |
| 843 | } | ||
| 844 | |||
| 845 | ✗ | static int parse_reinit_opts(AVFifo **pout, const char *opts, void *logctx) | |
| 846 | { | ||
| 847 | AVFifo *out; | ||
| 848 | ✗ | int ret = AVERROR_BUG; | |
| 849 | ✗ | char *ptr, *str, *substr = NULL; | |
| 850 | const char *token; | ||
| 851 | |||
| 852 | ✗ | str = av_strdup(opts); | |
| 853 | ✗ | if (!str) | |
| 854 | ✗ | return AVERROR(ENOMEM); | |
| 855 | |||
| 856 | ✗ | out = av_fifo_alloc2(1, sizeof(ReinitOpts), AV_FIFO_FLAG_AUTO_GROW); | |
| 857 | ✗ | if (!out) { | |
| 858 | ✗ | ret = AVERROR(ENOMEM); | |
| 859 | ✗ | goto end; | |
| 860 | } | ||
| 861 | |||
| 862 | ✗ | token = av_strtok(str, ",", &ptr); | |
| 863 | ✗ | while (token) { | |
| 864 | ✗ | ReinitOpts o = { 0 }; | |
| 865 | const char *subtoken; | ||
| 866 | char *subptr, *endptr; | ||
| 867 | |||
| 868 | ✗ | substr = av_strdup(token); | |
| 869 | ✗ | if (!substr) { | |
| 870 | ✗ | ret = AVERROR(ENOMEM); | |
| 871 | ✗ | goto end; | |
| 872 | } | ||
| 873 | ✗ | subtoken = av_strtok(substr, "|", &subptr); | |
| 874 | ✗ | if (subtoken && subptr) { | |
| 875 | ✗ | if (!av_strstart(subtoken, "pts=", &subtoken)) { | |
| 876 | ✗ | av_log(logctx, AV_LOG_ERROR, "Invalid reinit identifier\n"); | |
| 877 | ✗ | ret = AVERROR(ENOMEM); | |
| 878 | ✗ | goto end; | |
| 879 | } | ||
| 880 | ✗ | o.pts = strtoll(subtoken, &endptr, 0); | |
| 881 | ✗ | if (*endptr || o.pts < 0) { | |
| 882 | ✗ | ret = AVERROR(EINVAL); | |
| 883 | ✗ | goto end; | |
| 884 | } | ||
| 885 | ✗ | ret = av_dict_parse_string(&o.dict, subptr, "=", ":", 0); | |
| 886 | ✗ | if (ret < 0) { | |
| 887 | ✗ | av_log(logctx, AV_LOG_ERROR, "Error parsing encoder options\n"); | |
| 888 | ✗ | goto end; | |
| 889 | } | ||
| 890 | ✗ | ret = av_fifo_write(out, &o, 1); | |
| 891 | ✗ | if (ret < 0) { | |
| 892 | ✗ | av_dict_free(&o.dict); | |
| 893 | ✗ | goto end; | |
| 894 | } | ||
| 895 | } else { | ||
| 896 | ✗ | ret = AVERROR(EINVAL); | |
| 897 | ✗ | goto end; | |
| 898 | } | ||
| 899 | ✗ | av_freep(&substr); | |
| 900 | ✗ | if (ptr) | |
| 901 | ✗ | ptr += strspn(ptr, " \n\t\r"); | |
| 902 | ✗ | token = av_strtok(NULL, ",", &ptr); | |
| 903 | } | ||
| 904 | |||
| 905 | ✗ | ret = 0; | |
| 906 | ✗ | end: | |
| 907 | ✗ | *pout = out; | |
| 908 | ✗ | av_free(substr); | |
| 909 | ✗ | av_free(str); | |
| 910 | |||
| 911 | ✗ | return ret; | |
| 912 | } | ||
| 913 | |||
| 914 | 8809 | int ofilter_bind_enc(OutputFilter *ofilter, unsigned sched_idx_enc, | |
| 915 | const OutputFilterOptions *opts) | ||
| 916 | { | ||
| 917 | 8809 | OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); | |
| 918 | 8809 | FilterGraph *fg = ofilter->graph; | |
| 919 | 8809 | FilterGraphPriv *fgp = fgp_from_fg(fg); | |
| 920 | int ret; | ||
| 921 | |||
| 922 | 8809 | av_assert0(!ofilter->bound); | |
| 923 | 8809 | av_assert0(!opts->enc || | |
| 924 | ofilter->type == opts->enc->type); | ||
| 925 | |||
| 926 | 8809 | ofp->needed = NEEDED_EXPLICIT; | |
| 927 | 8809 | ofilter->bound = 1; | |
| 928 | 8809 | av_freep(&ofilter->linklabel); | |
| 929 | |||
| 930 | 8809 | ofp->flags |= opts->flags; | |
| 931 | 8809 | ofp->ts_offset = opts->ts_offset; | |
| 932 | 8809 | ofp->enc_timebase = opts->output_tb; | |
| 933 | |||
| 934 | 8809 | ofp->trim_start_us = opts->trim_start_us; | |
| 935 | 8809 | ofp->trim_duration_us = opts->trim_duration_us; | |
| 936 | |||
| 937 | 8809 | ofilter->output_name = av_strdup(opts->name); | |
| 938 | 8809 | if (!ofilter->output_name) | |
| 939 | ✗ | return AVERROR(EINVAL); | |
| 940 | |||
| 941 | 8809 | if (opts->reinit_opts) { | |
| 942 | ✗ | ret = parse_reinit_opts(&ofp->reinit_opts_fifo, opts->reinit_opts, ofilter); | |
| 943 | ✗ | if (ret < 0) | |
| 944 | ✗ | return ret; | |
| 945 | } | ||
| 946 | |||
| 947 | 8809 | ret = av_dict_copy(&ofp->sws_opts, opts->sws_opts, 0); | |
| 948 | 8809 | if (ret < 0) | |
| 949 | ✗ | return ret; | |
| 950 | |||
| 951 | 8809 | ret = av_dict_copy(&ofp->swr_opts, opts->swr_opts, 0); | |
| 952 | 8809 | if (ret < 0) | |
| 953 | ✗ | return ret; | |
| 954 | |||
| 955 | 8809 | if (opts->flags & OFILTER_FLAG_AUDIO_24BIT) | |
| 956 | 67 | av_dict_set(&ofp->swr_opts, "output_sample_bits", "24", 0); | |
| 957 | |||
| 958 | 8809 | if (fgp->is_simple) { | |
| 959 | // for simple filtergraph there is just one output, | ||
| 960 | // so use only graph-level information for logging | ||
| 961 | 7243 | ofp->log_parent = NULL; | |
| 962 | 7243 | av_strlcpy(ofp->log_name, fgp->log_name, sizeof(ofp->log_name)); | |
| 963 | } else | ||
| 964 | 1566 | av_strlcatf(ofp->log_name, sizeof(ofp->log_name), "->%s", ofilter->output_name); | |
| 965 | |||
| 966 | 8809 | switch (ofilter->type) { | |
| 967 | 7110 | case AVMEDIA_TYPE_VIDEO: | |
| 968 | 7110 | ofp->width = opts->width; | |
| 969 | 7110 | ofp->height = opts->height; | |
| 970 | 7110 | if (opts->format != AV_PIX_FMT_NONE) { | |
| 971 | 5477 | ofp->format = opts->format; | |
| 972 | } else | ||
| 973 | 1633 | ofp->pix_fmts = opts->pix_fmts; | |
| 974 | |||
| 975 | 7110 | if (opts->color_space != AVCOL_SPC_UNSPECIFIED) | |
| 976 | ✗ | ofp->color_space = opts->color_space; | |
| 977 | else | ||
| 978 | 7110 | ofp->color_spaces = opts->color_spaces; | |
| 979 | |||
| 980 | 7110 | if (opts->color_range != AVCOL_RANGE_UNSPECIFIED) | |
| 981 | 1656 | ofp->color_range = opts->color_range; | |
| 982 | else | ||
| 983 | 5454 | ofp->color_ranges = opts->color_ranges; | |
| 984 | |||
| 985 | 7110 | if (opts->alpha_mode != AVALPHA_MODE_UNSPECIFIED) | |
| 986 | ✗ | ofp->alpha_mode = opts->alpha_mode; | |
| 987 | else | ||
| 988 | 7110 | ofp->alpha_modes = opts->alpha_modes; | |
| 989 | |||
| 990 | 7110 | if (opts->chroma_location != AVCHROMA_LOC_UNSPECIFIED) | |
| 991 | 524 | ofp->chroma_location = opts->chroma_location; | |
| 992 | else | ||
| 993 | 6586 | ofp->chroma_locations = opts->chroma_locations; | |
| 994 | |||
| 995 | 7110 | fgp->disable_conversions |= !!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT); | |
| 996 | |||
| 997 | 7110 | ofp->fps.last_frame = av_frame_alloc(); | |
| 998 | 7110 | if (!ofp->fps.last_frame) | |
| 999 | ✗ | return AVERROR(ENOMEM); | |
| 1000 | |||
| 1001 | 7110 | ofp->fps.vsync_method = opts->vsync_method; | |
| 1002 | 7110 | ofp->fps.framerate = opts->frame_rate; | |
| 1003 | 7110 | ofp->fps.framerate_max = opts->max_frame_rate; | |
| 1004 | 7110 | ofp->fps.framerate_supported = opts->frame_rates; | |
| 1005 | |||
| 1006 | // reduce frame rate for mpeg4 to be within the spec limits | ||
| 1007 | 7110 | if (opts->enc && opts->enc->id == AV_CODEC_ID_MPEG4) | |
| 1008 | 79 | ofp->fps.framerate_clip = 65535; | |
| 1009 | |||
| 1010 | 7110 | ofp->fps.dup_warning = 1000; | |
| 1011 | |||
| 1012 | 7110 | break; | |
| 1013 | 1699 | case AVMEDIA_TYPE_AUDIO: | |
| 1014 | 1699 | if (opts->format != AV_SAMPLE_FMT_NONE) { | |
| 1015 | 9 | ofp->format = opts->format; | |
| 1016 | } else { | ||
| 1017 | 1690 | ofp->sample_fmts = opts->sample_fmts; | |
| 1018 | } | ||
| 1019 | 1699 | if (opts->sample_rate) { | |
| 1020 | 17 | ofp->sample_rate = opts->sample_rate; | |
| 1021 | } else | ||
| 1022 | 1682 | ofp->sample_rates = opts->sample_rates; | |
| 1023 | 1699 | if (opts->ch_layout.nb_channels) { | |
| 1024 | 11 | int ret = set_channel_layout(ofp, opts->ch_layouts, &opts->ch_layout); | |
| 1025 | 11 | if (ret < 0) | |
| 1026 | ✗ | return ret; | |
| 1027 | } else { | ||
| 1028 | 1688 | ofp->ch_layouts = opts->ch_layouts; | |
| 1029 | } | ||
| 1030 | 1699 | break; | |
| 1031 | } | ||
| 1032 | |||
| 1033 | 8809 | ret = sch_connect(fgp->sch, SCH_FILTER_OUT(fgp->sch_idx, ofilter->index), | |
| 1034 | 8809 | SCH_ENC(sched_idx_enc)); | |
| 1035 | 8809 | if (ret < 0) | |
| 1036 | ✗ | return ret; | |
| 1037 | |||
| 1038 | 8809 | return 0; | |
| 1039 | } | ||
| 1040 | |||
| 1041 | 1 | static int ofilter_bind_ifilter(OutputFilter *ofilter, InputFilterPriv *ifp, | |
| 1042 | const OutputFilterOptions *opts) | ||
| 1043 | { | ||
| 1044 | 1 | OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); | |
| 1045 | |||
| 1046 | 1 | av_assert0(!ofilter->bound); | |
| 1047 | 1 | av_assert0(ofilter->type == ifp->ifilter.type); | |
| 1048 | |||
| 1049 | 1 | ofilter->bound = 1; | |
| 1050 | 1 | av_freep(&ofilter->linklabel); | |
| 1051 | |||
| 1052 | 1 | ofilter->output_name = av_strdup(opts->name); | |
| 1053 | 1 | if (!ofilter->output_name) | |
| 1054 | ✗ | return AVERROR(EINVAL); | |
| 1055 | |||
| 1056 | 1 | ifp->ofilter_src = ofilter; | |
| 1057 | |||
| 1058 | 1 | av_strlcatf(ofp->log_name, sizeof(ofp->log_name), "->%s", ofilter->output_name); | |
| 1059 | |||
| 1060 | 1 | return 0; | |
| 1061 | } | ||
| 1062 | |||
| 1063 | 1 | static int ifilter_bind_fg(InputFilterPriv *ifp, FilterGraph *fg_src, int out_idx) | |
| 1064 | { | ||
| 1065 | 1 | FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph); | |
| 1066 | 1 | OutputFilter *ofilter_src = fg_src->outputs[out_idx]; | |
| 1067 | OutputFilterOptions opts; | ||
| 1068 | char name[32]; | ||
| 1069 | int ret; | ||
| 1070 | |||
| 1071 | 1 | av_assert0(!ifp->bound); | |
| 1072 | 1 | ifp->bound = 1; | |
| 1073 | |||
| 1074 | 1 | if (ifp->ifilter.type != ofilter_src->type) { | |
| 1075 | ✗ | av_log(fgp, AV_LOG_ERROR, "Tried to connect %s output to %s input\n", | |
| 1076 | av_get_media_type_string(ofilter_src->type), | ||
| 1077 | av_get_media_type_string(ifp->ifilter.type)); | ||
| 1078 | ✗ | return AVERROR(EINVAL); | |
| 1079 | } | ||
| 1080 | |||
| 1081 | 1 | ifp->type_src = ifp->ifilter.type; | |
| 1082 | |||
| 1083 | 1 | memset(&opts, 0, sizeof(opts)); | |
| 1084 | |||
| 1085 | 1 | snprintf(name, sizeof(name), "fg:%d:%d", fgp->fg.index, ifp->ifilter.index); | |
| 1086 | 1 | opts.name = name; | |
| 1087 | |||
| 1088 | 1 | ret = ofilter_bind_ifilter(ofilter_src, ifp, &opts); | |
| 1089 | 1 | if (ret < 0) | |
| 1090 | ✗ | return ret; | |
| 1091 | |||
| 1092 | 1 | ret = sch_connect(fgp->sch, SCH_FILTER_OUT(fg_src->index, out_idx), | |
| 1093 | 1 | SCH_FILTER_IN(fgp->sch_idx, ifp->ifilter.index)); | |
| 1094 | 1 | if (ret < 0) | |
| 1095 | ✗ | return ret; | |
| 1096 | |||
| 1097 | 1 | return 0; | |
| 1098 | } | ||
| 1099 | |||
| 1100 | 7469 | static InputFilter *ifilter_alloc(FilterGraph *fg) | |
| 1101 | { | ||
| 1102 | InputFilterPriv *ifp; | ||
| 1103 | InputFilter *ifilter; | ||
| 1104 | |||
| 1105 | 7469 | ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp), &fg->nb_inputs); | |
| 1106 | 7469 | if (!ifp) | |
| 1107 | ✗ | return NULL; | |
| 1108 | |||
| 1109 | 7469 | ifilter = &ifp->ifilter; | |
| 1110 | 7469 | ifilter->graph = fg; | |
| 1111 | |||
| 1112 | 7469 | ifp->frame = av_frame_alloc(); | |
| 1113 | 7469 | if (!ifp->frame) | |
| 1114 | ✗ | return NULL; | |
| 1115 | |||
| 1116 | 7469 | ifilter->index = fg->nb_inputs - 1; | |
| 1117 | 7469 | ifp->format = -1; | |
| 1118 | 7469 | ifp->color_space = AVCOL_SPC_UNSPECIFIED; | |
| 1119 | 7469 | ifp->color_range = AVCOL_RANGE_UNSPECIFIED; | |
| 1120 | 7469 | ifp->alpha_mode = AVALPHA_MODE_UNSPECIFIED; | |
| 1121 | 7469 | ifp->chroma_location = AVCHROMA_LOC_UNSPECIFIED; | |
| 1122 | |||
| 1123 | 7469 | ifp->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW); | |
| 1124 | 7469 | if (!ifp->frame_queue) | |
| 1125 | ✗ | return NULL; | |
| 1126 | |||
| 1127 | 7469 | return ifilter; | |
| 1128 | } | ||
| 1129 | |||
| 1130 | 11267 | void fg_free(FilterGraph **pfg) | |
| 1131 | { | ||
| 1132 | 11267 | FilterGraph *fg = *pfg; | |
| 1133 | FilterGraphPriv *fgp; | ||
| 1134 | |||
| 1135 | 11267 | if (!fg) | |
| 1136 | 2684 | return; | |
| 1137 | 8583 | fgp = fgp_from_fg(fg); | |
| 1138 | |||
| 1139 | 16052 | for (int j = 0; j < fg->nb_inputs; j++) { | |
| 1140 | 7469 | InputFilter *ifilter = fg->inputs[j]; | |
| 1141 | 7469 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 1142 | |||
| 1143 | 7469 | if (ifp->frame_queue) { | |
| 1144 | AVFrame *frame; | ||
| 1145 | 7469 | while (av_fifo_read(ifp->frame_queue, &frame, 1) >= 0) | |
| 1146 | ✗ | av_frame_free(&frame); | |
| 1147 | 7469 | av_fifo_freep2(&ifp->frame_queue); | |
| 1148 | } | ||
| 1149 | 7469 | av_frame_free(&ifp->sub2video.frame); | |
| 1150 | |||
| 1151 | 7469 | av_frame_free(&ifp->frame); | |
| 1152 | 7469 | av_frame_free(&ifp->opts.fallback); | |
| 1153 | |||
| 1154 | 7469 | av_buffer_unref(&ifp->hw_frames_ctx); | |
| 1155 | 7469 | av_channel_layout_uninit(&ifp->ch_layout); | |
| 1156 | 7469 | av_freep(&ifilter->linklabel); | |
| 1157 | 7469 | av_freep(&ifp->opts.name); | |
| 1158 | 7469 | av_buffer_unref(&ifp->downmixmatrix); | |
| 1159 | 7469 | av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data); | |
| 1160 | 7469 | av_freep(&ifilter->name); | |
| 1161 | 7469 | av_freep(&ifilter->input_name); | |
| 1162 | 7469 | av_freep(&fg->inputs[j]); | |
| 1163 | } | ||
| 1164 | 8583 | av_freep(&fg->inputs); | |
| 1165 | 17400 | for (int j = 0; j < fg->nb_outputs; j++) { | |
| 1166 | 8817 | OutputFilter *ofilter = fg->outputs[j]; | |
| 1167 | 8817 | OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); | |
| 1168 | |||
| 1169 | 8817 | av_frame_free(&ofp->fps.last_frame); | |
| 1170 | 8817 | av_dict_free(&ofp->sws_opts); | |
| 1171 | 8817 | av_dict_free(&ofp->swr_opts); | |
| 1172 | |||
| 1173 | 8817 | av_freep(&ofilter->linklabel); | |
| 1174 | 8817 | av_freep(&ofilter->name); | |
| 1175 | 8817 | av_freep(&ofilter->output_name); | |
| 1176 | 8817 | av_freep(&ofilter->apad); | |
| 1177 | 8817 | av_dict_free(&ofp->reinit_opts.dict); | |
| 1178 | 8817 | if (ofp->reinit_opts_fifo) { | |
| 1179 | ✗ | while (av_fifo_read(ofp->reinit_opts_fifo, &ofp->reinit_opts, 1) >= 0) | |
| 1180 | ✗ | av_dict_free(&ofp->reinit_opts.dict); | |
| 1181 | ✗ | av_fifo_freep2(&ofp->reinit_opts_fifo); | |
| 1182 | } | ||
| 1183 | 8817 | av_channel_layout_uninit(&ofp->ch_layout); | |
| 1184 | 8817 | av_frame_side_data_free(&ofp->side_data, &ofp->nb_side_data); | |
| 1185 | 8817 | av_freep(&fg->outputs[j]); | |
| 1186 | } | ||
| 1187 | 8583 | av_freep(&fg->outputs); | |
| 1188 | 8583 | av_freep(&fg->graph_desc); | |
| 1189 | |||
| 1190 | 8583 | av_frame_free(&fgp->frame); | |
| 1191 | 8583 | av_frame_free(&fgp->frame_enc); | |
| 1192 | |||
| 1193 | 8583 | av_freep(pfg); | |
| 1194 | } | ||
| 1195 | |||
| 1196 | 327 | static const char *fg_item_name(void *obj) | |
| 1197 | { | ||
| 1198 | 327 | const FilterGraphPriv *fgp = obj; | |
| 1199 | |||
| 1200 | 327 | return fgp->log_name; | |
| 1201 | } | ||
| 1202 | |||
| 1203 | static const AVClass fg_class = { | ||
| 1204 | .class_name = "FilterGraph", | ||
| 1205 | .version = LIBAVUTIL_VERSION_INT, | ||
| 1206 | .item_name = fg_item_name, | ||
| 1207 | .category = AV_CLASS_CATEGORY_FILTER, | ||
| 1208 | }; | ||
| 1209 | |||
| 1210 | 8583 | int fg_create(FilterGraph **pfg, char **graph_desc, Scheduler *sch, | |
| 1211 | const OutputFilterOptions *opts) | ||
| 1212 | { | ||
| 1213 | FilterGraphPriv *fgp; | ||
| 1214 | FilterGraph *fg; | ||
| 1215 | |||
| 1216 | AVFilterInOut *inputs, *outputs; | ||
| 1217 | AVFilterGraph *graph; | ||
| 1218 | 8583 | int ret = 0; | |
| 1219 | |||
| 1220 | 8583 | fgp = av_mallocz(sizeof(*fgp)); | |
| 1221 | 8583 | if (!fgp) { | |
| 1222 | ✗ | av_freep(graph_desc); | |
| 1223 | ✗ | return AVERROR(ENOMEM); | |
| 1224 | } | ||
| 1225 | 8583 | fg = &fgp->fg; | |
| 1226 | |||
| 1227 | 8583 | if (pfg) { | |
| 1228 | 7244 | *pfg = fg; | |
| 1229 | 7244 | fg->index = -1; | |
| 1230 | } else { | ||
| 1231 | 1339 | ret = av_dynarray_add_nofree(&filtergraphs, &nb_filtergraphs, fgp); | |
| 1232 | 1339 | if (ret < 0) { | |
| 1233 | ✗ | av_freep(graph_desc); | |
| 1234 | ✗ | av_freep(&fgp); | |
| 1235 | ✗ | return ret; | |
| 1236 | } | ||
| 1237 | |||
| 1238 | 1339 | fg->index = nb_filtergraphs - 1; | |
| 1239 | } | ||
| 1240 | |||
| 1241 | 8583 | fg->class = &fg_class; | |
| 1242 | 8583 | fg->graph_desc = *graph_desc; | |
| 1243 | 8583 | fgp->disable_conversions = !auto_conversion_filters; | |
| 1244 | 8583 | fgp->nb_threads = -1; | |
| 1245 | 8583 | fgp->sch = sch; | |
| 1246 | |||
| 1247 | 8583 | *graph_desc = NULL; | |
| 1248 | |||
| 1249 | 8583 | snprintf(fgp->log_name, sizeof(fgp->log_name), "fc#%d", fg->index); | |
| 1250 | |||
| 1251 | 8583 | fgp->frame = av_frame_alloc(); | |
| 1252 | 8583 | fgp->frame_enc = av_frame_alloc(); | |
| 1253 | 8583 | if (!fgp->frame || !fgp->frame_enc) | |
| 1254 | ✗ | return AVERROR(ENOMEM); | |
| 1255 | |||
| 1256 | /* this graph is only used for determining the kinds of inputs | ||
| 1257 | * and outputs we have, and is discarded on exit from this function */ | ||
| 1258 | 8583 | graph = avfilter_graph_alloc(); | |
| 1259 | 8583 | if (!graph) | |
| 1260 | ✗ | return AVERROR(ENOMEM);; | |
| 1261 | 8583 | graph->nb_threads = 1; | |
| 1262 | |||
| 1263 | 8583 | ret = graph_parse(fg, graph, fg->graph_desc, &inputs, &outputs, | |
| 1264 | hw_device_for_filter()); | ||
| 1265 | 8583 | if (ret < 0) | |
| 1266 | 1 | goto fail; | |
| 1267 | |||
| 1268 | 16051 | for (AVFilterInOut *cur = inputs; cur; cur = cur->next) { | |
| 1269 | 7469 | InputFilter *const ifilter = ifilter_alloc(fg); | |
| 1270 | |||
| 1271 | 7469 | if (!ifilter) { | |
| 1272 | ✗ | ret = AVERROR(ENOMEM); | |
| 1273 | ✗ | goto fail; | |
| 1274 | } | ||
| 1275 | |||
| 1276 | 7469 | ifilter->linklabel = cur->name; | |
| 1277 | 7469 | cur->name = NULL; | |
| 1278 | |||
| 1279 | 7469 | ifilter->type = avfilter_pad_get_type(cur->filter_ctx->input_pads, | |
| 1280 | cur->pad_idx); | ||
| 1281 | |||
| 1282 | 7469 | if (ifilter->type != AVMEDIA_TYPE_VIDEO && ifilter->type != AVMEDIA_TYPE_AUDIO) { | |
| 1283 | ✗ | av_log(fg, AV_LOG_FATAL, "Only video and audio filters supported " | |
| 1284 | "currently.\n"); | ||
| 1285 | ✗ | ret = AVERROR(ENOSYS); | |
| 1286 | ✗ | goto fail; | |
| 1287 | } | ||
| 1288 | |||
| 1289 | 7469 | ifilter->name = describe_filter_link(fg, cur, 1); | |
| 1290 | 7469 | if (!ifilter->name) { | |
| 1291 | ✗ | ret = AVERROR(ENOMEM); | |
| 1292 | ✗ | goto fail; | |
| 1293 | } | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | 17399 | for (AVFilterInOut *cur = outputs; cur; cur = cur->next) { | |
| 1297 | 8817 | const enum AVMediaType type = avfilter_pad_get_type(cur->filter_ctx->output_pads, | |
| 1298 | cur->pad_idx); | ||
| 1299 | 8817 | OutputFilter *const ofilter = ofilter_alloc(fg, type); | |
| 1300 | OutputFilterPriv *ofp; | ||
| 1301 | |||
| 1302 | 8817 | if (!ofilter) { | |
| 1303 | ✗ | ret = AVERROR(ENOMEM); | |
| 1304 | ✗ | goto fail; | |
| 1305 | } | ||
| 1306 | 8817 | ofp = ofp_from_ofilter(ofilter); | |
| 1307 | |||
| 1308 | 8817 | ofilter->linklabel = cur->name; | |
| 1309 | 8817 | cur->name = NULL; | |
| 1310 | |||
| 1311 | 8817 | ofilter->name = describe_filter_link(fg, cur, 0); | |
| 1312 | 8817 | if (!ofilter->name) { | |
| 1313 | ✗ | ret = AVERROR(ENOMEM); | |
| 1314 | ✗ | goto fail; | |
| 1315 | } | ||
| 1316 | |||
| 1317 | // opts should only be needed in this function to fill fields from filtergraphs | ||
| 1318 | // whose output is meant to be treated as if it was stream, e.g. merged HEIF | ||
| 1319 | // tile groups. | ||
| 1320 | 8817 | if (opts) { | |
| 1321 | 11 | ofp->flags = opts->flags; | |
| 1322 | 11 | ofp->side_data = opts->side_data; | |
| 1323 | 11 | ofp->nb_side_data = opts->nb_side_data; | |
| 1324 | |||
| 1325 | 11 | ofp->crop_top = opts->crop_top; | |
| 1326 | 11 | ofp->crop_bottom = opts->crop_bottom; | |
| 1327 | 11 | ofp->crop_left = opts->crop_left; | |
| 1328 | 11 | ofp->crop_right = opts->crop_right; | |
| 1329 | |||
| 1330 | 11 | const AVFrameSideData *sd = av_frame_side_data_get(ofp->side_data, ofp->nb_side_data, | |
| 1331 | AV_FRAME_DATA_DISPLAYMATRIX); | ||
| 1332 | 11 | if (sd) | |
| 1333 | ✗ | memcpy(ofp->displaymatrix, sd->data, sizeof(ofp->displaymatrix)); | |
| 1334 | } | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | 8582 | if (!fg->nb_outputs) { | |
| 1338 | ✗ | av_log(fg, AV_LOG_FATAL, "A filtergraph has zero outputs, this is not supported\n"); | |
| 1339 | ✗ | ret = AVERROR(ENOSYS); | |
| 1340 | ✗ | goto fail; | |
| 1341 | } | ||
| 1342 | |||
| 1343 | 8582 | ret = sch_add_filtergraph(sch, fg->nb_inputs, fg->nb_outputs, | |
| 1344 | filter_thread, fgp); | ||
| 1345 | 8582 | if (ret < 0) | |
| 1346 | ✗ | goto fail; | |
| 1347 | 8582 | fgp->sch_idx = ret; | |
| 1348 | |||
| 1349 | 8583 | fail: | |
| 1350 | 8583 | avfilter_inout_free(&inputs); | |
| 1351 | 8583 | avfilter_inout_free(&outputs); | |
| 1352 | 8583 | avfilter_graph_free(&graph); | |
| 1353 | |||
| 1354 | 8583 | if (ret < 0) | |
| 1355 | 1 | return ret; | |
| 1356 | |||
| 1357 | 8582 | return 0; | |
| 1358 | } | ||
| 1359 | |||
| 1360 | 7244 | int fg_create_simple(FilterGraph **pfg, | |
| 1361 | InputStream *ist, | ||
| 1362 | char **graph_desc, | ||
| 1363 | Scheduler *sch, unsigned sched_idx_enc, | ||
| 1364 | const OutputFilterOptions *opts) | ||
| 1365 | { | ||
| 1366 | 7244 | const enum AVMediaType type = ist->par->codec_type; | |
| 1367 | FilterGraph *fg; | ||
| 1368 | FilterGraphPriv *fgp; | ||
| 1369 | int ret; | ||
| 1370 | |||
| 1371 | 7244 | ret = fg_create(pfg, graph_desc, sch, NULL); | |
| 1372 | 7244 | if (ret < 0) | |
| 1373 | 1 | return ret; | |
| 1374 | 7243 | fg = *pfg; | |
| 1375 | 7243 | fgp = fgp_from_fg(fg); | |
| 1376 | |||
| 1377 | 7243 | fgp->is_simple = 1; | |
| 1378 | |||
| 1379 | 7243 | snprintf(fgp->log_name, sizeof(fgp->log_name), "%cf%s", | |
| 1380 | 7243 | av_get_media_type_string(type)[0], opts->name); | |
| 1381 | |||
| 1382 | 7243 | if (fg->nb_inputs != 1 || fg->nb_outputs != 1) { | |
| 1383 | ✗ | av_log(fg, AV_LOG_ERROR, "Simple filtergraph '%s' was expected " | |
| 1384 | "to have exactly 1 input and 1 output. " | ||
| 1385 | "However, it had %d input(s) and %d output(s). Please adjust, " | ||
| 1386 | "or use a complex filtergraph (-filter_complex) instead.\n", | ||
| 1387 | *graph_desc, fg->nb_inputs, fg->nb_outputs); | ||
| 1388 | ✗ | return AVERROR(EINVAL); | |
| 1389 | } | ||
| 1390 | 7243 | if (fg->outputs[0]->type != type) { | |
| 1391 | ✗ | av_log(fg, AV_LOG_ERROR, "Filtergraph has a %s output, cannot connect " | |
| 1392 | "it to %s output stream\n", | ||
| 1393 | ✗ | av_get_media_type_string(fg->outputs[0]->type), | |
| 1394 | av_get_media_type_string(type)); | ||
| 1395 | ✗ | return AVERROR(EINVAL); | |
| 1396 | } | ||
| 1397 | |||
| 1398 | 7243 | ret = ifilter_bind_ist(fg->inputs[0], ist, opts->vs); | |
| 1399 | 7243 | if (ret < 0) | |
| 1400 | ✗ | return ret; | |
| 1401 | |||
| 1402 | 7243 | ret = ofilter_bind_enc(fg->outputs[0], sched_idx_enc, opts); | |
| 1403 | 7243 | if (ret < 0) | |
| 1404 | ✗ | return ret; | |
| 1405 | |||
| 1406 | 7243 | if (opts->nb_threads >= 0) | |
| 1407 | 4423 | fgp->nb_threads = opts->nb_threads; | |
| 1408 | |||
| 1409 | 7243 | return 0; | |
| 1410 | } | ||
| 1411 | |||
| 1412 | 449 | static int fg_complex_bind_input(FilterGraph *fg, InputFilter *ifilter, int commit) | |
| 1413 | { | ||
| 1414 | 449 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 1415 | 449 | InputStream *ist = NULL; | |
| 1416 | 449 | enum AVMediaType type = ifilter->type; | |
| 1417 | 449 | ViewSpecifier vs = { .type = VIEW_SPECIFIER_TYPE_NONE }; | |
| 1418 | const char *spec; | ||
| 1419 | char *p; | ||
| 1420 | int i, ret; | ||
| 1421 | |||
| 1422 | 449 | if (ifilter->linklabel && !strncmp(ifilter->linklabel, "dec:", 4)) { | |
| 1423 | // bind to a standalone decoder | ||
| 1424 | int dec_idx; | ||
| 1425 | |||
| 1426 | 1 | dec_idx = strtol(ifilter->linklabel + 4, &p, 0); | |
| 1427 | 1 | if (dec_idx < 0 || dec_idx >= nb_decoders) { | |
| 1428 | ✗ | av_log(fg, AV_LOG_ERROR, "Invalid decoder index %d in filtergraph description %s\n", | |
| 1429 | dec_idx, fg->graph_desc); | ||
| 1430 | ✗ | return AVERROR(EINVAL); | |
| 1431 | } | ||
| 1432 | |||
| 1433 | 1 | if (type == AVMEDIA_TYPE_VIDEO) { | |
| 1434 | 1 | spec = *p == ':' ? p + 1 : p; | |
| 1435 | 1 | ret = view_specifier_parse(&spec, &vs); | |
| 1436 | 1 | if (ret < 0) | |
| 1437 | ✗ | return ret; | |
| 1438 | } | ||
| 1439 | |||
| 1440 | 1 | ret = ifilter_bind_dec(ifp, decoders[dec_idx], &vs); | |
| 1441 | 1 | if (ret < 0) | |
| 1442 | ✗ | av_log(fg, AV_LOG_ERROR, "Error binding a decoder to filtergraph input %s\n", | |
| 1443 | ifilter->name); | ||
| 1444 | 1 | return ret; | |
| 1445 | 448 | } else if (ifilter->linklabel) { | |
| 1446 | StreamSpecifier ss; | ||
| 1447 | AVFormatContext *s; | ||
| 1448 | 334 | AVStream *st = NULL; | |
| 1449 | int file_idx; | ||
| 1450 | |||
| 1451 | // try finding an unbound filtergraph output with this label | ||
| 1452 | 747 | for (int i = 0; i < nb_filtergraphs; i++) { | |
| 1453 | 417 | FilterGraph *fg_src = filtergraphs[i]; | |
| 1454 | |||
| 1455 | 417 | if (fg == fg_src) | |
| 1456 | 332 | continue; | |
| 1457 | |||
| 1458 | 166 | for (int j = 0; j < fg_src->nb_outputs; j++) { | |
| 1459 | 85 | OutputFilter *ofilter = fg_src->outputs[j]; | |
| 1460 | |||
| 1461 | 85 | if (!ofilter->bound && ofilter->linklabel && | |
| 1462 | 77 | !strcmp(ofilter->linklabel, ifilter->linklabel)) { | |
| 1463 | 4 | if (commit) { | |
| 1464 | 1 | av_log(fg, AV_LOG_VERBOSE, | |
| 1465 | "Binding input with label '%s' to filtergraph output %d:%d\n", | ||
| 1466 | ifilter->linklabel, i, j); | ||
| 1467 | |||
| 1468 | 1 | ret = ifilter_bind_fg(ifp, fg_src, j); | |
| 1469 | 1 | if (ret < 0) { | |
| 1470 | ✗ | av_log(fg, AV_LOG_ERROR, "Error binding filtergraph input %s\n", | |
| 1471 | ifilter->linklabel); | ||
| 1472 | 4 | return ret; | |
| 1473 | } | ||
| 1474 | } else | ||
| 1475 | 3 | ofp_from_ofilter(ofilter)->needed = NEEDED_INTERNAL; | |
| 1476 | 4 | return 0; | |
| 1477 | } | ||
| 1478 | } | ||
| 1479 | } | ||
| 1480 | |||
| 1481 | // bind to an explicitly specified demuxer stream | ||
| 1482 | 330 | file_idx = strtol(ifilter->linklabel, &p, 0); | |
| 1483 | 330 | if (file_idx < 0 || file_idx >= nb_input_files) { | |
| 1484 | ✗ | av_log(fg, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n", | |
| 1485 | file_idx, fg->graph_desc); | ||
| 1486 | ✗ | return AVERROR(EINVAL); | |
| 1487 | } | ||
| 1488 | 330 | s = input_files[file_idx]->ctx; | |
| 1489 | |||
| 1490 | 330 | ret = stream_specifier_parse(&ss, *p == ':' ? p + 1 : p, 1, fg); | |
| 1491 | 330 | if (ret < 0) { | |
| 1492 | ✗ | av_log(fg, AV_LOG_ERROR, "Invalid stream specifier: %s\n", p); | |
| 1493 | ✗ | return ret; | |
| 1494 | } | ||
| 1495 | |||
| 1496 | 330 | if (type == AVMEDIA_TYPE_VIDEO) { | |
| 1497 | 156 | spec = ss.remainder ? ss.remainder : ""; | |
| 1498 | 156 | ret = view_specifier_parse(&spec, &vs); | |
| 1499 | 156 | if (ret < 0) { | |
| 1500 | ✗ | stream_specifier_uninit(&ss); | |
| 1501 | ✗ | return ret; | |
| 1502 | } | ||
| 1503 | } | ||
| 1504 | |||
| 1505 | 1009 | for (i = 0; i < s->nb_streams; i++) { | |
| 1506 | 1009 | enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type; | |
| 1507 | 1009 | if (stream_type != type && | |
| 1508 | 10 | !(stream_type == AVMEDIA_TYPE_SUBTITLE && | |
| 1509 | type == AVMEDIA_TYPE_VIDEO /* sub2video hack */)) | ||
| 1510 | 6 | continue; | |
| 1511 | 1003 | if (stream_specifier_match(&ss, s, s->streams[i], fg)) { | |
| 1512 | 330 | st = s->streams[i]; | |
| 1513 | 330 | break; | |
| 1514 | } | ||
| 1515 | } | ||
| 1516 | 330 | stream_specifier_uninit(&ss); | |
| 1517 | 330 | if (!st) { | |
| 1518 | ✗ | av_log(fg, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s " | |
| 1519 | "matches no streams.\n", p, fg->graph_desc); | ||
| 1520 | ✗ | return AVERROR(EINVAL); | |
| 1521 | } | ||
| 1522 | 330 | ist = input_files[file_idx]->streams[st->index]; | |
| 1523 | |||
| 1524 | 330 | if (commit) | |
| 1525 | 129 | av_log(fg, AV_LOG_VERBOSE, | |
| 1526 | "Binding input with label '%s' to input stream %d:%d\n", | ||
| 1527 | 129 | ifilter->linklabel, ist->file->index, ist->index); | |
| 1528 | } else { | ||
| 1529 | // try finding an unbound filtergraph output | ||
| 1530 | 228 | for (int i = 0; i < nb_filtergraphs; i++) { | |
| 1531 | 114 | FilterGraph *fg_src = filtergraphs[i]; | |
| 1532 | |||
| 1533 | 114 | if (fg == fg_src) | |
| 1534 | 114 | continue; | |
| 1535 | |||
| 1536 | ✗ | for (int j = 0; j < fg_src->nb_outputs; j++) { | |
| 1537 | ✗ | OutputFilter *ofilter = fg_src->outputs[j]; | |
| 1538 | |||
| 1539 | ✗ | if (!ofilter->bound) { | |
| 1540 | ✗ | if (commit) { | |
| 1541 | ✗ | av_log(fg, AV_LOG_VERBOSE, | |
| 1542 | "Binding unlabeled filtergraph input to filtergraph output %d:%d\n", i, j); | ||
| 1543 | |||
| 1544 | ✗ | ret = ifilter_bind_fg(ifp, fg_src, j); | |
| 1545 | ✗ | if (ret < 0) { | |
| 1546 | ✗ | av_log(fg, AV_LOG_ERROR, "Error binding filtergraph input %d:%d\n", i, j); | |
| 1547 | ✗ | return ret; | |
| 1548 | } | ||
| 1549 | } else | ||
| 1550 | ✗ | ofp_from_ofilter(ofilter)->needed = NEEDED_INTERNAL; | |
| 1551 | ✗ | return 0; | |
| 1552 | } | ||
| 1553 | } | ||
| 1554 | } | ||
| 1555 | |||
| 1556 | 114 | ist = ist_find_unused(type); | |
| 1557 | 114 | if (!ist) { | |
| 1558 | ✗ | av_log(fg, AV_LOG_FATAL, | |
| 1559 | "Cannot find an unused %s input stream to feed the " | ||
| 1560 | "unlabeled input pad %s.\n", | ||
| 1561 | av_get_media_type_string(type), ifilter->name); | ||
| 1562 | ✗ | return AVERROR(EINVAL); | |
| 1563 | } | ||
| 1564 | |||
| 1565 | 114 | if (commit) | |
| 1566 | 57 | av_log(fg, AV_LOG_VERBOSE, | |
| 1567 | "Binding unlabeled input %d to input stream %d:%d\n", | ||
| 1568 | 57 | ifilter->index, ist->file->index, ist->index); | |
| 1569 | } | ||
| 1570 | 444 | av_assert0(ist); | |
| 1571 | |||
| 1572 | 444 | if (commit) { | |
| 1573 | 186 | ret = ifilter_bind_ist(ifilter, ist, &vs); | |
| 1574 | 186 | if (ret < 0) { | |
| 1575 | ✗ | av_log(fg, AV_LOG_ERROR, | |
| 1576 | "Error binding an input stream to complex filtergraph input %s.\n", | ||
| 1577 | ifilter->name); | ||
| 1578 | ✗ | return ret; | |
| 1579 | } | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | 444 | return 0; | |
| 1583 | } | ||
| 1584 | |||
| 1585 | 2674 | static int bind_inputs(FilterGraph *fg, int commit) | |
| 1586 | { | ||
| 1587 | // bind filtergraph inputs to input streams or other filtergraphs | ||
| 1588 | 3124 | for (int i = 0; i < fg->nb_inputs; i++) { | |
| 1589 | 450 | InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]); | |
| 1590 | int ret; | ||
| 1591 | |||
| 1592 | 450 | if (ifp->bound) | |
| 1593 | 1 | continue; | |
| 1594 | |||
| 1595 | 449 | ret = fg_complex_bind_input(fg, &ifp->ifilter, commit); | |
| 1596 | 449 | if (ret < 0) | |
| 1597 | ✗ | return ret; | |
| 1598 | } | ||
| 1599 | |||
| 1600 | 2674 | return 0; | |
| 1601 | } | ||
| 1602 | |||
| 1603 | 9215 | int fg_finalise_bindings(void) | |
| 1604 | { | ||
| 1605 | int ret; | ||
| 1606 | |||
| 1607 | 9222 | retry: | |
| 1608 | 10564 | for (int i = 0; i < nb_filtergraphs; i++) { | |
| 1609 | 1342 | FilterGraph *fg = filtergraphs[i]; | |
| 1610 | 2919 | for (int j = 0; j < fg->nb_outputs; j++) { | |
| 1611 | 1577 | OutputFilter *output = fg->outputs[j]; | |
| 1612 | 1577 | ofp_from_ofilter(output)->needed &= ~NEEDED_INTERNAL; | |
| 1613 | } | ||
| 1614 | } | ||
| 1615 | 10564 | for (int i = 0; i < nb_filtergraphs; i++) { | |
| 1616 | 1342 | ret = bind_inputs(filtergraphs[i], 0); | |
| 1617 | 1342 | if (ret < 0) | |
| 1618 | ✗ | return ret; | |
| 1619 | } | ||
| 1620 | |||
| 1621 | // check that all outputs were marked as needed to be bound | ||
| 1622 | 10554 | for (int i = nb_filtergraphs - 1; i >= 0; i--) { | |
| 1623 | 1339 | FilterGraph *fg = filtergraphs[i]; | |
| 1624 | 1339 | FilterGraphPriv *fgp = fgp_from_fg(filtergraphs[i]); | |
| 1625 | |||
| 1626 | 2906 | for (int j = 0; j < fg->nb_outputs; j++) { | |
| 1627 | 1574 | OutputFilter *output = fg->outputs[j]; | |
| 1628 | 1574 | if (!ofp_from_ofilter(output)->needed) { | |
| 1629 | 7 | if (!fg->is_internal) { | |
| 1630 | ✗ | av_log(fg, AV_LOG_FATAL, | |
| 1631 | "Filter '%s' has output %d (%s) unconnected\n", | ||
| 1632 | output->name, j, | ||
| 1633 | ✗ | output->linklabel ? (const char *)output->linklabel : "unlabeled"); | |
| 1634 | ✗ | return AVERROR(EINVAL); | |
| 1635 | } | ||
| 1636 | |||
| 1637 | 7 | av_log(fg, AV_LOG_DEBUG, | |
| 1638 | "Internal filter '%s' has output %d (%s) unconnected. Removing graph\n", | ||
| 1639 | output->name, j, | ||
| 1640 | 7 | output->linklabel ? (const char *)output->linklabel : "unlabeled"); | |
| 1641 | 7 | sch_remove_filtergraph(fgp->sch, fgp->sch_idx); | |
| 1642 | 7 | fg_free(&filtergraphs[i]); | |
| 1643 | 7 | nb_filtergraphs--; | |
| 1644 | 7 | if (nb_filtergraphs > 0) | |
| 1645 | 2 | memmove(&filtergraphs[i], | |
| 1646 | 2 | &filtergraphs[i + 1], | |
| 1647 | 2 | (nb_filtergraphs - i) * sizeof(*filtergraphs)); | |
| 1648 | 7 | goto retry; | |
| 1649 | } | ||
| 1650 | } | ||
| 1651 | } | ||
| 1652 | |||
| 1653 | 10547 | for (int i = 0; i < nb_filtergraphs; i++) { | |
| 1654 | 1332 | ret = bind_inputs(filtergraphs[i], 1); | |
| 1655 | 1332 | if (ret < 0) | |
| 1656 | ✗ | return ret; | |
| 1657 | } | ||
| 1658 | |||
| 1659 | 9215 | return 0; | |
| 1660 | } | ||
| 1661 | |||
| 1662 | 16853 | static int insert_trim(void *logctx, int64_t start_time, int64_t duration, | |
| 1663 | AVFilterContext **last_filter, int *pad_idx, | ||
| 1664 | const char *filter_name) | ||
| 1665 | { | ||
| 1666 | 16853 | AVFilterGraph *graph = (*last_filter)->graph; | |
| 1667 | AVFilterContext *ctx; | ||
| 1668 | const AVFilter *trim; | ||
| 1669 | 16853 | enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx); | |
| 1670 | 16853 | const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim"; | |
| 1671 | 16853 | int ret = 0; | |
| 1672 | |||
| 1673 | 16853 | if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE) | |
| 1674 | 15413 | return 0; | |
| 1675 | |||
| 1676 | 1440 | trim = avfilter_get_by_name(name); | |
| 1677 | 1440 | if (!trim) { | |
| 1678 | ✗ | av_log(logctx, AV_LOG_ERROR, "%s filter not present, cannot limit " | |
| 1679 | "recording time.\n", name); | ||
| 1680 | ✗ | return AVERROR_FILTER_NOT_FOUND; | |
| 1681 | } | ||
| 1682 | |||
| 1683 | 1440 | ctx = avfilter_graph_alloc_filter(graph, trim, filter_name); | |
| 1684 | 1440 | if (!ctx) | |
| 1685 | ✗ | return AVERROR(ENOMEM); | |
| 1686 | |||
| 1687 | 1440 | if (duration != INT64_MAX) { | |
| 1688 | 1415 | ret = av_opt_set_int(ctx, "durationi", duration, | |
| 1689 | AV_OPT_SEARCH_CHILDREN); | ||
| 1690 | } | ||
| 1691 | 1440 | if (ret >= 0 && start_time != AV_NOPTS_VALUE) { | |
| 1692 | 42 | ret = av_opt_set_int(ctx, "starti", start_time, | |
| 1693 | AV_OPT_SEARCH_CHILDREN); | ||
| 1694 | } | ||
| 1695 | 1440 | if (ret < 0) { | |
| 1696 | ✗ | av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name); | |
| 1697 | ✗ | return ret; | |
| 1698 | } | ||
| 1699 | |||
| 1700 | 1440 | ret = avfilter_init_str(ctx, NULL); | |
| 1701 | 1440 | if (ret < 0) | |
| 1702 | ✗ | return ret; | |
| 1703 | |||
| 1704 | 1440 | ret = avfilter_link(*last_filter, *pad_idx, ctx, 0); | |
| 1705 | 1440 | if (ret < 0) | |
| 1706 | ✗ | return ret; | |
| 1707 | |||
| 1708 | 1440 | *last_filter = ctx; | |
| 1709 | 1440 | *pad_idx = 0; | |
| 1710 | 1440 | return 0; | |
| 1711 | } | ||
| 1712 | |||
| 1713 | 24 | static int insert_filter(AVFilterContext **last_filter, int *pad_idx, | |
| 1714 | const char *filter_name, const char *args) | ||
| 1715 | { | ||
| 1716 | 24 | AVFilterGraph *graph = (*last_filter)->graph; | |
| 1717 | 24 | const AVFilter *filter = avfilter_get_by_name(filter_name); | |
| 1718 | AVFilterContext *ctx; | ||
| 1719 | int ret; | ||
| 1720 | |||
| 1721 | 24 | if (!filter) | |
| 1722 | ✗ | return AVERROR_BUG; | |
| 1723 | |||
| 1724 | 24 | ret = avfilter_graph_create_filter(&ctx, | |
| 1725 | filter, | ||
| 1726 | filter_name, args, NULL, graph); | ||
| 1727 | 24 | if (ret < 0) | |
| 1728 | ✗ | return ret; | |
| 1729 | |||
| 1730 | 24 | ret = avfilter_link(*last_filter, *pad_idx, ctx, 0); | |
| 1731 | 24 | if (ret < 0) | |
| 1732 | ✗ | return ret; | |
| 1733 | |||
| 1734 | 24 | *last_filter = ctx; | |
| 1735 | 24 | *pad_idx = 0; | |
| 1736 | 24 | return 0; | |
| 1737 | } | ||
| 1738 | |||
| 1739 | 7414 | static int configure_output_video_filter(FilterGraphPriv *fgp, AVFilterGraph *graph, | |
| 1740 | OutputFilter *ofilter, AVFilterInOut *out) | ||
| 1741 | { | ||
| 1742 | 7414 | OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); | |
| 1743 | 7414 | AVFilterContext *last_filter = out->filter_ctx; | |
| 1744 | AVBPrint bprint; | ||
| 1745 | 7414 | int pad_idx = out->pad_idx; | |
| 1746 | int ret; | ||
| 1747 | char name[255]; | ||
| 1748 | |||
| 1749 | 7414 | snprintf(name, sizeof(name), "out_%s", ofilter->output_name); | |
| 1750 | 7414 | ret = avfilter_graph_create_filter(&ofilter->filter, | |
| 1751 | avfilter_get_by_name("buffersink"), | ||
| 1752 | name, NULL, NULL, graph); | ||
| 1753 | |||
| 1754 | 7414 | if (ret < 0) | |
| 1755 | ✗ | return ret; | |
| 1756 | |||
| 1757 | 7414 | if (ofp->flags & OFILTER_FLAG_CROP) { | |
| 1758 | char crop_buf[64]; | ||
| 1759 | 3 | snprintf(crop_buf, sizeof(crop_buf), "w=iw-%u-%u:h=ih-%u-%u:x=%u:y=%u", | |
| 1760 | ofp->crop_left, ofp->crop_right, | ||
| 1761 | ofp->crop_top, ofp->crop_bottom, | ||
| 1762 | ofp->crop_left, ofp->crop_top); | ||
| 1763 | 3 | ret = insert_filter(&last_filter, &pad_idx, "crop", crop_buf); | |
| 1764 | 3 | if (ret < 0) | |
| 1765 | ✗ | return ret; | |
| 1766 | } | ||
| 1767 | |||
| 1768 | 7414 | if (ofp->flags & OFILTER_FLAG_AUTOROTATE) { | |
| 1769 | 3 | int32_t *displaymatrix = ofp->displaymatrix; | |
| 1770 | double theta; | ||
| 1771 | |||
| 1772 | 3 | theta = get_rotation(displaymatrix); | |
| 1773 | |||
| 1774 | 3 | if (fabs(theta - 90) < 1.0) { | |
| 1775 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "transpose", | |
| 1776 | ✗ | displaymatrix[3] > 0 ? "cclock_flip" : "clock"); | |
| 1777 | 3 | } else if (fabs(theta - 180) < 1.0) { | |
| 1778 | ✗ | if (displaymatrix[0] < 0) { | |
| 1779 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL); | |
| 1780 | ✗ | if (ret < 0) | |
| 1781 | ✗ | return ret; | |
| 1782 | } | ||
| 1783 | ✗ | if (displaymatrix[4] < 0) { | |
| 1784 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL); | |
| 1785 | } | ||
| 1786 | 3 | } else if (fabs(theta - 270) < 1.0) { | |
| 1787 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "transpose", | |
| 1788 | ✗ | displaymatrix[3] < 0 ? "clock_flip" : "cclock"); | |
| 1789 | 3 | } else if (fabs(theta) > 1.0) { | |
| 1790 | char rotate_buf[64]; | ||
| 1791 | ✗ | snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta); | |
| 1792 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf); | |
| 1793 | 3 | } else if (fabs(theta) < 1.0) { | |
| 1794 | ✗ | if (displaymatrix && displaymatrix[4] < 0) { | |
| 1795 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL); | |
| 1796 | } | ||
| 1797 | } | ||
| 1798 | 3 | if (ret < 0) | |
| 1799 | ✗ | return ret; | |
| 1800 | |||
| 1801 | 3 | av_frame_side_data_remove(&ofp->side_data, &ofp->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX); | |
| 1802 | } | ||
| 1803 | |||
| 1804 | 7414 | if ((ofp->width || ofp->height) && (ofp->flags & OFILTER_FLAG_AUTOSCALE) && | |
| 1805 | // skip add scale for hardware format | ||
| 1806 | 1653 | !(ofp->format != AV_PIX_FMT_NONE && | |
| 1807 | 1628 | av_pix_fmt_desc_get(ofp->format)->flags & AV_PIX_FMT_FLAG_HWACCEL)) { | |
| 1808 | char args[255]; | ||
| 1809 | AVFilterContext *filter; | ||
| 1810 | 1653 | const AVDictionaryEntry *e = NULL; | |
| 1811 | |||
| 1812 | 1653 | snprintf(args, sizeof(args), "%d:%d", | |
| 1813 | ofp->width, ofp->height); | ||
| 1814 | |||
| 1815 | 3035 | while ((e = av_dict_iterate(ofp->sws_opts, e))) { | |
| 1816 | 1382 | av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value); | |
| 1817 | } | ||
| 1818 | |||
| 1819 | 1653 | snprintf(name, sizeof(name), "scaler_out_%s", ofilter->output_name); | |
| 1820 | 1653 | if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"), | |
| 1821 | name, args, NULL, graph)) < 0) | ||
| 1822 | ✗ | return ret; | |
| 1823 | 1653 | if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0) | |
| 1824 | ✗ | return ret; | |
| 1825 | |||
| 1826 | 1653 | last_filter = filter; | |
| 1827 | 1653 | pad_idx = 0; | |
| 1828 | } | ||
| 1829 | |||
| 1830 | 7414 | av_assert0(!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT) || | |
| 1831 | ofp->format != AV_PIX_FMT_NONE || !ofp->pix_fmts); | ||
| 1832 | 7414 | av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 1833 | 7414 | choose_pix_fmts(ofp, &bprint); | |
| 1834 | 7414 | choose_color_spaces(ofp, &bprint); | |
| 1835 | 7414 | choose_color_ranges(ofp, &bprint); | |
| 1836 | 7414 | choose_alpha_modes(ofp, &bprint); | |
| 1837 | 7414 | choose_chroma_locations(ofp, &bprint); | |
| 1838 | 7414 | if (!av_bprint_is_complete(&bprint)) | |
| 1839 | ✗ | return AVERROR(ENOMEM); | |
| 1840 | |||
| 1841 | 7414 | if (bprint.len) { | |
| 1842 | AVFilterContext *filter; | ||
| 1843 | |||
| 1844 | 6173 | ret = avfilter_graph_create_filter(&filter, | |
| 1845 | avfilter_get_by_name("format"), | ||
| 1846 | 6173 | "format", bprint.str, NULL, graph); | |
| 1847 | 6173 | av_bprint_finalize(&bprint, NULL); | |
| 1848 | 6173 | if (ret < 0) | |
| 1849 | ✗ | return ret; | |
| 1850 | 6173 | if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0) | |
| 1851 | ✗ | return ret; | |
| 1852 | |||
| 1853 | 6173 | last_filter = filter; | |
| 1854 | 6173 | pad_idx = 0; | |
| 1855 | } | ||
| 1856 | |||
| 1857 | 7414 | snprintf(name, sizeof(name), "trim_out_%s", ofilter->output_name); | |
| 1858 | 7414 | ret = insert_trim(fgp, ofp->trim_start_us, ofp->trim_duration_us, | |
| 1859 | &last_filter, &pad_idx, name); | ||
| 1860 | 7414 | if (ret < 0) | |
| 1861 | ✗ | return ret; | |
| 1862 | |||
| 1863 | |||
| 1864 | 7414 | if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0) | |
| 1865 | ✗ | return ret; | |
| 1866 | |||
| 1867 | 7414 | return 0; | |
| 1868 | } | ||
| 1869 | |||
| 1870 | 1702 | static int configure_output_audio_filter(FilterGraphPriv *fgp, AVFilterGraph *graph, | |
| 1871 | OutputFilter *ofilter, AVFilterInOut *out) | ||
| 1872 | { | ||
| 1873 | 1702 | OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); | |
| 1874 | 1702 | AVFilterContext *last_filter = out->filter_ctx; | |
| 1875 | 1702 | int pad_idx = out->pad_idx; | |
| 1876 | AVBPrint args; | ||
| 1877 | char name[255]; | ||
| 1878 | int ret; | ||
| 1879 | |||
| 1880 | 1702 | snprintf(name, sizeof(name), "out_%s", ofilter->output_name); | |
| 1881 | 1702 | ret = avfilter_graph_create_filter(&ofilter->filter, | |
| 1882 | avfilter_get_by_name("abuffersink"), | ||
| 1883 | name, NULL, NULL, graph); | ||
| 1884 | 1702 | if (ret < 0) | |
| 1885 | ✗ | return ret; | |
| 1886 | |||
| 1887 | #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \ | ||
| 1888 | AVFilterContext *filt_ctx; \ | ||
| 1889 | \ | ||
| 1890 | av_log(ofilter, AV_LOG_INFO, opt_name " is forwarded to lavfi " \ | ||
| 1891 | "similarly to -af " filter_name "=%s.\n", arg); \ | ||
| 1892 | \ | ||
| 1893 | ret = avfilter_graph_create_filter(&filt_ctx, \ | ||
| 1894 | avfilter_get_by_name(filter_name), \ | ||
| 1895 | filter_name, arg, NULL, graph); \ | ||
| 1896 | if (ret < 0) \ | ||
| 1897 | goto fail; \ | ||
| 1898 | \ | ||
| 1899 | ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \ | ||
| 1900 | if (ret < 0) \ | ||
| 1901 | goto fail; \ | ||
| 1902 | \ | ||
| 1903 | last_filter = filt_ctx; \ | ||
| 1904 | pad_idx = 0; \ | ||
| 1905 | } while (0) | ||
| 1906 | 1702 | av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 1907 | |||
| 1908 | 1702 | choose_sample_fmts(ofp, &args); | |
| 1909 | 1702 | choose_sample_rates(ofp, &args); | |
| 1910 | 1702 | choose_channel_layouts(ofp, &args); | |
| 1911 | 1702 | if (!av_bprint_is_complete(&args)) { | |
| 1912 | ✗ | ret = AVERROR(ENOMEM); | |
| 1913 | ✗ | goto fail; | |
| 1914 | } | ||
| 1915 | 1702 | if (args.len) { | |
| 1916 | AVFilterContext *format; | ||
| 1917 | |||
| 1918 | 1702 | snprintf(name, sizeof(name), "format_out_%s", ofilter->output_name); | |
| 1919 | 1702 | ret = avfilter_graph_create_filter(&format, | |
| 1920 | avfilter_get_by_name("aformat"), | ||
| 1921 | 1702 | name, args.str, NULL, graph); | |
| 1922 | 1702 | if (ret < 0) | |
| 1923 | ✗ | goto fail; | |
| 1924 | |||
| 1925 | 1702 | ret = avfilter_link(last_filter, pad_idx, format, 0); | |
| 1926 | 1702 | if (ret < 0) | |
| 1927 | ✗ | goto fail; | |
| 1928 | |||
| 1929 | 1702 | last_filter = format; | |
| 1930 | 1702 | pad_idx = 0; | |
| 1931 | } | ||
| 1932 | |||
| 1933 | 1702 | if (ofilter->apad) | |
| 1934 | ✗ | AUTO_INSERT_FILTER("-apad", "apad", ofilter->apad); | |
| 1935 | |||
| 1936 | 1702 | snprintf(name, sizeof(name), "trim for output %s", ofilter->output_name); | |
| 1937 | 1702 | ret = insert_trim(fgp, ofp->trim_start_us, ofp->trim_duration_us, | |
| 1938 | &last_filter, &pad_idx, name); | ||
| 1939 | 1702 | if (ret < 0) | |
| 1940 | ✗ | goto fail; | |
| 1941 | |||
| 1942 | 1702 | if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0) | |
| 1943 | ✗ | goto fail; | |
| 1944 | 1702 | fail: | |
| 1945 | 1702 | av_bprint_finalize(&args, NULL); | |
| 1946 | |||
| 1947 | 1702 | return ret; | |
| 1948 | } | ||
| 1949 | |||
| 1950 | 9116 | static int configure_output_filter(FilterGraphPriv *fgp, AVFilterGraph *graph, | |
| 1951 | OutputFilter *ofilter, AVFilterInOut *out) | ||
| 1952 | { | ||
| 1953 | 9116 | switch (ofilter->type) { | |
| 1954 | 7414 | case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fgp, graph, ofilter, out); | |
| 1955 | 1702 | case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fgp, graph, ofilter, out); | |
| 1956 | ✗ | default: av_assert0(0); return 0; | |
| 1957 | } | ||
| 1958 | } | ||
| 1959 | |||
| 1960 | 4 | static void sub2video_prepare(InputFilterPriv *ifp) | |
| 1961 | { | ||
| 1962 | 4 | ifp->sub2video.last_pts = INT64_MIN; | |
| 1963 | 4 | ifp->sub2video.end_pts = INT64_MIN; | |
| 1964 | |||
| 1965 | /* sub2video structure has been (re-)initialized. | ||
| 1966 | Mark it as such so that the system will be | ||
| 1967 | initialized with the first received heartbeat. */ | ||
| 1968 | 4 | ifp->sub2video.initialize = 1; | |
| 1969 | 4 | } | |
| 1970 | |||
| 1971 | 6199 | static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph, | |
| 1972 | InputFilter *ifilter, AVFilterInOut *in) | ||
| 1973 | { | ||
| 1974 | 6199 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 1975 | |||
| 1976 | AVFilterContext *last_filter; | ||
| 1977 | 6199 | const AVFilter *buffer_filt = avfilter_get_by_name("buffer"); | |
| 1978 | const AVPixFmtDescriptor *desc; | ||
| 1979 | char name[255]; | ||
| 1980 | 6199 | int ret, pad_idx = 0; | |
| 1981 | 6199 | AVBufferSrcParameters *par = av_buffersrc_parameters_alloc(); | |
| 1982 | 6199 | if (!par) | |
| 1983 | ✗ | return AVERROR(ENOMEM); | |
| 1984 | |||
| 1985 | 6199 | if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) | |
| 1986 | 4 | sub2video_prepare(ifp); | |
| 1987 | |||
| 1988 | 6199 | snprintf(name, sizeof(name), "graph %d input from stream %s", fg->index, | |
| 1989 | ifp->opts.name); | ||
| 1990 | |||
| 1991 | 6199 | ifilter->filter = avfilter_graph_alloc_filter(graph, buffer_filt, name); | |
| 1992 | 6199 | if (!ifilter->filter) { | |
| 1993 | ✗ | ret = AVERROR(ENOMEM); | |
| 1994 | ✗ | goto fail; | |
| 1995 | } | ||
| 1996 | |||
| 1997 | 6199 | par->format = ifp->format; | |
| 1998 | 6199 | par->time_base = ifp->time_base; | |
| 1999 | 6199 | par->frame_rate = ifp->opts.framerate; | |
| 2000 | 6199 | par->width = ifp->width; | |
| 2001 | 6199 | par->height = ifp->height; | |
| 2002 | 6199 | par->sample_aspect_ratio = ifp->sample_aspect_ratio.den > 0 ? | |
| 2003 | 6199 | ifp->sample_aspect_ratio : (AVRational){ 0, 1 }; | |
| 2004 | 6199 | par->color_space = ifp->color_space; | |
| 2005 | 6199 | par->color_range = ifp->color_range; | |
| 2006 | 6199 | par->alpha_mode = ifp->alpha_mode; | |
| 2007 | 6199 | par->chroma_location = ifp->chroma_location; | |
| 2008 | 6199 | par->hw_frames_ctx = ifp->hw_frames_ctx; | |
| 2009 | 6199 | par->side_data = ifp->side_data; | |
| 2010 | 6199 | par->nb_side_data = ifp->nb_side_data; | |
| 2011 | |||
| 2012 | 6199 | ret = av_buffersrc_parameters_set(ifilter->filter, par); | |
| 2013 | 6199 | if (ret < 0) | |
| 2014 | ✗ | goto fail; | |
| 2015 | 6199 | av_freep(&par); | |
| 2016 | |||
| 2017 | 6199 | ret = avfilter_init_dict(ifilter->filter, NULL); | |
| 2018 | 6199 | if (ret < 0) | |
| 2019 | ✗ | goto fail; | |
| 2020 | |||
| 2021 | 6199 | last_filter = ifilter->filter; | |
| 2022 | |||
| 2023 | 6199 | desc = av_pix_fmt_desc_get(ifp->format); | |
| 2024 | 6199 | av_assert0(desc); | |
| 2025 | |||
| 2026 | 6199 | if ((ifp->opts.flags & IFILTER_FLAG_CROP)) { | |
| 2027 | char crop_buf[64]; | ||
| 2028 | 6 | snprintf(crop_buf, sizeof(crop_buf), "w=iw-%u-%u:h=ih-%u-%u:x=%u:y=%u", | |
| 2029 | ifp->opts.crop_left, ifp->opts.crop_right, | ||
| 2030 | ifp->opts.crop_top, ifp->opts.crop_bottom, | ||
| 2031 | ifp->opts.crop_left, ifp->opts.crop_top); | ||
| 2032 | 6 | ret = insert_filter(&last_filter, &pad_idx, "crop", crop_buf); | |
| 2033 | 6 | if (ret < 0) | |
| 2034 | ✗ | return ret; | |
| 2035 | } | ||
| 2036 | |||
| 2037 | // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph | ||
| 2038 | 6199 | ifp->displaymatrix_applied = 0; | |
| 2039 | 6199 | if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) && | |
| 2040 | 6194 | !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) { | |
| 2041 | 6194 | int32_t *displaymatrix = ifp->displaymatrix; | |
| 2042 | double theta; | ||
| 2043 | |||
| 2044 | 6194 | theta = get_rotation(displaymatrix); | |
| 2045 | |||
| 2046 | 6194 | if (fabs(theta - 90) < 1.0) { | |
| 2047 | 15 | ret = insert_filter(&last_filter, &pad_idx, "transpose", | |
| 2048 | 15 | displaymatrix[3] > 0 ? "cclock_flip" : "clock"); | |
| 2049 | 6179 | } else if (fabs(theta - 180) < 1.0) { | |
| 2050 | ✗ | if (displaymatrix[0] < 0) { | |
| 2051 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL); | |
| 2052 | ✗ | if (ret < 0) | |
| 2053 | ✗ | return ret; | |
| 2054 | } | ||
| 2055 | ✗ | if (displaymatrix[4] < 0) { | |
| 2056 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL); | |
| 2057 | } | ||
| 2058 | 6179 | } else if (fabs(theta - 270) < 1.0) { | |
| 2059 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "transpose", | |
| 2060 | ✗ | displaymatrix[3] < 0 ? "clock_flip" : "cclock"); | |
| 2061 | 6179 | } else if (fabs(theta) > 1.0) { | |
| 2062 | char rotate_buf[64]; | ||
| 2063 | ✗ | snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta); | |
| 2064 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf); | |
| 2065 | 6179 | } else if (fabs(theta) < 1.0) { | |
| 2066 | 9 | if (displaymatrix && displaymatrix[4] < 0) { | |
| 2067 | ✗ | ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL); | |
| 2068 | } | ||
| 2069 | } | ||
| 2070 | 6194 | if (ret < 0) | |
| 2071 | ✗ | return ret; | |
| 2072 | |||
| 2073 | 6194 | ifp->displaymatrix_applied = 1; | |
| 2074 | } | ||
| 2075 | |||
| 2076 | 6199 | snprintf(name, sizeof(name), "trim_in_%s", ifp->opts.name); | |
| 2077 | 6199 | ret = insert_trim(fg, ifp->opts.trim_start_us, ifp->opts.trim_end_us, | |
| 2078 | &last_filter, &pad_idx, name); | ||
| 2079 | 6199 | if (ret < 0) | |
| 2080 | ✗ | return ret; | |
| 2081 | |||
| 2082 | 6199 | if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0) | |
| 2083 | ✗ | return ret; | |
| 2084 | 6199 | return 0; | |
| 2085 | ✗ | fail: | |
| 2086 | ✗ | av_freep(&par); | |
| 2087 | |||
| 2088 | ✗ | return ret; | |
| 2089 | } | ||
| 2090 | |||
| 2091 | 1538 | static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph, | |
| 2092 | InputFilter *ifilter, AVFilterInOut *in) | ||
| 2093 | { | ||
| 2094 | 1538 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 2095 | AVFilterContext *last_filter; | ||
| 2096 | AVBufferSrcParameters *par; | ||
| 2097 | 1538 | const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer"); | |
| 2098 | AVBPrint args; | ||
| 2099 | char name[255]; | ||
| 2100 | 1538 | int ret, pad_idx = 0; | |
| 2101 | |||
| 2102 | 1538 | av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC); | |
| 2103 | 1538 | av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s", | |
| 2104 | ifp->time_base.num, ifp->time_base.den, | ||
| 2105 | ifp->sample_rate, | ||
| 2106 | 1538 | av_get_sample_fmt_name(ifp->format)); | |
| 2107 | 1538 | if (av_channel_layout_check(&ifp->ch_layout) && | |
| 2108 | 1538 | ifp->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) { | |
| 2109 | 1518 | av_bprintf(&args, ":channel_layout="); | |
| 2110 | 1518 | av_channel_layout_describe_bprint(&ifp->ch_layout, &args); | |
| 2111 | } else | ||
| 2112 | 20 | av_bprintf(&args, ":channels=%d", ifp->ch_layout.nb_channels); | |
| 2113 | 1538 | snprintf(name, sizeof(name), "graph_%d_in_%s", fg->index, ifp->opts.name); | |
| 2114 | |||
| 2115 | 1538 | if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt, | |
| 2116 | 1538 | name, args.str, NULL, | |
| 2117 | graph)) < 0) | ||
| 2118 | ✗ | return ret; | |
| 2119 | 1538 | par = av_buffersrc_parameters_alloc(); | |
| 2120 | 1538 | if (!par) | |
| 2121 | ✗ | return AVERROR(ENOMEM); | |
| 2122 | 1538 | par->side_data = ifp->side_data; | |
| 2123 | 1538 | par->nb_side_data = ifp->nb_side_data; | |
| 2124 | 1538 | ret = av_buffersrc_parameters_set(ifilter->filter, par); | |
| 2125 | 1538 | av_free(par); | |
| 2126 | 1538 | if (ret < 0) | |
| 2127 | ✗ | return ret; | |
| 2128 | 1538 | last_filter = ifilter->filter; | |
| 2129 | |||
| 2130 | 1538 | snprintf(name, sizeof(name), "trim for input stream %s", ifp->opts.name); | |
| 2131 | 1538 | ret = insert_trim(fg, ifp->opts.trim_start_us, ifp->opts.trim_end_us, | |
| 2132 | &last_filter, &pad_idx, name); | ||
| 2133 | 1538 | if (ret < 0) | |
| 2134 | ✗ | return ret; | |
| 2135 | |||
| 2136 | 1538 | if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0) | |
| 2137 | ✗ | return ret; | |
| 2138 | |||
| 2139 | 1538 | return 0; | |
| 2140 | } | ||
| 2141 | |||
| 2142 | 7737 | static int configure_input_filter(FilterGraph *fg, AVFilterGraph *graph, | |
| 2143 | InputFilter *ifilter, AVFilterInOut *in) | ||
| 2144 | { | ||
| 2145 | 7737 | switch (ifilter->type) { | |
| 2146 | 6199 | case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, graph, ifilter, in); | |
| 2147 | 1538 | case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, graph, ifilter, in); | |
| 2148 | ✗ | default: av_assert0(0); return 0; | |
| 2149 | } | ||
| 2150 | } | ||
| 2151 | |||
| 2152 | 8883 | static void cleanup_filtergraph(FilterGraph *fg, FilterGraphThread *fgt) | |
| 2153 | { | ||
| 2154 | 18001 | for (int i = 0; i < fg->nb_outputs; i++) | |
| 2155 | 9118 | fg->outputs[i]->filter = NULL; | |
| 2156 | 16622 | for (int i = 0; i < fg->nb_inputs; i++) | |
| 2157 | 7739 | fg->inputs[i]->filter = NULL; | |
| 2158 | 8883 | avfilter_graph_free(&fgt->graph); | |
| 2159 | 8883 | } | |
| 2160 | |||
| 2161 | 10335 | static int filter_is_buffersrc(const AVFilterContext *f) | |
| 2162 | { | ||
| 2163 | 14830 | return f->nb_inputs == 0 && | |
| 2164 | 4495 | (!strcmp(f->filter->name, "buffer") || | |
| 2165 | 2054 | !strcmp(f->filter->name, "abuffer")); | |
| 2166 | } | ||
| 2167 | |||
| 2168 | 8879 | static int graph_is_meta(AVFilterGraph *graph) | |
| 2169 | { | ||
| 2170 | 21275 | for (unsigned i = 0; i < graph->nb_filters; i++) { | |
| 2171 | 19491 | const AVFilterContext *f = graph->filters[i]; | |
| 2172 | |||
| 2173 | /* in addition to filters flagged as meta, also | ||
| 2174 | * disregard sinks and buffersources (but not other sources, | ||
| 2175 | * since they introduce data we are not aware of) | ||
| 2176 | */ | ||
| 2177 | 29826 | if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) || | |
| 2178 | 13575 | f->nb_outputs == 0 || | |
| 2179 | 10335 | filter_is_buffersrc(f))) | |
| 2180 | 7095 | return 0; | |
| 2181 | } | ||
| 2182 | 1784 | return 1; | |
| 2183 | } | ||
| 2184 | |||
| 2185 | static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer); | ||
| 2186 | |||
| 2187 | 8881 | static int configure_filtergraph(FilterGraph *fg, FilterGraphThread *fgt) | |
| 2188 | { | ||
| 2189 | 8881 | FilterGraphPriv *fgp = fgp_from_fg(fg); | |
| 2190 | AVBufferRef *hw_device; | ||
| 2191 | AVFilterInOut *inputs, *outputs, *cur; | ||
| 2192 | 8881 | int ret = AVERROR_BUG, i, simple = filtergraph_is_simple(fg); | |
| 2193 | 8881 | int have_input_eof = 0; | |
| 2194 | 8881 | const char *graph_desc = fg->graph_desc; | |
| 2195 | |||
| 2196 | 8881 | cleanup_filtergraph(fg, fgt); | |
| 2197 | 8881 | fgt->graph = avfilter_graph_alloc(); | |
| 2198 | 8881 | if (!fgt->graph) | |
| 2199 | ✗ | return AVERROR(ENOMEM); | |
| 2200 | |||
| 2201 | 8881 | if (simple) { | |
| 2202 | 7549 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]); | |
| 2203 | |||
| 2204 | 7549 | if (filter_nbthreads) { | |
| 2205 | 391 | ret = av_opt_set(fgt->graph, "threads", filter_nbthreads, 0); | |
| 2206 | 391 | if (ret < 0) | |
| 2207 | ✗ | goto fail; | |
| 2208 | 7158 | } else if (fgp->nb_threads >= 0) { | |
| 2209 | 4206 | ret = av_opt_set_int(fgt->graph, "threads", fgp->nb_threads, 0); | |
| 2210 | 4206 | if (ret < 0) | |
| 2211 | ✗ | return ret; | |
| 2212 | } | ||
| 2213 | |||
| 2214 | 7549 | if (av_dict_count(ofp->sws_opts)) { | |
| 2215 | 4567 | ret = av_dict_get_string(ofp->sws_opts, | |
| 2216 | 4567 | &fgt->graph->scale_sws_opts, | |
| 2217 | '=', ':'); | ||
| 2218 | 4567 | if (ret < 0) | |
| 2219 | ✗ | goto fail; | |
| 2220 | } | ||
| 2221 | |||
| 2222 | 7549 | if (av_dict_count(ofp->swr_opts)) { | |
| 2223 | char *args; | ||
| 2224 | 67 | ret = av_dict_get_string(ofp->swr_opts, &args, '=', ':'); | |
| 2225 | 67 | if (ret < 0) | |
| 2226 | ✗ | goto fail; | |
| 2227 | 67 | av_opt_set(fgt->graph, "aresample_swr_opts", args, 0); | |
| 2228 | 67 | av_free(args); | |
| 2229 | } | ||
| 2230 | } else { | ||
| 2231 | 1332 | fgt->graph->nb_threads = filter_complex_nbthreads; | |
| 2232 | } | ||
| 2233 | |||
| 2234 | 53286 | for (int type = 0; type < AVMEDIA_TYPE_NB; type++) { | |
| 2235 | 44405 | const int limit = filter_buffered_frames[type]; | |
| 2236 | 44405 | if (limit <= 0) | |
| 2237 | 44400 | continue; | |
| 2238 | 5 | ret = avfilter_graph_set_max_buffered_frames(fgt->graph, type, limit); | |
| 2239 | 5 | if (ret < 0) | |
| 2240 | ✗ | goto fail; | |
| 2241 | } | ||
| 2242 | |||
| 2243 | 8881 | hw_device = hw_device_for_filter(); | |
| 2244 | |||
| 2245 | 8881 | ret = graph_parse(fg, fgt->graph, graph_desc, &inputs, &outputs, hw_device); | |
| 2246 | 8881 | if (ret < 0) | |
| 2247 | ✗ | goto fail; | |
| 2248 | |||
| 2249 | 16618 | for (cur = inputs, i = 0; cur; cur = cur->next, i++) | |
| 2250 | 7737 | if ((ret = configure_input_filter(fg, fgt->graph, fg->inputs[i], cur)) < 0) { | |
| 2251 | ✗ | avfilter_inout_free(&inputs); | |
| 2252 | ✗ | avfilter_inout_free(&outputs); | |
| 2253 | ✗ | goto fail; | |
| 2254 | } | ||
| 2255 | 8881 | avfilter_inout_free(&inputs); | |
| 2256 | |||
| 2257 | 17997 | for (cur = outputs, i = 0; cur; cur = cur->next, i++) { | |
| 2258 | 9116 | ret = configure_output_filter(fgp, fgt->graph, fg->outputs[i], cur); | |
| 2259 | 9116 | if (ret < 0) { | |
| 2260 | ✗ | avfilter_inout_free(&outputs); | |
| 2261 | ✗ | goto fail; | |
| 2262 | } | ||
| 2263 | } | ||
| 2264 | 8881 | avfilter_inout_free(&outputs); | |
| 2265 | |||
| 2266 | 8881 | if (fgp->disable_conversions) | |
| 2267 | 6848 | avfilter_graph_set_auto_convert(fgt->graph, AVFILTER_AUTO_CONVERT_NONE); | |
| 2268 | 8881 | if ((ret = avfilter_graph_config(fgt->graph, NULL)) < 0) | |
| 2269 | 2 | goto fail; | |
| 2270 | |||
| 2271 | 8879 | fgp->is_meta = graph_is_meta(fgt->graph); | |
| 2272 | |||
| 2273 | /* limit the lists of allowed formats to the ones selected, to | ||
| 2274 | * make sure they stay the same if the filtergraph is reconfigured later */ | ||
| 2275 | 17993 | for (int i = 0; i < fg->nb_outputs; i++) { | |
| 2276 | const AVFrameSideData *const *sd; | ||
| 2277 | int nb_sd; | ||
| 2278 | 9114 | OutputFilter *ofilter = fg->outputs[i]; | |
| 2279 | 9114 | OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); | |
| 2280 | 9114 | AVFilterContext *sink = ofilter->filter; | |
| 2281 | |||
| 2282 | 9114 | ofp->format = av_buffersink_get_format(sink); | |
| 2283 | |||
| 2284 | 9114 | ofp->width = av_buffersink_get_w(sink); | |
| 2285 | 9114 | ofp->height = av_buffersink_get_h(sink); | |
| 2286 | 9114 | ofp->color_space = av_buffersink_get_colorspace(sink); | |
| 2287 | 9114 | ofp->color_range = av_buffersink_get_color_range(sink); | |
| 2288 | 9114 | ofp->alpha_mode = av_buffersink_get_alpha_mode(sink); | |
| 2289 | 9114 | ofp->chroma_location = av_buffersink_get_chroma_location(sink); | |
| 2290 | |||
| 2291 | // If the timing parameters are not locked yet, get the tentative values | ||
| 2292 | // here but don't lock them. They will only be used if no output frames | ||
| 2293 | // are ever produced. | ||
| 2294 | 9114 | if (!ofp->tb_out_locked) { | |
| 2295 | 8807 | AVRational fr = av_buffersink_get_frame_rate(sink); | |
| 2296 | 8807 | if (ofp->fps.framerate.num <= 0 && ofp->fps.framerate.den <= 0 && | |
| 2297 | 8786 | fr.num > 0 && fr.den > 0) | |
| 2298 | 7073 | ofp->fps.framerate = fr; | |
| 2299 | 8807 | ofp->tb_out = av_buffersink_get_time_base(sink); | |
| 2300 | } | ||
| 2301 | 9114 | ofp->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink); | |
| 2302 | |||
| 2303 | 9114 | ofp->sample_rate = av_buffersink_get_sample_rate(sink); | |
| 2304 | 9114 | av_channel_layout_uninit(&ofp->ch_layout); | |
| 2305 | 9114 | ret = av_buffersink_get_ch_layout(sink, &ofp->ch_layout); | |
| 2306 | 9114 | if (ret < 0) | |
| 2307 | ✗ | goto fail; | |
| 2308 | 9114 | sd = av_buffersink_get_side_data(sink, &nb_sd); | |
| 2309 | 9114 | if (nb_sd) | |
| 2310 | 561 | for (int j = 0; j < nb_sd; j++) { | |
| 2311 | 291 | ret = av_frame_side_data_clone(&ofp->side_data, &ofp->nb_side_data, | |
| 2312 | 291 | sd[j], AV_FRAME_SIDE_DATA_FLAG_REPLACE); | |
| 2313 | 291 | if (ret < 0) { | |
| 2314 | ✗ | av_frame_side_data_free(&ofp->side_data, &ofp->nb_side_data); | |
| 2315 | ✗ | goto fail; | |
| 2316 | } | ||
| 2317 | } | ||
| 2318 | } | ||
| 2319 | |||
| 2320 | 16614 | for (int i = 0; i < fg->nb_inputs; i++) { | |
| 2321 | 7735 | InputFilter *ifilter = fg->inputs[i]; | |
| 2322 | 7735 | InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]); | |
| 2323 | AVFrame *tmp; | ||
| 2324 | 7952 | while (av_fifo_read(ifp->frame_queue, &tmp, 1) >= 0) { | |
| 2325 | 217 | if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) { | |
| 2326 | 14 | ret = sub2video_frame(&ifp->ifilter, tmp, !fgt->graph); | |
| 2327 | } else { | ||
| 2328 | 203 | if (ifp->type_src == AVMEDIA_TYPE_VIDEO) { | |
| 2329 | 51 | if (ifp->displaymatrix_applied) | |
| 2330 | 51 | av_frame_remove_side_data(tmp, AV_FRAME_DATA_DISPLAYMATRIX); | |
| 2331 | } | ||
| 2332 | 203 | ret = av_buffersrc_add_frame(ifilter->filter, tmp); | |
| 2333 | } | ||
| 2334 | 217 | av_frame_free(&tmp); | |
| 2335 | 217 | if (ret < 0) | |
| 2336 | ✗ | goto fail; | |
| 2337 | } | ||
| 2338 | } | ||
| 2339 | |||
| 2340 | /* send the EOFs for the finished inputs */ | ||
| 2341 | 16614 | for (int i = 0; i < fg->nb_inputs; i++) { | |
| 2342 | 7735 | InputFilter *ifilter = fg->inputs[i]; | |
| 2343 | 7735 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 2344 | |||
| 2345 | 7735 | if (fgt->eof_in[i] || ifp->eof) { | |
| 2346 | 18 | ret = av_buffersrc_add_frame(ifilter->filter, NULL); | |
| 2347 | 18 | if (ret < 0) | |
| 2348 | ✗ | goto fail; | |
| 2349 | 18 | have_input_eof = 1; | |
| 2350 | } | ||
| 2351 | } | ||
| 2352 | |||
| 2353 | 8879 | if (have_input_eof) { | |
| 2354 | // make sure the EOF propagates to the end of the graph | ||
| 2355 | 12 | ret = avfilter_graph_request_oldest(fgt->graph); | |
| 2356 | 12 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) | |
| 2357 | ✗ | goto fail; | |
| 2358 | } | ||
| 2359 | |||
| 2360 | 8879 | return 0; | |
| 2361 | 2 | fail: | |
| 2362 | 2 | cleanup_filtergraph(fg, fgt); | |
| 2363 | 2 | return ret; | |
| 2364 | } | ||
| 2365 | |||
| 2366 | 7731 | static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame) | |
| 2367 | { | ||
| 2368 | 7731 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 2369 | AVFrameSideData *sd; | ||
| 2370 | int ret; | ||
| 2371 | |||
| 2372 | 7731 | ret = av_buffer_replace(&ifp->hw_frames_ctx, frame->hw_frames_ctx); | |
| 2373 | 7731 | if (ret < 0) | |
| 2374 | ✗ | return ret; | |
| 2375 | |||
| 2376 | 13895 | ifp->time_base = (ifilter->type == AVMEDIA_TYPE_AUDIO) ? (AVRational){ 1, frame->sample_rate } : | |
| 2377 | 6196 | (ifp->opts.flags & IFILTER_FLAG_CFR) ? av_inv_q(ifp->opts.framerate) : | |
| 2378 | frame->time_base; | ||
| 2379 | |||
| 2380 | 7731 | ifp->format = frame->format; | |
| 2381 | |||
| 2382 | 7731 | ifp->width = frame->width; | |
| 2383 | 7731 | ifp->height = frame->height; | |
| 2384 | 7731 | ifp->sample_aspect_ratio = frame->sample_aspect_ratio; | |
| 2385 | 7731 | ifp->color_space = frame->colorspace; | |
| 2386 | 7731 | ifp->color_range = frame->color_range; | |
| 2387 | 7731 | ifp->alpha_mode = frame->alpha_mode; | |
| 2388 | 7731 | ifp->chroma_location = frame->chroma_location; | |
| 2389 | |||
| 2390 | 7731 | ifp->sample_rate = frame->sample_rate; | |
| 2391 | 7731 | ret = av_channel_layout_copy(&ifp->ch_layout, &frame->ch_layout); | |
| 2392 | 7731 | if (ret < 0) | |
| 2393 | ✗ | return ret; | |
| 2394 | |||
| 2395 | 7731 | av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data); | |
| 2396 | 8375 | for (int i = 0; i < frame->nb_side_data; i++) { | |
| 2397 | 644 | const AVSideDataDescriptor *desc = av_frame_side_data_desc(frame->side_data[i]->type); | |
| 2398 | |||
| 2399 | 644 | if (!(desc->props & AV_SIDE_DATA_PROP_GLOBAL)) | |
| 2400 | 339 | continue; | |
| 2401 | |||
| 2402 | 305 | ret = av_frame_side_data_clone(&ifp->side_data, | |
| 2403 | &ifp->nb_side_data, | ||
| 2404 | 305 | frame->side_data[i], 0); | |
| 2405 | 305 | if (ret < 0) | |
| 2406 | ✗ | return ret; | |
| 2407 | } | ||
| 2408 | |||
| 2409 | 7731 | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX); | |
| 2410 | 7731 | if (sd) { | |
| 2411 | 27 | memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix)); | |
| 2412 | 27 | if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) && | |
| 2413 | 24 | !(av_pix_fmt_desc_get(ifp->format)->flags & AV_PIX_FMT_FLAG_HWACCEL)) | |
| 2414 | 24 | av_frame_side_data_remove(&ifp->side_data, &ifp->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX); | |
| 2415 | } | ||
| 2416 | 7731 | ifp->displaymatrix_present = !!sd; | |
| 2417 | |||
| 2418 | /* Copy downmix related side data to InputFilterPriv so it may be propagated | ||
| 2419 | * to the filter chain even though it's not "global", as filters like aresample | ||
| 2420 | * require this information during init and not when remixing a frame */ | ||
| 2421 | 7731 | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_INFO); | |
| 2422 | 7731 | if (sd) { | |
| 2423 | 8 | ret = av_frame_side_data_clone(&ifp->side_data, | |
| 2424 | &ifp->nb_side_data, sd, 0); | ||
| 2425 | 8 | if (ret < 0) | |
| 2426 | ✗ | return ret; | |
| 2427 | 8 | memcpy(&ifp->downmixinfo, sd->data, sizeof(ifp->downmixinfo)); | |
| 2428 | } | ||
| 2429 | 7731 | ifp->downmixinfo_present = !!sd; | |
| 2430 | 7731 | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_MATRIX); | |
| 2431 | 7731 | if (sd) { | |
| 2432 | 4 | ret = av_frame_side_data_clone(&ifp->side_data, | |
| 2433 | &ifp->nb_side_data, sd, 0); | ||
| 2434 | 4 | if (ret < 0) | |
| 2435 | ✗ | return ret; | |
| 2436 | 4 | ret = av_buffer_replace(&ifp->downmixmatrix, sd->buf); | |
| 2437 | 4 | if (ret < 0) | |
| 2438 | ✗ | return ret; | |
| 2439 | 4 | ifp->downmixmatrix_size = sd->size; | |
| 2440 | } | ||
| 2441 | 7731 | ifp->downmixmatrix_present = !!sd; | |
| 2442 | |||
| 2443 | 7731 | return 0; | |
| 2444 | } | ||
| 2445 | |||
| 2446 | 1 | static int ifilter_parameters_from_ofilter(InputFilter *ifilter, OutputFilter *ofilter) | |
| 2447 | { | ||
| 2448 | 1 | const OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); | |
| 2449 | 1 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 2450 | |||
| 2451 | 1 | if (!ifp->opts.framerate.num) { | |
| 2452 | 1 | ifp->opts.framerate = ofp->fps.framerate; | |
| 2453 | 1 | if (ifp->opts.framerate.num > 0 && ifp->opts.framerate.den > 0) | |
| 2454 | 1 | ifp->opts.flags |= IFILTER_FLAG_CFR; | |
| 2455 | } | ||
| 2456 | |||
| 2457 | 1 | for (int i = 0; i < ofp->nb_side_data; i++) { | |
| 2458 | ✗ | int ret = av_frame_side_data_clone(&ifp->side_data, &ifp->nb_side_data, | |
| 2459 | ✗ | ofp->side_data[i], AV_FRAME_SIDE_DATA_FLAG_REPLACE); | |
| 2460 | ✗ | if (ret < 0) | |
| 2461 | ✗ | return ret; | |
| 2462 | } | ||
| 2463 | |||
| 2464 | 1 | return 0; | |
| 2465 | } | ||
| 2466 | |||
| 2467 | 41120 | int filtergraph_is_simple(const FilterGraph *fg) | |
| 2468 | { | ||
| 2469 | 41120 | const FilterGraphPriv *fgp = cfgp_from_cfg(fg); | |
| 2470 | 41120 | return fgp->is_simple; | |
| 2471 | } | ||
| 2472 | |||
| 2473 | ✗ | static void send_command(FilterGraph *fg, AVFilterGraph *graph, | |
| 2474 | double time, const char *target, | ||
| 2475 | const char *command, const char *arg, int all_filters) | ||
| 2476 | { | ||
| 2477 | int ret; | ||
| 2478 | |||
| 2479 | ✗ | if (!graph) | |
| 2480 | ✗ | return; | |
| 2481 | |||
| 2482 | ✗ | if (time < 0) { | |
| 2483 | char response[4096]; | ||
| 2484 | ✗ | ret = avfilter_graph_send_command(graph, target, command, arg, | |
| 2485 | response, sizeof(response), | ||
| 2486 | all_filters ? 0 : AVFILTER_CMD_FLAG_ONE); | ||
| 2487 | ✗ | fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s", | |
| 2488 | fg->index, ret, response); | ||
| 2489 | ✗ | } else if (!all_filters) { | |
| 2490 | ✗ | fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n"); | |
| 2491 | } else { | ||
| 2492 | ✗ | ret = avfilter_graph_queue_command(graph, target, command, arg, 0, time); | |
| 2493 | ✗ | if (ret < 0) | |
| 2494 | ✗ | fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret)); | |
| 2495 | } | ||
| 2496 | } | ||
| 2497 | |||
| 2498 | 809205 | static int choose_input(const FilterGraph *fg, const FilterGraphThread *fgt) | |
| 2499 | { | ||
| 2500 | 809205 | int nb_requests, nb_requests_max = -1; | |
| 2501 | 809205 | int best_input = -1; | |
| 2502 | |||
| 2503 | 1633764 | for (int i = 0; i < fg->nb_inputs; i++) { | |
| 2504 | 824559 | InputFilter *ifilter = fg->inputs[i]; | |
| 2505 | 824559 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 2506 | |||
| 2507 | 824559 | if (fgt->eof_in[i] || ifp->eof) | |
| 2508 | 1530 | continue; | |
| 2509 | |||
| 2510 | 823029 | nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter); | |
| 2511 | 823029 | if (nb_requests > nb_requests_max) { | |
| 2512 | 811833 | nb_requests_max = nb_requests; | |
| 2513 | 811833 | best_input = i; | |
| 2514 | } | ||
| 2515 | } | ||
| 2516 | |||
| 2517 | 809205 | av_assert0(best_input >= 0); | |
| 2518 | |||
| 2519 | 809205 | return best_input; | |
| 2520 | } | ||
| 2521 | |||
| 2522 | 8802 | static int choose_out_timebase(OutputFilterPriv *ofp, AVFrame *frame) | |
| 2523 | { | ||
| 2524 | 8802 | OutputFilter *ofilter = &ofp->ofilter; | |
| 2525 | 8802 | FPSConvContext *fps = &ofp->fps; | |
| 2526 | 8802 | AVRational tb = (AVRational){ 0, 0 }; | |
| 2527 | AVRational fr; | ||
| 2528 | const FrameData *fd; | ||
| 2529 | |||
| 2530 | 8802 | fd = frame_data_c(frame); | |
| 2531 | |||
| 2532 | // apply -enc_time_base | ||
| 2533 | 8802 | if (ofp->enc_timebase.num == ENC_TIME_BASE_DEMUX && | |
| 2534 | 1 | (fd->dec.tb.num <= 0 || fd->dec.tb.den <= 0)) { | |
| 2535 | ✗ | av_log(ofp, AV_LOG_ERROR, | |
| 2536 | "Demuxing timebase not available - cannot use it for encoding\n"); | ||
| 2537 | ✗ | return AVERROR(EINVAL); | |
| 2538 | } | ||
| 2539 | |||
| 2540 | 8802 | switch (ofp->enc_timebase.num) { | |
| 2541 | 8800 | case 0: break; | |
| 2542 | 1 | case ENC_TIME_BASE_DEMUX: tb = fd->dec.tb; break; | |
| 2543 | ✗ | case ENC_TIME_BASE_FILTER: tb = frame->time_base; break; | |
| 2544 | 1 | default: tb = ofp->enc_timebase; break; | |
| 2545 | } | ||
| 2546 | |||
| 2547 | 8802 | if (ofilter->type == AVMEDIA_TYPE_AUDIO) { | |
| 2548 | 1694 | tb = tb.num ? tb : (AVRational){ 1, frame->sample_rate }; | |
| 2549 | 1694 | goto finish; | |
| 2550 | } | ||
| 2551 | |||
| 2552 | 7108 | fr = fps->framerate; | |
| 2553 | 7108 | if (!fr.num) { | |
| 2554 | 16 | AVRational fr_sink = av_buffersink_get_frame_rate(ofilter->filter); | |
| 2555 | 16 | if (fr_sink.num > 0 && fr_sink.den > 0) | |
| 2556 | ✗ | fr = fr_sink; | |
| 2557 | } | ||
| 2558 | |||
| 2559 | 7108 | if (fps->vsync_method == VSYNC_CFR || fps->vsync_method == VSYNC_VSCFR) { | |
| 2560 | 1528 | if (!fr.num && !fps->framerate_max.num) { | |
| 2561 | ✗ | fr = (AVRational){25, 1}; | |
| 2562 | ✗ | av_log(ofp, AV_LOG_WARNING, | |
| 2563 | "No information " | ||
| 2564 | "about the input framerate is available. Falling " | ||
| 2565 | "back to a default value of 25fps. Use the -r option " | ||
| 2566 | "if you want a different framerate.\n"); | ||
| 2567 | } | ||
| 2568 | |||
| 2569 | 1528 | if (fps->framerate_max.num && | |
| 2570 | ✗ | (av_q2d(fr) > av_q2d(fps->framerate_max) || | |
| 2571 | ✗ | !fr.den)) | |
| 2572 | ✗ | fr = fps->framerate_max; | |
| 2573 | } | ||
| 2574 | |||
| 2575 | 7108 | if (fr.num > 0) { | |
| 2576 | 7092 | if (fps->framerate_supported) { | |
| 2577 | 78 | int idx = av_find_nearest_q_idx(fr, fps->framerate_supported); | |
| 2578 | 78 | fr = fps->framerate_supported[idx]; | |
| 2579 | } | ||
| 2580 | 7092 | if (fps->framerate_clip) { | |
| 2581 | 79 | av_reduce(&fr.num, &fr.den, | |
| 2582 | 79 | fr.num, fr.den, fps->framerate_clip); | |
| 2583 | } | ||
| 2584 | } | ||
| 2585 | |||
| 2586 | 7108 | if (!(tb.num > 0 && tb.den > 0)) | |
| 2587 | 7107 | tb = av_inv_q(fr); | |
| 2588 | 7108 | if (!(tb.num > 0 && tb.den > 0)) | |
| 2589 | 16 | tb = frame->time_base; | |
| 2590 | |||
| 2591 | 7108 | fps->framerate = fr; | |
| 2592 | 8802 | finish: | |
| 2593 | 8802 | ofp->tb_out = tb; | |
| 2594 | 8802 | ofp->tb_out_locked = 1; | |
| 2595 | |||
| 2596 | 8802 | return 0; | |
| 2597 | } | ||
| 2598 | |||
| 2599 | 152945 | static double adjust_frame_pts_to_encoder_tb(void *logctx, AVFrame *frame, | |
| 2600 | AVRational tb_dst, int64_t start_time) | ||
| 2601 | { | ||
| 2602 | 152945 | double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision | |
| 2603 | |||
| 2604 | 152945 | AVRational tb = tb_dst; | |
| 2605 | 152945 | AVRational filter_tb = frame->time_base; | |
| 2606 | 152945 | const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16); | |
| 2607 | |||
| 2608 | 152945 | if (frame->pts == AV_NOPTS_VALUE) | |
| 2609 | ✗ | goto early_exit; | |
| 2610 | |||
| 2611 | 152945 | tb.den <<= extra_bits; | |
| 2612 | 152945 | float_pts = av_rescale_q(frame->pts, filter_tb, tb) - | |
| 2613 | 152945 | av_rescale_q(start_time, AV_TIME_BASE_Q, tb); | |
| 2614 | 152945 | float_pts /= 1 << extra_bits; | |
| 2615 | // when float_pts is not exactly an integer, | ||
| 2616 | // avoid exact midpoints to reduce the chance of rounding differences, this | ||
| 2617 | // can be removed in case the fps code is changed to work with integers | ||
| 2618 | 152945 | if (float_pts != llrint(float_pts)) | |
| 2619 | 6991 | float_pts += FFSIGN(float_pts) * 1.0 / (1<<17); | |
| 2620 | |||
| 2621 | 152945 | frame->pts = av_rescale_q(frame->pts, filter_tb, tb_dst) - | |
| 2622 | 152945 | av_rescale_q(start_time, AV_TIME_BASE_Q, tb_dst); | |
| 2623 | 152945 | frame->time_base = tb_dst; | |
| 2624 | |||
| 2625 | 152945 | early_exit: | |
| 2626 | |||
| 2627 | 152945 | if (debug_ts) { | |
| 2628 | ✗ | av_log(logctx, AV_LOG_INFO, | |
| 2629 | "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n", | ||
| 2630 | ✗ | frame ? av_ts2str(frame->pts) : "NULL", | |
| 2631 | ✗ | av_ts2timestr(frame->pts, &tb_dst), | |
| 2632 | float_pts, tb_dst.num, tb_dst.den); | ||
| 2633 | } | ||
| 2634 | |||
| 2635 | 152945 | return float_pts; | |
| 2636 | } | ||
| 2637 | |||
| 2638 | 3950 | static int64_t median3(int64_t a, int64_t b, int64_t c) | |
| 2639 | { | ||
| 2640 | int64_t max2, min2, m; | ||
| 2641 | |||
| 2642 | 3950 | if (a >= b) { | |
| 2643 | 3950 | max2 = a; | |
| 2644 | 3950 | min2 = b; | |
| 2645 | } else { | ||
| 2646 | ✗ | max2 = b; | |
| 2647 | ✗ | min2 = a; | |
| 2648 | } | ||
| 2649 | 3950 | m = (c >= max2) ? max2 : c; | |
| 2650 | |||
| 2651 | 3950 | return (m >= min2) ? m : min2; | |
| 2652 | } | ||
| 2653 | |||
| 2654 | |||
| 2655 | /* Convert frame timestamps to the encoder timebase and decide how many times | ||
| 2656 | * should this (and possibly previous) frame be repeated in order to conform to | ||
| 2657 | * desired target framerate (if any). | ||
| 2658 | */ | ||
| 2659 | 156895 | static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame, | |
| 2660 | int64_t *nb_frames, int64_t *nb_frames_prev) | ||
| 2661 | { | ||
| 2662 | 156895 | OutputFilter *ofilter = &ofp->ofilter; | |
| 2663 | 156895 | FPSConvContext *fps = &ofp->fps; | |
| 2664 | double delta0, delta, sync_ipts, duration; | ||
| 2665 | |||
| 2666 | 156895 | if (!frame) { | |
| 2667 | 3950 | *nb_frames_prev = *nb_frames = median3(fps->frames_prev_hist[0], | |
| 2668 | fps->frames_prev_hist[1], | ||
| 2669 | fps->frames_prev_hist[2]); | ||
| 2670 | |||
| 2671 | 3950 | if (!*nb_frames && fps->last_dropped) { | |
| 2672 | 2 | atomic_fetch_add(&ofilter->nb_frames_drop, 1); | |
| 2673 | 2 | fps->last_dropped++; | |
| 2674 | } | ||
| 2675 | |||
| 2676 | 3950 | goto finish; | |
| 2677 | } | ||
| 2678 | |||
| 2679 | 152945 | duration = frame->duration * av_q2d(frame->time_base) / av_q2d(ofp->tb_out); | |
| 2680 | |||
| 2681 | 152945 | sync_ipts = adjust_frame_pts_to_encoder_tb(ofilter->graph, frame, | |
| 2682 | ofp->tb_out, ofp->ts_offset); | ||
| 2683 | /* delta0 is the "drift" between the input frame and | ||
| 2684 | * where it would fall in the output. */ | ||
| 2685 | 152945 | delta0 = sync_ipts - ofp->next_pts; | |
| 2686 | 152945 | delta = delta0 + duration; | |
| 2687 | |||
| 2688 | // tracks the number of times the PREVIOUS frame should be duplicated, | ||
| 2689 | // mostly for variable framerate (VFR) | ||
| 2690 | 152945 | *nb_frames_prev = 0; | |
| 2691 | /* by default, we output a single frame */ | ||
| 2692 | 152945 | *nb_frames = 1; | |
| 2693 | |||
| 2694 | 152945 | if (delta0 < 0 && | |
| 2695 | 4333 | delta > 0 && | |
| 2696 | 4333 | fps->vsync_method != VSYNC_PASSTHROUGH) { | |
| 2697 | 4333 | if (delta0 < -0.6) { | |
| 2698 | 45 | av_log(ofp, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0); | |
| 2699 | } else | ||
| 2700 | 4288 | av_log(ofp, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0); | |
| 2701 | 4333 | sync_ipts = ofp->next_pts; | |
| 2702 | 4333 | duration += delta0; | |
| 2703 | 4333 | delta0 = 0; | |
| 2704 | } | ||
| 2705 | |||
| 2706 | 152945 | switch (fps->vsync_method) { | |
| 2707 | 9279 | case VSYNC_VSCFR: | |
| 2708 | 9279 | if (fps->frame_number == 0 && delta0 >= 0.5) { | |
| 2709 | ✗ | av_log(ofp, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); | |
| 2710 | ✗ | delta = duration; | |
| 2711 | ✗ | delta0 = 0; | |
| 2712 | ✗ | ofp->next_pts = llrint(sync_ipts); | |
| 2713 | } | ||
| 2714 | av_fallthrough; | ||
| 2715 | case VSYNC_CFR: | ||
| 2716 | // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c | ||
| 2717 | 38297 | if (frame_drop_threshold && delta < frame_drop_threshold && fps->frame_number) { | |
| 2718 | ✗ | *nb_frames = 0; | |
| 2719 | 38297 | } else if (delta < -1.1) | |
| 2720 | 375 | *nb_frames = 0; | |
| 2721 | 37922 | else if (delta > 1.1) { | |
| 2722 | 677 | *nb_frames = llrintf(delta); | |
| 2723 | 677 | if (delta0 > 1.1) | |
| 2724 | 2 | *nb_frames_prev = llrintf(delta0 - 0.6); | |
| 2725 | } | ||
| 2726 | 38297 | frame->duration = 1; | |
| 2727 | 38297 | break; | |
| 2728 | 91465 | case VSYNC_VFR: | |
| 2729 | 91465 | if (delta <= -0.6) | |
| 2730 | 99 | *nb_frames = 0; | |
| 2731 | 91366 | else if (delta > 0.6) | |
| 2732 | 72632 | ofp->next_pts = llrint(sync_ipts); | |
| 2733 | 91465 | frame->duration = llrint(duration); | |
| 2734 | 91465 | break; | |
| 2735 | 23183 | case VSYNC_PASSTHROUGH: | |
| 2736 | 23183 | ofp->next_pts = llrint(sync_ipts); | |
| 2737 | 23183 | frame->duration = llrint(duration); | |
| 2738 | 23183 | break; | |
| 2739 | ✗ | default: | |
| 2740 | ✗ | av_assert0(0); | |
| 2741 | } | ||
| 2742 | |||
| 2743 | 156895 | finish: | |
| 2744 | 156895 | memmove(fps->frames_prev_hist + 1, | |
| 2745 | 156895 | fps->frames_prev_hist, | |
| 2746 | sizeof(fps->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(fps->frames_prev_hist) - 1)); | ||
| 2747 | 156895 | fps->frames_prev_hist[0] = *nb_frames_prev; | |
| 2748 | |||
| 2749 | 156895 | if (*nb_frames_prev == 0 && fps->last_dropped) { | |
| 2750 | 474 | atomic_fetch_add(&ofilter->nb_frames_drop, 1); | |
| 2751 | 474 | av_log(ofp, AV_LOG_VERBOSE, | |
| 2752 | "*** dropping frame %"PRId64" at ts %"PRId64"\n", | ||
| 2753 | 474 | fps->frame_number, fps->last_frame->pts); | |
| 2754 | } | ||
| 2755 | 156895 | if (*nb_frames > (*nb_frames_prev && fps->last_dropped) + (*nb_frames > *nb_frames_prev)) { | |
| 2756 | uint64_t nb_frames_dup; | ||
| 2757 | 269 | if (*nb_frames > dts_error_threshold * 30) { | |
| 2758 | ✗ | av_log(ofp, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", *nb_frames - 1); | |
| 2759 | ✗ | atomic_fetch_add(&ofilter->nb_frames_drop, 1); | |
| 2760 | ✗ | *nb_frames = 0; | |
| 2761 | ✗ | return; | |
| 2762 | } | ||
| 2763 | 269 | nb_frames_dup = atomic_fetch_add(&ofilter->nb_frames_dup, | |
| 2764 | *nb_frames - (*nb_frames_prev && fps->last_dropped) - (*nb_frames > *nb_frames_prev)); | ||
| 2765 | 269 | av_log(ofp, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", *nb_frames - 1); | |
| 2766 | 269 | if (nb_frames_dup > fps->dup_warning) { | |
| 2767 | ✗ | av_log(ofp, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", fps->dup_warning); | |
| 2768 | ✗ | fps->dup_warning *= 10; | |
| 2769 | } | ||
| 2770 | } | ||
| 2771 | |||
| 2772 | 156895 | fps->last_dropped = *nb_frames == *nb_frames_prev && frame; | |
| 2773 | 156895 | fps->dropped_keyframe |= fps->last_dropped && (frame->flags & AV_FRAME_FLAG_KEY); | |
| 2774 | } | ||
| 2775 | |||
| 2776 | 6116 | static void close_input(InputFilterPriv *ifp) | |
| 2777 | { | ||
| 2778 | 6116 | FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph); | |
| 2779 | |||
| 2780 | 6116 | if (!ifp->eof) { | |
| 2781 | 4247 | sch_filter_receive_finish(fgp->sch, fgp->sch_idx, ifp->ifilter.index); | |
| 2782 | 4247 | ifp->eof = 1; | |
| 2783 | } | ||
| 2784 | 6116 | } | |
| 2785 | |||
| 2786 | 5604 | static int close_output(OutputFilterPriv *ofp, FilterGraphThread *fgt) | |
| 2787 | { | ||
| 2788 | 5604 | FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph); | |
| 2789 | int ret; | ||
| 2790 | |||
| 2791 | // we are finished and no frames were ever seen at this output, | ||
| 2792 | // at least initialize the encoder with a dummy frame | ||
| 2793 | 5604 | if (!fgt->got_frame) { | |
| 2794 | 5 | AVFrame *frame = fgt->frame; | |
| 2795 | FrameData *fd; | ||
| 2796 | |||
| 2797 | 5 | frame->time_base = ofp->tb_out; | |
| 2798 | 5 | frame->format = ofp->format; | |
| 2799 | |||
| 2800 | 5 | frame->width = ofp->width; | |
| 2801 | 5 | frame->height = ofp->height; | |
| 2802 | 5 | frame->sample_aspect_ratio = ofp->sample_aspect_ratio; | |
| 2803 | |||
| 2804 | 5 | frame->sample_rate = ofp->sample_rate; | |
| 2805 | 5 | if (ofp->ch_layout.nb_channels) { | |
| 2806 | 3 | ret = av_channel_layout_copy(&frame->ch_layout, &ofp->ch_layout); | |
| 2807 | 3 | if (ret < 0) | |
| 2808 | ✗ | return ret; | |
| 2809 | } | ||
| 2810 | |||
| 2811 | 5 | fd = frame_data(frame); | |
| 2812 | 5 | if (!fd) | |
| 2813 | ✗ | return AVERROR(ENOMEM); | |
| 2814 | |||
| 2815 | 5 | av_frame_side_data_free(&fd->side_data, &fd->nb_side_data); | |
| 2816 | 5 | ret = clone_side_data(&fd->side_data, &fd->nb_side_data, | |
| 2817 | 5 | ofp->side_data, ofp->nb_side_data, 0); | |
| 2818 | 5 | if (ret < 0) | |
| 2819 | ✗ | return ret; | |
| 2820 | |||
| 2821 | 5 | fd->frame_rate_filter = ofp->fps.framerate; | |
| 2822 | |||
| 2823 | 5 | av_assert0(!frame->buf[0]); | |
| 2824 | 5 | frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF; | |
| 2825 | |||
| 2826 | 5 | av_log(ofp, AV_LOG_WARNING, | |
| 2827 | "No filtered frames for output stream, trying to " | ||
| 2828 | "initialize anyway.\n"); | ||
| 2829 | |||
| 2830 | 5 | ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, frame); | |
| 2831 | 5 | if (ret < 0) { | |
| 2832 | ✗ | av_frame_unref(frame); | |
| 2833 | ✗ | return ret; | |
| 2834 | } | ||
| 2835 | } | ||
| 2836 | |||
| 2837 | 5604 | fgt->eof_out[ofp->ofilter.index] = 1; | |
| 2838 | |||
| 2839 | 5604 | ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, NULL); | |
| 2840 | 5604 | return (ret == AVERROR_EOF) ? 0 : ret; | |
| 2841 | } | ||
| 2842 | |||
| 2843 | 847143 | static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt, | |
| 2844 | AVFrame *frame) | ||
| 2845 | { | ||
| 2846 | 847143 | FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph); | |
| 2847 | 847143 | AVFrame *frame_prev = ofp->fps.last_frame; | |
| 2848 | 847143 | enum AVMediaType type = ofp->ofilter.type; | |
| 2849 | |||
| 2850 | 847143 | int64_t nb_frames = !!frame, nb_frames_prev = 0; | |
| 2851 | |||
| 2852 | 847143 | if (type == AVMEDIA_TYPE_VIDEO && (frame || fgt->got_frame)) | |
| 2853 | 156895 | video_sync_process(ofp, frame, &nb_frames, &nb_frames_prev); | |
| 2854 | |||
| 2855 | 1685437 | for (int64_t i = 0; i < nb_frames; i++) { | |
| 2856 | AVFrame *frame_out; | ||
| 2857 | int ret; | ||
| 2858 | |||
| 2859 | 841497 | if (type == AVMEDIA_TYPE_VIDEO) { | |
| 2860 | 152903 | AVFrame *frame_in = (i < nb_frames_prev && frame_prev->buf[0]) ? | |
| 2861 | 152903 | frame_prev : frame; | |
| 2862 | 152903 | if (!frame_in) | |
| 2863 | ✗ | break; | |
| 2864 | |||
| 2865 | 152903 | frame_out = fgp->frame_enc; | |
| 2866 | 152903 | ret = av_frame_ref(frame_out, frame_in); | |
| 2867 | 152903 | if (ret < 0) | |
| 2868 | ✗ | return ret; | |
| 2869 | |||
| 2870 | 152903 | frame_out->pts = ofp->next_pts; | |
| 2871 | |||
| 2872 | 152903 | if (ofp->fps.dropped_keyframe) { | |
| 2873 | 7 | frame_out->flags |= AV_FRAME_FLAG_KEY; | |
| 2874 | 7 | ofp->fps.dropped_keyframe = 0; | |
| 2875 | } | ||
| 2876 | } else { | ||
| 2877 | 688594 | frame->pts = (frame->pts == AV_NOPTS_VALUE) ? ofp->next_pts : | |
| 2878 | 688594 | av_rescale_q(frame->pts, frame->time_base, ofp->tb_out) - | |
| 2879 | 688594 | av_rescale_q(ofp->ts_offset, AV_TIME_BASE_Q, ofp->tb_out); | |
| 2880 | |||
| 2881 | 688594 | frame->time_base = ofp->tb_out; | |
| 2882 | 688594 | frame->duration = av_rescale_q(frame->nb_samples, | |
| 2883 | 688594 | (AVRational){ 1, frame->sample_rate }, | |
| 2884 | ofp->tb_out); | ||
| 2885 | |||
| 2886 | 688594 | ofp->next_pts = frame->pts + frame->duration; | |
| 2887 | |||
| 2888 | 688594 | frame_out = frame; | |
| 2889 | } | ||
| 2890 | |||
| 2891 | // send the frame to consumers | ||
| 2892 | 841497 | ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, frame_out); | |
| 2893 | 841497 | if (ret < 0) { | |
| 2894 | 3203 | av_frame_unref(frame_out); | |
| 2895 | |||
| 2896 | 3203 | if (!fgt->eof_out[ofp->ofilter.index]) { | |
| 2897 | 3203 | fgt->eof_out[ofp->ofilter.index] = 1; | |
| 2898 | 3203 | fgp->nb_outputs_done++; | |
| 2899 | } | ||
| 2900 | |||
| 2901 | 3203 | return ret == AVERROR_EOF ? 0 : ret; | |
| 2902 | } | ||
| 2903 | |||
| 2904 | 838294 | if (type == AVMEDIA_TYPE_VIDEO) { | |
| 2905 | 149745 | ofp->fps.frame_number++; | |
| 2906 | 149745 | ofp->next_pts++; | |
| 2907 | |||
| 2908 | 149745 | if (i == nb_frames_prev && frame) | |
| 2909 | 149313 | frame->flags &= ~AV_FRAME_FLAG_KEY; | |
| 2910 | } | ||
| 2911 | |||
| 2912 | 838294 | fgt->got_frame = 1; | |
| 2913 | } | ||
| 2914 | |||
| 2915 | 843940 | if (frame && frame_prev) { | |
| 2916 | 149786 | av_frame_unref(frame_prev); | |
| 2917 | 149786 | av_frame_move_ref(frame_prev, frame); | |
| 2918 | } | ||
| 2919 | |||
| 2920 | 843940 | if (!frame) | |
| 2921 | 5604 | return close_output(ofp, fgt); | |
| 2922 | |||
| 2923 | 838336 | return 0; | |
| 2924 | } | ||
| 2925 | |||
| 2926 | 1715054 | static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, | |
| 2927 | AVFrame *frame) | ||
| 2928 | { | ||
| 2929 | 1715054 | FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph); | |
| 2930 | 1715054 | FilterGraph *fg = &fgp->fg; | |
| 2931 | 1715054 | AVFilterContext *filter = ofp->ofilter.filter; | |
| 2932 | FrameData *fd; | ||
| 2933 | int ret; | ||
| 2934 | |||
| 2935 | 1715054 | ret = av_buffersink_get_frame_flags(filter, frame, | |
| 2936 | AV_BUFFERSINK_FLAG_NO_REQUEST); | ||
| 2937 | 1715054 | if (ret == AVERROR_EOF && !fgt->eof_out[ofp->ofilter.index]) { | |
| 2938 | 3861 | ret = fg_output_frame(ofp, fgt, NULL); | |
| 2939 | 3861 | return (ret < 0) ? ret : 1; | |
| 2940 | 1711193 | } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { | |
| 2941 | 869652 | return 1; | |
| 2942 | 841541 | } else if (ret < 0) { | |
| 2943 | ✗ | av_log(ofp, AV_LOG_WARNING, | |
| 2944 | "Error in retrieving a frame from the filtergraph: %s\n", | ||
| 2945 | ✗ | av_err2str(ret)); | |
| 2946 | ✗ | return ret; | |
| 2947 | } | ||
| 2948 | |||
| 2949 | 841541 | if (fgt->eof_out[ofp->ofilter.index]) { | |
| 2950 | 2 | av_frame_unref(frame); | |
| 2951 | 2 | return 0; | |
| 2952 | } | ||
| 2953 | |||
| 2954 | 841539 | frame->time_base = av_buffersink_get_time_base(filter); | |
| 2955 | |||
| 2956 | 841539 | if (debug_ts) | |
| 2957 | ✗ | av_log(ofp, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n", | |
| 2958 | ✗ | av_ts2str(frame->pts), av_ts2timestr(frame->pts, &frame->time_base), | |
| 2959 | frame->time_base.num, frame->time_base.den); | ||
| 2960 | |||
| 2961 | // Choose the output timebase the first time we get a frame. | ||
| 2962 | 841539 | if (!ofp->tb_out_locked) { | |
| 2963 | 8802 | ret = choose_out_timebase(ofp, frame); | |
| 2964 | 8802 | if (ret < 0) { | |
| 2965 | ✗ | av_log(ofp, AV_LOG_ERROR, "Could not choose an output time base\n"); | |
| 2966 | ✗ | av_frame_unref(frame); | |
| 2967 | ✗ | return ret; | |
| 2968 | } | ||
| 2969 | } | ||
| 2970 | |||
| 2971 | 841539 | fd = frame_data(frame); | |
| 2972 | 841539 | if (!fd) { | |
| 2973 | ✗ | av_frame_unref(frame); | |
| 2974 | ✗ | return AVERROR(ENOMEM); | |
| 2975 | } | ||
| 2976 | |||
| 2977 | 841539 | av_frame_side_data_free(&fd->side_data, &fd->nb_side_data); | |
| 2978 | 841539 | if (!fgt->got_frame) { | |
| 2979 | 8568 | ret = clone_side_data(&fd->side_data, &fd->nb_side_data, | |
| 2980 | 8568 | ofp->side_data, ofp->nb_side_data, 0); | |
| 2981 | 8568 | if (ret < 0) { | |
| 2982 | ✗ | av_frame_unref(frame); | |
| 2983 | ✗ | return ret; | |
| 2984 | } | ||
| 2985 | } | ||
| 2986 | |||
| 2987 | // Copy global side data to the output frame for the cases the | ||
| 2988 | // filtergraph is internal (e.g. created to merge a Tile Grid) | ||
| 2989 | // which don't come from a decoder that would have already | ||
| 2990 | // copied said side data from the global decoder context. | ||
| 2991 | 841539 | if (fg->is_internal) { | |
| 2992 | 4 | ret = clone_side_data(&frame->side_data, &frame->nb_side_data, | |
| 2993 | 4 | ofp->side_data, ofp->nb_side_data, | |
| 2994 | AV_FRAME_SIDE_DATA_FLAG_REPLACE); | ||
| 2995 | 4 | if (ret < 0 && ret != AVERROR(EEXIST)) { | |
| 2996 | ✗ | av_frame_unref(frame); | |
| 2997 | ✗ | return ret; | |
| 2998 | } | ||
| 2999 | } | ||
| 3000 | |||
| 3001 | 841539 | fd->wallclock[LATENCY_PROBE_FILTER_POST] = av_gettime_relative(); | |
| 3002 | |||
| 3003 | // only use bits_per_raw_sample passed through from the decoder | ||
| 3004 | // if the filtergraph did not touch the frame data | ||
| 3005 | 841539 | if (!fgp->is_meta) | |
| 3006 | 725600 | fd->bits_per_raw_sample = 0; | |
| 3007 | |||
| 3008 | 841539 | if (ofp->ofilter.type == AVMEDIA_TYPE_VIDEO) { | |
| 3009 | 152945 | if (!frame->duration) { | |
| 3010 | 2432 | AVRational fr = av_buffersink_get_frame_rate(filter); | |
| 3011 | 2432 | if (fr.num > 0 && fr.den > 0) | |
| 3012 | 1798 | frame->duration = av_rescale_q(1, av_inv_q(fr), frame->time_base); | |
| 3013 | } | ||
| 3014 | |||
| 3015 | 152945 | fd->frame_rate_filter = ofp->fps.framerate; | |
| 3016 | } | ||
| 3017 | |||
| 3018 | 841539 | ret = fg_output_frame(ofp, fgt, frame); | |
| 3019 | 841539 | av_frame_unref(frame); | |
| 3020 | 841539 | if (ret < 0) | |
| 3021 | ✗ | return ret; | |
| 3022 | |||
| 3023 | 841539 | return 0; | |
| 3024 | } | ||
| 3025 | |||
| 3026 | /* retrieve all frames available at filtergraph outputs | ||
| 3027 | * and send them to consumers */ | ||
| 3028 | 850934 | static int read_frames(FilterGraph *fg, FilterGraphThread *fgt, | |
| 3029 | AVFrame *frame) | ||
| 3030 | { | ||
| 3031 | 850934 | FilterGraphPriv *fgp = fgp_from_fg(fg); | |
| 3032 | |||
| 3033 | // graph not configured, just select the input to request | ||
| 3034 | 850934 | if (!fgt->graph) { | |
| 3035 | 527 | for (int i = 0; i < fg->nb_inputs; i++) { | |
| 3036 | 527 | InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]); | |
| 3037 | 527 | if (ifp->format < 0 && !fgt->eof_in[i]) { | |
| 3038 | 232 | fgt->next_in = i; | |
| 3039 | 232 | return 0; | |
| 3040 | } | ||
| 3041 | } | ||
| 3042 | |||
| 3043 | // This state - graph is not configured, but all inputs are either | ||
| 3044 | // initialized or EOF - should be unreachable because sending EOF to a | ||
| 3045 | // filter without even a fallback format should fail | ||
| 3046 | ✗ | av_assert0(0); | |
| 3047 | return AVERROR_BUG; | ||
| 3048 | } | ||
| 3049 | |||
| 3050 | 850702 | if (fgp->nb_outputs_done < fg->nb_outputs) { | |
| 3051 | int ret; | ||
| 3052 | |||
| 3053 | /* Reap all buffers present in the buffer sinks */ | ||
| 3054 | 1721036 | for (int i = 0; i < fg->nb_outputs; i++) { | |
| 3055 | 873513 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]); | |
| 3056 | |||
| 3057 | 873513 | ret = 0; | |
| 3058 | 2588567 | while (!ret) { | |
| 3059 | 1715054 | ret = fg_output_step(ofp, fgt, frame); | |
| 3060 | 1715054 | if (ret < 0) | |
| 3061 | ✗ | return ret; | |
| 3062 | } | ||
| 3063 | } | ||
| 3064 | |||
| 3065 | |||
| 3066 | 847523 | ret = avfilter_graph_request_oldest(fgt->graph); | |
| 3067 | |||
| 3068 | // ensure all inputs no longer accepting data are closed | ||
| 3069 | 1678239 | for (int i = 0; i < fg->nb_inputs; i++) { | |
| 3070 | 830716 | InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]); | |
| 3071 | 830716 | if (av_buffersrc_get_status(ifp->ifilter.filter)) | |
| 3072 | 6115 | close_input(ifp); | |
| 3073 | } | ||
| 3074 | |||
| 3075 | 847523 | if (ret == AVERROR(EAGAIN)) { | |
| 3076 | 809205 | fgt->next_in = choose_input(fg, fgt); | |
| 3077 | 809205 | return 0; | |
| 3078 | 38318 | } else if (ret < 0) { | |
| 3079 | 5378 | if (ret == AVERROR_EOF) | |
| 3080 | 5378 | av_log(fg, AV_LOG_VERBOSE, "Filtergraph returned EOF, finishing\n"); | |
| 3081 | else | ||
| 3082 | ✗ | av_log(fg, AV_LOG_ERROR, | |
| 3083 | "Error requesting a frame from the filtergraph: %s\n", | ||
| 3084 | ✗ | av_err2str(ret)); | |
| 3085 | 5378 | return ret; | |
| 3086 | } | ||
| 3087 | 32940 | fgt->next_in = fg->nb_inputs; | |
| 3088 | |||
| 3089 | // return so that scheduler can rate-control us | ||
| 3090 | 32940 | return 0; | |
| 3091 | } | ||
| 3092 | |||
| 3093 | 3179 | return AVERROR_EOF; | |
| 3094 | } | ||
| 3095 | |||
| 3096 | 948 | static void sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb) | |
| 3097 | { | ||
| 3098 | 948 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 3099 | int64_t pts2; | ||
| 3100 | |||
| 3101 | /* subtitles seem to be usually muxed ahead of other streams; | ||
| 3102 | if not, subtracting a larger time here is necessary */ | ||
| 3103 | 948 | pts2 = av_rescale_q(pts, tb, ifp->time_base) - 1; | |
| 3104 | |||
| 3105 | /* do not send the heartbeat frame if the subtitle is already ahead */ | ||
| 3106 | 948 | if (pts2 <= ifp->sub2video.last_pts) | |
| 3107 | 769 | return; | |
| 3108 | |||
| 3109 | 179 | if (pts2 >= ifp->sub2video.end_pts || ifp->sub2video.initialize) | |
| 3110 | /* if we have hit the end of the current displayed subpicture, | ||
| 3111 | or if we need to initialize the system, update the | ||
| 3112 | overlaid subpicture and its start/end times */ | ||
| 3113 | 88 | sub2video_update(ifp, pts2 + 1, NULL); | |
| 3114 | else | ||
| 3115 | 91 | sub2video_push_ref(ifp, pts2); | |
| 3116 | } | ||
| 3117 | |||
| 3118 | 1054 | static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer) | |
| 3119 | { | ||
| 3120 | 1054 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 3121 | int ret; | ||
| 3122 | |||
| 3123 | 1054 | if (buffer) { | |
| 3124 | // queue a NULL entry for EOF, so it is not lost when | ||
| 3125 | // the queue is replayed after configuring the graph | ||
| 3126 | 14 | AVFrame *tmp = NULL; | |
| 3127 | |||
| 3128 | 14 | if (frame) { | |
| 3129 | 14 | tmp = av_frame_alloc(); | |
| 3130 | 14 | if (!tmp) | |
| 3131 | ✗ | return AVERROR(ENOMEM); | |
| 3132 | |||
| 3133 | 14 | av_frame_move_ref(tmp, frame); | |
| 3134 | } | ||
| 3135 | |||
| 3136 | 14 | ret = av_fifo_write(ifp->frame_queue, &tmp, 1); | |
| 3137 | 14 | if (ret < 0) { | |
| 3138 | ✗ | av_frame_free(&tmp); | |
| 3139 | ✗ | return ret; | |
| 3140 | } | ||
| 3141 | |||
| 3142 | 14 | return 0; | |
| 3143 | } | ||
| 3144 | |||
| 3145 | // heartbeat frame | ||
| 3146 | 1040 | if (frame && !frame->buf[0]) { | |
| 3147 | 948 | sub2video_heartbeat(ifilter, frame->pts, frame->time_base); | |
| 3148 | 948 | return 0; | |
| 3149 | } | ||
| 3150 | |||
| 3151 | 92 | if (!frame) { | |
| 3152 | 4 | if (ifp->sub2video.end_pts < INT64_MAX) | |
| 3153 | 4 | sub2video_update(ifp, INT64_MAX, NULL); | |
| 3154 | |||
| 3155 | 4 | return av_buffersrc_add_frame(ifilter->filter, NULL); | |
| 3156 | } | ||
| 3157 | |||
| 3158 | 88 | ifp->width = frame->width ? frame->width : ifp->width; | |
| 3159 | 88 | ifp->height = frame->height ? frame->height : ifp->height; | |
| 3160 | |||
| 3161 | 88 | sub2video_update(ifp, INT64_MIN, (const AVSubtitle*)frame->buf[0]->data); | |
| 3162 | |||
| 3163 | 88 | return 0; | |
| 3164 | } | ||
| 3165 | |||
| 3166 | 3613 | static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter, | |
| 3167 | int64_t pts, AVRational tb) | ||
| 3168 | { | ||
| 3169 | 3613 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 3170 | int ret; | ||
| 3171 | |||
| 3172 | 3613 | if (fgt->eof_in[ifilter->index]) | |
| 3173 | ✗ | return 0; | |
| 3174 | |||
| 3175 | 3613 | fgt->eof_in[ifilter->index] = 1; | |
| 3176 | |||
| 3177 | 3613 | if (ifilter->filter) { | |
| 3178 | 3595 | pts = av_rescale_q_rnd(pts, tb, ifp->time_base, | |
| 3179 | AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); | ||
| 3180 | |||
| 3181 | 3595 | ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH); | |
| 3182 | 3595 | if (ret < 0) | |
| 3183 | ✗ | return ret; | |
| 3184 | } else { | ||
| 3185 | 18 | if (ifp->format < 0 && ifp->opts.fallback) { | |
| 3186 | // the filtergraph was never configured, use the fallback parameters | ||
| 3187 | 3 | ifp->format = ifp->opts.fallback->format; | |
| 3188 | 3 | ifp->sample_rate = ifp->opts.fallback->sample_rate; | |
| 3189 | 3 | ifp->width = ifp->opts.fallback->width; | |
| 3190 | 3 | ifp->height = ifp->opts.fallback->height; | |
| 3191 | 3 | ifp->sample_aspect_ratio = ifp->opts.fallback->sample_aspect_ratio; | |
| 3192 | 3 | ifp->color_space = ifp->opts.fallback->colorspace; | |
| 3193 | 3 | ifp->color_range = ifp->opts.fallback->color_range; | |
| 3194 | 3 | ifp->alpha_mode = ifp->opts.fallback->alpha_mode; | |
| 3195 | 3 | ifp->chroma_location = ifp->opts.fallback->chroma_location; | |
| 3196 | 3 | ifp->time_base = ifp->opts.fallback->time_base; | |
| 3197 | |||
| 3198 | 3 | ret = av_channel_layout_copy(&ifp->ch_layout, | |
| 3199 | 3 | &ifp->opts.fallback->ch_layout); | |
| 3200 | 3 | if (ret < 0) | |
| 3201 | ✗ | return ret; | |
| 3202 | |||
| 3203 | 3 | av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data); | |
| 3204 | 3 | ret = clone_side_data(&ifp->side_data, &ifp->nb_side_data, | |
| 3205 | 3 | ifp->opts.fallback->side_data, | |
| 3206 | 3 | ifp->opts.fallback->nb_side_data, 0); | |
| 3207 | 3 | if (ret < 0) | |
| 3208 | ✗ | return ret; | |
| 3209 | |||
| 3210 | 3 | if (ifilter_has_all_input_formats(ifilter->graph)) { | |
| 3211 | 3 | ret = configure_filtergraph(ifilter->graph, fgt); | |
| 3212 | 3 | if (ret < 0) { | |
| 3213 | ✗ | av_log(ifilter->graph, AV_LOG_ERROR, "Error initializing filters!\n"); | |
| 3214 | ✗ | return ret; | |
| 3215 | } | ||
| 3216 | } | ||
| 3217 | } | ||
| 3218 | |||
| 3219 | 18 | if (ifp->format < 0) { | |
| 3220 | ✗ | av_log(ifilter->graph, AV_LOG_ERROR, | |
| 3221 | "Cannot determine format of input %s after EOF\n", | ||
| 3222 | ifp->opts.name); | ||
| 3223 | ✗ | return AVERROR_INVALIDDATA; | |
| 3224 | } | ||
| 3225 | } | ||
| 3226 | |||
| 3227 | 3613 | return 0; | |
| 3228 | } | ||
| 3229 | |||
| 3230 | enum ReinitReason { | ||
| 3231 | VIDEO_CHANGED = (1 << 0), | ||
| 3232 | AUDIO_CHANGED = (1 << 1), | ||
| 3233 | MATRIX_CHANGED = (1 << 2), | ||
| 3234 | DOWNMIX_CHANGED = (1 << 3), | ||
| 3235 | HWACCEL_CHANGED = (1 << 4) | ||
| 3236 | }; | ||
| 3237 | |||
| 3238 | 1519 | static const char *unknown_if_null(const char *str) | |
| 3239 | { | ||
| 3240 | 1519 | return str ? str : "unknown"; | |
| 3241 | } | ||
| 3242 | |||
| 3243 | 812044 | static int send_frame(FilterGraph *fg, FilterGraphThread *fgt, | |
| 3244 | InputFilter *ifilter, AVFrame *frame, int force_reinit) | ||
| 3245 | { | ||
| 3246 | 812044 | FilterGraphPriv *fgp = fgp_from_fg(fg); | |
| 3247 | 812044 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | |
| 3248 | FrameData *fd; | ||
| 3249 | AVFrameSideData *sd; | ||
| 3250 | 812044 | int need_reinit = 0, ret; | |
| 3251 | |||
| 3252 | /* determine if the parameters for this input changed */ | ||
| 3253 | 812044 | switch (ifilter->type) { | |
| 3254 | 685019 | case AVMEDIA_TYPE_AUDIO: | |
| 3255 | 685019 | if (ifp->format != frame->format || | |
| 3256 | 1366971 | ifp->sample_rate != frame->sample_rate || | |
| 3257 | 683485 | av_channel_layout_compare(&ifp->ch_layout, &frame->ch_layout)) | |
| 3258 | 1535 | need_reinit |= AUDIO_CHANGED; | |
| 3259 | 685019 | break; | |
| 3260 | 127025 | case AVMEDIA_TYPE_VIDEO: | |
| 3261 | 127025 | if (ifp->format != frame->format || | |
| 3262 | 121042 | ifp->width != frame->width || | |
| 3263 | 120997 | ifp->height != frame->height || | |
| 3264 | 120750 | ifp->color_space != frame->colorspace || | |
| 3265 | 120733 | ifp->color_range != frame->color_range || | |
| 3266 | 120733 | ifp->alpha_mode != frame->alpha_mode || | |
| 3267 | 120733 | ifp->chroma_location != frame->chroma_location) | |
| 3268 | 6292 | need_reinit |= VIDEO_CHANGED; | |
| 3269 | 127025 | break; | |
| 3270 | } | ||
| 3271 | |||
| 3272 | 812044 | if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) { | |
| 3273 | 221 | if (!ifp->displaymatrix_present || | |
| 3274 | 194 | memcmp(sd->data, ifp->displaymatrix, sizeof(ifp->displaymatrix))) | |
| 3275 | 27 | need_reinit |= MATRIX_CHANGED; | |
| 3276 | 811823 | } else if (ifp->displaymatrix_present) | |
| 3277 | ✗ | need_reinit |= MATRIX_CHANGED; | |
| 3278 | |||
| 3279 | 812044 | if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_INFO)) { | |
| 3280 | 720 | if (!ifp->downmixinfo_present || | |
| 3281 | 712 | memcmp(sd->data, &ifp->downmixinfo, sizeof(ifp->downmixinfo))) | |
| 3282 | 8 | need_reinit |= DOWNMIX_CHANGED; | |
| 3283 | 811324 | } else if (ifp->downmixinfo_present) | |
| 3284 | ✗ | need_reinit |= DOWNMIX_CHANGED; | |
| 3285 | |||
| 3286 | 812044 | if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_MATRIX)) { | |
| 3287 | 22 | if (!ifp->downmixmatrix_present || | |
| 3288 | 18 | sd->size != ifp->downmixmatrix_size || memcmp(sd->data, ifp->downmixmatrix->data, sd->size)) | |
| 3289 | 4 | need_reinit |= DOWNMIX_CHANGED; | |
| 3290 | 812022 | } else if (ifp->downmixmatrix_present) | |
| 3291 | ✗ | need_reinit |= DOWNMIX_CHANGED; | |
| 3292 | |||
| 3293 | 812044 | if (need_reinit && fgt->graph && (ifp->opts.flags & IFILTER_FLAG_DROPCHANGED)) { | |
| 3294 | ✗ | ifp->nb_dropped++; | |
| 3295 | ✗ | av_log_once(fg, AV_LOG_WARNING, AV_LOG_DEBUG, &ifp->drop_warned, "Avoiding reinit; dropping frame pts: %s bound for %s\n", av_ts2str(frame->pts), ifilter->name); | |
| 3296 | ✗ | av_frame_unref(frame); | |
| 3297 | ✗ | return 0; | |
| 3298 | } | ||
| 3299 | |||
| 3300 | 812044 | if (!(ifp->opts.flags & IFILTER_FLAG_REINIT) && fgt->graph) | |
| 3301 | 182 | need_reinit = 0; | |
| 3302 | |||
| 3303 | 812044 | if (!!ifp->hw_frames_ctx != !!frame->hw_frames_ctx || | |
| 3304 | 812044 | (ifp->hw_frames_ctx && ifp->hw_frames_ctx->data != frame->hw_frames_ctx->data)) | |
| 3305 | ✗ | need_reinit |= HWACCEL_CHANGED; | |
| 3306 | |||
| 3307 | 812044 | if (need_reinit) { | |
| 3308 | 7731 | ret = ifilter_parameters_from_frame(ifilter, frame); | |
| 3309 | 7731 | if (ret < 0) | |
| 3310 | ✗ | return ret; | |
| 3311 | |||
| 3312 | /* Inputs bound to a filtergraph output will have some fields unset. | ||
| 3313 | * Handle them here */ | ||
| 3314 | 7731 | if (ifp->ofilter_src) { | |
| 3315 | 1 | ret = ifilter_parameters_from_ofilter(ifilter, ifp->ofilter_src); | |
| 3316 | 1 | if (ret < 0) | |
| 3317 | ✗ | return ret; | |
| 3318 | } | ||
| 3319 | } | ||
| 3320 | |||
| 3321 | /* (re)init the graph if possible, otherwise buffer the frame and return */ | ||
| 3322 | 812044 | if (need_reinit || force_reinit || !fgt->graph) { | |
| 3323 | 7827 | AVFrame *tmp = av_frame_alloc(); | |
| 3324 | |||
| 3325 | 7827 | if (!tmp) | |
| 3326 | 206 | return AVERROR(ENOMEM); | |
| 3327 | |||
| 3328 | 7827 | if (!ifilter_has_all_input_formats(fg)) { | |
| 3329 | 203 | av_frame_move_ref(tmp, frame); | |
| 3330 | |||
| 3331 | 203 | ret = av_fifo_write(ifp->frame_queue, &tmp, 1); | |
| 3332 | 203 | if (ret < 0) | |
| 3333 | ✗ | av_frame_free(&tmp); | |
| 3334 | |||
| 3335 | 203 | return ret; | |
| 3336 | } | ||
| 3337 | |||
| 3338 | 7624 | ret = fgt->graph ? read_frames(fg, fgt, tmp) : 0; | |
| 3339 | 7624 | av_frame_free(&tmp); | |
| 3340 | 7624 | if (ret < 0) | |
| 3341 | 1 | return ret; | |
| 3342 | |||
| 3343 | 7623 | if (fgt->graph) { | |
| 3344 | AVBPrint reason; | ||
| 3345 | 307 | av_bprint_init(&reason, 0, AV_BPRINT_SIZE_AUTOMATIC); | |
| 3346 | 307 | if (need_reinit & AUDIO_CHANGED) { | |
| 3347 | 4 | const char *sample_format_name = av_get_sample_fmt_name(frame->format); | |
| 3348 | 4 | av_bprintf(&reason, "audio parameters changed to %d Hz, ", frame->sample_rate); | |
| 3349 | 4 | av_channel_layout_describe_bprint(&frame->ch_layout, &reason); | |
| 3350 | 4 | av_bprintf(&reason, ", %s, ", unknown_if_null(sample_format_name)); | |
| 3351 | } | ||
| 3352 | 307 | if (need_reinit & VIDEO_CHANGED) { | |
| 3353 | 303 | const char *pixel_format_name = av_get_pix_fmt_name(frame->format); | |
| 3354 | 303 | const char *color_space_name = av_color_space_name(frame->colorspace); | |
| 3355 | 303 | const char *color_range_name = av_color_range_name(frame->color_range); | |
| 3356 | 303 | const char *alpha_mode = av_alpha_mode_name(frame->alpha_mode); | |
| 3357 | 303 | const char *chroma_location = av_chroma_location_name(frame->chroma_location); | |
| 3358 | 303 | av_bprintf(&reason, "video parameters changed to %s(%s, %s), %dx%d, %s alpha, %s chroma, ", | |
| 3359 | unknown_if_null(pixel_format_name), unknown_if_null(color_range_name), | ||
| 3360 | unknown_if_null(color_space_name), frame->width, frame->height, | ||
| 3361 | unknown_if_null(alpha_mode), unknown_if_null(chroma_location)); | ||
| 3362 | } | ||
| 3363 | 307 | if (need_reinit & MATRIX_CHANGED) | |
| 3364 | ✗ | av_bprintf(&reason, "display matrix changed, "); | |
| 3365 | 307 | if (need_reinit & DOWNMIX_CHANGED) | |
| 3366 | ✗ | av_bprintf(&reason, "downmix medatata changed, "); | |
| 3367 | 307 | if (need_reinit & HWACCEL_CHANGED) | |
| 3368 | ✗ | av_bprintf(&reason, "hwaccel changed, "); | |
| 3369 | 307 | if (force_reinit) | |
| 3370 | ✗ | av_bprintf(&reason, "reinitialization arguments were provided, "); | |
| 3371 | 307 | if (reason.len > 1) | |
| 3372 | 307 | reason.str[reason.len - 2] = '\0'; // remove last comma | |
| 3373 | 307 | av_log(fg, AV_LOG_INFO, "Reconfiguring filter graph%s%s\n", reason.len ? " because " : "", reason.str); | |
| 3374 | } else { | ||
| 3375 | /* Choke all input to avoid buffering excessive frames while the | ||
| 3376 | * initial filter graph is being configured, and before we have a | ||
| 3377 | * preferred input */ | ||
| 3378 | 7316 | sch_filter_choke_inputs(fgp->sch, fgp->sch_idx); | |
| 3379 | } | ||
| 3380 | |||
| 3381 | 7623 | ret = configure_filtergraph(fg, fgt); | |
| 3382 | 7623 | if (ret < 0) { | |
| 3383 | 2 | av_log(fg, AV_LOG_ERROR, "Error reinitializing filters!\n"); | |
| 3384 | 2 | return ret; | |
| 3385 | } | ||
| 3386 | } | ||
| 3387 | |||
| 3388 | 811838 | frame->pts = av_rescale_q(frame->pts, frame->time_base, ifp->time_base); | |
| 3389 | 811838 | frame->duration = av_rescale_q(frame->duration, frame->time_base, ifp->time_base); | |
| 3390 | 811838 | frame->time_base = ifp->time_base; | |
| 3391 | |||
| 3392 | 811838 | if (ifp->displaymatrix_applied) | |
| 3393 | 126918 | av_frame_remove_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX); | |
| 3394 | |||
| 3395 | 811838 | fd = frame_data(frame); | |
| 3396 | 811838 | if (!fd) | |
| 3397 | ✗ | return AVERROR(ENOMEM); | |
| 3398 | 811838 | fd->wallclock[LATENCY_PROBE_FILTER_PRE] = av_gettime_relative(); | |
| 3399 | |||
| 3400 | 811838 | ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, | |
| 3401 | AV_BUFFERSRC_FLAG_PUSH); | ||
| 3402 | 811838 | if (ret < 0) { | |
| 3403 | ✗ | av_frame_unref(frame); | |
| 3404 | ✗ | if (ret != AVERROR_EOF) | |
| 3405 | ✗ | av_log(fg, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret)); | |
| 3406 | ✗ | return ret; | |
| 3407 | } | ||
| 3408 | |||
| 3409 | 811838 | return 0; | |
| 3410 | } | ||
| 3411 | |||
| 3412 | 8574 | static void fg_thread_set_name(const FilterGraph *fg) | |
| 3413 | { | ||
| 3414 | char name[16]; | ||
| 3415 | 8574 | if (filtergraph_is_simple(fg)) { | |
| 3416 | 7242 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]); | |
| 3417 | 14484 | snprintf(name, sizeof(name), "%cf%s", | |
| 3418 | 7242 | av_get_media_type_string(ofp->ofilter.type)[0], | |
| 3419 | ofp->ofilter.output_name); | ||
| 3420 | } else { | ||
| 3421 | 1332 | snprintf(name, sizeof(name), "fc%d", fg->index); | |
| 3422 | } | ||
| 3423 | |||
| 3424 | 8574 | ff_thread_setname(name); | |
| 3425 | 8574 | } | |
| 3426 | |||
| 3427 | 8574 | static void fg_thread_uninit(FilterGraphThread *fgt) | |
| 3428 | { | ||
| 3429 | 8574 | if (fgt->frame_queue_out) { | |
| 3430 | AVFrame *frame; | ||
| 3431 | 8574 | while (av_fifo_read(fgt->frame_queue_out, &frame, 1) >= 0) | |
| 3432 | ✗ | av_frame_free(&frame); | |
| 3433 | 8574 | av_fifo_freep2(&fgt->frame_queue_out); | |
| 3434 | } | ||
| 3435 | |||
| 3436 | 8574 | av_frame_free(&fgt->frame); | |
| 3437 | 8574 | av_freep(&fgt->eof_in); | |
| 3438 | 8574 | av_freep(&fgt->eof_out); | |
| 3439 | |||
| 3440 | 8574 | avfilter_graph_free(&fgt->graph); | |
| 3441 | |||
| 3442 | 8574 | memset(fgt, 0, sizeof(*fgt)); | |
| 3443 | 8574 | } | |
| 3444 | |||
| 3445 | 8574 | static int fg_thread_init(FilterGraphThread *fgt, const FilterGraph *fg) | |
| 3446 | { | ||
| 3447 | 8574 | memset(fgt, 0, sizeof(*fgt)); | |
| 3448 | |||
| 3449 | 8574 | fgt->frame = av_frame_alloc(); | |
| 3450 | 8574 | if (!fgt->frame) | |
| 3451 | ✗ | goto fail; | |
| 3452 | |||
| 3453 | 8574 | fgt->eof_in = av_calloc(fg->nb_inputs, sizeof(*fgt->eof_in)); | |
| 3454 | 8574 | if (!fgt->eof_in) | |
| 3455 | ✗ | goto fail; | |
| 3456 | |||
| 3457 | 8574 | fgt->eof_out = av_calloc(fg->nb_outputs, sizeof(*fgt->eof_out)); | |
| 3458 | 8574 | if (!fgt->eof_out) | |
| 3459 | ✗ | goto fail; | |
| 3460 | |||
| 3461 | 8574 | fgt->frame_queue_out = av_fifo_alloc2(1, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW); | |
| 3462 | 8574 | if (!fgt->frame_queue_out) | |
| 3463 | ✗ | goto fail; | |
| 3464 | |||
| 3465 | 8574 | return 0; | |
| 3466 | |||
| 3467 | ✗ | fail: | |
| 3468 | ✗ | fg_thread_uninit(fgt); | |
| 3469 | ✗ | return AVERROR(ENOMEM); | |
| 3470 | } | ||
| 3471 | |||
| 3472 | 812044 | static int check_reinit(FilterGraph *fg, FilterGraphThread *fgt) | |
| 3473 | { | ||
| 3474 | 812044 | int ret, reinit = 0; | |
| 3475 | |||
| 3476 | 1634528 | for (unsigned i = 0; i < fg->nb_outputs; i++) { | |
| 3477 | 822484 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]); | |
| 3478 | |||
| 3479 | 822484 | if (ofp->reinit_opts.pts == AV_NOPTS_VALUE && ofp->reinit_opts_fifo && | |
| 3480 | ✗ | av_fifo_can_read(ofp->reinit_opts_fifo)) { | |
| 3481 | ✗ | av_fifo_read(ofp->reinit_opts_fifo, &ofp->reinit_opts, 1); | |
| 3482 | } | ||
| 3483 | 822484 | if (ofp->reinit_opts.pts != AV_NOPTS_VALUE && | |
| 3484 | ✗ | ofp->reinit_opts.pts <= av_rescale_q(fgt->frame->pts, fgt->frame->time_base, AV_TIME_BASE_Q)) { | |
| 3485 | ✗ | FrameData *fd = frame_data(fgt->frame); | |
| 3486 | ✗ | if (!fd) | |
| 3487 | ✗ | return AVERROR(ENOMEM); | |
| 3488 | |||
| 3489 | ✗ | av_dict_free(&fd->reinit_opts); | |
| 3490 | ✗ | ret = av_dict_copy(&fd->reinit_opts, ofp->reinit_opts.dict, 0); | |
| 3491 | ✗ | if (ret < 0) | |
| 3492 | ✗ | return ret; | |
| 3493 | |||
| 3494 | ✗ | av_opt_set_dict(ofp, &ofp->reinit_opts.dict); | |
| 3495 | ✗ | av_dict_free(&ofp->reinit_opts.dict); | |
| 3496 | |||
| 3497 | ✗ | if (av_fifo_can_read(ofp->reinit_opts_fifo)) | |
| 3498 | ✗ | av_fifo_read(ofp->reinit_opts_fifo, &ofp->reinit_opts, 1); | |
| 3499 | else | ||
| 3500 | ✗ | ofp->reinit_opts = (ReinitOpts){ .pts = AV_NOPTS_VALUE }; | |
| 3501 | ✗ | reinit = 1; | |
| 3502 | } | ||
| 3503 | } | ||
| 3504 | |||
| 3505 | 812044 | return reinit; | |
| 3506 | } | ||
| 3507 | |||
| 3508 | 8574 | static int filter_thread(void *arg) | |
| 3509 | { | ||
| 3510 | 8574 | FilterGraphPriv *fgp = arg; | |
| 3511 | 8574 | FilterGraph *fg = &fgp->fg; | |
| 3512 | |||
| 3513 | FilterGraphThread fgt; | ||
| 3514 | 8574 | int ret = 0, input_status = 0; | |
| 3515 | |||
| 3516 | 8574 | ret = fg_thread_init(&fgt, fg); | |
| 3517 | 8574 | if (ret < 0) | |
| 3518 | ✗ | goto finish; | |
| 3519 | |||
| 3520 | 8574 | fg_thread_set_name(fg); | |
| 3521 | |||
| 3522 | // if we have all input parameters the graph can now be configured | ||
| 3523 | 8574 | if (ifilter_has_all_input_formats(fg)) { | |
| 3524 | 1255 | ret = configure_filtergraph(fg, &fgt); | |
| 3525 | 1255 | if (ret < 0) { | |
| 3526 | ✗ | av_log(fg, AV_LOG_ERROR, "Error configuring filter graph: %s\n", | |
| 3527 | ✗ | av_err2str(ret)); | |
| 3528 | ✗ | goto finish; | |
| 3529 | } | ||
| 3530 | } | ||
| 3531 | |||
| 3532 | 842070 | while (1) { | |
| 3533 | InputFilter *ifilter; | ||
| 3534 | 850644 | InputFilterPriv *ifp = NULL; | |
| 3535 | enum FrameOpaque o; | ||
| 3536 | 850644 | unsigned input_idx = fgt.next_in; | |
| 3537 | |||
| 3538 | 850644 | input_status = sch_filter_receive(fgp->sch, fgp->sch_idx, | |
| 3539 | 850644 | &input_idx, fgt.frame); | |
| 3540 | 850644 | if (input_status == AVERROR_EOF) { | |
| 3541 | 16 | av_log(fg, AV_LOG_VERBOSE, "Filtering thread received EOF\n"); | |
| 3542 | 16 | break; | |
| 3543 | 850628 | } else if (input_status == AVERROR(EAGAIN)) { | |
| 3544 | // should only happen when we didn't request any input | ||
| 3545 | 33931 | av_assert0(input_idx == fg->nb_inputs); | |
| 3546 | 33931 | goto read_frames; | |
| 3547 | } | ||
| 3548 | 816697 | av_assert0(input_status >= 0); | |
| 3549 | |||
| 3550 | 816697 | o = (intptr_t)fgt.frame->opaque; | |
| 3551 | |||
| 3552 | // message on the control stream | ||
| 3553 | 816697 | if (input_idx == fg->nb_inputs) { | |
| 3554 | FilterCommand *fc; | ||
| 3555 | |||
| 3556 | ✗ | av_assert0(o == FRAME_OPAQUE_SEND_COMMAND && fgt.frame->buf[0]); | |
| 3557 | |||
| 3558 | ✗ | fc = (FilterCommand*)fgt.frame->buf[0]->data; | |
| 3559 | ✗ | send_command(fg, fgt.graph, fc->time, fc->target, fc->command, fc->arg, | |
| 3560 | fc->all_filters); | ||
| 3561 | ✗ | av_frame_unref(fgt.frame); | |
| 3562 | ✗ | continue; | |
| 3563 | } | ||
| 3564 | |||
| 3565 | // we received an input frame or EOF | ||
| 3566 | 816697 | ifilter = fg->inputs[input_idx]; | |
| 3567 | 816697 | ifp = ifp_from_ifilter(ifilter); | |
| 3568 | |||
| 3569 | 816697 | if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) { | |
| 3570 | 1040 | int hb_frame = input_status >= 0 && o == FRAME_OPAQUE_SUB_HEARTBEAT; | |
| 3571 | 1040 | ret = sub2video_frame(ifilter, (fgt.frame->buf[0] || hb_frame) ? fgt.frame : NULL, | |
| 3572 | 1040 | !fgt.graph); | |
| 3573 | 815657 | } else if (fgt.frame->buf[0]) { | |
| 3574 | 812044 | ret = check_reinit(fg, &fgt); | |
| 3575 | 812044 | if (ret >= 0) | |
| 3576 | 812044 | ret = send_frame(fg, &fgt, ifilter, fgt.frame, ret); | |
| 3577 | } else { | ||
| 3578 | av_assert1(o == FRAME_OPAQUE_EOF); | ||
| 3579 | 3613 | ret = send_eof(&fgt, ifilter, fgt.frame->pts, fgt.frame->time_base); | |
| 3580 | } | ||
| 3581 | 816697 | av_frame_unref(fgt.frame); | |
| 3582 | 816697 | if (ret == AVERROR_EOF) { | |
| 3583 | 1 | av_log(fg, AV_LOG_VERBOSE, "Input %u no longer accepts new data\n", | |
| 3584 | input_idx); | ||
| 3585 | 1 | close_input(ifp); | |
| 3586 | 1 | goto read_frames; | |
| 3587 | } | ||
| 3588 | 816696 | if (ret < 0) | |
| 3589 | 2 | goto finish; | |
| 3590 | |||
| 3591 | 816694 | read_frames: | |
| 3592 | // retrieve all newly available frames | ||
| 3593 | 850626 | ret = read_frames(fg, &fgt, fgt.frame); | |
| 3594 | 850626 | if (ret == AVERROR_EOF) { | |
| 3595 | 8556 | av_log(fg, AV_LOG_VERBOSE, "All consumers returned EOF\n"); | |
| 3596 | 8556 | if (ifp && ifp->opts.flags & IFILTER_FLAG_DROPCHANGED) | |
| 3597 | ✗ | av_log(fg, AV_LOG_INFO, "Total changed input frames dropped : %"PRId64"\n", ifp->nb_dropped); | |
| 3598 | 8556 | break; | |
| 3599 | 842070 | } else if (ret < 0) { | |
| 3600 | ✗ | av_log(fg, AV_LOG_ERROR, "Error sending frames to consumers: %s\n", | |
| 3601 | ✗ | av_err2str(ret)); | |
| 3602 | ✗ | goto finish; | |
| 3603 | } | ||
| 3604 | } | ||
| 3605 | |||
| 3606 | 17379 | for (unsigned i = 0; i < fg->nb_outputs; i++) { | |
| 3607 | 8807 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]); | |
| 3608 | |||
| 3609 | 8807 | if (fgt.eof_out[i] || !fgt.graph) | |
| 3610 | 7064 | continue; | |
| 3611 | |||
| 3612 | 1743 | ret = fg_output_frame(ofp, &fgt, NULL); | |
| 3613 | 1743 | if (ret < 0) | |
| 3614 | ✗ | goto finish; | |
| 3615 | } | ||
| 3616 | |||
| 3617 | 8572 | finish: | |
| 3618 | |||
| 3619 | 8574 | if (print_graphs || print_graphs_file) | |
| 3620 | 1 | print_filtergraph(fg, fgt.graph); | |
| 3621 | |||
| 3622 | // EOF is normal termination | ||
| 3623 | 8574 | if (ret == AVERROR_EOF) | |
| 3624 | 6985 | ret = 0; | |
| 3625 | |||
| 3626 | 8574 | fg_thread_uninit(&fgt); | |
| 3627 | |||
| 3628 | 8574 | return ret; | |
| 3629 | } | ||
| 3630 | |||
| 3631 | ✗ | void fg_send_command(FilterGraph *fg, double time, const char *target, | |
| 3632 | const char *command, const char *arg, int all_filters) | ||
| 3633 | { | ||
| 3634 | ✗ | FilterGraphPriv *fgp = fgp_from_fg(fg); | |
| 3635 | AVBufferRef *buf; | ||
| 3636 | FilterCommand *fc; | ||
| 3637 | |||
| 3638 | ✗ | fc = av_mallocz(sizeof(*fc)); | |
| 3639 | ✗ | if (!fc) | |
| 3640 | ✗ | return; | |
| 3641 | |||
| 3642 | ✗ | buf = av_buffer_create((uint8_t*)fc, sizeof(*fc), filter_command_free, NULL, 0); | |
| 3643 | ✗ | if (!buf) { | |
| 3644 | ✗ | av_freep(&fc); | |
| 3645 | ✗ | return; | |
| 3646 | } | ||
| 3647 | |||
| 3648 | ✗ | fc->target = av_strdup(target); | |
| 3649 | ✗ | fc->command = av_strdup(command); | |
| 3650 | ✗ | fc->arg = av_strdup(arg); | |
| 3651 | ✗ | if (!fc->target || !fc->command || !fc->arg) { | |
| 3652 | ✗ | av_buffer_unref(&buf); | |
| 3653 | ✗ | return; | |
| 3654 | } | ||
| 3655 | |||
| 3656 | ✗ | fc->time = time; | |
| 3657 | ✗ | fc->all_filters = all_filters; | |
| 3658 | |||
| 3659 | ✗ | fgp->frame->buf[0] = buf; | |
| 3660 | ✗ | fgp->frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SEND_COMMAND; | |
| 3661 | |||
| 3662 | ✗ | sch_filter_command(fgp->sch, fgp->sch_idx, fgp->frame); | |
| 3663 | } | ||
| 3664 |