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