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