MythTV  master
avformatdecoder.cpp
Go to the documentation of this file.
1 #include "avformatdecoder.h"
2 
3 #include <unistd.h>
4 
5 // C++ headers
6 #include <algorithm>
7 #include <array>
8 #include <cmath>
9 #include <cstdint>
10 #include <iostream>
11 #include <set>
12 
13 extern "C" {
14 #include "libavutil/avutil.h"
15 #include "libavutil/error.h"
16 #include "libavutil/intreadwrite.h" // for AV_RB32 and AV_RB24
17 #include "libavutil/log.h"
18 #include "libavutil/opt.h"
19 #include "libavcodec/avcodec.h"
20 #include "libavformat/avformat.h"
21 #include "libavformat/avio.h"
22 #include "libswscale/swscale.h"
23 #include "libavutil/stereo3d.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/display.h"
26 }
27 
28 #ifdef USING_MEDIACODEC // Android
29 extern "C" {
30 #include "libavcodec/jni.h"
31 }
32 #include <QtAndroidExtras>
33 #endif // Android
34 
35 // regardless of building with V4L2 or not, enable IVTV VBI data
36 // from <linux/videodev2.h> under SPDX-License-Identifier: ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause)
37 /*
38  * V4L2_MPEG_STREAM_VBI_FMT_IVTV:
39  *
40  * Structure of payload contained in an MPEG 2 Private Stream 1 PES Packet in an
41  * MPEG-2 Program Pack that contains V4L2_MPEG_STREAM_VBI_FMT_IVTV Sliced VBI
42  * data
43  *
44  * Note, the MPEG-2 Program Pack and Private Stream 1 PES packet header
45  * definitions are not included here. See the MPEG-2 specifications for details
46  * on these headers.
47  */
48 
49 /* Line type IDs */
55 };
56 // comments for each ID from ivtv_myth.h
57 
58 #include <QFileInfo>
59 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
60 #include <QTextCodec>
61 #else
62 #include <QStringDecoder>
63 #endif // Qt 6
64 
65 #ifdef _WIN32
66 # undef mkdir
67 #endif
68 
69 // MythTV headers
72 #include "libmythbase/iso639.h"
73 #include "libmythbase/lcddevice.h"
74 #include "libmythbase/mythchrono.h"
75 #include "libmythbase/mythconfig.h"
77 #include "libmythbase/mythdate.h"
78 #include "libmythbase/mythdbcon.h"
79 #include "libmythbase/stringutil.h"
80 #include "libmythui/mythuihelper.h"
81 
82 #include "mythtvexp.h"
83 
84 #include "Bluray/mythbdbuffer.h"
85 #include "DVD/mythdvdbuffer.h"
86 #include "captions/cc608decoder.h"
87 #include "captions/cc708decoder.h"
90 #include "io/mythmediabuffer.h"
91 #include "mheg/interactivetv.h"
92 #include "mpeg/atscdescriptors.h"
93 #include "mpeg/dvbdescriptors.h"
94 #include "mpeg/mpegtables.h"
95 #include "bytereader.h"
96 #include "mythavutil.h"
97 #include "mythframe.h"
98 #include "mythhdrvideometadata.h"
99 #include "mythvideoprofile.h"
100 #include "remoteencoder.h"
101 
102 #ifdef _MSC_VER
103 // MSVC isn't C99 compliant...
104 # ifdef AV_TIME_BASE_Q
105 # undef AV_TIME_BASE_Q
106 # endif
107 #define AV_TIME_BASE_Q GetAVTimeBaseQ()
108 
109 __inline AVRational GetAVTimeBaseQ()
110 {
111  AVRational av = {1, AV_TIME_BASE};
112  return av;
113 }
114 #endif
115 
116 #define LOC QString("AFD: ")
117 
118 // Maximum number of sequential invalid data packet errors before we try
119 // switching to software decoder. Packet errors are often seen when using
120 // hardware contexts and, for example, seeking. Hence this needs to be high and
121 // is probably best removed as it is treating the symptoms and not the cause.
122 // See also comment in MythCodecMap::freeCodecContext re trying to free an
123 // active hardware context when it is errored.
124 static constexpr int SEQ_PKT_ERR_MAX { 50 };
125 
126 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
127 static constexpr int16_t kMaxVideoQueueSize = 220;
128 #else
129 static constexpr ssize_t kMaxVideoQueueSize = 220;
130 #endif
131 
132 static bool silence_ffmpeg_logging = false;
133 
134 static QSize get_video_dim(const AVCodecContext &ctx)
135 {
136  return {ctx.width >> ctx.lowres, ctx.height >> ctx.lowres};
137 }
138 static float get_aspect(const AVCodecContext &ctx)
139 {
140  float aspect_ratio = 0.0F;
141 
142  if (ctx.sample_aspect_ratio.num && ctx.height)
143  {
144  aspect_ratio = av_q2d(ctx.sample_aspect_ratio) *
145  static_cast<double>(ctx.width);
146  aspect_ratio /= (float) ctx.height;
147  }
148 
149  if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
150  {
151  if (ctx.height)
152  aspect_ratio = (float)ctx.width / (float)ctx.height;
153  else
154  aspect_ratio = 4.0F / 3.0F;
155  }
156 
157  return aspect_ratio;
158 }
159 static float get_aspect(AVCParser &p)
160 {
161  static constexpr float kDefaultAspect = 4.0F / 3.0F;
162  int asp = p.aspectRatio();
163  switch (asp)
164  {
165  case 0: return kDefaultAspect;
166  case 2: return 4.0F / 3.0F;
167  case 3: return 16.0F / 9.0F;
168  case 4: return 2.21F;
169  default: break;
170  }
171 
172  float aspect_ratio = asp * 0.000001F;
173  if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
174  {
175  if (p.pictureHeight() && p.pictureWidth())
176  {
177  aspect_ratio =
178  (float) p.pictureWidth() /(float) p.pictureHeight();
179  }
180  else
181  {
182  aspect_ratio = kDefaultAspect;
183  }
184  }
185  return aspect_ratio;
186 }
187 
188 
189 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags);
190 #ifdef USING_DXVA2
191 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int flags);
192 #endif
193 
194 // currently unused
195 //static int determinable_frame_size(struct AVCodecContext *avctx)
196 //{
197 // if (/*avctx->codec_id == AV_CODEC_ID_AAC ||*/
198 // avctx->codec_id == AV_CODEC_ID_MP1 ||
199 // avctx->codec_id == AV_CODEC_ID_MP2 ||
200 // avctx->codec_id == AV_CODEC_ID_MP3/* ||
201 // avctx->codec_id == AV_CODEC_ID_CELT*/)
202 // return 1;
203 // return 0;
204 //}
205 
206 #define FAIL(errmsg) do { \
207  LOG(VB_PLAYBACK, LOG_INFO, LOC + (errmsg)); \
208  return false; \
209 } while (false)
210 
216 static bool StreamHasRequiredParameters(AVCodecContext *Context, AVStream *Stream)
217 {
218  switch (Stream->codecpar->codec_type)
219  {
220  // We fail on video first as this is generally the most serious error
221  // and if we have video, we usually have everything else
222  case AVMEDIA_TYPE_VIDEO:
223  if (!Context)
224  FAIL("No codec for video stream");
225  if (!Stream->codecpar->width || !Stream->codecpar->height)
226  FAIL("Unspecified video size");
227  if (Stream->codecpar->format == AV_PIX_FMT_NONE)
228  FAIL("Unspecified video pixel format");
229  // The proprietary RealVideo codecs are not used for TV broadcast
230  // and codec_info_nb_frames was moved to FFStream as it is an internal, private value.
231  //if (Context->codec_id == AV_CODEC_ID_RV30 || Context->codec_id == AV_CODEC_ID_RV40)
232  // if (!Stream->sample_aspect_ratio.num && !Context->sample_aspect_ratio.num && !Stream->codec_info_nb_frames)
233  // FAIL("No frame in rv30/40 and no sar");
234  break;
235  case AVMEDIA_TYPE_AUDIO:
236  if (!Context)
237  FAIL("No codec for audio stream");
238 
239  // These checks are currently disabled as they continually fail but
240  // codec initialisation is fine - which just delays live tv startup.
241  // The worst offender appears to be audio description channel...
242 
243  //if (!Stream->codecpar->frame_size && determinable_frame_size(avctx))
244  // FAIL("Unspecified audio frame size");
245  //if (Stream->codecpar->format == AV_SAMPLE_FMT_NONE)
246  // FAIL("Unspecified audio sample format");
247  //if (!Stream->codecpar->sample_rate)
248  // FAIL("Unspecified audio sample rate");
249  //if (!Stream->codecpar->channels)
250  // FAIL("Unspecified number of audio channels");
251  // if (!Stream->internal->nb_decoded_frames && Context->codec_id == AV_CODEC_ID_DTS)
252  // FAIL("No decodable DTS frames");
253  break;
254 
255  case AVMEDIA_TYPE_SUBTITLE:
256  if (Stream->codecpar->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !Stream->codecpar->width)
257  FAIL("Unspecified subtitle size");
258  break;
259  case AVMEDIA_TYPE_DATA:
260  if (Stream->codecpar->codec_id == AV_CODEC_ID_NONE)
261  return true;
262  break;
263  default:
264  break;
265  }
266 
267  if (Stream->codecpar->codec_id == AV_CODEC_ID_NONE)
268  FAIL("Unknown codec");
269  return true;
270 }
271 
272 static void myth_av_log(void *ptr, int level, const char* fmt, va_list vl)
273 {
275  return;
276 
277  if (VERBOSE_LEVEL_NONE())
278  return;
279 
280  static QString s_fullLine("");
281  static QMutex s_stringLock;
282  uint64_t verbose_mask = VB_LIBAV;
283  LogLevel_t verbose_level = LOG_EMERG;
284 
285  // determine mythtv debug level from av log level
286  switch (level)
287  {
288  case AV_LOG_PANIC:
289  verbose_level = LOG_EMERG;
290  verbose_mask |= VB_GENERAL;
291  break;
292  case AV_LOG_FATAL:
293  verbose_level = LOG_CRIT;
294  verbose_mask |= VB_GENERAL;
295  break;
296  case AV_LOG_ERROR:
297  verbose_level = LOG_ERR;
298  break;
299  case AV_LOG_WARNING:
300  verbose_level = LOG_WARNING;
301  break;
302  case AV_LOG_INFO:
303  verbose_level = LOG_INFO;
304  break;
305  case AV_LOG_VERBOSE:
306  case AV_LOG_DEBUG:
307  case AV_LOG_TRACE:
308  verbose_level = LOG_DEBUG;
309  break;
310  default:
311  return;
312  }
313 
314  if (!VERBOSE_LEVEL_CHECK(verbose_mask, verbose_level))
315  return;
316 
317  s_stringLock.lock();
318  if (s_fullLine.isEmpty() && ptr) {
319  AVClass* avc = *(AVClass**)ptr;
320  s_fullLine = QString("[%1 @ %2] ")
321  .arg(avc->item_name(ptr))
322  .arg((quintptr)avc, QT_POINTER_SIZE * 2, 16, QChar('0'));
323  }
324 
325  s_fullLine += QString::vasprintf(fmt, vl);
326  if (s_fullLine.endsWith("\n"))
327  {
328  LOG(verbose_mask, verbose_level, s_fullLine.trimmed());
329  s_fullLine.truncate(0);
330  }
331  s_stringLock.unlock();
332 }
333 
334 static int get_canonical_lang(const char *lang_cstr)
335 {
336  if (lang_cstr[0] == '\0' || lang_cstr[1] == '\0')
337  {
338  return iso639_str3_to_key("und");
339  }
340  if (lang_cstr[2] == '\0')
341  {
342  QString tmp2 = lang_cstr;
343  QString tmp3 = iso639_str2_to_str3(tmp2);
344  int lang = iso639_str3_to_key(tmp3);
345  return iso639_key_to_canonical_key(lang);
346  }
347  int lang = iso639_str3_to_key(lang_cstr);
348  return iso639_key_to_canonical_key(lang);
349 }
350 
356 static const char* AVMediaTypeToString(enum AVMediaType codec_type)
357 {
358  switch (codec_type)
359  {
360  case AVMEDIA_TYPE_UNKNOWN: return "Unknown";
361  case AVMEDIA_TYPE_VIDEO: return "Video";
362  case AVMEDIA_TYPE_AUDIO: return "Audio";
363  case AVMEDIA_TYPE_DATA: return "Data";
364  case AVMEDIA_TYPE_SUBTITLE: return "Subtitle";
365  case AVMEDIA_TYPE_ATTACHMENT: return "Attachment";
366  default: return "Invalid Codec Type";
367  }
368 }
369 
371  const ProgramInfo &pginfo,
372  PlayerFlags flags)
373  : DecoderBase(parent, pginfo),
374  m_isDbIgnored(gCoreContext->IsDatabaseIgnored()),
375  m_avcParser(new AVCParser()),
376  m_playerFlags(flags),
377  // Closed Caption & Teletext decoders
378  m_ccd608(new CC608Decoder(parent->GetCC608Reader())),
379  m_ccd708(new CC708Decoder(parent->GetCC708Reader())),
380  m_ttd(new TeletextDecoder(parent->GetTeletextReader()))
381 {
382  // this will be deleted and recreated once decoder is set up
384 
385  m_audioSamples = (uint8_t *)av_mallocz(AudioOutput::kMaxSizeBuffer);
387 
388  av_log_set_callback(myth_av_log);
389 
390  m_audioIn.m_sampleSize = -32;// force SetupAudioStream to run once
392 
394  m_audioReadAhead = gCoreContext->GetDurSetting<std::chrono::milliseconds>("AudioReadAhead", 100ms);
395 
396  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("PlayerFlags: 0x%1, AudioReadAhead: %2 msec")
397  .arg(m_playerFlags, 0, 16).arg(m_audioReadAhead.count()));
398 }
399 
401 {
402  while (!m_storedPackets.isEmpty())
403  {
404  AVPacket *pkt = m_storedPackets.takeFirst();
405  av_packet_free(&pkt);
406  }
407 
408  CloseContext();
409  delete m_ccd608;
410  delete m_ccd708;
411  delete m_ttd;
412  delete m_avcParser;
413  delete m_mythCodecCtx;
414 
415  sws_freeContext(m_swsCtx);
416 
417  av_freep(&m_audioSamples);
418 
419  delete m_avfRingBuffer;
420 
421  if (LCD *lcd = LCD::Get())
422  {
423  lcd->setAudioFormatLEDs(AUDIO_AC3, false);
424  lcd->setVideoFormatLEDs(VIDEO_MPG, false);
425  lcd->setVariousLEDs(VARIOUS_HDTV, false);
426  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
427  lcd->setSpeakerLEDs(SPEAKER_71, false); // should clear any and all speaker LEDs
428  }
429 }
430 
432 {
433  return &m_codecMap;
434 }
435 
437 {
438  if (m_ic)
439  {
440  m_avCodecLock.lock();
441  for (uint i = 0; i < m_ic->nb_streams; i++)
442  {
443  AVStream *st = m_ic->streams[i];
445  }
446  m_avCodecLock.unlock();
447  }
448 }
449 
451 {
452  if (m_ic)
453  {
454  CloseCodecs();
455 
456  av_free(m_ic->pb->buffer);
457  av_free(m_ic->pb);
458  avformat_close_input(&m_ic);
459  m_ic = nullptr;
460  }
461  m_avcParser->Reset();
462 }
463 
464 static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
465 {
466  int64_t mask = (lsb_bits < 64) ? (1LL<<lsb_bits)-1 : -1LL;
467  return ((lsb - base_ts)&mask);
468 }
469 
470 std::chrono::milliseconds AvFormatDecoder::NormalizeVideoTimecode(std::chrono::milliseconds timecode)
471 {
472  int64_t start_pts = 0;
473 
474  AVStream *st = nullptr;
475  for (uint i = 0; i < m_ic->nb_streams; i++)
476  {
477  AVStream *st1 = m_ic->streams[i];
478  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
479  {
480  st = st1;
481  break;
482  }
483  }
484  if (!st)
485  return 0ms;
486 
487  if (m_ic->start_time != AV_NOPTS_VALUE)
488  {
489  start_pts = av_rescale(m_ic->start_time,
490  st->time_base.den,
491  AV_TIME_BASE * (int64_t)st->time_base.num);
492  }
493 
494  int64_t pts = av_rescale(timecode.count() / 1000.0,
495  st->time_base.den,
496  st->time_base.num);
497 
498  // adjust for start time and wrap
499  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
500 
501  return millisecondsFromFloat(av_q2d(st->time_base) * pts * 1000);
502 }
503 
504 std::chrono::milliseconds AvFormatDecoder::NormalizeVideoTimecode(AVStream *st,
505  std::chrono::milliseconds timecode)
506 {
507  int64_t start_pts = 0;
508 
509  if (m_ic->start_time != AV_NOPTS_VALUE)
510  {
511  start_pts = av_rescale(m_ic->start_time,
512  st->time_base.den,
513  AV_TIME_BASE * (int64_t)st->time_base.num);
514  }
515 
516  int64_t pts = av_rescale(timecode.count() / 1000.0,
517  st->time_base.den,
518  st->time_base.num);
519 
520  // adjust for start time and wrap
521  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
522 
523  return millisecondsFromFloat(av_q2d(st->time_base) * pts * 1000);
524 }
525 
527 {
528  if (m_ic && m_ic->nb_chapters > 1)
529  return m_ic->nb_chapters;
530  return 0;
531 }
532 
533 void AvFormatDecoder::GetChapterTimes(QList<std::chrono::seconds> &times)
534 {
535  int total = GetNumChapters();
536  if (!total)
537  return;
538 
539  for (int i = 0; i < total; i++)
540  {
541  int num = m_ic->chapters[i]->time_base.num;
542  int den = m_ic->chapters[i]->time_base.den;
543  int64_t start = m_ic->chapters[i]->start;
544  long double total_secs = (long double)start * (long double)num /
545  (long double)den;
546  times.push_back(std::chrono::seconds((long long)total_secs));
547  }
548 }
549 
550 int AvFormatDecoder::GetCurrentChapter(long long framesPlayed)
551 {
552  if (!GetNumChapters())
553  return 0;
554 
555  for (int i = (m_ic->nb_chapters - 1); i > -1 ; i--)
556  {
557  int num = m_ic->chapters[i]->time_base.num;
558  int den = m_ic->chapters[i]->time_base.den;
559  int64_t start = m_ic->chapters[i]->start;
560  long double total_secs = (long double)start * (long double)num /
561  (long double)den;
562  auto framenum = (long long)(total_secs * m_fps);
563  if (framesPlayed >= framenum)
564  {
565  LOG(VB_PLAYBACK, LOG_INFO, LOC +
566  QString("GetCurrentChapter(selected chapter %1 framenum %2)")
567  .arg(i + 1).arg(framenum));
568  return i + 1;
569  }
570  }
571  return 0;
572 }
573 
574 long long AvFormatDecoder::GetChapter(int chapter)
575 {
576  if (chapter < 1 || chapter > GetNumChapters())
577  return -1;
578 
579  int num = m_ic->chapters[chapter - 1]->time_base.num;
580  int den = m_ic->chapters[chapter - 1]->time_base.den;
581  int64_t start = m_ic->chapters[chapter - 1]->start;
582  long double total_secs = (long double)start * (long double)num /
583  (long double)den;
584  auto framenum = (long long)(total_secs * m_fps);
585  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("GetChapter %1: framenum %2")
586  .arg(chapter).arg(framenum));
587  return framenum;
588 }
589 
590 bool AvFormatDecoder::DoRewind(long long desiredFrame, bool discardFrames)
591 {
592  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("DoRewind(%1, %2 discard frames)")
593  .arg(desiredFrame).arg( discardFrames ? "do" : "don't" ));
594 
596  return DecoderBase::DoRewind(desiredFrame, discardFrames);
597 
598  m_doRewind = true;
599 
600  // avformat-based seeking
601  return DoFastForward(desiredFrame, discardFrames);
602 }
603 
604 bool AvFormatDecoder::DoFastForward(long long desiredFrame, bool discardFrames)
605 {
606  LOG(VB_PLAYBACK, LOG_INFO, LOC +
607  QString("DoFastForward(%1 (%2), %3 discard frames)")
608  .arg(desiredFrame).arg(m_framesPlayed)
609  .arg((discardFrames) ? "do" : "don't"));
610 
612  return DecoderBase::DoFastForward(desiredFrame, discardFrames);
613 
614  bool oldrawstate = m_getRawFrames;
615  m_getRawFrames = false;
616 
617  int seekDelta = desiredFrame - m_framesPlayed;
618 
619  // avoid using av_frame_seek if we are seeking frame-by-frame when paused
620  if (seekDelta >= 0 && seekDelta < 2 && !m_doRewind && m_parent->GetPlaySpeed() == 0.0F)
621  {
622  SeekReset(m_framesPlayed, seekDelta, false, true);
624  m_getRawFrames = oldrawstate;
625  return true;
626  }
627 
628  long long ts = 0;
629  if (m_ic->start_time != AV_NOPTS_VALUE)
630  ts = m_ic->start_time;
631 
632  // convert framenumber to normalized timestamp
633  long double seekts = desiredFrame * AV_TIME_BASE / m_fps;
634  ts += (long long)seekts;
635 
636  // XXX figure out how to do snapping in this case
637  bool exactseeks = DecoderBase::GetSeekSnap() == 0U;
638 
639  int flags = (m_doRewind || exactseeks) ? AVSEEK_FLAG_BACKWARD : 0;
640 
641  if (av_seek_frame(m_ic, -1, ts, flags) < 0)
642  {
643  LOG(VB_GENERAL, LOG_ERR, LOC +
644  QString("av_seek_frame(ic, -1, %1, 0) -- error").arg(ts));
645  m_getRawFrames = oldrawstate;
646  return false;
647  }
648 
649  int normalframes = 0;
650 
651  {
652  m_framesPlayed = desiredFrame;
653  m_fpsSkip = 0;
654  m_framesRead = desiredFrame;
655  normalframes = 0;
656  }
657 
658  SeekReset(m_lastKey, normalframes, true, discardFrames);
659 
660  if (discardFrames)
662 
663  m_doRewind = false;
664 
665  m_getRawFrames = oldrawstate;
666 
667  return true;
668 }
669 
670 void AvFormatDecoder::SeekReset(long long newKey, uint skipFrames,
671  bool doflush, bool discardFrames)
672 {
673  if (!m_ringBuffer)
674  return; // nothing to reset...
675 
676  LOG(VB_PLAYBACK, LOG_INFO, LOC +
677  QString("SeekReset(%1, %2, %3 flush, %4 discard)")
678  .arg(newKey).arg(skipFrames)
679  .arg((doflush) ? "do" : "don't",
680  (discardFrames) ? "do" : "don't"));
681 
682  DecoderBase::SeekReset(newKey, skipFrames, doflush, discardFrames);
683 
684  QMutexLocker locker(&m_avCodecLock);
685 
686  // Discard all the queued up decoded frames
687  if (discardFrames)
688  {
689  bool releaseall = m_mythCodecCtx ? (m_mythCodecCtx->DecoderWillResetOnFlush() ||
690  m_mythCodecCtx->DecoderNeedsReset(nullptr)) : false;
691  m_parent->DiscardVideoFrames(doflush, doflush && releaseall);
692  }
693 
694  if (doflush)
695  {
696  m_lastAPts = 0ms;
697  m_lastVPts = 0ms;
698  m_lastCcPtsu = 0us;
699  m_faultyPts = m_faultyDts = 0;
702  m_ptsDetected = false;
703  m_reorderedPtsDetected = false;
704 
705  avformat_flush(m_ic);
706 
707  // Only reset the internal state if we're using our seeking,
708  // not when using libavformat's seeking
710  {
711  m_ic->pb->pos = m_ringBuffer->GetReadPosition();
712  m_ic->pb->buf_ptr = m_ic->pb->buffer;
713  m_ic->pb->buf_end = m_ic->pb->buffer;
714  m_ic->pb->eof_reached = 0;
715  }
716 
717  // Flush the avcodec buffers
718  LOG(VB_PLAYBACK, LOG_INFO, LOC + "SeekReset() flushing");
719  for (uint i = 0; i < m_ic->nb_streams; i++)
720  {
721  AVCodecContext *enc = m_codecMap.FindCodecContext(m_ic->streams[i]);
722  // note that contexts that have not been opened have
723  // enc->internal = nullptr and cause a segfault in
724  // avcodec_flush_buffers
725  if (enc && enc->internal)
726  avcodec_flush_buffers(enc);
727  }
728 
729  // Free up the stored up packets
730  while (!m_storedPackets.isEmpty())
731  {
732  AVPacket *pkt = m_storedPackets.takeFirst();
733  av_packet_free(&pkt);
734  }
735 
736  m_prevGopPos = 0;
737  m_gopSet = false;
738  }
739 
740  // Skip all the desired number of skipFrames
741 
742  // Some seeks can be very slow. The most common example comes
743  // from HD-PVR recordings, where keyframes are 128 frames apart
744  // and decoding (even hardware decoding) may not be much faster
745  // than realtime, causing some exact seeks to take 2-4 seconds.
746  // If exact seeking is not required, we take some shortcuts.
747  // First, we impose an absolute maximum time we are willing to
748  // spend (maxSeekTimeMs) on the forward frame-by-frame skip.
749  // After that much time has elapsed, we give up and stop the
750  // frame-by-frame seeking. Second, after skipping a few frames,
751  // we predict whether the situation is hopeless, i.e. the total
752  // skipping would take longer than giveUpPredictionMs, and if so,
753  // stop skipping right away.
754  bool exactSeeks = GetSeekSnap() == 0U;
755  static constexpr std::chrono::milliseconds maxSeekTimeMs { 200ms };
756  int profileFrames = 0;
758  for (; (skipFrames > 0 && !m_atEof &&
759  (exactSeeks || begin.elapsed() < maxSeekTimeMs));
760  --skipFrames, ++profileFrames)
761  {
762  // TODO this won't work well in conjunction with the MythTimer
763  // above...
764  QElapsedTimer getframetimer;
765  getframetimer.start();
766  bool retry = true;
767  while (retry && !getframetimer.hasExpired(100))
768  {
769  retry = false;
770  GetFrame(kDecodeVideo, retry);
771  if (retry)
772  std::this_thread::sleep_for(1ms);
773  }
774 
776  {
778  m_decodedVideoFrame = nullptr;
779  }
780  if (!exactSeeks && profileFrames >= 5 && profileFrames < 10)
781  {
782  const int giveUpPredictionMs = 400;
783  int remainingTimeMs =
784  skipFrames * (float)begin.elapsed().count() / profileFrames;
785  if (remainingTimeMs > giveUpPredictionMs)
786  {
787  LOG(VB_PLAYBACK, LOG_DEBUG,
788  QString("Frame-by-frame seeking would take "
789  "%1 ms to finish, skipping.").arg(remainingTimeMs));
790  break;
791  }
792  }
793  }
794 
795  if (doflush)
796  {
797  m_firstVPts = 0ms;
798  m_firstVPtsInuse = true;
799  }
800 }
801 
803 {
804  if (!eof && m_ic && m_ic->pb)
805  {
806  LOG(VB_GENERAL, LOG_NOTICE, LOC +
807  QString("Resetting byte context eof (livetv %1 was eof %2)")
808  .arg(m_livetv).arg(m_ic->pb->eof_reached));
809  m_ic->pb->eof_reached = 0;
810  }
811  DecoderBase::SetEof(eof);
812 }
813 
814 void AvFormatDecoder::Reset(bool reset_video_data, bool seek_reset,
815  bool reset_file)
816 {
817  LOG(VB_PLAYBACK, LOG_INFO, LOC +
818  QString("Reset: Video %1, Seek %2, File %3")
819  .arg(reset_video_data).arg(seek_reset).arg(reset_file));
820 
821  if (seek_reset)
822  SeekReset(0, 0, true, false);
823 
824  DecoderBase::Reset(reset_video_data, false, reset_file);
825 
826  if (reset_video_data)
827  {
828  m_seenGop = false;
829  m_seqCount = 0;
830  }
831 }
832 
833 bool AvFormatDecoder::CanHandle(TestBufferVec & testbuf, const QString &filename)
834 {
835  AVProbeData probe;
836  memset(&probe, 0, sizeof(AVProbeData));
837 
838  QByteArray fname = filename.toLatin1();
839  probe.filename = fname.constData();
840  probe.buf = (unsigned char *)testbuf.data();
841  probe.buf_size = testbuf.size();
842 
843  int score = AVPROBE_SCORE_MAX/4;
844 
845  if (testbuf.size() + AVPROBE_PADDING_SIZE > kDecoderProbeBufferSize)
846  {
847  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
848  score = 0;
849  }
850  else if (testbuf.size()*2 >= kDecoderProbeBufferSize)
851  {
852  score--;
853  }
854 
855  memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
856 
857  return av_probe_input_format2(&probe, static_cast<int>(true), &score) != nullptr;
858 }
859 
861 {
862  int buf_size = m_ringBuffer->BestBufferSize();
863  bool streamed = m_ringBuffer->IsStreamed();
865  m_readContext.flags = AVIO_FLAG_READ;
866  m_readContext.is_streamed = static_cast<int>(streamed);
867  m_readContext.max_packet_size = 0;
868  m_readContext.priv_data = m_avfRingBuffer;
869  auto *buffer = (unsigned char *)av_malloc(buf_size);
870  m_ic->pb = avio_alloc_context(buffer, buf_size, 0,
871  &m_readContext,
875 
876  // We can always seek during LiveTV
877  m_ic->pb->seekable = static_cast<int>(!streamed || forceseek);
878  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Buffer size: %1 Streamed %2 Seekable %3 Available %4")
879  .arg(buf_size).arg(streamed).arg(m_ic->pb->seekable).arg(m_ringBuffer->GetReadBufAvail()));
880 }
881 
882 extern "C" void HandleStreamChange(void *data)
883 {
884  auto *decoder = reinterpret_cast<AvFormatDecoder*>(data);
885 
886  int cnt = decoder->m_ic->nb_streams;
887 
888  LOG(VB_PLAYBACK, LOG_INFO, LOC +
889  QString("streams_changed 0x%1 -- stream count %2")
890  .arg((uint64_t)data,0,16).arg(cnt));
891 
892  decoder->m_streamsChanged = true;
893 }
894 
896 {
897  m_avCodecLock.lock();
898  int retval = avformat_find_stream_info(m_ic, nullptr);
899  m_avCodecLock.unlock();
900  silence_ffmpeg_logging = false;
901  // ffmpeg 3.0 is returning -1 code when there is a channel
902  // change or some encoding error just after the start
903  // of the file, but is has found the correct stream info
904  // Set rc to 0 so that playing can continue.
905  if (retval == -1)
906  retval = 0;
907  return retval;
908 }
909 
925  TestBufferVec & testbuf)
926 {
927  CloseContext();
928 
930 
931  // Process frames immediately unless we're decoding
932  // a DVD, in which case don't so that we don't show
933  // anything whilst probing the data streams.
935 
936  delete m_avfRingBuffer;
938 
939  const AVInputFormat *fmt = nullptr;
940  QString fnames = m_ringBuffer->GetFilename();
941  QByteArray fnamea = fnames.toLatin1();
942  const char *filename = fnamea.constData();
943 
944  AVProbeData probe;
945  memset(&probe, 0, sizeof(AVProbeData));
946  probe.filename = filename;
947  probe.buf = reinterpret_cast<unsigned char *>(testbuf.data());
948  if (testbuf.size() + AVPROBE_PADDING_SIZE <= kDecoderProbeBufferSize)
949  probe.buf_size = testbuf.size();
950  else
951  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
952  memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
953 
954  fmt = av_probe_input_format(&probe, static_cast<int>(true));
955  if (!fmt)
956  {
957  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Probe failed for '%1'").arg(filename));
958  return -1;
959  }
960 
961  if (strcmp(fmt->name, "mpegts") == 0 &&
962  gCoreContext->GetBoolSetting("FFMPEGTS", false))
963  {
964  const AVInputFormat *fmt2 = av_find_input_format("mpegts-ffmpeg");
965  if (fmt2)
966  {
967  fmt = fmt2;
968  LOG(VB_GENERAL, LOG_INFO, LOC + "Using FFmpeg MPEG-TS demuxer (forced)");
969  }
970  }
971 
972  int err = 0;
973  bool scancomplete = false;
974  int remainingscans = 5;
975 
976  while (!scancomplete && remainingscans--)
977  {
978  bool found = false;
979 
980  // With live tv, the ringbufer may contain insufficient data for complete
981  // initialisation so we try a few times with a slight pause each time to
982  // allow extra data to become available. In the worst case scenarios, the
983  // stream may not have a keyframe for 4-5 seconds.
984  // As a last resort, we will try and fallback to the original FFmpeg MPEG-TS
985  // demuxer if it is not already used.
986  // For regular videos, this shouldn't be an issue as the complete file
987  // should be available - though we try a little harder for streamed formats
988  int retries = m_livetv || m_ringBuffer->IsStreamed() ? 50 : 10;
989 
990  while (!found && --retries)
991  {
992  m_ic = avformat_alloc_context();
993  if (!m_ic)
994  {
995  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not allocate format context.");
996  return -1;
997  }
998 
999  m_avfRingBuffer->SetInInit(false);
1001 
1002  err = avformat_open_input(&m_ic, filename, fmt, nullptr);
1003  if (err < 0)
1004  {
1005  std::string error;
1006  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Failed to open input ('%1')")
1007  .arg(av_make_error_stdstring(error, err)));
1008 
1009  // note - m_ic (AVFormatContext) is freed on failure
1010  if (retries > 1)
1011  {
1012  // wait a little to buffer more data
1013  // 50*0.1 = 5 seconds max
1014  QThread::usleep(100000);
1015  // resets the read position
1016  m_avfRingBuffer->SetInInit(false);
1017  continue;
1018  }
1019 
1020  if (strcmp(fmt->name, "mpegts") == 0)
1021  {
1022  fmt = av_find_input_format("mpegts-ffmpeg");
1023  if (fmt)
1024  {
1025  LOG(VB_GENERAL, LOG_ERR, LOC + "Attempting to use original FFmpeg MPEG-TS demuxer.");
1026  // resets the read position
1027  m_avfRingBuffer->SetInInit(false);
1028  continue;
1029  }
1030  break;
1031  }
1032  }
1033  found = true;
1034  }
1035 
1036  if (err < 0)
1037  {
1038  LOG(VB_GENERAL, LOG_ERR, LOC + "Fatal error opening input. Aborting");
1039  m_ic = nullptr;
1040  return -1;
1041  }
1042 
1043  // With certain streams, we don't get a complete stream analysis and the video
1044  // codec/frame format is not fully detected. This can have various consequences - from
1045  // failed playback to not enabling hardware decoding (as the frame formt is not valid).
1046  // Bump the duration (FFmpeg defaults to 5 seconds) to 60 seconds. This should
1047  // not impact performance as in the vast majority of cases the scan is completed
1048  // within a second or two (seconds in this case referring to stream duration - not the time
1049  // it takes to complete the scan).
1050  m_ic->max_analyze_duration = 60LL * AV_TIME_BASE;
1051 
1053  err = FindStreamInfo();
1054  if (err < 0)
1055  {
1056  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find codec parameters for '%1'").arg(filename));
1057  CloseContext();
1058  return -1;
1059  }
1060  m_avfRingBuffer->SetInInit(false);
1061 
1062  // final sanity check that scanned streams are valid for live tv
1063  scancomplete = true;
1064  for (uint i = 0; m_livetv && (i < m_ic->nb_streams); i++)
1065  {
1066  if (!StreamHasRequiredParameters(m_codecMap.GetCodecContext(m_ic->streams[i]), m_ic->streams[i]))
1067  {
1068  scancomplete = false;
1069  if (remainingscans)
1070  {
1071  CloseContext();
1072  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Stream scan incomplete - retrying");
1073  QThread::usleep(250000); // wait 250ms
1074  }
1075  break;
1076  }
1077  }
1078  }
1079 
1080  if (!scancomplete)
1081  LOG(VB_GENERAL, LOG_WARNING, LOC + "Scan incomplete - playback may not work");
1082 
1083  m_ic->streams_changed = HandleStreamChange;
1084  m_ic->stream_change_data = this;
1085 
1086  if (!m_livetv && !m_ringBuffer->IsDisc())
1087  {
1088  // generate timings based on the video stream to avoid bogus ffmpeg
1089  // values for duration and bitrate
1091  }
1092 
1093  // FLAC, MP3 or M4A file may contains an artwork image, a single frame MJPEG,
1094  // we need to ignore it as we don't handle single frames or images in place of video
1095  // TODO: display single frame
1096  QString extension = QFileInfo(fnames).suffix();
1097  if (strcmp(fmt->name, "mp3") == 0 || strcmp(fmt->name, "flac") == 0 ||
1098  strcmp(fmt->name, "ogg") == 0 ||
1099  (extension.compare("m4a", Qt::CaseInsensitive) == 0))
1100  {
1101  novideo = true;
1102  }
1103 
1104  // Scan for the initial A/V streams
1105  err = ScanStreams(novideo);
1106  if (-1 == err)
1107  {
1108  CloseContext();
1109  return err;
1110  }
1111 
1112  AutoSelectTracks(); // This is needed for transcoder
1113 
1114 #ifdef USING_MHEG
1115  {
1116  int initialAudio = -1;
1117  int initialVideo = -1;
1118  if (m_itv || (m_itv = m_parent->GetInteractiveTV()))
1119  m_itv->GetInitialStreams(initialAudio, initialVideo);
1120  if (initialAudio >= 0)
1121  SetAudioByComponentTag(initialAudio);
1122  if (initialVideo >= 0)
1123  SetVideoByComponentTag(initialVideo);
1124  }
1125 #endif // USING_MHEG
1126 
1127  // Try to get a position map from the recorder if we don't have one yet.
1129  {
1131  {
1134  {
1135  m_hasFullPositionMap = true;
1136  m_gopSet = true;
1137  }
1138  }
1139  }
1140 
1141  // If watching pre-recorded television or video use the marked duration
1142  // from the db if it exists, else ffmpeg duration
1143  std::chrono::seconds dur = 0s;
1144 
1145  if (m_playbackInfo)
1146  {
1147  dur = duration_cast<std::chrono::seconds>(m_playbackInfo->QueryTotalDuration());
1148  }
1149 
1150  if (dur == 0s)
1151  {
1152  dur = duration_cast<std::chrono::seconds>(av_duration(m_ic->duration));
1153  }
1154 
1155  if (dur > 0s && !m_livetv && !m_watchingRecording)
1156  {
1157  m_parent->SetDuration(dur);
1158  }
1159 
1160  // If we don't have a position map, set up ffmpeg for seeking
1162  {
1163  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1164  "Recording has no position -- using libavformat seeking.");
1165 
1166  if (dur > 0s)
1167  {
1168  m_parent->SetFileLength(dur, (int)(dur.count() * m_fps));
1169  }
1170  else
1171  {
1172  // the pvr-250 seems to over report the bitrate by * 2
1173  float bytespersec = (float)m_bitrate / 8 / 2;
1174  float secs = m_ringBuffer->GetRealFileSize() * 1.0F / bytespersec;
1176  (int)(secs * static_cast<float>(m_fps)));
1177  }
1178 
1179  // we will not see a position map from db or remote encoder,
1180  // set the gop interval to 15 frames. if we guess wrong, the
1181  // auto detection will change it.
1182  m_keyframeDist = 15;
1184 
1185  if (strcmp(fmt->name, "avi") == 0)
1186  {
1187  // avi keyframes are too irregular
1188  m_keyframeDist = 1;
1189  }
1190 
1191  m_dontSyncPositionMap = true;
1192  }
1193 
1194  av_dump_format(m_ic, 0, filename, 0);
1195 
1196  // print some useful information if playback debugging is on
1198  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Position map found");
1199  else if (m_recordingHasPositionMap)
1200  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Partial position map found");
1201  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1202  QString("Successfully opened decoder for file: \"%1\". novideo(%2)")
1203  .arg(filename).arg(novideo));
1204 
1205  // Print AVChapter information
1206  for (unsigned int i=0; i < m_ic->nb_chapters; i++)
1207  {
1208  int num = m_ic->chapters[i]->time_base.num;
1209  int den = m_ic->chapters[i]->time_base.den;
1210  int64_t start = m_ic->chapters[i]->start;
1211  auto total_secs = static_cast<long double>(start) * static_cast<long double>(num) /
1212  static_cast<long double>(den);
1213  auto msec = millisecondsFromFloat(total_secs * 1000);
1214  auto framenum = static_cast<long long>(total_secs * static_cast<long double>(m_fps));
1215  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1216  QString("Chapter %1 found @ [%2]->%3")
1217  .arg(StringUtil::intToPaddedString(i + 1),
1218  MythDate::formatTime(msec, "HH:mm:ss.zzz"),
1219  QString::number(framenum)));
1220  }
1221 
1222  if (qEnvironmentVariableIsSet("FORCE_DTS_TIMESTAMPS"))
1223  m_forceDtsTimestamps = true;
1224 
1225  if (m_ringBuffer->IsDVD())
1226  {
1227  // Reset DVD playback and clear any of
1228  // our buffers so that none of the data
1229  // parsed so far to determine decoders
1230  // gets shown.
1232  return -1;
1234 
1235  Reset(true, true, true);
1236 
1237  // Now we're ready to process and show frames
1238  m_processFrames = true;
1239  }
1240 
1241 
1242  // Return true if recording has position map
1243  return static_cast<int>(m_recordingHasPositionMap);
1244 }
1245 
1246 float AvFormatDecoder::GetVideoFrameRate(AVStream *Stream, AVCodecContext *Context, bool Sanitise)
1247 {
1248  // MKV default_duration
1249  double avg_fps = (Stream->avg_frame_rate.den == 0) ? 0.0 : av_q2d(Stream->avg_frame_rate);
1250  double codec_fps = av_q2d(Context->framerate); // {0, 1} when unknown
1251  double container_fps = (Stream->time_base.num == 0) ? 0.0 : av_q2d(av_inv_q(Stream->time_base));
1252  // least common multiple of all framerates in a stream; this is a guess
1253  double estimated_fps = (Stream->r_frame_rate.den == 0) ? 0.0 : av_q2d(Stream->r_frame_rate);
1254 
1255  // build a list of possible rates, best first
1256  std::vector<double> rates;
1257  rates.reserve(7);
1258 
1259  // matroska demuxer sets the default_duration to avg_frame_rate
1260  // mov,mp4,m4a,3gp,3g2,mj2 demuxer sets avg_frame_rate
1261  if (QString(m_ic->iformat->name).contains("matroska") ||
1262  QString(m_ic->iformat->name).contains("mov"))
1263  {
1264  rates.emplace_back(avg_fps);
1265  }
1266 
1267  // avi uses container fps for timestamps
1268  if (QString(m_ic->iformat->name).contains("avi"))
1269  {
1270  rates.emplace_back(container_fps);
1271  }
1272 
1273  rates.emplace_back(codec_fps);
1274  rates.emplace_back(container_fps);
1275  rates.emplace_back(avg_fps);
1276  // certain H.264 interlaced streams are detected at 2x using estimated (i.e. wrong)
1277  rates.emplace_back(estimated_fps);
1278  // last resort, default to NTSC
1279  rates.emplace_back(30000.0 / 1001.0);
1280 
1281  auto invalid_fps = [](double rate) { return rate < 3.0 || rate > 121.0; };
1282  rates.erase(std::remove_if(rates.begin(), rates.end(), invalid_fps), rates.end());
1283 
1284  auto FuzzyEquals = [](double First, double Second) { return std::abs(First - Second) < 0.03; };
1285 
1286  // debug
1287  if (!FuzzyEquals(rates.front(), m_fps))
1288  {
1289  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1290  QString("Selected FPS: %1 (Avg:%2 Mult:%3 Codec:%4 Container:%5 Estimated:%6)")
1291  .arg(static_cast<double>(rates.front())).arg(avg_fps)
1292  .arg(m_fpsMultiplier).arg(codec_fps).arg(container_fps).arg(estimated_fps));
1293  }
1294 
1295  auto IsStandard = [&FuzzyEquals](double Rate)
1296  {
1297  // List of known, standards based frame rates
1298  static const std::set<double> k_standard_rates =
1299  {
1300  24000.0 / 1001.0,
1301  23.976,
1302  24.0,
1303  25.0,
1304  30000.0 / 1001.0,
1305  29.97,
1306  30.0,
1307  50.0,
1308  60000.0 / 1001.0,
1309  59.94,
1310  60.0,
1311  100.0,
1312  120000.0 / 1001.0,
1313  119.88,
1314  120.0
1315  };
1316 
1317  if (Rate > 23.0 && Rate < 121.0)
1318  {
1319  for (auto standard_rate : k_standard_rates)
1320  if (FuzzyEquals(Rate, standard_rate))
1321  return true;
1322  }
1323  return false;
1324  // TODO do not convert AVRational to double
1325  //return k_standard_rates.find(rate) != k_standard_rates.end();
1326  };
1327 
1328  // If the first choice rate is unusual, see if there is something more 'usual'
1329  double detected = rates.front();
1330  if (Sanitise && !IsStandard(detected))
1331  {
1332  for (auto rate : rates)
1333  {
1334  if (IsStandard(rate))
1335  {
1336  LOG(VB_GENERAL, LOG_INFO, LOC + QString("%1 is non-standard - using %2 instead.")
1337  .arg(rates.front()).arg(rate));
1338 
1339  // The most common problem here is mpegts files where the average
1340  // rate is slightly out and the estimated rate is the fallback.
1341  // As noted above, however, the estimated rate is sometimes twice
1342  // the actual for interlaced content. Try and detect and fix this
1343  // so that we don't throw out deinterlacing and video mode switching.
1344  // Assume anything under 30 may be interlaced - with +-10% error.
1345  if (rate > 33.0 && detected < 33.0)
1346  {
1347  double half = rate / 2.0;
1348  if (std::abs(half - detected) < (half * 0.1))
1349  {
1350  LOG(VB_GENERAL, LOG_INFO, LOC +
1351  QString("Assuming %1 is a better choice than %2")
1352  .arg(half).arg(rate));
1353  return static_cast<float>(half);
1354  }
1355  }
1356  return static_cast<float>(rate);
1357  }
1358  }
1359  }
1360 
1361  return static_cast<float>(detected);
1362 }
1363 
1364 #ifdef USING_DXVA2
1365 // Declared separately to allow attribute
1366 static enum AVPixelFormat get_format_dxva2(struct AVCodecContext *,
1367  const enum AVPixelFormat *);
1368 
1369 enum AVPixelFormat get_format_dxva2(struct AVCodecContext *avctx,
1370  const enum AVPixelFormat *valid_fmts)
1371 {
1372  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1373  if (nd && nd->GetPlayer())
1374  {
1375  static uint8_t *dummy[1] = { nullptr };
1376  avctx->hwaccel_context =
1377  (dxva_context*)nd->GetPlayer()->GetDecoderContext(nullptr, dummy[0]);
1378  }
1379 
1380  while (*valid_fmts != AV_PIX_FMT_NONE) {
1381  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_DXVA2_VLD))
1382  return AV_PIX_FMT_DXVA2_VLD;
1383  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1384  return AV_PIX_FMT_YUV420P;
1385  valid_fmts++;
1386  }
1387  return AV_PIX_FMT_NONE;
1388 }
1389 #endif
1390 
1391 int AvFormatDecoder::GetMaxReferenceFrames(AVCodecContext *Context)
1392 {
1393  switch (Context->codec_id)
1394  {
1395  case AV_CODEC_ID_H264:
1396  {
1397  int result = 16;
1398  if (Context->extradata && (Context->extradata_size >= 7))
1399  {
1400  uint8_t offset = 0;
1401  if (Context->extradata[0] == 1)
1402  offset = 9; // avCC
1403  else if (AV_RB24(Context->extradata) == 0x01) // Annex B - 3 byte startcode 0x000001
1404  offset = 4;
1405  else if (AV_RB32(Context->extradata) == 0x01) // Annex B - 4 byte startcode 0x00000001
1406  offset= 5;
1407 
1408  if (offset)
1409  {
1410  AVCParser parser;
1411  bool dummy = false;
1412  parser.parse_SPS(Context->extradata + offset,
1413  static_cast<uint>(Context->extradata_size - offset), dummy, result);
1414  }
1415  }
1416  return result;
1417  }
1418  case AV_CODEC_ID_H265: return 16;
1419  case AV_CODEC_ID_VP9: return 8;
1420  case AV_CODEC_ID_VP8: return 3;
1421  default: return 2;
1422  }
1423 }
1424 
1425 void AvFormatDecoder::InitVideoCodec(AVStream *stream, AVCodecContext *enc,
1426  bool selectedStream)
1427 {
1428  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1429  QString("InitVideoCodec ID:%1 Type:%2 Size:%3x%4")
1430  .arg(avcodec_get_name(enc->codec_id),
1431  AVMediaTypeToString(enc->codec_type))
1432  .arg(enc->width).arg(enc->height));
1433 
1434  if (m_ringBuffer && m_ringBuffer->IsDVD())
1435  m_directRendering = false;
1436 
1437  enc->opaque = static_cast<void*>(this);
1438  enc->get_buffer2 = get_avf_buffer;
1439  enc->slice_flags = 0;
1440 
1441  enc->err_recognition = AV_EF_COMPLIANT;
1442  enc->workaround_bugs = FF_BUG_AUTODETECT;
1443  enc->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
1444  enc->idct_algo = FF_IDCT_AUTO;
1445  enc->debug = 0;
1446  // enc->error_rate = 0;
1447 
1448  const AVCodec *codec1 = enc->codec;
1449 
1450  if (selectedStream)
1451  m_directRendering = true;
1452 
1453  // retrieve rotation information
1454  uint8_t* displaymatrix = av_stream_get_side_data(stream, AV_PKT_DATA_DISPLAYMATRIX, nullptr);
1455  if (displaymatrix)
1456  m_videoRotation = static_cast<int>(-av_display_rotation_get(reinterpret_cast<int32_t*>(displaymatrix)));
1457  else
1458  m_videoRotation = 0;
1459 
1460  // retrieve 3D type
1461  uint8_t* stereo3d = av_stream_get_side_data(stream, AV_PKT_DATA_STEREO3D, nullptr);
1462  if (stereo3d)
1463  {
1464  auto * avstereo = reinterpret_cast<AVStereo3D*>(stereo3d);
1465  m_stereo3D = avstereo->type;
1466  }
1467 
1468  delete m_mythCodecCtx;
1470  m_mythCodecCtx->InitVideoCodec(enc, selectedStream, m_directRendering);
1471  if (m_mythCodecCtx->HwDecoderInit(enc) < 0)
1472  {
1473  // force it to switch to software decoding
1475  m_streamsChanged = true;
1476  }
1477  else
1478  {
1479  // Note: This is never going to work as expected in all circumstances.
1480  // It will not account for changes in the stream and/or display. For
1481  // MediaCodec, Android will do its own thing (and judging by the Android logs,
1482  // shouldn't double rate the deinterlacing if the display cannot support it).
1483  // NVDEC will probably move to the FFmpeg YADIF CUDA deinterlacer - which
1484  // will avoid the issue (video player deinterlacing) and maybe disable
1485  // decoder VAAPI deinterlacing. If we get it wrong and the display cannot
1486  // keep up, the player should just drop frames.
1487 
1488  // FIXME - need a better way to handle this
1489  bool doublerate = true;//m_parent->CanSupportDoubleRate();
1491  }
1492 
1493  if (codec1 && ((AV_CODEC_ID_MPEG2VIDEO == codec1->id) ||
1494  (AV_CODEC_ID_MPEG1VIDEO == codec1->id)))
1495  {
1497  {
1498  int total_blocks = (enc->height + 15) / 16;
1499  enc->skip_top = (total_blocks + 3) / 4;
1500  enc->skip_bottom = (total_blocks + 3) / 4;
1501  }
1502 
1503  if (FlagIsSet(kDecodeLowRes))
1504  enc->lowres = 2; // 1 = 1/2 size, 2 = 1/4 size
1505  }
1506  else if (codec1 && (AV_CODEC_ID_H264 == codec1->id) && FlagIsSet(kDecodeNoLoopFilter))
1507  {
1508  enc->flags &= ~AV_CODEC_FLAG_LOOP_FILTER;
1509  enc->skip_loop_filter = AVDISCARD_ALL;
1510  }
1511 
1513  enc->skip_idct = AVDISCARD_ALL;
1514 
1515  if (selectedStream)
1516  {
1517  // m_fps is now set 'correctly' in ScanStreams so this additional call
1518  // to GetVideoFrameRate may now be redundant
1519  m_fps = GetVideoFrameRate(stream, enc, true);
1520  QSize dim = get_video_dim(*enc);
1521  int width = m_currentWidth = dim.width();
1522  int height = m_currentHeight = dim.height();
1523  m_currentAspect = get_aspect(*enc);
1524 
1525  if (!width || !height)
1526  {
1527  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1528  "InitVideoCodec invalid dimensions, resetting decoder.");
1529  width = 640;
1530  height = 480;
1531  m_fps = 29.97F;
1532  m_currentAspect = 4.0F / 3.0F;
1533  }
1534 
1536  const AVCodec *codec2 = enc->codec;
1537  QString codecName;
1538  if (codec2)
1539  codecName = codec2->name;
1540  m_parent->SetVideoParams(width, height, static_cast<double>(m_fps),
1542  kScan_Detect, codecName);
1543  if (LCD *lcd = LCD::Get())
1544  {
1545  LCDVideoFormatSet video_format = VIDEO_MPG;
1546 
1547  switch (enc->codec_id)
1548  {
1549  case AV_CODEC_ID_H263:
1550  case AV_CODEC_ID_MPEG4:
1551  case AV_CODEC_ID_MSMPEG4V1:
1552  case AV_CODEC_ID_MSMPEG4V2:
1553  case AV_CODEC_ID_MSMPEG4V3:
1554  case AV_CODEC_ID_H263P:
1555  case AV_CODEC_ID_H263I:
1556  video_format = VIDEO_DIVX;
1557  break;
1558  case AV_CODEC_ID_WMV1:
1559  case AV_CODEC_ID_WMV2:
1560  video_format = VIDEO_WMV;
1561  break;
1562 #if 0
1563  case AV_CODEC_ID_XVID:
1564  video_format = VIDEO_XVID;
1565  break;
1566 #endif
1567  default:
1568  video_format = VIDEO_MPG;
1569  break;
1570  }
1571 
1572  lcd->setVideoFormatLEDs(video_format, true);
1573 
1574  if(height >= 720)
1575  lcd->setVariousLEDs(VARIOUS_HDTV, true);
1576  else
1577  lcd->setVariousLEDs(VARIOUS_HDTV, false);
1578  }
1579  }
1580 }
1581 
1582 static bool cc608_good_parity(uint16_t data)
1583 {
1584  static constexpr std::array<uint8_t, 256> odd_parity_LUT
1585  {
1586  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1587  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1588  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1589  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1590  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1591  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1592  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1593  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1594  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1595  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1596  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1597  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1598  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1599  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1600  1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1601  0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1602  };
1603  bool ret = (odd_parity_LUT[data & 0xff] == 1) &&
1604  (odd_parity_LUT[(data & 0xff00) >> 8] == 1);
1605  if (!ret)
1606  {
1607  LOG(VB_VBI, LOG_ERR, LOC +
1608  QString("VBI: Bad parity in EIA-608 data (%1)") .arg(data,0,16));
1609  }
1610  return ret;
1611 }
1612 
1614 {
1615  QMutexLocker locker(&m_trackLock);
1616 
1617  m_ccX08InPmt.fill(false);
1618  m_pmtTracks.clear();
1619  m_pmtTrackTypes.clear();
1620 
1621  // Figure out languages of ATSC captions
1622  if (!m_ic->pmt_section)
1623  {
1624  LOG(VB_GENERAL, LOG_DEBUG, LOC +
1625  "ScanATSCCaptionStreams() called with no PMT");
1626  return;
1627  }
1628 
1629  auto pmt_buffer = MythAVBufferRef(m_ic->pmt_section);
1630  if (!pmt_buffer.has_buffer())
1631  {
1632  return;
1633  }
1634  const ProgramMapTable pmt(PSIPTable(pmt_buffer.data()));
1635 
1636  bool video_found = false;
1637  uint i = 0;
1638  for (i = 0; i < pmt.StreamCount(); i++)
1639  {
1640  // MythTV remaps OpenCable Video to normal video during recording
1641  // so "dvb" is the safest choice for system info type, since this
1642  // will ignore other uses of the same stream id in DVB countries.
1643  if (pmt.IsVideo(i, "dvb"))
1644  {
1645  video_found = true;
1646  break;
1647  }
1648  }
1649  if (!video_found)
1650  return;
1651 
1653  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1655 
1656  const desc_list_t desc_list2 = MPEGDescriptor::ParseOnlyInclude(
1657  pmt.ProgramInfo(), pmt.ProgramInfoLength(),
1659 
1660  desc_list.insert(desc_list.end(), desc_list2.begin(), desc_list2.end());
1661 
1662  for (auto & desc : desc_list)
1663  {
1664  const CaptionServiceDescriptor csd(desc);
1665  if (!csd.IsValid())
1666  continue;
1667 
1668  LOG(VB_VBI, LOG_DEBUG, LOC + csd.toString());
1669 
1670  for (uint k = 0; k < csd.ServicesCount(); k++)
1671  {
1672  int lang = csd.CanonicalLanguageKey(k);
1673  int type = csd.Type(k) ? 1 : 0;
1674  if (type)
1675  {
1676  StreamInfo si(av_index, lang, 0/*lang_idx*/,
1677  csd.CaptionServiceNumber(k),
1678  static_cast<int>(csd.EasyReader(k)),
1679  csd.WideAspectRatio(k));
1680  uint key = csd.CaptionServiceNumber(k) + 4;
1681  m_ccX08InPmt[key] = true;
1682  m_pmtTracks.push_back(si);
1683  m_pmtTrackTypes.push_back(kTrackTypeCC708);
1684  }
1685  else
1686  {
1687  int line21 = csd.Line21Field(k) ? 3 : 1;
1688  StreamInfo si(av_index, lang, 0/*lang_idx*/, line21, 0);
1689  m_ccX08InPmt[line21-1] = true;
1690  m_pmtTracks.push_back(si);
1691  m_pmtTrackTypes.push_back(kTrackTypeCC608);
1692  }
1693  }
1694  }
1695 }
1696 
1698 {
1699  QMutexLocker locker(&m_trackLock);
1700 
1701  m_tracks[kTrackTypeCC608].clear();
1702  m_tracks[kTrackTypeCC708].clear();
1703  m_ccX08InTracks.fill(false);
1704 
1705  uint pidx = 0;
1706  uint sidx = 0;
1707  std::array<std::map<int,uint>,2> lang_cc_cnt;
1708  while (true)
1709  {
1710  bool pofr = pidx >= (uint)m_pmtTracks.size();
1711  bool sofr = sidx >= (uint)m_streamTracks.size();
1712  if (pofr && sofr)
1713  break;
1714 
1715  // choose lowest available next..
1716  // stream_id's of 608 and 708 streams alias, but this
1717  // is ok as we just want each list to be ordered.
1718  StreamInfo const *si = nullptr;
1719  int type = 0; // 0 if 608, 1 if 708
1720  bool isp = true; // if true use m_pmtTracks next, else stream_tracks
1721 
1722  if (pofr && !sofr)
1723  isp = false; // NOLINT(bugprone-branch-clone)
1724  else if (!pofr && sofr)
1725  isp = true;
1726  else if (m_streamTracks[sidx] < m_pmtTracks[pidx])
1727  isp = false;
1728 
1729  if (isp)
1730  {
1731  si = &m_pmtTracks[pidx];
1732  type = kTrackTypeCC708 == m_pmtTrackTypes[pidx] ? 1 : 0;
1733  pidx++;
1734  }
1735  else
1736  {
1737  si = &m_streamTracks[sidx];
1738  type = kTrackTypeCC708 == m_streamTrackTypes[sidx] ? 1 : 0;
1739  sidx++;
1740  }
1741 
1742  StreamInfo nsi(*si);
1743  int lang_indx = lang_cc_cnt[type][nsi.m_language];
1744  lang_cc_cnt[type][nsi.m_language]++;
1745  nsi.m_language_index = lang_indx;
1746  m_tracks[(type) ? kTrackTypeCC708 : kTrackTypeCC608].push_back(nsi);
1747  int key = nsi.m_stream_id + ((type) ? 4 : -1);
1748  if (key < 0)
1749  {
1750  LOG(VB_GENERAL, LOG_ERR, LOC + "in_tracks key too small");
1751  }
1752  else
1753  {
1754  m_ccX08InTracks[key] = true;
1755  }
1756  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1757  QString("%1 caption service #%2 is in the %3 language.")
1758  .arg((type) ? "EIA-708" : "EIA-608")
1759  .arg(nsi.m_stream_id)
1760  .arg(iso639_key_toName(nsi.m_language)));
1761  }
1762 }
1763 
1765 {
1766  QMutexLocker locker(&m_trackLock);
1767 
1768  // ScanStreams() calls m_tracks[kTrackTypeTeletextCaptions].clear()
1769  if (!m_ic->pmt_section || !m_tracks[kTrackTypeTeletextCaptions].empty())
1770  return;
1771 
1772  auto pmt_buffer = MythAVBufferRef(m_ic->pmt_section);
1773  if (!pmt_buffer.has_buffer())
1774  {
1775  return;
1776  }
1777  const ProgramMapTable pmt(PSIPTable(pmt_buffer.data()));
1778 
1779  for (uint i = 0; i < pmt.StreamCount(); i++)
1780  {
1781  if (pmt.StreamType(i) != StreamID::PrivData)
1782  continue;
1783 
1785  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1787 
1788  for (const auto *desc : desc_list)
1789  {
1790  const TeletextDescriptor td(desc);
1791  if (!td.IsValid())
1792  continue;
1793  for (uint k = 0; k < td.StreamCount(); k++)
1794  {
1795  int type = td.TeletextType(k);
1796  int language = td.CanonicalLanguageKey(k);
1797  int magazine = td.TeletextMagazineNum(k);
1798  if (magazine == 0)
1799  magazine = 8;
1800  int pagenum = td.TeletextPageNum(k);
1801  int lang_idx = (magazine << 8) | pagenum;
1802  StreamInfo si(av_index, language, lang_idx, 0, 0);
1803  if (type == 2 || type == 1)
1804  {
1805  TrackType track = (type == 2) ? kTrackTypeTeletextCaptions :
1807  m_tracks[track].push_back(si);
1808  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1809  QString("Teletext stream #%1 (%2) is in the %3 language"
1810  " on page %4 %5.")
1811  .arg(QString::number(k),
1812  (type == 2) ? "Caption" : "Menu",
1813  iso639_key_toName(language),
1814  QString::number(magazine),
1815  QString::number(pagenum)));
1816  }
1817  }
1818  }
1819 
1820  // Assume there is only one multiplexed teletext stream in PMT..
1821  if (!m_tracks[kTrackTypeTeletextCaptions].empty())
1822  break;
1823  }
1824 }
1825 
1826 void AvFormatDecoder::ScanRawTextCaptions(int av_stream_index)
1827 {
1828  QMutexLocker locker(&m_trackLock);
1829 
1830  AVDictionaryEntry *metatag =
1831  av_dict_get(m_ic->streams[av_stream_index]->metadata, "language", nullptr,
1832  0);
1833  bool forced = (m_ic->streams[av_stream_index]->disposition & AV_DISPOSITION_FORCED) != 0;
1834  int lang = metatag ? get_canonical_lang(metatag->value) :
1835  iso639_str3_to_key("und");
1836  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1837  QString("Text Subtitle track #%1 is A/V stream #%2 "
1838  "and is in the %3 language(%4), forced=%5.")
1839  .arg(m_tracks[kTrackTypeRawText].size()).arg(av_stream_index)
1840  .arg(iso639_key_toName(lang)).arg(lang).arg(forced));
1841  StreamInfo si(av_stream_index, lang, 0, 0, 0, false, false, forced);
1842  m_tracks[kTrackTypeRawText].push_back(si);
1843 }
1844 
1850 {
1851  if (!m_ic || !m_ic->pmt_section)
1852  return;
1853 
1854  if (!m_itv && ! (m_itv = m_parent->GetInteractiveTV()))
1855  return;
1856 
1857  auto pmt_buffer = MythAVBufferRef(m_ic->pmt_section);
1858  if (!pmt_buffer.has_buffer())
1859  {
1860  return;
1861  }
1862  const ProgramMapTable pmt(PSIPTable(pmt_buffer.data()));
1863 
1864  for (uint i = 0; i < pmt.StreamCount(); i++)
1865  {
1866  if (! StreamID::IsObjectCarousel(pmt.StreamType(i)))
1867  continue;
1868 
1869  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams Found Object Carousel in Stream %1").arg(QString::number(i)));
1870 
1872  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1874 
1875  for (const auto *desc : desc_list)
1876  {
1877  desc++; // Skip tag
1878  uint length = *desc++;
1879  const unsigned char *endDesc = desc+length;
1880  uint dataBroadcastId = desc[0]<<8 | desc[1];
1881  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams dataBroadcastId %1").arg(QString::number(dataBroadcastId)));
1882  if (dataBroadcastId != 0x0106) // ETSI/UK Profile
1883  continue;
1884  desc += 2; // Skip data ID
1885  while (desc != endDesc)
1886  {
1887  uint appTypeCode = desc[0]<<8 | desc[1];
1888  desc += 3; // Skip app type code and boot priority hint
1889  uint appSpecDataLen = *desc++;
1890 #ifdef USING_MHEG
1891  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams AppTypeCode %1").arg(QString::number(appTypeCode)));
1892  if (appTypeCode == 0x101) // UK MHEG profile
1893  {
1894  const unsigned char *subDescEnd = desc + appSpecDataLen;
1895  while (desc < subDescEnd)
1896  {
1897  uint sub_desc_tag = *desc++;
1898  uint sub_desc_len = *desc++;
1899  // Network boot info sub-descriptor.
1900  if (sub_desc_tag == 1)
1901  m_itv->SetNetBootInfo(desc, sub_desc_len);
1902  desc += sub_desc_len;
1903  }
1904  }
1905  else
1906 #else
1907  (void) appTypeCode;
1908 #endif // USING_MHEG
1909  {
1910  desc += appSpecDataLen;
1911  }
1912  }
1913  }
1914  }
1915 }
1916 
1918 {
1919  QMutexLocker avlocker(&m_avCodecLock);
1920  QMutexLocker locker(&m_trackLock);
1921 
1922  bool unknownbitrate = false;
1923  int scanerror = 0;
1924  m_bitrate = 0;
1925 
1926  m_tracks[kTrackTypeAttachment].clear();
1927  m_tracks[kTrackTypeAudio].clear();
1928  m_tracks[kTrackTypeSubtitle].clear();
1931  m_tracks[kTrackTypeRawText].clear();
1932  if (!novideo)
1933  {
1934  // we will rescan video streams
1935  m_tracks[kTrackTypeVideo].clear();
1936  m_selectedTrack[kTrackTypeVideo].m_av_stream_index = -1;
1937  m_fps = 0;
1938  }
1939  std::map<int,uint> lang_sub_cnt;
1940  uint subtitleStreamCount = 0;
1941  std::map<int,uint> lang_aud_cnt;
1942  uint audioStreamCount = 0;
1943 
1944  if (m_ringBuffer && m_ringBuffer->IsDVD() &&
1946  {
1949  }
1950 
1951  for (uint strm = 0; strm < m_ic->nb_streams; strm++)
1952  {
1953  AVCodecParameters *par = m_ic->streams[strm]->codecpar;
1954  AVCodecContext *enc = nullptr;
1955 
1956  QString codectype(AVMediaTypeToString(par->codec_type));
1957  if (par->codec_type == AVMEDIA_TYPE_VIDEO)
1958  codectype += QString("(%1x%2)").arg(par->width).arg(par->height);
1959  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1960  QString("Stream #%1: ID: 0x%2 Codec ID: %3 Type: %4 Bitrate: %5")
1961  .arg(strm).arg(static_cast<uint64_t>(m_ic->streams[strm]->id), 0, 16)
1962  .arg(avcodec_get_name(par->codec_id),
1963  codectype).arg(par->bit_rate));
1964 
1965  switch (par->codec_type)
1966  {
1967  case AVMEDIA_TYPE_VIDEO:
1968  {
1969  // reset any potentially errored hardware decoders
1971  {
1972  if (m_codecMap.FindCodecContext(m_ic->streams[strm]))
1973  {
1974  AVCodecContext* ctx = m_codecMap.GetCodecContext(m_ic->streams[strm]);
1975  if (ctx && (ctx->hw_frames_ctx || ctx->hw_device_ctx))
1976  m_codecMap.FreeCodecContext(m_ic->streams[strm]);
1977  }
1978  }
1979 
1980  if (!par->codec_id)
1981  {
1982  LOG(VB_GENERAL, LOG_ERR, LOC +
1983  QString("Stream #%1 has an unknown video "
1984  "codec id, skipping.").arg(strm));
1985  continue;
1986  }
1987 
1988  // ffmpeg does not return a bitrate for several codecs and
1989  // formats. Typically the same streams do not have a duration either
1990  // - so we cannot estimate a bitrate (which would be subject
1991  // to significant error anyway if there were multiple video streams).
1992  // So we need to guesstimate a value that avoids low bitrate optimisations
1993  // (which typically kick in around 500,000) and provides a read
1994  // chunk size large enough to avoid starving the decoder of data.
1995  // Trying to read a 20Mbs stream with a 16KB chunk size does not work:)
1996  if (par->bit_rate == 0)
1997  {
1998  static constexpr int64_t s_baseBitrate { 1000000LL };
1999  int multiplier = 1;
2000  if (par->width && par->height)
2001  {
2002  static const int s_baseSize = 1920 * 1080;
2003  multiplier = ((par->width * par->height) + s_baseSize - 1) / s_baseSize;
2004  if (multiplier < 1)
2005  multiplier = 1;
2006  }
2007  par->bit_rate = s_baseBitrate * multiplier;
2008  unknownbitrate = true;
2009  }
2010  m_bitrate += par->bit_rate;
2011 
2012  break;
2013  }
2014  case AVMEDIA_TYPE_AUDIO:
2015  {
2016  enc = m_codecMap.FindCodecContext(m_ic->streams[strm]);
2017  if (enc && enc->internal)
2018  {
2019  LOG(VB_GENERAL, LOG_WARNING, LOC +
2020  QString("Warning, audio codec 0x%1 id(%2) "
2021  "type (%3) already open, leaving it alone.")
2022  .arg(reinterpret_cast<unsigned long long>(enc), 0, 16)
2023  .arg(avcodec_get_name(enc->codec_id),
2024  AVMediaTypeToString(enc->codec_type)));
2025  }
2026  LOG(VB_GENERAL, LOG_INFO, LOC +
2027  QString("codec %1 has %2 channels")
2028  .arg(avcodec_get_name(par->codec_id))
2029  .arg(par->ch_layout.nb_channels));
2030 
2031  m_bitrate += par->bit_rate;
2032  break;
2033  }
2034  case AVMEDIA_TYPE_SUBTITLE:
2035  {
2036  if (par->codec_id == AV_CODEC_ID_DVB_TELETEXT)
2037  ScanTeletextCaptions(static_cast<int>(strm));
2038  if (par->codec_id == AV_CODEC_ID_TEXT)
2039  ScanRawTextCaptions(static_cast<int>(strm));
2040  m_bitrate += par->bit_rate;
2041 
2042  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("subtitle codec (%1)")
2043  .arg(AVMediaTypeToString(par->codec_type)));
2044  break;
2045  }
2046  case AVMEDIA_TYPE_DATA:
2047  {
2048  ScanTeletextCaptions(static_cast<int>(strm));
2049  m_bitrate += par->bit_rate;
2050  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("data codec (%1)")
2051  .arg(AVMediaTypeToString(par->codec_type)));
2052  break;
2053  }
2054  case AVMEDIA_TYPE_ATTACHMENT:
2055  {
2056  if (par->codec_id == AV_CODEC_ID_TTF)
2057  m_tracks[kTrackTypeAttachment].emplace_back(
2058  static_cast<int>(strm), 0, 0, m_ic->streams[strm]->id, 0);
2059  m_bitrate += par->bit_rate;
2060  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2061  QString("Attachment codec (%1)")
2062  .arg(AVMediaTypeToString(par->codec_type)));
2063  break;
2064  }
2065  default:
2066  {
2067  m_bitrate += par->bit_rate;
2068  LOG(VB_PLAYBACK, LOG_ERR, LOC +
2069  QString("Unknown codec type (%1)")
2070  .arg(AVMediaTypeToString(par->codec_type)));
2071  break;
2072  }
2073  }
2074 
2075  if (par->codec_type != AVMEDIA_TYPE_AUDIO &&
2076  par->codec_type != AVMEDIA_TYPE_SUBTITLE)
2077  continue;
2078 
2079  // skip DVB teletext and text subs, there is no libavcodec decoder
2080  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE &&
2081  (par->codec_id == AV_CODEC_ID_DVB_TELETEXT ||
2082  par->codec_id == AV_CODEC_ID_TEXT))
2083  continue;
2084 
2085  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Looking for decoder for %1")
2086  .arg(avcodec_get_name(par->codec_id)));
2087 
2088  if (par->codec_id == AV_CODEC_ID_PROBE)
2089  {
2090  LOG(VB_GENERAL, LOG_ERR, LOC +
2091  QString("Probing of stream #%1 unsuccesful, ignoring.").arg(strm));
2092  continue;
2093  }
2094 
2095  if (!enc)
2096  enc = m_codecMap.GetCodecContext(m_ic->streams[strm]);
2097 
2098  const AVCodec *codec = nullptr;
2099  if (enc)
2100  codec = enc->codec;
2101  else
2102  {
2103  LOG(VB_GENERAL, LOG_ERR, LOC +
2104  QString("Could not find decoder for codec (%1), ignoring.")
2105  .arg(avcodec_get_name(par->codec_id)));
2106 
2107  // Nigel's bogus codec-debug. Dump the list of codecs & decoders,
2108  // and have one last attempt to find a decoder. This is usually
2109  // only caused by build problems, where libavcodec needs a rebuild
2110  if (VERBOSE_LEVEL_CHECK(VB_LIBAV, LOG_ANY))
2111  {
2112  void *opaque = nullptr;
2113  const AVCodec *p = av_codec_iterate(&opaque);
2114  while (p)
2115  {
2116  QString msg;
2117 
2118  if (p->name[0] != '\0')
2119  msg = QString("Codec %1:").arg(p->name);
2120  else
2121  msg = QString("Codec %1, null name,").arg(strm);
2122 
2123  LOG(VB_LIBAV, LOG_INFO, LOC + msg);
2124 
2125  if (p->id == par->codec_id)
2126  {
2127  codec = p;
2128  break;
2129  }
2130 
2131  LOG(VB_LIBAV, LOG_INFO, LOC +
2132  QString("Codec 0x%1 != 0x%2") .arg(p->id, 0, 16)
2133  .arg(par->codec_id, 0, 16));
2134  p = av_codec_iterate(&opaque);
2135  }
2136  }
2137  if (codec)
2138  enc = m_codecMap.GetCodecContext(m_ic->streams[strm], codec);
2139  else
2140  continue;
2141  }
2142  if (!enc)
2143  continue;
2144 
2145  if (enc->codec && par->codec_id != enc->codec_id)
2146  {
2147  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2148  QString("Already opened codec not matching (%1 vs %2). Reopening")
2149  .arg(avcodec_get_name(enc->codec_id),
2150  avcodec_get_name(enc->codec->id)));
2151  m_codecMap.FreeCodecContext(m_ic->streams[strm]);
2152  enc = m_codecMap.GetCodecContext(m_ic->streams[strm]);
2153  }
2154  if (!OpenAVCodec(enc, codec))
2155  continue;
2156  if (!enc)
2157  continue;
2158 
2159  if (!IsValidStream(m_ic->streams[strm]->id))
2160  {
2161  /* Hide this stream if it's not valid in this context.
2162  * This can happen, for example, on a Blu-ray disc if there
2163  * are more physical streams than there is metadata about them.
2164  * (e.g. Despicable Me)
2165  */
2166  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2167  QString("Stream 0x%1 is not valid in this context - skipping")
2168  .arg(m_ic->streams[strm]->id, 4, 16));
2169  continue;
2170  }
2171 
2172  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2173  {
2174  bool forced = (m_ic->streams[strm]->disposition & AV_DISPOSITION_FORCED) != 0;
2175  int lang = GetSubtitleLanguage(subtitleStreamCount, strm);
2176  uint lang_indx = lang_sub_cnt[lang]++;
2177  subtitleStreamCount++;
2178 
2179  m_tracks[kTrackTypeSubtitle].emplace_back(
2180  static_cast<int>(strm), lang, lang_indx, m_ic->streams[strm]->id, 0, 0, false, false, forced);
2181 
2182  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2183  QString("Subtitle track #%1 is A/V stream #%2 "
2184  "and is in the %3 language(%4).")
2185  .arg(m_tracks[kTrackTypeSubtitle].size()).arg(strm)
2186  .arg(iso639_key_toName(lang)).arg(lang));
2187  }
2188 
2189  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
2190  {
2191  int lang = GetAudioLanguage(audioStreamCount, strm);
2193  int channels = par->ch_layout.nb_channels;
2194  uint lang_indx = lang_aud_cnt[lang]++;
2195  audioStreamCount++;
2196 
2197  if (enc->avcodec_dual_language)
2198  {
2199  m_tracks[kTrackTypeAudio].emplace_back(
2200  static_cast<int>(strm), lang, lang_indx, m_ic->streams[strm]->id, channels,
2201  false, false, false, type);
2202  lang_indx = lang_aud_cnt[lang]++;
2203  m_tracks[kTrackTypeAudio].emplace_back(
2204  static_cast<int>(strm), lang, lang_indx, m_ic->streams[strm]->id, channels,
2205  true, false, false, type);
2206  }
2207  else
2208  {
2209  int logical_stream_id = 0;
2210  if (m_ringBuffer && m_ringBuffer->IsDVD())
2211  {
2212  logical_stream_id = m_ringBuffer->DVD()->GetAudioTrackNum(m_ic->streams[strm]->id);
2213  channels = m_ringBuffer->DVD()->GetNumAudioChannels(logical_stream_id);
2214  }
2215  else
2216  logical_stream_id = m_ic->streams[strm]->id;
2217 
2218  if (logical_stream_id == -1)
2219  {
2220  // This stream isn't mapped, so skip it
2221  continue;
2222  }
2223 
2224  m_tracks[kTrackTypeAudio].emplace_back(
2225  static_cast<int>(strm), lang, lang_indx, logical_stream_id, channels,
2226  false, false, false, type);
2227  }
2228 
2229  LOG(VB_AUDIO, LOG_INFO, LOC +
2230  QString("Audio Track #%1, of type (%2) is A/V stream #%3 (id=0x%4) "
2231  "and has %5 channels in the %6 language(%7).")
2232  .arg(m_tracks[kTrackTypeAudio].size()).arg(toString(type))
2233  .arg(strm).arg(m_ic->streams[strm]->id,0,16).arg(enc->ch_layout.nb_channels)
2234  .arg(iso639_key_toName(lang)).arg(lang));
2235  }
2236  }
2237 
2238  // Now find best video track to play
2239  m_resetHardwareDecoders = false;
2240  if (!novideo && m_ic)
2241  {
2242  for(;;)
2243  {
2244  const AVCodec *codec = nullptr;
2245  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Trying to select best video track");
2246 
2247  /*
2248  * Find the "best" stream in the file.
2249  *
2250  * The best stream is determined according to various heuristics as
2251  * the most likely to be what the user expects. If the decoder parameter
2252  * is not nullptr, av_find_best_stream will find the default decoder
2253  * for the stream's codec; streams for which no decoder can be found
2254  * are ignored.
2255  *
2256  * If av_find_best_stream returns successfully and decoder_ret is not nullptr,
2257  * then *decoder_ret is guaranteed to be set to a valid AVCodec.
2258  */
2259  int selTrack = av_find_best_stream(m_ic, AVMEDIA_TYPE_VIDEO,
2260  -1, -1, &codec, 0);
2261 
2262  if (selTrack < 0)
2263  {
2264  LOG(VB_PLAYBACK, LOG_INFO, LOC + "No video track found/selected.");
2265  break;
2266  }
2267 
2268  AVStream *stream = m_ic->streams[selTrack];
2270  m_codecMap.FreeCodecContext(stream);
2271  AVCodecContext *enc = m_codecMap.GetCodecContext(stream, codec);
2272  StreamInfo si(selTrack, 0, 0, 0, 0);
2273 
2274  m_tracks[kTrackTypeVideo].push_back(si);
2276 
2277  QString codectype(AVMediaTypeToString(enc->codec_type));
2278  if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2279  codectype += QString("(%1x%2)").arg(enc->width).arg(enc->height);
2280  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2281  QString("Selected track #%1: ID: 0x%2 Codec ID: %3 Profile: %4 Type: %5 Bitrate: %6")
2282  .arg(selTrack).arg(static_cast<uint64_t>(stream->id), 0, 16)
2283  .arg(avcodec_get_name(enc->codec_id),
2284  avcodec_profile_name(enc->codec_id, enc->profile),
2285  codectype,
2286  QString::number(enc->bit_rate)));
2287 
2288  // If ScanStreams has been called on a stream change triggered by a
2289  // decoder error - because the decoder does not handle resolution
2290  // changes gracefully (NVDEC and maybe MediaCodec) - then the stream/codec
2291  // will still contain the old resolution but the AVCodecContext will
2292  // have been updated. This causes mayhem for a second or two.
2293  if ((enc->width != stream->codecpar->width) || (enc->height != stream->codecpar->height))
2294  {
2295  LOG(VB_GENERAL, LOG_INFO, LOC + QString(
2296  "Video resolution mismatch: Context: %1x%2 Stream: %3x%4 Codec: %5 Stream change: %6")
2297  .arg(enc->width).arg(enc->height)
2298  .arg(stream->codecpar->width).arg(stream->codecpar->height)
2300  }
2301 
2302  m_avcParser->Reset();
2303 
2304  QSize dim = get_video_dim(*enc);
2305  int width = std::max(dim.width(), 16);
2306  int height = std::max(dim.height(), 16);
2307  QString dec = "ffmpeg";
2308  uint thread_count = 1;
2309  QString codecName;
2310  if (enc->codec)
2311  codecName = enc->codec->name;
2312  // framerate appears to never be set - which is probably why
2313  // GetVideoFrameRate never uses it:)
2314  // So fallback to the GetVideoFrameRate call which should then ensure
2315  // the video display profile gets an accurate frame rate - instead of 0
2316  if (enc->framerate.den && enc->framerate.num)
2317  m_fps = float(enc->framerate.num) / float(enc->framerate.den);
2318  else
2319  m_fps = GetVideoFrameRate(stream, enc, true);
2320 
2321  bool foundgpudecoder = false;
2322  QStringList unavailabledecoders;
2323  bool allowgpu = FlagIsSet(kDecodeAllowGPU);
2324 
2326  {
2327  // TODO this could be improved by appending the decoder that has
2328  // failed to the unavailable list - but that could lead to circular
2329  // failures if there are 2 or more hardware decoders that fail
2330  if (FlagIsSet(kDecodeAllowGPU) && (dec != "ffmpeg"))
2331  {
2332  LOG(VB_GENERAL, LOG_WARNING, LOC + QString(
2333  "GPU/hardware decoder '%1' failed - forcing software decode")
2334  .arg(dec));
2335  }
2336  m_averrorCount = 0;
2337  allowgpu = false;
2338  }
2339 
2340  while (unavailabledecoders.size() < 10)
2341  {
2342  if (!m_isDbIgnored)
2343  {
2344  if (!unavailabledecoders.isEmpty())
2345  {
2346  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Unavailable decoders: %1")
2347  .arg(unavailabledecoders.join(",")));
2348  }
2349  m_videoDisplayProfile.SetInput(QSize(width, height), m_fps, codecName, unavailabledecoders);
2351  thread_count = m_videoDisplayProfile.GetMaxCPUs();
2352  bool skip_loop_filter = m_videoDisplayProfile.IsSkipLoopEnabled();
2353  if (!skip_loop_filter)
2354  enc->skip_loop_filter = AVDISCARD_NONKEY;
2355  }
2356 
2358  uint version = mpeg_version(enc->codec_id);
2359  if (version)
2360  m_videoCodecId = static_cast<MythCodecID>(kCodec_MPEG1 + version - 1);
2361 
2362  if (version && allowgpu && dec != "ffmpeg")
2363  {
2364  // We need to set this so that MythyCodecContext can callback
2365  // to the player in use to check interop support.
2366  enc->opaque = static_cast<void*>(this);
2367  MythCodecID hwcodec = MythCodecContext::FindDecoder(dec, stream, &enc, &codec);
2368  if (hwcodec != kCodec_NONE)
2369  {
2370  // the context may have changed
2371  enc->opaque = static_cast<void*>(this);
2372  m_videoCodecId = hwcodec;
2373  foundgpudecoder = true;
2374  }
2375  else
2376  {
2377  // hardware decoder is not available - try the next best profile
2378  unavailabledecoders.append(dec);
2379  continue;
2380  }
2381  }
2382 
2383  // default to mpeg2
2384  if (m_videoCodecId == kCodec_NONE)
2385  {
2386  LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown video codec - defaulting to MPEG2");
2388  }
2389 
2390  break;
2391  }
2392 
2393  // N.B. MediaCodec and NVDEC require frame timing
2394  m_useFrameTiming = false;
2397  {
2398  m_useFrameTiming = true;
2399  }
2400 
2402  thread_count = 1;
2403 
2404  if (HAVE_THREADS)
2405  {
2406  // Only use a single thread for hardware decoding. There is no
2407  // performance improvement with multithreaded hardware decode
2408  // and asynchronous callbacks create issues with decoders that
2409  // use AVHWFrameContext where we need to release video resources
2410  // before they are recreated
2411  if (!foundgpudecoder)
2412  {
2413  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Using %1 CPUs for decoding")
2414  .arg(HAVE_THREADS ? thread_count : 1));
2415  enc->thread_count = static_cast<int>(thread_count);
2416  }
2417  }
2418 
2419  InitVideoCodec(stream, enc, true);
2420 
2421  ScanATSCCaptionStreams(selTrack);
2423 
2424  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Using %1 for video decoding").arg(GetCodecDecoderName()));
2425  m_mythCodecCtx->SetDecoderOptions(enc, codec);
2426  if (!OpenAVCodec(enc, codec))
2427  {
2428  scanerror = -1;
2429  break;
2430  }
2431  break;
2432  }
2433  }
2434 
2435  if (m_ic && (static_cast<uint>(m_ic->bit_rate) > m_bitrate))
2436  m_bitrate = static_cast<uint>(m_ic->bit_rate);
2437 
2438  if (m_bitrate > 0)
2439  {
2440  m_bitrate = (m_bitrate + 999) / 1000;
2441  if (m_ringBuffer)
2443  }
2444 
2445  // update RingBuffer buffer size
2446  if (m_ringBuffer)
2447  {
2448  m_ringBuffer->SetBufferSizeFactors(unknownbitrate,
2449  m_ic && QString(m_ic->iformat->name).contains("matroska"));
2450  }
2451 
2453 
2454  // Select a new track at the next opportunity.
2455  ResetTracks();
2456 
2457  // We have to do this here to avoid the NVP getting stuck
2458  // waiting on audio.
2459  if (m_audio->HasAudioIn() && m_tracks[kTrackTypeAudio].empty())
2460  {
2461  m_audio->SetAudioParams(FORMAT_NONE, -1, -1, AV_CODEC_ID_NONE, -1, false);
2462  m_audio->ReinitAudio();
2463  if (m_ringBuffer && m_ringBuffer->IsDVD())
2464  m_audioIn = AudioInfo();
2465  }
2466 
2467  // if we don't have a video stream we still need to make sure some
2468  // video params are set properly
2469  if (m_selectedTrack[kTrackTypeVideo].m_av_stream_index == -1)
2470  {
2471  m_fps = 23.97F; // minimum likey display refresh rate
2472  // N.B. we know longer need a 'dummy' frame to render overlays into
2473  m_parent->SetVideoParams(0, 0, 23.97, 1.0F, false, 0);
2474  }
2475 
2476  if (m_parent->IsErrored())
2477  scanerror = -1;
2478 
2479  ScanDSMCCStreams();
2480 
2481  return scanerror;
2482 }
2483 
2484 bool AvFormatDecoder::OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec)
2485 {
2486  m_avCodecLock.lock();
2487 #ifdef USING_MEDIACODEC
2488  if (QString("mediacodec") == codec->wrapper_name)
2489  av_jni_set_java_vm(QAndroidJniEnvironment::javaVM(), nullptr);
2490 #endif
2491  int ret = avcodec_open2(avctx, codec, nullptr);
2492  m_avCodecLock.unlock();
2493  if (ret < 0)
2494  {
2495  std::string error;
2496  LOG(VB_GENERAL, LOG_ERR, LOC +
2497  QString("Could not open codec 0x%1, id(%2) type(%3) "
2498  "ignoring. reason %4").arg((uint64_t)avctx,0,16)
2499  .arg(avcodec_get_name(avctx->codec_id),
2500  AVMediaTypeToString(avctx->codec_type),
2502  return false;
2503  }
2504 
2505  LOG(VB_GENERAL, LOG_INFO, LOC +
2506  QString("Opened codec 0x%1, id(%2) type(%3)")
2507  .arg((uint64_t)avctx,0,16)
2508  .arg(avcodec_get_name(avctx->codec_id),
2509  AVMediaTypeToString(avctx->codec_type)));
2510  return true;
2511 }
2512 
2514 {
2516 }
2517 
2518 bool AvFormatDecoder::DoRewindSeek(long long desiredFrame)
2519 {
2520  return DecoderBase::DoRewindSeek(desiredFrame);
2521 }
2522 
2523 void AvFormatDecoder::DoFastForwardSeek(long long desiredFrame, bool &needflush)
2524 {
2525  DecoderBase::DoFastForwardSeek(desiredFrame, needflush);
2526 }
2527 
2530 {
2531  QMutexLocker locker(&m_trackLock);
2532  for (const auto & si : m_tracks[kTrackTypeTeletextCaptions])
2533  if (si.m_language_index == Index)
2534  return si.m_language;
2535  return iso639_str3_to_key("und");
2536 }
2537 
2539 int AvFormatDecoder::GetSubtitleLanguage(uint /*unused*/, uint StreamIndex)
2540 {
2541  AVDictionaryEntry *metatag = av_dict_get(m_ic->streams[StreamIndex]->metadata, "language", nullptr, 0);
2542  return metatag ? get_canonical_lang(metatag->value) : iso639_str3_to_key("und");
2543 }
2544 
2547 {
2548  // This doesn't strictly need write lock but it is called internally while
2549  // write lock is held. All other (external) uses are safe
2550  QMutexLocker locker(&m_trackLock);
2551 
2552  int ret = -1;
2553  for (int i = 0; i < m_pmtTrackTypes.size(); i++)
2554  {
2555  if ((m_pmtTrackTypes[i] == TrackType) && (m_pmtTracks[i].m_stream_id == ServiceNum))
2556  {
2557  ret = m_pmtTracks[i].m_language;
2558  if (!iso639_is_key_undefined(ret))
2559  return ret;
2560  }
2561  }
2562 
2563  for (int i = 0; i < m_streamTrackTypes.size(); i++)
2564  {
2565  if ((m_streamTrackTypes[i] == TrackType) && (m_streamTracks[i].m_stream_id == ServiceNum))
2566  {
2567  ret = m_streamTracks[i].m_language;
2568  if (!iso639_is_key_undefined(ret))
2569  return ret;
2570  }
2571  }
2572 
2573  return ret;
2574 }
2575 
2576 int AvFormatDecoder::GetAudioLanguage(uint AudioIndex, uint StreamIndex)
2577 {
2578  return GetSubtitleLanguage(AudioIndex, StreamIndex);
2579 }
2580 
2582 {
2584  AVStream *stream = m_ic->streams[StreamIndex];
2585 
2586  {
2587  // We only support labelling/filtering of these two types for now
2588  if (stream->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
2590  else if (stream->disposition & AV_DISPOSITION_COMMENT)
2592  else if (stream->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
2594  else if (stream->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
2596  }
2597 
2598  return type;
2599 }
2600 
2614 {
2615  QMutexLocker locker(&m_trackLock);
2616 
2617  // Find the position of the streaminfo in m_tracks[kTrackTypeAudio]
2618  auto current = m_tracks[kTrackTypeAudio].begin();
2619  for (; current != m_tracks[kTrackTypeAudio].end(); ++current)
2620  {
2621  if (current->m_av_stream_index == streamIndex)
2622  break;
2623  }
2624 
2625  if (current == m_tracks[kTrackTypeAudio].end())
2626  {
2627  LOG(VB_GENERAL, LOG_WARNING, LOC +
2628  QString("Invalid stream index passed to "
2629  "SetupAudioStreamSubIndexes: %1").arg(streamIndex));
2630 
2631  return;
2632  }
2633 
2634  // Remove the extra substream or duplicate the current substream
2635  auto next = current + 1;
2636  if (current->m_av_substream_index == -1)
2637  {
2638  // Split stream in two (Language I + Language II)
2639  StreamInfo lang1 = *current;
2640  StreamInfo lang2 = *current;
2641  lang1.m_av_substream_index = 0;
2642  lang2.m_av_substream_index = 1;
2643  *current = lang1;
2644  m_tracks[kTrackTypeAudio].insert(next, lang2);
2645  return;
2646  }
2647 
2648  if ((next == m_tracks[kTrackTypeAudio].end()) ||
2649  (next->m_av_stream_index != streamIndex))
2650  {
2651  QString msg = QString(
2652  "Expected substream 1 (Language I) of stream %1\n\t\t\t"
2653  "following substream 0, found end of list or another stream.")
2654  .arg(streamIndex);
2655 
2656  LOG(VB_GENERAL, LOG_WARNING, LOC + msg);
2657 
2658  return;
2659  }
2660 
2661  // Remove extra stream info
2662  StreamInfo stream = *current;
2663  stream.m_av_substream_index = -1;
2664  *current = stream;
2665  m_tracks[kTrackTypeAudio].erase(next);
2666 }
2667 
2673 {
2674  if (!m_audio->HasAudioIn())
2675  return;
2676 
2677  m_avCodecLock.lock();
2678  for (uint i = 0; i < m_ic->nb_streams;)
2679  {
2680  AVStream *st = m_ic->streams[i];
2681  AVCodecContext *avctx = m_codecMap.FindCodecContext(st);
2682  if (avctx && avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2683  {
2685  av_remove_stream(m_ic, st->id, 0);
2686  i--;
2687  }
2688  else
2689  i++;
2690  }
2691  m_avCodecLock.unlock();
2692 }
2693 
2694 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
2695 {
2696  auto *decoder = static_cast<AvFormatDecoder*>(c->opaque);
2698  if (!std::any_of(decoder->m_renderFormats->cbegin(), decoder->m_renderFormats->cend(),
2699  [&type](auto Format) { return type == Format; }))
2700  {
2701  decoder->m_directRendering = false;
2702  return avcodec_default_get_buffer2(c, pic, flags);
2703  }
2704 
2705  decoder->m_directRendering = true;
2706  MythVideoFrame *frame = decoder->GetPlayer()->GetNextVideoFrame();
2707  if (!frame)
2708  return -1;
2709 
2710  // We pre-allocate frames to certain alignments. If the coded size differs from
2711  // those alignments then re-allocate the frame. Changes in frame type (e.g.
2712  // YV12 to NV12) are always reallocated.
2713  int width = (frame->m_width + MYTH_WIDTH_ALIGNMENT - 1) & ~(MYTH_WIDTH_ALIGNMENT - 1);
2714  int height = (frame->m_height + MYTH_HEIGHT_ALIGNMENT - 1) & ~(MYTH_HEIGHT_ALIGNMENT - 1);
2715 
2716  if ((frame->m_type != type) || (pic->width > width) || (pic->height > height))
2717  {
2718  if (!VideoBuffers::ReinitBuffer(frame, type, decoder->m_videoCodecId, pic->width, pic->height))
2719  return -1;
2720  // NB the video frame may now have a new size which is currenly not an issue
2721  // as the underlying size has already been passed through to the player.
2722  // But may cause issues down the line. We should add coded_width/height to
2723  // VideoFrame as well to ensure consistentency through the rendering
2724  // pipeline.
2725  // NB remember to compare coded width/height - not 'true' values - otherwsie
2726  // we continually re-allocate the frame.
2727  //frame->width = c->width;
2728  //frame->height = c->height;
2729  }
2730 
2731  frame->m_colorshifted = false;
2733  for (uint i = 0; i < 3; i++)
2734  {
2735  pic->data[i] = (i < max) ? (frame->m_buffer + frame->m_offsets[i]) : nullptr;
2736  pic->linesize[i] = frame->m_pitches[i];
2737  }
2738 
2739  pic->opaque = frame;
2740  pic->reordered_opaque = c->reordered_opaque;
2741 
2742  // Set release method
2743  AVBufferRef *buffer = av_buffer_create(reinterpret_cast<uint8_t*>(frame), 0,
2744  [](void* Opaque, uint8_t* Data)
2745  {
2746  auto *avfd = static_cast<AvFormatDecoder*>(Opaque);
2747  auto *vf = reinterpret_cast<MythVideoFrame*>(Data);
2748  if (avfd && avfd->GetPlayer())
2749  avfd->GetPlayer()->DeLimboFrame(vf);
2750  }
2751  , decoder, 0);
2752  pic->buf[0] = buffer;
2753 
2754  return 0;
2755 }
2756 
2757 #ifdef USING_DXVA2
2758 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
2759 {
2760  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
2761  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
2762 
2763  for (int i = 0; i < 4; i++)
2764  {
2765  pic->data[i] = nullptr;
2766  pic->linesize[i] = 0;
2767  }
2768  pic->opaque = frame;
2769  frame->pix_fmt = c->pix_fmt;
2770  pic->reordered_opaque = c->reordered_opaque;
2771  pic->data[0] = (uint8_t*)frame->buf;
2772  pic->data[3] = (uint8_t*)frame->buf;
2773 
2774  // Set release method
2775  AVBufferRef *buffer =
2776  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
2777  pic->buf[0] = buffer;
2778 
2779  return 0;
2780 }
2781 #endif
2782 
2783 void AvFormatDecoder::DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte)
2784 {
2785  if (!buf_size)
2786  return;
2787 
2788  // closed caption data
2789  //cc_data() {
2790  // reserved 1 0.0 1
2791  // process_cc_data_flag 1 0.1 bslbf
2792  bool process_cc_data = (buf[0] & 0x40) != 0;
2793  if (!process_cc_data)
2794  return; // early exit if process_cc_data_flag false
2795 
2796  // additional_data_flag 1 0.2 bslbf
2797  //bool additional_data = buf[0] & 0x20;
2798  // cc_count 5 0.3 uimsbf
2799  uint cc_count = buf[0] & 0x1f;
2800  // em_data 8 1.0
2801 
2802  if (buf_size < 2+(3*cc_count))
2803  return;
2804 
2805  DecodeCCx08(buf+2, cc_count*3, scte);
2806 }
2807 
2808 void AvFormatDecoder::DecodeCCx08(const uint8_t *buf, uint buf_size, bool scte)
2809 {
2810  if (buf_size < 3)
2811  return;
2812 
2813  bool had_608 = false;
2814  bool had_708 = false;
2815  for (uint cur = 0; cur + 2 < buf_size; cur += 3)
2816  {
2817  uint cc_code = buf[cur];
2818  bool cc_valid = (cc_code & 0x04) != 0U;
2819 
2820  uint data1 = buf[cur+1];
2821  uint data2 = buf[cur+2];
2822  uint data = (data2 << 8) | data1;
2823  uint cc_type = cc_code & 0x03;
2824 
2825  if (!cc_valid)
2826  {
2827  if (cc_type >= 0x2)
2829  continue;
2830  }
2831 
2832  if (scte || cc_type <= 0x1) // EIA-608 field-1/2
2833  {
2834  uint field = 0;
2835  if (cc_type == 0x2)
2836  {
2837  // SCTE repeated field
2838  field = (m_lastScteField == 0 ? 1 : 0);
2839  m_invertScteField = (m_invertScteField == 0 ? 1 : 0);
2840  }
2841  else
2842  {
2843  field = cc_type ^ m_invertScteField;
2844  }
2845 
2846  if (cc608_good_parity(data))
2847  {
2848  // in film mode, we may start at the wrong field;
2849  // correct if XDS start/cont/end code is detected
2850  // (must be field 2)
2851  if (scte && field == 0 &&
2852  (data1 & 0x7f) <= 0x0f && (data1 & 0x7f) != 0x00)
2853  {
2854  if (cc_type == 1)
2855  m_invertScteField = 0;
2856  field = 1;
2857 
2858  // flush decoder
2859  m_ccd608->FormatCC(0ms, -1, -1);
2860  }
2861 
2862  had_608 = true;
2863  m_ccd608->FormatCCField(duration_cast<std::chrono::milliseconds>(m_lastCcPtsu), field, data);
2864 
2865  m_lastScteField = field;
2866  }
2867  }
2868  else
2869  {
2870  had_708 = true;
2871  m_ccd708->decode_cc_data(cc_type, data1, data2);
2872  }
2873  }
2874  UpdateCaptionTracksFromStreams(had_608, had_708);
2875 }
2876 
2878  bool check_608, bool check_708)
2879 {
2880  bool need_change_608 = false;
2881  CC608Seen seen_608;
2882  if (check_608)
2883  {
2884  m_ccd608->GetServices(15s, seen_608);
2885  for (uint i = 0; i < 4; i++)
2886  {
2887  need_change_608 |= (seen_608[i] && !m_ccX08InTracks[i]) ||
2888  (!seen_608[i] && m_ccX08InTracks[i] && !m_ccX08InPmt[i]);
2889  }
2890  }
2891 
2892  bool need_change_708 = false;
2893  cc708_seen_flags seen_708;
2894  if (check_708 || need_change_608)
2895  {
2896  m_ccd708->services(15s, seen_708);
2897  for (uint i = 1; i < 64 && !need_change_608 && !need_change_708; i++)
2898  {
2899  need_change_708 |= (seen_708[i] && !m_ccX08InTracks[i+4]) ||
2900  (!seen_708[i] && m_ccX08InTracks[i+4] && !m_ccX08InPmt[i+4]);
2901  }
2902  if (need_change_708 && !check_608)
2903  m_ccd608->GetServices(15s, seen_608);
2904  }
2905 
2906  if (!need_change_608 && !need_change_708)
2907  return;
2908 
2909  m_trackLock.lock();
2910 
2912 
2913  m_streamTracks.clear();
2914  m_streamTrackTypes.clear();
2915  int av_index = m_selectedTrack[kTrackTypeVideo].m_av_stream_index;
2916  int lang = iso639_str3_to_key("und");
2917  for (uint i = 1; i < 64; i++)
2918  {
2919  if (seen_708[i] && !m_ccX08InPmt[i+4])
2920  {
2921  StreamInfo si(av_index, lang, 0/*lang_idx*/, i, 0, false/*easy*/, true/*wide*/);
2922  m_streamTracks.push_back(si);
2924  }
2925  }
2926  for (uint i = 0; i < 4; i++)
2927  {
2928  if (seen_608[i] && !m_ccX08InPmt[i])
2929  {
2930  if (0==i)
2932  else if (2==i)
2934  else
2935  lang = iso639_str3_to_key("und");
2936 
2937  StreamInfo si(av_index, lang, 0/*lang_idx*/, i+1, 0, false/*easy*/, false/*wide*/);
2938  m_streamTracks.push_back(si);
2940  }
2941  }
2942  m_trackLock.unlock();
2944 }
2945 
2947  AVPacket *pkt, bool can_reliably_parse_keyframes)
2948 {
2949  if (m_prevGopPos != 0 && m_keyframeDist != 1)
2950  {
2951  int tempKeyFrameDist = m_framesRead - 1 - m_prevGopPos;
2952  bool reset_kfd = false;
2953 
2954  if (!m_gopSet || m_livetv) // gopset: we've seen 2 keyframes
2955  {
2956  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2957  "gopset not set, syncing positionMap");
2958  SyncPositionMap();
2959  if (tempKeyFrameDist > 0 && !m_livetv)
2960  {
2961  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2962  QString("Initial key frame distance: %1.")
2963  .arg(m_keyframeDist));
2964  m_gopSet = true;
2965  reset_kfd = true;
2966  }
2967  }
2968  else if (m_keyframeDist != tempKeyFrameDist && tempKeyFrameDist > 0)
2969  {
2970  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2971  QString("Key frame distance changed from %1 to %2.")
2972  .arg(m_keyframeDist).arg(tempKeyFrameDist));
2973  reset_kfd = true;
2974  }
2975 
2976  if (reset_kfd)
2977  {
2978  m_keyframeDist = tempKeyFrameDist;
2980 
2982 
2983 #if 0
2984  // also reset length
2985  QMutexLocker locker(&m_positionMapLock);
2986  if (!m_positionMap.empty())
2987  {
2988  long long index = m_positionMap.back().index;
2989  long long totframes = index * m_keyframeDist;
2990  uint length = (uint)((totframes * 1.0F) / m_fps);
2991  m_parent->SetFileLength(length, totframes);
2992  }
2993 #endif
2994  }
2995  }
2996 
2998 
2999  if (can_reliably_parse_keyframes &&
3001  {
3002  long long last_frame = 0;
3003  {
3004  QMutexLocker locker(&m_positionMapLock);
3005  if (!m_positionMap.empty())
3006  last_frame = m_positionMap.back().index;
3007  }
3008 
3009 #if 0
3010  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
3011  QString("framesRead: %1 last_frame: %2 keyframedist: %3")
3012  .arg(m_framesRead) .arg(last_frame) .arg(m_keyframeDist));
3013 #endif
3014 
3015  // if we don't have an entry, fill it in with what we've just parsed
3016  if (m_framesRead > last_frame && m_keyframeDist > 0)
3017  {
3018  long long startpos = pkt->pos;
3019 
3020  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3021  QString("positionMap[ %1 ] == %2.")
3022  .arg(m_framesRead).arg(startpos));
3023 
3024  PosMapEntry entry = {m_framesRead, m_framesRead, startpos};
3025 
3026  QMutexLocker locker(&m_positionMapLock);
3027  // Create a dummy positionmap entry for frame 0 so that
3028  // seeking will work properly. (See
3029  // DecoderBase::FindPosition() which subtracts
3030  // DecoderBase::indexOffset from each frame number.)
3031  if (m_positionMap.empty())
3032  {
3033  PosMapEntry dur = {0, 0, 0};
3034  m_positionMap.push_back(dur);
3035  }
3036  m_positionMap.push_back(entry);
3038  {
3040  llround(m_totalDuration.num * 1000.0 / m_totalDuration.den);
3042  }
3043  }
3044 
3045 #if 0
3046  // If we are > 150 frames in and saw no positionmap at all, reset
3047  // length based on the actual bitrate seen so far
3049  {
3050  m_bitrate = (int)((pkt->pos * 8 * m_fps) / (m_framesRead - 1));
3051  float bytespersec = (float)m_bitrate / 8;
3052  float secs = m_ringBuffer->GetRealFileSize() * 1.0F / bytespersec;
3053  m_parent->SetFileLength((int)(secs), (int)(secs * m_fps));
3054  }
3055 #endif
3056  }
3057 }
3058 
3059 static constexpr uint32_t SEQ_START { 0x000001b3 };
3060 static constexpr uint32_t GOP_START { 0x000001b8 };
3061 //static constexpr uint32_t PICTURE_START { 0x00000100 };
3062 static constexpr uint32_t SLICE_MIN { 0x00000101 };
3063 static constexpr uint32_t SLICE_MAX { 0x000001af };
3064 //static constexpr uint32_t SEQ_END_CODE { 0x000001b7 };
3065 
3066 void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
3067 {
3068  AVCodecContext *context = m_codecMap.GetCodecContext(stream);
3069  const uint8_t *bufptr = pkt->data;
3070  const uint8_t *bufend = pkt->data + pkt->size;
3071 
3072  while (bufptr < bufend)
3073  {
3074  bufptr = ByteReader::find_start_code_truncated(bufptr, bufend, &m_startCodeState);
3075 
3076  float aspect_override = -1.0F;
3077  if (m_ringBuffer->IsDVD())
3078  aspect_override = m_ringBuffer->DVD()->GetAspectOverride();
3079 
3081  continue;
3082 
3083  if (SEQ_START == m_startCodeState)
3084  {
3085  if (bufptr + 11 >= pkt->data + pkt->size)
3086  continue; // not enough valid data...
3087  const auto *seq = reinterpret_cast<const SequenceHeader*>(bufptr);
3088 
3089  int width = static_cast<int>(seq->width()) >> context->lowres;
3090  int height = static_cast<int>(seq->height()) >> context->lowres;
3091  float aspect = seq->aspect(context->codec_id == AV_CODEC_ID_MPEG1VIDEO);
3092  if (stream->sample_aspect_ratio.num)
3093  aspect = static_cast<float>(av_q2d(stream->sample_aspect_ratio) * width / height);
3094  if (aspect_override > 0.0F)
3095  aspect = aspect_override;
3096  float seqFPS = seq->fps();
3097 
3098  bool changed = (width != m_currentWidth );
3099  changed |= (height != m_currentHeight);
3100  changed |= (seqFPS > static_cast<float>(m_fps)+0.01F) ||
3101  (seqFPS < static_cast<float>(m_fps)-0.01F);
3102 
3103  // some hardware decoders (e.g. VAAPI MPEG2) will reset when the aspect
3104  // ratio changes
3105  bool forceaspectchange = !qFuzzyCompare(m_currentAspect + 10.0F, aspect + 10.0F) &&
3107  m_currentAspect = aspect;
3108 
3109  if (changed || forceaspectchange)
3110  {
3111  // N.B. We now set the default scan to kScan_Ignore as interlaced detection based on frame
3112  // size and rate is extremely error prone and FFmpeg gets it right far more often.
3113  // As for H.264, if a decoder deinterlacer is in operation - the stream must be progressive
3114  bool doublerate = false;
3115  bool decoderdeint = m_mythCodecCtx && m_mythCodecCtx->IsDeinterlacing(doublerate, true);
3116  m_parent->SetVideoParams(width, height, static_cast<double>(seqFPS), m_currentAspect,
3117  forceaspectchange, 2,
3118  decoderdeint ? kScan_Progressive : kScan_Ignore);
3119 
3120  if (context->hw_frames_ctx)
3121  if (context->internal)
3122  avcodec_flush_buffers(context);
3123 
3124  m_currentWidth = width;
3125  m_currentHeight = height;
3126  m_fps = seqFPS;
3127 
3128  m_gopSet = false;
3129  m_prevGopPos = 0;
3130  m_firstVPts = m_lastAPts = m_lastVPts = 0ms;
3131  m_lastCcPtsu = 0us;
3132  m_firstVPtsInuse = true;
3133  m_faultyPts = m_faultyDts = 0;
3136  m_ptsDetected = false;
3137  m_reorderedPtsDetected = false;
3138 
3139  // fps debugging info
3140  float avFPS = GetVideoFrameRate(stream, context);
3141  if ((seqFPS > avFPS+0.01F) || (seqFPS < avFPS-0.01F))
3142  {
3143  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("avFPS(%1) != seqFPS(%2)")
3144  .arg(static_cast<double>(avFPS)).arg(static_cast<double>(seqFPS)));
3145  }
3146  }
3147 
3148  m_seqCount++;
3149 
3150  if (!m_seenGop && m_seqCount > 1)
3151  {
3152  HandleGopStart(pkt, true);
3153  pkt->flags |= AV_PKT_FLAG_KEY;
3154  }
3155  }
3156  else if (GOP_START == m_startCodeState)
3157  {
3158  HandleGopStart(pkt, true);
3159  m_seenGop = true;
3160  pkt->flags |= AV_PKT_FLAG_KEY;
3161  }
3162  }
3163 }
3164 
3165 // Returns the number of frame starts identified in the packet.
3166 int AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
3167 {
3168  AVCodecContext *context = m_codecMap.GetCodecContext(stream);
3169  const uint8_t *buf = pkt->data;
3170  const uint8_t *buf_end = pkt->data + pkt->size;
3171  int num_frames = 0;
3172 
3173  // The parser only understands Annex B/bytestream format - so check for avCC
3174  // format (starts with 0x01) and rely on FFmpeg keyframe detection
3175  if (context->extradata && (context->extradata_size >= 7) && (context->extradata[0] == 0x01))
3176  {
3177  if (pkt->flags & AV_PKT_FLAG_KEY)
3178  HandleGopStart(pkt, false);
3179  return 1;
3180  }
3181 
3182  while (buf < buf_end)
3183  {
3184  buf += m_avcParser->addBytes(buf, static_cast<unsigned int>(buf_end - buf), 0);
3185 
3186  if (m_avcParser->stateChanged())
3187  {
3189  {
3190  if (m_avcParser->onFrameStart())
3191  ++num_frames;
3192 
3193  if (!m_avcParser->onKeyFrameStart())
3194  continue;
3195  }
3196  else
3197  {
3198  continue;
3199  }
3200  }
3201  else
3202  {
3203  continue;
3204  }
3205 
3207  float aspect = get_aspect(*m_avcParser);
3208  int width = static_cast<int>(m_avcParser->pictureWidthCropped());
3209  int height = static_cast<int>(m_avcParser->pictureHeightCropped());
3210  double seqFPS = m_avcParser->frameRate();
3211 
3212  bool res_changed = ((width != m_currentWidth) || (height != m_currentHeight));
3213  bool fps_changed = (seqFPS > 0.0) && ((seqFPS > static_cast<double>(m_fps) + 0.01) ||
3214  (seqFPS < static_cast<double>(m_fps) - 0.01));
3215  bool forcechange = !qFuzzyCompare(aspect + 10.0F, m_currentAspect) &&
3217  m_currentAspect = aspect;
3218 
3219  if (fps_changed || res_changed || forcechange)
3220  {
3221  // N.B. we now set the default scan to kScan_Ignore as interlaced detection based on frame
3222  // size and rate is extremely error prone and FFmpeg gets it right far more often.
3223  // N.B. if a decoder deinterlacer is in use - the stream must be progressive
3224  bool doublerate = false;
3225  bool decoderdeint = m_mythCodecCtx ? m_mythCodecCtx->IsDeinterlacing(doublerate, true) : false;
3226  m_parent->SetVideoParams(width, height, seqFPS, m_currentAspect, forcechange,
3227  static_cast<int>(m_avcParser->getRefFrames()),
3228  decoderdeint ? kScan_Progressive : kScan_Ignore);
3229 
3230  // the SetVideoParams call above will have released all held frames
3231  // when using a hardware frames context - but for H264 (as opposed to mpeg2)
3232  // the codec will still hold references for reference frames (decoded picture buffer).
3233  // Flushing the context will release these references and the old
3234  // hardware context is released correctly before a new one is created.
3235  // TODO check what is needed here when a device context is used
3236  // TODO check whether any codecs need to be flushed for a frame rate change (e.g. mediacodec?)
3237  if (context->hw_frames_ctx && (forcechange || res_changed))
3238  if (context->internal)
3239  avcodec_flush_buffers(context);
3240 
3241  m_currentWidth = width;
3242  m_currentHeight = height;
3243 
3244  if (seqFPS > 0.0)
3245  m_fps = static_cast<float>(seqFPS);
3246 
3247  m_gopSet = false;
3248  m_prevGopPos = 0;
3249  m_firstVPts = m_lastAPts = m_lastVPts = 0ms;
3250  m_lastCcPtsu = 0us;
3251  m_firstVPtsInuse = true;
3252  m_faultyPts = m_faultyDts = 0;
3255  m_ptsDetected = false;
3256  m_reorderedPtsDetected = false;
3257 
3258  // fps debugging info
3259  auto avFPS = static_cast<double>(GetVideoFrameRate(stream, context));
3260  if ((seqFPS > avFPS + 0.01) || (seqFPS < avFPS - 0.01))
3261  {
3262  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3263  QString("avFPS(%1) != seqFPS(%2)").arg(avFPS).arg(seqFPS));
3264  }
3265  }
3266 
3267  HandleGopStart(pkt, true);
3268  pkt->flags |= AV_PKT_FLAG_KEY;
3269  }
3270 
3271  return num_frames;
3272 }
3273 
3274 bool AvFormatDecoder::PreProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
3275 {
3276  AVCodecContext *context = m_codecMap.GetCodecContext(curstream);
3277  int num_frames = 1;
3278 
3279  if (CODEC_IS_MPEG(context->codec_id))
3280  {
3281  MpegPreProcessPkt(curstream, pkt);
3282  }
3283  else if (CODEC_IS_H264(context->codec_id))
3284  {
3285  num_frames = H264PreProcessPkt(curstream, pkt);
3286  }
3287  else
3288  {
3289  if (pkt->flags & AV_PKT_FLAG_KEY)
3290  {
3291  HandleGopStart(pkt, false);
3292  m_seenGop = true;
3293  }
3294  else
3295  {
3296  m_seqCount++;
3297  if (!m_seenGop && m_seqCount > 1)
3298  {
3299  HandleGopStart(pkt, false);
3300  }
3301  }
3302  }
3303 
3304  if (m_framesRead == 0 && !m_justAfterChange &&
3305  !(pkt->flags & AV_PKT_FLAG_KEY))
3306  {
3307  av_packet_unref(pkt);
3308  return false;
3309  }
3310 
3311  m_framesRead += num_frames;
3312 
3314  {
3315  // The ffmpeg libraries represent a frame interval of a
3316  // 59.94fps video as 1501/90000 seconds, when it should
3317  // actually be 1501.5/90000 seconds.
3318  AVRational pkt_dur = AVRationalInit(pkt->duration);
3319  pkt_dur = av_mul_q(pkt_dur, curstream->time_base);
3320  if (pkt_dur.num == 1501 && pkt_dur.den == 90000)
3321  pkt_dur = AVRationalInit(1001, 60000); // 1501.5/90000
3322  m_totalDuration = av_add_q(m_totalDuration, pkt_dur);
3323  }
3324 
3325  m_justAfterChange = false;
3326 
3327  if (m_exitAfterDecoded)
3328  m_gotVideoFrame = true;
3329 
3330  return true;
3331 }
3332 
3333 bool AvFormatDecoder::ProcessVideoPacket(AVStream *curstream, AVPacket *pkt, bool &Retry)
3334 {
3335  int ret = 0;
3336  int gotpicture = 0;
3337  AVCodecContext *context = m_codecMap.GetCodecContext(curstream);
3338  MythAVFrame mpa_pic;
3339  if (!mpa_pic)
3340  return false;
3341  mpa_pic->reordered_opaque = AV_NOPTS_VALUE;
3342 
3343  if (pkt->pts != AV_NOPTS_VALUE)
3344  m_ptsDetected = true;
3345 
3346  bool sentPacket = false;
3347  int ret2 = 0;
3348 
3349  m_avCodecLock.lock();
3350  if (!m_useFrameTiming)
3351  context->reordered_opaque = pkt->pts;
3352 
3353  // SUGGESTION
3354  // Now that avcodec_decode_video2 is deprecated and replaced
3355  // by 2 calls (receive frame and send packet), this could be optimized
3356  // into separate routines or separate threads.
3357  // Also now that it always consumes a whole buffer some code
3358  // in the caller may be able to be optimized.
3359 
3360  // FilteredReceiveFrame will call avcodec_receive_frame and
3361  // apply any codec-dependent filtering
3362  ret = m_mythCodecCtx->FilteredReceiveFrame(context, mpa_pic);
3363 
3364  if (ret == 0)
3365  gotpicture = 1;
3366  else
3367  gotpicture = 0;
3368  if (ret == AVERROR(EAGAIN))
3369  ret = 0;
3370  // If we got a picture do not send the packet until we have
3371  // all available pictures
3372  if (ret==0 && !gotpicture)
3373  {
3374  ret2 = avcodec_send_packet(context, pkt);
3375  if (ret2 == AVERROR(EAGAIN))
3376  {
3377  Retry = true;
3378  ret2 = 0;
3379  }
3380  else
3381  {
3382  sentPacket = true;
3383  }
3384  }
3385  m_avCodecLock.unlock();
3386 
3387  if (ret < 0 || ret2 < 0)
3388  {
3389  std::string error;
3390  if (ret < 0)
3391  {
3392  LOG(VB_GENERAL, LOG_ERR, LOC +
3393  QString("video avcodec_receive_frame error: %1 (%2) gotpicture:%3")
3394  .arg(av_make_error_stdstring(error, ret))
3395  .arg(ret).arg(gotpicture));
3396  }
3397 
3398  if (ret2 < 0)
3399  {
3400  LOG(VB_GENERAL, LOG_ERR, LOC +
3401  QString("video avcodec_send_packet error: %1 (%2) gotpicture:%3")
3402  .arg(av_make_error_stdstring(error, ret2))
3403  .arg(ret2).arg(gotpicture));
3404  }
3405 
3407  {
3408  // If erroring on GPU assist, try switching to software decode
3410  m_parent->SetErrored(QObject::tr("Video Decode Error"));
3411  else
3412  m_streamsChanged = true;
3413  }
3414 
3415  if (m_mythCodecCtx->DecoderNeedsReset(context))
3416  {
3417  LOG(VB_GENERAL, LOG_INFO, LOC + "Decoder needs reset");
3419  }
3420 
3421  if (ret == AVERROR_EXTERNAL || ret2 == AVERROR_EXTERNAL)
3422  {
3423  LOG(VB_PLAYBACK, LOG_INFO, LOC + "FFmpeg external library error - assuming streams changed");
3424  m_streamsChanged = true;
3425  }
3426 
3427  return false;
3428  }
3429 
3430  // averror_count counts sequential errors, so if you have a successful
3431  // packet then reset it
3432  m_averrorCount = 0;
3433  if (gotpicture)
3434  {
3435  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3436  QString("video timecodes packet-pts:%1 frame-pts:%2 packet-dts: %3 frame-dts:%4")
3437  .arg(pkt->pts).arg(mpa_pic->pts).arg(pkt->pts)
3438  .arg(mpa_pic->pkt_dts));
3439 
3440  if (!m_useFrameTiming)
3441  {
3442  int64_t pts = 0;
3443 
3444  // Detect faulty video timestamps using logic from ffplay.
3445  if (pkt->dts != AV_NOPTS_VALUE)
3446  {
3447  if (pkt->dts <= m_lastDtsForFaultDetection)
3448  m_faultyDts += 1;
3449  m_lastDtsForFaultDetection = pkt->dts;
3450  }
3451  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3452  {
3453  if (mpa_pic->reordered_opaque <= m_lastPtsForFaultDetection)
3454  m_faultyPts += 1;
3455  m_lastPtsForFaultDetection = mpa_pic->reordered_opaque;
3456  m_reorderedPtsDetected = true;
3457  }
3458 
3459  // Explicity use DTS for DVD since they should always be valid for every
3460  // frame and fixups aren't enabled for DVD.
3461  // Select reordered_opaque (PTS) timestamps if they are less faulty or the
3462  // the DTS timestamp is missing. Also use fixups for missing PTS instead of
3463  // DTS to avoid oscillating between PTS and DTS. Only select DTS if PTS is
3464  // more faulty or never detected.
3466  {
3467  if (pkt->dts != AV_NOPTS_VALUE)
3468  pts = pkt->dts;
3469  m_ptsSelected = false;
3470  }
3472  {
3473  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3474  pts = mpa_pic->reordered_opaque;
3475  m_ptsSelected = true;
3476  }
3477  else if (pkt->dts != AV_NOPTS_VALUE)
3478  {
3479  pts = pkt->dts;
3480  m_ptsSelected = false;
3481  }
3482 
3483  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_DEBUG, LOC +
3484  QString("video packet timestamps reordered %1 pts %2 dts %3 (%4)")
3485  .arg(mpa_pic->reordered_opaque).arg(pkt->pts).arg(pkt->dts)
3486  .arg((m_forceDtsTimestamps) ? "dts forced" :
3487  (m_ptsSelected) ? "reordered" : "dts"));
3488 
3489  mpa_pic->reordered_opaque = pts;
3490  }
3491  ProcessVideoFrame(curstream, mpa_pic);
3492  }
3493 
3494  if (!sentPacket)
3495  {
3496  // MythTV logic expects that only one frame is processed
3497  // Save the packet for later and return.
3498  auto *newPkt = av_packet_clone(pkt);
3499  if (newPkt)
3500  m_storedPackets.prepend(newPkt);
3501  }
3502  return true;
3503 }
3504 
3505 bool AvFormatDecoder::ProcessVideoFrame(AVStream *Stream, AVFrame *AvFrame)
3506 {
3507 
3508  auto * context = m_codecMap.GetCodecContext(Stream);
3509 
3510  // We need to mediate between ATSC and SCTE data when both are present. If
3511  // both are present, we generally want to prefer ATSC. However, there may
3512  // be large sections of the recording where ATSC is used and other sections
3513  // where SCTE is used. In that case, we want to allow a natural transition
3514  // from ATSC back to SCTE. We do this by allowing 10 consecutive SCTE
3515  // frames, without an intervening ATSC frame, to cause a switch back to
3516  // considering SCTE frames. The number 10 is somewhat arbitrarily chosen.
3517 
3518  uint cc_len = static_cast<uint>(std::max(AvFrame->scte_cc_len,0));
3519  uint8_t *cc_buf = AvFrame->scte_cc_buf;
3520  bool scte = true;
3521 
3522  // If we saw SCTE, then decrement a nonzero ignore_scte count.
3523  if (cc_len > 0 && m_ignoreScte)
3524  --m_ignoreScte;
3525 
3526  // If both ATSC and SCTE caption data are available, prefer ATSC
3527  if ((AvFrame->atsc_cc_len > 0) || m_ignoreScte)
3528  {
3529  cc_len = static_cast<uint>(std::max(AvFrame->atsc_cc_len, 0));
3530  cc_buf = AvFrame->atsc_cc_buf;
3531  scte = false;
3532  // If we explicitly saw ATSC, then reset ignore_scte count.
3533  if (cc_len > 0)
3534  m_ignoreScte = 10;
3535  }
3536 
3537  // Decode CEA-608 and CEA-708 captions
3538  for (uint i = 0; i < cc_len; i += ((cc_buf[i] & 0x1f) * 3) + 2)
3539  DecodeDTVCC(cc_buf + i, cc_len - i, scte);
3540 
3541  // look for A53 captions
3542  if (cc_len == 0)
3543  {
3544  auto * side_data = av_frame_get_side_data(AvFrame, AV_FRAME_DATA_A53_CC);
3545  if (side_data && (side_data->size > 0))
3546  DecodeCCx08(side_data->data, static_cast<uint>(side_data->size), false);
3547  }
3548 
3549  auto * frame = static_cast<MythVideoFrame*>(AvFrame->opaque);
3550  if (frame)
3552 
3554  {
3555  // Do nothing, we just want the pts, captions, subtites, etc.
3556  // So we can release the unconverted blank video frame to the
3557  // display queue.
3558  if (frame)
3559  frame->m_directRendering = false;
3560  }
3561  else if (!m_directRendering)
3562  {
3563  MythVideoFrame *oldframe = frame;
3564  frame = m_parent->GetNextVideoFrame();
3565  frame->m_directRendering = false;
3566 
3567  if (!m_mythCodecCtx->RetrieveFrame(context, frame, AvFrame))
3568  {
3569  AVFrame tmppicture;
3570  av_image_fill_arrays(tmppicture.data, tmppicture.linesize,
3571  frame->m_buffer, AV_PIX_FMT_YUV420P, AvFrame->width,
3572  AvFrame->height, IMAGE_ALIGN);
3573  tmppicture.data[0] = frame->m_buffer + frame->m_offsets[0];
3574  tmppicture.data[1] = frame->m_buffer + frame->m_offsets[1];
3575  tmppicture.data[2] = frame->m_buffer + frame->m_offsets[2];
3576  tmppicture.linesize[0] = frame->m_pitches[0];
3577  tmppicture.linesize[1] = frame->m_pitches[1];
3578  tmppicture.linesize[2] = frame->m_pitches[2];
3579 
3580  QSize dim = get_video_dim(*context);
3581  m_swsCtx = sws_getCachedContext(m_swsCtx, AvFrame->width,
3582  AvFrame->height, static_cast<AVPixelFormat>(AvFrame->format),
3583  AvFrame->width, AvFrame->height,
3584  AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
3585  nullptr, nullptr, nullptr);
3586  if (!m_swsCtx)
3587  {
3588  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to allocate sws context");
3589  return false;
3590  }
3591  sws_scale(m_swsCtx, AvFrame->data, AvFrame->linesize, 0, dim.height(),
3592  tmppicture.data, tmppicture.linesize);
3593  }
3594 
3595  // Discard any old VideoFrames
3596  if (oldframe)
3597  {
3598  // Set the frame flags, but then discard it
3599  // since we are not using it for display.
3600  oldframe->m_interlaced = AvFrame->interlaced_frame;
3601  oldframe->m_topFieldFirst = AvFrame->top_field_first != 0;
3602  oldframe->m_colorspace = AvFrame->colorspace;
3603  oldframe->m_colorrange = AvFrame->color_range;
3604  oldframe->m_colorprimaries = AvFrame->color_primaries;
3605  oldframe->m_colortransfer = AvFrame->color_trc;
3606  oldframe->m_chromalocation = AvFrame->chroma_location;
3607  oldframe->m_frameNumber = m_framesPlayed;
3608  oldframe->m_frameCounter = m_frameCounter++;
3609  oldframe->m_aspect = m_currentAspect;
3610  oldframe->m_rotation = m_videoRotation;
3611  oldframe->m_stereo3D = m_stereo3D;
3612 
3613  oldframe->m_dummy = false;
3614  oldframe->m_pauseFrame = false;
3615  oldframe->m_interlacedReverse = false;
3616  oldframe->m_newGOP = false;
3617  oldframe->m_deinterlaceInuse = DEINT_NONE;
3618  oldframe->m_deinterlaceInuse2x = false;
3619  oldframe->m_alreadyDeinterlaced = false;
3620 
3621  m_parent->DiscardVideoFrame(oldframe);
3622  }
3623  }
3624 
3625  if (!frame)
3626  {
3627  LOG(VB_GENERAL, LOG_ERR, LOC + "NULL videoframe - direct rendering not "
3628  "correctly initialized.");
3629  return false;
3630  }
3631 
3632  std::chrono::milliseconds pts = 0ms;
3633  if (m_useFrameTiming)
3634  {
3635  long long av_pts = AvFrame->pts;
3636  if (av_pts == AV_NOPTS_VALUE)
3637  av_pts = AvFrame->pkt_dts;
3638  if (av_pts == AV_NOPTS_VALUE)
3639  av_pts = AvFrame->reordered_opaque;
3640  if (av_pts == AV_NOPTS_VALUE)
3641  {
3642  LOG(VB_GENERAL, LOG_ERR, LOC + "No PTS found - unable to process video.");
3643  return false;
3644  }
3645  pts = millisecondsFromFloat(av_q2d(Stream->time_base) * av_pts * 1000);
3646  }
3647  else
3648  pts = millisecondsFromFloat(av_q2d(Stream->time_base) * AvFrame->reordered_opaque * 1000);
3649 
3650  std::chrono::milliseconds temppts = pts;
3651  // Validate the video pts against the last pts. If it's
3652  // a little bit smaller, equal or missing, compute
3653  // it from the last. Otherwise assume a wraparound.
3654  if (!m_ringBuffer->IsDVD() &&
3655  temppts <= m_lastVPts &&
3656  (temppts + millisecondsFromFloat(1000 / m_fps) > m_lastVPts ||
3657  temppts <= 0ms))
3658  {
3659  temppts = m_lastVPts;
3660  temppts += millisecondsFromFloat(1000 / m_fps);
3661  // MPEG2/H264 frames can be repeated, update pts accordingly
3662  temppts += millisecondsFromFloat(AvFrame->repeat_pict * 500 / m_fps);
3663  }
3664 
3665  // Calculate actual fps from the pts values.
3666  std::chrono::milliseconds ptsdiff = temppts - m_lastVPts;
3667  double calcfps = 1000.0 / ptsdiff.count();
3668  if (calcfps < 121.0 && calcfps > 3.0)
3669  {
3670  // If fps has doubled due to frame-doubling deinterlace
3671  // Set fps to double value.
3672  double fpschange = calcfps / static_cast<double>(m_fps);
3673  int prior = m_fpsMultiplier;
3674  if (fpschange > 1.9 && fpschange < 2.1)
3675  m_fpsMultiplier = 2;
3676  if (fpschange > 0.9 && fpschange < 1.1)
3677  m_fpsMultiplier = 1;
3678  if (m_fpsMultiplier != prior)
3679  m_parent->SetFrameRate(static_cast<double>(m_fps));
3680  }
3681 
3682  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3683  QString("video timecode %1 %2 %3 %4%5")
3684  .arg(m_useFrameTiming ? AvFrame->pts : AvFrame->reordered_opaque)
3685  .arg(pts.count()).arg(temppts.count()).arg(m_lastVPts.count())
3686  .arg((pts != temppts) ? " fixup" : ""));
3687 
3688  frame->m_interlaced = AvFrame->interlaced_frame;
3689  frame->m_topFieldFirst = AvFrame->top_field_first != 0;
3690  frame->m_newGOP = m_nextDecodedFrameIsKeyFrame;
3691  frame->m_repeatPic = AvFrame->repeat_pict != 0;
3692  frame->m_displayTimecode = NormalizeVideoTimecode(Stream, std::chrono::milliseconds(temppts));
3693  frame->m_frameNumber = m_framesPlayed;
3694  frame->m_frameCounter = m_frameCounter++;
3695  frame->m_aspect = m_currentAspect;
3696  frame->m_colorspace = AvFrame->colorspace;
3697  frame->m_colorrange = AvFrame->color_range;
3698  frame->m_colorprimaries = AvFrame->color_primaries;
3699  frame->m_colortransfer = AvFrame->color_trc;
3700  frame->m_chromalocation = AvFrame->chroma_location;
3701  frame->m_pixFmt = AvFrame->format;
3702  frame->m_deinterlaceInuse = DEINT_NONE;
3703  frame->m_rotation = m_videoRotation;
3704  frame->m_stereo3D = m_stereo3D;
3705 
3706  frame->m_dummy = false;
3707  frame->m_pauseFrame = false;
3708  frame->m_deinterlaceInuse2x = false;
3709  frame->m_alreadyDeinterlaced = false;
3710  frame->m_interlacedReverse = false;
3711 
3712  // Retrieve HDR metadata
3713  MythHDRVideoMetadata::Populate(frame, AvFrame);
3714 
3715  m_parent->ReleaseNextVideoFrame(frame, std::chrono::milliseconds(temppts));
3716  m_mythCodecCtx->PostProcessFrame(context, frame);
3717 
3719  m_decodedVideoFrame = frame;
3720  m_gotVideoFrame = true;
3721  if (++m_fpsSkip >= m_fpsMultiplier)
3722  {
3723  ++m_framesPlayed;
3724  m_fpsSkip = 0;
3725  }
3726 
3727  m_lastVPts = temppts;
3728  if ((m_firstVPts == 0ms) && m_firstVPtsInuse)
3729  m_firstVPts = temppts;
3730 
3731  return true;
3732 }
3733 
3740  const AVStream *stream, const AVPacket *pkt)
3741 {
3742  (void) stream;
3743 
3744  const uint8_t *buf = pkt->data;
3745  uint64_t linemask = 0;
3746  std::chrono::microseconds utc = m_lastCcPtsu;
3747 
3748  // [i]tv0 means there is a linemask
3749  // [I]TV0 means there is no linemask and all lines are present
3750  if ((buf[0]=='t') && (buf[1]=='v') && (buf[2] == '0'))
3751  {
3753  memcpy(&linemask, buf + 3, 8);
3754  buf += 11;
3755  }
3756  else if ((buf[0]=='T') && (buf[1]=='V') && (buf[2] == '0'))
3757  {
3758  linemask = 0xffffffffffffffffLL;
3759  buf += 3;
3760  }
3761  else
3762  {
3763  LOG(VB_VBI, LOG_ERR, LOC + QString("Unknown VBI data stream '%1%2%3'")
3764  .arg(QChar(buf[0])).arg(QChar(buf[1])).arg(QChar(buf[2])));
3765  return;
3766  }
3767 
3768  static constexpr uint kMinBlank = 6;
3769  for (uint i = 0; i < 36; i++)
3770  {
3771  if (!((linemask >> i) & 0x1))
3772  continue;
3773 
3774  const uint line = ((i < 18) ? i : i-18) + kMinBlank;
3775  const uint field = (i<18) ? 0 : 1;
3776  const uint id2 = *buf & 0xf;
3777  switch (id2)
3778  {
3780  // SECAM lines 6-23
3781  // PAL lines 6-22
3782  // NTSC lines 10-21 (rare)
3783  m_trackLock.lock();
3784  if (m_tracks[kTrackTypeTeletextMenu].empty())
3785  {
3786  StreamInfo si(pkt->stream_index, 0, 0, 0, 0);
3787  m_trackLock.lock();
3788  m_tracks[kTrackTypeTeletextMenu].push_back(si);
3789  m_trackLock.unlock();
3790  }
3791  m_trackLock.unlock();
3792  m_ttd->Decode(buf+1, VBI_IVTV);
3793  break;
3795  // PAL line 22 (rare)
3796  // NTSC line 21
3797  if (21 == line)
3798  {
3799  int data = (buf[2] << 8) | buf[1];
3800  if (cc608_good_parity(data))
3801  m_ccd608->FormatCCField(duration_cast<std::chrono::milliseconds>(utc), field, data);
3802  utc += 33367us;
3803  }
3804  break;
3805  case V4L2_MPEG_VBI_IVTV_VPS: // Video Programming System
3806  // PAL line 16
3807  m_ccd608->DecodeVPS(buf+1); // a.k.a. PDC
3808  break;
3809  case V4L2_MPEG_VBI_IVTV_WSS_625: // Wide Screen Signal
3810  // PAL line 23
3811  // NTSC line 20
3812  m_ccd608->DecodeWSS(buf+1);
3813  break;
3814  }
3815  buf += 43;
3816  }
3817  m_lastCcPtsu = utc;
3818  UpdateCaptionTracksFromStreams(true, false);
3819 }
3820 
3826  const AVStream* /*stream*/, const AVPacket *pkt)
3827 {
3828  const uint8_t *buf = pkt->data;
3829  const uint8_t *buf_end = pkt->data + pkt->size;
3830 
3831 
3832  while (buf < buf_end)
3833  {
3834  if (*buf == 0x10)
3835  {
3836  buf++; // skip
3837  }
3838  else if (*buf == 0x02)
3839  {
3840  buf += 4;
3841  if ((buf_end - buf) >= 42)
3842  m_ttd->Decode(buf, VBI_DVB);
3843  buf += 42;
3844  }
3845  else if (*buf == 0x03)
3846  {
3847  buf += 4;
3848  if ((buf_end - buf) >= 42)
3849  m_ttd->Decode(buf, VBI_DVB_SUBTITLE);
3850  buf += 42;
3851  }
3852  else if (*buf == 0xff)
3853  {
3854  buf += 3;
3855  }
3856  else
3857  {
3858  LOG(VB_VBI, LOG_ERR, LOC +
3859  QString("VBI: Unknown descriptor: %1").arg(*buf));
3860  buf += 46;
3861  }
3862  }
3863 }
3864 
3868 void AvFormatDecoder::ProcessDSMCCPacket(const AVStream *str, const AVPacket *pkt)
3869 {
3870 #ifdef USING_MHEG
3871  if (!m_itv && ! (m_itv = m_parent->GetInteractiveTV()))
3872  return;
3873 
3874  // The packet may contain several tables.
3875  uint8_t *data = pkt->data;
3876  int length = pkt->size;
3877  int componentTag = 0;
3878  int dataBroadcastId = 0;
3879  unsigned carouselId = 0;
3880  {
3881  m_avCodecLock.lock();
3882  componentTag = str->component_tag;
3883  dataBroadcastId = str->data_id;
3884  carouselId = (unsigned) str->carousel_id;
3885  m_avCodecLock.unlock();
3886  }
3887  while (length > 3)
3888  {
3889  uint16_t sectionLen = (((data[1] & 0xF) << 8) | data[2]) + 3;
3890 
3891  if (sectionLen > length) // This may well be filler
3892  return;
3893 
3894  m_itv->ProcessDSMCCSection(data, sectionLen,
3895  componentTag, carouselId,
3896  dataBroadcastId);
3897  length -= sectionLen;
3898  data += sectionLen;
3899  }
3900 #else
3901  Q_UNUSED(str);
3902  Q_UNUSED(pkt);
3903 #endif // USING_MHEG
3904 }
3905 
3906 bool AvFormatDecoder::ProcessSubtitlePacket(AVStream *curstream, AVPacket *pkt)
3907 {
3908  if (!m_parent->GetSubReader(pkt->stream_index))
3909  return true;
3910 
3911  long long pts = pkt->pts;
3912  if (pts == AV_NOPTS_VALUE)
3913  pts = pkt->dts;
3914  if (pts == AV_NOPTS_VALUE)
3915  {
3916  LOG(VB_GENERAL, LOG_ERR, LOC + "No PTS found - unable to process subtitle.");
3917  return false;
3918  }
3919  pts = static_cast<long long>(av_q2d(curstream->time_base) * pts * 1000);
3920 
3921  m_trackLock.lock();
3922  int subIdx = m_selectedTrack[kTrackTypeSubtitle].m_av_stream_index;
3923  bool isForcedTrack = m_selectedTrack[kTrackTypeSubtitle].m_forced;
3924  m_trackLock.unlock();
3925 
3926  int gotSubtitles = 0;
3927  AVSubtitle subtitle;
3928  memset(&subtitle, 0, sizeof(AVSubtitle));
3929 
3930  if (m_ringBuffer->IsDVD())
3931  {
3932  if (m_ringBuffer->DVD()->NumMenuButtons() > 0)
3933  {
3934  m_ringBuffer->DVD()->GetMenuSPUPkt(pkt->data, pkt->size,
3935  curstream->id, pts);
3936  }
3937  else
3938  {
3939  if (pkt->stream_index == subIdx)
3940  {
3941  m_avCodecLock.lock();
3942  m_ringBuffer->DVD()->DecodeSubtitles(&subtitle, &gotSubtitles,
3943  pkt->data, pkt->size, pts);
3944  m_avCodecLock.unlock();
3945  }
3946  }
3947  }
3948  else if (m_decodeAllSubtitles || pkt->stream_index == subIdx)
3949  {
3950  m_avCodecLock.lock();
3951  AVCodecContext *ctx = m_codecMap.GetCodecContext(curstream);
3952  avcodec_decode_subtitle2(ctx, &subtitle, &gotSubtitles, pkt);
3953  m_avCodecLock.unlock();
3954 
3955  subtitle.start_display_time += pts;
3956  subtitle.end_display_time += pts;
3957  }
3958 
3959  if (gotSubtitles)
3960  {
3961  if (isForcedTrack)
3962  {
3963  for (unsigned i = 0; i < subtitle.num_rects; i++)
3964  {
3965  subtitle.rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
3966  }
3967  }
3968  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3969  QString("subtl timecode %1 %2 %3 %4")
3970  .arg(pkt->pts).arg(pkt->dts)
3971  .arg(subtitle.start_display_time)
3972  .arg(subtitle.end_display_time));
3973 
3974  bool forcedon = m_parent->GetSubReader(pkt->stream_index)->AddAVSubtitle(
3975  subtitle, curstream->codecpar->codec_id == AV_CODEC_ID_XSUB,
3977  m_parent->EnableForcedSubtitles(forcedon || isForcedTrack);
3978  }
3979 
3980  return true;
3981 }
3982 
3984 {
3985  if (!m_decodeAllSubtitles && m_selectedTrack[kTrackTypeRawText].m_av_stream_index != Packet->stream_index)
3986  return false;
3987 
3988  auto id = static_cast<uint>(Packet->stream_index + 0x2000);
3989  if (!m_parent->GetSubReader(id))
3990  return false;
3991 
3992 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
3993  const auto * codec = QTextCodec::codecForName("utf-8");
3994  auto text = codec->toUnicode(reinterpret_cast<const char *>(Packet->data), Packet->size - 1);
3995 #else
3996  auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
3997  QString text = toUtf16.decode(Packet->data);
3998 #endif
3999 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
4000  auto list = text.split('\n', QString::SkipEmptyParts);
4001 #else
4002  auto list = text.split('\n', Qt::SkipEmptyParts);
4003 #endif
4004  m_parent->GetSubReader(id)->AddRawTextSubtitle(list, std::chrono::milliseconds(Packet->duration));
4005  return true;
4006 }
4007 
4008 bool AvFormatDecoder::ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
4009  DecodeType decodetype)
4010 {
4011  enum AVCodecID codec_id = curstream->codecpar->codec_id;
4012 
4013  switch (codec_id)
4014  {
4015  case AV_CODEC_ID_MPEG2VBI:
4016  ProcessVBIDataPacket(curstream, pkt);
4017  break;
4018  case AV_CODEC_ID_DVB_VBI:
4019  ProcessDVBDataPacket(curstream, pkt);
4020  break;
4021  case AV_CODEC_ID_DSMCC_B:
4022  {
4023  ProcessDSMCCPacket(curstream, pkt);
4025  // Have to return regularly to ensure that the OSD is updated.
4026  // This applies both to MHEG and also channel browsing.
4027 #ifdef USING_MHEG
4028  if (!(decodetype & kDecodeVideo))
4030 #else
4031  Q_UNUSED(decodetype);
4032 #endif // USING_MHEG:
4033  break;
4034  }
4035  default:
4036  break;
4037  }
4038  return true;
4039 }
4040 
4041 int AvFormatDecoder::SetTrack(uint Type, int TrackNo)
4042 {
4043  QMutexLocker locker(&m_trackLock);
4044  int ret = DecoderBase::SetTrack(Type, TrackNo);
4045  if (kTrackTypeAudio == Type)
4046  {
4047  QString msg = SetupAudioStream() ? "" : "not ";
4048  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio stream type " + msg + "changed.");
4049  }
4050  return ret;
4051 }
4052 
4054 {
4055  QMutexLocker locker(&m_trackLock);
4056 
4057  if (!m_ic || TrackNo >= m_tracks[type].size())
4058  return "";
4059 
4060  bool forced = m_tracks[type][TrackNo].m_forced;
4061  int lang_key = m_tracks[type][TrackNo].m_language;
4062  QString forcedString = forced ? QObject::tr(" (forced)") : "";
4063 
4064  int av_index = m_tracks[type][TrackNo].m_av_stream_index;
4065  AVStream *stream { nullptr };
4066  if (av_index >= 0 && av_index < (int)m_ic->nb_streams)
4067  stream = m_ic->streams[av_index];
4068  AVDictionaryEntry *entry =
4069  stream ? av_dict_get(stream->metadata, "title", nullptr, 0) : nullptr;
4070  QString user_title = entry ? QString(R"( "%1")").arg(entry->value) : "";
4071 
4072  if (kTrackTypeAudio == type)
4073  {
4074  QString msg = iso639_key_toName(lang_key);
4075 
4076  switch (m_tracks[type][TrackNo].m_audio_type)
4077  {
4078  case kAudioTypeNormal :
4079  {
4080  if (stream)
4081  {
4082  AVCodecParameters *par = stream->codecpar;
4083  AVCodecContext *ctx = m_codecMap.GetCodecContext(stream);
4084  if (par->codec_id == AV_CODEC_ID_MP3)
4085  msg += QString(" MP3");
4086  else if (ctx && ctx->codec)
4087  msg += QString(" %1").arg(ctx->codec->name).toUpper();
4088  if (!user_title.isEmpty())
4089  msg += user_title;
4090 
4091  int channels = 0;
4092  if (m_ringBuffer->IsDVD() || par->ch_layout.nb_channels)
4093  channels = m_tracks[kTrackTypeAudio][TrackNo].m_orig_num_channels;
4094 
4095  if (channels == 0)
4096  msg += QString(" ?ch");
4097  else if((channels > 4) && !(channels & 1))
4098  msg += QString(" %1.1ch").arg(channels - 1);
4099  else
4100  msg += QString(" %1ch").arg(channels);
4101  }
4102  break;
4103  }
4105  case kAudioTypeCommentary :
4107  case kAudioTypeCleanEffects :
4108  case kAudioTypeSpokenSubs :
4109  default :
4110  if (!user_title.isEmpty())
4111  msg += user_title;
4112  else
4113  msg += QString(" (%1)")
4114  .arg(toString(m_tracks[type][TrackNo].m_audio_type));
4115  break;
4116  }
4117  return QString("%1: %2").arg(TrackNo + 1).arg(msg);
4118  }
4119  if (kTrackTypeSubtitle == type)
4120  {
4121  return QObject::tr("Subtitle") + QString(" %1: %2%3%4")
4122  .arg(QString::number(TrackNo + 1),
4123  iso639_key_toName(lang_key),
4124  user_title,
4125  forcedString);
4126  }
4127  if (forced && kTrackTypeRawText == type)
4128  return DecoderBase::GetTrackDesc(type, TrackNo) + forcedString;
4129  return DecoderBase::GetTrackDesc(type, TrackNo);
4130 }
4131 
4133 {
4134  return m_ttd->GetDecoderType();
4135 }
4136 
4137 QString AvFormatDecoder::GetXDS(const QString &Key) const
4138 {
4139  return m_ccd608->GetXDS(Key);
4140 }
4141 
4143 {
4144  QMutexLocker locker(&m_trackLock);
4145  if (TrackNo >= m_tracks[kTrackTypeSubtitle].size())
4146  return {};
4147 
4148  int index = m_tracks[kTrackTypeSubtitle][TrackNo].m_av_stream_index;
4149  AVCodecContext *ctx = m_codecMap.GetCodecContext(m_ic->streams[index]);
4150  return ctx ? QByteArray(reinterpret_cast<char*>(ctx->subtitle_header), ctx->subtitle_header_size) :
4151  QByteArray();
4152 }
4153 
4154 void AvFormatDecoder::GetAttachmentData(uint TrackNo, QByteArray &Filename, QByteArray &Data)
4155 {
4156  QMutexLocker locker(&m_trackLock);
4157  if (TrackNo >= m_tracks[kTrackTypeAttachment].size())
4158  return;
4159 
4160  int index = m_tracks[kTrackTypeAttachment][TrackNo].m_av_stream_index;
4161  AVDictionaryEntry *tag = av_dict_get(m_ic->streams[index]->metadata, "filename", nullptr, 0);
4162  if (tag)
4163  Filename = QByteArray(tag->value);
4164  AVCodecParameters *par = m_ic->streams[index]->codecpar;
4165  Data = QByteArray(reinterpret_cast<char*>(par->extradata), par->extradata_size);
4166 }
4167 
4169 {
4170  QMutexLocker locker(&m_trackLock);
4171  for (size_t i = 0; i < m_tracks[kTrackTypeAudio].size(); i++)
4172  {
4173  AVStream *stream = m_ic->streams[m_tracks[kTrackTypeAudio][i].m_av_stream_index];
4174  if (stream)
4175  if ((stream->component_tag == Tag) || ((Tag <= 0) && stream->component_tag <= 0))
4176  return SetTrack(kTrackTypeAudio, static_cast<int>(i)) != -1;
4177  }
4178  return false;
4179 }
4180 
4182 {
4183  QMutexLocker locker(&m_trackLock);
4184  for (uint i = 0; i < m_ic->nb_streams; i++)
4185  {
4186  AVStream *stream = m_ic->streams[i];
4187  if (stream)
4188  {
4189  if (stream->component_tag == Tag)
4190  {
4191  StreamInfo si(static_cast<int>(i), 0, 0, 0, 0);
4193  return true;
4194  }
4195  }
4196  }
4197  return false;
4198 }
4199 
4200 // documented in decoderbase.cpp
4202 {
4203  if (kTrackTypeAudio == type)
4204  return AutoSelectAudioTrack();
4205 
4207  return -1;
4208 
4210 }
4211 
4212 static std::vector<int> filter_lang(const sinfo_vec_t &tracks, int lang_key,
4213  const std::vector<int> &ftype)
4214 {
4215  std::vector<int> ret;
4216 
4217  for (int index : ftype)
4218  {
4219  if ((lang_key < 0) || tracks[index].m_language == lang_key)
4220  ret.push_back(index);
4221  }
4222 
4223  return ret;
4224 }
4225 
4226 static std::vector<int> filter_type(const sinfo_vec_t &tracks, AudioTrackType type)
4227 {
4228  std::vector<int> ret;
4229 
4230  for (size_t i = 0; i < tracks.size(); i++)
4231  {
4232  if (tracks[i].m_audio_type == type)
4233  ret.push_back(i);
4234  }
4235 
4236  return ret;
4237 }
4238 
4239 int AvFormatDecoder::filter_max_ch(const AVFormatContext *ic,
4240  const sinfo_vec_t &tracks,
4241  const std::vector<int>&fs,
4242  enum AVCodecID codecId,
4243  int profile)
4244 {
4245  int selectedTrack = -1;
4246  int max_seen = -1;
4247 
4248  for (int f : fs)
4249  {
4250  const int stream_index = tracks[f].m_av_stream_index;
4251  AVCodecParameters *par = ic->streams[stream_index]->codecpar;
4252  if ((codecId == AV_CODEC_ID_NONE || codecId == par->codec_id) &&
4253  (max_seen < par->ch_layout.nb_channels))
4254  {
4255  if (codecId == AV_CODEC_ID_DTS && profile > 0)
4256  {
4257  // we cannot decode dts-hd, so only select it if passthrough
4258  if (!DoPassThrough(par, true) || par->profile != profile)
4259  continue;
4260  }
4261  selectedTrack = f;
4262  max_seen = par->ch_layout.nb_channels;
4263  }
4264  }
4265 
4266  return selectedTrack;
4267 }
4268 
4316 {
4317  QMutexLocker locker(&m_trackLock);
4318 
4319  const sinfo_vec_t &atracks = m_tracks[kTrackTypeAudio];
4322  int &ctrack = m_currentTrack[kTrackTypeAudio];
4323 
4324  uint numStreams = atracks.size();
4325  if ((ctrack >= 0) && (ctrack < (int)numStreams))
4326  return ctrack; // audio already selected
4327 
4328 #if 0
4329  // enable this to print streams
4330  for (const auto & track : atracks)
4331  {
4332  int idx = track.m_av_stream_index;
4333  AVCodecContext *codec_ctx = m_ic->streams[idx]->codec;
4334  AudioInfo item(codec_ctx->codec_id, codec_ctx->bps,
4335  codec_ctx->sample_rate, codec_ctx->channels,
4336  DoPassThrough(codec_ctx, true));
4337  LOG(VB_AUDIO, LOG_DEBUG, LOC + " * " + item.toString());
4338  }
4339 #endif
4340 
4341  int selTrack = (1 == numStreams) ? 0 : -1;
4342  int wlang = wtrack.m_language;
4343 
4344  if ((selTrack < 0) && (wtrack.m_av_substream_index >= 0))
4345  {
4346  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio sub-stream");
4347  // Dual stream without language information: choose
4348  // the previous substream that was kept in wtrack,
4349  // ignoring the stream index (which might have changed).
4350  int substream_index = wtrack.m_av_substream_index;
4351 
4352  for (uint i = 0; i < numStreams; i++)
4353  {
4354  if (atracks[i].m_av_substream_index == substream_index)
4355  {
4356  selTrack = i;
4357  break;
4358  }
4359  }
4360  }
4361 
4362  if ((selTrack < 0) && wlang >= -1 && numStreams)
4363  {
4364  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio track");
4365  // Try to reselect user selected audio stream.
4366  // This should find the stream after a commercial
4367  // break and in some cases after a channel change.
4368  uint windx = wtrack.m_language_index;
4369  for (uint i = 0; i < numStreams; i++)
4370  {
4371  if (wlang == atracks[i].m_language)
4372  {
4373  selTrack = i;
4374 
4375  if (windx == atracks[i].m_language_index)
4376  break;
4377  }
4378  }
4379  }
4380 
4381  if (selTrack < 0 && numStreams)
4382  {
4383  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select audio track (w/lang)");
4384 
4385  // Filter out commentary and audio description tracks
4386  std::vector<int> ftype = filter_type(atracks, kAudioTypeNormal);
4387 
4388  if (ftype.empty())
4389  {
4390  LOG(VB_AUDIO, LOG_WARNING, "No audio tracks matched the type filter, "
4391  "so trying all tracks.");
4392  for (int i = 0; i < static_cast<int>(atracks.size()); i++)
4393  ftype.push_back(i);
4394  }
4395 
4396  // try to get the language track matching the frontend language.
4397  QString language_key_convert = iso639_str2_to_str3(gCoreContext->GetLanguage());
4398  uint language_key = iso639_str3_to_key(language_key_convert);
4399  uint canonical_key = iso639_key_to_canonical_key(language_key);
4400 
4401  std::vector<int> flang = filter_lang(atracks, canonical_key, ftype);
4402 
4403  if (m_audio->CanDTSHD())
4404  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4405  FF_PROFILE_DTS_HD_MA);
4406  if (selTrack < 0)
4407  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4408 
4409  if (selTrack < 0 && m_audio->CanDTSHD())
4410  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4411  FF_PROFILE_DTS_HD_HRA);
4412  if (selTrack < 0)
4413  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_EAC3);
4414 
4415  if (selTrack < 0)
4416  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4417 
4418  if (selTrack < 0)
4419  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4420 
4421  if (selTrack < 0)
4422  selTrack = filter_max_ch(m_ic, atracks, flang);
4423 
4424  // try to get best track for most preferred language
4425  // Set by the "Guide Data" language prefs in Appearance.
4426  if (selTrack < 0)
4427  {
4428  auto it = m_languagePreference.begin();
4429  for (; it != m_languagePreference.end() && selTrack < 0; ++it)
4430  {
4431  flang = filter_lang(atracks, *it, ftype);
4432 
4433  if (m_audio->CanDTSHD())
4434  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4435  FF_PROFILE_DTS_HD_MA);
4436  if (selTrack < 0)
4437  selTrack = filter_max_ch(m_ic, atracks, flang,
4438  AV_CODEC_ID_TRUEHD);
4439 
4440  if (selTrack < 0 && m_audio->CanDTSHD())
4441  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4442  FF_PROFILE_DTS_HD_HRA);
4443 
4444  if (selTrack < 0)
4445  selTrack = filter_max_ch(m_ic, atracks, flang,
4446  AV_CODEC_ID_EAC3);
4447 
4448  if (selTrack < 0)
4449  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4450 
4451  if (selTrack < 0)
4452  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4453 
4454  if (selTrack < 0)
4455  selTrack = filter_max_ch(m_ic, atracks, flang);
4456  }
4457  }
4458 
4459  // could not select track based on user preferences (language)
4460  // try to select the default track
4461  if (selTrack < 0)
4462  {
4463  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select default track");
4464  for (size_t i = 0; i < atracks.size(); i++) {
4465  int idx = atracks[i].m_av_stream_index;
4466  if (m_ic->streams[idx]->disposition & AV_DISPOSITION_DEFAULT)
4467  {
4468  selTrack = i;
4469  break;
4470  }
4471  }
4472  }
4473 
4474  // try to get best track for any language
4475  if (selTrack < 0)
4476  {
4477  LOG(VB_AUDIO, LOG_INFO, LOC +
4478  "Trying to select audio track (wo/lang)");
4479  flang = filter_lang(atracks, -1, ftype);
4480 
4481  if (m_audio->CanDTSHD())
4482  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4483  FF_PROFILE_DTS_HD_MA);
4484  if (selTrack < 0)
4485  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4486 
4487  if (selTrack < 0 && m_audio->CanDTSHD())
4488  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4489  FF_PROFILE_DTS_HD_HRA);
4490 
4491  if (selTrack < 0)
4492  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_EAC3);
4493 
4494  if (selTrack < 0)
4495  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4496 
4497  if (selTrack < 0)
4498  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4499 
4500  if (selTrack < 0)
4501  selTrack = filter_max_ch(m_ic, atracks, flang);
4502  }
4503  }
4504 
4505  if (selTrack < 0)
4506  {
4507  strack.m_av_stream_index = -1;
4508  if (ctrack != selTrack)
4509  {
4510  LOG(VB_AUDIO, LOG_INFO, LOC + "No suitable audio track exists.");
4511  ctrack = selTrack;
4512  }
4513  }
4514  else
4515  {
4516  ctrack = selTrack;
4517  strack = atracks[selTrack];
4518 
4519  if (wtrack.m_av_stream_index < 0)
4520  wtrack = strack;
4521 
4522  LOG(VB_AUDIO, LOG_INFO, LOC + QString("Selected track %1 (A/V Stream #%2)")
4523  .arg(static_cast<uint>(ctrack)).arg(strack.m_av_stream_index));
4524  }
4525 
4526  SetupAudioStream();
4527  return selTrack;
4528 }
4529 
4530 static void extract_mono_channel(uint channel, AudioInfo *audioInfo,
4531  char *buffer, int bufsize)
4532 {
4533  // Only stereo -> mono (left or right) is supported
4534  if (audioInfo->m_channels != 2)
4535  return;
4536 
4537  if (channel >= (uint)audioInfo->m_channels)
4538  return;
4539 
4540  const uint samplesize = audioInfo->m_sampleSize;
4541  const uint samples = bufsize / samplesize;
4542  const uint halfsample = samplesize >> 1;
4543 
4544  const char *from = (channel == 1) ? buffer + halfsample : buffer;
4545  char *to = (channel == 0) ? buffer + halfsample : buffer;
4546 
4547  for (uint sample = 0; sample < samples;
4548  (sample++), (from += samplesize), (to += samplesize))
4549  {
4550  memmove(to, from, halfsample);
4551  }
4552 }
4553 
4554 bool AvFormatDecoder::ProcessAudioPacket(AVStream *curstream, AVPacket *pkt,
4555  DecodeType decodetype)
4556 {
4557  AVCodecContext *ctx = m_codecMap.GetCodecContext(curstream);
4558  int ret = 0;
4559  int data_size = 0;
4560  bool firstloop = true;
4561  int decoded_size = -1;
4562 
4563  m_trackLock.lock();
4564  int audIdx = m_selectedTrack[kTrackTypeAudio].m_av_stream_index;
4565  int audSubIdx = m_selectedTrack[kTrackTypeAudio].m_av_substream_index;
4566  m_trackLock.unlock();
4567 
4568  AVPacket *tmp_pkt = av_packet_alloc();
4569  tmp_pkt->data = pkt->data;
4570  tmp_pkt->size = pkt->size;
4571  while (tmp_pkt->size > 0)
4572  {
4573  bool reselectAudioTrack = false;
4574 
4576  if (!m_audio->HasAudioIn())
4577  {
4578  LOG(VB_AUDIO, LOG_INFO, LOC +
4579  "Audio is disabled - trying to restart it");
4580  reselectAudioTrack = true;
4581  }
4583 
4584  // detect switches between stereo and dual languages
4585  bool wasDual = audSubIdx != -1;
4586  bool isDual = ctx->avcodec_dual_language != 0;
4587  if ((wasDual && !isDual) || (!wasDual && isDual))
4588  {
4590  reselectAudioTrack = true;
4591  }
4592 
4593  // detect channels on streams that need
4594  // to be decoded before we can know this
4595  bool already_decoded = false;
4596  if (!ctx->ch_layout.nb_channels)
4597  {
4598  m_avCodecLock.lock();
4599  if (DoPassThrough(curstream->codecpar, false) || !DecoderWillDownmix(ctx))
4600  {
4601  // for passthru or codecs for which the decoder won't downmix
4602  // let the decoder set the number of channels. For other codecs
4603  // we downmix if necessary in audiooutputbase
4604  ;
4605  }
4606  else // No passthru, the decoder will downmix
4607  {
4608  AVChannelLayout channel_layout;
4609  av_channel_layout_default(&channel_layout, m_audio->GetMaxChannels());
4610  av_opt_set_chlayout(ctx->priv_data, "downmix", &channel_layout, 0);
4611 
4612  if (ctx->codec_id == AV_CODEC_ID_AC3)
4613  ctx->ch_layout.nb_channels = m_audio->GetMaxChannels();
4614  }
4615 
4616  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, tmp_pkt);
4617  decoded_size = data_size;
4618  already_decoded = true;
4619  reselectAudioTrack |= ctx->ch_layout.nb_channels;
4620  m_avCodecLock.unlock();
4621  }
4622 
4623  if (reselectAudioTrack)
4624  {
4625  QMutexLocker locker(&m_trackLock);
4627  m_selectedTrack[kTrackTypeAudio].m_av_stream_index = -1;
4629  audIdx = m_selectedTrack[kTrackTypeAudio].m_av_stream_index;
4630  audSubIdx = m_selectedTrack[kTrackTypeAudio].m_av_substream_index;
4631  }
4632 
4633  if (!(decodetype & kDecodeAudio) || (pkt->stream_index != audIdx)
4634  || !m_audio->HasAudioOut())
4635  break;
4636 
4637  if (firstloop && pkt->pts != AV_NOPTS_VALUE)
4638  m_lastAPts = millisecondsFromFloat(av_q2d(curstream->time_base) * pkt->pts * 1000);
4639 
4640  if (!m_useFrameTiming)
4641  {
4642  // This code under certain conditions causes jump backwards to lose
4643  // audio.
4644  if (m_skipAudio && m_selectedTrack[kTrackTypeVideo].m_av_stream_index > -1)
4645  {
4646  if ((m_lastAPts < m_lastVPts - millisecondsFromFloat(10.0 / m_fps)) ||
4647  m_lastVPts == 0ms)
4648  break;
4649  m_skipAudio = false;
4650  }
4651 
4652  // skip any audio frames preceding first video frame
4653  if (m_firstVPtsInuse && (m_firstVPts != 0ms) && (m_lastAPts < m_firstVPts))
4654  {
4655  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
4656  QString("discarding early audio timecode %1 %2 %3")
4657  .arg(pkt->pts).arg(pkt->dts).arg(m_lastAPts.count()));
4658  break;
4659  }
4660  }
4661  m_firstVPtsInuse = false;
4662  m_avCodecLock.lock();
4663  data_size = 0;
4664 
4665  // Check if the number of channels or sampling rate have changed
4666  if (ctx->sample_rate != m_audioOut.m_sampleRate ||
4667  ctx->ch_layout.nb_channels != m_audioOut.m_channels ||
4669  ctx->bits_per_raw_sample) != m_audioOut.format)
4670  {
4671  LOG(VB_GENERAL, LOG_INFO, LOC + "Audio stream changed");
4672  if (ctx->ch_layout.nb_channels != m_audioOut.m_channels)
4673  {
4674  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Number of audio channels changed from %1 to %2")
4675  .arg(m_audioOut.m_channels).arg(ctx->ch_layout.nb_channels));
4676  }
4677  QMutexLocker locker(&m_trackLock);
4679  m_selectedTrack[kTrackTypeAudio].m_av_stream_index = -1;
4680  audIdx = -1;
4682  }
4683 
4685  {
4686  if (!already_decoded)
4687  {
4689  {
4690  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, tmp_pkt);
4691  decoded_size = data_size;
4692  }
4693  else
4694  {
4695  decoded_size = -1;
4696  }
4697  }
4698  memcpy(m_audioSamples, tmp_pkt->data, tmp_pkt->size);
4699  data_size = tmp_pkt->size;
4700  // We have processed all the data, there can't be any left
4701  tmp_pkt->size = 0;
4702  }
4703  else
4704  {
4705  if (!already_decoded)
4706  {
4707  if (DecoderWillDownmix(ctx))
4708  {
4709  AVChannelLayout channel_layout;
4710  av_channel_layout_default(&channel_layout, m_audio->GetMaxChannels());
4711  av_opt_set_chlayout(ctx->priv_data, "downmix", &channel_layout, 0);
4712  }
4713 
4714  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, tmp_pkt);
4715  decoded_size = data_size;
4716  }
4717  }
4718  m_avCodecLock.unlock();
4719 
4720  if (ret < 0)
4721  {
4722  LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown audio decoding error");
4723  av_packet_free(&tmp_pkt);
4724  return false;
4725  }
4726 
4727  if (data_size <= 0)
4728  {
4729  tmp_pkt->data += ret;
4730  tmp_pkt->size -= ret;
4731  continue;
4732  }
4733 
4734  std::chrono::milliseconds temppts = m_lastAPts;
4735 
4736  if (audSubIdx != -1 && !m_audioOut.m_doPassthru)
4737  extract_mono_channel(audSubIdx, &m_audioOut,
4738  (char *)m_audioSamples, data_size);
4739 
4740  int samplesize = AudioOutputSettings::SampleSize(m_audio->GetFormat());
4741  int frames = (ctx->ch_layout.nb_channels <= 0 || decoded_size < 0 || !samplesize) ? -1 :
4742  decoded_size / (ctx->ch_layout.nb_channels * samplesize);
4743  m_audio->AddAudioData((char *)m_audioSamples, data_size, temppts, frames);
4745  {
4747  }
4748  else
4749  {
4751  ((double)(frames * 1000) / ctx->sample_rate);
4752  }
4753 
4754  LOG(VB_TIMESTAMP, LOG_INFO, LOC + QString("audio timecode %1 %2 %3 %4")
4755  .arg(pkt->pts).arg(pkt->dts).arg(temppts.count()).arg(m_lastAPts.count()));
4756 
4759 
4760  tmp_pkt->data += ret;
4761  tmp_pkt->size -= ret;
4762  firstloop = false;
4763  }
4764 
4765  av_packet_free(&tmp_pkt);
4766  return true;
4767 }
4768 
4769 // documented in decoderbase.h
4770 bool AvFormatDecoder::GetFrame(DecodeType decodetype, bool &Retry)
4771 {
4772  AVPacket *pkt = nullptr;
4773  bool have_err = false;
4774 
4775  const DecodeType origDecodetype = decodetype;
4776 
4777  m_gotVideoFrame = false;
4778 
4779  m_frameDecoded = 0;
4780  m_decodedVideoFrame = nullptr;
4781 
4782  m_allowedQuit = false;
4783  bool storevideoframes = false;
4784 
4785  AutoSelectTracks();
4786 
4787  m_skipAudio = (m_lastVPts == 0ms);
4788 
4789  if( !m_processFrames )
4790  {
4791  return false;
4792  }
4793 
4795  m_needDummyVideoFrames = false;
4796 
4797  if (!m_hasVideo && (decodetype & kDecodeVideo))
4798  {
4799  // NB This could be an issue if the video stream is not
4800  // detected initially as the video buffers will be filled.
4801  m_needDummyVideoFrames = true;
4802  decodetype = (DecodeType)((int)decodetype & ~kDecodeVideo);
4803  m_skipAudio = false;
4804  }
4805 
4807 
4808  while (!m_allowedQuit)
4809  {
4810  if (decodetype & kDecodeAudio)
4811  {
4812  if (((m_currentTrack[kTrackTypeAudio] < 0) ||
4813  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index < 0)))
4814  {
4815  // disable audio request if there are no audio streams anymore
4816  // and we have video, otherwise allow decoding to stop
4817  if (m_hasVideo)
4818  decodetype = (DecodeType)((int)decodetype & ~kDecodeAudio);
4819  else
4820  m_allowedQuit = true;
4821  }
4822  }
4823  else if ((origDecodetype & kDecodeAudio) &&
4824  (m_currentTrack[kTrackTypeAudio] >= 0) &&
4825  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index >= 0))
4826  {
4827  // Turn on audio decoding again if it was on originally
4828  // and an audio stream has now appeared. This can happen
4829  // in still DVD menus with audio
4830  decodetype = (DecodeType)((int)decodetype | kDecodeAudio);
4831  }
4832 
4834 
4835  if (m_gotVideoFrame)
4836  {
4837  if (decodetype == kDecodeNothing)
4838  {
4839  // no need to buffer audio or video if we
4840  // only care about building a keyframe map.
4841  // NB but allow for data only (MHEG) streams
4842  m_allowedQuit = true;
4843  }
4844  else if ((decodetype & kDecodeAV) == kDecodeAV &&
4845  (m_storedPackets.count() < kMaxVideoQueueSize) &&
4846  // buffer audio to prevent audio buffer
4847  // underruns in case you are setting negative values
4848  // in Adjust Audio Sync.
4851  {
4852  storevideoframes = true;
4853  }
4854  else if (decodetype & kDecodeVideo)
4855  {
4856  if (m_storedPackets.count() >= kMaxVideoQueueSize)
4857  {
4858  LOG(VB_GENERAL, LOG_WARNING, LOC +
4859  QString("Audio %1 ms behind video but already %2 "
4860  "video frames queued. AV-Sync might be broken.")
4861  .arg((m_lastVPts-m_lastAPts).count()).arg(m_storedPackets.count()));
4862  }
4863  m_allowedQuit = true;
4864  continue;
4865  }
4866  }
4867 
4868  if (!storevideoframes && m_storedPackets.count() > 0)
4869  {
4870  if (pkt)
4871  av_packet_free(&pkt);
4872  pkt = m_storedPackets.takeFirst();
4873  }
4874  else
4875  {
4876  if (!pkt)
4877  pkt = av_packet_alloc();
4878 
4879  int retval = 0;
4880  if (!m_ic || ((retval = ReadPacket(m_ic, pkt, storevideoframes)) < 0))
4881  {
4882  if (retval == -EAGAIN)
4883  continue;
4884 
4885  SetEof(true);
4886  av_packet_free(&pkt);
4887  std::string errbuf(256,'\0');
4888  QString errmsg;
4889  if (av_strerror_stdstring(retval, errbuf) == 0)
4890  errmsg = QString::fromStdString(errbuf);
4891  else
4892  errmsg = "UNKNOWN";
4893 
4894  LOG(VB_GENERAL, LOG_ERR, QString("decoding error %1 (%2)")
4895  .arg(errmsg).arg(retval));
4896  return false;
4897  }
4898 
4899  if (m_waitingForChange && pkt->pos >= m_readAdjust)
4900  FileChanged();
4901 
4902  if (pkt->pos > m_readAdjust)
4903  pkt->pos -= m_readAdjust;
4904  }
4905 
4906  if (!m_ic)
4907  {
4908  LOG(VB_GENERAL, LOG_ERR, LOC + "No context");
4909  av_packet_unref(pkt);
4910  continue;
4911  }
4912 
4913  if (pkt->stream_index >= (int)m_ic->nb_streams)
4914  {
4915  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream");
4916  av_packet_unref(pkt);
4917  continue;
4918  }
4919 
4920  AVStream *curstream = m_ic->streams[pkt->stream_index];
4921 
4922  if (!curstream)
4923  {
4924  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream (NULL)");
4925  av_packet_unref(pkt);
4926  continue;
4927  }
4928 
4929  enum AVMediaType codec_type = curstream->codecpar->codec_type;
4930 
4931  if (storevideoframes && codec_type == AVMEDIA_TYPE_VIDEO)
4932  {
4933  // av_dup_packet(pkt);
4934  m_storedPackets.append(pkt);
4935  pkt = nullptr;
4936  continue;
4937  }
4938 
4939  if (codec_type == AVMEDIA_TYPE_VIDEO &&
4940  pkt->stream_index == m_selectedTrack[kTrackTypeVideo].m_av_stream_index)
4941  {
4942  if (!PreProcessVideoPacket(curstream, pkt))
4943  continue;
4944 
4945  // If the resolution changed in XXXPreProcessPkt, we may
4946  // have a fatal error, so check for this before continuing.
4947  if (m_parent->IsErrored())
4948  {
4949  av_packet_free(&pkt);
4950  return false;
4951  }
4952  }
4953 
4954  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
4955  curstream->codecpar->codec_id == AV_CODEC_ID_TEXT)
4956  {
4957  ProcessRawTextPacket(pkt);
4958  av_packet_unref(pkt);
4959  continue;
4960  }
4961 
4962  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
4963  curstream->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT)
4964  {
4965  ProcessDVBDataPacket(curstream, pkt);
4966  av_packet_unref(pkt);
4967  continue;
4968  }
4969 
4970  if (codec_type == AVMEDIA_TYPE_DATA)
4971  {
4972  ProcessDataPacket(curstream, pkt, decodetype);
4973  av_packet_unref(pkt);
4974  continue;
4975  }
4976 
4977  AVCodecContext *ctx = m_codecMap.GetCodecContext(curstream);
4978  if (!ctx)
4979  {
4980  if (codec_type != AVMEDIA_TYPE_VIDEO)
4981  {
4982  LOG(VB_PLAYBACK, LOG_ERR, LOC +
4983  QString("No codec for stream index %1, type(%2) id(%3:%4)")
4984  .arg(pkt->stream_index)
4985  .arg(AVMediaTypeToString(codec_type),
4986  avcodec_get_name(curstream->codecpar->codec_id))
4987  .arg(curstream->codecpar->codec_id));
4988  // Process Stream Change in case we have no audio
4989  if (codec_type == AVMEDIA_TYPE_AUDIO && !m_audio->HasAudioIn())
4990  m_streamsChanged = true;
4991  }
4992  av_packet_unref(pkt);
4993  continue;
4994  }
4995 
4996  have_err = false;
4997 
4998  switch (codec_type)
4999  {
5000  case AVMEDIA_TYPE_AUDIO:
5001  {
5002  if (!ProcessAudioPacket(curstream, pkt, decodetype))
5003  have_err = true;
5004  else
5006  break;
5007  }
5008 
5009  case AVMEDIA_TYPE_VIDEO:
5010  {
5011  if (pkt->stream_index != m_selectedTrack[kTrackTypeVideo].m_av_stream_index)
5012  {
5013  break;
5014  }
5015 
5016  if (pkt->pts != AV_NOPTS_VALUE)
5017  {
5019  (av_q2d(curstream->time_base)*pkt->pts*1000000);
5020  }
5021 
5022  if (!(decodetype & kDecodeVideo))
5023  {
5024  m_framesPlayed++;
5025  m_gotVideoFrame = true;
5026  break;
5027  }
5028 
5029  if (!ProcessVideoPacket(curstream, pkt, Retry))
5030  have_err = true;
5031  break;
5032  }
5033 
5034  case AVMEDIA_TYPE_SUBTITLE:
5035  {
5036  if (!ProcessSubtitlePacket(curstream, pkt))
5037  have_err = true;
5038  break;
5039  }
5040 
5041  default:
5042  {
5043  LOG(VB_GENERAL, LOG_ERR, LOC +
5044  QString("Decoding - id(%1) type(%2)")
5045  .arg(avcodec_get_name(ctx->codec_id),
5046  AVMediaTypeToString(ctx->codec_type)));
5047  have_err = true;
5048  break;
5049  }
5050  }
5051 
5052  if (!have_err && !Retry)
5053  m_frameDecoded = 1;
5054  av_packet_unref(pkt);
5055  if (Retry)
5056  break;
5057  }
5058 
5059  av_packet_free(&pkt);
5060  return true;
5061 }
5062 
5064 {
5065  if (m_streamsChanged)
5066  {
5067  SeekReset(0, 0, true, true);
5068  ScanStreams(false);
5069  m_streamsChanged = false;
5070  }
5071 }
5072 
5073 int AvFormatDecoder::ReadPacket(AVFormatContext *ctx, AVPacket *pkt, bool &/*storePacket*/)
5074 {
5075  m_avCodecLock.lock();
5076  int result = av_read_frame(ctx, pkt);
5077  m_avCodecLock.unlock();
5078  return result;
5079 }
5080 
5081 bool AvFormatDecoder::HasVideo(const AVFormatContext *ic)
5082 {
5083  if (ic && ic->pmt_section)
5084  {
5085  auto pmt_buffer = MythAVBufferRef(m_ic->pmt_section);
5086  if (!pmt_buffer.has_buffer())
5087  {
5088  return GetTrackCount(kTrackTypeVideo) != 0U;;
5089  }
5090  const ProgramMapTable pmt(PSIPTable(pmt_buffer.data()));
5091 
5092  for (uint i = 0; i < pmt.StreamCount(); i++)
5093  {
5094  // MythTV remaps OpenCable Video to normal video during recording
5095  // so "dvb" is the safest choice for system info type, since this
5096  // will ignore other uses of the same stream id in DVB countries.
5097  if (pmt.IsVideo(i, "dvb"))
5098  return true;
5099 
5100  // MHEG may explicitly select a private stream as video
5101  if ((i == (uint)m_selectedTrack[kTrackTypeVideo].m_av_stream_index) &&
5102  (pmt.StreamType(i) == StreamID::PrivData))
5103  {
5104  return true;
5105  }
5106  }
5107  }
5108 
5109  return GetTrackCount(kTrackTypeVideo) != 0U;
5110 }
5111 
5113 {
5115  {
5117  if (!frame)
5118  return false;
5119 
5120  frame->ClearMetadata();
5121  frame->ClearBufferToBlank();
5122 
5123  frame->m_dummy = true;
5124  frame->m_frameNumber = m_framesPlayed;
5125  frame->m_frameCounter = m_frameCounter++;
5126 
5128  m_parent->DeLimboFrame(frame);
5129 
5130  m_decodedVideoFrame = frame;
5131  m_framesPlayed++;
5132  m_gotVideoFrame = true;
5133  }
5134  return true;
5135 }
5136 
5138 {
5140 }
5141 
5143 {
5144  int stream = m_selectedTrack[kTrackTypeVideo].m_av_stream_index;
5145  if (stream < 0 || !m_ic)
5146  return {};
5147  return avcodec_get_name(m_ic->streams[stream]->codecpar->codec_id);
5148 }
5149 
5151 {
5152  if (m_selectedTrack[kTrackTypeAudio].m_av_stream_index < 0)
5153  {
5154  m_disablePassthru = disable;
5155  return;
5156  }
5157 
5158  if (disable != m_disablePassthru)
5159  {
5160  m_disablePassthru = disable;
5161  QString msg = (disable) ? "Disabling" : "Allowing";
5162  LOG(VB_AUDIO, LOG_INFO, LOC + msg + " pass through");
5163 
5164  // Force pass through state to be reanalyzed
5166  }
5167 }
5168 
5170 {
5171  QMutexLocker locker(&m_trackLock);
5172  SetupAudioStream();
5173 }
5174 
5175 inline bool AvFormatDecoder::DecoderWillDownmix(const AVCodecContext *ctx)
5176 {
5177  // Until ffmpeg properly implements dialnorm
5178  // use Myth internal downmixer if machine has SSE2
5180  return false;
5181  // use ffmpeg only for dolby codecs if we have to
5182  //return av_opt_find(ctx->priv_data, "downmix", nullptr, 0, 0);
5183  // av_opt_find was causing segmentation faults, so explicitly list the
5184  // compatible decoders
5185  switch (ctx->codec_id)
5186  {
5187  case AV_CODEC_ID_AC3:
5188  case AV_CODEC_ID_TRUEHD:
5189  case AV_CODEC_ID_EAC3:
5190  case AV_CODEC_ID_MLP:
5191  case AV_CODEC_ID_DTS:
5192  return true;
5193  default:
5194  return false;
5195  }
5196 }
5197 
5198 bool AvFormatDecoder::DoPassThrough(const AVCodecParameters *par, bool withProfile)
5199 {
5200  bool passthru = false;
5201 
5202  // if withProfile == false, we will accept any DTS stream regardless
5203  // of its profile. We do so, so we can bitstream DTS-HD as DTS core
5204  if (!withProfile && par->codec_id == AV_CODEC_ID_DTS && !m_audio->CanDTSHD())
5205  {
5206  passthru = m_audio->CanPassthrough(par->sample_rate, par->ch_layout.nb_channels,
5207  par->codec_id, FF_PROFILE_DTS);
5208  }
5209  else
5210  {
5211  passthru = m_audio->CanPassthrough(par->sample_rate, par->ch_layout.nb_channels,
5212  par->codec_id, par->profile);
5213  }
5214 
5215  passthru &= !m_disablePassthru;
5216 
5217  return passthru;
5218 }
5219 
5226 {
5227  AudioInfo info; // no_audio
5228  AVStream *curstream = nullptr;
5229  AVCodecContext *ctx = nullptr;
5230  AudioInfo old_in = m_audioIn;
5231  int requested_channels = 0;
5232 
5233  if ((m_currentTrack[kTrackTypeAudio] >= 0) && m_ic &&
5234  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index <=
5235  (int) m_ic->nb_streams) &&
5236  (curstream = m_ic->streams[m_selectedTrack[kTrackTypeAudio]
5237  .m_av_stream_index]) &&
5238  (ctx = m_codecMap.GetCodecContext(curstream)))
5239  {
5240  AudioFormat fmt =
5242  ctx->bits_per_raw_sample);
5243 
5244  if (av_sample_fmt_is_planar(ctx->sample_fmt))
5245  {
5246  LOG(VB_AUDIO, LOG_INFO, LOC + QString("Audio data is planar"));
5247  }
5248 
5249  if (fmt == FORMAT_NONE)
5250  {
5251  int bps = av_get_bytes_per_sample(ctx->sample_fmt) << 3;
5252  if (ctx->sample_fmt == AV_SAMPLE_FMT_S32 &&
5253  ctx->bits_per_raw_sample)
5254  bps = ctx->bits_per_raw_sample;
5255  LOG(VB_GENERAL, LOG_ERR, LOC +
5256  QString("Unsupported sample format with %1 bits").arg(bps));
5257  return false;
5258  }
5259 
5260  bool using_passthru = DoPassThrough(curstream->codecpar, false);
5261 
5262  requested_channels = ctx->ch_layout.nb_channels;
5263 
5264  if (!using_passthru &&
5265  ctx->ch_layout.nb_channels > (int)m_audio->GetMaxChannels() &&
5266  DecoderWillDownmix(ctx))
5267  {
5268  requested_channels = m_audio->GetMaxChannels();
5269 
5270  AVChannelLayout channel_layout;
5271  av_channel_layout_default(&channel_layout, requested_channels);
5272  av_opt_set_chlayout(ctx->priv_data, "downmix", &channel_layout, 0);
5273  }
5274 
5275  info = AudioInfo(ctx->codec_id, fmt, ctx->sample_rate,
5276  requested_channels, using_passthru, ctx->ch_layout.nb_channels,
5277  ctx->codec_id == AV_CODEC_ID_DTS ? ctx->profile : 0);
5278  }
5279 
5280  if (!ctx)
5281  {
5282  if (!m_tracks[kTrackTypeAudio].empty())
5283  LOG(VB_PLAYBACK, LOG_INFO, LOC + "No codec context. Returning false");
5284  return false;
5285  }
5286 
5287  if (info == m_audioIn)
5288  return false;
5289 
5290  LOG(VB_AUDIO, LOG_INFO, LOC + "Initializing audio parms from " +
5291  QString("audio track #%1").arg(m_currentTrack[kTrackTypeAudio]+1));
5292 
5293  m_audioOut = m_audioIn = info;
5294 
5295  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio format changed " +
5296  QString("\n\t\t\tfrom %1 to %2")
5297  .arg(old_in.toString(), m_audioOut.toString()));
5298 
5299  m_audio->SetAudioParams(m_audioOut.format, ctx->ch_layout.nb_channels,
5300  requested_channels,
5303  m_audio->ReinitAudio();
5304  AudioOutput *audioOutput = m_audio->GetAudioOutput();
5305  if (audioOutput)
5306  audioOutput->SetSourceBitrate(ctx->bit_rate);
5307 
5308  if (LCD *lcd = LCD::Get())
5309  {
5310  LCDAudioFormatSet audio_format = AUDIO_MP3;
5311 
5312  switch (ctx->codec_id)
5313  {
5314  case AV_CODEC_ID_MP2:
5315  audio_format = AUDIO_MPEG2;
5316  break;
5317  case AV_CODEC_ID_MP3:
5318  audio_format = AUDIO_MP3;
5319  break;
5320  case AV_CODEC_ID_AC3:
5321  audio_format = AUDIO_AC3;
5322  break;
5323  case AV_CODEC_ID_DTS:
5324  audio_format = AUDIO_DTS;
5325  break;
5326  case AV_CODEC_ID_VORBIS:
5327  audio_format = AUDIO_OGG;
5328  break;
5329  case AV_CODEC_ID_WMAV1:
5330  audio_format = AUDIO_WMA;
5331  break;
5332  case AV_CODEC_ID_WMAV2:
5333  audio_format = AUDIO_WMA2;
5334  break;
5335  default:
5336  audio_format = AUDIO_WAV;
5337  break;
5338  }
5339 
5340  lcd->setAudioFormatLEDs(audio_format, true);
5341 
5343  lcd->setVariousLEDs(VARIOUS_SPDIF, true);
5344  else
5345  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
5346 
5347  switch (m_audioIn.m_channels)
5348  {
5349  case 0:
5350  /* nb: aac and mp3 seem to be coming up 0 here, may point to an
5351  * avformatdecoder audio channel handling bug, per janneg */
5352  case 1:
5353  case 2:
5354  /* all audio codecs have at *least* one channel, but
5355  * LR is the fewest LED we can light up */
5356  lcd->setSpeakerLEDs(SPEAKER_LR, true);
5357  break;
5358  case 3:
5359  case 4:
5360  case 5:
5361  case 6:
5362  lcd->setSpeakerLEDs(SPEAKER_51, true);
5363  break;
5364  default:
5365  lcd->setSpeakerLEDs(SPEAKER_71, true);
5366  break;
5367  }
5368 
5369  }
5370  return true;
5371 }
5372 
5374 {
5375  int64_t start_time = INT64_MAX;
5376  int64_t end_time = INT64_MIN;
5377  AVStream *st = nullptr;
5378 
5379  for (uint i = 0; i < ic->nb_streams; i++)
5380  {
5381  AVStream *st1 = ic->streams[i];
5382  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
5383  {
5384  st = st1;
5385  break;
5386  }
5387  }
5388  if (!st)
5389  return;
5390 
5391  int64_t duration = INT64_MIN;
5392  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
5393  int64_t start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
5394  if (start_time1 < start_time)
5395  start_time = start_time1;
5396  if (st->duration != AV_NOPTS_VALUE) {
5397  int64_t end_time1 = start_time1 +
5398  av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5399  if (end_time1 > end_time)
5400  end_time = end_time1;
5401  }
5402  }
5403  if (st->duration != AV_NOPTS_VALUE) {
5404  int64_t duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5405  if (duration1 > duration)
5406  duration = duration1;
5407  }
5408  if (start_time != INT64_MAX) {
5409  ic->start_time = start_time;
5410  if (end_time != INT64_MIN) {
5411  if (end_time - start_time > duration)
5412  duration = end_time - start_time;
5413  }
5414  }
5415  if (duration != INT64_MIN) {
5416  int64_t filesize = 0;
5417  ic->duration = duration;
5418  if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
5419  /* compute the bitrate */
5420  ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
5421  (double)ic->duration;
5422  }
5423  }
5424 }
5425 
5426 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythHDRVideoMetadata::Populate
static void Populate(class MythVideoFrame *Frame, struct AVFrame *AvFrame)
Create, update or destroy HDR metadata for the given MythVideoFrame.
Definition: mythhdrvideometadata.cpp:47
ProgramMapTable::ProgramInfo
const unsigned char * ProgramInfo(void) const
Definition: mpegtables.h:735
AvFormatDecoder::get_avf_buffer
friend int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
Definition: avformatdecoder.cpp:2694
AvFormatDecoder::GetTrackDesc
QString GetTrackDesc(uint Type, uint TrackNo) override
Definition: avformatdecoder.cpp:4053
get_format_dxva2
static enum AVPixelFormat get_format_dxva2(struct AVCodecContext *, const enum AVPixelFormat *)
Definition: avformatdecoder.cpp:1369
TeletextDescriptor::TeletextMagazineNum
uint TeletextMagazineNum(uint i) const
Definition: dvbdescriptors.h:2371
bytereader.h
AVRationalInit
AVRational AVRationalInit(int num, int den=1)
Definition: decoderbase.h:113
FORMAT_NONE
@ FORMAT_NONE
Definition: audiooutputsettings.h:25
AudioInfo::m_channels
int m_channels
Definition: avformatdecoder.h:59
VBI_IVTV
@ VBI_IVTV
Definition: vbilut.h:23
AVCParser
Definition: AVCParser.h:30
kDecodeAV
@ kDecodeAV
Definition: decoderbase.h:52
AudioOutput::kMaxSizeBuffer
static const int kMaxSizeBuffer
kMaxSizeBuffer is the maximum size of a buffer to be used with DecodeAudio
Definition: audiooutput.h:197
DecoderBase::DoFastForward
virtual bool DoFastForward(long long desiredFrame, bool discardFrames=true)
Skips ahead or rewinds to desiredFrame.
Definition: decoderbase.cpp:710
kAudioTypeHearingImpaired
@ kAudioTypeHearingImpaired
Definition: decoderbase.h:60
AvFormatDecoder::m_lastPtsForFaultDetection
int64_t m_lastPtsForFaultDetection
Definition: avformatdecoder.h:306
secondsFromFloat
std::enable_if_t< std::is_floating_point_v< T >, std::chrono::seconds > secondsFromFloat(T value)
Helper function for convert a floating point number to a duration.
Definition: mythchrono.h:80
MythTimer::elapsed
std::chrono::milliseconds elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:91
iso639_key_to_canonical_key
int iso639_key_to_canonical_key(int iso639_2)
Definition: iso639.cpp:118
SLICE_MIN
static constexpr uint32_t SLICE_MIN
Definition: avformatdecoder.cpp:3062
CC608Decoder::SetIgnoreTimecode
void SetIgnoreTimecode(bool val)
Definition: cc608decoder.h:67
DecoderBase::SetTrack
virtual int SetTrack(uint Type, int TrackNo)
Definition: decoderbase.cpp:963
MythDate::toString
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:84
DecoderBase::m_readAdjust
long long m_readAdjust
Definition: decoderbase.h:348
AudioOutputSettings::AVSampleFormatToFormat
static AudioFormat AVSampleFormatToFormat(AVSampleFormat format, int bits=0)
Return AVSampleFormat closest equivalent to AudioFormat.
Definition: audiooutputsettings.cpp:198
AvFormatDecoder::GetRawEncodingType
QString GetRawEncodingType(void) override
Definition: avformatdecoder.cpp:5142
VIDEO_WMV
@ VIDEO_WMV
Definition: lcddevice.h:127
kDecoderProbeBufferSize
const int kDecoderProbeBufferSize
Definition: decoderbase.h:22
MythMediaBuffer::BestBufferSize
virtual int BestBufferSize(void)
Definition: mythmediabuffer.h:128
MythVideoFrame::m_colorspace
int m_colorspace
Definition: mythframe.h:148
AudioInfo::m_codecProfile
int m_codecProfile
Definition: avformatdecoder.h:60
MythPlayer::SetFileLength
void SetFileLength(std::chrono::seconds total, int frames)
Definition: mythplayer.cpp:391
MythMediaBuffer::SetBufferSizeFactors
void SetBufferSizeFactors(bool EstBitrate, bool Matroska)
Tells RingBuffer that the raw bitrate may be inaccurate and the underlying container is matroska,...
Definition: mythmediabuffer.cpp:329
SLICE_MAX
static constexpr uint32_t SLICE_MAX
Definition: avformatdecoder.cpp:3063
AudioTrackType
AudioTrackType
Definition: decoderbase.h:55
InteractiveTV::ImageHasChanged
bool ImageHasChanged(void)
Definition: interactivetv.cpp:41
AudioInfo::m_codecId
AVCodecID m_codecId
Definition: avformatdecoder.h:55
MythVideoFrame::m_colortransfer
int m_colortransfer
Definition: mythframe.h:151
DecoderBase::m_selectedTrack
std::array< StreamInfo, kTrackTypeCount > m_selectedTrack
Definition: decoderbase.h:362
MythVideoFrame::m_interlaced
int m_interlaced
Definition: mythframe.h:134
AvFormatDecoder::av_update_stream_timings_video
static void av_update_stream_timings_video(AVFormatContext *ic)
Definition: avformatdecoder.cpp:5373
AvFormatDecoder::GetChapterTimes
void GetChapterTimes(QList< std::chrono::seconds > &times) override
Definition: avformatdecoder.cpp:533
error
static void error(const char *str,...)
Definition: vbi.cpp:36
DecoderBase::m_ringBuffer
MythMediaBuffer * m_ringBuffer
Definition: decoderbase.h:289
codec_is_v4l2
static bool codec_is_v4l2(MythCodecID id)
Definition: mythcodecid.h:355
kScan_Detect
@ kScan_Detect
Definition: videoouttypes.h:97
MythDVDBuffer::GetAspectOverride
float GetAspectOverride(void) const
Definition: mythdvdbuffer.cpp:1152
AV_RB32
#define AV_RB32(x)
Definition: bytereader.cpp:27
StreamInfo::m_language_index
uint m_language_index
Definition: decoderbase.h:98
DecoderBase::PosMapEntry
Definition: decoderbase.h:278
AvFormatDecoder::SetTrack
int SetTrack(uint Type, int TrackNo) override
Definition: avformatdecoder.cpp:4041
AvFormatDecoder::m_doRewind
bool m_doRewind
Definition: avformatdecoder.h:277
MythVideoFrame::m_newGOP
bool m_newGOP
Definition: mythframe.h:137
ProgramMapTable::StreamCount
uint StreamCount(void) const
Definition: mpegtables.h:750
AvFormatDecoder::GetCurrentChapter
int GetCurrentChapter(long long framesPlayed) override
Definition: avformatdecoder.cpp:550
DecoderBase::m_currentWidth
int m_currentWidth
Definition: decoderbase.h:295
AudioInfo::m_sampleSize
int m_sampleSize
Definition: avformatdecoder.h:57
AvFormatDecoder::m_skipAudio
bool m_skipAudio
Definition: avformatdecoder.h:293
MythCodecMap::GetCodecContext
AVCodecContext * GetCodecContext(const AVStream *Stream, const AVCodec *Codec=nullptr, bool NullCodec=false)
Definition: mythavutil.cpp:287
V4L2_MPEG_VBI_IVTV_VPS
@ V4L2_MPEG_VBI_IVTV_VPS
Video Programming System (PAL) (line 16)
Definition: avformatdecoder.cpp:54
AudioPlayer::ReinitAudio
QString ReinitAudio(void)
Definition: audioplayer.cpp:105
cc708_seen_flags
std::array< bool, 64 > cc708_seen_flags
Definition: cc708decoder.h:15
AvFormatDecoder::AutoSelectAudioTrack
int AutoSelectAudioTrack(void)
Selects the best audio track.
Definition: avformatdecoder.cpp:4315
DecoderBase::m_fps
double m_fps
Definition: decoderbase.h:291
MythTimer
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
audiooutpututil.h
AvFormatDecoder::m_gotVideoFrame
bool m_gotVideoFrame
Definition: avformatdecoder.h:290
CaptionServiceDescriptor::EasyReader
bool EasyReader(int i) const
Definition: atscdescriptors.h:113
AvFormatDecoder::UpdateFramesPlayed
void UpdateFramesPlayed(void) override
Definition: avformatdecoder.cpp:2513
AvFormatDecoder::m_lastScteField
uint m_lastScteField
Definition: avformatdecoder.h:326
AvFormatDecoder::m_storedPackets
QList< AVPacket * > m_storedPackets
Definition: avformatdecoder.h:285
AvFormatDecoder::SetEof
void SetEof(bool eof) override
Definition: avformatdecoder.cpp:802
kCodec_NONE
@ kCodec_NONE
Definition: mythcodecid.h:14
TeletextDecoder::Decode
void Decode(const unsigned char *buf, int vbimode)
Decodes teletext data.
Definition: teletextdecoder.cpp:41
MythVideoFrame::ClearBufferToBlank
void ClearBufferToBlank()
Definition: mythframe.cpp:205
MythVideoFrame::m_chromalocation
int m_chromalocation
Definition: mythframe.h:152
InteractiveTV::SetNetBootInfo
void SetNetBootInfo(const unsigned char *data, uint length)
Definition: interactivetv.cpp:77
codec_is_nvdec
static bool codec_is_nvdec(MythCodecID id)
Definition: mythcodecid.h:341
DecoderBase::m_videoRotation
int m_videoRotation
Definition: decoderbase.h:349
AvFormatDecoder::GetAttachmentData
void GetAttachmentData(uint TrackNo, QByteArray &Filename, QByteArray &Data) override
Definition: avformatdecoder.cpp:4154
CC608Decoder::FormatCC
void FormatCC(std::chrono::milliseconds tc, int code1, int code2)
Definition: cc608decoder.cpp:50
DecoderBase::m_stereo3D
uint m_stereo3D
Definition: decoderbase.h:350
CaptionServiceDescriptor
Definition: atscdescriptors.h:74
AvFormatDecoder::m_itv
InteractiveTV * m_itv
MHEG/MHP decoder.
Definition: avformatdecoder.h:350
StreamInfo::m_av_stream_index
int m_av_stream_index
Definition: decoderbase.h:94
AudioInfo::toString
QString toString() const
Definition: avformatdecoder.h:72
AvFormatDecoder::m_maxKeyframeDist
int m_maxKeyframeDist
Definition: avformatdecoder.h:320
DecoderBase::AutoSelectTrack
virtual int AutoSelectTrack(uint Type)
Select best track.
Definition: decoderbase.cpp:1054
AvFormatDecoder::RemoveAudioStreams
void RemoveAudioStreams()
remove audio streams from the context used by dvd code during title transitions to remove stale audio...
Definition: avformatdecoder.cpp:2672
x2
static int x2
Definition: mythsocket.cpp:51
MythCodecContext::SetDecoderOptions
virtual void SetDecoderOptions(AVCodecContext *, const AVCodec *)
Definition: mythcodeccontext.h:159
MythPlayer::GetInteractiveTV
virtual InteractiveTV * GetInteractiveTV()
Definition: mythplayer.h:165
DecoderBase::m_atEof
EofState m_atEof
Definition: decoderbase.h:309
ProgramMapTable::StreamInfo
const unsigned char * StreamInfo(uint i) const
Definition: mpegtables.h:747
DecoderBase::m_bitrate
uint m_bitrate
Definition: decoderbase.h:294
DecoderBase::m_parent
MythPlayer * m_parent
Definition: decoderbase.h:286
AvFormatDecoder::NormalizeVideoTimecode
std::chrono::milliseconds NormalizeVideoTimecode(std::chrono::milliseconds timecode) override
Definition: avformatdecoder.cpp:470
MythVideoFrame::m_frameCounter
uint64_t m_frameCounter
Definition: mythframe.h:130
AvFormatDecoder::DecoderWillDownmix
bool DecoderWillDownmix(const AVCodecContext *ctx)
Definition: avformatdecoder.cpp:5175
mythtvexp.h
StreamInfo::m_stream_id
int m_stream_id
Definition: decoderbase.h:99
kTrackTypeTeletextMenu
@ kTrackTypeTeletextMenu
Definition: decoderbase.h:35
MythDVDBuffer::DecodeSubtitles
bool DecodeSubtitles(AVSubtitle *Subtitle, int *GotSubtitles, const uint8_t *SpuPkt, int BufSize, uint32_t StartTime)
generate dvd subtitle bitmap or dvd menu bitmap.
Definition: mythdvdbuffer.cpp:1458
kTrackTypeAttachment
@ kTrackTypeAttachment
Definition: decoderbase.h:37
cc708decoder.h
AvFormatDecoder::SetAudioByComponentTag
bool SetAudioByComponentTag(int Tag) override
Definition: avformatdecoder.cpp:4168
AVMediaTypeToString
static const char * AVMediaTypeToString(enum AVMediaType codec_type)
returns a human readable string for the AVMediaType enum.
Definition: avformatdecoder.cpp:356
CC608Decoder
Definition: cc608decoder.h:53
ProgramMapTable
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:692
MythAVFrame
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
Definition: mythaverror.h:52
TeletextDecoder
Definition: teletextdecoder.h:8
H2645Parser::FIELD_BOTTOM
@ FIELD_BOTTOM
Definition: H2645Parser.h:48
mythdvdbuffer.h
SPEAKER_71
@ SPEAKER_71
Definition: lcddevice.h:103
DecoderBase::m_livetv
bool m_livetv
Definition: decoderbase.h:336
SEQ_START
static constexpr uint32_t SEQ_START
Definition: avformatdecoder.cpp:3059
AvFormatDecoder::SetIdrOnlyKeyframes
void SetIdrOnlyKeyframes(bool value) override
Definition: avformatdecoder.h:156
DEINT_NONE
@ DEINT_NONE
Definition: mythframe.h:69
VERBOSE_LEVEL_CHECK
static bool VERBOSE_LEVEL_CHECK(uint64_t mask, LogLevel_t level)
Definition: mythlogging.h:29
MythCodecContext::CreateContext
static MythCodecContext * CreateContext(DecoderBase *Parent, MythCodecID Codec)
Definition: mythcodeccontext.cpp:73
AvFormatDecoder::m_faultyPts
int64_t m_faultyPts
Definition: avformatdecoder.h:304
microsecondsFromFloat
std::enable_if_t< std::is_floating_point_v< T >, std::chrono::microseconds > microsecondsFromFloat(T value)
Helper function for convert a floating point number to a duration.
Definition: mythchrono.h:102
DecoderBase::GetSeekSnap
uint64_t GetSeekSnap(void) const
Definition: decoderbase.h:138
mythdbcon.h
SPEAKER_LR
@ SPEAKER_LR
Definition: lcddevice.h:101
AvFormatDecoder::m_ccX08InTracks
std::array< bool, 68 > m_ccX08InTracks
Lookup table for whether a stream is represented in the UI entries 0-3 correspond to CEA-608 CC1 thro...
Definition: avformatdecoder.h:337
MythDate::formatTime
QString formatTime(std::chrono::milliseconds msecs, QString fmt)
Format a milliseconds time value.
Definition: mythdate.cpp:233
AudioOutputSettings::SampleSize
static int SampleSize(AudioFormat format)
Definition: audiooutputsettings.cpp:180
DecoderBase::ResetTracks
void ResetTracks(void)
Definition: decoderbase.cpp:1151
ProgramMapTable::IsVideo
bool IsVideo(uint i, const QString &sistandard) const
Returns true iff the stream at index i is a video stream.
Definition: mpegtables.cpp:514
AvFormatDecoder::m_audioSamples
uint8_t * m_audioSamples
Definition: avformatdecoder.h:353
DecoderBase::m_hasFullPositionMap
bool m_hasFullPositionMap
Definition: decoderbase.h:319
DescriptorID::caption_service
@ caption_service
Definition: mpegdescriptors.h:163
AudioPlayer::IsBufferAlmostFull
bool IsBufferAlmostFull(void)
Definition: audioplayer.cpp:494
mythhdrvideometadata.h
MythCodecContext::InitVideoCodec
virtual void InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering)
Definition: mythcodeccontext.cpp:302
MythDate::Format
Format
Definition: mythdate.h:15
SubtitleReader::AddAVSubtitle
bool AddAVSubtitle(AVSubtitle &subtitle, bool fix_position, bool allow_forced)
Definition: subtitlereader.cpp:35
MythVideoFrame::m_width
int m_width
Definition: mythframe.h:121
CC708Decoder::decode_cc_data
void decode_cc_data(uint cc_type, uint data1, uint data2)
Definition: cc708decoder.cpp:41
AvFormatDecoder::GetTeletextDecoderType
int GetTeletextDecoderType(void) const override
Definition: avformatdecoder.cpp:4132
MythMediaBuffer
Definition: mythmediabuffer.h:50
MythMediaBuffer::IgnoreWaitStates
virtual void IgnoreWaitStates(bool)
Definition: mythmediabuffer.h:130
MythPlayer::IsErrored
bool IsErrored(void) const
Definition: mythplayer.cpp:1954
MythMediaBuffer::IsDVD
bool IsDVD(void) const
Definition: mythmediabuffer.cpp:1831
AvFormatDecoder::IsValidStream
virtual bool IsValidStream(int)
Definition: avformatdecoder.h:250
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
VIDEO_XVID
@ VIDEO_XVID
Definition: lcddevice.h:126
DecoderBase::m_videoDisplayProfile
MythVideoProfile m_videoDisplayProfile
Definition: decoderbase.h:367
myth_av_log
static void myth_av_log(void *ptr, int level, const char *fmt, va_list vl)
Definition: avformatdecoder.cpp:272
AudioPlayer::HasAudioOut
bool HasAudioOut(void) const
Definition: audioplayer.h:56
MythMediaBuffer::GetReadBufAvail
int GetReadBufAvail(void) const
Returns number of bytes available for reading from buffer.
Definition: mythmediabuffer.cpp:456
TrackType
TrackType
Track types.
Definition: decoderbase.h:26
AvFormatDecoder::m_seenGop
bool m_seenGop
A flag to indicate that we've seen a GOP frame. Used in junction with seq_count.
Definition: avformatdecoder.h:281
DecoderBase::m_trackLock
QRecursiveMutex m_trackLock
Definition: decoderbase.h:356
kAudioTypeCleanEffects
@ kAudioTypeCleanEffects
Definition: decoderbase.h:59
MYTH_WIDTH_ALIGNMENT
static constexpr uint8_t MYTH_WIDTH_ALIGNMENT
Definition: mythframe.h:17
LOC
#define LOC
Definition: avformatdecoder.cpp:116
CC708Decoder
Definition: cc708decoder.h:29
DecoderBase::m_positionMapType
MarkTypes m_positionMapType
Definition: decoderbase.h:322
MythCodecContext::DecoderWillResetOnFlush
virtual bool DecoderWillResetOnFlush(void)
Definition: mythcodeccontext.h:160
mythframe.h
AUDIO_WMA2
@ AUDIO_WMA2
Definition: lcddevice.h:112
build_compdb.parser
parser
Definition: build_compdb.py:7
PlayerFlags
PlayerFlags
Definition: mythplayer.h:65
subtitlereader.h
MythPlayer
Definition: mythplayer.h:84
AvFormatDecoder::SeekReset
void SeekReset(long long newkey, uint skipFrames, bool doFlush, bool discardFrames) override
Definition: avformatdecoder.cpp:670
kDecodeLowRes
@ kDecodeLowRes
Definition: mythplayer.h:68
MythVideoFrame::m_offsets
FrameOffsets m_offsets
Definition: mythframe.h:143
DecoderBase::m_exitAfterDecoded
bool m_exitAfterDecoded
Definition: decoderbase.h:316
AvFormatDecoder::GetChapter
long long GetChapter(int chapter) override
Definition: avformatdecoder.cpp:574
DescriptorID::teletext
@ teletext
Definition: mpegdescriptors.h:95
codec_sw_copy
static bool codec_sw_copy(MythCodecID id)
Definition: mythcodecid.h:371
MythMediaBuffer::UpdateRawBitrate
void UpdateRawBitrate(uint RawBitrate)
Set the raw bit rate, to allow RingBuffer adjust effective bitrate.
Definition: mythmediabuffer.cpp:278
AvFormatDecoder::m_readContext
URLContext m_readContext
Definition: avformatdecoder.h:268
AvFormatDecoder::FindStreamInfo
int FindStreamInfo(void)
Definition: avformatdecoder.cpp:895
AvFormatDecoder::ScanATSCCaptionStreams
void ScanATSCCaptionStreams(int av_index)
Definition: avformatdecoder.cpp:1613
iso639_key_toName
QString iso639_key_toName(int iso639_2)
Converts a canonical key to language name in English.
Definition: iso639.cpp:109
sinfo_vec_t
std::vector< StreamInfo > sinfo_vec_t
Definition: decoderbase.h:111
CC608Seen
std::array< bool, 4 > CC608Seen
Definition: cc608decoder.h:23
DecoderBase::m_frameToDurMap
frm_pos_map_t m_frameToDurMap
Definition: decoderbase.h:330
AvFormatDecoder::m_ignoreScte
uint m_ignoreScte
Definition: avformatdecoder.h:324
InteractiveTV::GetInitialStreams
void GetInitialStreams(int &audioTag, int &videoTag)
Definition: interactivetv.cpp:72
AvFormatDecoder::GetSubHeader
QByteArray GetSubHeader(uint TrackNo) override
Definition: avformatdecoder.cpp:4142
MythVideoFrame::m_interlacedReverse
bool m_interlacedReverse
Definition: mythframe.h:136
AudioPlayer::AddAudioData
void AddAudioData(char *buffer, int len, std::chrono::milliseconds timecode, int frames)
Definition: audioplayer.cpp:450
LCDAudioFormatSet
LCDAudioFormatSet
Definition: lcddevice.h:107
AvFormatDecoder::m_playerFlags
PlayerFlags m_playerFlags
Definition: avformatdecoder.h:317
LCD::Get
static LCD * Get(void)
Definition: lcddevice.cpp:69
DecoderBase::m_currentTrack
std::array< int, kTrackTypeCount > m_currentTrack
Definition: decoderbase.h:359
MythMediaBuffer::StartFromBeginning
virtual bool StartFromBeginning(void)
Definition: mythmediabuffer.h:129
MythCodecContext::DecoderWillResetOnAspect
virtual bool DecoderWillResetOnAspect(void)
Definition: mythcodeccontext.h:161
extract_mono_channel
static void extract_mono_channel(uint channel, AudioInfo *audioInfo, char *buffer, int bufsize)
Definition: avformatdecoder.cpp:4530
AUDIO_MPEG2
@ AUDIO_MPEG2
Definition: lcddevice.h:115
iso639_is_key_undefined
static bool iso639_is_key_undefined(int code)
Returns true if the key is 0, 0xFFFFFF, or 'und'.
Definition: iso639.h:53
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
SPEAKER_51
@ SPEAKER_51
Definition: lcddevice.h:102
atscdescriptors.h
MythPlayer::SetErrored
void SetErrored(const QString &reason)
Definition: mythplayer.cpp:1930
AvFormatDecoder::m_streamTracks
QList< StreamInfo > m_streamTracks
StreamInfo for 608 and 708 Captions seen in the caption stream itself but not seen in the PMT.
Definition: avformatdecoder.h:344
MythPlayer::DiscardVideoFrames
void DiscardVideoFrames(bool KeyFrame, bool Flushed)
Places frames in the available frames queue.
Definition: mythplayer.cpp:652
AvFormatDecoder::CloseCodecs
void CloseCodecs()
Definition: avformatdecoder.cpp:436
AudioPlayer::GetFormat
AudioFormat GetFormat(void) const
Definition: audioplayer.h:83