forked from leixiaohua1020/VideoEye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ve_play.cpp
4149 lines (3711 loc) · 120 KB
/
ve_play.cpp
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
/*
*
*
* VideoEye
*
* 雷霄骅 Lei Xiaohua
* leixiaohua1020@126.com
* 中国传媒大学/数字电视技术
* Communication University of China / Digital TV Technology
* http://blog.csdn.net/leixiaohua1020
*
*/
#include "stdafx.h"
#include "ve_play.h"
const char program_name[] = "VE_Player";
const int program_birth_year = 2013;
#define MAX_QUEUE_SIZE (15 * 1024 * 1024)
#define MIN_FRAMES 5
/* SDL audio buffer size, in samples. Should be small to have precise
A/V sync as SDL does not have hardware buffer fullness info. */
#define SDL_AUDIO_BUFFER_SIZE 1024
/* no AV sync correction is done if below the AV sync threshold */
#define AV_SYNC_THRESHOLD 0.01
/* no AV correction is done if too big error */
#define AV_NOSYNC_THRESHOLD 10.0
/* maximum audio speed change to get correct sync */
#define SAMPLE_CORRECTION_PERCENT_MAX 10
/* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
#define AUDIO_DIFF_AVG_NB 20
/* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
/* TODO: We assume that a decoded and resampled frame fits into this buffer */
#define SAMPLE_ARRAY_SIZE (8 * 65536)
static int sws_flags = SWS_BICUBIC;
//读取输入文件协议的时候使用;来自ffmpeg源码
typedef struct URLContext {
const AVClass *av_class; ///< information for av_log(). Set by url_open().
struct URLProtocol *prot;
int flags;
int is_streamed; /**< true if streamed (no seek possible), default = false */
int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
void *priv_data;
char *filename; /**< specified URL */
int is_connected;
AVIOInterruptCB interrupt_callback;
} URLContext;
typedef struct URLProtocol {
const char *name;
int (*url_open)(URLContext *h, const char *url, int flags);
int (*url_read)(URLContext *h, unsigned char *buf, int size);
int (*url_write)(URLContext *h, const unsigned char *buf, int size);
int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
int (*url_close)(URLContext *h);
struct URLProtocol *next;
int (*url_read_pause)(URLContext *h, int pause);
int64_t (*url_read_seek)(URLContext *h, int stream_index,
int64_t timestamp, int flags);
int (*url_get_file_handle)(URLContext *h);
int priv_data_size;
const AVClass *priv_data_class;
int flags;
int (*url_check)(URLContext *h, int mask);
} URLProtocol;
//---------------------
typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
int abort_request;
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;
#define VIDEO_PICTURE_QUEUE_SIZE 4
#define SUBPICTURE_QUEUE_SIZE 4
long rint(double x)
{
if(x >= 0.)
return (long)(x + 0.5);
else
return (long)(x - 0.5);
}
typedef struct VideoPicture {
double pts; ///< presentation time stamp for this picture
int64_t pos; ///< byte position in file
int skip;
SDL_Overlay *bmp;
int width, height; /* source height & width */
AVRational sample_aspect_ratio;
int allocated;
int reallocate;
#if CONFIG_AVFILTER
AVFilterBufferRef *picref;
#endif
} VideoPicture;
typedef struct SubPicture {
double pts; /* presentation time stamp for this picture */
AVSubtitle sub;
} SubPicture;
typedef struct AudioParams {
int freq;
int channels;
int channel_layout;
enum AVSampleFormat fmt;
} AudioParams;
enum {
AV_SYNC_AUDIO_MASTER, /* default choice */
AV_SYNC_VIDEO_MASTER,
AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
};
//视频显示方式
enum V_Show_Mode {
SHOW_MODE_YUV = 0, SHOW_MODE_Y, SHOW_MODE_U, SHOW_MODE_V
};
typedef struct VideoState {
SDL_Thread *read_tid;
SDL_Thread *video_tid;
SDL_Thread *refresh_tid;
AVInputFormat *iformat;
int no_background;
int abort_request;
int force_refresh;
int paused;
int last_paused;
int que_attachments_req;
int seek_req;
int seek_flags;
int64_t seek_pos;
int64_t seek_rel;
int read_pause_return;
AVFormatContext *ic;
int audio_stream;
int av_sync_type;
double external_clock; /* external clock base */
int64_t external_clock_time;
double audio_clock;
double audio_diff_cum; /* used for AV difference average computation */
double audio_diff_avg_coef;
double audio_diff_threshold;
int audio_diff_avg_count;
AVStream *audio_st;
PacketQueue audioq;
int audio_hw_buf_size;
DECLARE_ALIGNED(16,uint8_t,audio_buf2)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
uint8_t *audio_buf;
uint8_t *audio_buf1;
unsigned int audio_buf_size; /* in bytes */
int audio_buf_index; /* in bytes */
int audio_write_buf_size;
AVPacket audio_pkt_temp;
AVPacket audio_pkt;
struct AudioParams audio_src;
struct AudioParams audio_tgt;
struct SwrContext *swr_ctx;
double audio_current_pts;
double audio_current_pts_drift;
int frame_drops_early;
int frame_drops_late;
AVFrame *frame;
enum ShowMode {
SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
} show_mode;
int16_t sample_array[SAMPLE_ARRAY_SIZE];
int sample_array_index;
int last_i_start;
RDFTContext *rdft;
int rdft_bits;
FFTSample *rdft_data;
int xpos;
SDL_Thread *subtitle_tid;
int subtitle_stream;
int subtitle_stream_changed;
AVStream *subtitle_st;
PacketQueue subtitleq;
SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
int subpq_size, subpq_rindex, subpq_windex;
SDL_mutex *subpq_mutex;
SDL_cond *subpq_cond;
double frame_timer;
double frame_last_pts;
double frame_last_duration;
double frame_last_dropped_pts;
double frame_last_returned_time;
double frame_last_filter_delay;
int64_t frame_last_dropped_pos;
double video_clock; ///< pts of last decoded frame / predicted pts of next decoded frame
int video_stream;
AVStream *video_st;
PacketQueue videoq;
double video_current_pts; ///< current displayed pts (different from video_clock if frame fifos are used)
double video_current_pts_drift; ///< video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
int64_t video_current_pos; ///< current displayed file pos
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size, pictq_rindex, pictq_windex;
SDL_mutex *pictq_mutex;
SDL_cond *pictq_cond;
#if !CONFIG_AVFILTER
struct SwsContext *img_convert_ctx;
#endif
char filename[1024];
int width, height, xleft, ytop;
int step;
#if CONFIG_AVFILTER
AVFilterContext *in_video_filter; ///< the first filter in the video chain
AVFilterContext *out_video_filter; ///< the last filter in the video chain
int use_dr1;
FrameBuffer *buffer_pool;
#endif
int refresh;
int last_video_stream, last_audio_stream, last_subtitle_stream;
SDL_cond *continue_read_thread;
//视频显示方式------------
enum V_Show_Mode v_show_mode;
} VideoState;
VideoState *g_is = NULL;
enum ShowMode {
SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
} ;
//指向MFC窗口的指针
CVideoEyeDlg * dlg;
/* options specified by the user */
static AVInputFormat *file_iformat;
static const char *input_filename;
static const char *window_title;
static int fs_screen_width;
static int fs_screen_height;
static int screen_width = 0;
static int screen_height = 0;
static int audio_disable ;
static int video_disable ;
static int wanted_stream[AVMEDIA_TYPE_NB] = {-1,-1,0,-1,0};
// [AVMEDIA_TYPE_AUDIO] = -1,
// [AVMEDIA_TYPE_VIDEO] = -1,
// [AVMEDIA_TYPE_SUBTITLE] = -1,};
static int seek_by_bytes = -1;
static int display_disable;
static int show_status = 1;
static int av_sync_type = AV_SYNC_AUDIO_MASTER;
static int64_t start_time = AV_NOPTS_VALUE;
static int64_t duration = AV_NOPTS_VALUE;
static int workaround_bugs = 1;
static int fast = 0;
static int genpts = 0;
static int lowres = 0;
static int idct = FF_IDCT_AUTO;
static enum AVDiscard skip_frame = AVDISCARD_DEFAULT;
static enum AVDiscard skip_idct = AVDISCARD_DEFAULT;
static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
static int error_concealment = 3;
static int decoder_reorder_pts = -1;
static int autoexit;
static int exit_on_keydown;
static int exit_on_mousedown;
static int loop = 1;
static int framedrop = -1;
static int infinite_buffer = -1;
static enum ShowMode show_mode = SHOW_MODE_NONE;
static const char *audio_codec_name;
static const char *subtitle_codec_name;
static const char *video_codec_name;
static int rdftspeed = 20;
#if CONFIG_AVFILTER
static char *vfilters = NULL;
#endif
/* current context */
static int is_full_screen;
static int64_t audio_callback_time;
static AVPacket flush_pkt;
//特殊容器(FLV,MP4等)输出h.264原始码流--------------
//需要在码流文件前面加上SPS,PPS------------
int dataoutput_h264_ft;
#define FF_ALLOC_EVENT (SDL_USEREVENT)
#define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
#define FF_QUIT_EVENT (SDL_USEREVENT + 2)
//---------------------------
//自定义一个事件,用于调整播放进度
#define VE_SEEK_BAR_EVENT (SDL_USEREVENT + 4)
int seek_bar_pos;
//是否拉伸-------------------------
#define VE_STRETCH_EVENT (SDL_USEREVENT + 5)
int is_stretch=1;
//--------------------------------
//专门设置的标记,在程序将要退出的时候会置1
static int exit_remark=0;
//---------------------------------
static SDL_Surface *screen;
//视频帧索引
int vframe_index=0;
//音频帧索引
int aframe_index=0;
//Packet索引
int packet_index=0;
int ve_reset_index(){
vframe_index=0;
aframe_index=0;
packet_index=0;
return 0;
}
//设置MFC参数,成功的话返回0,否则返回-1
//全局的,只设置一次
int ve_param_global(VideoState *is){
//初始化
CString input_protocol,input_format,wxh,decoder_name,
decoder_type,bitrate,extention,pix_fmt,framerate,timelong,decoder_name_au,sample_rate_au,channels_au;
float framerate_temp,timelong_temp,bitrate_temp;
//注意:把int等类型转换成LPCTSTR
//CString可以直接赋值给LPCTSTR
AVFormatContext *pFormatCtx = is->ic;
int video_stream=is->video_stream;
int audio_stream=is->audio_stream;
AVCodecContext *pCodecCtx = pFormatCtx->streams[video_stream]->codec;
AVCodecContext *pCodecCtx_au = pFormatCtx->streams[audio_stream]->codec;
#ifdef _UNICODE
USES_CONVERSION;
#endif
if(pFormatCtx->pb!=NULL){
URLContext *uc=(URLContext *)pFormatCtx->pb->opaque;
if(strcmp(is->filename,"read_memory")==0){
dlg->m_input_protocol.SetWindowText(_T("内存读入"));
}else{
URLProtocol *up=(URLProtocol *)uc->prot;
//输入文件的协议----------
#ifdef _UNICODE
input_protocol.Format(_T("%s"),A2W(up->name));
#else
input_protocol.Format(_T("%s"),up->name);
#endif
dlg->m_input_protocol.SetWindowText(input_protocol);
}
}
//视频解码参数,有视频的时候设置
if(video_stream!=-1){
wxh.Format(_T("%d x %d"),pCodecCtx->width,pCodecCtx->height);
dlg->m_wxh.SetWindowText(wxh);
#ifdef _UNICODE
decoder_name.Format(_T("%s"),A2W(pCodecCtx->codec->long_name));
#else
decoder_name.Format(_T("%s"),pCodecCtx->codec->long_name);
#endif
dlg->m_decoder_name.SetWindowText(decoder_name);
//帧率
framerate_temp=(pFormatCtx->streams[video_stream]->r_frame_rate.num)/(pFormatCtx->streams[video_stream]->r_frame_rate.den);
framerate.Format(_T("%5.2ffps"),framerate_temp);
dlg->m_framerate.SetWindowText(framerate);
switch(pCodecCtx->pix_fmt){
case 0:
pix_fmt.Format(_T("YUV420P"));break;
case 1:
pix_fmt.Format(_T("YUYV422"));break;
case 2:
pix_fmt.Format(_T("RGB24"));break;
case 3:
pix_fmt.Format(_T("BGR24"));break;
case 12:
pix_fmt.Format(_T("PIX_FMT_YUVJ420P"));break;
default:
pix_fmt.Format(_T("UNKNOWN"));
}
dlg->m_pix_fmt.SetWindowText(pix_fmt);
}
//音频解码参数,有音频的时候设置
if(audio_stream!=-1){
#ifdef _UNICODE
decoder_name_au.Format(_T("%s"),A2W(pCodecCtx_au->codec->long_name));
#else
decoder_name_au.Format(_T("%s"),pCodecCtx_au->codec->long_name);
#endif
dlg->m_decoder_name_au.SetWindowText(decoder_name_au);
sample_rate_au.Format(_T("%d"),pCodecCtx_au->sample_rate);
dlg->m_sample_rate_au.SetWindowText(sample_rate_au);
channels_au.Format(_T("%d"),pCodecCtx_au->channels);
dlg->m_channels_au.SetWindowText(channels_au);
}
//显示成以k为单位
bitrate_temp=((float)(pFormatCtx->bit_rate))/1000;
bitrate.Format(_T("%5.2fkbps"),bitrate_temp);
dlg->m_bitrate.SetWindowText(bitrate);
//duration是以微秒为单位
timelong_temp=(pFormatCtx->duration)/1000000;
//转换成hh:mm:ss形式
int tns, thh, tmm, tss;
tns = (pFormatCtx->duration)/1000000;
thh = tns / 3600;
tmm = (tns % 3600) / 60;
tss = (tns % 60);
timelong.Format(_T("%02d:%02d:%02d"),thh,tmm,tss);
dlg->m_timelong.SetWindowText(timelong);
dlg->m_duration.SetWindowText(timelong);
//输入文件的封装格式------
#ifdef _UNICODE
input_format.Format(_T("%s"),A2W(pFormatCtx->iformat->long_name));
#else
input_format.Format(_T("%s"),pFormatCtx->iformat->long_name);
#endif
dlg->m_input_format.SetWindowText(input_format);
//------------------------
//bitrate.Format("%d",pCodecCtx->bit_rate);
//dlg->m_bitrate.SetWindowText(bitrate);
#ifdef _UNICODE
extention.Format(_T("%s"),A2W(pFormatCtx->iformat->extensions));
#else
extention.Format(_T("%s"),pFormatCtx->iformat->extensions);
#endif
dlg->m_extention.SetWindowText(extention);
//MetaData------------------------------------------------------------
//从AVDictionary获得
//需要用到AVDictionaryEntry对象
//CString author,copyright,description;
CString meta=NULL,key,value;
AVDictionaryEntry *m = NULL;
//不用一个一个找出来
/* m=av_dict_get(pFormatCtx->metadata,"author",m,0);
author.Format("作者:%s",m->value);
m=av_dict_get(pFormatCtx->metadata,"copyright",m,0);
copyright.Format("版权:%s",m->value);
m=av_dict_get(pFormatCtx->metadata,"description",m,0);
description.Format("描述:%s",m->value);
*/
//使用循环读出
//(需要读取的数据,字段名称,前一条字段(循环时使用),参数)
while(m=av_dict_get(pFormatCtx->metadata,"",m,AV_DICT_IGNORE_SUFFIX)){
#ifdef _UNICODE
key.Format(_T("%s"),A2W(m->key));
value.Format(_T("%s"),A2W(m->value));
meta.AppendFormat(_T("%s\t:%s\r\n"),key,value);
#else
key.Format(_T("%s"),m->key);
value.Format(_T("%s"),m->value);
meta.AppendFormat(_T("%s\t:%s\r\n"),key,value);
#endif
}
//EditControl换行用\n不行,需要使用\r\n
//除了要用\r\n外,还要都CEdit 的属性进行设置:
//Auto HScroll 设置为 False
//MultiLine 设置为 True
//dlg->m_metadata.SetWindowText(author+"\r\n"+copyright+"\r\n"+description);
dlg->m_metadata.SetWindowText(meta);
//--------------------------------------------------------------------
return 0;
}
//每一帧设置一次
//有一些全局变量
//设置图表
int ve_param_packet(VideoState *is,AVPacket *packet){
//--------------------------------------------------------------------
AVFormatContext *pFormatCtx = is->ic;
int video_stream=is->video_stream;
int audio_stream=is->audio_stream;
AVCodecContext *pCodecCtx = pFormatCtx->streams[video_stream]->codec;
//避免数据太多,超过一定量之后,就会清零--------------------------
if(packet_index>=MAX_PACKET_NUM){
dlg->SystemClear();
}
//自增-----------------------------------------------
packet_index++;
return 0;
}
//视频帧参数提取
int ve_param_vframe(VideoState *is,AVFrame *pFrame,AVPacket *packet){
//--------------------------------------------------------------------
CString key_frame,pict_type,reference,f_index,pts,codednum;
AVFormatContext *pFormatCtx = is->ic;
int video_stream=is->video_stream;
AVCodecContext *pCodecCtx = pFormatCtx->streams[video_stream]->codec;
//避免数据太多,超过一定量之后,就会清零--------------------------
if(vframe_index>=MAX_FRAME_NUM){
dlg->SystemClear();
}
//
//------------------------------
f_index.Format(_T("%d"),vframe_index);
//获取当前记录条数
int nIndex=dlg->videodecode->m_decodeframe_v.GetItemCount();
//“行”数据结构
LV_ITEM lvitem;
lvitem.mask=LVIF_TEXT;
lvitem.iItem=nIndex;
lvitem.iSubItem=0;
//注:vframe_index不可以直接赋值!
//务必使用f_index执行Format!再赋值!
lvitem.pszText=f_index.GetBuffer();
//------------------------
switch(pFrame->key_frame){
case 0:
key_frame.Format(_T("No"));break;
case 1:
key_frame.Format(_T("Yes"));break;
default:
key_frame.Format(_T("Unknown"));
}
switch(pFrame->pict_type){
case 0:
pict_type.Format(_T("Unknown"));break;
case 1:
pict_type.Format(_T("I"));break;
case 2:
pict_type.Format(_T("P"));break;
case 3:
pict_type.Format(_T("B"));break;
case 4:
pict_type.Format(_T("S"));break;
case 5:
pict_type.Format(_T("SI"));break;
case 6:
pict_type.Format(_T("SP"));break;
case 7:
pict_type.Format(_T("BI"));break;
default:
pict_type.Format(_T("Unknown"));
}
reference.Format(_T("%d"),pFrame->reference);
pts.Format(_T("%d"),pFrame->pkt_pts);
codednum.Format(_T("%d"),pFrame->coded_picture_number);
//插入表格------------------------
dlg->videodecode->m_decodeframe_v.InsertItem(&lvitem);
dlg->videodecode->m_decodeframe_v.SetItemText(nIndex,1,pict_type);
dlg->videodecode->m_decodeframe_v.SetItemText(nIndex,2,key_frame);
dlg->videodecode->m_decodeframe_v.SetItemText(nIndex,3,codednum);
dlg->videodecode->m_decodeframe_v.SetItemText(nIndex,4,pts);
dlg->videodecode->m_decodeframe_v.SendMessage(WM_VSCROLL, SB_BOTTOM, NULL);
//准备画!
//视频帧解码单帧分析-----------------
dlg->dfanalysis->qscale_table=(char *)pFrame->qscale_table;
//QP类型
dlg->dfanalysis->qscale_type=pFrame->qscale_type;
//注意:stride要比每行的宏块数+1
dlg->dfanalysis->mb_stride = pCodecCtx->width/16+1;
//宏块总数,最后一行不足一个的按一个算
dlg->dfanalysis->mb_sum = ((pCodecCtx->height+15)>>4)*(pCodecCtx->width/16+1);
dlg->dfanalysis->mb_type = (int *)pFrame->mb_type;
//运动矢量
dlg->dfanalysis->motion_val0 = (short (*)[2])pFrame->motion_val[0];
dlg->dfanalysis->motion_val1 = (short (*)[2])pFrame->motion_val[1];
dlg->dfanalysis->motion_subsample_log2 = pFrame->motion_subsample_log2;
dlg->dfanalysis->mb_width =(pCodecCtx->width+15)>>4;
dlg->dfanalysis->codec_id=pCodecCtx->codec_id;
//--------------------
dlg->dfanalysis->ref_index0 = (char *)pFrame->ref_index[0];
dlg->dfanalysis->ref_index1 = (char *)pFrame->ref_index[1];
//其他相关的参数
dlg->dfanalysis->frame_index = vframe_index;
dlg->dfanalysis->pict_type = pFrame->pict_type;
dlg->dfanalysis->pts = pFrame->pkt_pts;
dlg->dfanalysis->width = pFrame->width;
dlg->dfanalysis->height = pFrame->height;
//数据
dlg->dfanalysis->data[0]=pFrame->data[0];
dlg->dfanalysis->data[1]=pFrame->data[1];
dlg->dfanalysis->data[2]=pFrame->data[2];
dlg->dfanalysis->linesize[0]=pFrame->linesize[0];
dlg->dfanalysis->linesize[1]=pFrame->linesize[1];
dlg->dfanalysis->linesize[2]=pFrame->linesize[2];
dlg->dfanalysis->pix_fmt=pCodecCtx->pix_fmt;
//当解码音频和图片的时候此值为空
if(pFrame->owner!=NULL){
dlg->dfanalysis->refs = pFrame->owner->refs;
}
//精确的时间,以微秒为单位
//PTS*time_base=真正的时间
//注意:time_base指的是AVStream里面的,其他地方也有,但是不能用于计算时间
dlg->dfanalysis->ptime =((float)pFrame->pkt_pts)*((float)pFormatCtx->streams[video_stream]->time_base.num)/((float)pFormatCtx->streams[video_stream]->time_base.den);
//视频帧解码分析-----------------------
//是否开启?
if(dlg->dfanalysis->m_dfanalysisauto.GetCheck()==1){
//获取帧间隔
int interframe=dlg->dfanalysis->m_dfanalysisautointerframenum;
if(vframe_index%interframe==0){
dlg->dfanalysis->DrawPic();
}
}
vframe_index++;
return 0;
}
//音频帧参数提取
int ve_param_aframe(VideoState *is,AVFrame *pFrame,AVPacket *packet){
//--------------------------------------------------------------------
AVFormatContext *pFormatCtx = is->ic;
int audio_stream=is->audio_stream;
AVCodecContext *pCodecCtx = pFormatCtx->streams[audio_stream]->codec;
//避免数据太多,超过一定量之后,就会清零--------------------------
if(aframe_index>=MAX_FRAME_NUM){
dlg->SystemClear();
}
//------------------------------
CString f_index,packet_size,dts,pts;
//---------------
f_index.Format(_T("%d"),aframe_index);
//获取当前记录条数
int nIndex=dlg->audiodecode->m_decodeframe_a.GetItemCount();
//“行”数据结构
LV_ITEM lvitem;
lvitem.mask=LVIF_TEXT;
lvitem.iItem=nIndex;
lvitem.iSubItem=0;
//注:frame_index不可以直接赋值!
//务必使用f_index执行Format!再赋值!
lvitem.pszText=f_index.GetBuffer();
//------------------------
packet_size.Format(_T("%d"),packet->size);
pts.Format(_T("%d"),packet->pts);
dts.Format(_T("%d"),packet->dts);
//---------------
dlg->audiodecode->m_decodeframe_a.InsertItem(&lvitem);
dlg->audiodecode->m_decodeframe_a.SetItemText(nIndex,1,packet_size);
dlg->audiodecode->m_decodeframe_a.SetItemText(nIndex,2,pts);
dlg->audiodecode->m_decodeframe_a.SetItemText(nIndex,3,dts);
dlg->audiodecode->m_decodeframe_a.SendMessage(WM_VSCROLL, SB_BOTTOM, NULL);
aframe_index++;
return 0;
}
//--------------------------------------------------
static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
//往队列里添加Packet(两层)?
static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
{
AVPacketList *pkt1;
if (q->abort_request)
return -1;
//PacketList将Packet组成一个链表
//每个PacketList只有一个Packet
pkt1 = (AVPacketList*)av_malloc(sizeof(AVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL;
if (!q->last_pkt)
//第一个
q->first_pkt = pkt1;
else
//其他情况
q->last_pkt->next = pkt1;
q->last_pkt = pkt1;
q->nb_packets++;
q->size += pkt1->pkt.size + sizeof(*pkt1);
/* XXX: should duplicate packet data in DV case */
SDL_CondSignal(q->cond);
return 0;
}
//往队列里添加Packet
static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
{
int ret;
/* duplicate the packet */
if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
return -1;
SDL_LockMutex(q->mutex);
//往队列里添加Packet(两层)?
ret = packet_queue_put_private(q, pkt);
SDL_UnlockMutex(q->mutex);
if (pkt != &flush_pkt && ret < 0)
av_free_packet(pkt);
return ret;
}
/* packet queue handling */
static void packet_queue_init(PacketQueue *q)
{
memset(q, 0, sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
q->abort_request = 1;
}
static void packet_queue_flush(PacketQueue *q)
{
AVPacketList *pkt, *pkt1;
SDL_LockMutex(q->mutex);
for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
pkt1 = pkt->next;
av_free_packet(&pkt->pkt);
av_freep(&pkt);
}
q->last_pkt = NULL;
q->first_pkt = NULL;
q->nb_packets = 0;
q->size = 0;
SDL_UnlockMutex(q->mutex);
}
static void packet_queue_destroy(PacketQueue *q)
{
packet_queue_flush(q);
SDL_DestroyMutex(q->mutex);
SDL_DestroyCond(q->cond);
}
static void packet_queue_abort(PacketQueue *q)
{
SDL_LockMutex(q->mutex);
q->abort_request = 1;
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
}
static void packet_queue_start(PacketQueue *q)
{
SDL_LockMutex(q->mutex);
q->abort_request = 0;
packet_queue_put_private(q, &flush_pkt);
SDL_UnlockMutex(q->mutex);
}
/* return < 0 if aborted, 0 if no packet and > 0 if packet. */
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
{
AVPacketList *pkt1;
int ret;
SDL_LockMutex(q->mutex);
for (;;) {
if (q->abort_request) {
ret = -1;
break;
}
pkt1 = q->first_pkt;
if (pkt1) {
q->first_pkt = pkt1->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
q->size -= pkt1->pkt.size + sizeof(*pkt1);
*pkt = pkt1->pkt;
av_free(pkt1);
ret = 1;
break;
} else if (!block) {
ret = 0;
break;
} else {
SDL_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
static inline void fill_rectangle(SDL_Surface *screen,
int x, int y, int w, int h, int color)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_FillRect(screen, &rect, color);
}
#define ALPHA_BLEND(a, oldp, newp, s)\
((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
#define RGBA_IN(r, g, b, a, s)\
{\
unsigned int v = ((const uint32_t *)(s))[0];\
a = (v >> 24) & 0xff;\
r = (v >> 16) & 0xff;\
g = (v >> 8) & 0xff;\
b = v & 0xff;\
}
#define YUVA_IN(y, u, v, a, s, pal)\
{\
unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
a = (val >> 24) & 0xff;\
y = (val >> 16) & 0xff;\
u = (val >> 8) & 0xff;\
v = val & 0xff;\
}
#define YUVA_OUT(d, y, u, v, a)\
{\
((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
}
#define BPP 1
static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
{
int wrap, wrap3, width2, skip2;
int y, u, v, a, u1, v1, a1, w, h;
uint8_t *lum, *cb, *cr;
const uint8_t *p;
const uint32_t *pal;
int dstx, dsty, dstw, dsth;
dstw = av_clip(rect->w, 0, imgw);
dsth = av_clip(rect->h, 0, imgh);
dstx = av_clip(rect->x, 0, imgw - dstw);
dsty = av_clip(rect->y, 0, imgh - dsth);
lum = dst->data[0] + dsty * dst->linesize[0];
cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
skip2 = dstx >> 1;
wrap = dst->linesize[0];
wrap3 = rect->pict.linesize[0];
p = rect->pict.data[0];
pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
if (dsty & 1) {
lum += dstx;
cb += skip2;
cr += skip2;
if (dstx & 1) {
YUVA_IN(y, u, v, a, p, pal);
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
cb++;
cr++;
lum++;
p += BPP;
}
for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
YUVA_IN(y, u, v, a, p + BPP, pal);
u1 += u;
v1 += v;
a1 += a;
lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
cb++;
cr++;
p += 2 * BPP;
lum += 2;
}
if (w) {
YUVA_IN(y, u, v, a, p, pal);
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
p++;
lum++;
}
p += wrap3 - dstw * BPP;
lum += wrap - dstw - dstx;
cb += dst->linesize[1] - width2 - skip2;
cr += dst->linesize[2] - width2 - skip2;
}
for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
lum += dstx;
cb += skip2;
cr += skip2;
if (dstx & 1) {
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
p += wrap3;
lum += wrap;
YUVA_IN(y, u, v, a, p, pal);
u1 += u;
v1 += v;
a1 += a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
cb++;
cr++;
p += -wrap3 + BPP;
lum += -wrap + 1;
}
for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
YUVA_IN(y, u, v, a, p + BPP, pal);
u1 += u;
v1 += v;
a1 += a;
lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
p += wrap3;
lum += wrap;
YUVA_IN(y, u, v, a, p, pal);
u1 += u;
v1 += v;
a1 += a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
YUVA_IN(y, u, v, a, p + BPP, pal);
u1 += u;
v1 += v;
a1 += a;
lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
cb++;
cr++;