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