/
transcode.cpp
executable file
·2207 lines (1915 loc) · 68.1 KB
/
transcode.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
#include <fcntl.h>
#include <math.h>
#include <iostream>
#include <QStringList>
#include <QMap>
#include <QRegExp>
#include <QList>
#include <QWaitCondition>
#include <QMutex>
#include <QMutexLocker>
#include <QtAlgorithms>
#include "mythconfig.h"
#include "transcode.h"
#include "audiooutput.h"
#include "recordingprofile.h"
#include "mythcorecontext.h"
#include "jobqueue.h"
#include "exitcodes.h"
#include "mthreadpool.h"
#include "deletemap.h"
#include "NuppelVideoRecorder.h"
#include "mythplayer.h"
#include "programinfo.h"
#include "mythdbcon.h"
#include "avformatwriter.h"
#include "HLS/httplivestream.h"
extern "C" {
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
}
using namespace std;
#define LOC QString("Transcode: ")
#define ABLOCK_SIZE 8192
class AudioBuffer
{
public:
AudioBuffer() : m_frames(0), m_time(-1)
{
m_size = 0;
m_buffer = (uint8_t *)av_malloc(ABLOCK_SIZE);
if (m_buffer == NULL)
{
throw std::bad_alloc();
}
m_realsize = ABLOCK_SIZE;
}
AudioBuffer(const AudioBuffer &old) : m_size(old.m_size),
m_realsize(old.m_realsize), m_frames(old.m_frames), m_time(old.m_time)
{
m_buffer = (uint8_t *)av_malloc(m_realsize);
if (m_buffer == NULL)
{
throw std::bad_alloc();
}
memcpy(m_buffer, old.m_buffer, m_size);
}
~AudioBuffer()
{
av_free(m_buffer);
}
void appendData(unsigned char *buffer, int len, int frames, long long time)
{
if ((m_size + len) > m_realsize)
{
// buffer is too small to fit all
// can't use av_realloc as it doesn't guarantee reallocated memory
// to be 16 bytes aligned
m_realsize = ((m_size + len) / ABLOCK_SIZE + 1 ) * ABLOCK_SIZE;
uint8_t *tmp = (uint8_t *)av_malloc(m_realsize);
if (tmp == NULL)
{
throw std::bad_alloc();
}
memcpy(tmp, m_buffer, m_size);
av_free(m_buffer);
m_buffer = tmp;
}
memcpy(m_buffer + m_size, buffer, len);
m_size += len;
m_frames += frames;
m_time = time;
}
char *data(void) { return (char *)m_buffer; }
int size(void) { return m_size; }
uint8_t *m_buffer;
int m_size;
int m_realsize;
int m_frames;
long long m_time;
};
// This class is to act as a fake audio output device to store the data
// for reencoding.
class AudioReencodeBuffer : public AudioOutput
{
public:
AudioReencodeBuffer(AudioFormat audio_format, int audio_channels,
bool passthru) :
m_saveBuffer(NULL)
{
Reset();
const AudioSettings settings(audio_format, audio_channels, 0, 0, false);
Reconfigure(settings);
m_initpassthru = passthru;
m_audioFrameSize = 0;
}
~AudioReencodeBuffer()
{
Reset();
if (m_saveBuffer)
delete m_saveBuffer;
}
// reconfigure sound out for new params
virtual void Reconfigure(const AudioSettings &settings)
{
ClearError();
m_passthru = settings.use_passthru;
m_channels = settings.channels;
m_bytes_per_frame = m_channels *
AudioOutputSettings::SampleSize(settings.format);
m_eff_audiorate = settings.samplerate;
}
// dsprate is in 100 * frames/second
virtual void SetEffDsp(int dsprate)
{
m_eff_audiorate = (dsprate / 100);
}
virtual void Reset(void)
{
QMutexLocker locker(&m_bufferMutex);
foreach (AudioBuffer *ab, m_bufferList)
{
delete ab;
}
m_bufferList.clear();
}
// timecode is in milliseconds.
virtual bool AddFrames(void *buffer, int frames, int64_t timecode)
{
return AddData(buffer, frames * m_bytes_per_frame, timecode, frames);
}
// timecode is in milliseconds.
virtual bool AddData(void *buffer, int len, int64_t timecode, int frames)
{
unsigned char *buf = (unsigned char *)buffer;
// Test if target is using a fixed buffer size.
if (m_audioFrameSize)
{
int index = 0;
// Target has a fixed buffer size, which may not match len.
// Redistribute the bytes into appropriately sized buffers.
while (index < len)
{
// See if we have some saved from last iteration in
// m_saveBuffer. If not create a new empty buffer.
if (!m_saveBuffer)
m_saveBuffer = new AudioBuffer();
// Use as many of the remaining frames as will fit in the space
// left in the buffer.
int bufsize = m_saveBuffer->size();
int part = min(len - index, m_audioFrameSize - bufsize);
int out_frames = part / m_bytes_per_frame;
timecode += out_frames * 1000 / m_eff_audiorate;
// Store frames in buffer, basing frame count on number of
// bytes, which works only for uncompressed data.
m_saveBuffer->appendData(&buf[index], part,
out_frames, timecode);
// If we have filled the buffer...
if (m_saveBuffer->size() == m_audioFrameSize)
{
QMutexLocker locker(&m_bufferMutex);
// store the buffer
m_bufferList.append(m_saveBuffer);
// mark m_saveBuffer as emtpy.
m_saveBuffer = NULL;
// m_last_audiotime is updated iff we store a buffer.
m_last_audiotime = timecode;
}
index += part;
}
}
else
{
// Target has no fixed buffer size. We can use a simpler algorithm
// and use 'frames' directly rather than 'len / m_bytes_per_frame',
// thus also covering the passthrough case.
m_saveBuffer = new AudioBuffer();
timecode += frames * 1000 / m_eff_audiorate;
m_saveBuffer->appendData(buf, len, frames, timecode);
QMutexLocker locker(&m_bufferMutex);
m_bufferList.append(m_saveBuffer);
m_saveBuffer = NULL;
m_last_audiotime = timecode;
}
return true;
}
AudioBuffer *GetData(long long time)
{
QMutexLocker locker(&m_bufferMutex);
if (m_bufferList.isEmpty())
return NULL;
AudioBuffer *ab = m_bufferList.front();
if (ab->m_time <= time)
{
m_bufferList.pop_front();
return ab;
}
return NULL;
}
long long GetSamples(long long time)
{
QMutexLocker locker(&m_bufferMutex);
if (m_bufferList.isEmpty())
return 0;
long long samples = 0;
for (QList<AudioBuffer *>::iterator it = m_bufferList.begin();
it != m_bufferList.end(); ++it)
{
AudioBuffer *ab = *it;
if (ab->m_time <= time)
samples += ab->m_frames;
else
break;
}
return samples;
}
virtual void SetTimecode(int64_t timecode)
{
m_last_audiotime = timecode;
}
virtual bool IsPaused(void) const
{
return false;
}
virtual void Pause(bool paused)
{
(void)paused;
}
virtual void PauseUntilBuffered(void)
{
// Do nothing
}
virtual void Drain(void)
{
// Do nothing
}
virtual int64_t GetAudiotime(void)
{
return m_last_audiotime;
}
virtual int GetVolumeChannel(int) const
{
// Do nothing
return 100;
}
virtual void SetVolumeChannel(int, int)
{
// Do nothing
}
virtual void SetVolumeAll(int)
{
// Do nothing
}
virtual uint GetCurrentVolume(void) const
{
// Do nothing
return 100;
}
virtual void SetCurrentVolume(int)
{
// Do nothing
}
virtual void AdjustCurrentVolume(int)
{
// Do nothing
}
virtual void SetMute(bool)
{
// Do nothing
}
virtual void ToggleMute(void)
{
// Do nothing
}
virtual MuteState GetMuteState(void) const
{
// Do nothing
return kMuteOff;
}
virtual MuteState IterateMutedChannels(void)
{
// Do nothing
return kMuteOff;
}
virtual bool IsUpmixing(void)
{
// Do nothing
return false;
}
virtual bool ToggleUpmix(void)
{
// Do nothing
return false;
}
virtual bool CanUpmix(void)
{
// Do nothing
return false;
}
virtual void SetSWVolume(int new_volume, bool save)
{
// Do nothing
return;
}
virtual int GetSWVolume(void)
{
// Do nothing
return 100;
}
// These are pure virtual in AudioOutput, but we don't need them here
virtual void bufferOutputData(bool){ return; }
virtual int readOutputData(unsigned char*, int ){ return 0; }
/**
* Test if we can output digital audio
*/
virtual bool CanPassthrough(int, int, int, int) const
{ return m_initpassthru; }
int m_channels;
int m_bits;
int m_bytes_per_frame;
int m_eff_audiorate;
long long m_last_audiotime;
bool m_passthru;
int m_audioFrameSize;
private:
bool m_initpassthru;
QMutex m_bufferMutex;
QList<AudioBuffer *> m_bufferList;
AudioBuffer *m_saveBuffer;
};
// Cutter object is used in performing clean cutting. The
// act of cutting is shared between the player and the
// transcode loop. The player performs the initial part
// of the cut by seeking, and the transcode loop handles
// the remaining part by discarding data.
class Cutter
{
private:
bool active;
frm_dir_map_t foreshortenedCutList;
DeleteMap tracker;
int64_t totalFrames;
int64_t videoFramesToCut;
int64_t audioFramesToCut;
float audioFramesPerVideoFrame;
enum
{
MAXLEADIN = 200,
MINCUT = 20
};
public:
Cutter() : active(false), videoFramesToCut(0), audioFramesToCut(0),
audioFramesPerVideoFrame(0.0) {};
void SetCutList(frm_dir_map_t &deleteMap)
{
// Break each cut into two parts, the first for
// the player and the second for the transcode loop.
frm_dir_map_t remainingCutList;
frm_dir_map_t::Iterator it;
int64_t start = 0;
int64_t leadinLength;
foreshortenedCutList.clear();
for (it = deleteMap.begin(); it != deleteMap.end(); ++it)
{
switch(it.value())
{
case MARK_CUT_START:
foreshortenedCutList[it.key()] = MARK_CUT_START;
start = it.key();
break;
case MARK_CUT_END:
leadinLength = min((int64_t)(it.key() - start),
(int64_t)MAXLEADIN);
if (leadinLength >= MINCUT)
{
foreshortenedCutList[it.key() - leadinLength + 2] =
MARK_CUT_END;
remainingCutList[it.key() - leadinLength + 1] =
MARK_CUT_START;
remainingCutList[it.key()] = MARK_CUT_END;
}
else
{
// Cut too short to use new method.
foreshortenedCutList[it.key()] = MARK_CUT_END;
}
break;
}
}
tracker.SetMap(remainingCutList);
}
frm_dir_map_t AdjustedCutList() const
{
return foreshortenedCutList;
}
void Activate(float v2a, int64_t total)
{
active = true;
audioFramesPerVideoFrame = v2a;
totalFrames = total;
videoFramesToCut = 0;
audioFramesToCut = 0;
tracker.TrackerReset(0, totalFrames);
}
void NewFrame(int64_t currentFrame)
{
if (active)
{
if (videoFramesToCut == 0)
{
uint64_t jumpTo = 0;
if (tracker.TrackerWantsToJump(currentFrame, totalFrames,
jumpTo))
{
// Reset the tracker and work out how much video and audio
// to drop
tracker.TrackerReset(jumpTo, totalFrames);
videoFramesToCut = jumpTo - currentFrame;
audioFramesToCut += (int64_t)(videoFramesToCut *
audioFramesPerVideoFrame + 0.5);
LOG(VB_GENERAL, LOG_INFO,
QString("Clean cut: discarding frame from %1 to %2: "
"vid %3 aud %4")
.arg((long)currentFrame).arg((long)jumpTo)
.arg((long)videoFramesToCut)
.arg((long)audioFramesToCut));
}
}
}
}
bool InhibitUseVideoFrame()
{
if (videoFramesToCut == 0)
{
return false;
}
else
{
// We are inside a cut. Inhibit use of this frame
videoFramesToCut--;
if(videoFramesToCut == 0)
LOG(VB_GENERAL, LOG_INFO,
QString("Clean cut: end of video cut; audio frames left "
"to cut %1") .arg((long)audioFramesToCut));
return true;
}
}
bool InhibitUseAudioFrames(int64_t frames, long *totalAudio)
{
int64_t delta = audioFramesToCut - frames;
if (delta < 0)
delta = -delta;
if (audioFramesToCut == 0)
{
return false;
}
else if (delta < audioFramesToCut)
{
// Drop the packet containing these frames if doing
// so gets us closer to zero left to drop
audioFramesToCut -= frames;
if(audioFramesToCut == 0)
LOG(VB_GENERAL, LOG_INFO,
QString("Clean cut: end of audio cut; vidio frames left "
"to cut %1") .arg((long)videoFramesToCut));
return true;
}
else
{
// Don't drop this packet even though we still have frames to cut,
// because doing so would put us further out. Instead, inflate the
// callers record of how many audio frames have been output.
*totalAudio += audioFramesToCut;
audioFramesToCut = 0;
LOG(VB_GENERAL, LOG_INFO,
QString("Clean cut: end of audio cut; vidio frames left to "
"cut %1") .arg((long)videoFramesToCut));
return false;
}
}
bool InhibitDummyFrame()
{
if (audioFramesToCut > 0)
{
// If the cutter is in the process of dropping audio then
// it is better to drop more audio rather than insert a dummy frame
audioFramesToCut += (int64_t)(audioFramesPerVideoFrame + 0.5);
return true;
}
else
{
return false;
}
}
bool InhibitDropFrame()
{
if (audioFramesToCut > (int64_t)(audioFramesPerVideoFrame + 0.5))
{
// If the cutter is in the process of dropping audio and the
// amount to drop is sufficient then we can drop less
// audio rather than drop a frame
audioFramesToCut -= (int64_t)(audioFramesPerVideoFrame + 0.5);
// But if it's a frame we are supposed to drop anyway, still do so,
// and record that we have
if (videoFramesToCut > 0)
{
videoFramesToCut-- ;
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
};
typedef struct transcodeFrameInfo
{
VideoFrame *frame;
int didFF;
bool isKey;
} TranscodeFrameInfo;
class TranscodeFrameQueue : public QRunnable
{
public:
TranscodeFrameQueue(MythPlayer *player, VideoOutput *videoout,
bool cutlist, int size = 5)
: m_player(player), m_videoOutput(videoout),
m_honorCutlist(cutlist),
m_eof(false), m_maxFrames(size),
m_runThread(true), m_isRunning(false)
{
}
~TranscodeFrameQueue()
{
m_runThread = false;
m_frameWaitCond.wakeAll();
while (m_isRunning)
usleep(50000);
}
void stop(void)
{
m_runThread = false;
m_frameWaitCond.wakeAll();
while (m_isRunning)
usleep(50000);
}
void run()
{
frm_dir_map_t::iterator dm_iter;
m_isRunning = true;
while (m_runThread)
{
if (m_frameList.size() < m_maxFrames && !m_eof)
{
TranscodeFrameInfo tfInfo;
tfInfo.frame = NULL;
tfInfo.didFF = 0;
tfInfo.isKey = false;
if (m_player->TranscodeGetNextFrame(dm_iter, tfInfo.didFF,
tfInfo.isKey, m_honorCutlist))
{
tfInfo.frame = m_videoOutput->GetLastDecodedFrame();
QMutexLocker locker(&m_queueLock);
m_frameList.append(tfInfo);
}
else
{
m_eof = true;
}
m_frameWaitCond.wakeAll();
}
else
{
m_frameWaitLock.lock();
m_frameWaitCond.wait(&m_frameWaitLock);
m_frameWaitLock.unlock();
}
}
m_isRunning = false;
}
VideoFrame *GetFrame(int &didFF, bool &isKey)
{
m_queueLock.lock();
if (m_frameList.isEmpty())
{
m_queueLock.unlock();
if (m_eof)
return NULL;
m_frameWaitLock.lock();
m_frameWaitCond.wait(&m_frameWaitLock);
m_frameWaitLock.unlock();
if (m_frameList.isEmpty())
return NULL;
m_queueLock.lock();
}
TranscodeFrameInfo tfInfo = m_frameList.takeFirst();
m_queueLock.unlock();
m_frameWaitCond.wakeAll();
didFF = tfInfo.didFF;
isKey = tfInfo.isKey;
return tfInfo.frame;
}
private:
MythPlayer *m_player;
VideoOutput *m_videoOutput;
bool m_honorCutlist;
bool m_eof;
int m_maxFrames;
bool m_runThread;
bool m_isRunning;
QMutex m_queueLock;
QList<TranscodeFrameInfo> m_frameList;
QWaitCondition m_frameWaitCond;
QMutex m_frameWaitLock;
};
Transcode::Transcode(ProgramInfo *pginfo) :
m_proginfo(pginfo),
keyframedist(30),
nvr(NULL),
ctx(NULL),
outRingBuffer(NULL),
fifow(NULL),
kfa_table(NULL),
showprogress(false),
recorderOptions(""),
avfMode(false),
hlsMode(false), hlsStreamID(-1),
hlsDisableAudioOnly(false),
hlsMaxSegments(0),
cmdContainer("mpegts"), cmdAudioCodec("aac"),
cmdVideoCodec("libx264"),
cmdWidth(480), cmdHeight(0),
cmdBitrate(600000), cmdAudioBitrate(64000)
{
}
Transcode::~Transcode()
{
if (nvr)
delete nvr;
SetPlayerContext(NULL);
if (outRingBuffer)
delete outRingBuffer;
if (fifow)
delete fifow;
if (kfa_table)
delete kfa_table;
}
void Transcode::ReencoderAddKFA(long curframe, long lastkey, long num_keyframes)
{
long delta = curframe - lastkey;
if (delta != 0 && delta != keyframedist)
{
struct kfatable_entry kfate;
kfate.adjust = keyframedist - delta;
kfate.keyframe_number = num_keyframes;
kfa_table->push_back(kfate);
}
}
bool Transcode::GetProfile(QString profileName, QString encodingType,
int height, int frameRate)
{
if (profileName.toLower() == "autodetect")
{
if (height == 1088)
height = 1080;
QString autoProfileName = QObject::tr("Autodetect from %1").arg(height);
if (frameRate == 25 || frameRate == 30)
autoProfileName += "i";
if (frameRate == 50 || frameRate == 60)
autoProfileName += "p";
bool result = false;
LOG(VB_GENERAL, LOG_NOTICE,
QString("Transcode: Looking for autodetect profile: %1")
.arg(autoProfileName));
result = profile.loadByGroup(autoProfileName, "Transcoders");
if (!result && encodingType == "MPEG-2")
{
result = profile.loadByGroup("MPEG2", "Transcoders");
autoProfileName = "MPEG2";
}
if (!result && (encodingType == "MPEG-4" || encodingType == "RTjpeg"))
{
result = profile.loadByGroup("RTjpeg/MPEG4",
"Transcoders");
autoProfileName = "RTjpeg/MPEG4";
}
if (!result)
{
LOG(VB_GENERAL, LOG_ERR,
QString("Transcode: Couldn't find profile for : %1")
.arg(encodingType));
return false;
}
LOG(VB_GENERAL, LOG_NOTICE,
QString("Transcode: Using autodetect profile: %1")
.arg(autoProfileName));
}
else
{
bool isNum;
int profileID;
profileID = profileName.toInt(&isNum);
// If a bad profile is specified, there will be trouble
if (isNum && profileID > 0)
profile.loadByID(profileID);
else if (!profile.loadByGroup(profileName, "Transcoders"))
{
LOG(VB_GENERAL, LOG_ERR, QString("Couldn't find profile #: %1")
.arg(profileName));
return false;
}
}
return true;
}
void Transcode::SetPlayerContext(PlayerContext *player_ctx)
{
if (player_ctx == ctx)
return;
delete ctx;
ctx = player_ctx;
}
static QString get_str_option(RecordingProfile &profile, const QString &name)
{
const Setting *setting = profile.byName(name);
if (setting)
return setting->getValue();
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("get_str_option(...%1): Option not in profile.").arg(name));
return QString::null;
}
static int get_int_option(RecordingProfile &profile, const QString &name)
{
QString ret_str = get_str_option(profile, name);
if (ret_str.isEmpty())
return 0;
bool ok = false;
int ret_int = ret_str.toInt(&ok);
if (!ok)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("get_int_option(...%1): Option is not an int.").arg(name));
}
return ret_int;
}
static void TranscodeWriteText(void *ptr, unsigned char *buf, int len,
int timecode, int pagenr)
{
NuppelVideoRecorder *nvr = (NuppelVideoRecorder *)ptr;
nvr->WriteText(buf, len, timecode, pagenr);
}
int Transcode::TranscodeFile(const QString &inputname,
const QString &outputname,
const QString &profileName,
bool honorCutList, bool framecontrol,
int jobID, QString fifodir,
bool fifo_info, bool cleanCut,
frm_dir_map_t &deleteMap,
int AudioTrackNo,
bool passthru)
{
QDateTime curtime = MythDate::current();
QDateTime statustime = curtime;
int audioFrame = 0;
Cutter *cutter = NULL;
AVFormatWriter *avfw = NULL;
AVFormatWriter *avfw2 = NULL;
HTTPLiveStream *hls = NULL;
int hlsSegmentSize = 0;
int hlsSegmentFrames = 0;
if (jobID >= 0)
JobQueue::ChangeJobComment(jobID, "0% " + QObject::tr("Completed"));
if (hlsMode)
{
avfMode = true;
if (hlsStreamID != -1)
{
hls = new HTTPLiveStream(hlsStreamID);
hls->UpdateStatus(kHLSStatusStarting);
hls->UpdateStatusMessage("Transcoding Starting");
cmdWidth = hls->GetWidth();
cmdHeight = hls->GetHeight();
cmdBitrate = hls->GetBitrate();
cmdAudioBitrate = hls->GetAudioBitrate();
}
}
if (!avfMode)
{
nvr = new NuppelVideoRecorder(NULL, NULL);
if (!nvr)
{
LOG(VB_GENERAL, LOG_ERR,
"Transcoding aborted, error creating NuppelVideoRecorder.");
return REENCODE_ERROR;
}
}
// Input setup
PlayerContext *player_ctx = new PlayerContext(kTranscoderInUseID);
player_ctx->SetPlayingInfo(m_proginfo);
player_ctx->SetRingBuffer((hls && (hlsStreamID != -1)) ?
RingBuffer::Create(hls->GetSourceFile(), false, false) :
RingBuffer::Create(inputname, false, false));
player_ctx->SetPlayer(new MythPlayer(kVideoIsNull));
SetPlayerContext(player_ctx);
GetPlayer()->SetPlayerInfo(NULL, NULL, GetPlayerContext());
if (showprogress)
{
statustime = statustime.addSecs(5);
}
AudioOutput *audioOutput = new AudioReencodeBuffer(FORMAT_NONE, 0,
passthru);
AudioReencodeBuffer *arb = ((AudioReencodeBuffer*)audioOutput);
GetPlayer()->GetAudio()->SetAudioOutput(audioOutput);
GetPlayer()->SetTranscoding(true);
if (GetPlayer()->OpenFile() < 0)
{
LOG(VB_GENERAL, LOG_ERR, "Transcoding aborted, error opening file.");
SetPlayerContext(NULL);
return REENCODE_ERROR;
}
if (AudioTrackNo > -1)
{
LOG(VB_GENERAL, LOG_INFO,
QString("Set audiotrack number to %1").arg(AudioTrackNo));
GetPlayer()->GetDecoder()->SetTrack(kTrackTypeAudio, AudioTrackNo);
}
long long total_frame_count = GetPlayer()->GetTotalFrameCount();
long long new_frame_count = total_frame_count;
if (honorCutList && m_proginfo)
{
LOG(VB_GENERAL, LOG_INFO, "Honoring the cutlist while transcoding");
frm_dir_map_t::const_iterator it;
QString cutStr;
long long lastStart = 0;
if (deleteMap.size() == 0)
m_proginfo->QueryCutList(deleteMap);
for (it = deleteMap.begin(); it != deleteMap.end(); ++it)
{
if (*it)
{
if (!cutStr.isEmpty())
cutStr += ",";
cutStr += QString("%1-").arg((long)it.key());
lastStart = it.key();
}
else
{
if (cutStr.isEmpty())
cutStr += "0-";
cutStr += QString("%1").arg((long)it.key());
new_frame_count -= (it.key() - lastStart);
}
}
if (cutStr.isEmpty())
cutStr = "Is Empty";
else if (cutStr.endsWith('-') && (total_frame_count > lastStart))
{
new_frame_count -= (total_frame_count - lastStart);
cutStr += QString("%1").arg(total_frame_count);
}
LOG(VB_GENERAL, LOG_INFO, QString("Cutlist : %1").arg(cutStr));
LOG(VB_GENERAL, LOG_INFO, QString("Original Length: %1 frames")