forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.c
3336 lines (3041 loc) · 102 KB
/
sync.c
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sync.c
Copyright (c) 2003-2021 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 "handbrake/handbrake.h"
#include "handbrake/hbffmpeg.h"
#include <stdio.h>
#include "handbrake/audio_resample.h"
#if HB_PROJECT_FEATURE_QSV
#include "handbrake/qsv_common.h"
#endif
#define SYNC_MAX_VIDEO_QUEUE_LEN 40
#define SYNC_MIN_VIDEO_QUEUE_LEN 20
// Audio is small, buffer a lot. It helps to ensure that we see
// the initial PTS from all input streams before setting the 'zero' point.
#define SYNC_MAX_AUDIO_QUEUE_LEN 200
#define SYNC_MIN_AUDIO_QUEUE_LEN 30
// We do not place a limit on the number of subtitle frames
// that are buffered (max_len == INT_MAX) because there are
// cases where we will receive all the subtitles for a file
// all at once (SSA subs).
//
// If we did not buffer these subs here, the following deadlock
// condition would occur:
// 1. Subtitle decoder blocks trying to generate more subtitle
// lines than will fit in sync input buffers.
// 2. This blocks the reader. Reader doesn't read any more
// audio or video, so sync won't receive buffers it needs
// to unblock subtitles.
#define SYNC_MAX_SUBTITLE_QUEUE_LEN INT_MAX
#define SYNC_MIN_SUBTITLE_QUEUE_LEN 0
typedef enum
{
SYNC_TYPE_VIDEO,
SYNC_TYPE_AUDIO,
SYNC_TYPE_SUBTITLE,
} sync_type_t;
typedef struct
{
int64_t pts;
int64_t delta;
} sync_delta_t;
typedef struct
{
int link;
int merge;
hb_buffer_list_t list_current;
} subtitle_sanitizer_t;
typedef struct sync_common_s sync_common_t;
#define SCR_HASH_SZ (2 << 3)
#define SCR_HASH_MASK (SCR_HASH_SZ - 1)
typedef struct
{
int scr_sequence;
int64_t scr_offset;
} scr_t;
typedef struct
{
sync_common_t * common;
// Stream I/O control
int done;
int flush;
hb_list_t * in_queue;
hb_list_t * scr_delay_queue;
int max_len;
int min_len;
hb_fifo_t * fifo_in;
hb_fifo_t * fifo_out;
// PTS synchronization
hb_list_t * delta_list;
int64_t pts_slip;
double next_pts;
double last_pts;
// SCR recovery
int last_scr_sequence;
double last_scr_pts;
double last_duration;
// frame statistics
int64_t first_pts;
double min_frame_duration;
double max_frame_duration;
int64_t current_duration;
int frame_count;
// Error reporting stats
int64_t drop_duration;
int drop;
int64_t drop_pts;
int64_t gap_duration;
int64_t gap_pts;
int first_frame;
// stream type specific context
sync_type_t type;
union
{
struct
{
int id;
int cadence[12];
int new_chap;
} video;
// Audio stream context
struct
{
hb_audio_t * audio;
// Audio filter settings
// Samplerate conversion
hb_audio_resample_t * resample;
double gain_factor;
} audio;
// Subtitle stream context
struct
{
hb_subtitle_t * subtitle;
subtitle_sanitizer_t sanitizer;
} subtitle;
};
} sync_stream_t;
struct sync_common_s
{
// Audio/Video sync thread synchronization
hb_job_t * job;
hb_lock_t * mutex;
int stream_count;
sync_stream_t * streams;
int found_first_pts;
int flush;
// SCR adjustments
scr_t scr[SCR_HASH_SZ];
int first_scr;
// point-to-point support
int start_found;
int64_t pts_to_start;
int64_t start_pts;
int64_t stop_pts;
int wait_for_frame;
int wait_for_pts;
// sync audio work objects
hb_list_t * list_work;
// UpdateState Statistics
int est_frame_count;
uint64_t st_counts[4];
uint64_t st_dates[4];
uint64_t st_first;
int chapter;
};
struct hb_work_private_s
{
sync_common_t * common;
sync_stream_t * stream;
};
/***********************************************************************
* Local prototypes
**********************************************************************/
static void UpdateState( sync_common_t * common, int frame_count );
static void UpdateSearchState( sync_common_t * common, int64_t start,
int frame_count );
static int UpdateSCR( sync_stream_t * stream, hb_buffer_t * buf );
static hb_buffer_t * FilterAudioFrame( sync_stream_t * stream,
hb_buffer_t *buf );
static void SortedQueueBuffer( sync_stream_t * stream, hb_buffer_t * buf );
static hb_buffer_t * sanitizeSubtitle(sync_stream_t * stream,
hb_buffer_t * sub);
static int OutputBuffer( sync_common_t * common );
static void saveChap( sync_stream_t * stream, hb_buffer_t * buf )
{
if (stream->type != SYNC_TYPE_VIDEO || buf == NULL)
{
return;
}
if (buf->s.new_chap > 0)
{
stream->video.new_chap = buf->s.new_chap;
}
}
static void restoreChap( sync_stream_t * stream, hb_buffer_t * buf )
{
if (stream->type != SYNC_TYPE_VIDEO || buf == NULL)
{
return;
}
if (stream->video.new_chap > 0 && buf->s.new_chap <= 0)
{
buf->s.new_chap = stream->video.new_chap;
stream->video.new_chap = 0;
}
}
static int fillQueues( sync_common_t * common )
{
int ii, wait = 0, abort = 0;
for (ii = 0; ii < common->stream_count; ii++)
{
sync_stream_t *stream = &common->streams[ii];
// Don't let the queues grow indefinitely
// abort when too large
if (hb_list_count(stream->in_queue) > stream->max_len)
{
abort = 1;
}
if (hb_list_count(stream->in_queue) <= stream->min_len)
{
wait = 1;
}
}
return !wait || abort;
}
static void scrSlip( sync_common_t * common, int64_t delta )
{
int ii;
for (ii = 0; ii < SCR_HASH_SZ; ii++)
{
common->scr[ii].scr_offset += delta;
}
}
static void shiftTS( sync_common_t * common, int64_t delta )
{
int ii, jj;
scrSlip(common, delta);
for (ii = 0; ii < common->stream_count; ii++)
{
hb_buffer_t * buf = NULL;
sync_stream_t * stream = &common->streams[ii];
int count = hb_list_count(stream->in_queue);
for (jj = 0; jj < count; jj++)
{
buf = hb_list_item(stream->in_queue, jj);
if (buf->s.start != AV_NOPTS_VALUE)
{
buf->s.start -= delta;
}
if (buf->s.stop != AV_NOPTS_VALUE)
{
buf->s.stop -= delta;
}
}
if (buf != NULL && buf->s.start != AV_NOPTS_VALUE)
{
stream->last_scr_pts = buf->s.start + buf->s.duration;
}
else
{
stream->last_scr_pts = (int64_t)AV_NOPTS_VALUE;
}
}
}
static hb_buffer_t * CreateSilenceBuf( sync_stream_t * stream,
int64_t dur, int64_t pts )
{
double frame_dur, next_pts, duration;
int size;
hb_buffer_list_t list;
hb_buffer_t * buf;
if (stream->audio.audio->config.out.codec & HB_ACODEC_PASS_FLAG)
{
return NULL;
}
duration = dur;
// Although frame size isn't technically important any more, we
// keep audio buffer durations <= input audio buffer durations.
frame_dur = (90000. * stream->audio.audio->config.in.samples_per_frame) /
stream->audio.audio->config.in.samplerate;
// Audio mixdown occurs in decoders before sync.
// So number of channels here is output channel count.
// But audio samplerate conversion happens in later here in sync.c
// FilterAudioFrame, so samples_per_frame is still the input sample count.
size = sizeof(float) * stream->audio.audio->config.in.samples_per_frame *
hb_mixdown_get_discrete_channel_count(
stream->audio.audio->config.out.mixdown);
hb_buffer_list_clear(&list);
next_pts = pts;
while (duration >= frame_dur)
{
buf = hb_buffer_init(size);
memset(buf->data, 0, buf->size);
buf->s.start = next_pts;
next_pts += frame_dur;
buf->s.stop = next_pts;
buf->s.duration = frame_dur;
duration -= frame_dur;
hb_buffer_list_append(&list, buf);
}
if (duration > 0)
{
// Make certain size is even multiple of sample size * num channels
size = (int)(duration * stream->audio.audio->config.in.samplerate /
90000) * sizeof(float) *
hb_mixdown_get_discrete_channel_count(
stream->audio.audio->config.out.mixdown);
if (size > 0)
{
buf = hb_buffer_init(size);
memset(buf->data, 0, buf->size);
buf->s.start = next_pts;
next_pts += duration;
buf->s.stop = next_pts;
buf->s.duration = duration;
hb_buffer_list_append(&list, buf);
}
}
return hb_buffer_list_clear(&list);
}
static hb_buffer_t * CreateBlackBuf( sync_stream_t * stream,
int64_t dur, int64_t pts )
{
// I would like to just allocate one black frame here and give
// it the full duration. But encoders that use B-Frames compute
// the dts delay based on the pts of the 2nd or 3rd frame which
// can be a very large value in this case. This large dts delay
// causes problems with computing the dts of the frames (which is
// extrapolated from the pts and the computed dts delay). And the
// dts problems lead to problems with frame duration.
double frame_dur, next_pts, duration;
hb_buffer_list_t list;
hb_buffer_t * buf = NULL;
hb_buffer_list_clear(&list);
duration = dur;
next_pts = pts;
frame_dur = 90000. * stream->common->job->title->vrate.den /
stream->common->job->title->vrate.num;
// Only create black buffers of frame_dur or longer
while (duration >= frame_dur)
{
if (buf == NULL)
{
buf = hb_frame_buffer_init(stream->common->job->pix_fmt,
stream->common->job->title->geometry.width,
stream->common->job->title->geometry.height);
uint8_t *planes[4];
ptrdiff_t linesizes[4];
for (int i = 0; i <= buf->f.max_plane; ++i)
{
planes[i] = buf->plane[i].data;
linesizes[i] = buf->plane[i].stride;
}
av_image_fill_black(planes, linesizes, stream->common->job->pix_fmt,
stream->common->job->color_range, buf->f.width, buf->f.height);
buf->f.color_prim = stream->common->job->title->color_prim;
buf->f.color_transfer = stream->common->job->title->color_transfer;
buf->f.color_matrix = stream->common->job->title->color_matrix;
buf->f.color_range = stream->common->job->color_range;
}
else
{
buf = hb_buffer_dup(buf);
}
buf->s.start = next_pts;
next_pts += frame_dur;
buf->s.stop = next_pts;
buf->s.duration = frame_dur;
duration -= frame_dur;
hb_buffer_list_append(&list, buf);
}
if (buf != NULL)
{
if (buf->s.stop < pts + dur)
{
// Extend the duration of the last black buffer to fill
// the remaining gap.
buf->s.duration += pts + dur - buf->s.stop;
buf->s.stop = pts + dur;
}
}
return hb_buffer_list_clear(&list);
}
static void setNextPts( sync_common_t * common )
{
int ii;
for (ii = 0; ii < common->stream_count; ii++)
{
sync_stream_t * stream = &common->streams[ii];
hb_buffer_t * buf = hb_list_item(stream->in_queue, 0);
if (buf != NULL)
{
stream->next_pts = buf->s.start;
}
else
{
stream->next_pts = (int64_t)AV_NOPTS_VALUE;
}
}
}
static void alignStream( sync_common_t * common, sync_stream_t * stream,
int64_t pts )
{
if (hb_list_count(stream->in_queue) <= 0 ||
stream->type == SYNC_TYPE_SUBTITLE)
{
return;
}
hb_buffer_t * buf = hb_list_item(stream->in_queue, 0);
int64_t gap = buf->s.start - pts;
if (gap == 0)
{
return;
}
if (gap < 0)
{
int ii;
// Drop frames from other streams
for (ii = 0; ii < common->stream_count; ii++)
{
sync_stream_t * other_stream = &common->streams[ii];
if (stream == other_stream)
{
continue;
}
while (hb_list_count(other_stream->in_queue) > 0)
{
buf = hb_list_item(other_stream->in_queue, 0);
if (buf->s.start < pts)
{
if (other_stream->type == SYNC_TYPE_SUBTITLE &&
buf->s.stop > pts)
{
// Subtitle ends after start time, keep sub and
// adjust it's start time
buf->s.start = pts;
break;
}
else
{
hb_list_rem(other_stream->in_queue, buf);
hb_buffer_close(&buf);
}
}
else
{
// Fill the partial frame gap left after dropping frames
alignStream(common, other_stream, pts);
break;
}
}
}
}
else
{
hb_buffer_t * blank_buf = NULL;
// Insert a blank frame to fill the gap
if (stream->type == SYNC_TYPE_AUDIO)
{
// Can't add silence padding to passthru streams
if (!(stream->audio.audio->config.out.codec & HB_ACODEC_PASS_FLAG))
{
blank_buf = CreateSilenceBuf(stream, gap, pts);
}
}
else if (stream->type == SYNC_TYPE_VIDEO)
{
blank_buf = CreateBlackBuf(stream, gap, pts);
}
int64_t last_stop = pts;
hb_buffer_t * next;
int pos;
for (pos = 0; blank_buf != NULL; blank_buf = next, pos++)
{
last_stop = blank_buf->s.stop;
next = blank_buf->next;
blank_buf->next = NULL;
hb_list_insert(stream->in_queue, pos, blank_buf);
}
if (stream->type == SYNC_TYPE_VIDEO && last_stop < buf->s.start)
{
// Extend the duration of the first frame to fill the remaining gap.
buf->s.duration += buf->s.start - last_stop;
buf->s.start = last_stop;
}
}
}
static void alignStreams( sync_common_t * common, int64_t pts )
{
int ii;
hb_buffer_t * buf;
if (common->job->align_av_start)
{
int64_t first_pts = AV_NOPTS_VALUE;
int audio_passthru = 0;
for (ii = 0; ii < common->stream_count; ii++)
{
sync_stream_t * stream = &common->streams[ii];
buf = hb_list_item(stream->in_queue, 0);
// P-to-P encoding will pass the start point in pts.
// Drop any buffers that are before the start point.
while (buf != NULL && buf->s.start < pts)
{
hb_list_rem(stream->in_queue, buf);
hb_buffer_close(&buf);
buf = hb_list_item(stream->in_queue, 0);
}
if (buf == NULL)
{
continue;
}
if (stream->type == SYNC_TYPE_AUDIO &&
stream->audio.audio->config.out.codec & HB_ACODEC_PASS_FLAG)
{
// Find the largest initial pts of all passthru audio streams.
// We can not add silence to passthru audio streams.
// To align with a passthru audio stream, we must drop
// buffers from all other streams that are before
// the first buffer in the passthru audio stream.
audio_passthru = 1;
if (first_pts < buf->s.start)
{
first_pts = buf->s.start;
}
}
else if (!audio_passthru)
{
// Find the smallest initial pts of all streams when
// there is *no* passthru audio.
// When there is no passthru audio stream, we insert
// silence or black buffers to fill any gaps between
// the start of any stream and the start of the stream
// with the smallest pts.
if (first_pts == AV_NOPTS_VALUE || first_pts > buf->s.start)
{
first_pts = buf->s.start;
}
}
}
if (first_pts != AV_NOPTS_VALUE)
{
for (ii = 0; ii < common->stream_count; ii++)
{
// Fill the gap (or drop frames for passthru audio)
alignStream(common, &common->streams[ii], first_pts);
}
}
}
}
static void computeInitialTS( sync_common_t * common,
sync_stream_t * first_stream )
{
int ii;
hb_buffer_t * prev, * buf;
// Process first_stream first since it has the initial PTS
prev = NULL;
for (ii = 0; ii < hb_list_count(first_stream->in_queue);)
{
buf = hb_list_item(first_stream->in_queue, ii);
if (!UpdateSCR(first_stream, buf))
{
hb_list_rem(first_stream->in_queue, buf);
}
else
{
ii++;
if (first_stream->type == SYNC_TYPE_VIDEO && prev != NULL)
{
double duration = buf->s.start - prev->s.start;
if (duration > 0)
{
prev->s.duration = duration;
prev->s.stop = buf->s.start;
}
}
prev = buf;
}
}
for (ii = 0; ii < common->stream_count; ii++)
{
sync_stream_t * stream = &common->streams[ii];
if (stream == first_stream)
{
// skip first_stream, already done
continue;
}
int jj;
prev = NULL;
for (jj = 0; jj < hb_list_count(stream->in_queue);)
{
buf = hb_list_item(stream->in_queue, jj);
if (!UpdateSCR(stream, buf))
{
// Subtitle put into delay queue, remove it from in_queue
hb_list_rem(stream->in_queue, buf);
}
else
{
jj++;
if (stream->type == SYNC_TYPE_VIDEO && prev != NULL)
{
double duration = buf->s.start - prev->s.start;
if (duration > 0)
{
prev->s.duration = duration;
prev->s.stop = buf->s.start;
}
}
prev = buf;
}
}
}
alignStreams(common, AV_NOPTS_VALUE);
}
static void checkFirstPts( sync_common_t * common )
{
int ii;
int64_t first_pts = INT64_MAX;
sync_stream_t * first_stream = NULL;
for (ii = 0; ii < common->stream_count; ii++)
{
sync_stream_t *stream = &common->streams[ii];
if (stream->type == SYNC_TYPE_SUBTITLE)
{
// only wait for audio and video
continue;
}
// If buffers are queued, find the lowest initial PTS
while (hb_list_count(stream->in_queue) > 0)
{
hb_buffer_t * buf = hb_list_item(stream->in_queue, 0);
if (buf->s.start != AV_NOPTS_VALUE)
{
// We require an initial pts for every stream
if (buf->s.start < first_pts)
{
first_pts = buf->s.start;
first_stream = stream;
}
break;
}
else
{
hb_list_rem(stream->in_queue, buf);
hb_buffer_close(&buf);
}
}
}
// We should *always* find a first pts because we let the queues
// fill before performing this test.
if (first_pts != INT64_MAX)
{
common->found_first_pts = 1;
// We may have buffers that have no timestamps (i.e. AV_NOPTS_VALUE).
// Compute these timestamps based on previous buffer's timestamp
// and duration.
computeInitialTS(common, first_stream);
// After this initialization, all AV_NOPTS_VALUE timestamps
// will be filled in by UpdateSCR()
}
else
{
// This should never happen
hb_error("checkFirstPts: No initial PTS found!\n");
}
}
static void addDelta( sync_common_t * common, int64_t start, int64_t delta)
{
int ii;
for (ii = 0; ii < common->stream_count; ii++)
{
sync_delta_t * delta_item = malloc(sizeof(sync_delta_t));
delta_item->pts = start;
delta_item->delta = delta;
hb_list_add(common->streams[ii].delta_list, delta_item);
}
}
static void applyDeltas( sync_common_t * common )
{
int ii;
// Apply delta to any applicable buffers in the queue
for (ii = 0; ii < common->stream_count; ii++)
{
sync_stream_t * stream = &common->streams[ii];
// Make adjustments for deltas found in other streams
sync_delta_t * delta = hb_list_item(stream->delta_list, 0);
if (delta != NULL)
{
int jj, index = -1;
int64_t prev_start, max = 0;
hb_buffer_t * buf;
prev_start = stream->next_pts;
for (jj = 0; jj < hb_list_count(stream->in_queue); jj++)
{
buf = hb_list_item(stream->in_queue, jj);
if (stream->type == SYNC_TYPE_SUBTITLE)
{
if (buf->s.start > delta->pts)
{
// absorb gaps in subtitles as soon as possible
index = jj;
break;
}
}
else if (buf->s.start > delta->pts)
{
// absorb gap in largest gap found in this stream.
if (buf->s.start - prev_start > max)
{
max = buf->s.start - prev_start;
index = jj;
}
if (stream->type == SYNC_TYPE_AUDIO && max >= delta->delta)
{
// absorb gaps in audio as soon as possible
// when there is a gap that will absorb it.
break;
}
}
prev_start = buf->s.start;
}
if (index >= 0)
{
for (jj = index; jj < hb_list_count(stream->in_queue); jj++)
{
buf = hb_list_item(stream->in_queue, jj);
buf->s.start -= delta->delta;
if (buf->s.stop != AV_NOPTS_VALUE)
{
buf->s.stop -= delta->delta;
}
}
// Correct the duration of the video buffer before
// the affected timestamp correction.
if (stream->type == SYNC_TYPE_VIDEO && index > 0)
{
buf = hb_list_item(stream->in_queue, index - 1);
if (buf->s.duration > delta->delta)
{
buf->s.duration -= delta->delta;
buf->s.stop -= delta->delta;
}
else
{
buf->s.duration = 0;
buf->s.stop = buf->s.start;
}
}
stream->pts_slip += delta->delta;
hb_list_rem(stream->delta_list, delta);
free(delta);
}
}
}
}
static void removeVideoJitter( sync_stream_t * stream, int stop )
{
int ii;
hb_buffer_t * buf;
double frame_duration, next_pts;
frame_duration = 90000. * stream->common->job->title->vrate.den /
stream->common->job->title->vrate.num;
buf = hb_list_item(stream->in_queue, 0);
buf->s.start = stream->next_pts;
next_pts = stream->next_pts + frame_duration;
for (ii = 1; ii <= stop; ii++)
{
buf->s.duration = frame_duration;
buf->s.stop = next_pts;
buf = hb_list_item(stream->in_queue, ii);
buf->s.start = next_pts;
next_pts += frame_duration;
}
}
// Look for a sequence of packets whose duration as measure by
// vrate closely matches the duration as measured by timestamp
// differences. When close matches are found, smooth the timestamps.
//
// Most often, video dejitter is applied when there is jitter due to
// soft telecine. I also have a couple sample files that have very
// bad video jitter that this corrects.
static void dejitterVideo( sync_stream_t * stream )
{
int ii, count, jitter_stop;
double frame_duration, duration;
hb_buffer_t * buf;
count = hb_list_count(stream->in_queue);
if (count < 2)
{
return;
}
frame_duration = 90000. * stream->common->job->title->vrate.den /
stream->common->job->title->vrate.num;
// Look for start of jittered sequence
buf = hb_list_item(stream->in_queue, 1);
duration = buf->s.start - stream->next_pts;
if (ABS(duration - frame_duration) < 1.1)
{
// Ignore small jitter
buf->s.start = stream->next_pts + frame_duration;
buf = hb_list_item(stream->in_queue, 0);
buf->s.start = stream->next_pts;
buf->s.duration = frame_duration;
buf->s.stop = stream->next_pts + frame_duration;
return;
}
// Look for end of jittered sequence
jitter_stop = 0;
for (ii = 1; ii < count; ii++)
{
buf = hb_list_item(stream->in_queue, ii);
duration = buf->s.start - stream->next_pts;
// Only dejitter video that aligns periodically
// with the frame durations.
if (ABS(duration - ii * frame_duration) < frame_duration / 3)
{
jitter_stop = ii;
}
}
if (jitter_stop > 0)
{
removeVideoJitter(stream, jitter_stop);
}
}
// Fix video overlaps that could not be corrected by dejitter
static void fixVideoOverlap( sync_stream_t * stream )
{
int drop = 0;
int64_t overlap;
hb_buffer_t * buf;
// If time goes backwards drop the frame.
// Check if subsequent buffers also overlap.
while ((buf = hb_list_item(stream->in_queue, 0)) != NULL)
{
// For video, an overlap is where the entire frame is
// in the past.
overlap = stream->next_pts - buf->s.stop;
if (overlap >= 0)
{
if (stream->drop == 0)
{
stream->drop_pts = buf->s.start;
}
hb_list_rem(stream->in_queue, buf);
// Video frame durations are assumed to be variable and are
// adjusted based on the start time of the next frame before
// we get to this point.
//
// Estimate duration dropped based on average framerate
stream->drop_duration +=
90000. * stream->common->job->title->vrate.den /
stream->common->job->title->vrate.num;
stream->drop++;
drop++;
saveChap(stream, buf);
hb_buffer_close(&buf);
}
else
{
break;
}
}
if (drop <= 0 && stream->drop > 0)
{
hb_log("sync: video time went backwards %d ms, dropped %d frames. "
"PTS %"PRId64"",
(int)stream->drop_duration / 90, stream->drop, stream->drop_pts);
stream->drop_duration = 0;
stream->drop = 0;
}
}
static void removeAudioJitter(sync_stream_t * stream, int stop)
{
int ii;
hb_buffer_t * buf;
double next_pts;
// If duration of sum of packet durations is close to duration
// as measured by timestamps, align timestamps to packet durations.
// The packet durations are computed based on samplerate and
// number of samples and are therefore a reliable measure
// of the actual duration of an audio frame.
buf = hb_list_item(stream->in_queue, 0);
buf->s.start = stream->next_pts;
next_pts = stream->next_pts + buf->s.duration;
for (ii = 1; ii <= stop; ii++)
{
// Duration can be fractional, so track fractional PTS
buf->s.stop = next_pts;
buf = hb_list_item(stream->in_queue, ii);
buf->s.start = next_pts;
next_pts += buf->s.duration;
}
}
// Look for a sequence of packets whose duration as measure by packet
// durations closely matches the duration as measured by timestamp
// differences. When close matches are found, smooth the timestamps.
//
// This fixes issues where there are false overlaps and gaps in audio
// timestamps. libav creates this type of problem sometimes with it's
// timestamp guessing code.
static void dejitterAudio( sync_stream_t * stream )
{
int ii, count, jitter_stop;
double duration;
hb_buffer_t * buf, * buf0, * buf1;
count = hb_list_count(stream->in_queue);
if (count < 4)
{
return;
}
// Look for start of jitter sequence
jitter_stop = 0;
buf0 = hb_list_item(stream->in_queue, 0);
buf1 = hb_list_item(stream->in_queue, 1);
if (ABS(buf0->s.duration - (buf1->s.start - stream->next_pts)) < 1.1)
{
// Ignore very small jitter
return;
}
buf = hb_list_item(stream->in_queue, 0);
duration = buf->s.duration;