forked from Rupan/comskip
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mpeg2dec.c
1812 lines (1525 loc) · 51.5 KB
/
mpeg2dec.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
/*
* mpeg2dec.c
* Copyright (C) 2000-2003 Michel Lespinasse <[email protected]>
* Copyright (C) 1999-2000 Aaron Holtzman <[email protected]>
*
* This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
* See http://libmpeg2.sourceforge.net/ for updates.
*
* mpeg2dec 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.
*
* mpeg2dec 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <conio.h>
#include <excpt.h>
#else
#include <stdio.h>
#endif
#define SELFTEST
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
#include <fcntl.h>
#include <inttypes.h>
#include "comskip.h"
#ifdef __FreeBSD__
#include <sys/time.h>
#include <sys/stat.h>
#endif
int pass = 0;
double test_pts = 0.0;
#define inline __inline
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/samplefmt.h>
int av_log_level;
#undef AV_TIME_BASE_Q
static AVRational AV_TIME_BASE_Q = {1, AV_TIME_BASE};
#define SDL_AUDIO_BUFFER_SIZE 1024
#define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
#define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
#define AV_SYNC_THRESHOLD 0.01
#define AV_NOSYNC_THRESHOLD 10.0
#define SAMPLE_CORRECTION_PERCENT_MAX 30
#define AUDIO_DIFF_AVG_NB 10
#define FF_ALLOC_EVENT (SDL_USEREVENT)
#define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
#define FF_QUIT_EVENT (SDL_USEREVENT + 2)
#define VIDEO_PICTURE_QUEUE_SIZE 1
#define DEFAULT_AV_SYNC_TYPE AV_SYNC_ADUIO_MASTER
typedef struct VideoPicture
{
int width, height; /* source height & width */
int allocated;
double pts;
} VideoPicture;
typedef struct VideoState
{
AVFormatContext *pFormatCtx;
int videoStream, audioStream, subtitleStream;
int av_sync_type;
int seek_req;
double seek_pts;
int seek_flags;
__int64 seek_pos;
double audio_clock;
AVStream *audio_st;
AVStream *subtitle_st;
DECLARE_ALIGNED(16, uint8_t, audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]);
unsigned int audio_buf_size;
unsigned int audio_buf_index;
AVPacket audio_pkt;
AVPacket audio_pkt_temp;
int audio_hw_buf_size;
double audio_diff_cum; /* used for AV difference average computation */
double audio_diff_avg_coef;
double audio_diff_threshold;
int audio_diff_avg_count;
double frame_timer;
double frame_last_pts;
double frame_last_delay;
double video_clock; ///<pts of last decoded frame / predicted pts of next decoded frame
double video_clock_submitted;
double video_current_pts; ///<current displayed pts (different from video_clock if frame fifos are used)
int64_t video_current_pts_time; ///<time (av_gettime) at which we updated video_current_pts - used to have running video pts
AVStream *video_st;
AVFrame *pFrame;
char filename[1024];
int quit;
AVFrame *frame;
double duration;
double fps;
} VideoState;
VideoState *is;
enum
{
AV_SYNC_AUDIO_MASTER,
AV_SYNC_VIDEO_MASTER,
AV_SYNC_EXTERNAL_MASTER,
};
/* Since we only have one decoding thread, the Big Struct
can be global in case we need it. */
VideoState *global_video_state;
AVPacket flush_pkt;
__int64 pev_best_effort_timestamp = 0;
int video_stream_index = -1;
int audio_stream_index = -1;
int width, height;
int have_frame_rate ;
int stream_index;
__int64 best_effort_timestamp;
#define USE_ASF 1
extern int coding_type;
#ifdef _WIN32
#include <sys/timeb.h>
static inline void gettimeofday(struct timeval * tp, void * dummy)
{
struct _timeb tm;
_ftime (&tm);
tp->tv_sec = tm.time;
tp->tv_usec = tm.millitm * 1000;
}
#endif
static FILE * in_file;
static FILE * sample_file;
static FILE * timing_file = 0;
extern int thread_count;
int is_AC3;
int AC3_rate;
int AC3_mode;
int is_h264=0;
int is_AAC=0;
extern unsigned int AC3_sampling_rate; //AC3
extern int AC3_byterate;
int demux_pid=0;
int demux_asf=0;
int last_pid;
#define PIDS 100
#define PID_MASK 0x1fff
int pids[PIDS];
int pid_type[PIDS];
int pid_pcr[PIDS];
int pid_pid[PIDS];
int top_pid_count[PID_MASK+1];
int top_pid_pid;
int pid;
int selected_video_pid=0;
int selected_audio_pid=0;
int selected_subtitle_pid=0;
int selection_restart_count = 0;
int found_pids=0;
__int64 pts;
__int64 initial_pts;
__int64 final_pts;
double pts_offset = 0.0;
int initial_pts_set = 0;
double initial_apts;
int initial_apts_set = 0;
//int bitrate;
int muxrate,byterate=10000;
#define PTS_FRAME (double)(1.0 / get_fps())
#define SAMPLE_TO_FRAME 2.8125
#define FSEEK _fseeki64
#define FTELL _ftelli64
// The following two functions are undocumented and not included in any public header,
// so we need to declare them ourselves
extern int _fseeki64(FILE *, __int64, int);
extern __int64 _ftelli64(FILE *);
int soft_seeking=0;
extern char basename[];
char field_t;
char tempstring[512];
FILE *myfopen(const char * f, char * m);
#define DUMP_OPEN if (output_timing) { sprintf(tempstring, "%s.timing.csv", basename); timing_file = myfopen(tempstring, "w"); DUMP_HEADER }
#define DUMP_HEADER if (timing_file) fprintf(timing_file, "type ,dts ,pts ,clock ,delta ,offset\n");
#define DUMP_TIMING(T, D, P, C) if (timing_file && !csStepping && !csJumping && !csStartJump) fprintf(timing_file, "%7s, %12.3f,%12.3f, %12.3f, %12.3f, %12.3f,\n", \
T, (double) (D)/frame_delay, (double) (P)/frame_delay, (double) (C)/frame_delay, ((double) (P) - (double) (C))/frame_delay, pts_offset/frame_delay );
#define DUMP_CLOSE if (timing_file) { fclose(timing_file); timing_file = NULL; }
extern int skip_B_frames;
extern int lowres;
static int sigint = 0;
static int verbose = 0;
extern int selftest;
#ifdef _DEBUG
static int dump_seek = 1; // Set to 1 to dump the seeking process
#else
static int dump_seek = 0; // Set to 1 to dump the seeking process
#endif
#ifdef _WIN32
struct _stati64 instat;
#define filesize instat.st_size
#else
struct stat instat;
#define filesize instat.st_size
#endif
extern int frame_count;
int framenum;
fpos_t filepos;
fpos_t fileendpos;
extern int standoff;
__int64 goppos,infopos,packpos,ptspos,headerpos,frompos,SeekPos;
extern int max_repair_size;
extern int variable_bitrate;
int max_internal_repair_size = 40;
int reviewing = 0;
int count=0;
int currentSecond=0;
int cur_hour = 0;
int cur_minute = 0;
int cur_second = 0;
extern char HomeDir[256];
extern int processCC;
int reorderCC = 0;
extern int live_tv;
int csRestart;
int csStartJump;
int csStepping;
int csJumping;
int csFound;
int seekIter = 0;
int seekDirection = 0;
extern FILE * out_file;
extern uint8_t ccData[500];
extern int ccDataLen;
static uint8_t prevccData[500];
static int prevccDataLen;
char prevfield_t = 0;
extern int height,width, videowidth;
extern int output_debugwindow;
extern int output_console;
extern int output_timing;
extern int output_srt;
extern int output_smi;
extern unsigned char *frame_ptr;
extern int lastFrameWasSceneChange;
extern int live_tv_retries;
extern int dvrms_live_tv_retries;
int retries;
static unsigned int frame_period;
extern void set_fps(unsigned int frame_period);
extern void dump_video (char *start, char *end);
extern void dump_audio (char *start, char *end);
extern void Debug(int level, char* fmt, ...);
extern void dump_video_start(void);
extern void dump_audio_start(void);
void file_open();
int DetectCommercials(int, double);
int BuildMasterCommList(void);
FILE* LoadSettings(int argc, char ** argv);
void ProcessCCData(void);
void dump_data(char *start, int length);
static void signal_handler (int sig)
{
sigint = 1;
signal (sig, SIG_DFL);
//return (RETSIGTYPE)0;
return;
}
#define AUDIOBUFFER 800000
static double base_apts, apts;
static DECLARE_ALIGNED(16, short, audio_buffer[AUDIOBUFFER]);
static short *audio_buffer_ptr = audio_buffer;
static int audio_samples = 0;
static int sound_frame_counter = 0;
extern double get_fps();
extern int get_samplerate();
extern int get_channels();
extern void add_volumes(int *volumes, int nr_frames);
extern void set_frame_volume(uint32_t framenr, int volume);
static int max_volume_found = 0;
int ms_audio_delay = 5;
int tracks_without_sound = 0;
int frames_without_sound = 0;
#define MAX_FRAMES_WITHOUT_SOUND 100
int frames_with_loud_sound = 0;
void sound_to_frames(VideoState *is, short *b, int s, int format)
{
int i,n;
int volume;
int delta = 0;
int s_per_frame;
short *buffer;
float *fb;
audio_samples = audio_buffer_ptr - audio_buffer;
n = is->audio_st->codec->channels * is->audio_st->codec->sample_rate;
base_apts = (is->audio_clock - ((double)audio_samples /(double)(n)));
s_per_frame = (int) ((double)(is->audio_st->codec->sample_rate) * (double) is->audio_st->codec->channels / get_fps());
if (s_per_frame == 0)
return;
if (format == AV_SAMPLE_FMT_FLTP)
{
fb = (float*)b;
if (s > 0)
{
for (i = 0; i < s; i++)
{
*audio_buffer_ptr++ = (short) ((*fb++) * 64000);
if ((LONG_PTR) audio_buffer_ptr > (LONG_PTR) &audio_buffer[AUDIOBUFFER-10])
break;
}
}
}
else
{
if (s > 0)
{
for (i = 0; i < s; i++)
{
*audio_buffer_ptr++ = *b++;
}
}
}
audio_samples = audio_buffer_ptr - audio_buffer;
buffer = audio_buffer;
while (audio_samples >= s_per_frame)
{
if (sample_file) fprintf(sample_file, "Frame %i\n", sound_frame_counter);
volume = 0;
for (i = 0; i < s_per_frame; i++)
{
if (sample_file) fprintf(sample_file, "%i\n", *buffer);
volume += (*buffer>0 ? *buffer : - *buffer);
buffer++;
}
volume = volume/s_per_frame;
audio_samples -= s_per_frame;
if (sound_frame_counter == 8)
sound_frame_counter = sound_frame_counter;
if (volume == 0)
{
frames_without_sound++;
}
else if (volume > 20000)
{
if (volume > 256000)
volume = 220000;
frames_with_loud_sound++;
volume = -1;
}
else
{
frames_without_sound = 0;
}
if (max_volume_found < volume)
max_volume_found = volume;
apts = base_apts;
delta = (base_apts - is->video_clock) * get_fps();
// delta = (__int64)(apts/PTS_FRAME) - (__int64)(pts/PTS_FRAME);
if (-max_internal_repair_size < delta && delta < max_internal_repair_size && abs( sound_frame_counter - delta - framenum) > 20 )
{
Debug(1, "Audio PTS jumped %d frames at frame %d\n", -sound_frame_counter + delta + framenum, framenum);
sound_frame_counter = delta + framenum;
}
// DUMP_TIMING("a frame", apts, is->video_clock);
set_frame_volume((demux_asf?ms_audio_delay:0)+sound_frame_counter++, volume);
base_apts += (double)s_per_frame/(double)n;
}
audio_buffer_ptr = audio_buffer;
if (audio_samples > 0)
{
for (i = 0; i < audio_samples; i++)
{
*audio_buffer_ptr++ = *buffer++;
}
}
}
#define STORAGE_SIZE 1000000
static short storage_buf[STORAGE_SIZE];
void audio_packet_process(VideoState *is, AVPacket *pkt)
{
int prev_codec_id = -1;
double frame_delay = 1.0;
int len1, data_size, n;
int dec_channel_layout;
AVPacket *pkt_temp = &is->audio_pkt_temp;
double pts;
int got_frame;
if (!reviewing)
{
dump_audio_start();
dump_audio((char *)pkt->data,(char *) (pkt->data + pkt->size));
}
pkt_temp->data = pkt->data;
pkt_temp->size = pkt->size;
/* Try to align on packet boundary as some demuxers don't do that, in particular dvr-ms
if (is->audio_st->codec->codec_id == CODEC_ID_MP1) {
while (pkt_temp->size > 2 && (pkt_temp->data[0] != 0xff || pkt_temp->data[1] != 0xe0) ) {
pkt_temp->data++;
pkt_temp->size--;
}
}
*/
n = (is->audio_st->codec->bits_per_coded_sample / 8) * is->audio_st->codec->channels * is->audio_st->codec->sample_rate;
#ifdef OLD_AUDIO_PTS_HANDLING
/* if update, update the audio clock w/pts */
if(pkt->pts == AV_NOPTS_VALUE)
{
pts = 0.0;
}
else
{
pts = av_q2d(is->audio_st->time_base)* ( pkt->pts - (is->video_st->start_time != AV_NOPTS_VALUE ? is->video_st->start_time : 0));
}
pts = pts + pts_offset;
// Debug(0 ,"apst[%3d] = %12.3f\n", framenum, pts);
if (pts != 0)
{
if (is->audio_clock != 0)
{
if ((pts - is->audio_clock) < -0.999 || (pts - is->audio_clock) > max_repair_size/get_fps() )
{
Debug(1 ,"Audio jumped by %6.3f at frame %d\n", (pts - is->audio_clock), framenum);
// DUMP_TIMING("a set", pts, is->audio_clock);
is->audio_clock = pts;
}
else
{
// DUMP_TIMING("a free", pts, is->audio_clock);
// Do nothing
}
}
else
{
// DUMP_TIMING("ajmpset", pts, is->audio_clock);
is->audio_clock = pts;
}
}
else
{
/* if we aren't given a pts, set it to the clock */
// DUMP_TIMING("a tick", pts, is->audio_clock);
pts = is->audio_clock;
}
#else
if (pkt->pts != AV_NOPTS_VALUE)
{
is->audio_clock = av_q2d(is->audio_st->time_base)*( pkt->pts - (is->video_st->start_time != AV_NOPTS_VALUE ? is->video_st->start_time : 0));
}
#endif
// fprintf(stderr, "sac = %f\n", is->audio_clock);
while(pkt_temp->size > 0)
{
data_size = STORAGE_SIZE;
if (!is->frame)
{
if (!(is->frame = avcodec_alloc_frame()))
return;
}
else
avcodec_get_frame_defaults(is->frame);
len1 = avcodec_decode_audio4(is->audio_st->codec, is->frame, &got_frame, pkt_temp);
if (prev_codec_id != -1 && prev_codec_id != is->audio_st->codec->codec_id)
{
Debug(2 ,"Audio format change\n");
}
prev_codec_id = is->audio_st->codec->codec_id;
if (len1 < 0)
{
/* if error, we skip the frame */
pkt_temp->size = 0;
break;
}
pkt_temp->data += len1;
pkt_temp->size -= len1;
if (!got_frame)
{
/* stop sending empty packets if the decoder is finished */
continue;
}
data_size = av_samples_get_buffer_size(NULL, is->frame->channels,
is->frame->nb_samples,
is->frame->format, 1);
dec_channel_layout =
(is->frame->channel_layout && is->frame->channels == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
// fprintf(stderr, "Audio %f\n", is->audio_clock);
if (data_size > 0)
{
sound_to_frames(is, (short *)is->frame->data[0], data_size / av_get_bytes_per_sample(is->frame->format), is->frame->format);
}
is->audio_clock += (double)data_size /
(is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
}
}
static void print_fps (int final)
{
static uint32_t frame_counter = 0;
static struct timeval tv_beg, tv_start;
static int total_elapsed;
static int last_count = 0;
struct timeval tv_end;
double fps, tfps;
int frames, elapsed;
char cur_pos[100] = "0:00:00";
if (verbose)
return;
if(csStepping)
return;
if(final < 0)
{
frame_counter = 0;
last_count = 0;
return;
}
again:
gettimeofday (&tv_end, NULL);
if (!frame_counter)
{
tv_start = tv_beg = tv_end;
signal (SIGINT, signal_handler);
}
elapsed = (tv_end.tv_sec - tv_beg.tv_sec) * 100 + (tv_end.tv_usec - tv_beg.tv_usec) / 10000;
total_elapsed = (tv_end.tv_sec - tv_start.tv_sec) * 100 + (tv_end.tv_usec - tv_start.tv_usec) / 10000;
if (final)
{
if (total_elapsed)
tfps = frame_counter * 100.0 / total_elapsed;
else
tfps = 0;
fprintf (stderr,"\n%d frames decoded in %.2f seconds (%.2f fps)\n",
frame_counter, total_elapsed / 100.0, tfps);
fflush(stderr);
return;
}
frame_counter++;
frames = frame_counter - last_count;
#ifdef DONATOR
#else
#ifndef DEBUG
if (is_h264 && frames > 15 && elapsed < 100)
{
Sleep((DWORD)100);
goto again;
}
#endif
#endif
if (elapsed < 100) /* only display every 1.00 seconds */
return;
tv_beg = tv_end;
cur_second = (int)((double)framenum / get_fps());
cur_hour = cur_second / (60 * 60);
cur_second -= cur_hour * 60 * 60;
cur_minute = cur_second / 60;
cur_second -= cur_minute * 60;
sprintf(cur_pos, "%2i:%.2i:%.2i", cur_hour, cur_minute, cur_second);
fps = frames * 100.0 / elapsed;
tfps = frame_counter * 100.0 / total_elapsed;
fprintf (stderr, "%s - %d frames in %.2f sec(%.2f fps), "
"%.2f sec(%.2f fps), %d%%\r", cur_pos, frame_counter,
total_elapsed / 100.0, tfps, elapsed / 100.0, fps, (int) (100.0 * ((double)(framenum) / get_fps()) / global_video_state->duration));
fflush(stderr);
last_count = frame_counter;
}
char field_t;
void SetField(char t)
{
field_t = t;
}
static void ResetInputFile()
{
global_video_state->seek_req = true;
global_video_state->seek_pos = 0;
global_video_state->seek_pts = 0.0;
pev_best_effort_timestamp = 0;
#ifdef PROCESS_CC
if (output_srt || output_smi) CEW_reinit();
#endif
framenum = 0;
sound_frame_counter = 0;
initial_pts = 0;
initial_pts_set = 0;
initial_apts_set = 0;
initial_apts = 0;
audio_samples = 0;
best_effort_timestamp = 0;
}
int SubmitFrame(AVStream *video_st, AVFrame *pFrame , double pts)
{
int res=0;
int changed = 0;
AVCodecContext *pCodecCtx = video_st->codec;
// bitrate = pFrame->bit_rate;
if (pFrame->linesize[0] > 2000 || pFrame->height > 1200 || pFrame->linesize[0] < 100 || pFrame->height < 100)
{
// printf("Panic: illegal height, width or frame period\n");
frame_ptr = NULL;
return(0);
}
if (height != pFrame->height && pFrame->height > 100 && pFrame->height < 2000)
{
height= pFrame->height;
changed = 1;
}
if (width != pFrame->linesize[0] && pFrame->linesize[0] > 100 && pFrame->linesize[0] < 2000)
{
width= pFrame->linesize[0];
changed = 1;
}
if (videowidth != pFrame->width && pFrame->width > 100 && pFrame->width < 2000)
{
videowidth= pFrame->width;
changed = 1;
}
if (changed) Debug(2, "Format changed to [%d : %d]\n", videowidth, height);
infopos = headerpos;
frame_ptr = pFrame->data[0];
if (frame_ptr == NULL)
{
return(0);; // return; // exit(2);
}
if (pFrame->pict_type == AV_PICTURE_TYPE_B)
SetField('B');
else if (pFrame->pict_type == AV_PICTURE_TYPE_I)
SetField('I');
else
SetField('P');
if (framenum == 0 && pass == 0 && test_pts == 0.0)
test_pts = pts;
if (framenum == 0 && pass > 0 && test_pts != pts)
Debug(1,"Reset File Failed, initial pts = %6.3f, seek pts = %6.3f, pass = %d\n", test_pts, pts, pass+1);
if (framenum == 0)
pass++;
#ifdef SELFTEST
if (selftest == 2 && framenum > 20)
{
if (pass > 1)
exit(1);
ResetInputFile();
}
#endif
if (!reviewing)
{
print_fps (0);
if (res == 0)
res = DetectCommercials((int)framenum, pts);
framenum++;
}
return (res);
}
Set_seek(VideoState *is, double pts, double length)
{
AVFormatContext *ic = is->pFormatCtx;
is->seek_flags = AVSEEK_FLAG_ANY;
is->seek_flags = AVSEEK_FLAG_BACKWARD;
is->seek_req = true;
if (is->duration <= 0)
{
uint64_t size = avio_size(ic->pb);
pts = size*pts/length;
is->seek_flags |= AVSEEK_FLAG_BYTE;
}
is->seek_pos = (pts - 1.6 < 0.0 ? 0.0 : pts - 1.6) / av_q2d(is->video_st->time_base);
if (is->video_st->start_time != AV_NOPTS_VALUE)
{
is->seek_pos += is->video_st->start_time;
}
is->seek_pts = pts;
}
void DecodeOnePicture(FILE * f, double pts, double length)
{
double frame_delay = 1.0;
VideoState *is = global_video_state;
AVPacket *packet;
int ret;
int single_frame=0;
int seek_flags = 0;
int count = 0;
int64_t pack_pts=0, comp_pts=0, pack_duration=0;
file_open();
is = global_video_state;
reviewing = 1;
Set_seek(is, pts, length);
pev_best_effort_timestamp = 0;
best_effort_timestamp = 0;
pts_offset = 0.0;
// Debug ( 5, "Seek to %f\n", pts);
frame_ptr = NULL;
packet = &(is->audio_pkt);
for(;;)
{
if(is->quit)
{
break;
}
// seek stuff goes here
if(is->seek_req)
{
ret = avformat_seek_file(is->pFormatCtx, is->videoStream, INT64_MIN, is->seek_pos, INT64_MAX, is->seek_flags);
pev_best_effort_timestamp = 0;
best_effort_timestamp = 0;
is->video_clock = 0.0;
is->audio_clock = 0.0;
if(ret < 0)
{
if (is->pFormatCtx->iformat->read_seek)
{
printf("format specific\n");
}
else if(is->pFormatCtx->iformat->read_timestamp)
{
printf("frame_binary\n");
}
else
{
printf("generic\n");
}
fprintf(stderr, "%s: error while seeking. target: %6.3f, stream_index: %d\n", is->pFormatCtx->filename, pts, stream_index);
}
if(is->audioStream >= 0)
{
avcodec_flush_buffers(is->audio_st->codec);
}
if(is->videoStream >= 0)
{
avcodec_flush_buffers(is->video_st->codec);
}
is->seek_req = 0;
}
if(av_read_frame(is->pFormatCtx, packet) < 0)
{
break;
}
if(packet->stream_index == is->videoStream)
{
if (packet->pts != AV_NOPTS_VALUE)
comp_pts = packet->pts;
pack_pts = comp_pts; // av_rescale_q(comp_pts, is->video_st->time_base, AV_TIME_BASE_Q);
pack_duration = packet->duration; //av_rescale_q(packet->duration, is->video_st->time_base, AV_TIME_BASE_Q);
comp_pts += packet->duration;
pass = 0;
retries = 1;
if (video_packet_process(is, packet) )
{
if (retries == 0)
{
av_free_packet(packet);
break;
}
/*
double frame_delay = av_q2d(is->video_st->codec->time_base)* is->video_st->codec->ticks_per_frame; // <------------------------ frame delay is the time in seconds till the next frame
if (is->video_clock - is->seek_pts > -frame_delay / 2.0)
{
av_free_packet(packet);
break;
}
if (is->video_clock + (pack_duration * av_q2d(is->video_st->time_base)) >= is->seek_pts)
{
av_free_packet(packet);
break;
}
*/
}
}
else if(packet->stream_index == is->audioStream)
{
// audio_packet_process(is, packet);
}
else
{
// Do nothing
}
av_free_packet(packet);
}
reviewing = 0;
}
void raise_exception(void)
{
*(int *)0 = 0;
}
int filter(void)
{
printf("Exception raised, Comskip is terminating\n");
exit(99);
}
// #include "libavformat/avformat.h"
extern char mpegfilename[];
int video_packet_process(VideoState *is,AVPacket *packet)
{
double frame_delay;
int len1, frameFinished;
double pts, dts;
double real_pts;
static int summed_repeat = 0;
double calculated_delay;
if (!reviewing)
{
dump_video_start();
dump_video((char *)packet->data,(char *) (packet->data + packet->size));
}
real_pts = 0.0;
pts = 0;
// is->video_st->codec->flags |= CODEC_FLAG_GRAY;
// Decode video frame
len1 = avcodec_decode_video2(is->video_st->codec, is->pFrame, &frameFinished,
packet);
if (len1<0)
{
if (len1 == -1 && thread_count > 1)
{
InitComSkip();
thread_count = 1;
is->seek_req = 1;
is->seek_pos = 0;
pev_best_effort_timestamp = 0;
best_effort_timestamp = 0;
Debug(1 ,"Restarting processing in single thread mode because frame size is changing \n");
goto quit;
}
}
// Did we get a video frame?
if(frameFinished)
{
if (is->video_st->codec->ticks_per_frame<1)
is->video_st->codec->ticks_per_frame = 1;
frame_delay = av_q2d(is->video_st->codec->time_base)* is->video_st->codec->ticks_per_frame; // <------------------------ frame delay is the time in seconds till the next frame
pev_best_effort_timestamp = best_effort_timestamp;
// best_effort_timestamp = *(__int64 *)av_opt_ptr(avcodec_get_frame_class(), is->pFrame, "best_effort_timestamp");
best_effort_timestamp = av_frame_get_best_effort_timestamp(is->pFrame);
// best_effort_timestamp = is->pFrame->pkt_pts;
calculated_delay = (best_effort_timestamp - pev_best_effort_timestamp) * av_q2d(is->video_st->time_base);