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
|
/* avfilter.c
Copyright (c) 2003-2015 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#include "hb.h"
#include "hbffmpeg.h"
#include "common.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersrc.h"
#include "libavfilter/buffersink.h"
#include "decomb.h"
/*****
* This is a meta-filter. By itself it makes no modifications to the video.
* Other filters use this filter to perform their function (see pad.c)
*/
struct hb_filter_private_s
{
hb_buffer_list_t list;
AVFilterGraph * graph;
AVFilterContext * last;
AVFilterContext * input;
AVFilterContext * output;
AVFrame * frame;
AVRational out_time_base;
char * settings;
};
static int avfilter_init( hb_filter_object_t * filter,
hb_filter_init_t * init );
static void avfilter_close( hb_filter_object_t * filter );
static int avfilter_work( hb_filter_object_t * filter,
hb_buffer_t ** buf_in, hb_buffer_t ** buf_out );
static hb_filter_info_t * avfilter_info( hb_filter_object_t * filter );
static int null_init( hb_filter_object_t * filter, hb_filter_init_t * init );
static void null_close( hb_filter_object_t * filter );
static int null_work( hb_filter_object_t * filter,
hb_buffer_t ** buf_in, hb_buffer_t ** buf_out );
static hb_filter_info_t * null_info( hb_filter_object_t * filter );
hb_filter_object_t hb_filter_avfilter =
{
.id = HB_FILTER_AVFILTER,
.enforce_order = 0,
.name = "avfilter",
.settings = NULL,
.init = avfilter_init,
.work = avfilter_work,
.close = avfilter_close,
.info = avfilter_info,
};
const char pad_template[] =
"width=^"HB_INT_REG"$:height=^"HB_INT_REG"$:color=^"HB_ALL_REG"$:"
"x=^"HB_INT_REG"$:y=^"HB_INT_REG"$";
hb_filter_object_t hb_filter_pad =
{
.id = HB_FILTER_PAD,
.enforce_order = 1,
.name = "pad",
.settings = NULL,
.init = null_init,
.work = null_work,
.close = null_close,
.info = null_info,
.settings_template = pad_template,
};
const char rotate_template[] =
"angle=^(0|90|180|270)$:hflip=^"HB_BOOL_REG"$:disable=^"HB_BOOL_REG"$";
hb_filter_object_t hb_filter_rotate =
{
.id = HB_FILTER_ROTATE,
.enforce_order = 1,
.name = "rotate",
.settings = NULL,
.init = null_init,
.work = null_work,
.close = null_close,
.info = null_info,
.settings_template = rotate_template,
};
const char deint_template[] =
"mode=^"HB_INT_REG"$:parity=^([01])$";
hb_filter_object_t hb_filter_deinterlace =
{
.id = HB_FILTER_DEINTERLACE,
.enforce_order = 1,
.name = "deinterlace",
.settings = NULL,
.init = null_init,
.work = null_work,
.close = null_close,
.info = null_info,
.settings_template = deint_template,
};
static int null_init( hb_filter_object_t * filter, hb_filter_init_t * init )
{
hb_log("null_init: It is an error to call this function.");
return 1;
}
static void null_close( hb_filter_object_t * filter )
{
hb_log("null_close: It is an error to call this function.");
return;
}
static int null_work( hb_filter_object_t * filter,
hb_buffer_t ** buf_in, hb_buffer_t ** buf_out )
{
hb_log("null_work: It is an error to call this function.");
return HB_WORK_DONE;
}
static hb_filter_info_t * null_info( hb_filter_object_t * filter )
{
hb_log("null_info: It is an error to call this function.");
return NULL;
}
static AVFilterContext * append_filter( hb_filter_private_t * pv,
const char * name, const char * args)
{
AVFilterContext * filter;
int result;
result = avfilter_graph_create_filter(&filter, avfilter_get_by_name(name),
name, args, NULL, pv->graph);
if (result < 0)
{
return NULL;
}
if (pv->last != NULL)
{
result = avfilter_link(pv->last, 0, filter, 0);
if (result < 0)
{
avfilter_free(filter);
return NULL;
}
}
pv->last = filter;
return filter;
}
static int avfilter_init( hb_filter_object_t * filter, hb_filter_init_t * init )
{
hb_filter_private_t * pv = filter->private_data;
char * sws_flags;
AVFilterContext * avfilter;
char * filter_args;
int result;
AVFilterInOut * in = NULL, * out = NULL;
char * avfilter_settings = NULL;
avfilter_settings = hb_filter_settings_string(HB_FILTER_AVFILTER,
filter->settings);
if (avfilter_settings == NULL)
{
hb_error("avfilter_init: no filter settings specified");
return 1;
}
pv = calloc(1, sizeof(struct hb_filter_private_s));
filter->private_data = pv;
pv->settings = avfilter_settings;
pv->graph = avfilter_graph_alloc();
if (pv->graph == NULL)
{
hb_error("avfilter_init: avfilter_graph_alloc failed");
goto fail;
}
sws_flags = hb_strdup_printf("flags=%d", SWS_LANCZOS|SWS_ACCURATE_RND);
// avfilter_graph_free uses av_free to release scale_sws_opts. Due
// to the hacky implementation of av_free/av_malloc on windows,
// you must av_malloc anything that is av_free'd.
pv->graph->scale_sws_opts = av_malloc(strlen(sws_flags) + 1);
strcpy(pv->graph->scale_sws_opts, sws_flags);
free(sws_flags);
result = avfilter_graph_parse2(pv->graph, avfilter_settings, &in, &out);
if (result < 0 || in == NULL || out == NULL)
{
hb_error("avfilter_init: avfilter_graph_parse2 failed (%s)",
avfilter_settings);
goto fail;
}
// Build filter input
filter_args = hb_strdup_printf(
"width=%d:height=%d:pix_fmt=%d:sar=%d/%d:"
"time_base=%d/%d:frame_rate=%d/%d",
init->geometry.width, init->geometry.height, init->pix_fmt,
init->geometry.par.num, init->geometry.par.den,
1, 90000, init->vrate.num, init->vrate.den);
avfilter = append_filter(pv, "buffer", filter_args);
free(filter_args);
if (avfilter == NULL)
{
hb_error("avfilter_init: failed to create buffer source filter");
goto fail;
}
pv->input = avfilter;
avfilter = append_filter(pv, "format", "yuv420p");
if (avfilter == NULL)
{
hb_error("avfilter_init: failed to create pix format filter");
goto fail;
}
// Link input to filter chain created by avfilter_graph_parse2
result = avfilter_link(pv->last, 0, in->filter_ctx, 0);
if (result < 0)
{
goto fail;
}
pv->last = out->filter_ctx;
// Build filter output
avfilter = append_filter(pv, "buffersink", NULL);
if (avfilter == NULL)
{
hb_error("avfilter_init: failed to create buffer output filter");
goto fail;
}
pv->output = avfilter;
result = avfilter_graph_config(pv->graph, NULL);
if (result < 0)
{
hb_error("avfilter_init: failed to configure filter graph");
goto fail;
}
pv->frame = av_frame_alloc();
if (pv->frame == NULL)
{
hb_error("avfilter_init: failed to allocate frame filter");
goto fail;
}
avfilter_inout_free(&in);
avfilter_inout_free(&out);
// Retrieve the parameters of the output filter
AVFilterLink *link = pv->output->inputs[0];
init->geometry.width = link->w;
init->geometry.height = link->h;
init->geometry.par.num = link->sample_aspect_ratio.num;
init->geometry.par.den = link->sample_aspect_ratio.den;
init->pix_fmt = link->format;
// avfilter can generate "unknown" framerates. If this happens
// just pass along the source framerate.
if (link->frame_rate.num > 0 && link->frame_rate.den > 0)
{
init->vrate.num = link->frame_rate.num;
init->vrate.den = link->frame_rate.den;
}
pv->out_time_base = link->time_base;
hb_buffer_list_clear(&pv->list);
return 0;
fail:
if (pv->input != NULL)
avfilter_free(pv->input);
if (pv->output != NULL)
avfilter_free(pv->output);
avfilter_inout_free(&in);
avfilter_inout_free(&out);
avfilter_graph_free(&pv->graph);
free(pv->settings);
free(pv);
return 1;
}
static hb_filter_info_t * avfilter_info( hb_filter_object_t * filter )
{
hb_filter_private_t * pv = filter->private_data;
hb_filter_info_t * info;
if( !pv )
return NULL;
info = calloc(1, sizeof(hb_filter_info_t));
info->human_readable_desc = malloc(1024);
info->human_readable_desc[0] = 0;
char * dst = info->human_readable_desc;
char * start = pv->settings;
while (start != NULL && *start != 0)
{
// Find end of a filter
char * comma = strchr(start, ',');
char * quote = strchr(start, '\'');
if (comma != NULL && quote != NULL && quote < comma)
{
// Find end of quote
quote = strchr(quote+1, '\'');
comma = strchr(start, ',');
}
// pretty print line
int name = 1;
while (*start != 0 && (comma == NULL || start < comma))
{
switch (*start)
{
case '=':
if (name)
{
*dst++ = ':';
*dst++ = ' ';
name = 0;
}
else
{
*dst++ = '=';
}
break;
case ':':
*dst++ = ',';
*dst++ = ' ';
break;
case '\'':
case ' ':
break;
default:
*dst++ = *start;
}
start++;
}
if (*start != 0)
{
*dst++ = '\n';
start++;
}
}
*dst = 0;
return info;
}
static void avfilter_close( hb_filter_object_t * filter )
{
hb_filter_private_t * pv = filter->private_data;
if (pv == NULL)
{
// Already closed
return;
}
hb_buffer_list_close(&pv->list);
av_frame_free(&pv->frame);
avfilter_graph_free(&pv->graph);
free(pv->settings);
free(pv);
filter->private_data = NULL;
}
static void fill_frame(hb_filter_private_t * pv,
AVFrame * frame, hb_buffer_t * buf)
{
frame->data[0] = buf->plane[0].data;
frame->data[1] = buf->plane[1].data;
frame->data[2] = buf->plane[2].data;
frame->linesize[0] = buf->plane[0].stride;
frame->linesize[1] = buf->plane[1].stride;
frame->linesize[2] = buf->plane[2].stride;
frame->pts = buf->s.start;
frame->reordered_opaque = buf->s.duration;
frame->width = buf->f.width;
frame->height = buf->f.height;
frame->format = buf->f.fmt;
frame->interlaced_frame = !!buf->s.combed;
frame->top_field_first = !!(buf->s.flags & PIC_FLAG_TOP_FIELD_FIRST);
}
static hb_buffer_t* avframe_to_buffer(hb_filter_private_t * pv, AVFrame *frame)
{
hb_buffer_t * buf;
buf = hb_frame_buffer_init(frame->format, frame->width, frame->height);
if (buf == NULL)
{
return NULL;
}
int pp;
for (pp = 0; pp < 3; pp++)
{
int yy;
int width = buf->plane[pp].width;
int stride = buf->plane[pp].stride;
int height = buf->plane[pp].height;
int linesize = frame->linesize[pp];
uint8_t * dst = buf->plane[pp].data;
uint8_t * src = frame->data[pp];
for (yy = 0; yy < height; yy++)
{
memcpy(dst, src, width);
dst += stride;
src += linesize;
}
}
buf->s.start = av_rescale_q(frame->pts, pv->out_time_base,
(AVRational){1, 90000});
buf->s.duration = frame->reordered_opaque;
if (frame->top_field_first)
{
buf->s.flags |= PIC_FLAG_TOP_FIELD_FIRST;
}
if (!frame->interlaced_frame)
{
buf->s.flags |= PIC_FLAG_PROGRESSIVE_FRAME;
}
else
{
buf->s.combed = HB_COMB_HEAVY;
}
if (frame->repeat_pict == 1)
{
buf->s.flags |= PIC_FLAG_REPEAT_FIRST_FIELD;
}
if (frame->repeat_pict == 2)
{
buf->s.flags |= PIC_FLAG_REPEAT_FRAME;
}
return buf;
}
static hb_buffer_t* filterFrame( hb_filter_private_t * pv, hb_buffer_t * in )
{
int result;
hb_buffer_list_t list;
fill_frame(pv, pv->frame, in);
result = av_buffersrc_add_frame(pv->input, pv->frame);
if (result < 0)
{
return NULL;
}
hb_buffer_list_clear(&list);
result = av_buffersink_get_frame(pv->output, pv->frame);
while (result >= 0)
{
hb_buffer_t * buf = avframe_to_buffer(pv, pv->frame);
hb_buffer_list_append(&pv->list, buf);
av_frame_unref(pv->frame);
result = av_buffersink_get_frame(pv->output, pv->frame);
}
while (hb_buffer_list_count(&pv->list) > 1)
{
hb_buffer_t * buf = hb_buffer_list_rem_head(&pv->list);
hb_buffer_t * next = hb_buffer_list_head(&pv->list);
buf->s.stop = next->s.start;
hb_buffer_list_append(&list, buf);
}
return hb_buffer_list_head(&list);
}
static int avfilter_work( hb_filter_object_t * filter,
hb_buffer_t ** buf_in, hb_buffer_t ** buf_out )
{
hb_filter_private_t * pv = filter->private_data;
hb_buffer_t * in = *buf_in;
if (in->s.flags & HB_BUF_FLAG_EOF)
{
hb_buffer_t * last = hb_buffer_list_tail(&pv->list);
if (last != NULL && last->s.start != AV_NOPTS_VALUE)
{
last->s.stop = last->s.start + last->s.duration;
}
hb_buffer_list_append(&pv->list, in);
*buf_out = hb_buffer_list_clear(&pv->list);
*buf_in = NULL;
return HB_FILTER_DONE;
}
*buf_out = filterFrame(pv, in);
return HB_FILTER_OK;
}
/* Deinterlace Settings
* mode:parity
*
* mode - yadif deinterlace mode
* parity - field parity
*
* Modes:
* 1 = Enabled
* 2 = Spatial
* 4 = Bob
* 8 = Selective
*
* Parity:
* 0 = Top Field First
* 1 = Bottom Field First
* -1 = Automatic detection of field parity
*
*/
static hb_dict_t *
convert_deint_settings(const hb_dict_t * settings)
{
int mode = 3, parity = -1;
hb_dict_extract_int(&mode, settings, "mode");
hb_dict_extract_int(&parity, settings, "parity");
if (!(mode & MODE_YADIF_ENABLE))
{
return hb_value_null();
}
int automatic = !!(mode & MODE_DECOMB_SELECTIVE);
int bob = !!(mode & MODE_YADIF_BOB);
int no_spatial = !(mode & MODE_YADIF_SPATIAL);
mode = bob | (no_spatial << 1);
hb_dict_t * result = hb_dict_init();
hb_dict_t * avsettings = hb_dict_init();
hb_dict_set(avsettings, "mode", hb_value_int(mode));
if (automatic)
{
hb_dict_set(avsettings, "auto", hb_value_int(automatic));
}
if (parity != -1)
{
hb_dict_set(avsettings, "parity", hb_value_int(parity));
}
hb_dict_set(result, "yadif", avsettings);
return result;
}
/* Rotate Settings:
* degrees:mirror
*
* degrees - Rotation angle, may be one of 90, 180, or 270
* mirror - Mirror image around x axis
*
* Examples:
* Mode 180:1 Mirror then rotate 180'
* Mode 0:1 Mirror
* Mode 180:0 Rotate 180'
* Mode 90:0 Rotate 90'
* Mode 270:0 Rotate 270'
*
* Legacy Mode Examples (also accepted):
* Mode 1: Flip vertically (y0 becomes yN and yN becomes y0) (aka 180:1)
* Mode 2: Flip horizontally (x0 becomes xN and xN becomes x0) (aka 0:1)
* Mode 3: Flip both horizontally and vertically (aka 180:0)
* Mode 4: Rotate 90' (aka 90:0)
* Mode 7: Flip horiz & vert plus Rotate 90' (aka 270:0)
*/
static hb_dict_t *
convert_rotate_settings(const hb_dict_t * settings)
{
const char * trans = NULL;
int angle = 180, flip = 0, hflip = 0, vflip = 0;
hb_dict_extract_int(&angle, settings, "angle");
hb_dict_extract_bool(&flip, settings, "hflip");
const char * clock;
const char * cclock;
if (flip)
{
clock = "clock_flip";
cclock = "cclock_flip";
}
else
{
clock = "clock";
cclock = "cclock";
}
switch (angle)
{
case 0:
hflip = flip;
break;
case 90:
trans = clock;
break;
case 180:
vflip = 1;
hflip = !flip;
break;
case 270:
trans = cclock;
break;
default:
break;
}
if (trans != NULL)
{
hb_dict_t * result = hb_dict_init();
hb_dict_t * avsettings = hb_dict_init();
hb_dict_set(avsettings, "dir", hb_value_string(trans));
hb_dict_set(result, "transpose", avsettings);
return result;
}
else if (hflip || vflip)
{
hb_dict_t * result = hb_value_array_init();
hb_dict_t * avfilter;
if (vflip)
{
avfilter = hb_dict_init();
hb_dict_set(avfilter, "vflip", hb_value_null());
hb_value_array_append(result, avfilter);
}
if (hflip)
{
avfilter = hb_dict_init();
hb_dict_set(avfilter, "hflip", hb_value_null());
hb_value_array_append(result, avfilter);
}
return result;
}
else
{
return hb_value_null();
}
}
/* Pad presets and tunes
*
* There are currently no presets and tunes for pad
* The custom pad string is converted to an avformat filter graph string
*/
static hb_dict_t *
convert_pad_settings(const hb_dict_t * settings)
{
int width = 0, height = 0, rgb = 0;
int x = -1, y = -1;
char * color = NULL;
hb_dict_extract_int(&width, settings, "width");
hb_dict_extract_int(&height, settings, "height");
hb_dict_extract_string(&color, settings, "color");
hb_dict_extract_int(&x, settings, "x");
hb_dict_extract_int(&y, settings, "y");
if (color != NULL)
{
char * end;
rgb = strtol(color, &end, 0);
if (end == color)
{
// Not a numeric value, lookup by name
rgb = hb_rgb_lookup_by_name(color);
}
free(color);
color = hb_strdup_printf("0x%06x", rgb);
}
char x_str[20];
char y_str[20];
if (x < 0)
{
snprintf(x_str, 20, "(out_w-in_w)/2");
}
else
{
snprintf(x_str, 20, "%d", x);
}
if (y < 0)
{
snprintf(y_str, 20, "(out_h-in_h)/2");
}
else
{
snprintf(y_str, 20, "%d", y);
}
hb_dict_t * result = hb_dict_init();
hb_dict_t * avsettings = hb_dict_init();
hb_dict_set(avsettings, "width", hb_value_int(width));
hb_dict_set(avsettings, "height", hb_value_int(height));
hb_dict_set(avsettings, "x", hb_value_string(x_str));
hb_dict_set(avsettings, "y", hb_value_string(y_str));
if (color != NULL)
{
hb_dict_set(avsettings, "color", hb_value_string(color));
}
hb_dict_set(result, "pad", avsettings);
free(color);
return result;
}
static hb_dict_t * convert_settings(int filter_id, hb_dict_t * settings)
{
switch (filter_id)
{
case HB_FILTER_ROTATE:
return convert_rotate_settings(settings);
case HB_FILTER_DEINTERLACE:
return convert_deint_settings(settings);
case HB_FILTER_PAD:
return convert_pad_settings(settings);
default:
return NULL;
}
}
void hb_avfilter_combine( hb_list_t * list )
{
hb_filter_object_t * avfilter = NULL;
hb_value_t * settings = NULL;
int ii;
for (ii = 0; ii < hb_list_count(list);)
{
hb_filter_object_t * filter = hb_list_item(list, ii);
switch (filter->id)
{
case HB_FILTER_AVFILTER:
{
settings = hb_value_dup(filter->settings);
} break;
case HB_FILTER_ROTATE:
case HB_FILTER_DEINTERLACE:
case HB_FILTER_PAD:
{
settings = convert_settings(filter->id, filter->settings);
} break;
default:
avfilter = NULL;
}
if (settings != NULL)
{
// Some filter values can result in no filter.
// E.g. rotate angle=0:hflip=0
if (hb_value_type(settings) == HB_VALUE_TYPE_NULL)
{
hb_list_rem(list, filter);
hb_filter_close(&filter);
continue;
}
if (avfilter == NULL)
{
avfilter = hb_filter_init(HB_FILTER_AVFILTER);
avfilter->settings = hb_value_array_init();
hb_list_insert(list, ii, avfilter);
ii++;
}
hb_list_rem(list, filter);
hb_filter_close(&filter);
hb_value_array_concat(avfilter->settings, settings);
hb_value_free(&settings);
continue;
}
ii++;
}
}
|