-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
546 lines (473 loc) · 18.9 KB
/
main.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
#include "MainWindow.h"
#include <QApplication>
#include <QDebug>
#include <QDateTime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//1.注册
av_register_all();
//2.初始化网络流媒体
avformat_network_init();
//3.SDL初始化
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)){
qDebug()<<"SDL_Init failure";
return -1;
}
MainWindow w;
w.show();
return a.exec();
}
////Buffer:
////|-----------|-------------|
////chunk-------pos---len-----|
//static Uint8 *audio_chunk;
//static Uint32 audio_len;
//static Uint8 *audio_pos;
///* Audio Callback
// * The audio function callback takes the following parameters:
// * stream: A pointer to the audio buffer to be filled
// * len: The length (in bytes) of the audio buffer
// *
//*/
//void fill_audio(void *udata,Uint8 *stream,int len){
// //SDL 2.0
// SDL_memset(stream, 0, len);
// if(audio_len==0)
// return;
// len=(len>audio_len?audio_len:len);
// SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
// audio_pos += len;
// audio_len -= len;
//}
//int main(int argc, char* argv[])
//{
// //Init
// if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
// qDebug( "Could not initialize SDL - %s\n", SDL_GetError());
// return -1;
// }
// //SDL_AudioSpec
// SDL_AudioSpec wanted_spec;
// wanted_spec.freq = 32000;
// wanted_spec.format = AUDIO_S16SYS;
// wanted_spec.channels = 1;
// wanted_spec.silence = 0;
// wanted_spec.samples = 1024;
// wanted_spec.callback = fill_audio;
// if (SDL_OpenAudio(&wanted_spec, NULL)<0){
// qDebug("can't open audio.\n");
// return -1;
// }
// FILE *fp=fopen("D:\\1.pcm","rb+");
// if(fp==NULL){
// qDebug("cannot open this file\n");
// return -1;
// }
// int pcm_buffer_size=4096;
// char *pcm_buffer=(char *)malloc(pcm_buffer_size);
// int data_count=0;
// //Play
// SDL_PauseAudio(0);
// while(1){
// if (fread(pcm_buffer, 1, pcm_buffer_size, fp) != pcm_buffer_size){
// // Loop
// fseek(fp, 0, SEEK_SET);
// fread(pcm_buffer, 1, pcm_buffer_size, fp);
// data_count=0;
// }
// qDebug("Now Playing %10d Bytes data.\n",data_count);
// data_count+=pcm_buffer_size;
// //Set audio buffer (PCM data)
// audio_chunk = (Uint8 *) pcm_buffer;
// //Audio buffer length
// audio_len =pcm_buffer_size;
// audio_pos = audio_chunk;
// while(audio_len>0)//Wait until finish
// SDL_Delay(1);
// }
// free(pcm_buffer);
// SDL_Quit();
// return 0;
//}
//#ifdef __cplusplus
//extern "C"
//{
//#include <libavcodec\avcodec.h>
//#include <libavformat\avformat.h>
//#include <libswscale\swscale.h>
//#include <libavutil\avutil.h>
//}
//#endif
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <windows.h>
//#include <excpt.h>
//#include <QDebug>
//#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
//void printAVFormatContext(AVFormatContext *ctx)
//{
// qDebug()<<"ctx->audio_codec:"<<ctx->audio_codec;
// qDebug()<<"ctx->audio_codec_id:"<<ctx->audio_codec_id;
// qDebug()<<"ctx->audio_preload:"<<ctx->audio_preload;
// qDebug()<<"ctx->avio_flags:"<<ctx->avio_flags;
// qDebug()<<"ctx->avoid_negative_ts:"<<ctx->avoid_negative_ts;
// qDebug()<<"ctx->av_class:"<<ctx->av_class;
// qDebug()<<"ctx->bit_rate:"<<ctx->bit_rate;
// qDebug()<<"ctx->codec_whitelist:"<<ctx->codec_whitelist;
// qDebug()<<"ctx->correct_ts_overflow:"<<ctx->correct_ts_overflow;
// qDebug()<<"ctx->ctx_flags:"<<ctx->ctx_flags;
// qDebug()<<"ctx->data_codec:"<<ctx->data_codec;
// qDebug()<<"ctx->data_codec_id:"<<ctx->data_codec_id;
// qDebug()<<"ctx->duration:"<<ctx->duration;
// qDebug()<<"ctx->filename:"<<ctx->filename;
// qDebug()<<"ctx->flags:"<<ctx->flags;
// qDebug()<<"ctx->flush_packets:"<<ctx->flush_packets;
// qDebug()<<"ctx->format_probesize:"<<ctx->format_probesize;
// qDebug()<<"ctx->format_whitelist:"<<ctx->format_whitelist;
// qDebug()<<"ctx->fps_probe_size:"<<ctx->fps_probe_size;
// qDebug()<<"ctx->error_recognition:"<<ctx->error_recognition;
// qDebug()<<"ctx->event_flags:"<<ctx->event_flags;
// qDebug()<<"ctx->iformat:"<<ctx->iformat;
// qDebug()<<"ctx->internal:"<<ctx->internal;
// qDebug()<<"ctx->io_repositioned:"<<ctx->io_repositioned;
// qDebug()<<"ctx->key:"<<ctx->key;
// qDebug()<<"ctx->keylen:"<<ctx->keylen;
// qDebug()<<"ctx->max_analyze_duration:"<<ctx->max_analyze_duration;
// qDebug()<<"ctx->max_chunk_duration:"<<ctx->max_chunk_duration;
// qDebug()<<"ctx->max_chunk_size:"<<ctx->max_chunk_size;
// qDebug()<<"ctx->max_delay:"<<ctx->max_delay;
// qDebug()<<"ctx->max_index_size:"<<ctx->max_index_size;
// qDebug()<<"ctx->max_interleave_delta:"<<ctx->max_interleave_delta;
// qDebug()<<"ctx->max_picture_buffer:"<<ctx->max_picture_buffer;
// qDebug()<<"ctx->max_ts_probe:"<<ctx->max_ts_probe;
// qDebug()<<"ctx->metadata:"<<ctx->metadata;
// qDebug()<<"ctx->metadata_header_padding:"<<ctx->metadata_header_padding;
// qDebug()<<"ctx->nb_chapters:"<<ctx->nb_chapters;
// qDebug()<<"ctx->nb_programs:"<<ctx->nb_programs;
// qDebug()<<"ctx->nb_streams:"<<ctx->nb_streams;
// qDebug()<<"ctx->video_codec_id:"<<ctx->video_codec_id;
// qDebug()<<"ctx->video_codec:"<<ctx->video_codec;
// qDebug()<<"ctx->ts_id:"<<ctx->ts_id;
// qDebug()<<"ctx->subtitle_codec_id:"<<ctx->subtitle_codec_id;
// qDebug()<<"ctx->subtitle_codec:"<<ctx->subtitle_codec;
// qDebug()<<"ctx->strict_std_compliance:"<<ctx->strict_std_compliance;
// qDebug()<<"ctx->start_time_realtime:"<<ctx->start_time_realtime;
// qDebug()<<"ctx->start_time:"<<ctx->start_time;
//}
//int main(int argc, char* argv[])
//{
// qDebug()<<"..................";
// av_register_all();//注册库中所有可用的文件格式和编码器
// char input_file_name[] = "3.mkv";
// AVFormatContext *ic = avformat_alloc_context();
// if(avformat_open_input(&ic,input_file_name,NULL,NULL)!=0)
// {
// qDebug("can't open the file %s\n",input_file_name);
// exit(1);
// }
// //打开输入文件
// if(avformat_find_stream_info(ic,NULL)<0)
// {
// qDebug("can't find suitable codec parameters\n");
// exit(1);
// }//取出流信息
// int i;
// int videoindex=-1;int audioindex=-1;
// for(i=0;i<ic->nb_streams;i++){
// if(ic->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
// videoindex=i;
// //qDebug("video\n");
// }else if(ic->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
// //qDebug("audio\n");
// audioindex=i;
// }
// }
// qDebug()<<"video";
////-----------------------------------------------------------------------------------//
// if(videoindex==-1)
// {
// qDebug("can't find video stream\n");
// exit(1);
// }//没有找到视频流
// AVCodecContext *vCodecCtx = ic->streams[videoindex]->codec;//取得视频流编码上下文指针
// AVCodec *vCodec = avcodec_find_decoder(vCodecCtx->codec_id);
// if(vCodec==NULL)
// {
// qDebug("can't find suitable video decoder\n");
// exit(1);
// }//找到合适的视频解码器
// if(avcodec_open2(vCodecCtx,vCodec,NULL)<0)
// {
// qDebug("can't open the video decoder\n");
// exit(1);
// }//打开该视频解码器
// qDebug()<<"audio";
// //--------------------------------------------------------------------------------//
// if(audioindex==-1)
// {
// qDebug("can't find audio stream\n");
// exit(1);
// }//没有找到音频流
// AVCodecContext *aCodecCtx;
// aCodecCtx=ic->streams[audioindex]->codec;
// AVCodec *aCodec;
// aCodec=avcodec_find_decoder(aCodecCtx->codec_id);
// if(aCodec==NULL)
// {
// qDebug("can't find suitable audio decoder\n");
// exit(1);
// }
// //找到合适的音频解码器
// if(avcodec_open2(aCodecCtx,aCodec,NULL)<0)
// {
// qDebug("can't open the audio decoder\n");
// exit(1);
// }//打开该音频解码器
// //-------------------------------------------------------------------------------------//
// //下面为输出文件处理部分
// qDebug()<<"output";
// char output_file_name[] = "1.mp4";
// AVFormatContext *oc;
// AVCodecContext *oVcc,*oAcc;
// AVCodec *oVc,*oAc;
// AVStream *video_st,*audio_st;
// AVFrame *oVFrame,*oAFrame;
// AVOutputFormat *fmt;
// double video_pts;//////////////////////////////////////////////////////////////////////////////////////no use
// oVFrame=av_frame_alloc();
// oAFrame=av_frame_alloc();
// fmt=av_guess_format(NULL,output_file_name,NULL);
// if(!fmt)
// {
// qDebug("could not deduce output format from outfile extension\n");
// exit(0);
// }//判断是否可以判断输出文件的编码格式
// qDebug()<<"000000000000000000000000000";
// oc=avformat_alloc_context();
// if(!oc)
// {
// qDebug("Memory error\n");
// exit(0);
// }
// oc->oformat=fmt;
// strncpy(oc->filename,output_file_name,sizeof(oc->filename));
//// (oc->priv_data, "preset", "superfast", 0);
//// av_opt_set(oc->priv_data, "tune", "zerolatency", 0);
////////////////////////////////视频
// qDebug()<<"==============output video============";
// qDebug()<<"fmt->video_codec:"<<fmt->video_codec;
// oVc=avcodec_find_encoder(fmt->video_codec);
// oVcc = avcodec_alloc_context3(oVc);
// oVcc->codec_id = fmt->video_codec;//会出现卡死现象,没有分配空间
// oVcc->codec_type=AVMEDIA_TYPE_VIDEO;
// oVcc->bit_rate=400000;
// oVcc->width=vCodecCtx->width;
// oVcc->height=vCodecCtx->height;
// video_st=avformat_new_stream(oc,oVc);
// if(!video_st)
// {
// qDebug("could not alloc video stream\n");
// exit(0);
// }
// oVcc->time_base.den = ic->streams[videoindex]->r_frame_rate.num;
// oVcc->time_base.num = ic->streams[videoindex]->r_frame_rate.den;
// oVcc->gop_size=vCodecCtx->gop_size;
// oVcc->pix_fmt=vCodecCtx->pix_fmt;
// oVcc->max_b_frames=vCodecCtx->max_b_frames;
// //video_st->r_frame_rate=ic->streams[videoindex]->r_frame_rate;
// oVcc->flags|= CODEC_FLAG_GLOBAL_HEADER;/////////////////////这样一个标志没注明,才不会导致报错没有使用global headers
///////////////////////////////////////音频
// qDebug()<<"fmt->audio_codec:"<<fmt->audio_codec;
// oAc=avcodec_find_encoder(fmt->audio_codec);
// oAcc=avcodec_alloc_context3(oAc);
// oAcc->codec_id = fmt->audio_codec;
// oAcc->codec_type =AVMEDIA_TYPE_AUDIO;
//// oAcc->bit_rate = ic->streams[audioindex]->codec->bit_rate;//aCodecCtx->bit_rate;
// oAcc->sample_fmt = ic->streams[audioindex]->codec->sample_fmt;
// oAcc->channel_layout = ic->streams[audioindex]->codec->channel_layout;
// oAcc->sample_rate = ic->streams[audioindex]->codec->sample_rate;//aCodecCtx->sample_rate;
// oAcc->channels = ic->streams[audioindex]->codec->channels;
// oAcc->flags|= CODEC_FLAG_GLOBAL_HEADER;/////////////////////这样一个标志没注明,才不会导致报错没有使用global headers
// audio_st=avformat_new_stream(oc,oAc);
// if(!audio_st)
// {
// qDebug("could not alloc audio stream\n");
// exit(0);
// }
//// av_dump_format(oc, 0, oc->filename, 1);
//// oc->bit_rate = 401582;
//// oc->duration = 307400000;
//// oc->iformat = ic->iformat;
//// oc->metadata = ic->metadata;
//// qDebug()<<"--------------ic-----------";
//// printAVFormatContext(ic);
//// qDebug()<<"--------------oc-----------";
//// printAVFormatContext(oc);
//// int ret1 = avformat_write_header(oc,NULL) ;
//// if (ret1 < 0)
//// {
//// qDebug( "Invalid output format parameters %d",ret1);
//// exit(0);
//// }//设置必要的输出参数
//// strcpy(oc->,ic->title);
//// strcpy(oc->author,ic->author);
//// strcpy(oc->copyright,ic->copyright);
//// strcpy(oc->comment,ic->comment);
//// strcpy(oc->album,ic->album);
//// oc->year=ic->year;
//// oc->track=ic->track;
//// strcpy(oc->genre,ic->genre);
//// dump_format(oc,0,output_file_name,1);//列出输出文件的相关流信息
// qDebug("ic->streams[audioindex]->codec->bit_rate = %d\n",ic->streams[audioindex]->codec->bit_rate);
// qDebug("aCodecCtx->sample_rate = %d\n",aCodecCtx->sample_rate);
// qDebug("ic->streams[videoindex]->r_frame_rate.den %d\n",ic->streams[videoindex]->r_frame_rate.den);
// qDebug("ic->streams[videoindex]->r_frame_rate.num %d\n",ic->streams[videoindex]->r_frame_rate.num);
// //-------------------------------------------------------------------------------
// if(avcodec_open2(oVcc,oVc,NULL)<0)
// {
// qDebug("can't open the output video codec\n");
// exit(0);
// }//打开视频编码器
// if(avcodec_open2(oAcc,oAc,NULL)<0)
// {
// qDebug("can't open the output audio codec");
// exit(0);
// }//打开音频编码器
// if (!(oc->flags & AVFMT_FLAG_NOFILLIN))
// {
// if(avio_open2(&oc->pb,output_file_name,AVIO_FLAG_WRITE,NULL,NULL)<0)
// {
// qDebug("can't open the output file %s\n",output_file_name);
// exit(0);
// }//打开输出文件
// qDebug()<<"open the output file success";
// }
// if(!oc->nb_streams)
// {
// qDebug("output file dose not contain any stream\n");
// exit(0);
// }//查看输出文件是否含有流信息
// if(avformat_write_header(oc,NULL)<0)
// {
// qDebug("Could not write header for output file\n");
// exit(1);
// }
// AVPacket packet;
// av_init_packet(&packet);
// uint8_t *ptr;
// uint8_t *out_buf=NULL;
// static short *samples=NULL;
// static unsigned int samples_size=0;
// /////////////////////分配音视频输出缓冲区大小/////////////////////
// uint8_t *video_outbuf,*audio_outbuf;
// int video_outbuf_size,audio_outbuf_size;
// video_outbuf_size=400000;
// video_outbuf= (uint8_t *) malloc(video_outbuf_size);
// audio_outbuf_size = FF_MIN_BUFFER_SIZE;
// audio_outbuf = (uint8_t *)av_malloc(audio_outbuf_size);
// int out_size=AVCODEC_MAX_AUDIO_FRAME_SIZE;
// uint8_t * inbuf = (uint8_t *)malloc(out_size);////////////////////////////////////////
// int flag;
// int frameFinished;
// int len=0;
// int frame_index=0,ret=0;
// int inputSampleSize = aCodecCtx->frame_size * 2 *aCodecCtx->channels;//获取Sample大小
// int outputSampleSize = oAcc->frame_size * 2 * oAcc->channels;//获取输出Sample大小////////////////////////////////no use
// int j=0;
// qDebug()<<"3333333333333333333333";
// while(av_read_frame(ic,&packet)>=0)//从输入文件中读取一个包
// {
// if(packet.stream_index==videoindex)//判断是否为当前视频流中的包
// {
// len=avcodec_decode_video2(vCodecCtx,oVFrame,&frameFinished,&packet);//若为视频包,解码该视频包
// if(len<0)
// {
// qDebug("Error while decoding\n");
// exit(0);
// }
// if(frameFinished)//判断视频祯是否读完
// {
// fflush(stdout);
// AVPacket pkt;
// av_init_packet(&pkt);
// oVFrame->pts=av_rescale(frame_index,AV_TIME_BASE*(int64_t)oVcc->time_base.num,oVcc->time_base.den);
// int got_packet = 0;
// out_size = avcodec_encode_video2(oVcc, &pkt, oVFrame,&got_packet);
// if (out_size >= 0)
// {
//// if(oVcc->coded_frame && oVcc->coded_frame->key_frame)
//// pkt.flags |= PKT_FLAG_KEY;
//// pkt.flags = packet.flags;
// pkt.stream_index= video_st->index;
//// pkt.data= video_outbuf;
//// pkt.size= out_size;
// //ret=av_write_frame(oc, &pkt);
// if((ret=av_interleaved_write_frame(oc,&pkt))!=0)
// {
// qDebug("Fail to write the video frame #%d.\n",frame_index);
// }
// frame_index++;
// }
// }else{
// //ret=av_write_frame(oc, &packet);
// ret=av_interleaved_write_frame(oc,&packet);
// if(ret!=0)
// {
// qDebug("while write video frame error\n");
// exit(0);
// }
// }
// }
// else if(packet.stream_index==audioindex)
// {
// while (len>0)
// {
// out_size = 0;
// if((ret = avcodec_decode_audio4(aCodecCtx,oAFrame,&out_size,&packet))<0)
// {
// qDebug("while decode audio failure\n");
// exit(0);
// }
// if (out_size>0)
// {
// AVPacket pkt;
// av_init_packet(&pkt);
// int got_packet = 0;
// pkt.size= avcodec_encode_audio2(oAcc, &pkt, oAFrame,&got_packet);
// if(got_packet>=0){
// if (oAcc->coded_frame && oAcc->coded_frame->pts != AV_NOPTS_VALUE)
// pkt.pts= av_rescale_q(oAcc->coded_frame->pts, oAcc->time_base, audio_st->time_base);
// pkt.flags |= AV_PKT_FLAG_KEY;
// pkt.stream_index= audio_st->index;
// //* write the compressed frame in the media file
// if (av_write_frame(oc, &pkt) != 0)
// {
// qDebug("Error while writing audio frame\n");
// break;
// }
// }
// }
// }
// }
// // Free the packet that was allocated by av_read_frame
// av_free_packet(&packet);
// j++;
// //qDebug(" 第%d帧 ",j);
// }
// av_write_trailer(oc);
// for(i = 0; i < oc->nb_streams; i++)
// {
// av_freep(&oc->streams[i]->codec);
// av_freep(&oc->streams[i]);
// }
// avio_close(oc->pb);
// av_free(oc);
// av_free(oVFrame);
// av_free(out_buf);
// avcodec_close(vCodecCtx);
// avcodec_close(aCodecCtx);
// avformat_close_input(&ic);
// return 0;
//}