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