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