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 <stdbool.h>
22 #include <math.h>
23 #include <assert.h>
24 #include <pthread.h>
25
26 #include <libavutil/buffer.h>
27 #include <libavutil/common.h>
28 #include <libavutil/rational.h>
29
30 #include "config.h"
31 #include "options/options.h"
32 #include "common/msg.h"
33 #include "options/m_config.h"
34 #include "osdep/timer.h"
35 #include "osdep/threads.h"
36
37 #include "demux/demux.h"
38 #include "demux/packet.h"
39
40 #include "common/codecs.h"
41 #include "common/global.h"
42 #include "common/recorder.h"
43 #include "misc/dispatch.h"
44
45 #include "audio/aframe.h"
46 #include "video/out/vo.h"
47 #include "video/csputils.h"
48
49 #include "demux/stheader.h"
50
51 #include "f_async_queue.h"
52 #include "f_decoder_wrapper.h"
53 #include "f_demux_in.h"
54 #include "filter_internal.h"
55
56 struct dec_queue_opts {
57 int use_queue;
58 int64_t max_bytes;
59 int64_t max_samples;
60 double max_duration;
61 };
62
63 #define OPT_BASE_STRUCT struct dec_queue_opts
64
65 static const struct m_option dec_queue_opts_list[] = {
66 {"enable", OPT_FLAG(use_queue)},
67 {"max-secs", OPT_DOUBLE(max_duration), M_RANGE(0, DBL_MAX)},
68 {"max-bytes", OPT_BYTE_SIZE(max_bytes), M_RANGE(0, M_MAX_MEM_BYTES)},
69 {"max-samples", OPT_INT64(max_samples), M_RANGE(0, DBL_MAX)},
70 {0}
71 };
72
73 static const struct m_sub_options vdec_queue_conf = {
74 .opts = dec_queue_opts_list,
75 .size = sizeof(struct dec_queue_opts),
76 .defaults = &(const struct dec_queue_opts){
77 .use_queue = 0,
78 .max_bytes = 512 * 1024 * 1024,
79 .max_samples = 50,
80 .max_duration = 2,
81 },
82 };
83
84 static const struct m_sub_options adec_queue_conf = {
85 .opts = dec_queue_opts_list,
86 .size = sizeof(struct dec_queue_opts),
87 .defaults = &(const struct dec_queue_opts){
88 .use_queue = 0,
89 .max_bytes = 1 * 1024 * 1024,
90 .max_samples = 48000,
91 .max_duration = 1,
92 },
93 };
94
95 #undef OPT_BASE_STRUCT
96 #define OPT_BASE_STRUCT struct dec_wrapper_opts
97
98 struct dec_wrapper_opts {
99 float movie_aspect;
100 int aspect_method;
101 double force_fps;
102 int correct_pts;
103 int video_rotate;
104 char *audio_decoders;
105 char *video_decoders;
106 char *audio_spdif;
107 struct dec_queue_opts *vdec_queue_opts;
108 struct dec_queue_opts *adec_queue_opts;
109 int64_t video_reverse_size;
110 int64_t audio_reverse_size;
111 };
112
113 static int decoder_list_help(struct mp_log *log, const m_option_t *opt,
114 struct bstr name);
115
116 const struct m_sub_options dec_wrapper_conf = {
117 .opts = (const struct m_option[]){
118 {"correct-pts", OPT_FLAG(correct_pts)},
119 {"fps", OPT_DOUBLE(force_fps), M_RANGE(0, DBL_MAX)},
120 {"ad", OPT_STRING(audio_decoders),
121 .help = decoder_list_help},
122 {"vd", OPT_STRING(video_decoders),
123 .help = decoder_list_help},
124 {"audio-spdif", OPT_STRING(audio_spdif),
125 .help = decoder_list_help},
126 {"video-rotate", OPT_CHOICE(video_rotate, {"no", -1}),
127 .flags = UPDATE_IMGPAR, M_RANGE(0, 359)},
128 {"video-aspect-override", OPT_ASPECT(movie_aspect),
129 .flags = UPDATE_IMGPAR, M_RANGE(-1, 10)},
130 {"video-aspect-method", OPT_CHOICE(aspect_method,
131 {"bitstream", 1}, {"container", 2}),
132 .flags = UPDATE_IMGPAR},
133 {"vd-queue", OPT_SUBSTRUCT(vdec_queue_opts, vdec_queue_conf)},
134 {"ad-queue", OPT_SUBSTRUCT(adec_queue_opts, adec_queue_conf)},
135 {"video-reversal-buffer", OPT_BYTE_SIZE(video_reverse_size),
136 M_RANGE(0, M_MAX_MEM_BYTES)},
137 {"audio-reversal-buffer", OPT_BYTE_SIZE(audio_reverse_size),
138 M_RANGE(0, M_MAX_MEM_BYTES)} ,
139 {0}
140 },
141 .size = sizeof(struct dec_wrapper_opts),
142 .defaults = &(const struct dec_wrapper_opts){
143 .correct_pts = 1,
144 .movie_aspect = -1.,
145 .aspect_method = 2,
146 .video_reverse_size = 1 * 1024 * 1024 * 1024,
147 .audio_reverse_size = 64 * 1024 * 1024,
148 },
149 };
150
151 struct priv {
152 struct mp_log *log;
153 struct sh_stream *header;
154
155 // --- The following fields are to be accessed by dec_dispatch (or if that
156 // field is NULL, by the mp_decoder_wrapper user thread).
157 // Use thread_lock() for access outside of the decoder thread.
158
159 bool request_terminate_dec_thread;
160 struct mp_filter *dec_root_filter; // thread root filter; no thread => NULL
161 struct mp_filter *decf; // wrapper filter which drives the decoder
162 struct m_config_cache *opt_cache;
163 struct dec_wrapper_opts *opts;
164 struct dec_queue_opts *queue_opts;
165 struct mp_stream_info stream_info;
166
167 struct mp_codec_params *codec;
168 struct mp_decoder *decoder;
169
170 // Demuxer output.
171 struct mp_pin *demux;
172
173 // Last PTS from decoder (set with each vd_driver->decode() call)
174 double codec_pts;
175 int num_codec_pts_problems;
176
177 // Last packet DTS from decoder (passed through from source packets)
178 double codec_dts;
179 int num_codec_dts_problems;
180
181 // PTS or DTS of packet first read
182 double first_packet_pdts;
183
184 // There was at least one packet with nonsense timestamps.
185 // Intentionally not reset on seeks; its whole purpose is to enable faster
186 // future seeks.
187 int has_broken_packet_pts; // <0: uninitialized, 0: no problems, 1: broken
188
189 int has_broken_decoded_pts;
190
191 int packets_without_output; // number packets sent without frame received
192
193 // Final PTS of previously decoded frame
194 double pts;
195
196 struct mp_image_params dec_format, last_format, fixed_format;
197
198 double fps;
199
200 double start_pts;
201 double start, end;
202 struct demux_packet *new_segment;
203 struct mp_frame packet;
204 bool packet_fed, preroll_discard;
205
206 size_t reverse_queue_byte_size;
207 struct mp_frame *reverse_queue;
208 int num_reverse_queue;
209 bool reverse_queue_complete;
210
211 struct mp_frame decoded_coverart;
212 int coverart_returned; // 0: no, 1: coverart frame itself, 2: EOF returned
213
214 int play_dir;
215
216 // --- The following fields can be accessed only from the mp_decoder_wrapper
217 // user thread.
218 struct mp_decoder_wrapper public;
219
220 // --- Specific access depending on threading stuff.
221 struct mp_async_queue *queue; // decoded frame output queue
222 struct mp_dispatch_queue *dec_dispatch; // non-NULL if decoding thread used
223 bool dec_thread_lock; // debugging (esp. for no-thread case)
224 pthread_t dec_thread;
225 bool dec_thread_valid;
226 pthread_mutex_t cache_lock;
227
228 // --- Protected by cache_lock.
229 char *cur_hwdec;
230 char *decoder_desc;
231 bool try_spdif;
232 bool attached_picture;
233 bool pts_reset;
234 int attempt_framedrops; // try dropping this many frames
235 int dropped_frames; // total frames _probably_ dropped
236 };
237
decoder_list_help(struct mp_log * log,const m_option_t * opt,struct bstr name)238 static int decoder_list_help(struct mp_log *log, const m_option_t *opt,
239 struct bstr name)
240 {
241 if (strcmp(opt->name, "ad") == 0) {
242 struct mp_decoder_list *list = audio_decoder_list();
243 mp_print_decoders(log, MSGL_INFO, "Audio decoders:", list);
244 talloc_free(list);
245 return M_OPT_EXIT;
246 }
247 if (strcmp(opt->name, "vd") == 0) {
248 struct mp_decoder_list *list = video_decoder_list();
249 mp_print_decoders(log, MSGL_INFO, "Video decoders:", list);
250 talloc_free(list);
251 return M_OPT_EXIT;
252 }
253 if (strcmp(opt->name, "audio-spdif") == 0) {
254 mp_info(log, "Choices: ac3,dts-hd,dts (and possibly more)\n");
255 return M_OPT_EXIT;
256 }
257 return 1;
258 }
259
260 // Update cached values for main thread which require access to the decoder
261 // thread state. Must run on/locked with decoder thread.
update_cached_values(struct priv * p)262 static void update_cached_values(struct priv *p)
263 {
264 pthread_mutex_lock(&p->cache_lock);
265
266 p->cur_hwdec = NULL;
267 if (p->decoder && p->decoder->control)
268 p->decoder->control(p->decoder->f, VDCTRL_GET_HWDEC, &p->cur_hwdec);
269
270 pthread_mutex_unlock(&p->cache_lock);
271 }
272
273 // Lock the decoder thread. This may synchronously wait until the decoder thread
274 // is done with its current work item (such as waiting for a frame), and thus
275 // may block for a while. (I.e. avoid during normal playback.)
276 // If no decoder thread is running, this is a no-op, except for some debug stuff.
thread_lock(struct priv * p)277 static void thread_lock(struct priv *p)
278 {
279 if (p->dec_dispatch)
280 mp_dispatch_lock(p->dec_dispatch);
281
282 assert(!p->dec_thread_lock);
283 p->dec_thread_lock = true;
284 }
285
286 // Undo thread_lock().
thread_unlock(struct priv * p)287 static void thread_unlock(struct priv *p)
288 {
289 assert(p->dec_thread_lock);
290 p->dec_thread_lock = false;
291
292 if (p->dec_dispatch)
293 mp_dispatch_unlock(p->dec_dispatch);
294 }
295
296 // This resets only the decoder. Unlike a full reset(), this doesn't imply a
297 // seek reset. This distinction exists only when using timeline stuff (EDL and
298 // ordered chapters). timeline stuff needs to reset the decoder state, but keep
299 // some of the user-relevant state.
reset_decoder(struct priv * p)300 static void reset_decoder(struct priv *p)
301 {
302 p->first_packet_pdts = MP_NOPTS_VALUE;
303 p->start_pts = MP_NOPTS_VALUE;
304 p->codec_pts = MP_NOPTS_VALUE;
305 p->codec_dts = MP_NOPTS_VALUE;
306 p->num_codec_pts_problems = 0;
307 p->num_codec_dts_problems = 0;
308 p->has_broken_decoded_pts = 0;
309 p->packets_without_output = 0;
310 mp_frame_unref(&p->packet);
311 p->packet_fed = false;
312 p->preroll_discard = false;
313 talloc_free(p->new_segment);
314 p->new_segment = NULL;
315 p->start = p->end = MP_NOPTS_VALUE;
316
317 if (p->decoder)
318 mp_filter_reset(p->decoder->f);
319 }
320
decf_reset(struct mp_filter * f)321 static void decf_reset(struct mp_filter *f)
322 {
323 struct priv *p = f->priv;
324 assert(p->decf == f);
325
326 p->pts = MP_NOPTS_VALUE;
327 p->last_format = p->fixed_format = (struct mp_image_params){0};
328
329 pthread_mutex_lock(&p->cache_lock);
330 p->pts_reset = false;
331 p->attempt_framedrops = 0;
332 p->dropped_frames = 0;
333 pthread_mutex_unlock(&p->cache_lock);
334
335 p->coverart_returned = 0;
336
337 for (int n = 0; n < p->num_reverse_queue; n++)
338 mp_frame_unref(&p->reverse_queue[n]);
339 p->num_reverse_queue = 0;
340 p->reverse_queue_byte_size = 0;
341 p->reverse_queue_complete = false;
342
343 reset_decoder(p);
344 }
345
mp_decoder_wrapper_control(struct mp_decoder_wrapper * d,enum dec_ctrl cmd,void * arg)346 int mp_decoder_wrapper_control(struct mp_decoder_wrapper *d,
347 enum dec_ctrl cmd, void *arg)
348 {
349 struct priv *p = d->f->priv;
350 int res = CONTROL_UNKNOWN;
351 if (cmd == VDCTRL_GET_HWDEC) {
352 pthread_mutex_lock(&p->cache_lock);
353 *(char **)arg = p->cur_hwdec;
354 pthread_mutex_unlock(&p->cache_lock);
355 } else {
356 thread_lock(p);
357 if (p->decoder && p->decoder->control)
358 res = p->decoder->control(p->decoder->f, cmd, arg);
359 update_cached_values(p);
360 thread_unlock(p);
361 }
362 return res;
363 }
364
decf_destroy(struct mp_filter * f)365 static void decf_destroy(struct mp_filter *f)
366 {
367 struct priv *p = f->priv;
368 assert(p->decf == f);
369
370 if (p->decoder) {
371 MP_DBG(f, "Uninit decoder.\n");
372 talloc_free(p->decoder->f);
373 p->decoder = NULL;
374 }
375
376 decf_reset(f);
377 mp_frame_unref(&p->decoded_coverart);
378 }
379
video_decoder_list(void)380 struct mp_decoder_list *video_decoder_list(void)
381 {
382 struct mp_decoder_list *list = talloc_zero(NULL, struct mp_decoder_list);
383 vd_lavc.add_decoders(list);
384 return list;
385 }
386
audio_decoder_list(void)387 struct mp_decoder_list *audio_decoder_list(void)
388 {
389 struct mp_decoder_list *list = talloc_zero(NULL, struct mp_decoder_list);
390 ad_lavc.add_decoders(list);
391 return list;
392 }
393
reinit_decoder(struct priv * p)394 static bool reinit_decoder(struct priv *p)
395 {
396 if (p->decoder)
397 talloc_free(p->decoder->f);
398 p->decoder = NULL;
399
400 reset_decoder(p);
401 p->has_broken_packet_pts = -10; // needs 10 packets to reach decision
402
403 talloc_free(p->decoder_desc);
404 p->decoder_desc = NULL;
405
406 const struct mp_decoder_fns *driver = NULL;
407 struct mp_decoder_list *list = NULL;
408 char *user_list = NULL;
409 char *fallback = NULL;
410
411 if (p->codec->type == STREAM_VIDEO) {
412 driver = &vd_lavc;
413 user_list = p->opts->video_decoders;
414 fallback = "h264";
415 } else if (p->codec->type == STREAM_AUDIO) {
416 driver = &ad_lavc;
417 user_list = p->opts->audio_decoders;
418 fallback = "aac";
419
420 pthread_mutex_lock(&p->cache_lock);
421 bool try_spdif = p->try_spdif;
422 pthread_mutex_unlock(&p->cache_lock);
423
424 if (try_spdif && p->codec->codec) {
425 struct mp_decoder_list *spdif =
426 select_spdif_codec(p->codec->codec, p->opts->audio_spdif);
427 if (spdif->num_entries) {
428 driver = &ad_spdif;
429 list = spdif;
430 } else {
431 talloc_free(spdif);
432 }
433 }
434 }
435
436 if (!list) {
437 struct mp_decoder_list *full = talloc_zero(NULL, struct mp_decoder_list);
438 if (driver)
439 driver->add_decoders(full);
440 const char *codec = p->codec->codec;
441 if (codec && strcmp(codec, "null") == 0)
442 codec = fallback;
443 list = mp_select_decoders(p->log, full, codec, user_list);
444 talloc_free(full);
445 }
446
447 mp_print_decoders(p->log, MSGL_V, "Codec list:", list);
448
449 for (int n = 0; n < list->num_entries; n++) {
450 struct mp_decoder_entry *sel = &list->entries[n];
451 MP_VERBOSE(p, "Opening decoder %s\n", sel->decoder);
452
453 p->decoder = driver->create(p->decf, p->codec, sel->decoder);
454 if (p->decoder) {
455 pthread_mutex_lock(&p->cache_lock);
456 p->decoder_desc =
457 talloc_asprintf(p, "%s (%s)", sel->decoder, sel->desc);
458 MP_VERBOSE(p, "Selected codec: %s\n", p->decoder_desc);
459 pthread_mutex_unlock(&p->cache_lock);
460 break;
461 }
462
463 MP_WARN(p, "Decoder init failed for %s\n", sel->decoder);
464 }
465
466 if (!p->decoder) {
467 MP_ERR(p, "Failed to initialize a decoder for codec '%s'.\n",
468 p->codec->codec ? p->codec->codec : "<?>");
469 }
470
471 update_cached_values(p);
472
473 talloc_free(list);
474 return !!p->decoder;
475 }
476
mp_decoder_wrapper_reinit(struct mp_decoder_wrapper * d)477 bool mp_decoder_wrapper_reinit(struct mp_decoder_wrapper *d)
478 {
479 struct priv *p = d->f->priv;
480 thread_lock(p);
481 bool res = reinit_decoder(p);
482 thread_unlock(p);
483 return res;
484 }
485
mp_decoder_wrapper_get_desc(struct mp_decoder_wrapper * d,char * buf,size_t buf_size)486 void mp_decoder_wrapper_get_desc(struct mp_decoder_wrapper *d,
487 char *buf, size_t buf_size)
488 {
489 struct priv *p = d->f->priv;
490 pthread_mutex_lock(&p->cache_lock);
491 snprintf(buf, buf_size, "%s", p->decoder_desc ? p->decoder_desc : "");
492 pthread_mutex_unlock(&p->cache_lock);
493 }
494
mp_decoder_wrapper_set_frame_drops(struct mp_decoder_wrapper * d,int num)495 void mp_decoder_wrapper_set_frame_drops(struct mp_decoder_wrapper *d, int num)
496 {
497 struct priv *p = d->f->priv;
498 pthread_mutex_lock(&p->cache_lock);
499 p->attempt_framedrops = num;
500 pthread_mutex_unlock(&p->cache_lock);
501 }
502
mp_decoder_wrapper_get_frames_dropped(struct mp_decoder_wrapper * d)503 int mp_decoder_wrapper_get_frames_dropped(struct mp_decoder_wrapper *d)
504 {
505 struct priv *p = d->f->priv;
506 pthread_mutex_lock(&p->cache_lock);
507 int res = p->dropped_frames;
508 pthread_mutex_unlock(&p->cache_lock);
509 return res;
510 }
511
mp_decoder_wrapper_get_container_fps(struct mp_decoder_wrapper * d)512 double mp_decoder_wrapper_get_container_fps(struct mp_decoder_wrapper *d)
513 {
514 struct priv *p = d->f->priv;
515 thread_lock(p);
516 double res = p->fps;
517 thread_unlock(p);
518 return res;
519 }
520
mp_decoder_wrapper_set_spdif_flag(struct mp_decoder_wrapper * d,bool spdif)521 void mp_decoder_wrapper_set_spdif_flag(struct mp_decoder_wrapper *d, bool spdif)
522 {
523 struct priv *p = d->f->priv;
524 pthread_mutex_lock(&p->cache_lock);
525 p->try_spdif = spdif;
526 pthread_mutex_unlock(&p->cache_lock);
527 }
528
mp_decoder_wrapper_set_coverart_flag(struct mp_decoder_wrapper * d,bool c)529 void mp_decoder_wrapper_set_coverart_flag(struct mp_decoder_wrapper *d, bool c)
530 {
531 struct priv *p = d->f->priv;
532 pthread_mutex_lock(&p->cache_lock);
533 p->attached_picture = c;
534 pthread_mutex_unlock(&p->cache_lock);
535 }
536
mp_decoder_wrapper_get_pts_reset(struct mp_decoder_wrapper * d)537 bool mp_decoder_wrapper_get_pts_reset(struct mp_decoder_wrapper *d)
538 {
539 struct priv *p = d->f->priv;
540 pthread_mutex_lock(&p->cache_lock);
541 bool res = p->pts_reset;
542 pthread_mutex_unlock(&p->cache_lock);
543 return res;
544 }
545
mp_decoder_wrapper_set_play_dir(struct mp_decoder_wrapper * d,int dir)546 void mp_decoder_wrapper_set_play_dir(struct mp_decoder_wrapper *d, int dir)
547 {
548 struct priv *p = d->f->priv;
549 thread_lock(p);
550 p->play_dir = dir;
551 thread_unlock(p);
552 }
553
is_valid_peak(float sig_peak)554 static bool is_valid_peak(float sig_peak)
555 {
556 return !sig_peak || (sig_peak >= 1 && sig_peak <= 100);
557 }
558
fix_image_params(struct priv * p,struct mp_image_params * params)559 static void fix_image_params(struct priv *p,
560 struct mp_image_params *params)
561 {
562 struct mp_image_params m = *params;
563 struct mp_codec_params *c = p->codec;
564 struct dec_wrapper_opts *opts = p->opts;
565
566 MP_VERBOSE(p, "Decoder format: %s\n", mp_image_params_to_str(params));
567 p->dec_format = *params;
568
569 // While mp_image_params normally always have to have d_w/d_h set, the
570 // decoder signals unknown bitstream aspect ratio with both set to 0.
571 bool use_container = true;
572 if (opts->aspect_method == 1 && m.p_w > 0 && m.p_h > 0) {
573 MP_VERBOSE(p, "Using bitstream aspect ratio.\n");
574 use_container = false;
575 }
576
577 if (use_container && c->par_w > 0 && c->par_h) {
578 MP_VERBOSE(p, "Using container aspect ratio.\n");
579 m.p_w = c->par_w;
580 m.p_h = c->par_h;
581 }
582
583 if (opts->movie_aspect >= 0) {
584 MP_VERBOSE(p, "Forcing user-set aspect ratio.\n");
585 if (opts->movie_aspect == 0) {
586 m.p_w = m.p_h = 1;
587 } else {
588 AVRational a = av_d2q(opts->movie_aspect, INT_MAX);
589 mp_image_params_set_dsize(&m, a.num, a.den);
590 }
591 }
592
593 // Assume square pixels if no aspect ratio is set at all.
594 if (m.p_w <= 0 || m.p_h <= 0)
595 m.p_w = m.p_h = 1;
596
597 m.rotate = p->codec->rotate;
598 m.stereo3d = p->codec->stereo_mode;
599
600 if (opts->video_rotate < 0) {
601 m.rotate = 0;
602 } else {
603 m.rotate = (m.rotate + opts->video_rotate) % 360;
604 }
605
606 mp_colorspace_merge(&m.color, &c->color);
607
608 // Sanitize the HDR peak. Sadly necessary
609 if (!is_valid_peak(m.color.sig_peak)) {
610 MP_WARN(p, "Invalid HDR peak in stream: %f\n", m.color.sig_peak);
611 m.color.sig_peak = 0.0;
612 }
613
614 // Guess missing colorspace fields from metadata. This guarantees all
615 // fields are at least set to legal values afterwards.
616 mp_image_params_guess_csp(&m);
617
618 p->last_format = *params;
619 p->fixed_format = m;
620 }
621
mp_decoder_wrapper_reset_params(struct mp_decoder_wrapper * d)622 void mp_decoder_wrapper_reset_params(struct mp_decoder_wrapper *d)
623 {
624 struct priv *p = d->f->priv;
625 p->last_format = (struct mp_image_params){0};
626 }
627
mp_decoder_wrapper_get_video_dec_params(struct mp_decoder_wrapper * d,struct mp_image_params * m)628 void mp_decoder_wrapper_get_video_dec_params(struct mp_decoder_wrapper *d,
629 struct mp_image_params *m)
630 {
631 struct priv *p = d->f->priv;
632 *m = p->dec_format;
633 }
634
635 // This code exists only because multimedia is so god damn crazy. In a sane
636 // world, the video decoder would always output a video frame with a valid PTS;
637 // this deals with cases where it doesn't.
crazy_video_pts_stuff(struct priv * p,struct mp_image * mpi)638 static void crazy_video_pts_stuff(struct priv *p, struct mp_image *mpi)
639 {
640 // Note: the PTS is reordered, but the DTS is not. Both must be monotonic.
641
642 if (mpi->pts != MP_NOPTS_VALUE) {
643 if (mpi->pts < p->codec_pts)
644 p->num_codec_pts_problems++;
645 p->codec_pts = mpi->pts;
646 }
647
648 if (mpi->dts != MP_NOPTS_VALUE) {
649 if (mpi->dts <= p->codec_dts)
650 p->num_codec_dts_problems++;
651 p->codec_dts = mpi->dts;
652 }
653
654 if (p->has_broken_packet_pts < 0)
655 p->has_broken_packet_pts++;
656 if (p->num_codec_pts_problems)
657 p->has_broken_packet_pts = 1;
658
659 // If PTS is unset, or non-monotonic, fall back to DTS.
660 if ((p->num_codec_pts_problems > p->num_codec_dts_problems ||
661 mpi->pts == MP_NOPTS_VALUE) && mpi->dts != MP_NOPTS_VALUE)
662 mpi->pts = mpi->dts;
663
664 // Compensate for incorrectly using mpeg-style DTS for avi timestamps.
665 if (p->decoder && p->decoder->control && p->codec->avi_dts &&
666 mpi->pts != MP_NOPTS_VALUE && p->fps > 0)
667 {
668 int delay = -1;
669 p->decoder->control(p->decoder->f, VDCTRL_GET_BFRAMES, &delay);
670 mpi->pts -= MPMAX(delay, 0) / p->fps;
671 }
672 }
673
674 // Return true if the current frame is outside segment range.
process_decoded_frame(struct priv * p,struct mp_frame * frame)675 static bool process_decoded_frame(struct priv *p, struct mp_frame *frame)
676 {
677 if (frame->type == MP_FRAME_EOF) {
678 // if we were just draining current segment, don't propagate EOF
679 if (p->new_segment)
680 mp_frame_unref(frame);
681 return true;
682 }
683
684 bool segment_ended = false;
685
686 if (frame->type == MP_FRAME_VIDEO) {
687 struct mp_image *mpi = frame->data;
688
689 crazy_video_pts_stuff(p, mpi);
690
691 struct demux_packet *ccpkt = new_demux_packet_from_buf(mpi->a53_cc);
692 if (ccpkt) {
693 av_buffer_unref(&mpi->a53_cc);
694 ccpkt->pts = mpi->pts;
695 ccpkt->dts = mpi->dts;
696 demuxer_feed_caption(p->header, ccpkt);
697 }
698
699 // Stop hr-seek logic.
700 if (mpi->pts == MP_NOPTS_VALUE || mpi->pts >= p->start_pts)
701 p->start_pts = MP_NOPTS_VALUE;
702
703 if (mpi->pts != MP_NOPTS_VALUE) {
704 segment_ended = p->end != MP_NOPTS_VALUE && mpi->pts >= p->end;
705 if ((p->start != MP_NOPTS_VALUE && mpi->pts < p->start) ||
706 segment_ended)
707 {
708 mp_frame_unref(frame);
709 goto done;
710 }
711 }
712 } else if (frame->type == MP_FRAME_AUDIO) {
713 struct mp_aframe *aframe = frame->data;
714
715 mp_aframe_clip_timestamps(aframe, p->start, p->end);
716 double pts = mp_aframe_get_pts(aframe);
717 if (pts != MP_NOPTS_VALUE && p->start != MP_NOPTS_VALUE)
718 segment_ended = pts >= p->end;
719
720 if (mp_aframe_get_size(aframe) == 0) {
721 mp_frame_unref(frame);
722 goto done;
723 }
724 } else {
725 MP_ERR(p, "unknown frame type from decoder\n");
726 }
727
728 done:
729 return segment_ended;
730 }
731
correct_video_pts(struct priv * p,struct mp_image * mpi)732 static void correct_video_pts(struct priv *p, struct mp_image *mpi)
733 {
734 mpi->pts *= p->play_dir;
735
736 if (!p->opts->correct_pts || mpi->pts == MP_NOPTS_VALUE) {
737 double fps = p->fps > 0 ? p->fps : 25;
738
739 if (p->opts->correct_pts) {
740 if (p->has_broken_decoded_pts <= 1) {
741 MP_WARN(p, "No video PTS! Making something up. Using "
742 "%f FPS.\n", fps);
743 if (p->has_broken_decoded_pts == 1)
744 MP_WARN(p, "Ignoring further missing PTS warnings.\n");
745 p->has_broken_decoded_pts++;
746 }
747 }
748
749 double frame_time = 1.0f / fps;
750 double base = p->first_packet_pdts;
751 mpi->pts = p->pts;
752 if (mpi->pts == MP_NOPTS_VALUE) {
753 mpi->pts = base == MP_NOPTS_VALUE ? 0 : base;
754 } else {
755 mpi->pts += frame_time;
756 }
757 }
758
759 p->pts = mpi->pts;
760 }
761
correct_audio_pts(struct priv * p,struct mp_aframe * aframe)762 static void correct_audio_pts(struct priv *p, struct mp_aframe *aframe)
763 {
764 double dir = p->play_dir;
765
766 double frame_pts = mp_aframe_get_pts(aframe);
767 double frame_len = mp_aframe_duration(aframe);
768
769 if (frame_pts != MP_NOPTS_VALUE) {
770 if (dir < 0)
771 frame_pts = -(frame_pts + frame_len);
772
773 if (p->pts != MP_NOPTS_VALUE)
774 MP_STATS(p, "value %f audio-pts-err", p->pts - frame_pts);
775
776 double diff = fabs(p->pts - frame_pts);
777
778 // Attempt to detect jumps in PTS. Even for the lowest sample rates and
779 // with worst container rounded timestamp, this should be a margin more
780 // than enough.
781 if (p->pts != MP_NOPTS_VALUE && diff > 0.1) {
782 MP_WARN(p, "Invalid audio PTS: %f -> %f\n", p->pts, frame_pts);
783 if (diff >= 5) {
784 pthread_mutex_lock(&p->cache_lock);
785 p->pts_reset = true;
786 pthread_mutex_unlock(&p->cache_lock);
787 }
788 }
789
790 // Keep the interpolated timestamp if it doesn't deviate more
791 // than 1 ms from the real one. (MKV rounded timestamps.)
792 if (p->pts == MP_NOPTS_VALUE || diff > 0.001)
793 p->pts = frame_pts;
794 }
795
796 if (p->pts == MP_NOPTS_VALUE && p->header->missing_timestamps)
797 p->pts = 0;
798
799 mp_aframe_set_pts(aframe, p->pts);
800
801 if (p->pts != MP_NOPTS_VALUE)
802 p->pts += frame_len;
803 }
804
process_output_frame(struct priv * p,struct mp_frame frame)805 static void process_output_frame(struct priv *p, struct mp_frame frame)
806 {
807 if (frame.type == MP_FRAME_VIDEO) {
808 struct mp_image *mpi = frame.data;
809
810 correct_video_pts(p, mpi);
811
812 if (!mp_image_params_equal(&p->last_format, &mpi->params))
813 fix_image_params(p, &mpi->params);
814
815 mpi->params = p->fixed_format;
816 mpi->nominal_fps = p->fps;
817 } else if (frame.type == MP_FRAME_AUDIO) {
818 struct mp_aframe *aframe = frame.data;
819
820 if (p->play_dir < 0 && !mp_aframe_reverse(aframe))
821 MP_ERR(p, "Couldn't reverse audio frame.\n");
822
823 correct_audio_pts(p, aframe);
824 }
825 }
826
mp_decoder_wrapper_set_start_pts(struct mp_decoder_wrapper * d,double pts)827 void mp_decoder_wrapper_set_start_pts(struct mp_decoder_wrapper *d, double pts)
828 {
829 struct priv *p = d->f->priv;
830 p->start_pts = pts;
831 }
832
is_new_segment(struct priv * p,struct mp_frame frame)833 static bool is_new_segment(struct priv *p, struct mp_frame frame)
834 {
835 if (frame.type != MP_FRAME_PACKET)
836 return false;
837 struct demux_packet *pkt = frame.data;
838 return (pkt->segmented && (pkt->start != p->start || pkt->end != p->end ||
839 pkt->codec != p->codec)) ||
840 (p->play_dir < 0 && pkt->back_restart && p->packet_fed);
841 }
842
feed_packet(struct priv * p)843 static void feed_packet(struct priv *p)
844 {
845 if (!p->decoder || !mp_pin_in_needs_data(p->decoder->f->pins[0]))
846 return;
847
848 if (p->decoded_coverart.type)
849 return;
850
851 if (!p->packet.type && !p->new_segment) {
852 p->packet = mp_pin_out_read(p->demux);
853 if (!p->packet.type)
854 return;
855 if (p->packet.type != MP_FRAME_EOF && p->packet.type != MP_FRAME_PACKET) {
856 MP_ERR(p, "invalid frame type from demuxer\n");
857 mp_frame_unref(&p->packet);
858 mp_filter_internal_mark_failed(p->decf);
859 return;
860 }
861 }
862
863 if (!p->packet.type)
864 return;
865
866 // Flush current data if the packet is a new segment.
867 if (is_new_segment(p, p->packet)) {
868 assert(!p->new_segment);
869 p->new_segment = p->packet.data;
870 p->packet = MP_EOF_FRAME;
871 }
872
873 assert(p->packet.type == MP_FRAME_PACKET || p->packet.type == MP_FRAME_EOF);
874 struct demux_packet *packet =
875 p->packet.type == MP_FRAME_PACKET ? p->packet.data : NULL;
876
877 // For video framedropping, including parts of the hr-seek logic.
878 if (p->decoder->control) {
879 double start_pts = p->start_pts;
880 if (p->start != MP_NOPTS_VALUE && (start_pts == MP_NOPTS_VALUE ||
881 p->start > start_pts))
882 start_pts = p->start;
883
884 int framedrop_type = 0;
885
886 pthread_mutex_lock(&p->cache_lock);
887 if (p->attempt_framedrops)
888 framedrop_type = 1;
889 pthread_mutex_unlock(&p->cache_lock);
890
891 if (start_pts != MP_NOPTS_VALUE && packet && p->play_dir > 0 &&
892 packet->pts < start_pts - .005 && !p->has_broken_packet_pts)
893 framedrop_type = 2;
894
895 p->decoder->control(p->decoder->f, VDCTRL_SET_FRAMEDROP, &framedrop_type);
896 }
897
898 if (!p->dec_dispatch && p->public.recorder_sink)
899 mp_recorder_feed_packet(p->public.recorder_sink, packet);
900
901 double pkt_pts = packet ? packet->pts : MP_NOPTS_VALUE;
902 double pkt_dts = packet ? packet->dts : MP_NOPTS_VALUE;
903
904 if (pkt_pts == MP_NOPTS_VALUE)
905 p->has_broken_packet_pts = 1;
906
907 if (packet && packet->dts == MP_NOPTS_VALUE && !p->codec->avi_dts)
908 packet->dts = packet->pts;
909
910 double pkt_pdts = pkt_pts == MP_NOPTS_VALUE ? pkt_dts : pkt_pts;
911 if (p->first_packet_pdts == MP_NOPTS_VALUE)
912 p->first_packet_pdts = pkt_pdts;
913
914 if (packet && packet->back_preroll) {
915 p->preroll_discard = true;
916 packet->pts = packet->dts = MP_NOPTS_VALUE;
917 }
918
919 mp_pin_in_write(p->decoder->f->pins[0], p->packet);
920 p->packet_fed = true;
921 p->packet = MP_NO_FRAME;
922
923 p->packets_without_output += 1;
924 }
925
enqueue_backward_frame(struct priv * p,struct mp_frame frame)926 static void enqueue_backward_frame(struct priv *p, struct mp_frame frame)
927 {
928 bool eof = frame.type == MP_FRAME_EOF;
929
930 if (!eof) {
931 struct dec_wrapper_opts *opts = p->opts;
932
933 uint64_t queue_size = 0;
934 switch (p->header->type) {
935 case STREAM_VIDEO: queue_size = opts->video_reverse_size; break;
936 case STREAM_AUDIO: queue_size = opts->audio_reverse_size; break;
937 }
938
939 if (p->reverse_queue_byte_size >= queue_size) {
940 MP_ERR(p, "Reversal queue overflow, discarding frame.\n");
941 mp_frame_unref(&frame);
942 return;
943 }
944
945 p->reverse_queue_byte_size += mp_frame_approx_size(frame);
946 }
947
948 // Note: EOF (really BOF) is propagated, but not reversed.
949 MP_TARRAY_INSERT_AT(p, p->reverse_queue, p->num_reverse_queue,
950 eof ? 0 : p->num_reverse_queue, frame);
951
952 p->reverse_queue_complete = eof;
953 }
954
read_frame(struct priv * p)955 static void read_frame(struct priv *p)
956 {
957 struct mp_pin *pin = p->decf->ppins[0];
958 struct mp_frame frame = {0};
959
960 if (!p->decoder || !mp_pin_in_needs_data(pin))
961 return;
962
963 if (p->decoded_coverart.type) {
964 if (p->coverart_returned == 0) {
965 frame = mp_frame_ref(p->decoded_coverart);
966 p->coverart_returned = 1;
967 goto output_frame;
968 } else if (p->coverart_returned == 1) {
969 frame = MP_EOF_FRAME;
970 p->coverart_returned = 2;
971 goto output_frame;
972 }
973 return;
974 }
975
976 if (p->reverse_queue_complete && p->num_reverse_queue) {
977 frame = p->reverse_queue[p->num_reverse_queue - 1];
978 p->num_reverse_queue -= 1;
979 goto output_frame;
980 }
981 p->reverse_queue_complete = false;
982
983 frame = mp_pin_out_read(p->decoder->f->pins[1]);
984 if (!frame.type)
985 return;
986
987 pthread_mutex_lock(&p->cache_lock);
988 if (p->attached_picture && frame.type == MP_FRAME_VIDEO)
989 p->decoded_coverart = frame;
990 if (p->attempt_framedrops) {
991 int dropped = MPMAX(0, p->packets_without_output - 1);
992 p->attempt_framedrops = MPMAX(0, p->attempt_framedrops - dropped);
993 p->dropped_frames += dropped;
994 }
995 pthread_mutex_unlock(&p->cache_lock);
996
997 if (p->decoded_coverart.type) {
998 mp_filter_internal_mark_progress(p->decf);
999 return;
1000 }
1001
1002 p->packets_without_output = 0;
1003
1004 if (p->preroll_discard && frame.type != MP_FRAME_EOF) {
1005 double ts = mp_frame_get_pts(frame);
1006 if (ts == MP_NOPTS_VALUE) {
1007 mp_frame_unref(&frame);
1008 mp_filter_internal_mark_progress(p->decf);
1009 return;
1010 }
1011 p->preroll_discard = false;
1012 }
1013
1014 bool segment_ended = process_decoded_frame(p, &frame);
1015
1016 if (p->play_dir < 0 && frame.type) {
1017 enqueue_backward_frame(p, frame);
1018 frame = MP_NO_FRAME;
1019 }
1020
1021 // If there's a new segment, start it as soon as we're drained/finished.
1022 if (segment_ended && p->new_segment) {
1023 struct demux_packet *new_segment = p->new_segment;
1024 p->new_segment = NULL;
1025
1026 reset_decoder(p);
1027
1028 if (new_segment->segmented) {
1029 if (p->codec != new_segment->codec) {
1030 p->codec = new_segment->codec;
1031 if (!mp_decoder_wrapper_reinit(&p->public))
1032 mp_filter_internal_mark_failed(p->decf);
1033 }
1034
1035 p->start = new_segment->start;
1036 p->end = new_segment->end;
1037 }
1038
1039 p->reverse_queue_byte_size = 0;
1040 p->reverse_queue_complete = p->num_reverse_queue > 0;
1041
1042 p->packet = MAKE_FRAME(MP_FRAME_PACKET, new_segment);
1043 mp_filter_internal_mark_progress(p->decf);
1044 }
1045
1046 if (!frame.type) {
1047 mp_filter_internal_mark_progress(p->decf); // make it retry
1048 return;
1049 }
1050
1051 output_frame:
1052 process_output_frame(p, frame);
1053 mp_pin_in_write(pin, frame);
1054 }
1055
update_queue_config(struct priv * p)1056 static void update_queue_config(struct priv *p)
1057 {
1058 if (!p->queue)
1059 return;
1060
1061 struct mp_async_queue_config cfg = {
1062 .max_bytes = p->queue_opts->max_bytes,
1063 .sample_unit = AQUEUE_UNIT_SAMPLES,
1064 .max_samples = p->queue_opts->max_samples,
1065 .max_duration = p->queue_opts->max_duration,
1066 };
1067 mp_async_queue_set_config(p->queue, cfg);
1068 }
1069
decf_process(struct mp_filter * f)1070 static void decf_process(struct mp_filter *f)
1071 {
1072 struct priv *p = f->priv;
1073 assert(p->decf == f);
1074
1075 if (m_config_cache_update(p->opt_cache))
1076 update_queue_config(p);
1077
1078 feed_packet(p);
1079 read_frame(p);
1080 }
1081
dec_thread(void * ptr)1082 static void *dec_thread(void *ptr)
1083 {
1084 struct priv *p = ptr;
1085
1086 char *t_name = "?";
1087 switch (p->header->type) {
1088 case STREAM_VIDEO: t_name = "vdec"; break;
1089 case STREAM_AUDIO: t_name = "adec"; break;
1090 }
1091 mpthread_set_name(t_name);
1092
1093 while (!p->request_terminate_dec_thread) {
1094 mp_filter_graph_run(p->dec_root_filter);
1095 update_cached_values(p);
1096 mp_dispatch_queue_process(p->dec_dispatch, INFINITY);
1097 }
1098
1099 return NULL;
1100 }
1101
public_f_reset(struct mp_filter * f)1102 static void public_f_reset(struct mp_filter *f)
1103 {
1104 struct priv *p = f->priv;
1105 assert(p->public.f == f);
1106
1107 if (p->queue) {
1108 mp_async_queue_reset(p->queue);
1109 thread_lock(p);
1110 if (p->dec_root_filter)
1111 mp_filter_reset(p->dec_root_filter);
1112 mp_dispatch_interrupt(p->dec_dispatch);
1113 thread_unlock(p);
1114 mp_async_queue_resume(p->queue);
1115 }
1116 }
1117
public_f_destroy(struct mp_filter * f)1118 static void public_f_destroy(struct mp_filter *f)
1119 {
1120 struct priv *p = f->priv;
1121 assert(p->public.f == f);
1122
1123 if (p->dec_thread_valid) {
1124 assert(p->dec_dispatch);
1125 thread_lock(p);
1126 p->request_terminate_dec_thread = 1;
1127 mp_dispatch_interrupt(p->dec_dispatch);
1128 thread_unlock(p);
1129 pthread_join(p->dec_thread, NULL);
1130 p->dec_thread_valid = false;
1131 }
1132
1133 mp_filter_free_children(f);
1134
1135 talloc_free(p->dec_root_filter);
1136 talloc_free(p->queue);
1137 pthread_mutex_destroy(&p->cache_lock);
1138 }
1139
1140 static const struct mp_filter_info decf_filter = {
1141 .name = "decode",
1142 .process = decf_process,
1143 .reset = decf_reset,
1144 .destroy = decf_destroy,
1145 };
1146
1147 static const struct mp_filter_info decode_wrapper_filter = {
1148 .name = "decode_wrapper",
1149 .priv_size = sizeof(struct priv),
1150 .reset = public_f_reset,
1151 .destroy = public_f_destroy,
1152 };
1153
wakeup_dec_thread(void * ptr)1154 static void wakeup_dec_thread(void *ptr)
1155 {
1156 struct priv *p = ptr;
1157
1158 mp_dispatch_interrupt(p->dec_dispatch);
1159 }
1160
onlock_dec_thread(void * ptr)1161 static void onlock_dec_thread(void *ptr)
1162 {
1163 struct priv *p = ptr;
1164
1165 mp_filter_graph_interrupt(p->dec_root_filter);
1166 }
1167
mp_decoder_wrapper_create(struct mp_filter * parent,struct sh_stream * src)1168 struct mp_decoder_wrapper *mp_decoder_wrapper_create(struct mp_filter *parent,
1169 struct sh_stream *src)
1170 {
1171 struct mp_filter *public_f = mp_filter_create(parent, &decode_wrapper_filter);
1172 if (!public_f)
1173 return NULL;
1174
1175 struct priv *p = public_f->priv;
1176 p->public.f = public_f;
1177
1178 pthread_mutex_init(&p->cache_lock, NULL);
1179 p->opt_cache = m_config_cache_alloc(p, public_f->global, &dec_wrapper_conf);
1180 p->opts = p->opt_cache->opts;
1181 p->header = src;
1182 p->codec = p->header->codec;
1183 p->play_dir = 1;
1184 mp_filter_add_pin(public_f, MP_PIN_OUT, "out");
1185
1186 if (p->header->type == STREAM_VIDEO) {
1187 p->log = mp_log_new(p, parent->global->log, "!vd");
1188
1189 p->fps = src->codec->fps;
1190
1191 MP_VERBOSE(p, "Container reported FPS: %f\n", p->fps);
1192
1193 if (p->opts->force_fps) {
1194 p->fps = p->opts->force_fps;
1195 MP_INFO(p, "FPS forced to %5.3f.\n", p->fps);
1196 MP_INFO(p, "Use --no-correct-pts to force FPS based timing.\n");
1197 }
1198
1199 p->queue_opts = p->opts->vdec_queue_opts;
1200 } else if (p->header->type == STREAM_AUDIO) {
1201 p->log = mp_log_new(p, parent->global->log, "!ad");
1202 p->queue_opts = p->opts->adec_queue_opts;
1203 } else {
1204 goto error;
1205 }
1206
1207 if (p->queue_opts && p->queue_opts->use_queue) {
1208 p->queue = mp_async_queue_create();
1209 p->dec_dispatch = mp_dispatch_create(p);
1210 p->dec_root_filter = mp_filter_create_root(public_f->global);
1211 mp_filter_graph_set_wakeup_cb(p->dec_root_filter, wakeup_dec_thread, p);
1212 mp_dispatch_set_onlock_fn(p->dec_dispatch, onlock_dec_thread, p);
1213
1214 struct mp_stream_info *sinfo = mp_filter_find_stream_info(parent);
1215 if (sinfo) {
1216 p->dec_root_filter->stream_info = &p->stream_info;
1217 p->stream_info = (struct mp_stream_info){
1218 .dr_vo = sinfo->dr_vo,
1219 .hwdec_devs = sinfo->hwdec_devs,
1220 };
1221 }
1222
1223 update_queue_config(p);
1224 }
1225
1226 p->decf = mp_filter_create(p->dec_root_filter ? p->dec_root_filter : public_f,
1227 &decf_filter);
1228 p->decf->priv = p;
1229 p->decf->log = public_f->log = p->log;
1230 mp_filter_add_pin(p->decf, MP_PIN_OUT, "out");
1231
1232 struct mp_filter *demux = mp_demux_in_create(p->decf, p->header);
1233 if (!demux)
1234 goto error;
1235 p->demux = demux->pins[0];
1236
1237 decf_reset(p->decf);
1238
1239 if (p->queue) {
1240 struct mp_filter *f_in =
1241 mp_async_queue_create_filter(public_f, MP_PIN_OUT, p->queue);
1242 struct mp_filter *f_out =
1243 mp_async_queue_create_filter(p->decf, MP_PIN_IN, p->queue);
1244 mp_pin_connect(public_f->ppins[0], f_in->pins[0]);
1245 mp_pin_connect(f_out->pins[0], p->decf->pins[0]);
1246
1247 p->dec_thread_valid = true;
1248 if (pthread_create(&p->dec_thread, NULL, dec_thread, p)) {
1249 p->dec_thread_valid = false;
1250 goto error;
1251 }
1252 } else {
1253 mp_pin_connect(public_f->ppins[0], p->decf->pins[0]);
1254 }
1255
1256 public_f_reset(public_f);
1257
1258 return &p->public;
1259 error:
1260 talloc_free(public_f);
1261 return NULL;
1262 }
1263
lavc_process(struct mp_filter * f,struct lavc_state * state,int (* send)(struct mp_filter * f,struct demux_packet * pkt),int (* receive)(struct mp_filter * f,struct mp_frame * res))1264 void lavc_process(struct mp_filter *f, struct lavc_state *state,
1265 int (*send)(struct mp_filter *f, struct demux_packet *pkt),
1266 int (*receive)(struct mp_filter *f, struct mp_frame *res))
1267 {
1268 if (!mp_pin_in_needs_data(f->ppins[1]))
1269 return;
1270
1271 struct mp_frame frame = {0};
1272 int ret_recv = receive(f, &frame);
1273 if (frame.type) {
1274 state->eof_returned = false;
1275 mp_pin_in_write(f->ppins[1], frame);
1276 } else if (ret_recv == AVERROR_EOF) {
1277 if (!state->eof_returned)
1278 mp_pin_in_write(f->ppins[1], MP_EOF_FRAME);
1279 state->eof_returned = true;
1280 state->packets_sent = false;
1281 } else if (ret_recv == AVERROR(EAGAIN)) {
1282 // Need to feed a packet.
1283 frame = mp_pin_out_read(f->ppins[0]);
1284 struct demux_packet *pkt = NULL;
1285 if (frame.type == MP_FRAME_PACKET) {
1286 pkt = frame.data;
1287 } else if (frame.type != MP_FRAME_EOF) {
1288 if (frame.type) {
1289 MP_ERR(f, "unexpected frame type\n");
1290 mp_frame_unref(&frame);
1291 mp_filter_internal_mark_failed(f);
1292 }
1293 return;
1294 } else if (!state->packets_sent) {
1295 // EOF only; just return it, without requiring send/receive to
1296 // pass it through properly.
1297 mp_pin_in_write(f->ppins[1], MP_EOF_FRAME);
1298 return;
1299 }
1300 int ret_send = send(f, pkt);
1301 if (ret_send == AVERROR(EAGAIN)) {
1302 // Should never happen, but can happen with broken decoders.
1303 MP_WARN(f, "could not consume packet\n");
1304 mp_pin_out_unread(f->ppins[0], frame);
1305 mp_filter_wakeup(f);
1306 return;
1307 }
1308 state->packets_sent = true;
1309 talloc_free(pkt);
1310 mp_filter_internal_mark_progress(f);
1311 } else {
1312 // Decoding error, or hwdec fallback recovery. Just try again.
1313 mp_filter_internal_mark_progress(f);
1314 }
1315 }
1316