blib/lib/Video/DVDRip/FilterSettings.pm untested

filter: Statementstmt 0.0% 0 / 236 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 65 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 0.0% 0 / 40 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 488 CC 39 SCAR 38.6
CC
39
Cov
0%
move_instance
210.0
check_filter_color_mode_ok
56.0
get_config_string
20.0
CRAP 47.5
1 # $Id: FilterSettings.pm 2187 2006-08-16 19:34:38Z joern $
2  
3 #-----------------------------------------------------------------------
4 # Copyright (C) 2001-2006 Jörn Reder <joern AT zyn.de>.
5 # All Rights Reserved. See file COPYRIGHT for details.
6 #
7 # This module is part of Video::DVDRip, which is free software; you can
8 # redistribute it and/or modify it under the same terms as Perl itself.
9 #-----------------------------------------------------------------------
10  
11 package Video::DVDRip::FilterSettings;
12 use Locale::TextDomain qw (video.dvdrip);
13  
14 use base Video::DVDRip::Base;
15 use Video::DVDRip::FilterList;
16  
17 use Carp;
18 use strict;
19  
20 sub filters { shift->{filters} }
21  
22 sub new {
23 my $class = shift;
24  
25 my $self = {
26 # list of FilterSettingsInstance objects
27 filters => [],
28 };
29  
30 return bless $self, $class;
31 }
32  
33 sub get_selected_filters {
34 my $self = shift;
35  
36 my @selected;
37 my $filters = Video::DVDRip::FilterList->get_filter_list->filters;
38  
39 foreach my $filter_instance ( @{ $self->filters } ) {
40 push @selected, $filter_instance
41 if exists $filters->{ $filter_instance->filter_name };
42 }
43  
44 return \@selected;
45 }
46  
47 sub get_filter_instance {
48 my $self = shift;
49 my %par = @_;
50 my ($id) = @par{'id'};
51  
52 foreach my $filter_instance ( @{ $self->filters } ) {
53 return $filter_instance if $filter_instance->id == $id;
54 }
55  
56 croak "can't find filter instance with id $id";
57 }
58  
59 sub get_filter_instance_index {
60 my $self = shift;
61 my %par = @_;
62 my ($id) = @par{'id'};
63  
64 my $i = 0;
65 my $filters = $self->filters;
66  
67 ++$i while $i < @{$filters} and $filters->[$i]->id != $id;
68  
69 croak "can't find filter filter instance with id $id"
70 if $i == @{$filters};
71  
72 return $i;
73 }
74  
75 sub get_next_filter_instance_id {
76 my $self = shift;
77  
78 my $max_id;
79 foreach my $filter_instance ( @{ $self->filters } ) {
80 $max_id = $filter_instance->id
81 if $filter_instance->id > $max_id;
82 }
83  
84 return $max_id + 1;
85 }
86  
87 sub allocate_suffixes {
88 my $self = shift;
89  
90 my %suffix_cnt = ();
91  
92 foreach my $filter ( @{ $self->filters } ) {
93 my $suffix = $suffix_cnt{ $filter->filter_name }++ || 0;
94 $filter->set_suffix($suffix);
95 }
96  
97 1;
98 }
99  
100 sub set_value {
101 my $self = shift;
102 my %par = @_;
103 my ( $id, $option, $idx, $value )
104 = @par{ 'id', 'option', 'idx', 'value' };
105  
106 $self->get_filter_instance( id => $id )->set_value(
107 option => $option,
108 idx => $idx,
109 value => $value,
110 );
111  
112 1;
113 }
114  
115 sub get_value {
116 my $self = shift;
117 my %par = @_;
118 my ( $id, $option, $idx ) = @par{ 'id', 'option', 'idx' };
119  
120 return $self->get_filter_instance( id => $id )->get_value(
121 option => $option,
122 idx => $idx,
123 );
124 }
125  
126 sub check_filter_color_mode_ok {
127 my $self = shift;
128 my %par = @_;
129 my ($filter_name) = @par{'filter_name'};
130  
131 my $filter
132 = Video::DVDRip::FilterList->get_filter( filter_name => $filter_name,
133 );
134  
135 return 1 if $filter->can_rgb and $filter->can_yuv;
136 return 1 if !$filter->can_video;
137  
138 foreach my $instance ( @{ $self->filters } ) {
139 next if !$instance->get_filter->can_video;
140 if ( $filter->can_rgb ) {
141 if ( not $instance->get_filter->can_rgb ) {
142 croak "msg:Filter is RGB only, but you "
143 . "selected non RGB filters already.";
144 }
145 }
146 else {
147 if ( not $instance->get_filter->can_yuv ) {
148 croak "msg:Filter is YUV only, but you "
149 . "selected non YUV filters already.";
150 }
151 }
152 }
153  
154 1;
155 }
156  
157 sub add_filter {
158 my $self = shift;
159 my %par = @_;
160 my ($filter_name) = @par{'filter_name'};
161  
162 $self->check_filter_color_mode_ok( filter_name => $filter_name );
163  
164 my $filter_instance = Video::DVDRip::FilterSettingsInstance->new(
165 id => $self->get_next_filter_instance_id,
166 filter_name => $filter_name,
167 );
168  
169 push @{ $self->filters }, $filter_instance;
170  
171 return $filter_instance;
172 }
173  
174 sub del_filter {
175 my $self = shift;
176 my %par = @_;
177 my ($id) = @par{'id'};
178  
179 my $index = $self->get_filter_instance_index( id => $id );
180 my ($instance) = splice @{ $self->filters }, $index, 1;
181  
182 return $instance;
183 }
184  
185 sub filter_used {
186 my $self = shift;
187 my %par = @_;
188 my ($filter_name) = @par{'filter_name'};
189  
190 foreach my $filter_instance ( @{ $self->filters } ) {
191 return 1 if $filter_instance->filter_name eq $filter_name;
192 }
193  
194 return 0;
195 }
196  
197 sub move_instance {
198 my $self = shift;
199 my %par = @_;
200 my ( $id, $before_id ) = @par{ 'id', 'before_id' };
201  
202 return if $id == 0; # no move of 'pre', 'post' markers
203 return if $before_id eq "pre"; # 'pre' marker is topmost
204  
205 my $from_instance = $self->get_filter_instance( id => $id );
206 my $from_filter = $from_instance->get_filter;
207  
208 my $append_to_pre;
209 if ( $before_id eq 'post' ) {
210 $append_to_pre = 1;
211 $before_id = $self->get_first_post_filter_instance->id;
212 }
213  
214 my ( $before_instance, $before_filter );
215 $before_instance = $self->get_filter_instance( id => $before_id )
216 if $before_id;
217 $before_filter = $before_instance->get_filter
218 if $before_instance;
219  
220 # no post in pre queue
221 return if not $from_filter->is_pre and $append_to_pre;
222  
223 # no post before pre
224 return
225 if not $from_filter->is_pre
226 and $before_instance
227 and $before_instance->queue eq 'pre';
228  
229 # no pre at the end
230 return if not $from_filter->is_post and not $before_instance;
231  
232 # no pre before post, if we don't append it to the pre list
233 return
234 if not $append_to_pre
235 and not $from_filter->is_post
236 and $before_instance
237 and $before_instance->queue eq 'post';
238  
239 # queue move?
240 if ($from_instance->queue eq 'pre'
241 and ( not $before_instance
242 or $before_instance && $before_instance->queue eq 'post' )
243 ) {
244 $from_instance->set_queue("post");
245 $from_instance->set_value(
246 option_name => "pre",
247 value => 0,
248 idx => 0
249 );
250 }
251 elsif (
252 $from_instance->queue eq 'post'
253 and ( $append_to_pre
254 or $before_instance && $before_instance->queue eq 'pre' )
255 ) {
256 $from_instance->set_queue("pre");
257 $from_instance->set_value(
258 option_name => "pre",
259 value => 1,
260 idx => 0
261 );
262 }
263  
264 my $from_idx = $self->get_filter_instance_index( id => $id );
265 my $to_idx = $before_id
266 ? $self->get_filter_instance_index( id => $before_id )
267 : @{ $self->filters };
268  
269 if ( $from_idx < $to_idx ) {
270 splice @{ $self->filters }, $to_idx, 0, $self->filters->[$from_idx];
271 splice @{ $self->filters }, $from_idx, 1;
272 }
273 else {
274 splice @{ $self->filters }, $to_idx, 0, $self->filters->[$from_idx];
275 splice @{ $self->filters }, $from_idx + 1, 1;
276 }
277  
278 1;
279 }
280  
281 sub get_first_post_filter_instance {
282 my $self = shift;
283  
284 foreach my $instance ( @{ $self->filters } ) {
285 return $instance if $instance->post;
286 }
287  
288 return;
289 }
290  
291 sub get_prepend_instance {
292 my $self = shift;
293 my %par = @_;
294 my ($id) = @par{'id'};
295  
296 my $prepend_instance;
297 foreach my $instance ( @{ $self->filters } ) {
298 return $prepend_instance if $instance->id == $id;
299 $prepend_instance = $instance;
300 }
301  
302 return;
303 }
304  
305 sub get_filter_config_strings {
306 my $self = shift;
307 my %par = @_;
308 my ($with_suffixes) = @par{'with_suffixes'};
309  
310 $self->allocate_suffixes if $with_suffixes;
311  
312 my @config_strings;
313 foreach my $instance ( @{ $self->filters } ) {
314 my $filter_name = $instance->filter_name;
315 $filter_name .= "#" . $instance->suffix
316 if $with_suffixes
317 and $instance->suffix;
318  
319 push @config_strings,
320 {
321 filter => $filter_name,
322 options => $instance->get_config_string,
323 enabled => $instance->enabled,
324 };
325 }
326  
327 return \@config_strings;
328 }
329  
330 sub get_max_frames_needed {
331 my $self = shift;
332  
333 my $max_frames_needed = 0;
334  
335 foreach my $instance ( @{ $self->filters } ) {
336 $max_frames_needed = $instance->get_filter->frames_needed
337 if $instance->get_filter->frames_needed > $max_frames_needed;
338 }
339  
340 return $max_frames_needed;
341 }
342  
343 package Video::DVDRip::FilterSettingsInstance;
344 use Locale::TextDomain qw (video.dvdrip);
345  
346 use Carp;
347  
348 sub id { shift->{id} }
349 sub filter_name { shift->{filter_name} }
350 sub options { shift->{options} }
351  
352 sub suffix { shift->{suffix} }
353 sub queue { shift->{queue} }
354 sub enabled { shift->{enabled} }
355  
356 sub set_suffix { shift->{suffix} = $_[1] }
357 sub set_queue { shift->{queue} = $_[1] }
358 sub set_enabled { shift->{enabled} = $_[1] }
359  
360 sub post { not shift->{pre} }
361  
362 sub new {
363 my $class = shift;
364 my %par = @_;
365 my ( $id, $filter_name, $suffix ) = @par{ 'id', 'filter_name', 'suffix' };
366  
367 my $self = bless {
368 id => $id,
369 filter_name => $filter_name,
370 suffix => $suffix,
371 enabled => 1,
372 options => {},
373 queue => 'pre',
374 }, $class;
375  
376 $self->set_queue('post')
377 if $self->get_filter->is_post
378 and not $self->get_filter->is_pre_post;
379  
380 $self->set_queue('post')
381 if $self->get_filter->is_pre_post
382 and
383 not eval { $self->get_filter->get_option( option_name => 'pre' )->fields->[0]
384 ->default };
385  
386 return bless $self, $class;
387 }
388  
389 sub get_filter {
390 my $self = shift;
391  
392 return Video::DVDRip::FilterList->get_filter(
393 filter_name => $self->filter_name );
394 }
395  
396 sub set_value {
397 my $self = shift;
398 my %par = @_;
399 my ( $option_name, $idx, $value ) = @par{ 'option_name', 'idx', 'value' };
400  
401 my $filter_name = $self->filter_name;
402 my $fields = $self->get_filter->get_option( option_name => $option_name )
403 ->fields;
404  
405 if ( $idx < 0 or $idx >= @{$fields} ) {
406 croak "msg:Illegal field index $idx (filter $filter_name, "
407 . "option $option_name)";
408 }
409  
410 my $field = $fields->[$idx];
411  
412 my $range_from = $field->range_from;
413 my $range_to = $field->range_to;
414  
415 if ( $range_from <= $value
416 and $range_to >= $value ) {
417 $self->options->{$option_name}->[$idx] = $value;
418 }
419 else {
420 return;
421 }
422  
423 1;
424 }
425  
426 sub get_value {
427 my $self = shift;
428 my %par = @_;
429 my ( $option_name, $idx ) = @par{ 'option_name', 'idx' };
430  
431 my $filter_name = $self->filter_name;
432 my $fields = $self->get_filter->get_option( option_name => $option_name )
433 ->fields;
434  
435 if ( $idx < 0 or $idx >= @{$fields} ) {
436 croak "msg:Illegal field index $idx (filter $filter_name, "
437 . "option $option_name)";
438 }
439  
440 return $fields->[$idx]->default
441 if not defined $self->options->{$option_name}->[$idx];
442  
443 return $self->options->{$option_name}->[$idx];
444 }
445  
446 sub get_config_string {
447 my $self = shift;
448  
449 my $filter = $self->get_filter;
450  
451 my $config;
452 foreach my $option ( @{ $filter->options } ) {
453 if ( $option->switch ) {
454 if ($self->get_value(
455 option_name => $option->option_name,
456 idx => 0
457 )
458 ) {
459 $config .= ":" . $option->option_name;
460 }
461 next;
462 }
463 my @values;
464 for ( my $idx = 0; $idx < @{ $option->fields }; ++$idx ) {
465 push @values,
466 $self->get_value(
467 option_name => $option->option_name,
468 idx => $idx,
469 );
470 }
471 next if join( '', @values ) eq '';
472 $config .= ":" . $option->option_name . "=";
473 $config .= sprintf( $option->format, @values );
474 }
475  
476 $config =~ s/^://;
477  
478 return $config;
479 }
480  
481 1;