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