MythTV  master
transcode.cpp
Go to the documentation of this file.
1 #include <cmath>
2 #include <fcntl.h>
3 #include <iostream>
4 #include <memory>
5 
6 #include <QStringList>
7 #include <QMap>
8 #include <QRegExp>
9 #include <QList>
10 #include <QWaitCondition>
11 #include <QMutex>
12 #include <QMutexLocker>
13 #include <QtAlgorithms>
14 
15 #include "mythconfig.h"
16 
17 #include "transcode.h"
18 #include "audiooutput.h"
19 #include "recordingprofile.h"
20 #include "mythcorecontext.h"
21 #include "jobqueue.h"
22 #include "exitcodes.h"
23 #include "mthreadpool.h"
24 #include "deletemap.h"
25 #include "tvremoteutil.h"
26 
27 #if CONFIG_LIBMP3LAME
28 #include "NuppelVideoRecorder.h"
29 #endif
30 #include "mythplayer.h"
31 #include "programinfo.h"
32 #include "mythdbcon.h"
33 #include "avformatwriter.h"
34 #include "HLS/httplivestream.h"
35 
36 #include "videodecodebuffer.h"
37 #include "cutter.h"
38 #include "audioreencodebuffer.h"
39 
40 extern "C" {
41 #include "libavcodec/avcodec.h"
42 #include "libswscale/swscale.h"
43 }
44 #include "mythavutil.h"
45 
46 #include <unistd.h> // for unlink()
47 
48 using namespace std;
49 
50 #define LOC QString("Transcode: ")
51 
53  m_proginfo(pginfo),
54  m_recProfile(new RecordingProfile("Transcoders"))
55 {
56 }
57 
59 {
60 #if CONFIG_LIBMP3LAME
61  delete m_nvr;
62 #endif
63  SetPlayerContext(nullptr);
64  delete m_outRingBuffer;
65  delete m_fifow;
66  delete m_kfaTable;
67  delete m_recProfile;
68 }
69 void Transcode::ReencoderAddKFA(long curframe, long lastkey, long num_keyframes)
70 {
71  long delta = curframe - lastkey;
72  if (delta != 0 && delta != m_keyframeDist)
73  {
74  struct kfatable_entry kfate {};
75  kfate.adjust = m_keyframeDist - delta;
76  kfate.keyframe_number = num_keyframes;
77  m_kfaTable->push_back(kfate);
78  }
79 }
80 
81 bool Transcode::GetProfile(const QString& profileName, const QString& encodingType,
82  int height, int frameRate)
83 {
84  if (profileName.toLower() == "autodetect")
85  {
86  if (height == 1088)
87  height = 1080;
88 
89  QString autoProfileName = QObject::tr("Autodetect from %1").arg(height);
90  if (frameRate == 25 || frameRate == 30)
91  autoProfileName += "i";
92  if (frameRate == 50 || frameRate == 60)
93  autoProfileName += "p";
94 
95  bool result = false;
96  LOG(VB_GENERAL, LOG_NOTICE,
97  QString("Transcode: Looking for autodetect profile: %1")
98  .arg(autoProfileName));
99  result = m_recProfile->loadByGroup(autoProfileName, "Transcoders");
100 
101  if (!result && encodingType == "MPEG-2")
102  {
103  result = m_recProfile->loadByGroup("MPEG2", "Transcoders");
104  autoProfileName = "MPEG2";
105  }
106  if (!result && (encodingType == "MPEG-4" || encodingType == "RTjpeg"))
107  {
108  result = m_recProfile->loadByGroup("RTjpeg/MPEG4",
109  "Transcoders");
110  autoProfileName = "RTjpeg/MPEG4";
111  }
112  if (!result)
113  {
114  LOG(VB_GENERAL, LOG_ERR,
115  QString("Transcode: Couldn't find profile for : %1")
116  .arg(encodingType));
117 
118  return false;
119  }
120 
121  LOG(VB_GENERAL, LOG_NOTICE,
122  QString("Transcode: Using autodetect profile: %1")
123  .arg(autoProfileName));
124  }
125  else
126  {
127  bool isNum = false;
128  int profileID = profileName.toInt(&isNum);
129  // If a bad profile is specified, there will be trouble
130  if (isNum && profileID > 0)
131  m_recProfile->loadByID(profileID);
132  else if (!m_recProfile->loadByGroup(profileName, "Transcoders"))
133  {
134  LOG(VB_GENERAL, LOG_ERR, QString("Couldn't find profile #: %1")
135  .arg(profileName));
136  return false;
137  }
138  }
139  return true;
140 }
141 
143 {
144  if (player_ctx == m_ctx)
145  return;
146 
147  delete m_ctx;
148  m_ctx = player_ctx;
149 }
150 
151 #if CONFIG_LIBMP3LAME
152 static QString get_str_option(RecordingProfile *profile, const QString &name)
153 {
154  const StandardSetting *setting = profile->byName(name);
155  if (setting)
156  return setting->getValue();
157 
158  LOG(VB_GENERAL, LOG_ERR, LOC +
159  QString("get_str_option(...%1): Option not in profile.").arg(name));
160 
161  return QString();
162 }
163 
164 static int get_int_option(RecordingProfile *profile, const QString &name)
165 {
166  QString ret_str = get_str_option(profile, name);
167  if (ret_str.isEmpty())
168  return 0;
169 
170  bool ok = false;
171  int ret_int = ret_str.toInt(&ok);
172 
173  if (!ok)
174  {
175  LOG(VB_GENERAL, LOG_ERR, LOC +
176  QString("get_int_option(...%1): Option is not an int.").arg(name));
177  }
178 
179  return ret_int;
180 }
181 
182 static bool get_bool_option(RecordingProfile *profile, const QString &name)
183 {
184  return get_int_option(profile, name) != 0;
185 }
186 
187 static void TranscodeWriteText(void *ptr, unsigned char *buf, int len,
188  int timecode, int pagenr)
189 {
190  auto *nvr = (NuppelVideoRecorder *)ptr;
191  nvr->WriteText(buf, len, timecode, pagenr);
192 }
193 #endif // CONFIG_LIBMP3LAME
194 
195 int Transcode::TranscodeFile(const QString &inputname,
196  const QString &outputname,
197  const QString &profileName,
198  bool honorCutList, bool framecontrol,
199  int jobID, const QString& fifodir,
200  bool fifo_info, bool cleanCut,
201  frm_dir_map_t &deleteMap,
202  int AudioTrackNo,
203  bool passthru)
204 {
205  QDateTime curtime = MythDate::current();
206  QDateTime statustime = curtime;
207  int audioFrame = 0;
208  std::unique_ptr<Cutter> cutter = nullptr;
209  std::unique_ptr<AVFormatWriter> avfw = nullptr;
210  std::unique_ptr<AVFormatWriter> avfw2 = nullptr;
211  std::unique_ptr<HTTPLiveStream> hls = nullptr;
212  int hlsSegmentSize = 0;
213  int hlsSegmentFrames = 0;
214 
215 #if !CONFIG_LIBMP3LAME
216  (void)profileName;
217 #endif
218 
219  if (jobID >= 0)
220  JobQueue::ChangeJobComment(jobID, "0% " + QObject::tr("Completed"));
221 
222  if (m_hlsMode)
223  {
224  m_avfMode = true;
225 
226  if (m_hlsStreamID != -1)
227  {
228  hls = std::make_unique<HTTPLiveStream>(m_hlsStreamID);
229  hls->UpdateStatus(kHLSStatusStarting);
230  hls->UpdateStatusMessage("Transcoding Starting");
231  m_cmdWidth = hls->GetWidth();
232  m_cmdHeight = hls->GetHeight();
233  m_cmdBitrate = hls->GetBitrate();
234  m_cmdAudioBitrate = hls->GetAudioBitrate();
235  }
236  }
237 
238  if (!m_avfMode)
239  {
240 #if CONFIG_LIBMP3LAME
241  m_nvr = new NuppelVideoRecorder(nullptr, nullptr);
242 #else
243  LOG(VB_GENERAL, LOG_ERR,
244  "Not compiled with libmp3lame support");
245  return REENCODE_ERROR;
246 #endif
247  }
248 
249  // Input setup
250  auto *player_ctx = new PlayerContext(kTranscoderInUseID);
251  player_ctx->SetPlayingInfo(m_proginfo);
252  RingBuffer *rb = (hls && (m_hlsStreamID != -1)) ?
253  RingBuffer::Create(hls->GetSourceFile(), false, false) :
254  RingBuffer::Create(inputname, false, false);
255  if (!rb || !rb->GetLastError().isEmpty())
256  {
257  LOG(VB_GENERAL, LOG_ERR,
258  QString("Transcoding aborted, error: '%1'")
259  .arg(rb? rb->GetLastError() : ""));
260  delete player_ctx;
261  return REENCODE_ERROR;
262  }
263  player_ctx->SetRingBuffer(rb);
264  player_ctx->SetPlayer(new MythPlayer((PlayerFlags)(kVideoIsNull | kNoITV)));
265  SetPlayerContext(player_ctx);
266  GetPlayer()->SetPlayerInfo(nullptr, nullptr, GetPlayerContext());
267  if (m_proginfo->GetRecordingEndTime() > curtime)
268  {
269  player_ctx->SetRecorder(RemoteGetExistingRecorder(m_proginfo));
271  }
272 
273  if (m_showProgress)
274  {
275  statustime = statustime.addSecs(5);
276  }
277 
278  AudioOutput *audioOutput = new AudioReencodeBuffer(FORMAT_NONE, 0,
279  passthru);
280  AudioReencodeBuffer *arb = ((AudioReencodeBuffer*)audioOutput);
281  GetPlayer()->GetAudio()->SetAudioOutput(audioOutput);
282  GetPlayer()->SetTranscoding(true);
283 
284  if (GetPlayer()->OpenFile() < 0)
285  {
286  LOG(VB_GENERAL, LOG_ERR, "Transcoding aborted, error opening file.");
287  SetPlayerContext(nullptr);
288  return REENCODE_ERROR;
289  }
290 
291  if (AudioTrackNo > -1)
292  {
293  LOG(VB_GENERAL, LOG_INFO,
294  QString("Set audiotrack number to %1").arg(AudioTrackNo));
295  GetPlayer()->GetDecoder()->SetTrack(kTrackTypeAudio, AudioTrackNo);
296  }
297 
298  long long total_frame_count = GetPlayer()->GetTotalFrameCount();
299  long long new_frame_count = total_frame_count;
300  if (honorCutList && m_proginfo)
301  {
302  LOG(VB_GENERAL, LOG_INFO, "Honoring the cutlist while transcoding");
303 
304  frm_dir_map_t::const_iterator it;
305  QString cutStr;
306  long long lastStart = 0;
307 
308  if (deleteMap.empty())
309  m_proginfo->QueryCutList(deleteMap);
310 
311  for (it = deleteMap.begin(); it != deleteMap.end(); ++it)
312  {
313  if (*it)
314  {
315  if (!cutStr.isEmpty())
316  cutStr += ",";
317  cutStr += QString("%1-").arg((long)it.key());
318  lastStart = it.key();
319  }
320  else
321  {
322  if (cutStr.isEmpty())
323  cutStr += "0-";
324  cutStr += QString("%1").arg((long)it.key());
325  new_frame_count -= (it.key() - lastStart);
326  }
327  }
328  if (cutStr.isEmpty())
329  cutStr = "Is Empty";
330  else if (cutStr.endsWith('-') && (total_frame_count > lastStart))
331  {
332  new_frame_count -= (total_frame_count - lastStart);
333  cutStr += QString("%1").arg(total_frame_count);
334  }
335  LOG(VB_GENERAL, LOG_INFO, QString("Cutlist : %1").arg(cutStr));
336  LOG(VB_GENERAL, LOG_INFO, QString("Original Length: %1 frames")
337  .arg((long)total_frame_count));
338  LOG(VB_GENERAL, LOG_INFO, QString("New Length : %1 frames")
339  .arg((long)new_frame_count));
340 
341  if ((m_proginfo->QueryIsEditing()) ||
343  {
344  LOG(VB_GENERAL, LOG_INFO, "Transcoding aborted, cutlist changed");
345  SetPlayerContext(nullptr);
347  }
349  curtime = curtime.addSecs(60);
350  }
351 
353  QString encodingType = GetPlayer()->GetEncodingType();
354  bool copyvideo = false;
355  bool copyaudio = false;
356 
357  QString vidsetting = nullptr;
358  QString audsetting = nullptr;
359  QString vidfilters = nullptr;
360 
361  QSize buf_size = GetPlayer()->GetVideoBufferSize();
362  int video_width = buf_size.width();
363  int video_height = buf_size.height();
364 
365  if (video_height == 1088) {
366  LOG(VB_GENERAL, LOG_NOTICE,
367  "Found video height of 1088. This is unusual and "
368  "more than likely the video is actually 1080 so mythtranscode "
369  "will treat it as such.");
370  }
371 
372  DecoderBase* dec = GetPlayer()->GetDecoder();
373  float video_aspect = dec ? dec->GetVideoAspect() : 4.0F / 3.0F;
374  float video_frame_rate = GetPlayer()->GetFrameRate();
375  int newWidth = video_width;
376  int newHeight = video_height;
377  bool halfFramerate = false;
378  bool skippedLastFrame = false;
379 
380  m_kfaTable = new vector<struct kfatable_entry>;
381 
382  if (m_avfMode)
383  {
384  newWidth = m_cmdWidth;
385  newHeight = m_cmdHeight;
386 
387  // Absolutely no purpose is served by scaling video up beyond it's
388  // original resolution, quality is degraded, transcoding is
389  // slower and in future we may wish to scale bitrate according to
390  // resolution, so it would also waste bandwidth (when streaming)
391  //
392  // This change could be said to apply for all transcoding, but for now
393  // we're limiting it to HLS where it's uncontroversial
394  if (m_hlsMode)
395  {
396 // if (newWidth > video_width)
397 // newWidth = video_width;
398  if (newHeight > video_height)
399  {
400  newHeight = video_height;
401  newWidth = 0;
402  }
403  }
404 
405  // TODO: is this necessary? It got commented out, but may still be
406  // needed.
407  // int actualHeight = (video_height == 1088 ? 1080 : video_height);
408 
409  // If height or width are 0, then we need to calculate them
410  if (newHeight == 0 && newWidth > 0)
411  newHeight = (int)(1.0F * newWidth / video_aspect);
412  else if (newWidth == 0 && newHeight > 0)
413  newWidth = (int)(1.0F * newHeight * video_aspect);
414  else if (newWidth == 0 && newHeight == 0)
415  {
416  newHeight = 480;
417  newWidth = (int)(1.0F * 480 * video_aspect);
418  if (newWidth > 640)
419  {
420  newWidth = 640;
421  newHeight = (int)(1.0F * 640 / video_aspect);
422  }
423  }
424 
425  // make sure dimensions are valid for MPEG codecs
426  newHeight = (newHeight + 15) & ~0xF;
427  newWidth = (newWidth + 15) & ~0xF;
428 
429  avfw = std::make_unique<AVFormatWriter>();
430  if (!avfw)
431  {
432  LOG(VB_GENERAL, LOG_ERR,
433  "Transcoding aborted, error creating AVFormatWriter.");
434  SetPlayerContext(nullptr);
435  return REENCODE_ERROR;
436  }
437 
438  avfw->SetVideoBitrate(m_cmdBitrate);
439  avfw->SetHeight(newHeight);
440  avfw->SetWidth(newWidth);
441  avfw->SetAspect(video_aspect);
442  avfw->SetAudioBitrate(m_cmdAudioBitrate);
443  avfw->SetAudioChannels(arb->m_channels);
444  avfw->SetAudioFrameRate(arb->m_eff_audiorate);
445  avfw->SetAudioFormat(FORMAT_S16);
446 
447  if (m_hlsMode)
448  {
449 
450  if (m_hlsStreamID == -1)
451  {
452  hls = std::make_unique<HTTPLiveStream>(inputname, newWidth, newHeight,
454  m_hlsMaxSegments, 0, 0);
455 
456  m_hlsStreamID = hls->GetStreamID();
457  if (!hls || m_hlsStreamID == -1)
458  {
459  LOG(VB_GENERAL, LOG_ERR, "Unable to create new stream");
460  SetPlayerContext(nullptr);
461  return REENCODE_ERROR;
462  }
463  }
464 
465  int segmentSize = hls->GetSegmentSize();
466 
467  LOG(VB_GENERAL, LOG_NOTICE,
468  QString("HLS: Using segment size of %1 seconds")
469  .arg(segmentSize));
470 
472  {
473  int audioOnlyBitrate = hls->GetAudioOnlyBitrate();
474 
475  avfw2 = std::make_unique<AVFormatWriter>();
476  avfw2->SetContainer("mpegts");
477  avfw2->SetAudioCodec("aac");
478  avfw2->SetAudioBitrate(audioOnlyBitrate);
479  avfw2->SetAudioChannels(arb->m_channels);
480  avfw2->SetAudioFrameRate(arb->m_eff_audiorate);
481  avfw2->SetAudioFormat(FORMAT_S16);
482  }
483 
484  avfw->SetContainer("mpegts");
485  avfw->SetVideoCodec("libx264");
486  avfw->SetAudioCodec("aac");
487  hls->UpdateStatus(kHLSStatusStarting);
488  hls->UpdateStatusMessage("Transcoding Starting");
489  hls->UpdateSizeInfo(newWidth, newHeight, video_width, video_height);
490 
491  if (!hls->InitForWrite())
492  {
493  LOG(VB_GENERAL, LOG_ERR, "hls->InitForWrite() failed");
494  SetPlayerContext(nullptr);
495  return REENCODE_ERROR;
496  }
497 
498  if (video_frame_rate > 30)
499  {
500  halfFramerate = true;
501  avfw->SetFramerate(video_frame_rate/2);
502 
503  if (avfw2)
504  avfw2->SetFramerate(video_frame_rate/2);
505 
506  hlsSegmentSize = (int)(segmentSize * video_frame_rate / 2);
507  }
508  else
509  {
510  avfw->SetFramerate(video_frame_rate);
511 
512  if (avfw2)
513  avfw2->SetFramerate(video_frame_rate);
514 
515  hlsSegmentSize = (int)(segmentSize * video_frame_rate);
516  }
517 
518  avfw->SetKeyFrameDist(30);
519  if (avfw2)
520  avfw2->SetKeyFrameDist(30);
521 
522  hls->AddSegment();
523  avfw->SetFilename(hls->GetCurrentFilename());
524  if (avfw2)
525  avfw2->SetFilename(hls->GetCurrentFilename(true));
526  }
527  else
528  {
529  avfw->SetContainer(m_cmdContainer);
530  avfw->SetVideoCodec(m_cmdVideoCodec);
531  avfw->SetAudioCodec(m_cmdAudioCodec);
532  avfw->SetFilename(outputname);
533  avfw->SetFramerate(video_frame_rate);
534  avfw->SetKeyFrameDist(30);
535  }
536 
537  int threads = gCoreContext->GetNumSetting("HTTPLiveStreamThreads", 2);
538  QString preset = gCoreContext->GetSetting("HTTPLiveStreamPreset", "veryfast");
539  QString tune = gCoreContext->GetSetting("HTTPLiveStreamTune", "film");
540 
541  LOG(VB_GENERAL, LOG_NOTICE,
542  QString("x264 HLS using: %1 threads, '%2' profile and '%3' tune")
543  .arg(threads).arg(preset).arg(tune));
544 
545  avfw->SetThreadCount(threads);
546  avfw->SetEncodingPreset(preset);
547  avfw->SetEncodingTune(tune);
548 
549  if (avfw2)
550  avfw2->SetThreadCount(1);
551 
552  if (!avfw->Init())
553  {
554  LOG(VB_GENERAL, LOG_ERR, "avfw->Init() failed");
555  SetPlayerContext(nullptr);
556  return REENCODE_ERROR;
557  }
558 
559  if (!avfw->OpenFile())
560  {
561  LOG(VB_GENERAL, LOG_ERR, "avfw->OpenFile() failed");
562  SetPlayerContext(nullptr);
563  return REENCODE_ERROR;
564  }
565 
566  if (avfw2 && !avfw2->Init())
567  {
568  LOG(VB_GENERAL, LOG_ERR, "avfw2->Init() failed");
569  SetPlayerContext(nullptr);
570  return REENCODE_ERROR;
571  }
572 
573  if (avfw2 && !avfw2->OpenFile())
574  {
575  LOG(VB_GENERAL, LOG_ERR, "avfw2->OpenFile() failed");
576  SetPlayerContext(nullptr);
577  return REENCODE_ERROR;
578  }
579 
580  arb->m_audioFrameSize = avfw->GetAudioFrameSize() * arb->m_channels * 2;
581  }
582 #if CONFIG_LIBMP3LAME
583  else if (fifodir.isEmpty())
584  {
585  if (!GetProfile(profileName, encodingType, video_height,
586  (int)round(video_frame_rate))) {
587  LOG(VB_GENERAL, LOG_ERR, "Transcoding aborted, no profile found.");
588  SetPlayerContext(nullptr);
589  return REENCODE_ERROR;
590  }
591 
592  // For overriding settings on the command line
593  QMap<QString, QString> recorderOptionsMap;
594  if (!m_recorderOptions.isEmpty())
595  {
596  QStringList options = m_recorderOptions
597  .split(",", QString::SkipEmptyParts);
598  int loop = 0;
599  while (loop < options.size())
600  {
601  QStringList tokens = options[loop].split("=");
602  if (tokens.length() < 2)
603  {
604  LOG(VB_GENERAL, LOG_ERR, "Transcoding aborted, invalid option settings.");
605  return REENCODE_ERROR;
606  }
607  recorderOptionsMap[tokens[0]] = tokens[1];
608 
609  loop++;
610  }
611  }
612 
613  vidsetting = get_str_option(m_recProfile, "videocodec");
614  audsetting = get_str_option(m_recProfile, "audiocodec");
615  vidfilters = get_str_option(m_recProfile, "transcodefilters");
616 
617  if (encodingType == "MPEG-2" &&
618  get_bool_option(m_recProfile, "transcodelossless"))
619  {
620  LOG(VB_GENERAL, LOG_NOTICE, "Switching to MPEG-2 transcoder.");
621  SetPlayerContext(nullptr);
622  return REENCODE_MPEG2TRANS;
623  }
624 
625  // Recorder setup
626  if (get_bool_option(m_recProfile, "transcodelossless"))
627  {
628  vidsetting = encodingType;
629  audsetting = "MP3";
630  }
631  else if (get_bool_option(m_recProfile, "transcoderesize"))
632  {
633  int actualHeight = (video_height == 1088 ? 1080 : video_height);
634 
635  //GetPlayer()->SetVideoFilters(vidfilters);
636  newWidth = get_int_option(m_recProfile, "width");
637  newHeight = get_int_option(m_recProfile, "height");
638 
639  // If height or width are 0, then we need to calculate them
640  if (newHeight == 0 && newWidth > 0)
641  newHeight = (int)(1.0 * newWidth * actualHeight / video_width);
642  else if (newWidth == 0 && newHeight > 0)
643  newWidth = (int)(1.0 * newHeight * video_width / actualHeight);
644  else if (newWidth == 0 && newHeight == 0)
645  {
646  newHeight = 480;
647  newWidth = (int)(1.0 * 480 * video_width / actualHeight);
648  if (newWidth > 640)
649  {
650  newWidth = 640;
651  newHeight = (int)(1.0 * 640 * actualHeight / video_width);
652  }
653  }
654 
655  if (encodingType.startsWith("mpeg", Qt::CaseInsensitive))
656  {
657  // make sure dimensions are valid for MPEG codecs
658  newHeight = (newHeight + 15) & ~0xF;
659  newWidth = (newWidth + 15) & ~0xF;
660  }
661 
662  LOG(VB_GENERAL, LOG_INFO, QString("Resizing from %1x%2 to %3x%4")
663  .arg(video_width).arg(video_height)
664  .arg(newWidth).arg(newHeight));
665  }
666  else // lossy and no resize
667  {
668  //GetPlayer()->SetVideoFilters(vidfilters);
669  }
670 
671  // this is ripped from tv_rec SetupRecording. It'd be nice to merge
672  m_nvr->SetOption("inpixfmt", FMT_YV12);
673 
674  m_nvr->SetOption("width", newWidth);
675  m_nvr->SetOption("height", newHeight);
676 
677  m_nvr->SetOption("tvformat", gCoreContext->GetSetting("TVFormat"));
678  m_nvr->SetOption("vbiformat", gCoreContext->GetSetting("VbiFormat"));
679 
680  m_nvr->SetFrameRate(video_frame_rate);
681  m_nvr->SetVideoAspect(video_aspect);
682  m_nvr->SetTranscoding(true);
683 
684  if ((vidsetting == "MPEG-4") ||
685  (recorderOptionsMap["videocodec"] == "mpeg4"))
686  {
687  m_nvr->SetOption("videocodec", "mpeg4");
688 
689  m_nvr->SetIntOption(m_recProfile, "mpeg4bitrate");
690  m_nvr->SetIntOption(m_recProfile, "scalebitrate");
691  m_nvr->SetIntOption(m_recProfile, "mpeg4maxquality");
692  m_nvr->SetIntOption(m_recProfile, "mpeg4minquality");
693  m_nvr->SetIntOption(m_recProfile, "mpeg4qualdiff");
694  m_nvr->SetIntOption(m_recProfile, "mpeg4optionvhq");
695  m_nvr->SetIntOption(m_recProfile, "mpeg4option4mv");
696 #ifdef USING_FFMPEG_THREADS
697  m_nvr->SetIntOption(m_recProfile, "encodingthreadcount");
698 #endif
699  }
700  else if ((vidsetting == "MPEG-2") ||
701  (recorderOptionsMap["videocodec"] == "mpeg2video"))
702  {
703  m_nvr->SetOption("videocodec", "mpeg2video");
704 
705  m_nvr->SetIntOption(m_recProfile, "mpeg2bitrate");
706  m_nvr->SetIntOption(m_recProfile, "scalebitrate");
707 #ifdef USING_FFMPEG_THREADS
708  m_nvr->SetIntOption(m_recProfile, "encodingthreadcount");
709 #endif
710  }
711  else if ((vidsetting == "RTjpeg") ||
712  (recorderOptionsMap["videocodec"] == "rtjpeg"))
713  {
714  m_nvr->SetOption("videocodec", "rtjpeg");
715  m_nvr->SetIntOption(m_recProfile, "rtjpegquality");
716  m_nvr->SetIntOption(m_recProfile, "rtjpegchromafilter");
717  m_nvr->SetIntOption(m_recProfile, "rtjpeglumafilter");
718  }
719  else if (vidsetting.isEmpty())
720  {
721  LOG(VB_GENERAL, LOG_ERR, "No video information found!");
722  LOG(VB_GENERAL, LOG_ERR, "Please ensure that recording profiles "
723  "for the transcoder are set");
724  SetPlayerContext(nullptr);
725  return REENCODE_ERROR;
726  }
727  else
728  {
729  LOG(VB_GENERAL, LOG_ERR,
730  QString("Unknown video codec: %1").arg(vidsetting));
731  SetPlayerContext(nullptr);
732  return REENCODE_ERROR;
733  }
734 
735  m_nvr->SetOption("samplerate", arb->m_eff_audiorate);
736  if (audsetting == "MP3")
737  {
738  m_nvr->SetOption("audiocompression", 1);
739  m_nvr->SetIntOption(m_recProfile, "mp3quality");
740  copyaudio = true;
741  }
742  else if (audsetting == "Uncompressed")
743  {
744  m_nvr->SetOption("audiocompression", 0);
745  }
746  else
747  {
748  LOG(VB_GENERAL, LOG_ERR,
749  QString("Unknown audio codec: %1").arg(audsetting));
750  }
751 
752  m_nvr->AudioInit(true);
753 
754  // For overriding settings on the command line
755  if (!recorderOptionsMap.empty())
756  {
757  QMap<QString, QString>::Iterator it;
758  QString key;
759  QString value;
760  for (it = recorderOptionsMap.begin();
761  it != recorderOptionsMap.end(); ++it)
762  {
763  key = it.key();
764  value = *it;
765 
766  LOG(VB_GENERAL, LOG_NOTICE,
767  QString("Forcing Recorder option '%1' to '%2'")
768  .arg(key).arg(value));
769 
770  if (value.contains(QRegExp("[^0-9]")))
771  m_nvr->SetOption(key, value);
772  else
773  m_nvr->SetOption(key, value.toInt());
774 
775  if (key == "width")
776  newWidth = (value.toInt() + 15) & ~0xF;
777  else if (key == "height")
778  newHeight = (value.toInt() + 15) & ~0xF;
779  else if (key == "videocodec")
780  {
781  if (value == "mpeg4")
782  vidsetting = "MPEG-4";
783  else if (value == "mpeg2video")
784  vidsetting = "MPEG-2";
785  else if (value == "rtjpeg")
786  vidsetting = "RTjpeg";
787  }
788  }
789  }
790 
791  if ((vidsetting == "MPEG-4") ||
792  (vidsetting == "MPEG-2"))
793  m_nvr->SetupAVCodecVideo();
794  else if (vidsetting == "RTjpeg")
795  m_nvr->SetupRTjpeg();
796 
797  m_outRingBuffer = RingBuffer::Create(outputname, true, false);
798  m_nvr->SetRingBuffer(m_outRingBuffer);
799  m_nvr->WriteHeader();
800  m_nvr->StreamAllocate();
801  }
802 
803  if (vidsetting == encodingType && !framecontrol && !m_avfMode &&
804  fifodir.isEmpty() && honorCutList &&
805  video_width == newWidth && video_height == newHeight)
806  {
807  copyvideo = true;
808  LOG(VB_GENERAL, LOG_INFO, "Reencoding video in 'raw' mode");
809  }
810 #endif // CONFIG_LIBMP3LAME
811 
812  if (honorCutList && !deleteMap.empty())
813  {
814  if (cleanCut)
815  {
816  // Have the player seek only part of the way
817  // through a cut, and then use the cutter to
818  // discard the rest
819  cutter = std::make_unique<Cutter>();
820  cutter->SetCutList(deleteMap, m_ctx);
821  GetPlayer()->SetCutList(cutter->AdjustedCutList());
822  }
823  else
824  {
825  // Have the player apply the cut list
826  GetPlayer()->SetCutList(deleteMap);
827  }
828  }
829 
830  GetPlayer()->InitForTranscode(copyaudio, copyvideo);
831  if (GetPlayer()->IsErrored())
832  {
833  LOG(VB_GENERAL, LOG_ERR,
834  "Unable to initialize MythPlayer for Transcode");
835  SetPlayerContext(nullptr);
836  return REENCODE_ERROR;
837  }
838 
839  // must come after InitForTranscode - which creates the VideoOutput instance
840  if (m_hlsMode)
842 
843  VideoFrame frame {};
844  memset(&frame, 0, sizeof(frame));
845  // Do not use padding when compressing to RTjpeg or when in fifomode.
846  // The RTjpeg compressor doesn't know how to handle strides different to
847  // video width.
848  // cppcheck-suppress knownConditionTrueFalse
849  bool nonAligned = vidsetting == "RTjpeg" || !fifodir.isEmpty();
850  bool rescale =
851  (video_width != newWidth) || (video_height != newHeight)
852  || nonAligned;
853 
854  if (rescale)
855  {
856  size_t newSize = 0;
857  if (nonAligned)
858  {
859  // Set a stride identical to actual width, to ease fifo post-conversion process.
860  // 1080i/p video is actually 1088 because of the 16x16 blocks so
861  // we have to fudge the output size here. nuvexport knows how to handle
862  // this and as of right now it is the only app that uses the fifo ability.
863  newSize = GetBufferSize(FMT_YV12, video_width, video_height == 1080 ? 1088 : video_height, 0 /* aligned */);
864  }
865  else
866  {
867  newSize = GetBufferSize(FMT_YV12, newWidth, newHeight);
868  }
869  unsigned char *newFrame = GetAlignedBuffer(newSize);
870  if (!newFrame)
871  {
872  // OOM
873  return REENCODE_ERROR;
874  }
875  if (nonAligned)
876  {
877  // Set a stride identical to actual width, to ease fifo post-conversion process.
878  init(&frame, FMT_YV12, newFrame, video_width, video_height,
879  static_cast<int>(newSize), nullptr, nullptr, -1, -1, 0 /* aligned */);
880  }
881  else
882  {
883  // use default stride size.
884  init(&frame, FMT_YV12, newFrame, newWidth, newHeight, static_cast<int>(newSize));
885  }
886  }
887 
888  if (!fifodir.isEmpty())
889  {
890  AudioPlayer *aplayer = GetPlayer()->GetAudio();
891  const char *audio_codec_name = "unknown";
892 
893  switch(aplayer->GetCodec())
894  {
895  case AV_CODEC_ID_AC3:
896  audio_codec_name = "ac3";
897  break;
898  case AV_CODEC_ID_EAC3:
899  audio_codec_name = "eac3";
900  break;
901  case AV_CODEC_ID_DTS:
902  audio_codec_name = "dts";
903  break;
904  case AV_CODEC_ID_TRUEHD:
905  audio_codec_name = "truehd";
906  break;
907  case AV_CODEC_ID_MP3:
908  audio_codec_name = "mp3";
909  break;
910  case AV_CODEC_ID_MP2:
911  audio_codec_name = "mp2";
912  break;
913  case AV_CODEC_ID_AAC:
914  audio_codec_name = "aac";
915  break;
916  case AV_CODEC_ID_AAC_LATM:
917  audio_codec_name = "aac_latm";
918  break;
919  default:
920  audio_codec_name = "unknown";
921  }
922 
923  if (!arb->m_passthru)
924  audio_codec_name = "raw";
925 
926  // If cutlist is used then get info on first uncut frame
927  if (honorCutList && fifo_info)
928  {
929  bool is_key = false;
930  int did_ff = 0;
931  GetPlayer()->TranscodeGetNextFrame(did_ff, is_key, true);
932 
933  QSize buf_size2 = GetPlayer()->GetVideoBufferSize();
934  video_width = buf_size2.width();
935  video_height = buf_size2.height();
936  video_aspect = GetPlayer()->GetVideoAspect();
937  video_frame_rate = GetPlayer()->GetFrameRate();
938  }
939 
940  // Display details of the format of the fifo data.
941  LOG(VB_GENERAL, LOG_INFO,
942  QString("FifoVideoWidth %1").arg(video_width));
943  LOG(VB_GENERAL, LOG_INFO,
944  QString("FifoVideoHeight %1").arg(video_height));
945  LOG(VB_GENERAL, LOG_INFO,
946  QString("FifoVideoAspectRatio %1").arg(video_aspect));
947  LOG(VB_GENERAL, LOG_INFO,
948  QString("FifoVideoFrameRate %1").arg(video_frame_rate));
949  LOG(VB_GENERAL, LOG_INFO,
950  QString("FifoAudioFormat %1").arg(audio_codec_name));
951  LOG(VB_GENERAL, LOG_INFO,
952  QString("FifoAudioChannels %1").arg(arb->m_channels));
953  LOG(VB_GENERAL, LOG_INFO,
954  QString("FifoAudioSampleRate %1").arg(arb->m_eff_audiorate));
955 
956  if(fifo_info)
957  {
958  // Request was for just the format of fifo data, not for
959  // the actual transcode, so stop here.
960  unlink(outputname.toLocal8Bit().constData());
961  SetPlayerContext(nullptr);
962  if (rescale)
963  {
964  av_freep(&frame.buf);
965  }
966  return REENCODE_OK;
967  }
968 
969  QString audfifo = fifodir + QString("/audout");
970  QString vidfifo = fifodir + QString("/vidout");
971  int audio_size = arb->m_eff_audiorate * arb->m_bytes_per_frame;
972  // framecontrol is true if we want to enforce fifo sync.
973  if (framecontrol)
974  LOG(VB_GENERAL, LOG_INFO, "Enforcing sync on fifos");
975  m_fifow = new FIFOWriter(2, framecontrol);
976 
977  if (!m_fifow->FIFOInit(0, QString("video"), vidfifo, frame.size, 50) ||
978  !m_fifow->FIFOInit(1, QString("audio"), audfifo, audio_size, 25))
979  {
980  LOG(VB_GENERAL, LOG_ERR,
981  "Error initializing fifo writer. Aborting");
982  unlink(outputname.toLocal8Bit().constData());
983  SetPlayerContext(nullptr);
984  if (rescale)
985  {
986  av_freep(&frame.buf);
987  }
988  return REENCODE_ERROR;
989  }
990  LOG(VB_GENERAL, LOG_INFO,
991  QString("Video %1x%2@%3fps Audio rate: %4")
992  .arg(video_width).arg(video_height)
993  .arg(video_frame_rate)
994  .arg(arb->m_eff_audiorate));
995  LOG(VB_GENERAL, LOG_INFO, "Created fifos. Waiting for connection.");
996  }
997 
998 #if CONFIG_LIBMP3LAME
999  bool forceKeyFrames = (m_fifow == nullptr) ? framecontrol : false;
1000  bool writekeyframe = true;
1001  long lastKeyFrame = 0;
1002  int num_keyframes = 0;
1003 #endif
1004 
1005  frm_dir_map_t::iterator dm_iter;
1006 
1007  int did_ff = 0;
1008 
1009  long curFrameNum = 0;
1010  frame.frameNumber = 1;
1011  long totalAudio = 0;
1012  int dropvideo = 0;
1013  // timecode of the last read video frame in input time
1014  long long lasttimecode = 0;
1015  // timecode of the last write video frame in input or output time
1016  long long lastWrittenTime = 0;
1017  // delta between the same video frame in input and output due to applying the cut list
1018  long long timecodeOffset = 0;
1019 
1020  float rateTimeConv = arb->m_eff_audiorate / 1000.0F;
1021  float vidFrameTime = 1000.0F / video_frame_rate;
1022  int wait_recover = 0;
1023  MythVideoOutput *videoOutput = GetPlayer()->GetVideoOutput();
1024  bool is_key = false;
1025  bool first_loop = true;
1026  AVFrame imageIn;
1027  AVFrame imageOut;
1028  struct SwsContext *scontext = nullptr;
1029 
1030  if (m_fifow)
1031  LOG(VB_GENERAL, LOG_INFO, "Dumping Video and Audio data to fifos");
1032  else if (copyaudio)
1033  LOG(VB_GENERAL, LOG_INFO, "Copying Audio while transcoding Video");
1034  else if (m_hlsMode)
1035  LOG(VB_GENERAL, LOG_INFO, "Transcoding for HTTP Live Streaming");
1036  else if (m_avfMode)
1037  LOG(VB_GENERAL, LOG_INFO, "Transcoding to libavformat container");
1038  else
1039  LOG(VB_GENERAL, LOG_INFO, "Transcoding Video and Audio");
1040 
1041  auto *videoBuffer =
1042  new VideoDecodeBuffer(GetPlayer(), videoOutput, honorCutList);
1043  MThreadPool::globalInstance()->start(videoBuffer, "VideoDecodeBuffer");
1044 
1045  QElapsedTimer flagTime;
1046  flagTime.start();
1047 
1048  if (cutter)
1049  cutter->Activate(vidFrameTime * rateTimeConv, total_frame_count);
1050 
1051  bool stopSignalled = false;
1052  VideoFrame *lastDecode = nullptr;
1053 
1054  if (hls)
1055  {
1056  hls->UpdateStatus(kHLSStatusRunning);
1057  hls->UpdateStatusMessage("Transcoding");
1058  }
1059 
1060  while ((!stopSignalled) &&
1061  (lastDecode = videoBuffer->GetFrame(did_ff, is_key)))
1062  {
1063  if (first_loop)
1064  {
1065  copyaudio = GetPlayer()->GetRawAudioState();
1066  first_loop = false;
1067  }
1068 
1069  float new_aspect = lastDecode->aspect;
1070 
1071  if (cutter)
1072  cutter->NewFrame(lastDecode->frameNumber);
1073 
1074 // frame timecode is on input time base
1075  frame.timecode = lastDecode->timecode;
1076 
1077  // if the timecode jumps backwards just use the last frame's timecode plus the duration of a frame
1078  if (frame.timecode < lasttimecode)
1079  frame.timecode = (long long)(lasttimecode + vidFrameTime);
1080 
1081  if (m_fifow)
1082  {
1083  AVPictureFill(&imageIn, lastDecode);
1084  AVPictureFill(&imageOut, &frame);
1085 
1086  scontext = sws_getCachedContext(scontext,
1087  lastDecode->width, lastDecode->height, FrameTypeToPixelFormat(lastDecode->codec),
1088  frame.width, frame.height, FrameTypeToPixelFormat(frame.codec),
1089  SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
1090  // Typically, wee aren't rescaling per say, we're just correcting the stride set by the decoder.
1091  // However, it allows to properly handle recordings that see their resolution change half-way.
1092  sws_scale(scontext, imageIn.data, imageIn.linesize, 0,
1093  lastDecode->height, imageOut.data, imageOut.linesize);
1094 
1095  totalAudio += arb->GetSamples(frame.timecode);
1096  int audbufTime = (int)(totalAudio / rateTimeConv);
1097  int auddelta = frame.timecode - audbufTime;
1098  int vidTime = lroundf(curFrameNum * vidFrameTime);
1099  int viddelta = frame.timecode - vidTime;
1100  int delta = viddelta - auddelta;
1101  int absdelta = delta < 0 ? -delta : delta;
1102  if (absdelta < 500 && absdelta >= vidFrameTime)
1103  {
1104  QString msg = QString("Audio is %1ms %2 video at # %3: "
1105  "auddelta=%4, viddelta=%5")
1106  .arg(absdelta)
1107  .arg(((delta > 0) ? "ahead of" : "behind"))
1108  .arg((int)curFrameNum)
1109  .arg(auddelta)
1110  .arg(viddelta);
1111  LOG(VB_GENERAL, LOG_INFO, msg);
1112  dropvideo = (delta > 0) ? 1 : -1;
1113  wait_recover = 0;
1114  }
1115  else if (delta >= 500 && delta < 10000)
1116  {
1117  if (wait_recover == 0)
1118  {
1119  dropvideo = 5;
1120  wait_recover = 6;
1121  }
1122  else if (wait_recover == 1)
1123  {
1124  // Video is badly lagging. Try to catch up.
1125  int count = 0;
1126  while (delta > vidFrameTime)
1127  {
1128  if (!cutter || !cutter->InhibitDummyFrame())
1129  m_fifow->FIFOWrite(0, frame.buf, frame.size);
1130 
1131  count++;
1132  delta -= (int)vidFrameTime;
1133  }
1134  QString msg = QString("Added %1 blank video frames")
1135  .arg(count);
1136  LOG(VB_GENERAL, LOG_INFO, msg);
1137  curFrameNum += count;
1138  dropvideo = 0;
1139  wait_recover = 0;
1140  }
1141  else
1142  wait_recover--;
1143  }
1144  else
1145  {
1146  dropvideo = 0;
1147  wait_recover = 0;
1148  }
1149 
1150 #if 0
1151  int buflen = (int)(arb->audiobuffer_len / rateTimeConv);
1152  LOG(VB_GENERAL, LOG_DEBUG,
1153  QString("%1: video time: %2 audio time: %3 "
1154  "buf: %4 exp: %5 delta: %6")
1155  .arg(curFrameNum) .arg(frame.timecode)
1156  .arg(arb->last_audiotime) .arg(buflen) .arg(audbufTime)
1157  .arg(delta));
1158 #endif
1159  AudioBuffer *ab = nullptr;
1160  while ((ab = arb->GetData(frame.timecode)) != nullptr)
1161  {
1162  if (!cutter ||
1163  !cutter->InhibitUseAudioFrames(ab->m_frames, &totalAudio))
1164  m_fifow->FIFOWrite(1, ab->data(), ab->size());
1165 
1166  delete ab;
1167  }
1168 
1169  if (dropvideo < 0)
1170  {
1171  if (cutter && cutter->InhibitDropFrame())
1172  m_fifow->FIFOWrite(0, frame.buf, frame.size);
1173 
1174  LOG(VB_GENERAL, LOG_INFO, "Dropping video frame");
1175  dropvideo++;
1176  curFrameNum--;
1177  }
1178  else
1179  {
1180  if (!cutter || !cutter->InhibitUseVideoFrame())
1181  m_fifow->FIFOWrite(0, frame.buf, frame.size);
1182 
1183  if (dropvideo)
1184  {
1185  if (!cutter || !cutter->InhibitDummyFrame())
1186  m_fifow->FIFOWrite(0, frame.buf, frame.size);
1187 
1188  curFrameNum++;
1189  dropvideo--;
1190  }
1191  }
1192  videoOutput->DoneDisplayingFrame(lastDecode);
1194  lasttimecode = frame.timecode;
1195  }
1196  else if (copyaudio)
1197  {
1198 #if CONFIG_LIBMP3LAME
1199  // Encoding from NuppelVideo to NuppelVideo with MP3 audio
1200  // So let's not decode/reencode audio
1201  if (!GetPlayer()->GetRawAudioState())
1202  {
1203  // The Raw state changed during decode. This is not good
1204  LOG(VB_GENERAL, LOG_ERR, "Transcoding aborted, MythPlayer "
1205  "is not in raw audio mode.");
1206 
1207  unlink(outputname.toLocal8Bit().constData());
1208  if (rescale)
1209  {
1210  av_freep(&frame.buf);
1211  }
1212  SetPlayerContext(nullptr);
1213  if (videoBuffer)
1214  videoBuffer->stop();
1215  if (hls)
1216  {
1217  hls->UpdateStatus(kHLSStatusErrored);
1218  hls->UpdateStatusMessage("Transcoding Errored");
1219  }
1220  return REENCODE_ERROR;
1221  }
1222 
1223  if (forceKeyFrames)
1224  writekeyframe = true;
1225  else
1226  {
1227  writekeyframe = is_key;
1228  if (writekeyframe)
1229  {
1230  // Currently, we don't create new sync frames,
1231  // (though we do create new 'I' frames), so we mark
1232  // the key-frames before deciding whether we need a
1233  // new 'I' frame.
1234 
1235  //need to correct the frame# and timecode here
1236  // Question: Is it necessary to change the timecodes?
1237  long sync_offset =
1238  GetPlayer()->UpdateStoredFrameNum(curFrameNum);
1239  m_nvr->UpdateSeekTable(num_keyframes, sync_offset);
1240  ReencoderAddKFA(curFrameNum, lastKeyFrame, num_keyframes);
1241  num_keyframes++;
1242  lastKeyFrame = curFrameNum;
1243 
1244  if (did_ff)
1245  did_ff = 0;
1246  }
1247  }
1248 
1249  if (did_ff == 1)
1250  {
1251  timecodeOffset +=
1252  (frame.timecode - lasttimecode - (int)vidFrameTime);
1253  }
1254  lasttimecode = frame.timecode;
1255 // from here on the timecode is on the output time base
1256  frame.timecode -= timecodeOffset;
1257 
1258  if (!GetPlayer()->WriteStoredData(
1259  m_outRingBuffer, (did_ff == 0), timecodeOffset))
1260  {
1261  if (video_aspect != new_aspect)
1262  {
1263  video_aspect = new_aspect;
1264  m_nvr->SetNewVideoParams(video_aspect);
1265  }
1266 
1267  QSize buf_size3 = GetPlayer()->GetVideoBufferSize();
1268 
1269  if (video_width != buf_size3.width() ||
1270  video_height != buf_size3.height())
1271  {
1272  video_width = buf_size3.width();
1273  video_height = buf_size3.height();
1274 
1275  LOG(VB_GENERAL, LOG_INFO,
1276  QString("Resizing from %1x%2 to %3x%4")
1277  .arg(video_width).arg(video_height)
1278  .arg(newWidth).arg(newHeight));
1279 
1280  }
1281 
1282  if (did_ff == 1)
1283  {
1284  // Create a new 'I' frame if we just processed a cut.
1285  did_ff = 2;
1286  writekeyframe = true;
1287  }
1288 
1289  if (rescale)
1290  {
1291  AVPictureFill(&imageIn, lastDecode);
1292  AVPictureFill(&imageOut, &frame);
1293 
1294  int bottomBand = (lastDecode->height == 1088) ? 8 : 0;
1295  scontext = sws_getCachedContext(scontext,
1296  lastDecode->width, lastDecode->height, FrameTypeToPixelFormat(lastDecode->codec),
1297  frame.width, frame.height, FrameTypeToPixelFormat(frame.codec),
1298  SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
1299 
1300  sws_scale(scontext, imageIn.data, imageIn.linesize, 0,
1301  lastDecode->height - bottomBand,
1302  imageOut.data, imageOut.linesize);
1303  }
1304 
1305  m_nvr->WriteVideo(rescale ? &frame : lastDecode, true, writekeyframe);
1306  }
1308 #else
1309  LOG(VB_GENERAL, LOG_ERR,
1310  "Not compiled with libmp3lame support. Should never get here");
1311  return REENCODE_ERROR;
1312 #endif // CONFIG_LIBMP3LAME
1313  }
1314  else
1315  {
1316  if (did_ff == 1)
1317  {
1318  did_ff = 2;
1319  timecodeOffset +=
1320  (frame.timecode - lasttimecode - (int)vidFrameTime);
1321  }
1322 
1323  if (video_aspect != new_aspect)
1324  {
1325  video_aspect = new_aspect;
1326 #if CONFIG_LIBMP3LAME
1327  if (m_nvr)
1328  m_nvr->SetNewVideoParams(video_aspect);
1329 #endif
1330  }
1331 
1332 
1333  QSize buf_size4 = GetPlayer()->GetVideoBufferSize();
1334 
1335  if (video_width != buf_size4.width() ||
1336  video_height != buf_size4.height())
1337  {
1338  video_width = buf_size4.width();
1339  video_height = buf_size4.height();
1340 
1341  LOG(VB_GENERAL, LOG_INFO,
1342  QString("Resizing from %1x%2 to %3x%4")
1343  .arg(video_width).arg(video_height)
1344  .arg(newWidth).arg(newHeight));
1345  }
1346 
1347  if (rescale)
1348  {
1349  AVPictureFill(&imageIn, lastDecode);
1350  AVPictureFill(&imageOut, &frame);
1351 
1352  int bottomBand = (lastDecode->height == 1088) ? 8 : 0;
1353  scontext = sws_getCachedContext(scontext,
1354  lastDecode->width, lastDecode->height, FrameTypeToPixelFormat(lastDecode->codec),
1355  frame.width, frame.height, FrameTypeToPixelFormat(frame.codec),
1356  SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
1357 
1358  sws_scale(scontext, imageIn.data, imageIn.linesize, 0,
1359  lastDecode->height - bottomBand,
1360  imageOut.data, imageOut.linesize);
1361  }
1362 
1363  // audio is fully decoded, so we need to reencode it
1364  AudioBuffer *ab = nullptr;
1365  while ((ab = arb->GetData(lastWrittenTime)) != nullptr)
1366  {
1367  auto *buf = (unsigned char *)ab->data();
1368  if (m_avfMode)
1369  {
1370  if (did_ff != 1)
1371  {
1372  long long tc = ab->m_time - timecodeOffset;
1373  avfw->WriteAudioFrame(buf, audioFrame, tc);
1374 
1375  if (avfw2)
1376  {
1377  if ((avfw2->GetTimecodeOffset() == -1) &&
1378  (avfw->GetTimecodeOffset() != -1))
1379  {
1380  avfw2->SetTimecodeOffset(
1381  avfw->GetTimecodeOffset());
1382  }
1383 
1384  tc = ab->m_time - timecodeOffset;
1385  avfw2->WriteAudioFrame(buf, audioFrame, tc);
1386  }
1387 
1388  ++audioFrame;
1389  }
1390  }
1391 #if CONFIG_LIBMP3LAME
1392  else
1393  {
1394  m_nvr->SetOption("audioframesize", ab->size());
1395  m_nvr->WriteAudio(buf, audioFrame++,
1396  ab->m_time - timecodeOffset);
1397  if (m_nvr->IsErrored())
1398  {
1399  LOG(VB_GENERAL, LOG_ERR,
1400  "Transcode: Encountered irrecoverable error in "
1401  "NVR::WriteAudio");
1402 
1403  if (rescale)
1404  {
1405  av_freep(&frame.buf);
1406  }
1407  SetPlayerContext(nullptr);
1408  if (videoBuffer)
1409  videoBuffer->stop();
1410  delete ab;
1411  return REENCODE_ERROR;
1412  }
1413  }
1414 #endif
1415  delete ab;
1416  }
1417 
1418  if (!m_avfMode)
1419  {
1420 #if CONFIG_LIBMP3LAME
1421  GetPlayer()->GetCC608Reader()->
1422  TranscodeWriteText(&TranscodeWriteText, (void *)(m_nvr));
1423 #else
1424  LOG(VB_GENERAL, LOG_ERR,
1425  "Not compiled with libmp3lame support");
1426  return REENCODE_ERROR;
1427 #endif
1428  }
1429  lasttimecode = frame.timecode;
1430  frame.timecode -= timecodeOffset;
1431 
1432  if (m_avfMode)
1433  {
1434  if (halfFramerate && !skippedLastFrame)
1435  {
1436  skippedLastFrame = true;
1437  }
1438  else
1439  {
1440  skippedLastFrame = false;
1441 
1442  if ((hls) &&
1443  (avfw->GetFramesWritten()) &&
1444  (hlsSegmentFrames > hlsSegmentSize) &&
1445  (avfw->NextFrameIsKeyFrame()))
1446  {
1447  hls->AddSegment();
1448  avfw->ReOpen(hls->GetCurrentFilename());
1449 
1450  if (avfw2)
1451  avfw2->ReOpen(hls->GetCurrentFilename(true));
1452 
1453  hlsSegmentFrames = 0;
1454  }
1455 
1456  if (avfw->WriteVideoFrame(rescale ? &frame : lastDecode) > 0)
1457  {
1458  lastWrittenTime = frame.timecode + timecodeOffset;
1459  if (hls)
1460  ++hlsSegmentFrames;
1461  }
1462 
1463  }
1464  }
1465 #if CONFIG_LIBMP3LAME
1466  else
1467  {
1468  if (forceKeyFrames)
1469  m_nvr->WriteVideo(rescale ? &frame : lastDecode, true, true);
1470  else
1471  m_nvr->WriteVideo(rescale ? &frame : lastDecode);
1472  lastWrittenTime = frame.timecode + timecodeOffset;
1473  }
1474 #endif
1475  }
1476  if (MythDate::current() > statustime)
1477  {
1478  if (m_showProgress)
1479  {
1480  LOG(VB_GENERAL, LOG_INFO,
1481  QString("Processed: %1 of %2 frames(%3 seconds)").
1482  arg(curFrameNum).arg((long)total_frame_count).
1483  arg((long)(curFrameNum / video_frame_rate)));
1484  }
1485 
1486  if (hls && hls->CheckStop())
1487  {
1488  hls->UpdateStatus(kHLSStatusStopping);
1489  stopSignalled = true;
1490  }
1491 
1492  statustime = MythDate::current().addSecs(5);
1493  }
1494  if (MythDate::current() > curtime)
1495  {
1496  if (honorCutList && m_proginfo && !m_avfMode &&
1498  {
1499  LOG(VB_GENERAL, LOG_NOTICE,
1500  "Transcoding aborted, cutlist updated");
1501 
1502  unlink(outputname.toLocal8Bit().constData());
1503  if (rescale)
1504  {
1505  av_freep(&frame.buf);
1506  }
1507  SetPlayerContext(nullptr);
1508  if (videoBuffer)
1509  videoBuffer->stop();
1510  return REENCODE_CUTLIST_CHANGE;
1511  }
1512 
1513  if ((jobID >= 0) || (VERBOSE_LEVEL_CHECK(VB_GENERAL, LOG_INFO)))
1514  {
1516  {
1517  LOG(VB_GENERAL, LOG_NOTICE,
1518  "Transcoding STOPped by JobQueue");
1519 
1520  unlink(outputname.toLocal8Bit().constData());
1521  if (rescale)
1522  {
1523  av_freep(&frame.buf);
1524  }
1525  SetPlayerContext(nullptr);
1526  if (videoBuffer)
1527  videoBuffer->stop();
1528  if (hls)
1529  {
1530  hls->UpdateStatus(kHLSStatusStopped);
1531  hls->UpdateStatusMessage("Transcoding Stopped");
1532  }
1533  return REENCODE_STOPPED;
1534  }
1535 
1536  float flagFPS = 0.0;
1537  float elapsed = flagTime.elapsed() / 1000.0;
1538  if (elapsed != 0.0F)
1539  flagFPS = curFrameNum / elapsed;
1540 
1541  total_frame_count = GetPlayer()->GetCurrentFrameCount();
1542  int percentage = curFrameNum * 100 / total_frame_count;
1543 
1544  if (hls)
1545  hls->UpdatePercentComplete(percentage);
1546 
1547  if (jobID >= 0)
1548  {
1550  QObject::tr("%1% Completed @ %2 fps.")
1551  .arg(percentage).arg(flagFPS));
1552  }
1553  else
1554  {
1555  LOG(VB_GENERAL, LOG_INFO,
1556  QString("mythtranscode: %1% Completed @ %2 fps.")
1557  .arg(percentage).arg(flagFPS));
1558  }
1559 
1560  }
1561  curtime = MythDate::current().addSecs(20);
1562  }
1563 
1564  curFrameNum++;
1565  frame.frameNumber = 1 + (curFrameNum << 1);
1566 
1567  GetPlayer()->DiscardVideoFrame(lastDecode);
1568  }
1569 
1570  sws_freeContext(scontext);
1571 
1572  if (!m_fifow)
1573  {
1574  if (avfw)
1575  avfw->CloseFile();
1576 
1577  if (avfw2)
1578  avfw2->CloseFile();
1579 
1580  if (!m_avfMode && m_proginfo)
1581  {
1586  }
1587 
1588 #if CONFIG_LIBMP3LAME
1589  if (m_nvr)
1590  {
1591  m_nvr->WriteSeekTable();
1592  if (!m_kfaTable->empty())
1593  m_nvr->WriteKeyFrameAdjustTable(*m_kfaTable);
1594  }
1595 #endif // CONFIG_LIBMP3LAME
1596  } else {
1597  m_fifow->FIFODrain();
1598  }
1599 
1600  if (hls)
1601  {
1602  if (!stopSignalled)
1603  {
1604  hls->UpdateStatus(kHLSStatusCompleted);
1605  hls->UpdateStatusMessage("Transcoding Completed");
1606  hls->UpdatePercentComplete(100);
1607  }
1608  else
1609  {
1610  hls->UpdateStatus(kHLSStatusStopped);
1611  hls->UpdateStatusMessage("Transcoding Stopped");
1612  }
1613  }
1614 
1615  if (videoBuffer)
1616  {
1617  videoBuffer->stop();
1618  }
1619 
1620  if (rescale)
1621  {
1622  av_freep(&frame.buf);
1623  }
1624  SetPlayerContext(nullptr);
1625 
1626  return REENCODE_OK;
1627 }
1628 
1629 /* vim: set expandtab tabstop=4 shiftwidth=4: */
1630 
AudioBuffer * GetData(long long time)
bool m_showProgress
Definition: transcode.h:60
#define REENCODE_MPEG2TRANS
Definition: transcodedefs.h:4
float GetVideoAspect(void) const
Definition: mythplayer.h:212
PlayerContext * GetPlayerContext(void)
Definition: transcode.h:46
void ReencoderAddKFA(long curframe, long lastkey, long num_keyframes)
Definition: transcode.cpp:69
bool GetRawAudioState(void) const
PlayerFlags
Definition: mythplayer.h:85
void SetCutList(const frm_dir_map_t &newCutList)
PlayerContext * m_ctx
Definition: transcode.h:56
static RingBuffer * Create(const QString &Filename, bool Write, bool UseReadAhead=true, int Timeout=kDefaultOpenTimeout, bool StreamOnly=false)
Creates a RingBuffer instance.
Definition: ringbuffer.cpp:104
bool WriteStoredData(RingBuffer *outRingBuffer, bool writevideo, long timecodeOffset)
int m_cmdWidth
Definition: transcode.h:70
long long timecode
Definition: mythframe.h:149
QString GetLastError(void) const
#define round(x)
Definition: mythplayer.cpp:64
int m_hlsMaxSegments
Definition: transcode.h:66
VideoFrameType codec
Definition: mythframe.h:138
bool m_avfMode
Definition: transcode.h:62
struct AVFrame AVFrame
static enum JobCmds GetJobCmd(int jobID)
Definition: jobqueue.cpp:1459
AVPixelFormat FrameTypeToPixelFormat(VideoFrameType type)
Convert VideoFrameType into FFmpeg's PixelFormat equivalent and vice-versa.
Definition: mythavutil.cpp:24
int m_keyframeDist
Definition: transcode.h:52
ProgramInfo * m_proginfo
Definition: transcode.h:50
int keyframe_number
Definition: format.h:132
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString ReinitAudio(void)
static unsigned char * GetAlignedBuffer(size_t Size)
Definition: mythframe.h:678
QString m_recorderOptions
Definition: transcode.h:61
void FlushTxtBuffers(void)
Definition: cc608reader.cpp:32
virtual bool loadByGroup(const QString &name, const QString &group)
float aspect
Definition: mythframe.h:143
virtual QString getValue(void) const
static bool ChangeJobComment(int jobID, const QString &comment="")
Definition: jobqueue.cpp:1013
void SetAudioOutput(AudioOutput *ao)
bool TranscodeGetNextFrame(int &did_ff, bool &is_key, bool honorCutList)
Holds information on recordings and videos.
Definition: programinfo.h:67
void InitForTranscode(bool copyaudio, bool copyvideo)
~Transcode() override
Definition: transcode.cpp:58
void ClearMarkupFlag(MarkTypes type) const
Definition: programinfo.h:623
MythVideoOutput * GetVideoOutput(void)
Definition: mythplayer.h:275
AudioPlayer * GetAudio(void)
Definition: mythplayer.h:225
int AVPictureFill(AVFrame *pic, const VideoFrame *frame, AVPixelFormat fmt)
AVPictureFill Initialise AVFrame pic with content from VideoFrame frame.
Definition: mythavutil.cpp:196
QString m_cmdAudioCodec
Definition: transcode.h:68
#define VERBOSE_LEVEL_CHECK(_MASK_, _LEVEL_)
Definition: mythlogging.h:24
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString GetSetting(const QString &key, const QString &defaultval="")
QString m_cmdContainer
Definition: transcode.h:67
RecordingProfile * m_recProfile
Definition: transcode.h:51
int m_hlsStreamID
Definition: transcode.h:64
bool GetProfile(const QString &profileName, const QString &encodingType, int height, int frameRate)
Definition: transcode.cpp:81
RingBuffer * m_outRingBuffer
Definition: transcode.h:57
int m_cmdHeight
Definition: transcode.h:71
float GetFrameRate(void) const
Definition: mythplayer.h:213
#define REENCODE_OK
Definition: transcodedefs.h:6
AVCodecID GetCodec(void) const
Definition: audioplayer.h:59
bool FIFOInit(int id, const QString &desc, const QString &name, long size, int num_bufs)
Definition: fifowriter.cpp:81
MythPlayer * GetPlayer(void)
Definition: transcode.h:47
QString GetEncodingType(void) const
int m_cmdAudioBitrate
Definition: transcode.h:73
#define LOC
Definition: transcode.cpp:50
This class is to act as a fake audio output device to store the data for reencoding.
void SetPlayerContext(PlayerContext *player_ctx)
Definition: transcode.cpp:142
static void init(VideoFrame *vf, VideoFrameType _codec, unsigned char *_buf, int _width, int _height, int _size, const int *p=nullptr, const int *o=nullptr, float _aspect=-1.0F, double _rate=-1.0F, int _aligned=MYTH_WIDTH_ALIGNMENT)
Definition: mythframe.h:231
static bool IsJobRunning(int jobType, uint chanid, const QDateTime &recstartts)
Definition: jobqueue.cpp:1089
long long frameNumber
Definition: mythframe.h:147
const char * kTranscoderInUseID
void SetPlayerInfo(TV *tv, QWidget *widget, PlayerContext *ctx)
static MThreadPool * globalInstance(void)
int GetNumSetting(const QString &key, int defaultval=0)
#define REENCODE_CUTLIST_CHANGE
Definition: transcodedefs.h:5
long long GetSamples(long long time)
FIFOWriter * m_fifow
Definition: transcode.h:58
float GetVideoAspect(void) const
Definition: decoderbase.h:184
QSize GetVideoBufferSize(void) const
Definition: mythplayer.h:210
void start(QRunnable *runnable, const QString &debugName, int priority=0)
bool m_hlsDisableAudioOnly
Definition: transcode.h:65
long UpdateStoredFrameNum(long curFrameNum)
void DiscardVideoFrame(VideoFrame *buffer)
Places frame in the available frames queue.
Definition: mythplayer.cpp:996
KFATable * m_kfaTable
Definition: transcode.h:59
void SetTranscoding(bool value)
bool QueryIsEditing(void) const
Queries "recorded" table for its "editing" field and returns true if it is set to true.
int height
Definition: mythframe.h:142
int TranscodeFile(const QString &inputname, const QString &outputname, const QString &profileName, bool honorCutList, bool framecontrol, int jobID, const QString &fifodir, bool fifo_info, bool cleanCut, frm_dir_map_t &deleteMap, int AudioTrackNo, bool passthru=false)
Definition: transcode.cpp:195
uint64_t GetTotalFrameCount(void) const
Definition: mythplayer.h:230
Transcode(ProgramInfo *pginfo)
Definition: transcode.cpp:52
void FIFODrain(void)
Definition: fifowriter.cpp:234
char * data(void)
int m_cmdBitrate
Definition: transcode.h:72
QDateTime GetRecordingEndTime(void) const
Approximate time the recording should have ended, did end, or is intended to end.
Definition: programinfo.h:406
static size_t GetBufferSize(VideoFrameType Type, int Width, int Height, int Aligned=MYTH_WIDTH_ALIGNMENT)
Definition: mythframe.h:657
int adjust
Definition: format.h:131
virtual void loadByID(int id)
QMap< uint64_t, MarkTypes > frm_dir_map_t
Frame # -> Mark map.
Definition: programtypes.h:81
Implements a file/stream reader/writer.
virtual CC608Reader * GetCC608Reader(uint=0)
Definition: mythplayer.h:325
bool QueryMarkupFlag(MarkTypes type) const
Returns true iff the speficied mark type is set on frame 0.
virtual int SetTrack(uint Type, int TrackNo)
void FIFOWrite(int id, void *buf, long size)
Definition: fifowriter.cpp:191
#define REENCODE_ERROR
Definition: transcodedefs.h:7
bool m_hlsMode
Definition: transcode.h:63
#define REENCODE_STOPPED
Definition: transcodedefs.h:8
bool QueryCutList(frm_dir_map_t &delMap, bool loadAutosave=false) const
virtual void DoneDisplayingFrame(VideoFrame *Frame)
Releases frame returned from GetLastShownFrame() onto the queue of frames ready for decoding onto.
void SetWatchingRecording(bool mode)
Definition: mythplayer.cpp:214
void ForceDeinterlacer(bool DoubleRate, MythDeintType Deinterlacer)
void ClearPositionMap(MarkTypes type) const
DecoderBase * GetDecoder(void)
Returns the stream decoder currently in use.
Definition: mythplayer.h:308
Definition: format.h:129
QString m_cmdVideoCodec
Definition: transcode.h:69
RemoteEncoder * RemoteGetExistingRecorder(const ProgramInfo *pginfo)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:41
uint64_t GetCurrentFrameCount(void) const