-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputTS.cpp
1463 lines (1250 loc) · 42.8 KB
/
OutputTS.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
/*
* Copyright (c) 2022 John Patrick Poet
*
* Based on:
* Muxing.c Copyright (c) 2003 Fabrice Bellard
* avio_reading.c Copyright (c) 2014 Stefano Sabatini
* encode_audio.c Copyright (c) 2001 Fabrice Bellard
* encode_video.c Copyright (c) 2001 Fabrice Bellard
*
* 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.
*/
/**
* Output a Transport Stream
*/
#include <csignal>
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sstream>
#include <thread>
#include <cstdlib>
#include <fcntl.h>
#include <chrono>
extern "C" {
#include <libavutil/avassert.h>
#include <libavutil/channel_layout.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/timestamp.h>
#include <libavutil/imgutils.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
}
#define BURST_HEADER_SIZE 0x4
#define SYNCWORD1 0xF872
#define SYNCWORD2 0x4E1F
#include "OutputTS.h"
using namespace std;
std::string AV_ts2str(int64_t ts)
{
char astr[AV_TS_MAX_STRING_SIZE] = { 0 };
av_ts_make_string(astr, ts);
return string(astr);
}
std::string AV_ts2timestr(int64_t ts, AVRational* tb)
{
ostringstream os;
os << av_q2d(*tb) * ts;
return os.str();
}
static std::string AVerr2str(int code)
{
char astr[AV_ERROR_MAX_STRING_SIZE] = { 0 };
av_make_error_string(astr, AV_ERROR_MAX_STRING_SIZE, code);
return string(astr);
}
static void log_packet(string where, const AVFormatContext* fmt_ctx,
const AVPacket* pkt)
{
AVRational* time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
cerr << where << "[" << pkt->stream_index << "] pts: " << pkt->pts
<< " pts_time: " << AV_ts2timestr(pkt->pts, time_base)
<< " dts: " << AV_ts2str(pkt->dts)
<< " dts_time: " << AV_ts2timestr(pkt->dts, time_base)
<< " duration: " << AV_ts2str(pkt->duration)
<< " duration_time: " << AV_ts2timestr(pkt->duration, time_base)
<< endl;
}
OutputTS::OutputTS(int verbose_level, const string & video_codec_name,
const string & preset, int quality, int look_ahead,
bool no_audio, const string & device,
MagCallback image_buffer_avail)
: m_audioIO(verbose_level)
, m_verbose(verbose_level)
, m_no_audio(no_audio)
, m_video_codec_name(video_codec_name)
, m_device("/dev/dri/" + device)
, m_preset(preset)
, m_quality(quality)
, m_look_ahead(look_ahead)
, m_image_buffer_available(image_buffer_avail)
{
if (m_video_codec_name.find("qsv") != string::npos)
m_encoderType = EncoderType::QSV;
else if (m_video_codec_name.find("vaapi") != string::npos)
m_encoderType = EncoderType::VAAPI;
else if (m_video_codec_name.find("nvenc") != string::npos)
m_encoderType = EncoderType::NV;
else
{
m_encoderType = EncoderType::UNKNOWN;
cerr << "ERROR: Codec '" << m_video_codec_name << "' not supported.\n";
Shutdown();
}
m_audio_ready = m_no_audio;
m_image_ready_thread = std::thread(&OutputTS::Write, this);
}
void OutputTS::Shutdown(void)
{
m_audioIO.Shutdown();
m_running.store(false);
}
/* Add an output stream. */
bool OutputTS::add_stream(OutputStream* ost, AVFormatContext* oc,
const AVCodec* *codec)
{
AVChannelLayout channel_layout = m_audioIO.ChannelLayout();
AVCodecContext* codec_context;
int idx;
ost->tmp_pkt = av_packet_alloc();
if (!ost->tmp_pkt)
{
cerr << "ERROR: Could not allocate AVPacket\n";
return false;
}
ost->st = avformat_new_stream(oc, NULL);
if (!ost->st)
{
cerr << "ERROR: Could not allocate stream\n";
return false;
}
ost->st->id = oc->nb_streams-1;
codec_context = avcodec_alloc_context3(*codec);
if (!codec_context)
{
cerr << "ERROR: Could not alloc an encoding context\n";
return false;
}
ost->enc = codec_context;
ost->next_pts = 0;
switch ((*codec)->type)
{
case AVMEDIA_TYPE_AUDIO:
ost->enc->sample_fmt = (*codec)->sample_fmts ?
(*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
ost->enc->bit_rate = 192000;
if ((*codec)->supported_samplerates)
{
ost->enc->sample_rate = (*codec)->supported_samplerates[0];
for (idx = 0; (*codec)->supported_samplerates[idx]; ++idx)
{
if ((*codec)->supported_samplerates[idx] == m_audioIO.SampleRate())
{
ost->enc->sample_rate = m_audioIO.SampleRate();
break;
}
}
}
else
ost->enc->sample_rate = 48000;
av_channel_layout_copy(&ost->enc->ch_layout, &channel_layout);
ost->st->time_base = (AVRational){ 1, ost->enc->sample_rate };
if (ost->enc->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS)
{
ost->enc->thread_type = FF_THREAD_SLICE;
if (m_verbose > 1)
cerr << " Audio = THREAD SLICE\n";
}
else if (ost->enc->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
{
ost->enc->thread_type = FF_THREAD_FRAME;
if (m_verbose > 1)
cerr << " Audio = THREAD FRAME\n";
}
if (m_verbose > 1)
{
cerr << "Audio time base " << ost->st->time_base.num << "/"
<< ost->st->time_base.den << "\n";
}
break;
case AVMEDIA_TYPE_VIDEO:
ost->enc->codec_id = (*codec)->id;
// ost->enc->bit_rate = m_video_bitrate;
/* Resolution must be a multiple of two. */
ost->enc->width = m_input_width;
ost->enc->height = m_input_height;
/* timebase: This is the fundamental unit of time (in
* seconds) in terms of which frame timestamps are
* represented. For fixed-fps content, timebase should be
* 1/framerate and timestamp increments should be identical
* to 1. */
ost->st->time_base = AVRational{m_input_frame_rate.den,
m_input_frame_rate.num};
ost->enc->time_base = ost->st->time_base;
#if 0
ost->enc->gop_size = 12; /* emit one intra frame every twelve frames at most */
#endif
/*
For av1_qsv, pix_fmt options are:
AV_PIX_FMT_NV12, AV_PIX_FMT_P010, AV_PIX_FMT_QSV
*/
if (m_encoderType == EncoderType::QSV)
ost->enc->pix_fmt = AV_PIX_FMT_QSV;
else if (m_encoderType == EncoderType::VAAPI)
ost->enc->pix_fmt = AV_PIX_FMT_VAAPI;
else
ost->enc->pix_fmt = AV_PIX_FMT_YUV420P;
if (ost->enc->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS)
{
ost->enc->thread_type = FF_THREAD_SLICE;
if (m_verbose > 1)
cerr << " Video = THREAD SLICE\n";
}
else if (ost->enc->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
{
ost->enc->thread_type = FF_THREAD_FRAME;
if (m_verbose > 1)
cerr << " Video = THREAD FRAME\n";
}
if (m_verbose > 1)
{
cerr << "Output stream< Video: " << ost->enc->width
<< "x" << ost->enc->height
<< " time_base: " << ost->st->time_base.num
<< "/" << ost->st->time_base.den
<< (m_interlaced ? 'i' : 'p')
<< "\n";
}
break;
default:
break;
}
/* Some formats want stream headers to be separate. */
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
ost->enc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
return true;
}
AVFrame* OutputTS::alloc_audio_frame(enum AVSampleFormat sample_fmt,
const AVChannelLayout* channel_layout,
int sample_rate, int nb_samples)
{
AVFrame* frame = av_frame_alloc();
int ret;
if (!frame)
{
cerr << "ERROR: Failed to allocate an audio frame\n";
return nullptr;
}
frame->format = sample_fmt;
av_channel_layout_copy(&frame->ch_layout, channel_layout);
frame->sample_rate = sample_rate;
/* Frame size passed from magewell includes all channels */
frame->nb_samples = nb_samples;
if (nb_samples)
{
ret = av_frame_get_buffer(frame, 0);
if (ret < 0)
{
cerr << "ERROR: failed to allocate an audio buffer\n";
return nullptr;
}
}
return frame;
}
bool OutputTS::open_audio(void)
{
if (m_no_audio)
return true;
close_stream(&m_audio_stream);
const AVCodec* audio_codec = nullptr;
audio_codec = avcodec_find_encoder_by_name(m_audioIO.CodecName().c_str());
if (!audio_codec)
{
cerr << "ERROR: Could not find audio encoder for '"
<< m_audioIO.CodecName() << "'\n";
Shutdown();
return false;
}
if (!add_stream(&m_audio_stream, m_output_format_context, &audio_codec))
return false;
OutputStream* ost = &m_audio_stream;
// AVFormatContext* oc = m_output_format_context;
const AVCodec* codec = audio_codec;
AVCodecContext* enc_ctx = m_audio_stream.enc;
AVDictionary* opt = NULL;
int nb_samples;
int ret;
if ((ret = avcodec_open2(enc_ctx, codec, &opt)) < 0)
{
cerr << "ERROR: Could not open audio codec: " << AVerr2str(ret) << endl;
return false;
}
if (enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
nb_samples = 10000;
else
{
nb_samples = enc_ctx->frame_size;
}
ost->frame = alloc_audio_frame(enc_ctx->sample_fmt, &enc_ctx->ch_layout,
enc_ctx->sample_rate, nb_samples);
if (ost->frame == nullptr)
{
Shutdown();
return false;
}
if (m_audioIO.BytesPerSample() == 4)
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S32,
&enc_ctx->ch_layout,
enc_ctx->sample_rate, nb_samples);
else
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16,
&enc_ctx->ch_layout,
enc_ctx->sample_rate, nb_samples);
if (ost->tmp_frame == nullptr)
{
cerr << "ERROR: Unable to allocate a temporary audio frame.\n";
Shutdown();
return false;
}
/* copy the stream parameters to the muxer */
ret = avcodec_parameters_from_context(ost->st->codecpar, enc_ctx);
if (ret < 0)
{
cerr << "ERROR: Could not copy the stream parameters\n";
return false;
}
/* create resampler context */
ost->swr_ctx = swr_alloc();
if (!ost->swr_ctx)
{
cerr << "ERROR: Could not allocate resampler context\n";
return false;
}
/* set options */
av_opt_set_chlayout (ost->swr_ctx, "in_chlayout",
&enc_ctx->ch_layout, 0);
av_opt_set_int (ost->swr_ctx, "in_sample_rate",
enc_ctx->sample_rate, 0);
if (m_audioIO.BytesPerSample() == 4)
{
av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt",
AV_SAMPLE_FMT_S32, 0);
// ctx->bits_per_raw_sample = 24;
}
else
av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt",
AV_SAMPLE_FMT_S16, 0);
av_opt_set_chlayout (ost->swr_ctx, "out_chlayout",
&enc_ctx->ch_layout, 0);
av_opt_set_int (ost->swr_ctx, "out_sample_rate",
enc_ctx->sample_rate, 0);
av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt",
enc_ctx->sample_fmt, 0);
/* initialize the resampling context */
if ((ret = swr_init(ost->swr_ctx)) < 0)
{
cerr << "ERROR: Failed to initialize the resampling context\n";
return false;
}
return true;
}
bool OutputTS::open_video(void)
{
close_stream(&m_video_stream);
AVDictionary* opt = NULL;
const AVCodec* video_codec =
avcodec_find_encoder_by_name(m_video_codec_name.c_str());
if (video_codec)
{
if (m_verbose > 0)
{
cerr << "Video codec: " << video_codec->id << " : "
<< video_codec->name << " '"
<< video_codec->long_name << "' "
<< endl;
}
}
else
{
cerr << "ERROR: Could not find video encoder for '"
<< m_video_codec_name << "'\n";
return false;
}
/* Add the audio and video streams using the default format codecs
* and initialize the codecs. */
if (!add_stream(&m_video_stream, m_output_format_context,
&video_codec))
return false;
/* Now that all the parameters are set, we can open the audio and
* video codecs and allocate the necessary encode buffers. */
switch (m_encoderType)
{
case EncoderType::QSV:
if (!open_qsv(video_codec, &m_video_stream, opt))
return false;
break;
case EncoderType::VAAPI:
if (!open_vaapi(video_codec, &m_video_stream, opt))
return false;
break;
case EncoderType::NV:
if (!open_nvidia(video_codec, &m_video_stream, opt))
return false;
break;
default:
cerr << "ERROR: Could not determine video encoder type.\n";
return false;
}
return true;
}
bool OutputTS::open_container(void)
{
int ret;
AVDictionary* opt = NULL;
close_container();
if (m_running.load() == false)
return false;
#if 1
if (m_verbose > 1)
cerr << "\n================== open_container begin ==================\n";
#endif
/* allocate the output media context */
avformat_alloc_output_context2(&m_output_format_context,
NULL, "mpegts", m_filename.c_str());
if (!m_output_format_context)
{
cerr << "ERROR: Could not create output format context.\n";
Shutdown();
return false;
}
m_fmt = m_output_format_context->oformat;
if (!open_audio())
return false;
if (!open_video())
return false;
if (m_verbose > 0)
av_dump_format(m_output_format_context, 0, m_filename.c_str(), 1);
/* open the output file, if needed */
if (!(m_fmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&m_output_format_context->pb,
m_filename.c_str(), AVIO_FLAG_WRITE);
if (ret < 0) {
cerr << "ERROR: Could not open '" << m_filename << "': "
<< AVerr2str(ret) << endl;
Shutdown();
return false;
}
}
/* Write the stream header, if any. */
ret = avformat_write_header(m_output_format_context, &opt);
if (ret < 0)
{
cerr << "ERROR: Could not open output file: %s\n"
<< AVerr2str(ret) << endl;
Shutdown();
return false;
}
#if 1
if (m_verbose > 1)
cerr << "\n================== open_container end ==================\n";
#endif
return true;
}
bool OutputTS::setAudioParams(uint8_t* capture_buf, size_t capture_buf_size,
int num_channels, bool is_lpcm,
int bytes_per_sample, int sample_rate,
int samples_per_frame, int frame_size,
int64_t* timestamps)
{
if (!m_audioIO.AddBuffer(capture_buf, capture_buf + capture_buf_size,
num_channels, is_lpcm,
bytes_per_sample, sample_rate,
samples_per_frame, frame_size,
timestamps))
return false;
if (m_verbose > 0)
cerr << "setAudioParams " << (is_lpcm ? "LPCM" : "Bitstream") << endl;
return true;
}
bool OutputTS::setVideoParams(int width, int height, bool interlaced,
AVRational time_base, double frame_duration,
AVRational frame_rate)
{
m_input_width = width;
m_input_height = height;
m_interlaced = interlaced;
m_input_time_base = time_base;
m_input_frame_duration = frame_duration;
m_input_frame_wait_ms = frame_duration / 10000 / 2;
m_input_frame_rate = frame_rate;
double fps = static_cast<double>(frame_rate.num) / frame_rate.den;
if (m_verbose > 0)
{
cerr << "Video: " << width << "x" << height
<< " fps: " << fps
<< (m_interlaced ? 'i' : 'p')
<< "\n";
if (m_verbose > 2)
cerr << "Video Params set\n";
}
m_init_needed = true;
return true;
}
OutputTS::~OutputTS(void)
{
Shutdown();
if (m_image_ready_thread.joinable())
m_image_ready_thread.join();
close_stream(&m_video_stream);
if (m_video_stream.hw_device_ctx != nullptr)
av_buffer_unref(&m_video_stream.hw_device_ctx);
close_stream(&m_audio_stream);
close_container();
}
bool OutputTS::addAudio(uint8_t* buf, size_t len, int64_t timestamp)
{
m_audioIO.Add(buf, len, timestamp);
return true;
}
bool OutputTS::write_frame(AVFormatContext* fmt_ctx,
AVCodecContext* codec_ctx,
AVFrame* frame,
OutputStream* ost)
{
int ret;
AVPacket* pkt = ost->tmp_pkt;
if (ost->prev_pts >= frame->pts)
++frame->pts;
ost->prev_pts = frame->pts;
// send the frame to the encoder
ret = avcodec_send_frame(codec_ctx, frame);
if (ret < 0)
{
if (m_verbose > 0)
{
cerr << "WARNING: Failed sending a frame to the encoder: "
<< AVerr2str(ret) << "\n";
}
return false;
}
while (ret >= 0)
{
ret = avcodec_receive_packet(codec_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < 0)
{
if (m_verbose > 0)
{
cerr << "WARNING: Failed encoding a frame: AVerr2str(ret)\n";
}
return false;
}
/* rescale output packet timestamp values from codec to stream
* timebase */
av_packet_rescale_ts(pkt, codec_ctx->time_base, ost->st->time_base);
pkt->stream_index = ost->st->index;
if (ost->prev_dts >= pkt->dts)
pkt->dts = ost->prev_dts + 1;
ost->prev_dts = pkt->dts;
if (pkt->pts < pkt->dts)
pkt->pts = pkt->dts;
/* Write the compressed frame to the media file. */
#if 0
log_packet("write_frame", fmt_ctx, pkt);
#endif
ret = av_interleaved_write_frame(fmt_ctx, pkt);
/* pkt is now blank (av_interleaved_write_frame() takes ownership of
* its contents and resets pkt), so that no unreferencing is necessary.
* This would be different if one used av_write_frame(). */
if (ret < 0)
{
if (m_verbose > 0)
{
cerr << "WARNING: Failed to write packet: " << AVerr2str(ret)
<< "\n";
}
if (m_verbose > 1)
{
cerr << "Codec time base " << codec_ctx->time_base.num
<< "/" << codec_ctx->time_base.den
<< "\n"
<< "Stream " << ost->st->time_base.num
<< "/" << ost->st->time_base.den
<< "\n";
log_packet("write_frame", fmt_ctx, pkt);
}
return false;
}
}
return ret == AVERROR_EOF ? false : true;
}
void OutputTS::close_container(void)
{
#if 0
if (m_output_format_context)
av_write_trailer(m_output_format_context);
#endif
if (m_fmt && !(m_fmt->flags & AVFMT_NOFILE))
/* Close the output file. */
avio_closep(&m_output_format_context->pb);
/* free the output stream */
avformat_free_context(m_output_format_context);
}
void OutputTS::close_stream(OutputStream* ost)
{
if (ost == nullptr)
return;
if (ost->hw_device)
{
av_buffer_unref(&ost->enc->hw_frames_ctx);
ost->hw_device = false;
}
#if 1
if (ost->tmp_frame /* && ost->tmp_frame->data[0] */)
{
#if 0
av_frame_free(&ost->tmp_frame);
#else
av_free(&ost->frame->data[0]);
#endif
ost->tmp_frame = nullptr;
}
#endif
if (ost->swr_ctx)
{
swr_free(&ost->swr_ctx);
ost->swr_ctx = nullptr;
}
if (ost->enc)
{
/*
FFmpeg docs now say:
Opening and closing a codec context multiple times is not
supported anymore – use multiple codec contexts instead.
So, does this mean to never free a codect context?
*/
#if 0
avcodec_free_context(&ost->enc);
ost->enc = nullptr;
#endif
}
/* Documentation says to call avformat_free_context, but
that does not compile. The example code does not free
the stream either, so... */
#if 0
avformat_free_context(&ost->st);
ost->st = nullptr;
#endif
}
/**************************************************************/
/* audio output */
AVFrame* OutputTS::get_pcm_audio_frame(OutputStream* ost)
{
AVFrame* frame = ost->tmp_frame;
uint8_t* q = (uint8_t*)frame->data[0];
int bytes = ost->enc->ch_layout.nb_channels *
frame->nb_samples * m_audioIO.BytesPerSample();
if (m_audioIO.Size() < bytes)
{
if (m_verbose > 4)
cerr << "Not enough audio data.\n";
return nullptr;
}
if (m_audioIO.Read(q, bytes) == 0)
return nullptr;
frame->pts = m_audioIO.TimeStamp();
ost->frame->pts = av_rescale_q(frame->pts, m_input_time_base,
ost->enc->time_base);
ost->next_pts = frame->pts + frame->nb_samples;
return frame;
}
bool OutputTS::write_pcm_frame(AVFormatContext* oc, OutputStream* ost)
{
AVCodecContext* enc_ctx = ost->enc;
AVFrame* frame = get_pcm_audio_frame(ost);
int dst_nb_samples = 0;
int ret = 0;
if (!frame)
return false;
/* convert samples from native format to destination codec format,
* using the resampler */
/* compute destination number of samples */
dst_nb_samples = av_rescale(swr_get_delay(ost->swr_ctx,
enc_ctx->sample_rate)
+ frame->nb_samples,
enc_ctx->sample_rate,
enc_ctx->sample_rate);
av_assert0(dst_nb_samples == frame->nb_samples);
/* when we pass a frame to the encoder, it may keep a reference to it
* internally;
* make sure we do not overwrite it here
*/
if (0 > av_frame_make_writable(ost->frame))
{
cerr << "WARNING: write_pcm_frame: Failed to make frame writable\n";
return false;
}
/* convert to destination format */
ret = swr_convert(ost->swr_ctx,
ost->frame->data, dst_nb_samples,
const_cast<const uint8_t** >(frame->data),
frame->nb_samples);
if (ret < 0)
{
cerr << "WARNING: write_pcm_frame: Error while converting\n";
return false;
}
frame = ost->frame;
frame->pts = av_rescale_q(m_audioIO.TimeStamp(),
m_input_time_base,
enc_ctx->time_base);
ost->samples_count += dst_nb_samples;
return write_frame(oc, enc_ctx, frame, ost);
}
bool OutputTS::write_bitstream_frame(AVFormatContext* oc, OutputStream* ost)
{
AVPacket* pkt = m_audioIO.ReadSPDIF();
if (pkt == nullptr)
return false;
pkt->pts = av_rescale_q(m_audioIO.TimeStamp(),
m_input_time_base,
ost->st->time_base);
if (pkt->pts - ost->prev_audio_pts > ost->frame->nb_samples * 3)
{
/* This a bit of hack, but seems to solve the problem.
Jumping around in some applications can cause the S/PDIF to
get out of sync. For example, This can happen if the
audio_capture::audio_buf_sz is too small.
*/
if (++m_slow_audio_cnt > 10)
{
if (m_verbose > 0)
cerr << "WARNING: S/PDIF audio out of sync, resetting." << endl;
m_slow_audio_cnt = 0;
if (!m_audioIO.RescanSPDIF())
Shutdown();
return false;
}
}
else
m_slow_audio_cnt = 0;
ost->prev_audio_pts = pkt->pts;
/* Frame size passed from magewell includes all channels */
ost->next_pts = m_audioIO.TimeStamp() + ost->frame->nb_samples;
pkt->duration = pkt->pts;
pkt->dts = pkt->pts;
pkt->stream_index = ost->st->index;
/* Write the frame to the media file. */
#if 0
log_packet("write_bitstream_frame", oc, pkt);
#endif
int ret = av_interleaved_write_frame(oc, pkt);
/* pkt is now blank (av_interleaved_write_frame() takes ownership of
* its contents and resets pkt), so that no unreferencing is necessary.
* This would be different if one used av_write_frame(). */
if (ret < 0)
{
cerr << "WARNING: Failed to write audio packet: " << AVerr2str(ret)
<< "\n";
return false;
}
return true;
}
/*
* encode one audio frame and send it to the muxer
*/
bool OutputTS::write_audio_frame(AVFormatContext* oc, OutputStream* ost)
{
if (m_audioIO.CodecChanged())
{
m_init_needed = true;
return false;
}
if (!m_audioIO.BlockReady())
{
return false;
}
if (m_audioIO.Bitstream())
return write_bitstream_frame(oc, ost);
else
return write_pcm_frame(oc, ost);
}
/**************************************************************/
/* video output */
AVFrame* OutputTS::alloc_picture(enum AVPixelFormat pix_fmt,
int width, int height)
{
AVFrame* picture;
int ret;
picture = av_frame_alloc();
if (!picture)
return nullptr;
picture->format = pix_fmt;
picture->width = width;
picture->height = height;
/* allocate the buffers for the frame data */
ret = av_frame_get_buffer(picture, 0);
if (ret < 0)
{
const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(pix_fmt);
cerr << "ERROR: Could not allocate " << desc->name
<< " video frame of " << width << "x" << height
<< " : " << AV_ts2str(ret) << endl;
return nullptr;
}
return picture;
}
bool OutputTS::open_nvidia(const AVCodec* codec,
OutputStream* ost, AVDictionary* opt_arg)
{
int ret;
AVCodecContext* ctx = ost->enc;
AVDictionary* opt = NULL;
av_dict_copy(&opt, opt_arg, 0);
if (!m_preset.empty())
{
av_opt_set(ctx->priv_data, "preset", m_preset.c_str(), 0);
if (m_verbose > 0)
cerr << "Using preset " << m_preset << " for "
<< m_video_codec_name << endl;
}
av_opt_set(ctx->priv_data, "tune", "hq", 0);
av_opt_set(ctx->priv_data, "rc", "constqp", 0);
av_opt_set_int(ctx->priv_data, "cq", m_quality, 0);
if (m_look_ahead >= 0)
{
av_opt_set_int(ctx->priv_data, "rc-lookahead", m_look_ahead, 0);
av_opt_set_int(ctx->priv_data, "surfaces", 50, 0);
}
av_opt_set_int(ctx->priv_data, "b", 0, 0);
av_opt_set_int(ctx->priv_data, "minrate", 4000000, 0);
av_opt_set_int(ctx->priv_data, "maxrate", 25000000, 0);
av_opt_set_int(ctx->priv_data, "bufsize", 400000000, 0);
av_opt_set_int(ctx->priv_data, "bf", 0, 0);
av_opt_set_int(ctx->priv_data, "b_ref_mode", 0, 0);