[81392c]: / src / post / deinterlace / xine_plugin.c  Maximize  Restore  History

Download this file

939 lines (783 with data), 35.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/*
* Copyright (C) 2000-2004 the xine project
*
* This file is part of xine, a free video player.
*
* xine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* xine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*
* advanced video deinterlacer plugin
* Jun/2003 by Miguel Freitas
*
* heavily based on tvtime.sf.net by Billy Biggs
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/*
#define LOG
*/
#include "xine_internal.h"
#include "post.h"
#include "xineutils.h"
#include "xine_buffer.h"
#include <pthread.h>
#include "tvtime.h"
#include "speedy.h"
#include "deinterlace.h"
#include "plugins/plugins.h"
/* plugin class initialization function */
static void *deinterlace_init_plugin(xine_t *xine, void *);
/* plugin catalog information */
static const post_info_t deinterlace_special_info = { XINE_POST_TYPE_VIDEO_FILTER };
const plugin_info_t xine_plugin_info[] EXPORTED = {
/* type, API, "name", version, special_info, init_function */
{ PLUGIN_POST | PLUGIN_MUST_PRELOAD, 9, "tvtime", XINE_VERSION_CODE, &deinterlace_special_info, &deinterlace_init_plugin },
{ PLUGIN_NONE, 0, "", 0, NULL, NULL }
};
typedef struct post_plugin_deinterlace_s post_plugin_deinterlace_t;
#define MAX_NUM_METHODS 30
static const char *enum_methods[MAX_NUM_METHODS];
static char *enum_pulldown[] = { "none", "vektor", NULL };
static char *enum_framerate[] = { "full", "half_top", "half_bottom", NULL };
static void *help_string;
/*
* this is the struct used by "parameters api"
*/
typedef struct deinterlace_parameters_s {
int method;
int enabled;
int pulldown;
int pulldown_error_wait;
int framerate_mode;
int judder_correction;
int use_progressive_frame_flag;
int chroma_filter;
int cheap_mode;
} deinterlace_parameters_t;
/*
* description of params struct
*/
START_PARAM_DESCR( deinterlace_parameters_t )
PARAM_ITEM( POST_PARAM_TYPE_INT, method, enum_methods, 0, 0, 0,
"deinterlace method" )
PARAM_ITEM( POST_PARAM_TYPE_BOOL, enabled, NULL, 0, 1, 0,
"enable/disable" )
PARAM_ITEM( POST_PARAM_TYPE_INT, pulldown, enum_pulldown, 0, 0, 0,
"pulldown algorithm" )
PARAM_ITEM( POST_PARAM_TYPE_INT, pulldown_error_wait, NULL, 0, 0, 0,
"number of frames of telecine pattern sync required before mode change" )
PARAM_ITEM( POST_PARAM_TYPE_INT, framerate_mode, enum_framerate, 0, 0, 0,
"framerate output mode" )
PARAM_ITEM( POST_PARAM_TYPE_BOOL, judder_correction, NULL, 0, 1, 0,
"make frames evenly spaced for film mode (24 fps)" )
PARAM_ITEM( POST_PARAM_TYPE_BOOL, use_progressive_frame_flag, NULL, 0, 1, 0,
"disable deinterlacing when progressive_frame flag is set" )
PARAM_ITEM( POST_PARAM_TYPE_BOOL, chroma_filter, NULL, 0, 1, 0,
"apply chroma filter after deinterlacing" )
PARAM_ITEM( POST_PARAM_TYPE_BOOL, cheap_mode, NULL, 0, 1, 0,
"skip image format conversion - cheaper but not 100% correct" )
END_PARAM_DESCR( param_descr )
#define NUM_RECENT_FRAMES 2
#define FPS_24_DURATION 3754
#define FRAMES_TO_SYNC 20
/* plugin structure */
struct post_plugin_deinterlace_s {
post_plugin_t post;
xine_post_in_t parameter_input;
/* private data */
int cur_method;
int enabled;
int pulldown;
int framerate_mode;
int judder_correction;
int use_progressive_frame_flag;
int chroma_filter;
int cheap_mode;
tvtime_t *tvtime;
int tvtime_changed;
int tvtime_last_filmmode;
int vo_deinterlace_enabled;
int framecounter;
uint8_t rff_pattern;
vo_frame_t *recent_frame[NUM_RECENT_FRAMES];
pthread_mutex_t lock;
};
typedef struct post_class_deinterlace_s {
post_class_t class;
deinterlace_parameters_t init_param;
} post_class_deinterlace_t;
static void _flush_frames(post_plugin_deinterlace_t *this)
{
int i;
for( i = 0; i < NUM_RECENT_FRAMES; i++ ) {
if( this->recent_frame[i] ) {
this->recent_frame[i]->free(this->recent_frame[i]);
this->recent_frame[i] = NULL;
}
}
this->tvtime_changed++;
}
static int set_parameters (xine_post_t *this_gen, void *param_gen) {
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)this_gen;
deinterlace_parameters_t *param = (deinterlace_parameters_t *)param_gen;
pthread_mutex_lock (&this->lock);
if( this->enabled != param->enabled ||
this->cheap_mode != param->cheap_mode )
_flush_frames(this);
this->cur_method = param->method;
this->enabled = param->enabled;
this->pulldown = param->pulldown;
this->tvtime->pulldown_error_wait = param->pulldown_error_wait;
this->framerate_mode = param->framerate_mode;
this->judder_correction = param->judder_correction;
this->use_progressive_frame_flag = param->use_progressive_frame_flag;
this->chroma_filter = param->chroma_filter;
this->cheap_mode = param->cheap_mode;
this->tvtime_changed++;
pthread_mutex_unlock (&this->lock);
return 1;
}
static int get_parameters (xine_post_t *this_gen, void *param_gen) {
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)this_gen;
deinterlace_parameters_t *param = (deinterlace_parameters_t *)param_gen;
param->method = this->cur_method;
param->enabled = this->enabled;
param->pulldown = this->pulldown;
param->pulldown_error_wait = this->tvtime->pulldown_error_wait;
param->framerate_mode = this->framerate_mode;
param->judder_correction = this->judder_correction;
param->use_progressive_frame_flag = this->use_progressive_frame_flag;
param->chroma_filter = this->chroma_filter;
param->cheap_mode = this->cheap_mode;
return 1;
}
static xine_post_api_descr_t * get_param_descr (void) {
return &param_descr;
}
static char * get_static_help (void) {
return _("Advanced tvtime/deinterlacer plugin with pulldown detection\n"
"This plugin aims to provide deinterlacing mechanisms comparable "
"to high quality progressive DVD players and so called "
"line-doublers, for use with computer monitors, projectors and "
"other progressive display devices.\n"
"\n"
"Parameters\n"
"\n"
" Method: Select deinterlacing method/algorithm to use, see below for "
"explanation of each method.\n"
"\n"
" Enabled: Enable/disable the plugin.\n"
"\n"
" Pulldown_error_wait: Ensures that the telecine pattern has been "
"locked for this many frames before changing to filmmode.\n"
"\n"
" Pulldown: Choose the 2-3 pulldown detection algorithm. 24 FPS films "
"that have being converted to NTSC can be detected and intelligently "
"reconstructed to their original (non-interlaced) frames.\n"
"\n"
" Framerate_mode: Selecting 'full' will deinterlace every field "
"to an unique frame for television quality and beyond. This feature will "
"effetively double the frame rate, improving smoothness. Note, however, "
"that full 59.94 FPS is not possible with plain 2.4 Linux kernel (that "
"use a timer interrupt frequency of 100Hz). Newer RedHat and 2.6 kernels "
"use higher HZ settings (512 and 1000, respectively) and should work fine.\n"
"\n"
" Judder_correction: Once 2-3 pulldown is enabled and a film material "
"is detected, it is possible to reduce the frame rate to original rate "
"used (24 FPS). This will make the frames evenly spaced in time, "
"matching the speed they were shot and eliminating the judder effect.\n"
"\n"
" Use_progressive_frame_flag: Well mastered MPEG2 streams uses a flag "
"to indicate progressive material. This setting control whether we trust "
"this flag or not (some rare and buggy mpeg2 streams set it wrong).\n"
"\n"
" Chroma_filter: DVD/MPEG2 use an interlaced image format that has "
"a very poor vertical chroma resolution. Upsampling the chroma for purposes "
"of deinterlacing may cause some artifacts to occur (eg. colour stripes). Use "
"this option to blur the chroma vertically after deinterlacing to remove "
"the artifacts. Warning: cpu intensive.\n"
"\n"
" Cheap_mode: This will skip the expensive YV12->YUY2 image conversion, "
"tricking tvtime/dscaler routines like if they were still handling YUY2 "
"images. Of course, this is not correct, not all pixels will be evaluated "
"by the algorithms to decide the regions to deinterlace and chroma will be "
"processed separately. Nevertheless, it allows people with not so fast "
"systems to try deinterlace algorithms, in a tradeoff between quality "
"and cpu usage.\n"
"\n"
"* Uses several algorithms from tvtime and dscaler projects.\n"
"Deinterlacing methods: (Not all methods are available for all platforms)\n"
"\n"
);
}
static char * get_help (void) {
return (char *)help_string;
}
static xine_post_api_t post_api = {
set_parameters,
get_parameters,
get_param_descr,
get_help,
};
/* plugin class functions */
static post_plugin_t *deinterlace_open_plugin(post_class_t *class_gen, int inputs,
xine_audio_port_t **audio_target,
xine_video_port_t **video_target);
static char *deinterlace_get_identifier(post_class_t *class_gen);
static char *deinterlace_get_description(post_class_t *class_gen);
static void deinterlace_class_dispose(post_class_t *class_gen);
/* plugin instance functions */
static void deinterlace_dispose(post_plugin_t *this_gen);
/* replaced video_port functions */
static int deinterlace_get_property(xine_video_port_t *port_gen, int property);
static int deinterlace_set_property(xine_video_port_t *port_gen, int property, int value);
static void deinterlace_flush(xine_video_port_t *port_gen);
static void deinterlace_open(xine_video_port_t *port_gen, xine_stream_t *stream);
static void deinterlace_close(xine_video_port_t *port_gen, xine_stream_t *stream);
/* frame intercept check */
static int deinterlace_intercept_frame(post_video_port_t *port, vo_frame_t *frame);
/* replaced vo_frame functions */
static int deinterlace_draw(vo_frame_t *frame, xine_stream_t *stream);
static void *deinterlace_init_plugin(xine_t *xine, void *data)
{
post_class_deinterlace_t *class = calloc(1, sizeof(post_class_deinterlace_t));
uint32_t config_flags = xine_mm_accel();
int i;
if (!class)
return NULL;
class->class.open_plugin = deinterlace_open_plugin;
class->class.get_identifier = deinterlace_get_identifier;
class->class.get_description = deinterlace_get_description;
class->class.dispose = deinterlace_class_dispose;
setup_speedy_calls(xine_mm_accel(),0);
register_deinterlace_method( linear_get_method() );
register_deinterlace_method( linearblend_get_method() );
register_deinterlace_method( greedy_get_method() );
register_deinterlace_method( greedy2frame_get_method() );
register_deinterlace_method( weave_get_method() );
register_deinterlace_method( double_get_method() );
register_deinterlace_method( vfir_get_method() );
register_deinterlace_method( scalerbob_get_method() );
register_deinterlace_method( dscaler_greedyh_get_method() );
register_deinterlace_method( dscaler_tomsmocomp_get_method() );
filter_deinterlace_methods( config_flags, 5 /*fieldsavailable*/ );
if( !get_num_deinterlace_methods() ) {
xprintf(xine, XINE_VERBOSITY_LOG,
_("tvtime: No deinterlacing methods available, exiting.\n"));
return NULL;
}
help_string = xine_buffer_init(1024);
xine_buffer_strcat( help_string, get_static_help() );
enum_methods[0] = "use_vo_driver";
for(i = 0; i < get_num_deinterlace_methods(); i++ ) {
deinterlace_method_t *method;
method = get_deinterlace_method(i);
enum_methods[i+1] = method->short_name;
xine_buffer_strcat( help_string, "[" );
xine_buffer_strcat( help_string, method->short_name );
xine_buffer_strcat( help_string, "] " );
xine_buffer_strcat( help_string, method->name );
xine_buffer_strcat( help_string, ":\n" );
if (method->description)
xine_buffer_strcat( help_string, method->description );
xine_buffer_strcat( help_string, "\n---\n" );
}
enum_methods[i+1] = NULL;
/* Some default values */
class->init_param.method = 1; /* First (plugin) method available */
class->init_param.enabled = 1;
class->init_param.pulldown = 1; /* vektor */
class->init_param.pulldown_error_wait = 60; /* about one second */
class->init_param.framerate_mode = 0; /* full */
class->init_param.judder_correction = 1;
class->init_param.use_progressive_frame_flag = 1;
class->init_param.chroma_filter = 0;
class->init_param.cheap_mode = 0;
return &class->class;
}
static post_plugin_t *deinterlace_open_plugin(post_class_t *class_gen, int inputs,
xine_audio_port_t **audio_target,
xine_video_port_t **video_target)
{
post_plugin_deinterlace_t *this = calloc(1, sizeof(post_plugin_deinterlace_t));
post_in_t *input;
xine_post_in_t *input_api;
post_out_t *output;
post_class_deinterlace_t *class = (post_class_deinterlace_t *)class_gen;
post_video_port_t *port;
if (!this || !video_target || !video_target[0]) {
free(this);
return NULL;
}
_x_post_init(&this->post, 0, 1);
this->tvtime = tvtime_new_context();
this->tvtime_changed++;
this->tvtime_last_filmmode = 0;
pthread_mutex_init (&this->lock, NULL);
set_parameters (&this->post.xine_post, &class->init_param);
port = _x_post_intercept_video_port(&this->post, video_target[0], &input, &output);
/* replace with our own get_frame function */
port->new_port.open = deinterlace_open;
port->new_port.close = deinterlace_close;
port->new_port.get_property = deinterlace_get_property;
port->new_port.set_property = deinterlace_set_property;
port->new_port.flush = deinterlace_flush;
port->intercept_frame = deinterlace_intercept_frame;
port->new_frame->draw = deinterlace_draw;
input_api = &this->parameter_input;
input_api->name = "parameters";
input_api->type = XINE_POST_DATA_PARAMETERS;
input_api->data = &post_api;
xine_list_push_back(this->post.input, input_api);
input->xine_in.name = "video";
output->xine_out.name = "deinterlaced video";
this->post.xine_post.video_input[0] = &port->new_port;
this->post.dispose = deinterlace_dispose;
return &this->post;
}
static char *deinterlace_get_identifier(post_class_t *class_gen)
{
return "tvtime";
}
static char *deinterlace_get_description(post_class_t *class_gen)
{
return "advanced deinterlacer plugin with pulldown detection";
}
static void deinterlace_class_dispose(post_class_t *class_gen)
{
xine_buffer_free(help_string);
free(class_gen);
}
static void deinterlace_dispose(post_plugin_t *this_gen)
{
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)this_gen;
if (_x_post_dispose(this_gen)) {
_flush_frames(this);
pthread_mutex_destroy(&this->lock);
free(this->tvtime);
free(this);
}
}
static int deinterlace_get_property(xine_video_port_t *port_gen, int property) {
post_video_port_t *port = (post_video_port_t *)port_gen;
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)port->post;
if( property == XINE_PARAM_VO_DEINTERLACE && this->cur_method )
return this->enabled;
else
return port->original_port->get_property(port->original_port, property);
}
static int deinterlace_set_property(xine_video_port_t *port_gen, int property, int value) {
post_video_port_t *port = (post_video_port_t *)port_gen;
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)port->post;
if( property == XINE_PARAM_VO_DEINTERLACE ) {
pthread_mutex_lock (&this->lock);
if( this->enabled != value )
_flush_frames(this);
this->enabled = value;
pthread_mutex_unlock (&this->lock);
this->vo_deinterlace_enabled = this->enabled && (!this->cur_method);
port->original_port->set_property(port->original_port,
XINE_PARAM_VO_DEINTERLACE,
this->vo_deinterlace_enabled);
return this->enabled;
} else
return port->original_port->set_property(port->original_port, property, value);
}
static void deinterlace_flush(xine_video_port_t *port_gen) {
post_video_port_t *port = (post_video_port_t *)port_gen;
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)port->post;
_flush_frames(this);
port->original_port->flush(port->original_port);
}
static void deinterlace_open(xine_video_port_t *port_gen, xine_stream_t *stream)
{
post_video_port_t *port = (post_video_port_t *)port_gen;
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)port->post;
_x_post_rewire(&this->post);
_x_post_inc_usage(port);
port->stream = stream;
(port->original_port->open) (port->original_port, stream);
this->vo_deinterlace_enabled = !this->cur_method;
port->original_port->set_property(port->original_port,
XINE_PARAM_VO_DEINTERLACE,
this->vo_deinterlace_enabled);
}
static void deinterlace_close(xine_video_port_t *port_gen, xine_stream_t *stream)
{
post_video_port_t *port = (post_video_port_t *)port_gen;
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)port->post;
port->stream = NULL;
_flush_frames(this);
port->original_port->set_property(port->original_port,
XINE_PARAM_VO_DEINTERLACE,
0);
port->original_port->close(port->original_port, stream);
_x_post_dec_usage(port);
}
static int deinterlace_intercept_frame(post_video_port_t *port, vo_frame_t *frame)
{
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)port->post;
int vo_deinterlace_enabled = 0;
vo_deinterlace_enabled = ( frame->format != XINE_IMGFMT_YV12 &&
frame->format != XINE_IMGFMT_YUY2 &&
this->enabled );
if( this->cur_method &&
this->vo_deinterlace_enabled != vo_deinterlace_enabled ) {
this->vo_deinterlace_enabled = vo_deinterlace_enabled;
port->original_port->set_property(port->original_port,
XINE_PARAM_VO_DEINTERLACE,
this->vo_deinterlace_enabled);
}
return (this->enabled && this->cur_method &&
(frame->flags & VO_INTERLACED_FLAG) &&
(frame->format == XINE_IMGFMT_YV12 || frame->format == XINE_IMGFMT_YUY2) );
}
static void apply_chroma_filter( uint8_t *data, int stride, int width, int height )
{
int i;
/* ok, using linearblend inplace is a bit weird: the result of a scanline
* interpolation will affect the next scanline. this might not be a problem
* at all, we just want a kind of filter here.
*/
for( i = 0; i < height; i++, data += stride ) {
vfilter_chroma_332_packed422_scanline( data, width,
data,
(i) ? (data - stride) : data,
(i < height-1) ? (data + stride) : data );
}
}
/* Build the output frame from the specified field. */
static int deinterlace_build_output_field(
post_plugin_deinterlace_t *this, post_video_port_t *port,
xine_stream_t *stream,
vo_frame_t *frame, vo_frame_t *yuy2_frame,
int bottom_field, int second_field,
int64_t pts, int64_t duration, int skip)
{
vo_frame_t *deinterlaced_frame;
int scaler = 1;
int force24fps;
force24fps = this->judder_correction && !this->cheap_mode &&
( this->pulldown == PULLDOWN_VEKTOR && this->tvtime->filmmode );
if( this->tvtime->curmethod->doscalerbob ) {
scaler = 2;
}
pthread_mutex_unlock (&this->lock);
deinterlaced_frame = port->original_port->get_frame(port->original_port,
frame->width, frame->height / scaler, frame->ratio, yuy2_frame->format,
frame->flags | VO_BOTH_FIELDS);
pthread_mutex_lock (&this->lock);
deinterlaced_frame->crop_left = frame->crop_left;
deinterlaced_frame->crop_right = frame->crop_right;
deinterlaced_frame->crop_top = frame->crop_top;
deinterlaced_frame->crop_bottom = frame->crop_bottom;
_x_extra_info_merge(deinterlaced_frame->extra_info, frame->extra_info);
if( skip > 0 && !this->pulldown ) {
deinterlaced_frame->bad_frame = 1;
} else {
if( this->tvtime->curmethod->doscalerbob ) {
if( yuy2_frame->format == XINE_IMGFMT_YUY2 ) {
deinterlaced_frame->bad_frame = !tvtime_build_copied_field(this->tvtime,
deinterlaced_frame->base[0],
yuy2_frame->base[0], bottom_field,
frame->width, frame->height,
yuy2_frame->pitches[0], deinterlaced_frame->pitches[0] );
} else {
deinterlaced_frame->bad_frame = !tvtime_build_copied_field(this->tvtime,
deinterlaced_frame->base[0],
yuy2_frame->base[0], bottom_field,
frame->width/2, frame->height,
yuy2_frame->pitches[0], deinterlaced_frame->pitches[0] );
deinterlaced_frame->bad_frame += !tvtime_build_copied_field(this->tvtime,
deinterlaced_frame->base[1],
yuy2_frame->base[1], bottom_field,
frame->width/4, frame->height/2,
yuy2_frame->pitches[1], deinterlaced_frame->pitches[1] );
deinterlaced_frame->bad_frame += !tvtime_build_copied_field(this->tvtime,
deinterlaced_frame->base[2],
yuy2_frame->base[2], bottom_field,
frame->width/4, frame->height/2,
yuy2_frame->pitches[2], deinterlaced_frame->pitches[2] );
}
} else {
if( yuy2_frame->format == XINE_IMGFMT_YUY2 ) {
deinterlaced_frame->bad_frame = !tvtime_build_deinterlaced_frame(this->tvtime,
deinterlaced_frame->base[0],
yuy2_frame->base[0],
(this->recent_frame[0])?this->recent_frame[0]->base[0]:yuy2_frame->base[0],
(this->recent_frame[1])?this->recent_frame[1]->base[0]:yuy2_frame->base[0],
bottom_field, second_field, frame->width, frame->height,
yuy2_frame->pitches[0], deinterlaced_frame->pitches[0]);
} else {
deinterlaced_frame->bad_frame = !tvtime_build_deinterlaced_frame(this->tvtime,
deinterlaced_frame->base[0],
yuy2_frame->base[0],
(this->recent_frame[0])?this->recent_frame[0]->base[0]:yuy2_frame->base[0],
(this->recent_frame[1])?this->recent_frame[1]->base[0]:yuy2_frame->base[0],
bottom_field, second_field, frame->width/2, frame->height,
yuy2_frame->pitches[0], deinterlaced_frame->pitches[0]);
deinterlaced_frame->bad_frame += !tvtime_build_deinterlaced_frame(this->tvtime,
deinterlaced_frame->base[1],
yuy2_frame->base[1],
(this->recent_frame[0])?this->recent_frame[0]->base[1]:yuy2_frame->base[1],
(this->recent_frame[1])?this->recent_frame[1]->base[1]:yuy2_frame->base[1],
bottom_field, second_field, frame->width/4, frame->height/2,
yuy2_frame->pitches[1], deinterlaced_frame->pitches[1]);
deinterlaced_frame->bad_frame += !tvtime_build_deinterlaced_frame(this->tvtime,
deinterlaced_frame->base[2],
yuy2_frame->base[2],
(this->recent_frame[0])?this->recent_frame[0]->base[2]:yuy2_frame->base[2],
(this->recent_frame[1])?this->recent_frame[1]->base[2]:yuy2_frame->base[2],
bottom_field, second_field, frame->width/4, frame->height/2,
yuy2_frame->pitches[2], deinterlaced_frame->pitches[2]);
}
}
}
pthread_mutex_unlock (&this->lock);
if( force24fps ) {
if( !deinterlaced_frame->bad_frame ) {
this->framecounter++;
if( pts && this->framecounter > FRAMES_TO_SYNC ) {
deinterlaced_frame->pts = pts;
this->framecounter = 0;
} else
deinterlaced_frame->pts = 0;
deinterlaced_frame->duration = FPS_24_DURATION;
if( this->chroma_filter && !this->cheap_mode )
apply_chroma_filter( deinterlaced_frame->base[0], deinterlaced_frame->pitches[0],
frame->width, frame->height / scaler );
skip = deinterlaced_frame->draw(deinterlaced_frame, stream);
} else {
skip = 0;
}
} else {
deinterlaced_frame->pts = pts;
deinterlaced_frame->duration = duration;
if( this->chroma_filter && !this->cheap_mode && !deinterlaced_frame->bad_frame )
apply_chroma_filter( deinterlaced_frame->base[0], deinterlaced_frame->pitches[0],
frame->width, frame->height / scaler );
skip = deinterlaced_frame->draw(deinterlaced_frame, stream);
}
/* _x_post_frame_copy_up(frame, deinterlaced_frame); */
deinterlaced_frame->free(deinterlaced_frame);
pthread_mutex_lock (&this->lock);
return skip;
}
static int deinterlace_draw(vo_frame_t *frame, xine_stream_t *stream)
{
post_video_port_t *port = (post_video_port_t *)frame->port;
post_plugin_deinterlace_t *this = (post_plugin_deinterlace_t *)port->post;
vo_frame_t *orig_frame;
vo_frame_t *yuy2_frame;
int i, skip = 0, progressive = 0;
int fields[2];
int framerate_mode;
orig_frame = frame;
_x_post_frame_copy_down(frame, frame->next);
frame = frame->next;
/* update tvtime context and method */
pthread_mutex_lock (&this->lock);
if( this->tvtime_changed ) {
tvtime_reset_context(this->tvtime);
if( this->cur_method )
this->tvtime->curmethod = get_deinterlace_method( this->cur_method-1 );
else
this->tvtime->curmethod = NULL;
port->original_port->set_property(port->original_port,
XINE_PARAM_VO_DEINTERLACE,
!this->cur_method);
this->tvtime_changed = 0;
}
if( this->tvtime_last_filmmode != this->tvtime->filmmode ) {
xine_event_t event;
event.type = XINE_EVENT_POST_TVTIME_FILMMODE_CHANGE;
event.stream = stream;
event.data = (void *)&this->tvtime->filmmode;
event.data_length = sizeof(this->tvtime->filmmode);
xine_event_send(stream, &event);
this->tvtime_last_filmmode = this->tvtime->filmmode;
}
pthread_mutex_unlock (&this->lock);
lprintf("frame flags pf: %d rff: %d tff: %d duration: %d\n",
frame->progressive_frame, frame->repeat_first_field,
frame->top_field_first, frame->duration);
/* detect special rff patterns */
this->rff_pattern = this->rff_pattern << 1;
this->rff_pattern |= !!frame->repeat_first_field;
if( ((this->rff_pattern & 0xff) == 0xaa ||
(this->rff_pattern & 0xff) == 0x55) ) {
/*
* special case for ntsc 3:2 pulldown (called flags or soft pulldown).
* we know all frames are indeed progressive.
*/
progressive = 1;
}
/* using frame->progressive_frame may help displaying still menus.
* however, it is known that some rare material set it wrong.
*
* we also assume that repeat_first_field is progressive (it doesn't
* make much sense to display interlaced fields out of order)
*/
if( this->use_progressive_frame_flag &&
(frame->repeat_first_field || frame->progressive_frame) ) {
progressive = 1;
}
if( !frame->bad_frame &&
(frame->flags & VO_INTERLACED_FLAG) &&
this->tvtime->curmethod ) {
frame->flags &= ~VO_INTERLACED_FLAG;
/* convert to YUY2 if needed */
if( frame->format == XINE_IMGFMT_YV12 && !this->cheap_mode ) {
yuy2_frame = port->original_port->get_frame(port->original_port,
frame->width, frame->height, frame->ratio, XINE_IMGFMT_YUY2, frame->flags | VO_BOTH_FIELDS);
_x_post_frame_copy_down(frame, yuy2_frame);
/* the logic for deciding upsampling to use comes from:
* http://www.hometheaterhifi.com/volume_8_2/dvd-benchmark-special-report-chroma-bug-4-2001.html
*/
yv12_to_yuy2(frame->base[0], frame->pitches[0],
frame->base[1], frame->pitches[1],
frame->base[2], frame->pitches[2],
yuy2_frame->base[0], yuy2_frame->pitches[0],
frame->width, frame->height,
frame->progressive_frame || progressive );
} else {
yuy2_frame = frame;
yuy2_frame->lock(yuy2_frame);
}
pthread_mutex_lock (&this->lock);
/* check if frame format changed */
for(i = 0; i < NUM_RECENT_FRAMES; i++ ) {
if( this->recent_frame[i] &&
(this->recent_frame[i]->width != frame->width ||
this->recent_frame[i]->height != frame->height ||
this->recent_frame[i]->format != yuy2_frame->format ) ) {
this->recent_frame[i]->free(this->recent_frame[i]);
this->recent_frame[i] = NULL;
}
}
if( !this->cheap_mode ) {
framerate_mode = this->framerate_mode;
this->tvtime->pulldown_alg = this->pulldown;
} else {
framerate_mode = FRAMERATE_HALF_TFF;
this->tvtime->pulldown_alg = PULLDOWN_NONE;
}
if( framerate_mode == FRAMERATE_FULL ) {
int top_field_first = frame->top_field_first;
/* if i understood mpeg2 specs correctly, top_field_first
* shall be zero for field pictures and the output order
* is the same that the fields are decoded.
* frame->flags allow us to find the first decoded field.
*
* note: frame->field() is called later to switch decoded
* field but frame->flags do not change.
*/
if ( (frame->flags & VO_BOTH_FIELDS) != VO_BOTH_FIELDS ) {
top_field_first = (frame->flags & VO_TOP_FIELD) ? 1 : 0;
}
if ( top_field_first ) {
fields[0] = 0;
fields[1] = 1;
} else {
fields[0] = 1;
fields[1] = 0;
}
} else if ( framerate_mode == FRAMERATE_HALF_TFF ) {
fields[0] = 0;
} else if ( framerate_mode == FRAMERATE_HALF_BFF ) {
fields[0] = 1;
}
if( progressive ) {
/* If the previous field was interlaced and this one is progressive
* we need to run a deinterlace on the first field of this frame
* in order to let output for the previous frames last field be
* generated. This is only necessary for the deinterlacers that
* delay output by one field. This is signaled by the delaysfield
* flag in the deinterlace method structure. The previous frames
* duration is used in the calculation because the generated frame
* represents the second half of the previous frame.
*/
if (this->recent_frame[0] && !this->recent_frame[0]->progressive_frame &&
this->tvtime->curmethod->delaysfield)
{
skip = deinterlace_build_output_field(
this, port, stream,
frame, yuy2_frame,
fields[0], 0,
0,
(framerate_mode == FRAMERATE_FULL) ? this->recent_frame[0]->duration/2 : this->recent_frame[0]->duration,
0);
}
pthread_mutex_unlock (&this->lock);
skip = yuy2_frame->draw(yuy2_frame, stream);
pthread_mutex_lock (&this->lock);
_x_post_frame_copy_up(frame, yuy2_frame);
} else {
/* If the previous field was progressive and we are using a
* filter that delays it's output by one field then we need
* to skip the first field's output. Otherwise the effective
* display duration of the previous frame will be extended
* by 1/2 of this frames duration when output is generated
* using the last field of the progressive frame. */
/* Build the output from the first field. */
if ( !(this->recent_frame[0] && this->recent_frame[0]->progressive_frame &&
this->tvtime->curmethod->delaysfield) ) {
skip = deinterlace_build_output_field(
this, port, stream,
frame, yuy2_frame,
fields[0], 0,
frame->pts,
(framerate_mode == FRAMERATE_FULL) ? frame->duration/2 : frame->duration,
0);
}
if( framerate_mode == FRAMERATE_FULL ) {
/* Build the output from the second field. */
skip = deinterlace_build_output_field(
this, port, stream,
frame, yuy2_frame,
fields[1], 1,
0,
frame->duration/2,
skip);
}
}
/* don't drop frames when pulldown mode is enabled. otherwise
* pulldown detection fails (yo-yo effect has also been seen)
*/
if( this->pulldown )
skip = 0;
/* store back progressive flag for frame history */
yuy2_frame->progressive_frame = progressive;
/* keep track of recent frames */
i = NUM_RECENT_FRAMES-1;
if( this->recent_frame[i] )
this->recent_frame[i]->free(this->recent_frame[i]);
for( ; i ; i-- )
this->recent_frame[i] = this->recent_frame[i-1];
if (port->stream)
this->recent_frame[0] = yuy2_frame;
else {
/* do not keep this frame when no stream is connected to us,
* otherwise, this frame might never get freed */
yuy2_frame->free(yuy2_frame);
this->recent_frame[0] = NULL;
}
pthread_mutex_unlock (&this->lock);
} else {
skip = frame->draw(frame, stream);
}
_x_post_frame_copy_up(orig_frame, frame);
return skip;
}