/
my_example.cpp
912 lines (855 loc) · 31.2 KB
/
my_example.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
#include <iostream>
#include <fstream>
//#define NOVIDEO //不解码视频
//#define NOSAVEYUV //不保存YUV
//#define SWSCALE //视频帧转换,需禁用NOVIDEO和HWDECODE
//#define NOAUDIO //不解码音频
//#define NOSAVEPCM //不保存PCM
//#define RESAMPLE //音频重采样,需禁用NOAUDIO
//#define AVIO //使用AVIO
//#define ENCODE //编码,需禁用NOVIDEO或者NOAUDIO,视频只在禁用HWDECODE下做了编码
//#define REMUX //转封装
//#define MUXING //封装,需打开ENCODE
#define HWDECODE //硬解码
#ifdef __cplusplus
extern "C"
{
#endif
// FFmpeg 头文件
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libswscale/swscale.h> // sws_cale
#include <libswresample/swresample.h> // swr_alloc_set_opts
#include <libavutil/file.h> // av_file_map
#include <libavutil/imgutils.h> // av_image_alloc
#include <libavutil/opt.h> // av_opt_set
#ifdef __cplusplus
}
// C++中使用av_err2str宏
#ifdef av_err2str
#undef av_err2str
#endif
char av_error[AV_ERROR_MAX_STRING_SIZE] = { 0 };
#define av_err2str(errnum) \
av_make_error_string(av_error, AV_ERROR_MAX_STRING_SIZE, errnum)
#endif
// 自定义参数,传递内存buf和大小
typedef struct __BUFER_DATA__
{
uint8_t* buf;
int size;
}Bufdata;
// 自定义文件读操作
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
Bufdata* bd = static_cast<Bufdata*>(opaque);
buf_size = FFMIN(buf_size, bd->size);
if (buf_size == 0)
{
return AVERROR_EOF;
}
memcpy(buf, bd->buf, buf_size);
bd->buf += buf_size;
bd->size -= buf_size;
return buf_size;
}
int main_my_example(int argc, char* argv[])
{
// DECODE
AVFormatContext* fmt_ctx = nullptr;
AVDictionaryEntry* dic = nullptr;
AVCodecContext *vcodectx = nullptr, *acodectx = nullptr;
AVCodecParameters *vcodecpar = nullptr, *acodecpar = nullptr;
AVCodec *vcodec = nullptr, *acodec = nullptr;
AVPacket* pkt = nullptr;
AVFrame* frame = nullptr;
uint8_t* pt[4] = { 0 };
int lz[4] = { 0 };
int s = 0;
std::ofstream out_yuv, out_hw, out_pcm, out_bgr, out_pcm2, out_h264, out_mp3;
const char* in = "in.flv";
int vindex = -1, aindex = -1;
int ret = 0;
// avio
uint8_t *buf = nullptr, *aviobuf = nullptr;
size_t size = 0;
Bufdata bd = { 0 };
AVIOContext* avioctx = nullptr;
// swscale
SwsContext* swsctx = nullptr;
uint8_t* pointers[4] = { 0 };
int linesizes[4] = { 0 };
// resample
SwrContext* swrctx = nullptr;
int samplessize = 0;
uint8_t * sambuf = nullptr;
// ENCODE
AVCodecContext *ovcodectx = nullptr, *oacodectx = nullptr;
AVCodec *ovcodec = nullptr, *oacodec = nullptr;
AVDictionary* param = nullptr;
AVPacket* opkt = nullptr;
// REMUX
AVFormatContext* ofmt_ctx = nullptr;
AVStream *ovstream = nullptr, *oastream = nullptr, *streamtmp = nullptr;
// MUXING
AVFormatContext* ofmt_ctx2 = nullptr;
AVStream *ovstream2 = nullptr, *oastream2 = nullptr;
// HWDECODE
AVBufferRef* hwbufref = nullptr;
AVFrame* hwframe = nullptr;
uint8_t* hwframebuf = nullptr;
int hwbufsize = 0;
out_yuv.open("out.yuv", std::ios::binary | std::ios::trunc);
out_hw.open("out2.yuv", std::ios::binary | std::ios::trunc);
out_pcm.open("out.pcm", std::ios::binary | std::ios::trunc);
out_bgr.open("out.bgr", std::ios::binary | std::ios::trunc);
out_pcm2.open("out2.pcm", std::ios::binary | std::ios::trunc);
out_h264.open("out.h264", std::ios::binary | std::ios::trunc);
out_mp3.open("out.mp3", std::ios::binary | std::ios::trunc);
if (!out_yuv.is_open() || !out_hw.is_open() || !out_pcm.is_open() || !out_bgr.is_open() || !out_pcm2.is_open() || !out_h264.is_open() || !out_mp3.is_open())
{
std::cerr << "创建/打开输出文件失败" << std::endl;
goto END;
}
// 日志
av_log_set_level(AV_LOG_ERROR);
// 打开输入
#ifdef AVIO
// 内存映射
ret = av_file_map("in.mkv", &buf, &size, 0, nullptr);
if (ret < 0)
{
std::cerr << "av_file_map err : " << av_err2str(ret) << std::endl;
goto END;
}
fmt_ctx = avformat_alloc_context();
if (fmt_ctx == nullptr)
{
std::cerr << "avformat_alloc_context err" << std::endl;
goto END;
}
aviobuf = static_cast<uint8_t*>(av_malloc(1024));
if (aviobuf == nullptr)
{
std::cerr << "av_malloc err" << std::endl;
goto END;
}
bd.buf = buf;
bd.size = size;
avioctx = avio_alloc_context(aviobuf, 1024, 0, &bd, read_packet, nullptr, nullptr); if (avioctx == nullptr) { std::cerr << "avio_alloc_context err" << std::endl;
goto END; } fmt_ctx->pb = avioctx;
ret = avformat_open_input(&fmt_ctx, nullptr, nullptr, nullptr);
if (ret < 0)
{
std::cerr << "avformat_open_input err : " << av_err2str(ret) << std::endl;
goto END;
}
#else
ret = avformat_open_input(&fmt_ctx, in, nullptr, nullptr);
if (ret < 0)
{
std::cerr << "avformat_open_input err : " << av_err2str(ret) << std::endl;
goto END;
}
#endif // AVIO
std::cout << "get metadata : " << std::endl;
while ((dic = av_dict_get(fmt_ctx->metadata, "", dic, AV_DICT_IGNORE_SUFFIX)) != nullptr)
{
std::cerr << dic->key << " : " << dic->value << std::endl;
}
// 查找流信息,对输入进行预处理
ret = avformat_find_stream_info(fmt_ctx, nullptr);
if (ret < 0)
{
std::cerr << "avformat_find_stream_info err : " << av_err2str(ret) << std::endl;
goto END;
}
// 打印输入信息
av_dump_format(fmt_ctx, 0, fmt_ctx->url, 0);
// 查找流
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &vcodec, 0);
if (ret < 0)
{
std::cerr << "av_find_best_stream err : " << av_err2str(ret) << std::endl;
}
vindex = ret;
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &acodec, 0);
if (ret < 0)
{
std::cerr << "av_find_best_stream err : " << av_err2str(ret) << std::endl;
}
aindex = ret;
// 查找解码器
if (vindex != -1)
{
// 准备打开解码器
vcodecpar = fmt_ctx->streams[vindex]->codecpar;
vcodectx = avcodec_alloc_context3(vcodec);
ret = avcodec_parameters_to_context(vcodectx, vcodecpar);// 参数拷贝
if (ret < 0)
{
std::cerr << "avcodec_parameters_to_context err : " << av_err2str(ret) << std::endl;
goto END;
}
#ifdef HWDECODE
// 查询硬解码支持
std::cout << "support hwdecode : " << std::endl;
auto type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);;
for (; type != AV_HWDEVICE_TYPE_NONE; type = av_hwdevice_iterate_types(type))
{
std::cout << av_hwdevice_get_type_name(type) << std::endl;
}
for (int i = 0;; i++)
{
const AVCodecHWConfig* config = avcodec_get_hw_config(vcodec, i);
if (config == nullptr)
{
std::cerr << "not support" << std::endl;
goto END;
}
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
config->device_type == AV_HWDEVICE_TYPE_DXVA2)
{
// 支持AV_HWDEVICE_TYPE_DXVA2
break;
}
}
// 硬解上下文
ret = av_hwdevice_ctx_create(&hwbufref, AV_HWDEVICE_TYPE_DXVA2, nullptr, nullptr, 0);
if (ret < 0)
{
std::cerr << "av_hwdevice_ctx_create err : " << av_err2str(ret) << std::endl;
goto END;
}
vcodectx->hw_device_ctx = av_buffer_ref(hwbufref);
if (vcodectx->hw_device_ctx == nullptr)
{
std::cerr << "av_buffer_ref err" << std::endl;
goto END;
}
// 硬解帧结构
hwframe = av_frame_alloc();
if (hwframe == nullptr)
{
std::cerr << "av_frame_alloc err" << std::endl;
goto END;
}
hwbufsize = av_image_get_buffer_size(AV_PIX_FMT_NV12/*假设是输出nv12*/, vcodectx->width, vcodectx->height, 1);
if (hwbufsize < 0)
{
std::cerr << "av_image_get_buffer_size err : " << av_err2str(ret) << std::endl;
goto END;
}
hwframebuf = static_cast<uint8_t*>(av_malloc(hwbufsize));
if (hwframebuf == nullptr)
{
std::cerr << "av_malloc err : " << std::endl;
goto END;
}
#endif // HWDECODE
// 打开解码器
ret = avcodec_open2(vcodectx, vcodec, nullptr);
if (ret < 0)
{
std::cerr << "avcodec_open2 err : " << av_err2str(ret) << std::endl;
goto END;
}
}
if (aindex != -1)
{
// 准备打开解码器
acodecpar = fmt_ctx->streams[aindex]->codecpar;
acodectx = avcodec_alloc_context3(acodec);
ret = avcodec_parameters_to_context(acodectx, acodecpar);// 参数拷贝
if (ret < 0)
{
std::cerr << "avcodec_parameters_to_context err : " << av_err2str(ret) << std::endl;
goto END;
}
// 打开解码器
ret = avcodec_open2(acodectx, acodec, nullptr);
if (ret < 0)
{
std::cerr << "avcodec_open2 err : " << av_err2str(ret) << std::endl;
goto END;
}
}
// 创建AVPacket
pkt = av_packet_alloc();
if (pkt == nullptr)
{
std::cerr << "av_packet_alloc err" << std::endl;
goto END;
}
av_init_packet(pkt);
// 创建AVFrame
frame = av_frame_alloc();
if (frame == nullptr)
{
std::cerr << "av_frame_alloc err" << std::endl;
goto END;
}
// 申请保存解码帧的内存
ret = av_image_alloc(pt, lz, vcodectx->width, vcodectx->height, vcodectx->pix_fmt, 1);
if (ret < 0)
{
std::cerr << "av_image_alloc err : " << av_err2str(ret) << std::endl;
goto END;
}
// 记录内存大小
s = ret;
#ifdef SWSCALE
// 创建转换上下文
swsctx = sws_getContext(vcodectx->width, vcodectx->height, AV_PIX_FMT_YUV420P, 320, 240, AV_PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr);
if (swsctx == nullptr)
{
std::cerr << "sws_getContext err" << std::endl;
goto END;
}
// 分配内存空间
// ffmpe里很多汇编优化,它一次读取或写入的数据可能比你想象中的要多(某些对齐要求),所以ffmpeg操作的内存区域,一般都应该用av_malloc分配,这个函数通常分配的内存会比你要求的多,就是为了应付这些场景
ret = av_image_alloc(pointers, linesizes, 320, 240, AV_PIX_FMT_RGB24, 16);
if (ret < 0)
{
std::cerr << "av_image_alloc err : " << av_err2str(ret) << std::endl;
goto END;
}
#endif // SWSCALE
#ifdef RESAMPLE
// 创建转换上下文
swrctx = swr_alloc_set_opts(NULL, av_get_default_channel_layout(acodectx->channels), AV_SAMPLE_FMT_S16,
acodectx->sample_rate, av_get_default_channel_layout(acodectx->channels), acodectx->sample_fmt,
acodectx->sample_rate, 0, NULL);
if (swrctx == nullptr)
{
std::cerr << "swr_alloc_set_opts" << std::endl;
goto END;
}
// 初始化转换上下文
ret = swr_init(swrctx);
if (ret < 0)
{
std::cerr << "swr_init err : " << av_err2str(ret) << std::endl;
goto END;
}
//计算1s的数据大小,使缓冲区足够大
samplessize = av_samples_get_buffer_size(nullptr, acodectx->channels, acodectx->sample_rate, AV_SAMPLE_FMT_S16, 1);
if (samplessize < 0)
{
std::cerr << "av_samples_get_buffer_size err : " << av_err2str(samplessize) << std::endl;
goto END;
}
sambuf = static_cast<uint8_t*>(av_mallocz(samplessize));
if (sambuf == nullptr)
{
std::cerr << "av_mallocz err" << std::endl;
goto END;
}
#endif // RESAMPLE
#ifdef ENCODE
//---ENCODEVIDEO
// 查找编码器
ovcodec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (ovcodec == nullptr)
{
std::cerr << "avcodec_find_encoder AV_CODEC_ID_H264 err" << std::endl;
goto END;
}
ovcodectx = avcodec_alloc_context3(ovcodec);
if (ovcodectx == nullptr)
{
std::cerr << "avcodec_alloc_context3 err" << std::endl;
goto END;
}
// 设置参数
ovcodectx->bit_rate = vcodectx->bit_rate == 0 ? 850000 : vcodectx->bit_rate;
ovcodectx->width = vcodectx->width;
ovcodectx->height = vcodectx->height;
ovcodectx->time_base = { 1, 25 };
ovcodectx->framerate = vcodectx->framerate;
ovcodectx->gop_size = vcodectx->gop_size;
ovcodectx->max_b_frames = vcodectx->max_b_frames;
ovcodectx->pix_fmt = AV_PIX_FMT_YUV420P;
// --preset的参数主要调节编码速度和质量的平衡,有ultrafast、superfast、veryfast、faster、fast、medium、slow、slower、veryslow、placebo这10个选项,从快到慢。
ret = av_dict_set(¶m, "preset", "medium", 0);
if (ret < 0)
{
std::cerr << "av_opt_set err : " << av_err2str(ret) << std::endl;
goto END;
}
ret = av_dict_set(¶m, "tune", "zerolatency", 0); //实现实时编码,有效降低输出大小
if (ret < 0)
{
std::cerr << "av_opt_set err : " << av_err2str(ret) << std::endl;
goto END;
}
//ret = av_dict_set(¶m, "profile", "main", 0);
//if (ret < 0)
//{
// std::cerr << "av_opt_set err : " << av_err2str(ret) << std::endl;
// goto END;
//}
ret = avcodec_open2(ovcodectx, ovcodec, ¶m);
if (ret < 0)
{
std::cerr << "avcodec_open2 err : " << av_err2str(ret) << std::endl;
goto END;
}
//ENCODEVIDEO---
//---ENCODEAUDIO
// 查找编码器
oacodec = avcodec_find_encoder(AV_CODEC_ID_MP3);
if (oacodec == nullptr)
{
std::cerr << "avcodec_find_encoder AV_CODEC_ID_MP3 err" << std::endl;
goto END;
}
oacodectx = avcodec_alloc_context3(oacodec);
if (oacodectx == nullptr)
{
std::cerr << "avcodec_alloc_context3 err" << std::endl;
goto END;
}
// 设置参数
oacodectx->bit_rate = acodectx->bit_rate;
oacodectx->sample_fmt = acodectx->sample_fmt;
oacodectx->sample_rate = acodectx->sample_rate;
oacodectx->channel_layout = acodectx->channel_layout;
oacodectx->channels = acodectx->channels;
ret = avcodec_open2(oacodectx, oacodec, nullptr);
if (ret < 0)
{
std::cerr << "avcodec_open2 err : " << av_err2str(ret) << std::endl;
goto END;
}
//ENCODEAUDIO---
opkt = av_packet_alloc();
if (opkt == nullptr)
{
std::cerr << "av_packet_alloc err" << std::endl;
goto END;
}
av_init_packet(opkt);
#endif // ENCODE
#ifdef REMUX
// 创建输出
ret = avformat_alloc_output_context2(&ofmt_ctx, nullptr, nullptr, "out.mp4");
if (ret < 0)
{
std::cerr << "avformat_alloc_output_context2 err : " << av_err2str(ret) << std::endl;
goto END;
}
//创建流
ovstream = avformat_new_stream(ofmt_ctx, nullptr);
oastream = avformat_new_stream(ofmt_ctx, nullptr);
if (ovstream == nullptr || oastream == nullptr)
{
std::cerr << "avformat_new_stream err" << std::endl;
goto END;
}
//复制配置信息
ret = avcodec_parameters_from_context(ovstream->codecpar, vcodectx);
if (ret < 0)
{
std::cerr << "avcodec_parameters_from_context err : " << av_err2str(ret) << std::endl;
goto END;
}
ret = avcodec_parameters_from_context(oastream->codecpar, acodectx);
if (ret < 0)
{
std::cerr << "avcodec_parameters_from_context err : " << av_err2str(ret) << std::endl;
goto END;
}
av_dump_format(ofmt_ctx, 0, ofmt_ctx->url, 1);
// 标记不需要重新编解码
ovstream->codecpar->codec_tag = 0;
oastream->codecpar->codec_tag = 0;
// 打开io
if (!(ofmt_ctx->flags & AVFMT_NOFILE))
{
// Demuxer will use avio_open, no opened file should be provided by the caller./
ret = avio_open(&ofmt_ctx->pb, "out.mp4", AVIO_FLAG_WRITE);
if (ret < 0)
{
std::cerr << "avio_open err : " << av_err2str(ret) << std::endl;
goto END;
}
}
// 写文件头
ret = avformat_write_header(ofmt_ctx, nullptr);
if (ret < 0)
{
std::cerr << "avformat_write_header err : " << av_err2str(ret) << std::endl;
goto END;
}
#endif // REMUX
#ifdef MUXING
// 创建输出
ret = avformat_alloc_output_context2(&ofmt_ctx2, nullptr, nullptr, "out2.mp4");
if (ret < 0)
{
std::cerr << "avformat_alloc_output_context2 err : " << av_err2str(ret) << std::endl;
goto END;
}
//创建流
ovstream2 = avformat_new_stream(ofmt_ctx2, nullptr);
oastream2 = avformat_new_stream(ofmt_ctx2, nullptr);
if (ovstream2 == nullptr || oastream2 == nullptr)
{
std::cerr << "avformat_new_stream err" << std::endl;
goto END;
}
//复制配置信息
ret = avcodec_parameters_from_context(ovstream2->codecpar, ovcodectx);
if (ret < 0)
{
std::cerr << "avcodec_parameters_from_context err : " << av_err2str(ret) << std::endl;
goto END;
}
ret = avcodec_parameters_from_context(oastream2->codecpar, oacodectx);
if (ret < 0)
{
std::cerr << "avcodec_parameters_from_context err : " << av_err2str(ret) << std::endl;
goto END;
}
av_dump_format(ofmt_ctx2, 0, ofmt_ctx2->url, 1);
// 标记不需要重新编解码
ovstream2->codecpar->codec_tag = 0;
oastream2->codecpar->codec_tag = 0;
// 打开io
if (!(ofmt_ctx2->flags & AVFMT_NOFILE))
{
ret = avio_open(&ofmt_ctx2->pb, "out2.mp4", AVIO_FLAG_WRITE);
if (ret < 0)
{
std::cerr << "avio_open err : " << av_err2str(ret) << std::endl;
goto END;
}
}
// 写文件头
ret = avformat_write_header(ofmt_ctx2, nullptr);
if (ret < 0)
{
std::cerr << "avformat_write_header err : " << av_err2str(ret) << std::endl;
goto END;
}
#endif // MUXING
// 从输入读取数据
while (av_read_frame(fmt_ctx, pkt) >= 0)
{
if (pkt->stream_index == vindex)
{
#ifndef NOVIDEO
// 解码视频帧
ret = avcodec_send_packet(vcodectx, pkt);
if (ret < 0)
{
std::cerr << "avcodec_send_packet err : " << av_err2str(ret) << std::endl;
break;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(vcodectx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0)
{
std::cerr << "avcodec_receive_frame err : " << av_err2str(ret) << std::endl;
break;
}
else
{
// 得到解码数据
if (frame->format == AV_PIX_FMT_YUV420P)
{
#ifndef NOSAVEYUV
//out_yuv.write(reinterpret_cast<const char*>(frame->data[0]), frame->linesize[0] * frame->height);
//out_yuv.write(reinterpret_cast<const char*>(frame->data[1]), frame->linesize[1] * frame->height / 2);
//out_yuv.write(reinterpret_cast<const char*>(frame->data[2]), frame->linesize[2] * frame->height / 2);
// 这种方式可以自动去除画面右边多余数据
av_image_copy(pt, lz,
(const uint8_t* *)frame->data, frame->linesize,
static_cast<AVPixelFormat>(frame->format), frame->width, frame->height);
out_yuv.write(reinterpret_cast<const char*>(pt[0]), s);
#endif // NOSAVEYUV
#ifdef SWSCALE
// 视频帧格式转换
ret = sws_scale(swsctx, frame->data, frame->linesize, 0, frame->height, pointers, linesizes);
if (ret <= 0)
{
std::cerr << "sws_scale err : " << av_err2str(ret) << std::endl;
break;
}
// 翻转
pointers[0] += linesizes[0] * (ret - 1);
linesizes[0] *= -1;
out_bgr.write(reinterpret_cast<const char*>(pointers[0]), linesizes[0] * ret);
#endif // SWSCALE
#ifdef ENCODE
ret = avcodec_send_frame(ovcodectx, frame);
if (ret < 0)
{
std::cerr << "avcodec_send_frame err : " << av_err2str(ret) << std::endl;
break;
}
while (ret >= 0)
{
ret = avcodec_receive_packet(ovcodectx, opkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0)
{
std::cerr << "avcodec_receive_packet err : " << av_err2str(ret) << std::endl;
break;
}
else
{
// 得到编码数据
out_h264.write(reinterpret_cast<const char*>(opkt->data), opkt->size);
#ifdef MUXING
opkt->pts = av_rescale_q(opkt->pts, fmt_ctx->streams[vindex]->time_base, ovstream2->time_base);
opkt->dts = av_rescale_q(opkt->dts, fmt_ctx->streams[vindex]->time_base, ovstream2->time_base);
opkt->duration = av_rescale_q(opkt->duration, fmt_ctx->streams[vindex]->time_base, ovstream2->time_base);
opkt->pos = -1;
opkt->stream_index = 0;
ret = av_interleaved_write_frame(ofmt_ctx2, opkt);
if (ret < 0)
{
std::cerr << "av_interleaved_write_frame err : " << av_err2str(ret) << std::endl;
}
#endif // MUXING
av_packet_unref(opkt);
}
}
#endif // ENCODE
}
#ifdef HWDECODE
else if (frame->format == AV_PIX_FMT_DXVA2_VLD/*AV_HWDEVICE_TYPE_DXVA2对应的输出格式*/)
{
ret = av_hwframe_transfer_data(hwframe, frame, 0);
if (ret < 0)
{
std::cerr << "av_hwframe_transfer_data err : " << av_err2str(ret) << std::endl;
break;
}
ret = av_image_copy_to_buffer(static_cast<uint8_t*>(hwframebuf), hwbufsize,
(const uint8_t * const*)hwframe->data,
hwframe->linesize, static_cast<AVPixelFormat>(hwframe->format),
hwframe->width, hwframe->height, 1);
if (ret <= 0)
{
std::cerr << "av_image_copy_to_buffer err : " << av_err2str(ret) << std::endl;
break;
}
out_hw.write(reinterpret_cast<const char*>(hwframebuf), ret);
}
#endif // HWDECODE
}
}
#endif // NOVIDEO
}
else if (pkt->stream_index == aindex)
{
#ifndef NOAUDIO
// 解码音频帧
ret = avcodec_send_packet(acodectx, pkt);
if (ret < 0)
{
std::cerr << "avcodec_send_packet err : " << av_err2str(ret) << std::endl;
break;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(acodectx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0)
{
std::cerr << "avcodec_receive_frame err : " << av_err2str(ret) << std::endl;
break;
}
else
{
// 得到解码数据
if (frame->format == AV_SAMPLE_FMT_FLTP)
{
#ifndef NOSAVEPCM
auto size = av_get_bytes_per_sample(static_cast<AVSampleFormat>(frame->format));
for (int i = 0; i < frame->nb_samples; ++i)
{
for (int j = 0; j < frame->channels; ++j)
{
out_pcm.write(reinterpret_cast<const char*>(frame->data[j] + size * i), size);
}
}
#ifdef RESAMPLE
//转换,返回每个通道的样本数
ret = swr_convert(swrctx, &sambuf, samplessize, (const uint8_t **)frame->data, frame->nb_samples);
if (ret < 0)
{
std::cerr << "swr_convert err : " << av_err2str(ret) << std::endl;
break;
}
out_pcm2.write(reinterpret_cast<const char*>(sambuf),
av_samples_get_buffer_size(nullptr, frame->channels, ret, AV_SAMPLE_FMT_S16, 1));
#endif // RESAMPLE
#endif // NOSAVEPCM
#ifdef ENCODE
ret = avcodec_send_frame(oacodectx, frame);
if (ret < 0)
{
std::cerr << "avcodec_send_frame err : " << av_err2str(ret) << std::endl;
break;
}
while (ret >= 0)
{
ret = avcodec_receive_packet(oacodectx, opkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0)
{
std::cerr << "avcodec_receive_packet err : " << av_err2str(ret) << std::endl;
break;
}
else
{
// 得到编码数据
out_mp3.write(reinterpret_cast<const char*>(opkt->data), opkt->size);
#ifdef MUXING
opkt->pts = av_rescale_q(opkt->pts, fmt_ctx->streams[aindex]->time_base, oastream2->time_base);
opkt->dts = av_rescale_q(opkt->dts, fmt_ctx->streams[aindex]->time_base, oastream2->time_base);
opkt->duration = av_rescale_q(opkt->duration, fmt_ctx->streams[aindex]->time_base, oastream2->time_base);
opkt->pos = -1;
opkt->stream_index = 1;
ret = av_interleaved_write_frame(ofmt_ctx2, opkt);
if (ret < 0)
{
std::cerr << "av_interleaved_write_frame err : " << av_err2str(ret) << std::endl;
}
#endif // MUXING
av_packet_unref(opkt);
}
}
#endif // ENCODE
}
}
}
#endif // NOAUDIO
}
#ifdef REMUX
streamtmp = nullptr;
if (pkt->stream_index == vindex)
{
streamtmp = ovstream;
pkt->stream_index = 0;
}
else if (pkt->stream_index == aindex)
{
streamtmp = oastream;
pkt->stream_index = 1;
}
if (streamtmp != nullptr)
{
pkt->pts = av_rescale_q(pkt->pts, fmt_ctx->streams[pkt->stream_index]->time_base, streamtmp->time_base);
pkt->dts = av_rescale_q(pkt->dts, fmt_ctx->streams[pkt->stream_index]->time_base, streamtmp->time_base);
pkt->duration = av_rescale_q(pkt->duration, fmt_ctx->streams[pkt->stream_index]->time_base, streamtmp->time_base);
pkt->pos = -1;
ret = av_interleaved_write_frame(ofmt_ctx, pkt);
if (ret < 0)
{
std::cerr << "REMUX av_interleaved_write_frame err : " << av_err2str(ret) << std::endl;
}
}
#endif // REMUX
// 复位data和size
av_packet_unref(pkt);
}
END:
#ifdef REMUX
if (ofmt_ctx != nullptr)
{
// 写文件尾
ret = av_write_trailer(ofmt_ctx);
if (ret < 0)
{
std::cerr << "av_write_trailer err : " << av_err2str(ret) << std::endl;
}
}
#endif // REMUX
#ifdef MUXING
if (ofmt_ctx2 != nullptr)
{
// 写文件尾
ret = av_write_trailer(ofmt_ctx2);
if (ret < 0)
{
std::cerr << "av_write_trailer err : " << av_err2str(ret) << std::endl;
}
}
#endif // MUXING
// 关闭文件
out_yuv.close();
out_hw.close();
out_pcm.close();
out_bgr.close();
out_pcm2.close();
out_h264.close();
out_mp3.close();
std::cerr << "end..." << std::endl;
std::cin.get();
// DECODE
av_freep(&pt[0]);
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&vcodectx);
avcodec_free_context(&acodectx);
avformat_close_input(&fmt_ctx);
// HWDECODE
av_buffer_unref(&hwbufref);
av_frame_free(&hwframe);
av_free(hwframebuf);
// AVIO
av_free(aviobuf);
avio_context_free(&avioctx);
av_file_unmap(buf, size);
// REMUX
// 关闭io
if (ofmt_ctx != nullptr && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
{
avio_closep(&ofmt_ctx->pb);
}
avformat_free_context(ofmt_ctx);
ofmt_ctx = nullptr;
// MUXING
// 关闭io
if (ofmt_ctx2 != nullptr && !(ofmt_ctx2->oformat->flags & AVFMT_NOFILE))
{
avio_closep(&ofmt_ctx2->pb);
}
avformat_free_context(ofmt_ctx2);
ofmt_ctx2 = nullptr;
// ENCODE
av_packet_free(&opkt);
avcodec_free_context(&ovcodectx);
avcodec_free_context(&oacodectx);
// SWSCALE
sws_freeContext(swsctx);
av_freep(&pointers[0]);
// RESAMPLE
swr_free(&swrctx);
av_free(sambuf);
return 0;
}