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