/src/mpv/video/decode/vd_lavc.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <float.h> |
19 | | #include <stdio.h> |
20 | | #include <stdlib.h> |
21 | | #include <assert.h> |
22 | | #include <stdbool.h> |
23 | | |
24 | | #include <libavcodec/avcodec.h> |
25 | | #include <libavformat/version.h> |
26 | | #include <libavutil/common.h> |
27 | | #include <libavutil/hwcontext.h> |
28 | | #include <libavutil/opt.h> |
29 | | #include <libavutil/intreadwrite.h> |
30 | | #include <libavutil/pixdesc.h> |
31 | | |
32 | | #include "mpv_talloc.h" |
33 | | #include "common/msg.h" |
34 | | #include "options/m_config.h" |
35 | | #include "options/options.h" |
36 | | #include "osdep/threads.h" |
37 | | #include "misc/bstr.h" |
38 | | #include "common/av_common.h" |
39 | | #include "common/codecs.h" |
40 | | |
41 | | #include "video/fmt-conversion.h" |
42 | | |
43 | | #include "filters/f_decoder_wrapper.h" |
44 | | #include "filters/filter_internal.h" |
45 | | #include "video/hwdec.h" |
46 | | #include "video/img_format.h" |
47 | | #include "video/mp_image.h" |
48 | | #include "video/mp_image_pool.h" |
49 | | #include "demux/demux.h" |
50 | | #include "demux/stheader.h" |
51 | | #include "demux/packet.h" |
52 | | #include "demux/packet_pool.h" |
53 | | #include "video/csputils.h" |
54 | | #include "video/sws_utils.h" |
55 | | #include "video/out/vo.h" |
56 | | |
57 | | #include "options/m_option.h" |
58 | | |
59 | | static void init_avctx(struct mp_filter *vd); |
60 | | static void uninit_avctx(struct mp_filter *vd); |
61 | | |
62 | | static int get_buffer2_direct(AVCodecContext *avctx, AVFrame *pic, int flags); |
63 | | static enum AVPixelFormat get_format_hwdec(struct AVCodecContext *avctx, |
64 | | const enum AVPixelFormat *pix_fmt); |
65 | | static int hwdec_opt_help(struct mp_log *log, const m_option_t *opt, |
66 | | struct bstr name); |
67 | | |
68 | 0 | #define HWDEC_DELAY_QUEUE_COUNT 4 |
69 | 0 | #define HWDEC_WAIT_KEYFRAME_COUNT 96 |
70 | | |
71 | | // Fallback number of extra surfaces for fixed-size hw frame pools, if the |
72 | | // actual requirement cannot be determined. |
73 | 0 | #define HWDEC_EXTRA_FRAMES 6 |
74 | | |
75 | | // Surfaces referenced outside of libavcodec on top of what the VO declares |
76 | | #define HWDEC_EXTRA_INFLIGHT_FRAMES 3 |
77 | | |
78 | | #define OPT_BASE_STRUCT struct vd_lavc_params |
79 | | |
80 | | struct vd_lavc_params { |
81 | | bool fast; |
82 | | int film_grain; |
83 | | bool show_all; |
84 | | int skip_loop_filter; |
85 | | int skip_idct; |
86 | | int skip_frame; |
87 | | int framedrop; |
88 | | int threads; |
89 | | bool bitexact; |
90 | | bool old_x264; |
91 | | bool apply_cropping; |
92 | | bool check_hw_profile; |
93 | | char **avopts; |
94 | | int dr; |
95 | | }; |
96 | | |
97 | | static const struct m_opt_choice_alternatives discard_names[] = { |
98 | | {"none", AVDISCARD_NONE}, |
99 | | {"default", AVDISCARD_DEFAULT}, |
100 | | {"nonref", AVDISCARD_NONREF}, |
101 | | {"bidir", AVDISCARD_BIDIR}, |
102 | | {"nonkey", AVDISCARD_NONKEY}, |
103 | | {"all", AVDISCARD_ALL}, |
104 | | {0} |
105 | | }; |
106 | | #define OPT_DISCARD(field) OPT_CHOICE_C(field, discard_names) |
107 | | |
108 | | const struct m_sub_options vd_lavc_conf = { |
109 | | .opts = (const m_option_t[]){ |
110 | | {"vd-lavc-fast", OPT_BOOL(fast)}, |
111 | | {"vd-lavc-film-grain", OPT_CHOICE(film_grain, |
112 | | {"auto", -1}, {"cpu", 0}, {"gpu", 1})}, |
113 | | {"vd-lavc-show-all", OPT_BOOL(show_all)}, |
114 | | {"vd-lavc-skiploopfilter", OPT_DISCARD(skip_loop_filter)}, |
115 | | {"vd-lavc-skipidct", OPT_DISCARD(skip_idct)}, |
116 | | {"vd-lavc-skipframe", OPT_DISCARD(skip_frame)}, |
117 | | {"vd-lavc-framedrop", OPT_DISCARD(framedrop)}, |
118 | | {"vd-lavc-threads", OPT_INT(threads), M_RANGE(0, DBL_MAX)}, |
119 | | {"vd-lavc-bitexact", OPT_BOOL(bitexact)}, |
120 | | {"vd-lavc-assume-old-x264", OPT_BOOL(old_x264)}, |
121 | | {"vd-lavc-check-hw-profile", OPT_BOOL(check_hw_profile)}, |
122 | | {"vd-lavc-o", OPT_KEYVALUELIST(avopts)}, |
123 | | {"vd-lavc-dr", OPT_CHOICE(dr, |
124 | | {"auto", -1}, {"no", 0}, {"yes", 1})}, |
125 | | {"vd-apply-cropping", OPT_BOOL(apply_cropping)}, |
126 | | {0} |
127 | | }, |
128 | | .change_flags = UPDATE_VD, |
129 | | .size = sizeof(struct vd_lavc_params), |
130 | | .defaults = &(const struct vd_lavc_params){ |
131 | | .film_grain = -1 /*auto*/, |
132 | | .check_hw_profile = true, |
133 | | .skip_loop_filter = AVDISCARD_DEFAULT, |
134 | | .skip_idct = AVDISCARD_DEFAULT, |
135 | | .skip_frame = AVDISCARD_DEFAULT, |
136 | | .framedrop = AVDISCARD_NONREF, |
137 | | .dr = -1, |
138 | | .apply_cropping = true, |
139 | | }, |
140 | | }; |
141 | | |
142 | | #undef OPT_BASE_STRUCT |
143 | | #define OPT_BASE_STRUCT struct hwdec_opts |
144 | | |
145 | | struct hwdec_opts { |
146 | | int software_fallback; |
147 | | char **hwdec_api; |
148 | | char *hwdec_codecs; |
149 | | int hwdec_image_format; |
150 | | int hwdec_extra_frames; |
151 | | int hwdec_threads; |
152 | | }; |
153 | | |
154 | | const struct m_sub_options hwdec_conf = { |
155 | | .opts = (const m_option_t[]){ |
156 | | {"hwdec", OPT_STRINGLIST(hwdec_api), |
157 | | .help = hwdec_opt_help, |
158 | | .flags = M_OPT_OPTIONAL_PARAM | M_OPT_ALLOW_NO | UPDATE_HWDEC}, |
159 | | {"hwdec-codecs", OPT_STRING(hwdec_codecs), |
160 | | .flags = UPDATE_HWDEC}, |
161 | | {"hwdec-extra-frames", OPT_CHOICE(hwdec_extra_frames, {"auto", -1}), |
162 | | M_RANGE(0, 256), .flags = UPDATE_VD}, |
163 | | {"hwdec-image-format", OPT_IMAGEFORMAT(hwdec_image_format), |
164 | | .flags = UPDATE_VO}, |
165 | | {"hwdec-software-fallback", OPT_CHOICE(software_fallback, |
166 | | {"no", INT_MAX}, {"yes", 1}), M_RANGE(1, INT_MAX), |
167 | | .flags = UPDATE_HWDEC}, |
168 | | {"hwdec-threads", OPT_INT(hwdec_threads), M_RANGE(0, DBL_MAX)}, |
169 | | {"vd-lavc-software-fallback", OPT_REPLACED("hwdec-software-fallback")}, |
170 | | {0} |
171 | | }, |
172 | | .size = sizeof(struct hwdec_opts), |
173 | | .defaults = &(const struct hwdec_opts){ |
174 | | .software_fallback = 3, |
175 | | .hwdec_api = (char *[]){"no", NULL,}, |
176 | | .hwdec_codecs = "h264,vc1,hevc,vp8,vp9,av1,prores,prores_raw,ffv1,dpx,apv", |
177 | | .hwdec_extra_frames = -1, |
178 | | .hwdec_threads = 4, |
179 | | }, |
180 | | }; |
181 | | |
182 | | struct hwdec_info { |
183 | | char name[64]; |
184 | | char method_name[24]; // non-unique name describing the hwdec method |
185 | | const AVCodec *codec; // implemented by this codec |
186 | | enum AVHWDeviceType lavc_device; // if not NONE, get a hwdevice |
187 | | bool copying; // if true, outputs sw frames, or copy to sw ourselves |
188 | | enum AVPixelFormat pix_fmt; // if not NONE, select in get_format |
189 | | bool use_hw_frames; // set AVCodecContext.hw_frames_ctx |
190 | | bool use_hw_device; // set AVCodecContext.hw_device_ctx |
191 | | unsigned int flags; // HWDEC_FLAG_* |
192 | | |
193 | | // for internal sorting |
194 | | int auto_pos; |
195 | | int rank; |
196 | | }; |
197 | | |
198 | | typedef struct lavc_ctx { |
199 | | struct mp_log *log; |
200 | | struct m_config_cache *opts_cache; |
201 | | struct vd_lavc_params *opts; |
202 | | struct m_config_cache *hwdec_opts_cache; |
203 | | struct hwdec_opts *hwdec_opts; |
204 | | struct mp_codec_params *codec; |
205 | | AVCodecContext *avctx; |
206 | | AVFrame *pic; |
207 | | AVPacket *avpkt; |
208 | | bool use_hwdec; |
209 | | struct hwdec_info hwdec; // valid only if use_hwdec==true |
210 | | bstr *attempted_hwdecs; |
211 | | int num_attempted_hwdecs; |
212 | | AVRational codec_timebase; |
213 | | enum AVDiscard skip_frame; |
214 | | bool flushing; |
215 | | struct lavc_state state; |
216 | | const char *decoder; |
217 | | bool hwdec_failed; |
218 | | bool hwdec_notified; |
219 | | bool force_eof; |
220 | | int wait_for_keyframe; // max number of frames to wait for keyframe after reset |
221 | | |
222 | | bool intra_only; |
223 | | int framedrop_flags; |
224 | | int extra_hw_frames_hint; // VDCTRL_SET_EXTRA_HW_FRAMES |
225 | | |
226 | | bool hw_probing; |
227 | | struct demux_packet **sent_packets; |
228 | | int num_sent_packets; |
229 | | |
230 | | struct demux_packet **requeue_packets; |
231 | | int num_requeue_packets; |
232 | | |
233 | | struct mp_image **delay_queue; |
234 | | int num_delay_queue; |
235 | | int max_delay_queue; |
236 | | |
237 | | // From VO |
238 | | struct vo *vo; |
239 | | struct mp_hwdec_devices *hwdec_devs; |
240 | | bool force_swdec; |
241 | | |
242 | | // Wrapped AVHWDeviceContext* used for decoding. |
243 | | AVBufferRef *hwdec_dev; |
244 | | |
245 | | bool hwdec_request_reinit; |
246 | | int hwdec_fail_count; |
247 | | |
248 | | struct mp_image_pool *hwdec_swpool; |
249 | | |
250 | | AVBufferRef *cached_hw_frames_ctx; |
251 | | |
252 | | // --- The following fields are protected by dr_lock. |
253 | | mp_mutex dr_lock; |
254 | | bool dr_failed; |
255 | | struct mp_image_pool *dr_pool; |
256 | | int dr_imgfmt, dr_w, dr_h, dr_stride_align; |
257 | | |
258 | | struct mp_decoder public; |
259 | | } vd_ffmpeg_ctx; |
260 | | |
261 | | enum { |
262 | | HWDEC_FLAG_AUTO = (1 << 0), // prioritize in autoprobe order |
263 | | HWDEC_FLAG_WHITELIST = (1 << 1), // whitelist for auto |
264 | | }; |
265 | | |
266 | | struct autoprobe_info { |
267 | | const char *method_name; |
268 | | unsigned int flags; // HWDEC_FLAG_* |
269 | | }; |
270 | | |
271 | | // Things not included in this list will be tried last, in random order. |
272 | | const struct autoprobe_info hwdec_autoprobe_info[] = { |
273 | | {"d3d11va", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
274 | | {"vulkan", HWDEC_FLAG_AUTO}, |
275 | | {"dxva2", HWDEC_FLAG_AUTO}, |
276 | | {"nvdec", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
277 | | {"vaapi", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
278 | | {"vdpau", HWDEC_FLAG_AUTO}, |
279 | | {"drm", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
280 | | {"mediacodec", HWDEC_FLAG_AUTO}, |
281 | | {"videotoolbox", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
282 | | {"d3d11va-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
283 | | {"vulkan-copy", HWDEC_FLAG_AUTO}, |
284 | | {"dxva2-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
285 | | {"nvdec-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
286 | | {"vaapi-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
287 | | {"vdpau-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
288 | | {"drm-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
289 | | {"mediacodec-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
290 | | {"videotoolbox-copy", HWDEC_FLAG_AUTO | HWDEC_FLAG_WHITELIST}, |
291 | | {0} |
292 | | }; |
293 | | |
294 | | static int hwdec_compare(const void *p1, const void *p2) |
295 | 1.07M | { |
296 | 1.07M | struct hwdec_info *h1 = (void *)p1; |
297 | 1.07M | struct hwdec_info *h2 = (void *)p2; |
298 | | |
299 | 1.07M | if (h1 == h2) |
300 | 0 | return 0; |
301 | | |
302 | | // Strictly put non-preferred hwdecs to the end of the list. |
303 | 1.07M | if ((h1->auto_pos == INT_MAX) != (h2->auto_pos == INT_MAX)) |
304 | 0 | return h1->auto_pos == INT_MAX ? 1 : -1; |
305 | | // List non-copying entries first, so --hwdec=auto takes them. |
306 | 1.07M | if (h1->copying != h2->copying) |
307 | 0 | return h1->copying ? 1 : -1; |
308 | | // Order by autoprobe preference order. |
309 | 1.07M | if (h1->auto_pos != h2->auto_pos) |
310 | 0 | return h1->auto_pos > h2->auto_pos ? 1 : -1; |
311 | | // Put hwdecs without hw_device_ctx last |
312 | 1.07M | if ((!!h1->lavc_device) != (!!h2->lavc_device)) |
313 | 0 | return h1->lavc_device ? -1 : 1; |
314 | | // Fallback sort order to make sorting stable. |
315 | 1.07M | return h1->rank > h2->rank ? 1 :-1; |
316 | 1.07M | } |
317 | | |
318 | | // (This takes care of some bookkeeping too, like setting info.name) |
319 | | static void add_hwdec_item(struct hwdec_info **infos, int *num_infos, |
320 | | struct hwdec_info info) |
321 | 746k | { |
322 | 746k | if (info.copying) |
323 | 746k | mp_snprintf_cat(info.method_name, sizeof(info.method_name), "-copy"); |
324 | | |
325 | | // (Including the codec name in case this is a wrapper looks pretty dumb, |
326 | | // but better not have them clash with hwaccels and others.) |
327 | 746k | snprintf(info.name, sizeof(info.name), "%s-%s", |
328 | 746k | info.codec->name, info.method_name); |
329 | | |
330 | 746k | info.rank = *num_infos; |
331 | 746k | info.auto_pos = INT_MAX; |
332 | | |
333 | 14.1M | for (int x = 0; hwdec_autoprobe_info[x].method_name; x++) { |
334 | 13.4M | const struct autoprobe_info *entry = &hwdec_autoprobe_info[x]; |
335 | 13.4M | if (strcmp(entry->method_name, info.method_name) == 0) { |
336 | 0 | info.flags |= entry->flags; |
337 | 0 | if (info.flags & HWDEC_FLAG_AUTO) |
338 | 0 | info.auto_pos = x; |
339 | 0 | } |
340 | 13.4M | } |
341 | | |
342 | 746k | MP_TARRAY_APPEND(NULL, *infos, *num_infos, info); |
343 | 746k | } |
344 | | |
345 | | static void add_all_hwdec_methods(struct hwdec_info **infos, int *num_infos) |
346 | 82.9k | { |
347 | 82.9k | const AVCodec *codec = NULL; |
348 | 82.9k | void *iter = NULL; |
349 | 41.6M | while (1) { |
350 | 41.6M | codec = av_codec_iterate(&iter); |
351 | 41.6M | if (!codec) |
352 | 82.9k | break; |
353 | 41.5M | if (codec->type != AVMEDIA_TYPE_VIDEO || !av_codec_is_decoder(codec)) |
354 | 20.3M | continue; |
355 | | |
356 | 21.2M | struct hwdec_info info_template = { |
357 | 21.2M | .pix_fmt = AV_PIX_FMT_NONE, |
358 | 21.2M | .codec = codec, |
359 | 21.2M | }; |
360 | | |
361 | 21.2M | const char *wrapper = NULL; |
362 | 21.2M | if (codec->capabilities & (AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_HYBRID)) |
363 | 746k | wrapper = codec->wrapper_name; |
364 | | |
365 | | // A decoder can provide multiple methods. In particular, hwaccels |
366 | | // provide various methods (e.g. native h264 with vaapi & d3d11), but |
367 | | // even wrapper decoders could provide multiple methods. |
368 | 21.2M | bool found_any = false; |
369 | 21.2M | for (int n = 0; ; n++) { |
370 | 21.2M | const AVCodecHWConfig *cfg = avcodec_get_hw_config(codec, n); |
371 | 21.2M | if (!cfg) |
372 | 21.2M | break; |
373 | | |
374 | 0 | if ((cfg->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX) || |
375 | 0 | (cfg->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) |
376 | 0 | { |
377 | 0 | struct hwdec_info info = info_template; |
378 | 0 | info.lavc_device = cfg->device_type; |
379 | 0 | info.pix_fmt = cfg->pix_fmt; |
380 | |
|
381 | 0 | const char *name = av_hwdevice_get_type_name(cfg->device_type); |
382 | 0 | mp_assert(name); // API violation by libavcodec |
383 | | |
384 | | // nvdec hwaccels and the cuvid full decoder clash with their |
385 | | // naming, so fix it here; we also prefer nvdec for the hwaccel. |
386 | 0 | if (strcmp(name, "cuda") == 0 && !wrapper) |
387 | 0 | name = "nvdec"; |
388 | |
|
389 | 0 | snprintf(info.method_name, sizeof(info.method_name), "%s", name); |
390 | | |
391 | | // Usually we want to prefer using hw_frames_ctx for true |
392 | | // hwaccels only, but we actually don't have any way to detect |
393 | | // those, so always use hw_frames_ctx if offered. |
394 | 0 | if (cfg->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX) { |
395 | 0 | info.use_hw_frames = true; |
396 | 0 | } else { |
397 | 0 | info.use_hw_device = true; |
398 | 0 | } |
399 | | |
400 | | // Direct variant. |
401 | 0 | add_hwdec_item(infos, num_infos, info); |
402 | | |
403 | | // Copy variant. |
404 | 0 | info.copying = true; |
405 | 0 | if (cfg->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) { |
406 | 0 | info.use_hw_frames = false; |
407 | 0 | info.use_hw_device = true; |
408 | 0 | } |
409 | 0 | add_hwdec_item(infos, num_infos, info); |
410 | |
|
411 | 0 | found_any = true; |
412 | 0 | } else if (cfg->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) { |
413 | 0 | struct hwdec_info info = info_template; |
414 | 0 | info.pix_fmt = cfg->pix_fmt; |
415 | |
|
416 | 0 | const char *name = wrapper; |
417 | 0 | if (!name) |
418 | 0 | name = av_get_pix_fmt_name(info.pix_fmt); |
419 | 0 | mp_assert(name); // API violation by libavcodec |
420 | | |
421 | 0 | snprintf(info.method_name, sizeof(info.method_name), "%s", name); |
422 | | |
423 | | // Direct variant. |
424 | 0 | add_hwdec_item(infos, num_infos, info); |
425 | | |
426 | | // Copy variant. |
427 | 0 | info.copying = true; |
428 | 0 | info.pix_fmt = AV_PIX_FMT_NONE; // trust it can do sw output |
429 | 0 | add_hwdec_item(infos, num_infos, info); |
430 | |
|
431 | 0 | found_any = true; |
432 | 0 | } |
433 | 0 | } |
434 | | |
435 | 21.2M | if (!found_any && wrapper) { |
436 | | // We _know_ there's something supported here, usually outputting |
437 | | // sw surfaces. E.g. mediacodec (before hw_device_ctx support). |
438 | | |
439 | 746k | struct hwdec_info info = info_template; |
440 | 746k | info.copying = true; // probably |
441 | | |
442 | 746k | snprintf(info.method_name, sizeof(info.method_name), "%s", wrapper); |
443 | 746k | add_hwdec_item(infos, num_infos, info); |
444 | 746k | } |
445 | 21.2M | } |
446 | | |
447 | 82.9k | qsort(*infos, *num_infos, sizeof(struct hwdec_info), hwdec_compare); |
448 | 82.9k | } |
449 | | |
450 | | static bool hwdec_codec_allowed(struct mp_filter *vd, const char *codec) |
451 | 52 | { |
452 | 52 | vd_ffmpeg_ctx *ctx = vd->priv; |
453 | 52 | bstr s = bstr0(ctx->hwdec_opts->hwdec_codecs); |
454 | 624 | while (s.len) { |
455 | 572 | bstr item; |
456 | 572 | bstr_split_tok(s, ",", &item, &s); |
457 | 572 | if (bstr_equals0(item, "all") || bstr_equals0(item, codec)) |
458 | 0 | return true; |
459 | 572 | } |
460 | 52 | return false; |
461 | 52 | } |
462 | | |
463 | | static AVBufferRef *hwdec_create_dev(struct mp_filter *vd, |
464 | | struct hwdec_info *hwdec, |
465 | | bool autoprobe) |
466 | 0 | { |
467 | 0 | vd_ffmpeg_ctx *ctx = vd->priv; |
468 | 0 | mp_assert(hwdec->lavc_device); |
469 | | |
470 | 0 | if (hwdec->copying) { |
471 | 0 | const struct hwcontext_fns *fns = |
472 | 0 | hwdec_get_hwcontext_fns(hwdec->lavc_device); |
473 | 0 | if (fns && fns->create_dev) { |
474 | 0 | struct hwcontext_create_dev_params params = { |
475 | 0 | .probing = autoprobe, |
476 | 0 | }; |
477 | 0 | return fns->create_dev(vd->global, vd->log, ¶ms); |
478 | 0 | } else { |
479 | 0 | AVBufferRef* ref = NULL; |
480 | 0 | av_hwdevice_ctx_create(&ref, hwdec->lavc_device, NULL, NULL, 0); |
481 | 0 | return ref; |
482 | 0 | } |
483 | 0 | } else if (ctx->hwdec_devs) { |
484 | 0 | int imgfmt = pixfmt2imgfmt(hwdec->pix_fmt); |
485 | 0 | struct hwdec_imgfmt_request params = { |
486 | 0 | .imgfmt = imgfmt, |
487 | 0 | .probing = autoprobe, |
488 | 0 | }; |
489 | 0 | hwdec_devices_request_for_img_fmt(ctx->hwdec_devs, ¶ms); |
490 | |
|
491 | 0 | const struct mp_hwdec_ctx *hw_ctx = |
492 | 0 | hwdec_devices_get_by_imgfmt_and_type(ctx->hwdec_devs, imgfmt, |
493 | 0 | hwdec->lavc_device); |
494 | |
|
495 | 0 | if (hw_ctx && hw_ctx->av_device_ref) |
496 | 0 | return av_buffer_ref(hw_ctx->av_device_ref); |
497 | 0 | } |
498 | | |
499 | 0 | return NULL; |
500 | 0 | } |
501 | | |
502 | | // Select if and which hwdec to use. Also makes sure to get the decode device. |
503 | | static void select_and_set_hwdec(struct mp_filter *vd) |
504 | 79.9k | { |
505 | 79.9k | vd_ffmpeg_ctx *ctx = vd->priv; |
506 | 79.9k | const char *codec = ctx->codec->codec; |
507 | | |
508 | 79.9k | m_config_cache_update(ctx->hwdec_opts_cache); |
509 | | |
510 | 79.9k | struct hwdec_info *hwdecs = NULL; |
511 | 79.9k | int num_hwdecs = 0; |
512 | 79.9k | add_all_hwdec_methods(&hwdecs, &num_hwdecs); |
513 | | |
514 | 79.9k | char **hwdec_api = ctx->hwdec_opts->hwdec_api; |
515 | 79.9k | for (int i = 0; hwdec_api && hwdec_api[i]; i++) { |
516 | 79.9k | bstr opt = bstr0(hwdec_api[i]); |
517 | | |
518 | 79.9k | bool hwdec_requested = !bstr_equals0(opt, "no"); |
519 | 79.9k | bool hwdec_auto_safe = bstr_equals0(opt, "auto") || |
520 | 79.9k | bstr_equals0(opt, "auto-safe") || |
521 | 79.9k | bstr_equals0(opt, "auto-copy") || |
522 | 79.9k | bstr_equals0(opt, "auto-copy-safe") || |
523 | 79.9k | bstr_equals0(opt, "yes") || |
524 | 79.9k | bstr_equals0(opt, ""); |
525 | 79.9k | bool hwdec_auto_unsafe = bstr_equals0(opt, "auto-unsafe"); |
526 | 79.9k | bool hwdec_auto_copy = bstr_equals0(opt, "auto-copy") || |
527 | 79.9k | bstr_equals0(opt, "auto-copy-safe") || |
528 | 79.9k | bstr_equals0(opt, "auto-copy-unsafe"); |
529 | 79.9k | bool hwdec_auto = hwdec_auto_unsafe || hwdec_auto_copy || hwdec_auto_safe; |
530 | | |
531 | 79.9k | if (!hwdec_requested) { |
532 | 79.8k | MP_VERBOSE(vd, "No hardware decoding requested.\n"); |
533 | 79.8k | break; |
534 | 79.8k | } else if (!hwdec_codec_allowed(vd, codec)) { |
535 | 52 | MP_VERBOSE(vd, "Not trying to use hardware decoding: codec %s is not " |
536 | 52 | "on whitelist.\n", codec); |
537 | 52 | break; |
538 | 52 | } else if (ctx->force_swdec) { |
539 | 0 | MP_VERBOSE(vd, "Not trying to use hardware decoding: disallowed\n"); |
540 | 0 | break; |
541 | 0 | } else { |
542 | 0 | bool hwdec_name_supported = false; // relevant only if !hwdec_auto |
543 | 0 | for (int n = 0; n < num_hwdecs; n++) { |
544 | 0 | struct hwdec_info *hwdec = &hwdecs[n]; |
545 | |
|
546 | 0 | if (!hwdec_auto && !(bstr_equals0(opt, hwdec->method_name) || |
547 | 0 | bstr_equals0(opt, hwdec->name))) |
548 | 0 | continue; |
549 | 0 | hwdec_name_supported = true; |
550 | |
|
551 | 0 | bool already_attempted = false; |
552 | 0 | for (int j = 0; j < ctx->num_attempted_hwdecs; j++) { |
553 | 0 | if (bstr_equals0(ctx->attempted_hwdecs[j], hwdec->name)) { |
554 | 0 | MP_DBG(vd, "Skipping previously attempted hwdec: %s\n", |
555 | 0 | hwdec->name); |
556 | 0 | already_attempted = true; |
557 | 0 | break; |
558 | 0 | } |
559 | 0 | } |
560 | 0 | if (already_attempted) |
561 | 0 | continue; |
562 | | |
563 | 0 | const char *hw_codec = mp_codec_from_av_codec_id(hwdec->codec->id); |
564 | 0 | if (!hw_codec || strcmp(hw_codec, codec) != 0) |
565 | 0 | continue; |
566 | | |
567 | 0 | if (hwdec_auto_safe && !(hwdec->flags & HWDEC_FLAG_WHITELIST)) |
568 | 0 | continue; |
569 | | |
570 | 0 | MP_VERBOSE(vd, "Looking at hwdec %s...\n", hwdec->name); |
571 | | |
572 | | /* |
573 | | * Past this point, any kind of failure that results in us |
574 | | * looking for a new hwdec should not lead to use trying this |
575 | | * hwdec again - so add it to the list, regardless of whether |
576 | | * initialisation will succeed or not. |
577 | | */ |
578 | 0 | MP_TARRAY_APPEND(ctx, ctx->attempted_hwdecs, |
579 | 0 | ctx->num_attempted_hwdecs, |
580 | 0 | bstrdup(ctx, bstr0(hwdec->name))); |
581 | |
|
582 | 0 | if (hwdec_auto_copy && !hwdec->copying) { |
583 | 0 | MP_VERBOSE(vd, "Not using this for auto-copy.\n"); |
584 | 0 | continue; |
585 | 0 | } |
586 | | |
587 | 0 | if (hwdec->lavc_device) { |
588 | 0 | ctx->hwdec_dev = hwdec_create_dev(vd, hwdec, hwdec_auto); |
589 | 0 | if (!ctx->hwdec_dev) { |
590 | 0 | MP_VERBOSE(vd, "Could not create device.\n"); |
591 | 0 | continue; |
592 | 0 | } |
593 | | |
594 | 0 | const struct hwcontext_fns *fns = |
595 | 0 | hwdec_get_hwcontext_fns(hwdec->lavc_device); |
596 | 0 | if (fns && fns->is_codec_allowed && |
597 | 0 | !fns->is_codec_allowed(ctx->hwdec_dev, hwdec->codec->id)) |
598 | 0 | { |
599 | 0 | MP_WARN(vd, "Hardware decoding of '%s' is disabled on this " |
600 | 0 | "device.\n", codec); |
601 | 0 | av_buffer_unref(&ctx->hwdec_dev); |
602 | 0 | continue; |
603 | 0 | } |
604 | 0 | if (fns && fns->is_emulated && fns->is_emulated(ctx->hwdec_dev)) { |
605 | 0 | if (hwdec_auto) { |
606 | 0 | MP_VERBOSE(vd, "Not using emulated API.\n"); |
607 | 0 | av_buffer_unref(&ctx->hwdec_dev); |
608 | 0 | continue; |
609 | 0 | } |
610 | 0 | MP_WARN(vd, "Using emulated hardware decoding API.\n"); |
611 | 0 | } |
612 | 0 | } else if (!hwdec->copying) { |
613 | | // Most likely METHOD_INTERNAL, which often use delay-loaded |
614 | | // VO support as well. |
615 | 0 | if (ctx->hwdec_devs) { |
616 | 0 | struct hwdec_imgfmt_request params = { |
617 | 0 | .imgfmt = pixfmt2imgfmt(hwdec->pix_fmt), |
618 | 0 | .probing = hwdec_auto, |
619 | 0 | }; |
620 | 0 | hwdec_devices_request_for_img_fmt( |
621 | 0 | ctx->hwdec_devs, ¶ms); |
622 | 0 | } |
623 | 0 | } |
624 | | |
625 | 0 | ctx->use_hwdec = true; |
626 | 0 | ctx->hwdec = *hwdec; |
627 | 0 | break; |
628 | 0 | } |
629 | 0 | if (ctx->use_hwdec) |
630 | 0 | break; |
631 | 0 | else if (!hwdec_auto && !hwdec_name_supported) |
632 | 0 | MP_WARN(vd, "Unsupported hwdec: %.*s\n", BSTR_P(opt)); |
633 | 0 | } |
634 | 79.9k | } |
635 | 79.9k | talloc_free(hwdecs); |
636 | | |
637 | | |
638 | 79.9k | if (ctx->use_hwdec) { |
639 | 0 | MP_VERBOSE(vd, "Trying hardware decoding via %s.\n", ctx->hwdec.name); |
640 | 0 | if (strcmp(ctx->decoder, ctx->hwdec.codec->name) != 0) |
641 | 0 | MP_VERBOSE(vd, "Using underlying hw-decoder '%s'\n", |
642 | 0 | ctx->hwdec.codec->name); |
643 | 79.9k | } else { |
644 | | // If software fallback is disabled and we get here, all hwdec must |
645 | | // have failed. Tell the ctx to always force an eof. |
646 | 79.9k | if (ctx->hwdec_opts->software_fallback == INT_MAX) { |
647 | 2 | MP_WARN(ctx, "Software decoding fallback is disabled.\n"); |
648 | 2 | ctx->force_eof = true; |
649 | 79.9k | } else { |
650 | 79.9k | MP_VERBOSE(vd, "Using software decoding.\n"); |
651 | 79.9k | } |
652 | 79.9k | } |
653 | 79.9k | } |
654 | | |
655 | | static int hwdec_opt_help(struct mp_log *log, const m_option_t *opt, |
656 | | struct bstr name) |
657 | 3.02k | { |
658 | 3.02k | struct hwdec_info *hwdecs = NULL; |
659 | 3.02k | int num_hwdecs = 0; |
660 | 3.02k | add_all_hwdec_methods(&hwdecs, &num_hwdecs); |
661 | | |
662 | 3.02k | mp_info(log, "Valid values (with alternative full names):\n"); |
663 | | |
664 | 30.2k | for (int n = 0; n < num_hwdecs; n++) { |
665 | 27.2k | struct hwdec_info *hwdec = &hwdecs[n]; |
666 | | |
667 | 27.2k | mp_info(log, " %s (%s)\n", hwdec->method_name, hwdec->name); |
668 | 27.2k | } |
669 | | |
670 | 3.02k | talloc_free(hwdecs); |
671 | | |
672 | 3.02k | mp_info(log, " auto (yes '')\n"); |
673 | 3.02k | mp_info(log, " no\n"); |
674 | 3.02k | mp_info(log, " auto-safe\n"); |
675 | 3.02k | mp_info(log, " auto-copy\n"); |
676 | 3.02k | mp_info(log, " auto-unsafe\n"); |
677 | 3.02k | mp_info(log, " auto-copy-safe\n"); |
678 | 3.02k | mp_info(log, " auto-copy-unsafe\n"); |
679 | | |
680 | 3.02k | return M_OPT_EXIT; |
681 | 3.02k | } |
682 | | |
683 | | static void force_fallback(struct mp_filter *vd) |
684 | 0 | { |
685 | 0 | vd_ffmpeg_ctx *ctx = vd->priv; |
686 | |
|
687 | 0 | uninit_avctx(vd); |
688 | 0 | int lev = ctx->hwdec_notified ? MSGL_WARN : MSGL_V; |
689 | 0 | mp_msg(vd->log, lev, "Attempting next decoding method after failure of %.*s.\n", |
690 | 0 | BSTR_P(ctx->attempted_hwdecs[ctx->num_attempted_hwdecs - 1])); |
691 | 0 | select_and_set_hwdec(vd); |
692 | 0 | init_avctx(vd); |
693 | 0 | } |
694 | | |
695 | | static void reinit(struct mp_filter *vd) |
696 | 79.9k | { |
697 | 79.9k | vd_ffmpeg_ctx *ctx = vd->priv; |
698 | | |
699 | 79.9k | uninit_avctx(vd); |
700 | | |
701 | | /* |
702 | | * Reset attempted hwdecs so that if the hwdec list is reconfigured |
703 | | * we attempt all of them from the beginning. The most practical |
704 | | * reason for this is that ctrl+h toggles between `no` and |
705 | | * `auto`, and we want to reevaluate from a clean slate each time. |
706 | | */ |
707 | 79.9k | TA_FREEP(&ctx->attempted_hwdecs); |
708 | 79.9k | ctx->num_attempted_hwdecs = 0; |
709 | 79.9k | ctx->hwdec_notified = false; |
710 | | |
711 | 79.9k | select_and_set_hwdec(vd); |
712 | | |
713 | 79.9k | bool use_hwdec = ctx->use_hwdec; |
714 | 79.9k | init_avctx(vd); |
715 | 79.9k | if (!ctx->avctx && use_hwdec) { |
716 | 0 | do { |
717 | 0 | force_fallback(vd); |
718 | 0 | } while (!ctx->avctx); |
719 | 0 | } |
720 | | |
721 | | // Wait for the first keyframe after reinit to ensure the decoder state is |
722 | | // valid and to avoid decoding errors that could cause hwdec to fail and |
723 | | // fall back immediately after a reinit. |
724 | 79.9k | ctx->wait_for_keyframe = ctx->use_hwdec ? HWDEC_WAIT_KEYFRAME_COUNT : 0; |
725 | 79.9k | } |
726 | | |
727 | | static void init_avctx(struct mp_filter *vd) |
728 | 79.9k | { |
729 | 79.9k | vd_ffmpeg_ctx *ctx = vd->priv; |
730 | 79.9k | struct vd_lavc_params *lavc_param = ctx->opts; |
731 | 79.9k | struct mp_codec_params *c = ctx->codec; |
732 | | |
733 | 79.9k | m_config_cache_update(ctx->opts_cache); |
734 | | |
735 | 79.9k | mp_assert(!ctx->avctx); |
736 | | |
737 | 79.9k | const AVCodec *lavc_codec = NULL; |
738 | | |
739 | 79.9k | if (ctx->use_hwdec) { |
740 | 0 | lavc_codec = ctx->hwdec.codec; |
741 | 79.9k | } else { |
742 | 79.9k | lavc_codec = avcodec_find_decoder_by_name(ctx->decoder); |
743 | 79.9k | } |
744 | 79.9k | if (!lavc_codec) |
745 | 0 | return; |
746 | | |
747 | 79.9k | const AVCodecDescriptor *desc = avcodec_descriptor_get(lavc_codec->id); |
748 | 79.9k | ctx->intra_only = desc && (desc->props & AV_CODEC_PROP_INTRA_ONLY); |
749 | | |
750 | 79.9k | ctx->codec_timebase = mp_get_codec_timebase(ctx->codec); |
751 | | |
752 | 79.9k | ctx->hwdec_failed = false; |
753 | 79.9k | ctx->hwdec_request_reinit = false; |
754 | 79.9k | ctx->avctx = avcodec_alloc_context3(lavc_codec); |
755 | 79.9k | AVCodecContext *avctx = ctx->avctx; |
756 | 79.9k | if (!ctx->avctx) |
757 | 0 | goto error; |
758 | 79.9k | avctx->codec_type = AVMEDIA_TYPE_VIDEO; |
759 | 79.9k | avctx->codec_id = lavc_codec->id; |
760 | 79.9k | avctx->pkt_timebase = ctx->codec_timebase; |
761 | | |
762 | 79.9k | ctx->pic = av_frame_alloc(); |
763 | 79.9k | if (!ctx->pic) |
764 | 0 | goto error; |
765 | | |
766 | 79.9k | ctx->avpkt = av_packet_alloc(); |
767 | 79.9k | if (!ctx->avpkt) |
768 | 0 | goto error; |
769 | | |
770 | 79.9k | int threads = lavc_param->threads; |
771 | 79.9k | if (ctx->use_hwdec) { |
772 | 0 | avctx->opaque = vd; |
773 | 0 | avctx->hwaccel_flags |= AV_HWACCEL_FLAG_IGNORE_LEVEL; |
774 | 0 | if (!lavc_param->check_hw_profile) |
775 | 0 | avctx->hwaccel_flags |= AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH; |
776 | |
|
777 | 0 | #ifdef AV_HWACCEL_FLAG_UNSAFE_OUTPUT |
778 | | /* |
779 | | * This flag primarily exists for nvdec which has a very limited |
780 | | * output frame pool, which can get exhausted if consumers don't |
781 | | * release frames quickly. However, as an implementation |
782 | | * requirement, we have to copy the frames anyway, so we don't |
783 | | * need this extra implicit copy. |
784 | | */ |
785 | 0 | avctx->hwaccel_flags |= AV_HWACCEL_FLAG_UNSAFE_OUTPUT; |
786 | 0 | #endif |
787 | |
|
788 | 0 | if (ctx->hwdec.use_hw_device) { |
789 | 0 | if (ctx->hwdec_dev) |
790 | 0 | avctx->hw_device_ctx = av_buffer_ref(ctx->hwdec_dev); |
791 | 0 | if (!avctx->hw_device_ctx) |
792 | 0 | goto error; |
793 | 0 | } |
794 | 0 | if (ctx->hwdec.use_hw_frames) { |
795 | 0 | if (!ctx->hwdec_dev) |
796 | 0 | goto error; |
797 | 0 | } |
798 | | |
799 | 0 | if (ctx->hwdec.pix_fmt != AV_PIX_FMT_NONE) |
800 | 0 | avctx->get_format = get_format_hwdec; |
801 | | |
802 | | // Allow decoder to finish decoding before the frames are passed to the |
803 | | // renderer. |
804 | 0 | if (ctx->hwdec.copying || ctx->hwdec.use_hw_frames) |
805 | 0 | ctx->max_delay_queue = HWDEC_DELAY_QUEUE_COUNT; |
806 | 0 | if (ctx->max_delay_queue && !ctx->hwdec.use_hw_frames) |
807 | 0 | avctx->extra_hw_frames = ctx->max_delay_queue; |
808 | 0 | ctx->hw_probing = true; |
809 | |
|
810 | 0 | threads = ctx->hwdec_opts->hwdec_threads; |
811 | | #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(62, 11, 100) |
812 | | // Vulkan threading was not safe before 62.11.100 |
813 | | bstr hwdec_name = bstr0(ctx->hwdec.name); |
814 | | if (bstr_endswith0(hwdec_name, "vulkan") || bstr_endswith0(hwdec_name, "vulkan-copy")) |
815 | | threads = 1; |
816 | | #endif |
817 | 0 | } |
818 | | |
819 | 79.9k | mp_set_avcodec_threads(vd->log, avctx, threads); |
820 | | |
821 | 79.9k | if (!ctx->use_hwdec && ctx->vo && lavc_param->dr) { |
822 | 79.9k | avctx->opaque = vd; |
823 | 79.9k | avctx->get_buffer2 = get_buffer2_direct; |
824 | 79.9k | } |
825 | | |
826 | 79.9k | avctx->flags |= lavc_param->bitexact ? AV_CODEC_FLAG_BITEXACT : 0; |
827 | 79.9k | avctx->flags2 |= lavc_param->fast ? AV_CODEC_FLAG2_FAST : 0; |
828 | | |
829 | 79.9k | if (lavc_param->show_all) |
830 | 0 | avctx->flags |= AV_CODEC_FLAG_OUTPUT_CORRUPT; |
831 | | |
832 | 79.9k | avctx->skip_loop_filter = lavc_param->skip_loop_filter; |
833 | 79.9k | avctx->skip_idct = lavc_param->skip_idct; |
834 | 79.9k | avctx->skip_frame = lavc_param->skip_frame; |
835 | 79.9k | avctx->apply_cropping = lavc_param->apply_cropping; |
836 | | |
837 | 79.9k | if (lavc_codec->id == AV_CODEC_ID_H264 && lavc_param->old_x264) |
838 | 0 | av_opt_set(avctx, "x264_build", "150", AV_OPT_SEARCH_CHILDREN); |
839 | | |
840 | 79.9k | switch(ctx->opts->film_grain) { |
841 | 0 | case 0: /*CPU*/ |
842 | | // default lavc flags handle film grain within the decoder. |
843 | 0 | break; |
844 | 0 | case 1: /*GPU*/ |
845 | 0 | if (!ctx->vo || |
846 | 0 | (ctx->vo && !(ctx->vo->driver->caps & VO_CAP_FILM_GRAIN))) { |
847 | 0 | MP_MSG(vd, ctx->vo ? MSGL_WARN : MSGL_V, |
848 | 0 | "GPU film grain requested, but VO %s, expect wrong output.\n", |
849 | 0 | ctx->vo ? |
850 | 0 | "does not support applying film grain" : |
851 | 0 | "is not available at decoder initialization to verify support"); |
852 | 0 | } |
853 | |
|
854 | 0 | avctx->export_side_data |= AV_CODEC_EXPORT_DATA_FILM_GRAIN; |
855 | 0 | break; |
856 | 79.9k | default: |
857 | 79.9k | if (ctx->vo && (ctx->vo->driver->caps & VO_CAP_FILM_GRAIN)) |
858 | 0 | avctx->export_side_data |= AV_CODEC_EXPORT_DATA_FILM_GRAIN; |
859 | | |
860 | 79.9k | break; |
861 | 79.9k | } |
862 | | |
863 | 79.9k | mp_set_avopts(vd->log, avctx, lavc_param->avopts); |
864 | | |
865 | | // Do this after the above avopt handling in case it changes values |
866 | 79.9k | ctx->skip_frame = avctx->skip_frame; |
867 | | |
868 | 79.9k | if (mp_set_avctx_codec_headers(avctx, c) < 0) { |
869 | 0 | MP_ERR(vd, "Could not set codec parameters.\n"); |
870 | 0 | goto error; |
871 | 0 | } |
872 | | |
873 | 79.9k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
874 | 79.9k | if (avctx->width > 8192 || avctx->height > 8192) { |
875 | 721 | MP_ERR(vd, "Frame size too big %" PRIu32 "x%" PRIu32 ".\n", avctx->width, avctx->height); |
876 | 721 | goto error; |
877 | 721 | } |
878 | 79.1k | avctx->max_pixels = 2048 * 2048; |
879 | 79.1k | #endif |
880 | | |
881 | | /* open it */ |
882 | 79.1k | if (avcodec_open2(avctx, lavc_codec, NULL) < 0) |
883 | 1.01k | goto error; |
884 | | |
885 | | // Sometimes, the first packet contains information required for correct |
886 | | // decoding of the rest of the stream. The only currently known case is the |
887 | | // x264 build number (encoded in a SEI element), needed to enable a |
888 | | // workaround for broken 4:4:4 streams produced by older x264 versions. |
889 | 78.1k | if (lavc_codec->id == AV_CODEC_ID_H264 && c->first_packet) { |
890 | 5.08k | mp_set_av_packet(ctx->avpkt, c->first_packet, &ctx->codec_timebase); |
891 | 5.08k | avcodec_send_packet(avctx, ctx->avpkt); |
892 | 5.08k | avcodec_receive_frame(avctx, ctx->pic); |
893 | 5.08k | av_frame_unref(ctx->pic); |
894 | 5.08k | avcodec_flush_buffers(ctx->avctx); |
895 | 5.08k | } |
896 | | |
897 | 78.1k | return; |
898 | | |
899 | 1.73k | error: |
900 | 1.73k | MP_ERR(vd, "Could not open codec.\n"); |
901 | 1.73k | uninit_avctx(vd); |
902 | 1.73k | } |
903 | | |
904 | | static void reset_avctx(struct mp_filter *vd) |
905 | 233k | { |
906 | 233k | vd_ffmpeg_ctx *ctx = vd->priv; |
907 | | |
908 | 233k | if (ctx->avctx && avcodec_is_open(ctx->avctx)) |
909 | 150k | avcodec_flush_buffers(ctx->avctx); |
910 | 233k | ctx->flushing = false; |
911 | 233k | ctx->hwdec_request_reinit = false; |
912 | | // Wait for the first keyframe after reset to ensure the decoder state is |
913 | | // valid. Seeking should already jump to a keyframe, so this is safe. |
914 | 233k | ctx->wait_for_keyframe = ctx->use_hwdec ? HWDEC_WAIT_KEYFRAME_COUNT : 0; |
915 | 233k | } |
916 | | |
917 | | static void flush_all(struct mp_filter *vd) |
918 | 169k | { |
919 | 169k | vd_ffmpeg_ctx *ctx = vd->priv; |
920 | | |
921 | 169k | for (int n = 0; n < ctx->num_delay_queue; n++) |
922 | 0 | talloc_free(ctx->delay_queue[n]); |
923 | 169k | ctx->num_delay_queue = 0; |
924 | | |
925 | 169k | for (int n = 0; n < ctx->num_sent_packets; n++) |
926 | 0 | talloc_free(ctx->sent_packets[n]); |
927 | 169k | ctx->num_sent_packets = 0; |
928 | | |
929 | 169k | for (int n = 0; n < ctx->num_requeue_packets; n++) |
930 | 0 | talloc_free(ctx->requeue_packets[n]); |
931 | 169k | ctx->num_requeue_packets = 0; |
932 | | |
933 | 169k | reset_avctx(vd); |
934 | 169k | } |
935 | | |
936 | | static void uninit_avctx(struct mp_filter *vd) |
937 | 161k | { |
938 | 161k | vd_ffmpeg_ctx *ctx = vd->priv; |
939 | | |
940 | 161k | flush_all(vd); |
941 | 161k | av_frame_free(&ctx->pic); |
942 | 161k | mp_free_av_packet(&ctx->avpkt); |
943 | 161k | av_buffer_unref(&ctx->cached_hw_frames_ctx); |
944 | | |
945 | 161k | avcodec_free_context(&ctx->avctx); |
946 | | |
947 | 161k | av_buffer_unref(&ctx->hwdec_dev); |
948 | | |
949 | 161k | ctx->hwdec_failed = false; |
950 | 161k | ctx->hwdec_fail_count = 0; |
951 | 161k | ctx->max_delay_queue = 0; |
952 | 161k | ctx->hw_probing = false; |
953 | 161k | ctx->hwdec = (struct hwdec_info){0}; |
954 | 161k | ctx->use_hwdec = false; |
955 | 161k | } |
956 | | |
957 | | // Number of extra surfaces to allocate on top of what libavcodec computed |
958 | | // for the codec itself, i.e. how many frames the rest of the player may hold |
959 | | // on to at any given time. |
960 | | static int hwdec_get_extra_frames(vd_ffmpeg_ctx *ctx) |
961 | 0 | { |
962 | | // Surface requirement of the player + decoder queue. |
963 | 0 | int hint = ctx->extra_hw_frames_hint + ctx->max_delay_queue; |
964 | |
|
965 | 0 | int extra = ctx->hwdec_opts->hwdec_extra_frames; |
966 | 0 | if (extra >= 0) |
967 | 0 | return extra + hint; |
968 | | |
969 | 0 | extra = HWDEC_EXTRA_FRAMES; |
970 | | // With native hwdec, every frame queued towards or retained by the VO |
971 | | // pins a surface from the fixed pool. Copying wrappers release surfaces |
972 | | // right after download, the fallback is enough for them. |
973 | 0 | if (!ctx->hwdec.copying && ctx->vo) |
974 | 0 | extra = MPMAX(extra, vo_get_num_frame_refs(ctx->vo) + HWDEC_EXTRA_INFLIGHT_FRAMES); |
975 | 0 | return extra + hint; |
976 | 0 | } |
977 | | |
978 | | static int init_generic_hwaccel(struct AVCodecContext *avctx, enum AVPixelFormat hw_fmt) |
979 | 0 | { |
980 | 0 | struct mp_filter *vd = avctx->opaque; |
981 | 0 | vd_ffmpeg_ctx *ctx = vd->priv; |
982 | 0 | AVBufferRef *new_frames_ctx = NULL; |
983 | |
|
984 | 0 | if (!ctx->hwdec.use_hw_frames) |
985 | 0 | return 0; |
986 | | |
987 | 0 | if (!ctx->hwdec_dev) { |
988 | 0 | MP_ERR(ctx, "Missing device context.\n"); |
989 | 0 | goto error; |
990 | 0 | } |
991 | | |
992 | 0 | if (avcodec_get_hw_frames_parameters(avctx, |
993 | 0 | ctx->hwdec_dev, hw_fmt, &new_frames_ctx) < 0) |
994 | 0 | { |
995 | 0 | MP_VERBOSE(ctx, "Hardware decoding of this stream is unsupported?\n"); |
996 | 0 | goto error; |
997 | 0 | } |
998 | | |
999 | 0 | AVHWFramesContext *new_fctx = (void *)new_frames_ctx->data; |
1000 | |
|
1001 | 0 | if (ctx->hwdec_opts->hwdec_image_format) |
1002 | 0 | new_fctx->sw_format = imgfmt2pixfmt(ctx->hwdec_opts->hwdec_image_format); |
1003 | | |
1004 | | // 1 surface is already included by libavcodec. The field is 0 if the |
1005 | | // hwaccel supports dynamic surface allocation. |
1006 | 0 | if (new_fctx->initial_pool_size) |
1007 | 0 | new_fctx->initial_pool_size += hwdec_get_extra_frames(ctx) - 1; |
1008 | |
|
1009 | 0 | const struct hwcontext_fns *fns = |
1010 | 0 | hwdec_get_hwcontext_fns(new_fctx->device_ctx->type); |
1011 | |
|
1012 | 0 | if (fns && fns->refine_hwframes) |
1013 | 0 | fns->refine_hwframes(new_frames_ctx); |
1014 | | |
1015 | | // We might be able to reuse a previously allocated frame pool. |
1016 | 0 | if (ctx->cached_hw_frames_ctx) { |
1017 | 0 | AVHWFramesContext *old_fctx = (void *)ctx->cached_hw_frames_ctx->data; |
1018 | |
|
1019 | 0 | if (new_fctx->format != old_fctx->format || |
1020 | 0 | new_fctx->sw_format != old_fctx->sw_format || |
1021 | 0 | new_fctx->width != old_fctx->width || |
1022 | 0 | new_fctx->height != old_fctx->height || |
1023 | 0 | new_fctx->initial_pool_size != old_fctx->initial_pool_size) |
1024 | 0 | av_buffer_unref(&ctx->cached_hw_frames_ctx); |
1025 | 0 | } |
1026 | |
|
1027 | 0 | if (!ctx->cached_hw_frames_ctx) { |
1028 | 0 | if (av_hwframe_ctx_init(new_frames_ctx) < 0) { |
1029 | 0 | MP_ERR(ctx, "Failed to allocate hw frames.\n"); |
1030 | 0 | goto error; |
1031 | 0 | } |
1032 | | |
1033 | 0 | if (new_fctx->initial_pool_size) { |
1034 | 0 | MP_VERBOSE(ctx, "Allocated a fixed pool of %d hw surfaces.\n", |
1035 | 0 | new_fctx->initial_pool_size); |
1036 | 0 | } |
1037 | |
|
1038 | 0 | ctx->cached_hw_frames_ctx = new_frames_ctx; |
1039 | 0 | new_frames_ctx = NULL; |
1040 | 0 | } |
1041 | | |
1042 | 0 | avctx->hw_frames_ctx = av_buffer_ref(ctx->cached_hw_frames_ctx); |
1043 | 0 | if (!avctx->hw_frames_ctx) |
1044 | 0 | goto error; |
1045 | | |
1046 | 0 | av_buffer_unref(&new_frames_ctx); |
1047 | 0 | return 0; |
1048 | | |
1049 | 0 | error: |
1050 | 0 | av_buffer_unref(&new_frames_ctx); |
1051 | 0 | av_buffer_unref(&ctx->cached_hw_frames_ctx); |
1052 | 0 | return -1; |
1053 | 0 | } |
1054 | | |
1055 | | static enum AVPixelFormat get_format_hwdec(struct AVCodecContext *avctx, |
1056 | | const enum AVPixelFormat *fmt) |
1057 | 0 | { |
1058 | 0 | struct mp_filter *vd = avctx->opaque; |
1059 | 0 | vd_ffmpeg_ctx *ctx = vd->priv; |
1060 | |
|
1061 | 0 | MP_VERBOSE(vd, "Pixel formats supported by decoder:"); |
1062 | 0 | for (int i = 0; fmt[i] != AV_PIX_FMT_NONE; i++) |
1063 | 0 | MP_VERBOSE(vd, " %s", av_get_pix_fmt_name(fmt[i])); |
1064 | 0 | MP_VERBOSE(vd, "\n"); |
1065 | |
|
1066 | 0 | const char *profile = avcodec_profile_name(avctx->codec_id, avctx->profile); |
1067 | 0 | MP_VERBOSE(vd, "Codec profile: %s (0x%x)\n", profile ? profile : "unknown", |
1068 | 0 | avctx->profile); |
1069 | |
|
1070 | 0 | mp_assert(ctx->use_hwdec); |
1071 | | |
1072 | 0 | ctx->hwdec_request_reinit |= ctx->hwdec_failed; |
1073 | 0 | ctx->hwdec_failed = false; |
1074 | |
|
1075 | 0 | enum AVPixelFormat select = AV_PIX_FMT_NONE; |
1076 | 0 | for (int i = 0; fmt[i] != AV_PIX_FMT_NONE; i++) { |
1077 | 0 | if (ctx->hwdec.pix_fmt == fmt[i]) { |
1078 | 0 | if (init_generic_hwaccel(avctx, fmt[i]) < 0) |
1079 | 0 | break; |
1080 | 0 | select = fmt[i]; |
1081 | 0 | break; |
1082 | 0 | } |
1083 | 0 | } |
1084 | |
|
1085 | 0 | if (select == AV_PIX_FMT_NONE) |
1086 | 0 | ctx->hwdec_failed = true; |
1087 | |
|
1088 | 0 | const char *name = av_get_pix_fmt_name(select); |
1089 | 0 | MP_VERBOSE(vd, "Requesting pixfmt '%s' from decoder.\n", name ? name : "-"); |
1090 | 0 | return select; |
1091 | 0 | } |
1092 | | |
1093 | | static int get_buffer2_direct(AVCodecContext *avctx, AVFrame *pic, int flags) |
1094 | 702k | { |
1095 | 702k | struct mp_filter *vd = avctx->opaque; |
1096 | 702k | vd_ffmpeg_ctx *p = vd->priv; |
1097 | | |
1098 | 702k | mp_mutex_lock(&p->dr_lock); |
1099 | | |
1100 | 702k | int w = pic->width; |
1101 | 702k | int h = pic->height; |
1102 | 702k | int linesize_align[AV_NUM_DATA_POINTERS] = {0}; |
1103 | 702k | avcodec_align_dimensions2(avctx, &w, &h, linesize_align); |
1104 | | |
1105 | | // We assume that different alignments are just different power-of-2s. |
1106 | | // Thus, a higher alignment always satisfies a lower alignment. |
1107 | 702k | int stride_align = MP_IMAGE_BYTE_ALIGN; |
1108 | 6.32M | for (int n = 0; n < AV_NUM_DATA_POINTERS; n++) |
1109 | 5.62M | stride_align = MPMAX(stride_align, linesize_align[n]); |
1110 | | |
1111 | | // Note: texel sizes may be NPOT, so use full lcm instead of max |
1112 | 702k | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format); |
1113 | 702k | if (!(desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)) { |
1114 | 2.29M | for (int n = 0; n < desc->nb_components; n++) |
1115 | 1.60M | stride_align = mp_lcm(stride_align, desc->comp[n].step); |
1116 | 686k | } |
1117 | | |
1118 | 702k | int imgfmt = pixfmt2imgfmt(pic->format); |
1119 | 702k | if (!imgfmt) |
1120 | 0 | goto fallback; |
1121 | | |
1122 | 702k | if (p->dr_failed) |
1123 | 666k | goto fallback; |
1124 | | |
1125 | | // (For simplicity, we realloc on any parameter change, instead of trying |
1126 | | // to be clever.) |
1127 | 35.9k | if (stride_align != p->dr_stride_align || w != p->dr_w || h != p->dr_h || |
1128 | 0 | imgfmt != p->dr_imgfmt) |
1129 | 35.9k | { |
1130 | 35.9k | mp_image_pool_clear(p->dr_pool); |
1131 | 35.9k | p->dr_imgfmt = imgfmt; |
1132 | 35.9k | p->dr_w = w; |
1133 | 35.9k | p->dr_h = h; |
1134 | 35.9k | p->dr_stride_align = stride_align; |
1135 | 35.9k | MP_DBG(p, "DR parameter change to %dx%d %s align=%d\n", w, h, |
1136 | 35.9k | mp_imgfmt_to_name(imgfmt), stride_align); |
1137 | 35.9k | } |
1138 | | |
1139 | 35.9k | struct mp_image *img = mp_image_pool_get_no_alloc(p->dr_pool, imgfmt, w, h); |
1140 | 35.9k | if (!img) { |
1141 | 35.9k | bool host_cached = p->opts->dr == -1; // auto |
1142 | 35.9k | int dr_flags = host_cached ? VO_DR_FLAG_HOST_CACHED : 0; |
1143 | 35.9k | MP_DBG(p, "Allocating new%s DR image...\n", host_cached ? " (host-cached)" : ""); |
1144 | 35.9k | img = vo_get_image(p->vo, imgfmt, w, h, stride_align, dr_flags); |
1145 | 35.9k | if (!img) { |
1146 | 35.9k | MP_DBG(p, "...failed..\n"); |
1147 | 35.9k | goto fallback; |
1148 | 35.9k | } |
1149 | | |
1150 | | // Now make the mp_image part of the pool. This requires doing magic to |
1151 | | // the image, so just add it to the pool and get it back to avoid |
1152 | | // dealing with magic ourselves. (Normally this never fails.) |
1153 | 0 | mp_image_pool_add(p->dr_pool, img); |
1154 | 0 | img = mp_image_pool_get_no_alloc(p->dr_pool, imgfmt, w, h); |
1155 | 0 | if (!img) |
1156 | 0 | goto fallback; |
1157 | 0 | } |
1158 | | |
1159 | | // get_buffer2 callers seem very unappreciative of overwriting pic with a |
1160 | | // new reference. The AVCodecContext.get_buffer2 comments tell us exactly |
1161 | | // what we should do, so follow that. |
1162 | 0 | for (int n = 0; n < 4; n++) { |
1163 | 0 | pic->data[n] = img->planes[n]; |
1164 | 0 | pic->linesize[n] = img->stride[n]; |
1165 | 0 | pic->buf[n] = img->bufs[n]; |
1166 | 0 | img->bufs[n] = NULL; |
1167 | 0 | } |
1168 | 0 | talloc_free(img); |
1169 | |
|
1170 | 0 | mp_mutex_unlock(&p->dr_lock); |
1171 | |
|
1172 | 0 | return 0; |
1173 | | |
1174 | 702k | fallback: |
1175 | 702k | if (!p->dr_failed) |
1176 | 702k | MP_VERBOSE(p, "DR failed - disabling.\n"); |
1177 | 702k | p->dr_failed = true; |
1178 | 702k | mp_mutex_unlock(&p->dr_lock); |
1179 | | |
1180 | 702k | return avcodec_default_get_buffer2(avctx, pic, flags); |
1181 | 35.9k | } |
1182 | | |
1183 | | static void prepare_decoding(struct mp_filter *vd) |
1184 | 4.52M | { |
1185 | 4.52M | vd_ffmpeg_ctx *ctx = vd->priv; |
1186 | 4.52M | AVCodecContext *avctx = ctx->avctx; |
1187 | 4.52M | struct vd_lavc_params *opts = ctx->opts; |
1188 | | |
1189 | 4.52M | if (!avctx) |
1190 | 0 | return; |
1191 | | |
1192 | 4.52M | int drop = ctx->framedrop_flags; |
1193 | 4.52M | if (drop == 1) { |
1194 | 0 | avctx->skip_frame = opts->framedrop; // normal framedrop |
1195 | 4.52M | } else if (drop == 2) { |
1196 | 0 | avctx->skip_frame = AVDISCARD_NONREF; // hr-seek framedrop |
1197 | | // Can be much more aggressive for true intra codecs. |
1198 | 0 | if (ctx->intra_only) |
1199 | 0 | avctx->skip_frame = AVDISCARD_ALL; |
1200 | 4.52M | } else { |
1201 | 4.52M | avctx->skip_frame = ctx->skip_frame; // normal playback |
1202 | 4.52M | } |
1203 | | |
1204 | 4.52M | if (ctx->hwdec_request_reinit) |
1205 | 0 | reset_avctx(vd); |
1206 | 4.52M | } |
1207 | | |
1208 | | static void handle_err(struct mp_filter *vd) |
1209 | 397k | { |
1210 | 397k | vd_ffmpeg_ctx *ctx = vd->priv; |
1211 | 397k | struct hwdec_opts *hwdec_opts = ctx->hwdec_opts; |
1212 | | |
1213 | 397k | MP_WARN(vd, "Error while decoding frame%s!\n", |
1214 | 397k | ctx->use_hwdec ? " (hardware decoding)" : ""); |
1215 | | |
1216 | 397k | if (ctx->use_hwdec) { |
1217 | 0 | ctx->hwdec_fail_count += 1; |
1218 | 0 | if (ctx->hwdec_fail_count >= hwdec_opts->software_fallback) |
1219 | 0 | ctx->hwdec_failed = true; |
1220 | 0 | } |
1221 | 397k | } |
1222 | | |
1223 | | static int send_packet(struct mp_filter *vd, struct demux_packet *pkt) |
1224 | 1.30M | { |
1225 | 1.30M | vd_ffmpeg_ctx *ctx = vd->priv; |
1226 | 1.30M | AVCodecContext *avctx = ctx->avctx; |
1227 | | |
1228 | 1.30M | if (ctx->num_requeue_packets && ctx->requeue_packets[0] != pkt) |
1229 | 0 | return AVERROR(EAGAIN); // cannot consume the packet |
1230 | | |
1231 | 1.30M | if (ctx->hwdec_failed) |
1232 | 0 | return AVERROR(EAGAIN); |
1233 | | |
1234 | 1.30M | if (!ctx->avctx) |
1235 | 0 | return AVERROR_EOF; |
1236 | | |
1237 | 1.30M | prepare_decoding(vd); |
1238 | | |
1239 | 1.30M | if (ctx->wait_for_keyframe > 0 && pkt) { |
1240 | 0 | if (!pkt->keyframe && !ctx->intra_only) { |
1241 | 0 | MP_DBG(vd, "Waiting for keyframe after reinit (dropping frame).\n"); |
1242 | 0 | ctx->wait_for_keyframe--; |
1243 | 0 | return 0; |
1244 | 0 | } |
1245 | 0 | ctx->wait_for_keyframe = 0; |
1246 | 0 | } |
1247 | | |
1248 | 1.30M | if (avctx->skip_frame == AVDISCARD_ALL) |
1249 | 0 | return 0; |
1250 | | |
1251 | 1.30M | mp_set_av_packet(ctx->avpkt, pkt, &ctx->codec_timebase); |
1252 | | |
1253 | 1.30M | int ret = avcodec_send_packet(avctx, pkt ? ctx->avpkt : NULL); |
1254 | 1.30M | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) |
1255 | 0 | return ret; |
1256 | | |
1257 | 1.30M | if (ctx->hw_probing && ctx->num_sent_packets < 32 && |
1258 | 0 | ctx->hwdec_opts->software_fallback <= 32) |
1259 | 0 | { |
1260 | 0 | pkt = pkt ? demux_copy_packet(vd->packet_pool, pkt) : NULL; |
1261 | 0 | MP_TARRAY_APPEND(ctx, ctx->sent_packets, ctx->num_sent_packets, pkt); |
1262 | 0 | } |
1263 | | |
1264 | 1.30M | if (ret < 0) |
1265 | 351k | handle_err(vd); |
1266 | 1.30M | return ret; |
1267 | 1.30M | } |
1268 | | |
1269 | | static void send_queued_packet(struct mp_filter *vd) |
1270 | 0 | { |
1271 | 0 | vd_ffmpeg_ctx *ctx = vd->priv; |
1272 | |
|
1273 | 0 | mp_assert(ctx->num_requeue_packets); |
1274 | | |
1275 | 0 | if (send_packet(vd, ctx->requeue_packets[0]) != AVERROR(EAGAIN)) { |
1276 | 0 | talloc_free(ctx->requeue_packets[0]); |
1277 | 0 | MP_TARRAY_REMOVE_AT(ctx->requeue_packets, ctx->num_requeue_packets, 0); |
1278 | 0 | } |
1279 | 0 | } |
1280 | | |
1281 | | // Returns whether decoder is still active (!EOF state). |
1282 | | static int decode_frame(struct mp_filter *vd) |
1283 | 3.22M | { |
1284 | 3.22M | vd_ffmpeg_ctx *ctx = vd->priv; |
1285 | 3.22M | AVCodecContext *avctx = ctx->avctx; |
1286 | | |
1287 | 3.22M | if (!avctx || ctx->force_eof) |
1288 | 2 | return AVERROR_EOF; |
1289 | | |
1290 | 3.22M | prepare_decoding(vd); |
1291 | | |
1292 | | // Re-send old packets (typically after a hwdec fallback during init). |
1293 | 3.22M | if (ctx->num_requeue_packets) |
1294 | 0 | send_queued_packet(vd); |
1295 | | |
1296 | 3.22M | int ret = avcodec_receive_frame(avctx, ctx->pic); |
1297 | 3.22M | if (ret < 0) { |
1298 | 2.75M | if (ret == AVERROR_EOF) { |
1299 | | // If flushing was initialized earlier and has ended now, make it |
1300 | | // start over in case we get new packets at some point in the future. |
1301 | | // This must take the delay queue into account, so avctx returns EOF |
1302 | | // until the delay queue has been drained. |
1303 | 63.5k | if (!ctx->num_delay_queue) |
1304 | 63.5k | reset_avctx(vd); |
1305 | 2.69M | } else if (ret == AVERROR(EAGAIN)) { |
1306 | | // just retry after caller writes a packet |
1307 | 2.64M | } else { |
1308 | 46.3k | handle_err(vd); |
1309 | 46.3k | } |
1310 | 2.75M | return ret; |
1311 | 2.75M | } |
1312 | | |
1313 | 467k | mp_codec_info_from_av(avctx, ctx->codec); |
1314 | | |
1315 | | // If something was decoded successfully, it must return a frame with valid |
1316 | | // data. |
1317 | 467k | mp_assert(ctx->pic->buf[0]); |
1318 | | |
1319 | 467k | struct mp_image *mpi = mp_image_from_av_frame(ctx->pic); |
1320 | 467k | if (!mpi) { |
1321 | 0 | av_frame_unref(ctx->pic); |
1322 | 0 | return ret; |
1323 | 0 | } |
1324 | | |
1325 | 467k | if (mpi->imgfmt == IMGFMT_CUDA && !mpi->planes[0]) { |
1326 | 0 | MP_ERR(vd, "CUDA frame without data. This is a FFmpeg bug.\n"); |
1327 | 0 | talloc_free(mpi); |
1328 | 0 | handle_err(vd); |
1329 | 0 | return AVERROR_BUG; |
1330 | 0 | } |
1331 | | |
1332 | 467k | ctx->hwdec_fail_count = 0; |
1333 | | |
1334 | 467k | mpi->pts = mp_pts_from_av(ctx->pic->pts, &ctx->codec_timebase); |
1335 | 467k | mpi->dts = mp_pts_from_av(ctx->pic->pkt_dts, &ctx->codec_timebase); |
1336 | 467k | mpi->pkt_duration = mp_pts_from_av(ctx->pic->duration, &ctx->codec_timebase); |
1337 | | |
1338 | 467k | av_frame_unref(ctx->pic); |
1339 | | |
1340 | 467k | MP_TARRAY_APPEND(ctx, ctx->delay_queue, ctx->num_delay_queue, mpi); |
1341 | 467k | return ret; |
1342 | 467k | } |
1343 | | |
1344 | | static int receive_frame(struct mp_filter *vd, struct mp_frame *out_frame) |
1345 | 3.22M | { |
1346 | 3.22M | vd_ffmpeg_ctx *ctx = vd->priv; |
1347 | | |
1348 | 3.22M | int ret = decode_frame(vd); |
1349 | | |
1350 | 3.22M | if (ctx->hwdec_failed) { |
1351 | | // Failed hardware decoding? Try the next one, and eventually software. |
1352 | 0 | struct demux_packet **pkts = ctx->sent_packets; |
1353 | 0 | int num_pkts = ctx->num_sent_packets; |
1354 | 0 | ctx->sent_packets = NULL; |
1355 | 0 | ctx->num_sent_packets = 0; |
1356 | | |
1357 | | /* |
1358 | | * We repeatedly force_fallback until we get an avctx, because there are |
1359 | | * certain hwdecs that are really full decoders, and so if these fail, |
1360 | | * they also fail to give us a valid avctx, and the early return path |
1361 | | * here will simply give up on decoding completely if there is no |
1362 | | * decoder. We should never hit an infinite loop as the hwdec list is |
1363 | | * finite and we will eventually exhaust it and fall back to software |
1364 | | * decoding (and in practice, most hwdecs are hwaccels and so the |
1365 | | * decoder will successfully init even if the hwaccel fails later.) |
1366 | | */ |
1367 | 0 | do { |
1368 | 0 | force_fallback(vd); |
1369 | 0 | } while (!ctx->avctx); |
1370 | |
|
1371 | 0 | ctx->requeue_packets = pkts; |
1372 | 0 | ctx->num_requeue_packets = num_pkts; |
1373 | |
|
1374 | 0 | return 0; // force retry |
1375 | 0 | } |
1376 | | |
1377 | 3.22M | if (ret == AVERROR(EAGAIN) && ctx->num_requeue_packets) |
1378 | 0 | return 0; // force retry, so send_queued_packet() gets called |
1379 | | |
1380 | 3.22M | if (ctx->num_delay_queue <= ctx->max_delay_queue && ret != AVERROR_EOF) |
1381 | 2.69M | return ret; |
1382 | | |
1383 | 531k | if (!ctx->num_delay_queue) |
1384 | 63.5k | return ret; |
1385 | | |
1386 | 467k | struct mp_image *res = ctx->delay_queue[0]; |
1387 | 467k | MP_TARRAY_REMOVE_AT(ctx->delay_queue, ctx->num_delay_queue, 0); |
1388 | | |
1389 | 467k | res = res ? mp_img_swap_to_native(res) : NULL; |
1390 | 467k | if (!res) |
1391 | 0 | return AVERROR_UNKNOWN; |
1392 | | |
1393 | 467k | if (ctx->use_hwdec && ctx->hwdec.copying && res->hwctx) { |
1394 | 0 | struct mp_image *sw = mp_image_hw_download(res, ctx->hwdec_swpool); |
1395 | 0 | mp_image_unrefp(&res); |
1396 | 0 | res = sw; |
1397 | 0 | if (!res) { |
1398 | 0 | MP_ERR(vd, "Could not copy back hardware decoded frame.\n"); |
1399 | 0 | ctx->hwdec_fail_count = INT_MAX - 1; // force fallback |
1400 | 0 | handle_err(vd); |
1401 | 0 | return AVERROR_UNKNOWN; |
1402 | 0 | } |
1403 | 0 | } |
1404 | | |
1405 | 467k | if (!ctx->hwdec_notified) { |
1406 | 34.9k | if (ctx->use_hwdec) { |
1407 | 0 | MP_INFO(vd, "Using hardware decoding (%s).\n", |
1408 | 0 | ctx->hwdec.method_name); |
1409 | 34.9k | } else { |
1410 | 34.9k | MP_VERBOSE(vd, "Using software decoding.\n"); |
1411 | 34.9k | } |
1412 | 34.9k | ctx->hwdec_notified = true; |
1413 | 34.9k | } |
1414 | | |
1415 | 467k | if (ctx->hw_probing) { |
1416 | 0 | for (int n = 0; n < ctx->num_sent_packets; n++) |
1417 | 0 | demux_packet_pool_push(vd->packet_pool, ctx->sent_packets[n]); |
1418 | 0 | ctx->num_sent_packets = 0; |
1419 | 0 | ctx->hw_probing = false; |
1420 | 0 | } |
1421 | | |
1422 | 467k | *out_frame = MAKE_FRAME(MP_FRAME_VIDEO, res); |
1423 | 467k | return 0; |
1424 | 467k | } |
1425 | | |
1426 | | static int control(struct mp_filter *vd, enum dec_ctrl cmd, void *arg) |
1427 | 1.74M | { |
1428 | 1.74M | vd_ffmpeg_ctx *ctx = vd->priv; |
1429 | 1.74M | switch (cmd) { |
1430 | 1.32M | case VDCTRL_SET_FRAMEDROP: |
1431 | 1.32M | ctx->framedrop_flags = *(int *)arg; |
1432 | 1.32M | return CONTROL_TRUE; |
1433 | 0 | case VDCTRL_SET_EXTRA_HW_FRAMES: |
1434 | 0 | ctx->extra_hw_frames_hint = *(int *)arg; |
1435 | 0 | return CONTROL_TRUE; |
1436 | 169k | case VDCTRL_CHECK_FORCED_EOF: { |
1437 | 169k | *(bool *)arg = ctx->force_eof; |
1438 | 169k | return CONTROL_TRUE; |
1439 | 0 | } |
1440 | 0 | case VDCTRL_GET_BFRAMES: { |
1441 | 0 | AVCodecContext *avctx = ctx->avctx; |
1442 | 0 | if (!avctx) |
1443 | 0 | break; |
1444 | 0 | *(int *)arg = avctx->has_b_frames; |
1445 | 0 | return CONTROL_TRUE; |
1446 | 0 | } |
1447 | 248k | case VDCTRL_GET_HWDEC: { |
1448 | 248k | if (!ctx->hwdec_notified) |
1449 | 139k | return CONTROL_FALSE; |
1450 | 108k | *(char **)arg = ctx->use_hwdec ? ctx->hwdec.method_name : NULL; |
1451 | 108k | return CONTROL_TRUE; |
1452 | 248k | } |
1453 | 0 | case VDCTRL_FORCE_HWDEC_FALLBACK: |
1454 | 0 | if (ctx->use_hwdec) { |
1455 | 0 | force_fallback(vd); |
1456 | 0 | return ctx->avctx ? CONTROL_OK : CONTROL_ERROR; |
1457 | 0 | } |
1458 | 0 | return CONTROL_FALSE; |
1459 | 0 | case VDCTRL_REINIT: |
1460 | 0 | reinit(vd); |
1461 | 0 | return CONTROL_TRUE; |
1462 | 1.74M | } |
1463 | 0 | return CONTROL_UNKNOWN; |
1464 | 1.74M | } |
1465 | | |
1466 | | static void vd_lavc_process(struct mp_filter *vd) |
1467 | 3.29M | { |
1468 | 3.29M | vd_ffmpeg_ctx *ctx = vd->priv; |
1469 | | |
1470 | 3.29M | lavc_process(vd, &ctx->state, send_packet, receive_frame); |
1471 | 3.29M | } |
1472 | | |
1473 | | static void vd_lavc_reset(struct mp_filter *vd) |
1474 | 8.30k | { |
1475 | 8.30k | vd_ffmpeg_ctx *ctx = vd->priv; |
1476 | | |
1477 | 8.30k | flush_all(vd); |
1478 | | |
1479 | 8.30k | ctx->state = (struct lavc_state){0}; |
1480 | 8.30k | ctx->framedrop_flags = 0; |
1481 | 8.30k | } |
1482 | | |
1483 | | static void vd_lavc_destroy(struct mp_filter *vd) |
1484 | 79.9k | { |
1485 | 79.9k | vd_ffmpeg_ctx *ctx = vd->priv; |
1486 | | |
1487 | 79.9k | uninit_avctx(vd); |
1488 | | |
1489 | 79.9k | mp_mutex_destroy(&ctx->dr_lock); |
1490 | 79.9k | } |
1491 | | |
1492 | | static const struct mp_filter_info vd_lavc_filter = { |
1493 | | .name = "vd_lavc", |
1494 | | .priv_size = sizeof(vd_ffmpeg_ctx), |
1495 | | .process = vd_lavc_process, |
1496 | | .reset = vd_lavc_reset, |
1497 | | .destroy = vd_lavc_destroy, |
1498 | | }; |
1499 | | |
1500 | | static struct mp_decoder *create(struct mp_filter *parent, |
1501 | | struct mp_codec_params *codec, |
1502 | | const char *decoder) |
1503 | 79.9k | { |
1504 | 79.9k | struct mp_filter *vd = mp_filter_create(parent, &vd_lavc_filter); |
1505 | 79.9k | if (!vd) |
1506 | 0 | return NULL; |
1507 | | |
1508 | 79.9k | mp_filter_add_pin(vd, MP_PIN_IN, "in"); |
1509 | 79.9k | mp_filter_add_pin(vd, MP_PIN_OUT, "out"); |
1510 | | |
1511 | 79.9k | vd->log = mp_log_new(vd, parent->log, NULL); |
1512 | | |
1513 | 79.9k | vd_ffmpeg_ctx *ctx = vd->priv; |
1514 | 79.9k | ctx->log = vd->log; |
1515 | 79.9k | ctx->opts_cache = m_config_cache_alloc(ctx, vd->global, &vd_lavc_conf); |
1516 | 79.9k | ctx->opts = ctx->opts_cache->opts; |
1517 | 79.9k | ctx->hwdec_opts_cache = m_config_cache_alloc(ctx, vd->global, &hwdec_conf); |
1518 | 79.9k | ctx->hwdec_opts = ctx->hwdec_opts_cache->opts; |
1519 | 79.9k | ctx->codec = codec; |
1520 | 79.9k | ctx->decoder = talloc_strdup(ctx, decoder); |
1521 | 79.9k | ctx->hwdec_swpool = mp_image_pool_new(ctx); |
1522 | 79.9k | ctx->dr_pool = mp_image_pool_new(ctx); |
1523 | | |
1524 | 79.9k | ctx->public.f = vd; |
1525 | 79.9k | ctx->public.control = control; |
1526 | | |
1527 | 79.9k | mp_mutex_init(&ctx->dr_lock); |
1528 | | |
1529 | | // hwdec/DR |
1530 | 79.9k | struct mp_stream_info *info = mp_filter_find_stream_info(vd); |
1531 | 79.9k | if (info) { |
1532 | 79.9k | ctx->hwdec_devs = info->hwdec_devs; |
1533 | 79.9k | ctx->vo = info->dr_vo; |
1534 | 79.9k | ctx->force_swdec = info->force_swdec; |
1535 | 79.9k | } |
1536 | | |
1537 | 79.9k | reinit(vd); |
1538 | | |
1539 | 79.9k | if (!ctx->avctx) { |
1540 | 1.73k | talloc_free(vd); |
1541 | 1.73k | return NULL; |
1542 | 1.73k | } |
1543 | | |
1544 | 78.1k | codec->codec_desc = ctx->avctx->codec_descriptor->long_name; |
1545 | | |
1546 | 78.1k | return &ctx->public; |
1547 | 79.9k | } |
1548 | | |
1549 | | static void add_decoders(struct mp_decoder_list *list) |
1550 | 96.1k | { |
1551 | 96.1k | mp_add_lavc_decoders(list, AVMEDIA_TYPE_VIDEO); |
1552 | 96.1k | } |
1553 | | |
1554 | | const struct mp_decoder_fns vd_lavc = { |
1555 | | .create = create, |
1556 | | .add_decoders = add_decoders, |
1557 | | }; |