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