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