-
Notifications
You must be signed in to change notification settings - Fork 0
/
asd.c
747 lines (613 loc) · 23 KB
/
asd.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
/*
* Copyright (c) 2012 Stefano Sabatini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file
* Demuxing and decoding example.
*
* Show how to use the libavformat and libavcodec API to demux and
* decode audio and video data.
* @example demuxing_decoding.c
*/
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
static AVFormatContext *fmt_ctx = NULL;
static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
static int width, height;
static enum AVPixelFormat pix_fmt;
static AVStream *video_stream = NULL, *audio_stream = NULL;
static const char *src_filename = NULL;
static const char *video_dst_filename = NULL;
static const char *audio_dst_filename = NULL;
static const char *out_filename = NULL;
static FILE *video_dst_file = NULL;
static FILE *audio_dst_file = NULL;
static uint8_t *video_dst_data[4] = {NULL};
static int video_dst_linesize[4];
static int video_dst_bufsize;
static int video_stream_idx = -1, audio_stream_idx = -1;
static AVFrame *frame = NULL;
static AVPacket pkt;
static int video_frame_count = 0;
static int audio_frame_count = 0;
static AVFormatContext* pFormatCtx;
static AVOutputFormat* fmt;
static AVStream* video_st;
static AVCodecContext* pCodecCtx;
static AVCodec* pCodec;
static AVPacket epkt;
static uint8_t* picture_buf;
static AVFrame* pFrame;
static int picture_size;
static int y_size;
static AVCodec *AudCodec;
static AVCodecContext *AudCodecCtx= NULL;
static AVFormatContext* AudFormatCtx;
static AVOutputFormat* Audofmt;
static AVStream* audio_st;
static AVFrame *AudFrame;
static AVPacket AudPkt;
SwrContext *swr;
void print_hex(uint8_t *s, size_t len) {
int counter=0;
for(int i = 0; i < len; i++) {
if(i%4!=0){
printf("%02x", s[i]);
}else{
if(counter==4){
printf("\n");
counter=0;
}
printf(" %02x", s[i]);
counter++;
}
}
printf("\n");
}
int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
CODEC_CAP_DELAY))
return 0;
while (1) {
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
NULL, &got_frame);
av_frame_free(NULL);
if (ret < 0)
break;
if (!got_frame){
ret=0;
break;
}
printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
/* mux encoded frame */
ret = av_write_frame(fmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
return ret;
}
static int decode_packet()
{
int ret = 0;
int decoded = pkt.size;
int got_frame = 0;
if (pkt.stream_index == video_stream_idx) {
/* decode video frame */
ret = avcodec_decode_video2(video_dec_ctx, frame, &got_frame, &pkt);
if (ret < 0) {
fprintf(stderr, "Error decoding video frame (%s)\n", av_err2str(ret));
return ret;
}
if (got_frame) {
if (frame->width != width || frame->height != height ||
frame->format != pix_fmt) {
/* To handle this change, one could call av_image_alloc again and
* decode the following frames into another rawvideo file. */
fprintf(stderr, "Error: Width, height and pixel format have to be "
"constant in a rawvideo file, but the width, height or "
"pixel format of the input video changed:\n"
"old: width = %d, height = %d, format = %s\n"
"new: width = %d, height = %d, format = %s\n",
width, height, av_get_pix_fmt_name(pix_fmt),
frame->width, frame->height,
av_get_pix_fmt_name(frame->format));
return -1;
}
// print_hex(frame->data[0],strlen((char*)frame->data[0]));
/* copy decoded frame to destination buffer:
* this is required since rawvideo expects non aligned data */
av_image_copy(video_dst_data, video_dst_linesize,
(const uint8_t **)(frame->data), frame->linesize,
pix_fmt, width, height);
//pFrame data 跟video_dst_data不一樣
picture_buf = video_dst_data[0];
/* write to rawvideo file */
// fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
pFrame->data[0] = picture_buf; // Y
pFrame->data[1] = picture_buf+ y_size; // U
pFrame->data[2] = picture_buf+ y_size*5/4; // V
//PTS
int got_picture=0;
//Encode
ret = avcodec_encode_video2(pCodecCtx, &epkt,pFrame, &got_picture);
if(ret < 0){
printf("Failed to encode! \n");
return -1;
}
if (got_picture==1){
printf("Succeed to encode frame: %5d\tsize:%5d\n",video_frame_count,epkt.size);
video_frame_count++;
epkt.stream_index = video_st->index;
ret = av_interleaved_write_frame(pFormatCtx, &epkt);
av_free_packet(&epkt);
}
}
} else if (pkt.stream_index == audio_stream_idx) {
int retuu;
/* decode audio frame */
ret = avcodec_decode_audio4(audio_dec_ctx, frame, &got_frame, &pkt);
if (ret < 0) {
fprintf(stderr, "Error decoding audio frame (%s)\n", av_err2str(ret));
return ret;
}
/* Some audio decoders decode only part of the packet, and have to be
* called again with the remainder of the packet data.
* Sample: fate-suite/lossless-audio/luckynight-partial.shn
* Also, some decoders might over-read the packet. */
decoded = FFMIN(ret, pkt.size);
if (got_frame) {
ret = swr_convert(swr,
AudFrame->data, frame->nb_samples,
(const uint8_t **)frame->extended_data, frame->nb_samples);
if (ret < 0) {
fprintf(stderr, "Error while converting\n");
exit(1);
}
int got_audio_frame=0;
//Encode
retuu = avcodec_encode_audio2(AudCodecCtx, &AudPkt,AudFrame, &got_audio_frame);
if(retuu < 0){
printf("Failed to encode!\n");
return -1;
}
if (got_audio_frame==1){
printf("audio_frame_count = %d\n", audio_frame_count);
printf("SUCCESS to encode 1 audio frame! \tsize:%5d\n",AudPkt.size);
audio_frame_count++;
AudPkt.stream_index = audio_st->index;
ret = av_interleaved_write_frame(AudFormatCtx, &AudPkt);
av_free_packet(&AudPkt);
}
}
}
return decoded;
}
static int open_codec_context(int *stream_idx,
AVFormatContext *fmt_ctx, enum AVMediaType type)
{
int ret, stream_index;
AVStream *st;
AVCodecContext *dec_ctx = NULL;
AVCodec *dec = NULL;
AVDictionary *opts = NULL;
ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
if (ret < 0) {
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
av_get_media_type_string(type), src_filename);
return ret;
} else {
stream_index = ret;
st = fmt_ctx->streams[stream_index];
/* find decoder for the stream */
dec_ctx = st->codec;
dec = avcodec_find_decoder(dec_ctx->codec_id);
if (!dec) {
fprintf(stderr, "Failed to find %s codec\n",
av_get_media_type_string(type));
return AVERROR(EINVAL);
}
if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) {
fprintf(stderr, "Failed to open %s codec\n",
av_get_media_type_string(type));
return ret;
}
*stream_idx = stream_index;
}
return 0;
}
static int init_video_out_context(){
//init output format
pFormatCtx = avformat_alloc_context();
fmt = av_guess_format(NULL, video_dst_filename, NULL);
pFormatCtx->oformat = fmt;
//Open output
if (avio_open(&pFormatCtx->pb,video_dst_filename, AVIO_FLAG_READ_WRITE) < 0){
printf("Failed to open output file! \n");
return 1;
}
video_st = avformat_new_stream(pFormatCtx, 0);
video_st->time_base.num = 1;
video_st->time_base.den = 25;
if (video_st==NULL){
return 1;
}
//Param that must set
pCodecCtx = video_st->codec;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
pCodecCtx->width = width;
pCodecCtx->height = height;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 24;
// pCodecCtx->time_base = video_dec_ctx->time_base;
pCodecCtx->bit_rate = video_dec_ctx->bit_rate;
pCodecCtx->gop_size=10;
//H264
//pCodecCtx->me_range = 16;
//pCodecCtx->max_qdiff = 4;
//pCodecCtx->qcompress = 0.6;
pCodecCtx->qmin = 10;
pCodecCtx->qmax = 51;
//Optional Param
pCodecCtx->max_b_frames=video_dec_ctx->max_b_frames;
// Set Option
AVDictionary *param = 0;
//H.264
if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {
av_dict_set(¶m, "preset", "slow", 0);
av_dict_set(¶m, "tune", "zerolatency", 0);
//av_dict_set(¶m, "profile", "main", 0);
}
//H.265
if(pCodecCtx->codec_id == AV_CODEC_ID_H265){
av_dict_set(¶m, "preset", "ultrafast", 0);
av_dict_set(¶m, "tune", "zero-latency", 0);
}
//Show some Information
av_dump_format(pFormatCtx, 0, video_dst_filename, 1);
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec){
printf("Can not find encoder! \n");
return 1;
}
if (avcodec_open2(pCodecCtx, pCodec,¶m) < 0){
printf("Failed to open encoder! \n");
return 1;
}
y_size = pCodecCtx->width * pCodecCtx->height;
pFrame = av_frame_alloc();
pFrame->width = 240;
pFrame->height = 160;
pFrame->format = pCodecCtx->pix_fmt;
picture_size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
picture_buf = (uint8_t *)av_malloc(picture_size);
avpicture_fill((AVPicture *)pFrame, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
return 0;
}
static int init_audio_out_context(uint8_t *sample_buf){
AudFormatCtx = avformat_alloc_context();
Audofmt = av_guess_format(NULL, audio_dst_filename, NULL);
AudFormatCtx->oformat = Audofmt;
if (avio_open(&AudFormatCtx->pb,audio_dst_filename, AVIO_FLAG_READ_WRITE) < 0){
printf("Failed to open output file!\n");
return 1;
}
audio_st = avformat_new_stream(AudFormatCtx, 0);
if (audio_st==NULL){
return 1;
}
AudCodecCtx = audio_st->codec;
AudCodecCtx->codec_id = Audofmt->audio_codec;
AudCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
AudCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;
AudCodecCtx->sample_rate= audio_dec_ctx->sample_rate;
AudCodecCtx->channel_layout=AV_CH_LAYOUT_STEREO;
AudCodecCtx->channels = av_get_channel_layout_nb_channels(AudCodecCtx->channel_layout);
AudCodecCtx->bit_rate = audio_dec_ctx->bit_rate;
AudCodec = avcodec_find_encoder(AudCodecCtx->codec_id);
if (!AudCodec){
printf("Can not find encoder!\n");
return 1;
}
if (avcodec_open2(AudCodecCtx, AudCodec,NULL) < 0){
printf("Failed to open encoder!\n");
return 1;
}
//Show some information
av_dump_format(AudFormatCtx, 0, audio_dst_filename, 1);
return 0;
}
static int init_input(){
int ret;
//get input format
/* open input file, and allocate format context */
if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open source file %s\n", src_filename);
exit(1);
}
/* retrieve stream information */
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
//get input codec and stream
if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) {
video_stream = fmt_ctx->streams[video_stream_idx];
video_dec_ctx = video_stream->codec;
pCodecCtx = video_stream->codec;
video_dst_file = fopen(video_dst_filename, "wb");
if (!video_dst_file) {
fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
ret = 1;
return 1;
}
/* allocate image where the decoded image will be put */
width = video_dec_ctx->width;
height = video_dec_ctx->height;
pix_fmt = video_dec_ctx->pix_fmt;
ret = av_image_alloc(video_dst_data, video_dst_linesize,
width, height, pix_fmt, 1);
if (ret < 0) {
fprintf(stderr, "Could not allocate raw video buffer\n");
return 1;
}
video_dst_bufsize = ret;
}
if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) {
audio_stream = fmt_ctx->streams[audio_stream_idx];
audio_dec_ctx = audio_stream->codec;
AudCodecCtx = audio_stream->codec;
audio_dst_file = fopen(audio_dst_filename, "wb");
if (!audio_dst_file) {
fprintf(stderr, "Could not open destination file %s\n", audio_dst_filename);
return 1;
}
}
/* dump input information to stderr */
av_dump_format(fmt_ctx, 0, src_filename, 0);
return 0;
}
static int init_output(AVFormatContext *ofmt_ctx){
int ret;
AVStream *in_stream;
AVStream *out_stream;
for (int i = 0; i < fmt_ctx->nb_streams; i++) {
in_stream = fmt_ctx->streams[i];
out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
if (!out_stream) {
printf( "Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
return 1;
}
//copy codec_context from input
ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if (ret < 0) {
printf( "Failed to copy context from input to output stream codec context\n");
return 1;
}
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
printf("==========Output Information==========\n");
av_dump_format(ofmt_ctx, 0, out_filename, 1);
printf("======================================\n");
return 0;
}
int remux_packet(AVFormatContext *ofmt_ctx,AVPacket *pkt){
int ret;
//figure out audio stream or video stream to rescale
AVStream *in_stream, *out_stream;
in_stream = fmt_ctx->streams[pkt->stream_index];
out_stream = ofmt_ctx->streams[pkt->stream_index];
//convert pts,dts
pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base, out_stream->time_base, (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base, out_stream->time_base, (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt->duration = av_rescale_q(pkt->duration, in_stream->time_base, out_stream->time_base);
pkt->pos = -1;
//remux into certain format video
ret = av_interleaved_write_frame(ofmt_ctx, pkt);
return 0;
}
int main (int argc, char **argv){
int ret = 0, got_frame;
AVFormatContext *ofmt_ctx = NULL;
AVOutputFormat *ofmt = NULL;
uint8_t *sample_buf;
if (argc != 4 && argc != 5) {
fprintf(stderr, "input 1.source file:%s\n"
"2.output_video\n"
"3.output_audio\n"
"4.mux video file(Optional)\n"
"\n", argv[0]);
exit(1);
}
src_filename = argv[1];
video_dst_filename = argv[2];
audio_dst_filename = argv[3];
//optional mux to any type video
if(argc == 5){
out_filename = argv[4];
}
/* register all formats and codecs */
av_register_all();
//for network stream
avformat_network_init();
ret = init_input();
if(ret){
goto end;
}
ret = init_video_out_context();
if(ret){
goto end;
}
ret = init_audio_out_context(sample_buf);
if(ret){
goto end;
}else{
int aud_buffer_size;
//alloc frame and packet
AudFrame = av_frame_alloc();
AudFrame->nb_samples = AudCodecCtx->frame_size;
AudFrame->format = AudCodecCtx->sample_fmt;
AudFrame->channel_layout = AudCodecCtx->channel_layout;
aud_buffer_size = av_samples_get_buffer_size(NULL, AudCodecCtx->channels,AudCodecCtx->frame_size,AudCodecCtx->sample_fmt, 1);
sample_buf = (uint8_t *)av_malloc(aud_buffer_size);
avcodec_fill_audio_frame(AudFrame, AudCodecCtx->channels, AudCodecCtx->sample_fmt,(const uint8_t*)sample_buf, aud_buffer_size, 1);
av_new_packet(&AudPkt,aud_buffer_size);
}
if(argc == 5){
//alloc memory
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx) {
printf( "Could not create output context\n");
ret = AVERROR_UNKNOWN;
return 1;
}
ofmt = ofmt_ctx->oformat;
ret = init_output(ofmt_ctx);
if(ret){
printf("Init output ERROR\n");
goto end;
}
}
if (!(ofmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0) {
printf( "Could not open output file '%s'", out_filename);
goto end;
}
}
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
printf( "Error occurred when opening output file\n");
goto end;
}
//this will fill up by decoder(|read frame|->packet->|decoder|->frame)
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate frame\n");
ret = AVERROR(ENOMEM);
goto end;
}
if (video_stream)
printf("Demuxing video from file '%s' into '%s'\n", src_filename, video_dst_filename);
if (audio_stream)
printf("Demuxing audio from file '%s' into '%s'\n", src_filename, audio_dst_filename);
//Write video Header
avformat_write_header(pFormatCtx,NULL);
//Write audio Header
avformat_write_header(AudFormatCtx,NULL);
//alloc packet to get copy from pkt
av_new_packet(&epkt,picture_size);
/*setup the convert parameter
*due to input sample format AV_SAMPLE_FMT_FLTP
*can't be converted to AV_SAMPLE_FMT_S16
*which only accepted by the aac encoder
*/
swr = swr_alloc();
av_opt_set_int(swr, "in_channel_layout", audio_dec_ctx->channel_layout, 0);
av_opt_set_int(swr, "out_channel_layout", AudCodecCtx->channel_layout, 0);
av_opt_set_int(swr, "in_sample_rate", audio_dec_ctx->sample_rate, 0);
av_opt_set_int(swr, "out_sample_rate", AudCodecCtx->sample_rate, 0);
av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
swr_init(swr);
/*start read frames from the file */
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
//do demux & decode -> encode -> output h264 & aac file
ret = decode_packet();
if (ret < 0)
break;
if(argc == 5){
remux_packet(ofmt_ctx,&pkt);
}
av_free_packet(&pkt);
}
/* flush cached frames */
pkt.data = NULL;
pkt.size = 0;
//Flush Encoder
int retfe = flush_encoder(pFormatCtx,0);
if (retfe < 0) {
printf("Flushing encoder failed\n");
return -1;
}
//Flush Encoder
ret = flush_encoder(pFormatCtx,0);
if (ret < 0) {
printf("Flushing encoder failed\n");
return -1;
}
//Write video trailer
av_write_trailer(pFormatCtx);
//Write audio Trailer
av_write_trailer(AudFormatCtx);
//Write remux Trailer
if(argc == 5){
av_write_trailer(ofmt_ctx);
}
printf("Output succeeded!!!!\n");
end:
//free remux
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_close(ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
//free audio
if (audio_st){
avcodec_close(audio_st->codec);
av_free(AudFrame);
av_free(sample_buf);
}
avio_close(AudFormatCtx->pb);
avformat_free_context(AudFormatCtx);
//free video
if (video_st){
avcodec_close(video_st->codec);
av_free(pFrame);
av_free(picture_buf);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
//free decode
avcodec_close(video_dec_ctx);
avcodec_close(audio_dec_ctx);
avformat_close_input(&fmt_ctx);
if (video_dst_file)
fclose(video_dst_file);
if (audio_dst_file)
fclose(audio_dst_file);
av_frame_free(&frame);
return ret < 0;
}